Fixes: debbugs:13797
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2
3 Copyright (C) 1985-1988, 1993-1995, 1997-2013 Free Software Foundation,
4 Inc.
5
6 This file is part of GNU Emacs.
7
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20
21 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
22
23 Redisplay.
24
25 Emacs separates the task of updating the display from code
26 modifying global state, e.g. buffer text. This way functions
27 operating on buffers don't also have to be concerned with updating
28 the display.
29
30 Updating the display is triggered by the Lisp interpreter when it
31 decides it's time to do it. This is done either automatically for
32 you as part of the interpreter's command loop or as the result of
33 calling Lisp functions like `sit-for'. The C function `redisplay'
34 in xdisp.c is the only entry into the inner redisplay code.
35
36 The following diagram shows how redisplay code is invoked. As you
37 can see, Lisp calls redisplay and vice versa. Under window systems
38 like X, some portions of the redisplay code are also called
39 asynchronously during mouse movement or expose events. It is very
40 important that these code parts do NOT use the C library (malloc,
41 free) because many C libraries under Unix are not reentrant. They
42 may also NOT call functions of the Lisp interpreter which could
43 change the interpreter's state. If you don't follow these rules,
44 you will encounter bugs which are very hard to explain.
45
46 +--------------+ redisplay +----------------+
47 | Lisp machine |---------------->| Redisplay code |<--+
48 +--------------+ (xdisp.c) +----------------+ |
49 ^ | |
50 +----------------------------------+ |
51 Don't use this path when called |
52 asynchronously! |
53 |
54 expose_window (asynchronous) |
55 |
56 X expose events -----+
57
58 What does redisplay do? Obviously, it has to figure out somehow what
59 has been changed since the last time the display has been updated,
60 and to make these changes visible. Preferably it would do that in
61 a moderately intelligent way, i.e. fast.
62
63 Changes in buffer text can be deduced from window and buffer
64 structures, and from some global variables like `beg_unchanged' and
65 `end_unchanged'. The contents of the display are additionally
66 recorded in a `glyph matrix', a two-dimensional matrix of glyph
67 structures. Each row in such a matrix corresponds to a line on the
68 display, and each glyph in a row corresponds to a column displaying
69 a character, an image, or what else. This matrix is called the
70 `current glyph matrix' or `current matrix' in redisplay
71 terminology.
72
73 For buffer parts that have been changed since the last update, a
74 second glyph matrix is constructed, the so called `desired glyph
75 matrix' or short `desired matrix'. Current and desired matrix are
76 then compared to find a cheap way to update the display, e.g. by
77 reusing part of the display by scrolling lines.
78
79 You will find a lot of redisplay optimizations when you start
80 looking at the innards of redisplay. The overall goal of all these
81 optimizations is to make redisplay fast because it is done
82 frequently. Some of these optimizations are implemented by the
83 following functions:
84
85 . try_cursor_movement
86
87 This function tries to update the display if the text in the
88 window did not change and did not scroll, only point moved, and
89 it did not move off the displayed portion of the text.
90
91 . try_window_reusing_current_matrix
92
93 This function reuses the current matrix of a window when text
94 has not changed, but the window start changed (e.g., due to
95 scrolling).
96
97 . try_window_id
98
99 This function attempts to redisplay a window by reusing parts of
100 its existing display. It finds and reuses the part that was not
101 changed, and redraws the rest.
102
103 . try_window
104
105 This function performs the full redisplay of a single window
106 assuming that its fonts were not changed and that the cursor
107 will not end up in the scroll margins. (Loading fonts requires
108 re-adjustment of dimensions of glyph matrices, which makes this
109 method impossible to use.)
110
111 These optimizations are tried in sequence (some can be skipped if
112 it is known that they are not applicable). If none of the
113 optimizations were successful, redisplay calls redisplay_windows,
114 which performs a full redisplay of all windows.
115
116 Desired matrices.
117
118 Desired matrices are always built per Emacs window. The function
119 `display_line' is the central function to look at if you are
120 interested. It constructs one row in a desired matrix given an
121 iterator structure containing both a buffer position and a
122 description of the environment in which the text is to be
123 displayed. But this is too early, read on.
124
125 Characters and pixmaps displayed for a range of buffer text depend
126 on various settings of buffers and windows, on overlays and text
127 properties, on display tables, on selective display. The good news
128 is that all this hairy stuff is hidden behind a small set of
129 interface functions taking an iterator structure (struct it)
130 argument.
131
132 Iteration over things to be displayed is then simple. It is
133 started by initializing an iterator with a call to init_iterator,
134 passing it the buffer position where to start iteration. For
135 iteration over strings, pass -1 as the position to init_iterator,
136 and call reseat_to_string when the string is ready, to initialize
137 the iterator for that string. Thereafter, calls to
138 get_next_display_element fill the iterator structure with relevant
139 information about the next thing to display. Calls to
140 set_iterator_to_next move the iterator to the next thing.
141
142 Besides this, an iterator also contains information about the
143 display environment in which glyphs for display elements are to be
144 produced. It has fields for the width and height of the display,
145 the information whether long lines are truncated or continued, a
146 current X and Y position, and lots of other stuff you can better
147 see in dispextern.h.
148
149 Glyphs in a desired matrix are normally constructed in a loop
150 calling get_next_display_element and then PRODUCE_GLYPHS. The call
151 to PRODUCE_GLYPHS will fill the iterator structure with pixel
152 information about the element being displayed and at the same time
153 produce glyphs for it. If the display element fits on the line
154 being displayed, set_iterator_to_next is called next, otherwise the
155 glyphs produced are discarded. The function display_line is the
156 workhorse of filling glyph rows in the desired matrix with glyphs.
157 In addition to producing glyphs, it also handles line truncation
158 and continuation, word wrap, and cursor positioning (for the
159 latter, see also set_cursor_from_row).
160
161 Frame matrices.
162
163 That just couldn't be all, could it? What about terminal types not
164 supporting operations on sub-windows of the screen? To update the
165 display on such a terminal, window-based glyph matrices are not
166 well suited. To be able to reuse part of the display (scrolling
167 lines up and down), we must instead have a view of the whole
168 screen. This is what `frame matrices' are for. They are a trick.
169
170 Frames on terminals like above have a glyph pool. Windows on such
171 a frame sub-allocate their glyph memory from their frame's glyph
172 pool. The frame itself is given its own glyph matrices. By
173 coincidence---or maybe something else---rows in window glyph
174 matrices are slices of corresponding rows in frame matrices. Thus
175 writing to window matrices implicitly updates a frame matrix which
176 provides us with the view of the whole screen that we originally
177 wanted to have without having to move many bytes around. To be
178 honest, there is a little bit more done, but not much more. If you
179 plan to extend that code, take a look at dispnew.c. The function
180 build_frame_matrix is a good starting point.
181
182 Bidirectional display.
183
184 Bidirectional display adds quite some hair to this already complex
185 design. The good news are that a large portion of that hairy stuff
186 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
187 reordering engine which is called by set_iterator_to_next and
188 returns the next character to display in the visual order. See
189 commentary on bidi.c for more details. As far as redisplay is
190 concerned, the effect of calling bidi_move_to_visually_next, the
191 main interface of the reordering engine, is that the iterator gets
192 magically placed on the buffer or string position that is to be
193 displayed next. In other words, a linear iteration through the
194 buffer/string is replaced with a non-linear one. All the rest of
195 the redisplay is oblivious to the bidi reordering.
196
197 Well, almost oblivious---there are still complications, most of
198 them due to the fact that buffer and string positions no longer
199 change monotonously with glyph indices in a glyph row. Moreover,
200 for continued lines, the buffer positions may not even be
201 monotonously changing with vertical positions. Also, accounting
202 for face changes, overlays, etc. becomes more complex because
203 non-linear iteration could potentially skip many positions with
204 changes, and then cross them again on the way back...
205
206 One other prominent effect of bidirectional display is that some
207 paragraphs of text need to be displayed starting at the right
208 margin of the window---the so-called right-to-left, or R2L
209 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
210 which have their reversed_p flag set. The bidi reordering engine
211 produces characters in such rows starting from the character which
212 should be the rightmost on display. PRODUCE_GLYPHS then reverses
213 the order, when it fills up the glyph row whose reversed_p flag is
214 set, by prepending each new glyph to what is already there, instead
215 of appending it. When the glyph row is complete, the function
216 extend_face_to_end_of_line fills the empty space to the left of the
217 leftmost character with special glyphs, which will display as,
218 well, empty. On text terminals, these special glyphs are simply
219 blank characters. On graphics terminals, there's a single stretch
220 glyph of a suitably computed width. Both the blanks and the
221 stretch glyph are given the face of the background of the line.
222 This way, the terminal-specific back-end can still draw the glyphs
223 left to right, even for R2L lines.
224
225 Bidirectional display and character compositions
226
227 Some scripts cannot be displayed by drawing each character
228 individually, because adjacent characters change each other's shape
229 on display. For example, Arabic and Indic scripts belong to this
230 category.
231
232 Emacs display supports this by providing "character compositions",
233 most of which is implemented in composite.c. During the buffer
234 scan that delivers characters to PRODUCE_GLYPHS, if the next
235 character to be delivered is a composed character, the iteration
236 calls composition_reseat_it and next_element_from_composition. If
237 they succeed to compose the character with one or more of the
238 following characters, the whole sequence of characters that where
239 composed is recorded in the `struct composition_it' object that is
240 part of the buffer iterator. The composed sequence could produce
241 one or more font glyphs (called "grapheme clusters") on the screen.
242 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
243 in the direction corresponding to the current bidi scan direction
244 (recorded in the scan_dir member of the `struct bidi_it' object
245 that is part of the buffer iterator). In particular, if the bidi
246 iterator currently scans the buffer backwards, the grapheme
247 clusters are delivered back to front. This reorders the grapheme
248 clusters as appropriate for the current bidi context. Note that
249 this means that the grapheme clusters are always stored in the
250 LGSTRING object (see composite.c) in the logical order.
251
252 Moving an iterator in bidirectional text
253 without producing glyphs
254
255 Note one important detail mentioned above: that the bidi reordering
256 engine, driven by the iterator, produces characters in R2L rows
257 starting at the character that will be the rightmost on display.
258 As far as the iterator is concerned, the geometry of such rows is
259 still left to right, i.e. the iterator "thinks" the first character
260 is at the leftmost pixel position. The iterator does not know that
261 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
262 delivers. This is important when functions from the move_it_*
263 family are used to get to certain screen position or to match
264 screen coordinates with buffer coordinates: these functions use the
265 iterator geometry, which is left to right even in R2L paragraphs.
266 This works well with most callers of move_it_*, because they need
267 to get to a specific column, and columns are still numbered in the
268 reading order, i.e. the rightmost character in a R2L paragraph is
269 still column zero. But some callers do not get well with this; a
270 notable example is mouse clicks that need to find the character
271 that corresponds to certain pixel coordinates. See
272 buffer_posn_from_coords in dispnew.c for how this is handled. */
273
274 #include <config.h>
275 #include <stdio.h>
276 #include <limits.h>
277
278 #include "lisp.h"
279 #include "atimer.h"
280 #include "keyboard.h"
281 #include "frame.h"
282 #include "window.h"
283 #include "termchar.h"
284 #include "dispextern.h"
285 #include "character.h"
286 #include "buffer.h"
287 #include "charset.h"
288 #include "indent.h"
289 #include "commands.h"
290 #include "keymap.h"
291 #include "macros.h"
292 #include "disptab.h"
293 #include "termhooks.h"
294 #include "termopts.h"
295 #include "intervals.h"
296 #include "coding.h"
297 #include "process.h"
298 #include "region-cache.h"
299 #include "font.h"
300 #include "fontset.h"
301 #include "blockinput.h"
302
303 #ifdef HAVE_X_WINDOWS
304 #include "xterm.h"
305 #endif
306 #ifdef HAVE_NTGUI
307 #include "w32term.h"
308 #endif
309 #ifdef HAVE_NS
310 #include "nsterm.h"
311 #endif
312 #ifdef USE_GTK
313 #include "gtkutil.h"
314 #endif
315
316 #include "font.h"
317
318 #ifndef FRAME_X_OUTPUT
319 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
320 #endif
321
322 #define INFINITY 10000000
323
324 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
325 Lisp_Object Qwindow_scroll_functions;
326 static Lisp_Object Qwindow_text_change_functions;
327 static Lisp_Object Qredisplay_end_trigger_functions;
328 Lisp_Object Qinhibit_point_motion_hooks;
329 static Lisp_Object QCeval, QCpropertize;
330 Lisp_Object QCfile, QCdata;
331 static Lisp_Object Qfontified;
332 static Lisp_Object Qgrow_only;
333 static Lisp_Object Qinhibit_eval_during_redisplay;
334 static Lisp_Object Qbuffer_position, Qposition, Qobject;
335 static Lisp_Object Qright_to_left, Qleft_to_right;
336
337 /* Cursor shapes. */
338 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
339
340 /* Pointer shapes. */
341 static Lisp_Object Qarrow, Qhand;
342 Lisp_Object Qtext;
343
344 /* Holds the list (error). */
345 static Lisp_Object list_of_error;
346
347 static Lisp_Object Qfontification_functions;
348
349 static Lisp_Object Qwrap_prefix;
350 static Lisp_Object Qline_prefix;
351 static Lisp_Object Qredisplay_internal;
352
353 /* Non-nil means don't actually do any redisplay. */
354
355 Lisp_Object Qinhibit_redisplay;
356
357 /* Names of text properties relevant for redisplay. */
358
359 Lisp_Object Qdisplay;
360
361 Lisp_Object Qspace, QCalign_to;
362 static Lisp_Object QCrelative_width, QCrelative_height;
363 Lisp_Object Qleft_margin, Qright_margin;
364 static Lisp_Object Qspace_width, Qraise;
365 static Lisp_Object Qslice;
366 Lisp_Object Qcenter;
367 static Lisp_Object Qmargin, Qpointer;
368 static Lisp_Object Qline_height;
369
370 /* These setters are used only in this file, so they can be private. */
371 static void
372 wset_base_line_number (struct window *w, Lisp_Object val)
373 {
374 w->base_line_number = val;
375 }
376 static void
377 wset_base_line_pos (struct window *w, Lisp_Object val)
378 {
379 w->base_line_pos = val;
380 }
381 static void
382 wset_column_number_displayed (struct window *w, Lisp_Object val)
383 {
384 w->column_number_displayed = val;
385 }
386 static void
387 wset_region_showing (struct window *w, Lisp_Object val)
388 {
389 w->region_showing = val;
390 }
391
392 #ifdef HAVE_WINDOW_SYSTEM
393
394 /* Test if overflow newline into fringe. Called with iterator IT
395 at or past right window margin, and with IT->current_x set. */
396
397 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
398 (!NILP (Voverflow_newline_into_fringe) \
399 && FRAME_WINDOW_P ((IT)->f) \
400 && ((IT)->bidi_it.paragraph_dir == R2L \
401 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
402 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
403 && (IT)->current_x == (IT)->last_visible_x \
404 && (IT)->line_wrap != WORD_WRAP)
405
406 #else /* !HAVE_WINDOW_SYSTEM */
407 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
408 #endif /* HAVE_WINDOW_SYSTEM */
409
410 /* Test if the display element loaded in IT, or the underlying buffer
411 or string character, is a space or a TAB character. This is used
412 to determine where word wrapping can occur. */
413
414 #define IT_DISPLAYING_WHITESPACE(it) \
415 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
416 || ((STRINGP (it->string) \
417 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
418 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
419 || (it->s \
420 && (it->s[IT_BYTEPOS (*it)] == ' ' \
421 || it->s[IT_BYTEPOS (*it)] == '\t')) \
422 || (IT_BYTEPOS (*it) < ZV_BYTE \
423 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
424 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
425
426 /* Name of the face used to highlight trailing whitespace. */
427
428 static Lisp_Object Qtrailing_whitespace;
429
430 /* Name and number of the face used to highlight escape glyphs. */
431
432 static Lisp_Object Qescape_glyph;
433
434 /* Name and number of the face used to highlight non-breaking spaces. */
435
436 static Lisp_Object Qnobreak_space;
437
438 /* The symbol `image' which is the car of the lists used to represent
439 images in Lisp. Also a tool bar style. */
440
441 Lisp_Object Qimage;
442
443 /* The image map types. */
444 Lisp_Object QCmap;
445 static Lisp_Object QCpointer;
446 static Lisp_Object Qrect, Qcircle, Qpoly;
447
448 /* Tool bar styles */
449 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
450
451 /* Non-zero means print newline to stdout before next mini-buffer
452 message. */
453
454 int noninteractive_need_newline;
455
456 /* Non-zero means print newline to message log before next message. */
457
458 static int message_log_need_newline;
459
460 /* Three markers that message_dolog uses.
461 It could allocate them itself, but that causes trouble
462 in handling memory-full errors. */
463 static Lisp_Object message_dolog_marker1;
464 static Lisp_Object message_dolog_marker2;
465 static Lisp_Object message_dolog_marker3;
466 \f
467 /* The buffer position of the first character appearing entirely or
468 partially on the line of the selected window which contains the
469 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
470 redisplay optimization in redisplay_internal. */
471
472 static struct text_pos this_line_start_pos;
473
474 /* Number of characters past the end of the line above, including the
475 terminating newline. */
476
477 static struct text_pos this_line_end_pos;
478
479 /* The vertical positions and the height of this line. */
480
481 static int this_line_vpos;
482 static int this_line_y;
483 static int this_line_pixel_height;
484
485 /* X position at which this display line starts. Usually zero;
486 negative if first character is partially visible. */
487
488 static int this_line_start_x;
489
490 /* The smallest character position seen by move_it_* functions as they
491 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
492 hscrolled lines, see display_line. */
493
494 static struct text_pos this_line_min_pos;
495
496 /* Buffer that this_line_.* variables are referring to. */
497
498 static struct buffer *this_line_buffer;
499
500
501 /* Values of those variables at last redisplay are stored as
502 properties on `overlay-arrow-position' symbol. However, if
503 Voverlay_arrow_position is a marker, last-arrow-position is its
504 numerical position. */
505
506 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
507
508 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
509 properties on a symbol in overlay-arrow-variable-list. */
510
511 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
512
513 Lisp_Object Qmenu_bar_update_hook;
514
515 /* Nonzero if an overlay arrow has been displayed in this window. */
516
517 static int overlay_arrow_seen;
518
519 /* Number of windows showing the buffer of the selected window (or
520 another buffer with the same base buffer). keyboard.c refers to
521 this. */
522
523 int buffer_shared;
524
525 /* Vector containing glyphs for an ellipsis `...'. */
526
527 static Lisp_Object default_invis_vector[3];
528
529 /* This is the window where the echo area message was displayed. It
530 is always a mini-buffer window, but it may not be the same window
531 currently active as a mini-buffer. */
532
533 Lisp_Object echo_area_window;
534
535 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
536 pushes the current message and the value of
537 message_enable_multibyte on the stack, the function restore_message
538 pops the stack and displays MESSAGE again. */
539
540 static Lisp_Object Vmessage_stack;
541
542 /* Nonzero means multibyte characters were enabled when the echo area
543 message was specified. */
544
545 static int message_enable_multibyte;
546
547 /* Nonzero if we should redraw the mode lines on the next redisplay. */
548
549 int update_mode_lines;
550
551 /* Nonzero if window sizes or contents have changed since last
552 redisplay that finished. */
553
554 int windows_or_buffers_changed;
555
556 /* Nonzero means a frame's cursor type has been changed. */
557
558 int cursor_type_changed;
559
560 /* Nonzero after display_mode_line if %l was used and it displayed a
561 line number. */
562
563 static int line_number_displayed;
564
565 /* The name of the *Messages* buffer, a string. */
566
567 static Lisp_Object Vmessages_buffer_name;
568
569 /* Current, index 0, and last displayed echo area message. Either
570 buffers from echo_buffers, or nil to indicate no message. */
571
572 Lisp_Object echo_area_buffer[2];
573
574 /* The buffers referenced from echo_area_buffer. */
575
576 static Lisp_Object echo_buffer[2];
577
578 /* A vector saved used in with_area_buffer to reduce consing. */
579
580 static Lisp_Object Vwith_echo_area_save_vector;
581
582 /* Non-zero means display_echo_area should display the last echo area
583 message again. Set by redisplay_preserve_echo_area. */
584
585 static int display_last_displayed_message_p;
586
587 /* Nonzero if echo area is being used by print; zero if being used by
588 message. */
589
590 static int message_buf_print;
591
592 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
593
594 static Lisp_Object Qinhibit_menubar_update;
595 static Lisp_Object Qmessage_truncate_lines;
596
597 /* Set to 1 in clear_message to make redisplay_internal aware
598 of an emptied echo area. */
599
600 static int message_cleared_p;
601
602 /* A scratch glyph row with contents used for generating truncation
603 glyphs. Also used in direct_output_for_insert. */
604
605 #define MAX_SCRATCH_GLYPHS 100
606 static struct glyph_row scratch_glyph_row;
607 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
608
609 /* Ascent and height of the last line processed by move_it_to. */
610
611 static int last_max_ascent, last_height;
612
613 /* Non-zero if there's a help-echo in the echo area. */
614
615 int help_echo_showing_p;
616
617 /* If >= 0, computed, exact values of mode-line and header-line height
618 to use in the macros CURRENT_MODE_LINE_HEIGHT and
619 CURRENT_HEADER_LINE_HEIGHT. */
620
621 int current_mode_line_height, current_header_line_height;
622
623 /* The maximum distance to look ahead for text properties. Values
624 that are too small let us call compute_char_face and similar
625 functions too often which is expensive. Values that are too large
626 let us call compute_char_face and alike too often because we
627 might not be interested in text properties that far away. */
628
629 #define TEXT_PROP_DISTANCE_LIMIT 100
630
631 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
632 iterator state and later restore it. This is needed because the
633 bidi iterator on bidi.c keeps a stacked cache of its states, which
634 is really a singleton. When we use scratch iterator objects to
635 move around the buffer, we can cause the bidi cache to be pushed or
636 popped, and therefore we need to restore the cache state when we
637 return to the original iterator. */
638 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
639 do { \
640 if (CACHE) \
641 bidi_unshelve_cache (CACHE, 1); \
642 ITCOPY = ITORIG; \
643 CACHE = bidi_shelve_cache (); \
644 } while (0)
645
646 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
647 do { \
648 if (pITORIG != pITCOPY) \
649 *(pITORIG) = *(pITCOPY); \
650 bidi_unshelve_cache (CACHE, 0); \
651 CACHE = NULL; \
652 } while (0)
653
654 #ifdef GLYPH_DEBUG
655
656 /* Non-zero means print traces of redisplay if compiled with
657 GLYPH_DEBUG defined. */
658
659 int trace_redisplay_p;
660
661 #endif /* GLYPH_DEBUG */
662
663 #ifdef DEBUG_TRACE_MOVE
664 /* Non-zero means trace with TRACE_MOVE to stderr. */
665 int trace_move;
666
667 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
668 #else
669 #define TRACE_MOVE(x) (void) 0
670 #endif
671
672 static Lisp_Object Qauto_hscroll_mode;
673
674 /* Buffer being redisplayed -- for redisplay_window_error. */
675
676 static struct buffer *displayed_buffer;
677
678 /* Value returned from text property handlers (see below). */
679
680 enum prop_handled
681 {
682 HANDLED_NORMALLY,
683 HANDLED_RECOMPUTE_PROPS,
684 HANDLED_OVERLAY_STRING_CONSUMED,
685 HANDLED_RETURN
686 };
687
688 /* A description of text properties that redisplay is interested
689 in. */
690
691 struct props
692 {
693 /* The name of the property. */
694 Lisp_Object *name;
695
696 /* A unique index for the property. */
697 enum prop_idx idx;
698
699 /* A handler function called to set up iterator IT from the property
700 at IT's current position. Value is used to steer handle_stop. */
701 enum prop_handled (*handler) (struct it *it);
702 };
703
704 static enum prop_handled handle_face_prop (struct it *);
705 static enum prop_handled handle_invisible_prop (struct it *);
706 static enum prop_handled handle_display_prop (struct it *);
707 static enum prop_handled handle_composition_prop (struct it *);
708 static enum prop_handled handle_overlay_change (struct it *);
709 static enum prop_handled handle_fontified_prop (struct it *);
710
711 /* Properties handled by iterators. */
712
713 static struct props it_props[] =
714 {
715 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
716 /* Handle `face' before `display' because some sub-properties of
717 `display' need to know the face. */
718 {&Qface, FACE_PROP_IDX, handle_face_prop},
719 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
720 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
721 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
722 {NULL, 0, NULL}
723 };
724
725 /* Value is the position described by X. If X is a marker, value is
726 the marker_position of X. Otherwise, value is X. */
727
728 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
729
730 /* Enumeration returned by some move_it_.* functions internally. */
731
732 enum move_it_result
733 {
734 /* Not used. Undefined value. */
735 MOVE_UNDEFINED,
736
737 /* Move ended at the requested buffer position or ZV. */
738 MOVE_POS_MATCH_OR_ZV,
739
740 /* Move ended at the requested X pixel position. */
741 MOVE_X_REACHED,
742
743 /* Move within a line ended at the end of a line that must be
744 continued. */
745 MOVE_LINE_CONTINUED,
746
747 /* Move within a line ended at the end of a line that would
748 be displayed truncated. */
749 MOVE_LINE_TRUNCATED,
750
751 /* Move within a line ended at a line end. */
752 MOVE_NEWLINE_OR_CR
753 };
754
755 /* This counter is used to clear the face cache every once in a while
756 in redisplay_internal. It is incremented for each redisplay.
757 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
758 cleared. */
759
760 #define CLEAR_FACE_CACHE_COUNT 500
761 static int clear_face_cache_count;
762
763 /* Similarly for the image cache. */
764
765 #ifdef HAVE_WINDOW_SYSTEM
766 #define CLEAR_IMAGE_CACHE_COUNT 101
767 static int clear_image_cache_count;
768
769 /* Null glyph slice */
770 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
771 #endif
772
773 /* True while redisplay_internal is in progress. */
774
775 bool redisplaying_p;
776
777 static Lisp_Object Qinhibit_free_realized_faces;
778 static Lisp_Object Qmode_line_default_help_echo;
779
780 /* If a string, XTread_socket generates an event to display that string.
781 (The display is done in read_char.) */
782
783 Lisp_Object help_echo_string;
784 Lisp_Object help_echo_window;
785 Lisp_Object help_echo_object;
786 ptrdiff_t help_echo_pos;
787
788 /* Temporary variable for XTread_socket. */
789
790 Lisp_Object previous_help_echo_string;
791
792 /* Platform-independent portion of hourglass implementation. */
793
794 /* Non-zero means an hourglass cursor is currently shown. */
795 int hourglass_shown_p;
796
797 /* If non-null, an asynchronous timer that, when it expires, displays
798 an hourglass cursor on all frames. */
799 struct atimer *hourglass_atimer;
800
801 /* Name of the face used to display glyphless characters. */
802 Lisp_Object Qglyphless_char;
803
804 /* Symbol for the purpose of Vglyphless_char_display. */
805 static Lisp_Object Qglyphless_char_display;
806
807 /* Method symbols for Vglyphless_char_display. */
808 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
809
810 /* Default pixel width of `thin-space' display method. */
811 #define THIN_SPACE_WIDTH 1
812
813 /* Default number of seconds to wait before displaying an hourglass
814 cursor. */
815 #define DEFAULT_HOURGLASS_DELAY 1
816
817 \f
818 /* Function prototypes. */
819
820 static void setup_for_ellipsis (struct it *, int);
821 static void set_iterator_to_next (struct it *, int);
822 static void mark_window_display_accurate_1 (struct window *, int);
823 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
824 static int display_prop_string_p (Lisp_Object, Lisp_Object);
825 static int cursor_row_p (struct glyph_row *);
826 static int redisplay_mode_lines (Lisp_Object, int);
827 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
828
829 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
830
831 static void handle_line_prefix (struct it *);
832
833 static void pint2str (char *, int, ptrdiff_t);
834 static void pint2hrstr (char *, int, ptrdiff_t);
835 static struct text_pos run_window_scroll_functions (Lisp_Object,
836 struct text_pos);
837 static void reconsider_clip_changes (struct window *, struct buffer *);
838 static int text_outside_line_unchanged_p (struct window *,
839 ptrdiff_t, ptrdiff_t);
840 static void store_mode_line_noprop_char (char);
841 static int store_mode_line_noprop (const char *, int, int);
842 static void handle_stop (struct it *);
843 static void handle_stop_backwards (struct it *, ptrdiff_t);
844 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
845 static void ensure_echo_area_buffers (void);
846 static Lisp_Object unwind_with_echo_area_buffer (Lisp_Object);
847 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
848 static int with_echo_area_buffer (struct window *, int,
849 int (*) (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t),
850 ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
851 static void clear_garbaged_frames (void);
852 static int current_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
853 static void pop_message (void);
854 static int truncate_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
855 static void set_message (const char *, Lisp_Object, ptrdiff_t, int);
856 static int set_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
857 static int display_echo_area (struct window *);
858 static int display_echo_area_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
859 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
860 static Lisp_Object unwind_redisplay (Lisp_Object);
861 static int string_char_and_length (const unsigned char *, int *);
862 static struct text_pos display_prop_end (struct it *, Lisp_Object,
863 struct text_pos);
864 static int compute_window_start_on_continuation_line (struct window *);
865 static void insert_left_trunc_glyphs (struct it *);
866 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
867 Lisp_Object);
868 static void extend_face_to_end_of_line (struct it *);
869 static int append_space_for_newline (struct it *, int);
870 static int cursor_row_fully_visible_p (struct window *, int, int);
871 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
872 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
873 static int trailing_whitespace_p (ptrdiff_t);
874 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
875 static void push_it (struct it *, struct text_pos *);
876 static void iterate_out_of_display_property (struct it *);
877 static void pop_it (struct it *);
878 static void sync_frame_with_window_matrix_rows (struct window *);
879 static void select_frame_for_redisplay (Lisp_Object);
880 static void redisplay_internal (void);
881 static int echo_area_display (int);
882 static void redisplay_windows (Lisp_Object);
883 static void redisplay_window (Lisp_Object, int);
884 static Lisp_Object redisplay_window_error (Lisp_Object);
885 static Lisp_Object redisplay_window_0 (Lisp_Object);
886 static Lisp_Object redisplay_window_1 (Lisp_Object);
887 static int set_cursor_from_row (struct window *, struct glyph_row *,
888 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
889 int, int);
890 static int update_menu_bar (struct frame *, int, int);
891 static int try_window_reusing_current_matrix (struct window *);
892 static int try_window_id (struct window *);
893 static int display_line (struct it *);
894 static int display_mode_lines (struct window *);
895 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
896 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
897 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
898 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
899 static void display_menu_bar (struct window *);
900 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
901 ptrdiff_t *);
902 static int display_string (const char *, Lisp_Object, Lisp_Object,
903 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
904 static void compute_line_metrics (struct it *);
905 static void run_redisplay_end_trigger_hook (struct it *);
906 static int get_overlay_strings (struct it *, ptrdiff_t);
907 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
908 static void next_overlay_string (struct it *);
909 static void reseat (struct it *, struct text_pos, int);
910 static void reseat_1 (struct it *, struct text_pos, int);
911 static void back_to_previous_visible_line_start (struct it *);
912 void reseat_at_previous_visible_line_start (struct it *);
913 static void reseat_at_next_visible_line_start (struct it *, int);
914 static int next_element_from_ellipsis (struct it *);
915 static int next_element_from_display_vector (struct it *);
916 static int next_element_from_string (struct it *);
917 static int next_element_from_c_string (struct it *);
918 static int next_element_from_buffer (struct it *);
919 static int next_element_from_composition (struct it *);
920 static int next_element_from_image (struct it *);
921 static int next_element_from_stretch (struct it *);
922 static void load_overlay_strings (struct it *, ptrdiff_t);
923 static int init_from_display_pos (struct it *, struct window *,
924 struct display_pos *);
925 static void reseat_to_string (struct it *, const char *,
926 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
927 static int get_next_display_element (struct it *);
928 static enum move_it_result
929 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
930 enum move_operation_enum);
931 void move_it_vertically_backward (struct it *, int);
932 static void get_visually_first_element (struct it *);
933 static void init_to_row_start (struct it *, struct window *,
934 struct glyph_row *);
935 static int init_to_row_end (struct it *, struct window *,
936 struct glyph_row *);
937 static void back_to_previous_line_start (struct it *);
938 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
939 static struct text_pos string_pos_nchars_ahead (struct text_pos,
940 Lisp_Object, ptrdiff_t);
941 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
942 static struct text_pos c_string_pos (ptrdiff_t, const char *, int);
943 static ptrdiff_t number_of_chars (const char *, int);
944 static void compute_stop_pos (struct it *);
945 static void compute_string_pos (struct text_pos *, struct text_pos,
946 Lisp_Object);
947 static int face_before_or_after_it_pos (struct it *, int);
948 static ptrdiff_t next_overlay_change (ptrdiff_t);
949 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
950 Lisp_Object, struct text_pos *, ptrdiff_t, int);
951 static int handle_single_display_spec (struct it *, Lisp_Object,
952 Lisp_Object, Lisp_Object,
953 struct text_pos *, ptrdiff_t, int, int);
954 static int underlying_face_id (struct it *);
955 static int in_ellipses_for_invisible_text_p (struct display_pos *,
956 struct window *);
957
958 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
959 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
960
961 #ifdef HAVE_WINDOW_SYSTEM
962
963 static void x_consider_frame_title (Lisp_Object);
964 static int tool_bar_lines_needed (struct frame *, int *);
965 static void update_tool_bar (struct frame *, int);
966 static void build_desired_tool_bar_string (struct frame *f);
967 static int redisplay_tool_bar (struct frame *);
968 static void display_tool_bar_line (struct it *, int);
969 static void notice_overwritten_cursor (struct window *,
970 enum glyph_row_area,
971 int, int, int, int);
972 static void append_stretch_glyph (struct it *, Lisp_Object,
973 int, int, int);
974
975
976 #endif /* HAVE_WINDOW_SYSTEM */
977
978 static void produce_special_glyphs (struct it *, enum display_element_type);
979 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
980 static int coords_in_mouse_face_p (struct window *, int, int);
981
982
983 \f
984 /***********************************************************************
985 Window display dimensions
986 ***********************************************************************/
987
988 /* Return the bottom boundary y-position for text lines in window W.
989 This is the first y position at which a line cannot start.
990 It is relative to the top of the window.
991
992 This is the height of W minus the height of a mode line, if any. */
993
994 int
995 window_text_bottom_y (struct window *w)
996 {
997 int height = WINDOW_TOTAL_HEIGHT (w);
998
999 if (WINDOW_WANTS_MODELINE_P (w))
1000 height -= CURRENT_MODE_LINE_HEIGHT (w);
1001 return height;
1002 }
1003
1004 /* Return the pixel width of display area AREA of window W. AREA < 0
1005 means return the total width of W, not including fringes to
1006 the left and right of the window. */
1007
1008 int
1009 window_box_width (struct window *w, int area)
1010 {
1011 int cols = XFASTINT (w->total_cols);
1012 int pixels = 0;
1013
1014 if (!w->pseudo_window_p)
1015 {
1016 cols -= WINDOW_SCROLL_BAR_COLS (w);
1017
1018 if (area == TEXT_AREA)
1019 {
1020 if (INTEGERP (w->left_margin_cols))
1021 cols -= XFASTINT (w->left_margin_cols);
1022 if (INTEGERP (w->right_margin_cols))
1023 cols -= XFASTINT (w->right_margin_cols);
1024 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1025 }
1026 else if (area == LEFT_MARGIN_AREA)
1027 {
1028 cols = (INTEGERP (w->left_margin_cols)
1029 ? XFASTINT (w->left_margin_cols) : 0);
1030 pixels = 0;
1031 }
1032 else if (area == RIGHT_MARGIN_AREA)
1033 {
1034 cols = (INTEGERP (w->right_margin_cols)
1035 ? XFASTINT (w->right_margin_cols) : 0);
1036 pixels = 0;
1037 }
1038 }
1039
1040 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1041 }
1042
1043
1044 /* Return the pixel height of the display area of window W, not
1045 including mode lines of W, if any. */
1046
1047 int
1048 window_box_height (struct window *w)
1049 {
1050 struct frame *f = XFRAME (w->frame);
1051 int height = WINDOW_TOTAL_HEIGHT (w);
1052
1053 eassert (height >= 0);
1054
1055 /* Note: the code below that determines the mode-line/header-line
1056 height is essentially the same as that contained in the macro
1057 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1058 the appropriate glyph row has its `mode_line_p' flag set,
1059 and if it doesn't, uses estimate_mode_line_height instead. */
1060
1061 if (WINDOW_WANTS_MODELINE_P (w))
1062 {
1063 struct glyph_row *ml_row
1064 = (w->current_matrix && w->current_matrix->rows
1065 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1066 : 0);
1067 if (ml_row && ml_row->mode_line_p)
1068 height -= ml_row->height;
1069 else
1070 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1071 }
1072
1073 if (WINDOW_WANTS_HEADER_LINE_P (w))
1074 {
1075 struct glyph_row *hl_row
1076 = (w->current_matrix && w->current_matrix->rows
1077 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1078 : 0);
1079 if (hl_row && hl_row->mode_line_p)
1080 height -= hl_row->height;
1081 else
1082 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1083 }
1084
1085 /* With a very small font and a mode-line that's taller than
1086 default, we might end up with a negative height. */
1087 return max (0, height);
1088 }
1089
1090 /* Return the window-relative coordinate of the left edge of display
1091 area AREA of window W. AREA < 0 means return the left edge of the
1092 whole window, to the right of the left fringe of W. */
1093
1094 int
1095 window_box_left_offset (struct window *w, int area)
1096 {
1097 int x;
1098
1099 if (w->pseudo_window_p)
1100 return 0;
1101
1102 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1103
1104 if (area == TEXT_AREA)
1105 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1106 + window_box_width (w, LEFT_MARGIN_AREA));
1107 else if (area == RIGHT_MARGIN_AREA)
1108 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1109 + window_box_width (w, LEFT_MARGIN_AREA)
1110 + window_box_width (w, TEXT_AREA)
1111 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1112 ? 0
1113 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1114 else if (area == LEFT_MARGIN_AREA
1115 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1116 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1117
1118 return x;
1119 }
1120
1121
1122 /* Return the window-relative coordinate of the right edge of display
1123 area AREA of window W. AREA < 0 means return the right edge of the
1124 whole window, to the left of the right fringe of W. */
1125
1126 int
1127 window_box_right_offset (struct window *w, int area)
1128 {
1129 return window_box_left_offset (w, area) + window_box_width (w, area);
1130 }
1131
1132 /* Return the frame-relative coordinate of the left edge of display
1133 area AREA of window W. AREA < 0 means return the left edge of the
1134 whole window, to the right of the left fringe of W. */
1135
1136 int
1137 window_box_left (struct window *w, int area)
1138 {
1139 struct frame *f = XFRAME (w->frame);
1140 int x;
1141
1142 if (w->pseudo_window_p)
1143 return FRAME_INTERNAL_BORDER_WIDTH (f);
1144
1145 x = (WINDOW_LEFT_EDGE_X (w)
1146 + window_box_left_offset (w, area));
1147
1148 return x;
1149 }
1150
1151
1152 /* Return the frame-relative coordinate of the right edge of display
1153 area AREA of window W. AREA < 0 means return the right edge of the
1154 whole window, to the left of the right fringe of W. */
1155
1156 int
1157 window_box_right (struct window *w, int area)
1158 {
1159 return window_box_left (w, area) + window_box_width (w, area);
1160 }
1161
1162 /* Get the bounding box of the display area AREA of window W, without
1163 mode lines, in frame-relative coordinates. AREA < 0 means the
1164 whole window, not including the left and right fringes of
1165 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1166 coordinates of the upper-left corner of the box. Return in
1167 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1168
1169 void
1170 window_box (struct window *w, int area, int *box_x, int *box_y,
1171 int *box_width, int *box_height)
1172 {
1173 if (box_width)
1174 *box_width = window_box_width (w, area);
1175 if (box_height)
1176 *box_height = window_box_height (w);
1177 if (box_x)
1178 *box_x = window_box_left (w, area);
1179 if (box_y)
1180 {
1181 *box_y = WINDOW_TOP_EDGE_Y (w);
1182 if (WINDOW_WANTS_HEADER_LINE_P (w))
1183 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1184 }
1185 }
1186
1187
1188 /* Get the bounding box of the display area AREA of window W, without
1189 mode lines. AREA < 0 means the whole window, not including the
1190 left and right fringe of the window. Return in *TOP_LEFT_X
1191 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1192 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1193 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1194 box. */
1195
1196 static void
1197 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1198 int *bottom_right_x, int *bottom_right_y)
1199 {
1200 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1201 bottom_right_y);
1202 *bottom_right_x += *top_left_x;
1203 *bottom_right_y += *top_left_y;
1204 }
1205
1206
1207 \f
1208 /***********************************************************************
1209 Utilities
1210 ***********************************************************************/
1211
1212 /* Return the bottom y-position of the line the iterator IT is in.
1213 This can modify IT's settings. */
1214
1215 int
1216 line_bottom_y (struct it *it)
1217 {
1218 int line_height = it->max_ascent + it->max_descent;
1219 int line_top_y = it->current_y;
1220
1221 if (line_height == 0)
1222 {
1223 if (last_height)
1224 line_height = last_height;
1225 else if (IT_CHARPOS (*it) < ZV)
1226 {
1227 move_it_by_lines (it, 1);
1228 line_height = (it->max_ascent || it->max_descent
1229 ? it->max_ascent + it->max_descent
1230 : last_height);
1231 }
1232 else
1233 {
1234 struct glyph_row *row = it->glyph_row;
1235
1236 /* Use the default character height. */
1237 it->glyph_row = NULL;
1238 it->what = IT_CHARACTER;
1239 it->c = ' ';
1240 it->len = 1;
1241 PRODUCE_GLYPHS (it);
1242 line_height = it->ascent + it->descent;
1243 it->glyph_row = row;
1244 }
1245 }
1246
1247 return line_top_y + line_height;
1248 }
1249
1250 /* Subroutine of pos_visible_p below. Extracts a display string, if
1251 any, from the display spec given as its argument. */
1252 static Lisp_Object
1253 string_from_display_spec (Lisp_Object spec)
1254 {
1255 if (CONSP (spec))
1256 {
1257 while (CONSP (spec))
1258 {
1259 if (STRINGP (XCAR (spec)))
1260 return XCAR (spec);
1261 spec = XCDR (spec);
1262 }
1263 }
1264 else if (VECTORP (spec))
1265 {
1266 ptrdiff_t i;
1267
1268 for (i = 0; i < ASIZE (spec); i++)
1269 {
1270 if (STRINGP (AREF (spec, i)))
1271 return AREF (spec, i);
1272 }
1273 return Qnil;
1274 }
1275
1276 return spec;
1277 }
1278
1279
1280 /* Limit insanely large values of W->hscroll on frame F to the largest
1281 value that will still prevent first_visible_x and last_visible_x of
1282 'struct it' from overflowing an int. */
1283 static int
1284 window_hscroll_limited (struct window *w, struct frame *f)
1285 {
1286 ptrdiff_t window_hscroll = w->hscroll;
1287 int window_text_width = window_box_width (w, TEXT_AREA);
1288 int colwidth = FRAME_COLUMN_WIDTH (f);
1289
1290 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1291 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1292
1293 return window_hscroll;
1294 }
1295
1296 /* Return 1 if position CHARPOS is visible in window W.
1297 CHARPOS < 0 means return info about WINDOW_END position.
1298 If visible, set *X and *Y to pixel coordinates of top left corner.
1299 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1300 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1301
1302 int
1303 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1304 int *rtop, int *rbot, int *rowh, int *vpos)
1305 {
1306 struct it it;
1307 void *itdata = bidi_shelve_cache ();
1308 struct text_pos top;
1309 int visible_p = 0;
1310 struct buffer *old_buffer = NULL;
1311
1312 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1313 return visible_p;
1314
1315 if (XBUFFER (w->buffer) != current_buffer)
1316 {
1317 old_buffer = current_buffer;
1318 set_buffer_internal_1 (XBUFFER (w->buffer));
1319 }
1320
1321 SET_TEXT_POS_FROM_MARKER (top, w->start);
1322 /* Scrolling a minibuffer window via scroll bar when the echo area
1323 shows long text sometimes resets the minibuffer contents behind
1324 our backs. */
1325 if (CHARPOS (top) > ZV)
1326 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1327
1328 /* Compute exact mode line heights. */
1329 if (WINDOW_WANTS_MODELINE_P (w))
1330 current_mode_line_height
1331 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1332 BVAR (current_buffer, mode_line_format));
1333
1334 if (WINDOW_WANTS_HEADER_LINE_P (w))
1335 current_header_line_height
1336 = display_mode_line (w, HEADER_LINE_FACE_ID,
1337 BVAR (current_buffer, header_line_format));
1338
1339 start_display (&it, w, top);
1340 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1341 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1342
1343 if (charpos >= 0
1344 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1345 && IT_CHARPOS (it) >= charpos)
1346 /* When scanning backwards under bidi iteration, move_it_to
1347 stops at or _before_ CHARPOS, because it stops at or to
1348 the _right_ of the character at CHARPOS. */
1349 || (it.bidi_p && it.bidi_it.scan_dir == -1
1350 && IT_CHARPOS (it) <= charpos)))
1351 {
1352 /* We have reached CHARPOS, or passed it. How the call to
1353 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1354 or covered by a display property, move_it_to stops at the end
1355 of the invisible text, to the right of CHARPOS. (ii) If
1356 CHARPOS is in a display vector, move_it_to stops on its last
1357 glyph. */
1358 int top_x = it.current_x;
1359 int top_y = it.current_y;
1360 /* Calling line_bottom_y may change it.method, it.position, etc. */
1361 enum it_method it_method = it.method;
1362 int bottom_y = (last_height = 0, line_bottom_y (&it));
1363 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1364
1365 if (top_y < window_top_y)
1366 visible_p = bottom_y > window_top_y;
1367 else if (top_y < it.last_visible_y)
1368 visible_p = 1;
1369 if (bottom_y >= it.last_visible_y
1370 && it.bidi_p && it.bidi_it.scan_dir == -1
1371 && IT_CHARPOS (it) < charpos)
1372 {
1373 /* When the last line of the window is scanned backwards
1374 under bidi iteration, we could be duped into thinking
1375 that we have passed CHARPOS, when in fact move_it_to
1376 simply stopped short of CHARPOS because it reached
1377 last_visible_y. To see if that's what happened, we call
1378 move_it_to again with a slightly larger vertical limit,
1379 and see if it actually moved vertically; if it did, we
1380 didn't really reach CHARPOS, which is beyond window end. */
1381 struct it save_it = it;
1382 /* Why 10? because we don't know how many canonical lines
1383 will the height of the next line(s) be. So we guess. */
1384 int ten_more_lines =
1385 10 * FRAME_LINE_HEIGHT (XFRAME (WINDOW_FRAME (w)));
1386
1387 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1388 MOVE_TO_POS | MOVE_TO_Y);
1389 if (it.current_y > top_y)
1390 visible_p = 0;
1391
1392 it = save_it;
1393 }
1394 if (visible_p)
1395 {
1396 if (it_method == GET_FROM_DISPLAY_VECTOR)
1397 {
1398 /* We stopped on the last glyph of a display vector.
1399 Try and recompute. Hack alert! */
1400 if (charpos < 2 || top.charpos >= charpos)
1401 top_x = it.glyph_row->x;
1402 else
1403 {
1404 struct it it2;
1405 start_display (&it2, w, top);
1406 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1407 get_next_display_element (&it2);
1408 PRODUCE_GLYPHS (&it2);
1409 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1410 || it2.current_x > it2.last_visible_x)
1411 top_x = it.glyph_row->x;
1412 else
1413 {
1414 top_x = it2.current_x;
1415 top_y = it2.current_y;
1416 }
1417 }
1418 }
1419 else if (IT_CHARPOS (it) != charpos)
1420 {
1421 Lisp_Object cpos = make_number (charpos);
1422 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1423 Lisp_Object string = string_from_display_spec (spec);
1424 int newline_in_string = 0;
1425
1426 if (STRINGP (string))
1427 {
1428 const char *s = SSDATA (string);
1429 const char *e = s + SBYTES (string);
1430 while (s < e)
1431 {
1432 if (*s++ == '\n')
1433 {
1434 newline_in_string = 1;
1435 break;
1436 }
1437 }
1438 }
1439 /* The tricky code below is needed because there's a
1440 discrepancy between move_it_to and how we set cursor
1441 when the display line ends in a newline from a
1442 display string. move_it_to will stop _after_ such
1443 display strings, whereas set_cursor_from_row
1444 conspires with cursor_row_p to place the cursor on
1445 the first glyph produced from the display string. */
1446
1447 /* We have overshoot PT because it is covered by a
1448 display property whose value is a string. If the
1449 string includes embedded newlines, we are also in the
1450 wrong display line. Backtrack to the correct line,
1451 where the display string begins. */
1452 if (newline_in_string)
1453 {
1454 Lisp_Object startpos, endpos;
1455 EMACS_INT start, end;
1456 struct it it3;
1457 int it3_moved;
1458
1459 /* Find the first and the last buffer positions
1460 covered by the display string. */
1461 endpos =
1462 Fnext_single_char_property_change (cpos, Qdisplay,
1463 Qnil, Qnil);
1464 startpos =
1465 Fprevious_single_char_property_change (endpos, Qdisplay,
1466 Qnil, Qnil);
1467 start = XFASTINT (startpos);
1468 end = XFASTINT (endpos);
1469 /* Move to the last buffer position before the
1470 display property. */
1471 start_display (&it3, w, top);
1472 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1473 /* Move forward one more line if the position before
1474 the display string is a newline or if it is the
1475 rightmost character on a line that is
1476 continued or word-wrapped. */
1477 if (it3.method == GET_FROM_BUFFER
1478 && it3.c == '\n')
1479 move_it_by_lines (&it3, 1);
1480 else if (move_it_in_display_line_to (&it3, -1,
1481 it3.current_x
1482 + it3.pixel_width,
1483 MOVE_TO_X)
1484 == MOVE_LINE_CONTINUED)
1485 {
1486 move_it_by_lines (&it3, 1);
1487 /* When we are under word-wrap, the #$@%!
1488 move_it_by_lines moves 2 lines, so we need to
1489 fix that up. */
1490 if (it3.line_wrap == WORD_WRAP)
1491 move_it_by_lines (&it3, -1);
1492 }
1493
1494 /* Record the vertical coordinate of the display
1495 line where we wound up. */
1496 top_y = it3.current_y;
1497 if (it3.bidi_p)
1498 {
1499 /* When characters are reordered for display,
1500 the character displayed to the left of the
1501 display string could be _after_ the display
1502 property in the logical order. Use the
1503 smallest vertical position of these two. */
1504 start_display (&it3, w, top);
1505 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1506 if (it3.current_y < top_y)
1507 top_y = it3.current_y;
1508 }
1509 /* Move from the top of the window to the beginning
1510 of the display line where the display string
1511 begins. */
1512 start_display (&it3, w, top);
1513 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1514 /* If it3_moved stays zero after the 'while' loop
1515 below, that means we already were at a newline
1516 before the loop (e.g., the display string begins
1517 with a newline), so we don't need to (and cannot)
1518 inspect the glyphs of it3.glyph_row, because
1519 PRODUCE_GLYPHS will not produce anything for a
1520 newline, and thus it3.glyph_row stays at its
1521 stale content it got at top of the window. */
1522 it3_moved = 0;
1523 /* Finally, advance the iterator until we hit the
1524 first display element whose character position is
1525 CHARPOS, or until the first newline from the
1526 display string, which signals the end of the
1527 display line. */
1528 while (get_next_display_element (&it3))
1529 {
1530 PRODUCE_GLYPHS (&it3);
1531 if (IT_CHARPOS (it3) == charpos
1532 || ITERATOR_AT_END_OF_LINE_P (&it3))
1533 break;
1534 it3_moved = 1;
1535 set_iterator_to_next (&it3, 0);
1536 }
1537 top_x = it3.current_x - it3.pixel_width;
1538 /* Normally, we would exit the above loop because we
1539 found the display element whose character
1540 position is CHARPOS. For the contingency that we
1541 didn't, and stopped at the first newline from the
1542 display string, move back over the glyphs
1543 produced from the string, until we find the
1544 rightmost glyph not from the string. */
1545 if (it3_moved
1546 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1547 {
1548 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1549 + it3.glyph_row->used[TEXT_AREA];
1550
1551 while (EQ ((g - 1)->object, string))
1552 {
1553 --g;
1554 top_x -= g->pixel_width;
1555 }
1556 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1557 + it3.glyph_row->used[TEXT_AREA]);
1558 }
1559 }
1560 }
1561
1562 *x = top_x;
1563 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1564 *rtop = max (0, window_top_y - top_y);
1565 *rbot = max (0, bottom_y - it.last_visible_y);
1566 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1567 - max (top_y, window_top_y)));
1568 *vpos = it.vpos;
1569 }
1570 }
1571 else
1572 {
1573 /* We were asked to provide info about WINDOW_END. */
1574 struct it it2;
1575 void *it2data = NULL;
1576
1577 SAVE_IT (it2, it, it2data);
1578 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1579 move_it_by_lines (&it, 1);
1580 if (charpos < IT_CHARPOS (it)
1581 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1582 {
1583 visible_p = 1;
1584 RESTORE_IT (&it2, &it2, it2data);
1585 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1586 *x = it2.current_x;
1587 *y = it2.current_y + it2.max_ascent - it2.ascent;
1588 *rtop = max (0, -it2.current_y);
1589 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1590 - it.last_visible_y));
1591 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1592 it.last_visible_y)
1593 - max (it2.current_y,
1594 WINDOW_HEADER_LINE_HEIGHT (w))));
1595 *vpos = it2.vpos;
1596 }
1597 else
1598 bidi_unshelve_cache (it2data, 1);
1599 }
1600 bidi_unshelve_cache (itdata, 0);
1601
1602 if (old_buffer)
1603 set_buffer_internal_1 (old_buffer);
1604
1605 current_header_line_height = current_mode_line_height = -1;
1606
1607 if (visible_p && w->hscroll > 0)
1608 *x -=
1609 window_hscroll_limited (w, WINDOW_XFRAME (w))
1610 * WINDOW_FRAME_COLUMN_WIDTH (w);
1611
1612 #if 0
1613 /* Debugging code. */
1614 if (visible_p)
1615 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1616 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1617 else
1618 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1619 #endif
1620
1621 return visible_p;
1622 }
1623
1624
1625 /* Return the next character from STR. Return in *LEN the length of
1626 the character. This is like STRING_CHAR_AND_LENGTH but never
1627 returns an invalid character. If we find one, we return a `?', but
1628 with the length of the invalid character. */
1629
1630 static int
1631 string_char_and_length (const unsigned char *str, int *len)
1632 {
1633 int c;
1634
1635 c = STRING_CHAR_AND_LENGTH (str, *len);
1636 if (!CHAR_VALID_P (c))
1637 /* We may not change the length here because other places in Emacs
1638 don't use this function, i.e. they silently accept invalid
1639 characters. */
1640 c = '?';
1641
1642 return c;
1643 }
1644
1645
1646
1647 /* Given a position POS containing a valid character and byte position
1648 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1649
1650 static struct text_pos
1651 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1652 {
1653 eassert (STRINGP (string) && nchars >= 0);
1654
1655 if (STRING_MULTIBYTE (string))
1656 {
1657 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1658 int len;
1659
1660 while (nchars--)
1661 {
1662 string_char_and_length (p, &len);
1663 p += len;
1664 CHARPOS (pos) += 1;
1665 BYTEPOS (pos) += len;
1666 }
1667 }
1668 else
1669 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1670
1671 return pos;
1672 }
1673
1674
1675 /* Value is the text position, i.e. character and byte position,
1676 for character position CHARPOS in STRING. */
1677
1678 static struct text_pos
1679 string_pos (ptrdiff_t charpos, Lisp_Object string)
1680 {
1681 struct text_pos pos;
1682 eassert (STRINGP (string));
1683 eassert (charpos >= 0);
1684 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1685 return pos;
1686 }
1687
1688
1689 /* Value is a text position, i.e. character and byte position, for
1690 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1691 means recognize multibyte characters. */
1692
1693 static struct text_pos
1694 c_string_pos (ptrdiff_t charpos, const char *s, int multibyte_p)
1695 {
1696 struct text_pos pos;
1697
1698 eassert (s != NULL);
1699 eassert (charpos >= 0);
1700
1701 if (multibyte_p)
1702 {
1703 int len;
1704
1705 SET_TEXT_POS (pos, 0, 0);
1706 while (charpos--)
1707 {
1708 string_char_and_length ((const unsigned char *) s, &len);
1709 s += len;
1710 CHARPOS (pos) += 1;
1711 BYTEPOS (pos) += len;
1712 }
1713 }
1714 else
1715 SET_TEXT_POS (pos, charpos, charpos);
1716
1717 return pos;
1718 }
1719
1720
1721 /* Value is the number of characters in C string S. MULTIBYTE_P
1722 non-zero means recognize multibyte characters. */
1723
1724 static ptrdiff_t
1725 number_of_chars (const char *s, int multibyte_p)
1726 {
1727 ptrdiff_t nchars;
1728
1729 if (multibyte_p)
1730 {
1731 ptrdiff_t rest = strlen (s);
1732 int len;
1733 const unsigned char *p = (const unsigned char *) s;
1734
1735 for (nchars = 0; rest > 0; ++nchars)
1736 {
1737 string_char_and_length (p, &len);
1738 rest -= len, p += len;
1739 }
1740 }
1741 else
1742 nchars = strlen (s);
1743
1744 return nchars;
1745 }
1746
1747
1748 /* Compute byte position NEWPOS->bytepos corresponding to
1749 NEWPOS->charpos. POS is a known position in string STRING.
1750 NEWPOS->charpos must be >= POS.charpos. */
1751
1752 static void
1753 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1754 {
1755 eassert (STRINGP (string));
1756 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1757
1758 if (STRING_MULTIBYTE (string))
1759 *newpos = string_pos_nchars_ahead (pos, string,
1760 CHARPOS (*newpos) - CHARPOS (pos));
1761 else
1762 BYTEPOS (*newpos) = CHARPOS (*newpos);
1763 }
1764
1765 /* EXPORT:
1766 Return an estimation of the pixel height of mode or header lines on
1767 frame F. FACE_ID specifies what line's height to estimate. */
1768
1769 int
1770 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1771 {
1772 #ifdef HAVE_WINDOW_SYSTEM
1773 if (FRAME_WINDOW_P (f))
1774 {
1775 int height = FONT_HEIGHT (FRAME_FONT (f));
1776
1777 /* This function is called so early when Emacs starts that the face
1778 cache and mode line face are not yet initialized. */
1779 if (FRAME_FACE_CACHE (f))
1780 {
1781 struct face *face = FACE_FROM_ID (f, face_id);
1782 if (face)
1783 {
1784 if (face->font)
1785 height = FONT_HEIGHT (face->font);
1786 if (face->box_line_width > 0)
1787 height += 2 * face->box_line_width;
1788 }
1789 }
1790
1791 return height;
1792 }
1793 #endif
1794
1795 return 1;
1796 }
1797
1798 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1799 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1800 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1801 not force the value into range. */
1802
1803 void
1804 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1805 int *x, int *y, NativeRectangle *bounds, int noclip)
1806 {
1807
1808 #ifdef HAVE_WINDOW_SYSTEM
1809 if (FRAME_WINDOW_P (f))
1810 {
1811 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1812 even for negative values. */
1813 if (pix_x < 0)
1814 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1815 if (pix_y < 0)
1816 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1817
1818 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1819 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1820
1821 if (bounds)
1822 STORE_NATIVE_RECT (*bounds,
1823 FRAME_COL_TO_PIXEL_X (f, pix_x),
1824 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1825 FRAME_COLUMN_WIDTH (f) - 1,
1826 FRAME_LINE_HEIGHT (f) - 1);
1827
1828 if (!noclip)
1829 {
1830 if (pix_x < 0)
1831 pix_x = 0;
1832 else if (pix_x > FRAME_TOTAL_COLS (f))
1833 pix_x = FRAME_TOTAL_COLS (f);
1834
1835 if (pix_y < 0)
1836 pix_y = 0;
1837 else if (pix_y > FRAME_LINES (f))
1838 pix_y = FRAME_LINES (f);
1839 }
1840 }
1841 #endif
1842
1843 *x = pix_x;
1844 *y = pix_y;
1845 }
1846
1847
1848 /* Find the glyph under window-relative coordinates X/Y in window W.
1849 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1850 strings. Return in *HPOS and *VPOS the row and column number of
1851 the glyph found. Return in *AREA the glyph area containing X.
1852 Value is a pointer to the glyph found or null if X/Y is not on
1853 text, or we can't tell because W's current matrix is not up to
1854 date. */
1855
1856 static
1857 struct glyph *
1858 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1859 int *dx, int *dy, int *area)
1860 {
1861 struct glyph *glyph, *end;
1862 struct glyph_row *row = NULL;
1863 int x0, i;
1864
1865 /* Find row containing Y. Give up if some row is not enabled. */
1866 for (i = 0; i < w->current_matrix->nrows; ++i)
1867 {
1868 row = MATRIX_ROW (w->current_matrix, i);
1869 if (!row->enabled_p)
1870 return NULL;
1871 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1872 break;
1873 }
1874
1875 *vpos = i;
1876 *hpos = 0;
1877
1878 /* Give up if Y is not in the window. */
1879 if (i == w->current_matrix->nrows)
1880 return NULL;
1881
1882 /* Get the glyph area containing X. */
1883 if (w->pseudo_window_p)
1884 {
1885 *area = TEXT_AREA;
1886 x0 = 0;
1887 }
1888 else
1889 {
1890 if (x < window_box_left_offset (w, TEXT_AREA))
1891 {
1892 *area = LEFT_MARGIN_AREA;
1893 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1894 }
1895 else if (x < window_box_right_offset (w, TEXT_AREA))
1896 {
1897 *area = TEXT_AREA;
1898 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1899 }
1900 else
1901 {
1902 *area = RIGHT_MARGIN_AREA;
1903 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1904 }
1905 }
1906
1907 /* Find glyph containing X. */
1908 glyph = row->glyphs[*area];
1909 end = glyph + row->used[*area];
1910 x -= x0;
1911 while (glyph < end && x >= glyph->pixel_width)
1912 {
1913 x -= glyph->pixel_width;
1914 ++glyph;
1915 }
1916
1917 if (glyph == end)
1918 return NULL;
1919
1920 if (dx)
1921 {
1922 *dx = x;
1923 *dy = y - (row->y + row->ascent - glyph->ascent);
1924 }
1925
1926 *hpos = glyph - row->glyphs[*area];
1927 return glyph;
1928 }
1929
1930 /* Convert frame-relative x/y to coordinates relative to window W.
1931 Takes pseudo-windows into account. */
1932
1933 static void
1934 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1935 {
1936 if (w->pseudo_window_p)
1937 {
1938 /* A pseudo-window is always full-width, and starts at the
1939 left edge of the frame, plus a frame border. */
1940 struct frame *f = XFRAME (w->frame);
1941 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1942 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1943 }
1944 else
1945 {
1946 *x -= WINDOW_LEFT_EDGE_X (w);
1947 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1948 }
1949 }
1950
1951 #ifdef HAVE_WINDOW_SYSTEM
1952
1953 /* EXPORT:
1954 Return in RECTS[] at most N clipping rectangles for glyph string S.
1955 Return the number of stored rectangles. */
1956
1957 int
1958 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1959 {
1960 XRectangle r;
1961
1962 if (n <= 0)
1963 return 0;
1964
1965 if (s->row->full_width_p)
1966 {
1967 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1968 r.x = WINDOW_LEFT_EDGE_X (s->w);
1969 r.width = WINDOW_TOTAL_WIDTH (s->w);
1970
1971 /* Unless displaying a mode or menu bar line, which are always
1972 fully visible, clip to the visible part of the row. */
1973 if (s->w->pseudo_window_p)
1974 r.height = s->row->visible_height;
1975 else
1976 r.height = s->height;
1977 }
1978 else
1979 {
1980 /* This is a text line that may be partially visible. */
1981 r.x = window_box_left (s->w, s->area);
1982 r.width = window_box_width (s->w, s->area);
1983 r.height = s->row->visible_height;
1984 }
1985
1986 if (s->clip_head)
1987 if (r.x < s->clip_head->x)
1988 {
1989 if (r.width >= s->clip_head->x - r.x)
1990 r.width -= s->clip_head->x - r.x;
1991 else
1992 r.width = 0;
1993 r.x = s->clip_head->x;
1994 }
1995 if (s->clip_tail)
1996 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1997 {
1998 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1999 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2000 else
2001 r.width = 0;
2002 }
2003
2004 /* If S draws overlapping rows, it's sufficient to use the top and
2005 bottom of the window for clipping because this glyph string
2006 intentionally draws over other lines. */
2007 if (s->for_overlaps)
2008 {
2009 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2010 r.height = window_text_bottom_y (s->w) - r.y;
2011
2012 /* Alas, the above simple strategy does not work for the
2013 environments with anti-aliased text: if the same text is
2014 drawn onto the same place multiple times, it gets thicker.
2015 If the overlap we are processing is for the erased cursor, we
2016 take the intersection with the rectangle of the cursor. */
2017 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2018 {
2019 XRectangle rc, r_save = r;
2020
2021 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2022 rc.y = s->w->phys_cursor.y;
2023 rc.width = s->w->phys_cursor_width;
2024 rc.height = s->w->phys_cursor_height;
2025
2026 x_intersect_rectangles (&r_save, &rc, &r);
2027 }
2028 }
2029 else
2030 {
2031 /* Don't use S->y for clipping because it doesn't take partially
2032 visible lines into account. For example, it can be negative for
2033 partially visible lines at the top of a window. */
2034 if (!s->row->full_width_p
2035 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2036 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2037 else
2038 r.y = max (0, s->row->y);
2039 }
2040
2041 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2042
2043 /* If drawing the cursor, don't let glyph draw outside its
2044 advertised boundaries. Cleartype does this under some circumstances. */
2045 if (s->hl == DRAW_CURSOR)
2046 {
2047 struct glyph *glyph = s->first_glyph;
2048 int height, max_y;
2049
2050 if (s->x > r.x)
2051 {
2052 r.width -= s->x - r.x;
2053 r.x = s->x;
2054 }
2055 r.width = min (r.width, glyph->pixel_width);
2056
2057 /* If r.y is below window bottom, ensure that we still see a cursor. */
2058 height = min (glyph->ascent + glyph->descent,
2059 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2060 max_y = window_text_bottom_y (s->w) - height;
2061 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2062 if (s->ybase - glyph->ascent > max_y)
2063 {
2064 r.y = max_y;
2065 r.height = height;
2066 }
2067 else
2068 {
2069 /* Don't draw cursor glyph taller than our actual glyph. */
2070 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2071 if (height < r.height)
2072 {
2073 max_y = r.y + r.height;
2074 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2075 r.height = min (max_y - r.y, height);
2076 }
2077 }
2078 }
2079
2080 if (s->row->clip)
2081 {
2082 XRectangle r_save = r;
2083
2084 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2085 r.width = 0;
2086 }
2087
2088 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2089 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2090 {
2091 #ifdef CONVERT_FROM_XRECT
2092 CONVERT_FROM_XRECT (r, *rects);
2093 #else
2094 *rects = r;
2095 #endif
2096 return 1;
2097 }
2098 else
2099 {
2100 /* If we are processing overlapping and allowed to return
2101 multiple clipping rectangles, we exclude the row of the glyph
2102 string from the clipping rectangle. This is to avoid drawing
2103 the same text on the environment with anti-aliasing. */
2104 #ifdef CONVERT_FROM_XRECT
2105 XRectangle rs[2];
2106 #else
2107 XRectangle *rs = rects;
2108 #endif
2109 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2110
2111 if (s->for_overlaps & OVERLAPS_PRED)
2112 {
2113 rs[i] = r;
2114 if (r.y + r.height > row_y)
2115 {
2116 if (r.y < row_y)
2117 rs[i].height = row_y - r.y;
2118 else
2119 rs[i].height = 0;
2120 }
2121 i++;
2122 }
2123 if (s->for_overlaps & OVERLAPS_SUCC)
2124 {
2125 rs[i] = r;
2126 if (r.y < row_y + s->row->visible_height)
2127 {
2128 if (r.y + r.height > row_y + s->row->visible_height)
2129 {
2130 rs[i].y = row_y + s->row->visible_height;
2131 rs[i].height = r.y + r.height - rs[i].y;
2132 }
2133 else
2134 rs[i].height = 0;
2135 }
2136 i++;
2137 }
2138
2139 n = i;
2140 #ifdef CONVERT_FROM_XRECT
2141 for (i = 0; i < n; i++)
2142 CONVERT_FROM_XRECT (rs[i], rects[i]);
2143 #endif
2144 return n;
2145 }
2146 }
2147
2148 /* EXPORT:
2149 Return in *NR the clipping rectangle for glyph string S. */
2150
2151 void
2152 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2153 {
2154 get_glyph_string_clip_rects (s, nr, 1);
2155 }
2156
2157
2158 /* EXPORT:
2159 Return the position and height of the phys cursor in window W.
2160 Set w->phys_cursor_width to width of phys cursor.
2161 */
2162
2163 void
2164 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2165 struct glyph *glyph, int *xp, int *yp, int *heightp)
2166 {
2167 struct frame *f = XFRAME (WINDOW_FRAME (w));
2168 int x, y, wd, h, h0, y0;
2169
2170 /* Compute the width of the rectangle to draw. If on a stretch
2171 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2172 rectangle as wide as the glyph, but use a canonical character
2173 width instead. */
2174 wd = glyph->pixel_width - 1;
2175 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2176 wd++; /* Why? */
2177 #endif
2178
2179 x = w->phys_cursor.x;
2180 if (x < 0)
2181 {
2182 wd += x;
2183 x = 0;
2184 }
2185
2186 if (glyph->type == STRETCH_GLYPH
2187 && !x_stretch_cursor_p)
2188 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2189 w->phys_cursor_width = wd;
2190
2191 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2192
2193 /* If y is below window bottom, ensure that we still see a cursor. */
2194 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2195
2196 h = max (h0, glyph->ascent + glyph->descent);
2197 h0 = min (h0, glyph->ascent + glyph->descent);
2198
2199 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2200 if (y < y0)
2201 {
2202 h = max (h - (y0 - y) + 1, h0);
2203 y = y0 - 1;
2204 }
2205 else
2206 {
2207 y0 = window_text_bottom_y (w) - h0;
2208 if (y > y0)
2209 {
2210 h += y - y0;
2211 y = y0;
2212 }
2213 }
2214
2215 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2216 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2217 *heightp = h;
2218 }
2219
2220 /*
2221 * Remember which glyph the mouse is over.
2222 */
2223
2224 void
2225 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2226 {
2227 Lisp_Object window;
2228 struct window *w;
2229 struct glyph_row *r, *gr, *end_row;
2230 enum window_part part;
2231 enum glyph_row_area area;
2232 int x, y, width, height;
2233
2234 /* Try to determine frame pixel position and size of the glyph under
2235 frame pixel coordinates X/Y on frame F. */
2236
2237 if (!f->glyphs_initialized_p
2238 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2239 NILP (window)))
2240 {
2241 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2242 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2243 goto virtual_glyph;
2244 }
2245
2246 w = XWINDOW (window);
2247 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2248 height = WINDOW_FRAME_LINE_HEIGHT (w);
2249
2250 x = window_relative_x_coord (w, part, gx);
2251 y = gy - WINDOW_TOP_EDGE_Y (w);
2252
2253 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2254 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2255
2256 if (w->pseudo_window_p)
2257 {
2258 area = TEXT_AREA;
2259 part = ON_MODE_LINE; /* Don't adjust margin. */
2260 goto text_glyph;
2261 }
2262
2263 switch (part)
2264 {
2265 case ON_LEFT_MARGIN:
2266 area = LEFT_MARGIN_AREA;
2267 goto text_glyph;
2268
2269 case ON_RIGHT_MARGIN:
2270 area = RIGHT_MARGIN_AREA;
2271 goto text_glyph;
2272
2273 case ON_HEADER_LINE:
2274 case ON_MODE_LINE:
2275 gr = (part == ON_HEADER_LINE
2276 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2277 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2278 gy = gr->y;
2279 area = TEXT_AREA;
2280 goto text_glyph_row_found;
2281
2282 case ON_TEXT:
2283 area = TEXT_AREA;
2284
2285 text_glyph:
2286 gr = 0; gy = 0;
2287 for (; r <= end_row && r->enabled_p; ++r)
2288 if (r->y + r->height > y)
2289 {
2290 gr = r; gy = r->y;
2291 break;
2292 }
2293
2294 text_glyph_row_found:
2295 if (gr && gy <= y)
2296 {
2297 struct glyph *g = gr->glyphs[area];
2298 struct glyph *end = g + gr->used[area];
2299
2300 height = gr->height;
2301 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2302 if (gx + g->pixel_width > x)
2303 break;
2304
2305 if (g < end)
2306 {
2307 if (g->type == IMAGE_GLYPH)
2308 {
2309 /* Don't remember when mouse is over image, as
2310 image may have hot-spots. */
2311 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2312 return;
2313 }
2314 width = g->pixel_width;
2315 }
2316 else
2317 {
2318 /* Use nominal char spacing at end of line. */
2319 x -= gx;
2320 gx += (x / width) * width;
2321 }
2322
2323 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2324 gx += window_box_left_offset (w, area);
2325 }
2326 else
2327 {
2328 /* Use nominal line height at end of window. */
2329 gx = (x / width) * width;
2330 y -= gy;
2331 gy += (y / height) * height;
2332 }
2333 break;
2334
2335 case ON_LEFT_FRINGE:
2336 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2337 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2338 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2339 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2340 goto row_glyph;
2341
2342 case ON_RIGHT_FRINGE:
2343 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2344 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2345 : window_box_right_offset (w, TEXT_AREA));
2346 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2347 goto row_glyph;
2348
2349 case ON_SCROLL_BAR:
2350 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2351 ? 0
2352 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2353 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2354 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2355 : 0)));
2356 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2357
2358 row_glyph:
2359 gr = 0, gy = 0;
2360 for (; r <= end_row && r->enabled_p; ++r)
2361 if (r->y + r->height > y)
2362 {
2363 gr = r; gy = r->y;
2364 break;
2365 }
2366
2367 if (gr && gy <= y)
2368 height = gr->height;
2369 else
2370 {
2371 /* Use nominal line height at end of window. */
2372 y -= gy;
2373 gy += (y / height) * height;
2374 }
2375 break;
2376
2377 default:
2378 ;
2379 virtual_glyph:
2380 /* If there is no glyph under the mouse, then we divide the screen
2381 into a grid of the smallest glyph in the frame, and use that
2382 as our "glyph". */
2383
2384 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2385 round down even for negative values. */
2386 if (gx < 0)
2387 gx -= width - 1;
2388 if (gy < 0)
2389 gy -= height - 1;
2390
2391 gx = (gx / width) * width;
2392 gy = (gy / height) * height;
2393
2394 goto store_rect;
2395 }
2396
2397 gx += WINDOW_LEFT_EDGE_X (w);
2398 gy += WINDOW_TOP_EDGE_Y (w);
2399
2400 store_rect:
2401 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2402
2403 /* Visible feedback for debugging. */
2404 #if 0
2405 #if HAVE_X_WINDOWS
2406 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2407 f->output_data.x->normal_gc,
2408 gx, gy, width, height);
2409 #endif
2410 #endif
2411 }
2412
2413
2414 #endif /* HAVE_WINDOW_SYSTEM */
2415
2416 \f
2417 /***********************************************************************
2418 Lisp form evaluation
2419 ***********************************************************************/
2420
2421 /* Error handler for safe_eval and safe_call. */
2422
2423 static Lisp_Object
2424 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2425 {
2426 add_to_log ("Error during redisplay: %S signaled %S",
2427 Flist (nargs, args), arg);
2428 return Qnil;
2429 }
2430
2431 /* Call function FUNC with the rest of NARGS - 1 arguments
2432 following. Return the result, or nil if something went
2433 wrong. Prevent redisplay during the evaluation. */
2434
2435 Lisp_Object
2436 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2437 {
2438 Lisp_Object val;
2439
2440 if (inhibit_eval_during_redisplay)
2441 val = Qnil;
2442 else
2443 {
2444 va_list ap;
2445 ptrdiff_t i;
2446 ptrdiff_t count = SPECPDL_INDEX ();
2447 struct gcpro gcpro1;
2448 Lisp_Object *args = alloca (nargs * word_size);
2449
2450 args[0] = func;
2451 va_start (ap, func);
2452 for (i = 1; i < nargs; i++)
2453 args[i] = va_arg (ap, Lisp_Object);
2454 va_end (ap);
2455
2456 GCPRO1 (args[0]);
2457 gcpro1.nvars = nargs;
2458 specbind (Qinhibit_redisplay, Qt);
2459 /* Use Qt to ensure debugger does not run,
2460 so there is no possibility of wanting to redisplay. */
2461 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2462 safe_eval_handler);
2463 UNGCPRO;
2464 val = unbind_to (count, val);
2465 }
2466
2467 return val;
2468 }
2469
2470
2471 /* Call function FN with one argument ARG.
2472 Return the result, or nil if something went wrong. */
2473
2474 Lisp_Object
2475 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2476 {
2477 return safe_call (2, fn, arg);
2478 }
2479
2480 static Lisp_Object Qeval;
2481
2482 Lisp_Object
2483 safe_eval (Lisp_Object sexpr)
2484 {
2485 return safe_call1 (Qeval, sexpr);
2486 }
2487
2488 /* Call function FN with two arguments ARG1 and ARG2.
2489 Return the result, or nil if something went wrong. */
2490
2491 Lisp_Object
2492 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2493 {
2494 return safe_call (3, fn, arg1, arg2);
2495 }
2496
2497
2498 \f
2499 /***********************************************************************
2500 Debugging
2501 ***********************************************************************/
2502
2503 #if 0
2504
2505 /* Define CHECK_IT to perform sanity checks on iterators.
2506 This is for debugging. It is too slow to do unconditionally. */
2507
2508 static void
2509 check_it (struct it *it)
2510 {
2511 if (it->method == GET_FROM_STRING)
2512 {
2513 eassert (STRINGP (it->string));
2514 eassert (IT_STRING_CHARPOS (*it) >= 0);
2515 }
2516 else
2517 {
2518 eassert (IT_STRING_CHARPOS (*it) < 0);
2519 if (it->method == GET_FROM_BUFFER)
2520 {
2521 /* Check that character and byte positions agree. */
2522 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2523 }
2524 }
2525
2526 if (it->dpvec)
2527 eassert (it->current.dpvec_index >= 0);
2528 else
2529 eassert (it->current.dpvec_index < 0);
2530 }
2531
2532 #define CHECK_IT(IT) check_it ((IT))
2533
2534 #else /* not 0 */
2535
2536 #define CHECK_IT(IT) (void) 0
2537
2538 #endif /* not 0 */
2539
2540
2541 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2542
2543 /* Check that the window end of window W is what we expect it
2544 to be---the last row in the current matrix displaying text. */
2545
2546 static void
2547 check_window_end (struct window *w)
2548 {
2549 if (!MINI_WINDOW_P (w)
2550 && !NILP (w->window_end_valid))
2551 {
2552 struct glyph_row *row;
2553 eassert ((row = MATRIX_ROW (w->current_matrix,
2554 XFASTINT (w->window_end_vpos)),
2555 !row->enabled_p
2556 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2557 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2558 }
2559 }
2560
2561 #define CHECK_WINDOW_END(W) check_window_end ((W))
2562
2563 #else
2564
2565 #define CHECK_WINDOW_END(W) (void) 0
2566
2567 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2568
2569
2570 \f
2571 /***********************************************************************
2572 Iterator initialization
2573 ***********************************************************************/
2574
2575 /* Initialize IT for displaying current_buffer in window W, starting
2576 at character position CHARPOS. CHARPOS < 0 means that no buffer
2577 position is specified which is useful when the iterator is assigned
2578 a position later. BYTEPOS is the byte position corresponding to
2579 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2580
2581 If ROW is not null, calls to produce_glyphs with IT as parameter
2582 will produce glyphs in that row.
2583
2584 BASE_FACE_ID is the id of a base face to use. It must be one of
2585 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2586 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2587 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2588
2589 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2590 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2591 will be initialized to use the corresponding mode line glyph row of
2592 the desired matrix of W. */
2593
2594 void
2595 init_iterator (struct it *it, struct window *w,
2596 ptrdiff_t charpos, ptrdiff_t bytepos,
2597 struct glyph_row *row, enum face_id base_face_id)
2598 {
2599 int highlight_region_p;
2600 enum face_id remapped_base_face_id = base_face_id;
2601
2602 /* Some precondition checks. */
2603 eassert (w != NULL && it != NULL);
2604 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2605 && charpos <= ZV));
2606
2607 /* If face attributes have been changed since the last redisplay,
2608 free realized faces now because they depend on face definitions
2609 that might have changed. Don't free faces while there might be
2610 desired matrices pending which reference these faces. */
2611 if (face_change_count && !inhibit_free_realized_faces)
2612 {
2613 face_change_count = 0;
2614 free_all_realized_faces (Qnil);
2615 }
2616
2617 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2618 if (! NILP (Vface_remapping_alist))
2619 remapped_base_face_id
2620 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2621
2622 /* Use one of the mode line rows of W's desired matrix if
2623 appropriate. */
2624 if (row == NULL)
2625 {
2626 if (base_face_id == MODE_LINE_FACE_ID
2627 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2628 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2629 else if (base_face_id == HEADER_LINE_FACE_ID)
2630 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2631 }
2632
2633 /* Clear IT. */
2634 memset (it, 0, sizeof *it);
2635 it->current.overlay_string_index = -1;
2636 it->current.dpvec_index = -1;
2637 it->base_face_id = remapped_base_face_id;
2638 it->string = Qnil;
2639 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2640 it->paragraph_embedding = L2R;
2641 it->bidi_it.string.lstring = Qnil;
2642 it->bidi_it.string.s = NULL;
2643 it->bidi_it.string.bufpos = 0;
2644
2645 /* The window in which we iterate over current_buffer: */
2646 XSETWINDOW (it->window, w);
2647 it->w = w;
2648 it->f = XFRAME (w->frame);
2649
2650 it->cmp_it.id = -1;
2651
2652 /* Extra space between lines (on window systems only). */
2653 if (base_face_id == DEFAULT_FACE_ID
2654 && FRAME_WINDOW_P (it->f))
2655 {
2656 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2657 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2658 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2659 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2660 * FRAME_LINE_HEIGHT (it->f));
2661 else if (it->f->extra_line_spacing > 0)
2662 it->extra_line_spacing = it->f->extra_line_spacing;
2663 it->max_extra_line_spacing = 0;
2664 }
2665
2666 /* If realized faces have been removed, e.g. because of face
2667 attribute changes of named faces, recompute them. When running
2668 in batch mode, the face cache of the initial frame is null. If
2669 we happen to get called, make a dummy face cache. */
2670 if (FRAME_FACE_CACHE (it->f) == NULL)
2671 init_frame_faces (it->f);
2672 if (FRAME_FACE_CACHE (it->f)->used == 0)
2673 recompute_basic_faces (it->f);
2674
2675 /* Current value of the `slice', `space-width', and 'height' properties. */
2676 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2677 it->space_width = Qnil;
2678 it->font_height = Qnil;
2679 it->override_ascent = -1;
2680
2681 /* Are control characters displayed as `^C'? */
2682 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2683
2684 /* -1 means everything between a CR and the following line end
2685 is invisible. >0 means lines indented more than this value are
2686 invisible. */
2687 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2688 ? (clip_to_bounds
2689 (-1, XINT (BVAR (current_buffer, selective_display)),
2690 PTRDIFF_MAX))
2691 : (!NILP (BVAR (current_buffer, selective_display))
2692 ? -1 : 0));
2693 it->selective_display_ellipsis_p
2694 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2695
2696 /* Display table to use. */
2697 it->dp = window_display_table (w);
2698
2699 /* Are multibyte characters enabled in current_buffer? */
2700 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2701
2702 /* Non-zero if we should highlight the region. */
2703 highlight_region_p
2704 = (!NILP (Vtransient_mark_mode)
2705 && !NILP (BVAR (current_buffer, mark_active))
2706 && XMARKER (BVAR (current_buffer, mark))->buffer != 0);
2707
2708 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2709 start and end of a visible region in window IT->w. Set both to
2710 -1 to indicate no region. */
2711 if (highlight_region_p
2712 /* Maybe highlight only in selected window. */
2713 && (/* Either show region everywhere. */
2714 highlight_nonselected_windows
2715 /* Or show region in the selected window. */
2716 || w == XWINDOW (selected_window)
2717 /* Or show the region if we are in the mini-buffer and W is
2718 the window the mini-buffer refers to. */
2719 || (MINI_WINDOW_P (XWINDOW (selected_window))
2720 && WINDOWP (minibuf_selected_window)
2721 && w == XWINDOW (minibuf_selected_window))))
2722 {
2723 ptrdiff_t markpos = marker_position (BVAR (current_buffer, mark));
2724 it->region_beg_charpos = min (PT, markpos);
2725 it->region_end_charpos = max (PT, markpos);
2726 }
2727 else
2728 it->region_beg_charpos = it->region_end_charpos = -1;
2729
2730 /* Get the position at which the redisplay_end_trigger hook should
2731 be run, if it is to be run at all. */
2732 if (MARKERP (w->redisplay_end_trigger)
2733 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2734 it->redisplay_end_trigger_charpos
2735 = marker_position (w->redisplay_end_trigger);
2736 else if (INTEGERP (w->redisplay_end_trigger))
2737 it->redisplay_end_trigger_charpos =
2738 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2739
2740 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2741
2742 /* Are lines in the display truncated? */
2743 if (base_face_id != DEFAULT_FACE_ID
2744 || it->w->hscroll
2745 || (! WINDOW_FULL_WIDTH_P (it->w)
2746 && ((!NILP (Vtruncate_partial_width_windows)
2747 && !INTEGERP (Vtruncate_partial_width_windows))
2748 || (INTEGERP (Vtruncate_partial_width_windows)
2749 && (WINDOW_TOTAL_COLS (it->w)
2750 < XINT (Vtruncate_partial_width_windows))))))
2751 it->line_wrap = TRUNCATE;
2752 else if (NILP (BVAR (current_buffer, truncate_lines)))
2753 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2754 ? WINDOW_WRAP : WORD_WRAP;
2755 else
2756 it->line_wrap = TRUNCATE;
2757
2758 /* Get dimensions of truncation and continuation glyphs. These are
2759 displayed as fringe bitmaps under X, but we need them for such
2760 frames when the fringes are turned off. But leave the dimensions
2761 zero for tooltip frames, as these glyphs look ugly there and also
2762 sabotage calculations of tooltip dimensions in x-show-tip. */
2763 #ifdef HAVE_WINDOW_SYSTEM
2764 if (!(FRAME_WINDOW_P (it->f)
2765 && FRAMEP (tip_frame)
2766 && it->f == XFRAME (tip_frame)))
2767 #endif
2768 {
2769 if (it->line_wrap == TRUNCATE)
2770 {
2771 /* We will need the truncation glyph. */
2772 eassert (it->glyph_row == NULL);
2773 produce_special_glyphs (it, IT_TRUNCATION);
2774 it->truncation_pixel_width = it->pixel_width;
2775 }
2776 else
2777 {
2778 /* We will need the continuation glyph. */
2779 eassert (it->glyph_row == NULL);
2780 produce_special_glyphs (it, IT_CONTINUATION);
2781 it->continuation_pixel_width = it->pixel_width;
2782 }
2783 }
2784
2785 /* Reset these values to zero because the produce_special_glyphs
2786 above has changed them. */
2787 it->pixel_width = it->ascent = it->descent = 0;
2788 it->phys_ascent = it->phys_descent = 0;
2789
2790 /* Set this after getting the dimensions of truncation and
2791 continuation glyphs, so that we don't produce glyphs when calling
2792 produce_special_glyphs, above. */
2793 it->glyph_row = row;
2794 it->area = TEXT_AREA;
2795
2796 /* Forget any previous info about this row being reversed. */
2797 if (it->glyph_row)
2798 it->glyph_row->reversed_p = 0;
2799
2800 /* Get the dimensions of the display area. The display area
2801 consists of the visible window area plus a horizontally scrolled
2802 part to the left of the window. All x-values are relative to the
2803 start of this total display area. */
2804 if (base_face_id != DEFAULT_FACE_ID)
2805 {
2806 /* Mode lines, menu bar in terminal frames. */
2807 it->first_visible_x = 0;
2808 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2809 }
2810 else
2811 {
2812 it->first_visible_x =
2813 window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2814 it->last_visible_x = (it->first_visible_x
2815 + window_box_width (w, TEXT_AREA));
2816
2817 /* If we truncate lines, leave room for the truncation glyph(s) at
2818 the right margin. Otherwise, leave room for the continuation
2819 glyph(s). Done only if the window has no fringes. Since we
2820 don't know at this point whether there will be any R2L lines in
2821 the window, we reserve space for truncation/continuation glyphs
2822 even if only one of the fringes is absent. */
2823 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2824 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2825 {
2826 if (it->line_wrap == TRUNCATE)
2827 it->last_visible_x -= it->truncation_pixel_width;
2828 else
2829 it->last_visible_x -= it->continuation_pixel_width;
2830 }
2831
2832 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2833 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2834 }
2835
2836 /* Leave room for a border glyph. */
2837 if (!FRAME_WINDOW_P (it->f)
2838 && !WINDOW_RIGHTMOST_P (it->w))
2839 it->last_visible_x -= 1;
2840
2841 it->last_visible_y = window_text_bottom_y (w);
2842
2843 /* For mode lines and alike, arrange for the first glyph having a
2844 left box line if the face specifies a box. */
2845 if (base_face_id != DEFAULT_FACE_ID)
2846 {
2847 struct face *face;
2848
2849 it->face_id = remapped_base_face_id;
2850
2851 /* If we have a boxed mode line, make the first character appear
2852 with a left box line. */
2853 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2854 if (face->box != FACE_NO_BOX)
2855 it->start_of_box_run_p = 1;
2856 }
2857
2858 /* If a buffer position was specified, set the iterator there,
2859 getting overlays and face properties from that position. */
2860 if (charpos >= BUF_BEG (current_buffer))
2861 {
2862 it->end_charpos = ZV;
2863 IT_CHARPOS (*it) = charpos;
2864
2865 /* We will rely on `reseat' to set this up properly, via
2866 handle_face_prop. */
2867 it->face_id = it->base_face_id;
2868
2869 /* Compute byte position if not specified. */
2870 if (bytepos < charpos)
2871 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2872 else
2873 IT_BYTEPOS (*it) = bytepos;
2874
2875 it->start = it->current;
2876 /* Do we need to reorder bidirectional text? Not if this is a
2877 unibyte buffer: by definition, none of the single-byte
2878 characters are strong R2L, so no reordering is needed. And
2879 bidi.c doesn't support unibyte buffers anyway. Also, don't
2880 reorder while we are loading loadup.el, since the tables of
2881 character properties needed for reordering are not yet
2882 available. */
2883 it->bidi_p =
2884 NILP (Vpurify_flag)
2885 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2886 && it->multibyte_p;
2887
2888 /* If we are to reorder bidirectional text, init the bidi
2889 iterator. */
2890 if (it->bidi_p)
2891 {
2892 /* Note the paragraph direction that this buffer wants to
2893 use. */
2894 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2895 Qleft_to_right))
2896 it->paragraph_embedding = L2R;
2897 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2898 Qright_to_left))
2899 it->paragraph_embedding = R2L;
2900 else
2901 it->paragraph_embedding = NEUTRAL_DIR;
2902 bidi_unshelve_cache (NULL, 0);
2903 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2904 &it->bidi_it);
2905 }
2906
2907 /* Compute faces etc. */
2908 reseat (it, it->current.pos, 1);
2909 }
2910
2911 CHECK_IT (it);
2912 }
2913
2914
2915 /* Initialize IT for the display of window W with window start POS. */
2916
2917 void
2918 start_display (struct it *it, struct window *w, struct text_pos pos)
2919 {
2920 struct glyph_row *row;
2921 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2922
2923 row = w->desired_matrix->rows + first_vpos;
2924 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2925 it->first_vpos = first_vpos;
2926
2927 /* Don't reseat to previous visible line start if current start
2928 position is in a string or image. */
2929 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2930 {
2931 int start_at_line_beg_p;
2932 int first_y = it->current_y;
2933
2934 /* If window start is not at a line start, skip forward to POS to
2935 get the correct continuation lines width. */
2936 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2937 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2938 if (!start_at_line_beg_p)
2939 {
2940 int new_x;
2941
2942 reseat_at_previous_visible_line_start (it);
2943 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2944
2945 new_x = it->current_x + it->pixel_width;
2946
2947 /* If lines are continued, this line may end in the middle
2948 of a multi-glyph character (e.g. a control character
2949 displayed as \003, or in the middle of an overlay
2950 string). In this case move_it_to above will not have
2951 taken us to the start of the continuation line but to the
2952 end of the continued line. */
2953 if (it->current_x > 0
2954 && it->line_wrap != TRUNCATE /* Lines are continued. */
2955 && (/* And glyph doesn't fit on the line. */
2956 new_x > it->last_visible_x
2957 /* Or it fits exactly and we're on a window
2958 system frame. */
2959 || (new_x == it->last_visible_x
2960 && FRAME_WINDOW_P (it->f)
2961 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
2962 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
2963 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
2964 {
2965 if ((it->current.dpvec_index >= 0
2966 || it->current.overlay_string_index >= 0)
2967 /* If we are on a newline from a display vector or
2968 overlay string, then we are already at the end of
2969 a screen line; no need to go to the next line in
2970 that case, as this line is not really continued.
2971 (If we do go to the next line, C-e will not DTRT.) */
2972 && it->c != '\n')
2973 {
2974 set_iterator_to_next (it, 1);
2975 move_it_in_display_line_to (it, -1, -1, 0);
2976 }
2977
2978 it->continuation_lines_width += it->current_x;
2979 }
2980 /* If the character at POS is displayed via a display
2981 vector, move_it_to above stops at the final glyph of
2982 IT->dpvec. To make the caller redisplay that character
2983 again (a.k.a. start at POS), we need to reset the
2984 dpvec_index to the beginning of IT->dpvec. */
2985 else if (it->current.dpvec_index >= 0)
2986 it->current.dpvec_index = 0;
2987
2988 /* We're starting a new display line, not affected by the
2989 height of the continued line, so clear the appropriate
2990 fields in the iterator structure. */
2991 it->max_ascent = it->max_descent = 0;
2992 it->max_phys_ascent = it->max_phys_descent = 0;
2993
2994 it->current_y = first_y;
2995 it->vpos = 0;
2996 it->current_x = it->hpos = 0;
2997 }
2998 }
2999 }
3000
3001
3002 /* Return 1 if POS is a position in ellipses displayed for invisible
3003 text. W is the window we display, for text property lookup. */
3004
3005 static int
3006 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3007 {
3008 Lisp_Object prop, window;
3009 int ellipses_p = 0;
3010 ptrdiff_t charpos = CHARPOS (pos->pos);
3011
3012 /* If POS specifies a position in a display vector, this might
3013 be for an ellipsis displayed for invisible text. We won't
3014 get the iterator set up for delivering that ellipsis unless
3015 we make sure that it gets aware of the invisible text. */
3016 if (pos->dpvec_index >= 0
3017 && pos->overlay_string_index < 0
3018 && CHARPOS (pos->string_pos) < 0
3019 && charpos > BEGV
3020 && (XSETWINDOW (window, w),
3021 prop = Fget_char_property (make_number (charpos),
3022 Qinvisible, window),
3023 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3024 {
3025 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3026 window);
3027 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3028 }
3029
3030 return ellipses_p;
3031 }
3032
3033
3034 /* Initialize IT for stepping through current_buffer in window W,
3035 starting at position POS that includes overlay string and display
3036 vector/ control character translation position information. Value
3037 is zero if there are overlay strings with newlines at POS. */
3038
3039 static int
3040 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3041 {
3042 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3043 int i, overlay_strings_with_newlines = 0;
3044
3045 /* If POS specifies a position in a display vector, this might
3046 be for an ellipsis displayed for invisible text. We won't
3047 get the iterator set up for delivering that ellipsis unless
3048 we make sure that it gets aware of the invisible text. */
3049 if (in_ellipses_for_invisible_text_p (pos, w))
3050 {
3051 --charpos;
3052 bytepos = 0;
3053 }
3054
3055 /* Keep in mind: the call to reseat in init_iterator skips invisible
3056 text, so we might end up at a position different from POS. This
3057 is only a problem when POS is a row start after a newline and an
3058 overlay starts there with an after-string, and the overlay has an
3059 invisible property. Since we don't skip invisible text in
3060 display_line and elsewhere immediately after consuming the
3061 newline before the row start, such a POS will not be in a string,
3062 but the call to init_iterator below will move us to the
3063 after-string. */
3064 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3065
3066 /* This only scans the current chunk -- it should scan all chunks.
3067 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3068 to 16 in 22.1 to make this a lesser problem. */
3069 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3070 {
3071 const char *s = SSDATA (it->overlay_strings[i]);
3072 const char *e = s + SBYTES (it->overlay_strings[i]);
3073
3074 while (s < e && *s != '\n')
3075 ++s;
3076
3077 if (s < e)
3078 {
3079 overlay_strings_with_newlines = 1;
3080 break;
3081 }
3082 }
3083
3084 /* If position is within an overlay string, set up IT to the right
3085 overlay string. */
3086 if (pos->overlay_string_index >= 0)
3087 {
3088 int relative_index;
3089
3090 /* If the first overlay string happens to have a `display'
3091 property for an image, the iterator will be set up for that
3092 image, and we have to undo that setup first before we can
3093 correct the overlay string index. */
3094 if (it->method == GET_FROM_IMAGE)
3095 pop_it (it);
3096
3097 /* We already have the first chunk of overlay strings in
3098 IT->overlay_strings. Load more until the one for
3099 pos->overlay_string_index is in IT->overlay_strings. */
3100 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3101 {
3102 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3103 it->current.overlay_string_index = 0;
3104 while (n--)
3105 {
3106 load_overlay_strings (it, 0);
3107 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3108 }
3109 }
3110
3111 it->current.overlay_string_index = pos->overlay_string_index;
3112 relative_index = (it->current.overlay_string_index
3113 % OVERLAY_STRING_CHUNK_SIZE);
3114 it->string = it->overlay_strings[relative_index];
3115 eassert (STRINGP (it->string));
3116 it->current.string_pos = pos->string_pos;
3117 it->method = GET_FROM_STRING;
3118 it->end_charpos = SCHARS (it->string);
3119 /* Set up the bidi iterator for this overlay string. */
3120 if (it->bidi_p)
3121 {
3122 it->bidi_it.string.lstring = it->string;
3123 it->bidi_it.string.s = NULL;
3124 it->bidi_it.string.schars = SCHARS (it->string);
3125 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3126 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3127 it->bidi_it.string.unibyte = !it->multibyte_p;
3128 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3129 FRAME_WINDOW_P (it->f), &it->bidi_it);
3130
3131 /* Synchronize the state of the bidi iterator with
3132 pos->string_pos. For any string position other than
3133 zero, this will be done automagically when we resume
3134 iteration over the string and get_visually_first_element
3135 is called. But if string_pos is zero, and the string is
3136 to be reordered for display, we need to resync manually,
3137 since it could be that the iteration state recorded in
3138 pos ended at string_pos of 0 moving backwards in string. */
3139 if (CHARPOS (pos->string_pos) == 0)
3140 {
3141 get_visually_first_element (it);
3142 if (IT_STRING_CHARPOS (*it) != 0)
3143 do {
3144 /* Paranoia. */
3145 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3146 bidi_move_to_visually_next (&it->bidi_it);
3147 } while (it->bidi_it.charpos != 0);
3148 }
3149 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3150 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3151 }
3152 }
3153
3154 if (CHARPOS (pos->string_pos) >= 0)
3155 {
3156 /* Recorded position is not in an overlay string, but in another
3157 string. This can only be a string from a `display' property.
3158 IT should already be filled with that string. */
3159 it->current.string_pos = pos->string_pos;
3160 eassert (STRINGP (it->string));
3161 if (it->bidi_p)
3162 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3163 FRAME_WINDOW_P (it->f), &it->bidi_it);
3164 }
3165
3166 /* Restore position in display vector translations, control
3167 character translations or ellipses. */
3168 if (pos->dpvec_index >= 0)
3169 {
3170 if (it->dpvec == NULL)
3171 get_next_display_element (it);
3172 eassert (it->dpvec && it->current.dpvec_index == 0);
3173 it->current.dpvec_index = pos->dpvec_index;
3174 }
3175
3176 CHECK_IT (it);
3177 return !overlay_strings_with_newlines;
3178 }
3179
3180
3181 /* Initialize IT for stepping through current_buffer in window W
3182 starting at ROW->start. */
3183
3184 static void
3185 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3186 {
3187 init_from_display_pos (it, w, &row->start);
3188 it->start = row->start;
3189 it->continuation_lines_width = row->continuation_lines_width;
3190 CHECK_IT (it);
3191 }
3192
3193
3194 /* Initialize IT for stepping through current_buffer in window W
3195 starting in the line following ROW, i.e. starting at ROW->end.
3196 Value is zero if there are overlay strings with newlines at ROW's
3197 end position. */
3198
3199 static int
3200 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3201 {
3202 int success = 0;
3203
3204 if (init_from_display_pos (it, w, &row->end))
3205 {
3206 if (row->continued_p)
3207 it->continuation_lines_width
3208 = row->continuation_lines_width + row->pixel_width;
3209 CHECK_IT (it);
3210 success = 1;
3211 }
3212
3213 return success;
3214 }
3215
3216
3217
3218 \f
3219 /***********************************************************************
3220 Text properties
3221 ***********************************************************************/
3222
3223 /* Called when IT reaches IT->stop_charpos. Handle text property and
3224 overlay changes. Set IT->stop_charpos to the next position where
3225 to stop. */
3226
3227 static void
3228 handle_stop (struct it *it)
3229 {
3230 enum prop_handled handled;
3231 int handle_overlay_change_p;
3232 struct props *p;
3233
3234 it->dpvec = NULL;
3235 it->current.dpvec_index = -1;
3236 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3237 it->ignore_overlay_strings_at_pos_p = 0;
3238 it->ellipsis_p = 0;
3239
3240 /* Use face of preceding text for ellipsis (if invisible) */
3241 if (it->selective_display_ellipsis_p)
3242 it->saved_face_id = it->face_id;
3243
3244 do
3245 {
3246 handled = HANDLED_NORMALLY;
3247
3248 /* Call text property handlers. */
3249 for (p = it_props; p->handler; ++p)
3250 {
3251 handled = p->handler (it);
3252
3253 if (handled == HANDLED_RECOMPUTE_PROPS)
3254 break;
3255 else if (handled == HANDLED_RETURN)
3256 {
3257 /* We still want to show before and after strings from
3258 overlays even if the actual buffer text is replaced. */
3259 if (!handle_overlay_change_p
3260 || it->sp > 1
3261 /* Don't call get_overlay_strings_1 if we already
3262 have overlay strings loaded, because doing so
3263 will load them again and push the iterator state
3264 onto the stack one more time, which is not
3265 expected by the rest of the code that processes
3266 overlay strings. */
3267 || (it->current.overlay_string_index < 0
3268 ? !get_overlay_strings_1 (it, 0, 0)
3269 : 0))
3270 {
3271 if (it->ellipsis_p)
3272 setup_for_ellipsis (it, 0);
3273 /* When handling a display spec, we might load an
3274 empty string. In that case, discard it here. We
3275 used to discard it in handle_single_display_spec,
3276 but that causes get_overlay_strings_1, above, to
3277 ignore overlay strings that we must check. */
3278 if (STRINGP (it->string) && !SCHARS (it->string))
3279 pop_it (it);
3280 return;
3281 }
3282 else if (STRINGP (it->string) && !SCHARS (it->string))
3283 pop_it (it);
3284 else
3285 {
3286 it->ignore_overlay_strings_at_pos_p = 1;
3287 it->string_from_display_prop_p = 0;
3288 it->from_disp_prop_p = 0;
3289 handle_overlay_change_p = 0;
3290 }
3291 handled = HANDLED_RECOMPUTE_PROPS;
3292 break;
3293 }
3294 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3295 handle_overlay_change_p = 0;
3296 }
3297
3298 if (handled != HANDLED_RECOMPUTE_PROPS)
3299 {
3300 /* Don't check for overlay strings below when set to deliver
3301 characters from a display vector. */
3302 if (it->method == GET_FROM_DISPLAY_VECTOR)
3303 handle_overlay_change_p = 0;
3304
3305 /* Handle overlay changes.
3306 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3307 if it finds overlays. */
3308 if (handle_overlay_change_p)
3309 handled = handle_overlay_change (it);
3310 }
3311
3312 if (it->ellipsis_p)
3313 {
3314 setup_for_ellipsis (it, 0);
3315 break;
3316 }
3317 }
3318 while (handled == HANDLED_RECOMPUTE_PROPS);
3319
3320 /* Determine where to stop next. */
3321 if (handled == HANDLED_NORMALLY)
3322 compute_stop_pos (it);
3323 }
3324
3325
3326 /* Compute IT->stop_charpos from text property and overlay change
3327 information for IT's current position. */
3328
3329 static void
3330 compute_stop_pos (struct it *it)
3331 {
3332 register INTERVAL iv, next_iv;
3333 Lisp_Object object, limit, position;
3334 ptrdiff_t charpos, bytepos;
3335
3336 if (STRINGP (it->string))
3337 {
3338 /* Strings are usually short, so don't limit the search for
3339 properties. */
3340 it->stop_charpos = it->end_charpos;
3341 object = it->string;
3342 limit = Qnil;
3343 charpos = IT_STRING_CHARPOS (*it);
3344 bytepos = IT_STRING_BYTEPOS (*it);
3345 }
3346 else
3347 {
3348 ptrdiff_t pos;
3349
3350 /* If end_charpos is out of range for some reason, such as a
3351 misbehaving display function, rationalize it (Bug#5984). */
3352 if (it->end_charpos > ZV)
3353 it->end_charpos = ZV;
3354 it->stop_charpos = it->end_charpos;
3355
3356 /* If next overlay change is in front of the current stop pos
3357 (which is IT->end_charpos), stop there. Note: value of
3358 next_overlay_change is point-max if no overlay change
3359 follows. */
3360 charpos = IT_CHARPOS (*it);
3361 bytepos = IT_BYTEPOS (*it);
3362 pos = next_overlay_change (charpos);
3363 if (pos < it->stop_charpos)
3364 it->stop_charpos = pos;
3365
3366 /* If showing the region, we have to stop at the region
3367 start or end because the face might change there. */
3368 if (it->region_beg_charpos > 0)
3369 {
3370 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3371 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3372 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3373 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3374 }
3375
3376 /* Set up variables for computing the stop position from text
3377 property changes. */
3378 XSETBUFFER (object, current_buffer);
3379 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3380 }
3381
3382 /* Get the interval containing IT's position. Value is a null
3383 interval if there isn't such an interval. */
3384 position = make_number (charpos);
3385 iv = validate_interval_range (object, &position, &position, 0);
3386 if (iv)
3387 {
3388 Lisp_Object values_here[LAST_PROP_IDX];
3389 struct props *p;
3390
3391 /* Get properties here. */
3392 for (p = it_props; p->handler; ++p)
3393 values_here[p->idx] = textget (iv->plist, *p->name);
3394
3395 /* Look for an interval following iv that has different
3396 properties. */
3397 for (next_iv = next_interval (iv);
3398 (next_iv
3399 && (NILP (limit)
3400 || XFASTINT (limit) > next_iv->position));
3401 next_iv = next_interval (next_iv))
3402 {
3403 for (p = it_props; p->handler; ++p)
3404 {
3405 Lisp_Object new_value;
3406
3407 new_value = textget (next_iv->plist, *p->name);
3408 if (!EQ (values_here[p->idx], new_value))
3409 break;
3410 }
3411
3412 if (p->handler)
3413 break;
3414 }
3415
3416 if (next_iv)
3417 {
3418 if (INTEGERP (limit)
3419 && next_iv->position >= XFASTINT (limit))
3420 /* No text property change up to limit. */
3421 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3422 else
3423 /* Text properties change in next_iv. */
3424 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3425 }
3426 }
3427
3428 if (it->cmp_it.id < 0)
3429 {
3430 ptrdiff_t stoppos = it->end_charpos;
3431
3432 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3433 stoppos = -1;
3434 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3435 stoppos, it->string);
3436 }
3437
3438 eassert (STRINGP (it->string)
3439 || (it->stop_charpos >= BEGV
3440 && it->stop_charpos >= IT_CHARPOS (*it)));
3441 }
3442
3443
3444 /* Return the position of the next overlay change after POS in
3445 current_buffer. Value is point-max if no overlay change
3446 follows. This is like `next-overlay-change' but doesn't use
3447 xmalloc. */
3448
3449 static ptrdiff_t
3450 next_overlay_change (ptrdiff_t pos)
3451 {
3452 ptrdiff_t i, noverlays;
3453 ptrdiff_t endpos;
3454 Lisp_Object *overlays;
3455
3456 /* Get all overlays at the given position. */
3457 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3458
3459 /* If any of these overlays ends before endpos,
3460 use its ending point instead. */
3461 for (i = 0; i < noverlays; ++i)
3462 {
3463 Lisp_Object oend;
3464 ptrdiff_t oendpos;
3465
3466 oend = OVERLAY_END (overlays[i]);
3467 oendpos = OVERLAY_POSITION (oend);
3468 endpos = min (endpos, oendpos);
3469 }
3470
3471 return endpos;
3472 }
3473
3474 /* How many characters forward to search for a display property or
3475 display string. Searching too far forward makes the bidi display
3476 sluggish, especially in small windows. */
3477 #define MAX_DISP_SCAN 250
3478
3479 /* Return the character position of a display string at or after
3480 position specified by POSITION. If no display string exists at or
3481 after POSITION, return ZV. A display string is either an overlay
3482 with `display' property whose value is a string, or a `display'
3483 text property whose value is a string. STRING is data about the
3484 string to iterate; if STRING->lstring is nil, we are iterating a
3485 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3486 on a GUI frame. DISP_PROP is set to zero if we searched
3487 MAX_DISP_SCAN characters forward without finding any display
3488 strings, non-zero otherwise. It is set to 2 if the display string
3489 uses any kind of `(space ...)' spec that will produce a stretch of
3490 white space in the text area. */
3491 ptrdiff_t
3492 compute_display_string_pos (struct text_pos *position,
3493 struct bidi_string_data *string,
3494 int frame_window_p, int *disp_prop)
3495 {
3496 /* OBJECT = nil means current buffer. */
3497 Lisp_Object object =
3498 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3499 Lisp_Object pos, spec, limpos;
3500 int string_p = (string && (STRINGP (string->lstring) || string->s));
3501 ptrdiff_t eob = string_p ? string->schars : ZV;
3502 ptrdiff_t begb = string_p ? 0 : BEGV;
3503 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3504 ptrdiff_t lim =
3505 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3506 struct text_pos tpos;
3507 int rv = 0;
3508
3509 *disp_prop = 1;
3510
3511 if (charpos >= eob
3512 /* We don't support display properties whose values are strings
3513 that have display string properties. */
3514 || string->from_disp_str
3515 /* C strings cannot have display properties. */
3516 || (string->s && !STRINGP (object)))
3517 {
3518 *disp_prop = 0;
3519 return eob;
3520 }
3521
3522 /* If the character at CHARPOS is where the display string begins,
3523 return CHARPOS. */
3524 pos = make_number (charpos);
3525 if (STRINGP (object))
3526 bufpos = string->bufpos;
3527 else
3528 bufpos = charpos;
3529 tpos = *position;
3530 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3531 && (charpos <= begb
3532 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3533 object),
3534 spec))
3535 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3536 frame_window_p)))
3537 {
3538 if (rv == 2)
3539 *disp_prop = 2;
3540 return charpos;
3541 }
3542
3543 /* Look forward for the first character with a `display' property
3544 that will replace the underlying text when displayed. */
3545 limpos = make_number (lim);
3546 do {
3547 pos = Fnext_single_char_property_change (pos, Qdisplay, object, limpos);
3548 CHARPOS (tpos) = XFASTINT (pos);
3549 if (CHARPOS (tpos) >= lim)
3550 {
3551 *disp_prop = 0;
3552 break;
3553 }
3554 if (STRINGP (object))
3555 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3556 else
3557 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3558 spec = Fget_char_property (pos, Qdisplay, object);
3559 if (!STRINGP (object))
3560 bufpos = CHARPOS (tpos);
3561 } while (NILP (spec)
3562 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3563 bufpos, frame_window_p)));
3564 if (rv == 2)
3565 *disp_prop = 2;
3566
3567 return CHARPOS (tpos);
3568 }
3569
3570 /* Return the character position of the end of the display string that
3571 started at CHARPOS. If there's no display string at CHARPOS,
3572 return -1. A display string is either an overlay with `display'
3573 property whose value is a string or a `display' text property whose
3574 value is a string. */
3575 ptrdiff_t
3576 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3577 {
3578 /* OBJECT = nil means current buffer. */
3579 Lisp_Object object =
3580 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3581 Lisp_Object pos = make_number (charpos);
3582 ptrdiff_t eob =
3583 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3584
3585 if (charpos >= eob || (string->s && !STRINGP (object)))
3586 return eob;
3587
3588 /* It could happen that the display property or overlay was removed
3589 since we found it in compute_display_string_pos above. One way
3590 this can happen is if JIT font-lock was called (through
3591 handle_fontified_prop), and jit-lock-functions remove text
3592 properties or overlays from the portion of buffer that includes
3593 CHARPOS. Muse mode is known to do that, for example. In this
3594 case, we return -1 to the caller, to signal that no display
3595 string is actually present at CHARPOS. See bidi_fetch_char for
3596 how this is handled.
3597
3598 An alternative would be to never look for display properties past
3599 it->stop_charpos. But neither compute_display_string_pos nor
3600 bidi_fetch_char that calls it know or care where the next
3601 stop_charpos is. */
3602 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3603 return -1;
3604
3605 /* Look forward for the first character where the `display' property
3606 changes. */
3607 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3608
3609 return XFASTINT (pos);
3610 }
3611
3612
3613 \f
3614 /***********************************************************************
3615 Fontification
3616 ***********************************************************************/
3617
3618 /* Handle changes in the `fontified' property of the current buffer by
3619 calling hook functions from Qfontification_functions to fontify
3620 regions of text. */
3621
3622 static enum prop_handled
3623 handle_fontified_prop (struct it *it)
3624 {
3625 Lisp_Object prop, pos;
3626 enum prop_handled handled = HANDLED_NORMALLY;
3627
3628 if (!NILP (Vmemory_full))
3629 return handled;
3630
3631 /* Get the value of the `fontified' property at IT's current buffer
3632 position. (The `fontified' property doesn't have a special
3633 meaning in strings.) If the value is nil, call functions from
3634 Qfontification_functions. */
3635 if (!STRINGP (it->string)
3636 && it->s == NULL
3637 && !NILP (Vfontification_functions)
3638 && !NILP (Vrun_hooks)
3639 && (pos = make_number (IT_CHARPOS (*it)),
3640 prop = Fget_char_property (pos, Qfontified, Qnil),
3641 /* Ignore the special cased nil value always present at EOB since
3642 no amount of fontifying will be able to change it. */
3643 NILP (prop) && IT_CHARPOS (*it) < Z))
3644 {
3645 ptrdiff_t count = SPECPDL_INDEX ();
3646 Lisp_Object val;
3647 struct buffer *obuf = current_buffer;
3648 int begv = BEGV, zv = ZV;
3649 int old_clip_changed = current_buffer->clip_changed;
3650
3651 val = Vfontification_functions;
3652 specbind (Qfontification_functions, Qnil);
3653
3654 eassert (it->end_charpos == ZV);
3655
3656 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3657 safe_call1 (val, pos);
3658 else
3659 {
3660 Lisp_Object fns, fn;
3661 struct gcpro gcpro1, gcpro2;
3662
3663 fns = Qnil;
3664 GCPRO2 (val, fns);
3665
3666 for (; CONSP (val); val = XCDR (val))
3667 {
3668 fn = XCAR (val);
3669
3670 if (EQ (fn, Qt))
3671 {
3672 /* A value of t indicates this hook has a local
3673 binding; it means to run the global binding too.
3674 In a global value, t should not occur. If it
3675 does, we must ignore it to avoid an endless
3676 loop. */
3677 for (fns = Fdefault_value (Qfontification_functions);
3678 CONSP (fns);
3679 fns = XCDR (fns))
3680 {
3681 fn = XCAR (fns);
3682 if (!EQ (fn, Qt))
3683 safe_call1 (fn, pos);
3684 }
3685 }
3686 else
3687 safe_call1 (fn, pos);
3688 }
3689
3690 UNGCPRO;
3691 }
3692
3693 unbind_to (count, Qnil);
3694
3695 /* Fontification functions routinely call `save-restriction'.
3696 Normally, this tags clip_changed, which can confuse redisplay
3697 (see discussion in Bug#6671). Since we don't perform any
3698 special handling of fontification changes in the case where
3699 `save-restriction' isn't called, there's no point doing so in
3700 this case either. So, if the buffer's restrictions are
3701 actually left unchanged, reset clip_changed. */
3702 if (obuf == current_buffer)
3703 {
3704 if (begv == BEGV && zv == ZV)
3705 current_buffer->clip_changed = old_clip_changed;
3706 }
3707 /* There isn't much we can reasonably do to protect against
3708 misbehaving fontification, but here's a fig leaf. */
3709 else if (BUFFER_LIVE_P (obuf))
3710 set_buffer_internal_1 (obuf);
3711
3712 /* The fontification code may have added/removed text.
3713 It could do even a lot worse, but let's at least protect against
3714 the most obvious case where only the text past `pos' gets changed',
3715 as is/was done in grep.el where some escapes sequences are turned
3716 into face properties (bug#7876). */
3717 it->end_charpos = ZV;
3718
3719 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3720 something. This avoids an endless loop if they failed to
3721 fontify the text for which reason ever. */
3722 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3723 handled = HANDLED_RECOMPUTE_PROPS;
3724 }
3725
3726 return handled;
3727 }
3728
3729
3730 \f
3731 /***********************************************************************
3732 Faces
3733 ***********************************************************************/
3734
3735 /* Set up iterator IT from face properties at its current position.
3736 Called from handle_stop. */
3737
3738 static enum prop_handled
3739 handle_face_prop (struct it *it)
3740 {
3741 int new_face_id;
3742 ptrdiff_t next_stop;
3743
3744 if (!STRINGP (it->string))
3745 {
3746 new_face_id
3747 = face_at_buffer_position (it->w,
3748 IT_CHARPOS (*it),
3749 it->region_beg_charpos,
3750 it->region_end_charpos,
3751 &next_stop,
3752 (IT_CHARPOS (*it)
3753 + TEXT_PROP_DISTANCE_LIMIT),
3754 0, it->base_face_id);
3755
3756 /* Is this a start of a run of characters with box face?
3757 Caveat: this can be called for a freshly initialized
3758 iterator; face_id is -1 in this case. We know that the new
3759 face will not change until limit, i.e. if the new face has a
3760 box, all characters up to limit will have one. But, as
3761 usual, we don't know whether limit is really the end. */
3762 if (new_face_id != it->face_id)
3763 {
3764 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3765
3766 /* If new face has a box but old face has not, this is
3767 the start of a run of characters with box, i.e. it has
3768 a shadow on the left side. The value of face_id of the
3769 iterator will be -1 if this is the initial call that gets
3770 the face. In this case, we have to look in front of IT's
3771 position and see whether there is a face != new_face_id. */
3772 it->start_of_box_run_p
3773 = (new_face->box != FACE_NO_BOX
3774 && (it->face_id >= 0
3775 || IT_CHARPOS (*it) == BEG
3776 || new_face_id != face_before_it_pos (it)));
3777 it->face_box_p = new_face->box != FACE_NO_BOX;
3778 }
3779 }
3780 else
3781 {
3782 int base_face_id;
3783 ptrdiff_t bufpos;
3784 int i;
3785 Lisp_Object from_overlay
3786 = (it->current.overlay_string_index >= 0
3787 ? it->string_overlays[it->current.overlay_string_index
3788 % OVERLAY_STRING_CHUNK_SIZE]
3789 : Qnil);
3790
3791 /* See if we got to this string directly or indirectly from
3792 an overlay property. That includes the before-string or
3793 after-string of an overlay, strings in display properties
3794 provided by an overlay, their text properties, etc.
3795
3796 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3797 if (! NILP (from_overlay))
3798 for (i = it->sp - 1; i >= 0; i--)
3799 {
3800 if (it->stack[i].current.overlay_string_index >= 0)
3801 from_overlay
3802 = it->string_overlays[it->stack[i].current.overlay_string_index
3803 % OVERLAY_STRING_CHUNK_SIZE];
3804 else if (! NILP (it->stack[i].from_overlay))
3805 from_overlay = it->stack[i].from_overlay;
3806
3807 if (!NILP (from_overlay))
3808 break;
3809 }
3810
3811 if (! NILP (from_overlay))
3812 {
3813 bufpos = IT_CHARPOS (*it);
3814 /* For a string from an overlay, the base face depends
3815 only on text properties and ignores overlays. */
3816 base_face_id
3817 = face_for_overlay_string (it->w,
3818 IT_CHARPOS (*it),
3819 it->region_beg_charpos,
3820 it->region_end_charpos,
3821 &next_stop,
3822 (IT_CHARPOS (*it)
3823 + TEXT_PROP_DISTANCE_LIMIT),
3824 0,
3825 from_overlay);
3826 }
3827 else
3828 {
3829 bufpos = 0;
3830
3831 /* For strings from a `display' property, use the face at
3832 IT's current buffer position as the base face to merge
3833 with, so that overlay strings appear in the same face as
3834 surrounding text, unless they specify their own
3835 faces. */
3836 base_face_id = it->string_from_prefix_prop_p
3837 ? DEFAULT_FACE_ID
3838 : underlying_face_id (it);
3839 }
3840
3841 new_face_id = face_at_string_position (it->w,
3842 it->string,
3843 IT_STRING_CHARPOS (*it),
3844 bufpos,
3845 it->region_beg_charpos,
3846 it->region_end_charpos,
3847 &next_stop,
3848 base_face_id, 0);
3849
3850 /* Is this a start of a run of characters with box? Caveat:
3851 this can be called for a freshly allocated iterator; face_id
3852 is -1 is this case. We know that the new face will not
3853 change until the next check pos, i.e. if the new face has a
3854 box, all characters up to that position will have a
3855 box. But, as usual, we don't know whether that position
3856 is really the end. */
3857 if (new_face_id != it->face_id)
3858 {
3859 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3860 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3861
3862 /* If new face has a box but old face hasn't, this is the
3863 start of a run of characters with box, i.e. it has a
3864 shadow on the left side. */
3865 it->start_of_box_run_p
3866 = new_face->box && (old_face == NULL || !old_face->box);
3867 it->face_box_p = new_face->box != FACE_NO_BOX;
3868 }
3869 }
3870
3871 it->face_id = new_face_id;
3872 return HANDLED_NORMALLY;
3873 }
3874
3875
3876 /* Return the ID of the face ``underlying'' IT's current position,
3877 which is in a string. If the iterator is associated with a
3878 buffer, return the face at IT's current buffer position.
3879 Otherwise, use the iterator's base_face_id. */
3880
3881 static int
3882 underlying_face_id (struct it *it)
3883 {
3884 int face_id = it->base_face_id, i;
3885
3886 eassert (STRINGP (it->string));
3887
3888 for (i = it->sp - 1; i >= 0; --i)
3889 if (NILP (it->stack[i].string))
3890 face_id = it->stack[i].face_id;
3891
3892 return face_id;
3893 }
3894
3895
3896 /* Compute the face one character before or after the current position
3897 of IT, in the visual order. BEFORE_P non-zero means get the face
3898 in front (to the left in L2R paragraphs, to the right in R2L
3899 paragraphs) of IT's screen position. Value is the ID of the face. */
3900
3901 static int
3902 face_before_or_after_it_pos (struct it *it, int before_p)
3903 {
3904 int face_id, limit;
3905 ptrdiff_t next_check_charpos;
3906 struct it it_copy;
3907 void *it_copy_data = NULL;
3908
3909 eassert (it->s == NULL);
3910
3911 if (STRINGP (it->string))
3912 {
3913 ptrdiff_t bufpos, charpos;
3914 int base_face_id;
3915
3916 /* No face change past the end of the string (for the case
3917 we are padding with spaces). No face change before the
3918 string start. */
3919 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3920 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3921 return it->face_id;
3922
3923 if (!it->bidi_p)
3924 {
3925 /* Set charpos to the position before or after IT's current
3926 position, in the logical order, which in the non-bidi
3927 case is the same as the visual order. */
3928 if (before_p)
3929 charpos = IT_STRING_CHARPOS (*it) - 1;
3930 else if (it->what == IT_COMPOSITION)
3931 /* For composition, we must check the character after the
3932 composition. */
3933 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3934 else
3935 charpos = IT_STRING_CHARPOS (*it) + 1;
3936 }
3937 else
3938 {
3939 if (before_p)
3940 {
3941 /* With bidi iteration, the character before the current
3942 in the visual order cannot be found by simple
3943 iteration, because "reverse" reordering is not
3944 supported. Instead, we need to use the move_it_*
3945 family of functions. */
3946 /* Ignore face changes before the first visible
3947 character on this display line. */
3948 if (it->current_x <= it->first_visible_x)
3949 return it->face_id;
3950 SAVE_IT (it_copy, *it, it_copy_data);
3951 /* Implementation note: Since move_it_in_display_line
3952 works in the iterator geometry, and thinks the first
3953 character is always the leftmost, even in R2L lines,
3954 we don't need to distinguish between the R2L and L2R
3955 cases here. */
3956 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3957 it_copy.current_x - 1, MOVE_TO_X);
3958 charpos = IT_STRING_CHARPOS (it_copy);
3959 RESTORE_IT (it, it, it_copy_data);
3960 }
3961 else
3962 {
3963 /* Set charpos to the string position of the character
3964 that comes after IT's current position in the visual
3965 order. */
3966 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3967
3968 it_copy = *it;
3969 while (n--)
3970 bidi_move_to_visually_next (&it_copy.bidi_it);
3971
3972 charpos = it_copy.bidi_it.charpos;
3973 }
3974 }
3975 eassert (0 <= charpos && charpos <= SCHARS (it->string));
3976
3977 if (it->current.overlay_string_index >= 0)
3978 bufpos = IT_CHARPOS (*it);
3979 else
3980 bufpos = 0;
3981
3982 base_face_id = underlying_face_id (it);
3983
3984 /* Get the face for ASCII, or unibyte. */
3985 face_id = face_at_string_position (it->w,
3986 it->string,
3987 charpos,
3988 bufpos,
3989 it->region_beg_charpos,
3990 it->region_end_charpos,
3991 &next_check_charpos,
3992 base_face_id, 0);
3993
3994 /* Correct the face for charsets different from ASCII. Do it
3995 for the multibyte case only. The face returned above is
3996 suitable for unibyte text if IT->string is unibyte. */
3997 if (STRING_MULTIBYTE (it->string))
3998 {
3999 struct text_pos pos1 = string_pos (charpos, it->string);
4000 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4001 int c, len;
4002 struct face *face = FACE_FROM_ID (it->f, face_id);
4003
4004 c = string_char_and_length (p, &len);
4005 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4006 }
4007 }
4008 else
4009 {
4010 struct text_pos pos;
4011
4012 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4013 || (IT_CHARPOS (*it) <= BEGV && before_p))
4014 return it->face_id;
4015
4016 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4017 pos = it->current.pos;
4018
4019 if (!it->bidi_p)
4020 {
4021 if (before_p)
4022 DEC_TEXT_POS (pos, it->multibyte_p);
4023 else
4024 {
4025 if (it->what == IT_COMPOSITION)
4026 {
4027 /* For composition, we must check the position after
4028 the composition. */
4029 pos.charpos += it->cmp_it.nchars;
4030 pos.bytepos += it->len;
4031 }
4032 else
4033 INC_TEXT_POS (pos, it->multibyte_p);
4034 }
4035 }
4036 else
4037 {
4038 if (before_p)
4039 {
4040 /* With bidi iteration, the character before the current
4041 in the visual order cannot be found by simple
4042 iteration, because "reverse" reordering is not
4043 supported. Instead, we need to use the move_it_*
4044 family of functions. */
4045 /* Ignore face changes before the first visible
4046 character on this display line. */
4047 if (it->current_x <= it->first_visible_x)
4048 return it->face_id;
4049 SAVE_IT (it_copy, *it, it_copy_data);
4050 /* Implementation note: Since move_it_in_display_line
4051 works in the iterator geometry, and thinks the first
4052 character is always the leftmost, even in R2L lines,
4053 we don't need to distinguish between the R2L and L2R
4054 cases here. */
4055 move_it_in_display_line (&it_copy, ZV,
4056 it_copy.current_x - 1, MOVE_TO_X);
4057 pos = it_copy.current.pos;
4058 RESTORE_IT (it, it, it_copy_data);
4059 }
4060 else
4061 {
4062 /* Set charpos to the buffer position of the character
4063 that comes after IT's current position in the visual
4064 order. */
4065 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4066
4067 it_copy = *it;
4068 while (n--)
4069 bidi_move_to_visually_next (&it_copy.bidi_it);
4070
4071 SET_TEXT_POS (pos,
4072 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4073 }
4074 }
4075 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4076
4077 /* Determine face for CHARSET_ASCII, or unibyte. */
4078 face_id = face_at_buffer_position (it->w,
4079 CHARPOS (pos),
4080 it->region_beg_charpos,
4081 it->region_end_charpos,
4082 &next_check_charpos,
4083 limit, 0, -1);
4084
4085 /* Correct the face for charsets different from ASCII. Do it
4086 for the multibyte case only. The face returned above is
4087 suitable for unibyte text if current_buffer is unibyte. */
4088 if (it->multibyte_p)
4089 {
4090 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4091 struct face *face = FACE_FROM_ID (it->f, face_id);
4092 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4093 }
4094 }
4095
4096 return face_id;
4097 }
4098
4099
4100 \f
4101 /***********************************************************************
4102 Invisible text
4103 ***********************************************************************/
4104
4105 /* Set up iterator IT from invisible properties at its current
4106 position. Called from handle_stop. */
4107
4108 static enum prop_handled
4109 handle_invisible_prop (struct it *it)
4110 {
4111 enum prop_handled handled = HANDLED_NORMALLY;
4112 int invis_p;
4113 Lisp_Object prop;
4114
4115 if (STRINGP (it->string))
4116 {
4117 Lisp_Object end_charpos, limit, charpos;
4118
4119 /* Get the value of the invisible text property at the
4120 current position. Value will be nil if there is no such
4121 property. */
4122 charpos = make_number (IT_STRING_CHARPOS (*it));
4123 prop = Fget_text_property (charpos, Qinvisible, it->string);
4124 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4125
4126 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4127 {
4128 /* Record whether we have to display an ellipsis for the
4129 invisible text. */
4130 int display_ellipsis_p = (invis_p == 2);
4131 ptrdiff_t len, endpos;
4132
4133 handled = HANDLED_RECOMPUTE_PROPS;
4134
4135 /* Get the position at which the next visible text can be
4136 found in IT->string, if any. */
4137 endpos = len = SCHARS (it->string);
4138 XSETINT (limit, len);
4139 do
4140 {
4141 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4142 it->string, limit);
4143 if (INTEGERP (end_charpos))
4144 {
4145 endpos = XFASTINT (end_charpos);
4146 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4147 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4148 if (invis_p == 2)
4149 display_ellipsis_p = 1;
4150 }
4151 }
4152 while (invis_p && endpos < len);
4153
4154 if (display_ellipsis_p)
4155 it->ellipsis_p = 1;
4156
4157 if (endpos < len)
4158 {
4159 /* Text at END_CHARPOS is visible. Move IT there. */
4160 struct text_pos old;
4161 ptrdiff_t oldpos;
4162
4163 old = it->current.string_pos;
4164 oldpos = CHARPOS (old);
4165 if (it->bidi_p)
4166 {
4167 if (it->bidi_it.first_elt
4168 && it->bidi_it.charpos < SCHARS (it->string))
4169 bidi_paragraph_init (it->paragraph_embedding,
4170 &it->bidi_it, 1);
4171 /* Bidi-iterate out of the invisible text. */
4172 do
4173 {
4174 bidi_move_to_visually_next (&it->bidi_it);
4175 }
4176 while (oldpos <= it->bidi_it.charpos
4177 && it->bidi_it.charpos < endpos);
4178
4179 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4180 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4181 if (IT_CHARPOS (*it) >= endpos)
4182 it->prev_stop = endpos;
4183 }
4184 else
4185 {
4186 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4187 compute_string_pos (&it->current.string_pos, old, it->string);
4188 }
4189 }
4190 else
4191 {
4192 /* The rest of the string is invisible. If this is an
4193 overlay string, proceed with the next overlay string
4194 or whatever comes and return a character from there. */
4195 if (it->current.overlay_string_index >= 0
4196 && !display_ellipsis_p)
4197 {
4198 next_overlay_string (it);
4199 /* Don't check for overlay strings when we just
4200 finished processing them. */
4201 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4202 }
4203 else
4204 {
4205 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4206 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4207 }
4208 }
4209 }
4210 }
4211 else
4212 {
4213 ptrdiff_t newpos, next_stop, start_charpos, tem;
4214 Lisp_Object pos, overlay;
4215
4216 /* First of all, is there invisible text at this position? */
4217 tem = start_charpos = IT_CHARPOS (*it);
4218 pos = make_number (tem);
4219 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4220 &overlay);
4221 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4222
4223 /* If we are on invisible text, skip over it. */
4224 if (invis_p && start_charpos < it->end_charpos)
4225 {
4226 /* Record whether we have to display an ellipsis for the
4227 invisible text. */
4228 int display_ellipsis_p = invis_p == 2;
4229
4230 handled = HANDLED_RECOMPUTE_PROPS;
4231
4232 /* Loop skipping over invisible text. The loop is left at
4233 ZV or with IT on the first char being visible again. */
4234 do
4235 {
4236 /* Try to skip some invisible text. Return value is the
4237 position reached which can be equal to where we start
4238 if there is nothing invisible there. This skips both
4239 over invisible text properties and overlays with
4240 invisible property. */
4241 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4242
4243 /* If we skipped nothing at all we weren't at invisible
4244 text in the first place. If everything to the end of
4245 the buffer was skipped, end the loop. */
4246 if (newpos == tem || newpos >= ZV)
4247 invis_p = 0;
4248 else
4249 {
4250 /* We skipped some characters but not necessarily
4251 all there are. Check if we ended up on visible
4252 text. Fget_char_property returns the property of
4253 the char before the given position, i.e. if we
4254 get invis_p = 0, this means that the char at
4255 newpos is visible. */
4256 pos = make_number (newpos);
4257 prop = Fget_char_property (pos, Qinvisible, it->window);
4258 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4259 }
4260
4261 /* If we ended up on invisible text, proceed to
4262 skip starting with next_stop. */
4263 if (invis_p)
4264 tem = next_stop;
4265
4266 /* If there are adjacent invisible texts, don't lose the
4267 second one's ellipsis. */
4268 if (invis_p == 2)
4269 display_ellipsis_p = 1;
4270 }
4271 while (invis_p);
4272
4273 /* The position newpos is now either ZV or on visible text. */
4274 if (it->bidi_p)
4275 {
4276 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4277 int on_newline =
4278 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4279 int after_newline =
4280 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4281
4282 /* If the invisible text ends on a newline or on a
4283 character after a newline, we can avoid the costly,
4284 character by character, bidi iteration to NEWPOS, and
4285 instead simply reseat the iterator there. That's
4286 because all bidi reordering information is tossed at
4287 the newline. This is a big win for modes that hide
4288 complete lines, like Outline, Org, etc. */
4289 if (on_newline || after_newline)
4290 {
4291 struct text_pos tpos;
4292 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4293
4294 SET_TEXT_POS (tpos, newpos, bpos);
4295 reseat_1 (it, tpos, 0);
4296 /* If we reseat on a newline/ZV, we need to prep the
4297 bidi iterator for advancing to the next character
4298 after the newline/EOB, keeping the current paragraph
4299 direction (so that PRODUCE_GLYPHS does TRT wrt
4300 prepending/appending glyphs to a glyph row). */
4301 if (on_newline)
4302 {
4303 it->bidi_it.first_elt = 0;
4304 it->bidi_it.paragraph_dir = pdir;
4305 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4306 it->bidi_it.nchars = 1;
4307 it->bidi_it.ch_len = 1;
4308 }
4309 }
4310 else /* Must use the slow method. */
4311 {
4312 /* With bidi iteration, the region of invisible text
4313 could start and/or end in the middle of a
4314 non-base embedding level. Therefore, we need to
4315 skip invisible text using the bidi iterator,
4316 starting at IT's current position, until we find
4317 ourselves outside of the invisible text.
4318 Skipping invisible text _after_ bidi iteration
4319 avoids affecting the visual order of the
4320 displayed text when invisible properties are
4321 added or removed. */
4322 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4323 {
4324 /* If we were `reseat'ed to a new paragraph,
4325 determine the paragraph base direction. We
4326 need to do it now because
4327 next_element_from_buffer may not have a
4328 chance to do it, if we are going to skip any
4329 text at the beginning, which resets the
4330 FIRST_ELT flag. */
4331 bidi_paragraph_init (it->paragraph_embedding,
4332 &it->bidi_it, 1);
4333 }
4334 do
4335 {
4336 bidi_move_to_visually_next (&it->bidi_it);
4337 }
4338 while (it->stop_charpos <= it->bidi_it.charpos
4339 && it->bidi_it.charpos < newpos);
4340 IT_CHARPOS (*it) = it->bidi_it.charpos;
4341 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4342 /* If we overstepped NEWPOS, record its position in
4343 the iterator, so that we skip invisible text if
4344 later the bidi iteration lands us in the
4345 invisible region again. */
4346 if (IT_CHARPOS (*it) >= newpos)
4347 it->prev_stop = newpos;
4348 }
4349 }
4350 else
4351 {
4352 IT_CHARPOS (*it) = newpos;
4353 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4354 }
4355
4356 /* If there are before-strings at the start of invisible
4357 text, and the text is invisible because of a text
4358 property, arrange to show before-strings because 20.x did
4359 it that way. (If the text is invisible because of an
4360 overlay property instead of a text property, this is
4361 already handled in the overlay code.) */
4362 if (NILP (overlay)
4363 && get_overlay_strings (it, it->stop_charpos))
4364 {
4365 handled = HANDLED_RECOMPUTE_PROPS;
4366 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4367 }
4368 else if (display_ellipsis_p)
4369 {
4370 /* Make sure that the glyphs of the ellipsis will get
4371 correct `charpos' values. If we would not update
4372 it->position here, the glyphs would belong to the
4373 last visible character _before_ the invisible
4374 text, which confuses `set_cursor_from_row'.
4375
4376 We use the last invisible position instead of the
4377 first because this way the cursor is always drawn on
4378 the first "." of the ellipsis, whenever PT is inside
4379 the invisible text. Otherwise the cursor would be
4380 placed _after_ the ellipsis when the point is after the
4381 first invisible character. */
4382 if (!STRINGP (it->object))
4383 {
4384 it->position.charpos = newpos - 1;
4385 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4386 }
4387 it->ellipsis_p = 1;
4388 /* Let the ellipsis display before
4389 considering any properties of the following char.
4390 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4391 handled = HANDLED_RETURN;
4392 }
4393 }
4394 }
4395
4396 return handled;
4397 }
4398
4399
4400 /* Make iterator IT return `...' next.
4401 Replaces LEN characters from buffer. */
4402
4403 static void
4404 setup_for_ellipsis (struct it *it, int len)
4405 {
4406 /* Use the display table definition for `...'. Invalid glyphs
4407 will be handled by the method returning elements from dpvec. */
4408 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4409 {
4410 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4411 it->dpvec = v->contents;
4412 it->dpend = v->contents + v->header.size;
4413 }
4414 else
4415 {
4416 /* Default `...'. */
4417 it->dpvec = default_invis_vector;
4418 it->dpend = default_invis_vector + 3;
4419 }
4420
4421 it->dpvec_char_len = len;
4422 it->current.dpvec_index = 0;
4423 it->dpvec_face_id = -1;
4424
4425 /* Remember the current face id in case glyphs specify faces.
4426 IT's face is restored in set_iterator_to_next.
4427 saved_face_id was set to preceding char's face in handle_stop. */
4428 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4429 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4430
4431 it->method = GET_FROM_DISPLAY_VECTOR;
4432 it->ellipsis_p = 1;
4433 }
4434
4435
4436 \f
4437 /***********************************************************************
4438 'display' property
4439 ***********************************************************************/
4440
4441 /* Set up iterator IT from `display' property at its current position.
4442 Called from handle_stop.
4443 We return HANDLED_RETURN if some part of the display property
4444 overrides the display of the buffer text itself.
4445 Otherwise we return HANDLED_NORMALLY. */
4446
4447 static enum prop_handled
4448 handle_display_prop (struct it *it)
4449 {
4450 Lisp_Object propval, object, overlay;
4451 struct text_pos *position;
4452 ptrdiff_t bufpos;
4453 /* Nonzero if some property replaces the display of the text itself. */
4454 int display_replaced_p = 0;
4455
4456 if (STRINGP (it->string))
4457 {
4458 object = it->string;
4459 position = &it->current.string_pos;
4460 bufpos = CHARPOS (it->current.pos);
4461 }
4462 else
4463 {
4464 XSETWINDOW (object, it->w);
4465 position = &it->current.pos;
4466 bufpos = CHARPOS (*position);
4467 }
4468
4469 /* Reset those iterator values set from display property values. */
4470 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4471 it->space_width = Qnil;
4472 it->font_height = Qnil;
4473 it->voffset = 0;
4474
4475 /* We don't support recursive `display' properties, i.e. string
4476 values that have a string `display' property, that have a string
4477 `display' property etc. */
4478 if (!it->string_from_display_prop_p)
4479 it->area = TEXT_AREA;
4480
4481 propval = get_char_property_and_overlay (make_number (position->charpos),
4482 Qdisplay, object, &overlay);
4483 if (NILP (propval))
4484 return HANDLED_NORMALLY;
4485 /* Now OVERLAY is the overlay that gave us this property, or nil
4486 if it was a text property. */
4487
4488 if (!STRINGP (it->string))
4489 object = it->w->buffer;
4490
4491 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4492 position, bufpos,
4493 FRAME_WINDOW_P (it->f));
4494
4495 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4496 }
4497
4498 /* Subroutine of handle_display_prop. Returns non-zero if the display
4499 specification in SPEC is a replacing specification, i.e. it would
4500 replace the text covered by `display' property with something else,
4501 such as an image or a display string. If SPEC includes any kind or
4502 `(space ...) specification, the value is 2; this is used by
4503 compute_display_string_pos, which see.
4504
4505 See handle_single_display_spec for documentation of arguments.
4506 frame_window_p is non-zero if the window being redisplayed is on a
4507 GUI frame; this argument is used only if IT is NULL, see below.
4508
4509 IT can be NULL, if this is called by the bidi reordering code
4510 through compute_display_string_pos, which see. In that case, this
4511 function only examines SPEC, but does not otherwise "handle" it, in
4512 the sense that it doesn't set up members of IT from the display
4513 spec. */
4514 static int
4515 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4516 Lisp_Object overlay, struct text_pos *position,
4517 ptrdiff_t bufpos, int frame_window_p)
4518 {
4519 int replacing_p = 0;
4520 int rv;
4521
4522 if (CONSP (spec)
4523 /* Simple specifications. */
4524 && !EQ (XCAR (spec), Qimage)
4525 && !EQ (XCAR (spec), Qspace)
4526 && !EQ (XCAR (spec), Qwhen)
4527 && !EQ (XCAR (spec), Qslice)
4528 && !EQ (XCAR (spec), Qspace_width)
4529 && !EQ (XCAR (spec), Qheight)
4530 && !EQ (XCAR (spec), Qraise)
4531 /* Marginal area specifications. */
4532 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4533 && !EQ (XCAR (spec), Qleft_fringe)
4534 && !EQ (XCAR (spec), Qright_fringe)
4535 && !NILP (XCAR (spec)))
4536 {
4537 for (; CONSP (spec); spec = XCDR (spec))
4538 {
4539 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4540 overlay, position, bufpos,
4541 replacing_p, frame_window_p)))
4542 {
4543 replacing_p = rv;
4544 /* If some text in a string is replaced, `position' no
4545 longer points to the position of `object'. */
4546 if (!it || STRINGP (object))
4547 break;
4548 }
4549 }
4550 }
4551 else if (VECTORP (spec))
4552 {
4553 ptrdiff_t i;
4554 for (i = 0; i < ASIZE (spec); ++i)
4555 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4556 overlay, position, bufpos,
4557 replacing_p, frame_window_p)))
4558 {
4559 replacing_p = rv;
4560 /* If some text in a string is replaced, `position' no
4561 longer points to the position of `object'. */
4562 if (!it || STRINGP (object))
4563 break;
4564 }
4565 }
4566 else
4567 {
4568 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4569 position, bufpos, 0,
4570 frame_window_p)))
4571 replacing_p = rv;
4572 }
4573
4574 return replacing_p;
4575 }
4576
4577 /* Value is the position of the end of the `display' property starting
4578 at START_POS in OBJECT. */
4579
4580 static struct text_pos
4581 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4582 {
4583 Lisp_Object end;
4584 struct text_pos end_pos;
4585
4586 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4587 Qdisplay, object, Qnil);
4588 CHARPOS (end_pos) = XFASTINT (end);
4589 if (STRINGP (object))
4590 compute_string_pos (&end_pos, start_pos, it->string);
4591 else
4592 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4593
4594 return end_pos;
4595 }
4596
4597
4598 /* Set up IT from a single `display' property specification SPEC. OBJECT
4599 is the object in which the `display' property was found. *POSITION
4600 is the position in OBJECT at which the `display' property was found.
4601 BUFPOS is the buffer position of OBJECT (different from POSITION if
4602 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4603 previously saw a display specification which already replaced text
4604 display with something else, for example an image; we ignore such
4605 properties after the first one has been processed.
4606
4607 OVERLAY is the overlay this `display' property came from,
4608 or nil if it was a text property.
4609
4610 If SPEC is a `space' or `image' specification, and in some other
4611 cases too, set *POSITION to the position where the `display'
4612 property ends.
4613
4614 If IT is NULL, only examine the property specification in SPEC, but
4615 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4616 is intended to be displayed in a window on a GUI frame.
4617
4618 Value is non-zero if something was found which replaces the display
4619 of buffer or string text. */
4620
4621 static int
4622 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4623 Lisp_Object overlay, struct text_pos *position,
4624 ptrdiff_t bufpos, int display_replaced_p,
4625 int frame_window_p)
4626 {
4627 Lisp_Object form;
4628 Lisp_Object location, value;
4629 struct text_pos start_pos = *position;
4630 int valid_p;
4631
4632 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4633 If the result is non-nil, use VALUE instead of SPEC. */
4634 form = Qt;
4635 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4636 {
4637 spec = XCDR (spec);
4638 if (!CONSP (spec))
4639 return 0;
4640 form = XCAR (spec);
4641 spec = XCDR (spec);
4642 }
4643
4644 if (!NILP (form) && !EQ (form, Qt))
4645 {
4646 ptrdiff_t count = SPECPDL_INDEX ();
4647 struct gcpro gcpro1;
4648
4649 /* Bind `object' to the object having the `display' property, a
4650 buffer or string. Bind `position' to the position in the
4651 object where the property was found, and `buffer-position'
4652 to the current position in the buffer. */
4653
4654 if (NILP (object))
4655 XSETBUFFER (object, current_buffer);
4656 specbind (Qobject, object);
4657 specbind (Qposition, make_number (CHARPOS (*position)));
4658 specbind (Qbuffer_position, make_number (bufpos));
4659 GCPRO1 (form);
4660 form = safe_eval (form);
4661 UNGCPRO;
4662 unbind_to (count, Qnil);
4663 }
4664
4665 if (NILP (form))
4666 return 0;
4667
4668 /* Handle `(height HEIGHT)' specifications. */
4669 if (CONSP (spec)
4670 && EQ (XCAR (spec), Qheight)
4671 && CONSP (XCDR (spec)))
4672 {
4673 if (it)
4674 {
4675 if (!FRAME_WINDOW_P (it->f))
4676 return 0;
4677
4678 it->font_height = XCAR (XCDR (spec));
4679 if (!NILP (it->font_height))
4680 {
4681 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4682 int new_height = -1;
4683
4684 if (CONSP (it->font_height)
4685 && (EQ (XCAR (it->font_height), Qplus)
4686 || EQ (XCAR (it->font_height), Qminus))
4687 && CONSP (XCDR (it->font_height))
4688 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4689 {
4690 /* `(+ N)' or `(- N)' where N is an integer. */
4691 int steps = XINT (XCAR (XCDR (it->font_height)));
4692 if (EQ (XCAR (it->font_height), Qplus))
4693 steps = - steps;
4694 it->face_id = smaller_face (it->f, it->face_id, steps);
4695 }
4696 else if (FUNCTIONP (it->font_height))
4697 {
4698 /* Call function with current height as argument.
4699 Value is the new height. */
4700 Lisp_Object height;
4701 height = safe_call1 (it->font_height,
4702 face->lface[LFACE_HEIGHT_INDEX]);
4703 if (NUMBERP (height))
4704 new_height = XFLOATINT (height);
4705 }
4706 else if (NUMBERP (it->font_height))
4707 {
4708 /* Value is a multiple of the canonical char height. */
4709 struct face *f;
4710
4711 f = FACE_FROM_ID (it->f,
4712 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4713 new_height = (XFLOATINT (it->font_height)
4714 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4715 }
4716 else
4717 {
4718 /* Evaluate IT->font_height with `height' bound to the
4719 current specified height to get the new height. */
4720 ptrdiff_t count = SPECPDL_INDEX ();
4721
4722 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4723 value = safe_eval (it->font_height);
4724 unbind_to (count, Qnil);
4725
4726 if (NUMBERP (value))
4727 new_height = XFLOATINT (value);
4728 }
4729
4730 if (new_height > 0)
4731 it->face_id = face_with_height (it->f, it->face_id, new_height);
4732 }
4733 }
4734
4735 return 0;
4736 }
4737
4738 /* Handle `(space-width WIDTH)'. */
4739 if (CONSP (spec)
4740 && EQ (XCAR (spec), Qspace_width)
4741 && CONSP (XCDR (spec)))
4742 {
4743 if (it)
4744 {
4745 if (!FRAME_WINDOW_P (it->f))
4746 return 0;
4747
4748 value = XCAR (XCDR (spec));
4749 if (NUMBERP (value) && XFLOATINT (value) > 0)
4750 it->space_width = value;
4751 }
4752
4753 return 0;
4754 }
4755
4756 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4757 if (CONSP (spec)
4758 && EQ (XCAR (spec), Qslice))
4759 {
4760 Lisp_Object tem;
4761
4762 if (it)
4763 {
4764 if (!FRAME_WINDOW_P (it->f))
4765 return 0;
4766
4767 if (tem = XCDR (spec), CONSP (tem))
4768 {
4769 it->slice.x = XCAR (tem);
4770 if (tem = XCDR (tem), CONSP (tem))
4771 {
4772 it->slice.y = XCAR (tem);
4773 if (tem = XCDR (tem), CONSP (tem))
4774 {
4775 it->slice.width = XCAR (tem);
4776 if (tem = XCDR (tem), CONSP (tem))
4777 it->slice.height = XCAR (tem);
4778 }
4779 }
4780 }
4781 }
4782
4783 return 0;
4784 }
4785
4786 /* Handle `(raise FACTOR)'. */
4787 if (CONSP (spec)
4788 && EQ (XCAR (spec), Qraise)
4789 && CONSP (XCDR (spec)))
4790 {
4791 if (it)
4792 {
4793 if (!FRAME_WINDOW_P (it->f))
4794 return 0;
4795
4796 #ifdef HAVE_WINDOW_SYSTEM
4797 value = XCAR (XCDR (spec));
4798 if (NUMBERP (value))
4799 {
4800 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4801 it->voffset = - (XFLOATINT (value)
4802 * (FONT_HEIGHT (face->font)));
4803 }
4804 #endif /* HAVE_WINDOW_SYSTEM */
4805 }
4806
4807 return 0;
4808 }
4809
4810 /* Don't handle the other kinds of display specifications
4811 inside a string that we got from a `display' property. */
4812 if (it && it->string_from_display_prop_p)
4813 return 0;
4814
4815 /* Characters having this form of property are not displayed, so
4816 we have to find the end of the property. */
4817 if (it)
4818 {
4819 start_pos = *position;
4820 *position = display_prop_end (it, object, start_pos);
4821 }
4822 value = Qnil;
4823
4824 /* Stop the scan at that end position--we assume that all
4825 text properties change there. */
4826 if (it)
4827 it->stop_charpos = position->charpos;
4828
4829 /* Handle `(left-fringe BITMAP [FACE])'
4830 and `(right-fringe BITMAP [FACE])'. */
4831 if (CONSP (spec)
4832 && (EQ (XCAR (spec), Qleft_fringe)
4833 || EQ (XCAR (spec), Qright_fringe))
4834 && CONSP (XCDR (spec)))
4835 {
4836 int fringe_bitmap;
4837
4838 if (it)
4839 {
4840 if (!FRAME_WINDOW_P (it->f))
4841 /* If we return here, POSITION has been advanced
4842 across the text with this property. */
4843 {
4844 /* Synchronize the bidi iterator with POSITION. This is
4845 needed because we are not going to push the iterator
4846 on behalf of this display property, so there will be
4847 no pop_it call to do this synchronization for us. */
4848 if (it->bidi_p)
4849 {
4850 it->position = *position;
4851 iterate_out_of_display_property (it);
4852 *position = it->position;
4853 }
4854 return 1;
4855 }
4856 }
4857 else if (!frame_window_p)
4858 return 1;
4859
4860 #ifdef HAVE_WINDOW_SYSTEM
4861 value = XCAR (XCDR (spec));
4862 if (!SYMBOLP (value)
4863 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4864 /* If we return here, POSITION has been advanced
4865 across the text with this property. */
4866 {
4867 if (it && it->bidi_p)
4868 {
4869 it->position = *position;
4870 iterate_out_of_display_property (it);
4871 *position = it->position;
4872 }
4873 return 1;
4874 }
4875
4876 if (it)
4877 {
4878 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4879
4880 if (CONSP (XCDR (XCDR (spec))))
4881 {
4882 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4883 int face_id2 = lookup_derived_face (it->f, face_name,
4884 FRINGE_FACE_ID, 0);
4885 if (face_id2 >= 0)
4886 face_id = face_id2;
4887 }
4888
4889 /* Save current settings of IT so that we can restore them
4890 when we are finished with the glyph property value. */
4891 push_it (it, position);
4892
4893 it->area = TEXT_AREA;
4894 it->what = IT_IMAGE;
4895 it->image_id = -1; /* no image */
4896 it->position = start_pos;
4897 it->object = NILP (object) ? it->w->buffer : object;
4898 it->method = GET_FROM_IMAGE;
4899 it->from_overlay = Qnil;
4900 it->face_id = face_id;
4901 it->from_disp_prop_p = 1;
4902
4903 /* Say that we haven't consumed the characters with
4904 `display' property yet. The call to pop_it in
4905 set_iterator_to_next will clean this up. */
4906 *position = start_pos;
4907
4908 if (EQ (XCAR (spec), Qleft_fringe))
4909 {
4910 it->left_user_fringe_bitmap = fringe_bitmap;
4911 it->left_user_fringe_face_id = face_id;
4912 }
4913 else
4914 {
4915 it->right_user_fringe_bitmap = fringe_bitmap;
4916 it->right_user_fringe_face_id = face_id;
4917 }
4918 }
4919 #endif /* HAVE_WINDOW_SYSTEM */
4920 return 1;
4921 }
4922
4923 /* Prepare to handle `((margin left-margin) ...)',
4924 `((margin right-margin) ...)' and `((margin nil) ...)'
4925 prefixes for display specifications. */
4926 location = Qunbound;
4927 if (CONSP (spec) && CONSP (XCAR (spec)))
4928 {
4929 Lisp_Object tem;
4930
4931 value = XCDR (spec);
4932 if (CONSP (value))
4933 value = XCAR (value);
4934
4935 tem = XCAR (spec);
4936 if (EQ (XCAR (tem), Qmargin)
4937 && (tem = XCDR (tem),
4938 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4939 (NILP (tem)
4940 || EQ (tem, Qleft_margin)
4941 || EQ (tem, Qright_margin))))
4942 location = tem;
4943 }
4944
4945 if (EQ (location, Qunbound))
4946 {
4947 location = Qnil;
4948 value = spec;
4949 }
4950
4951 /* After this point, VALUE is the property after any
4952 margin prefix has been stripped. It must be a string,
4953 an image specification, or `(space ...)'.
4954
4955 LOCATION specifies where to display: `left-margin',
4956 `right-margin' or nil. */
4957
4958 valid_p = (STRINGP (value)
4959 #ifdef HAVE_WINDOW_SYSTEM
4960 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4961 && valid_image_p (value))
4962 #endif /* not HAVE_WINDOW_SYSTEM */
4963 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4964
4965 if (valid_p && !display_replaced_p)
4966 {
4967 int retval = 1;
4968
4969 if (!it)
4970 {
4971 /* Callers need to know whether the display spec is any kind
4972 of `(space ...)' spec that is about to affect text-area
4973 display. */
4974 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
4975 retval = 2;
4976 return retval;
4977 }
4978
4979 /* Save current settings of IT so that we can restore them
4980 when we are finished with the glyph property value. */
4981 push_it (it, position);
4982 it->from_overlay = overlay;
4983 it->from_disp_prop_p = 1;
4984
4985 if (NILP (location))
4986 it->area = TEXT_AREA;
4987 else if (EQ (location, Qleft_margin))
4988 it->area = LEFT_MARGIN_AREA;
4989 else
4990 it->area = RIGHT_MARGIN_AREA;
4991
4992 if (STRINGP (value))
4993 {
4994 it->string = value;
4995 it->multibyte_p = STRING_MULTIBYTE (it->string);
4996 it->current.overlay_string_index = -1;
4997 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4998 it->end_charpos = it->string_nchars = SCHARS (it->string);
4999 it->method = GET_FROM_STRING;
5000 it->stop_charpos = 0;
5001 it->prev_stop = 0;
5002 it->base_level_stop = 0;
5003 it->string_from_display_prop_p = 1;
5004 /* Say that we haven't consumed the characters with
5005 `display' property yet. The call to pop_it in
5006 set_iterator_to_next will clean this up. */
5007 if (BUFFERP (object))
5008 *position = start_pos;
5009
5010 /* Force paragraph direction to be that of the parent
5011 object. If the parent object's paragraph direction is
5012 not yet determined, default to L2R. */
5013 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5014 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5015 else
5016 it->paragraph_embedding = L2R;
5017
5018 /* Set up the bidi iterator for this display string. */
5019 if (it->bidi_p)
5020 {
5021 it->bidi_it.string.lstring = it->string;
5022 it->bidi_it.string.s = NULL;
5023 it->bidi_it.string.schars = it->end_charpos;
5024 it->bidi_it.string.bufpos = bufpos;
5025 it->bidi_it.string.from_disp_str = 1;
5026 it->bidi_it.string.unibyte = !it->multibyte_p;
5027 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5028 }
5029 }
5030 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5031 {
5032 it->method = GET_FROM_STRETCH;
5033 it->object = value;
5034 *position = it->position = start_pos;
5035 retval = 1 + (it->area == TEXT_AREA);
5036 }
5037 #ifdef HAVE_WINDOW_SYSTEM
5038 else
5039 {
5040 it->what = IT_IMAGE;
5041 it->image_id = lookup_image (it->f, value);
5042 it->position = start_pos;
5043 it->object = NILP (object) ? it->w->buffer : object;
5044 it->method = GET_FROM_IMAGE;
5045
5046 /* Say that we haven't consumed the characters with
5047 `display' property yet. The call to pop_it in
5048 set_iterator_to_next will clean this up. */
5049 *position = start_pos;
5050 }
5051 #endif /* HAVE_WINDOW_SYSTEM */
5052
5053 return retval;
5054 }
5055
5056 /* Invalid property or property not supported. Restore
5057 POSITION to what it was before. */
5058 *position = start_pos;
5059 return 0;
5060 }
5061
5062 /* Check if PROP is a display property value whose text should be
5063 treated as intangible. OVERLAY is the overlay from which PROP
5064 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5065 specify the buffer position covered by PROP. */
5066
5067 int
5068 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5069 ptrdiff_t charpos, ptrdiff_t bytepos)
5070 {
5071 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5072 struct text_pos position;
5073
5074 SET_TEXT_POS (position, charpos, bytepos);
5075 return handle_display_spec (NULL, prop, Qnil, overlay,
5076 &position, charpos, frame_window_p);
5077 }
5078
5079
5080 /* Return 1 if PROP is a display sub-property value containing STRING.
5081
5082 Implementation note: this and the following function are really
5083 special cases of handle_display_spec and
5084 handle_single_display_spec, and should ideally use the same code.
5085 Until they do, these two pairs must be consistent and must be
5086 modified in sync. */
5087
5088 static int
5089 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5090 {
5091 if (EQ (string, prop))
5092 return 1;
5093
5094 /* Skip over `when FORM'. */
5095 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5096 {
5097 prop = XCDR (prop);
5098 if (!CONSP (prop))
5099 return 0;
5100 /* Actually, the condition following `when' should be eval'ed,
5101 like handle_single_display_spec does, and we should return
5102 zero if it evaluates to nil. However, this function is
5103 called only when the buffer was already displayed and some
5104 glyph in the glyph matrix was found to come from a display
5105 string. Therefore, the condition was already evaluated, and
5106 the result was non-nil, otherwise the display string wouldn't
5107 have been displayed and we would have never been called for
5108 this property. Thus, we can skip the evaluation and assume
5109 its result is non-nil. */
5110 prop = XCDR (prop);
5111 }
5112
5113 if (CONSP (prop))
5114 /* Skip over `margin LOCATION'. */
5115 if (EQ (XCAR (prop), Qmargin))
5116 {
5117 prop = XCDR (prop);
5118 if (!CONSP (prop))
5119 return 0;
5120
5121 prop = XCDR (prop);
5122 if (!CONSP (prop))
5123 return 0;
5124 }
5125
5126 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5127 }
5128
5129
5130 /* Return 1 if STRING appears in the `display' property PROP. */
5131
5132 static int
5133 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5134 {
5135 if (CONSP (prop)
5136 && !EQ (XCAR (prop), Qwhen)
5137 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5138 {
5139 /* A list of sub-properties. */
5140 while (CONSP (prop))
5141 {
5142 if (single_display_spec_string_p (XCAR (prop), string))
5143 return 1;
5144 prop = XCDR (prop);
5145 }
5146 }
5147 else if (VECTORP (prop))
5148 {
5149 /* A vector of sub-properties. */
5150 ptrdiff_t i;
5151 for (i = 0; i < ASIZE (prop); ++i)
5152 if (single_display_spec_string_p (AREF (prop, i), string))
5153 return 1;
5154 }
5155 else
5156 return single_display_spec_string_p (prop, string);
5157
5158 return 0;
5159 }
5160
5161 /* Look for STRING in overlays and text properties in the current
5162 buffer, between character positions FROM and TO (excluding TO).
5163 BACK_P non-zero means look back (in this case, TO is supposed to be
5164 less than FROM).
5165 Value is the first character position where STRING was found, or
5166 zero if it wasn't found before hitting TO.
5167
5168 This function may only use code that doesn't eval because it is
5169 called asynchronously from note_mouse_highlight. */
5170
5171 static ptrdiff_t
5172 string_buffer_position_lim (Lisp_Object string,
5173 ptrdiff_t from, ptrdiff_t to, int back_p)
5174 {
5175 Lisp_Object limit, prop, pos;
5176 int found = 0;
5177
5178 pos = make_number (max (from, BEGV));
5179
5180 if (!back_p) /* looking forward */
5181 {
5182 limit = make_number (min (to, ZV));
5183 while (!found && !EQ (pos, limit))
5184 {
5185 prop = Fget_char_property (pos, Qdisplay, Qnil);
5186 if (!NILP (prop) && display_prop_string_p (prop, string))
5187 found = 1;
5188 else
5189 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5190 limit);
5191 }
5192 }
5193 else /* looking back */
5194 {
5195 limit = make_number (max (to, BEGV));
5196 while (!found && !EQ (pos, limit))
5197 {
5198 prop = Fget_char_property (pos, Qdisplay, Qnil);
5199 if (!NILP (prop) && display_prop_string_p (prop, string))
5200 found = 1;
5201 else
5202 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5203 limit);
5204 }
5205 }
5206
5207 return found ? XINT (pos) : 0;
5208 }
5209
5210 /* Determine which buffer position in current buffer STRING comes from.
5211 AROUND_CHARPOS is an approximate position where it could come from.
5212 Value is the buffer position or 0 if it couldn't be determined.
5213
5214 This function is necessary because we don't record buffer positions
5215 in glyphs generated from strings (to keep struct glyph small).
5216 This function may only use code that doesn't eval because it is
5217 called asynchronously from note_mouse_highlight. */
5218
5219 static ptrdiff_t
5220 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5221 {
5222 const int MAX_DISTANCE = 1000;
5223 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5224 around_charpos + MAX_DISTANCE,
5225 0);
5226
5227 if (!found)
5228 found = string_buffer_position_lim (string, around_charpos,
5229 around_charpos - MAX_DISTANCE, 1);
5230 return found;
5231 }
5232
5233
5234 \f
5235 /***********************************************************************
5236 `composition' property
5237 ***********************************************************************/
5238
5239 /* Set up iterator IT from `composition' property at its current
5240 position. Called from handle_stop. */
5241
5242 static enum prop_handled
5243 handle_composition_prop (struct it *it)
5244 {
5245 Lisp_Object prop, string;
5246 ptrdiff_t pos, pos_byte, start, end;
5247
5248 if (STRINGP (it->string))
5249 {
5250 unsigned char *s;
5251
5252 pos = IT_STRING_CHARPOS (*it);
5253 pos_byte = IT_STRING_BYTEPOS (*it);
5254 string = it->string;
5255 s = SDATA (string) + pos_byte;
5256 it->c = STRING_CHAR (s);
5257 }
5258 else
5259 {
5260 pos = IT_CHARPOS (*it);
5261 pos_byte = IT_BYTEPOS (*it);
5262 string = Qnil;
5263 it->c = FETCH_CHAR (pos_byte);
5264 }
5265
5266 /* If there's a valid composition and point is not inside of the
5267 composition (in the case that the composition is from the current
5268 buffer), draw a glyph composed from the composition components. */
5269 if (find_composition (pos, -1, &start, &end, &prop, string)
5270 && COMPOSITION_VALID_P (start, end, prop)
5271 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5272 {
5273 if (start < pos)
5274 /* As we can't handle this situation (perhaps font-lock added
5275 a new composition), we just return here hoping that next
5276 redisplay will detect this composition much earlier. */
5277 return HANDLED_NORMALLY;
5278 if (start != pos)
5279 {
5280 if (STRINGP (it->string))
5281 pos_byte = string_char_to_byte (it->string, start);
5282 else
5283 pos_byte = CHAR_TO_BYTE (start);
5284 }
5285 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5286 prop, string);
5287
5288 if (it->cmp_it.id >= 0)
5289 {
5290 it->cmp_it.ch = -1;
5291 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5292 it->cmp_it.nglyphs = -1;
5293 }
5294 }
5295
5296 return HANDLED_NORMALLY;
5297 }
5298
5299
5300 \f
5301 /***********************************************************************
5302 Overlay strings
5303 ***********************************************************************/
5304
5305 /* The following structure is used to record overlay strings for
5306 later sorting in load_overlay_strings. */
5307
5308 struct overlay_entry
5309 {
5310 Lisp_Object overlay;
5311 Lisp_Object string;
5312 EMACS_INT priority;
5313 int after_string_p;
5314 };
5315
5316
5317 /* Set up iterator IT from overlay strings at its current position.
5318 Called from handle_stop. */
5319
5320 static enum prop_handled
5321 handle_overlay_change (struct it *it)
5322 {
5323 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5324 return HANDLED_RECOMPUTE_PROPS;
5325 else
5326 return HANDLED_NORMALLY;
5327 }
5328
5329
5330 /* Set up the next overlay string for delivery by IT, if there is an
5331 overlay string to deliver. Called by set_iterator_to_next when the
5332 end of the current overlay string is reached. If there are more
5333 overlay strings to display, IT->string and
5334 IT->current.overlay_string_index are set appropriately here.
5335 Otherwise IT->string is set to nil. */
5336
5337 static void
5338 next_overlay_string (struct it *it)
5339 {
5340 ++it->current.overlay_string_index;
5341 if (it->current.overlay_string_index == it->n_overlay_strings)
5342 {
5343 /* No more overlay strings. Restore IT's settings to what
5344 they were before overlay strings were processed, and
5345 continue to deliver from current_buffer. */
5346
5347 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5348 pop_it (it);
5349 eassert (it->sp > 0
5350 || (NILP (it->string)
5351 && it->method == GET_FROM_BUFFER
5352 && it->stop_charpos >= BEGV
5353 && it->stop_charpos <= it->end_charpos));
5354 it->current.overlay_string_index = -1;
5355 it->n_overlay_strings = 0;
5356 it->overlay_strings_charpos = -1;
5357 /* If there's an empty display string on the stack, pop the
5358 stack, to resync the bidi iterator with IT's position. Such
5359 empty strings are pushed onto the stack in
5360 get_overlay_strings_1. */
5361 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5362 pop_it (it);
5363
5364 /* If we're at the end of the buffer, record that we have
5365 processed the overlay strings there already, so that
5366 next_element_from_buffer doesn't try it again. */
5367 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5368 it->overlay_strings_at_end_processed_p = 1;
5369 }
5370 else
5371 {
5372 /* There are more overlay strings to process. If
5373 IT->current.overlay_string_index has advanced to a position
5374 where we must load IT->overlay_strings with more strings, do
5375 it. We must load at the IT->overlay_strings_charpos where
5376 IT->n_overlay_strings was originally computed; when invisible
5377 text is present, this might not be IT_CHARPOS (Bug#7016). */
5378 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5379
5380 if (it->current.overlay_string_index && i == 0)
5381 load_overlay_strings (it, it->overlay_strings_charpos);
5382
5383 /* Initialize IT to deliver display elements from the overlay
5384 string. */
5385 it->string = it->overlay_strings[i];
5386 it->multibyte_p = STRING_MULTIBYTE (it->string);
5387 SET_TEXT_POS (it->current.string_pos, 0, 0);
5388 it->method = GET_FROM_STRING;
5389 it->stop_charpos = 0;
5390 it->end_charpos = SCHARS (it->string);
5391 if (it->cmp_it.stop_pos >= 0)
5392 it->cmp_it.stop_pos = 0;
5393 it->prev_stop = 0;
5394 it->base_level_stop = 0;
5395
5396 /* Set up the bidi iterator for this overlay string. */
5397 if (it->bidi_p)
5398 {
5399 it->bidi_it.string.lstring = it->string;
5400 it->bidi_it.string.s = NULL;
5401 it->bidi_it.string.schars = SCHARS (it->string);
5402 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5403 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5404 it->bidi_it.string.unibyte = !it->multibyte_p;
5405 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5406 }
5407 }
5408
5409 CHECK_IT (it);
5410 }
5411
5412
5413 /* Compare two overlay_entry structures E1 and E2. Used as a
5414 comparison function for qsort in load_overlay_strings. Overlay
5415 strings for the same position are sorted so that
5416
5417 1. All after-strings come in front of before-strings, except
5418 when they come from the same overlay.
5419
5420 2. Within after-strings, strings are sorted so that overlay strings
5421 from overlays with higher priorities come first.
5422
5423 2. Within before-strings, strings are sorted so that overlay
5424 strings from overlays with higher priorities come last.
5425
5426 Value is analogous to strcmp. */
5427
5428
5429 static int
5430 compare_overlay_entries (const void *e1, const void *e2)
5431 {
5432 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
5433 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
5434 int result;
5435
5436 if (entry1->after_string_p != entry2->after_string_p)
5437 {
5438 /* Let after-strings appear in front of before-strings if
5439 they come from different overlays. */
5440 if (EQ (entry1->overlay, entry2->overlay))
5441 result = entry1->after_string_p ? 1 : -1;
5442 else
5443 result = entry1->after_string_p ? -1 : 1;
5444 }
5445 else if (entry1->priority != entry2->priority)
5446 {
5447 if (entry1->after_string_p)
5448 /* After-strings sorted in order of decreasing priority. */
5449 result = entry2->priority < entry1->priority ? -1 : 1;
5450 else
5451 /* Before-strings sorted in order of increasing priority. */
5452 result = entry1->priority < entry2->priority ? -1 : 1;
5453 }
5454 else
5455 result = 0;
5456
5457 return result;
5458 }
5459
5460
5461 /* Load the vector IT->overlay_strings with overlay strings from IT's
5462 current buffer position, or from CHARPOS if that is > 0. Set
5463 IT->n_overlays to the total number of overlay strings found.
5464
5465 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5466 a time. On entry into load_overlay_strings,
5467 IT->current.overlay_string_index gives the number of overlay
5468 strings that have already been loaded by previous calls to this
5469 function.
5470
5471 IT->add_overlay_start contains an additional overlay start
5472 position to consider for taking overlay strings from, if non-zero.
5473 This position comes into play when the overlay has an `invisible'
5474 property, and both before and after-strings. When we've skipped to
5475 the end of the overlay, because of its `invisible' property, we
5476 nevertheless want its before-string to appear.
5477 IT->add_overlay_start will contain the overlay start position
5478 in this case.
5479
5480 Overlay strings are sorted so that after-string strings come in
5481 front of before-string strings. Within before and after-strings,
5482 strings are sorted by overlay priority. See also function
5483 compare_overlay_entries. */
5484
5485 static void
5486 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5487 {
5488 Lisp_Object overlay, window, str, invisible;
5489 struct Lisp_Overlay *ov;
5490 ptrdiff_t start, end;
5491 ptrdiff_t size = 20;
5492 ptrdiff_t n = 0, i, j;
5493 int invis_p;
5494 struct overlay_entry *entries = alloca (size * sizeof *entries);
5495 USE_SAFE_ALLOCA;
5496
5497 if (charpos <= 0)
5498 charpos = IT_CHARPOS (*it);
5499
5500 /* Append the overlay string STRING of overlay OVERLAY to vector
5501 `entries' which has size `size' and currently contains `n'
5502 elements. AFTER_P non-zero means STRING is an after-string of
5503 OVERLAY. */
5504 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5505 do \
5506 { \
5507 Lisp_Object priority; \
5508 \
5509 if (n == size) \
5510 { \
5511 struct overlay_entry *old = entries; \
5512 SAFE_NALLOCA (entries, 2, size); \
5513 memcpy (entries, old, size * sizeof *entries); \
5514 size *= 2; \
5515 } \
5516 \
5517 entries[n].string = (STRING); \
5518 entries[n].overlay = (OVERLAY); \
5519 priority = Foverlay_get ((OVERLAY), Qpriority); \
5520 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5521 entries[n].after_string_p = (AFTER_P); \
5522 ++n; \
5523 } \
5524 while (0)
5525
5526 /* Process overlay before the overlay center. */
5527 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5528 {
5529 XSETMISC (overlay, ov);
5530 eassert (OVERLAYP (overlay));
5531 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5532 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5533
5534 if (end < charpos)
5535 break;
5536
5537 /* Skip this overlay if it doesn't start or end at IT's current
5538 position. */
5539 if (end != charpos && start != charpos)
5540 continue;
5541
5542 /* Skip this overlay if it doesn't apply to IT->w. */
5543 window = Foverlay_get (overlay, Qwindow);
5544 if (WINDOWP (window) && XWINDOW (window) != it->w)
5545 continue;
5546
5547 /* If the text ``under'' the overlay is invisible, both before-
5548 and after-strings from this overlay are visible; start and
5549 end position are indistinguishable. */
5550 invisible = Foverlay_get (overlay, Qinvisible);
5551 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5552
5553 /* If overlay has a non-empty before-string, record it. */
5554 if ((start == charpos || (end == charpos && invis_p))
5555 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5556 && SCHARS (str))
5557 RECORD_OVERLAY_STRING (overlay, str, 0);
5558
5559 /* If overlay has a non-empty after-string, record it. */
5560 if ((end == charpos || (start == charpos && invis_p))
5561 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5562 && SCHARS (str))
5563 RECORD_OVERLAY_STRING (overlay, str, 1);
5564 }
5565
5566 /* Process overlays after the overlay center. */
5567 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5568 {
5569 XSETMISC (overlay, ov);
5570 eassert (OVERLAYP (overlay));
5571 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5572 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5573
5574 if (start > charpos)
5575 break;
5576
5577 /* Skip this overlay if it doesn't start or end at IT's current
5578 position. */
5579 if (end != charpos && start != charpos)
5580 continue;
5581
5582 /* Skip this overlay if it doesn't apply to IT->w. */
5583 window = Foverlay_get (overlay, Qwindow);
5584 if (WINDOWP (window) && XWINDOW (window) != it->w)
5585 continue;
5586
5587 /* If the text ``under'' the overlay is invisible, it has a zero
5588 dimension, and both before- and after-strings apply. */
5589 invisible = Foverlay_get (overlay, Qinvisible);
5590 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5591
5592 /* If overlay has a non-empty before-string, record it. */
5593 if ((start == charpos || (end == charpos && invis_p))
5594 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5595 && SCHARS (str))
5596 RECORD_OVERLAY_STRING (overlay, str, 0);
5597
5598 /* If overlay has a non-empty after-string, record it. */
5599 if ((end == charpos || (start == charpos && invis_p))
5600 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5601 && SCHARS (str))
5602 RECORD_OVERLAY_STRING (overlay, str, 1);
5603 }
5604
5605 #undef RECORD_OVERLAY_STRING
5606
5607 /* Sort entries. */
5608 if (n > 1)
5609 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5610
5611 /* Record number of overlay strings, and where we computed it. */
5612 it->n_overlay_strings = n;
5613 it->overlay_strings_charpos = charpos;
5614
5615 /* IT->current.overlay_string_index is the number of overlay strings
5616 that have already been consumed by IT. Copy some of the
5617 remaining overlay strings to IT->overlay_strings. */
5618 i = 0;
5619 j = it->current.overlay_string_index;
5620 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5621 {
5622 it->overlay_strings[i] = entries[j].string;
5623 it->string_overlays[i++] = entries[j++].overlay;
5624 }
5625
5626 CHECK_IT (it);
5627 SAFE_FREE ();
5628 }
5629
5630
5631 /* Get the first chunk of overlay strings at IT's current buffer
5632 position, or at CHARPOS if that is > 0. Value is non-zero if at
5633 least one overlay string was found. */
5634
5635 static int
5636 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5637 {
5638 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5639 process. This fills IT->overlay_strings with strings, and sets
5640 IT->n_overlay_strings to the total number of strings to process.
5641 IT->pos.overlay_string_index has to be set temporarily to zero
5642 because load_overlay_strings needs this; it must be set to -1
5643 when no overlay strings are found because a zero value would
5644 indicate a position in the first overlay string. */
5645 it->current.overlay_string_index = 0;
5646 load_overlay_strings (it, charpos);
5647
5648 /* If we found overlay strings, set up IT to deliver display
5649 elements from the first one. Otherwise set up IT to deliver
5650 from current_buffer. */
5651 if (it->n_overlay_strings)
5652 {
5653 /* Make sure we know settings in current_buffer, so that we can
5654 restore meaningful values when we're done with the overlay
5655 strings. */
5656 if (compute_stop_p)
5657 compute_stop_pos (it);
5658 eassert (it->face_id >= 0);
5659
5660 /* Save IT's settings. They are restored after all overlay
5661 strings have been processed. */
5662 eassert (!compute_stop_p || it->sp == 0);
5663
5664 /* When called from handle_stop, there might be an empty display
5665 string loaded. In that case, don't bother saving it. But
5666 don't use this optimization with the bidi iterator, since we
5667 need the corresponding pop_it call to resync the bidi
5668 iterator's position with IT's position, after we are done
5669 with the overlay strings. (The corresponding call to pop_it
5670 in case of an empty display string is in
5671 next_overlay_string.) */
5672 if (!(!it->bidi_p
5673 && STRINGP (it->string) && !SCHARS (it->string)))
5674 push_it (it, NULL);
5675
5676 /* Set up IT to deliver display elements from the first overlay
5677 string. */
5678 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5679 it->string = it->overlay_strings[0];
5680 it->from_overlay = Qnil;
5681 it->stop_charpos = 0;
5682 eassert (STRINGP (it->string));
5683 it->end_charpos = SCHARS (it->string);
5684 it->prev_stop = 0;
5685 it->base_level_stop = 0;
5686 it->multibyte_p = STRING_MULTIBYTE (it->string);
5687 it->method = GET_FROM_STRING;
5688 it->from_disp_prop_p = 0;
5689
5690 /* Force paragraph direction to be that of the parent
5691 buffer. */
5692 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5693 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5694 else
5695 it->paragraph_embedding = L2R;
5696
5697 /* Set up the bidi iterator for this overlay string. */
5698 if (it->bidi_p)
5699 {
5700 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5701
5702 it->bidi_it.string.lstring = it->string;
5703 it->bidi_it.string.s = NULL;
5704 it->bidi_it.string.schars = SCHARS (it->string);
5705 it->bidi_it.string.bufpos = pos;
5706 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5707 it->bidi_it.string.unibyte = !it->multibyte_p;
5708 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5709 }
5710 return 1;
5711 }
5712
5713 it->current.overlay_string_index = -1;
5714 return 0;
5715 }
5716
5717 static int
5718 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5719 {
5720 it->string = Qnil;
5721 it->method = GET_FROM_BUFFER;
5722
5723 (void) get_overlay_strings_1 (it, charpos, 1);
5724
5725 CHECK_IT (it);
5726
5727 /* Value is non-zero if we found at least one overlay string. */
5728 return STRINGP (it->string);
5729 }
5730
5731
5732 \f
5733 /***********************************************************************
5734 Saving and restoring state
5735 ***********************************************************************/
5736
5737 /* Save current settings of IT on IT->stack. Called, for example,
5738 before setting up IT for an overlay string, to be able to restore
5739 IT's settings to what they were after the overlay string has been
5740 processed. If POSITION is non-NULL, it is the position to save on
5741 the stack instead of IT->position. */
5742
5743 static void
5744 push_it (struct it *it, struct text_pos *position)
5745 {
5746 struct iterator_stack_entry *p;
5747
5748 eassert (it->sp < IT_STACK_SIZE);
5749 p = it->stack + it->sp;
5750
5751 p->stop_charpos = it->stop_charpos;
5752 p->prev_stop = it->prev_stop;
5753 p->base_level_stop = it->base_level_stop;
5754 p->cmp_it = it->cmp_it;
5755 eassert (it->face_id >= 0);
5756 p->face_id = it->face_id;
5757 p->string = it->string;
5758 p->method = it->method;
5759 p->from_overlay = it->from_overlay;
5760 switch (p->method)
5761 {
5762 case GET_FROM_IMAGE:
5763 p->u.image.object = it->object;
5764 p->u.image.image_id = it->image_id;
5765 p->u.image.slice = it->slice;
5766 break;
5767 case GET_FROM_STRETCH:
5768 p->u.stretch.object = it->object;
5769 break;
5770 }
5771 p->position = position ? *position : it->position;
5772 p->current = it->current;
5773 p->end_charpos = it->end_charpos;
5774 p->string_nchars = it->string_nchars;
5775 p->area = it->area;
5776 p->multibyte_p = it->multibyte_p;
5777 p->avoid_cursor_p = it->avoid_cursor_p;
5778 p->space_width = it->space_width;
5779 p->font_height = it->font_height;
5780 p->voffset = it->voffset;
5781 p->string_from_display_prop_p = it->string_from_display_prop_p;
5782 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5783 p->display_ellipsis_p = 0;
5784 p->line_wrap = it->line_wrap;
5785 p->bidi_p = it->bidi_p;
5786 p->paragraph_embedding = it->paragraph_embedding;
5787 p->from_disp_prop_p = it->from_disp_prop_p;
5788 ++it->sp;
5789
5790 /* Save the state of the bidi iterator as well. */
5791 if (it->bidi_p)
5792 bidi_push_it (&it->bidi_it);
5793 }
5794
5795 static void
5796 iterate_out_of_display_property (struct it *it)
5797 {
5798 int buffer_p = !STRINGP (it->string);
5799 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5800 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5801
5802 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5803
5804 /* Maybe initialize paragraph direction. If we are at the beginning
5805 of a new paragraph, next_element_from_buffer may not have a
5806 chance to do that. */
5807 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5808 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5809 /* prev_stop can be zero, so check against BEGV as well. */
5810 while (it->bidi_it.charpos >= bob
5811 && it->prev_stop <= it->bidi_it.charpos
5812 && it->bidi_it.charpos < CHARPOS (it->position)
5813 && it->bidi_it.charpos < eob)
5814 bidi_move_to_visually_next (&it->bidi_it);
5815 /* Record the stop_pos we just crossed, for when we cross it
5816 back, maybe. */
5817 if (it->bidi_it.charpos > CHARPOS (it->position))
5818 it->prev_stop = CHARPOS (it->position);
5819 /* If we ended up not where pop_it put us, resync IT's
5820 positional members with the bidi iterator. */
5821 if (it->bidi_it.charpos != CHARPOS (it->position))
5822 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5823 if (buffer_p)
5824 it->current.pos = it->position;
5825 else
5826 it->current.string_pos = it->position;
5827 }
5828
5829 /* Restore IT's settings from IT->stack. Called, for example, when no
5830 more overlay strings must be processed, and we return to delivering
5831 display elements from a buffer, or when the end of a string from a
5832 `display' property is reached and we return to delivering display
5833 elements from an overlay string, or from a buffer. */
5834
5835 static void
5836 pop_it (struct it *it)
5837 {
5838 struct iterator_stack_entry *p;
5839 int from_display_prop = it->from_disp_prop_p;
5840
5841 eassert (it->sp > 0);
5842 --it->sp;
5843 p = it->stack + it->sp;
5844 it->stop_charpos = p->stop_charpos;
5845 it->prev_stop = p->prev_stop;
5846 it->base_level_stop = p->base_level_stop;
5847 it->cmp_it = p->cmp_it;
5848 it->face_id = p->face_id;
5849 it->current = p->current;
5850 it->position = p->position;
5851 it->string = p->string;
5852 it->from_overlay = p->from_overlay;
5853 if (NILP (it->string))
5854 SET_TEXT_POS (it->current.string_pos, -1, -1);
5855 it->method = p->method;
5856 switch (it->method)
5857 {
5858 case GET_FROM_IMAGE:
5859 it->image_id = p->u.image.image_id;
5860 it->object = p->u.image.object;
5861 it->slice = p->u.image.slice;
5862 break;
5863 case GET_FROM_STRETCH:
5864 it->object = p->u.stretch.object;
5865 break;
5866 case GET_FROM_BUFFER:
5867 it->object = it->w->buffer;
5868 break;
5869 case GET_FROM_STRING:
5870 it->object = it->string;
5871 break;
5872 case GET_FROM_DISPLAY_VECTOR:
5873 if (it->s)
5874 it->method = GET_FROM_C_STRING;
5875 else if (STRINGP (it->string))
5876 it->method = GET_FROM_STRING;
5877 else
5878 {
5879 it->method = GET_FROM_BUFFER;
5880 it->object = it->w->buffer;
5881 }
5882 }
5883 it->end_charpos = p->end_charpos;
5884 it->string_nchars = p->string_nchars;
5885 it->area = p->area;
5886 it->multibyte_p = p->multibyte_p;
5887 it->avoid_cursor_p = p->avoid_cursor_p;
5888 it->space_width = p->space_width;
5889 it->font_height = p->font_height;
5890 it->voffset = p->voffset;
5891 it->string_from_display_prop_p = p->string_from_display_prop_p;
5892 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5893 it->line_wrap = p->line_wrap;
5894 it->bidi_p = p->bidi_p;
5895 it->paragraph_embedding = p->paragraph_embedding;
5896 it->from_disp_prop_p = p->from_disp_prop_p;
5897 if (it->bidi_p)
5898 {
5899 bidi_pop_it (&it->bidi_it);
5900 /* Bidi-iterate until we get out of the portion of text, if any,
5901 covered by a `display' text property or by an overlay with
5902 `display' property. (We cannot just jump there, because the
5903 internal coherency of the bidi iterator state can not be
5904 preserved across such jumps.) We also must determine the
5905 paragraph base direction if the overlay we just processed is
5906 at the beginning of a new paragraph. */
5907 if (from_display_prop
5908 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5909 iterate_out_of_display_property (it);
5910
5911 eassert ((BUFFERP (it->object)
5912 && IT_CHARPOS (*it) == it->bidi_it.charpos
5913 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5914 || (STRINGP (it->object)
5915 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5916 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5917 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5918 }
5919 }
5920
5921
5922 \f
5923 /***********************************************************************
5924 Moving over lines
5925 ***********************************************************************/
5926
5927 /* Set IT's current position to the previous line start. */
5928
5929 static void
5930 back_to_previous_line_start (struct it *it)
5931 {
5932 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5933 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5934 }
5935
5936
5937 /* Move IT to the next line start.
5938
5939 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5940 we skipped over part of the text (as opposed to moving the iterator
5941 continuously over the text). Otherwise, don't change the value
5942 of *SKIPPED_P.
5943
5944 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
5945 iterator on the newline, if it was found.
5946
5947 Newlines may come from buffer text, overlay strings, or strings
5948 displayed via the `display' property. That's the reason we can't
5949 simply use find_next_newline_no_quit.
5950
5951 Note that this function may not skip over invisible text that is so
5952 because of text properties and immediately follows a newline. If
5953 it would, function reseat_at_next_visible_line_start, when called
5954 from set_iterator_to_next, would effectively make invisible
5955 characters following a newline part of the wrong glyph row, which
5956 leads to wrong cursor motion. */
5957
5958 static int
5959 forward_to_next_line_start (struct it *it, int *skipped_p,
5960 struct bidi_it *bidi_it_prev)
5961 {
5962 ptrdiff_t old_selective;
5963 int newline_found_p, n;
5964 const int MAX_NEWLINE_DISTANCE = 500;
5965
5966 /* If already on a newline, just consume it to avoid unintended
5967 skipping over invisible text below. */
5968 if (it->what == IT_CHARACTER
5969 && it->c == '\n'
5970 && CHARPOS (it->position) == IT_CHARPOS (*it))
5971 {
5972 if (it->bidi_p && bidi_it_prev)
5973 *bidi_it_prev = it->bidi_it;
5974 set_iterator_to_next (it, 0);
5975 it->c = 0;
5976 return 1;
5977 }
5978
5979 /* Don't handle selective display in the following. It's (a)
5980 unnecessary because it's done by the caller, and (b) leads to an
5981 infinite recursion because next_element_from_ellipsis indirectly
5982 calls this function. */
5983 old_selective = it->selective;
5984 it->selective = 0;
5985
5986 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5987 from buffer text. */
5988 for (n = newline_found_p = 0;
5989 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5990 n += STRINGP (it->string) ? 0 : 1)
5991 {
5992 if (!get_next_display_element (it))
5993 return 0;
5994 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5995 if (newline_found_p && it->bidi_p && bidi_it_prev)
5996 *bidi_it_prev = it->bidi_it;
5997 set_iterator_to_next (it, 0);
5998 }
5999
6000 /* If we didn't find a newline near enough, see if we can use a
6001 short-cut. */
6002 if (!newline_found_p)
6003 {
6004 ptrdiff_t start = IT_CHARPOS (*it);
6005 ptrdiff_t limit = find_next_newline_no_quit (start, 1);
6006 Lisp_Object pos;
6007
6008 eassert (!STRINGP (it->string));
6009
6010 /* If there isn't any `display' property in sight, and no
6011 overlays, we can just use the position of the newline in
6012 buffer text. */
6013 if (it->stop_charpos >= limit
6014 || ((pos = Fnext_single_property_change (make_number (start),
6015 Qdisplay, Qnil,
6016 make_number (limit)),
6017 NILP (pos))
6018 && next_overlay_change (start) == ZV))
6019 {
6020 if (!it->bidi_p)
6021 {
6022 IT_CHARPOS (*it) = limit;
6023 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
6024 }
6025 else
6026 {
6027 struct bidi_it bprev;
6028
6029 /* Help bidi.c avoid expensive searches for display
6030 properties and overlays, by telling it that there are
6031 none up to `limit'. */
6032 if (it->bidi_it.disp_pos < limit)
6033 {
6034 it->bidi_it.disp_pos = limit;
6035 it->bidi_it.disp_prop = 0;
6036 }
6037 do {
6038 bprev = it->bidi_it;
6039 bidi_move_to_visually_next (&it->bidi_it);
6040 } while (it->bidi_it.charpos != limit);
6041 IT_CHARPOS (*it) = limit;
6042 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6043 if (bidi_it_prev)
6044 *bidi_it_prev = bprev;
6045 }
6046 *skipped_p = newline_found_p = 1;
6047 }
6048 else
6049 {
6050 while (get_next_display_element (it)
6051 && !newline_found_p)
6052 {
6053 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6054 if (newline_found_p && it->bidi_p && bidi_it_prev)
6055 *bidi_it_prev = it->bidi_it;
6056 set_iterator_to_next (it, 0);
6057 }
6058 }
6059 }
6060
6061 it->selective = old_selective;
6062 return newline_found_p;
6063 }
6064
6065
6066 /* Set IT's current position to the previous visible line start. Skip
6067 invisible text that is so either due to text properties or due to
6068 selective display. Caution: this does not change IT->current_x and
6069 IT->hpos. */
6070
6071 static void
6072 back_to_previous_visible_line_start (struct it *it)
6073 {
6074 while (IT_CHARPOS (*it) > BEGV)
6075 {
6076 back_to_previous_line_start (it);
6077
6078 if (IT_CHARPOS (*it) <= BEGV)
6079 break;
6080
6081 /* If selective > 0, then lines indented more than its value are
6082 invisible. */
6083 if (it->selective > 0
6084 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6085 it->selective))
6086 continue;
6087
6088 /* Check the newline before point for invisibility. */
6089 {
6090 Lisp_Object prop;
6091 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6092 Qinvisible, it->window);
6093 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6094 continue;
6095 }
6096
6097 if (IT_CHARPOS (*it) <= BEGV)
6098 break;
6099
6100 {
6101 struct it it2;
6102 void *it2data = NULL;
6103 ptrdiff_t pos;
6104 ptrdiff_t beg, end;
6105 Lisp_Object val, overlay;
6106
6107 SAVE_IT (it2, *it, it2data);
6108
6109 /* If newline is part of a composition, continue from start of composition */
6110 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6111 && beg < IT_CHARPOS (*it))
6112 goto replaced;
6113
6114 /* If newline is replaced by a display property, find start of overlay
6115 or interval and continue search from that point. */
6116 pos = --IT_CHARPOS (it2);
6117 --IT_BYTEPOS (it2);
6118 it2.sp = 0;
6119 bidi_unshelve_cache (NULL, 0);
6120 it2.string_from_display_prop_p = 0;
6121 it2.from_disp_prop_p = 0;
6122 if (handle_display_prop (&it2) == HANDLED_RETURN
6123 && !NILP (val = get_char_property_and_overlay
6124 (make_number (pos), Qdisplay, Qnil, &overlay))
6125 && (OVERLAYP (overlay)
6126 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6127 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6128 {
6129 RESTORE_IT (it, it, it2data);
6130 goto replaced;
6131 }
6132
6133 /* Newline is not replaced by anything -- so we are done. */
6134 RESTORE_IT (it, it, it2data);
6135 break;
6136
6137 replaced:
6138 if (beg < BEGV)
6139 beg = BEGV;
6140 IT_CHARPOS (*it) = beg;
6141 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6142 }
6143 }
6144
6145 it->continuation_lines_width = 0;
6146
6147 eassert (IT_CHARPOS (*it) >= BEGV);
6148 eassert (IT_CHARPOS (*it) == BEGV
6149 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6150 CHECK_IT (it);
6151 }
6152
6153
6154 /* Reseat iterator IT at the previous visible line start. Skip
6155 invisible text that is so either due to text properties or due to
6156 selective display. At the end, update IT's overlay information,
6157 face information etc. */
6158
6159 void
6160 reseat_at_previous_visible_line_start (struct it *it)
6161 {
6162 back_to_previous_visible_line_start (it);
6163 reseat (it, it->current.pos, 1);
6164 CHECK_IT (it);
6165 }
6166
6167
6168 /* Reseat iterator IT on the next visible line start in the current
6169 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6170 preceding the line start. Skip over invisible text that is so
6171 because of selective display. Compute faces, overlays etc at the
6172 new position. Note that this function does not skip over text that
6173 is invisible because of text properties. */
6174
6175 static void
6176 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6177 {
6178 int newline_found_p, skipped_p = 0;
6179 struct bidi_it bidi_it_prev;
6180
6181 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6182
6183 /* Skip over lines that are invisible because they are indented
6184 more than the value of IT->selective. */
6185 if (it->selective > 0)
6186 while (IT_CHARPOS (*it) < ZV
6187 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6188 it->selective))
6189 {
6190 eassert (IT_BYTEPOS (*it) == BEGV
6191 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6192 newline_found_p =
6193 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6194 }
6195
6196 /* Position on the newline if that's what's requested. */
6197 if (on_newline_p && newline_found_p)
6198 {
6199 if (STRINGP (it->string))
6200 {
6201 if (IT_STRING_CHARPOS (*it) > 0)
6202 {
6203 if (!it->bidi_p)
6204 {
6205 --IT_STRING_CHARPOS (*it);
6206 --IT_STRING_BYTEPOS (*it);
6207 }
6208 else
6209 {
6210 /* We need to restore the bidi iterator to the state
6211 it had on the newline, and resync the IT's
6212 position with that. */
6213 it->bidi_it = bidi_it_prev;
6214 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6215 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6216 }
6217 }
6218 }
6219 else if (IT_CHARPOS (*it) > BEGV)
6220 {
6221 if (!it->bidi_p)
6222 {
6223 --IT_CHARPOS (*it);
6224 --IT_BYTEPOS (*it);
6225 }
6226 else
6227 {
6228 /* We need to restore the bidi iterator to the state it
6229 had on the newline and resync IT with that. */
6230 it->bidi_it = bidi_it_prev;
6231 IT_CHARPOS (*it) = it->bidi_it.charpos;
6232 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6233 }
6234 reseat (it, it->current.pos, 0);
6235 }
6236 }
6237 else if (skipped_p)
6238 reseat (it, it->current.pos, 0);
6239
6240 CHECK_IT (it);
6241 }
6242
6243
6244 \f
6245 /***********************************************************************
6246 Changing an iterator's position
6247 ***********************************************************************/
6248
6249 /* Change IT's current position to POS in current_buffer. If FORCE_P
6250 is non-zero, always check for text properties at the new position.
6251 Otherwise, text properties are only looked up if POS >=
6252 IT->check_charpos of a property. */
6253
6254 static void
6255 reseat (struct it *it, struct text_pos pos, int force_p)
6256 {
6257 ptrdiff_t original_pos = IT_CHARPOS (*it);
6258
6259 reseat_1 (it, pos, 0);
6260
6261 /* Determine where to check text properties. Avoid doing it
6262 where possible because text property lookup is very expensive. */
6263 if (force_p
6264 || CHARPOS (pos) > it->stop_charpos
6265 || CHARPOS (pos) < original_pos)
6266 {
6267 if (it->bidi_p)
6268 {
6269 /* For bidi iteration, we need to prime prev_stop and
6270 base_level_stop with our best estimations. */
6271 /* Implementation note: Of course, POS is not necessarily a
6272 stop position, so assigning prev_pos to it is a lie; we
6273 should have called compute_stop_backwards. However, if
6274 the current buffer does not include any R2L characters,
6275 that call would be a waste of cycles, because the
6276 iterator will never move back, and thus never cross this
6277 "fake" stop position. So we delay that backward search
6278 until the time we really need it, in next_element_from_buffer. */
6279 if (CHARPOS (pos) != it->prev_stop)
6280 it->prev_stop = CHARPOS (pos);
6281 if (CHARPOS (pos) < it->base_level_stop)
6282 it->base_level_stop = 0; /* meaning it's unknown */
6283 handle_stop (it);
6284 }
6285 else
6286 {
6287 handle_stop (it);
6288 it->prev_stop = it->base_level_stop = 0;
6289 }
6290
6291 }
6292
6293 CHECK_IT (it);
6294 }
6295
6296
6297 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6298 IT->stop_pos to POS, also. */
6299
6300 static void
6301 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6302 {
6303 /* Don't call this function when scanning a C string. */
6304 eassert (it->s == NULL);
6305
6306 /* POS must be a reasonable value. */
6307 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6308
6309 it->current.pos = it->position = pos;
6310 it->end_charpos = ZV;
6311 it->dpvec = NULL;
6312 it->current.dpvec_index = -1;
6313 it->current.overlay_string_index = -1;
6314 IT_STRING_CHARPOS (*it) = -1;
6315 IT_STRING_BYTEPOS (*it) = -1;
6316 it->string = Qnil;
6317 it->method = GET_FROM_BUFFER;
6318 it->object = it->w->buffer;
6319 it->area = TEXT_AREA;
6320 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6321 it->sp = 0;
6322 it->string_from_display_prop_p = 0;
6323 it->string_from_prefix_prop_p = 0;
6324
6325 it->from_disp_prop_p = 0;
6326 it->face_before_selective_p = 0;
6327 if (it->bidi_p)
6328 {
6329 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6330 &it->bidi_it);
6331 bidi_unshelve_cache (NULL, 0);
6332 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6333 it->bidi_it.string.s = NULL;
6334 it->bidi_it.string.lstring = Qnil;
6335 it->bidi_it.string.bufpos = 0;
6336 it->bidi_it.string.unibyte = 0;
6337 }
6338
6339 if (set_stop_p)
6340 {
6341 it->stop_charpos = CHARPOS (pos);
6342 it->base_level_stop = CHARPOS (pos);
6343 }
6344 /* This make the information stored in it->cmp_it invalidate. */
6345 it->cmp_it.id = -1;
6346 }
6347
6348
6349 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6350 If S is non-null, it is a C string to iterate over. Otherwise,
6351 STRING gives a Lisp string to iterate over.
6352
6353 If PRECISION > 0, don't return more then PRECISION number of
6354 characters from the string.
6355
6356 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6357 characters have been returned. FIELD_WIDTH < 0 means an infinite
6358 field width.
6359
6360 MULTIBYTE = 0 means disable processing of multibyte characters,
6361 MULTIBYTE > 0 means enable it,
6362 MULTIBYTE < 0 means use IT->multibyte_p.
6363
6364 IT must be initialized via a prior call to init_iterator before
6365 calling this function. */
6366
6367 static void
6368 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6369 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6370 int multibyte)
6371 {
6372 /* No region in strings. */
6373 it->region_beg_charpos = it->region_end_charpos = -1;
6374
6375 /* No text property checks performed by default, but see below. */
6376 it->stop_charpos = -1;
6377
6378 /* Set iterator position and end position. */
6379 memset (&it->current, 0, sizeof it->current);
6380 it->current.overlay_string_index = -1;
6381 it->current.dpvec_index = -1;
6382 eassert (charpos >= 0);
6383
6384 /* If STRING is specified, use its multibyteness, otherwise use the
6385 setting of MULTIBYTE, if specified. */
6386 if (multibyte >= 0)
6387 it->multibyte_p = multibyte > 0;
6388
6389 /* Bidirectional reordering of strings is controlled by the default
6390 value of bidi-display-reordering. Don't try to reorder while
6391 loading loadup.el, as the necessary character property tables are
6392 not yet available. */
6393 it->bidi_p =
6394 NILP (Vpurify_flag)
6395 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6396
6397 if (s == NULL)
6398 {
6399 eassert (STRINGP (string));
6400 it->string = string;
6401 it->s = NULL;
6402 it->end_charpos = it->string_nchars = SCHARS (string);
6403 it->method = GET_FROM_STRING;
6404 it->current.string_pos = string_pos (charpos, string);
6405
6406 if (it->bidi_p)
6407 {
6408 it->bidi_it.string.lstring = string;
6409 it->bidi_it.string.s = NULL;
6410 it->bidi_it.string.schars = it->end_charpos;
6411 it->bidi_it.string.bufpos = 0;
6412 it->bidi_it.string.from_disp_str = 0;
6413 it->bidi_it.string.unibyte = !it->multibyte_p;
6414 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6415 FRAME_WINDOW_P (it->f), &it->bidi_it);
6416 }
6417 }
6418 else
6419 {
6420 it->s = (const unsigned char *) s;
6421 it->string = Qnil;
6422
6423 /* Note that we use IT->current.pos, not it->current.string_pos,
6424 for displaying C strings. */
6425 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6426 if (it->multibyte_p)
6427 {
6428 it->current.pos = c_string_pos (charpos, s, 1);
6429 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6430 }
6431 else
6432 {
6433 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6434 it->end_charpos = it->string_nchars = strlen (s);
6435 }
6436
6437 if (it->bidi_p)
6438 {
6439 it->bidi_it.string.lstring = Qnil;
6440 it->bidi_it.string.s = (const unsigned char *) s;
6441 it->bidi_it.string.schars = it->end_charpos;
6442 it->bidi_it.string.bufpos = 0;
6443 it->bidi_it.string.from_disp_str = 0;
6444 it->bidi_it.string.unibyte = !it->multibyte_p;
6445 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6446 &it->bidi_it);
6447 }
6448 it->method = GET_FROM_C_STRING;
6449 }
6450
6451 /* PRECISION > 0 means don't return more than PRECISION characters
6452 from the string. */
6453 if (precision > 0 && it->end_charpos - charpos > precision)
6454 {
6455 it->end_charpos = it->string_nchars = charpos + precision;
6456 if (it->bidi_p)
6457 it->bidi_it.string.schars = it->end_charpos;
6458 }
6459
6460 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6461 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6462 FIELD_WIDTH < 0 means infinite field width. This is useful for
6463 padding with `-' at the end of a mode line. */
6464 if (field_width < 0)
6465 field_width = INFINITY;
6466 /* Implementation note: We deliberately don't enlarge
6467 it->bidi_it.string.schars here to fit it->end_charpos, because
6468 the bidi iterator cannot produce characters out of thin air. */
6469 if (field_width > it->end_charpos - charpos)
6470 it->end_charpos = charpos + field_width;
6471
6472 /* Use the standard display table for displaying strings. */
6473 if (DISP_TABLE_P (Vstandard_display_table))
6474 it->dp = XCHAR_TABLE (Vstandard_display_table);
6475
6476 it->stop_charpos = charpos;
6477 it->prev_stop = charpos;
6478 it->base_level_stop = 0;
6479 if (it->bidi_p)
6480 {
6481 it->bidi_it.first_elt = 1;
6482 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6483 it->bidi_it.disp_pos = -1;
6484 }
6485 if (s == NULL && it->multibyte_p)
6486 {
6487 ptrdiff_t endpos = SCHARS (it->string);
6488 if (endpos > it->end_charpos)
6489 endpos = it->end_charpos;
6490 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6491 it->string);
6492 }
6493 CHECK_IT (it);
6494 }
6495
6496
6497 \f
6498 /***********************************************************************
6499 Iteration
6500 ***********************************************************************/
6501
6502 /* Map enum it_method value to corresponding next_element_from_* function. */
6503
6504 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6505 {
6506 next_element_from_buffer,
6507 next_element_from_display_vector,
6508 next_element_from_string,
6509 next_element_from_c_string,
6510 next_element_from_image,
6511 next_element_from_stretch
6512 };
6513
6514 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6515
6516
6517 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6518 (possibly with the following characters). */
6519
6520 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6521 ((IT)->cmp_it.id >= 0 \
6522 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6523 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6524 END_CHARPOS, (IT)->w, \
6525 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6526 (IT)->string)))
6527
6528
6529 /* Lookup the char-table Vglyphless_char_display for character C (-1
6530 if we want information for no-font case), and return the display
6531 method symbol. By side-effect, update it->what and
6532 it->glyphless_method. This function is called from
6533 get_next_display_element for each character element, and from
6534 x_produce_glyphs when no suitable font was found. */
6535
6536 Lisp_Object
6537 lookup_glyphless_char_display (int c, struct it *it)
6538 {
6539 Lisp_Object glyphless_method = Qnil;
6540
6541 if (CHAR_TABLE_P (Vglyphless_char_display)
6542 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6543 {
6544 if (c >= 0)
6545 {
6546 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6547 if (CONSP (glyphless_method))
6548 glyphless_method = FRAME_WINDOW_P (it->f)
6549 ? XCAR (glyphless_method)
6550 : XCDR (glyphless_method);
6551 }
6552 else
6553 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6554 }
6555
6556 retry:
6557 if (NILP (glyphless_method))
6558 {
6559 if (c >= 0)
6560 /* The default is to display the character by a proper font. */
6561 return Qnil;
6562 /* The default for the no-font case is to display an empty box. */
6563 glyphless_method = Qempty_box;
6564 }
6565 if (EQ (glyphless_method, Qzero_width))
6566 {
6567 if (c >= 0)
6568 return glyphless_method;
6569 /* This method can't be used for the no-font case. */
6570 glyphless_method = Qempty_box;
6571 }
6572 if (EQ (glyphless_method, Qthin_space))
6573 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6574 else if (EQ (glyphless_method, Qempty_box))
6575 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6576 else if (EQ (glyphless_method, Qhex_code))
6577 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6578 else if (STRINGP (glyphless_method))
6579 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6580 else
6581 {
6582 /* Invalid value. We use the default method. */
6583 glyphless_method = Qnil;
6584 goto retry;
6585 }
6586 it->what = IT_GLYPHLESS;
6587 return glyphless_method;
6588 }
6589
6590 /* Load IT's display element fields with information about the next
6591 display element from the current position of IT. Value is zero if
6592 end of buffer (or C string) is reached. */
6593
6594 static struct frame *last_escape_glyph_frame = NULL;
6595 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6596 static int last_escape_glyph_merged_face_id = 0;
6597
6598 struct frame *last_glyphless_glyph_frame = NULL;
6599 int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6600 int last_glyphless_glyph_merged_face_id = 0;
6601
6602 static int
6603 get_next_display_element (struct it *it)
6604 {
6605 /* Non-zero means that we found a display element. Zero means that
6606 we hit the end of what we iterate over. Performance note: the
6607 function pointer `method' used here turns out to be faster than
6608 using a sequence of if-statements. */
6609 int success_p;
6610
6611 get_next:
6612 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6613
6614 if (it->what == IT_CHARACTER)
6615 {
6616 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6617 and only if (a) the resolved directionality of that character
6618 is R..." */
6619 /* FIXME: Do we need an exception for characters from display
6620 tables? */
6621 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6622 it->c = bidi_mirror_char (it->c);
6623 /* Map via display table or translate control characters.
6624 IT->c, IT->len etc. have been set to the next character by
6625 the function call above. If we have a display table, and it
6626 contains an entry for IT->c, translate it. Don't do this if
6627 IT->c itself comes from a display table, otherwise we could
6628 end up in an infinite recursion. (An alternative could be to
6629 count the recursion depth of this function and signal an
6630 error when a certain maximum depth is reached.) Is it worth
6631 it? */
6632 if (success_p && it->dpvec == NULL)
6633 {
6634 Lisp_Object dv;
6635 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6636 int nonascii_space_p = 0;
6637 int nonascii_hyphen_p = 0;
6638 int c = it->c; /* This is the character to display. */
6639
6640 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6641 {
6642 eassert (SINGLE_BYTE_CHAR_P (c));
6643 if (unibyte_display_via_language_environment)
6644 {
6645 c = DECODE_CHAR (unibyte, c);
6646 if (c < 0)
6647 c = BYTE8_TO_CHAR (it->c);
6648 }
6649 else
6650 c = BYTE8_TO_CHAR (it->c);
6651 }
6652
6653 if (it->dp
6654 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6655 VECTORP (dv)))
6656 {
6657 struct Lisp_Vector *v = XVECTOR (dv);
6658
6659 /* Return the first character from the display table
6660 entry, if not empty. If empty, don't display the
6661 current character. */
6662 if (v->header.size)
6663 {
6664 it->dpvec_char_len = it->len;
6665 it->dpvec = v->contents;
6666 it->dpend = v->contents + v->header.size;
6667 it->current.dpvec_index = 0;
6668 it->dpvec_face_id = -1;
6669 it->saved_face_id = it->face_id;
6670 it->method = GET_FROM_DISPLAY_VECTOR;
6671 it->ellipsis_p = 0;
6672 }
6673 else
6674 {
6675 set_iterator_to_next (it, 0);
6676 }
6677 goto get_next;
6678 }
6679
6680 if (! NILP (lookup_glyphless_char_display (c, it)))
6681 {
6682 if (it->what == IT_GLYPHLESS)
6683 goto done;
6684 /* Don't display this character. */
6685 set_iterator_to_next (it, 0);
6686 goto get_next;
6687 }
6688
6689 /* If `nobreak-char-display' is non-nil, we display
6690 non-ASCII spaces and hyphens specially. */
6691 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6692 {
6693 if (c == 0xA0)
6694 nonascii_space_p = 1;
6695 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6696 nonascii_hyphen_p = 1;
6697 }
6698
6699 /* Translate control characters into `\003' or `^C' form.
6700 Control characters coming from a display table entry are
6701 currently not translated because we use IT->dpvec to hold
6702 the translation. This could easily be changed but I
6703 don't believe that it is worth doing.
6704
6705 The characters handled by `nobreak-char-display' must be
6706 translated too.
6707
6708 Non-printable characters and raw-byte characters are also
6709 translated to octal form. */
6710 if (((c < ' ' || c == 127) /* ASCII control chars */
6711 ? (it->area != TEXT_AREA
6712 /* In mode line, treat \n, \t like other crl chars. */
6713 || (c != '\t'
6714 && it->glyph_row
6715 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6716 || (c != '\n' && c != '\t'))
6717 : (nonascii_space_p
6718 || nonascii_hyphen_p
6719 || CHAR_BYTE8_P (c)
6720 || ! CHAR_PRINTABLE_P (c))))
6721 {
6722 /* C is a control character, non-ASCII space/hyphen,
6723 raw-byte, or a non-printable character which must be
6724 displayed either as '\003' or as `^C' where the '\\'
6725 and '^' can be defined in the display table. Fill
6726 IT->ctl_chars with glyphs for what we have to
6727 display. Then, set IT->dpvec to these glyphs. */
6728 Lisp_Object gc;
6729 int ctl_len;
6730 int face_id;
6731 int lface_id = 0;
6732 int escape_glyph;
6733
6734 /* Handle control characters with ^. */
6735
6736 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6737 {
6738 int g;
6739
6740 g = '^'; /* default glyph for Control */
6741 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6742 if (it->dp
6743 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6744 {
6745 g = GLYPH_CODE_CHAR (gc);
6746 lface_id = GLYPH_CODE_FACE (gc);
6747 }
6748 if (lface_id)
6749 {
6750 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6751 }
6752 else if (it->f == last_escape_glyph_frame
6753 && it->face_id == last_escape_glyph_face_id)
6754 {
6755 face_id = last_escape_glyph_merged_face_id;
6756 }
6757 else
6758 {
6759 /* Merge the escape-glyph face into the current face. */
6760 face_id = merge_faces (it->f, Qescape_glyph, 0,
6761 it->face_id);
6762 last_escape_glyph_frame = it->f;
6763 last_escape_glyph_face_id = it->face_id;
6764 last_escape_glyph_merged_face_id = face_id;
6765 }
6766
6767 XSETINT (it->ctl_chars[0], g);
6768 XSETINT (it->ctl_chars[1], c ^ 0100);
6769 ctl_len = 2;
6770 goto display_control;
6771 }
6772
6773 /* Handle non-ascii space in the mode where it only gets
6774 highlighting. */
6775
6776 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6777 {
6778 /* Merge `nobreak-space' into the current face. */
6779 face_id = merge_faces (it->f, Qnobreak_space, 0,
6780 it->face_id);
6781 XSETINT (it->ctl_chars[0], ' ');
6782 ctl_len = 1;
6783 goto display_control;
6784 }
6785
6786 /* Handle sequences that start with the "escape glyph". */
6787
6788 /* the default escape glyph is \. */
6789 escape_glyph = '\\';
6790
6791 if (it->dp
6792 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6793 {
6794 escape_glyph = GLYPH_CODE_CHAR (gc);
6795 lface_id = GLYPH_CODE_FACE (gc);
6796 }
6797 if (lface_id)
6798 {
6799 /* The display table specified a face.
6800 Merge it into face_id and also into escape_glyph. */
6801 face_id = merge_faces (it->f, Qt, lface_id,
6802 it->face_id);
6803 }
6804 else if (it->f == last_escape_glyph_frame
6805 && it->face_id == last_escape_glyph_face_id)
6806 {
6807 face_id = last_escape_glyph_merged_face_id;
6808 }
6809 else
6810 {
6811 /* Merge the escape-glyph face into the current face. */
6812 face_id = merge_faces (it->f, Qescape_glyph, 0,
6813 it->face_id);
6814 last_escape_glyph_frame = it->f;
6815 last_escape_glyph_face_id = it->face_id;
6816 last_escape_glyph_merged_face_id = face_id;
6817 }
6818
6819 /* Draw non-ASCII hyphen with just highlighting: */
6820
6821 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6822 {
6823 XSETINT (it->ctl_chars[0], '-');
6824 ctl_len = 1;
6825 goto display_control;
6826 }
6827
6828 /* Draw non-ASCII space/hyphen with escape glyph: */
6829
6830 if (nonascii_space_p || nonascii_hyphen_p)
6831 {
6832 XSETINT (it->ctl_chars[0], escape_glyph);
6833 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6834 ctl_len = 2;
6835 goto display_control;
6836 }
6837
6838 {
6839 char str[10];
6840 int len, i;
6841
6842 if (CHAR_BYTE8_P (c))
6843 /* Display \200 instead of \17777600. */
6844 c = CHAR_TO_BYTE8 (c);
6845 len = sprintf (str, "%03o", c);
6846
6847 XSETINT (it->ctl_chars[0], escape_glyph);
6848 for (i = 0; i < len; i++)
6849 XSETINT (it->ctl_chars[i + 1], str[i]);
6850 ctl_len = len + 1;
6851 }
6852
6853 display_control:
6854 /* Set up IT->dpvec and return first character from it. */
6855 it->dpvec_char_len = it->len;
6856 it->dpvec = it->ctl_chars;
6857 it->dpend = it->dpvec + ctl_len;
6858 it->current.dpvec_index = 0;
6859 it->dpvec_face_id = face_id;
6860 it->saved_face_id = it->face_id;
6861 it->method = GET_FROM_DISPLAY_VECTOR;
6862 it->ellipsis_p = 0;
6863 goto get_next;
6864 }
6865 it->char_to_display = c;
6866 }
6867 else if (success_p)
6868 {
6869 it->char_to_display = it->c;
6870 }
6871 }
6872
6873 /* Adjust face id for a multibyte character. There are no multibyte
6874 character in unibyte text. */
6875 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6876 && it->multibyte_p
6877 && success_p
6878 && FRAME_WINDOW_P (it->f))
6879 {
6880 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6881
6882 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6883 {
6884 /* Automatic composition with glyph-string. */
6885 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6886
6887 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6888 }
6889 else
6890 {
6891 ptrdiff_t pos = (it->s ? -1
6892 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6893 : IT_CHARPOS (*it));
6894 int c;
6895
6896 if (it->what == IT_CHARACTER)
6897 c = it->char_to_display;
6898 else
6899 {
6900 struct composition *cmp = composition_table[it->cmp_it.id];
6901 int i;
6902
6903 c = ' ';
6904 for (i = 0; i < cmp->glyph_len; i++)
6905 /* TAB in a composition means display glyphs with
6906 padding space on the left or right. */
6907 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6908 break;
6909 }
6910 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6911 }
6912 }
6913
6914 done:
6915 /* Is this character the last one of a run of characters with
6916 box? If yes, set IT->end_of_box_run_p to 1. */
6917 if (it->face_box_p
6918 && it->s == NULL)
6919 {
6920 if (it->method == GET_FROM_STRING && it->sp)
6921 {
6922 int face_id = underlying_face_id (it);
6923 struct face *face = FACE_FROM_ID (it->f, face_id);
6924
6925 if (face)
6926 {
6927 if (face->box == FACE_NO_BOX)
6928 {
6929 /* If the box comes from face properties in a
6930 display string, check faces in that string. */
6931 int string_face_id = face_after_it_pos (it);
6932 it->end_of_box_run_p
6933 = (FACE_FROM_ID (it->f, string_face_id)->box
6934 == FACE_NO_BOX);
6935 }
6936 /* Otherwise, the box comes from the underlying face.
6937 If this is the last string character displayed, check
6938 the next buffer location. */
6939 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6940 && (it->current.overlay_string_index
6941 == it->n_overlay_strings - 1))
6942 {
6943 ptrdiff_t ignore;
6944 int next_face_id;
6945 struct text_pos pos = it->current.pos;
6946 INC_TEXT_POS (pos, it->multibyte_p);
6947
6948 next_face_id = face_at_buffer_position
6949 (it->w, CHARPOS (pos), it->region_beg_charpos,
6950 it->region_end_charpos, &ignore,
6951 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6952 -1);
6953 it->end_of_box_run_p
6954 = (FACE_FROM_ID (it->f, next_face_id)->box
6955 == FACE_NO_BOX);
6956 }
6957 }
6958 }
6959 else
6960 {
6961 int face_id = face_after_it_pos (it);
6962 it->end_of_box_run_p
6963 = (face_id != it->face_id
6964 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6965 }
6966 }
6967 /* If we reached the end of the object we've been iterating (e.g., a
6968 display string or an overlay string), and there's something on
6969 IT->stack, proceed with what's on the stack. It doesn't make
6970 sense to return zero if there's unprocessed stuff on the stack,
6971 because otherwise that stuff will never be displayed. */
6972 if (!success_p && it->sp > 0)
6973 {
6974 set_iterator_to_next (it, 0);
6975 success_p = get_next_display_element (it);
6976 }
6977
6978 /* Value is 0 if end of buffer or string reached. */
6979 return success_p;
6980 }
6981
6982
6983 /* Move IT to the next display element.
6984
6985 RESEAT_P non-zero means if called on a newline in buffer text,
6986 skip to the next visible line start.
6987
6988 Functions get_next_display_element and set_iterator_to_next are
6989 separate because I find this arrangement easier to handle than a
6990 get_next_display_element function that also increments IT's
6991 position. The way it is we can first look at an iterator's current
6992 display element, decide whether it fits on a line, and if it does,
6993 increment the iterator position. The other way around we probably
6994 would either need a flag indicating whether the iterator has to be
6995 incremented the next time, or we would have to implement a
6996 decrement position function which would not be easy to write. */
6997
6998 void
6999 set_iterator_to_next (struct it *it, int reseat_p)
7000 {
7001 /* Reset flags indicating start and end of a sequence of characters
7002 with box. Reset them at the start of this function because
7003 moving the iterator to a new position might set them. */
7004 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7005
7006 switch (it->method)
7007 {
7008 case GET_FROM_BUFFER:
7009 /* The current display element of IT is a character from
7010 current_buffer. Advance in the buffer, and maybe skip over
7011 invisible lines that are so because of selective display. */
7012 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7013 reseat_at_next_visible_line_start (it, 0);
7014 else if (it->cmp_it.id >= 0)
7015 {
7016 /* We are currently getting glyphs from a composition. */
7017 int i;
7018
7019 if (! it->bidi_p)
7020 {
7021 IT_CHARPOS (*it) += it->cmp_it.nchars;
7022 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7023 if (it->cmp_it.to < it->cmp_it.nglyphs)
7024 {
7025 it->cmp_it.from = it->cmp_it.to;
7026 }
7027 else
7028 {
7029 it->cmp_it.id = -1;
7030 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7031 IT_BYTEPOS (*it),
7032 it->end_charpos, Qnil);
7033 }
7034 }
7035 else if (! it->cmp_it.reversed_p)
7036 {
7037 /* Composition created while scanning forward. */
7038 /* Update IT's char/byte positions to point to the first
7039 character of the next grapheme cluster, or to the
7040 character visually after the current composition. */
7041 for (i = 0; i < it->cmp_it.nchars; i++)
7042 bidi_move_to_visually_next (&it->bidi_it);
7043 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7044 IT_CHARPOS (*it) = it->bidi_it.charpos;
7045
7046 if (it->cmp_it.to < it->cmp_it.nglyphs)
7047 {
7048 /* Proceed to the next grapheme cluster. */
7049 it->cmp_it.from = it->cmp_it.to;
7050 }
7051 else
7052 {
7053 /* No more grapheme clusters in this composition.
7054 Find the next stop position. */
7055 ptrdiff_t stop = it->end_charpos;
7056 if (it->bidi_it.scan_dir < 0)
7057 /* Now we are scanning backward and don't know
7058 where to stop. */
7059 stop = -1;
7060 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7061 IT_BYTEPOS (*it), stop, Qnil);
7062 }
7063 }
7064 else
7065 {
7066 /* Composition created while scanning backward. */
7067 /* Update IT's char/byte positions to point to the last
7068 character of the previous grapheme cluster, or the
7069 character visually after the current composition. */
7070 for (i = 0; i < it->cmp_it.nchars; i++)
7071 bidi_move_to_visually_next (&it->bidi_it);
7072 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7073 IT_CHARPOS (*it) = it->bidi_it.charpos;
7074 if (it->cmp_it.from > 0)
7075 {
7076 /* Proceed to the previous grapheme cluster. */
7077 it->cmp_it.to = it->cmp_it.from;
7078 }
7079 else
7080 {
7081 /* No more grapheme clusters in this composition.
7082 Find the next stop position. */
7083 ptrdiff_t stop = it->end_charpos;
7084 if (it->bidi_it.scan_dir < 0)
7085 /* Now we are scanning backward and don't know
7086 where to stop. */
7087 stop = -1;
7088 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7089 IT_BYTEPOS (*it), stop, Qnil);
7090 }
7091 }
7092 }
7093 else
7094 {
7095 eassert (it->len != 0);
7096
7097 if (!it->bidi_p)
7098 {
7099 IT_BYTEPOS (*it) += it->len;
7100 IT_CHARPOS (*it) += 1;
7101 }
7102 else
7103 {
7104 int prev_scan_dir = it->bidi_it.scan_dir;
7105 /* If this is a new paragraph, determine its base
7106 direction (a.k.a. its base embedding level). */
7107 if (it->bidi_it.new_paragraph)
7108 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7109 bidi_move_to_visually_next (&it->bidi_it);
7110 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7111 IT_CHARPOS (*it) = it->bidi_it.charpos;
7112 if (prev_scan_dir != it->bidi_it.scan_dir)
7113 {
7114 /* As the scan direction was changed, we must
7115 re-compute the stop position for composition. */
7116 ptrdiff_t stop = it->end_charpos;
7117 if (it->bidi_it.scan_dir < 0)
7118 stop = -1;
7119 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7120 IT_BYTEPOS (*it), stop, Qnil);
7121 }
7122 }
7123 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7124 }
7125 break;
7126
7127 case GET_FROM_C_STRING:
7128 /* Current display element of IT is from a C string. */
7129 if (!it->bidi_p
7130 /* If the string position is beyond string's end, it means
7131 next_element_from_c_string is padding the string with
7132 blanks, in which case we bypass the bidi iterator,
7133 because it cannot deal with such virtual characters. */
7134 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7135 {
7136 IT_BYTEPOS (*it) += it->len;
7137 IT_CHARPOS (*it) += 1;
7138 }
7139 else
7140 {
7141 bidi_move_to_visually_next (&it->bidi_it);
7142 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7143 IT_CHARPOS (*it) = it->bidi_it.charpos;
7144 }
7145 break;
7146
7147 case GET_FROM_DISPLAY_VECTOR:
7148 /* Current display element of IT is from a display table entry.
7149 Advance in the display table definition. Reset it to null if
7150 end reached, and continue with characters from buffers/
7151 strings. */
7152 ++it->current.dpvec_index;
7153
7154 /* Restore face of the iterator to what they were before the
7155 display vector entry (these entries may contain faces). */
7156 it->face_id = it->saved_face_id;
7157
7158 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7159 {
7160 int recheck_faces = it->ellipsis_p;
7161
7162 if (it->s)
7163 it->method = GET_FROM_C_STRING;
7164 else if (STRINGP (it->string))
7165 it->method = GET_FROM_STRING;
7166 else
7167 {
7168 it->method = GET_FROM_BUFFER;
7169 it->object = it->w->buffer;
7170 }
7171
7172 it->dpvec = NULL;
7173 it->current.dpvec_index = -1;
7174
7175 /* Skip over characters which were displayed via IT->dpvec. */
7176 if (it->dpvec_char_len < 0)
7177 reseat_at_next_visible_line_start (it, 1);
7178 else if (it->dpvec_char_len > 0)
7179 {
7180 if (it->method == GET_FROM_STRING
7181 && it->n_overlay_strings > 0)
7182 it->ignore_overlay_strings_at_pos_p = 1;
7183 it->len = it->dpvec_char_len;
7184 set_iterator_to_next (it, reseat_p);
7185 }
7186
7187 /* Maybe recheck faces after display vector */
7188 if (recheck_faces)
7189 it->stop_charpos = IT_CHARPOS (*it);
7190 }
7191 break;
7192
7193 case GET_FROM_STRING:
7194 /* Current display element is a character from a Lisp string. */
7195 eassert (it->s == NULL && STRINGP (it->string));
7196 /* Don't advance past string end. These conditions are true
7197 when set_iterator_to_next is called at the end of
7198 get_next_display_element, in which case the Lisp string is
7199 already exhausted, and all we want is pop the iterator
7200 stack. */
7201 if (it->current.overlay_string_index >= 0)
7202 {
7203 /* This is an overlay string, so there's no padding with
7204 spaces, and the number of characters in the string is
7205 where the string ends. */
7206 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7207 goto consider_string_end;
7208 }
7209 else
7210 {
7211 /* Not an overlay string. There could be padding, so test
7212 against it->end_charpos . */
7213 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7214 goto consider_string_end;
7215 }
7216 if (it->cmp_it.id >= 0)
7217 {
7218 int i;
7219
7220 if (! it->bidi_p)
7221 {
7222 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7223 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7224 if (it->cmp_it.to < it->cmp_it.nglyphs)
7225 it->cmp_it.from = it->cmp_it.to;
7226 else
7227 {
7228 it->cmp_it.id = -1;
7229 composition_compute_stop_pos (&it->cmp_it,
7230 IT_STRING_CHARPOS (*it),
7231 IT_STRING_BYTEPOS (*it),
7232 it->end_charpos, it->string);
7233 }
7234 }
7235 else if (! it->cmp_it.reversed_p)
7236 {
7237 for (i = 0; i < it->cmp_it.nchars; i++)
7238 bidi_move_to_visually_next (&it->bidi_it);
7239 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7240 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7241
7242 if (it->cmp_it.to < it->cmp_it.nglyphs)
7243 it->cmp_it.from = it->cmp_it.to;
7244 else
7245 {
7246 ptrdiff_t stop = it->end_charpos;
7247 if (it->bidi_it.scan_dir < 0)
7248 stop = -1;
7249 composition_compute_stop_pos (&it->cmp_it,
7250 IT_STRING_CHARPOS (*it),
7251 IT_STRING_BYTEPOS (*it), stop,
7252 it->string);
7253 }
7254 }
7255 else
7256 {
7257 for (i = 0; i < it->cmp_it.nchars; i++)
7258 bidi_move_to_visually_next (&it->bidi_it);
7259 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7260 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7261 if (it->cmp_it.from > 0)
7262 it->cmp_it.to = it->cmp_it.from;
7263 else
7264 {
7265 ptrdiff_t stop = it->end_charpos;
7266 if (it->bidi_it.scan_dir < 0)
7267 stop = -1;
7268 composition_compute_stop_pos (&it->cmp_it,
7269 IT_STRING_CHARPOS (*it),
7270 IT_STRING_BYTEPOS (*it), stop,
7271 it->string);
7272 }
7273 }
7274 }
7275 else
7276 {
7277 if (!it->bidi_p
7278 /* If the string position is beyond string's end, it
7279 means next_element_from_string is padding the string
7280 with blanks, in which case we bypass the bidi
7281 iterator, because it cannot deal with such virtual
7282 characters. */
7283 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7284 {
7285 IT_STRING_BYTEPOS (*it) += it->len;
7286 IT_STRING_CHARPOS (*it) += 1;
7287 }
7288 else
7289 {
7290 int prev_scan_dir = it->bidi_it.scan_dir;
7291
7292 bidi_move_to_visually_next (&it->bidi_it);
7293 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7294 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7295 if (prev_scan_dir != it->bidi_it.scan_dir)
7296 {
7297 ptrdiff_t stop = it->end_charpos;
7298
7299 if (it->bidi_it.scan_dir < 0)
7300 stop = -1;
7301 composition_compute_stop_pos (&it->cmp_it,
7302 IT_STRING_CHARPOS (*it),
7303 IT_STRING_BYTEPOS (*it), stop,
7304 it->string);
7305 }
7306 }
7307 }
7308
7309 consider_string_end:
7310
7311 if (it->current.overlay_string_index >= 0)
7312 {
7313 /* IT->string is an overlay string. Advance to the
7314 next, if there is one. */
7315 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7316 {
7317 it->ellipsis_p = 0;
7318 next_overlay_string (it);
7319 if (it->ellipsis_p)
7320 setup_for_ellipsis (it, 0);
7321 }
7322 }
7323 else
7324 {
7325 /* IT->string is not an overlay string. If we reached
7326 its end, and there is something on IT->stack, proceed
7327 with what is on the stack. This can be either another
7328 string, this time an overlay string, or a buffer. */
7329 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7330 && it->sp > 0)
7331 {
7332 pop_it (it);
7333 if (it->method == GET_FROM_STRING)
7334 goto consider_string_end;
7335 }
7336 }
7337 break;
7338
7339 case GET_FROM_IMAGE:
7340 case GET_FROM_STRETCH:
7341 /* The position etc with which we have to proceed are on
7342 the stack. The position may be at the end of a string,
7343 if the `display' property takes up the whole string. */
7344 eassert (it->sp > 0);
7345 pop_it (it);
7346 if (it->method == GET_FROM_STRING)
7347 goto consider_string_end;
7348 break;
7349
7350 default:
7351 /* There are no other methods defined, so this should be a bug. */
7352 emacs_abort ();
7353 }
7354
7355 eassert (it->method != GET_FROM_STRING
7356 || (STRINGP (it->string)
7357 && IT_STRING_CHARPOS (*it) >= 0));
7358 }
7359
7360 /* Load IT's display element fields with information about the next
7361 display element which comes from a display table entry or from the
7362 result of translating a control character to one of the forms `^C'
7363 or `\003'.
7364
7365 IT->dpvec holds the glyphs to return as characters.
7366 IT->saved_face_id holds the face id before the display vector--it
7367 is restored into IT->face_id in set_iterator_to_next. */
7368
7369 static int
7370 next_element_from_display_vector (struct it *it)
7371 {
7372 Lisp_Object gc;
7373
7374 /* Precondition. */
7375 eassert (it->dpvec && it->current.dpvec_index >= 0);
7376
7377 it->face_id = it->saved_face_id;
7378
7379 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7380 That seemed totally bogus - so I changed it... */
7381 gc = it->dpvec[it->current.dpvec_index];
7382
7383 if (GLYPH_CODE_P (gc))
7384 {
7385 it->c = GLYPH_CODE_CHAR (gc);
7386 it->len = CHAR_BYTES (it->c);
7387
7388 /* The entry may contain a face id to use. Such a face id is
7389 the id of a Lisp face, not a realized face. A face id of
7390 zero means no face is specified. */
7391 if (it->dpvec_face_id >= 0)
7392 it->face_id = it->dpvec_face_id;
7393 else
7394 {
7395 int lface_id = GLYPH_CODE_FACE (gc);
7396 if (lface_id > 0)
7397 it->face_id = merge_faces (it->f, Qt, lface_id,
7398 it->saved_face_id);
7399 }
7400 }
7401 else
7402 /* Display table entry is invalid. Return a space. */
7403 it->c = ' ', it->len = 1;
7404
7405 /* Don't change position and object of the iterator here. They are
7406 still the values of the character that had this display table
7407 entry or was translated, and that's what we want. */
7408 it->what = IT_CHARACTER;
7409 return 1;
7410 }
7411
7412 /* Get the first element of string/buffer in the visual order, after
7413 being reseated to a new position in a string or a buffer. */
7414 static void
7415 get_visually_first_element (struct it *it)
7416 {
7417 int string_p = STRINGP (it->string) || it->s;
7418 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7419 ptrdiff_t bob = (string_p ? 0 : BEGV);
7420
7421 if (STRINGP (it->string))
7422 {
7423 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7424 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7425 }
7426 else
7427 {
7428 it->bidi_it.charpos = IT_CHARPOS (*it);
7429 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7430 }
7431
7432 if (it->bidi_it.charpos == eob)
7433 {
7434 /* Nothing to do, but reset the FIRST_ELT flag, like
7435 bidi_paragraph_init does, because we are not going to
7436 call it. */
7437 it->bidi_it.first_elt = 0;
7438 }
7439 else if (it->bidi_it.charpos == bob
7440 || (!string_p
7441 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7442 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7443 {
7444 /* If we are at the beginning of a line/string, we can produce
7445 the next element right away. */
7446 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7447 bidi_move_to_visually_next (&it->bidi_it);
7448 }
7449 else
7450 {
7451 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7452
7453 /* We need to prime the bidi iterator starting at the line's or
7454 string's beginning, before we will be able to produce the
7455 next element. */
7456 if (string_p)
7457 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7458 else
7459 {
7460 it->bidi_it.charpos = find_next_newline_no_quit (IT_CHARPOS (*it),
7461 -1);
7462 it->bidi_it.bytepos = CHAR_TO_BYTE (it->bidi_it.charpos);
7463 }
7464 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7465 do
7466 {
7467 /* Now return to buffer/string position where we were asked
7468 to get the next display element, and produce that. */
7469 bidi_move_to_visually_next (&it->bidi_it);
7470 }
7471 while (it->bidi_it.bytepos != orig_bytepos
7472 && it->bidi_it.charpos < eob);
7473 }
7474
7475 /* Adjust IT's position information to where we ended up. */
7476 if (STRINGP (it->string))
7477 {
7478 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7479 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7480 }
7481 else
7482 {
7483 IT_CHARPOS (*it) = it->bidi_it.charpos;
7484 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7485 }
7486
7487 if (STRINGP (it->string) || !it->s)
7488 {
7489 ptrdiff_t stop, charpos, bytepos;
7490
7491 if (STRINGP (it->string))
7492 {
7493 eassert (!it->s);
7494 stop = SCHARS (it->string);
7495 if (stop > it->end_charpos)
7496 stop = it->end_charpos;
7497 charpos = IT_STRING_CHARPOS (*it);
7498 bytepos = IT_STRING_BYTEPOS (*it);
7499 }
7500 else
7501 {
7502 stop = it->end_charpos;
7503 charpos = IT_CHARPOS (*it);
7504 bytepos = IT_BYTEPOS (*it);
7505 }
7506 if (it->bidi_it.scan_dir < 0)
7507 stop = -1;
7508 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7509 it->string);
7510 }
7511 }
7512
7513 /* Load IT with the next display element from Lisp string IT->string.
7514 IT->current.string_pos is the current position within the string.
7515 If IT->current.overlay_string_index >= 0, the Lisp string is an
7516 overlay string. */
7517
7518 static int
7519 next_element_from_string (struct it *it)
7520 {
7521 struct text_pos position;
7522
7523 eassert (STRINGP (it->string));
7524 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7525 eassert (IT_STRING_CHARPOS (*it) >= 0);
7526 position = it->current.string_pos;
7527
7528 /* With bidi reordering, the character to display might not be the
7529 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7530 that we were reseat()ed to a new string, whose paragraph
7531 direction is not known. */
7532 if (it->bidi_p && it->bidi_it.first_elt)
7533 {
7534 get_visually_first_element (it);
7535 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7536 }
7537
7538 /* Time to check for invisible text? */
7539 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7540 {
7541 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7542 {
7543 if (!(!it->bidi_p
7544 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7545 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7546 {
7547 /* With bidi non-linear iteration, we could find
7548 ourselves far beyond the last computed stop_charpos,
7549 with several other stop positions in between that we
7550 missed. Scan them all now, in buffer's logical
7551 order, until we find and handle the last stop_charpos
7552 that precedes our current position. */
7553 handle_stop_backwards (it, it->stop_charpos);
7554 return GET_NEXT_DISPLAY_ELEMENT (it);
7555 }
7556 else
7557 {
7558 if (it->bidi_p)
7559 {
7560 /* Take note of the stop position we just moved
7561 across, for when we will move back across it. */
7562 it->prev_stop = it->stop_charpos;
7563 /* If we are at base paragraph embedding level, take
7564 note of the last stop position seen at this
7565 level. */
7566 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7567 it->base_level_stop = it->stop_charpos;
7568 }
7569 handle_stop (it);
7570
7571 /* Since a handler may have changed IT->method, we must
7572 recurse here. */
7573 return GET_NEXT_DISPLAY_ELEMENT (it);
7574 }
7575 }
7576 else if (it->bidi_p
7577 /* If we are before prev_stop, we may have overstepped
7578 on our way backwards a stop_pos, and if so, we need
7579 to handle that stop_pos. */
7580 && IT_STRING_CHARPOS (*it) < it->prev_stop
7581 /* We can sometimes back up for reasons that have nothing
7582 to do with bidi reordering. E.g., compositions. The
7583 code below is only needed when we are above the base
7584 embedding level, so test for that explicitly. */
7585 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7586 {
7587 /* If we lost track of base_level_stop, we have no better
7588 place for handle_stop_backwards to start from than string
7589 beginning. This happens, e.g., when we were reseated to
7590 the previous screenful of text by vertical-motion. */
7591 if (it->base_level_stop <= 0
7592 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7593 it->base_level_stop = 0;
7594 handle_stop_backwards (it, it->base_level_stop);
7595 return GET_NEXT_DISPLAY_ELEMENT (it);
7596 }
7597 }
7598
7599 if (it->current.overlay_string_index >= 0)
7600 {
7601 /* Get the next character from an overlay string. In overlay
7602 strings, there is no field width or padding with spaces to
7603 do. */
7604 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7605 {
7606 it->what = IT_EOB;
7607 return 0;
7608 }
7609 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7610 IT_STRING_BYTEPOS (*it),
7611 it->bidi_it.scan_dir < 0
7612 ? -1
7613 : SCHARS (it->string))
7614 && next_element_from_composition (it))
7615 {
7616 return 1;
7617 }
7618 else if (STRING_MULTIBYTE (it->string))
7619 {
7620 const unsigned char *s = (SDATA (it->string)
7621 + IT_STRING_BYTEPOS (*it));
7622 it->c = string_char_and_length (s, &it->len);
7623 }
7624 else
7625 {
7626 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7627 it->len = 1;
7628 }
7629 }
7630 else
7631 {
7632 /* Get the next character from a Lisp string that is not an
7633 overlay string. Such strings come from the mode line, for
7634 example. We may have to pad with spaces, or truncate the
7635 string. See also next_element_from_c_string. */
7636 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7637 {
7638 it->what = IT_EOB;
7639 return 0;
7640 }
7641 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7642 {
7643 /* Pad with spaces. */
7644 it->c = ' ', it->len = 1;
7645 CHARPOS (position) = BYTEPOS (position) = -1;
7646 }
7647 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7648 IT_STRING_BYTEPOS (*it),
7649 it->bidi_it.scan_dir < 0
7650 ? -1
7651 : it->string_nchars)
7652 && next_element_from_composition (it))
7653 {
7654 return 1;
7655 }
7656 else if (STRING_MULTIBYTE (it->string))
7657 {
7658 const unsigned char *s = (SDATA (it->string)
7659 + IT_STRING_BYTEPOS (*it));
7660 it->c = string_char_and_length (s, &it->len);
7661 }
7662 else
7663 {
7664 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7665 it->len = 1;
7666 }
7667 }
7668
7669 /* Record what we have and where it came from. */
7670 it->what = IT_CHARACTER;
7671 it->object = it->string;
7672 it->position = position;
7673 return 1;
7674 }
7675
7676
7677 /* Load IT with next display element from C string IT->s.
7678 IT->string_nchars is the maximum number of characters to return
7679 from the string. IT->end_charpos may be greater than
7680 IT->string_nchars when this function is called, in which case we
7681 may have to return padding spaces. Value is zero if end of string
7682 reached, including padding spaces. */
7683
7684 static int
7685 next_element_from_c_string (struct it *it)
7686 {
7687 int success_p = 1;
7688
7689 eassert (it->s);
7690 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7691 it->what = IT_CHARACTER;
7692 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7693 it->object = Qnil;
7694
7695 /* With bidi reordering, the character to display might not be the
7696 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7697 we were reseated to a new string, whose paragraph direction is
7698 not known. */
7699 if (it->bidi_p && it->bidi_it.first_elt)
7700 get_visually_first_element (it);
7701
7702 /* IT's position can be greater than IT->string_nchars in case a
7703 field width or precision has been specified when the iterator was
7704 initialized. */
7705 if (IT_CHARPOS (*it) >= it->end_charpos)
7706 {
7707 /* End of the game. */
7708 it->what = IT_EOB;
7709 success_p = 0;
7710 }
7711 else if (IT_CHARPOS (*it) >= it->string_nchars)
7712 {
7713 /* Pad with spaces. */
7714 it->c = ' ', it->len = 1;
7715 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7716 }
7717 else if (it->multibyte_p)
7718 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7719 else
7720 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7721
7722 return success_p;
7723 }
7724
7725
7726 /* Set up IT to return characters from an ellipsis, if appropriate.
7727 The definition of the ellipsis glyphs may come from a display table
7728 entry. This function fills IT with the first glyph from the
7729 ellipsis if an ellipsis is to be displayed. */
7730
7731 static int
7732 next_element_from_ellipsis (struct it *it)
7733 {
7734 if (it->selective_display_ellipsis_p)
7735 setup_for_ellipsis (it, it->len);
7736 else
7737 {
7738 /* The face at the current position may be different from the
7739 face we find after the invisible text. Remember what it
7740 was in IT->saved_face_id, and signal that it's there by
7741 setting face_before_selective_p. */
7742 it->saved_face_id = it->face_id;
7743 it->method = GET_FROM_BUFFER;
7744 it->object = it->w->buffer;
7745 reseat_at_next_visible_line_start (it, 1);
7746 it->face_before_selective_p = 1;
7747 }
7748
7749 return GET_NEXT_DISPLAY_ELEMENT (it);
7750 }
7751
7752
7753 /* Deliver an image display element. The iterator IT is already
7754 filled with image information (done in handle_display_prop). Value
7755 is always 1. */
7756
7757
7758 static int
7759 next_element_from_image (struct it *it)
7760 {
7761 it->what = IT_IMAGE;
7762 it->ignore_overlay_strings_at_pos_p = 0;
7763 return 1;
7764 }
7765
7766
7767 /* Fill iterator IT with next display element from a stretch glyph
7768 property. IT->object is the value of the text property. Value is
7769 always 1. */
7770
7771 static int
7772 next_element_from_stretch (struct it *it)
7773 {
7774 it->what = IT_STRETCH;
7775 return 1;
7776 }
7777
7778 /* Scan backwards from IT's current position until we find a stop
7779 position, or until BEGV. This is called when we find ourself
7780 before both the last known prev_stop and base_level_stop while
7781 reordering bidirectional text. */
7782
7783 static void
7784 compute_stop_pos_backwards (struct it *it)
7785 {
7786 const int SCAN_BACK_LIMIT = 1000;
7787 struct text_pos pos;
7788 struct display_pos save_current = it->current;
7789 struct text_pos save_position = it->position;
7790 ptrdiff_t charpos = IT_CHARPOS (*it);
7791 ptrdiff_t where_we_are = charpos;
7792 ptrdiff_t save_stop_pos = it->stop_charpos;
7793 ptrdiff_t save_end_pos = it->end_charpos;
7794
7795 eassert (NILP (it->string) && !it->s);
7796 eassert (it->bidi_p);
7797 it->bidi_p = 0;
7798 do
7799 {
7800 it->end_charpos = min (charpos + 1, ZV);
7801 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7802 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7803 reseat_1 (it, pos, 0);
7804 compute_stop_pos (it);
7805 /* We must advance forward, right? */
7806 if (it->stop_charpos <= charpos)
7807 emacs_abort ();
7808 }
7809 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7810
7811 if (it->stop_charpos <= where_we_are)
7812 it->prev_stop = it->stop_charpos;
7813 else
7814 it->prev_stop = BEGV;
7815 it->bidi_p = 1;
7816 it->current = save_current;
7817 it->position = save_position;
7818 it->stop_charpos = save_stop_pos;
7819 it->end_charpos = save_end_pos;
7820 }
7821
7822 /* Scan forward from CHARPOS in the current buffer/string, until we
7823 find a stop position > current IT's position. Then handle the stop
7824 position before that. This is called when we bump into a stop
7825 position while reordering bidirectional text. CHARPOS should be
7826 the last previously processed stop_pos (or BEGV/0, if none were
7827 processed yet) whose position is less that IT's current
7828 position. */
7829
7830 static void
7831 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7832 {
7833 int bufp = !STRINGP (it->string);
7834 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7835 struct display_pos save_current = it->current;
7836 struct text_pos save_position = it->position;
7837 struct text_pos pos1;
7838 ptrdiff_t next_stop;
7839
7840 /* Scan in strict logical order. */
7841 eassert (it->bidi_p);
7842 it->bidi_p = 0;
7843 do
7844 {
7845 it->prev_stop = charpos;
7846 if (bufp)
7847 {
7848 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7849 reseat_1 (it, pos1, 0);
7850 }
7851 else
7852 it->current.string_pos = string_pos (charpos, it->string);
7853 compute_stop_pos (it);
7854 /* We must advance forward, right? */
7855 if (it->stop_charpos <= it->prev_stop)
7856 emacs_abort ();
7857 charpos = it->stop_charpos;
7858 }
7859 while (charpos <= where_we_are);
7860
7861 it->bidi_p = 1;
7862 it->current = save_current;
7863 it->position = save_position;
7864 next_stop = it->stop_charpos;
7865 it->stop_charpos = it->prev_stop;
7866 handle_stop (it);
7867 it->stop_charpos = next_stop;
7868 }
7869
7870 /* Load IT with the next display element from current_buffer. Value
7871 is zero if end of buffer reached. IT->stop_charpos is the next
7872 position at which to stop and check for text properties or buffer
7873 end. */
7874
7875 static int
7876 next_element_from_buffer (struct it *it)
7877 {
7878 int success_p = 1;
7879
7880 eassert (IT_CHARPOS (*it) >= BEGV);
7881 eassert (NILP (it->string) && !it->s);
7882 eassert (!it->bidi_p
7883 || (EQ (it->bidi_it.string.lstring, Qnil)
7884 && it->bidi_it.string.s == NULL));
7885
7886 /* With bidi reordering, the character to display might not be the
7887 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7888 we were reseat()ed to a new buffer position, which is potentially
7889 a different paragraph. */
7890 if (it->bidi_p && it->bidi_it.first_elt)
7891 {
7892 get_visually_first_element (it);
7893 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7894 }
7895
7896 if (IT_CHARPOS (*it) >= it->stop_charpos)
7897 {
7898 if (IT_CHARPOS (*it) >= it->end_charpos)
7899 {
7900 int overlay_strings_follow_p;
7901
7902 /* End of the game, except when overlay strings follow that
7903 haven't been returned yet. */
7904 if (it->overlay_strings_at_end_processed_p)
7905 overlay_strings_follow_p = 0;
7906 else
7907 {
7908 it->overlay_strings_at_end_processed_p = 1;
7909 overlay_strings_follow_p = get_overlay_strings (it, 0);
7910 }
7911
7912 if (overlay_strings_follow_p)
7913 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7914 else
7915 {
7916 it->what = IT_EOB;
7917 it->position = it->current.pos;
7918 success_p = 0;
7919 }
7920 }
7921 else if (!(!it->bidi_p
7922 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7923 || IT_CHARPOS (*it) == it->stop_charpos))
7924 {
7925 /* With bidi non-linear iteration, we could find ourselves
7926 far beyond the last computed stop_charpos, with several
7927 other stop positions in between that we missed. Scan
7928 them all now, in buffer's logical order, until we find
7929 and handle the last stop_charpos that precedes our
7930 current position. */
7931 handle_stop_backwards (it, it->stop_charpos);
7932 return GET_NEXT_DISPLAY_ELEMENT (it);
7933 }
7934 else
7935 {
7936 if (it->bidi_p)
7937 {
7938 /* Take note of the stop position we just moved across,
7939 for when we will move back across it. */
7940 it->prev_stop = it->stop_charpos;
7941 /* If we are at base paragraph embedding level, take
7942 note of the last stop position seen at this
7943 level. */
7944 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7945 it->base_level_stop = it->stop_charpos;
7946 }
7947 handle_stop (it);
7948 return GET_NEXT_DISPLAY_ELEMENT (it);
7949 }
7950 }
7951 else if (it->bidi_p
7952 /* If we are before prev_stop, we may have overstepped on
7953 our way backwards a stop_pos, and if so, we need to
7954 handle that stop_pos. */
7955 && IT_CHARPOS (*it) < it->prev_stop
7956 /* We can sometimes back up for reasons that have nothing
7957 to do with bidi reordering. E.g., compositions. The
7958 code below is only needed when we are above the base
7959 embedding level, so test for that explicitly. */
7960 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7961 {
7962 if (it->base_level_stop <= 0
7963 || IT_CHARPOS (*it) < it->base_level_stop)
7964 {
7965 /* If we lost track of base_level_stop, we need to find
7966 prev_stop by looking backwards. This happens, e.g., when
7967 we were reseated to the previous screenful of text by
7968 vertical-motion. */
7969 it->base_level_stop = BEGV;
7970 compute_stop_pos_backwards (it);
7971 handle_stop_backwards (it, it->prev_stop);
7972 }
7973 else
7974 handle_stop_backwards (it, it->base_level_stop);
7975 return GET_NEXT_DISPLAY_ELEMENT (it);
7976 }
7977 else
7978 {
7979 /* No face changes, overlays etc. in sight, so just return a
7980 character from current_buffer. */
7981 unsigned char *p;
7982 ptrdiff_t stop;
7983
7984 /* Maybe run the redisplay end trigger hook. Performance note:
7985 This doesn't seem to cost measurable time. */
7986 if (it->redisplay_end_trigger_charpos
7987 && it->glyph_row
7988 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
7989 run_redisplay_end_trigger_hook (it);
7990
7991 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
7992 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
7993 stop)
7994 && next_element_from_composition (it))
7995 {
7996 return 1;
7997 }
7998
7999 /* Get the next character, maybe multibyte. */
8000 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8001 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8002 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8003 else
8004 it->c = *p, it->len = 1;
8005
8006 /* Record what we have and where it came from. */
8007 it->what = IT_CHARACTER;
8008 it->object = it->w->buffer;
8009 it->position = it->current.pos;
8010
8011 /* Normally we return the character found above, except when we
8012 really want to return an ellipsis for selective display. */
8013 if (it->selective)
8014 {
8015 if (it->c == '\n')
8016 {
8017 /* A value of selective > 0 means hide lines indented more
8018 than that number of columns. */
8019 if (it->selective > 0
8020 && IT_CHARPOS (*it) + 1 < ZV
8021 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8022 IT_BYTEPOS (*it) + 1,
8023 it->selective))
8024 {
8025 success_p = next_element_from_ellipsis (it);
8026 it->dpvec_char_len = -1;
8027 }
8028 }
8029 else if (it->c == '\r' && it->selective == -1)
8030 {
8031 /* A value of selective == -1 means that everything from the
8032 CR to the end of the line is invisible, with maybe an
8033 ellipsis displayed for it. */
8034 success_p = next_element_from_ellipsis (it);
8035 it->dpvec_char_len = -1;
8036 }
8037 }
8038 }
8039
8040 /* Value is zero if end of buffer reached. */
8041 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8042 return success_p;
8043 }
8044
8045
8046 /* Run the redisplay end trigger hook for IT. */
8047
8048 static void
8049 run_redisplay_end_trigger_hook (struct it *it)
8050 {
8051 Lisp_Object args[3];
8052
8053 /* IT->glyph_row should be non-null, i.e. we should be actually
8054 displaying something, or otherwise we should not run the hook. */
8055 eassert (it->glyph_row);
8056
8057 /* Set up hook arguments. */
8058 args[0] = Qredisplay_end_trigger_functions;
8059 args[1] = it->window;
8060 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8061 it->redisplay_end_trigger_charpos = 0;
8062
8063 /* Since we are *trying* to run these functions, don't try to run
8064 them again, even if they get an error. */
8065 wset_redisplay_end_trigger (it->w, Qnil);
8066 Frun_hook_with_args (3, args);
8067
8068 /* Notice if it changed the face of the character we are on. */
8069 handle_face_prop (it);
8070 }
8071
8072
8073 /* Deliver a composition display element. Unlike the other
8074 next_element_from_XXX, this function is not registered in the array
8075 get_next_element[]. It is called from next_element_from_buffer and
8076 next_element_from_string when necessary. */
8077
8078 static int
8079 next_element_from_composition (struct it *it)
8080 {
8081 it->what = IT_COMPOSITION;
8082 it->len = it->cmp_it.nbytes;
8083 if (STRINGP (it->string))
8084 {
8085 if (it->c < 0)
8086 {
8087 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8088 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8089 return 0;
8090 }
8091 it->position = it->current.string_pos;
8092 it->object = it->string;
8093 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8094 IT_STRING_BYTEPOS (*it), it->string);
8095 }
8096 else
8097 {
8098 if (it->c < 0)
8099 {
8100 IT_CHARPOS (*it) += it->cmp_it.nchars;
8101 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8102 if (it->bidi_p)
8103 {
8104 if (it->bidi_it.new_paragraph)
8105 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8106 /* Resync the bidi iterator with IT's new position.
8107 FIXME: this doesn't support bidirectional text. */
8108 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8109 bidi_move_to_visually_next (&it->bidi_it);
8110 }
8111 return 0;
8112 }
8113 it->position = it->current.pos;
8114 it->object = it->w->buffer;
8115 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8116 IT_BYTEPOS (*it), Qnil);
8117 }
8118 return 1;
8119 }
8120
8121
8122 \f
8123 /***********************************************************************
8124 Moving an iterator without producing glyphs
8125 ***********************************************************************/
8126
8127 /* Check if iterator is at a position corresponding to a valid buffer
8128 position after some move_it_ call. */
8129
8130 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8131 ((it)->method == GET_FROM_STRING \
8132 ? IT_STRING_CHARPOS (*it) == 0 \
8133 : 1)
8134
8135
8136 /* Move iterator IT to a specified buffer or X position within one
8137 line on the display without producing glyphs.
8138
8139 OP should be a bit mask including some or all of these bits:
8140 MOVE_TO_X: Stop upon reaching x-position TO_X.
8141 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8142 Regardless of OP's value, stop upon reaching the end of the display line.
8143
8144 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8145 This means, in particular, that TO_X includes window's horizontal
8146 scroll amount.
8147
8148 The return value has several possible values that
8149 say what condition caused the scan to stop:
8150
8151 MOVE_POS_MATCH_OR_ZV
8152 - when TO_POS or ZV was reached.
8153
8154 MOVE_X_REACHED
8155 -when TO_X was reached before TO_POS or ZV were reached.
8156
8157 MOVE_LINE_CONTINUED
8158 - when we reached the end of the display area and the line must
8159 be continued.
8160
8161 MOVE_LINE_TRUNCATED
8162 - when we reached the end of the display area and the line is
8163 truncated.
8164
8165 MOVE_NEWLINE_OR_CR
8166 - when we stopped at a line end, i.e. a newline or a CR and selective
8167 display is on. */
8168
8169 static enum move_it_result
8170 move_it_in_display_line_to (struct it *it,
8171 ptrdiff_t to_charpos, int to_x,
8172 enum move_operation_enum op)
8173 {
8174 enum move_it_result result = MOVE_UNDEFINED;
8175 struct glyph_row *saved_glyph_row;
8176 struct it wrap_it, atpos_it, atx_it, ppos_it;
8177 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8178 void *ppos_data = NULL;
8179 int may_wrap = 0;
8180 enum it_method prev_method = it->method;
8181 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8182 int saw_smaller_pos = prev_pos < to_charpos;
8183
8184 /* Don't produce glyphs in produce_glyphs. */
8185 saved_glyph_row = it->glyph_row;
8186 it->glyph_row = NULL;
8187
8188 /* Use wrap_it to save a copy of IT wherever a word wrap could
8189 occur. Use atpos_it to save a copy of IT at the desired buffer
8190 position, if found, so that we can scan ahead and check if the
8191 word later overshoots the window edge. Use atx_it similarly, for
8192 pixel positions. */
8193 wrap_it.sp = -1;
8194 atpos_it.sp = -1;
8195 atx_it.sp = -1;
8196
8197 /* Use ppos_it under bidi reordering to save a copy of IT for the
8198 position > CHARPOS that is the closest to CHARPOS. We restore
8199 that position in IT when we have scanned the entire display line
8200 without finding a match for CHARPOS and all the character
8201 positions are greater than CHARPOS. */
8202 if (it->bidi_p)
8203 {
8204 SAVE_IT (ppos_it, *it, ppos_data);
8205 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8206 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8207 SAVE_IT (ppos_it, *it, ppos_data);
8208 }
8209
8210 #define BUFFER_POS_REACHED_P() \
8211 ((op & MOVE_TO_POS) != 0 \
8212 && BUFFERP (it->object) \
8213 && (IT_CHARPOS (*it) == to_charpos \
8214 || ((!it->bidi_p \
8215 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8216 && IT_CHARPOS (*it) > to_charpos) \
8217 || (it->what == IT_COMPOSITION \
8218 && ((IT_CHARPOS (*it) > to_charpos \
8219 && to_charpos >= it->cmp_it.charpos) \
8220 || (IT_CHARPOS (*it) < to_charpos \
8221 && to_charpos <= it->cmp_it.charpos)))) \
8222 && (it->method == GET_FROM_BUFFER \
8223 || (it->method == GET_FROM_DISPLAY_VECTOR \
8224 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8225
8226 /* If there's a line-/wrap-prefix, handle it. */
8227 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8228 && it->current_y < it->last_visible_y)
8229 handle_line_prefix (it);
8230
8231 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8232 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8233
8234 while (1)
8235 {
8236 int x, i, ascent = 0, descent = 0;
8237
8238 /* Utility macro to reset an iterator with x, ascent, and descent. */
8239 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8240 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8241 (IT)->max_descent = descent)
8242
8243 /* Stop if we move beyond TO_CHARPOS (after an image or a
8244 display string or stretch glyph). */
8245 if ((op & MOVE_TO_POS) != 0
8246 && BUFFERP (it->object)
8247 && it->method == GET_FROM_BUFFER
8248 && (((!it->bidi_p
8249 /* When the iterator is at base embedding level, we
8250 are guaranteed that characters are delivered for
8251 display in strictly increasing order of their
8252 buffer positions. */
8253 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8254 && IT_CHARPOS (*it) > to_charpos)
8255 || (it->bidi_p
8256 && (prev_method == GET_FROM_IMAGE
8257 || prev_method == GET_FROM_STRETCH
8258 || prev_method == GET_FROM_STRING)
8259 /* Passed TO_CHARPOS from left to right. */
8260 && ((prev_pos < to_charpos
8261 && IT_CHARPOS (*it) > to_charpos)
8262 /* Passed TO_CHARPOS from right to left. */
8263 || (prev_pos > to_charpos
8264 && IT_CHARPOS (*it) < to_charpos)))))
8265 {
8266 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8267 {
8268 result = MOVE_POS_MATCH_OR_ZV;
8269 break;
8270 }
8271 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8272 /* If wrap_it is valid, the current position might be in a
8273 word that is wrapped. So, save the iterator in
8274 atpos_it and continue to see if wrapping happens. */
8275 SAVE_IT (atpos_it, *it, atpos_data);
8276 }
8277
8278 /* Stop when ZV reached.
8279 We used to stop here when TO_CHARPOS reached as well, but that is
8280 too soon if this glyph does not fit on this line. So we handle it
8281 explicitly below. */
8282 if (!get_next_display_element (it))
8283 {
8284 result = MOVE_POS_MATCH_OR_ZV;
8285 break;
8286 }
8287
8288 if (it->line_wrap == TRUNCATE)
8289 {
8290 if (BUFFER_POS_REACHED_P ())
8291 {
8292 result = MOVE_POS_MATCH_OR_ZV;
8293 break;
8294 }
8295 }
8296 else
8297 {
8298 if (it->line_wrap == WORD_WRAP)
8299 {
8300 if (IT_DISPLAYING_WHITESPACE (it))
8301 may_wrap = 1;
8302 else if (may_wrap)
8303 {
8304 /* We have reached a glyph that follows one or more
8305 whitespace characters. If the position is
8306 already found, we are done. */
8307 if (atpos_it.sp >= 0)
8308 {
8309 RESTORE_IT (it, &atpos_it, atpos_data);
8310 result = MOVE_POS_MATCH_OR_ZV;
8311 goto done;
8312 }
8313 if (atx_it.sp >= 0)
8314 {
8315 RESTORE_IT (it, &atx_it, atx_data);
8316 result = MOVE_X_REACHED;
8317 goto done;
8318 }
8319 /* Otherwise, we can wrap here. */
8320 SAVE_IT (wrap_it, *it, wrap_data);
8321 may_wrap = 0;
8322 }
8323 }
8324 }
8325
8326 /* Remember the line height for the current line, in case
8327 the next element doesn't fit on the line. */
8328 ascent = it->max_ascent;
8329 descent = it->max_descent;
8330
8331 /* The call to produce_glyphs will get the metrics of the
8332 display element IT is loaded with. Record the x-position
8333 before this display element, in case it doesn't fit on the
8334 line. */
8335 x = it->current_x;
8336
8337 PRODUCE_GLYPHS (it);
8338
8339 if (it->area != TEXT_AREA)
8340 {
8341 prev_method = it->method;
8342 if (it->method == GET_FROM_BUFFER)
8343 prev_pos = IT_CHARPOS (*it);
8344 set_iterator_to_next (it, 1);
8345 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8346 SET_TEXT_POS (this_line_min_pos,
8347 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8348 if (it->bidi_p
8349 && (op & MOVE_TO_POS)
8350 && IT_CHARPOS (*it) > to_charpos
8351 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8352 SAVE_IT (ppos_it, *it, ppos_data);
8353 continue;
8354 }
8355
8356 /* The number of glyphs we get back in IT->nglyphs will normally
8357 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8358 character on a terminal frame, or (iii) a line end. For the
8359 second case, IT->nglyphs - 1 padding glyphs will be present.
8360 (On X frames, there is only one glyph produced for a
8361 composite character.)
8362
8363 The behavior implemented below means, for continuation lines,
8364 that as many spaces of a TAB as fit on the current line are
8365 displayed there. For terminal frames, as many glyphs of a
8366 multi-glyph character are displayed in the current line, too.
8367 This is what the old redisplay code did, and we keep it that
8368 way. Under X, the whole shape of a complex character must
8369 fit on the line or it will be completely displayed in the
8370 next line.
8371
8372 Note that both for tabs and padding glyphs, all glyphs have
8373 the same width. */
8374 if (it->nglyphs)
8375 {
8376 /* More than one glyph or glyph doesn't fit on line. All
8377 glyphs have the same width. */
8378 int single_glyph_width = it->pixel_width / it->nglyphs;
8379 int new_x;
8380 int x_before_this_char = x;
8381 int hpos_before_this_char = it->hpos;
8382
8383 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8384 {
8385 new_x = x + single_glyph_width;
8386
8387 /* We want to leave anything reaching TO_X to the caller. */
8388 if ((op & MOVE_TO_X) && new_x > to_x)
8389 {
8390 if (BUFFER_POS_REACHED_P ())
8391 {
8392 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8393 goto buffer_pos_reached;
8394 if (atpos_it.sp < 0)
8395 {
8396 SAVE_IT (atpos_it, *it, atpos_data);
8397 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8398 }
8399 }
8400 else
8401 {
8402 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8403 {
8404 it->current_x = x;
8405 result = MOVE_X_REACHED;
8406 break;
8407 }
8408 if (atx_it.sp < 0)
8409 {
8410 SAVE_IT (atx_it, *it, atx_data);
8411 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8412 }
8413 }
8414 }
8415
8416 if (/* Lines are continued. */
8417 it->line_wrap != TRUNCATE
8418 && (/* And glyph doesn't fit on the line. */
8419 new_x > it->last_visible_x
8420 /* Or it fits exactly and we're on a window
8421 system frame. */
8422 || (new_x == it->last_visible_x
8423 && FRAME_WINDOW_P (it->f)
8424 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8425 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8426 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8427 {
8428 if (/* IT->hpos == 0 means the very first glyph
8429 doesn't fit on the line, e.g. a wide image. */
8430 it->hpos == 0
8431 || (new_x == it->last_visible_x
8432 && FRAME_WINDOW_P (it->f)))
8433 {
8434 ++it->hpos;
8435 it->current_x = new_x;
8436
8437 /* The character's last glyph just barely fits
8438 in this row. */
8439 if (i == it->nglyphs - 1)
8440 {
8441 /* If this is the destination position,
8442 return a position *before* it in this row,
8443 now that we know it fits in this row. */
8444 if (BUFFER_POS_REACHED_P ())
8445 {
8446 if (it->line_wrap != WORD_WRAP
8447 || wrap_it.sp < 0)
8448 {
8449 it->hpos = hpos_before_this_char;
8450 it->current_x = x_before_this_char;
8451 result = MOVE_POS_MATCH_OR_ZV;
8452 break;
8453 }
8454 if (it->line_wrap == WORD_WRAP
8455 && atpos_it.sp < 0)
8456 {
8457 SAVE_IT (atpos_it, *it, atpos_data);
8458 atpos_it.current_x = x_before_this_char;
8459 atpos_it.hpos = hpos_before_this_char;
8460 }
8461 }
8462
8463 prev_method = it->method;
8464 if (it->method == GET_FROM_BUFFER)
8465 prev_pos = IT_CHARPOS (*it);
8466 set_iterator_to_next (it, 1);
8467 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8468 SET_TEXT_POS (this_line_min_pos,
8469 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8470 /* On graphical terminals, newlines may
8471 "overflow" into the fringe if
8472 overflow-newline-into-fringe is non-nil.
8473 On text terminals, and on graphical
8474 terminals with no right margin, newlines
8475 may overflow into the last glyph on the
8476 display line.*/
8477 if (!FRAME_WINDOW_P (it->f)
8478 || ((it->bidi_p
8479 && it->bidi_it.paragraph_dir == R2L)
8480 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8481 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8482 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8483 {
8484 if (!get_next_display_element (it))
8485 {
8486 result = MOVE_POS_MATCH_OR_ZV;
8487 break;
8488 }
8489 if (BUFFER_POS_REACHED_P ())
8490 {
8491 if (ITERATOR_AT_END_OF_LINE_P (it))
8492 result = MOVE_POS_MATCH_OR_ZV;
8493 else
8494 result = MOVE_LINE_CONTINUED;
8495 break;
8496 }
8497 if (ITERATOR_AT_END_OF_LINE_P (it))
8498 {
8499 result = MOVE_NEWLINE_OR_CR;
8500 break;
8501 }
8502 }
8503 }
8504 }
8505 else
8506 IT_RESET_X_ASCENT_DESCENT (it);
8507
8508 if (wrap_it.sp >= 0)
8509 {
8510 RESTORE_IT (it, &wrap_it, wrap_data);
8511 atpos_it.sp = -1;
8512 atx_it.sp = -1;
8513 }
8514
8515 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8516 IT_CHARPOS (*it)));
8517 result = MOVE_LINE_CONTINUED;
8518 break;
8519 }
8520
8521 if (BUFFER_POS_REACHED_P ())
8522 {
8523 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8524 goto buffer_pos_reached;
8525 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8526 {
8527 SAVE_IT (atpos_it, *it, atpos_data);
8528 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8529 }
8530 }
8531
8532 if (new_x > it->first_visible_x)
8533 {
8534 /* Glyph is visible. Increment number of glyphs that
8535 would be displayed. */
8536 ++it->hpos;
8537 }
8538 }
8539
8540 if (result != MOVE_UNDEFINED)
8541 break;
8542 }
8543 else if (BUFFER_POS_REACHED_P ())
8544 {
8545 buffer_pos_reached:
8546 IT_RESET_X_ASCENT_DESCENT (it);
8547 result = MOVE_POS_MATCH_OR_ZV;
8548 break;
8549 }
8550 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8551 {
8552 /* Stop when TO_X specified and reached. This check is
8553 necessary here because of lines consisting of a line end,
8554 only. The line end will not produce any glyphs and we
8555 would never get MOVE_X_REACHED. */
8556 eassert (it->nglyphs == 0);
8557 result = MOVE_X_REACHED;
8558 break;
8559 }
8560
8561 /* Is this a line end? If yes, we're done. */
8562 if (ITERATOR_AT_END_OF_LINE_P (it))
8563 {
8564 /* If we are past TO_CHARPOS, but never saw any character
8565 positions smaller than TO_CHARPOS, return
8566 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8567 did. */
8568 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8569 {
8570 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8571 {
8572 if (IT_CHARPOS (ppos_it) < ZV)
8573 {
8574 RESTORE_IT (it, &ppos_it, ppos_data);
8575 result = MOVE_POS_MATCH_OR_ZV;
8576 }
8577 else
8578 goto buffer_pos_reached;
8579 }
8580 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8581 && IT_CHARPOS (*it) > to_charpos)
8582 goto buffer_pos_reached;
8583 else
8584 result = MOVE_NEWLINE_OR_CR;
8585 }
8586 else
8587 result = MOVE_NEWLINE_OR_CR;
8588 break;
8589 }
8590
8591 prev_method = it->method;
8592 if (it->method == GET_FROM_BUFFER)
8593 prev_pos = IT_CHARPOS (*it);
8594 /* The current display element has been consumed. Advance
8595 to the next. */
8596 set_iterator_to_next (it, 1);
8597 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8598 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8599 if (IT_CHARPOS (*it) < to_charpos)
8600 saw_smaller_pos = 1;
8601 if (it->bidi_p
8602 && (op & MOVE_TO_POS)
8603 && IT_CHARPOS (*it) >= to_charpos
8604 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8605 SAVE_IT (ppos_it, *it, ppos_data);
8606
8607 /* Stop if lines are truncated and IT's current x-position is
8608 past the right edge of the window now. */
8609 if (it->line_wrap == TRUNCATE
8610 && it->current_x >= it->last_visible_x)
8611 {
8612 if (!FRAME_WINDOW_P (it->f)
8613 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8614 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8615 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8616 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8617 {
8618 int at_eob_p = 0;
8619
8620 if ((at_eob_p = !get_next_display_element (it))
8621 || BUFFER_POS_REACHED_P ()
8622 /* If we are past TO_CHARPOS, but never saw any
8623 character positions smaller than TO_CHARPOS,
8624 return MOVE_POS_MATCH_OR_ZV, like the
8625 unidirectional display did. */
8626 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8627 && !saw_smaller_pos
8628 && IT_CHARPOS (*it) > to_charpos))
8629 {
8630 if (it->bidi_p
8631 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8632 RESTORE_IT (it, &ppos_it, ppos_data);
8633 result = MOVE_POS_MATCH_OR_ZV;
8634 break;
8635 }
8636 if (ITERATOR_AT_END_OF_LINE_P (it))
8637 {
8638 result = MOVE_NEWLINE_OR_CR;
8639 break;
8640 }
8641 }
8642 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8643 && !saw_smaller_pos
8644 && IT_CHARPOS (*it) > to_charpos)
8645 {
8646 if (IT_CHARPOS (ppos_it) < ZV)
8647 RESTORE_IT (it, &ppos_it, ppos_data);
8648 result = MOVE_POS_MATCH_OR_ZV;
8649 break;
8650 }
8651 result = MOVE_LINE_TRUNCATED;
8652 break;
8653 }
8654 #undef IT_RESET_X_ASCENT_DESCENT
8655 }
8656
8657 #undef BUFFER_POS_REACHED_P
8658
8659 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8660 restore the saved iterator. */
8661 if (atpos_it.sp >= 0)
8662 RESTORE_IT (it, &atpos_it, atpos_data);
8663 else if (atx_it.sp >= 0)
8664 RESTORE_IT (it, &atx_it, atx_data);
8665
8666 done:
8667
8668 if (atpos_data)
8669 bidi_unshelve_cache (atpos_data, 1);
8670 if (atx_data)
8671 bidi_unshelve_cache (atx_data, 1);
8672 if (wrap_data)
8673 bidi_unshelve_cache (wrap_data, 1);
8674 if (ppos_data)
8675 bidi_unshelve_cache (ppos_data, 1);
8676
8677 /* Restore the iterator settings altered at the beginning of this
8678 function. */
8679 it->glyph_row = saved_glyph_row;
8680 return result;
8681 }
8682
8683 /* For external use. */
8684 void
8685 move_it_in_display_line (struct it *it,
8686 ptrdiff_t to_charpos, int to_x,
8687 enum move_operation_enum op)
8688 {
8689 if (it->line_wrap == WORD_WRAP
8690 && (op & MOVE_TO_X))
8691 {
8692 struct it save_it;
8693 void *save_data = NULL;
8694 int skip;
8695
8696 SAVE_IT (save_it, *it, save_data);
8697 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8698 /* When word-wrap is on, TO_X may lie past the end
8699 of a wrapped line. Then it->current is the
8700 character on the next line, so backtrack to the
8701 space before the wrap point. */
8702 if (skip == MOVE_LINE_CONTINUED)
8703 {
8704 int prev_x = max (it->current_x - 1, 0);
8705 RESTORE_IT (it, &save_it, save_data);
8706 move_it_in_display_line_to
8707 (it, -1, prev_x, MOVE_TO_X);
8708 }
8709 else
8710 bidi_unshelve_cache (save_data, 1);
8711 }
8712 else
8713 move_it_in_display_line_to (it, to_charpos, to_x, op);
8714 }
8715
8716
8717 /* Move IT forward until it satisfies one or more of the criteria in
8718 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8719
8720 OP is a bit-mask that specifies where to stop, and in particular,
8721 which of those four position arguments makes a difference. See the
8722 description of enum move_operation_enum.
8723
8724 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8725 screen line, this function will set IT to the next position that is
8726 displayed to the right of TO_CHARPOS on the screen. */
8727
8728 void
8729 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8730 {
8731 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8732 int line_height, line_start_x = 0, reached = 0;
8733 void *backup_data = NULL;
8734
8735 for (;;)
8736 {
8737 if (op & MOVE_TO_VPOS)
8738 {
8739 /* If no TO_CHARPOS and no TO_X specified, stop at the
8740 start of the line TO_VPOS. */
8741 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8742 {
8743 if (it->vpos == to_vpos)
8744 {
8745 reached = 1;
8746 break;
8747 }
8748 else
8749 skip = move_it_in_display_line_to (it, -1, -1, 0);
8750 }
8751 else
8752 {
8753 /* TO_VPOS >= 0 means stop at TO_X in the line at
8754 TO_VPOS, or at TO_POS, whichever comes first. */
8755 if (it->vpos == to_vpos)
8756 {
8757 reached = 2;
8758 break;
8759 }
8760
8761 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8762
8763 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8764 {
8765 reached = 3;
8766 break;
8767 }
8768 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8769 {
8770 /* We have reached TO_X but not in the line we want. */
8771 skip = move_it_in_display_line_to (it, to_charpos,
8772 -1, MOVE_TO_POS);
8773 if (skip == MOVE_POS_MATCH_OR_ZV)
8774 {
8775 reached = 4;
8776 break;
8777 }
8778 }
8779 }
8780 }
8781 else if (op & MOVE_TO_Y)
8782 {
8783 struct it it_backup;
8784
8785 if (it->line_wrap == WORD_WRAP)
8786 SAVE_IT (it_backup, *it, backup_data);
8787
8788 /* TO_Y specified means stop at TO_X in the line containing
8789 TO_Y---or at TO_CHARPOS if this is reached first. The
8790 problem is that we can't really tell whether the line
8791 contains TO_Y before we have completely scanned it, and
8792 this may skip past TO_X. What we do is to first scan to
8793 TO_X.
8794
8795 If TO_X is not specified, use a TO_X of zero. The reason
8796 is to make the outcome of this function more predictable.
8797 If we didn't use TO_X == 0, we would stop at the end of
8798 the line which is probably not what a caller would expect
8799 to happen. */
8800 skip = move_it_in_display_line_to
8801 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8802 (MOVE_TO_X | (op & MOVE_TO_POS)));
8803
8804 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8805 if (skip == MOVE_POS_MATCH_OR_ZV)
8806 reached = 5;
8807 else if (skip == MOVE_X_REACHED)
8808 {
8809 /* If TO_X was reached, we want to know whether TO_Y is
8810 in the line. We know this is the case if the already
8811 scanned glyphs make the line tall enough. Otherwise,
8812 we must check by scanning the rest of the line. */
8813 line_height = it->max_ascent + it->max_descent;
8814 if (to_y >= it->current_y
8815 && to_y < it->current_y + line_height)
8816 {
8817 reached = 6;
8818 break;
8819 }
8820 SAVE_IT (it_backup, *it, backup_data);
8821 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8822 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8823 op & MOVE_TO_POS);
8824 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8825 line_height = it->max_ascent + it->max_descent;
8826 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8827
8828 if (to_y >= it->current_y
8829 && to_y < it->current_y + line_height)
8830 {
8831 /* If TO_Y is in this line and TO_X was reached
8832 above, we scanned too far. We have to restore
8833 IT's settings to the ones before skipping. But
8834 keep the more accurate values of max_ascent and
8835 max_descent we've found while skipping the rest
8836 of the line, for the sake of callers, such as
8837 pos_visible_p, that need to know the line
8838 height. */
8839 int max_ascent = it->max_ascent;
8840 int max_descent = it->max_descent;
8841
8842 RESTORE_IT (it, &it_backup, backup_data);
8843 it->max_ascent = max_ascent;
8844 it->max_descent = max_descent;
8845 reached = 6;
8846 }
8847 else
8848 {
8849 skip = skip2;
8850 if (skip == MOVE_POS_MATCH_OR_ZV)
8851 reached = 7;
8852 }
8853 }
8854 else
8855 {
8856 /* Check whether TO_Y is in this line. */
8857 line_height = it->max_ascent + it->max_descent;
8858 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8859
8860 if (to_y >= it->current_y
8861 && to_y < it->current_y + line_height)
8862 {
8863 /* When word-wrap is on, TO_X may lie past the end
8864 of a wrapped line. Then it->current is the
8865 character on the next line, so backtrack to the
8866 space before the wrap point. */
8867 if (skip == MOVE_LINE_CONTINUED
8868 && it->line_wrap == WORD_WRAP)
8869 {
8870 int prev_x = max (it->current_x - 1, 0);
8871 RESTORE_IT (it, &it_backup, backup_data);
8872 skip = move_it_in_display_line_to
8873 (it, -1, prev_x, MOVE_TO_X);
8874 }
8875 reached = 6;
8876 }
8877 }
8878
8879 if (reached)
8880 break;
8881 }
8882 else if (BUFFERP (it->object)
8883 && (it->method == GET_FROM_BUFFER
8884 || it->method == GET_FROM_STRETCH)
8885 && IT_CHARPOS (*it) >= to_charpos
8886 /* Under bidi iteration, a call to set_iterator_to_next
8887 can scan far beyond to_charpos if the initial
8888 portion of the next line needs to be reordered. In
8889 that case, give move_it_in_display_line_to another
8890 chance below. */
8891 && !(it->bidi_p
8892 && it->bidi_it.scan_dir == -1))
8893 skip = MOVE_POS_MATCH_OR_ZV;
8894 else
8895 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8896
8897 switch (skip)
8898 {
8899 case MOVE_POS_MATCH_OR_ZV:
8900 reached = 8;
8901 goto out;
8902
8903 case MOVE_NEWLINE_OR_CR:
8904 set_iterator_to_next (it, 1);
8905 it->continuation_lines_width = 0;
8906 break;
8907
8908 case MOVE_LINE_TRUNCATED:
8909 it->continuation_lines_width = 0;
8910 reseat_at_next_visible_line_start (it, 0);
8911 if ((op & MOVE_TO_POS) != 0
8912 && IT_CHARPOS (*it) > to_charpos)
8913 {
8914 reached = 9;
8915 goto out;
8916 }
8917 break;
8918
8919 case MOVE_LINE_CONTINUED:
8920 /* For continued lines ending in a tab, some of the glyphs
8921 associated with the tab are displayed on the current
8922 line. Since it->current_x does not include these glyphs,
8923 we use it->last_visible_x instead. */
8924 if (it->c == '\t')
8925 {
8926 it->continuation_lines_width += it->last_visible_x;
8927 /* When moving by vpos, ensure that the iterator really
8928 advances to the next line (bug#847, bug#969). Fixme:
8929 do we need to do this in other circumstances? */
8930 if (it->current_x != it->last_visible_x
8931 && (op & MOVE_TO_VPOS)
8932 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8933 {
8934 line_start_x = it->current_x + it->pixel_width
8935 - it->last_visible_x;
8936 set_iterator_to_next (it, 0);
8937 }
8938 }
8939 else
8940 it->continuation_lines_width += it->current_x;
8941 break;
8942
8943 default:
8944 emacs_abort ();
8945 }
8946
8947 /* Reset/increment for the next run. */
8948 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
8949 it->current_x = line_start_x;
8950 line_start_x = 0;
8951 it->hpos = 0;
8952 it->current_y += it->max_ascent + it->max_descent;
8953 ++it->vpos;
8954 last_height = it->max_ascent + it->max_descent;
8955 last_max_ascent = it->max_ascent;
8956 it->max_ascent = it->max_descent = 0;
8957 }
8958
8959 out:
8960
8961 /* On text terminals, we may stop at the end of a line in the middle
8962 of a multi-character glyph. If the glyph itself is continued,
8963 i.e. it is actually displayed on the next line, don't treat this
8964 stopping point as valid; move to the next line instead (unless
8965 that brings us offscreen). */
8966 if (!FRAME_WINDOW_P (it->f)
8967 && op & MOVE_TO_POS
8968 && IT_CHARPOS (*it) == to_charpos
8969 && it->what == IT_CHARACTER
8970 && it->nglyphs > 1
8971 && it->line_wrap == WINDOW_WRAP
8972 && it->current_x == it->last_visible_x - 1
8973 && it->c != '\n'
8974 && it->c != '\t'
8975 && it->vpos < XFASTINT (it->w->window_end_vpos))
8976 {
8977 it->continuation_lines_width += it->current_x;
8978 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
8979 it->current_y += it->max_ascent + it->max_descent;
8980 ++it->vpos;
8981 last_height = it->max_ascent + it->max_descent;
8982 last_max_ascent = it->max_ascent;
8983 }
8984
8985 if (backup_data)
8986 bidi_unshelve_cache (backup_data, 1);
8987
8988 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
8989 }
8990
8991
8992 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
8993
8994 If DY > 0, move IT backward at least that many pixels. DY = 0
8995 means move IT backward to the preceding line start or BEGV. This
8996 function may move over more than DY pixels if IT->current_y - DY
8997 ends up in the middle of a line; in this case IT->current_y will be
8998 set to the top of the line moved to. */
8999
9000 void
9001 move_it_vertically_backward (struct it *it, int dy)
9002 {
9003 int nlines, h;
9004 struct it it2, it3;
9005 void *it2data = NULL, *it3data = NULL;
9006 ptrdiff_t start_pos;
9007
9008 move_further_back:
9009 eassert (dy >= 0);
9010
9011 start_pos = IT_CHARPOS (*it);
9012
9013 /* Estimate how many newlines we must move back. */
9014 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
9015
9016 /* Set the iterator's position that many lines back. */
9017 while (nlines-- && IT_CHARPOS (*it) > BEGV)
9018 back_to_previous_visible_line_start (it);
9019
9020 /* Reseat the iterator here. When moving backward, we don't want
9021 reseat to skip forward over invisible text, set up the iterator
9022 to deliver from overlay strings at the new position etc. So,
9023 use reseat_1 here. */
9024 reseat_1 (it, it->current.pos, 1);
9025
9026 /* We are now surely at a line start. */
9027 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9028 reordering is in effect. */
9029 it->continuation_lines_width = 0;
9030
9031 /* Move forward and see what y-distance we moved. First move to the
9032 start of the next line so that we get its height. We need this
9033 height to be able to tell whether we reached the specified
9034 y-distance. */
9035 SAVE_IT (it2, *it, it2data);
9036 it2.max_ascent = it2.max_descent = 0;
9037 do
9038 {
9039 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9040 MOVE_TO_POS | MOVE_TO_VPOS);
9041 }
9042 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9043 /* If we are in a display string which starts at START_POS,
9044 and that display string includes a newline, and we are
9045 right after that newline (i.e. at the beginning of a
9046 display line), exit the loop, because otherwise we will
9047 infloop, since move_it_to will see that it is already at
9048 START_POS and will not move. */
9049 || (it2.method == GET_FROM_STRING
9050 && IT_CHARPOS (it2) == start_pos
9051 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9052 eassert (IT_CHARPOS (*it) >= BEGV);
9053 SAVE_IT (it3, it2, it3data);
9054
9055 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9056 eassert (IT_CHARPOS (*it) >= BEGV);
9057 /* H is the actual vertical distance from the position in *IT
9058 and the starting position. */
9059 h = it2.current_y - it->current_y;
9060 /* NLINES is the distance in number of lines. */
9061 nlines = it2.vpos - it->vpos;
9062
9063 /* Correct IT's y and vpos position
9064 so that they are relative to the starting point. */
9065 it->vpos -= nlines;
9066 it->current_y -= h;
9067
9068 if (dy == 0)
9069 {
9070 /* DY == 0 means move to the start of the screen line. The
9071 value of nlines is > 0 if continuation lines were involved,
9072 or if the original IT position was at start of a line. */
9073 RESTORE_IT (it, it, it2data);
9074 if (nlines > 0)
9075 move_it_by_lines (it, nlines);
9076 /* The above code moves us to some position NLINES down,
9077 usually to its first glyph (leftmost in an L2R line), but
9078 that's not necessarily the start of the line, under bidi
9079 reordering. We want to get to the character position
9080 that is immediately after the newline of the previous
9081 line. */
9082 if (it->bidi_p
9083 && !it->continuation_lines_width
9084 && !STRINGP (it->string)
9085 && IT_CHARPOS (*it) > BEGV
9086 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9087 {
9088 ptrdiff_t nl_pos =
9089 find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
9090
9091 move_it_to (it, nl_pos, -1, -1, -1, MOVE_TO_POS);
9092 }
9093 bidi_unshelve_cache (it3data, 1);
9094 }
9095 else
9096 {
9097 /* The y-position we try to reach, relative to *IT.
9098 Note that H has been subtracted in front of the if-statement. */
9099 int target_y = it->current_y + h - dy;
9100 int y0 = it3.current_y;
9101 int y1;
9102 int line_height;
9103
9104 RESTORE_IT (&it3, &it3, it3data);
9105 y1 = line_bottom_y (&it3);
9106 line_height = y1 - y0;
9107 RESTORE_IT (it, it, it2data);
9108 /* If we did not reach target_y, try to move further backward if
9109 we can. If we moved too far backward, try to move forward. */
9110 if (target_y < it->current_y
9111 /* This is heuristic. In a window that's 3 lines high, with
9112 a line height of 13 pixels each, recentering with point
9113 on the bottom line will try to move -39/2 = 19 pixels
9114 backward. Try to avoid moving into the first line. */
9115 && (it->current_y - target_y
9116 > min (window_box_height (it->w), line_height * 2 / 3))
9117 && IT_CHARPOS (*it) > BEGV)
9118 {
9119 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9120 target_y - it->current_y));
9121 dy = it->current_y - target_y;
9122 goto move_further_back;
9123 }
9124 else if (target_y >= it->current_y + line_height
9125 && IT_CHARPOS (*it) < ZV)
9126 {
9127 /* Should move forward by at least one line, maybe more.
9128
9129 Note: Calling move_it_by_lines can be expensive on
9130 terminal frames, where compute_motion is used (via
9131 vmotion) to do the job, when there are very long lines
9132 and truncate-lines is nil. That's the reason for
9133 treating terminal frames specially here. */
9134
9135 if (!FRAME_WINDOW_P (it->f))
9136 move_it_vertically (it, target_y - (it->current_y + line_height));
9137 else
9138 {
9139 do
9140 {
9141 move_it_by_lines (it, 1);
9142 }
9143 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9144 }
9145 }
9146 }
9147 }
9148
9149
9150 /* Move IT by a specified amount of pixel lines DY. DY negative means
9151 move backwards. DY = 0 means move to start of screen line. At the
9152 end, IT will be on the start of a screen line. */
9153
9154 void
9155 move_it_vertically (struct it *it, int dy)
9156 {
9157 if (dy <= 0)
9158 move_it_vertically_backward (it, -dy);
9159 else
9160 {
9161 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9162 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9163 MOVE_TO_POS | MOVE_TO_Y);
9164 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9165
9166 /* If buffer ends in ZV without a newline, move to the start of
9167 the line to satisfy the post-condition. */
9168 if (IT_CHARPOS (*it) == ZV
9169 && ZV > BEGV
9170 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9171 move_it_by_lines (it, 0);
9172 }
9173 }
9174
9175
9176 /* Move iterator IT past the end of the text line it is in. */
9177
9178 void
9179 move_it_past_eol (struct it *it)
9180 {
9181 enum move_it_result rc;
9182
9183 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9184 if (rc == MOVE_NEWLINE_OR_CR)
9185 set_iterator_to_next (it, 0);
9186 }
9187
9188
9189 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9190 negative means move up. DVPOS == 0 means move to the start of the
9191 screen line.
9192
9193 Optimization idea: If we would know that IT->f doesn't use
9194 a face with proportional font, we could be faster for
9195 truncate-lines nil. */
9196
9197 void
9198 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9199 {
9200
9201 /* The commented-out optimization uses vmotion on terminals. This
9202 gives bad results, because elements like it->what, on which
9203 callers such as pos_visible_p rely, aren't updated. */
9204 /* struct position pos;
9205 if (!FRAME_WINDOW_P (it->f))
9206 {
9207 struct text_pos textpos;
9208
9209 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9210 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9211 reseat (it, textpos, 1);
9212 it->vpos += pos.vpos;
9213 it->current_y += pos.vpos;
9214 }
9215 else */
9216
9217 if (dvpos == 0)
9218 {
9219 /* DVPOS == 0 means move to the start of the screen line. */
9220 move_it_vertically_backward (it, 0);
9221 /* Let next call to line_bottom_y calculate real line height */
9222 last_height = 0;
9223 }
9224 else if (dvpos > 0)
9225 {
9226 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9227 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9228 {
9229 /* Only move to the next buffer position if we ended up in a
9230 string from display property, not in an overlay string
9231 (before-string or after-string). That is because the
9232 latter don't conceal the underlying buffer position, so
9233 we can ask to move the iterator to the exact position we
9234 are interested in. Note that, even if we are already at
9235 IT_CHARPOS (*it), the call below is not a no-op, as it
9236 will detect that we are at the end of the string, pop the
9237 iterator, and compute it->current_x and it->hpos
9238 correctly. */
9239 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9240 -1, -1, -1, MOVE_TO_POS);
9241 }
9242 }
9243 else
9244 {
9245 struct it it2;
9246 void *it2data = NULL;
9247 ptrdiff_t start_charpos, i;
9248
9249 /* Start at the beginning of the screen line containing IT's
9250 position. This may actually move vertically backwards,
9251 in case of overlays, so adjust dvpos accordingly. */
9252 dvpos += it->vpos;
9253 move_it_vertically_backward (it, 0);
9254 dvpos -= it->vpos;
9255
9256 /* Go back -DVPOS visible lines and reseat the iterator there. */
9257 start_charpos = IT_CHARPOS (*it);
9258 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
9259 back_to_previous_visible_line_start (it);
9260 reseat (it, it->current.pos, 1);
9261
9262 /* Move further back if we end up in a string or an image. */
9263 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9264 {
9265 /* First try to move to start of display line. */
9266 dvpos += it->vpos;
9267 move_it_vertically_backward (it, 0);
9268 dvpos -= it->vpos;
9269 if (IT_POS_VALID_AFTER_MOVE_P (it))
9270 break;
9271 /* If start of line is still in string or image,
9272 move further back. */
9273 back_to_previous_visible_line_start (it);
9274 reseat (it, it->current.pos, 1);
9275 dvpos--;
9276 }
9277
9278 it->current_x = it->hpos = 0;
9279
9280 /* Above call may have moved too far if continuation lines
9281 are involved. Scan forward and see if it did. */
9282 SAVE_IT (it2, *it, it2data);
9283 it2.vpos = it2.current_y = 0;
9284 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9285 it->vpos -= it2.vpos;
9286 it->current_y -= it2.current_y;
9287 it->current_x = it->hpos = 0;
9288
9289 /* If we moved too far back, move IT some lines forward. */
9290 if (it2.vpos > -dvpos)
9291 {
9292 int delta = it2.vpos + dvpos;
9293
9294 RESTORE_IT (&it2, &it2, it2data);
9295 SAVE_IT (it2, *it, it2data);
9296 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9297 /* Move back again if we got too far ahead. */
9298 if (IT_CHARPOS (*it) >= start_charpos)
9299 RESTORE_IT (it, &it2, it2data);
9300 else
9301 bidi_unshelve_cache (it2data, 1);
9302 }
9303 else
9304 RESTORE_IT (it, it, it2data);
9305 }
9306 }
9307
9308 /* Return 1 if IT points into the middle of a display vector. */
9309
9310 int
9311 in_display_vector_p (struct it *it)
9312 {
9313 return (it->method == GET_FROM_DISPLAY_VECTOR
9314 && it->current.dpvec_index > 0
9315 && it->dpvec + it->current.dpvec_index != it->dpend);
9316 }
9317
9318 \f
9319 /***********************************************************************
9320 Messages
9321 ***********************************************************************/
9322
9323
9324 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9325 to *Messages*. */
9326
9327 void
9328 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9329 {
9330 Lisp_Object args[3];
9331 Lisp_Object msg, fmt;
9332 char *buffer;
9333 ptrdiff_t len;
9334 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9335 USE_SAFE_ALLOCA;
9336
9337 fmt = msg = Qnil;
9338 GCPRO4 (fmt, msg, arg1, arg2);
9339
9340 args[0] = fmt = build_string (format);
9341 args[1] = arg1;
9342 args[2] = arg2;
9343 msg = Fformat (3, args);
9344
9345 len = SBYTES (msg) + 1;
9346 buffer = SAFE_ALLOCA (len);
9347 memcpy (buffer, SDATA (msg), len);
9348
9349 message_dolog (buffer, len - 1, 1, 0);
9350 SAFE_FREE ();
9351
9352 UNGCPRO;
9353 }
9354
9355
9356 /* Output a newline in the *Messages* buffer if "needs" one. */
9357
9358 void
9359 message_log_maybe_newline (void)
9360 {
9361 if (message_log_need_newline)
9362 message_dolog ("", 0, 1, 0);
9363 }
9364
9365
9366 /* Add a string M of length NBYTES to the message log, optionally
9367 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
9368 nonzero, means interpret the contents of M as multibyte. This
9369 function calls low-level routines in order to bypass text property
9370 hooks, etc. which might not be safe to run.
9371
9372 This may GC (insert may run before/after change hooks),
9373 so the buffer M must NOT point to a Lisp string. */
9374
9375 void
9376 message_dolog (const char *m, ptrdiff_t nbytes, int nlflag, int multibyte)
9377 {
9378 const unsigned char *msg = (const unsigned char *) m;
9379
9380 if (!NILP (Vmemory_full))
9381 return;
9382
9383 if (!NILP (Vmessage_log_max))
9384 {
9385 struct buffer *oldbuf;
9386 Lisp_Object oldpoint, oldbegv, oldzv;
9387 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9388 ptrdiff_t point_at_end = 0;
9389 ptrdiff_t zv_at_end = 0;
9390 Lisp_Object old_deactivate_mark, tem;
9391 struct gcpro gcpro1;
9392
9393 old_deactivate_mark = Vdeactivate_mark;
9394 oldbuf = current_buffer;
9395 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9396 bset_undo_list (current_buffer, Qt);
9397
9398 oldpoint = message_dolog_marker1;
9399 set_marker_restricted (oldpoint, make_number (PT), Qnil);
9400 oldbegv = message_dolog_marker2;
9401 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
9402 oldzv = message_dolog_marker3;
9403 set_marker_restricted (oldzv, make_number (ZV), Qnil);
9404 GCPRO1 (old_deactivate_mark);
9405
9406 if (PT == Z)
9407 point_at_end = 1;
9408 if (ZV == Z)
9409 zv_at_end = 1;
9410
9411 BEGV = BEG;
9412 BEGV_BYTE = BEG_BYTE;
9413 ZV = Z;
9414 ZV_BYTE = Z_BYTE;
9415 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9416
9417 /* Insert the string--maybe converting multibyte to single byte
9418 or vice versa, so that all the text fits the buffer. */
9419 if (multibyte
9420 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9421 {
9422 ptrdiff_t i;
9423 int c, char_bytes;
9424 char work[1];
9425
9426 /* Convert a multibyte string to single-byte
9427 for the *Message* buffer. */
9428 for (i = 0; i < nbytes; i += char_bytes)
9429 {
9430 c = string_char_and_length (msg + i, &char_bytes);
9431 work[0] = (ASCII_CHAR_P (c)
9432 ? c
9433 : multibyte_char_to_unibyte (c));
9434 insert_1_both (work, 1, 1, 1, 0, 0);
9435 }
9436 }
9437 else if (! multibyte
9438 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9439 {
9440 ptrdiff_t i;
9441 int c, char_bytes;
9442 unsigned char str[MAX_MULTIBYTE_LENGTH];
9443 /* Convert a single-byte string to multibyte
9444 for the *Message* buffer. */
9445 for (i = 0; i < nbytes; i++)
9446 {
9447 c = msg[i];
9448 MAKE_CHAR_MULTIBYTE (c);
9449 char_bytes = CHAR_STRING (c, str);
9450 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9451 }
9452 }
9453 else if (nbytes)
9454 insert_1 (m, nbytes, 1, 0, 0);
9455
9456 if (nlflag)
9457 {
9458 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9459 printmax_t dups;
9460 insert_1 ("\n", 1, 1, 0, 0);
9461
9462 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9463 this_bol = PT;
9464 this_bol_byte = PT_BYTE;
9465
9466 /* See if this line duplicates the previous one.
9467 If so, combine duplicates. */
9468 if (this_bol > BEG)
9469 {
9470 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9471 prev_bol = PT;
9472 prev_bol_byte = PT_BYTE;
9473
9474 dups = message_log_check_duplicate (prev_bol_byte,
9475 this_bol_byte);
9476 if (dups)
9477 {
9478 del_range_both (prev_bol, prev_bol_byte,
9479 this_bol, this_bol_byte, 0);
9480 if (dups > 1)
9481 {
9482 char dupstr[sizeof " [ times]"
9483 + INT_STRLEN_BOUND (printmax_t)];
9484
9485 /* If you change this format, don't forget to also
9486 change message_log_check_duplicate. */
9487 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9488 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9489 insert_1 (dupstr, duplen, 1, 0, 1);
9490 }
9491 }
9492 }
9493
9494 /* If we have more than the desired maximum number of lines
9495 in the *Messages* buffer now, delete the oldest ones.
9496 This is safe because we don't have undo in this buffer. */
9497
9498 if (NATNUMP (Vmessage_log_max))
9499 {
9500 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9501 -XFASTINT (Vmessage_log_max) - 1, 0);
9502 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9503 }
9504 }
9505 BEGV = XMARKER (oldbegv)->charpos;
9506 BEGV_BYTE = marker_byte_position (oldbegv);
9507
9508 if (zv_at_end)
9509 {
9510 ZV = Z;
9511 ZV_BYTE = Z_BYTE;
9512 }
9513 else
9514 {
9515 ZV = XMARKER (oldzv)->charpos;
9516 ZV_BYTE = marker_byte_position (oldzv);
9517 }
9518
9519 if (point_at_end)
9520 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9521 else
9522 /* We can't do Fgoto_char (oldpoint) because it will run some
9523 Lisp code. */
9524 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
9525 XMARKER (oldpoint)->bytepos);
9526
9527 UNGCPRO;
9528 unchain_marker (XMARKER (oldpoint));
9529 unchain_marker (XMARKER (oldbegv));
9530 unchain_marker (XMARKER (oldzv));
9531
9532 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
9533 set_buffer_internal (oldbuf);
9534 if (NILP (tem))
9535 windows_or_buffers_changed = old_windows_or_buffers_changed;
9536 message_log_need_newline = !nlflag;
9537 Vdeactivate_mark = old_deactivate_mark;
9538 }
9539 }
9540
9541
9542 /* We are at the end of the buffer after just having inserted a newline.
9543 (Note: We depend on the fact we won't be crossing the gap.)
9544 Check to see if the most recent message looks a lot like the previous one.
9545 Return 0 if different, 1 if the new one should just replace it, or a
9546 value N > 1 if we should also append " [N times]". */
9547
9548 static intmax_t
9549 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9550 {
9551 ptrdiff_t i;
9552 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9553 int seen_dots = 0;
9554 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9555 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9556
9557 for (i = 0; i < len; i++)
9558 {
9559 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
9560 seen_dots = 1;
9561 if (p1[i] != p2[i])
9562 return seen_dots;
9563 }
9564 p1 += len;
9565 if (*p1 == '\n')
9566 return 2;
9567 if (*p1++ == ' ' && *p1++ == '[')
9568 {
9569 char *pend;
9570 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9571 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9572 return n+1;
9573 }
9574 return 0;
9575 }
9576 \f
9577
9578 /* Display an echo area message M with a specified length of NBYTES
9579 bytes. The string may include null characters. If M is 0, clear
9580 out any existing message, and let the mini-buffer text show
9581 through.
9582
9583 This may GC, so the buffer M must NOT point to a Lisp string. */
9584
9585 void
9586 message2 (const char *m, ptrdiff_t nbytes, int multibyte)
9587 {
9588 /* First flush out any partial line written with print. */
9589 message_log_maybe_newline ();
9590 if (m)
9591 message_dolog (m, nbytes, 1, multibyte);
9592 message2_nolog (m, nbytes, multibyte);
9593 }
9594
9595
9596 /* The non-logging counterpart of message2. */
9597
9598 void
9599 message2_nolog (const char *m, ptrdiff_t nbytes, int multibyte)
9600 {
9601 struct frame *sf = SELECTED_FRAME ();
9602 message_enable_multibyte = multibyte;
9603
9604 if (FRAME_INITIAL_P (sf))
9605 {
9606 if (noninteractive_need_newline)
9607 putc ('\n', stderr);
9608 noninteractive_need_newline = 0;
9609 if (m)
9610 fwrite (m, nbytes, 1, stderr);
9611 if (cursor_in_echo_area == 0)
9612 fprintf (stderr, "\n");
9613 fflush (stderr);
9614 }
9615 /* A null message buffer means that the frame hasn't really been
9616 initialized yet. Error messages get reported properly by
9617 cmd_error, so this must be just an informative message; toss it. */
9618 else if (INTERACTIVE
9619 && sf->glyphs_initialized_p
9620 && FRAME_MESSAGE_BUF (sf))
9621 {
9622 Lisp_Object mini_window;
9623 struct frame *f;
9624
9625 /* Get the frame containing the mini-buffer
9626 that the selected frame is using. */
9627 mini_window = FRAME_MINIBUF_WINDOW (sf);
9628 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9629
9630 FRAME_SAMPLE_VISIBILITY (f);
9631 if (FRAME_VISIBLE_P (sf)
9632 && ! FRAME_VISIBLE_P (f))
9633 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
9634
9635 if (m)
9636 {
9637 set_message (m, Qnil, nbytes, multibyte);
9638 if (minibuffer_auto_raise)
9639 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9640 }
9641 else
9642 clear_message (1, 1);
9643
9644 do_pending_window_change (0);
9645 echo_area_display (1);
9646 do_pending_window_change (0);
9647 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9648 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9649 }
9650 }
9651
9652
9653 /* Display an echo area message M with a specified length of NBYTES
9654 bytes. The string may include null characters. If M is not a
9655 string, clear out any existing message, and let the mini-buffer
9656 text show through.
9657
9658 This function cancels echoing. */
9659
9660 void
9661 message3 (Lisp_Object m, ptrdiff_t nbytes, int multibyte)
9662 {
9663 struct gcpro gcpro1;
9664
9665 GCPRO1 (m);
9666 clear_message (1,1);
9667 cancel_echoing ();
9668
9669 /* First flush out any partial line written with print. */
9670 message_log_maybe_newline ();
9671 if (STRINGP (m))
9672 {
9673 USE_SAFE_ALLOCA;
9674 char *buffer = SAFE_ALLOCA (nbytes);
9675 memcpy (buffer, SDATA (m), nbytes);
9676 message_dolog (buffer, nbytes, 1, multibyte);
9677 SAFE_FREE ();
9678 }
9679 message3_nolog (m, nbytes, multibyte);
9680
9681 UNGCPRO;
9682 }
9683
9684
9685 /* The non-logging version of message3.
9686 This does not cancel echoing, because it is used for echoing.
9687 Perhaps we need to make a separate function for echoing
9688 and make this cancel echoing. */
9689
9690 void
9691 message3_nolog (Lisp_Object m, ptrdiff_t nbytes, int multibyte)
9692 {
9693 struct frame *sf = SELECTED_FRAME ();
9694 message_enable_multibyte = multibyte;
9695
9696 if (FRAME_INITIAL_P (sf))
9697 {
9698 if (noninteractive_need_newline)
9699 putc ('\n', stderr);
9700 noninteractive_need_newline = 0;
9701 if (STRINGP (m))
9702 fwrite (SDATA (m), nbytes, 1, stderr);
9703 if (cursor_in_echo_area == 0)
9704 fprintf (stderr, "\n");
9705 fflush (stderr);
9706 }
9707 /* A null message buffer means that the frame hasn't really been
9708 initialized yet. Error messages get reported properly by
9709 cmd_error, so this must be just an informative message; toss it. */
9710 else if (INTERACTIVE
9711 && sf->glyphs_initialized_p
9712 && FRAME_MESSAGE_BUF (sf))
9713 {
9714 Lisp_Object mini_window;
9715 Lisp_Object frame;
9716 struct frame *f;
9717
9718 /* Get the frame containing the mini-buffer
9719 that the selected frame is using. */
9720 mini_window = FRAME_MINIBUF_WINDOW (sf);
9721 frame = XWINDOW (mini_window)->frame;
9722 f = XFRAME (frame);
9723
9724 FRAME_SAMPLE_VISIBILITY (f);
9725 if (FRAME_VISIBLE_P (sf)
9726 && !FRAME_VISIBLE_P (f))
9727 Fmake_frame_visible (frame);
9728
9729 if (STRINGP (m) && SCHARS (m) > 0)
9730 {
9731 set_message (NULL, m, nbytes, multibyte);
9732 if (minibuffer_auto_raise)
9733 Fraise_frame (frame);
9734 /* Assume we are not echoing.
9735 (If we are, echo_now will override this.) */
9736 echo_message_buffer = Qnil;
9737 }
9738 else
9739 clear_message (1, 1);
9740
9741 do_pending_window_change (0);
9742 echo_area_display (1);
9743 do_pending_window_change (0);
9744 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9745 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9746 }
9747 }
9748
9749
9750 /* Display a null-terminated echo area message M. If M is 0, clear
9751 out any existing message, and let the mini-buffer text show through.
9752
9753 The buffer M must continue to exist until after the echo area gets
9754 cleared or some other message gets displayed there. Do not pass
9755 text that is stored in a Lisp string. Do not pass text in a buffer
9756 that was alloca'd. */
9757
9758 void
9759 message1 (const char *m)
9760 {
9761 message2 (m, (m ? strlen (m) : 0), 0);
9762 }
9763
9764
9765 /* The non-logging counterpart of message1. */
9766
9767 void
9768 message1_nolog (const char *m)
9769 {
9770 message2_nolog (m, (m ? strlen (m) : 0), 0);
9771 }
9772
9773 /* Display a message M which contains a single %s
9774 which gets replaced with STRING. */
9775
9776 void
9777 message_with_string (const char *m, Lisp_Object string, int log)
9778 {
9779 CHECK_STRING (string);
9780
9781 if (noninteractive)
9782 {
9783 if (m)
9784 {
9785 if (noninteractive_need_newline)
9786 putc ('\n', stderr);
9787 noninteractive_need_newline = 0;
9788 fprintf (stderr, m, SDATA (string));
9789 if (!cursor_in_echo_area)
9790 fprintf (stderr, "\n");
9791 fflush (stderr);
9792 }
9793 }
9794 else if (INTERACTIVE)
9795 {
9796 /* The frame whose minibuffer we're going to display the message on.
9797 It may be larger than the selected frame, so we need
9798 to use its buffer, not the selected frame's buffer. */
9799 Lisp_Object mini_window;
9800 struct frame *f, *sf = SELECTED_FRAME ();
9801
9802 /* Get the frame containing the minibuffer
9803 that the selected frame is using. */
9804 mini_window = FRAME_MINIBUF_WINDOW (sf);
9805 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9806
9807 /* A null message buffer means that the frame hasn't really been
9808 initialized yet. Error messages get reported properly by
9809 cmd_error, so this must be just an informative message; toss it. */
9810 if (FRAME_MESSAGE_BUF (f))
9811 {
9812 Lisp_Object args[2], msg;
9813 struct gcpro gcpro1, gcpro2;
9814
9815 args[0] = build_string (m);
9816 args[1] = msg = string;
9817 GCPRO2 (args[0], msg);
9818 gcpro1.nvars = 2;
9819
9820 msg = Fformat (2, args);
9821
9822 if (log)
9823 message3 (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9824 else
9825 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9826
9827 UNGCPRO;
9828
9829 /* Print should start at the beginning of the message
9830 buffer next time. */
9831 message_buf_print = 0;
9832 }
9833 }
9834 }
9835
9836
9837 /* Dump an informative message to the minibuf. If M is 0, clear out
9838 any existing message, and let the mini-buffer text show through. */
9839
9840 static void
9841 vmessage (const char *m, va_list ap)
9842 {
9843 if (noninteractive)
9844 {
9845 if (m)
9846 {
9847 if (noninteractive_need_newline)
9848 putc ('\n', stderr);
9849 noninteractive_need_newline = 0;
9850 vfprintf (stderr, m, ap);
9851 if (cursor_in_echo_area == 0)
9852 fprintf (stderr, "\n");
9853 fflush (stderr);
9854 }
9855 }
9856 else if (INTERACTIVE)
9857 {
9858 /* The frame whose mini-buffer we're going to display the message
9859 on. It may be larger than the selected frame, so we need to
9860 use its buffer, not the selected frame's buffer. */
9861 Lisp_Object mini_window;
9862 struct frame *f, *sf = SELECTED_FRAME ();
9863
9864 /* Get the frame containing the mini-buffer
9865 that the selected frame is using. */
9866 mini_window = FRAME_MINIBUF_WINDOW (sf);
9867 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9868
9869 /* A null message buffer means that the frame hasn't really been
9870 initialized yet. Error messages get reported properly by
9871 cmd_error, so this must be just an informative message; toss
9872 it. */
9873 if (FRAME_MESSAGE_BUF (f))
9874 {
9875 if (m)
9876 {
9877 ptrdiff_t len;
9878
9879 len = doprnt (FRAME_MESSAGE_BUF (f),
9880 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
9881
9882 message2 (FRAME_MESSAGE_BUF (f), len, 1);
9883 }
9884 else
9885 message1 (0);
9886
9887 /* Print should start at the beginning of the message
9888 buffer next time. */
9889 message_buf_print = 0;
9890 }
9891 }
9892 }
9893
9894 void
9895 message (const char *m, ...)
9896 {
9897 va_list ap;
9898 va_start (ap, m);
9899 vmessage (m, ap);
9900 va_end (ap);
9901 }
9902
9903
9904 #if 0
9905 /* The non-logging version of message. */
9906
9907 void
9908 message_nolog (const char *m, ...)
9909 {
9910 Lisp_Object old_log_max;
9911 va_list ap;
9912 va_start (ap, m);
9913 old_log_max = Vmessage_log_max;
9914 Vmessage_log_max = Qnil;
9915 vmessage (m, ap);
9916 Vmessage_log_max = old_log_max;
9917 va_end (ap);
9918 }
9919 #endif
9920
9921
9922 /* Display the current message in the current mini-buffer. This is
9923 only called from error handlers in process.c, and is not time
9924 critical. */
9925
9926 void
9927 update_echo_area (void)
9928 {
9929 if (!NILP (echo_area_buffer[0]))
9930 {
9931 Lisp_Object string;
9932 string = Fcurrent_message ();
9933 message3 (string, SBYTES (string),
9934 !NILP (BVAR (current_buffer, enable_multibyte_characters)));
9935 }
9936 }
9937
9938
9939 /* Make sure echo area buffers in `echo_buffers' are live.
9940 If they aren't, make new ones. */
9941
9942 static void
9943 ensure_echo_area_buffers (void)
9944 {
9945 int i;
9946
9947 for (i = 0; i < 2; ++i)
9948 if (!BUFFERP (echo_buffer[i])
9949 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
9950 {
9951 char name[30];
9952 Lisp_Object old_buffer;
9953 int j;
9954
9955 old_buffer = echo_buffer[i];
9956 echo_buffer[i] = Fget_buffer_create
9957 (make_formatted_string (name, " *Echo Area %d*", i));
9958 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
9959 /* to force word wrap in echo area -
9960 it was decided to postpone this*/
9961 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9962
9963 for (j = 0; j < 2; ++j)
9964 if (EQ (old_buffer, echo_area_buffer[j]))
9965 echo_area_buffer[j] = echo_buffer[i];
9966 }
9967 }
9968
9969
9970 /* Call FN with args A1..A4 with either the current or last displayed
9971 echo_area_buffer as current buffer.
9972
9973 WHICH zero means use the current message buffer
9974 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9975 from echo_buffer[] and clear it.
9976
9977 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9978 suitable buffer from echo_buffer[] and clear it.
9979
9980 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
9981 that the current message becomes the last displayed one, make
9982 choose a suitable buffer for echo_area_buffer[0], and clear it.
9983
9984 Value is what FN returns. */
9985
9986 static int
9987 with_echo_area_buffer (struct window *w, int which,
9988 int (*fn) (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t),
9989 ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
9990 {
9991 Lisp_Object buffer;
9992 int this_one, the_other, clear_buffer_p, rc;
9993 ptrdiff_t count = SPECPDL_INDEX ();
9994
9995 /* If buffers aren't live, make new ones. */
9996 ensure_echo_area_buffers ();
9997
9998 clear_buffer_p = 0;
9999
10000 if (which == 0)
10001 this_one = 0, the_other = 1;
10002 else if (which > 0)
10003 this_one = 1, the_other = 0;
10004 else
10005 {
10006 this_one = 0, the_other = 1;
10007 clear_buffer_p = 1;
10008
10009 /* We need a fresh one in case the current echo buffer equals
10010 the one containing the last displayed echo area message. */
10011 if (!NILP (echo_area_buffer[this_one])
10012 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10013 echo_area_buffer[this_one] = Qnil;
10014 }
10015
10016 /* Choose a suitable buffer from echo_buffer[] is we don't
10017 have one. */
10018 if (NILP (echo_area_buffer[this_one]))
10019 {
10020 echo_area_buffer[this_one]
10021 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10022 ? echo_buffer[the_other]
10023 : echo_buffer[this_one]);
10024 clear_buffer_p = 1;
10025 }
10026
10027 buffer = echo_area_buffer[this_one];
10028
10029 /* Don't get confused by reusing the buffer used for echoing
10030 for a different purpose. */
10031 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10032 cancel_echoing ();
10033
10034 record_unwind_protect (unwind_with_echo_area_buffer,
10035 with_echo_area_buffer_unwind_data (w));
10036
10037 /* Make the echo area buffer current. Note that for display
10038 purposes, it is not necessary that the displayed window's buffer
10039 == current_buffer, except for text property lookup. So, let's
10040 only set that buffer temporarily here without doing a full
10041 Fset_window_buffer. We must also change w->pointm, though,
10042 because otherwise an assertions in unshow_buffer fails, and Emacs
10043 aborts. */
10044 set_buffer_internal_1 (XBUFFER (buffer));
10045 if (w)
10046 {
10047 wset_buffer (w, buffer);
10048 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10049 }
10050
10051 bset_undo_list (current_buffer, Qt);
10052 bset_read_only (current_buffer, Qnil);
10053 specbind (Qinhibit_read_only, Qt);
10054 specbind (Qinhibit_modification_hooks, Qt);
10055
10056 if (clear_buffer_p && Z > BEG)
10057 del_range (BEG, Z);
10058
10059 eassert (BEGV >= BEG);
10060 eassert (ZV <= Z && ZV >= BEGV);
10061
10062 rc = fn (a1, a2, a3, a4);
10063
10064 eassert (BEGV >= BEG);
10065 eassert (ZV <= Z && ZV >= BEGV);
10066
10067 unbind_to (count, Qnil);
10068 return rc;
10069 }
10070
10071
10072 /* Save state that should be preserved around the call to the function
10073 FN called in with_echo_area_buffer. */
10074
10075 static Lisp_Object
10076 with_echo_area_buffer_unwind_data (struct window *w)
10077 {
10078 int i = 0;
10079 Lisp_Object vector, tmp;
10080
10081 /* Reduce consing by keeping one vector in
10082 Vwith_echo_area_save_vector. */
10083 vector = Vwith_echo_area_save_vector;
10084 Vwith_echo_area_save_vector = Qnil;
10085
10086 if (NILP (vector))
10087 vector = Fmake_vector (make_number (7), Qnil);
10088
10089 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10090 ASET (vector, i, Vdeactivate_mark); ++i;
10091 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10092
10093 if (w)
10094 {
10095 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10096 ASET (vector, i, w->buffer); ++i;
10097 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
10098 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
10099 }
10100 else
10101 {
10102 int end = i + 4;
10103 for (; i < end; ++i)
10104 ASET (vector, i, Qnil);
10105 }
10106
10107 eassert (i == ASIZE (vector));
10108 return vector;
10109 }
10110
10111
10112 /* Restore global state from VECTOR which was created by
10113 with_echo_area_buffer_unwind_data. */
10114
10115 static Lisp_Object
10116 unwind_with_echo_area_buffer (Lisp_Object vector)
10117 {
10118 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10119 Vdeactivate_mark = AREF (vector, 1);
10120 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10121
10122 if (WINDOWP (AREF (vector, 3)))
10123 {
10124 struct window *w;
10125 Lisp_Object buffer, charpos, bytepos;
10126
10127 w = XWINDOW (AREF (vector, 3));
10128 buffer = AREF (vector, 4);
10129 charpos = AREF (vector, 5);
10130 bytepos = AREF (vector, 6);
10131
10132 wset_buffer (w, buffer);
10133 set_marker_both (w->pointm, buffer,
10134 XFASTINT (charpos), XFASTINT (bytepos));
10135 }
10136
10137 Vwith_echo_area_save_vector = vector;
10138 return Qnil;
10139 }
10140
10141
10142 /* Set up the echo area for use by print functions. MULTIBYTE_P
10143 non-zero means we will print multibyte. */
10144
10145 void
10146 setup_echo_area_for_printing (int multibyte_p)
10147 {
10148 /* If we can't find an echo area any more, exit. */
10149 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10150 Fkill_emacs (Qnil);
10151
10152 ensure_echo_area_buffers ();
10153
10154 if (!message_buf_print)
10155 {
10156 /* A message has been output since the last time we printed.
10157 Choose a fresh echo area buffer. */
10158 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10159 echo_area_buffer[0] = echo_buffer[1];
10160 else
10161 echo_area_buffer[0] = echo_buffer[0];
10162
10163 /* Switch to that buffer and clear it. */
10164 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10165 bset_truncate_lines (current_buffer, Qnil);
10166
10167 if (Z > BEG)
10168 {
10169 ptrdiff_t count = SPECPDL_INDEX ();
10170 specbind (Qinhibit_read_only, Qt);
10171 /* Note that undo recording is always disabled. */
10172 del_range (BEG, Z);
10173 unbind_to (count, Qnil);
10174 }
10175 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10176
10177 /* Set up the buffer for the multibyteness we need. */
10178 if (multibyte_p
10179 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10180 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10181
10182 /* Raise the frame containing the echo area. */
10183 if (minibuffer_auto_raise)
10184 {
10185 struct frame *sf = SELECTED_FRAME ();
10186 Lisp_Object mini_window;
10187 mini_window = FRAME_MINIBUF_WINDOW (sf);
10188 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10189 }
10190
10191 message_log_maybe_newline ();
10192 message_buf_print = 1;
10193 }
10194 else
10195 {
10196 if (NILP (echo_area_buffer[0]))
10197 {
10198 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10199 echo_area_buffer[0] = echo_buffer[1];
10200 else
10201 echo_area_buffer[0] = echo_buffer[0];
10202 }
10203
10204 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10205 {
10206 /* Someone switched buffers between print requests. */
10207 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10208 bset_truncate_lines (current_buffer, Qnil);
10209 }
10210 }
10211 }
10212
10213
10214 /* Display an echo area message in window W. Value is non-zero if W's
10215 height is changed. If display_last_displayed_message_p is
10216 non-zero, display the message that was last displayed, otherwise
10217 display the current message. */
10218
10219 static int
10220 display_echo_area (struct window *w)
10221 {
10222 int i, no_message_p, window_height_changed_p;
10223
10224 /* Temporarily disable garbage collections while displaying the echo
10225 area. This is done because a GC can print a message itself.
10226 That message would modify the echo area buffer's contents while a
10227 redisplay of the buffer is going on, and seriously confuse
10228 redisplay. */
10229 ptrdiff_t count = inhibit_garbage_collection ();
10230
10231 /* If there is no message, we must call display_echo_area_1
10232 nevertheless because it resizes the window. But we will have to
10233 reset the echo_area_buffer in question to nil at the end because
10234 with_echo_area_buffer will sets it to an empty buffer. */
10235 i = display_last_displayed_message_p ? 1 : 0;
10236 no_message_p = NILP (echo_area_buffer[i]);
10237
10238 window_height_changed_p
10239 = with_echo_area_buffer (w, display_last_displayed_message_p,
10240 display_echo_area_1,
10241 (intptr_t) w, Qnil, 0, 0);
10242
10243 if (no_message_p)
10244 echo_area_buffer[i] = Qnil;
10245
10246 unbind_to (count, Qnil);
10247 return window_height_changed_p;
10248 }
10249
10250
10251 /* Helper for display_echo_area. Display the current buffer which
10252 contains the current echo area message in window W, a mini-window,
10253 a pointer to which is passed in A1. A2..A4 are currently not used.
10254 Change the height of W so that all of the message is displayed.
10255 Value is non-zero if height of W was changed. */
10256
10257 static int
10258 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10259 {
10260 intptr_t i1 = a1;
10261 struct window *w = (struct window *) i1;
10262 Lisp_Object window;
10263 struct text_pos start;
10264 int window_height_changed_p = 0;
10265
10266 /* Do this before displaying, so that we have a large enough glyph
10267 matrix for the display. If we can't get enough space for the
10268 whole text, display the last N lines. That works by setting w->start. */
10269 window_height_changed_p = resize_mini_window (w, 0);
10270
10271 /* Use the starting position chosen by resize_mini_window. */
10272 SET_TEXT_POS_FROM_MARKER (start, w->start);
10273
10274 /* Display. */
10275 clear_glyph_matrix (w->desired_matrix);
10276 XSETWINDOW (window, w);
10277 try_window (window, start, 0);
10278
10279 return window_height_changed_p;
10280 }
10281
10282
10283 /* Resize the echo area window to exactly the size needed for the
10284 currently displayed message, if there is one. If a mini-buffer
10285 is active, don't shrink it. */
10286
10287 void
10288 resize_echo_area_exactly (void)
10289 {
10290 if (BUFFERP (echo_area_buffer[0])
10291 && WINDOWP (echo_area_window))
10292 {
10293 struct window *w = XWINDOW (echo_area_window);
10294 int resized_p;
10295 Lisp_Object resize_exactly;
10296
10297 if (minibuf_level == 0)
10298 resize_exactly = Qt;
10299 else
10300 resize_exactly = Qnil;
10301
10302 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10303 (intptr_t) w, resize_exactly,
10304 0, 0);
10305 if (resized_p)
10306 {
10307 ++windows_or_buffers_changed;
10308 ++update_mode_lines;
10309 redisplay_internal ();
10310 }
10311 }
10312 }
10313
10314
10315 /* Callback function for with_echo_area_buffer, when used from
10316 resize_echo_area_exactly. A1 contains a pointer to the window to
10317 resize, EXACTLY non-nil means resize the mini-window exactly to the
10318 size of the text displayed. A3 and A4 are not used. Value is what
10319 resize_mini_window returns. */
10320
10321 static int
10322 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly, ptrdiff_t a3, ptrdiff_t a4)
10323 {
10324 intptr_t i1 = a1;
10325 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10326 }
10327
10328
10329 /* Resize mini-window W to fit the size of its contents. EXACT_P
10330 means size the window exactly to the size needed. Otherwise, it's
10331 only enlarged until W's buffer is empty.
10332
10333 Set W->start to the right place to begin display. If the whole
10334 contents fit, start at the beginning. Otherwise, start so as
10335 to make the end of the contents appear. This is particularly
10336 important for y-or-n-p, but seems desirable generally.
10337
10338 Value is non-zero if the window height has been changed. */
10339
10340 int
10341 resize_mini_window (struct window *w, int exact_p)
10342 {
10343 struct frame *f = XFRAME (w->frame);
10344 int window_height_changed_p = 0;
10345
10346 eassert (MINI_WINDOW_P (w));
10347
10348 /* By default, start display at the beginning. */
10349 set_marker_both (w->start, w->buffer,
10350 BUF_BEGV (XBUFFER (w->buffer)),
10351 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
10352
10353 /* Don't resize windows while redisplaying a window; it would
10354 confuse redisplay functions when the size of the window they are
10355 displaying changes from under them. Such a resizing can happen,
10356 for instance, when which-func prints a long message while
10357 we are running fontification-functions. We're running these
10358 functions with safe_call which binds inhibit-redisplay to t. */
10359 if (!NILP (Vinhibit_redisplay))
10360 return 0;
10361
10362 /* Nil means don't try to resize. */
10363 if (NILP (Vresize_mini_windows)
10364 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10365 return 0;
10366
10367 if (!FRAME_MINIBUF_ONLY_P (f))
10368 {
10369 struct it it;
10370 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10371 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10372 int height;
10373 EMACS_INT max_height;
10374 int unit = FRAME_LINE_HEIGHT (f);
10375 struct text_pos start;
10376 struct buffer *old_current_buffer = NULL;
10377
10378 if (current_buffer != XBUFFER (w->buffer))
10379 {
10380 old_current_buffer = current_buffer;
10381 set_buffer_internal (XBUFFER (w->buffer));
10382 }
10383
10384 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10385
10386 /* Compute the max. number of lines specified by the user. */
10387 if (FLOATP (Vmax_mini_window_height))
10388 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10389 else if (INTEGERP (Vmax_mini_window_height))
10390 max_height = XINT (Vmax_mini_window_height);
10391 else
10392 max_height = total_height / 4;
10393
10394 /* Correct that max. height if it's bogus. */
10395 max_height = max (1, max_height);
10396 max_height = min (total_height, max_height);
10397
10398 /* Find out the height of the text in the window. */
10399 if (it.line_wrap == TRUNCATE)
10400 height = 1;
10401 else
10402 {
10403 last_height = 0;
10404 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10405 if (it.max_ascent == 0 && it.max_descent == 0)
10406 height = it.current_y + last_height;
10407 else
10408 height = it.current_y + it.max_ascent + it.max_descent;
10409 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10410 height = (height + unit - 1) / unit;
10411 }
10412
10413 /* Compute a suitable window start. */
10414 if (height > max_height)
10415 {
10416 height = max_height;
10417 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10418 move_it_vertically_backward (&it, (height - 1) * unit);
10419 start = it.current.pos;
10420 }
10421 else
10422 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10423 SET_MARKER_FROM_TEXT_POS (w->start, start);
10424
10425 if (EQ (Vresize_mini_windows, Qgrow_only))
10426 {
10427 /* Let it grow only, until we display an empty message, in which
10428 case the window shrinks again. */
10429 if (height > WINDOW_TOTAL_LINES (w))
10430 {
10431 int old_height = WINDOW_TOTAL_LINES (w);
10432 freeze_window_starts (f, 1);
10433 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10434 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10435 }
10436 else if (height < WINDOW_TOTAL_LINES (w)
10437 && (exact_p || BEGV == ZV))
10438 {
10439 int old_height = WINDOW_TOTAL_LINES (w);
10440 freeze_window_starts (f, 0);
10441 shrink_mini_window (w);
10442 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10443 }
10444 }
10445 else
10446 {
10447 /* Always resize to exact size needed. */
10448 if (height > WINDOW_TOTAL_LINES (w))
10449 {
10450 int old_height = WINDOW_TOTAL_LINES (w);
10451 freeze_window_starts (f, 1);
10452 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10453 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10454 }
10455 else if (height < WINDOW_TOTAL_LINES (w))
10456 {
10457 int old_height = WINDOW_TOTAL_LINES (w);
10458 freeze_window_starts (f, 0);
10459 shrink_mini_window (w);
10460
10461 if (height)
10462 {
10463 freeze_window_starts (f, 1);
10464 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10465 }
10466
10467 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10468 }
10469 }
10470
10471 if (old_current_buffer)
10472 set_buffer_internal (old_current_buffer);
10473 }
10474
10475 return window_height_changed_p;
10476 }
10477
10478
10479 /* Value is the current message, a string, or nil if there is no
10480 current message. */
10481
10482 Lisp_Object
10483 current_message (void)
10484 {
10485 Lisp_Object msg;
10486
10487 if (!BUFFERP (echo_area_buffer[0]))
10488 msg = Qnil;
10489 else
10490 {
10491 with_echo_area_buffer (0, 0, current_message_1,
10492 (intptr_t) &msg, Qnil, 0, 0);
10493 if (NILP (msg))
10494 echo_area_buffer[0] = Qnil;
10495 }
10496
10497 return msg;
10498 }
10499
10500
10501 static int
10502 current_message_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10503 {
10504 intptr_t i1 = a1;
10505 Lisp_Object *msg = (Lisp_Object *) i1;
10506
10507 if (Z > BEG)
10508 *msg = make_buffer_string (BEG, Z, 1);
10509 else
10510 *msg = Qnil;
10511 return 0;
10512 }
10513
10514
10515 /* Push the current message on Vmessage_stack for later restoration
10516 by restore_message. Value is non-zero if the current message isn't
10517 empty. This is a relatively infrequent operation, so it's not
10518 worth optimizing. */
10519
10520 bool
10521 push_message (void)
10522 {
10523 Lisp_Object msg = current_message ();
10524 Vmessage_stack = Fcons (msg, Vmessage_stack);
10525 return STRINGP (msg);
10526 }
10527
10528
10529 /* Restore message display from the top of Vmessage_stack. */
10530
10531 void
10532 restore_message (void)
10533 {
10534 Lisp_Object msg;
10535
10536 eassert (CONSP (Vmessage_stack));
10537 msg = XCAR (Vmessage_stack);
10538 if (STRINGP (msg))
10539 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
10540 else
10541 message3_nolog (msg, 0, 0);
10542 }
10543
10544
10545 /* Handler for record_unwind_protect calling pop_message. */
10546
10547 Lisp_Object
10548 pop_message_unwind (Lisp_Object dummy)
10549 {
10550 pop_message ();
10551 return Qnil;
10552 }
10553
10554 /* Pop the top-most entry off Vmessage_stack. */
10555
10556 static void
10557 pop_message (void)
10558 {
10559 eassert (CONSP (Vmessage_stack));
10560 Vmessage_stack = XCDR (Vmessage_stack);
10561 }
10562
10563
10564 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10565 exits. If the stack is not empty, we have a missing pop_message
10566 somewhere. */
10567
10568 void
10569 check_message_stack (void)
10570 {
10571 if (!NILP (Vmessage_stack))
10572 emacs_abort ();
10573 }
10574
10575
10576 /* Truncate to NCHARS what will be displayed in the echo area the next
10577 time we display it---but don't redisplay it now. */
10578
10579 void
10580 truncate_echo_area (ptrdiff_t nchars)
10581 {
10582 if (nchars == 0)
10583 echo_area_buffer[0] = Qnil;
10584 /* A null message buffer means that the frame hasn't really been
10585 initialized yet. Error messages get reported properly by
10586 cmd_error, so this must be just an informative message; toss it. */
10587 else if (!noninteractive
10588 && INTERACTIVE
10589 && !NILP (echo_area_buffer[0]))
10590 {
10591 struct frame *sf = SELECTED_FRAME ();
10592 if (FRAME_MESSAGE_BUF (sf))
10593 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
10594 }
10595 }
10596
10597
10598 /* Helper function for truncate_echo_area. Truncate the current
10599 message to at most NCHARS characters. */
10600
10601 static int
10602 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10603 {
10604 if (BEG + nchars < Z)
10605 del_range (BEG + nchars, Z);
10606 if (Z == BEG)
10607 echo_area_buffer[0] = Qnil;
10608 return 0;
10609 }
10610
10611 /* Set the current message to a substring of S or STRING.
10612
10613 If STRING is a Lisp string, set the message to the first NBYTES
10614 bytes from STRING. NBYTES zero means use the whole string. If
10615 STRING is multibyte, the message will be displayed multibyte.
10616
10617 If S is not null, set the message to the first LEN bytes of S. LEN
10618 zero means use the whole string. MULTIBYTE_P non-zero means S is
10619 multibyte. Display the message multibyte in that case.
10620
10621 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
10622 to t before calling set_message_1 (which calls insert).
10623 */
10624
10625 static void
10626 set_message (const char *s, Lisp_Object string,
10627 ptrdiff_t nbytes, int multibyte_p)
10628 {
10629 message_enable_multibyte
10630 = ((s && multibyte_p)
10631 || (STRINGP (string) && STRING_MULTIBYTE (string)));
10632
10633 with_echo_area_buffer (0, -1, set_message_1,
10634 (intptr_t) s, string, nbytes, multibyte_p);
10635 message_buf_print = 0;
10636 help_echo_showing_p = 0;
10637
10638 if (STRINGP (Vdebug_on_message)
10639 && STRINGP (string)
10640 && fast_string_match (Vdebug_on_message, string) >= 0)
10641 call_debugger (list2 (Qerror, string));
10642 }
10643
10644
10645 /* Helper function for set_message. Arguments have the same meaning
10646 as there, with A1 corresponding to S and A2 corresponding to STRING
10647 This function is called with the echo area buffer being
10648 current. */
10649
10650 static int
10651 set_message_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t nbytes, ptrdiff_t multibyte_p)
10652 {
10653 intptr_t i1 = a1;
10654 const char *s = (const char *) i1;
10655 const unsigned char *msg = (const unsigned char *) s;
10656 Lisp_Object string = a2;
10657
10658 /* Change multibyteness of the echo buffer appropriately. */
10659 if (message_enable_multibyte
10660 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10661 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10662
10663 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10664 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10665 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10666
10667 /* Insert new message at BEG. */
10668 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10669
10670 if (STRINGP (string))
10671 {
10672 ptrdiff_t nchars;
10673
10674 if (nbytes == 0)
10675 nbytes = SBYTES (string);
10676 nchars = string_byte_to_char (string, nbytes);
10677
10678 /* This function takes care of single/multibyte conversion. We
10679 just have to ensure that the echo area buffer has the right
10680 setting of enable_multibyte_characters. */
10681 insert_from_string (string, 0, 0, nchars, nbytes, 1);
10682 }
10683 else if (s)
10684 {
10685 if (nbytes == 0)
10686 nbytes = strlen (s);
10687
10688 if (multibyte_p && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10689 {
10690 /* Convert from multi-byte to single-byte. */
10691 ptrdiff_t i;
10692 int c, n;
10693 char work[1];
10694
10695 /* Convert a multibyte string to single-byte. */
10696 for (i = 0; i < nbytes; i += n)
10697 {
10698 c = string_char_and_length (msg + i, &n);
10699 work[0] = (ASCII_CHAR_P (c)
10700 ? c
10701 : multibyte_char_to_unibyte (c));
10702 insert_1_both (work, 1, 1, 1, 0, 0);
10703 }
10704 }
10705 else if (!multibyte_p
10706 && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10707 {
10708 /* Convert from single-byte to multi-byte. */
10709 ptrdiff_t i;
10710 int c, n;
10711 unsigned char str[MAX_MULTIBYTE_LENGTH];
10712
10713 /* Convert a single-byte string to multibyte. */
10714 for (i = 0; i < nbytes; i++)
10715 {
10716 c = msg[i];
10717 MAKE_CHAR_MULTIBYTE (c);
10718 n = CHAR_STRING (c, str);
10719 insert_1_both ((char *) str, 1, n, 1, 0, 0);
10720 }
10721 }
10722 else
10723 insert_1 (s, nbytes, 1, 0, 0);
10724 }
10725
10726 return 0;
10727 }
10728
10729
10730 /* Clear messages. CURRENT_P non-zero means clear the current
10731 message. LAST_DISPLAYED_P non-zero means clear the message
10732 last displayed. */
10733
10734 void
10735 clear_message (int current_p, int last_displayed_p)
10736 {
10737 if (current_p)
10738 {
10739 echo_area_buffer[0] = Qnil;
10740 message_cleared_p = 1;
10741 }
10742
10743 if (last_displayed_p)
10744 echo_area_buffer[1] = Qnil;
10745
10746 message_buf_print = 0;
10747 }
10748
10749 /* Clear garbaged frames.
10750
10751 This function is used where the old redisplay called
10752 redraw_garbaged_frames which in turn called redraw_frame which in
10753 turn called clear_frame. The call to clear_frame was a source of
10754 flickering. I believe a clear_frame is not necessary. It should
10755 suffice in the new redisplay to invalidate all current matrices,
10756 and ensure a complete redisplay of all windows. */
10757
10758 static void
10759 clear_garbaged_frames (void)
10760 {
10761 if (frame_garbaged)
10762 {
10763 Lisp_Object tail, frame;
10764 int changed_count = 0;
10765
10766 FOR_EACH_FRAME (tail, frame)
10767 {
10768 struct frame *f = XFRAME (frame);
10769
10770 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10771 {
10772 if (f->resized_p)
10773 {
10774 Fredraw_frame (frame);
10775 f->force_flush_display_p = 1;
10776 }
10777 clear_current_matrices (f);
10778 changed_count++;
10779 f->garbaged = 0;
10780 f->resized_p = 0;
10781 }
10782 }
10783
10784 frame_garbaged = 0;
10785 if (changed_count)
10786 ++windows_or_buffers_changed;
10787 }
10788 }
10789
10790
10791 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10792 is non-zero update selected_frame. Value is non-zero if the
10793 mini-windows height has been changed. */
10794
10795 static int
10796 echo_area_display (int update_frame_p)
10797 {
10798 Lisp_Object mini_window;
10799 struct window *w;
10800 struct frame *f;
10801 int window_height_changed_p = 0;
10802 struct frame *sf = SELECTED_FRAME ();
10803
10804 mini_window = FRAME_MINIBUF_WINDOW (sf);
10805 w = XWINDOW (mini_window);
10806 f = XFRAME (WINDOW_FRAME (w));
10807
10808 /* Don't display if frame is invisible or not yet initialized. */
10809 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10810 return 0;
10811
10812 #ifdef HAVE_WINDOW_SYSTEM
10813 /* When Emacs starts, selected_frame may be the initial terminal
10814 frame. If we let this through, a message would be displayed on
10815 the terminal. */
10816 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10817 return 0;
10818 #endif /* HAVE_WINDOW_SYSTEM */
10819
10820 /* Redraw garbaged frames. */
10821 if (frame_garbaged)
10822 clear_garbaged_frames ();
10823
10824 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10825 {
10826 echo_area_window = mini_window;
10827 window_height_changed_p = display_echo_area (w);
10828 w->must_be_updated_p = 1;
10829
10830 /* Update the display, unless called from redisplay_internal.
10831 Also don't update the screen during redisplay itself. The
10832 update will happen at the end of redisplay, and an update
10833 here could cause confusion. */
10834 if (update_frame_p && !redisplaying_p)
10835 {
10836 int n = 0;
10837
10838 /* If the display update has been interrupted by pending
10839 input, update mode lines in the frame. Due to the
10840 pending input, it might have been that redisplay hasn't
10841 been called, so that mode lines above the echo area are
10842 garbaged. This looks odd, so we prevent it here. */
10843 if (!display_completed)
10844 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10845
10846 if (window_height_changed_p
10847 /* Don't do this if Emacs is shutting down. Redisplay
10848 needs to run hooks. */
10849 && !NILP (Vrun_hooks))
10850 {
10851 /* Must update other windows. Likewise as in other
10852 cases, don't let this update be interrupted by
10853 pending input. */
10854 ptrdiff_t count = SPECPDL_INDEX ();
10855 specbind (Qredisplay_dont_pause, Qt);
10856 windows_or_buffers_changed = 1;
10857 redisplay_internal ();
10858 unbind_to (count, Qnil);
10859 }
10860 else if (FRAME_WINDOW_P (f) && n == 0)
10861 {
10862 /* Window configuration is the same as before.
10863 Can do with a display update of the echo area,
10864 unless we displayed some mode lines. */
10865 update_single_window (w, 1);
10866 FRAME_RIF (f)->flush_display (f);
10867 }
10868 else
10869 update_frame (f, 1, 1);
10870
10871 /* If cursor is in the echo area, make sure that the next
10872 redisplay displays the minibuffer, so that the cursor will
10873 be replaced with what the minibuffer wants. */
10874 if (cursor_in_echo_area)
10875 ++windows_or_buffers_changed;
10876 }
10877 }
10878 else if (!EQ (mini_window, selected_window))
10879 windows_or_buffers_changed++;
10880
10881 /* Last displayed message is now the current message. */
10882 echo_area_buffer[1] = echo_area_buffer[0];
10883 /* Inform read_char that we're not echoing. */
10884 echo_message_buffer = Qnil;
10885
10886 /* Prevent redisplay optimization in redisplay_internal by resetting
10887 this_line_start_pos. This is done because the mini-buffer now
10888 displays the message instead of its buffer text. */
10889 if (EQ (mini_window, selected_window))
10890 CHARPOS (this_line_start_pos) = 0;
10891
10892 return window_height_changed_p;
10893 }
10894
10895
10896 \f
10897 /***********************************************************************
10898 Mode Lines and Frame Titles
10899 ***********************************************************************/
10900
10901 /* A buffer for constructing non-propertized mode-line strings and
10902 frame titles in it; allocated from the heap in init_xdisp and
10903 resized as needed in store_mode_line_noprop_char. */
10904
10905 static char *mode_line_noprop_buf;
10906
10907 /* The buffer's end, and a current output position in it. */
10908
10909 static char *mode_line_noprop_buf_end;
10910 static char *mode_line_noprop_ptr;
10911
10912 #define MODE_LINE_NOPROP_LEN(start) \
10913 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10914
10915 static enum {
10916 MODE_LINE_DISPLAY = 0,
10917 MODE_LINE_TITLE,
10918 MODE_LINE_NOPROP,
10919 MODE_LINE_STRING
10920 } mode_line_target;
10921
10922 /* Alist that caches the results of :propertize.
10923 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10924 static Lisp_Object mode_line_proptrans_alist;
10925
10926 /* List of strings making up the mode-line. */
10927 static Lisp_Object mode_line_string_list;
10928
10929 /* Base face property when building propertized mode line string. */
10930 static Lisp_Object mode_line_string_face;
10931 static Lisp_Object mode_line_string_face_prop;
10932
10933
10934 /* Unwind data for mode line strings */
10935
10936 static Lisp_Object Vmode_line_unwind_vector;
10937
10938 static Lisp_Object
10939 format_mode_line_unwind_data (struct frame *target_frame,
10940 struct buffer *obuf,
10941 Lisp_Object owin,
10942 int save_proptrans)
10943 {
10944 Lisp_Object vector, tmp;
10945
10946 /* Reduce consing by keeping one vector in
10947 Vwith_echo_area_save_vector. */
10948 vector = Vmode_line_unwind_vector;
10949 Vmode_line_unwind_vector = Qnil;
10950
10951 if (NILP (vector))
10952 vector = Fmake_vector (make_number (10), Qnil);
10953
10954 ASET (vector, 0, make_number (mode_line_target));
10955 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10956 ASET (vector, 2, mode_line_string_list);
10957 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10958 ASET (vector, 4, mode_line_string_face);
10959 ASET (vector, 5, mode_line_string_face_prop);
10960
10961 if (obuf)
10962 XSETBUFFER (tmp, obuf);
10963 else
10964 tmp = Qnil;
10965 ASET (vector, 6, tmp);
10966 ASET (vector, 7, owin);
10967 if (target_frame)
10968 {
10969 /* Similarly to `with-selected-window', if the operation selects
10970 a window on another frame, we must restore that frame's
10971 selected window, and (for a tty) the top-frame. */
10972 ASET (vector, 8, target_frame->selected_window);
10973 if (FRAME_TERMCAP_P (target_frame))
10974 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
10975 }
10976
10977 return vector;
10978 }
10979
10980 static Lisp_Object
10981 unwind_format_mode_line (Lisp_Object vector)
10982 {
10983 Lisp_Object old_window = AREF (vector, 7);
10984 Lisp_Object target_frame_window = AREF (vector, 8);
10985 Lisp_Object old_top_frame = AREF (vector, 9);
10986
10987 mode_line_target = XINT (AREF (vector, 0));
10988 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10989 mode_line_string_list = AREF (vector, 2);
10990 if (! EQ (AREF (vector, 3), Qt))
10991 mode_line_proptrans_alist = AREF (vector, 3);
10992 mode_line_string_face = AREF (vector, 4);
10993 mode_line_string_face_prop = AREF (vector, 5);
10994
10995 /* Select window before buffer, since it may change the buffer. */
10996 if (!NILP (old_window))
10997 {
10998 /* If the operation that we are unwinding had selected a window
10999 on a different frame, reset its frame-selected-window. For a
11000 text terminal, reset its top-frame if necessary. */
11001 if (!NILP (target_frame_window))
11002 {
11003 Lisp_Object frame
11004 = WINDOW_FRAME (XWINDOW (target_frame_window));
11005
11006 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11007 Fselect_window (target_frame_window, Qt);
11008
11009 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11010 Fselect_frame (old_top_frame, Qt);
11011 }
11012
11013 Fselect_window (old_window, Qt);
11014 }
11015
11016 if (!NILP (AREF (vector, 6)))
11017 {
11018 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11019 ASET (vector, 6, Qnil);
11020 }
11021
11022 Vmode_line_unwind_vector = vector;
11023 return Qnil;
11024 }
11025
11026
11027 /* Store a single character C for the frame title in mode_line_noprop_buf.
11028 Re-allocate mode_line_noprop_buf if necessary. */
11029
11030 static void
11031 store_mode_line_noprop_char (char c)
11032 {
11033 /* If output position has reached the end of the allocated buffer,
11034 increase the buffer's size. */
11035 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11036 {
11037 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11038 ptrdiff_t size = len;
11039 mode_line_noprop_buf =
11040 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11041 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11042 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11043 }
11044
11045 *mode_line_noprop_ptr++ = c;
11046 }
11047
11048
11049 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11050 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11051 characters that yield more columns than PRECISION; PRECISION <= 0
11052 means copy the whole string. Pad with spaces until FIELD_WIDTH
11053 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11054 pad. Called from display_mode_element when it is used to build a
11055 frame title. */
11056
11057 static int
11058 store_mode_line_noprop (const char *string, int field_width, int precision)
11059 {
11060 const unsigned char *str = (const unsigned char *) string;
11061 int n = 0;
11062 ptrdiff_t dummy, nbytes;
11063
11064 /* Copy at most PRECISION chars from STR. */
11065 nbytes = strlen (string);
11066 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11067 while (nbytes--)
11068 store_mode_line_noprop_char (*str++);
11069
11070 /* Fill up with spaces until FIELD_WIDTH reached. */
11071 while (field_width > 0
11072 && n < field_width)
11073 {
11074 store_mode_line_noprop_char (' ');
11075 ++n;
11076 }
11077
11078 return n;
11079 }
11080
11081 /***********************************************************************
11082 Frame Titles
11083 ***********************************************************************/
11084
11085 #ifdef HAVE_WINDOW_SYSTEM
11086
11087 /* Set the title of FRAME, if it has changed. The title format is
11088 Vicon_title_format if FRAME is iconified, otherwise it is
11089 frame_title_format. */
11090
11091 static void
11092 x_consider_frame_title (Lisp_Object frame)
11093 {
11094 struct frame *f = XFRAME (frame);
11095
11096 if (FRAME_WINDOW_P (f)
11097 || FRAME_MINIBUF_ONLY_P (f)
11098 || f->explicit_name)
11099 {
11100 /* Do we have more than one visible frame on this X display? */
11101 Lisp_Object tail;
11102 Lisp_Object fmt;
11103 ptrdiff_t title_start;
11104 char *title;
11105 ptrdiff_t len;
11106 struct it it;
11107 ptrdiff_t count = SPECPDL_INDEX ();
11108
11109 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
11110 {
11111 Lisp_Object other_frame = XCAR (tail);
11112 struct frame *tf = XFRAME (other_frame);
11113
11114 if (tf != f
11115 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11116 && !FRAME_MINIBUF_ONLY_P (tf)
11117 && !EQ (other_frame, tip_frame)
11118 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11119 break;
11120 }
11121
11122 /* Set global variable indicating that multiple frames exist. */
11123 multiple_frames = CONSP (tail);
11124
11125 /* Switch to the buffer of selected window of the frame. Set up
11126 mode_line_target so that display_mode_element will output into
11127 mode_line_noprop_buf; then display the title. */
11128 record_unwind_protect (unwind_format_mode_line,
11129 format_mode_line_unwind_data
11130 (f, current_buffer, selected_window, 0));
11131
11132 Fselect_window (f->selected_window, Qt);
11133 set_buffer_internal_1
11134 (XBUFFER (XWINDOW (f->selected_window)->buffer));
11135 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11136
11137 mode_line_target = MODE_LINE_TITLE;
11138 title_start = MODE_LINE_NOPROP_LEN (0);
11139 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11140 NULL, DEFAULT_FACE_ID);
11141 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11142 len = MODE_LINE_NOPROP_LEN (title_start);
11143 title = mode_line_noprop_buf + title_start;
11144 unbind_to (count, Qnil);
11145
11146 /* Set the title only if it's changed. This avoids consing in
11147 the common case where it hasn't. (If it turns out that we've
11148 already wasted too much time by walking through the list with
11149 display_mode_element, then we might need to optimize at a
11150 higher level than this.) */
11151 if (! STRINGP (f->name)
11152 || SBYTES (f->name) != len
11153 || memcmp (title, SDATA (f->name), len) != 0)
11154 x_implicitly_set_name (f, make_string (title, len), Qnil);
11155 }
11156 }
11157
11158 #endif /* not HAVE_WINDOW_SYSTEM */
11159
11160 \f
11161 /***********************************************************************
11162 Menu Bars
11163 ***********************************************************************/
11164
11165
11166 /* Prepare for redisplay by updating menu-bar item lists when
11167 appropriate. This can call eval. */
11168
11169 void
11170 prepare_menu_bars (void)
11171 {
11172 int all_windows;
11173 struct gcpro gcpro1, gcpro2;
11174 struct frame *f;
11175 Lisp_Object tooltip_frame;
11176
11177 #ifdef HAVE_WINDOW_SYSTEM
11178 tooltip_frame = tip_frame;
11179 #else
11180 tooltip_frame = Qnil;
11181 #endif
11182
11183 /* Update all frame titles based on their buffer names, etc. We do
11184 this before the menu bars so that the buffer-menu will show the
11185 up-to-date frame titles. */
11186 #ifdef HAVE_WINDOW_SYSTEM
11187 if (windows_or_buffers_changed || update_mode_lines)
11188 {
11189 Lisp_Object tail, frame;
11190
11191 FOR_EACH_FRAME (tail, frame)
11192 {
11193 f = XFRAME (frame);
11194 if (!EQ (frame, tooltip_frame)
11195 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
11196 x_consider_frame_title (frame);
11197 }
11198 }
11199 #endif /* HAVE_WINDOW_SYSTEM */
11200
11201 /* Update the menu bar item lists, if appropriate. This has to be
11202 done before any actual redisplay or generation of display lines. */
11203 all_windows = (update_mode_lines
11204 || buffer_shared > 1
11205 || windows_or_buffers_changed);
11206 if (all_windows)
11207 {
11208 Lisp_Object tail, frame;
11209 ptrdiff_t count = SPECPDL_INDEX ();
11210 /* 1 means that update_menu_bar has run its hooks
11211 so any further calls to update_menu_bar shouldn't do so again. */
11212 int menu_bar_hooks_run = 0;
11213
11214 record_unwind_save_match_data ();
11215
11216 FOR_EACH_FRAME (tail, frame)
11217 {
11218 f = XFRAME (frame);
11219
11220 /* Ignore tooltip frame. */
11221 if (EQ (frame, tooltip_frame))
11222 continue;
11223
11224 /* If a window on this frame changed size, report that to
11225 the user and clear the size-change flag. */
11226 if (FRAME_WINDOW_SIZES_CHANGED (f))
11227 {
11228 Lisp_Object functions;
11229
11230 /* Clear flag first in case we get an error below. */
11231 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11232 functions = Vwindow_size_change_functions;
11233 GCPRO2 (tail, functions);
11234
11235 while (CONSP (functions))
11236 {
11237 if (!EQ (XCAR (functions), Qt))
11238 call1 (XCAR (functions), frame);
11239 functions = XCDR (functions);
11240 }
11241 UNGCPRO;
11242 }
11243
11244 GCPRO1 (tail);
11245 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11246 #ifdef HAVE_WINDOW_SYSTEM
11247 update_tool_bar (f, 0);
11248 #endif
11249 #ifdef HAVE_NS
11250 if (windows_or_buffers_changed
11251 && FRAME_NS_P (f))
11252 ns_set_doc_edited
11253 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->buffer));
11254 #endif
11255 UNGCPRO;
11256 }
11257
11258 unbind_to (count, Qnil);
11259 }
11260 else
11261 {
11262 struct frame *sf = SELECTED_FRAME ();
11263 update_menu_bar (sf, 1, 0);
11264 #ifdef HAVE_WINDOW_SYSTEM
11265 update_tool_bar (sf, 1);
11266 #endif
11267 }
11268 }
11269
11270
11271 /* Update the menu bar item list for frame F. This has to be done
11272 before we start to fill in any display lines, because it can call
11273 eval.
11274
11275 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11276
11277 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11278 already ran the menu bar hooks for this redisplay, so there
11279 is no need to run them again. The return value is the
11280 updated value of this flag, to pass to the next call. */
11281
11282 static int
11283 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11284 {
11285 Lisp_Object window;
11286 register struct window *w;
11287
11288 /* If called recursively during a menu update, do nothing. This can
11289 happen when, for instance, an activate-menubar-hook causes a
11290 redisplay. */
11291 if (inhibit_menubar_update)
11292 return hooks_run;
11293
11294 window = FRAME_SELECTED_WINDOW (f);
11295 w = XWINDOW (window);
11296
11297 if (FRAME_WINDOW_P (f)
11298 ?
11299 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11300 || defined (HAVE_NS) || defined (USE_GTK)
11301 FRAME_EXTERNAL_MENU_BAR (f)
11302 #else
11303 FRAME_MENU_BAR_LINES (f) > 0
11304 #endif
11305 : FRAME_MENU_BAR_LINES (f) > 0)
11306 {
11307 /* If the user has switched buffers or windows, we need to
11308 recompute to reflect the new bindings. But we'll
11309 recompute when update_mode_lines is set too; that means
11310 that people can use force-mode-line-update to request
11311 that the menu bar be recomputed. The adverse effect on
11312 the rest of the redisplay algorithm is about the same as
11313 windows_or_buffers_changed anyway. */
11314 if (windows_or_buffers_changed
11315 /* This used to test w->update_mode_line, but we believe
11316 there is no need to recompute the menu in that case. */
11317 || update_mode_lines
11318 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11319 < BUF_MODIFF (XBUFFER (w->buffer)))
11320 != w->last_had_star)
11321 || ((!NILP (Vtransient_mark_mode)
11322 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11323 != !NILP (w->region_showing)))
11324 {
11325 struct buffer *prev = current_buffer;
11326 ptrdiff_t count = SPECPDL_INDEX ();
11327
11328 specbind (Qinhibit_menubar_update, Qt);
11329
11330 set_buffer_internal_1 (XBUFFER (w->buffer));
11331 if (save_match_data)
11332 record_unwind_save_match_data ();
11333 if (NILP (Voverriding_local_map_menu_flag))
11334 {
11335 specbind (Qoverriding_terminal_local_map, Qnil);
11336 specbind (Qoverriding_local_map, Qnil);
11337 }
11338
11339 if (!hooks_run)
11340 {
11341 /* Run the Lucid hook. */
11342 safe_run_hooks (Qactivate_menubar_hook);
11343
11344 /* If it has changed current-menubar from previous value,
11345 really recompute the menu-bar from the value. */
11346 if (! NILP (Vlucid_menu_bar_dirty_flag))
11347 call0 (Qrecompute_lucid_menubar);
11348
11349 safe_run_hooks (Qmenu_bar_update_hook);
11350
11351 hooks_run = 1;
11352 }
11353
11354 XSETFRAME (Vmenu_updating_frame, f);
11355 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11356
11357 /* Redisplay the menu bar in case we changed it. */
11358 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11359 || defined (HAVE_NS) || defined (USE_GTK)
11360 if (FRAME_WINDOW_P (f))
11361 {
11362 #if defined (HAVE_NS)
11363 /* All frames on Mac OS share the same menubar. So only
11364 the selected frame should be allowed to set it. */
11365 if (f == SELECTED_FRAME ())
11366 #endif
11367 set_frame_menubar (f, 0, 0);
11368 }
11369 else
11370 /* On a terminal screen, the menu bar is an ordinary screen
11371 line, and this makes it get updated. */
11372 w->update_mode_line = 1;
11373 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11374 /* In the non-toolkit version, the menu bar is an ordinary screen
11375 line, and this makes it get updated. */
11376 w->update_mode_line = 1;
11377 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11378
11379 unbind_to (count, Qnil);
11380 set_buffer_internal_1 (prev);
11381 }
11382 }
11383
11384 return hooks_run;
11385 }
11386
11387
11388 \f
11389 /***********************************************************************
11390 Output Cursor
11391 ***********************************************************************/
11392
11393 #ifdef HAVE_WINDOW_SYSTEM
11394
11395 /* EXPORT:
11396 Nominal cursor position -- where to draw output.
11397 HPOS and VPOS are window relative glyph matrix coordinates.
11398 X and Y are window relative pixel coordinates. */
11399
11400 struct cursor_pos output_cursor;
11401
11402
11403 /* EXPORT:
11404 Set the global variable output_cursor to CURSOR. All cursor
11405 positions are relative to updated_window. */
11406
11407 void
11408 set_output_cursor (struct cursor_pos *cursor)
11409 {
11410 output_cursor.hpos = cursor->hpos;
11411 output_cursor.vpos = cursor->vpos;
11412 output_cursor.x = cursor->x;
11413 output_cursor.y = cursor->y;
11414 }
11415
11416
11417 /* EXPORT for RIF:
11418 Set a nominal cursor position.
11419
11420 HPOS and VPOS are column/row positions in a window glyph matrix. X
11421 and Y are window text area relative pixel positions.
11422
11423 If this is done during an update, updated_window will contain the
11424 window that is being updated and the position is the future output
11425 cursor position for that window. If updated_window is null, use
11426 selected_window and display the cursor at the given position. */
11427
11428 void
11429 x_cursor_to (int vpos, int hpos, int y, int x)
11430 {
11431 struct window *w;
11432
11433 /* If updated_window is not set, work on selected_window. */
11434 if (updated_window)
11435 w = updated_window;
11436 else
11437 w = XWINDOW (selected_window);
11438
11439 /* Set the output cursor. */
11440 output_cursor.hpos = hpos;
11441 output_cursor.vpos = vpos;
11442 output_cursor.x = x;
11443 output_cursor.y = y;
11444
11445 /* If not called as part of an update, really display the cursor.
11446 This will also set the cursor position of W. */
11447 if (updated_window == NULL)
11448 {
11449 block_input ();
11450 display_and_set_cursor (w, 1, hpos, vpos, x, y);
11451 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11452 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
11453 unblock_input ();
11454 }
11455 }
11456
11457 #endif /* HAVE_WINDOW_SYSTEM */
11458
11459 \f
11460 /***********************************************************************
11461 Tool-bars
11462 ***********************************************************************/
11463
11464 #ifdef HAVE_WINDOW_SYSTEM
11465
11466 /* Where the mouse was last time we reported a mouse event. */
11467
11468 FRAME_PTR last_mouse_frame;
11469
11470 /* Tool-bar item index of the item on which a mouse button was pressed
11471 or -1. */
11472
11473 int last_tool_bar_item;
11474
11475
11476 static Lisp_Object
11477 update_tool_bar_unwind (Lisp_Object frame)
11478 {
11479 selected_frame = frame;
11480 return Qnil;
11481 }
11482
11483 /* Update the tool-bar item list for frame F. This has to be done
11484 before we start to fill in any display lines. Called from
11485 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11486 and restore it here. */
11487
11488 static void
11489 update_tool_bar (struct frame *f, int save_match_data)
11490 {
11491 #if defined (USE_GTK) || defined (HAVE_NS)
11492 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11493 #else
11494 int do_update = WINDOWP (f->tool_bar_window)
11495 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11496 #endif
11497
11498 if (do_update)
11499 {
11500 Lisp_Object window;
11501 struct window *w;
11502
11503 window = FRAME_SELECTED_WINDOW (f);
11504 w = XWINDOW (window);
11505
11506 /* If the user has switched buffers or windows, we need to
11507 recompute to reflect the new bindings. But we'll
11508 recompute when update_mode_lines is set too; that means
11509 that people can use force-mode-line-update to request
11510 that the menu bar be recomputed. The adverse effect on
11511 the rest of the redisplay algorithm is about the same as
11512 windows_or_buffers_changed anyway. */
11513 if (windows_or_buffers_changed
11514 || w->update_mode_line
11515 || update_mode_lines
11516 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11517 < BUF_MODIFF (XBUFFER (w->buffer)))
11518 != w->last_had_star)
11519 || ((!NILP (Vtransient_mark_mode)
11520 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11521 != !NILP (w->region_showing)))
11522 {
11523 struct buffer *prev = current_buffer;
11524 ptrdiff_t count = SPECPDL_INDEX ();
11525 Lisp_Object frame, new_tool_bar;
11526 int new_n_tool_bar;
11527 struct gcpro gcpro1;
11528
11529 /* Set current_buffer to the buffer of the selected
11530 window of the frame, so that we get the right local
11531 keymaps. */
11532 set_buffer_internal_1 (XBUFFER (w->buffer));
11533
11534 /* Save match data, if we must. */
11535 if (save_match_data)
11536 record_unwind_save_match_data ();
11537
11538 /* Make sure that we don't accidentally use bogus keymaps. */
11539 if (NILP (Voverriding_local_map_menu_flag))
11540 {
11541 specbind (Qoverriding_terminal_local_map, Qnil);
11542 specbind (Qoverriding_local_map, Qnil);
11543 }
11544
11545 GCPRO1 (new_tool_bar);
11546
11547 /* We must temporarily set the selected frame to this frame
11548 before calling tool_bar_items, because the calculation of
11549 the tool-bar keymap uses the selected frame (see
11550 `tool-bar-make-keymap' in tool-bar.el). */
11551 record_unwind_protect (update_tool_bar_unwind, selected_frame);
11552 XSETFRAME (frame, f);
11553 selected_frame = frame;
11554
11555 /* Build desired tool-bar items from keymaps. */
11556 new_tool_bar
11557 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11558 &new_n_tool_bar);
11559
11560 /* Redisplay the tool-bar if we changed it. */
11561 if (new_n_tool_bar != f->n_tool_bar_items
11562 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11563 {
11564 /* Redisplay that happens asynchronously due to an expose event
11565 may access f->tool_bar_items. Make sure we update both
11566 variables within BLOCK_INPUT so no such event interrupts. */
11567 block_input ();
11568 fset_tool_bar_items (f, new_tool_bar);
11569 f->n_tool_bar_items = new_n_tool_bar;
11570 w->update_mode_line = 1;
11571 unblock_input ();
11572 }
11573
11574 UNGCPRO;
11575
11576 unbind_to (count, Qnil);
11577 set_buffer_internal_1 (prev);
11578 }
11579 }
11580 }
11581
11582
11583 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11584 F's desired tool-bar contents. F->tool_bar_items must have
11585 been set up previously by calling prepare_menu_bars. */
11586
11587 static void
11588 build_desired_tool_bar_string (struct frame *f)
11589 {
11590 int i, size, size_needed;
11591 struct gcpro gcpro1, gcpro2, gcpro3;
11592 Lisp_Object image, plist, props;
11593
11594 image = plist = props = Qnil;
11595 GCPRO3 (image, plist, props);
11596
11597 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11598 Otherwise, make a new string. */
11599
11600 /* The size of the string we might be able to reuse. */
11601 size = (STRINGP (f->desired_tool_bar_string)
11602 ? SCHARS (f->desired_tool_bar_string)
11603 : 0);
11604
11605 /* We need one space in the string for each image. */
11606 size_needed = f->n_tool_bar_items;
11607
11608 /* Reuse f->desired_tool_bar_string, if possible. */
11609 if (size < size_needed || NILP (f->desired_tool_bar_string))
11610 fset_desired_tool_bar_string
11611 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11612 else
11613 {
11614 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11615 Fremove_text_properties (make_number (0), make_number (size),
11616 props, f->desired_tool_bar_string);
11617 }
11618
11619 /* Put a `display' property on the string for the images to display,
11620 put a `menu_item' property on tool-bar items with a value that
11621 is the index of the item in F's tool-bar item vector. */
11622 for (i = 0; i < f->n_tool_bar_items; ++i)
11623 {
11624 #define PROP(IDX) \
11625 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11626
11627 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11628 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11629 int hmargin, vmargin, relief, idx, end;
11630
11631 /* If image is a vector, choose the image according to the
11632 button state. */
11633 image = PROP (TOOL_BAR_ITEM_IMAGES);
11634 if (VECTORP (image))
11635 {
11636 if (enabled_p)
11637 idx = (selected_p
11638 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11639 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11640 else
11641 idx = (selected_p
11642 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11643 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11644
11645 eassert (ASIZE (image) >= idx);
11646 image = AREF (image, idx);
11647 }
11648 else
11649 idx = -1;
11650
11651 /* Ignore invalid image specifications. */
11652 if (!valid_image_p (image))
11653 continue;
11654
11655 /* Display the tool-bar button pressed, or depressed. */
11656 plist = Fcopy_sequence (XCDR (image));
11657
11658 /* Compute margin and relief to draw. */
11659 relief = (tool_bar_button_relief >= 0
11660 ? tool_bar_button_relief
11661 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11662 hmargin = vmargin = relief;
11663
11664 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11665 INT_MAX - max (hmargin, vmargin)))
11666 {
11667 hmargin += XFASTINT (Vtool_bar_button_margin);
11668 vmargin += XFASTINT (Vtool_bar_button_margin);
11669 }
11670 else if (CONSP (Vtool_bar_button_margin))
11671 {
11672 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11673 INT_MAX - hmargin))
11674 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11675
11676 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11677 INT_MAX - vmargin))
11678 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11679 }
11680
11681 if (auto_raise_tool_bar_buttons_p)
11682 {
11683 /* Add a `:relief' property to the image spec if the item is
11684 selected. */
11685 if (selected_p)
11686 {
11687 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11688 hmargin -= relief;
11689 vmargin -= relief;
11690 }
11691 }
11692 else
11693 {
11694 /* If image is selected, display it pressed, i.e. with a
11695 negative relief. If it's not selected, display it with a
11696 raised relief. */
11697 plist = Fplist_put (plist, QCrelief,
11698 (selected_p
11699 ? make_number (-relief)
11700 : make_number (relief)));
11701 hmargin -= relief;
11702 vmargin -= relief;
11703 }
11704
11705 /* Put a margin around the image. */
11706 if (hmargin || vmargin)
11707 {
11708 if (hmargin == vmargin)
11709 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11710 else
11711 plist = Fplist_put (plist, QCmargin,
11712 Fcons (make_number (hmargin),
11713 make_number (vmargin)));
11714 }
11715
11716 /* If button is not enabled, and we don't have special images
11717 for the disabled state, make the image appear disabled by
11718 applying an appropriate algorithm to it. */
11719 if (!enabled_p && idx < 0)
11720 plist = Fplist_put (plist, QCconversion, Qdisabled);
11721
11722 /* Put a `display' text property on the string for the image to
11723 display. Put a `menu-item' property on the string that gives
11724 the start of this item's properties in the tool-bar items
11725 vector. */
11726 image = Fcons (Qimage, plist);
11727 props = list4 (Qdisplay, image,
11728 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11729
11730 /* Let the last image hide all remaining spaces in the tool bar
11731 string. The string can be longer than needed when we reuse a
11732 previous string. */
11733 if (i + 1 == f->n_tool_bar_items)
11734 end = SCHARS (f->desired_tool_bar_string);
11735 else
11736 end = i + 1;
11737 Fadd_text_properties (make_number (i), make_number (end),
11738 props, f->desired_tool_bar_string);
11739 #undef PROP
11740 }
11741
11742 UNGCPRO;
11743 }
11744
11745
11746 /* Display one line of the tool-bar of frame IT->f.
11747
11748 HEIGHT specifies the desired height of the tool-bar line.
11749 If the actual height of the glyph row is less than HEIGHT, the
11750 row's height is increased to HEIGHT, and the icons are centered
11751 vertically in the new height.
11752
11753 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11754 count a final empty row in case the tool-bar width exactly matches
11755 the window width.
11756 */
11757
11758 static void
11759 display_tool_bar_line (struct it *it, int height)
11760 {
11761 struct glyph_row *row = it->glyph_row;
11762 int max_x = it->last_visible_x;
11763 struct glyph *last;
11764
11765 prepare_desired_row (row);
11766 row->y = it->current_y;
11767
11768 /* Note that this isn't made use of if the face hasn't a box,
11769 so there's no need to check the face here. */
11770 it->start_of_box_run_p = 1;
11771
11772 while (it->current_x < max_x)
11773 {
11774 int x, n_glyphs_before, i, nglyphs;
11775 struct it it_before;
11776
11777 /* Get the next display element. */
11778 if (!get_next_display_element (it))
11779 {
11780 /* Don't count empty row if we are counting needed tool-bar lines. */
11781 if (height < 0 && !it->hpos)
11782 return;
11783 break;
11784 }
11785
11786 /* Produce glyphs. */
11787 n_glyphs_before = row->used[TEXT_AREA];
11788 it_before = *it;
11789
11790 PRODUCE_GLYPHS (it);
11791
11792 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11793 i = 0;
11794 x = it_before.current_x;
11795 while (i < nglyphs)
11796 {
11797 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11798
11799 if (x + glyph->pixel_width > max_x)
11800 {
11801 /* Glyph doesn't fit on line. Backtrack. */
11802 row->used[TEXT_AREA] = n_glyphs_before;
11803 *it = it_before;
11804 /* If this is the only glyph on this line, it will never fit on the
11805 tool-bar, so skip it. But ensure there is at least one glyph,
11806 so we don't accidentally disable the tool-bar. */
11807 if (n_glyphs_before == 0
11808 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11809 break;
11810 goto out;
11811 }
11812
11813 ++it->hpos;
11814 x += glyph->pixel_width;
11815 ++i;
11816 }
11817
11818 /* Stop at line end. */
11819 if (ITERATOR_AT_END_OF_LINE_P (it))
11820 break;
11821
11822 set_iterator_to_next (it, 1);
11823 }
11824
11825 out:;
11826
11827 row->displays_text_p = row->used[TEXT_AREA] != 0;
11828
11829 /* Use default face for the border below the tool bar.
11830
11831 FIXME: When auto-resize-tool-bars is grow-only, there is
11832 no additional border below the possibly empty tool-bar lines.
11833 So to make the extra empty lines look "normal", we have to
11834 use the tool-bar face for the border too. */
11835 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11836 it->face_id = DEFAULT_FACE_ID;
11837
11838 extend_face_to_end_of_line (it);
11839 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11840 last->right_box_line_p = 1;
11841 if (last == row->glyphs[TEXT_AREA])
11842 last->left_box_line_p = 1;
11843
11844 /* Make line the desired height and center it vertically. */
11845 if ((height -= it->max_ascent + it->max_descent) > 0)
11846 {
11847 /* Don't add more than one line height. */
11848 height %= FRAME_LINE_HEIGHT (it->f);
11849 it->max_ascent += height / 2;
11850 it->max_descent += (height + 1) / 2;
11851 }
11852
11853 compute_line_metrics (it);
11854
11855 /* If line is empty, make it occupy the rest of the tool-bar. */
11856 if (!row->displays_text_p)
11857 {
11858 row->height = row->phys_height = it->last_visible_y - row->y;
11859 row->visible_height = row->height;
11860 row->ascent = row->phys_ascent = 0;
11861 row->extra_line_spacing = 0;
11862 }
11863
11864 row->full_width_p = 1;
11865 row->continued_p = 0;
11866 row->truncated_on_left_p = 0;
11867 row->truncated_on_right_p = 0;
11868
11869 it->current_x = it->hpos = 0;
11870 it->current_y += row->height;
11871 ++it->vpos;
11872 ++it->glyph_row;
11873 }
11874
11875
11876 /* Max tool-bar height. */
11877
11878 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11879 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11880
11881 /* Value is the number of screen lines needed to make all tool-bar
11882 items of frame F visible. The number of actual rows needed is
11883 returned in *N_ROWS if non-NULL. */
11884
11885 static int
11886 tool_bar_lines_needed (struct frame *f, int *n_rows)
11887 {
11888 struct window *w = XWINDOW (f->tool_bar_window);
11889 struct it it;
11890 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11891 the desired matrix, so use (unused) mode-line row as temporary row to
11892 avoid destroying the first tool-bar row. */
11893 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11894
11895 /* Initialize an iterator for iteration over
11896 F->desired_tool_bar_string in the tool-bar window of frame F. */
11897 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11898 it.first_visible_x = 0;
11899 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11900 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11901 it.paragraph_embedding = L2R;
11902
11903 while (!ITERATOR_AT_END_P (&it))
11904 {
11905 clear_glyph_row (temp_row);
11906 it.glyph_row = temp_row;
11907 display_tool_bar_line (&it, -1);
11908 }
11909 clear_glyph_row (temp_row);
11910
11911 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11912 if (n_rows)
11913 *n_rows = it.vpos > 0 ? it.vpos : -1;
11914
11915 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11916 }
11917
11918
11919 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11920 0, 1, 0,
11921 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
11922 (Lisp_Object frame)
11923 {
11924 struct frame *f;
11925 struct window *w;
11926 int nlines = 0;
11927
11928 if (NILP (frame))
11929 frame = selected_frame;
11930 else
11931 CHECK_FRAME (frame);
11932 f = XFRAME (frame);
11933
11934 if (WINDOWP (f->tool_bar_window)
11935 && (w = XWINDOW (f->tool_bar_window),
11936 WINDOW_TOTAL_LINES (w) > 0))
11937 {
11938 update_tool_bar (f, 1);
11939 if (f->n_tool_bar_items)
11940 {
11941 build_desired_tool_bar_string (f);
11942 nlines = tool_bar_lines_needed (f, NULL);
11943 }
11944 }
11945
11946 return make_number (nlines);
11947 }
11948
11949
11950 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11951 height should be changed. */
11952
11953 static int
11954 redisplay_tool_bar (struct frame *f)
11955 {
11956 struct window *w;
11957 struct it it;
11958 struct glyph_row *row;
11959
11960 #if defined (USE_GTK) || defined (HAVE_NS)
11961 if (FRAME_EXTERNAL_TOOL_BAR (f))
11962 update_frame_tool_bar (f);
11963 return 0;
11964 #endif
11965
11966 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11967 do anything. This means you must start with tool-bar-lines
11968 non-zero to get the auto-sizing effect. Or in other words, you
11969 can turn off tool-bars by specifying tool-bar-lines zero. */
11970 if (!WINDOWP (f->tool_bar_window)
11971 || (w = XWINDOW (f->tool_bar_window),
11972 WINDOW_TOTAL_LINES (w) == 0))
11973 return 0;
11974
11975 /* Set up an iterator for the tool-bar window. */
11976 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11977 it.first_visible_x = 0;
11978 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11979 row = it.glyph_row;
11980
11981 /* Build a string that represents the contents of the tool-bar. */
11982 build_desired_tool_bar_string (f);
11983 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11984 /* FIXME: This should be controlled by a user option. But it
11985 doesn't make sense to have an R2L tool bar if the menu bar cannot
11986 be drawn also R2L, and making the menu bar R2L is tricky due
11987 toolkit-specific code that implements it. If an R2L tool bar is
11988 ever supported, display_tool_bar_line should also be augmented to
11989 call unproduce_glyphs like display_line and display_string
11990 do. */
11991 it.paragraph_embedding = L2R;
11992
11993 if (f->n_tool_bar_rows == 0)
11994 {
11995 int nlines;
11996
11997 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11998 nlines != WINDOW_TOTAL_LINES (w)))
11999 {
12000 Lisp_Object frame;
12001 int old_height = WINDOW_TOTAL_LINES (w);
12002
12003 XSETFRAME (frame, f);
12004 Fmodify_frame_parameters (frame,
12005 Fcons (Fcons (Qtool_bar_lines,
12006 make_number (nlines)),
12007 Qnil));
12008 if (WINDOW_TOTAL_LINES (w) != old_height)
12009 {
12010 clear_glyph_matrix (w->desired_matrix);
12011 fonts_changed_p = 1;
12012 return 1;
12013 }
12014 }
12015 }
12016
12017 /* Display as many lines as needed to display all tool-bar items. */
12018
12019 if (f->n_tool_bar_rows > 0)
12020 {
12021 int border, rows, height, extra;
12022
12023 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12024 border = XINT (Vtool_bar_border);
12025 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12026 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12027 else if (EQ (Vtool_bar_border, Qborder_width))
12028 border = f->border_width;
12029 else
12030 border = 0;
12031 if (border < 0)
12032 border = 0;
12033
12034 rows = f->n_tool_bar_rows;
12035 height = max (1, (it.last_visible_y - border) / rows);
12036 extra = it.last_visible_y - border - height * rows;
12037
12038 while (it.current_y < it.last_visible_y)
12039 {
12040 int h = 0;
12041 if (extra > 0 && rows-- > 0)
12042 {
12043 h = (extra + rows - 1) / rows;
12044 extra -= h;
12045 }
12046 display_tool_bar_line (&it, height + h);
12047 }
12048 }
12049 else
12050 {
12051 while (it.current_y < it.last_visible_y)
12052 display_tool_bar_line (&it, 0);
12053 }
12054
12055 /* It doesn't make much sense to try scrolling in the tool-bar
12056 window, so don't do it. */
12057 w->desired_matrix->no_scrolling_p = 1;
12058 w->must_be_updated_p = 1;
12059
12060 if (!NILP (Vauto_resize_tool_bars))
12061 {
12062 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12063 int change_height_p = 0;
12064
12065 /* If we couldn't display everything, change the tool-bar's
12066 height if there is room for more. */
12067 if (IT_STRING_CHARPOS (it) < it.end_charpos
12068 && it.current_y < max_tool_bar_height)
12069 change_height_p = 1;
12070
12071 row = it.glyph_row - 1;
12072
12073 /* If there are blank lines at the end, except for a partially
12074 visible blank line at the end that is smaller than
12075 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12076 if (!row->displays_text_p
12077 && row->height >= FRAME_LINE_HEIGHT (f))
12078 change_height_p = 1;
12079
12080 /* If row displays tool-bar items, but is partially visible,
12081 change the tool-bar's height. */
12082 if (row->displays_text_p
12083 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12084 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12085 change_height_p = 1;
12086
12087 /* Resize windows as needed by changing the `tool-bar-lines'
12088 frame parameter. */
12089 if (change_height_p)
12090 {
12091 Lisp_Object frame;
12092 int old_height = WINDOW_TOTAL_LINES (w);
12093 int nrows;
12094 int nlines = tool_bar_lines_needed (f, &nrows);
12095
12096 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12097 && !f->minimize_tool_bar_window_p)
12098 ? (nlines > old_height)
12099 : (nlines != old_height));
12100 f->minimize_tool_bar_window_p = 0;
12101
12102 if (change_height_p)
12103 {
12104 XSETFRAME (frame, f);
12105 Fmodify_frame_parameters (frame,
12106 Fcons (Fcons (Qtool_bar_lines,
12107 make_number (nlines)),
12108 Qnil));
12109 if (WINDOW_TOTAL_LINES (w) != old_height)
12110 {
12111 clear_glyph_matrix (w->desired_matrix);
12112 f->n_tool_bar_rows = nrows;
12113 fonts_changed_p = 1;
12114 return 1;
12115 }
12116 }
12117 }
12118 }
12119
12120 f->minimize_tool_bar_window_p = 0;
12121 return 0;
12122 }
12123
12124
12125 /* Get information about the tool-bar item which is displayed in GLYPH
12126 on frame F. Return in *PROP_IDX the index where tool-bar item
12127 properties start in F->tool_bar_items. Value is zero if
12128 GLYPH doesn't display a tool-bar item. */
12129
12130 static int
12131 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12132 {
12133 Lisp_Object prop;
12134 int success_p;
12135 int charpos;
12136
12137 /* This function can be called asynchronously, which means we must
12138 exclude any possibility that Fget_text_property signals an
12139 error. */
12140 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12141 charpos = max (0, charpos);
12142
12143 /* Get the text property `menu-item' at pos. The value of that
12144 property is the start index of this item's properties in
12145 F->tool_bar_items. */
12146 prop = Fget_text_property (make_number (charpos),
12147 Qmenu_item, f->current_tool_bar_string);
12148 if (INTEGERP (prop))
12149 {
12150 *prop_idx = XINT (prop);
12151 success_p = 1;
12152 }
12153 else
12154 success_p = 0;
12155
12156 return success_p;
12157 }
12158
12159 \f
12160 /* Get information about the tool-bar item at position X/Y on frame F.
12161 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12162 the current matrix of the tool-bar window of F, or NULL if not
12163 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12164 item in F->tool_bar_items. Value is
12165
12166 -1 if X/Y is not on a tool-bar item
12167 0 if X/Y is on the same item that was highlighted before.
12168 1 otherwise. */
12169
12170 static int
12171 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12172 int *hpos, int *vpos, int *prop_idx)
12173 {
12174 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12175 struct window *w = XWINDOW (f->tool_bar_window);
12176 int area;
12177
12178 /* Find the glyph under X/Y. */
12179 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12180 if (*glyph == NULL)
12181 return -1;
12182
12183 /* Get the start of this tool-bar item's properties in
12184 f->tool_bar_items. */
12185 if (!tool_bar_item_info (f, *glyph, prop_idx))
12186 return -1;
12187
12188 /* Is mouse on the highlighted item? */
12189 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12190 && *vpos >= hlinfo->mouse_face_beg_row
12191 && *vpos <= hlinfo->mouse_face_end_row
12192 && (*vpos > hlinfo->mouse_face_beg_row
12193 || *hpos >= hlinfo->mouse_face_beg_col)
12194 && (*vpos < hlinfo->mouse_face_end_row
12195 || *hpos < hlinfo->mouse_face_end_col
12196 || hlinfo->mouse_face_past_end))
12197 return 0;
12198
12199 return 1;
12200 }
12201
12202
12203 /* EXPORT:
12204 Handle mouse button event on the tool-bar of frame F, at
12205 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12206 0 for button release. MODIFIERS is event modifiers for button
12207 release. */
12208
12209 void
12210 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12211 int modifiers)
12212 {
12213 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12214 struct window *w = XWINDOW (f->tool_bar_window);
12215 int hpos, vpos, prop_idx;
12216 struct glyph *glyph;
12217 Lisp_Object enabled_p;
12218
12219 /* If not on the highlighted tool-bar item, return. */
12220 frame_to_window_pixel_xy (w, &x, &y);
12221 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
12222 return;
12223
12224 /* If item is disabled, do nothing. */
12225 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12226 if (NILP (enabled_p))
12227 return;
12228
12229 if (down_p)
12230 {
12231 /* Show item in pressed state. */
12232 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12233 hlinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
12234 last_tool_bar_item = prop_idx;
12235 }
12236 else
12237 {
12238 Lisp_Object key, frame;
12239 struct input_event event;
12240 EVENT_INIT (event);
12241
12242 /* Show item in released state. */
12243 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12244 hlinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
12245
12246 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12247
12248 XSETFRAME (frame, f);
12249 event.kind = TOOL_BAR_EVENT;
12250 event.frame_or_window = frame;
12251 event.arg = frame;
12252 kbd_buffer_store_event (&event);
12253
12254 event.kind = TOOL_BAR_EVENT;
12255 event.frame_or_window = frame;
12256 event.arg = key;
12257 event.modifiers = modifiers;
12258 kbd_buffer_store_event (&event);
12259 last_tool_bar_item = -1;
12260 }
12261 }
12262
12263
12264 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12265 tool-bar window-relative coordinates X/Y. Called from
12266 note_mouse_highlight. */
12267
12268 static void
12269 note_tool_bar_highlight (struct frame *f, int x, int y)
12270 {
12271 Lisp_Object window = f->tool_bar_window;
12272 struct window *w = XWINDOW (window);
12273 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12274 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12275 int hpos, vpos;
12276 struct glyph *glyph;
12277 struct glyph_row *row;
12278 int i;
12279 Lisp_Object enabled_p;
12280 int prop_idx;
12281 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12282 int mouse_down_p, rc;
12283
12284 /* Function note_mouse_highlight is called with negative X/Y
12285 values when mouse moves outside of the frame. */
12286 if (x <= 0 || y <= 0)
12287 {
12288 clear_mouse_face (hlinfo);
12289 return;
12290 }
12291
12292 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12293 if (rc < 0)
12294 {
12295 /* Not on tool-bar item. */
12296 clear_mouse_face (hlinfo);
12297 return;
12298 }
12299 else if (rc == 0)
12300 /* On same tool-bar item as before. */
12301 goto set_help_echo;
12302
12303 clear_mouse_face (hlinfo);
12304
12305 /* Mouse is down, but on different tool-bar item? */
12306 mouse_down_p = (dpyinfo->grabbed
12307 && f == last_mouse_frame
12308 && FRAME_LIVE_P (f));
12309 if (mouse_down_p
12310 && last_tool_bar_item != prop_idx)
12311 return;
12312
12313 hlinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
12314 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12315
12316 /* If tool-bar item is not enabled, don't highlight it. */
12317 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12318 if (!NILP (enabled_p))
12319 {
12320 /* Compute the x-position of the glyph. In front and past the
12321 image is a space. We include this in the highlighted area. */
12322 row = MATRIX_ROW (w->current_matrix, vpos);
12323 for (i = x = 0; i < hpos; ++i)
12324 x += row->glyphs[TEXT_AREA][i].pixel_width;
12325
12326 /* Record this as the current active region. */
12327 hlinfo->mouse_face_beg_col = hpos;
12328 hlinfo->mouse_face_beg_row = vpos;
12329 hlinfo->mouse_face_beg_x = x;
12330 hlinfo->mouse_face_beg_y = row->y;
12331 hlinfo->mouse_face_past_end = 0;
12332
12333 hlinfo->mouse_face_end_col = hpos + 1;
12334 hlinfo->mouse_face_end_row = vpos;
12335 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12336 hlinfo->mouse_face_end_y = row->y;
12337 hlinfo->mouse_face_window = window;
12338 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12339
12340 /* Display it as active. */
12341 show_mouse_face (hlinfo, draw);
12342 hlinfo->mouse_face_image_state = draw;
12343 }
12344
12345 set_help_echo:
12346
12347 /* Set help_echo_string to a help string to display for this tool-bar item.
12348 XTread_socket does the rest. */
12349 help_echo_object = help_echo_window = Qnil;
12350 help_echo_pos = -1;
12351 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12352 if (NILP (help_echo_string))
12353 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12354 }
12355
12356 #endif /* HAVE_WINDOW_SYSTEM */
12357
12358
12359 \f
12360 /************************************************************************
12361 Horizontal scrolling
12362 ************************************************************************/
12363
12364 static int hscroll_window_tree (Lisp_Object);
12365 static int hscroll_windows (Lisp_Object);
12366
12367 /* For all leaf windows in the window tree rooted at WINDOW, set their
12368 hscroll value so that PT is (i) visible in the window, and (ii) so
12369 that it is not within a certain margin at the window's left and
12370 right border. Value is non-zero if any window's hscroll has been
12371 changed. */
12372
12373 static int
12374 hscroll_window_tree (Lisp_Object window)
12375 {
12376 int hscrolled_p = 0;
12377 int hscroll_relative_p = FLOATP (Vhscroll_step);
12378 int hscroll_step_abs = 0;
12379 double hscroll_step_rel = 0;
12380
12381 if (hscroll_relative_p)
12382 {
12383 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12384 if (hscroll_step_rel < 0)
12385 {
12386 hscroll_relative_p = 0;
12387 hscroll_step_abs = 0;
12388 }
12389 }
12390 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12391 {
12392 hscroll_step_abs = XINT (Vhscroll_step);
12393 if (hscroll_step_abs < 0)
12394 hscroll_step_abs = 0;
12395 }
12396 else
12397 hscroll_step_abs = 0;
12398
12399 while (WINDOWP (window))
12400 {
12401 struct window *w = XWINDOW (window);
12402
12403 if (WINDOWP (w->hchild))
12404 hscrolled_p |= hscroll_window_tree (w->hchild);
12405 else if (WINDOWP (w->vchild))
12406 hscrolled_p |= hscroll_window_tree (w->vchild);
12407 else if (w->cursor.vpos >= 0)
12408 {
12409 int h_margin;
12410 int text_area_width;
12411 struct glyph_row *current_cursor_row
12412 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12413 struct glyph_row *desired_cursor_row
12414 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12415 struct glyph_row *cursor_row
12416 = (desired_cursor_row->enabled_p
12417 ? desired_cursor_row
12418 : current_cursor_row);
12419 int row_r2l_p = cursor_row->reversed_p;
12420
12421 text_area_width = window_box_width (w, TEXT_AREA);
12422
12423 /* Scroll when cursor is inside this scroll margin. */
12424 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12425
12426 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
12427 /* For left-to-right rows, hscroll when cursor is either
12428 (i) inside the right hscroll margin, or (ii) if it is
12429 inside the left margin and the window is already
12430 hscrolled. */
12431 && ((!row_r2l_p
12432 && ((w->hscroll
12433 && w->cursor.x <= h_margin)
12434 || (cursor_row->enabled_p
12435 && cursor_row->truncated_on_right_p
12436 && (w->cursor.x >= text_area_width - h_margin))))
12437 /* For right-to-left rows, the logic is similar,
12438 except that rules for scrolling to left and right
12439 are reversed. E.g., if cursor.x <= h_margin, we
12440 need to hscroll "to the right" unconditionally,
12441 and that will scroll the screen to the left so as
12442 to reveal the next portion of the row. */
12443 || (row_r2l_p
12444 && ((cursor_row->enabled_p
12445 /* FIXME: It is confusing to set the
12446 truncated_on_right_p flag when R2L rows
12447 are actually truncated on the left. */
12448 && cursor_row->truncated_on_right_p
12449 && w->cursor.x <= h_margin)
12450 || (w->hscroll
12451 && (w->cursor.x >= text_area_width - h_margin))))))
12452 {
12453 struct it it;
12454 ptrdiff_t hscroll;
12455 struct buffer *saved_current_buffer;
12456 ptrdiff_t pt;
12457 int wanted_x;
12458
12459 /* Find point in a display of infinite width. */
12460 saved_current_buffer = current_buffer;
12461 current_buffer = XBUFFER (w->buffer);
12462
12463 if (w == XWINDOW (selected_window))
12464 pt = PT;
12465 else
12466 {
12467 pt = marker_position (w->pointm);
12468 pt = max (BEGV, pt);
12469 pt = min (ZV, pt);
12470 }
12471
12472 /* Move iterator to pt starting at cursor_row->start in
12473 a line with infinite width. */
12474 init_to_row_start (&it, w, cursor_row);
12475 it.last_visible_x = INFINITY;
12476 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12477 current_buffer = saved_current_buffer;
12478
12479 /* Position cursor in window. */
12480 if (!hscroll_relative_p && hscroll_step_abs == 0)
12481 hscroll = max (0, (it.current_x
12482 - (ITERATOR_AT_END_OF_LINE_P (&it)
12483 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12484 : (text_area_width / 2))))
12485 / FRAME_COLUMN_WIDTH (it.f);
12486 else if ((!row_r2l_p
12487 && w->cursor.x >= text_area_width - h_margin)
12488 || (row_r2l_p && w->cursor.x <= h_margin))
12489 {
12490 if (hscroll_relative_p)
12491 wanted_x = text_area_width * (1 - hscroll_step_rel)
12492 - h_margin;
12493 else
12494 wanted_x = text_area_width
12495 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12496 - h_margin;
12497 hscroll
12498 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12499 }
12500 else
12501 {
12502 if (hscroll_relative_p)
12503 wanted_x = text_area_width * hscroll_step_rel
12504 + h_margin;
12505 else
12506 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12507 + h_margin;
12508 hscroll
12509 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12510 }
12511 hscroll = max (hscroll, w->min_hscroll);
12512
12513 /* Don't prevent redisplay optimizations if hscroll
12514 hasn't changed, as it will unnecessarily slow down
12515 redisplay. */
12516 if (w->hscroll != hscroll)
12517 {
12518 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
12519 w->hscroll = hscroll;
12520 hscrolled_p = 1;
12521 }
12522 }
12523 }
12524
12525 window = w->next;
12526 }
12527
12528 /* Value is non-zero if hscroll of any leaf window has been changed. */
12529 return hscrolled_p;
12530 }
12531
12532
12533 /* Set hscroll so that cursor is visible and not inside horizontal
12534 scroll margins for all windows in the tree rooted at WINDOW. See
12535 also hscroll_window_tree above. Value is non-zero if any window's
12536 hscroll has been changed. If it has, desired matrices on the frame
12537 of WINDOW are cleared. */
12538
12539 static int
12540 hscroll_windows (Lisp_Object window)
12541 {
12542 int hscrolled_p = hscroll_window_tree (window);
12543 if (hscrolled_p)
12544 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12545 return hscrolled_p;
12546 }
12547
12548
12549 \f
12550 /************************************************************************
12551 Redisplay
12552 ************************************************************************/
12553
12554 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12555 to a non-zero value. This is sometimes handy to have in a debugger
12556 session. */
12557
12558 #ifdef GLYPH_DEBUG
12559
12560 /* First and last unchanged row for try_window_id. */
12561
12562 static int debug_first_unchanged_at_end_vpos;
12563 static int debug_last_unchanged_at_beg_vpos;
12564
12565 /* Delta vpos and y. */
12566
12567 static int debug_dvpos, debug_dy;
12568
12569 /* Delta in characters and bytes for try_window_id. */
12570
12571 static ptrdiff_t debug_delta, debug_delta_bytes;
12572
12573 /* Values of window_end_pos and window_end_vpos at the end of
12574 try_window_id. */
12575
12576 static ptrdiff_t debug_end_vpos;
12577
12578 /* Append a string to W->desired_matrix->method. FMT is a printf
12579 format string. If trace_redisplay_p is non-zero also printf the
12580 resulting string to stderr. */
12581
12582 static void debug_method_add (struct window *, char const *, ...)
12583 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12584
12585 static void
12586 debug_method_add (struct window *w, char const *fmt, ...)
12587 {
12588 char *method = w->desired_matrix->method;
12589 int len = strlen (method);
12590 int size = sizeof w->desired_matrix->method;
12591 int remaining = size - len - 1;
12592 va_list ap;
12593
12594 if (len && remaining)
12595 {
12596 method[len] = '|';
12597 --remaining, ++len;
12598 }
12599
12600 va_start (ap, fmt);
12601 vsnprintf (method + len, remaining + 1, fmt, ap);
12602 va_end (ap);
12603
12604 if (trace_redisplay_p)
12605 fprintf (stderr, "%p (%s): %s\n",
12606 w,
12607 ((BUFFERP (w->buffer)
12608 && STRINGP (BVAR (XBUFFER (w->buffer), name)))
12609 ? SSDATA (BVAR (XBUFFER (w->buffer), name))
12610 : "no buffer"),
12611 method + len);
12612 }
12613
12614 #endif /* GLYPH_DEBUG */
12615
12616
12617 /* Value is non-zero if all changes in window W, which displays
12618 current_buffer, are in the text between START and END. START is a
12619 buffer position, END is given as a distance from Z. Used in
12620 redisplay_internal for display optimization. */
12621
12622 static int
12623 text_outside_line_unchanged_p (struct window *w,
12624 ptrdiff_t start, ptrdiff_t end)
12625 {
12626 int unchanged_p = 1;
12627
12628 /* If text or overlays have changed, see where. */
12629 if (w->last_modified < MODIFF
12630 || w->last_overlay_modified < OVERLAY_MODIFF)
12631 {
12632 /* Gap in the line? */
12633 if (GPT < start || Z - GPT < end)
12634 unchanged_p = 0;
12635
12636 /* Changes start in front of the line, or end after it? */
12637 if (unchanged_p
12638 && (BEG_UNCHANGED < start - 1
12639 || END_UNCHANGED < end))
12640 unchanged_p = 0;
12641
12642 /* If selective display, can't optimize if changes start at the
12643 beginning of the line. */
12644 if (unchanged_p
12645 && INTEGERP (BVAR (current_buffer, selective_display))
12646 && XINT (BVAR (current_buffer, selective_display)) > 0
12647 && (BEG_UNCHANGED < start || GPT <= start))
12648 unchanged_p = 0;
12649
12650 /* If there are overlays at the start or end of the line, these
12651 may have overlay strings with newlines in them. A change at
12652 START, for instance, may actually concern the display of such
12653 overlay strings as well, and they are displayed on different
12654 lines. So, quickly rule out this case. (For the future, it
12655 might be desirable to implement something more telling than
12656 just BEG/END_UNCHANGED.) */
12657 if (unchanged_p)
12658 {
12659 if (BEG + BEG_UNCHANGED == start
12660 && overlay_touches_p (start))
12661 unchanged_p = 0;
12662 if (END_UNCHANGED == end
12663 && overlay_touches_p (Z - end))
12664 unchanged_p = 0;
12665 }
12666
12667 /* Under bidi reordering, adding or deleting a character in the
12668 beginning of a paragraph, before the first strong directional
12669 character, can change the base direction of the paragraph (unless
12670 the buffer specifies a fixed paragraph direction), which will
12671 require to redisplay the whole paragraph. It might be worthwhile
12672 to find the paragraph limits and widen the range of redisplayed
12673 lines to that, but for now just give up this optimization. */
12674 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
12675 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
12676 unchanged_p = 0;
12677 }
12678
12679 return unchanged_p;
12680 }
12681
12682
12683 /* Do a frame update, taking possible shortcuts into account. This is
12684 the main external entry point for redisplay.
12685
12686 If the last redisplay displayed an echo area message and that message
12687 is no longer requested, we clear the echo area or bring back the
12688 mini-buffer if that is in use. */
12689
12690 void
12691 redisplay (void)
12692 {
12693 redisplay_internal ();
12694 }
12695
12696
12697 static Lisp_Object
12698 overlay_arrow_string_or_property (Lisp_Object var)
12699 {
12700 Lisp_Object val;
12701
12702 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12703 return val;
12704
12705 return Voverlay_arrow_string;
12706 }
12707
12708 /* Return 1 if there are any overlay-arrows in current_buffer. */
12709 static int
12710 overlay_arrow_in_current_buffer_p (void)
12711 {
12712 Lisp_Object vlist;
12713
12714 for (vlist = Voverlay_arrow_variable_list;
12715 CONSP (vlist);
12716 vlist = XCDR (vlist))
12717 {
12718 Lisp_Object var = XCAR (vlist);
12719 Lisp_Object val;
12720
12721 if (!SYMBOLP (var))
12722 continue;
12723 val = find_symbol_value (var);
12724 if (MARKERP (val)
12725 && current_buffer == XMARKER (val)->buffer)
12726 return 1;
12727 }
12728 return 0;
12729 }
12730
12731
12732 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12733 has changed. */
12734
12735 static int
12736 overlay_arrows_changed_p (void)
12737 {
12738 Lisp_Object vlist;
12739
12740 for (vlist = Voverlay_arrow_variable_list;
12741 CONSP (vlist);
12742 vlist = XCDR (vlist))
12743 {
12744 Lisp_Object var = XCAR (vlist);
12745 Lisp_Object val, pstr;
12746
12747 if (!SYMBOLP (var))
12748 continue;
12749 val = find_symbol_value (var);
12750 if (!MARKERP (val))
12751 continue;
12752 if (! EQ (COERCE_MARKER (val),
12753 Fget (var, Qlast_arrow_position))
12754 || ! (pstr = overlay_arrow_string_or_property (var),
12755 EQ (pstr, Fget (var, Qlast_arrow_string))))
12756 return 1;
12757 }
12758 return 0;
12759 }
12760
12761 /* Mark overlay arrows to be updated on next redisplay. */
12762
12763 static void
12764 update_overlay_arrows (int up_to_date)
12765 {
12766 Lisp_Object vlist;
12767
12768 for (vlist = Voverlay_arrow_variable_list;
12769 CONSP (vlist);
12770 vlist = XCDR (vlist))
12771 {
12772 Lisp_Object var = XCAR (vlist);
12773
12774 if (!SYMBOLP (var))
12775 continue;
12776
12777 if (up_to_date > 0)
12778 {
12779 Lisp_Object val = find_symbol_value (var);
12780 Fput (var, Qlast_arrow_position,
12781 COERCE_MARKER (val));
12782 Fput (var, Qlast_arrow_string,
12783 overlay_arrow_string_or_property (var));
12784 }
12785 else if (up_to_date < 0
12786 || !NILP (Fget (var, Qlast_arrow_position)))
12787 {
12788 Fput (var, Qlast_arrow_position, Qt);
12789 Fput (var, Qlast_arrow_string, Qt);
12790 }
12791 }
12792 }
12793
12794
12795 /* Return overlay arrow string to display at row.
12796 Return integer (bitmap number) for arrow bitmap in left fringe.
12797 Return nil if no overlay arrow. */
12798
12799 static Lisp_Object
12800 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12801 {
12802 Lisp_Object vlist;
12803
12804 for (vlist = Voverlay_arrow_variable_list;
12805 CONSP (vlist);
12806 vlist = XCDR (vlist))
12807 {
12808 Lisp_Object var = XCAR (vlist);
12809 Lisp_Object val;
12810
12811 if (!SYMBOLP (var))
12812 continue;
12813
12814 val = find_symbol_value (var);
12815
12816 if (MARKERP (val)
12817 && current_buffer == XMARKER (val)->buffer
12818 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12819 {
12820 if (FRAME_WINDOW_P (it->f)
12821 /* FIXME: if ROW->reversed_p is set, this should test
12822 the right fringe, not the left one. */
12823 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12824 {
12825 #ifdef HAVE_WINDOW_SYSTEM
12826 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12827 {
12828 int fringe_bitmap;
12829 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12830 return make_number (fringe_bitmap);
12831 }
12832 #endif
12833 return make_number (-1); /* Use default arrow bitmap. */
12834 }
12835 return overlay_arrow_string_or_property (var);
12836 }
12837 }
12838
12839 return Qnil;
12840 }
12841
12842 /* Return 1 if point moved out of or into a composition. Otherwise
12843 return 0. PREV_BUF and PREV_PT are the last point buffer and
12844 position. BUF and PT are the current point buffer and position. */
12845
12846 static int
12847 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12848 struct buffer *buf, ptrdiff_t pt)
12849 {
12850 ptrdiff_t start, end;
12851 Lisp_Object prop;
12852 Lisp_Object buffer;
12853
12854 XSETBUFFER (buffer, buf);
12855 /* Check a composition at the last point if point moved within the
12856 same buffer. */
12857 if (prev_buf == buf)
12858 {
12859 if (prev_pt == pt)
12860 /* Point didn't move. */
12861 return 0;
12862
12863 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12864 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12865 && COMPOSITION_VALID_P (start, end, prop)
12866 && start < prev_pt && end > prev_pt)
12867 /* The last point was within the composition. Return 1 iff
12868 point moved out of the composition. */
12869 return (pt <= start || pt >= end);
12870 }
12871
12872 /* Check a composition at the current point. */
12873 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12874 && find_composition (pt, -1, &start, &end, &prop, buffer)
12875 && COMPOSITION_VALID_P (start, end, prop)
12876 && start < pt && end > pt);
12877 }
12878
12879
12880 /* Reconsider the setting of B->clip_changed which is displayed
12881 in window W. */
12882
12883 static void
12884 reconsider_clip_changes (struct window *w, struct buffer *b)
12885 {
12886 if (b->clip_changed
12887 && !NILP (w->window_end_valid)
12888 && w->current_matrix->buffer == b
12889 && w->current_matrix->zv == BUF_ZV (b)
12890 && w->current_matrix->begv == BUF_BEGV (b))
12891 b->clip_changed = 0;
12892
12893 /* If display wasn't paused, and W is not a tool bar window, see if
12894 point has been moved into or out of a composition. In that case,
12895 we set b->clip_changed to 1 to force updating the screen. If
12896 b->clip_changed has already been set to 1, we can skip this
12897 check. */
12898 if (!b->clip_changed
12899 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
12900 {
12901 ptrdiff_t pt;
12902
12903 if (w == XWINDOW (selected_window))
12904 pt = PT;
12905 else
12906 pt = marker_position (w->pointm);
12907
12908 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
12909 || pt != w->last_point)
12910 && check_point_in_composition (w->current_matrix->buffer,
12911 w->last_point,
12912 XBUFFER (w->buffer), pt))
12913 b->clip_changed = 1;
12914 }
12915 }
12916 \f
12917
12918 /* Select FRAME to forward the values of frame-local variables into C
12919 variables so that the redisplay routines can access those values
12920 directly. */
12921
12922 static void
12923 select_frame_for_redisplay (Lisp_Object frame)
12924 {
12925 Lisp_Object tail, tem;
12926 Lisp_Object old = selected_frame;
12927 struct Lisp_Symbol *sym;
12928
12929 eassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
12930
12931 selected_frame = frame;
12932
12933 do {
12934 for (tail = XFRAME (frame)->param_alist;
12935 CONSP (tail); tail = XCDR (tail))
12936 if (CONSP (XCAR (tail))
12937 && (tem = XCAR (XCAR (tail)),
12938 SYMBOLP (tem))
12939 && (sym = indirect_variable (XSYMBOL (tem)),
12940 sym->redirect == SYMBOL_LOCALIZED)
12941 && sym->val.blv->frame_local)
12942 /* Use find_symbol_value rather than Fsymbol_value
12943 to avoid an error if it is void. */
12944 find_symbol_value (tem);
12945 } while (!EQ (frame, old) && (frame = old, 1));
12946 }
12947
12948
12949 #define STOP_POLLING \
12950 do { if (! polling_stopped_here) stop_polling (); \
12951 polling_stopped_here = 1; } while (0)
12952
12953 #define RESUME_POLLING \
12954 do { if (polling_stopped_here) start_polling (); \
12955 polling_stopped_here = 0; } while (0)
12956
12957
12958 /* Perhaps in the future avoid recentering windows if it
12959 is not necessary; currently that causes some problems. */
12960
12961 static void
12962 redisplay_internal (void)
12963 {
12964 struct window *w = XWINDOW (selected_window);
12965 struct window *sw;
12966 struct frame *fr;
12967 int pending;
12968 int must_finish = 0;
12969 struct text_pos tlbufpos, tlendpos;
12970 int number_of_visible_frames;
12971 ptrdiff_t count, count1;
12972 struct frame *sf;
12973 int polling_stopped_here = 0;
12974 Lisp_Object old_frame = selected_frame;
12975 struct backtrace backtrace;
12976
12977 /* Non-zero means redisplay has to consider all windows on all
12978 frames. Zero means, only selected_window is considered. */
12979 int consider_all_windows_p;
12980
12981 /* Non-zero means redisplay has to redisplay the miniwindow. */
12982 int update_miniwindow_p = 0;
12983
12984 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12985
12986 /* No redisplay if running in batch mode or frame is not yet fully
12987 initialized, or redisplay is explicitly turned off by setting
12988 Vinhibit_redisplay. */
12989 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12990 || !NILP (Vinhibit_redisplay))
12991 return;
12992
12993 /* Don't examine these until after testing Vinhibit_redisplay.
12994 When Emacs is shutting down, perhaps because its connection to
12995 X has dropped, we should not look at them at all. */
12996 fr = XFRAME (w->frame);
12997 sf = SELECTED_FRAME ();
12998
12999 if (!fr->glyphs_initialized_p)
13000 return;
13001
13002 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13003 if (popup_activated ())
13004 return;
13005 #endif
13006
13007 /* I don't think this happens but let's be paranoid. */
13008 if (redisplaying_p)
13009 return;
13010
13011 /* Record a function that clears redisplaying_p
13012 when we leave this function. */
13013 count = SPECPDL_INDEX ();
13014 record_unwind_protect (unwind_redisplay, selected_frame);
13015 redisplaying_p = 1;
13016 specbind (Qinhibit_free_realized_faces, Qnil);
13017
13018 /* Record this function, so it appears on the profiler's backtraces. */
13019 backtrace.next = backtrace_list;
13020 backtrace.function = Qredisplay_internal;
13021 backtrace.args = &Qnil;
13022 backtrace.nargs = 0;
13023 backtrace.debug_on_exit = 0;
13024 backtrace_list = &backtrace;
13025
13026 {
13027 Lisp_Object tail, frame;
13028
13029 FOR_EACH_FRAME (tail, frame)
13030 {
13031 struct frame *f = XFRAME (frame);
13032 f->already_hscrolled_p = 0;
13033 }
13034 }
13035
13036 retry:
13037 /* Remember the currently selected window. */
13038 sw = w;
13039
13040 if (!EQ (old_frame, selected_frame)
13041 && FRAME_LIVE_P (XFRAME (old_frame)))
13042 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
13043 selected_frame and selected_window to be temporarily out-of-sync so
13044 when we come back here via `goto retry', we need to resync because we
13045 may need to run Elisp code (via prepare_menu_bars). */
13046 select_frame_for_redisplay (old_frame);
13047
13048 pending = 0;
13049 reconsider_clip_changes (w, current_buffer);
13050 last_escape_glyph_frame = NULL;
13051 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
13052 last_glyphless_glyph_frame = NULL;
13053 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
13054
13055 /* If new fonts have been loaded that make a glyph matrix adjustment
13056 necessary, do it. */
13057 if (fonts_changed_p)
13058 {
13059 adjust_glyphs (NULL);
13060 ++windows_or_buffers_changed;
13061 fonts_changed_p = 0;
13062 }
13063
13064 /* If face_change_count is non-zero, init_iterator will free all
13065 realized faces, which includes the faces referenced from current
13066 matrices. So, we can't reuse current matrices in this case. */
13067 if (face_change_count)
13068 ++windows_or_buffers_changed;
13069
13070 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13071 && FRAME_TTY (sf)->previous_frame != sf)
13072 {
13073 /* Since frames on a single ASCII terminal share the same
13074 display area, displaying a different frame means redisplay
13075 the whole thing. */
13076 windows_or_buffers_changed++;
13077 SET_FRAME_GARBAGED (sf);
13078 #ifndef DOS_NT
13079 set_tty_color_mode (FRAME_TTY (sf), sf);
13080 #endif
13081 FRAME_TTY (sf)->previous_frame = sf;
13082 }
13083
13084 /* Set the visible flags for all frames. Do this before checking
13085 for resized or garbaged frames; they want to know if their frames
13086 are visible. See the comment in frame.h for
13087 FRAME_SAMPLE_VISIBILITY. */
13088 {
13089 Lisp_Object tail, frame;
13090
13091 number_of_visible_frames = 0;
13092
13093 FOR_EACH_FRAME (tail, frame)
13094 {
13095 struct frame *f = XFRAME (frame);
13096
13097 FRAME_SAMPLE_VISIBILITY (f);
13098 if (FRAME_VISIBLE_P (f))
13099 ++number_of_visible_frames;
13100 clear_desired_matrices (f);
13101 }
13102 }
13103
13104 /* Notice any pending interrupt request to change frame size. */
13105 do_pending_window_change (1);
13106
13107 /* do_pending_window_change could change the selected_window due to
13108 frame resizing which makes the selected window too small. */
13109 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13110 {
13111 sw = w;
13112 reconsider_clip_changes (w, current_buffer);
13113 }
13114
13115 /* Clear frames marked as garbaged. */
13116 if (frame_garbaged)
13117 clear_garbaged_frames ();
13118
13119 /* Build menubar and tool-bar items. */
13120 if (NILP (Vmemory_full))
13121 prepare_menu_bars ();
13122
13123 if (windows_or_buffers_changed)
13124 update_mode_lines++;
13125
13126 /* Detect case that we need to write or remove a star in the mode line. */
13127 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13128 {
13129 w->update_mode_line = 1;
13130 if (buffer_shared > 1)
13131 update_mode_lines++;
13132 }
13133
13134 /* Avoid invocation of point motion hooks by `current_column' below. */
13135 count1 = SPECPDL_INDEX ();
13136 specbind (Qinhibit_point_motion_hooks, Qt);
13137
13138 /* If %c is in the mode line, update it if needed. */
13139 if (!NILP (w->column_number_displayed)
13140 /* This alternative quickly identifies a common case
13141 where no change is needed. */
13142 && !(PT == w->last_point
13143 && w->last_modified >= MODIFF
13144 && w->last_overlay_modified >= OVERLAY_MODIFF)
13145 && (XFASTINT (w->column_number_displayed) != current_column ()))
13146 w->update_mode_line = 1;
13147
13148 unbind_to (count1, Qnil);
13149
13150 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
13151
13152 /* The variable buffer_shared is set in redisplay_window and
13153 indicates that we redisplay a buffer in different windows. See
13154 there. */
13155 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
13156 || cursor_type_changed);
13157
13158 /* If specs for an arrow have changed, do thorough redisplay
13159 to ensure we remove any arrow that should no longer exist. */
13160 if (overlay_arrows_changed_p ())
13161 consider_all_windows_p = windows_or_buffers_changed = 1;
13162
13163 /* Normally the message* functions will have already displayed and
13164 updated the echo area, but the frame may have been trashed, or
13165 the update may have been preempted, so display the echo area
13166 again here. Checking message_cleared_p captures the case that
13167 the echo area should be cleared. */
13168 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13169 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13170 || (message_cleared_p
13171 && minibuf_level == 0
13172 /* If the mini-window is currently selected, this means the
13173 echo-area doesn't show through. */
13174 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13175 {
13176 int window_height_changed_p = echo_area_display (0);
13177
13178 if (message_cleared_p)
13179 update_miniwindow_p = 1;
13180
13181 must_finish = 1;
13182
13183 /* If we don't display the current message, don't clear the
13184 message_cleared_p flag, because, if we did, we wouldn't clear
13185 the echo area in the next redisplay which doesn't preserve
13186 the echo area. */
13187 if (!display_last_displayed_message_p)
13188 message_cleared_p = 0;
13189
13190 if (fonts_changed_p)
13191 goto retry;
13192 else if (window_height_changed_p)
13193 {
13194 consider_all_windows_p = 1;
13195 ++update_mode_lines;
13196 ++windows_or_buffers_changed;
13197
13198 /* If window configuration was changed, frames may have been
13199 marked garbaged. Clear them or we will experience
13200 surprises wrt scrolling. */
13201 if (frame_garbaged)
13202 clear_garbaged_frames ();
13203 }
13204 }
13205 else if (EQ (selected_window, minibuf_window)
13206 && (current_buffer->clip_changed
13207 || w->last_modified < MODIFF
13208 || w->last_overlay_modified < OVERLAY_MODIFF)
13209 && resize_mini_window (w, 0))
13210 {
13211 /* Resized active mini-window to fit the size of what it is
13212 showing if its contents might have changed. */
13213 must_finish = 1;
13214 /* FIXME: this causes all frames to be updated, which seems unnecessary
13215 since only the current frame needs to be considered. This function needs
13216 to be rewritten with two variables, consider_all_windows and
13217 consider_all_frames. */
13218 consider_all_windows_p = 1;
13219 ++windows_or_buffers_changed;
13220 ++update_mode_lines;
13221
13222 /* If window configuration was changed, frames may have been
13223 marked garbaged. Clear them or we will experience
13224 surprises wrt scrolling. */
13225 if (frame_garbaged)
13226 clear_garbaged_frames ();
13227 }
13228
13229
13230 /* If showing the region, and mark has changed, we must redisplay
13231 the whole window. The assignment to this_line_start_pos prevents
13232 the optimization directly below this if-statement. */
13233 if (((!NILP (Vtransient_mark_mode)
13234 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
13235 != !NILP (w->region_showing))
13236 || (!NILP (w->region_showing)
13237 && !EQ (w->region_showing,
13238 Fmarker_position (BVAR (XBUFFER (w->buffer), mark)))))
13239 CHARPOS (this_line_start_pos) = 0;
13240
13241 /* Optimize the case that only the line containing the cursor in the
13242 selected window has changed. Variables starting with this_ are
13243 set in display_line and record information about the line
13244 containing the cursor. */
13245 tlbufpos = this_line_start_pos;
13246 tlendpos = this_line_end_pos;
13247 if (!consider_all_windows_p
13248 && CHARPOS (tlbufpos) > 0
13249 && !w->update_mode_line
13250 && !current_buffer->clip_changed
13251 && !current_buffer->prevent_redisplay_optimizations_p
13252 && FRAME_VISIBLE_P (XFRAME (w->frame))
13253 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13254 /* Make sure recorded data applies to current buffer, etc. */
13255 && this_line_buffer == current_buffer
13256 && current_buffer == XBUFFER (w->buffer)
13257 && !w->force_start
13258 && !w->optional_new_start
13259 /* Point must be on the line that we have info recorded about. */
13260 && PT >= CHARPOS (tlbufpos)
13261 && PT <= Z - CHARPOS (tlendpos)
13262 /* All text outside that line, including its final newline,
13263 must be unchanged. */
13264 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13265 CHARPOS (tlendpos)))
13266 {
13267 if (CHARPOS (tlbufpos) > BEGV
13268 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13269 && (CHARPOS (tlbufpos) == ZV
13270 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13271 /* Former continuation line has disappeared by becoming empty. */
13272 goto cancel;
13273 else if (w->last_modified < MODIFF
13274 || w->last_overlay_modified < OVERLAY_MODIFF
13275 || MINI_WINDOW_P (w))
13276 {
13277 /* We have to handle the case of continuation around a
13278 wide-column character (see the comment in indent.c around
13279 line 1340).
13280
13281 For instance, in the following case:
13282
13283 -------- Insert --------
13284 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13285 J_I_ ==> J_I_ `^^' are cursors.
13286 ^^ ^^
13287 -------- --------
13288
13289 As we have to redraw the line above, we cannot use this
13290 optimization. */
13291
13292 struct it it;
13293 int line_height_before = this_line_pixel_height;
13294
13295 /* Note that start_display will handle the case that the
13296 line starting at tlbufpos is a continuation line. */
13297 start_display (&it, w, tlbufpos);
13298
13299 /* Implementation note: It this still necessary? */
13300 if (it.current_x != this_line_start_x)
13301 goto cancel;
13302
13303 TRACE ((stderr, "trying display optimization 1\n"));
13304 w->cursor.vpos = -1;
13305 overlay_arrow_seen = 0;
13306 it.vpos = this_line_vpos;
13307 it.current_y = this_line_y;
13308 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13309 display_line (&it);
13310
13311 /* If line contains point, is not continued,
13312 and ends at same distance from eob as before, we win. */
13313 if (w->cursor.vpos >= 0
13314 /* Line is not continued, otherwise this_line_start_pos
13315 would have been set to 0 in display_line. */
13316 && CHARPOS (this_line_start_pos)
13317 /* Line ends as before. */
13318 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13319 /* Line has same height as before. Otherwise other lines
13320 would have to be shifted up or down. */
13321 && this_line_pixel_height == line_height_before)
13322 {
13323 /* If this is not the window's last line, we must adjust
13324 the charstarts of the lines below. */
13325 if (it.current_y < it.last_visible_y)
13326 {
13327 struct glyph_row *row
13328 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13329 ptrdiff_t delta, delta_bytes;
13330
13331 /* We used to distinguish between two cases here,
13332 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13333 when the line ends in a newline or the end of the
13334 buffer's accessible portion. But both cases did
13335 the same, so they were collapsed. */
13336 delta = (Z
13337 - CHARPOS (tlendpos)
13338 - MATRIX_ROW_START_CHARPOS (row));
13339 delta_bytes = (Z_BYTE
13340 - BYTEPOS (tlendpos)
13341 - MATRIX_ROW_START_BYTEPOS (row));
13342
13343 increment_matrix_positions (w->current_matrix,
13344 this_line_vpos + 1,
13345 w->current_matrix->nrows,
13346 delta, delta_bytes);
13347 }
13348
13349 /* If this row displays text now but previously didn't,
13350 or vice versa, w->window_end_vpos may have to be
13351 adjusted. */
13352 if ((it.glyph_row - 1)->displays_text_p)
13353 {
13354 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
13355 wset_window_end_vpos (w, make_number (this_line_vpos));
13356 }
13357 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
13358 && this_line_vpos > 0)
13359 wset_window_end_vpos (w, make_number (this_line_vpos - 1));
13360 wset_window_end_valid (w, Qnil);
13361
13362 /* Update hint: No need to try to scroll in update_window. */
13363 w->desired_matrix->no_scrolling_p = 1;
13364
13365 #ifdef GLYPH_DEBUG
13366 *w->desired_matrix->method = 0;
13367 debug_method_add (w, "optimization 1");
13368 #endif
13369 #ifdef HAVE_WINDOW_SYSTEM
13370 update_window_fringes (w, 0);
13371 #endif
13372 goto update;
13373 }
13374 else
13375 goto cancel;
13376 }
13377 else if (/* Cursor position hasn't changed. */
13378 PT == w->last_point
13379 /* Make sure the cursor was last displayed
13380 in this window. Otherwise we have to reposition it. */
13381 && 0 <= w->cursor.vpos
13382 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
13383 {
13384 if (!must_finish)
13385 {
13386 do_pending_window_change (1);
13387 /* If selected_window changed, redisplay again. */
13388 if (WINDOWP (selected_window)
13389 && (w = XWINDOW (selected_window)) != sw)
13390 goto retry;
13391
13392 /* We used to always goto end_of_redisplay here, but this
13393 isn't enough if we have a blinking cursor. */
13394 if (w->cursor_off_p == w->last_cursor_off_p)
13395 goto end_of_redisplay;
13396 }
13397 goto update;
13398 }
13399 /* If highlighting the region, or if the cursor is in the echo area,
13400 then we can't just move the cursor. */
13401 else if (! (!NILP (Vtransient_mark_mode)
13402 && !NILP (BVAR (current_buffer, mark_active)))
13403 && (EQ (selected_window,
13404 BVAR (current_buffer, last_selected_window))
13405 || highlight_nonselected_windows)
13406 && NILP (w->region_showing)
13407 && NILP (Vshow_trailing_whitespace)
13408 && !cursor_in_echo_area)
13409 {
13410 struct it it;
13411 struct glyph_row *row;
13412
13413 /* Skip from tlbufpos to PT and see where it is. Note that
13414 PT may be in invisible text. If so, we will end at the
13415 next visible position. */
13416 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13417 NULL, DEFAULT_FACE_ID);
13418 it.current_x = this_line_start_x;
13419 it.current_y = this_line_y;
13420 it.vpos = this_line_vpos;
13421
13422 /* The call to move_it_to stops in front of PT, but
13423 moves over before-strings. */
13424 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13425
13426 if (it.vpos == this_line_vpos
13427 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13428 row->enabled_p))
13429 {
13430 eassert (this_line_vpos == it.vpos);
13431 eassert (this_line_y == it.current_y);
13432 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13433 #ifdef GLYPH_DEBUG
13434 *w->desired_matrix->method = 0;
13435 debug_method_add (w, "optimization 3");
13436 #endif
13437 goto update;
13438 }
13439 else
13440 goto cancel;
13441 }
13442
13443 cancel:
13444 /* Text changed drastically or point moved off of line. */
13445 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13446 }
13447
13448 CHARPOS (this_line_start_pos) = 0;
13449 consider_all_windows_p |= buffer_shared > 1;
13450 ++clear_face_cache_count;
13451 #ifdef HAVE_WINDOW_SYSTEM
13452 ++clear_image_cache_count;
13453 #endif
13454
13455 /* Build desired matrices, and update the display. If
13456 consider_all_windows_p is non-zero, do it for all windows on all
13457 frames. Otherwise do it for selected_window, only. */
13458
13459 if (consider_all_windows_p)
13460 {
13461 Lisp_Object tail, frame;
13462
13463 FOR_EACH_FRAME (tail, frame)
13464 XFRAME (frame)->updated_p = 0;
13465
13466 /* Recompute # windows showing selected buffer. This will be
13467 incremented each time such a window is displayed. */
13468 buffer_shared = 0;
13469
13470 FOR_EACH_FRAME (tail, frame)
13471 {
13472 struct frame *f = XFRAME (frame);
13473
13474 /* We don't have to do anything for unselected terminal
13475 frames. */
13476 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13477 && !EQ (FRAME_TTY (f)->top_frame, frame))
13478 continue;
13479
13480 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13481 {
13482 if (! EQ (frame, selected_frame))
13483 /* Select the frame, for the sake of frame-local
13484 variables. */
13485 select_frame_for_redisplay (frame);
13486
13487 /* Mark all the scroll bars to be removed; we'll redeem
13488 the ones we want when we redisplay their windows. */
13489 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13490 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13491
13492 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13493 redisplay_windows (FRAME_ROOT_WINDOW (f));
13494
13495 /* The X error handler may have deleted that frame. */
13496 if (!FRAME_LIVE_P (f))
13497 continue;
13498
13499 /* Any scroll bars which redisplay_windows should have
13500 nuked should now go away. */
13501 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13502 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13503
13504 /* If fonts changed, display again. */
13505 /* ??? rms: I suspect it is a mistake to jump all the way
13506 back to retry here. It should just retry this frame. */
13507 if (fonts_changed_p)
13508 goto retry;
13509
13510 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13511 {
13512 /* See if we have to hscroll. */
13513 if (!f->already_hscrolled_p)
13514 {
13515 f->already_hscrolled_p = 1;
13516 if (hscroll_windows (f->root_window))
13517 goto retry;
13518 }
13519
13520 /* Prevent various kinds of signals during display
13521 update. stdio is not robust about handling
13522 signals, which can cause an apparent I/O
13523 error. */
13524 if (interrupt_input)
13525 unrequest_sigio ();
13526 STOP_POLLING;
13527
13528 /* Update the display. */
13529 set_window_update_flags (XWINDOW (f->root_window), 1);
13530 pending |= update_frame (f, 0, 0);
13531 f->updated_p = 1;
13532 }
13533 }
13534 }
13535
13536 if (!EQ (old_frame, selected_frame)
13537 && FRAME_LIVE_P (XFRAME (old_frame)))
13538 /* We played a bit fast-and-loose above and allowed selected_frame
13539 and selected_window to be temporarily out-of-sync but let's make
13540 sure this stays contained. */
13541 select_frame_for_redisplay (old_frame);
13542 eassert (EQ (XFRAME (selected_frame)->selected_window,
13543 selected_window));
13544
13545 if (!pending)
13546 {
13547 /* Do the mark_window_display_accurate after all windows have
13548 been redisplayed because this call resets flags in buffers
13549 which are needed for proper redisplay. */
13550 FOR_EACH_FRAME (tail, frame)
13551 {
13552 struct frame *f = XFRAME (frame);
13553 if (f->updated_p)
13554 {
13555 mark_window_display_accurate (f->root_window, 1);
13556 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13557 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13558 }
13559 }
13560 }
13561 }
13562 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13563 {
13564 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13565 struct frame *mini_frame;
13566
13567 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
13568 /* Use list_of_error, not Qerror, so that
13569 we catch only errors and don't run the debugger. */
13570 internal_condition_case_1 (redisplay_window_1, selected_window,
13571 list_of_error,
13572 redisplay_window_error);
13573 if (update_miniwindow_p)
13574 internal_condition_case_1 (redisplay_window_1, mini_window,
13575 list_of_error,
13576 redisplay_window_error);
13577
13578 /* Compare desired and current matrices, perform output. */
13579
13580 update:
13581 /* If fonts changed, display again. */
13582 if (fonts_changed_p)
13583 goto retry;
13584
13585 /* Prevent various kinds of signals during display update.
13586 stdio is not robust about handling signals,
13587 which can cause an apparent I/O error. */
13588 if (interrupt_input)
13589 unrequest_sigio ();
13590 STOP_POLLING;
13591
13592 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13593 {
13594 if (hscroll_windows (selected_window))
13595 goto retry;
13596
13597 XWINDOW (selected_window)->must_be_updated_p = 1;
13598 pending = update_frame (sf, 0, 0);
13599 }
13600
13601 /* We may have called echo_area_display at the top of this
13602 function. If the echo area is on another frame, that may
13603 have put text on a frame other than the selected one, so the
13604 above call to update_frame would not have caught it. Catch
13605 it here. */
13606 mini_window = FRAME_MINIBUF_WINDOW (sf);
13607 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13608
13609 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13610 {
13611 XWINDOW (mini_window)->must_be_updated_p = 1;
13612 pending |= update_frame (mini_frame, 0, 0);
13613 if (!pending && hscroll_windows (mini_window))
13614 goto retry;
13615 }
13616 }
13617
13618 /* If display was paused because of pending input, make sure we do a
13619 thorough update the next time. */
13620 if (pending)
13621 {
13622 /* Prevent the optimization at the beginning of
13623 redisplay_internal that tries a single-line update of the
13624 line containing the cursor in the selected window. */
13625 CHARPOS (this_line_start_pos) = 0;
13626
13627 /* Let the overlay arrow be updated the next time. */
13628 update_overlay_arrows (0);
13629
13630 /* If we pause after scrolling, some rows in the current
13631 matrices of some windows are not valid. */
13632 if (!WINDOW_FULL_WIDTH_P (w)
13633 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13634 update_mode_lines = 1;
13635 }
13636 else
13637 {
13638 if (!consider_all_windows_p)
13639 {
13640 /* This has already been done above if
13641 consider_all_windows_p is set. */
13642 mark_window_display_accurate_1 (w, 1);
13643
13644 /* Say overlay arrows are up to date. */
13645 update_overlay_arrows (1);
13646
13647 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13648 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13649 }
13650
13651 update_mode_lines = 0;
13652 windows_or_buffers_changed = 0;
13653 cursor_type_changed = 0;
13654 }
13655
13656 /* Start SIGIO interrupts coming again. Having them off during the
13657 code above makes it less likely one will discard output, but not
13658 impossible, since there might be stuff in the system buffer here.
13659 But it is much hairier to try to do anything about that. */
13660 if (interrupt_input)
13661 request_sigio ();
13662 RESUME_POLLING;
13663
13664 /* If a frame has become visible which was not before, redisplay
13665 again, so that we display it. Expose events for such a frame
13666 (which it gets when becoming visible) don't call the parts of
13667 redisplay constructing glyphs, so simply exposing a frame won't
13668 display anything in this case. So, we have to display these
13669 frames here explicitly. */
13670 if (!pending)
13671 {
13672 Lisp_Object tail, frame;
13673 int new_count = 0;
13674
13675 FOR_EACH_FRAME (tail, frame)
13676 {
13677 int this_is_visible = 0;
13678
13679 if (XFRAME (frame)->visible)
13680 this_is_visible = 1;
13681 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
13682 if (XFRAME (frame)->visible)
13683 this_is_visible = 1;
13684
13685 if (this_is_visible)
13686 new_count++;
13687 }
13688
13689 if (new_count != number_of_visible_frames)
13690 windows_or_buffers_changed++;
13691 }
13692
13693 /* Change frame size now if a change is pending. */
13694 do_pending_window_change (1);
13695
13696 /* If we just did a pending size change, or have additional
13697 visible frames, or selected_window changed, redisplay again. */
13698 if ((windows_or_buffers_changed && !pending)
13699 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13700 goto retry;
13701
13702 /* Clear the face and image caches.
13703
13704 We used to do this only if consider_all_windows_p. But the cache
13705 needs to be cleared if a timer creates images in the current
13706 buffer (e.g. the test case in Bug#6230). */
13707
13708 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13709 {
13710 clear_face_cache (0);
13711 clear_face_cache_count = 0;
13712 }
13713
13714 #ifdef HAVE_WINDOW_SYSTEM
13715 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13716 {
13717 clear_image_caches (Qnil);
13718 clear_image_cache_count = 0;
13719 }
13720 #endif /* HAVE_WINDOW_SYSTEM */
13721
13722 end_of_redisplay:
13723 backtrace_list = backtrace.next;
13724 unbind_to (count, Qnil);
13725 RESUME_POLLING;
13726 }
13727
13728
13729 /* Redisplay, but leave alone any recent echo area message unless
13730 another message has been requested in its place.
13731
13732 This is useful in situations where you need to redisplay but no
13733 user action has occurred, making it inappropriate for the message
13734 area to be cleared. See tracking_off and
13735 wait_reading_process_output for examples of these situations.
13736
13737 FROM_WHERE is an integer saying from where this function was
13738 called. This is useful for debugging. */
13739
13740 void
13741 redisplay_preserve_echo_area (int from_where)
13742 {
13743 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13744
13745 if (!NILP (echo_area_buffer[1]))
13746 {
13747 /* We have a previously displayed message, but no current
13748 message. Redisplay the previous message. */
13749 display_last_displayed_message_p = 1;
13750 redisplay_internal ();
13751 display_last_displayed_message_p = 0;
13752 }
13753 else
13754 redisplay_internal ();
13755
13756 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13757 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13758 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13759 }
13760
13761
13762 /* Function registered with record_unwind_protect in redisplay_internal.
13763 Clear redisplaying_p. Also, select the previously
13764 selected frame, unless it has been deleted (by an X connection
13765 failure during redisplay, for example). */
13766
13767 static Lisp_Object
13768 unwind_redisplay (Lisp_Object old_frame)
13769 {
13770 redisplaying_p = 0;
13771 if (! EQ (old_frame, selected_frame)
13772 && FRAME_LIVE_P (XFRAME (old_frame)))
13773 select_frame_for_redisplay (old_frame);
13774 return Qnil;
13775 }
13776
13777
13778 /* Mark the display of window W as accurate or inaccurate. If
13779 ACCURATE_P is non-zero mark display of W as accurate. If
13780 ACCURATE_P is zero, arrange for W to be redisplayed the next time
13781 redisplay_internal is called. */
13782
13783 static void
13784 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13785 {
13786 if (BUFFERP (w->buffer))
13787 {
13788 struct buffer *b = XBUFFER (w->buffer);
13789
13790 w->last_modified = accurate_p ? BUF_MODIFF(b) : 0;
13791 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF(b) : 0;
13792 w->last_had_star
13793 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13794
13795 if (accurate_p)
13796 {
13797 b->clip_changed = 0;
13798 b->prevent_redisplay_optimizations_p = 0;
13799
13800 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13801 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13802 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13803 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13804
13805 w->current_matrix->buffer = b;
13806 w->current_matrix->begv = BUF_BEGV (b);
13807 w->current_matrix->zv = BUF_ZV (b);
13808
13809 w->last_cursor = w->cursor;
13810 w->last_cursor_off_p = w->cursor_off_p;
13811
13812 if (w == XWINDOW (selected_window))
13813 w->last_point = BUF_PT (b);
13814 else
13815 w->last_point = XMARKER (w->pointm)->charpos;
13816 }
13817 }
13818
13819 if (accurate_p)
13820 {
13821 wset_window_end_valid (w, w->buffer);
13822 w->update_mode_line = 0;
13823 }
13824 }
13825
13826
13827 /* Mark the display of windows in the window tree rooted at WINDOW as
13828 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13829 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13830 be redisplayed the next time redisplay_internal is called. */
13831
13832 void
13833 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13834 {
13835 struct window *w;
13836
13837 for (; !NILP (window); window = w->next)
13838 {
13839 w = XWINDOW (window);
13840 mark_window_display_accurate_1 (w, accurate_p);
13841
13842 if (!NILP (w->vchild))
13843 mark_window_display_accurate (w->vchild, accurate_p);
13844 if (!NILP (w->hchild))
13845 mark_window_display_accurate (w->hchild, accurate_p);
13846 }
13847
13848 if (accurate_p)
13849 {
13850 update_overlay_arrows (1);
13851 }
13852 else
13853 {
13854 /* Force a thorough redisplay the next time by setting
13855 last_arrow_position and last_arrow_string to t, which is
13856 unequal to any useful value of Voverlay_arrow_... */
13857 update_overlay_arrows (-1);
13858 }
13859 }
13860
13861
13862 /* Return value in display table DP (Lisp_Char_Table *) for character
13863 C. Since a display table doesn't have any parent, we don't have to
13864 follow parent. Do not call this function directly but use the
13865 macro DISP_CHAR_VECTOR. */
13866
13867 Lisp_Object
13868 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13869 {
13870 Lisp_Object val;
13871
13872 if (ASCII_CHAR_P (c))
13873 {
13874 val = dp->ascii;
13875 if (SUB_CHAR_TABLE_P (val))
13876 val = XSUB_CHAR_TABLE (val)->contents[c];
13877 }
13878 else
13879 {
13880 Lisp_Object table;
13881
13882 XSETCHAR_TABLE (table, dp);
13883 val = char_table_ref (table, c);
13884 }
13885 if (NILP (val))
13886 val = dp->defalt;
13887 return val;
13888 }
13889
13890
13891 \f
13892 /***********************************************************************
13893 Window Redisplay
13894 ***********************************************************************/
13895
13896 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13897
13898 static void
13899 redisplay_windows (Lisp_Object window)
13900 {
13901 while (!NILP (window))
13902 {
13903 struct window *w = XWINDOW (window);
13904
13905 if (!NILP (w->hchild))
13906 redisplay_windows (w->hchild);
13907 else if (!NILP (w->vchild))
13908 redisplay_windows (w->vchild);
13909 else if (!NILP (w->buffer))
13910 {
13911 displayed_buffer = XBUFFER (w->buffer);
13912 /* Use list_of_error, not Qerror, so that
13913 we catch only errors and don't run the debugger. */
13914 internal_condition_case_1 (redisplay_window_0, window,
13915 list_of_error,
13916 redisplay_window_error);
13917 }
13918
13919 window = w->next;
13920 }
13921 }
13922
13923 static Lisp_Object
13924 redisplay_window_error (Lisp_Object ignore)
13925 {
13926 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13927 return Qnil;
13928 }
13929
13930 static Lisp_Object
13931 redisplay_window_0 (Lisp_Object window)
13932 {
13933 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13934 redisplay_window (window, 0);
13935 return Qnil;
13936 }
13937
13938 static Lisp_Object
13939 redisplay_window_1 (Lisp_Object window)
13940 {
13941 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13942 redisplay_window (window, 1);
13943 return Qnil;
13944 }
13945 \f
13946
13947 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13948 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13949 which positions recorded in ROW differ from current buffer
13950 positions.
13951
13952 Return 0 if cursor is not on this row, 1 otherwise. */
13953
13954 static int
13955 set_cursor_from_row (struct window *w, struct glyph_row *row,
13956 struct glyph_matrix *matrix,
13957 ptrdiff_t delta, ptrdiff_t delta_bytes,
13958 int dy, int dvpos)
13959 {
13960 struct glyph *glyph = row->glyphs[TEXT_AREA];
13961 struct glyph *end = glyph + row->used[TEXT_AREA];
13962 struct glyph *cursor = NULL;
13963 /* The last known character position in row. */
13964 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13965 int x = row->x;
13966 ptrdiff_t pt_old = PT - delta;
13967 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13968 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13969 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13970 /* A glyph beyond the edge of TEXT_AREA which we should never
13971 touch. */
13972 struct glyph *glyphs_end = end;
13973 /* Non-zero means we've found a match for cursor position, but that
13974 glyph has the avoid_cursor_p flag set. */
13975 int match_with_avoid_cursor = 0;
13976 /* Non-zero means we've seen at least one glyph that came from a
13977 display string. */
13978 int string_seen = 0;
13979 /* Largest and smallest buffer positions seen so far during scan of
13980 glyph row. */
13981 ptrdiff_t bpos_max = pos_before;
13982 ptrdiff_t bpos_min = pos_after;
13983 /* Last buffer position covered by an overlay string with an integer
13984 `cursor' property. */
13985 ptrdiff_t bpos_covered = 0;
13986 /* Non-zero means the display string on which to display the cursor
13987 comes from a text property, not from an overlay. */
13988 int string_from_text_prop = 0;
13989
13990 /* Don't even try doing anything if called for a mode-line or
13991 header-line row, since the rest of the code isn't prepared to
13992 deal with such calamities. */
13993 eassert (!row->mode_line_p);
13994 if (row->mode_line_p)
13995 return 0;
13996
13997 /* Skip over glyphs not having an object at the start and the end of
13998 the row. These are special glyphs like truncation marks on
13999 terminal frames. */
14000 if (row->displays_text_p)
14001 {
14002 if (!row->reversed_p)
14003 {
14004 while (glyph < end
14005 && INTEGERP (glyph->object)
14006 && glyph->charpos < 0)
14007 {
14008 x += glyph->pixel_width;
14009 ++glyph;
14010 }
14011 while (end > glyph
14012 && INTEGERP ((end - 1)->object)
14013 /* CHARPOS is zero for blanks and stretch glyphs
14014 inserted by extend_face_to_end_of_line. */
14015 && (end - 1)->charpos <= 0)
14016 --end;
14017 glyph_before = glyph - 1;
14018 glyph_after = end;
14019 }
14020 else
14021 {
14022 struct glyph *g;
14023
14024 /* If the glyph row is reversed, we need to process it from back
14025 to front, so swap the edge pointers. */
14026 glyphs_end = end = glyph - 1;
14027 glyph += row->used[TEXT_AREA] - 1;
14028
14029 while (glyph > end + 1
14030 && INTEGERP (glyph->object)
14031 && glyph->charpos < 0)
14032 {
14033 --glyph;
14034 x -= glyph->pixel_width;
14035 }
14036 if (INTEGERP (glyph->object) && glyph->charpos < 0)
14037 --glyph;
14038 /* By default, in reversed rows we put the cursor on the
14039 rightmost (first in the reading order) glyph. */
14040 for (g = end + 1; g < glyph; g++)
14041 x += g->pixel_width;
14042 while (end < glyph
14043 && INTEGERP ((end + 1)->object)
14044 && (end + 1)->charpos <= 0)
14045 ++end;
14046 glyph_before = glyph + 1;
14047 glyph_after = end;
14048 }
14049 }
14050 else if (row->reversed_p)
14051 {
14052 /* In R2L rows that don't display text, put the cursor on the
14053 rightmost glyph. Case in point: an empty last line that is
14054 part of an R2L paragraph. */
14055 cursor = end - 1;
14056 /* Avoid placing the cursor on the last glyph of the row, where
14057 on terminal frames we hold the vertical border between
14058 adjacent windows. */
14059 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14060 && !WINDOW_RIGHTMOST_P (w)
14061 && cursor == row->glyphs[LAST_AREA] - 1)
14062 cursor--;
14063 x = -1; /* will be computed below, at label compute_x */
14064 }
14065
14066 /* Step 1: Try to find the glyph whose character position
14067 corresponds to point. If that's not possible, find 2 glyphs
14068 whose character positions are the closest to point, one before
14069 point, the other after it. */
14070 if (!row->reversed_p)
14071 while (/* not marched to end of glyph row */
14072 glyph < end
14073 /* glyph was not inserted by redisplay for internal purposes */
14074 && !INTEGERP (glyph->object))
14075 {
14076 if (BUFFERP (glyph->object))
14077 {
14078 ptrdiff_t dpos = glyph->charpos - pt_old;
14079
14080 if (glyph->charpos > bpos_max)
14081 bpos_max = glyph->charpos;
14082 if (glyph->charpos < bpos_min)
14083 bpos_min = glyph->charpos;
14084 if (!glyph->avoid_cursor_p)
14085 {
14086 /* If we hit point, we've found the glyph on which to
14087 display the cursor. */
14088 if (dpos == 0)
14089 {
14090 match_with_avoid_cursor = 0;
14091 break;
14092 }
14093 /* See if we've found a better approximation to
14094 POS_BEFORE or to POS_AFTER. */
14095 if (0 > dpos && dpos > pos_before - pt_old)
14096 {
14097 pos_before = glyph->charpos;
14098 glyph_before = glyph;
14099 }
14100 else if (0 < dpos && dpos < pos_after - pt_old)
14101 {
14102 pos_after = glyph->charpos;
14103 glyph_after = glyph;
14104 }
14105 }
14106 else if (dpos == 0)
14107 match_with_avoid_cursor = 1;
14108 }
14109 else if (STRINGP (glyph->object))
14110 {
14111 Lisp_Object chprop;
14112 ptrdiff_t glyph_pos = glyph->charpos;
14113
14114 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14115 glyph->object);
14116 if (!NILP (chprop))
14117 {
14118 /* If the string came from a `display' text property,
14119 look up the buffer position of that property and
14120 use that position to update bpos_max, as if we
14121 actually saw such a position in one of the row's
14122 glyphs. This helps with supporting integer values
14123 of `cursor' property on the display string in
14124 situations where most or all of the row's buffer
14125 text is completely covered by display properties,
14126 so that no glyph with valid buffer positions is
14127 ever seen in the row. */
14128 ptrdiff_t prop_pos =
14129 string_buffer_position_lim (glyph->object, pos_before,
14130 pos_after, 0);
14131
14132 if (prop_pos >= pos_before)
14133 bpos_max = prop_pos - 1;
14134 }
14135 if (INTEGERP (chprop))
14136 {
14137 bpos_covered = bpos_max + XINT (chprop);
14138 /* If the `cursor' property covers buffer positions up
14139 to and including point, we should display cursor on
14140 this glyph. Note that, if a `cursor' property on one
14141 of the string's characters has an integer value, we
14142 will break out of the loop below _before_ we get to
14143 the position match above. IOW, integer values of
14144 the `cursor' property override the "exact match for
14145 point" strategy of positioning the cursor. */
14146 /* Implementation note: bpos_max == pt_old when, e.g.,
14147 we are in an empty line, where bpos_max is set to
14148 MATRIX_ROW_START_CHARPOS, see above. */
14149 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14150 {
14151 cursor = glyph;
14152 break;
14153 }
14154 }
14155
14156 string_seen = 1;
14157 }
14158 x += glyph->pixel_width;
14159 ++glyph;
14160 }
14161 else if (glyph > end) /* row is reversed */
14162 while (!INTEGERP (glyph->object))
14163 {
14164 if (BUFFERP (glyph->object))
14165 {
14166 ptrdiff_t dpos = glyph->charpos - pt_old;
14167
14168 if (glyph->charpos > bpos_max)
14169 bpos_max = glyph->charpos;
14170 if (glyph->charpos < bpos_min)
14171 bpos_min = glyph->charpos;
14172 if (!glyph->avoid_cursor_p)
14173 {
14174 if (dpos == 0)
14175 {
14176 match_with_avoid_cursor = 0;
14177 break;
14178 }
14179 if (0 > dpos && dpos > pos_before - pt_old)
14180 {
14181 pos_before = glyph->charpos;
14182 glyph_before = glyph;
14183 }
14184 else if (0 < dpos && dpos < pos_after - pt_old)
14185 {
14186 pos_after = glyph->charpos;
14187 glyph_after = glyph;
14188 }
14189 }
14190 else if (dpos == 0)
14191 match_with_avoid_cursor = 1;
14192 }
14193 else if (STRINGP (glyph->object))
14194 {
14195 Lisp_Object chprop;
14196 ptrdiff_t glyph_pos = glyph->charpos;
14197
14198 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14199 glyph->object);
14200 if (!NILP (chprop))
14201 {
14202 ptrdiff_t prop_pos =
14203 string_buffer_position_lim (glyph->object, pos_before,
14204 pos_after, 0);
14205
14206 if (prop_pos >= pos_before)
14207 bpos_max = prop_pos - 1;
14208 }
14209 if (INTEGERP (chprop))
14210 {
14211 bpos_covered = bpos_max + XINT (chprop);
14212 /* If the `cursor' property covers buffer positions up
14213 to and including point, we should display cursor on
14214 this glyph. */
14215 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14216 {
14217 cursor = glyph;
14218 break;
14219 }
14220 }
14221 string_seen = 1;
14222 }
14223 --glyph;
14224 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14225 {
14226 x--; /* can't use any pixel_width */
14227 break;
14228 }
14229 x -= glyph->pixel_width;
14230 }
14231
14232 /* Step 2: If we didn't find an exact match for point, we need to
14233 look for a proper place to put the cursor among glyphs between
14234 GLYPH_BEFORE and GLYPH_AFTER. */
14235 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14236 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14237 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14238 {
14239 /* An empty line has a single glyph whose OBJECT is zero and
14240 whose CHARPOS is the position of a newline on that line.
14241 Note that on a TTY, there are more glyphs after that, which
14242 were produced by extend_face_to_end_of_line, but their
14243 CHARPOS is zero or negative. */
14244 int empty_line_p =
14245 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14246 && INTEGERP (glyph->object) && glyph->charpos > 0
14247 /* On a TTY, continued and truncated rows also have a glyph at
14248 their end whose OBJECT is zero and whose CHARPOS is
14249 positive (the continuation and truncation glyphs), but such
14250 rows are obviously not "empty". */
14251 && !(row->continued_p || row->truncated_on_right_p);
14252
14253 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14254 {
14255 ptrdiff_t ellipsis_pos;
14256
14257 /* Scan back over the ellipsis glyphs. */
14258 if (!row->reversed_p)
14259 {
14260 ellipsis_pos = (glyph - 1)->charpos;
14261 while (glyph > row->glyphs[TEXT_AREA]
14262 && (glyph - 1)->charpos == ellipsis_pos)
14263 glyph--, x -= glyph->pixel_width;
14264 /* That loop always goes one position too far, including
14265 the glyph before the ellipsis. So scan forward over
14266 that one. */
14267 x += glyph->pixel_width;
14268 glyph++;
14269 }
14270 else /* row is reversed */
14271 {
14272 ellipsis_pos = (glyph + 1)->charpos;
14273 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14274 && (glyph + 1)->charpos == ellipsis_pos)
14275 glyph++, x += glyph->pixel_width;
14276 x -= glyph->pixel_width;
14277 glyph--;
14278 }
14279 }
14280 else if (match_with_avoid_cursor)
14281 {
14282 cursor = glyph_after;
14283 x = -1;
14284 }
14285 else if (string_seen)
14286 {
14287 int incr = row->reversed_p ? -1 : +1;
14288
14289 /* Need to find the glyph that came out of a string which is
14290 present at point. That glyph is somewhere between
14291 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14292 positioned between POS_BEFORE and POS_AFTER in the
14293 buffer. */
14294 struct glyph *start, *stop;
14295 ptrdiff_t pos = pos_before;
14296
14297 x = -1;
14298
14299 /* If the row ends in a newline from a display string,
14300 reordering could have moved the glyphs belonging to the
14301 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14302 in this case we extend the search to the last glyph in
14303 the row that was not inserted by redisplay. */
14304 if (row->ends_in_newline_from_string_p)
14305 {
14306 glyph_after = end;
14307 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14308 }
14309
14310 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14311 correspond to POS_BEFORE and POS_AFTER, respectively. We
14312 need START and STOP in the order that corresponds to the
14313 row's direction as given by its reversed_p flag. If the
14314 directionality of characters between POS_BEFORE and
14315 POS_AFTER is the opposite of the row's base direction,
14316 these characters will have been reordered for display,
14317 and we need to reverse START and STOP. */
14318 if (!row->reversed_p)
14319 {
14320 start = min (glyph_before, glyph_after);
14321 stop = max (glyph_before, glyph_after);
14322 }
14323 else
14324 {
14325 start = max (glyph_before, glyph_after);
14326 stop = min (glyph_before, glyph_after);
14327 }
14328 for (glyph = start + incr;
14329 row->reversed_p ? glyph > stop : glyph < stop; )
14330 {
14331
14332 /* Any glyphs that come from the buffer are here because
14333 of bidi reordering. Skip them, and only pay
14334 attention to glyphs that came from some string. */
14335 if (STRINGP (glyph->object))
14336 {
14337 Lisp_Object str;
14338 ptrdiff_t tem;
14339 /* If the display property covers the newline, we
14340 need to search for it one position farther. */
14341 ptrdiff_t lim = pos_after
14342 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14343
14344 string_from_text_prop = 0;
14345 str = glyph->object;
14346 tem = string_buffer_position_lim (str, pos, lim, 0);
14347 if (tem == 0 /* from overlay */
14348 || pos <= tem)
14349 {
14350 /* If the string from which this glyph came is
14351 found in the buffer at point, or at position
14352 that is closer to point than pos_after, then
14353 we've found the glyph we've been looking for.
14354 If it comes from an overlay (tem == 0), and
14355 it has the `cursor' property on one of its
14356 glyphs, record that glyph as a candidate for
14357 displaying the cursor. (As in the
14358 unidirectional version, we will display the
14359 cursor on the last candidate we find.) */
14360 if (tem == 0
14361 || tem == pt_old
14362 || (tem - pt_old > 0 && tem < pos_after))
14363 {
14364 /* The glyphs from this string could have
14365 been reordered. Find the one with the
14366 smallest string position. Or there could
14367 be a character in the string with the
14368 `cursor' property, which means display
14369 cursor on that character's glyph. */
14370 ptrdiff_t strpos = glyph->charpos;
14371
14372 if (tem)
14373 {
14374 cursor = glyph;
14375 string_from_text_prop = 1;
14376 }
14377 for ( ;
14378 (row->reversed_p ? glyph > stop : glyph < stop)
14379 && EQ (glyph->object, str);
14380 glyph += incr)
14381 {
14382 Lisp_Object cprop;
14383 ptrdiff_t gpos = glyph->charpos;
14384
14385 cprop = Fget_char_property (make_number (gpos),
14386 Qcursor,
14387 glyph->object);
14388 if (!NILP (cprop))
14389 {
14390 cursor = glyph;
14391 break;
14392 }
14393 if (tem && glyph->charpos < strpos)
14394 {
14395 strpos = glyph->charpos;
14396 cursor = glyph;
14397 }
14398 }
14399
14400 if (tem == pt_old
14401 || (tem - pt_old > 0 && tem < pos_after))
14402 goto compute_x;
14403 }
14404 if (tem)
14405 pos = tem + 1; /* don't find previous instances */
14406 }
14407 /* This string is not what we want; skip all of the
14408 glyphs that came from it. */
14409 while ((row->reversed_p ? glyph > stop : glyph < stop)
14410 && EQ (glyph->object, str))
14411 glyph += incr;
14412 }
14413 else
14414 glyph += incr;
14415 }
14416
14417 /* If we reached the end of the line, and END was from a string,
14418 the cursor is not on this line. */
14419 if (cursor == NULL
14420 && (row->reversed_p ? glyph <= end : glyph >= end)
14421 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14422 && STRINGP (end->object)
14423 && row->continued_p)
14424 return 0;
14425 }
14426 /* A truncated row may not include PT among its character positions.
14427 Setting the cursor inside the scroll margin will trigger
14428 recalculation of hscroll in hscroll_window_tree. But if a
14429 display string covers point, defer to the string-handling
14430 code below to figure this out. */
14431 else if (row->truncated_on_left_p && pt_old < bpos_min)
14432 {
14433 cursor = glyph_before;
14434 x = -1;
14435 }
14436 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14437 /* Zero-width characters produce no glyphs. */
14438 || (!empty_line_p
14439 && (row->reversed_p
14440 ? glyph_after > glyphs_end
14441 : glyph_after < glyphs_end)))
14442 {
14443 cursor = glyph_after;
14444 x = -1;
14445 }
14446 }
14447
14448 compute_x:
14449 if (cursor != NULL)
14450 glyph = cursor;
14451 else if (glyph == glyphs_end
14452 && pos_before == pos_after
14453 && STRINGP ((row->reversed_p
14454 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14455 : row->glyphs[TEXT_AREA])->object))
14456 {
14457 /* If all the glyphs of this row came from strings, put the
14458 cursor on the first glyph of the row. This avoids having the
14459 cursor outside of the text area in this very rare and hard
14460 use case. */
14461 glyph =
14462 row->reversed_p
14463 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14464 : row->glyphs[TEXT_AREA];
14465 }
14466 if (x < 0)
14467 {
14468 struct glyph *g;
14469
14470 /* Need to compute x that corresponds to GLYPH. */
14471 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14472 {
14473 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14474 emacs_abort ();
14475 x += g->pixel_width;
14476 }
14477 }
14478
14479 /* ROW could be part of a continued line, which, under bidi
14480 reordering, might have other rows whose start and end charpos
14481 occlude point. Only set w->cursor if we found a better
14482 approximation to the cursor position than we have from previously
14483 examined candidate rows belonging to the same continued line. */
14484 if (/* we already have a candidate row */
14485 w->cursor.vpos >= 0
14486 /* that candidate is not the row we are processing */
14487 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14488 /* Make sure cursor.vpos specifies a row whose start and end
14489 charpos occlude point, and it is valid candidate for being a
14490 cursor-row. This is because some callers of this function
14491 leave cursor.vpos at the row where the cursor was displayed
14492 during the last redisplay cycle. */
14493 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14494 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14495 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14496 {
14497 struct glyph *g1 =
14498 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14499
14500 /* Don't consider glyphs that are outside TEXT_AREA. */
14501 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14502 return 0;
14503 /* Keep the candidate whose buffer position is the closest to
14504 point or has the `cursor' property. */
14505 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14506 w->cursor.hpos >= 0
14507 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14508 && ((BUFFERP (g1->object)
14509 && (g1->charpos == pt_old /* an exact match always wins */
14510 || (BUFFERP (glyph->object)
14511 && eabs (g1->charpos - pt_old)
14512 < eabs (glyph->charpos - pt_old))))
14513 /* previous candidate is a glyph from a string that has
14514 a non-nil `cursor' property */
14515 || (STRINGP (g1->object)
14516 && (!NILP (Fget_char_property (make_number (g1->charpos),
14517 Qcursor, g1->object))
14518 /* previous candidate is from the same display
14519 string as this one, and the display string
14520 came from a text property */
14521 || (EQ (g1->object, glyph->object)
14522 && string_from_text_prop)
14523 /* this candidate is from newline and its
14524 position is not an exact match */
14525 || (INTEGERP (glyph->object)
14526 && glyph->charpos != pt_old)))))
14527 return 0;
14528 /* If this candidate gives an exact match, use that. */
14529 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14530 /* If this candidate is a glyph created for the
14531 terminating newline of a line, and point is on that
14532 newline, it wins because it's an exact match. */
14533 || (!row->continued_p
14534 && INTEGERP (glyph->object)
14535 && glyph->charpos == 0
14536 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14537 /* Otherwise, keep the candidate that comes from a row
14538 spanning less buffer positions. This may win when one or
14539 both candidate positions are on glyphs that came from
14540 display strings, for which we cannot compare buffer
14541 positions. */
14542 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14543 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14544 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14545 return 0;
14546 }
14547 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14548 w->cursor.x = x;
14549 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14550 w->cursor.y = row->y + dy;
14551
14552 if (w == XWINDOW (selected_window))
14553 {
14554 if (!row->continued_p
14555 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14556 && row->x == 0)
14557 {
14558 this_line_buffer = XBUFFER (w->buffer);
14559
14560 CHARPOS (this_line_start_pos)
14561 = MATRIX_ROW_START_CHARPOS (row) + delta;
14562 BYTEPOS (this_line_start_pos)
14563 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14564
14565 CHARPOS (this_line_end_pos)
14566 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14567 BYTEPOS (this_line_end_pos)
14568 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14569
14570 this_line_y = w->cursor.y;
14571 this_line_pixel_height = row->height;
14572 this_line_vpos = w->cursor.vpos;
14573 this_line_start_x = row->x;
14574 }
14575 else
14576 CHARPOS (this_line_start_pos) = 0;
14577 }
14578
14579 return 1;
14580 }
14581
14582
14583 /* Run window scroll functions, if any, for WINDOW with new window
14584 start STARTP. Sets the window start of WINDOW to that position.
14585
14586 We assume that the window's buffer is really current. */
14587
14588 static struct text_pos
14589 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14590 {
14591 struct window *w = XWINDOW (window);
14592 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14593
14594 if (current_buffer != XBUFFER (w->buffer))
14595 emacs_abort ();
14596
14597 if (!NILP (Vwindow_scroll_functions))
14598 {
14599 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14600 make_number (CHARPOS (startp)));
14601 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14602 /* In case the hook functions switch buffers. */
14603 set_buffer_internal (XBUFFER (w->buffer));
14604 }
14605
14606 return startp;
14607 }
14608
14609
14610 /* Make sure the line containing the cursor is fully visible.
14611 A value of 1 means there is nothing to be done.
14612 (Either the line is fully visible, or it cannot be made so,
14613 or we cannot tell.)
14614
14615 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14616 is higher than window.
14617
14618 A value of 0 means the caller should do scrolling
14619 as if point had gone off the screen. */
14620
14621 static int
14622 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14623 {
14624 struct glyph_matrix *matrix;
14625 struct glyph_row *row;
14626 int window_height;
14627
14628 if (!make_cursor_line_fully_visible_p)
14629 return 1;
14630
14631 /* It's not always possible to find the cursor, e.g, when a window
14632 is full of overlay strings. Don't do anything in that case. */
14633 if (w->cursor.vpos < 0)
14634 return 1;
14635
14636 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14637 row = MATRIX_ROW (matrix, w->cursor.vpos);
14638
14639 /* If the cursor row is not partially visible, there's nothing to do. */
14640 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14641 return 1;
14642
14643 /* If the row the cursor is in is taller than the window's height,
14644 it's not clear what to do, so do nothing. */
14645 window_height = window_box_height (w);
14646 if (row->height >= window_height)
14647 {
14648 if (!force_p || MINI_WINDOW_P (w)
14649 || w->vscroll || w->cursor.vpos == 0)
14650 return 1;
14651 }
14652 return 0;
14653 }
14654
14655
14656 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14657 non-zero means only WINDOW is redisplayed in redisplay_internal.
14658 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14659 in redisplay_window to bring a partially visible line into view in
14660 the case that only the cursor has moved.
14661
14662 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14663 last screen line's vertical height extends past the end of the screen.
14664
14665 Value is
14666
14667 1 if scrolling succeeded
14668
14669 0 if scrolling didn't find point.
14670
14671 -1 if new fonts have been loaded so that we must interrupt
14672 redisplay, adjust glyph matrices, and try again. */
14673
14674 enum
14675 {
14676 SCROLLING_SUCCESS,
14677 SCROLLING_FAILED,
14678 SCROLLING_NEED_LARGER_MATRICES
14679 };
14680
14681 /* If scroll-conservatively is more than this, never recenter.
14682
14683 If you change this, don't forget to update the doc string of
14684 `scroll-conservatively' and the Emacs manual. */
14685 #define SCROLL_LIMIT 100
14686
14687 static int
14688 try_scrolling (Lisp_Object window, int just_this_one_p,
14689 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14690 int temp_scroll_step, int last_line_misfit)
14691 {
14692 struct window *w = XWINDOW (window);
14693 struct frame *f = XFRAME (w->frame);
14694 struct text_pos pos, startp;
14695 struct it it;
14696 int this_scroll_margin, scroll_max, rc, height;
14697 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14698 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14699 Lisp_Object aggressive;
14700 /* We will never try scrolling more than this number of lines. */
14701 int scroll_limit = SCROLL_LIMIT;
14702
14703 #ifdef GLYPH_DEBUG
14704 debug_method_add (w, "try_scrolling");
14705 #endif
14706
14707 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14708
14709 /* Compute scroll margin height in pixels. We scroll when point is
14710 within this distance from the top or bottom of the window. */
14711 if (scroll_margin > 0)
14712 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14713 * FRAME_LINE_HEIGHT (f);
14714 else
14715 this_scroll_margin = 0;
14716
14717 /* Force arg_scroll_conservatively to have a reasonable value, to
14718 avoid scrolling too far away with slow move_it_* functions. Note
14719 that the user can supply scroll-conservatively equal to
14720 `most-positive-fixnum', which can be larger than INT_MAX. */
14721 if (arg_scroll_conservatively > scroll_limit)
14722 {
14723 arg_scroll_conservatively = scroll_limit + 1;
14724 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
14725 }
14726 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14727 /* Compute how much we should try to scroll maximally to bring
14728 point into view. */
14729 scroll_max = (max (scroll_step,
14730 max (arg_scroll_conservatively, temp_scroll_step))
14731 * FRAME_LINE_HEIGHT (f));
14732 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14733 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14734 /* We're trying to scroll because of aggressive scrolling but no
14735 scroll_step is set. Choose an arbitrary one. */
14736 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
14737 else
14738 scroll_max = 0;
14739
14740 too_near_end:
14741
14742 /* Decide whether to scroll down. */
14743 if (PT > CHARPOS (startp))
14744 {
14745 int scroll_margin_y;
14746
14747 /* Compute the pixel ypos of the scroll margin, then move IT to
14748 either that ypos or PT, whichever comes first. */
14749 start_display (&it, w, startp);
14750 scroll_margin_y = it.last_visible_y - this_scroll_margin
14751 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
14752 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14753 (MOVE_TO_POS | MOVE_TO_Y));
14754
14755 if (PT > CHARPOS (it.current.pos))
14756 {
14757 int y0 = line_bottom_y (&it);
14758 /* Compute how many pixels below window bottom to stop searching
14759 for PT. This avoids costly search for PT that is far away if
14760 the user limited scrolling by a small number of lines, but
14761 always finds PT if scroll_conservatively is set to a large
14762 number, such as most-positive-fixnum. */
14763 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
14764 int y_to_move = it.last_visible_y + slack;
14765
14766 /* Compute the distance from the scroll margin to PT or to
14767 the scroll limit, whichever comes first. This should
14768 include the height of the cursor line, to make that line
14769 fully visible. */
14770 move_it_to (&it, PT, -1, y_to_move,
14771 -1, MOVE_TO_POS | MOVE_TO_Y);
14772 dy = line_bottom_y (&it) - y0;
14773
14774 if (dy > scroll_max)
14775 return SCROLLING_FAILED;
14776
14777 if (dy > 0)
14778 scroll_down_p = 1;
14779 }
14780 }
14781
14782 if (scroll_down_p)
14783 {
14784 /* Point is in or below the bottom scroll margin, so move the
14785 window start down. If scrolling conservatively, move it just
14786 enough down to make point visible. If scroll_step is set,
14787 move it down by scroll_step. */
14788 if (arg_scroll_conservatively)
14789 amount_to_scroll
14790 = min (max (dy, FRAME_LINE_HEIGHT (f)),
14791 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
14792 else if (scroll_step || temp_scroll_step)
14793 amount_to_scroll = scroll_max;
14794 else
14795 {
14796 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14797 height = WINDOW_BOX_TEXT_HEIGHT (w);
14798 if (NUMBERP (aggressive))
14799 {
14800 double float_amount = XFLOATINT (aggressive) * height;
14801 int aggressive_scroll = float_amount;
14802 if (aggressive_scroll == 0 && float_amount > 0)
14803 aggressive_scroll = 1;
14804 /* Don't let point enter the scroll margin near top of
14805 the window. This could happen if the value of
14806 scroll_up_aggressively is too large and there are
14807 non-zero margins, because scroll_up_aggressively
14808 means put point that fraction of window height
14809 _from_the_bottom_margin_. */
14810 if (aggressive_scroll + 2*this_scroll_margin > height)
14811 aggressive_scroll = height - 2*this_scroll_margin;
14812 amount_to_scroll = dy + aggressive_scroll;
14813 }
14814 }
14815
14816 if (amount_to_scroll <= 0)
14817 return SCROLLING_FAILED;
14818
14819 start_display (&it, w, startp);
14820 if (arg_scroll_conservatively <= scroll_limit)
14821 move_it_vertically (&it, amount_to_scroll);
14822 else
14823 {
14824 /* Extra precision for users who set scroll-conservatively
14825 to a large number: make sure the amount we scroll
14826 the window start is never less than amount_to_scroll,
14827 which was computed as distance from window bottom to
14828 point. This matters when lines at window top and lines
14829 below window bottom have different height. */
14830 struct it it1;
14831 void *it1data = NULL;
14832 /* We use a temporary it1 because line_bottom_y can modify
14833 its argument, if it moves one line down; see there. */
14834 int start_y;
14835
14836 SAVE_IT (it1, it, it1data);
14837 start_y = line_bottom_y (&it1);
14838 do {
14839 RESTORE_IT (&it, &it, it1data);
14840 move_it_by_lines (&it, 1);
14841 SAVE_IT (it1, it, it1data);
14842 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14843 }
14844
14845 /* If STARTP is unchanged, move it down another screen line. */
14846 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14847 move_it_by_lines (&it, 1);
14848 startp = it.current.pos;
14849 }
14850 else
14851 {
14852 struct text_pos scroll_margin_pos = startp;
14853
14854 /* See if point is inside the scroll margin at the top of the
14855 window. */
14856 if (this_scroll_margin)
14857 {
14858 start_display (&it, w, startp);
14859 move_it_vertically (&it, this_scroll_margin);
14860 scroll_margin_pos = it.current.pos;
14861 }
14862
14863 if (PT < CHARPOS (scroll_margin_pos))
14864 {
14865 /* Point is in the scroll margin at the top of the window or
14866 above what is displayed in the window. */
14867 int y0, y_to_move;
14868
14869 /* Compute the vertical distance from PT to the scroll
14870 margin position. Move as far as scroll_max allows, or
14871 one screenful, or 10 screen lines, whichever is largest.
14872 Give up if distance is greater than scroll_max or if we
14873 didn't reach the scroll margin position. */
14874 SET_TEXT_POS (pos, PT, PT_BYTE);
14875 start_display (&it, w, pos);
14876 y0 = it.current_y;
14877 y_to_move = max (it.last_visible_y,
14878 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
14879 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14880 y_to_move, -1,
14881 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14882 dy = it.current_y - y0;
14883 if (dy > scroll_max
14884 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
14885 return SCROLLING_FAILED;
14886
14887 /* Compute new window start. */
14888 start_display (&it, w, startp);
14889
14890 if (arg_scroll_conservatively)
14891 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
14892 max (scroll_step, temp_scroll_step));
14893 else if (scroll_step || temp_scroll_step)
14894 amount_to_scroll = scroll_max;
14895 else
14896 {
14897 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14898 height = WINDOW_BOX_TEXT_HEIGHT (w);
14899 if (NUMBERP (aggressive))
14900 {
14901 double float_amount = XFLOATINT (aggressive) * height;
14902 int aggressive_scroll = float_amount;
14903 if (aggressive_scroll == 0 && float_amount > 0)
14904 aggressive_scroll = 1;
14905 /* Don't let point enter the scroll margin near
14906 bottom of the window, if the value of
14907 scroll_down_aggressively happens to be too
14908 large. */
14909 if (aggressive_scroll + 2*this_scroll_margin > height)
14910 aggressive_scroll = height - 2*this_scroll_margin;
14911 amount_to_scroll = dy + aggressive_scroll;
14912 }
14913 }
14914
14915 if (amount_to_scroll <= 0)
14916 return SCROLLING_FAILED;
14917
14918 move_it_vertically_backward (&it, amount_to_scroll);
14919 startp = it.current.pos;
14920 }
14921 }
14922
14923 /* Run window scroll functions. */
14924 startp = run_window_scroll_functions (window, startp);
14925
14926 /* Display the window. Give up if new fonts are loaded, or if point
14927 doesn't appear. */
14928 if (!try_window (window, startp, 0))
14929 rc = SCROLLING_NEED_LARGER_MATRICES;
14930 else if (w->cursor.vpos < 0)
14931 {
14932 clear_glyph_matrix (w->desired_matrix);
14933 rc = SCROLLING_FAILED;
14934 }
14935 else
14936 {
14937 /* Maybe forget recorded base line for line number display. */
14938 if (!just_this_one_p
14939 || current_buffer->clip_changed
14940 || BEG_UNCHANGED < CHARPOS (startp))
14941 wset_base_line_number (w, Qnil);
14942
14943 /* If cursor ends up on a partially visible line,
14944 treat that as being off the bottom of the screen. */
14945 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14946 /* It's possible that the cursor is on the first line of the
14947 buffer, which is partially obscured due to a vscroll
14948 (Bug#7537). In that case, avoid looping forever . */
14949 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14950 {
14951 clear_glyph_matrix (w->desired_matrix);
14952 ++extra_scroll_margin_lines;
14953 goto too_near_end;
14954 }
14955 rc = SCROLLING_SUCCESS;
14956 }
14957
14958 return rc;
14959 }
14960
14961
14962 /* Compute a suitable window start for window W if display of W starts
14963 on a continuation line. Value is non-zero if a new window start
14964 was computed.
14965
14966 The new window start will be computed, based on W's width, starting
14967 from the start of the continued line. It is the start of the
14968 screen line with the minimum distance from the old start W->start. */
14969
14970 static int
14971 compute_window_start_on_continuation_line (struct window *w)
14972 {
14973 struct text_pos pos, start_pos;
14974 int window_start_changed_p = 0;
14975
14976 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14977
14978 /* If window start is on a continuation line... Window start may be
14979 < BEGV in case there's invisible text at the start of the
14980 buffer (M-x rmail, for example). */
14981 if (CHARPOS (start_pos) > BEGV
14982 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14983 {
14984 struct it it;
14985 struct glyph_row *row;
14986
14987 /* Handle the case that the window start is out of range. */
14988 if (CHARPOS (start_pos) < BEGV)
14989 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14990 else if (CHARPOS (start_pos) > ZV)
14991 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14992
14993 /* Find the start of the continued line. This should be fast
14994 because scan_buffer is fast (newline cache). */
14995 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14996 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14997 row, DEFAULT_FACE_ID);
14998 reseat_at_previous_visible_line_start (&it);
14999
15000 /* If the line start is "too far" away from the window start,
15001 say it takes too much time to compute a new window start. */
15002 if (CHARPOS (start_pos) - IT_CHARPOS (it)
15003 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
15004 {
15005 int min_distance, distance;
15006
15007 /* Move forward by display lines to find the new window
15008 start. If window width was enlarged, the new start can
15009 be expected to be > the old start. If window width was
15010 decreased, the new window start will be < the old start.
15011 So, we're looking for the display line start with the
15012 minimum distance from the old window start. */
15013 pos = it.current.pos;
15014 min_distance = INFINITY;
15015 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15016 distance < min_distance)
15017 {
15018 min_distance = distance;
15019 pos = it.current.pos;
15020 move_it_by_lines (&it, 1);
15021 }
15022
15023 /* Set the window start there. */
15024 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15025 window_start_changed_p = 1;
15026 }
15027 }
15028
15029 return window_start_changed_p;
15030 }
15031
15032
15033 /* Try cursor movement in case text has not changed in window WINDOW,
15034 with window start STARTP. Value is
15035
15036 CURSOR_MOVEMENT_SUCCESS if successful
15037
15038 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15039
15040 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15041 display. *SCROLL_STEP is set to 1, under certain circumstances, if
15042 we want to scroll as if scroll-step were set to 1. See the code.
15043
15044 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15045 which case we have to abort this redisplay, and adjust matrices
15046 first. */
15047
15048 enum
15049 {
15050 CURSOR_MOVEMENT_SUCCESS,
15051 CURSOR_MOVEMENT_CANNOT_BE_USED,
15052 CURSOR_MOVEMENT_MUST_SCROLL,
15053 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15054 };
15055
15056 static int
15057 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
15058 {
15059 struct window *w = XWINDOW (window);
15060 struct frame *f = XFRAME (w->frame);
15061 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15062
15063 #ifdef GLYPH_DEBUG
15064 if (inhibit_try_cursor_movement)
15065 return rc;
15066 #endif
15067
15068 /* Previously, there was a check for Lisp integer in the
15069 if-statement below. Now, this field is converted to
15070 ptrdiff_t, thus zero means invalid position in a buffer. */
15071 eassert (w->last_point > 0);
15072
15073 /* Handle case where text has not changed, only point, and it has
15074 not moved off the frame. */
15075 if (/* Point may be in this window. */
15076 PT >= CHARPOS (startp)
15077 /* Selective display hasn't changed. */
15078 && !current_buffer->clip_changed
15079 /* Function force-mode-line-update is used to force a thorough
15080 redisplay. It sets either windows_or_buffers_changed or
15081 update_mode_lines. So don't take a shortcut here for these
15082 cases. */
15083 && !update_mode_lines
15084 && !windows_or_buffers_changed
15085 && !cursor_type_changed
15086 /* Can't use this case if highlighting a region. When a
15087 region exists, cursor movement has to do more than just
15088 set the cursor. */
15089 && !(!NILP (Vtransient_mark_mode)
15090 && !NILP (BVAR (current_buffer, mark_active)))
15091 && NILP (w->region_showing)
15092 && NILP (Vshow_trailing_whitespace)
15093 /* This code is not used for mini-buffer for the sake of the case
15094 of redisplaying to replace an echo area message; since in
15095 that case the mini-buffer contents per se are usually
15096 unchanged. This code is of no real use in the mini-buffer
15097 since the handling of this_line_start_pos, etc., in redisplay
15098 handles the same cases. */
15099 && !EQ (window, minibuf_window)
15100 /* When splitting windows or for new windows, it happens that
15101 redisplay is called with a nil window_end_vpos or one being
15102 larger than the window. This should really be fixed in
15103 window.c. I don't have this on my list, now, so we do
15104 approximately the same as the old redisplay code. --gerd. */
15105 && INTEGERP (w->window_end_vpos)
15106 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
15107 && (FRAME_WINDOW_P (f)
15108 || !overlay_arrow_in_current_buffer_p ()))
15109 {
15110 int this_scroll_margin, top_scroll_margin;
15111 struct glyph_row *row = NULL;
15112
15113 #ifdef GLYPH_DEBUG
15114 debug_method_add (w, "cursor movement");
15115 #endif
15116
15117 /* Scroll if point within this distance from the top or bottom
15118 of the window. This is a pixel value. */
15119 if (scroll_margin > 0)
15120 {
15121 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15122 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
15123 }
15124 else
15125 this_scroll_margin = 0;
15126
15127 top_scroll_margin = this_scroll_margin;
15128 if (WINDOW_WANTS_HEADER_LINE_P (w))
15129 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15130
15131 /* Start with the row the cursor was displayed during the last
15132 not paused redisplay. Give up if that row is not valid. */
15133 if (w->last_cursor.vpos < 0
15134 || w->last_cursor.vpos >= w->current_matrix->nrows)
15135 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15136 else
15137 {
15138 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
15139 if (row->mode_line_p)
15140 ++row;
15141 if (!row->enabled_p)
15142 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15143 }
15144
15145 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15146 {
15147 int scroll_p = 0, must_scroll = 0;
15148 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15149
15150 if (PT > w->last_point)
15151 {
15152 /* Point has moved forward. */
15153 while (MATRIX_ROW_END_CHARPOS (row) < PT
15154 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15155 {
15156 eassert (row->enabled_p);
15157 ++row;
15158 }
15159
15160 /* If the end position of a row equals the start
15161 position of the next row, and PT is at that position,
15162 we would rather display cursor in the next line. */
15163 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15164 && MATRIX_ROW_END_CHARPOS (row) == PT
15165 && row < w->current_matrix->rows
15166 + w->current_matrix->nrows - 1
15167 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15168 && !cursor_row_p (row))
15169 ++row;
15170
15171 /* If within the scroll margin, scroll. Note that
15172 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15173 the next line would be drawn, and that
15174 this_scroll_margin can be zero. */
15175 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15176 || PT > MATRIX_ROW_END_CHARPOS (row)
15177 /* Line is completely visible last line in window
15178 and PT is to be set in the next line. */
15179 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15180 && PT == MATRIX_ROW_END_CHARPOS (row)
15181 && !row->ends_at_zv_p
15182 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15183 scroll_p = 1;
15184 }
15185 else if (PT < w->last_point)
15186 {
15187 /* Cursor has to be moved backward. Note that PT >=
15188 CHARPOS (startp) because of the outer if-statement. */
15189 while (!row->mode_line_p
15190 && (MATRIX_ROW_START_CHARPOS (row) > PT
15191 || (MATRIX_ROW_START_CHARPOS (row) == PT
15192 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15193 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15194 row > w->current_matrix->rows
15195 && (row-1)->ends_in_newline_from_string_p))))
15196 && (row->y > top_scroll_margin
15197 || CHARPOS (startp) == BEGV))
15198 {
15199 eassert (row->enabled_p);
15200 --row;
15201 }
15202
15203 /* Consider the following case: Window starts at BEGV,
15204 there is invisible, intangible text at BEGV, so that
15205 display starts at some point START > BEGV. It can
15206 happen that we are called with PT somewhere between
15207 BEGV and START. Try to handle that case. */
15208 if (row < w->current_matrix->rows
15209 || row->mode_line_p)
15210 {
15211 row = w->current_matrix->rows;
15212 if (row->mode_line_p)
15213 ++row;
15214 }
15215
15216 /* Due to newlines in overlay strings, we may have to
15217 skip forward over overlay strings. */
15218 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15219 && MATRIX_ROW_END_CHARPOS (row) == PT
15220 && !cursor_row_p (row))
15221 ++row;
15222
15223 /* If within the scroll margin, scroll. */
15224 if (row->y < top_scroll_margin
15225 && CHARPOS (startp) != BEGV)
15226 scroll_p = 1;
15227 }
15228 else
15229 {
15230 /* Cursor did not move. So don't scroll even if cursor line
15231 is partially visible, as it was so before. */
15232 rc = CURSOR_MOVEMENT_SUCCESS;
15233 }
15234
15235 if (PT < MATRIX_ROW_START_CHARPOS (row)
15236 || PT > MATRIX_ROW_END_CHARPOS (row))
15237 {
15238 /* if PT is not in the glyph row, give up. */
15239 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15240 must_scroll = 1;
15241 }
15242 else if (rc != CURSOR_MOVEMENT_SUCCESS
15243 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15244 {
15245 struct glyph_row *row1;
15246
15247 /* If rows are bidi-reordered and point moved, back up
15248 until we find a row that does not belong to a
15249 continuation line. This is because we must consider
15250 all rows of a continued line as candidates for the
15251 new cursor positioning, since row start and end
15252 positions change non-linearly with vertical position
15253 in such rows. */
15254 /* FIXME: Revisit this when glyph ``spilling'' in
15255 continuation lines' rows is implemented for
15256 bidi-reordered rows. */
15257 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15258 MATRIX_ROW_CONTINUATION_LINE_P (row);
15259 --row)
15260 {
15261 /* If we hit the beginning of the displayed portion
15262 without finding the first row of a continued
15263 line, give up. */
15264 if (row <= row1)
15265 {
15266 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15267 break;
15268 }
15269 eassert (row->enabled_p);
15270 }
15271 }
15272 if (must_scroll)
15273 ;
15274 else if (rc != CURSOR_MOVEMENT_SUCCESS
15275 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15276 /* Make sure this isn't a header line by any chance, since
15277 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15278 && !row->mode_line_p
15279 && make_cursor_line_fully_visible_p)
15280 {
15281 if (PT == MATRIX_ROW_END_CHARPOS (row)
15282 && !row->ends_at_zv_p
15283 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15284 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15285 else if (row->height > window_box_height (w))
15286 {
15287 /* If we end up in a partially visible line, let's
15288 make it fully visible, except when it's taller
15289 than the window, in which case we can't do much
15290 about it. */
15291 *scroll_step = 1;
15292 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15293 }
15294 else
15295 {
15296 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15297 if (!cursor_row_fully_visible_p (w, 0, 1))
15298 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15299 else
15300 rc = CURSOR_MOVEMENT_SUCCESS;
15301 }
15302 }
15303 else if (scroll_p)
15304 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15305 else if (rc != CURSOR_MOVEMENT_SUCCESS
15306 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15307 {
15308 /* With bidi-reordered rows, there could be more than
15309 one candidate row whose start and end positions
15310 occlude point. We need to let set_cursor_from_row
15311 find the best candidate. */
15312 /* FIXME: Revisit this when glyph ``spilling'' in
15313 continuation lines' rows is implemented for
15314 bidi-reordered rows. */
15315 int rv = 0;
15316
15317 do
15318 {
15319 int at_zv_p = 0, exact_match_p = 0;
15320
15321 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15322 && PT <= MATRIX_ROW_END_CHARPOS (row)
15323 && cursor_row_p (row))
15324 rv |= set_cursor_from_row (w, row, w->current_matrix,
15325 0, 0, 0, 0);
15326 /* As soon as we've found the exact match for point,
15327 or the first suitable row whose ends_at_zv_p flag
15328 is set, we are done. */
15329 at_zv_p =
15330 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15331 if (rv && !at_zv_p
15332 && w->cursor.hpos >= 0
15333 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15334 w->cursor.vpos))
15335 {
15336 struct glyph_row *candidate =
15337 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15338 struct glyph *g =
15339 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15340 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15341
15342 exact_match_p =
15343 (BUFFERP (g->object) && g->charpos == PT)
15344 || (INTEGERP (g->object)
15345 && (g->charpos == PT
15346 || (g->charpos == 0 && endpos - 1 == PT)));
15347 }
15348 if (rv && (at_zv_p || exact_match_p))
15349 {
15350 rc = CURSOR_MOVEMENT_SUCCESS;
15351 break;
15352 }
15353 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15354 break;
15355 ++row;
15356 }
15357 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15358 || row->continued_p)
15359 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15360 || (MATRIX_ROW_START_CHARPOS (row) == PT
15361 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15362 /* If we didn't find any candidate rows, or exited the
15363 loop before all the candidates were examined, signal
15364 to the caller that this method failed. */
15365 if (rc != CURSOR_MOVEMENT_SUCCESS
15366 && !(rv
15367 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15368 && !row->continued_p))
15369 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15370 else if (rv)
15371 rc = CURSOR_MOVEMENT_SUCCESS;
15372 }
15373 else
15374 {
15375 do
15376 {
15377 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15378 {
15379 rc = CURSOR_MOVEMENT_SUCCESS;
15380 break;
15381 }
15382 ++row;
15383 }
15384 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15385 && MATRIX_ROW_START_CHARPOS (row) == PT
15386 && cursor_row_p (row));
15387 }
15388 }
15389 }
15390
15391 return rc;
15392 }
15393
15394 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15395 static
15396 #endif
15397 void
15398 set_vertical_scroll_bar (struct window *w)
15399 {
15400 ptrdiff_t start, end, whole;
15401
15402 /* Calculate the start and end positions for the current window.
15403 At some point, it would be nice to choose between scrollbars
15404 which reflect the whole buffer size, with special markers
15405 indicating narrowing, and scrollbars which reflect only the
15406 visible region.
15407
15408 Note that mini-buffers sometimes aren't displaying any text. */
15409 if (!MINI_WINDOW_P (w)
15410 || (w == XWINDOW (minibuf_window)
15411 && NILP (echo_area_buffer[0])))
15412 {
15413 struct buffer *buf = XBUFFER (w->buffer);
15414 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15415 start = marker_position (w->start) - BUF_BEGV (buf);
15416 /* I don't think this is guaranteed to be right. For the
15417 moment, we'll pretend it is. */
15418 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
15419
15420 if (end < start)
15421 end = start;
15422 if (whole < (end - start))
15423 whole = end - start;
15424 }
15425 else
15426 start = end = whole = 0;
15427
15428 /* Indicate what this scroll bar ought to be displaying now. */
15429 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15430 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15431 (w, end - start, whole, start);
15432 }
15433
15434
15435 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15436 selected_window is redisplayed.
15437
15438 We can return without actually redisplaying the window if
15439 fonts_changed_p. In that case, redisplay_internal will
15440 retry. */
15441
15442 static void
15443 redisplay_window (Lisp_Object window, int just_this_one_p)
15444 {
15445 struct window *w = XWINDOW (window);
15446 struct frame *f = XFRAME (w->frame);
15447 struct buffer *buffer = XBUFFER (w->buffer);
15448 struct buffer *old = current_buffer;
15449 struct text_pos lpoint, opoint, startp;
15450 int update_mode_line;
15451 int tem;
15452 struct it it;
15453 /* Record it now because it's overwritten. */
15454 int current_matrix_up_to_date_p = 0;
15455 int used_current_matrix_p = 0;
15456 /* This is less strict than current_matrix_up_to_date_p.
15457 It indicates that the buffer contents and narrowing are unchanged. */
15458 int buffer_unchanged_p = 0;
15459 int temp_scroll_step = 0;
15460 ptrdiff_t count = SPECPDL_INDEX ();
15461 int rc;
15462 int centering_position = -1;
15463 int last_line_misfit = 0;
15464 ptrdiff_t beg_unchanged, end_unchanged;
15465
15466 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15467 opoint = lpoint;
15468
15469 /* W must be a leaf window here. */
15470 eassert (!NILP (w->buffer));
15471 #ifdef GLYPH_DEBUG
15472 *w->desired_matrix->method = 0;
15473 #endif
15474
15475 restart:
15476 reconsider_clip_changes (w, buffer);
15477
15478 /* Has the mode line to be updated? */
15479 update_mode_line = (w->update_mode_line
15480 || update_mode_lines
15481 || buffer->clip_changed
15482 || buffer->prevent_redisplay_optimizations_p);
15483
15484 if (MINI_WINDOW_P (w))
15485 {
15486 if (w == XWINDOW (echo_area_window)
15487 && !NILP (echo_area_buffer[0]))
15488 {
15489 if (update_mode_line)
15490 /* We may have to update a tty frame's menu bar or a
15491 tool-bar. Example `M-x C-h C-h C-g'. */
15492 goto finish_menu_bars;
15493 else
15494 /* We've already displayed the echo area glyphs in this window. */
15495 goto finish_scroll_bars;
15496 }
15497 else if ((w != XWINDOW (minibuf_window)
15498 || minibuf_level == 0)
15499 /* When buffer is nonempty, redisplay window normally. */
15500 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
15501 /* Quail displays non-mini buffers in minibuffer window.
15502 In that case, redisplay the window normally. */
15503 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
15504 {
15505 /* W is a mini-buffer window, but it's not active, so clear
15506 it. */
15507 int yb = window_text_bottom_y (w);
15508 struct glyph_row *row;
15509 int y;
15510
15511 for (y = 0, row = w->desired_matrix->rows;
15512 y < yb;
15513 y += row->height, ++row)
15514 blank_row (w, row, y);
15515 goto finish_scroll_bars;
15516 }
15517
15518 clear_glyph_matrix (w->desired_matrix);
15519 }
15520
15521 /* Otherwise set up data on this window; select its buffer and point
15522 value. */
15523 /* Really select the buffer, for the sake of buffer-local
15524 variables. */
15525 set_buffer_internal_1 (XBUFFER (w->buffer));
15526
15527 current_matrix_up_to_date_p
15528 = (!NILP (w->window_end_valid)
15529 && !current_buffer->clip_changed
15530 && !current_buffer->prevent_redisplay_optimizations_p
15531 && w->last_modified >= MODIFF
15532 && w->last_overlay_modified >= OVERLAY_MODIFF);
15533
15534 /* Run the window-bottom-change-functions
15535 if it is possible that the text on the screen has changed
15536 (either due to modification of the text, or any other reason). */
15537 if (!current_matrix_up_to_date_p
15538 && !NILP (Vwindow_text_change_functions))
15539 {
15540 safe_run_hooks (Qwindow_text_change_functions);
15541 goto restart;
15542 }
15543
15544 beg_unchanged = BEG_UNCHANGED;
15545 end_unchanged = END_UNCHANGED;
15546
15547 SET_TEXT_POS (opoint, PT, PT_BYTE);
15548
15549 specbind (Qinhibit_point_motion_hooks, Qt);
15550
15551 buffer_unchanged_p
15552 = (!NILP (w->window_end_valid)
15553 && !current_buffer->clip_changed
15554 && w->last_modified >= MODIFF
15555 && w->last_overlay_modified >= OVERLAY_MODIFF);
15556
15557 /* When windows_or_buffers_changed is non-zero, we can't rely on
15558 the window end being valid, so set it to nil there. */
15559 if (windows_or_buffers_changed)
15560 {
15561 /* If window starts on a continuation line, maybe adjust the
15562 window start in case the window's width changed. */
15563 if (XMARKER (w->start)->buffer == current_buffer)
15564 compute_window_start_on_continuation_line (w);
15565
15566 wset_window_end_valid (w, Qnil);
15567 }
15568
15569 /* Some sanity checks. */
15570 CHECK_WINDOW_END (w);
15571 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15572 emacs_abort ();
15573 if (BYTEPOS (opoint) < CHARPOS (opoint))
15574 emacs_abort ();
15575
15576 /* If %c is in mode line, update it if needed. */
15577 if (!NILP (w->column_number_displayed)
15578 /* This alternative quickly identifies a common case
15579 where no change is needed. */
15580 && !(PT == w->last_point
15581 && w->last_modified >= MODIFF
15582 && w->last_overlay_modified >= OVERLAY_MODIFF)
15583 && (XFASTINT (w->column_number_displayed) != current_column ()))
15584 update_mode_line = 1;
15585
15586 /* Count number of windows showing the selected buffer. An indirect
15587 buffer counts as its base buffer. */
15588 if (!just_this_one_p)
15589 {
15590 struct buffer *current_base, *window_base;
15591 current_base = current_buffer;
15592 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
15593 if (current_base->base_buffer)
15594 current_base = current_base->base_buffer;
15595 if (window_base->base_buffer)
15596 window_base = window_base->base_buffer;
15597 if (current_base == window_base)
15598 buffer_shared++;
15599 }
15600
15601 /* Point refers normally to the selected window. For any other
15602 window, set up appropriate value. */
15603 if (!EQ (window, selected_window))
15604 {
15605 ptrdiff_t new_pt = XMARKER (w->pointm)->charpos;
15606 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15607 if (new_pt < BEGV)
15608 {
15609 new_pt = BEGV;
15610 new_pt_byte = BEGV_BYTE;
15611 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15612 }
15613 else if (new_pt > (ZV - 1))
15614 {
15615 new_pt = ZV;
15616 new_pt_byte = ZV_BYTE;
15617 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15618 }
15619
15620 /* We don't use SET_PT so that the point-motion hooks don't run. */
15621 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15622 }
15623
15624 /* If any of the character widths specified in the display table
15625 have changed, invalidate the width run cache. It's true that
15626 this may be a bit late to catch such changes, but the rest of
15627 redisplay goes (non-fatally) haywire when the display table is
15628 changed, so why should we worry about doing any better? */
15629 if (current_buffer->width_run_cache)
15630 {
15631 struct Lisp_Char_Table *disptab = buffer_display_table ();
15632
15633 if (! disptab_matches_widthtab
15634 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15635 {
15636 invalidate_region_cache (current_buffer,
15637 current_buffer->width_run_cache,
15638 BEG, Z);
15639 recompute_width_table (current_buffer, disptab);
15640 }
15641 }
15642
15643 /* If window-start is screwed up, choose a new one. */
15644 if (XMARKER (w->start)->buffer != current_buffer)
15645 goto recenter;
15646
15647 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15648
15649 /* If someone specified a new starting point but did not insist,
15650 check whether it can be used. */
15651 if (w->optional_new_start
15652 && CHARPOS (startp) >= BEGV
15653 && CHARPOS (startp) <= ZV)
15654 {
15655 w->optional_new_start = 0;
15656 start_display (&it, w, startp);
15657 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15658 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15659 if (IT_CHARPOS (it) == PT)
15660 w->force_start = 1;
15661 /* IT may overshoot PT if text at PT is invisible. */
15662 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15663 w->force_start = 1;
15664 }
15665
15666 force_start:
15667
15668 /* Handle case where place to start displaying has been specified,
15669 unless the specified location is outside the accessible range. */
15670 if (w->force_start || w->frozen_window_start_p)
15671 {
15672 /* We set this later on if we have to adjust point. */
15673 int new_vpos = -1;
15674
15675 w->force_start = 0;
15676 w->vscroll = 0;
15677 wset_window_end_valid (w, Qnil);
15678
15679 /* Forget any recorded base line for line number display. */
15680 if (!buffer_unchanged_p)
15681 wset_base_line_number (w, Qnil);
15682
15683 /* Redisplay the mode line. Select the buffer properly for that.
15684 Also, run the hook window-scroll-functions
15685 because we have scrolled. */
15686 /* Note, we do this after clearing force_start because
15687 if there's an error, it is better to forget about force_start
15688 than to get into an infinite loop calling the hook functions
15689 and having them get more errors. */
15690 if (!update_mode_line
15691 || ! NILP (Vwindow_scroll_functions))
15692 {
15693 update_mode_line = 1;
15694 w->update_mode_line = 1;
15695 startp = run_window_scroll_functions (window, startp);
15696 }
15697
15698 w->last_modified = 0;
15699 w->last_overlay_modified = 0;
15700 if (CHARPOS (startp) < BEGV)
15701 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15702 else if (CHARPOS (startp) > ZV)
15703 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15704
15705 /* Redisplay, then check if cursor has been set during the
15706 redisplay. Give up if new fonts were loaded. */
15707 /* We used to issue a CHECK_MARGINS argument to try_window here,
15708 but this causes scrolling to fail when point begins inside
15709 the scroll margin (bug#148) -- cyd */
15710 if (!try_window (window, startp, 0))
15711 {
15712 w->force_start = 1;
15713 clear_glyph_matrix (w->desired_matrix);
15714 goto need_larger_matrices;
15715 }
15716
15717 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
15718 {
15719 /* If point does not appear, try to move point so it does
15720 appear. The desired matrix has been built above, so we
15721 can use it here. */
15722 new_vpos = window_box_height (w) / 2;
15723 }
15724
15725 if (!cursor_row_fully_visible_p (w, 0, 0))
15726 {
15727 /* Point does appear, but on a line partly visible at end of window.
15728 Move it back to a fully-visible line. */
15729 new_vpos = window_box_height (w);
15730 }
15731
15732 /* If we need to move point for either of the above reasons,
15733 now actually do it. */
15734 if (new_vpos >= 0)
15735 {
15736 struct glyph_row *row;
15737
15738 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15739 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15740 ++row;
15741
15742 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15743 MATRIX_ROW_START_BYTEPOS (row));
15744
15745 if (w != XWINDOW (selected_window))
15746 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15747 else if (current_buffer == old)
15748 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15749
15750 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15751
15752 /* If we are highlighting the region, then we just changed
15753 the region, so redisplay to show it. */
15754 if (!NILP (Vtransient_mark_mode)
15755 && !NILP (BVAR (current_buffer, mark_active)))
15756 {
15757 clear_glyph_matrix (w->desired_matrix);
15758 if (!try_window (window, startp, 0))
15759 goto need_larger_matrices;
15760 }
15761 }
15762
15763 #ifdef GLYPH_DEBUG
15764 debug_method_add (w, "forced window start");
15765 #endif
15766 goto done;
15767 }
15768
15769 /* Handle case where text has not changed, only point, and it has
15770 not moved off the frame, and we are not retrying after hscroll.
15771 (current_matrix_up_to_date_p is nonzero when retrying.) */
15772 if (current_matrix_up_to_date_p
15773 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15774 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15775 {
15776 switch (rc)
15777 {
15778 case CURSOR_MOVEMENT_SUCCESS:
15779 used_current_matrix_p = 1;
15780 goto done;
15781
15782 case CURSOR_MOVEMENT_MUST_SCROLL:
15783 goto try_to_scroll;
15784
15785 default:
15786 emacs_abort ();
15787 }
15788 }
15789 /* If current starting point was originally the beginning of a line
15790 but no longer is, find a new starting point. */
15791 else if (w->start_at_line_beg
15792 && !(CHARPOS (startp) <= BEGV
15793 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15794 {
15795 #ifdef GLYPH_DEBUG
15796 debug_method_add (w, "recenter 1");
15797 #endif
15798 goto recenter;
15799 }
15800
15801 /* Try scrolling with try_window_id. Value is > 0 if update has
15802 been done, it is -1 if we know that the same window start will
15803 not work. It is 0 if unsuccessful for some other reason. */
15804 else if ((tem = try_window_id (w)) != 0)
15805 {
15806 #ifdef GLYPH_DEBUG
15807 debug_method_add (w, "try_window_id %d", tem);
15808 #endif
15809
15810 if (fonts_changed_p)
15811 goto need_larger_matrices;
15812 if (tem > 0)
15813 goto done;
15814
15815 /* Otherwise try_window_id has returned -1 which means that we
15816 don't want the alternative below this comment to execute. */
15817 }
15818 else if (CHARPOS (startp) >= BEGV
15819 && CHARPOS (startp) <= ZV
15820 && PT >= CHARPOS (startp)
15821 && (CHARPOS (startp) < ZV
15822 /* Avoid starting at end of buffer. */
15823 || CHARPOS (startp) == BEGV
15824 || (w->last_modified >= MODIFF
15825 && w->last_overlay_modified >= OVERLAY_MODIFF)))
15826 {
15827 int d1, d2, d3, d4, d5, d6;
15828
15829 /* If first window line is a continuation line, and window start
15830 is inside the modified region, but the first change is before
15831 current window start, we must select a new window start.
15832
15833 However, if this is the result of a down-mouse event (e.g. by
15834 extending the mouse-drag-overlay), we don't want to select a
15835 new window start, since that would change the position under
15836 the mouse, resulting in an unwanted mouse-movement rather
15837 than a simple mouse-click. */
15838 if (!w->start_at_line_beg
15839 && NILP (do_mouse_tracking)
15840 && CHARPOS (startp) > BEGV
15841 && CHARPOS (startp) > BEG + beg_unchanged
15842 && CHARPOS (startp) <= Z - end_unchanged
15843 /* Even if w->start_at_line_beg is nil, a new window may
15844 start at a line_beg, since that's how set_buffer_window
15845 sets it. So, we need to check the return value of
15846 compute_window_start_on_continuation_line. (See also
15847 bug#197). */
15848 && XMARKER (w->start)->buffer == current_buffer
15849 && compute_window_start_on_continuation_line (w)
15850 /* It doesn't make sense to force the window start like we
15851 do at label force_start if it is already known that point
15852 will not be visible in the resulting window, because
15853 doing so will move point from its correct position
15854 instead of scrolling the window to bring point into view.
15855 See bug#9324. */
15856 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15857 {
15858 w->force_start = 1;
15859 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15860 goto force_start;
15861 }
15862
15863 #ifdef GLYPH_DEBUG
15864 debug_method_add (w, "same window start");
15865 #endif
15866
15867 /* Try to redisplay starting at same place as before.
15868 If point has not moved off frame, accept the results. */
15869 if (!current_matrix_up_to_date_p
15870 /* Don't use try_window_reusing_current_matrix in this case
15871 because a window scroll function can have changed the
15872 buffer. */
15873 || !NILP (Vwindow_scroll_functions)
15874 || MINI_WINDOW_P (w)
15875 || !(used_current_matrix_p
15876 = try_window_reusing_current_matrix (w)))
15877 {
15878 IF_DEBUG (debug_method_add (w, "1"));
15879 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15880 /* -1 means we need to scroll.
15881 0 means we need new matrices, but fonts_changed_p
15882 is set in that case, so we will detect it below. */
15883 goto try_to_scroll;
15884 }
15885
15886 if (fonts_changed_p)
15887 goto need_larger_matrices;
15888
15889 if (w->cursor.vpos >= 0)
15890 {
15891 if (!just_this_one_p
15892 || current_buffer->clip_changed
15893 || BEG_UNCHANGED < CHARPOS (startp))
15894 /* Forget any recorded base line for line number display. */
15895 wset_base_line_number (w, Qnil);
15896
15897 if (!cursor_row_fully_visible_p (w, 1, 0))
15898 {
15899 clear_glyph_matrix (w->desired_matrix);
15900 last_line_misfit = 1;
15901 }
15902 /* Drop through and scroll. */
15903 else
15904 goto done;
15905 }
15906 else
15907 clear_glyph_matrix (w->desired_matrix);
15908 }
15909
15910 try_to_scroll:
15911
15912 w->last_modified = 0;
15913 w->last_overlay_modified = 0;
15914
15915 /* Redisplay the mode line. Select the buffer properly for that. */
15916 if (!update_mode_line)
15917 {
15918 update_mode_line = 1;
15919 w->update_mode_line = 1;
15920 }
15921
15922 /* Try to scroll by specified few lines. */
15923 if ((scroll_conservatively
15924 || emacs_scroll_step
15925 || temp_scroll_step
15926 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15927 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15928 && CHARPOS (startp) >= BEGV
15929 && CHARPOS (startp) <= ZV)
15930 {
15931 /* The function returns -1 if new fonts were loaded, 1 if
15932 successful, 0 if not successful. */
15933 int ss = try_scrolling (window, just_this_one_p,
15934 scroll_conservatively,
15935 emacs_scroll_step,
15936 temp_scroll_step, last_line_misfit);
15937 switch (ss)
15938 {
15939 case SCROLLING_SUCCESS:
15940 goto done;
15941
15942 case SCROLLING_NEED_LARGER_MATRICES:
15943 goto need_larger_matrices;
15944
15945 case SCROLLING_FAILED:
15946 break;
15947
15948 default:
15949 emacs_abort ();
15950 }
15951 }
15952
15953 /* Finally, just choose a place to start which positions point
15954 according to user preferences. */
15955
15956 recenter:
15957
15958 #ifdef GLYPH_DEBUG
15959 debug_method_add (w, "recenter");
15960 #endif
15961
15962 /* w->vscroll = 0; */
15963
15964 /* Forget any previously recorded base line for line number display. */
15965 if (!buffer_unchanged_p)
15966 wset_base_line_number (w, Qnil);
15967
15968 /* Determine the window start relative to point. */
15969 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15970 it.current_y = it.last_visible_y;
15971 if (centering_position < 0)
15972 {
15973 int margin =
15974 scroll_margin > 0
15975 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15976 : 0;
15977 ptrdiff_t margin_pos = CHARPOS (startp);
15978 Lisp_Object aggressive;
15979 int scrolling_up;
15980
15981 /* If there is a scroll margin at the top of the window, find
15982 its character position. */
15983 if (margin
15984 /* Cannot call start_display if startp is not in the
15985 accessible region of the buffer. This can happen when we
15986 have just switched to a different buffer and/or changed
15987 its restriction. In that case, startp is initialized to
15988 the character position 1 (BEGV) because we did not yet
15989 have chance to display the buffer even once. */
15990 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15991 {
15992 struct it it1;
15993 void *it1data = NULL;
15994
15995 SAVE_IT (it1, it, it1data);
15996 start_display (&it1, w, startp);
15997 move_it_vertically (&it1, margin * FRAME_LINE_HEIGHT (f));
15998 margin_pos = IT_CHARPOS (it1);
15999 RESTORE_IT (&it, &it, it1data);
16000 }
16001 scrolling_up = PT > margin_pos;
16002 aggressive =
16003 scrolling_up
16004 ? BVAR (current_buffer, scroll_up_aggressively)
16005 : BVAR (current_buffer, scroll_down_aggressively);
16006
16007 if (!MINI_WINDOW_P (w)
16008 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
16009 {
16010 int pt_offset = 0;
16011
16012 /* Setting scroll-conservatively overrides
16013 scroll-*-aggressively. */
16014 if (!scroll_conservatively && NUMBERP (aggressive))
16015 {
16016 double float_amount = XFLOATINT (aggressive);
16017
16018 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
16019 if (pt_offset == 0 && float_amount > 0)
16020 pt_offset = 1;
16021 if (pt_offset && margin > 0)
16022 margin -= 1;
16023 }
16024 /* Compute how much to move the window start backward from
16025 point so that point will be displayed where the user
16026 wants it. */
16027 if (scrolling_up)
16028 {
16029 centering_position = it.last_visible_y;
16030 if (pt_offset)
16031 centering_position -= pt_offset;
16032 centering_position -=
16033 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0))
16034 + WINDOW_HEADER_LINE_HEIGHT (w);
16035 /* Don't let point enter the scroll margin near top of
16036 the window. */
16037 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
16038 centering_position = margin * FRAME_LINE_HEIGHT (f);
16039 }
16040 else
16041 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
16042 }
16043 else
16044 /* Set the window start half the height of the window backward
16045 from point. */
16046 centering_position = window_box_height (w) / 2;
16047 }
16048 move_it_vertically_backward (&it, centering_position);
16049
16050 eassert (IT_CHARPOS (it) >= BEGV);
16051
16052 /* The function move_it_vertically_backward may move over more
16053 than the specified y-distance. If it->w is small, e.g. a
16054 mini-buffer window, we may end up in front of the window's
16055 display area. Start displaying at the start of the line
16056 containing PT in this case. */
16057 if (it.current_y <= 0)
16058 {
16059 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16060 move_it_vertically_backward (&it, 0);
16061 it.current_y = 0;
16062 }
16063
16064 it.current_x = it.hpos = 0;
16065
16066 /* Set the window start position here explicitly, to avoid an
16067 infinite loop in case the functions in window-scroll-functions
16068 get errors. */
16069 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16070
16071 /* Run scroll hooks. */
16072 startp = run_window_scroll_functions (window, it.current.pos);
16073
16074 /* Redisplay the window. */
16075 if (!current_matrix_up_to_date_p
16076 || windows_or_buffers_changed
16077 || cursor_type_changed
16078 /* Don't use try_window_reusing_current_matrix in this case
16079 because it can have changed the buffer. */
16080 || !NILP (Vwindow_scroll_functions)
16081 || !just_this_one_p
16082 || MINI_WINDOW_P (w)
16083 || !(used_current_matrix_p
16084 = try_window_reusing_current_matrix (w)))
16085 try_window (window, startp, 0);
16086
16087 /* If new fonts have been loaded (due to fontsets), give up. We
16088 have to start a new redisplay since we need to re-adjust glyph
16089 matrices. */
16090 if (fonts_changed_p)
16091 goto need_larger_matrices;
16092
16093 /* If cursor did not appear assume that the middle of the window is
16094 in the first line of the window. Do it again with the next line.
16095 (Imagine a window of height 100, displaying two lines of height
16096 60. Moving back 50 from it->last_visible_y will end in the first
16097 line.) */
16098 if (w->cursor.vpos < 0)
16099 {
16100 if (!NILP (w->window_end_valid)
16101 && PT >= Z - XFASTINT (w->window_end_pos))
16102 {
16103 clear_glyph_matrix (w->desired_matrix);
16104 move_it_by_lines (&it, 1);
16105 try_window (window, it.current.pos, 0);
16106 }
16107 else if (PT < IT_CHARPOS (it))
16108 {
16109 clear_glyph_matrix (w->desired_matrix);
16110 move_it_by_lines (&it, -1);
16111 try_window (window, it.current.pos, 0);
16112 }
16113 else
16114 {
16115 /* Not much we can do about it. */
16116 }
16117 }
16118
16119 /* Consider the following case: Window starts at BEGV, there is
16120 invisible, intangible text at BEGV, so that display starts at
16121 some point START > BEGV. It can happen that we are called with
16122 PT somewhere between BEGV and START. Try to handle that case. */
16123 if (w->cursor.vpos < 0)
16124 {
16125 struct glyph_row *row = w->current_matrix->rows;
16126 if (row->mode_line_p)
16127 ++row;
16128 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16129 }
16130
16131 if (!cursor_row_fully_visible_p (w, 0, 0))
16132 {
16133 /* If vscroll is enabled, disable it and try again. */
16134 if (w->vscroll)
16135 {
16136 w->vscroll = 0;
16137 clear_glyph_matrix (w->desired_matrix);
16138 goto recenter;
16139 }
16140
16141 /* Users who set scroll-conservatively to a large number want
16142 point just above/below the scroll margin. If we ended up
16143 with point's row partially visible, move the window start to
16144 make that row fully visible and out of the margin. */
16145 if (scroll_conservatively > SCROLL_LIMIT)
16146 {
16147 int margin =
16148 scroll_margin > 0
16149 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
16150 : 0;
16151 int move_down = w->cursor.vpos >= WINDOW_TOTAL_LINES (w) / 2;
16152
16153 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16154 clear_glyph_matrix (w->desired_matrix);
16155 if (1 == try_window (window, it.current.pos,
16156 TRY_WINDOW_CHECK_MARGINS))
16157 goto done;
16158 }
16159
16160 /* If centering point failed to make the whole line visible,
16161 put point at the top instead. That has to make the whole line
16162 visible, if it can be done. */
16163 if (centering_position == 0)
16164 goto done;
16165
16166 clear_glyph_matrix (w->desired_matrix);
16167 centering_position = 0;
16168 goto recenter;
16169 }
16170
16171 done:
16172
16173 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16174 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16175 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16176
16177 /* Display the mode line, if we must. */
16178 if ((update_mode_line
16179 /* If window not full width, must redo its mode line
16180 if (a) the window to its side is being redone and
16181 (b) we do a frame-based redisplay. This is a consequence
16182 of how inverted lines are drawn in frame-based redisplay. */
16183 || (!just_this_one_p
16184 && !FRAME_WINDOW_P (f)
16185 && !WINDOW_FULL_WIDTH_P (w))
16186 /* Line number to display. */
16187 || INTEGERP (w->base_line_pos)
16188 /* Column number is displayed and different from the one displayed. */
16189 || (!NILP (w->column_number_displayed)
16190 && (XFASTINT (w->column_number_displayed) != current_column ())))
16191 /* This means that the window has a mode line. */
16192 && (WINDOW_WANTS_MODELINE_P (w)
16193 || WINDOW_WANTS_HEADER_LINE_P (w)))
16194 {
16195 display_mode_lines (w);
16196
16197 /* If mode line height has changed, arrange for a thorough
16198 immediate redisplay using the correct mode line height. */
16199 if (WINDOW_WANTS_MODELINE_P (w)
16200 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16201 {
16202 fonts_changed_p = 1;
16203 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16204 = DESIRED_MODE_LINE_HEIGHT (w);
16205 }
16206
16207 /* If header line height has changed, arrange for a thorough
16208 immediate redisplay using the correct header line height. */
16209 if (WINDOW_WANTS_HEADER_LINE_P (w)
16210 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16211 {
16212 fonts_changed_p = 1;
16213 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16214 = DESIRED_HEADER_LINE_HEIGHT (w);
16215 }
16216
16217 if (fonts_changed_p)
16218 goto need_larger_matrices;
16219 }
16220
16221 if (!line_number_displayed
16222 && !BUFFERP (w->base_line_pos))
16223 {
16224 wset_base_line_pos (w, Qnil);
16225 wset_base_line_number (w, Qnil);
16226 }
16227
16228 finish_menu_bars:
16229
16230 /* When we reach a frame's selected window, redo the frame's menu bar. */
16231 if (update_mode_line
16232 && EQ (FRAME_SELECTED_WINDOW (f), window))
16233 {
16234 int redisplay_menu_p = 0;
16235
16236 if (FRAME_WINDOW_P (f))
16237 {
16238 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16239 || defined (HAVE_NS) || defined (USE_GTK)
16240 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16241 #else
16242 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16243 #endif
16244 }
16245 else
16246 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16247
16248 if (redisplay_menu_p)
16249 display_menu_bar (w);
16250
16251 #ifdef HAVE_WINDOW_SYSTEM
16252 if (FRAME_WINDOW_P (f))
16253 {
16254 #if defined (USE_GTK) || defined (HAVE_NS)
16255 if (FRAME_EXTERNAL_TOOL_BAR (f))
16256 redisplay_tool_bar (f);
16257 #else
16258 if (WINDOWP (f->tool_bar_window)
16259 && (FRAME_TOOL_BAR_LINES (f) > 0
16260 || !NILP (Vauto_resize_tool_bars))
16261 && redisplay_tool_bar (f))
16262 ignore_mouse_drag_p = 1;
16263 #endif
16264 }
16265 #endif
16266 }
16267
16268 #ifdef HAVE_WINDOW_SYSTEM
16269 if (FRAME_WINDOW_P (f)
16270 && update_window_fringes (w, (just_this_one_p
16271 || (!used_current_matrix_p && !overlay_arrow_seen)
16272 || w->pseudo_window_p)))
16273 {
16274 update_begin (f);
16275 block_input ();
16276 if (draw_window_fringes (w, 1))
16277 x_draw_vertical_border (w);
16278 unblock_input ();
16279 update_end (f);
16280 }
16281 #endif /* HAVE_WINDOW_SYSTEM */
16282
16283 /* We go to this label, with fonts_changed_p set,
16284 if it is necessary to try again using larger glyph matrices.
16285 We have to redeem the scroll bar even in this case,
16286 because the loop in redisplay_internal expects that. */
16287 need_larger_matrices:
16288 ;
16289 finish_scroll_bars:
16290
16291 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16292 {
16293 /* Set the thumb's position and size. */
16294 set_vertical_scroll_bar (w);
16295
16296 /* Note that we actually used the scroll bar attached to this
16297 window, so it shouldn't be deleted at the end of redisplay. */
16298 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16299 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16300 }
16301
16302 /* Restore current_buffer and value of point in it. The window
16303 update may have changed the buffer, so first make sure `opoint'
16304 is still valid (Bug#6177). */
16305 if (CHARPOS (opoint) < BEGV)
16306 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16307 else if (CHARPOS (opoint) > ZV)
16308 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16309 else
16310 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16311
16312 set_buffer_internal_1 (old);
16313 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16314 shorter. This can be caused by log truncation in *Messages*. */
16315 if (CHARPOS (lpoint) <= ZV)
16316 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16317
16318 unbind_to (count, Qnil);
16319 }
16320
16321
16322 /* Build the complete desired matrix of WINDOW with a window start
16323 buffer position POS.
16324
16325 Value is 1 if successful. It is zero if fonts were loaded during
16326 redisplay which makes re-adjusting glyph matrices necessary, and -1
16327 if point would appear in the scroll margins.
16328 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16329 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16330 set in FLAGS.) */
16331
16332 int
16333 try_window (Lisp_Object window, struct text_pos pos, int flags)
16334 {
16335 struct window *w = XWINDOW (window);
16336 struct it it;
16337 struct glyph_row *last_text_row = NULL;
16338 struct frame *f = XFRAME (w->frame);
16339
16340 /* Make POS the new window start. */
16341 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16342
16343 /* Mark cursor position as unknown. No overlay arrow seen. */
16344 w->cursor.vpos = -1;
16345 overlay_arrow_seen = 0;
16346
16347 /* Initialize iterator and info to start at POS. */
16348 start_display (&it, w, pos);
16349
16350 /* Display all lines of W. */
16351 while (it.current_y < it.last_visible_y)
16352 {
16353 if (display_line (&it))
16354 last_text_row = it.glyph_row - 1;
16355 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16356 return 0;
16357 }
16358
16359 /* Don't let the cursor end in the scroll margins. */
16360 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16361 && !MINI_WINDOW_P (w))
16362 {
16363 int this_scroll_margin;
16364
16365 if (scroll_margin > 0)
16366 {
16367 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
16368 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
16369 }
16370 else
16371 this_scroll_margin = 0;
16372
16373 if ((w->cursor.y >= 0 /* not vscrolled */
16374 && w->cursor.y < this_scroll_margin
16375 && CHARPOS (pos) > BEGV
16376 && IT_CHARPOS (it) < ZV)
16377 /* rms: considering make_cursor_line_fully_visible_p here
16378 seems to give wrong results. We don't want to recenter
16379 when the last line is partly visible, we want to allow
16380 that case to be handled in the usual way. */
16381 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16382 {
16383 w->cursor.vpos = -1;
16384 clear_glyph_matrix (w->desired_matrix);
16385 return -1;
16386 }
16387 }
16388
16389 /* If bottom moved off end of frame, change mode line percentage. */
16390 if (XFASTINT (w->window_end_pos) <= 0
16391 && Z != IT_CHARPOS (it))
16392 w->update_mode_line = 1;
16393
16394 /* Set window_end_pos to the offset of the last character displayed
16395 on the window from the end of current_buffer. Set
16396 window_end_vpos to its row number. */
16397 if (last_text_row)
16398 {
16399 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16400 w->window_end_bytepos
16401 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16402 wset_window_end_pos
16403 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16404 wset_window_end_vpos
16405 (w, make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix)));
16406 eassert
16407 (MATRIX_ROW (w->desired_matrix,
16408 XFASTINT (w->window_end_vpos))->displays_text_p);
16409 }
16410 else
16411 {
16412 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16413 wset_window_end_pos (w, make_number (Z - ZV));
16414 wset_window_end_vpos (w, make_number (0));
16415 }
16416
16417 /* But that is not valid info until redisplay finishes. */
16418 wset_window_end_valid (w, Qnil);
16419 return 1;
16420 }
16421
16422
16423 \f
16424 /************************************************************************
16425 Window redisplay reusing current matrix when buffer has not changed
16426 ************************************************************************/
16427
16428 /* Try redisplay of window W showing an unchanged buffer with a
16429 different window start than the last time it was displayed by
16430 reusing its current matrix. Value is non-zero if successful.
16431 W->start is the new window start. */
16432
16433 static int
16434 try_window_reusing_current_matrix (struct window *w)
16435 {
16436 struct frame *f = XFRAME (w->frame);
16437 struct glyph_row *bottom_row;
16438 struct it it;
16439 struct run run;
16440 struct text_pos start, new_start;
16441 int nrows_scrolled, i;
16442 struct glyph_row *last_text_row;
16443 struct glyph_row *last_reused_text_row;
16444 struct glyph_row *start_row;
16445 int start_vpos, min_y, max_y;
16446
16447 #ifdef GLYPH_DEBUG
16448 if (inhibit_try_window_reusing)
16449 return 0;
16450 #endif
16451
16452 if (/* This function doesn't handle terminal frames. */
16453 !FRAME_WINDOW_P (f)
16454 /* Don't try to reuse the display if windows have been split
16455 or such. */
16456 || windows_or_buffers_changed
16457 || cursor_type_changed)
16458 return 0;
16459
16460 /* Can't do this if region may have changed. */
16461 if ((!NILP (Vtransient_mark_mode)
16462 && !NILP (BVAR (current_buffer, mark_active)))
16463 || !NILP (w->region_showing)
16464 || !NILP (Vshow_trailing_whitespace))
16465 return 0;
16466
16467 /* If top-line visibility has changed, give up. */
16468 if (WINDOW_WANTS_HEADER_LINE_P (w)
16469 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16470 return 0;
16471
16472 /* Give up if old or new display is scrolled vertically. We could
16473 make this function handle this, but right now it doesn't. */
16474 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16475 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16476 return 0;
16477
16478 /* The variable new_start now holds the new window start. The old
16479 start `start' can be determined from the current matrix. */
16480 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16481 start = start_row->minpos;
16482 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16483
16484 /* Clear the desired matrix for the display below. */
16485 clear_glyph_matrix (w->desired_matrix);
16486
16487 if (CHARPOS (new_start) <= CHARPOS (start))
16488 {
16489 /* Don't use this method if the display starts with an ellipsis
16490 displayed for invisible text. It's not easy to handle that case
16491 below, and it's certainly not worth the effort since this is
16492 not a frequent case. */
16493 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16494 return 0;
16495
16496 IF_DEBUG (debug_method_add (w, "twu1"));
16497
16498 /* Display up to a row that can be reused. The variable
16499 last_text_row is set to the last row displayed that displays
16500 text. Note that it.vpos == 0 if or if not there is a
16501 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16502 start_display (&it, w, new_start);
16503 w->cursor.vpos = -1;
16504 last_text_row = last_reused_text_row = NULL;
16505
16506 while (it.current_y < it.last_visible_y
16507 && !fonts_changed_p)
16508 {
16509 /* If we have reached into the characters in the START row,
16510 that means the line boundaries have changed. So we
16511 can't start copying with the row START. Maybe it will
16512 work to start copying with the following row. */
16513 while (IT_CHARPOS (it) > CHARPOS (start))
16514 {
16515 /* Advance to the next row as the "start". */
16516 start_row++;
16517 start = start_row->minpos;
16518 /* If there are no more rows to try, or just one, give up. */
16519 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16520 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16521 || CHARPOS (start) == ZV)
16522 {
16523 clear_glyph_matrix (w->desired_matrix);
16524 return 0;
16525 }
16526
16527 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16528 }
16529 /* If we have reached alignment, we can copy the rest of the
16530 rows. */
16531 if (IT_CHARPOS (it) == CHARPOS (start)
16532 /* Don't accept "alignment" inside a display vector,
16533 since start_row could have started in the middle of
16534 that same display vector (thus their character
16535 positions match), and we have no way of telling if
16536 that is the case. */
16537 && it.current.dpvec_index < 0)
16538 break;
16539
16540 if (display_line (&it))
16541 last_text_row = it.glyph_row - 1;
16542
16543 }
16544
16545 /* A value of current_y < last_visible_y means that we stopped
16546 at the previous window start, which in turn means that we
16547 have at least one reusable row. */
16548 if (it.current_y < it.last_visible_y)
16549 {
16550 struct glyph_row *row;
16551
16552 /* IT.vpos always starts from 0; it counts text lines. */
16553 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16554
16555 /* Find PT if not already found in the lines displayed. */
16556 if (w->cursor.vpos < 0)
16557 {
16558 int dy = it.current_y - start_row->y;
16559
16560 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16561 row = row_containing_pos (w, PT, row, NULL, dy);
16562 if (row)
16563 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16564 dy, nrows_scrolled);
16565 else
16566 {
16567 clear_glyph_matrix (w->desired_matrix);
16568 return 0;
16569 }
16570 }
16571
16572 /* Scroll the display. Do it before the current matrix is
16573 changed. The problem here is that update has not yet
16574 run, i.e. part of the current matrix is not up to date.
16575 scroll_run_hook will clear the cursor, and use the
16576 current matrix to get the height of the row the cursor is
16577 in. */
16578 run.current_y = start_row->y;
16579 run.desired_y = it.current_y;
16580 run.height = it.last_visible_y - it.current_y;
16581
16582 if (run.height > 0 && run.current_y != run.desired_y)
16583 {
16584 update_begin (f);
16585 FRAME_RIF (f)->update_window_begin_hook (w);
16586 FRAME_RIF (f)->clear_window_mouse_face (w);
16587 FRAME_RIF (f)->scroll_run_hook (w, &run);
16588 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16589 update_end (f);
16590 }
16591
16592 /* Shift current matrix down by nrows_scrolled lines. */
16593 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16594 rotate_matrix (w->current_matrix,
16595 start_vpos,
16596 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16597 nrows_scrolled);
16598
16599 /* Disable lines that must be updated. */
16600 for (i = 0; i < nrows_scrolled; ++i)
16601 (start_row + i)->enabled_p = 0;
16602
16603 /* Re-compute Y positions. */
16604 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16605 max_y = it.last_visible_y;
16606 for (row = start_row + nrows_scrolled;
16607 row < bottom_row;
16608 ++row)
16609 {
16610 row->y = it.current_y;
16611 row->visible_height = row->height;
16612
16613 if (row->y < min_y)
16614 row->visible_height -= min_y - row->y;
16615 if (row->y + row->height > max_y)
16616 row->visible_height -= row->y + row->height - max_y;
16617 if (row->fringe_bitmap_periodic_p)
16618 row->redraw_fringe_bitmaps_p = 1;
16619
16620 it.current_y += row->height;
16621
16622 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16623 last_reused_text_row = row;
16624 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16625 break;
16626 }
16627
16628 /* Disable lines in the current matrix which are now
16629 below the window. */
16630 for (++row; row < bottom_row; ++row)
16631 row->enabled_p = row->mode_line_p = 0;
16632 }
16633
16634 /* Update window_end_pos etc.; last_reused_text_row is the last
16635 reused row from the current matrix containing text, if any.
16636 The value of last_text_row is the last displayed line
16637 containing text. */
16638 if (last_reused_text_row)
16639 {
16640 w->window_end_bytepos
16641 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
16642 wset_window_end_pos
16643 (w, make_number (Z
16644 - MATRIX_ROW_END_CHARPOS (last_reused_text_row)));
16645 wset_window_end_vpos
16646 (w, make_number (MATRIX_ROW_VPOS (last_reused_text_row,
16647 w->current_matrix)));
16648 }
16649 else if (last_text_row)
16650 {
16651 w->window_end_bytepos
16652 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16653 wset_window_end_pos
16654 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16655 wset_window_end_vpos
16656 (w, make_number (MATRIX_ROW_VPOS (last_text_row,
16657 w->desired_matrix)));
16658 }
16659 else
16660 {
16661 /* This window must be completely empty. */
16662 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16663 wset_window_end_pos (w, make_number (Z - ZV));
16664 wset_window_end_vpos (w, make_number (0));
16665 }
16666 wset_window_end_valid (w, Qnil);
16667
16668 /* Update hint: don't try scrolling again in update_window. */
16669 w->desired_matrix->no_scrolling_p = 1;
16670
16671 #ifdef GLYPH_DEBUG
16672 debug_method_add (w, "try_window_reusing_current_matrix 1");
16673 #endif
16674 return 1;
16675 }
16676 else if (CHARPOS (new_start) > CHARPOS (start))
16677 {
16678 struct glyph_row *pt_row, *row;
16679 struct glyph_row *first_reusable_row;
16680 struct glyph_row *first_row_to_display;
16681 int dy;
16682 int yb = window_text_bottom_y (w);
16683
16684 /* Find the row starting at new_start, if there is one. Don't
16685 reuse a partially visible line at the end. */
16686 first_reusable_row = start_row;
16687 while (first_reusable_row->enabled_p
16688 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16689 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16690 < CHARPOS (new_start)))
16691 ++first_reusable_row;
16692
16693 /* Give up if there is no row to reuse. */
16694 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16695 || !first_reusable_row->enabled_p
16696 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16697 != CHARPOS (new_start)))
16698 return 0;
16699
16700 /* We can reuse fully visible rows beginning with
16701 first_reusable_row to the end of the window. Set
16702 first_row_to_display to the first row that cannot be reused.
16703 Set pt_row to the row containing point, if there is any. */
16704 pt_row = NULL;
16705 for (first_row_to_display = first_reusable_row;
16706 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16707 ++first_row_to_display)
16708 {
16709 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16710 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16711 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16712 && first_row_to_display->ends_at_zv_p
16713 && pt_row == NULL)))
16714 pt_row = first_row_to_display;
16715 }
16716
16717 /* Start displaying at the start of first_row_to_display. */
16718 eassert (first_row_to_display->y < yb);
16719 init_to_row_start (&it, w, first_row_to_display);
16720
16721 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16722 - start_vpos);
16723 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16724 - nrows_scrolled);
16725 it.current_y = (first_row_to_display->y - first_reusable_row->y
16726 + WINDOW_HEADER_LINE_HEIGHT (w));
16727
16728 /* Display lines beginning with first_row_to_display in the
16729 desired matrix. Set last_text_row to the last row displayed
16730 that displays text. */
16731 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16732 if (pt_row == NULL)
16733 w->cursor.vpos = -1;
16734 last_text_row = NULL;
16735 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16736 if (display_line (&it))
16737 last_text_row = it.glyph_row - 1;
16738
16739 /* If point is in a reused row, adjust y and vpos of the cursor
16740 position. */
16741 if (pt_row)
16742 {
16743 w->cursor.vpos -= nrows_scrolled;
16744 w->cursor.y -= first_reusable_row->y - start_row->y;
16745 }
16746
16747 /* Give up if point isn't in a row displayed or reused. (This
16748 also handles the case where w->cursor.vpos < nrows_scrolled
16749 after the calls to display_line, which can happen with scroll
16750 margins. See bug#1295.) */
16751 if (w->cursor.vpos < 0)
16752 {
16753 clear_glyph_matrix (w->desired_matrix);
16754 return 0;
16755 }
16756
16757 /* Scroll the display. */
16758 run.current_y = first_reusable_row->y;
16759 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16760 run.height = it.last_visible_y - run.current_y;
16761 dy = run.current_y - run.desired_y;
16762
16763 if (run.height)
16764 {
16765 update_begin (f);
16766 FRAME_RIF (f)->update_window_begin_hook (w);
16767 FRAME_RIF (f)->clear_window_mouse_face (w);
16768 FRAME_RIF (f)->scroll_run_hook (w, &run);
16769 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16770 update_end (f);
16771 }
16772
16773 /* Adjust Y positions of reused rows. */
16774 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16775 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16776 max_y = it.last_visible_y;
16777 for (row = first_reusable_row; row < first_row_to_display; ++row)
16778 {
16779 row->y -= dy;
16780 row->visible_height = row->height;
16781 if (row->y < min_y)
16782 row->visible_height -= min_y - row->y;
16783 if (row->y + row->height > max_y)
16784 row->visible_height -= row->y + row->height - max_y;
16785 if (row->fringe_bitmap_periodic_p)
16786 row->redraw_fringe_bitmaps_p = 1;
16787 }
16788
16789 /* Scroll the current matrix. */
16790 eassert (nrows_scrolled > 0);
16791 rotate_matrix (w->current_matrix,
16792 start_vpos,
16793 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16794 -nrows_scrolled);
16795
16796 /* Disable rows not reused. */
16797 for (row -= nrows_scrolled; row < bottom_row; ++row)
16798 row->enabled_p = 0;
16799
16800 /* Point may have moved to a different line, so we cannot assume that
16801 the previous cursor position is valid; locate the correct row. */
16802 if (pt_row)
16803 {
16804 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16805 row < bottom_row
16806 && PT >= MATRIX_ROW_END_CHARPOS (row)
16807 && !row->ends_at_zv_p;
16808 row++)
16809 {
16810 w->cursor.vpos++;
16811 w->cursor.y = row->y;
16812 }
16813 if (row < bottom_row)
16814 {
16815 /* Can't simply scan the row for point with
16816 bidi-reordered glyph rows. Let set_cursor_from_row
16817 figure out where to put the cursor, and if it fails,
16818 give up. */
16819 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
16820 {
16821 if (!set_cursor_from_row (w, row, w->current_matrix,
16822 0, 0, 0, 0))
16823 {
16824 clear_glyph_matrix (w->desired_matrix);
16825 return 0;
16826 }
16827 }
16828 else
16829 {
16830 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16831 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16832
16833 for (; glyph < end
16834 && (!BUFFERP (glyph->object)
16835 || glyph->charpos < PT);
16836 glyph++)
16837 {
16838 w->cursor.hpos++;
16839 w->cursor.x += glyph->pixel_width;
16840 }
16841 }
16842 }
16843 }
16844
16845 /* Adjust window end. A null value of last_text_row means that
16846 the window end is in reused rows which in turn means that
16847 only its vpos can have changed. */
16848 if (last_text_row)
16849 {
16850 w->window_end_bytepos
16851 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16852 wset_window_end_pos
16853 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16854 wset_window_end_vpos
16855 (w, make_number (MATRIX_ROW_VPOS (last_text_row,
16856 w->desired_matrix)));
16857 }
16858 else
16859 {
16860 wset_window_end_vpos
16861 (w, make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled));
16862 }
16863
16864 wset_window_end_valid (w, Qnil);
16865 w->desired_matrix->no_scrolling_p = 1;
16866
16867 #ifdef GLYPH_DEBUG
16868 debug_method_add (w, "try_window_reusing_current_matrix 2");
16869 #endif
16870 return 1;
16871 }
16872
16873 return 0;
16874 }
16875
16876
16877 \f
16878 /************************************************************************
16879 Window redisplay reusing current matrix when buffer has changed
16880 ************************************************************************/
16881
16882 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16883 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16884 ptrdiff_t *, ptrdiff_t *);
16885 static struct glyph_row *
16886 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16887 struct glyph_row *);
16888
16889
16890 /* Return the last row in MATRIX displaying text. If row START is
16891 non-null, start searching with that row. IT gives the dimensions
16892 of the display. Value is null if matrix is empty; otherwise it is
16893 a pointer to the row found. */
16894
16895 static struct glyph_row *
16896 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16897 struct glyph_row *start)
16898 {
16899 struct glyph_row *row, *row_found;
16900
16901 /* Set row_found to the last row in IT->w's current matrix
16902 displaying text. The loop looks funny but think of partially
16903 visible lines. */
16904 row_found = NULL;
16905 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16906 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16907 {
16908 eassert (row->enabled_p);
16909 row_found = row;
16910 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16911 break;
16912 ++row;
16913 }
16914
16915 return row_found;
16916 }
16917
16918
16919 /* Return the last row in the current matrix of W that is not affected
16920 by changes at the start of current_buffer that occurred since W's
16921 current matrix was built. Value is null if no such row exists.
16922
16923 BEG_UNCHANGED us the number of characters unchanged at the start of
16924 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16925 first changed character in current_buffer. Characters at positions <
16926 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16927 when the current matrix was built. */
16928
16929 static struct glyph_row *
16930 find_last_unchanged_at_beg_row (struct window *w)
16931 {
16932 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16933 struct glyph_row *row;
16934 struct glyph_row *row_found = NULL;
16935 int yb = window_text_bottom_y (w);
16936
16937 /* Find the last row displaying unchanged text. */
16938 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16939 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16940 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16941 ++row)
16942 {
16943 if (/* If row ends before first_changed_pos, it is unchanged,
16944 except in some case. */
16945 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16946 /* When row ends in ZV and we write at ZV it is not
16947 unchanged. */
16948 && !row->ends_at_zv_p
16949 /* When first_changed_pos is the end of a continued line,
16950 row is not unchanged because it may be no longer
16951 continued. */
16952 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16953 && (row->continued_p
16954 || row->exact_window_width_line_p))
16955 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16956 needs to be recomputed, so don't consider this row as
16957 unchanged. This happens when the last line was
16958 bidi-reordered and was killed immediately before this
16959 redisplay cycle. In that case, ROW->end stores the
16960 buffer position of the first visual-order character of
16961 the killed text, which is now beyond ZV. */
16962 && CHARPOS (row->end.pos) <= ZV)
16963 row_found = row;
16964
16965 /* Stop if last visible row. */
16966 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16967 break;
16968 }
16969
16970 return row_found;
16971 }
16972
16973
16974 /* Find the first glyph row in the current matrix of W that is not
16975 affected by changes at the end of current_buffer since the
16976 time W's current matrix was built.
16977
16978 Return in *DELTA the number of chars by which buffer positions in
16979 unchanged text at the end of current_buffer must be adjusted.
16980
16981 Return in *DELTA_BYTES the corresponding number of bytes.
16982
16983 Value is null if no such row exists, i.e. all rows are affected by
16984 changes. */
16985
16986 static struct glyph_row *
16987 find_first_unchanged_at_end_row (struct window *w,
16988 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16989 {
16990 struct glyph_row *row;
16991 struct glyph_row *row_found = NULL;
16992
16993 *delta = *delta_bytes = 0;
16994
16995 /* Display must not have been paused, otherwise the current matrix
16996 is not up to date. */
16997 eassert (!NILP (w->window_end_valid));
16998
16999 /* A value of window_end_pos >= END_UNCHANGED means that the window
17000 end is in the range of changed text. If so, there is no
17001 unchanged row at the end of W's current matrix. */
17002 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
17003 return NULL;
17004
17005 /* Set row to the last row in W's current matrix displaying text. */
17006 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
17007
17008 /* If matrix is entirely empty, no unchanged row exists. */
17009 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17010 {
17011 /* The value of row is the last glyph row in the matrix having a
17012 meaningful buffer position in it. The end position of row
17013 corresponds to window_end_pos. This allows us to translate
17014 buffer positions in the current matrix to current buffer
17015 positions for characters not in changed text. */
17016 ptrdiff_t Z_old =
17017 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
17018 ptrdiff_t Z_BYTE_old =
17019 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17020 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
17021 struct glyph_row *first_text_row
17022 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17023
17024 *delta = Z - Z_old;
17025 *delta_bytes = Z_BYTE - Z_BYTE_old;
17026
17027 /* Set last_unchanged_pos to the buffer position of the last
17028 character in the buffer that has not been changed. Z is the
17029 index + 1 of the last character in current_buffer, i.e. by
17030 subtracting END_UNCHANGED we get the index of the last
17031 unchanged character, and we have to add BEG to get its buffer
17032 position. */
17033 last_unchanged_pos = Z - END_UNCHANGED + BEG;
17034 last_unchanged_pos_old = last_unchanged_pos - *delta;
17035
17036 /* Search backward from ROW for a row displaying a line that
17037 starts at a minimum position >= last_unchanged_pos_old. */
17038 for (; row > first_text_row; --row)
17039 {
17040 /* This used to abort, but it can happen.
17041 It is ok to just stop the search instead here. KFS. */
17042 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
17043 break;
17044
17045 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
17046 row_found = row;
17047 }
17048 }
17049
17050 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
17051
17052 return row_found;
17053 }
17054
17055
17056 /* Make sure that glyph rows in the current matrix of window W
17057 reference the same glyph memory as corresponding rows in the
17058 frame's frame matrix. This function is called after scrolling W's
17059 current matrix on a terminal frame in try_window_id and
17060 try_window_reusing_current_matrix. */
17061
17062 static void
17063 sync_frame_with_window_matrix_rows (struct window *w)
17064 {
17065 struct frame *f = XFRAME (w->frame);
17066 struct glyph_row *window_row, *window_row_end, *frame_row;
17067
17068 /* Preconditions: W must be a leaf window and full-width. Its frame
17069 must have a frame matrix. */
17070 eassert (NILP (w->hchild) && NILP (w->vchild));
17071 eassert (WINDOW_FULL_WIDTH_P (w));
17072 eassert (!FRAME_WINDOW_P (f));
17073
17074 /* If W is a full-width window, glyph pointers in W's current matrix
17075 have, by definition, to be the same as glyph pointers in the
17076 corresponding frame matrix. Note that frame matrices have no
17077 marginal areas (see build_frame_matrix). */
17078 window_row = w->current_matrix->rows;
17079 window_row_end = window_row + w->current_matrix->nrows;
17080 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17081 while (window_row < window_row_end)
17082 {
17083 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17084 struct glyph *end = window_row->glyphs[LAST_AREA];
17085
17086 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17087 frame_row->glyphs[TEXT_AREA] = start;
17088 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17089 frame_row->glyphs[LAST_AREA] = end;
17090
17091 /* Disable frame rows whose corresponding window rows have
17092 been disabled in try_window_id. */
17093 if (!window_row->enabled_p)
17094 frame_row->enabled_p = 0;
17095
17096 ++window_row, ++frame_row;
17097 }
17098 }
17099
17100
17101 /* Find the glyph row in window W containing CHARPOS. Consider all
17102 rows between START and END (not inclusive). END null means search
17103 all rows to the end of the display area of W. Value is the row
17104 containing CHARPOS or null. */
17105
17106 struct glyph_row *
17107 row_containing_pos (struct window *w, ptrdiff_t charpos,
17108 struct glyph_row *start, struct glyph_row *end, int dy)
17109 {
17110 struct glyph_row *row = start;
17111 struct glyph_row *best_row = NULL;
17112 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
17113 int last_y;
17114
17115 /* If we happen to start on a header-line, skip that. */
17116 if (row->mode_line_p)
17117 ++row;
17118
17119 if ((end && row >= end) || !row->enabled_p)
17120 return NULL;
17121
17122 last_y = window_text_bottom_y (w) - dy;
17123
17124 while (1)
17125 {
17126 /* Give up if we have gone too far. */
17127 if (end && row >= end)
17128 return NULL;
17129 /* This formerly returned if they were equal.
17130 I think that both quantities are of a "last plus one" type;
17131 if so, when they are equal, the row is within the screen. -- rms. */
17132 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17133 return NULL;
17134
17135 /* If it is in this row, return this row. */
17136 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17137 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17138 /* The end position of a row equals the start
17139 position of the next row. If CHARPOS is there, we
17140 would rather display it in the next line, except
17141 when this line ends in ZV. */
17142 && !row->ends_at_zv_p
17143 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
17144 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17145 {
17146 struct glyph *g;
17147
17148 if (NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
17149 || (!best_row && !row->continued_p))
17150 return row;
17151 /* In bidi-reordered rows, there could be several rows
17152 occluding point, all of them belonging to the same
17153 continued line. We need to find the row which fits
17154 CHARPOS the best. */
17155 for (g = row->glyphs[TEXT_AREA];
17156 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17157 g++)
17158 {
17159 if (!STRINGP (g->object))
17160 {
17161 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17162 {
17163 mindif = eabs (g->charpos - charpos);
17164 best_row = row;
17165 /* Exact match always wins. */
17166 if (mindif == 0)
17167 return best_row;
17168 }
17169 }
17170 }
17171 }
17172 else if (best_row && !row->continued_p)
17173 return best_row;
17174 ++row;
17175 }
17176 }
17177
17178
17179 /* Try to redisplay window W by reusing its existing display. W's
17180 current matrix must be up to date when this function is called,
17181 i.e. window_end_valid must not be nil.
17182
17183 Value is
17184
17185 1 if display has been updated
17186 0 if otherwise unsuccessful
17187 -1 if redisplay with same window start is known not to succeed
17188
17189 The following steps are performed:
17190
17191 1. Find the last row in the current matrix of W that is not
17192 affected by changes at the start of current_buffer. If no such row
17193 is found, give up.
17194
17195 2. Find the first row in W's current matrix that is not affected by
17196 changes at the end of current_buffer. Maybe there is no such row.
17197
17198 3. Display lines beginning with the row + 1 found in step 1 to the
17199 row found in step 2 or, if step 2 didn't find a row, to the end of
17200 the window.
17201
17202 4. If cursor is not known to appear on the window, give up.
17203
17204 5. If display stopped at the row found in step 2, scroll the
17205 display and current matrix as needed.
17206
17207 6. Maybe display some lines at the end of W, if we must. This can
17208 happen under various circumstances, like a partially visible line
17209 becoming fully visible, or because newly displayed lines are displayed
17210 in smaller font sizes.
17211
17212 7. Update W's window end information. */
17213
17214 static int
17215 try_window_id (struct window *w)
17216 {
17217 struct frame *f = XFRAME (w->frame);
17218 struct glyph_matrix *current_matrix = w->current_matrix;
17219 struct glyph_matrix *desired_matrix = w->desired_matrix;
17220 struct glyph_row *last_unchanged_at_beg_row;
17221 struct glyph_row *first_unchanged_at_end_row;
17222 struct glyph_row *row;
17223 struct glyph_row *bottom_row;
17224 int bottom_vpos;
17225 struct it it;
17226 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17227 int dvpos, dy;
17228 struct text_pos start_pos;
17229 struct run run;
17230 int first_unchanged_at_end_vpos = 0;
17231 struct glyph_row *last_text_row, *last_text_row_at_end;
17232 struct text_pos start;
17233 ptrdiff_t first_changed_charpos, last_changed_charpos;
17234
17235 #ifdef GLYPH_DEBUG
17236 if (inhibit_try_window_id)
17237 return 0;
17238 #endif
17239
17240 /* This is handy for debugging. */
17241 #if 0
17242 #define GIVE_UP(X) \
17243 do { \
17244 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17245 return 0; \
17246 } while (0)
17247 #else
17248 #define GIVE_UP(X) return 0
17249 #endif
17250
17251 SET_TEXT_POS_FROM_MARKER (start, w->start);
17252
17253 /* Don't use this for mini-windows because these can show
17254 messages and mini-buffers, and we don't handle that here. */
17255 if (MINI_WINDOW_P (w))
17256 GIVE_UP (1);
17257
17258 /* This flag is used to prevent redisplay optimizations. */
17259 if (windows_or_buffers_changed || cursor_type_changed)
17260 GIVE_UP (2);
17261
17262 /* Verify that narrowing has not changed.
17263 Also verify that we were not told to prevent redisplay optimizations.
17264 It would be nice to further
17265 reduce the number of cases where this prevents try_window_id. */
17266 if (current_buffer->clip_changed
17267 || current_buffer->prevent_redisplay_optimizations_p)
17268 GIVE_UP (3);
17269
17270 /* Window must either use window-based redisplay or be full width. */
17271 if (!FRAME_WINDOW_P (f)
17272 && (!FRAME_LINE_INS_DEL_OK (f)
17273 || !WINDOW_FULL_WIDTH_P (w)))
17274 GIVE_UP (4);
17275
17276 /* Give up if point is known NOT to appear in W. */
17277 if (PT < CHARPOS (start))
17278 GIVE_UP (5);
17279
17280 /* Another way to prevent redisplay optimizations. */
17281 if (w->last_modified == 0)
17282 GIVE_UP (6);
17283
17284 /* Verify that window is not hscrolled. */
17285 if (w->hscroll != 0)
17286 GIVE_UP (7);
17287
17288 /* Verify that display wasn't paused. */
17289 if (NILP (w->window_end_valid))
17290 GIVE_UP (8);
17291
17292 /* Can't use this if highlighting a region because a cursor movement
17293 will do more than just set the cursor. */
17294 if (!NILP (Vtransient_mark_mode)
17295 && !NILP (BVAR (current_buffer, mark_active)))
17296 GIVE_UP (9);
17297
17298 /* Likewise if highlighting trailing whitespace. */
17299 if (!NILP (Vshow_trailing_whitespace))
17300 GIVE_UP (11);
17301
17302 /* Likewise if showing a region. */
17303 if (!NILP (w->region_showing))
17304 GIVE_UP (10);
17305
17306 /* Can't use this if overlay arrow position and/or string have
17307 changed. */
17308 if (overlay_arrows_changed_p ())
17309 GIVE_UP (12);
17310
17311 /* When word-wrap is on, adding a space to the first word of a
17312 wrapped line can change the wrap position, altering the line
17313 above it. It might be worthwhile to handle this more
17314 intelligently, but for now just redisplay from scratch. */
17315 if (!NILP (BVAR (XBUFFER (w->buffer), word_wrap)))
17316 GIVE_UP (21);
17317
17318 /* Under bidi reordering, adding or deleting a character in the
17319 beginning of a paragraph, before the first strong directional
17320 character, can change the base direction of the paragraph (unless
17321 the buffer specifies a fixed paragraph direction), which will
17322 require to redisplay the whole paragraph. It might be worthwhile
17323 to find the paragraph limits and widen the range of redisplayed
17324 lines to that, but for now just give up this optimization and
17325 redisplay from scratch. */
17326 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
17327 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
17328 GIVE_UP (22);
17329
17330 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17331 only if buffer has really changed. The reason is that the gap is
17332 initially at Z for freshly visited files. The code below would
17333 set end_unchanged to 0 in that case. */
17334 if (MODIFF > SAVE_MODIFF
17335 /* This seems to happen sometimes after saving a buffer. */
17336 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17337 {
17338 if (GPT - BEG < BEG_UNCHANGED)
17339 BEG_UNCHANGED = GPT - BEG;
17340 if (Z - GPT < END_UNCHANGED)
17341 END_UNCHANGED = Z - GPT;
17342 }
17343
17344 /* The position of the first and last character that has been changed. */
17345 first_changed_charpos = BEG + BEG_UNCHANGED;
17346 last_changed_charpos = Z - END_UNCHANGED;
17347
17348 /* If window starts after a line end, and the last change is in
17349 front of that newline, then changes don't affect the display.
17350 This case happens with stealth-fontification. Note that although
17351 the display is unchanged, glyph positions in the matrix have to
17352 be adjusted, of course. */
17353 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
17354 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17355 && ((last_changed_charpos < CHARPOS (start)
17356 && CHARPOS (start) == BEGV)
17357 || (last_changed_charpos < CHARPOS (start) - 1
17358 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17359 {
17360 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17361 struct glyph_row *r0;
17362
17363 /* Compute how many chars/bytes have been added to or removed
17364 from the buffer. */
17365 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
17366 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17367 Z_delta = Z - Z_old;
17368 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17369
17370 /* Give up if PT is not in the window. Note that it already has
17371 been checked at the start of try_window_id that PT is not in
17372 front of the window start. */
17373 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17374 GIVE_UP (13);
17375
17376 /* If window start is unchanged, we can reuse the whole matrix
17377 as is, after adjusting glyph positions. No need to compute
17378 the window end again, since its offset from Z hasn't changed. */
17379 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17380 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17381 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17382 /* PT must not be in a partially visible line. */
17383 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17384 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17385 {
17386 /* Adjust positions in the glyph matrix. */
17387 if (Z_delta || Z_delta_bytes)
17388 {
17389 struct glyph_row *r1
17390 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17391 increment_matrix_positions (w->current_matrix,
17392 MATRIX_ROW_VPOS (r0, current_matrix),
17393 MATRIX_ROW_VPOS (r1, current_matrix),
17394 Z_delta, Z_delta_bytes);
17395 }
17396
17397 /* Set the cursor. */
17398 row = row_containing_pos (w, PT, r0, NULL, 0);
17399 if (row)
17400 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17401 else
17402 emacs_abort ();
17403 return 1;
17404 }
17405 }
17406
17407 /* Handle the case that changes are all below what is displayed in
17408 the window, and that PT is in the window. This shortcut cannot
17409 be taken if ZV is visible in the window, and text has been added
17410 there that is visible in the window. */
17411 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17412 /* ZV is not visible in the window, or there are no
17413 changes at ZV, actually. */
17414 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17415 || first_changed_charpos == last_changed_charpos))
17416 {
17417 struct glyph_row *r0;
17418
17419 /* Give up if PT is not in the window. Note that it already has
17420 been checked at the start of try_window_id that PT is not in
17421 front of the window start. */
17422 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17423 GIVE_UP (14);
17424
17425 /* If window start is unchanged, we can reuse the whole matrix
17426 as is, without changing glyph positions since no text has
17427 been added/removed in front of the window end. */
17428 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17429 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17430 /* PT must not be in a partially visible line. */
17431 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17432 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17433 {
17434 /* We have to compute the window end anew since text
17435 could have been added/removed after it. */
17436 wset_window_end_pos
17437 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17438 w->window_end_bytepos
17439 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17440
17441 /* Set the cursor. */
17442 row = row_containing_pos (w, PT, r0, NULL, 0);
17443 if (row)
17444 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17445 else
17446 emacs_abort ();
17447 return 2;
17448 }
17449 }
17450
17451 /* Give up if window start is in the changed area.
17452
17453 The condition used to read
17454
17455 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17456
17457 but why that was tested escapes me at the moment. */
17458 if (CHARPOS (start) >= first_changed_charpos
17459 && CHARPOS (start) <= last_changed_charpos)
17460 GIVE_UP (15);
17461
17462 /* Check that window start agrees with the start of the first glyph
17463 row in its current matrix. Check this after we know the window
17464 start is not in changed text, otherwise positions would not be
17465 comparable. */
17466 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17467 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17468 GIVE_UP (16);
17469
17470 /* Give up if the window ends in strings. Overlay strings
17471 at the end are difficult to handle, so don't try. */
17472 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
17473 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17474 GIVE_UP (20);
17475
17476 /* Compute the position at which we have to start displaying new
17477 lines. Some of the lines at the top of the window might be
17478 reusable because they are not displaying changed text. Find the
17479 last row in W's current matrix not affected by changes at the
17480 start of current_buffer. Value is null if changes start in the
17481 first line of window. */
17482 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17483 if (last_unchanged_at_beg_row)
17484 {
17485 /* Avoid starting to display in the middle of a character, a TAB
17486 for instance. This is easier than to set up the iterator
17487 exactly, and it's not a frequent case, so the additional
17488 effort wouldn't really pay off. */
17489 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17490 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17491 && last_unchanged_at_beg_row > w->current_matrix->rows)
17492 --last_unchanged_at_beg_row;
17493
17494 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17495 GIVE_UP (17);
17496
17497 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17498 GIVE_UP (18);
17499 start_pos = it.current.pos;
17500
17501 /* Start displaying new lines in the desired matrix at the same
17502 vpos we would use in the current matrix, i.e. below
17503 last_unchanged_at_beg_row. */
17504 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17505 current_matrix);
17506 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17507 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17508
17509 eassert (it.hpos == 0 && it.current_x == 0);
17510 }
17511 else
17512 {
17513 /* There are no reusable lines at the start of the window.
17514 Start displaying in the first text line. */
17515 start_display (&it, w, start);
17516 it.vpos = it.first_vpos;
17517 start_pos = it.current.pos;
17518 }
17519
17520 /* Find the first row that is not affected by changes at the end of
17521 the buffer. Value will be null if there is no unchanged row, in
17522 which case we must redisplay to the end of the window. delta
17523 will be set to the value by which buffer positions beginning with
17524 first_unchanged_at_end_row have to be adjusted due to text
17525 changes. */
17526 first_unchanged_at_end_row
17527 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17528 IF_DEBUG (debug_delta = delta);
17529 IF_DEBUG (debug_delta_bytes = delta_bytes);
17530
17531 /* Set stop_pos to the buffer position up to which we will have to
17532 display new lines. If first_unchanged_at_end_row != NULL, this
17533 is the buffer position of the start of the line displayed in that
17534 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17535 that we don't stop at a buffer position. */
17536 stop_pos = 0;
17537 if (first_unchanged_at_end_row)
17538 {
17539 eassert (last_unchanged_at_beg_row == NULL
17540 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17541
17542 /* If this is a continuation line, move forward to the next one
17543 that isn't. Changes in lines above affect this line.
17544 Caution: this may move first_unchanged_at_end_row to a row
17545 not displaying text. */
17546 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17547 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17548 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17549 < it.last_visible_y))
17550 ++first_unchanged_at_end_row;
17551
17552 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17553 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17554 >= it.last_visible_y))
17555 first_unchanged_at_end_row = NULL;
17556 else
17557 {
17558 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17559 + delta);
17560 first_unchanged_at_end_vpos
17561 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17562 eassert (stop_pos >= Z - END_UNCHANGED);
17563 }
17564 }
17565 else if (last_unchanged_at_beg_row == NULL)
17566 GIVE_UP (19);
17567
17568
17569 #ifdef GLYPH_DEBUG
17570
17571 /* Either there is no unchanged row at the end, or the one we have
17572 now displays text. This is a necessary condition for the window
17573 end pos calculation at the end of this function. */
17574 eassert (first_unchanged_at_end_row == NULL
17575 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17576
17577 debug_last_unchanged_at_beg_vpos
17578 = (last_unchanged_at_beg_row
17579 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17580 : -1);
17581 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17582
17583 #endif /* GLYPH_DEBUG */
17584
17585
17586 /* Display new lines. Set last_text_row to the last new line
17587 displayed which has text on it, i.e. might end up as being the
17588 line where the window_end_vpos is. */
17589 w->cursor.vpos = -1;
17590 last_text_row = NULL;
17591 overlay_arrow_seen = 0;
17592 while (it.current_y < it.last_visible_y
17593 && !fonts_changed_p
17594 && (first_unchanged_at_end_row == NULL
17595 || IT_CHARPOS (it) < stop_pos))
17596 {
17597 if (display_line (&it))
17598 last_text_row = it.glyph_row - 1;
17599 }
17600
17601 if (fonts_changed_p)
17602 return -1;
17603
17604
17605 /* Compute differences in buffer positions, y-positions etc. for
17606 lines reused at the bottom of the window. Compute what we can
17607 scroll. */
17608 if (first_unchanged_at_end_row
17609 /* No lines reused because we displayed everything up to the
17610 bottom of the window. */
17611 && it.current_y < it.last_visible_y)
17612 {
17613 dvpos = (it.vpos
17614 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17615 current_matrix));
17616 dy = it.current_y - first_unchanged_at_end_row->y;
17617 run.current_y = first_unchanged_at_end_row->y;
17618 run.desired_y = run.current_y + dy;
17619 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17620 }
17621 else
17622 {
17623 delta = delta_bytes = dvpos = dy
17624 = run.current_y = run.desired_y = run.height = 0;
17625 first_unchanged_at_end_row = NULL;
17626 }
17627 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17628
17629
17630 /* Find the cursor if not already found. We have to decide whether
17631 PT will appear on this window (it sometimes doesn't, but this is
17632 not a very frequent case.) This decision has to be made before
17633 the current matrix is altered. A value of cursor.vpos < 0 means
17634 that PT is either in one of the lines beginning at
17635 first_unchanged_at_end_row or below the window. Don't care for
17636 lines that might be displayed later at the window end; as
17637 mentioned, this is not a frequent case. */
17638 if (w->cursor.vpos < 0)
17639 {
17640 /* Cursor in unchanged rows at the top? */
17641 if (PT < CHARPOS (start_pos)
17642 && last_unchanged_at_beg_row)
17643 {
17644 row = row_containing_pos (w, PT,
17645 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17646 last_unchanged_at_beg_row + 1, 0);
17647 if (row)
17648 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17649 }
17650
17651 /* Start from first_unchanged_at_end_row looking for PT. */
17652 else if (first_unchanged_at_end_row)
17653 {
17654 row = row_containing_pos (w, PT - delta,
17655 first_unchanged_at_end_row, NULL, 0);
17656 if (row)
17657 set_cursor_from_row (w, row, w->current_matrix, delta,
17658 delta_bytes, dy, dvpos);
17659 }
17660
17661 /* Give up if cursor was not found. */
17662 if (w->cursor.vpos < 0)
17663 {
17664 clear_glyph_matrix (w->desired_matrix);
17665 return -1;
17666 }
17667 }
17668
17669 /* Don't let the cursor end in the scroll margins. */
17670 {
17671 int this_scroll_margin, cursor_height;
17672
17673 this_scroll_margin =
17674 max (0, min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4));
17675 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
17676 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17677
17678 if ((w->cursor.y < this_scroll_margin
17679 && CHARPOS (start) > BEGV)
17680 /* Old redisplay didn't take scroll margin into account at the bottom,
17681 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17682 || (w->cursor.y + (make_cursor_line_fully_visible_p
17683 ? cursor_height + this_scroll_margin
17684 : 1)) > it.last_visible_y)
17685 {
17686 w->cursor.vpos = -1;
17687 clear_glyph_matrix (w->desired_matrix);
17688 return -1;
17689 }
17690 }
17691
17692 /* Scroll the display. Do it before changing the current matrix so
17693 that xterm.c doesn't get confused about where the cursor glyph is
17694 found. */
17695 if (dy && run.height)
17696 {
17697 update_begin (f);
17698
17699 if (FRAME_WINDOW_P (f))
17700 {
17701 FRAME_RIF (f)->update_window_begin_hook (w);
17702 FRAME_RIF (f)->clear_window_mouse_face (w);
17703 FRAME_RIF (f)->scroll_run_hook (w, &run);
17704 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17705 }
17706 else
17707 {
17708 /* Terminal frame. In this case, dvpos gives the number of
17709 lines to scroll by; dvpos < 0 means scroll up. */
17710 int from_vpos
17711 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17712 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17713 int end = (WINDOW_TOP_EDGE_LINE (w)
17714 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17715 + window_internal_height (w));
17716
17717 #if defined (HAVE_GPM) || defined (MSDOS)
17718 x_clear_window_mouse_face (w);
17719 #endif
17720 /* Perform the operation on the screen. */
17721 if (dvpos > 0)
17722 {
17723 /* Scroll last_unchanged_at_beg_row to the end of the
17724 window down dvpos lines. */
17725 set_terminal_window (f, end);
17726
17727 /* On dumb terminals delete dvpos lines at the end
17728 before inserting dvpos empty lines. */
17729 if (!FRAME_SCROLL_REGION_OK (f))
17730 ins_del_lines (f, end - dvpos, -dvpos);
17731
17732 /* Insert dvpos empty lines in front of
17733 last_unchanged_at_beg_row. */
17734 ins_del_lines (f, from, dvpos);
17735 }
17736 else if (dvpos < 0)
17737 {
17738 /* Scroll up last_unchanged_at_beg_vpos to the end of
17739 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17740 set_terminal_window (f, end);
17741
17742 /* Delete dvpos lines in front of
17743 last_unchanged_at_beg_vpos. ins_del_lines will set
17744 the cursor to the given vpos and emit |dvpos| delete
17745 line sequences. */
17746 ins_del_lines (f, from + dvpos, dvpos);
17747
17748 /* On a dumb terminal insert dvpos empty lines at the
17749 end. */
17750 if (!FRAME_SCROLL_REGION_OK (f))
17751 ins_del_lines (f, end + dvpos, -dvpos);
17752 }
17753
17754 set_terminal_window (f, 0);
17755 }
17756
17757 update_end (f);
17758 }
17759
17760 /* Shift reused rows of the current matrix to the right position.
17761 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17762 text. */
17763 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17764 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17765 if (dvpos < 0)
17766 {
17767 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17768 bottom_vpos, dvpos);
17769 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17770 bottom_vpos);
17771 }
17772 else if (dvpos > 0)
17773 {
17774 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17775 bottom_vpos, dvpos);
17776 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17777 first_unchanged_at_end_vpos + dvpos);
17778 }
17779
17780 /* For frame-based redisplay, make sure that current frame and window
17781 matrix are in sync with respect to glyph memory. */
17782 if (!FRAME_WINDOW_P (f))
17783 sync_frame_with_window_matrix_rows (w);
17784
17785 /* Adjust buffer positions in reused rows. */
17786 if (delta || delta_bytes)
17787 increment_matrix_positions (current_matrix,
17788 first_unchanged_at_end_vpos + dvpos,
17789 bottom_vpos, delta, delta_bytes);
17790
17791 /* Adjust Y positions. */
17792 if (dy)
17793 shift_glyph_matrix (w, current_matrix,
17794 first_unchanged_at_end_vpos + dvpos,
17795 bottom_vpos, dy);
17796
17797 if (first_unchanged_at_end_row)
17798 {
17799 first_unchanged_at_end_row += dvpos;
17800 if (first_unchanged_at_end_row->y >= it.last_visible_y
17801 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17802 first_unchanged_at_end_row = NULL;
17803 }
17804
17805 /* If scrolling up, there may be some lines to display at the end of
17806 the window. */
17807 last_text_row_at_end = NULL;
17808 if (dy < 0)
17809 {
17810 /* Scrolling up can leave for example a partially visible line
17811 at the end of the window to be redisplayed. */
17812 /* Set last_row to the glyph row in the current matrix where the
17813 window end line is found. It has been moved up or down in
17814 the matrix by dvpos. */
17815 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
17816 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17817
17818 /* If last_row is the window end line, it should display text. */
17819 eassert (last_row->displays_text_p);
17820
17821 /* If window end line was partially visible before, begin
17822 displaying at that line. Otherwise begin displaying with the
17823 line following it. */
17824 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17825 {
17826 init_to_row_start (&it, w, last_row);
17827 it.vpos = last_vpos;
17828 it.current_y = last_row->y;
17829 }
17830 else
17831 {
17832 init_to_row_end (&it, w, last_row);
17833 it.vpos = 1 + last_vpos;
17834 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17835 ++last_row;
17836 }
17837
17838 /* We may start in a continuation line. If so, we have to
17839 get the right continuation_lines_width and current_x. */
17840 it.continuation_lines_width = last_row->continuation_lines_width;
17841 it.hpos = it.current_x = 0;
17842
17843 /* Display the rest of the lines at the window end. */
17844 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17845 while (it.current_y < it.last_visible_y
17846 && !fonts_changed_p)
17847 {
17848 /* Is it always sure that the display agrees with lines in
17849 the current matrix? I don't think so, so we mark rows
17850 displayed invalid in the current matrix by setting their
17851 enabled_p flag to zero. */
17852 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17853 if (display_line (&it))
17854 last_text_row_at_end = it.glyph_row - 1;
17855 }
17856 }
17857
17858 /* Update window_end_pos and window_end_vpos. */
17859 if (first_unchanged_at_end_row
17860 && !last_text_row_at_end)
17861 {
17862 /* Window end line if one of the preserved rows from the current
17863 matrix. Set row to the last row displaying text in current
17864 matrix starting at first_unchanged_at_end_row, after
17865 scrolling. */
17866 eassert (first_unchanged_at_end_row->displays_text_p);
17867 row = find_last_row_displaying_text (w->current_matrix, &it,
17868 first_unchanged_at_end_row);
17869 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17870
17871 wset_window_end_pos (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17872 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17873 wset_window_end_vpos
17874 (w, make_number (MATRIX_ROW_VPOS (row, w->current_matrix)));
17875 eassert (w->window_end_bytepos >= 0);
17876 IF_DEBUG (debug_method_add (w, "A"));
17877 }
17878 else if (last_text_row_at_end)
17879 {
17880 wset_window_end_pos
17881 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end)));
17882 w->window_end_bytepos
17883 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17884 wset_window_end_vpos
17885 (w, make_number (MATRIX_ROW_VPOS (last_text_row_at_end,
17886 desired_matrix)));
17887 eassert (w->window_end_bytepos >= 0);
17888 IF_DEBUG (debug_method_add (w, "B"));
17889 }
17890 else if (last_text_row)
17891 {
17892 /* We have displayed either to the end of the window or at the
17893 end of the window, i.e. the last row with text is to be found
17894 in the desired matrix. */
17895 wset_window_end_pos
17896 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
17897 w->window_end_bytepos
17898 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17899 wset_window_end_vpos
17900 (w, make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix)));
17901 eassert (w->window_end_bytepos >= 0);
17902 }
17903 else if (first_unchanged_at_end_row == NULL
17904 && last_text_row == NULL
17905 && last_text_row_at_end == NULL)
17906 {
17907 /* Displayed to end of window, but no line containing text was
17908 displayed. Lines were deleted at the end of the window. */
17909 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17910 int vpos = XFASTINT (w->window_end_vpos);
17911 struct glyph_row *current_row = current_matrix->rows + vpos;
17912 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17913
17914 for (row = NULL;
17915 row == NULL && vpos >= first_vpos;
17916 --vpos, --current_row, --desired_row)
17917 {
17918 if (desired_row->enabled_p)
17919 {
17920 if (desired_row->displays_text_p)
17921 row = desired_row;
17922 }
17923 else if (current_row->displays_text_p)
17924 row = current_row;
17925 }
17926
17927 eassert (row != NULL);
17928 wset_window_end_vpos (w, make_number (vpos + 1));
17929 wset_window_end_pos (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17930 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17931 eassert (w->window_end_bytepos >= 0);
17932 IF_DEBUG (debug_method_add (w, "C"));
17933 }
17934 else
17935 emacs_abort ();
17936
17937 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
17938 debug_end_vpos = XFASTINT (w->window_end_vpos));
17939
17940 /* Record that display has not been completed. */
17941 wset_window_end_valid (w, Qnil);
17942 w->desired_matrix->no_scrolling_p = 1;
17943 return 3;
17944
17945 #undef GIVE_UP
17946 }
17947
17948
17949 \f
17950 /***********************************************************************
17951 More debugging support
17952 ***********************************************************************/
17953
17954 #ifdef GLYPH_DEBUG
17955
17956 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17957 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17958 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17959
17960
17961 /* Dump the contents of glyph matrix MATRIX on stderr.
17962
17963 GLYPHS 0 means don't show glyph contents.
17964 GLYPHS 1 means show glyphs in short form
17965 GLYPHS > 1 means show glyphs in long form. */
17966
17967 void
17968 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17969 {
17970 int i;
17971 for (i = 0; i < matrix->nrows; ++i)
17972 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17973 }
17974
17975
17976 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17977 the glyph row and area where the glyph comes from. */
17978
17979 void
17980 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17981 {
17982 if (glyph->type == CHAR_GLYPH
17983 || glyph->type == GLYPHLESS_GLYPH)
17984 {
17985 fprintf (stderr,
17986 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17987 glyph - row->glyphs[TEXT_AREA],
17988 (glyph->type == CHAR_GLYPH
17989 ? 'C'
17990 : 'G'),
17991 glyph->charpos,
17992 (BUFFERP (glyph->object)
17993 ? 'B'
17994 : (STRINGP (glyph->object)
17995 ? 'S'
17996 : (INTEGERP (glyph->object)
17997 ? '0'
17998 : '-'))),
17999 glyph->pixel_width,
18000 glyph->u.ch,
18001 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
18002 ? glyph->u.ch
18003 : '.'),
18004 glyph->face_id,
18005 glyph->left_box_line_p,
18006 glyph->right_box_line_p);
18007 }
18008 else if (glyph->type == STRETCH_GLYPH)
18009 {
18010 fprintf (stderr,
18011 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18012 glyph - row->glyphs[TEXT_AREA],
18013 'S',
18014 glyph->charpos,
18015 (BUFFERP (glyph->object)
18016 ? 'B'
18017 : (STRINGP (glyph->object)
18018 ? 'S'
18019 : (INTEGERP (glyph->object)
18020 ? '0'
18021 : '-'))),
18022 glyph->pixel_width,
18023 0,
18024 ' ',
18025 glyph->face_id,
18026 glyph->left_box_line_p,
18027 glyph->right_box_line_p);
18028 }
18029 else if (glyph->type == IMAGE_GLYPH)
18030 {
18031 fprintf (stderr,
18032 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18033 glyph - row->glyphs[TEXT_AREA],
18034 'I',
18035 glyph->charpos,
18036 (BUFFERP (glyph->object)
18037 ? 'B'
18038 : (STRINGP (glyph->object)
18039 ? 'S'
18040 : (INTEGERP (glyph->object)
18041 ? '0'
18042 : '-'))),
18043 glyph->pixel_width,
18044 glyph->u.img_id,
18045 '.',
18046 glyph->face_id,
18047 glyph->left_box_line_p,
18048 glyph->right_box_line_p);
18049 }
18050 else if (glyph->type == COMPOSITE_GLYPH)
18051 {
18052 fprintf (stderr,
18053 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
18054 glyph - row->glyphs[TEXT_AREA],
18055 '+',
18056 glyph->charpos,
18057 (BUFFERP (glyph->object)
18058 ? 'B'
18059 : (STRINGP (glyph->object)
18060 ? 'S'
18061 : (INTEGERP (glyph->object)
18062 ? '0'
18063 : '-'))),
18064 glyph->pixel_width,
18065 glyph->u.cmp.id);
18066 if (glyph->u.cmp.automatic)
18067 fprintf (stderr,
18068 "[%d-%d]",
18069 glyph->slice.cmp.from, glyph->slice.cmp.to);
18070 fprintf (stderr, " . %4d %1.1d%1.1d\n",
18071 glyph->face_id,
18072 glyph->left_box_line_p,
18073 glyph->right_box_line_p);
18074 }
18075 }
18076
18077
18078 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
18079 GLYPHS 0 means don't show glyph contents.
18080 GLYPHS 1 means show glyphs in short form
18081 GLYPHS > 1 means show glyphs in long form. */
18082
18083 void
18084 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
18085 {
18086 if (glyphs != 1)
18087 {
18088 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
18089 fprintf (stderr, "==============================================================================\n");
18090
18091 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18092 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18093 vpos,
18094 MATRIX_ROW_START_CHARPOS (row),
18095 MATRIX_ROW_END_CHARPOS (row),
18096 row->used[TEXT_AREA],
18097 row->contains_overlapping_glyphs_p,
18098 row->enabled_p,
18099 row->truncated_on_left_p,
18100 row->truncated_on_right_p,
18101 row->continued_p,
18102 MATRIX_ROW_CONTINUATION_LINE_P (row),
18103 row->displays_text_p,
18104 row->ends_at_zv_p,
18105 row->fill_line_p,
18106 row->ends_in_middle_of_char_p,
18107 row->starts_in_middle_of_char_p,
18108 row->mouse_face_p,
18109 row->x,
18110 row->y,
18111 row->pixel_width,
18112 row->height,
18113 row->visible_height,
18114 row->ascent,
18115 row->phys_ascent);
18116 /* The next 3 lines should align to "Start" in the header. */
18117 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
18118 row->end.overlay_string_index,
18119 row->continuation_lines_width);
18120 fprintf (stderr, " %9"pI"d %9"pI"d\n",
18121 CHARPOS (row->start.string_pos),
18122 CHARPOS (row->end.string_pos));
18123 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18124 row->end.dpvec_index);
18125 }
18126
18127 if (glyphs > 1)
18128 {
18129 int area;
18130
18131 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18132 {
18133 struct glyph *glyph = row->glyphs[area];
18134 struct glyph *glyph_end = glyph + row->used[area];
18135
18136 /* Glyph for a line end in text. */
18137 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18138 ++glyph_end;
18139
18140 if (glyph < glyph_end)
18141 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18142
18143 for (; glyph < glyph_end; ++glyph)
18144 dump_glyph (row, glyph, area);
18145 }
18146 }
18147 else if (glyphs == 1)
18148 {
18149 int area;
18150
18151 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18152 {
18153 char *s = alloca (row->used[area] + 4);
18154 int i;
18155
18156 for (i = 0; i < row->used[area]; ++i)
18157 {
18158 struct glyph *glyph = row->glyphs[area] + i;
18159 if (i == row->used[area] - 1
18160 && area == TEXT_AREA
18161 && INTEGERP (glyph->object)
18162 && glyph->type == CHAR_GLYPH
18163 && glyph->u.ch == ' ')
18164 {
18165 strcpy (&s[i], "[\\n]");
18166 i += 4;
18167 }
18168 else if (glyph->type == CHAR_GLYPH
18169 && glyph->u.ch < 0x80
18170 && glyph->u.ch >= ' ')
18171 s[i] = glyph->u.ch;
18172 else
18173 s[i] = '.';
18174 }
18175
18176 s[i] = '\0';
18177 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18178 }
18179 }
18180 }
18181
18182
18183 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18184 Sdump_glyph_matrix, 0, 1, "p",
18185 doc: /* Dump the current matrix of the selected window to stderr.
18186 Shows contents of glyph row structures. With non-nil
18187 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18188 glyphs in short form, otherwise show glyphs in long form. */)
18189 (Lisp_Object glyphs)
18190 {
18191 struct window *w = XWINDOW (selected_window);
18192 struct buffer *buffer = XBUFFER (w->buffer);
18193
18194 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18195 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18196 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18197 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18198 fprintf (stderr, "=============================================\n");
18199 dump_glyph_matrix (w->current_matrix,
18200 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18201 return Qnil;
18202 }
18203
18204
18205 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18206 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18207 (void)
18208 {
18209 struct frame *f = XFRAME (selected_frame);
18210 dump_glyph_matrix (f->current_matrix, 1);
18211 return Qnil;
18212 }
18213
18214
18215 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18216 doc: /* Dump glyph row ROW to stderr.
18217 GLYPH 0 means don't dump glyphs.
18218 GLYPH 1 means dump glyphs in short form.
18219 GLYPH > 1 or omitted means dump glyphs in long form. */)
18220 (Lisp_Object row, Lisp_Object glyphs)
18221 {
18222 struct glyph_matrix *matrix;
18223 EMACS_INT vpos;
18224
18225 CHECK_NUMBER (row);
18226 matrix = XWINDOW (selected_window)->current_matrix;
18227 vpos = XINT (row);
18228 if (vpos >= 0 && vpos < matrix->nrows)
18229 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18230 vpos,
18231 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18232 return Qnil;
18233 }
18234
18235
18236 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18237 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18238 GLYPH 0 means don't dump glyphs.
18239 GLYPH 1 means dump glyphs in short form.
18240 GLYPH > 1 or omitted means dump glyphs in long form. */)
18241 (Lisp_Object row, Lisp_Object glyphs)
18242 {
18243 struct frame *sf = SELECTED_FRAME ();
18244 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18245 EMACS_INT vpos;
18246
18247 CHECK_NUMBER (row);
18248 vpos = XINT (row);
18249 if (vpos >= 0 && vpos < m->nrows)
18250 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18251 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18252 return Qnil;
18253 }
18254
18255
18256 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18257 doc: /* Toggle tracing of redisplay.
18258 With ARG, turn tracing on if and only if ARG is positive. */)
18259 (Lisp_Object arg)
18260 {
18261 if (NILP (arg))
18262 trace_redisplay_p = !trace_redisplay_p;
18263 else
18264 {
18265 arg = Fprefix_numeric_value (arg);
18266 trace_redisplay_p = XINT (arg) > 0;
18267 }
18268
18269 return Qnil;
18270 }
18271
18272
18273 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18274 doc: /* Like `format', but print result to stderr.
18275 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18276 (ptrdiff_t nargs, Lisp_Object *args)
18277 {
18278 Lisp_Object s = Fformat (nargs, args);
18279 fprintf (stderr, "%s", SDATA (s));
18280 return Qnil;
18281 }
18282
18283 #endif /* GLYPH_DEBUG */
18284
18285
18286 \f
18287 /***********************************************************************
18288 Building Desired Matrix Rows
18289 ***********************************************************************/
18290
18291 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18292 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18293
18294 static struct glyph_row *
18295 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18296 {
18297 struct frame *f = XFRAME (WINDOW_FRAME (w));
18298 struct buffer *buffer = XBUFFER (w->buffer);
18299 struct buffer *old = current_buffer;
18300 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18301 int arrow_len = SCHARS (overlay_arrow_string);
18302 const unsigned char *arrow_end = arrow_string + arrow_len;
18303 const unsigned char *p;
18304 struct it it;
18305 int multibyte_p;
18306 int n_glyphs_before;
18307
18308 set_buffer_temp (buffer);
18309 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18310 it.glyph_row->used[TEXT_AREA] = 0;
18311 SET_TEXT_POS (it.position, 0, 0);
18312
18313 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18314 p = arrow_string;
18315 while (p < arrow_end)
18316 {
18317 Lisp_Object face, ilisp;
18318
18319 /* Get the next character. */
18320 if (multibyte_p)
18321 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18322 else
18323 {
18324 it.c = it.char_to_display = *p, it.len = 1;
18325 if (! ASCII_CHAR_P (it.c))
18326 it.char_to_display = BYTE8_TO_CHAR (it.c);
18327 }
18328 p += it.len;
18329
18330 /* Get its face. */
18331 ilisp = make_number (p - arrow_string);
18332 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18333 it.face_id = compute_char_face (f, it.char_to_display, face);
18334
18335 /* Compute its width, get its glyphs. */
18336 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18337 SET_TEXT_POS (it.position, -1, -1);
18338 PRODUCE_GLYPHS (&it);
18339
18340 /* If this character doesn't fit any more in the line, we have
18341 to remove some glyphs. */
18342 if (it.current_x > it.last_visible_x)
18343 {
18344 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18345 break;
18346 }
18347 }
18348
18349 set_buffer_temp (old);
18350 return it.glyph_row;
18351 }
18352
18353
18354 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18355 glyphs to insert is determined by produce_special_glyphs. */
18356
18357 static void
18358 insert_left_trunc_glyphs (struct it *it)
18359 {
18360 struct it truncate_it;
18361 struct glyph *from, *end, *to, *toend;
18362
18363 eassert (!FRAME_WINDOW_P (it->f)
18364 || (!it->glyph_row->reversed_p
18365 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18366 || (it->glyph_row->reversed_p
18367 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18368
18369 /* Get the truncation glyphs. */
18370 truncate_it = *it;
18371 truncate_it.current_x = 0;
18372 truncate_it.face_id = DEFAULT_FACE_ID;
18373 truncate_it.glyph_row = &scratch_glyph_row;
18374 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18375 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18376 truncate_it.object = make_number (0);
18377 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18378
18379 /* Overwrite glyphs from IT with truncation glyphs. */
18380 if (!it->glyph_row->reversed_p)
18381 {
18382 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18383
18384 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18385 end = from + tused;
18386 to = it->glyph_row->glyphs[TEXT_AREA];
18387 toend = to + it->glyph_row->used[TEXT_AREA];
18388 if (FRAME_WINDOW_P (it->f))
18389 {
18390 /* On GUI frames, when variable-size fonts are displayed,
18391 the truncation glyphs may need more pixels than the row's
18392 glyphs they overwrite. We overwrite more glyphs to free
18393 enough screen real estate, and enlarge the stretch glyph
18394 on the right (see display_line), if there is one, to
18395 preserve the screen position of the truncation glyphs on
18396 the right. */
18397 int w = 0;
18398 struct glyph *g = to;
18399 short used;
18400
18401 /* The first glyph could be partially visible, in which case
18402 it->glyph_row->x will be negative. But we want the left
18403 truncation glyphs to be aligned at the left margin of the
18404 window, so we override the x coordinate at which the row
18405 will begin. */
18406 it->glyph_row->x = 0;
18407 while (g < toend && w < it->truncation_pixel_width)
18408 {
18409 w += g->pixel_width;
18410 ++g;
18411 }
18412 if (g - to - tused > 0)
18413 {
18414 memmove (to + tused, g, (toend - g) * sizeof(*g));
18415 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18416 }
18417 used = it->glyph_row->used[TEXT_AREA];
18418 if (it->glyph_row->truncated_on_right_p
18419 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18420 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18421 == STRETCH_GLYPH)
18422 {
18423 int extra = w - it->truncation_pixel_width;
18424
18425 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18426 }
18427 }
18428
18429 while (from < end)
18430 *to++ = *from++;
18431
18432 /* There may be padding glyphs left over. Overwrite them too. */
18433 if (!FRAME_WINDOW_P (it->f))
18434 {
18435 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18436 {
18437 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18438 while (from < end)
18439 *to++ = *from++;
18440 }
18441 }
18442
18443 if (to > toend)
18444 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18445 }
18446 else
18447 {
18448 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18449
18450 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18451 that back to front. */
18452 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18453 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18454 toend = it->glyph_row->glyphs[TEXT_AREA];
18455 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18456 if (FRAME_WINDOW_P (it->f))
18457 {
18458 int w = 0;
18459 struct glyph *g = to;
18460
18461 while (g >= toend && w < it->truncation_pixel_width)
18462 {
18463 w += g->pixel_width;
18464 --g;
18465 }
18466 if (to - g - tused > 0)
18467 to = g + tused;
18468 if (it->glyph_row->truncated_on_right_p
18469 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18470 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18471 {
18472 int extra = w - it->truncation_pixel_width;
18473
18474 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18475 }
18476 }
18477
18478 while (from >= end && to >= toend)
18479 *to-- = *from--;
18480 if (!FRAME_WINDOW_P (it->f))
18481 {
18482 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18483 {
18484 from =
18485 truncate_it.glyph_row->glyphs[TEXT_AREA]
18486 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18487 while (from >= end && to >= toend)
18488 *to-- = *from--;
18489 }
18490 }
18491 if (from >= end)
18492 {
18493 /* Need to free some room before prepending additional
18494 glyphs. */
18495 int move_by = from - end + 1;
18496 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18497 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18498
18499 for ( ; g >= g0; g--)
18500 g[move_by] = *g;
18501 while (from >= end)
18502 *to-- = *from--;
18503 it->glyph_row->used[TEXT_AREA] += move_by;
18504 }
18505 }
18506 }
18507
18508 /* Compute the hash code for ROW. */
18509 unsigned
18510 row_hash (struct glyph_row *row)
18511 {
18512 int area, k;
18513 unsigned hashval = 0;
18514
18515 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18516 for (k = 0; k < row->used[area]; ++k)
18517 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18518 + row->glyphs[area][k].u.val
18519 + row->glyphs[area][k].face_id
18520 + row->glyphs[area][k].padding_p
18521 + (row->glyphs[area][k].type << 2));
18522
18523 return hashval;
18524 }
18525
18526 /* Compute the pixel height and width of IT->glyph_row.
18527
18528 Most of the time, ascent and height of a display line will be equal
18529 to the max_ascent and max_height values of the display iterator
18530 structure. This is not the case if
18531
18532 1. We hit ZV without displaying anything. In this case, max_ascent
18533 and max_height will be zero.
18534
18535 2. We have some glyphs that don't contribute to the line height.
18536 (The glyph row flag contributes_to_line_height_p is for future
18537 pixmap extensions).
18538
18539 The first case is easily covered by using default values because in
18540 these cases, the line height does not really matter, except that it
18541 must not be zero. */
18542
18543 static void
18544 compute_line_metrics (struct it *it)
18545 {
18546 struct glyph_row *row = it->glyph_row;
18547
18548 if (FRAME_WINDOW_P (it->f))
18549 {
18550 int i, min_y, max_y;
18551
18552 /* The line may consist of one space only, that was added to
18553 place the cursor on it. If so, the row's height hasn't been
18554 computed yet. */
18555 if (row->height == 0)
18556 {
18557 if (it->max_ascent + it->max_descent == 0)
18558 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18559 row->ascent = it->max_ascent;
18560 row->height = it->max_ascent + it->max_descent;
18561 row->phys_ascent = it->max_phys_ascent;
18562 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18563 row->extra_line_spacing = it->max_extra_line_spacing;
18564 }
18565
18566 /* Compute the width of this line. */
18567 row->pixel_width = row->x;
18568 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18569 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18570
18571 eassert (row->pixel_width >= 0);
18572 eassert (row->ascent >= 0 && row->height > 0);
18573
18574 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18575 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18576
18577 /* If first line's physical ascent is larger than its logical
18578 ascent, use the physical ascent, and make the row taller.
18579 This makes accented characters fully visible. */
18580 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18581 && row->phys_ascent > row->ascent)
18582 {
18583 row->height += row->phys_ascent - row->ascent;
18584 row->ascent = row->phys_ascent;
18585 }
18586
18587 /* Compute how much of the line is visible. */
18588 row->visible_height = row->height;
18589
18590 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18591 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18592
18593 if (row->y < min_y)
18594 row->visible_height -= min_y - row->y;
18595 if (row->y + row->height > max_y)
18596 row->visible_height -= row->y + row->height - max_y;
18597 }
18598 else
18599 {
18600 row->pixel_width = row->used[TEXT_AREA];
18601 if (row->continued_p)
18602 row->pixel_width -= it->continuation_pixel_width;
18603 else if (row->truncated_on_right_p)
18604 row->pixel_width -= it->truncation_pixel_width;
18605 row->ascent = row->phys_ascent = 0;
18606 row->height = row->phys_height = row->visible_height = 1;
18607 row->extra_line_spacing = 0;
18608 }
18609
18610 /* Compute a hash code for this row. */
18611 row->hash = row_hash (row);
18612
18613 it->max_ascent = it->max_descent = 0;
18614 it->max_phys_ascent = it->max_phys_descent = 0;
18615 }
18616
18617
18618 /* Append one space to the glyph row of iterator IT if doing a
18619 window-based redisplay. The space has the same face as
18620 IT->face_id. Value is non-zero if a space was added.
18621
18622 This function is called to make sure that there is always one glyph
18623 at the end of a glyph row that the cursor can be set on under
18624 window-systems. (If there weren't such a glyph we would not know
18625 how wide and tall a box cursor should be displayed).
18626
18627 At the same time this space let's a nicely handle clearing to the
18628 end of the line if the row ends in italic text. */
18629
18630 static int
18631 append_space_for_newline (struct it *it, int default_face_p)
18632 {
18633 if (FRAME_WINDOW_P (it->f))
18634 {
18635 int n = it->glyph_row->used[TEXT_AREA];
18636
18637 if (it->glyph_row->glyphs[TEXT_AREA] + n
18638 < it->glyph_row->glyphs[1 + TEXT_AREA])
18639 {
18640 /* Save some values that must not be changed.
18641 Must save IT->c and IT->len because otherwise
18642 ITERATOR_AT_END_P wouldn't work anymore after
18643 append_space_for_newline has been called. */
18644 enum display_element_type saved_what = it->what;
18645 int saved_c = it->c, saved_len = it->len;
18646 int saved_char_to_display = it->char_to_display;
18647 int saved_x = it->current_x;
18648 int saved_face_id = it->face_id;
18649 struct text_pos saved_pos;
18650 Lisp_Object saved_object;
18651 struct face *face;
18652
18653 saved_object = it->object;
18654 saved_pos = it->position;
18655
18656 it->what = IT_CHARACTER;
18657 memset (&it->position, 0, sizeof it->position);
18658 it->object = make_number (0);
18659 it->c = it->char_to_display = ' ';
18660 it->len = 1;
18661
18662 /* If the default face was remapped, be sure to use the
18663 remapped face for the appended newline. */
18664 if (default_face_p)
18665 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18666 else if (it->face_before_selective_p)
18667 it->face_id = it->saved_face_id;
18668 face = FACE_FROM_ID (it->f, it->face_id);
18669 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18670
18671 PRODUCE_GLYPHS (it);
18672
18673 it->override_ascent = -1;
18674 it->constrain_row_ascent_descent_p = 0;
18675 it->current_x = saved_x;
18676 it->object = saved_object;
18677 it->position = saved_pos;
18678 it->what = saved_what;
18679 it->face_id = saved_face_id;
18680 it->len = saved_len;
18681 it->c = saved_c;
18682 it->char_to_display = saved_char_to_display;
18683 return 1;
18684 }
18685 }
18686
18687 return 0;
18688 }
18689
18690
18691 /* Extend the face of the last glyph in the text area of IT->glyph_row
18692 to the end of the display line. Called from display_line. If the
18693 glyph row is empty, add a space glyph to it so that we know the
18694 face to draw. Set the glyph row flag fill_line_p. If the glyph
18695 row is R2L, prepend a stretch glyph to cover the empty space to the
18696 left of the leftmost glyph. */
18697
18698 static void
18699 extend_face_to_end_of_line (struct it *it)
18700 {
18701 struct face *face, *default_face;
18702 struct frame *f = it->f;
18703
18704 /* If line is already filled, do nothing. Non window-system frames
18705 get a grace of one more ``pixel'' because their characters are
18706 1-``pixel'' wide, so they hit the equality too early. This grace
18707 is needed only for R2L rows that are not continued, to produce
18708 one extra blank where we could display the cursor. */
18709 if (it->current_x >= it->last_visible_x
18710 + (!FRAME_WINDOW_P (f)
18711 && it->glyph_row->reversed_p
18712 && !it->glyph_row->continued_p))
18713 return;
18714
18715 /* The default face, possibly remapped. */
18716 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18717
18718 /* Face extension extends the background and box of IT->face_id
18719 to the end of the line. If the background equals the background
18720 of the frame, we don't have to do anything. */
18721 if (it->face_before_selective_p)
18722 face = FACE_FROM_ID (f, it->saved_face_id);
18723 else
18724 face = FACE_FROM_ID (f, it->face_id);
18725
18726 if (FRAME_WINDOW_P (f)
18727 && it->glyph_row->displays_text_p
18728 && face->box == FACE_NO_BOX
18729 && face->background == FRAME_BACKGROUND_PIXEL (f)
18730 && !face->stipple
18731 && !it->glyph_row->reversed_p)
18732 return;
18733
18734 /* Set the glyph row flag indicating that the face of the last glyph
18735 in the text area has to be drawn to the end of the text area. */
18736 it->glyph_row->fill_line_p = 1;
18737
18738 /* If current character of IT is not ASCII, make sure we have the
18739 ASCII face. This will be automatically undone the next time
18740 get_next_display_element returns a multibyte character. Note
18741 that the character will always be single byte in unibyte
18742 text. */
18743 if (!ASCII_CHAR_P (it->c))
18744 {
18745 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18746 }
18747
18748 if (FRAME_WINDOW_P (f))
18749 {
18750 /* If the row is empty, add a space with the current face of IT,
18751 so that we know which face to draw. */
18752 if (it->glyph_row->used[TEXT_AREA] == 0)
18753 {
18754 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18755 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18756 it->glyph_row->used[TEXT_AREA] = 1;
18757 }
18758 #ifdef HAVE_WINDOW_SYSTEM
18759 if (it->glyph_row->reversed_p)
18760 {
18761 /* Prepend a stretch glyph to the row, such that the
18762 rightmost glyph will be drawn flushed all the way to the
18763 right margin of the window. The stretch glyph that will
18764 occupy the empty space, if any, to the left of the
18765 glyphs. */
18766 struct font *font = face->font ? face->font : FRAME_FONT (f);
18767 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18768 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18769 struct glyph *g;
18770 int row_width, stretch_ascent, stretch_width;
18771 struct text_pos saved_pos;
18772 int saved_face_id, saved_avoid_cursor;
18773
18774 for (row_width = 0, g = row_start; g < row_end; g++)
18775 row_width += g->pixel_width;
18776 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18777 if (stretch_width > 0)
18778 {
18779 stretch_ascent =
18780 (((it->ascent + it->descent)
18781 * FONT_BASE (font)) / FONT_HEIGHT (font));
18782 saved_pos = it->position;
18783 memset (&it->position, 0, sizeof it->position);
18784 saved_avoid_cursor = it->avoid_cursor_p;
18785 it->avoid_cursor_p = 1;
18786 saved_face_id = it->face_id;
18787 /* The last row's stretch glyph should get the default
18788 face, to avoid painting the rest of the window with
18789 the region face, if the region ends at ZV. */
18790 if (it->glyph_row->ends_at_zv_p)
18791 it->face_id = default_face->id;
18792 else
18793 it->face_id = face->id;
18794 append_stretch_glyph (it, make_number (0), stretch_width,
18795 it->ascent + it->descent, stretch_ascent);
18796 it->position = saved_pos;
18797 it->avoid_cursor_p = saved_avoid_cursor;
18798 it->face_id = saved_face_id;
18799 }
18800 }
18801 #endif /* HAVE_WINDOW_SYSTEM */
18802 }
18803 else
18804 {
18805 /* Save some values that must not be changed. */
18806 int saved_x = it->current_x;
18807 struct text_pos saved_pos;
18808 Lisp_Object saved_object;
18809 enum display_element_type saved_what = it->what;
18810 int saved_face_id = it->face_id;
18811
18812 saved_object = it->object;
18813 saved_pos = it->position;
18814
18815 it->what = IT_CHARACTER;
18816 memset (&it->position, 0, sizeof it->position);
18817 it->object = make_number (0);
18818 it->c = it->char_to_display = ' ';
18819 it->len = 1;
18820 /* The last row's blank glyphs should get the default face, to
18821 avoid painting the rest of the window with the region face,
18822 if the region ends at ZV. */
18823 if (it->glyph_row->ends_at_zv_p)
18824 it->face_id = default_face->id;
18825 else
18826 it->face_id = face->id;
18827
18828 PRODUCE_GLYPHS (it);
18829
18830 while (it->current_x <= it->last_visible_x)
18831 PRODUCE_GLYPHS (it);
18832
18833 /* Don't count these blanks really. It would let us insert a left
18834 truncation glyph below and make us set the cursor on them, maybe. */
18835 it->current_x = saved_x;
18836 it->object = saved_object;
18837 it->position = saved_pos;
18838 it->what = saved_what;
18839 it->face_id = saved_face_id;
18840 }
18841 }
18842
18843
18844 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18845 trailing whitespace. */
18846
18847 static int
18848 trailing_whitespace_p (ptrdiff_t charpos)
18849 {
18850 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18851 int c = 0;
18852
18853 while (bytepos < ZV_BYTE
18854 && (c = FETCH_CHAR (bytepos),
18855 c == ' ' || c == '\t'))
18856 ++bytepos;
18857
18858 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18859 {
18860 if (bytepos != PT_BYTE)
18861 return 1;
18862 }
18863 return 0;
18864 }
18865
18866
18867 /* Highlight trailing whitespace, if any, in ROW. */
18868
18869 static void
18870 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18871 {
18872 int used = row->used[TEXT_AREA];
18873
18874 if (used)
18875 {
18876 struct glyph *start = row->glyphs[TEXT_AREA];
18877 struct glyph *glyph = start + used - 1;
18878
18879 if (row->reversed_p)
18880 {
18881 /* Right-to-left rows need to be processed in the opposite
18882 direction, so swap the edge pointers. */
18883 glyph = start;
18884 start = row->glyphs[TEXT_AREA] + used - 1;
18885 }
18886
18887 /* Skip over glyphs inserted to display the cursor at the
18888 end of a line, for extending the face of the last glyph
18889 to the end of the line on terminals, and for truncation
18890 and continuation glyphs. */
18891 if (!row->reversed_p)
18892 {
18893 while (glyph >= start
18894 && glyph->type == CHAR_GLYPH
18895 && INTEGERP (glyph->object))
18896 --glyph;
18897 }
18898 else
18899 {
18900 while (glyph <= start
18901 && glyph->type == CHAR_GLYPH
18902 && INTEGERP (glyph->object))
18903 ++glyph;
18904 }
18905
18906 /* If last glyph is a space or stretch, and it's trailing
18907 whitespace, set the face of all trailing whitespace glyphs in
18908 IT->glyph_row to `trailing-whitespace'. */
18909 if ((row->reversed_p ? glyph <= start : glyph >= start)
18910 && BUFFERP (glyph->object)
18911 && (glyph->type == STRETCH_GLYPH
18912 || (glyph->type == CHAR_GLYPH
18913 && glyph->u.ch == ' '))
18914 && trailing_whitespace_p (glyph->charpos))
18915 {
18916 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18917 if (face_id < 0)
18918 return;
18919
18920 if (!row->reversed_p)
18921 {
18922 while (glyph >= start
18923 && BUFFERP (glyph->object)
18924 && (glyph->type == STRETCH_GLYPH
18925 || (glyph->type == CHAR_GLYPH
18926 && glyph->u.ch == ' ')))
18927 (glyph--)->face_id = face_id;
18928 }
18929 else
18930 {
18931 while (glyph <= start
18932 && BUFFERP (glyph->object)
18933 && (glyph->type == STRETCH_GLYPH
18934 || (glyph->type == CHAR_GLYPH
18935 && glyph->u.ch == ' ')))
18936 (glyph++)->face_id = face_id;
18937 }
18938 }
18939 }
18940 }
18941
18942
18943 /* Value is non-zero if glyph row ROW should be
18944 used to hold the cursor. */
18945
18946 static int
18947 cursor_row_p (struct glyph_row *row)
18948 {
18949 int result = 1;
18950
18951 if (PT == CHARPOS (row->end.pos)
18952 || PT == MATRIX_ROW_END_CHARPOS (row))
18953 {
18954 /* Suppose the row ends on a string.
18955 Unless the row is continued, that means it ends on a newline
18956 in the string. If it's anything other than a display string
18957 (e.g., a before-string from an overlay), we don't want the
18958 cursor there. (This heuristic seems to give the optimal
18959 behavior for the various types of multi-line strings.)
18960 One exception: if the string has `cursor' property on one of
18961 its characters, we _do_ want the cursor there. */
18962 if (CHARPOS (row->end.string_pos) >= 0)
18963 {
18964 if (row->continued_p)
18965 result = 1;
18966 else
18967 {
18968 /* Check for `display' property. */
18969 struct glyph *beg = row->glyphs[TEXT_AREA];
18970 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18971 struct glyph *glyph;
18972
18973 result = 0;
18974 for (glyph = end; glyph >= beg; --glyph)
18975 if (STRINGP (glyph->object))
18976 {
18977 Lisp_Object prop
18978 = Fget_char_property (make_number (PT),
18979 Qdisplay, Qnil);
18980 result =
18981 (!NILP (prop)
18982 && display_prop_string_p (prop, glyph->object));
18983 /* If there's a `cursor' property on one of the
18984 string's characters, this row is a cursor row,
18985 even though this is not a display string. */
18986 if (!result)
18987 {
18988 Lisp_Object s = glyph->object;
18989
18990 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18991 {
18992 ptrdiff_t gpos = glyph->charpos;
18993
18994 if (!NILP (Fget_char_property (make_number (gpos),
18995 Qcursor, s)))
18996 {
18997 result = 1;
18998 break;
18999 }
19000 }
19001 }
19002 break;
19003 }
19004 }
19005 }
19006 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
19007 {
19008 /* If the row ends in middle of a real character,
19009 and the line is continued, we want the cursor here.
19010 That's because CHARPOS (ROW->end.pos) would equal
19011 PT if PT is before the character. */
19012 if (!row->ends_in_ellipsis_p)
19013 result = row->continued_p;
19014 else
19015 /* If the row ends in an ellipsis, then
19016 CHARPOS (ROW->end.pos) will equal point after the
19017 invisible text. We want that position to be displayed
19018 after the ellipsis. */
19019 result = 0;
19020 }
19021 /* If the row ends at ZV, display the cursor at the end of that
19022 row instead of at the start of the row below. */
19023 else if (row->ends_at_zv_p)
19024 result = 1;
19025 else
19026 result = 0;
19027 }
19028
19029 return result;
19030 }
19031
19032 \f
19033
19034 /* Push the property PROP so that it will be rendered at the current
19035 position in IT. Return 1 if PROP was successfully pushed, 0
19036 otherwise. Called from handle_line_prefix to handle the
19037 `line-prefix' and `wrap-prefix' properties. */
19038
19039 static int
19040 push_prefix_prop (struct it *it, Lisp_Object prop)
19041 {
19042 struct text_pos pos =
19043 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
19044
19045 eassert (it->method == GET_FROM_BUFFER
19046 || it->method == GET_FROM_DISPLAY_VECTOR
19047 || it->method == GET_FROM_STRING);
19048
19049 /* We need to save the current buffer/string position, so it will be
19050 restored by pop_it, because iterate_out_of_display_property
19051 depends on that being set correctly, but some situations leave
19052 it->position not yet set when this function is called. */
19053 push_it (it, &pos);
19054
19055 if (STRINGP (prop))
19056 {
19057 if (SCHARS (prop) == 0)
19058 {
19059 pop_it (it);
19060 return 0;
19061 }
19062
19063 it->string = prop;
19064 it->string_from_prefix_prop_p = 1;
19065 it->multibyte_p = STRING_MULTIBYTE (it->string);
19066 it->current.overlay_string_index = -1;
19067 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
19068 it->end_charpos = it->string_nchars = SCHARS (it->string);
19069 it->method = GET_FROM_STRING;
19070 it->stop_charpos = 0;
19071 it->prev_stop = 0;
19072 it->base_level_stop = 0;
19073
19074 /* Force paragraph direction to be that of the parent
19075 buffer/string. */
19076 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
19077 it->paragraph_embedding = it->bidi_it.paragraph_dir;
19078 else
19079 it->paragraph_embedding = L2R;
19080
19081 /* Set up the bidi iterator for this display string. */
19082 if (it->bidi_p)
19083 {
19084 it->bidi_it.string.lstring = it->string;
19085 it->bidi_it.string.s = NULL;
19086 it->bidi_it.string.schars = it->end_charpos;
19087 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
19088 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19089 it->bidi_it.string.unibyte = !it->multibyte_p;
19090 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19091 }
19092 }
19093 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19094 {
19095 it->method = GET_FROM_STRETCH;
19096 it->object = prop;
19097 }
19098 #ifdef HAVE_WINDOW_SYSTEM
19099 else if (IMAGEP (prop))
19100 {
19101 it->what = IT_IMAGE;
19102 it->image_id = lookup_image (it->f, prop);
19103 it->method = GET_FROM_IMAGE;
19104 }
19105 #endif /* HAVE_WINDOW_SYSTEM */
19106 else
19107 {
19108 pop_it (it); /* bogus display property, give up */
19109 return 0;
19110 }
19111
19112 return 1;
19113 }
19114
19115 /* Return the character-property PROP at the current position in IT. */
19116
19117 static Lisp_Object
19118 get_it_property (struct it *it, Lisp_Object prop)
19119 {
19120 Lisp_Object position;
19121
19122 if (STRINGP (it->object))
19123 position = make_number (IT_STRING_CHARPOS (*it));
19124 else if (BUFFERP (it->object))
19125 position = make_number (IT_CHARPOS (*it));
19126 else
19127 return Qnil;
19128
19129 return Fget_char_property (position, prop, it->object);
19130 }
19131
19132 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19133
19134 static void
19135 handle_line_prefix (struct it *it)
19136 {
19137 Lisp_Object prefix;
19138
19139 if (it->continuation_lines_width > 0)
19140 {
19141 prefix = get_it_property (it, Qwrap_prefix);
19142 if (NILP (prefix))
19143 prefix = Vwrap_prefix;
19144 }
19145 else
19146 {
19147 prefix = get_it_property (it, Qline_prefix);
19148 if (NILP (prefix))
19149 prefix = Vline_prefix;
19150 }
19151 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19152 {
19153 /* If the prefix is wider than the window, and we try to wrap
19154 it, it would acquire its own wrap prefix, and so on till the
19155 iterator stack overflows. So, don't wrap the prefix. */
19156 it->line_wrap = TRUNCATE;
19157 it->avoid_cursor_p = 1;
19158 }
19159 }
19160
19161 \f
19162
19163 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19164 only for R2L lines from display_line and display_string, when they
19165 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19166 the line/string needs to be continued on the next glyph row. */
19167 static void
19168 unproduce_glyphs (struct it *it, int n)
19169 {
19170 struct glyph *glyph, *end;
19171
19172 eassert (it->glyph_row);
19173 eassert (it->glyph_row->reversed_p);
19174 eassert (it->area == TEXT_AREA);
19175 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19176
19177 if (n > it->glyph_row->used[TEXT_AREA])
19178 n = it->glyph_row->used[TEXT_AREA];
19179 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19180 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19181 for ( ; glyph < end; glyph++)
19182 glyph[-n] = *glyph;
19183 }
19184
19185 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19186 and ROW->maxpos. */
19187 static void
19188 find_row_edges (struct it *it, struct glyph_row *row,
19189 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19190 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19191 {
19192 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19193 lines' rows is implemented for bidi-reordered rows. */
19194
19195 /* ROW->minpos is the value of min_pos, the minimal buffer position
19196 we have in ROW, or ROW->start.pos if that is smaller. */
19197 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19198 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19199 else
19200 /* We didn't find buffer positions smaller than ROW->start, or
19201 didn't find _any_ valid buffer positions in any of the glyphs,
19202 so we must trust the iterator's computed positions. */
19203 row->minpos = row->start.pos;
19204 if (max_pos <= 0)
19205 {
19206 max_pos = CHARPOS (it->current.pos);
19207 max_bpos = BYTEPOS (it->current.pos);
19208 }
19209
19210 /* Here are the various use-cases for ending the row, and the
19211 corresponding values for ROW->maxpos:
19212
19213 Line ends in a newline from buffer eol_pos + 1
19214 Line is continued from buffer max_pos + 1
19215 Line is truncated on right it->current.pos
19216 Line ends in a newline from string max_pos + 1(*)
19217 (*) + 1 only when line ends in a forward scan
19218 Line is continued from string max_pos
19219 Line is continued from display vector max_pos
19220 Line is entirely from a string min_pos == max_pos
19221 Line is entirely from a display vector min_pos == max_pos
19222 Line that ends at ZV ZV
19223
19224 If you discover other use-cases, please add them here as
19225 appropriate. */
19226 if (row->ends_at_zv_p)
19227 row->maxpos = it->current.pos;
19228 else if (row->used[TEXT_AREA])
19229 {
19230 int seen_this_string = 0;
19231 struct glyph_row *r1 = row - 1;
19232
19233 /* Did we see the same display string on the previous row? */
19234 if (STRINGP (it->object)
19235 /* this is not the first row */
19236 && row > it->w->desired_matrix->rows
19237 /* previous row is not the header line */
19238 && !r1->mode_line_p
19239 /* previous row also ends in a newline from a string */
19240 && r1->ends_in_newline_from_string_p)
19241 {
19242 struct glyph *start, *end;
19243
19244 /* Search for the last glyph of the previous row that came
19245 from buffer or string. Depending on whether the row is
19246 L2R or R2L, we need to process it front to back or the
19247 other way round. */
19248 if (!r1->reversed_p)
19249 {
19250 start = r1->glyphs[TEXT_AREA];
19251 end = start + r1->used[TEXT_AREA];
19252 /* Glyphs inserted by redisplay have an integer (zero)
19253 as their object. */
19254 while (end > start
19255 && INTEGERP ((end - 1)->object)
19256 && (end - 1)->charpos <= 0)
19257 --end;
19258 if (end > start)
19259 {
19260 if (EQ ((end - 1)->object, it->object))
19261 seen_this_string = 1;
19262 }
19263 else
19264 /* If all the glyphs of the previous row were inserted
19265 by redisplay, it means the previous row was
19266 produced from a single newline, which is only
19267 possible if that newline came from the same string
19268 as the one which produced this ROW. */
19269 seen_this_string = 1;
19270 }
19271 else
19272 {
19273 end = r1->glyphs[TEXT_AREA] - 1;
19274 start = end + r1->used[TEXT_AREA];
19275 while (end < start
19276 && INTEGERP ((end + 1)->object)
19277 && (end + 1)->charpos <= 0)
19278 ++end;
19279 if (end < start)
19280 {
19281 if (EQ ((end + 1)->object, it->object))
19282 seen_this_string = 1;
19283 }
19284 else
19285 seen_this_string = 1;
19286 }
19287 }
19288 /* Take note of each display string that covers a newline only
19289 once, the first time we see it. This is for when a display
19290 string includes more than one newline in it. */
19291 if (row->ends_in_newline_from_string_p && !seen_this_string)
19292 {
19293 /* If we were scanning the buffer forward when we displayed
19294 the string, we want to account for at least one buffer
19295 position that belongs to this row (position covered by
19296 the display string), so that cursor positioning will
19297 consider this row as a candidate when point is at the end
19298 of the visual line represented by this row. This is not
19299 required when scanning back, because max_pos will already
19300 have a much larger value. */
19301 if (CHARPOS (row->end.pos) > max_pos)
19302 INC_BOTH (max_pos, max_bpos);
19303 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19304 }
19305 else if (CHARPOS (it->eol_pos) > 0)
19306 SET_TEXT_POS (row->maxpos,
19307 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19308 else if (row->continued_p)
19309 {
19310 /* If max_pos is different from IT's current position, it
19311 means IT->method does not belong to the display element
19312 at max_pos. However, it also means that the display
19313 element at max_pos was displayed in its entirety on this
19314 line, which is equivalent to saying that the next line
19315 starts at the next buffer position. */
19316 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19317 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19318 else
19319 {
19320 INC_BOTH (max_pos, max_bpos);
19321 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19322 }
19323 }
19324 else if (row->truncated_on_right_p)
19325 /* display_line already called reseat_at_next_visible_line_start,
19326 which puts the iterator at the beginning of the next line, in
19327 the logical order. */
19328 row->maxpos = it->current.pos;
19329 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19330 /* A line that is entirely from a string/image/stretch... */
19331 row->maxpos = row->minpos;
19332 else
19333 emacs_abort ();
19334 }
19335 else
19336 row->maxpos = it->current.pos;
19337 }
19338
19339 /* Construct the glyph row IT->glyph_row in the desired matrix of
19340 IT->w from text at the current position of IT. See dispextern.h
19341 for an overview of struct it. Value is non-zero if
19342 IT->glyph_row displays text, as opposed to a line displaying ZV
19343 only. */
19344
19345 static int
19346 display_line (struct it *it)
19347 {
19348 struct glyph_row *row = it->glyph_row;
19349 Lisp_Object overlay_arrow_string;
19350 struct it wrap_it;
19351 void *wrap_data = NULL;
19352 int may_wrap = 0, wrap_x IF_LINT (= 0);
19353 int wrap_row_used = -1;
19354 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19355 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19356 int wrap_row_extra_line_spacing IF_LINT (= 0);
19357 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19358 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19359 int cvpos;
19360 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19361 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19362
19363 /* We always start displaying at hpos zero even if hscrolled. */
19364 eassert (it->hpos == 0 && it->current_x == 0);
19365
19366 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19367 >= it->w->desired_matrix->nrows)
19368 {
19369 it->w->nrows_scale_factor++;
19370 fonts_changed_p = 1;
19371 return 0;
19372 }
19373
19374 /* Is IT->w showing the region? */
19375 wset_region_showing (it->w, it->region_beg_charpos > 0 ? Qt : Qnil);
19376
19377 /* Clear the result glyph row and enable it. */
19378 prepare_desired_row (row);
19379
19380 row->y = it->current_y;
19381 row->start = it->start;
19382 row->continuation_lines_width = it->continuation_lines_width;
19383 row->displays_text_p = 1;
19384 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19385 it->starts_in_middle_of_char_p = 0;
19386
19387 /* Arrange the overlays nicely for our purposes. Usually, we call
19388 display_line on only one line at a time, in which case this
19389 can't really hurt too much, or we call it on lines which appear
19390 one after another in the buffer, in which case all calls to
19391 recenter_overlay_lists but the first will be pretty cheap. */
19392 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19393
19394 /* Move over display elements that are not visible because we are
19395 hscrolled. This may stop at an x-position < IT->first_visible_x
19396 if the first glyph is partially visible or if we hit a line end. */
19397 if (it->current_x < it->first_visible_x)
19398 {
19399 enum move_it_result move_result;
19400
19401 this_line_min_pos = row->start.pos;
19402 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19403 MOVE_TO_POS | MOVE_TO_X);
19404 /* If we are under a large hscroll, move_it_in_display_line_to
19405 could hit the end of the line without reaching
19406 it->first_visible_x. Pretend that we did reach it. This is
19407 especially important on a TTY, where we will call
19408 extend_face_to_end_of_line, which needs to know how many
19409 blank glyphs to produce. */
19410 if (it->current_x < it->first_visible_x
19411 && (move_result == MOVE_NEWLINE_OR_CR
19412 || move_result == MOVE_POS_MATCH_OR_ZV))
19413 it->current_x = it->first_visible_x;
19414
19415 /* Record the smallest positions seen while we moved over
19416 display elements that are not visible. This is needed by
19417 redisplay_internal for optimizing the case where the cursor
19418 stays inside the same line. The rest of this function only
19419 considers positions that are actually displayed, so
19420 RECORD_MAX_MIN_POS will not otherwise record positions that
19421 are hscrolled to the left of the left edge of the window. */
19422 min_pos = CHARPOS (this_line_min_pos);
19423 min_bpos = BYTEPOS (this_line_min_pos);
19424 }
19425 else
19426 {
19427 /* We only do this when not calling `move_it_in_display_line_to'
19428 above, because move_it_in_display_line_to calls
19429 handle_line_prefix itself. */
19430 handle_line_prefix (it);
19431 }
19432
19433 /* Get the initial row height. This is either the height of the
19434 text hscrolled, if there is any, or zero. */
19435 row->ascent = it->max_ascent;
19436 row->height = it->max_ascent + it->max_descent;
19437 row->phys_ascent = it->max_phys_ascent;
19438 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19439 row->extra_line_spacing = it->max_extra_line_spacing;
19440
19441 /* Utility macro to record max and min buffer positions seen until now. */
19442 #define RECORD_MAX_MIN_POS(IT) \
19443 do \
19444 { \
19445 int composition_p = !STRINGP ((IT)->string) \
19446 && ((IT)->what == IT_COMPOSITION); \
19447 ptrdiff_t current_pos = \
19448 composition_p ? (IT)->cmp_it.charpos \
19449 : IT_CHARPOS (*(IT)); \
19450 ptrdiff_t current_bpos = \
19451 composition_p ? CHAR_TO_BYTE (current_pos) \
19452 : IT_BYTEPOS (*(IT)); \
19453 if (current_pos < min_pos) \
19454 { \
19455 min_pos = current_pos; \
19456 min_bpos = current_bpos; \
19457 } \
19458 if (IT_CHARPOS (*it) > max_pos) \
19459 { \
19460 max_pos = IT_CHARPOS (*it); \
19461 max_bpos = IT_BYTEPOS (*it); \
19462 } \
19463 } \
19464 while (0)
19465
19466 /* Loop generating characters. The loop is left with IT on the next
19467 character to display. */
19468 while (1)
19469 {
19470 int n_glyphs_before, hpos_before, x_before;
19471 int x, nglyphs;
19472 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19473
19474 /* Retrieve the next thing to display. Value is zero if end of
19475 buffer reached. */
19476 if (!get_next_display_element (it))
19477 {
19478 /* Maybe add a space at the end of this line that is used to
19479 display the cursor there under X. Set the charpos of the
19480 first glyph of blank lines not corresponding to any text
19481 to -1. */
19482 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19483 row->exact_window_width_line_p = 1;
19484 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19485 || row->used[TEXT_AREA] == 0)
19486 {
19487 row->glyphs[TEXT_AREA]->charpos = -1;
19488 row->displays_text_p = 0;
19489
19490 if (!NILP (BVAR (XBUFFER (it->w->buffer), indicate_empty_lines))
19491 && (!MINI_WINDOW_P (it->w)
19492 || (minibuf_level && EQ (it->window, minibuf_window))))
19493 row->indicate_empty_line_p = 1;
19494 }
19495
19496 it->continuation_lines_width = 0;
19497 row->ends_at_zv_p = 1;
19498 /* A row that displays right-to-left text must always have
19499 its last face extended all the way to the end of line,
19500 even if this row ends in ZV, because we still write to
19501 the screen left to right. We also need to extend the
19502 last face if the default face is remapped to some
19503 different face, otherwise the functions that clear
19504 portions of the screen will clear with the default face's
19505 background color. */
19506 if (row->reversed_p
19507 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19508 extend_face_to_end_of_line (it);
19509 break;
19510 }
19511
19512 /* Now, get the metrics of what we want to display. This also
19513 generates glyphs in `row' (which is IT->glyph_row). */
19514 n_glyphs_before = row->used[TEXT_AREA];
19515 x = it->current_x;
19516
19517 /* Remember the line height so far in case the next element doesn't
19518 fit on the line. */
19519 if (it->line_wrap != TRUNCATE)
19520 {
19521 ascent = it->max_ascent;
19522 descent = it->max_descent;
19523 phys_ascent = it->max_phys_ascent;
19524 phys_descent = it->max_phys_descent;
19525
19526 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19527 {
19528 if (IT_DISPLAYING_WHITESPACE (it))
19529 may_wrap = 1;
19530 else if (may_wrap)
19531 {
19532 SAVE_IT (wrap_it, *it, wrap_data);
19533 wrap_x = x;
19534 wrap_row_used = row->used[TEXT_AREA];
19535 wrap_row_ascent = row->ascent;
19536 wrap_row_height = row->height;
19537 wrap_row_phys_ascent = row->phys_ascent;
19538 wrap_row_phys_height = row->phys_height;
19539 wrap_row_extra_line_spacing = row->extra_line_spacing;
19540 wrap_row_min_pos = min_pos;
19541 wrap_row_min_bpos = min_bpos;
19542 wrap_row_max_pos = max_pos;
19543 wrap_row_max_bpos = max_bpos;
19544 may_wrap = 0;
19545 }
19546 }
19547 }
19548
19549 PRODUCE_GLYPHS (it);
19550
19551 /* If this display element was in marginal areas, continue with
19552 the next one. */
19553 if (it->area != TEXT_AREA)
19554 {
19555 row->ascent = max (row->ascent, it->max_ascent);
19556 row->height = max (row->height, it->max_ascent + it->max_descent);
19557 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19558 row->phys_height = max (row->phys_height,
19559 it->max_phys_ascent + it->max_phys_descent);
19560 row->extra_line_spacing = max (row->extra_line_spacing,
19561 it->max_extra_line_spacing);
19562 set_iterator_to_next (it, 1);
19563 continue;
19564 }
19565
19566 /* Does the display element fit on the line? If we truncate
19567 lines, we should draw past the right edge of the window. If
19568 we don't truncate, we want to stop so that we can display the
19569 continuation glyph before the right margin. If lines are
19570 continued, there are two possible strategies for characters
19571 resulting in more than 1 glyph (e.g. tabs): Display as many
19572 glyphs as possible in this line and leave the rest for the
19573 continuation line, or display the whole element in the next
19574 line. Original redisplay did the former, so we do it also. */
19575 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19576 hpos_before = it->hpos;
19577 x_before = x;
19578
19579 if (/* Not a newline. */
19580 nglyphs > 0
19581 /* Glyphs produced fit entirely in the line. */
19582 && it->current_x < it->last_visible_x)
19583 {
19584 it->hpos += nglyphs;
19585 row->ascent = max (row->ascent, it->max_ascent);
19586 row->height = max (row->height, it->max_ascent + it->max_descent);
19587 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19588 row->phys_height = max (row->phys_height,
19589 it->max_phys_ascent + it->max_phys_descent);
19590 row->extra_line_spacing = max (row->extra_line_spacing,
19591 it->max_extra_line_spacing);
19592 if (it->current_x - it->pixel_width < it->first_visible_x)
19593 row->x = x - it->first_visible_x;
19594 /* Record the maximum and minimum buffer positions seen so
19595 far in glyphs that will be displayed by this row. */
19596 if (it->bidi_p)
19597 RECORD_MAX_MIN_POS (it);
19598 }
19599 else
19600 {
19601 int i, new_x;
19602 struct glyph *glyph;
19603
19604 for (i = 0; i < nglyphs; ++i, x = new_x)
19605 {
19606 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19607 new_x = x + glyph->pixel_width;
19608
19609 if (/* Lines are continued. */
19610 it->line_wrap != TRUNCATE
19611 && (/* Glyph doesn't fit on the line. */
19612 new_x > it->last_visible_x
19613 /* Or it fits exactly on a window system frame. */
19614 || (new_x == it->last_visible_x
19615 && FRAME_WINDOW_P (it->f)
19616 && (row->reversed_p
19617 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19618 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19619 {
19620 /* End of a continued line. */
19621
19622 if (it->hpos == 0
19623 || (new_x == it->last_visible_x
19624 && FRAME_WINDOW_P (it->f)
19625 && (row->reversed_p
19626 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19627 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19628 {
19629 /* Current glyph is the only one on the line or
19630 fits exactly on the line. We must continue
19631 the line because we can't draw the cursor
19632 after the glyph. */
19633 row->continued_p = 1;
19634 it->current_x = new_x;
19635 it->continuation_lines_width += new_x;
19636 ++it->hpos;
19637 if (i == nglyphs - 1)
19638 {
19639 /* If line-wrap is on, check if a previous
19640 wrap point was found. */
19641 if (wrap_row_used > 0
19642 /* Even if there is a previous wrap
19643 point, continue the line here as
19644 usual, if (i) the previous character
19645 was a space or tab AND (ii) the
19646 current character is not. */
19647 && (!may_wrap
19648 || IT_DISPLAYING_WHITESPACE (it)))
19649 goto back_to_wrap;
19650
19651 /* Record the maximum and minimum buffer
19652 positions seen so far in glyphs that will be
19653 displayed by this row. */
19654 if (it->bidi_p)
19655 RECORD_MAX_MIN_POS (it);
19656 set_iterator_to_next (it, 1);
19657 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19658 {
19659 if (!get_next_display_element (it))
19660 {
19661 row->exact_window_width_line_p = 1;
19662 it->continuation_lines_width = 0;
19663 row->continued_p = 0;
19664 row->ends_at_zv_p = 1;
19665 }
19666 else if (ITERATOR_AT_END_OF_LINE_P (it))
19667 {
19668 row->continued_p = 0;
19669 row->exact_window_width_line_p = 1;
19670 }
19671 }
19672 }
19673 else if (it->bidi_p)
19674 RECORD_MAX_MIN_POS (it);
19675 }
19676 else if (CHAR_GLYPH_PADDING_P (*glyph)
19677 && !FRAME_WINDOW_P (it->f))
19678 {
19679 /* A padding glyph that doesn't fit on this line.
19680 This means the whole character doesn't fit
19681 on the line. */
19682 if (row->reversed_p)
19683 unproduce_glyphs (it, row->used[TEXT_AREA]
19684 - n_glyphs_before);
19685 row->used[TEXT_AREA] = n_glyphs_before;
19686
19687 /* Fill the rest of the row with continuation
19688 glyphs like in 20.x. */
19689 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19690 < row->glyphs[1 + TEXT_AREA])
19691 produce_special_glyphs (it, IT_CONTINUATION);
19692
19693 row->continued_p = 1;
19694 it->current_x = x_before;
19695 it->continuation_lines_width += x_before;
19696
19697 /* Restore the height to what it was before the
19698 element not fitting on the line. */
19699 it->max_ascent = ascent;
19700 it->max_descent = descent;
19701 it->max_phys_ascent = phys_ascent;
19702 it->max_phys_descent = phys_descent;
19703 }
19704 else if (wrap_row_used > 0)
19705 {
19706 back_to_wrap:
19707 if (row->reversed_p)
19708 unproduce_glyphs (it,
19709 row->used[TEXT_AREA] - wrap_row_used);
19710 RESTORE_IT (it, &wrap_it, wrap_data);
19711 it->continuation_lines_width += wrap_x;
19712 row->used[TEXT_AREA] = wrap_row_used;
19713 row->ascent = wrap_row_ascent;
19714 row->height = wrap_row_height;
19715 row->phys_ascent = wrap_row_phys_ascent;
19716 row->phys_height = wrap_row_phys_height;
19717 row->extra_line_spacing = wrap_row_extra_line_spacing;
19718 min_pos = wrap_row_min_pos;
19719 min_bpos = wrap_row_min_bpos;
19720 max_pos = wrap_row_max_pos;
19721 max_bpos = wrap_row_max_bpos;
19722 row->continued_p = 1;
19723 row->ends_at_zv_p = 0;
19724 row->exact_window_width_line_p = 0;
19725 it->continuation_lines_width += x;
19726
19727 /* Make sure that a non-default face is extended
19728 up to the right margin of the window. */
19729 extend_face_to_end_of_line (it);
19730 }
19731 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19732 {
19733 /* A TAB that extends past the right edge of the
19734 window. This produces a single glyph on
19735 window system frames. We leave the glyph in
19736 this row and let it fill the row, but don't
19737 consume the TAB. */
19738 if ((row->reversed_p
19739 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19740 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19741 produce_special_glyphs (it, IT_CONTINUATION);
19742 it->continuation_lines_width += it->last_visible_x;
19743 row->ends_in_middle_of_char_p = 1;
19744 row->continued_p = 1;
19745 glyph->pixel_width = it->last_visible_x - x;
19746 it->starts_in_middle_of_char_p = 1;
19747 }
19748 else
19749 {
19750 /* Something other than a TAB that draws past
19751 the right edge of the window. Restore
19752 positions to values before the element. */
19753 if (row->reversed_p)
19754 unproduce_glyphs (it, row->used[TEXT_AREA]
19755 - (n_glyphs_before + i));
19756 row->used[TEXT_AREA] = n_glyphs_before + i;
19757
19758 /* Display continuation glyphs. */
19759 it->current_x = x_before;
19760 it->continuation_lines_width += x;
19761 if (!FRAME_WINDOW_P (it->f)
19762 || (row->reversed_p
19763 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19764 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19765 produce_special_glyphs (it, IT_CONTINUATION);
19766 row->continued_p = 1;
19767
19768 extend_face_to_end_of_line (it);
19769
19770 if (nglyphs > 1 && i > 0)
19771 {
19772 row->ends_in_middle_of_char_p = 1;
19773 it->starts_in_middle_of_char_p = 1;
19774 }
19775
19776 /* Restore the height to what it was before the
19777 element not fitting on the line. */
19778 it->max_ascent = ascent;
19779 it->max_descent = descent;
19780 it->max_phys_ascent = phys_ascent;
19781 it->max_phys_descent = phys_descent;
19782 }
19783
19784 break;
19785 }
19786 else if (new_x > it->first_visible_x)
19787 {
19788 /* Increment number of glyphs actually displayed. */
19789 ++it->hpos;
19790
19791 /* Record the maximum and minimum buffer positions
19792 seen so far in glyphs that will be displayed by
19793 this row. */
19794 if (it->bidi_p)
19795 RECORD_MAX_MIN_POS (it);
19796
19797 if (x < it->first_visible_x)
19798 /* Glyph is partially visible, i.e. row starts at
19799 negative X position. */
19800 row->x = x - it->first_visible_x;
19801 }
19802 else
19803 {
19804 /* Glyph is completely off the left margin of the
19805 window. This should not happen because of the
19806 move_it_in_display_line at the start of this
19807 function, unless the text display area of the
19808 window is empty. */
19809 eassert (it->first_visible_x <= it->last_visible_x);
19810 }
19811 }
19812 /* Even if this display element produced no glyphs at all,
19813 we want to record its position. */
19814 if (it->bidi_p && nglyphs == 0)
19815 RECORD_MAX_MIN_POS (it);
19816
19817 row->ascent = max (row->ascent, it->max_ascent);
19818 row->height = max (row->height, it->max_ascent + it->max_descent);
19819 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19820 row->phys_height = max (row->phys_height,
19821 it->max_phys_ascent + it->max_phys_descent);
19822 row->extra_line_spacing = max (row->extra_line_spacing,
19823 it->max_extra_line_spacing);
19824
19825 /* End of this display line if row is continued. */
19826 if (row->continued_p || row->ends_at_zv_p)
19827 break;
19828 }
19829
19830 at_end_of_line:
19831 /* Is this a line end? If yes, we're also done, after making
19832 sure that a non-default face is extended up to the right
19833 margin of the window. */
19834 if (ITERATOR_AT_END_OF_LINE_P (it))
19835 {
19836 int used_before = row->used[TEXT_AREA];
19837
19838 row->ends_in_newline_from_string_p = STRINGP (it->object);
19839
19840 /* Add a space at the end of the line that is used to
19841 display the cursor there. */
19842 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19843 append_space_for_newline (it, 0);
19844
19845 /* Extend the face to the end of the line. */
19846 extend_face_to_end_of_line (it);
19847
19848 /* Make sure we have the position. */
19849 if (used_before == 0)
19850 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19851
19852 /* Record the position of the newline, for use in
19853 find_row_edges. */
19854 it->eol_pos = it->current.pos;
19855
19856 /* Consume the line end. This skips over invisible lines. */
19857 set_iterator_to_next (it, 1);
19858 it->continuation_lines_width = 0;
19859 break;
19860 }
19861
19862 /* Proceed with next display element. Note that this skips
19863 over lines invisible because of selective display. */
19864 set_iterator_to_next (it, 1);
19865
19866 /* If we truncate lines, we are done when the last displayed
19867 glyphs reach past the right margin of the window. */
19868 if (it->line_wrap == TRUNCATE
19869 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19870 ? (it->current_x >= it->last_visible_x)
19871 : (it->current_x > it->last_visible_x)))
19872 {
19873 /* Maybe add truncation glyphs. */
19874 if (!FRAME_WINDOW_P (it->f)
19875 || (row->reversed_p
19876 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19877 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19878 {
19879 int i, n;
19880
19881 if (!row->reversed_p)
19882 {
19883 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19884 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19885 break;
19886 }
19887 else
19888 {
19889 for (i = 0; i < row->used[TEXT_AREA]; i++)
19890 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19891 break;
19892 /* Remove any padding glyphs at the front of ROW, to
19893 make room for the truncation glyphs we will be
19894 adding below. The loop below always inserts at
19895 least one truncation glyph, so also remove the
19896 last glyph added to ROW. */
19897 unproduce_glyphs (it, i + 1);
19898 /* Adjust i for the loop below. */
19899 i = row->used[TEXT_AREA] - (i + 1);
19900 }
19901
19902 it->current_x = x_before;
19903 if (!FRAME_WINDOW_P (it->f))
19904 {
19905 for (n = row->used[TEXT_AREA]; i < n; ++i)
19906 {
19907 row->used[TEXT_AREA] = i;
19908 produce_special_glyphs (it, IT_TRUNCATION);
19909 }
19910 }
19911 else
19912 {
19913 row->used[TEXT_AREA] = i;
19914 produce_special_glyphs (it, IT_TRUNCATION);
19915 }
19916 }
19917 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19918 {
19919 /* Don't truncate if we can overflow newline into fringe. */
19920 if (!get_next_display_element (it))
19921 {
19922 it->continuation_lines_width = 0;
19923 row->ends_at_zv_p = 1;
19924 row->exact_window_width_line_p = 1;
19925 break;
19926 }
19927 if (ITERATOR_AT_END_OF_LINE_P (it))
19928 {
19929 row->exact_window_width_line_p = 1;
19930 goto at_end_of_line;
19931 }
19932 it->current_x = x_before;
19933 }
19934
19935 row->truncated_on_right_p = 1;
19936 it->continuation_lines_width = 0;
19937 reseat_at_next_visible_line_start (it, 0);
19938 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19939 it->hpos = hpos_before;
19940 break;
19941 }
19942 }
19943
19944 if (wrap_data)
19945 bidi_unshelve_cache (wrap_data, 1);
19946
19947 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19948 at the left window margin. */
19949 if (it->first_visible_x
19950 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19951 {
19952 if (!FRAME_WINDOW_P (it->f)
19953 || (row->reversed_p
19954 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19955 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19956 insert_left_trunc_glyphs (it);
19957 row->truncated_on_left_p = 1;
19958 }
19959
19960 /* Remember the position at which this line ends.
19961
19962 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19963 cannot be before the call to find_row_edges below, since that is
19964 where these positions are determined. */
19965 row->end = it->current;
19966 if (!it->bidi_p)
19967 {
19968 row->minpos = row->start.pos;
19969 row->maxpos = row->end.pos;
19970 }
19971 else
19972 {
19973 /* ROW->minpos and ROW->maxpos must be the smallest and
19974 `1 + the largest' buffer positions in ROW. But if ROW was
19975 bidi-reordered, these two positions can be anywhere in the
19976 row, so we must determine them now. */
19977 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19978 }
19979
19980 /* If the start of this line is the overlay arrow-position, then
19981 mark this glyph row as the one containing the overlay arrow.
19982 This is clearly a mess with variable size fonts. It would be
19983 better to let it be displayed like cursors under X. */
19984 if ((row->displays_text_p || !overlay_arrow_seen)
19985 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19986 !NILP (overlay_arrow_string)))
19987 {
19988 /* Overlay arrow in window redisplay is a fringe bitmap. */
19989 if (STRINGP (overlay_arrow_string))
19990 {
19991 struct glyph_row *arrow_row
19992 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19993 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19994 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19995 struct glyph *p = row->glyphs[TEXT_AREA];
19996 struct glyph *p2, *end;
19997
19998 /* Copy the arrow glyphs. */
19999 while (glyph < arrow_end)
20000 *p++ = *glyph++;
20001
20002 /* Throw away padding glyphs. */
20003 p2 = p;
20004 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
20005 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
20006 ++p2;
20007 if (p2 > p)
20008 {
20009 while (p2 < end)
20010 *p++ = *p2++;
20011 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
20012 }
20013 }
20014 else
20015 {
20016 eassert (INTEGERP (overlay_arrow_string));
20017 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
20018 }
20019 overlay_arrow_seen = 1;
20020 }
20021
20022 /* Highlight trailing whitespace. */
20023 if (!NILP (Vshow_trailing_whitespace))
20024 highlight_trailing_whitespace (it->f, it->glyph_row);
20025
20026 /* Compute pixel dimensions of this line. */
20027 compute_line_metrics (it);
20028
20029 /* Implementation note: No changes in the glyphs of ROW or in their
20030 faces can be done past this point, because compute_line_metrics
20031 computes ROW's hash value and stores it within the glyph_row
20032 structure. */
20033
20034 /* Record whether this row ends inside an ellipsis. */
20035 row->ends_in_ellipsis_p
20036 = (it->method == GET_FROM_DISPLAY_VECTOR
20037 && it->ellipsis_p);
20038
20039 /* Save fringe bitmaps in this row. */
20040 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
20041 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
20042 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
20043 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
20044
20045 it->left_user_fringe_bitmap = 0;
20046 it->left_user_fringe_face_id = 0;
20047 it->right_user_fringe_bitmap = 0;
20048 it->right_user_fringe_face_id = 0;
20049
20050 /* Maybe set the cursor. */
20051 cvpos = it->w->cursor.vpos;
20052 if ((cvpos < 0
20053 /* In bidi-reordered rows, keep checking for proper cursor
20054 position even if one has been found already, because buffer
20055 positions in such rows change non-linearly with ROW->VPOS,
20056 when a line is continued. One exception: when we are at ZV,
20057 display cursor on the first suitable glyph row, since all
20058 the empty rows after that also have their position set to ZV. */
20059 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20060 lines' rows is implemented for bidi-reordered rows. */
20061 || (it->bidi_p
20062 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
20063 && PT >= MATRIX_ROW_START_CHARPOS (row)
20064 && PT <= MATRIX_ROW_END_CHARPOS (row)
20065 && cursor_row_p (row))
20066 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
20067
20068 /* Prepare for the next line. This line starts horizontally at (X
20069 HPOS) = (0 0). Vertical positions are incremented. As a
20070 convenience for the caller, IT->glyph_row is set to the next
20071 row to be used. */
20072 it->current_x = it->hpos = 0;
20073 it->current_y += row->height;
20074 SET_TEXT_POS (it->eol_pos, 0, 0);
20075 ++it->vpos;
20076 ++it->glyph_row;
20077 /* The next row should by default use the same value of the
20078 reversed_p flag as this one. set_iterator_to_next decides when
20079 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
20080 the flag accordingly. */
20081 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
20082 it->glyph_row->reversed_p = row->reversed_p;
20083 it->start = row->end;
20084 return row->displays_text_p;
20085
20086 #undef RECORD_MAX_MIN_POS
20087 }
20088
20089 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
20090 Scurrent_bidi_paragraph_direction, 0, 1, 0,
20091 doc: /* Return paragraph direction at point in BUFFER.
20092 Value is either `left-to-right' or `right-to-left'.
20093 If BUFFER is omitted or nil, it defaults to the current buffer.
20094
20095 Paragraph direction determines how the text in the paragraph is displayed.
20096 In left-to-right paragraphs, text begins at the left margin of the window
20097 and the reading direction is generally left to right. In right-to-left
20098 paragraphs, text begins at the right margin and is read from right to left.
20099
20100 See also `bidi-paragraph-direction'. */)
20101 (Lisp_Object buffer)
20102 {
20103 struct buffer *buf = current_buffer;
20104 struct buffer *old = buf;
20105
20106 if (! NILP (buffer))
20107 {
20108 CHECK_BUFFER (buffer);
20109 buf = XBUFFER (buffer);
20110 }
20111
20112 if (NILP (BVAR (buf, bidi_display_reordering))
20113 || NILP (BVAR (buf, enable_multibyte_characters))
20114 /* When we are loading loadup.el, the character property tables
20115 needed for bidi iteration are not yet available. */
20116 || !NILP (Vpurify_flag))
20117 return Qleft_to_right;
20118 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20119 return BVAR (buf, bidi_paragraph_direction);
20120 else
20121 {
20122 /* Determine the direction from buffer text. We could try to
20123 use current_matrix if it is up to date, but this seems fast
20124 enough as it is. */
20125 struct bidi_it itb;
20126 ptrdiff_t pos = BUF_PT (buf);
20127 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20128 int c;
20129 void *itb_data = bidi_shelve_cache ();
20130
20131 set_buffer_temp (buf);
20132 /* bidi_paragraph_init finds the base direction of the paragraph
20133 by searching forward from paragraph start. We need the base
20134 direction of the current or _previous_ paragraph, so we need
20135 to make sure we are within that paragraph. To that end, find
20136 the previous non-empty line. */
20137 if (pos >= ZV && pos > BEGV)
20138 {
20139 pos--;
20140 bytepos = CHAR_TO_BYTE (pos);
20141 }
20142 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20143 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20144 {
20145 while ((c = FETCH_BYTE (bytepos)) == '\n'
20146 || c == ' ' || c == '\t' || c == '\f')
20147 {
20148 if (bytepos <= BEGV_BYTE)
20149 break;
20150 bytepos--;
20151 pos--;
20152 }
20153 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20154 bytepos--;
20155 }
20156 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20157 itb.paragraph_dir = NEUTRAL_DIR;
20158 itb.string.s = NULL;
20159 itb.string.lstring = Qnil;
20160 itb.string.bufpos = 0;
20161 itb.string.unibyte = 0;
20162 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20163 bidi_unshelve_cache (itb_data, 0);
20164 set_buffer_temp (old);
20165 switch (itb.paragraph_dir)
20166 {
20167 case L2R:
20168 return Qleft_to_right;
20169 break;
20170 case R2L:
20171 return Qright_to_left;
20172 break;
20173 default:
20174 emacs_abort ();
20175 }
20176 }
20177 }
20178
20179
20180 \f
20181 /***********************************************************************
20182 Menu Bar
20183 ***********************************************************************/
20184
20185 /* Redisplay the menu bar in the frame for window W.
20186
20187 The menu bar of X frames that don't have X toolkit support is
20188 displayed in a special window W->frame->menu_bar_window.
20189
20190 The menu bar of terminal frames is treated specially as far as
20191 glyph matrices are concerned. Menu bar lines are not part of
20192 windows, so the update is done directly on the frame matrix rows
20193 for the menu bar. */
20194
20195 static void
20196 display_menu_bar (struct window *w)
20197 {
20198 struct frame *f = XFRAME (WINDOW_FRAME (w));
20199 struct it it;
20200 Lisp_Object items;
20201 int i;
20202
20203 /* Don't do all this for graphical frames. */
20204 #ifdef HAVE_NTGUI
20205 if (FRAME_W32_P (f))
20206 return;
20207 #endif
20208 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20209 if (FRAME_X_P (f))
20210 return;
20211 #endif
20212
20213 #ifdef HAVE_NS
20214 if (FRAME_NS_P (f))
20215 return;
20216 #endif /* HAVE_NS */
20217
20218 #ifdef USE_X_TOOLKIT
20219 eassert (!FRAME_WINDOW_P (f));
20220 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20221 it.first_visible_x = 0;
20222 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20223 #else /* not USE_X_TOOLKIT */
20224 if (FRAME_WINDOW_P (f))
20225 {
20226 /* Menu bar lines are displayed in the desired matrix of the
20227 dummy window menu_bar_window. */
20228 struct window *menu_w;
20229 eassert (WINDOWP (f->menu_bar_window));
20230 menu_w = XWINDOW (f->menu_bar_window);
20231 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20232 MENU_FACE_ID);
20233 it.first_visible_x = 0;
20234 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20235 }
20236 else
20237 {
20238 /* This is a TTY frame, i.e. character hpos/vpos are used as
20239 pixel x/y. */
20240 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20241 MENU_FACE_ID);
20242 it.first_visible_x = 0;
20243 it.last_visible_x = FRAME_COLS (f);
20244 }
20245 #endif /* not USE_X_TOOLKIT */
20246
20247 /* FIXME: This should be controlled by a user option. See the
20248 comments in redisplay_tool_bar and display_mode_line about
20249 this. */
20250 it.paragraph_embedding = L2R;
20251
20252 /* Clear all rows of the menu bar. */
20253 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20254 {
20255 struct glyph_row *row = it.glyph_row + i;
20256 clear_glyph_row (row);
20257 row->enabled_p = 1;
20258 row->full_width_p = 1;
20259 }
20260
20261 /* Display all items of the menu bar. */
20262 items = FRAME_MENU_BAR_ITEMS (it.f);
20263 for (i = 0; i < ASIZE (items); i += 4)
20264 {
20265 Lisp_Object string;
20266
20267 /* Stop at nil string. */
20268 string = AREF (items, i + 1);
20269 if (NILP (string))
20270 break;
20271
20272 /* Remember where item was displayed. */
20273 ASET (items, i + 3, make_number (it.hpos));
20274
20275 /* Display the item, pad with one space. */
20276 if (it.current_x < it.last_visible_x)
20277 display_string (NULL, string, Qnil, 0, 0, &it,
20278 SCHARS (string) + 1, 0, 0, -1);
20279 }
20280
20281 /* Fill out the line with spaces. */
20282 if (it.current_x < it.last_visible_x)
20283 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20284
20285 /* Compute the total height of the lines. */
20286 compute_line_metrics (&it);
20287 }
20288
20289
20290 \f
20291 /***********************************************************************
20292 Mode Line
20293 ***********************************************************************/
20294
20295 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20296 FORCE is non-zero, redisplay mode lines unconditionally.
20297 Otherwise, redisplay only mode lines that are garbaged. Value is
20298 the number of windows whose mode lines were redisplayed. */
20299
20300 static int
20301 redisplay_mode_lines (Lisp_Object window, int force)
20302 {
20303 int nwindows = 0;
20304
20305 while (!NILP (window))
20306 {
20307 struct window *w = XWINDOW (window);
20308
20309 if (WINDOWP (w->hchild))
20310 nwindows += redisplay_mode_lines (w->hchild, force);
20311 else if (WINDOWP (w->vchild))
20312 nwindows += redisplay_mode_lines (w->vchild, force);
20313 else if (force
20314 || FRAME_GARBAGED_P (XFRAME (w->frame))
20315 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20316 {
20317 struct text_pos lpoint;
20318 struct buffer *old = current_buffer;
20319
20320 /* Set the window's buffer for the mode line display. */
20321 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20322 set_buffer_internal_1 (XBUFFER (w->buffer));
20323
20324 /* Point refers normally to the selected window. For any
20325 other window, set up appropriate value. */
20326 if (!EQ (window, selected_window))
20327 {
20328 struct text_pos pt;
20329
20330 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
20331 if (CHARPOS (pt) < BEGV)
20332 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
20333 else if (CHARPOS (pt) > (ZV - 1))
20334 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
20335 else
20336 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20337 }
20338
20339 /* Display mode lines. */
20340 clear_glyph_matrix (w->desired_matrix);
20341 if (display_mode_lines (w))
20342 {
20343 ++nwindows;
20344 w->must_be_updated_p = 1;
20345 }
20346
20347 /* Restore old settings. */
20348 set_buffer_internal_1 (old);
20349 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20350 }
20351
20352 window = w->next;
20353 }
20354
20355 return nwindows;
20356 }
20357
20358
20359 /* Display the mode and/or header line of window W. Value is the
20360 sum number of mode lines and header lines displayed. */
20361
20362 static int
20363 display_mode_lines (struct window *w)
20364 {
20365 Lisp_Object old_selected_window, old_selected_frame;
20366 int n = 0;
20367
20368 old_selected_frame = selected_frame;
20369 selected_frame = w->frame;
20370 old_selected_window = selected_window;
20371 XSETWINDOW (selected_window, w);
20372
20373 /* These will be set while the mode line specs are processed. */
20374 line_number_displayed = 0;
20375 wset_column_number_displayed (w, Qnil);
20376
20377 if (WINDOW_WANTS_MODELINE_P (w))
20378 {
20379 struct window *sel_w = XWINDOW (old_selected_window);
20380
20381 /* Select mode line face based on the real selected window. */
20382 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20383 BVAR (current_buffer, mode_line_format));
20384 ++n;
20385 }
20386
20387 if (WINDOW_WANTS_HEADER_LINE_P (w))
20388 {
20389 display_mode_line (w, HEADER_LINE_FACE_ID,
20390 BVAR (current_buffer, header_line_format));
20391 ++n;
20392 }
20393
20394 selected_frame = old_selected_frame;
20395 selected_window = old_selected_window;
20396 return n;
20397 }
20398
20399
20400 /* Display mode or header line of window W. FACE_ID specifies which
20401 line to display; it is either MODE_LINE_FACE_ID or
20402 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20403 display. Value is the pixel height of the mode/header line
20404 displayed. */
20405
20406 static int
20407 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20408 {
20409 struct it it;
20410 struct face *face;
20411 ptrdiff_t count = SPECPDL_INDEX ();
20412
20413 init_iterator (&it, w, -1, -1, NULL, face_id);
20414 /* Don't extend on a previously drawn mode-line.
20415 This may happen if called from pos_visible_p. */
20416 it.glyph_row->enabled_p = 0;
20417 prepare_desired_row (it.glyph_row);
20418
20419 it.glyph_row->mode_line_p = 1;
20420
20421 /* FIXME: This should be controlled by a user option. But
20422 supporting such an option is not trivial, since the mode line is
20423 made up of many separate strings. */
20424 it.paragraph_embedding = L2R;
20425
20426 record_unwind_protect (unwind_format_mode_line,
20427 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20428
20429 mode_line_target = MODE_LINE_DISPLAY;
20430
20431 /* Temporarily make frame's keyboard the current kboard so that
20432 kboard-local variables in the mode_line_format will get the right
20433 values. */
20434 push_kboard (FRAME_KBOARD (it.f));
20435 record_unwind_save_match_data ();
20436 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20437 pop_kboard ();
20438
20439 unbind_to (count, Qnil);
20440
20441 /* Fill up with spaces. */
20442 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20443
20444 compute_line_metrics (&it);
20445 it.glyph_row->full_width_p = 1;
20446 it.glyph_row->continued_p = 0;
20447 it.glyph_row->truncated_on_left_p = 0;
20448 it.glyph_row->truncated_on_right_p = 0;
20449
20450 /* Make a 3D mode-line have a shadow at its right end. */
20451 face = FACE_FROM_ID (it.f, face_id);
20452 extend_face_to_end_of_line (&it);
20453 if (face->box != FACE_NO_BOX)
20454 {
20455 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20456 + it.glyph_row->used[TEXT_AREA] - 1);
20457 last->right_box_line_p = 1;
20458 }
20459
20460 return it.glyph_row->height;
20461 }
20462
20463 /* Move element ELT in LIST to the front of LIST.
20464 Return the updated list. */
20465
20466 static Lisp_Object
20467 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20468 {
20469 register Lisp_Object tail, prev;
20470 register Lisp_Object tem;
20471
20472 tail = list;
20473 prev = Qnil;
20474 while (CONSP (tail))
20475 {
20476 tem = XCAR (tail);
20477
20478 if (EQ (elt, tem))
20479 {
20480 /* Splice out the link TAIL. */
20481 if (NILP (prev))
20482 list = XCDR (tail);
20483 else
20484 Fsetcdr (prev, XCDR (tail));
20485
20486 /* Now make it the first. */
20487 Fsetcdr (tail, list);
20488 return tail;
20489 }
20490 else
20491 prev = tail;
20492 tail = XCDR (tail);
20493 QUIT;
20494 }
20495
20496 /* Not found--return unchanged LIST. */
20497 return list;
20498 }
20499
20500 /* Contribute ELT to the mode line for window IT->w. How it
20501 translates into text depends on its data type.
20502
20503 IT describes the display environment in which we display, as usual.
20504
20505 DEPTH is the depth in recursion. It is used to prevent
20506 infinite recursion here.
20507
20508 FIELD_WIDTH is the number of characters the display of ELT should
20509 occupy in the mode line, and PRECISION is the maximum number of
20510 characters to display from ELT's representation. See
20511 display_string for details.
20512
20513 Returns the hpos of the end of the text generated by ELT.
20514
20515 PROPS is a property list to add to any string we encounter.
20516
20517 If RISKY is nonzero, remove (disregard) any properties in any string
20518 we encounter, and ignore :eval and :propertize.
20519
20520 The global variable `mode_line_target' determines whether the
20521 output is passed to `store_mode_line_noprop',
20522 `store_mode_line_string', or `display_string'. */
20523
20524 static int
20525 display_mode_element (struct it *it, int depth, int field_width, int precision,
20526 Lisp_Object elt, Lisp_Object props, int risky)
20527 {
20528 int n = 0, field, prec;
20529 int literal = 0;
20530
20531 tail_recurse:
20532 if (depth > 100)
20533 elt = build_string ("*too-deep*");
20534
20535 depth++;
20536
20537 switch (XTYPE (elt))
20538 {
20539 case Lisp_String:
20540 {
20541 /* A string: output it and check for %-constructs within it. */
20542 unsigned char c;
20543 ptrdiff_t offset = 0;
20544
20545 if (SCHARS (elt) > 0
20546 && (!NILP (props) || risky))
20547 {
20548 Lisp_Object oprops, aelt;
20549 oprops = Ftext_properties_at (make_number (0), elt);
20550
20551 /* If the starting string's properties are not what
20552 we want, translate the string. Also, if the string
20553 is risky, do that anyway. */
20554
20555 if (NILP (Fequal (props, oprops)) || risky)
20556 {
20557 /* If the starting string has properties,
20558 merge the specified ones onto the existing ones. */
20559 if (! NILP (oprops) && !risky)
20560 {
20561 Lisp_Object tem;
20562
20563 oprops = Fcopy_sequence (oprops);
20564 tem = props;
20565 while (CONSP (tem))
20566 {
20567 oprops = Fplist_put (oprops, XCAR (tem),
20568 XCAR (XCDR (tem)));
20569 tem = XCDR (XCDR (tem));
20570 }
20571 props = oprops;
20572 }
20573
20574 aelt = Fassoc (elt, mode_line_proptrans_alist);
20575 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20576 {
20577 /* AELT is what we want. Move it to the front
20578 without consing. */
20579 elt = XCAR (aelt);
20580 mode_line_proptrans_alist
20581 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20582 }
20583 else
20584 {
20585 Lisp_Object tem;
20586
20587 /* If AELT has the wrong props, it is useless.
20588 so get rid of it. */
20589 if (! NILP (aelt))
20590 mode_line_proptrans_alist
20591 = Fdelq (aelt, mode_line_proptrans_alist);
20592
20593 elt = Fcopy_sequence (elt);
20594 Fset_text_properties (make_number (0), Flength (elt),
20595 props, elt);
20596 /* Add this item to mode_line_proptrans_alist. */
20597 mode_line_proptrans_alist
20598 = Fcons (Fcons (elt, props),
20599 mode_line_proptrans_alist);
20600 /* Truncate mode_line_proptrans_alist
20601 to at most 50 elements. */
20602 tem = Fnthcdr (make_number (50),
20603 mode_line_proptrans_alist);
20604 if (! NILP (tem))
20605 XSETCDR (tem, Qnil);
20606 }
20607 }
20608 }
20609
20610 offset = 0;
20611
20612 if (literal)
20613 {
20614 prec = precision - n;
20615 switch (mode_line_target)
20616 {
20617 case MODE_LINE_NOPROP:
20618 case MODE_LINE_TITLE:
20619 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20620 break;
20621 case MODE_LINE_STRING:
20622 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20623 break;
20624 case MODE_LINE_DISPLAY:
20625 n += display_string (NULL, elt, Qnil, 0, 0, it,
20626 0, prec, 0, STRING_MULTIBYTE (elt));
20627 break;
20628 }
20629
20630 break;
20631 }
20632
20633 /* Handle the non-literal case. */
20634
20635 while ((precision <= 0 || n < precision)
20636 && SREF (elt, offset) != 0
20637 && (mode_line_target != MODE_LINE_DISPLAY
20638 || it->current_x < it->last_visible_x))
20639 {
20640 ptrdiff_t last_offset = offset;
20641
20642 /* Advance to end of string or next format specifier. */
20643 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20644 ;
20645
20646 if (offset - 1 != last_offset)
20647 {
20648 ptrdiff_t nchars, nbytes;
20649
20650 /* Output to end of string or up to '%'. Field width
20651 is length of string. Don't output more than
20652 PRECISION allows us. */
20653 offset--;
20654
20655 prec = c_string_width (SDATA (elt) + last_offset,
20656 offset - last_offset, precision - n,
20657 &nchars, &nbytes);
20658
20659 switch (mode_line_target)
20660 {
20661 case MODE_LINE_NOPROP:
20662 case MODE_LINE_TITLE:
20663 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20664 break;
20665 case MODE_LINE_STRING:
20666 {
20667 ptrdiff_t bytepos = last_offset;
20668 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20669 ptrdiff_t endpos = (precision <= 0
20670 ? string_byte_to_char (elt, offset)
20671 : charpos + nchars);
20672
20673 n += store_mode_line_string (NULL,
20674 Fsubstring (elt, make_number (charpos),
20675 make_number (endpos)),
20676 0, 0, 0, Qnil);
20677 }
20678 break;
20679 case MODE_LINE_DISPLAY:
20680 {
20681 ptrdiff_t bytepos = last_offset;
20682 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20683
20684 if (precision <= 0)
20685 nchars = string_byte_to_char (elt, offset) - charpos;
20686 n += display_string (NULL, elt, Qnil, 0, charpos,
20687 it, 0, nchars, 0,
20688 STRING_MULTIBYTE (elt));
20689 }
20690 break;
20691 }
20692 }
20693 else /* c == '%' */
20694 {
20695 ptrdiff_t percent_position = offset;
20696
20697 /* Get the specified minimum width. Zero means
20698 don't pad. */
20699 field = 0;
20700 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20701 field = field * 10 + c - '0';
20702
20703 /* Don't pad beyond the total padding allowed. */
20704 if (field_width - n > 0 && field > field_width - n)
20705 field = field_width - n;
20706
20707 /* Note that either PRECISION <= 0 or N < PRECISION. */
20708 prec = precision - n;
20709
20710 if (c == 'M')
20711 n += display_mode_element (it, depth, field, prec,
20712 Vglobal_mode_string, props,
20713 risky);
20714 else if (c != 0)
20715 {
20716 int multibyte;
20717 ptrdiff_t bytepos, charpos;
20718 const char *spec;
20719 Lisp_Object string;
20720
20721 bytepos = percent_position;
20722 charpos = (STRING_MULTIBYTE (elt)
20723 ? string_byte_to_char (elt, bytepos)
20724 : bytepos);
20725 spec = decode_mode_spec (it->w, c, field, &string);
20726 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
20727
20728 switch (mode_line_target)
20729 {
20730 case MODE_LINE_NOPROP:
20731 case MODE_LINE_TITLE:
20732 n += store_mode_line_noprop (spec, field, prec);
20733 break;
20734 case MODE_LINE_STRING:
20735 {
20736 Lisp_Object tem = build_string (spec);
20737 props = Ftext_properties_at (make_number (charpos), elt);
20738 /* Should only keep face property in props */
20739 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
20740 }
20741 break;
20742 case MODE_LINE_DISPLAY:
20743 {
20744 int nglyphs_before, nwritten;
20745
20746 nglyphs_before = it->glyph_row->used[TEXT_AREA];
20747 nwritten = display_string (spec, string, elt,
20748 charpos, 0, it,
20749 field, prec, 0,
20750 multibyte);
20751
20752 /* Assign to the glyphs written above the
20753 string where the `%x' came from, position
20754 of the `%'. */
20755 if (nwritten > 0)
20756 {
20757 struct glyph *glyph
20758 = (it->glyph_row->glyphs[TEXT_AREA]
20759 + nglyphs_before);
20760 int i;
20761
20762 for (i = 0; i < nwritten; ++i)
20763 {
20764 glyph[i].object = elt;
20765 glyph[i].charpos = charpos;
20766 }
20767
20768 n += nwritten;
20769 }
20770 }
20771 break;
20772 }
20773 }
20774 else /* c == 0 */
20775 break;
20776 }
20777 }
20778 }
20779 break;
20780
20781 case Lisp_Symbol:
20782 /* A symbol: process the value of the symbol recursively
20783 as if it appeared here directly. Avoid error if symbol void.
20784 Special case: if value of symbol is a string, output the string
20785 literally. */
20786 {
20787 register Lisp_Object tem;
20788
20789 /* If the variable is not marked as risky to set
20790 then its contents are risky to use. */
20791 if (NILP (Fget (elt, Qrisky_local_variable)))
20792 risky = 1;
20793
20794 tem = Fboundp (elt);
20795 if (!NILP (tem))
20796 {
20797 tem = Fsymbol_value (elt);
20798 /* If value is a string, output that string literally:
20799 don't check for % within it. */
20800 if (STRINGP (tem))
20801 literal = 1;
20802
20803 if (!EQ (tem, elt))
20804 {
20805 /* Give up right away for nil or t. */
20806 elt = tem;
20807 goto tail_recurse;
20808 }
20809 }
20810 }
20811 break;
20812
20813 case Lisp_Cons:
20814 {
20815 register Lisp_Object car, tem;
20816
20817 /* A cons cell: five distinct cases.
20818 If first element is :eval or :propertize, do something special.
20819 If first element is a string or a cons, process all the elements
20820 and effectively concatenate them.
20821 If first element is a negative number, truncate displaying cdr to
20822 at most that many characters. If positive, pad (with spaces)
20823 to at least that many characters.
20824 If first element is a symbol, process the cadr or caddr recursively
20825 according to whether the symbol's value is non-nil or nil. */
20826 car = XCAR (elt);
20827 if (EQ (car, QCeval))
20828 {
20829 /* An element of the form (:eval FORM) means evaluate FORM
20830 and use the result as mode line elements. */
20831
20832 if (risky)
20833 break;
20834
20835 if (CONSP (XCDR (elt)))
20836 {
20837 Lisp_Object spec;
20838 spec = safe_eval (XCAR (XCDR (elt)));
20839 n += display_mode_element (it, depth, field_width - n,
20840 precision - n, spec, props,
20841 risky);
20842 }
20843 }
20844 else if (EQ (car, QCpropertize))
20845 {
20846 /* An element of the form (:propertize ELT PROPS...)
20847 means display ELT but applying properties PROPS. */
20848
20849 if (risky)
20850 break;
20851
20852 if (CONSP (XCDR (elt)))
20853 n += display_mode_element (it, depth, field_width - n,
20854 precision - n, XCAR (XCDR (elt)),
20855 XCDR (XCDR (elt)), risky);
20856 }
20857 else if (SYMBOLP (car))
20858 {
20859 tem = Fboundp (car);
20860 elt = XCDR (elt);
20861 if (!CONSP (elt))
20862 goto invalid;
20863 /* elt is now the cdr, and we know it is a cons cell.
20864 Use its car if CAR has a non-nil value. */
20865 if (!NILP (tem))
20866 {
20867 tem = Fsymbol_value (car);
20868 if (!NILP (tem))
20869 {
20870 elt = XCAR (elt);
20871 goto tail_recurse;
20872 }
20873 }
20874 /* Symbol's value is nil (or symbol is unbound)
20875 Get the cddr of the original list
20876 and if possible find the caddr and use that. */
20877 elt = XCDR (elt);
20878 if (NILP (elt))
20879 break;
20880 else if (!CONSP (elt))
20881 goto invalid;
20882 elt = XCAR (elt);
20883 goto tail_recurse;
20884 }
20885 else if (INTEGERP (car))
20886 {
20887 register int lim = XINT (car);
20888 elt = XCDR (elt);
20889 if (lim < 0)
20890 {
20891 /* Negative int means reduce maximum width. */
20892 if (precision <= 0)
20893 precision = -lim;
20894 else
20895 precision = min (precision, -lim);
20896 }
20897 else if (lim > 0)
20898 {
20899 /* Padding specified. Don't let it be more than
20900 current maximum. */
20901 if (precision > 0)
20902 lim = min (precision, lim);
20903
20904 /* If that's more padding than already wanted, queue it.
20905 But don't reduce padding already specified even if
20906 that is beyond the current truncation point. */
20907 field_width = max (lim, field_width);
20908 }
20909 goto tail_recurse;
20910 }
20911 else if (STRINGP (car) || CONSP (car))
20912 {
20913 Lisp_Object halftail = elt;
20914 int len = 0;
20915
20916 while (CONSP (elt)
20917 && (precision <= 0 || n < precision))
20918 {
20919 n += display_mode_element (it, depth,
20920 /* Do padding only after the last
20921 element in the list. */
20922 (! CONSP (XCDR (elt))
20923 ? field_width - n
20924 : 0),
20925 precision - n, XCAR (elt),
20926 props, risky);
20927 elt = XCDR (elt);
20928 len++;
20929 if ((len & 1) == 0)
20930 halftail = XCDR (halftail);
20931 /* Check for cycle. */
20932 if (EQ (halftail, elt))
20933 break;
20934 }
20935 }
20936 }
20937 break;
20938
20939 default:
20940 invalid:
20941 elt = build_string ("*invalid*");
20942 goto tail_recurse;
20943 }
20944
20945 /* Pad to FIELD_WIDTH. */
20946 if (field_width > 0 && n < field_width)
20947 {
20948 switch (mode_line_target)
20949 {
20950 case MODE_LINE_NOPROP:
20951 case MODE_LINE_TITLE:
20952 n += store_mode_line_noprop ("", field_width - n, 0);
20953 break;
20954 case MODE_LINE_STRING:
20955 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
20956 break;
20957 case MODE_LINE_DISPLAY:
20958 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
20959 0, 0, 0);
20960 break;
20961 }
20962 }
20963
20964 return n;
20965 }
20966
20967 /* Store a mode-line string element in mode_line_string_list.
20968
20969 If STRING is non-null, display that C string. Otherwise, the Lisp
20970 string LISP_STRING is displayed.
20971
20972 FIELD_WIDTH is the minimum number of output glyphs to produce.
20973 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20974 with spaces. FIELD_WIDTH <= 0 means don't pad.
20975
20976 PRECISION is the maximum number of characters to output from
20977 STRING. PRECISION <= 0 means don't truncate the string.
20978
20979 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
20980 properties to the string.
20981
20982 PROPS are the properties to add to the string.
20983 The mode_line_string_face face property is always added to the string.
20984 */
20985
20986 static int
20987 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
20988 int field_width, int precision, Lisp_Object props)
20989 {
20990 ptrdiff_t len;
20991 int n = 0;
20992
20993 if (string != NULL)
20994 {
20995 len = strlen (string);
20996 if (precision > 0 && len > precision)
20997 len = precision;
20998 lisp_string = make_string (string, len);
20999 if (NILP (props))
21000 props = mode_line_string_face_prop;
21001 else if (!NILP (mode_line_string_face))
21002 {
21003 Lisp_Object face = Fplist_get (props, Qface);
21004 props = Fcopy_sequence (props);
21005 if (NILP (face))
21006 face = mode_line_string_face;
21007 else
21008 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
21009 props = Fplist_put (props, Qface, face);
21010 }
21011 Fadd_text_properties (make_number (0), make_number (len),
21012 props, lisp_string);
21013 }
21014 else
21015 {
21016 len = XFASTINT (Flength (lisp_string));
21017 if (precision > 0 && len > precision)
21018 {
21019 len = precision;
21020 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
21021 precision = -1;
21022 }
21023 if (!NILP (mode_line_string_face))
21024 {
21025 Lisp_Object face;
21026 if (NILP (props))
21027 props = Ftext_properties_at (make_number (0), lisp_string);
21028 face = Fplist_get (props, Qface);
21029 if (NILP (face))
21030 face = mode_line_string_face;
21031 else
21032 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
21033 props = Fcons (Qface, Fcons (face, Qnil));
21034 if (copy_string)
21035 lisp_string = Fcopy_sequence (lisp_string);
21036 }
21037 if (!NILP (props))
21038 Fadd_text_properties (make_number (0), make_number (len),
21039 props, lisp_string);
21040 }
21041
21042 if (len > 0)
21043 {
21044 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21045 n += len;
21046 }
21047
21048 if (field_width > len)
21049 {
21050 field_width -= len;
21051 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
21052 if (!NILP (props))
21053 Fadd_text_properties (make_number (0), make_number (field_width),
21054 props, lisp_string);
21055 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21056 n += field_width;
21057 }
21058
21059 return n;
21060 }
21061
21062
21063 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
21064 1, 4, 0,
21065 doc: /* Format a string out of a mode line format specification.
21066 First arg FORMAT specifies the mode line format (see `mode-line-format'
21067 for details) to use.
21068
21069 By default, the format is evaluated for the currently selected window.
21070
21071 Optional second arg FACE specifies the face property to put on all
21072 characters for which no face is specified. The value nil means the
21073 default face. The value t means whatever face the window's mode line
21074 currently uses (either `mode-line' or `mode-line-inactive',
21075 depending on whether the window is the selected window or not).
21076 An integer value means the value string has no text
21077 properties.
21078
21079 Optional third and fourth args WINDOW and BUFFER specify the window
21080 and buffer to use as the context for the formatting (defaults
21081 are the selected window and the WINDOW's buffer). */)
21082 (Lisp_Object format, Lisp_Object face,
21083 Lisp_Object window, Lisp_Object buffer)
21084 {
21085 struct it it;
21086 int len;
21087 struct window *w;
21088 struct buffer *old_buffer = NULL;
21089 int face_id;
21090 int no_props = INTEGERP (face);
21091 ptrdiff_t count = SPECPDL_INDEX ();
21092 Lisp_Object str;
21093 int string_start = 0;
21094
21095 if (NILP (window))
21096 window = selected_window;
21097 CHECK_WINDOW (window);
21098 w = XWINDOW (window);
21099
21100 if (NILP (buffer))
21101 buffer = w->buffer;
21102 CHECK_BUFFER (buffer);
21103
21104 /* Make formatting the modeline a non-op when noninteractive, otherwise
21105 there will be problems later caused by a partially initialized frame. */
21106 if (NILP (format) || noninteractive)
21107 return empty_unibyte_string;
21108
21109 if (no_props)
21110 face = Qnil;
21111
21112 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
21113 : EQ (face, Qt) ? (EQ (window, selected_window)
21114 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21115 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21116 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21117 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21118 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21119 : DEFAULT_FACE_ID;
21120
21121 old_buffer = current_buffer;
21122
21123 /* Save things including mode_line_proptrans_alist,
21124 and set that to nil so that we don't alter the outer value. */
21125 record_unwind_protect (unwind_format_mode_line,
21126 format_mode_line_unwind_data
21127 (XFRAME (WINDOW_FRAME (XWINDOW (window))),
21128 old_buffer, selected_window, 1));
21129 mode_line_proptrans_alist = Qnil;
21130
21131 Fselect_window (window, Qt);
21132 set_buffer_internal_1 (XBUFFER (buffer));
21133
21134 init_iterator (&it, w, -1, -1, NULL, face_id);
21135
21136 if (no_props)
21137 {
21138 mode_line_target = MODE_LINE_NOPROP;
21139 mode_line_string_face_prop = Qnil;
21140 mode_line_string_list = Qnil;
21141 string_start = MODE_LINE_NOPROP_LEN (0);
21142 }
21143 else
21144 {
21145 mode_line_target = MODE_LINE_STRING;
21146 mode_line_string_list = Qnil;
21147 mode_line_string_face = face;
21148 mode_line_string_face_prop
21149 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
21150 }
21151
21152 push_kboard (FRAME_KBOARD (it.f));
21153 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21154 pop_kboard ();
21155
21156 if (no_props)
21157 {
21158 len = MODE_LINE_NOPROP_LEN (string_start);
21159 str = make_string (mode_line_noprop_buf + string_start, len);
21160 }
21161 else
21162 {
21163 mode_line_string_list = Fnreverse (mode_line_string_list);
21164 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21165 empty_unibyte_string);
21166 }
21167
21168 unbind_to (count, Qnil);
21169 return str;
21170 }
21171
21172 /* Write a null-terminated, right justified decimal representation of
21173 the positive integer D to BUF using a minimal field width WIDTH. */
21174
21175 static void
21176 pint2str (register char *buf, register int width, register ptrdiff_t d)
21177 {
21178 register char *p = buf;
21179
21180 if (d <= 0)
21181 *p++ = '0';
21182 else
21183 {
21184 while (d > 0)
21185 {
21186 *p++ = d % 10 + '0';
21187 d /= 10;
21188 }
21189 }
21190
21191 for (width -= (int) (p - buf); width > 0; --width)
21192 *p++ = ' ';
21193 *p-- = '\0';
21194 while (p > buf)
21195 {
21196 d = *buf;
21197 *buf++ = *p;
21198 *p-- = d;
21199 }
21200 }
21201
21202 /* Write a null-terminated, right justified decimal and "human
21203 readable" representation of the nonnegative integer D to BUF using
21204 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21205
21206 static const char power_letter[] =
21207 {
21208 0, /* no letter */
21209 'k', /* kilo */
21210 'M', /* mega */
21211 'G', /* giga */
21212 'T', /* tera */
21213 'P', /* peta */
21214 'E', /* exa */
21215 'Z', /* zetta */
21216 'Y' /* yotta */
21217 };
21218
21219 static void
21220 pint2hrstr (char *buf, int width, ptrdiff_t d)
21221 {
21222 /* We aim to represent the nonnegative integer D as
21223 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21224 ptrdiff_t quotient = d;
21225 int remainder = 0;
21226 /* -1 means: do not use TENTHS. */
21227 int tenths = -1;
21228 int exponent = 0;
21229
21230 /* Length of QUOTIENT.TENTHS as a string. */
21231 int length;
21232
21233 char * psuffix;
21234 char * p;
21235
21236 if (1000 <= quotient)
21237 {
21238 /* Scale to the appropriate EXPONENT. */
21239 do
21240 {
21241 remainder = quotient % 1000;
21242 quotient /= 1000;
21243 exponent++;
21244 }
21245 while (1000 <= quotient);
21246
21247 /* Round to nearest and decide whether to use TENTHS or not. */
21248 if (quotient <= 9)
21249 {
21250 tenths = remainder / 100;
21251 if (50 <= remainder % 100)
21252 {
21253 if (tenths < 9)
21254 tenths++;
21255 else
21256 {
21257 quotient++;
21258 if (quotient == 10)
21259 tenths = -1;
21260 else
21261 tenths = 0;
21262 }
21263 }
21264 }
21265 else
21266 if (500 <= remainder)
21267 {
21268 if (quotient < 999)
21269 quotient++;
21270 else
21271 {
21272 quotient = 1;
21273 exponent++;
21274 tenths = 0;
21275 }
21276 }
21277 }
21278
21279 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21280 if (tenths == -1 && quotient <= 99)
21281 if (quotient <= 9)
21282 length = 1;
21283 else
21284 length = 2;
21285 else
21286 length = 3;
21287 p = psuffix = buf + max (width, length);
21288
21289 /* Print EXPONENT. */
21290 *psuffix++ = power_letter[exponent];
21291 *psuffix = '\0';
21292
21293 /* Print TENTHS. */
21294 if (tenths >= 0)
21295 {
21296 *--p = '0' + tenths;
21297 *--p = '.';
21298 }
21299
21300 /* Print QUOTIENT. */
21301 do
21302 {
21303 int digit = quotient % 10;
21304 *--p = '0' + digit;
21305 }
21306 while ((quotient /= 10) != 0);
21307
21308 /* Print leading spaces. */
21309 while (buf < p)
21310 *--p = ' ';
21311 }
21312
21313 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21314 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21315 type of CODING_SYSTEM. Return updated pointer into BUF. */
21316
21317 static unsigned char invalid_eol_type[] = "(*invalid*)";
21318
21319 static char *
21320 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21321 {
21322 Lisp_Object val;
21323 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21324 const unsigned char *eol_str;
21325 int eol_str_len;
21326 /* The EOL conversion we are using. */
21327 Lisp_Object eoltype;
21328
21329 val = CODING_SYSTEM_SPEC (coding_system);
21330 eoltype = Qnil;
21331
21332 if (!VECTORP (val)) /* Not yet decided. */
21333 {
21334 *buf++ = multibyte ? '-' : ' ';
21335 if (eol_flag)
21336 eoltype = eol_mnemonic_undecided;
21337 /* Don't mention EOL conversion if it isn't decided. */
21338 }
21339 else
21340 {
21341 Lisp_Object attrs;
21342 Lisp_Object eolvalue;
21343
21344 attrs = AREF (val, 0);
21345 eolvalue = AREF (val, 2);
21346
21347 *buf++ = multibyte
21348 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21349 : ' ';
21350
21351 if (eol_flag)
21352 {
21353 /* The EOL conversion that is normal on this system. */
21354
21355 if (NILP (eolvalue)) /* Not yet decided. */
21356 eoltype = eol_mnemonic_undecided;
21357 else if (VECTORP (eolvalue)) /* Not yet decided. */
21358 eoltype = eol_mnemonic_undecided;
21359 else /* eolvalue is Qunix, Qdos, or Qmac. */
21360 eoltype = (EQ (eolvalue, Qunix)
21361 ? eol_mnemonic_unix
21362 : (EQ (eolvalue, Qdos) == 1
21363 ? eol_mnemonic_dos : eol_mnemonic_mac));
21364 }
21365 }
21366
21367 if (eol_flag)
21368 {
21369 /* Mention the EOL conversion if it is not the usual one. */
21370 if (STRINGP (eoltype))
21371 {
21372 eol_str = SDATA (eoltype);
21373 eol_str_len = SBYTES (eoltype);
21374 }
21375 else if (CHARACTERP (eoltype))
21376 {
21377 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21378 int c = XFASTINT (eoltype);
21379 eol_str_len = CHAR_STRING (c, tmp);
21380 eol_str = tmp;
21381 }
21382 else
21383 {
21384 eol_str = invalid_eol_type;
21385 eol_str_len = sizeof (invalid_eol_type) - 1;
21386 }
21387 memcpy (buf, eol_str, eol_str_len);
21388 buf += eol_str_len;
21389 }
21390
21391 return buf;
21392 }
21393
21394 /* Return a string for the output of a mode line %-spec for window W,
21395 generated by character C. FIELD_WIDTH > 0 means pad the string
21396 returned with spaces to that value. Return a Lisp string in
21397 *STRING if the resulting string is taken from that Lisp string.
21398
21399 Note we operate on the current buffer for most purposes,
21400 the exception being w->base_line_pos. */
21401
21402 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21403
21404 static const char *
21405 decode_mode_spec (struct window *w, register int c, int field_width,
21406 Lisp_Object *string)
21407 {
21408 Lisp_Object obj;
21409 struct frame *f = XFRAME (WINDOW_FRAME (w));
21410 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21411 /* We are going to use f->decode_mode_spec_buffer as the buffer to
21412 produce strings from numerical values, so limit preposterously
21413 large values of FIELD_WIDTH to avoid overrunning the buffer's
21414 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
21415 bytes plus the terminating null. */
21416 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
21417 struct buffer *b = current_buffer;
21418
21419 obj = Qnil;
21420 *string = Qnil;
21421
21422 switch (c)
21423 {
21424 case '*':
21425 if (!NILP (BVAR (b, read_only)))
21426 return "%";
21427 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21428 return "*";
21429 return "-";
21430
21431 case '+':
21432 /* This differs from %* only for a modified read-only buffer. */
21433 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21434 return "*";
21435 if (!NILP (BVAR (b, read_only)))
21436 return "%";
21437 return "-";
21438
21439 case '&':
21440 /* This differs from %* in ignoring read-only-ness. */
21441 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21442 return "*";
21443 return "-";
21444
21445 case '%':
21446 return "%";
21447
21448 case '[':
21449 {
21450 int i;
21451 char *p;
21452
21453 if (command_loop_level > 5)
21454 return "[[[... ";
21455 p = decode_mode_spec_buf;
21456 for (i = 0; i < command_loop_level; i++)
21457 *p++ = '[';
21458 *p = 0;
21459 return decode_mode_spec_buf;
21460 }
21461
21462 case ']':
21463 {
21464 int i;
21465 char *p;
21466
21467 if (command_loop_level > 5)
21468 return " ...]]]";
21469 p = decode_mode_spec_buf;
21470 for (i = 0; i < command_loop_level; i++)
21471 *p++ = ']';
21472 *p = 0;
21473 return decode_mode_spec_buf;
21474 }
21475
21476 case '-':
21477 {
21478 register int i;
21479
21480 /* Let lots_of_dashes be a string of infinite length. */
21481 if (mode_line_target == MODE_LINE_NOPROP ||
21482 mode_line_target == MODE_LINE_STRING)
21483 return "--";
21484 if (field_width <= 0
21485 || field_width > sizeof (lots_of_dashes))
21486 {
21487 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21488 decode_mode_spec_buf[i] = '-';
21489 decode_mode_spec_buf[i] = '\0';
21490 return decode_mode_spec_buf;
21491 }
21492 else
21493 return lots_of_dashes;
21494 }
21495
21496 case 'b':
21497 obj = BVAR (b, name);
21498 break;
21499
21500 case 'c':
21501 /* %c and %l are ignored in `frame-title-format'.
21502 (In redisplay_internal, the frame title is drawn _before_ the
21503 windows are updated, so the stuff which depends on actual
21504 window contents (such as %l) may fail to render properly, or
21505 even crash emacs.) */
21506 if (mode_line_target == MODE_LINE_TITLE)
21507 return "";
21508 else
21509 {
21510 ptrdiff_t col = current_column ();
21511 wset_column_number_displayed (w, make_number (col));
21512 pint2str (decode_mode_spec_buf, width, col);
21513 return decode_mode_spec_buf;
21514 }
21515
21516 case 'e':
21517 #ifndef SYSTEM_MALLOC
21518 {
21519 if (NILP (Vmemory_full))
21520 return "";
21521 else
21522 return "!MEM FULL! ";
21523 }
21524 #else
21525 return "";
21526 #endif
21527
21528 case 'F':
21529 /* %F displays the frame name. */
21530 if (!NILP (f->title))
21531 return SSDATA (f->title);
21532 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21533 return SSDATA (f->name);
21534 return "Emacs";
21535
21536 case 'f':
21537 obj = BVAR (b, filename);
21538 break;
21539
21540 case 'i':
21541 {
21542 ptrdiff_t size = ZV - BEGV;
21543 pint2str (decode_mode_spec_buf, width, size);
21544 return decode_mode_spec_buf;
21545 }
21546
21547 case 'I':
21548 {
21549 ptrdiff_t size = ZV - BEGV;
21550 pint2hrstr (decode_mode_spec_buf, width, size);
21551 return decode_mode_spec_buf;
21552 }
21553
21554 case 'l':
21555 {
21556 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21557 ptrdiff_t topline, nlines, height;
21558 ptrdiff_t junk;
21559
21560 /* %c and %l are ignored in `frame-title-format'. */
21561 if (mode_line_target == MODE_LINE_TITLE)
21562 return "";
21563
21564 startpos = XMARKER (w->start)->charpos;
21565 startpos_byte = marker_byte_position (w->start);
21566 height = WINDOW_TOTAL_LINES (w);
21567
21568 /* If we decided that this buffer isn't suitable for line numbers,
21569 don't forget that too fast. */
21570 if (EQ (w->base_line_pos, w->buffer))
21571 goto no_value;
21572 /* But do forget it, if the window shows a different buffer now. */
21573 else if (BUFFERP (w->base_line_pos))
21574 wset_base_line_pos (w, Qnil);
21575
21576 /* If the buffer is very big, don't waste time. */
21577 if (INTEGERP (Vline_number_display_limit)
21578 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21579 {
21580 wset_base_line_pos (w, Qnil);
21581 wset_base_line_number (w, Qnil);
21582 goto no_value;
21583 }
21584
21585 if (INTEGERP (w->base_line_number)
21586 && INTEGERP (w->base_line_pos)
21587 && XFASTINT (w->base_line_pos) <= startpos)
21588 {
21589 line = XFASTINT (w->base_line_number);
21590 linepos = XFASTINT (w->base_line_pos);
21591 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21592 }
21593 else
21594 {
21595 line = 1;
21596 linepos = BUF_BEGV (b);
21597 linepos_byte = BUF_BEGV_BYTE (b);
21598 }
21599
21600 /* Count lines from base line to window start position. */
21601 nlines = display_count_lines (linepos_byte,
21602 startpos_byte,
21603 startpos, &junk);
21604
21605 topline = nlines + line;
21606
21607 /* Determine a new base line, if the old one is too close
21608 or too far away, or if we did not have one.
21609 "Too close" means it's plausible a scroll-down would
21610 go back past it. */
21611 if (startpos == BUF_BEGV (b))
21612 {
21613 wset_base_line_number (w, make_number (topline));
21614 wset_base_line_pos (w, make_number (BUF_BEGV (b)));
21615 }
21616 else if (nlines < height + 25 || nlines > height * 3 + 50
21617 || linepos == BUF_BEGV (b))
21618 {
21619 ptrdiff_t limit = BUF_BEGV (b);
21620 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21621 ptrdiff_t position;
21622 ptrdiff_t distance =
21623 (height * 2 + 30) * line_number_display_limit_width;
21624
21625 if (startpos - distance > limit)
21626 {
21627 limit = startpos - distance;
21628 limit_byte = CHAR_TO_BYTE (limit);
21629 }
21630
21631 nlines = display_count_lines (startpos_byte,
21632 limit_byte,
21633 - (height * 2 + 30),
21634 &position);
21635 /* If we couldn't find the lines we wanted within
21636 line_number_display_limit_width chars per line,
21637 give up on line numbers for this window. */
21638 if (position == limit_byte && limit == startpos - distance)
21639 {
21640 wset_base_line_pos (w, w->buffer);
21641 wset_base_line_number (w, Qnil);
21642 goto no_value;
21643 }
21644
21645 wset_base_line_number (w, make_number (topline - nlines));
21646 wset_base_line_pos (w, make_number (BYTE_TO_CHAR (position)));
21647 }
21648
21649 /* Now count lines from the start pos to point. */
21650 nlines = display_count_lines (startpos_byte,
21651 PT_BYTE, PT, &junk);
21652
21653 /* Record that we did display the line number. */
21654 line_number_displayed = 1;
21655
21656 /* Make the string to show. */
21657 pint2str (decode_mode_spec_buf, width, topline + nlines);
21658 return decode_mode_spec_buf;
21659 no_value:
21660 {
21661 char* p = decode_mode_spec_buf;
21662 int pad = width - 2;
21663 while (pad-- > 0)
21664 *p++ = ' ';
21665 *p++ = '?';
21666 *p++ = '?';
21667 *p = '\0';
21668 return decode_mode_spec_buf;
21669 }
21670 }
21671 break;
21672
21673 case 'm':
21674 obj = BVAR (b, mode_name);
21675 break;
21676
21677 case 'n':
21678 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21679 return " Narrow";
21680 break;
21681
21682 case 'p':
21683 {
21684 ptrdiff_t pos = marker_position (w->start);
21685 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21686
21687 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
21688 {
21689 if (pos <= BUF_BEGV (b))
21690 return "All";
21691 else
21692 return "Bottom";
21693 }
21694 else if (pos <= BUF_BEGV (b))
21695 return "Top";
21696 else
21697 {
21698 if (total > 1000000)
21699 /* Do it differently for a large value, to avoid overflow. */
21700 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21701 else
21702 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21703 /* We can't normally display a 3-digit number,
21704 so get us a 2-digit number that is close. */
21705 if (total == 100)
21706 total = 99;
21707 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21708 return decode_mode_spec_buf;
21709 }
21710 }
21711
21712 /* Display percentage of size above the bottom of the screen. */
21713 case 'P':
21714 {
21715 ptrdiff_t toppos = marker_position (w->start);
21716 ptrdiff_t botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
21717 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21718
21719 if (botpos >= BUF_ZV (b))
21720 {
21721 if (toppos <= BUF_BEGV (b))
21722 return "All";
21723 else
21724 return "Bottom";
21725 }
21726 else
21727 {
21728 if (total > 1000000)
21729 /* Do it differently for a large value, to avoid overflow. */
21730 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21731 else
21732 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
21733 /* We can't normally display a 3-digit number,
21734 so get us a 2-digit number that is close. */
21735 if (total == 100)
21736 total = 99;
21737 if (toppos <= BUF_BEGV (b))
21738 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
21739 else
21740 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21741 return decode_mode_spec_buf;
21742 }
21743 }
21744
21745 case 's':
21746 /* status of process */
21747 obj = Fget_buffer_process (Fcurrent_buffer ());
21748 if (NILP (obj))
21749 return "no process";
21750 #ifndef MSDOS
21751 obj = Fsymbol_name (Fprocess_status (obj));
21752 #endif
21753 break;
21754
21755 case '@':
21756 {
21757 ptrdiff_t count = inhibit_garbage_collection ();
21758 Lisp_Object val = call1 (intern ("file-remote-p"),
21759 BVAR (current_buffer, directory));
21760 unbind_to (count, Qnil);
21761
21762 if (NILP (val))
21763 return "-";
21764 else
21765 return "@";
21766 }
21767
21768 case 't': /* indicate TEXT or BINARY */
21769 return "T";
21770
21771 case 'z':
21772 /* coding-system (not including end-of-line format) */
21773 case 'Z':
21774 /* coding-system (including end-of-line type) */
21775 {
21776 int eol_flag = (c == 'Z');
21777 char *p = decode_mode_spec_buf;
21778
21779 if (! FRAME_WINDOW_P (f))
21780 {
21781 /* No need to mention EOL here--the terminal never needs
21782 to do EOL conversion. */
21783 p = decode_mode_spec_coding (CODING_ID_NAME
21784 (FRAME_KEYBOARD_CODING (f)->id),
21785 p, 0);
21786 p = decode_mode_spec_coding (CODING_ID_NAME
21787 (FRAME_TERMINAL_CODING (f)->id),
21788 p, 0);
21789 }
21790 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
21791 p, eol_flag);
21792
21793 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
21794 #ifdef subprocesses
21795 obj = Fget_buffer_process (Fcurrent_buffer ());
21796 if (PROCESSP (obj))
21797 {
21798 p = decode_mode_spec_coding
21799 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
21800 p = decode_mode_spec_coding
21801 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
21802 }
21803 #endif /* subprocesses */
21804 #endif /* 0 */
21805 *p = 0;
21806 return decode_mode_spec_buf;
21807 }
21808 }
21809
21810 if (STRINGP (obj))
21811 {
21812 *string = obj;
21813 return SSDATA (obj);
21814 }
21815 else
21816 return "";
21817 }
21818
21819
21820 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
21821 means count lines back from START_BYTE. But don't go beyond
21822 LIMIT_BYTE. Return the number of lines thus found (always
21823 nonnegative).
21824
21825 Set *BYTE_POS_PTR to the byte position where we stopped. This is
21826 either the position COUNT lines after/before START_BYTE, if we
21827 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
21828 COUNT lines. */
21829
21830 static ptrdiff_t
21831 display_count_lines (ptrdiff_t start_byte,
21832 ptrdiff_t limit_byte, ptrdiff_t count,
21833 ptrdiff_t *byte_pos_ptr)
21834 {
21835 register unsigned char *cursor;
21836 unsigned char *base;
21837
21838 register ptrdiff_t ceiling;
21839 register unsigned char *ceiling_addr;
21840 ptrdiff_t orig_count = count;
21841
21842 /* If we are not in selective display mode,
21843 check only for newlines. */
21844 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
21845 && !INTEGERP (BVAR (current_buffer, selective_display)));
21846
21847 if (count > 0)
21848 {
21849 while (start_byte < limit_byte)
21850 {
21851 ceiling = BUFFER_CEILING_OF (start_byte);
21852 ceiling = min (limit_byte - 1, ceiling);
21853 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
21854 base = (cursor = BYTE_POS_ADDR (start_byte));
21855 while (1)
21856 {
21857 if (selective_display)
21858 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
21859 ;
21860 else
21861 while (*cursor != '\n' && ++cursor != ceiling_addr)
21862 ;
21863
21864 if (cursor != ceiling_addr)
21865 {
21866 if (--count == 0)
21867 {
21868 start_byte += cursor - base + 1;
21869 *byte_pos_ptr = start_byte;
21870 return orig_count;
21871 }
21872 else
21873 if (++cursor == ceiling_addr)
21874 break;
21875 }
21876 else
21877 break;
21878 }
21879 start_byte += cursor - base;
21880 }
21881 }
21882 else
21883 {
21884 while (start_byte > limit_byte)
21885 {
21886 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
21887 ceiling = max (limit_byte, ceiling);
21888 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
21889 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
21890 while (1)
21891 {
21892 if (selective_display)
21893 while (--cursor != ceiling_addr
21894 && *cursor != '\n' && *cursor != 015)
21895 ;
21896 else
21897 while (--cursor != ceiling_addr && *cursor != '\n')
21898 ;
21899
21900 if (cursor != ceiling_addr)
21901 {
21902 if (++count == 0)
21903 {
21904 start_byte += cursor - base + 1;
21905 *byte_pos_ptr = start_byte;
21906 /* When scanning backwards, we should
21907 not count the newline posterior to which we stop. */
21908 return - orig_count - 1;
21909 }
21910 }
21911 else
21912 break;
21913 }
21914 /* Here we add 1 to compensate for the last decrement
21915 of CURSOR, which took it past the valid range. */
21916 start_byte += cursor - base + 1;
21917 }
21918 }
21919
21920 *byte_pos_ptr = limit_byte;
21921
21922 if (count < 0)
21923 return - orig_count + count;
21924 return orig_count - count;
21925
21926 }
21927
21928
21929 \f
21930 /***********************************************************************
21931 Displaying strings
21932 ***********************************************************************/
21933
21934 /* Display a NUL-terminated string, starting with index START.
21935
21936 If STRING is non-null, display that C string. Otherwise, the Lisp
21937 string LISP_STRING is displayed. There's a case that STRING is
21938 non-null and LISP_STRING is not nil. It means STRING is a string
21939 data of LISP_STRING. In that case, we display LISP_STRING while
21940 ignoring its text properties.
21941
21942 If FACE_STRING is not nil, FACE_STRING_POS is a position in
21943 FACE_STRING. Display STRING or LISP_STRING with the face at
21944 FACE_STRING_POS in FACE_STRING:
21945
21946 Display the string in the environment given by IT, but use the
21947 standard display table, temporarily.
21948
21949 FIELD_WIDTH is the minimum number of output glyphs to produce.
21950 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21951 with spaces. If STRING has more characters, more than FIELD_WIDTH
21952 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
21953
21954 PRECISION is the maximum number of characters to output from
21955 STRING. PRECISION < 0 means don't truncate the string.
21956
21957 This is roughly equivalent to printf format specifiers:
21958
21959 FIELD_WIDTH PRECISION PRINTF
21960 ----------------------------------------
21961 -1 -1 %s
21962 -1 10 %.10s
21963 10 -1 %10s
21964 20 10 %20.10s
21965
21966 MULTIBYTE zero means do not display multibyte chars, > 0 means do
21967 display them, and < 0 means obey the current buffer's value of
21968 enable_multibyte_characters.
21969
21970 Value is the number of columns displayed. */
21971
21972 static int
21973 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
21974 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
21975 int field_width, int precision, int max_x, int multibyte)
21976 {
21977 int hpos_at_start = it->hpos;
21978 int saved_face_id = it->face_id;
21979 struct glyph_row *row = it->glyph_row;
21980 ptrdiff_t it_charpos;
21981
21982 /* Initialize the iterator IT for iteration over STRING beginning
21983 with index START. */
21984 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
21985 precision, field_width, multibyte);
21986 if (string && STRINGP (lisp_string))
21987 /* LISP_STRING is the one returned by decode_mode_spec. We should
21988 ignore its text properties. */
21989 it->stop_charpos = it->end_charpos;
21990
21991 /* If displaying STRING, set up the face of the iterator from
21992 FACE_STRING, if that's given. */
21993 if (STRINGP (face_string))
21994 {
21995 ptrdiff_t endptr;
21996 struct face *face;
21997
21998 it->face_id
21999 = face_at_string_position (it->w, face_string, face_string_pos,
22000 0, it->region_beg_charpos,
22001 it->region_end_charpos,
22002 &endptr, it->base_face_id, 0);
22003 face = FACE_FROM_ID (it->f, it->face_id);
22004 it->face_box_p = face->box != FACE_NO_BOX;
22005 }
22006
22007 /* Set max_x to the maximum allowed X position. Don't let it go
22008 beyond the right edge of the window. */
22009 if (max_x <= 0)
22010 max_x = it->last_visible_x;
22011 else
22012 max_x = min (max_x, it->last_visible_x);
22013
22014 /* Skip over display elements that are not visible. because IT->w is
22015 hscrolled. */
22016 if (it->current_x < it->first_visible_x)
22017 move_it_in_display_line_to (it, 100000, it->first_visible_x,
22018 MOVE_TO_POS | MOVE_TO_X);
22019
22020 row->ascent = it->max_ascent;
22021 row->height = it->max_ascent + it->max_descent;
22022 row->phys_ascent = it->max_phys_ascent;
22023 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
22024 row->extra_line_spacing = it->max_extra_line_spacing;
22025
22026 if (STRINGP (it->string))
22027 it_charpos = IT_STRING_CHARPOS (*it);
22028 else
22029 it_charpos = IT_CHARPOS (*it);
22030
22031 /* This condition is for the case that we are called with current_x
22032 past last_visible_x. */
22033 while (it->current_x < max_x)
22034 {
22035 int x_before, x, n_glyphs_before, i, nglyphs;
22036
22037 /* Get the next display element. */
22038 if (!get_next_display_element (it))
22039 break;
22040
22041 /* Produce glyphs. */
22042 x_before = it->current_x;
22043 n_glyphs_before = row->used[TEXT_AREA];
22044 PRODUCE_GLYPHS (it);
22045
22046 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
22047 i = 0;
22048 x = x_before;
22049 while (i < nglyphs)
22050 {
22051 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
22052
22053 if (it->line_wrap != TRUNCATE
22054 && x + glyph->pixel_width > max_x)
22055 {
22056 /* End of continued line or max_x reached. */
22057 if (CHAR_GLYPH_PADDING_P (*glyph))
22058 {
22059 /* A wide character is unbreakable. */
22060 if (row->reversed_p)
22061 unproduce_glyphs (it, row->used[TEXT_AREA]
22062 - n_glyphs_before);
22063 row->used[TEXT_AREA] = n_glyphs_before;
22064 it->current_x = x_before;
22065 }
22066 else
22067 {
22068 if (row->reversed_p)
22069 unproduce_glyphs (it, row->used[TEXT_AREA]
22070 - (n_glyphs_before + i));
22071 row->used[TEXT_AREA] = n_glyphs_before + i;
22072 it->current_x = x;
22073 }
22074 break;
22075 }
22076 else if (x + glyph->pixel_width >= it->first_visible_x)
22077 {
22078 /* Glyph is at least partially visible. */
22079 ++it->hpos;
22080 if (x < it->first_visible_x)
22081 row->x = x - it->first_visible_x;
22082 }
22083 else
22084 {
22085 /* Glyph is off the left margin of the display area.
22086 Should not happen. */
22087 emacs_abort ();
22088 }
22089
22090 row->ascent = max (row->ascent, it->max_ascent);
22091 row->height = max (row->height, it->max_ascent + it->max_descent);
22092 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
22093 row->phys_height = max (row->phys_height,
22094 it->max_phys_ascent + it->max_phys_descent);
22095 row->extra_line_spacing = max (row->extra_line_spacing,
22096 it->max_extra_line_spacing);
22097 x += glyph->pixel_width;
22098 ++i;
22099 }
22100
22101 /* Stop if max_x reached. */
22102 if (i < nglyphs)
22103 break;
22104
22105 /* Stop at line ends. */
22106 if (ITERATOR_AT_END_OF_LINE_P (it))
22107 {
22108 it->continuation_lines_width = 0;
22109 break;
22110 }
22111
22112 set_iterator_to_next (it, 1);
22113 if (STRINGP (it->string))
22114 it_charpos = IT_STRING_CHARPOS (*it);
22115 else
22116 it_charpos = IT_CHARPOS (*it);
22117
22118 /* Stop if truncating at the right edge. */
22119 if (it->line_wrap == TRUNCATE
22120 && it->current_x >= it->last_visible_x)
22121 {
22122 /* Add truncation mark, but don't do it if the line is
22123 truncated at a padding space. */
22124 if (it_charpos < it->string_nchars)
22125 {
22126 if (!FRAME_WINDOW_P (it->f))
22127 {
22128 int ii, n;
22129
22130 if (it->current_x > it->last_visible_x)
22131 {
22132 if (!row->reversed_p)
22133 {
22134 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22135 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22136 break;
22137 }
22138 else
22139 {
22140 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22141 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22142 break;
22143 unproduce_glyphs (it, ii + 1);
22144 ii = row->used[TEXT_AREA] - (ii + 1);
22145 }
22146 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22147 {
22148 row->used[TEXT_AREA] = ii;
22149 produce_special_glyphs (it, IT_TRUNCATION);
22150 }
22151 }
22152 produce_special_glyphs (it, IT_TRUNCATION);
22153 }
22154 row->truncated_on_right_p = 1;
22155 }
22156 break;
22157 }
22158 }
22159
22160 /* Maybe insert a truncation at the left. */
22161 if (it->first_visible_x
22162 && it_charpos > 0)
22163 {
22164 if (!FRAME_WINDOW_P (it->f)
22165 || (row->reversed_p
22166 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22167 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22168 insert_left_trunc_glyphs (it);
22169 row->truncated_on_left_p = 1;
22170 }
22171
22172 it->face_id = saved_face_id;
22173
22174 /* Value is number of columns displayed. */
22175 return it->hpos - hpos_at_start;
22176 }
22177
22178
22179 \f
22180 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22181 appears as an element of LIST or as the car of an element of LIST.
22182 If PROPVAL is a list, compare each element against LIST in that
22183 way, and return 1/2 if any element of PROPVAL is found in LIST.
22184 Otherwise return 0. This function cannot quit.
22185 The return value is 2 if the text is invisible but with an ellipsis
22186 and 1 if it's invisible and without an ellipsis. */
22187
22188 int
22189 invisible_p (register Lisp_Object propval, Lisp_Object list)
22190 {
22191 register Lisp_Object tail, proptail;
22192
22193 for (tail = list; CONSP (tail); tail = XCDR (tail))
22194 {
22195 register Lisp_Object tem;
22196 tem = XCAR (tail);
22197 if (EQ (propval, tem))
22198 return 1;
22199 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22200 return NILP (XCDR (tem)) ? 1 : 2;
22201 }
22202
22203 if (CONSP (propval))
22204 {
22205 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22206 {
22207 Lisp_Object propelt;
22208 propelt = XCAR (proptail);
22209 for (tail = list; CONSP (tail); tail = XCDR (tail))
22210 {
22211 register Lisp_Object tem;
22212 tem = XCAR (tail);
22213 if (EQ (propelt, tem))
22214 return 1;
22215 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22216 return NILP (XCDR (tem)) ? 1 : 2;
22217 }
22218 }
22219 }
22220
22221 return 0;
22222 }
22223
22224 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22225 doc: /* Non-nil if the property makes the text invisible.
22226 POS-OR-PROP can be a marker or number, in which case it is taken to be
22227 a position in the current buffer and the value of the `invisible' property
22228 is checked; or it can be some other value, which is then presumed to be the
22229 value of the `invisible' property of the text of interest.
22230 The non-nil value returned can be t for truly invisible text or something
22231 else if the text is replaced by an ellipsis. */)
22232 (Lisp_Object pos_or_prop)
22233 {
22234 Lisp_Object prop
22235 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22236 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22237 : pos_or_prop);
22238 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22239 return (invis == 0 ? Qnil
22240 : invis == 1 ? Qt
22241 : make_number (invis));
22242 }
22243
22244 /* Calculate a width or height in pixels from a specification using
22245 the following elements:
22246
22247 SPEC ::=
22248 NUM - a (fractional) multiple of the default font width/height
22249 (NUM) - specifies exactly NUM pixels
22250 UNIT - a fixed number of pixels, see below.
22251 ELEMENT - size of a display element in pixels, see below.
22252 (NUM . SPEC) - equals NUM * SPEC
22253 (+ SPEC SPEC ...) - add pixel values
22254 (- SPEC SPEC ...) - subtract pixel values
22255 (- SPEC) - negate pixel value
22256
22257 NUM ::=
22258 INT or FLOAT - a number constant
22259 SYMBOL - use symbol's (buffer local) variable binding.
22260
22261 UNIT ::=
22262 in - pixels per inch *)
22263 mm - pixels per 1/1000 meter *)
22264 cm - pixels per 1/100 meter *)
22265 width - width of current font in pixels.
22266 height - height of current font in pixels.
22267
22268 *) using the ratio(s) defined in display-pixels-per-inch.
22269
22270 ELEMENT ::=
22271
22272 left-fringe - left fringe width in pixels
22273 right-fringe - right fringe width in pixels
22274
22275 left-margin - left margin width in pixels
22276 right-margin - right margin width in pixels
22277
22278 scroll-bar - scroll-bar area width in pixels
22279
22280 Examples:
22281
22282 Pixels corresponding to 5 inches:
22283 (5 . in)
22284
22285 Total width of non-text areas on left side of window (if scroll-bar is on left):
22286 '(space :width (+ left-fringe left-margin scroll-bar))
22287
22288 Align to first text column (in header line):
22289 '(space :align-to 0)
22290
22291 Align to middle of text area minus half the width of variable `my-image'
22292 containing a loaded image:
22293 '(space :align-to (0.5 . (- text my-image)))
22294
22295 Width of left margin minus width of 1 character in the default font:
22296 '(space :width (- left-margin 1))
22297
22298 Width of left margin minus width of 2 characters in the current font:
22299 '(space :width (- left-margin (2 . width)))
22300
22301 Center 1 character over left-margin (in header line):
22302 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22303
22304 Different ways to express width of left fringe plus left margin minus one pixel:
22305 '(space :width (- (+ left-fringe left-margin) (1)))
22306 '(space :width (+ left-fringe left-margin (- (1))))
22307 '(space :width (+ left-fringe left-margin (-1)))
22308
22309 */
22310
22311 #define NUMVAL(X) \
22312 ((INTEGERP (X) || FLOATP (X)) \
22313 ? XFLOATINT (X) \
22314 : - 1)
22315
22316 static int
22317 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22318 struct font *font, int width_p, int *align_to)
22319 {
22320 double pixels;
22321
22322 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22323 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22324
22325 if (NILP (prop))
22326 return OK_PIXELS (0);
22327
22328 eassert (FRAME_LIVE_P (it->f));
22329
22330 if (SYMBOLP (prop))
22331 {
22332 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22333 {
22334 char *unit = SSDATA (SYMBOL_NAME (prop));
22335
22336 if (unit[0] == 'i' && unit[1] == 'n')
22337 pixels = 1.0;
22338 else if (unit[0] == 'm' && unit[1] == 'm')
22339 pixels = 25.4;
22340 else if (unit[0] == 'c' && unit[1] == 'm')
22341 pixels = 2.54;
22342 else
22343 pixels = 0;
22344 if (pixels > 0)
22345 {
22346 double ppi;
22347 #ifdef HAVE_WINDOW_SYSTEM
22348 if (FRAME_WINDOW_P (it->f)
22349 && (ppi = (width_p
22350 ? FRAME_X_DISPLAY_INFO (it->f)->resx
22351 : FRAME_X_DISPLAY_INFO (it->f)->resy),
22352 ppi > 0))
22353 return OK_PIXELS (ppi / pixels);
22354 #endif
22355
22356 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
22357 || (CONSP (Vdisplay_pixels_per_inch)
22358 && (ppi = (width_p
22359 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
22360 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
22361 ppi > 0)))
22362 return OK_PIXELS (ppi / pixels);
22363
22364 return 0;
22365 }
22366 }
22367
22368 #ifdef HAVE_WINDOW_SYSTEM
22369 if (EQ (prop, Qheight))
22370 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22371 if (EQ (prop, Qwidth))
22372 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22373 #else
22374 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22375 return OK_PIXELS (1);
22376 #endif
22377
22378 if (EQ (prop, Qtext))
22379 return OK_PIXELS (width_p
22380 ? window_box_width (it->w, TEXT_AREA)
22381 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22382
22383 if (align_to && *align_to < 0)
22384 {
22385 *res = 0;
22386 if (EQ (prop, Qleft))
22387 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22388 if (EQ (prop, Qright))
22389 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22390 if (EQ (prop, Qcenter))
22391 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22392 + window_box_width (it->w, TEXT_AREA) / 2);
22393 if (EQ (prop, Qleft_fringe))
22394 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22395 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22396 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22397 if (EQ (prop, Qright_fringe))
22398 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22399 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22400 : window_box_right_offset (it->w, TEXT_AREA));
22401 if (EQ (prop, Qleft_margin))
22402 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22403 if (EQ (prop, Qright_margin))
22404 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22405 if (EQ (prop, Qscroll_bar))
22406 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22407 ? 0
22408 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22409 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22410 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22411 : 0)));
22412 }
22413 else
22414 {
22415 if (EQ (prop, Qleft_fringe))
22416 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22417 if (EQ (prop, Qright_fringe))
22418 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22419 if (EQ (prop, Qleft_margin))
22420 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22421 if (EQ (prop, Qright_margin))
22422 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22423 if (EQ (prop, Qscroll_bar))
22424 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22425 }
22426
22427 prop = buffer_local_value_1 (prop, it->w->buffer);
22428 if (EQ (prop, Qunbound))
22429 prop = Qnil;
22430 }
22431
22432 if (INTEGERP (prop) || FLOATP (prop))
22433 {
22434 int base_unit = (width_p
22435 ? FRAME_COLUMN_WIDTH (it->f)
22436 : FRAME_LINE_HEIGHT (it->f));
22437 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22438 }
22439
22440 if (CONSP (prop))
22441 {
22442 Lisp_Object car = XCAR (prop);
22443 Lisp_Object cdr = XCDR (prop);
22444
22445 if (SYMBOLP (car))
22446 {
22447 #ifdef HAVE_WINDOW_SYSTEM
22448 if (FRAME_WINDOW_P (it->f)
22449 && valid_image_p (prop))
22450 {
22451 ptrdiff_t id = lookup_image (it->f, prop);
22452 struct image *img = IMAGE_FROM_ID (it->f, id);
22453
22454 return OK_PIXELS (width_p ? img->width : img->height);
22455 }
22456 #endif
22457 if (EQ (car, Qplus) || EQ (car, Qminus))
22458 {
22459 int first = 1;
22460 double px;
22461
22462 pixels = 0;
22463 while (CONSP (cdr))
22464 {
22465 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22466 font, width_p, align_to))
22467 return 0;
22468 if (first)
22469 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22470 else
22471 pixels += px;
22472 cdr = XCDR (cdr);
22473 }
22474 if (EQ (car, Qminus))
22475 pixels = -pixels;
22476 return OK_PIXELS (pixels);
22477 }
22478
22479 car = buffer_local_value_1 (car, it->w->buffer);
22480 if (EQ (car, Qunbound))
22481 car = Qnil;
22482 }
22483
22484 if (INTEGERP (car) || FLOATP (car))
22485 {
22486 double fact;
22487 pixels = XFLOATINT (car);
22488 if (NILP (cdr))
22489 return OK_PIXELS (pixels);
22490 if (calc_pixel_width_or_height (&fact, it, cdr,
22491 font, width_p, align_to))
22492 return OK_PIXELS (pixels * fact);
22493 return 0;
22494 }
22495
22496 return 0;
22497 }
22498
22499 return 0;
22500 }
22501
22502 \f
22503 /***********************************************************************
22504 Glyph Display
22505 ***********************************************************************/
22506
22507 #ifdef HAVE_WINDOW_SYSTEM
22508
22509 #ifdef GLYPH_DEBUG
22510
22511 void
22512 dump_glyph_string (struct glyph_string *s)
22513 {
22514 fprintf (stderr, "glyph string\n");
22515 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22516 s->x, s->y, s->width, s->height);
22517 fprintf (stderr, " ybase = %d\n", s->ybase);
22518 fprintf (stderr, " hl = %d\n", s->hl);
22519 fprintf (stderr, " left overhang = %d, right = %d\n",
22520 s->left_overhang, s->right_overhang);
22521 fprintf (stderr, " nchars = %d\n", s->nchars);
22522 fprintf (stderr, " extends to end of line = %d\n",
22523 s->extends_to_end_of_line_p);
22524 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22525 fprintf (stderr, " bg width = %d\n", s->background_width);
22526 }
22527
22528 #endif /* GLYPH_DEBUG */
22529
22530 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22531 of XChar2b structures for S; it can't be allocated in
22532 init_glyph_string because it must be allocated via `alloca'. W
22533 is the window on which S is drawn. ROW and AREA are the glyph row
22534 and area within the row from which S is constructed. START is the
22535 index of the first glyph structure covered by S. HL is a
22536 face-override for drawing S. */
22537
22538 #ifdef HAVE_NTGUI
22539 #define OPTIONAL_HDC(hdc) HDC hdc,
22540 #define DECLARE_HDC(hdc) HDC hdc;
22541 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22542 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22543 #endif
22544
22545 #ifndef OPTIONAL_HDC
22546 #define OPTIONAL_HDC(hdc)
22547 #define DECLARE_HDC(hdc)
22548 #define ALLOCATE_HDC(hdc, f)
22549 #define RELEASE_HDC(hdc, f)
22550 #endif
22551
22552 static void
22553 init_glyph_string (struct glyph_string *s,
22554 OPTIONAL_HDC (hdc)
22555 XChar2b *char2b, struct window *w, struct glyph_row *row,
22556 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22557 {
22558 memset (s, 0, sizeof *s);
22559 s->w = w;
22560 s->f = XFRAME (w->frame);
22561 #ifdef HAVE_NTGUI
22562 s->hdc = hdc;
22563 #endif
22564 s->display = FRAME_X_DISPLAY (s->f);
22565 s->window = FRAME_X_WINDOW (s->f);
22566 s->char2b = char2b;
22567 s->hl = hl;
22568 s->row = row;
22569 s->area = area;
22570 s->first_glyph = row->glyphs[area] + start;
22571 s->height = row->height;
22572 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22573 s->ybase = s->y + row->ascent;
22574 }
22575
22576
22577 /* Append the list of glyph strings with head H and tail T to the list
22578 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22579
22580 static void
22581 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22582 struct glyph_string *h, struct glyph_string *t)
22583 {
22584 if (h)
22585 {
22586 if (*head)
22587 (*tail)->next = h;
22588 else
22589 *head = h;
22590 h->prev = *tail;
22591 *tail = t;
22592 }
22593 }
22594
22595
22596 /* Prepend the list of glyph strings with head H and tail T to the
22597 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22598 result. */
22599
22600 static void
22601 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22602 struct glyph_string *h, struct glyph_string *t)
22603 {
22604 if (h)
22605 {
22606 if (*head)
22607 (*head)->prev = t;
22608 else
22609 *tail = t;
22610 t->next = *head;
22611 *head = h;
22612 }
22613 }
22614
22615
22616 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22617 Set *HEAD and *TAIL to the resulting list. */
22618
22619 static void
22620 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22621 struct glyph_string *s)
22622 {
22623 s->next = s->prev = NULL;
22624 append_glyph_string_lists (head, tail, s, s);
22625 }
22626
22627
22628 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22629 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22630 make sure that X resources for the face returned are allocated.
22631 Value is a pointer to a realized face that is ready for display if
22632 DISPLAY_P is non-zero. */
22633
22634 static struct face *
22635 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22636 XChar2b *char2b, int display_p)
22637 {
22638 struct face *face = FACE_FROM_ID (f, face_id);
22639
22640 if (face->font)
22641 {
22642 unsigned code = face->font->driver->encode_char (face->font, c);
22643
22644 if (code != FONT_INVALID_CODE)
22645 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22646 else
22647 STORE_XCHAR2B (char2b, 0, 0);
22648 }
22649
22650 /* Make sure X resources of the face are allocated. */
22651 #ifdef HAVE_X_WINDOWS
22652 if (display_p)
22653 #endif
22654 {
22655 eassert (face != NULL);
22656 PREPARE_FACE_FOR_DISPLAY (f, face);
22657 }
22658
22659 return face;
22660 }
22661
22662
22663 /* Get face and two-byte form of character glyph GLYPH on frame F.
22664 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22665 a pointer to a realized face that is ready for display. */
22666
22667 static struct face *
22668 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22669 XChar2b *char2b, int *two_byte_p)
22670 {
22671 struct face *face;
22672
22673 eassert (glyph->type == CHAR_GLYPH);
22674 face = FACE_FROM_ID (f, glyph->face_id);
22675
22676 if (two_byte_p)
22677 *two_byte_p = 0;
22678
22679 if (face->font)
22680 {
22681 unsigned code;
22682
22683 if (CHAR_BYTE8_P (glyph->u.ch))
22684 code = CHAR_TO_BYTE8 (glyph->u.ch);
22685 else
22686 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22687
22688 if (code != FONT_INVALID_CODE)
22689 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22690 else
22691 STORE_XCHAR2B (char2b, 0, 0);
22692 }
22693
22694 /* Make sure X resources of the face are allocated. */
22695 eassert (face != NULL);
22696 PREPARE_FACE_FOR_DISPLAY (f, face);
22697 return face;
22698 }
22699
22700
22701 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22702 Return 1 if FONT has a glyph for C, otherwise return 0. */
22703
22704 static int
22705 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22706 {
22707 unsigned code;
22708
22709 if (CHAR_BYTE8_P (c))
22710 code = CHAR_TO_BYTE8 (c);
22711 else
22712 code = font->driver->encode_char (font, c);
22713
22714 if (code == FONT_INVALID_CODE)
22715 return 0;
22716 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22717 return 1;
22718 }
22719
22720
22721 /* Fill glyph string S with composition components specified by S->cmp.
22722
22723 BASE_FACE is the base face of the composition.
22724 S->cmp_from is the index of the first component for S.
22725
22726 OVERLAPS non-zero means S should draw the foreground only, and use
22727 its physical height for clipping. See also draw_glyphs.
22728
22729 Value is the index of a component not in S. */
22730
22731 static int
22732 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
22733 int overlaps)
22734 {
22735 int i;
22736 /* For all glyphs of this composition, starting at the offset
22737 S->cmp_from, until we reach the end of the definition or encounter a
22738 glyph that requires the different face, add it to S. */
22739 struct face *face;
22740
22741 eassert (s);
22742
22743 s->for_overlaps = overlaps;
22744 s->face = NULL;
22745 s->font = NULL;
22746 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
22747 {
22748 int c = COMPOSITION_GLYPH (s->cmp, i);
22749
22750 /* TAB in a composition means display glyphs with padding space
22751 on the left or right. */
22752 if (c != '\t')
22753 {
22754 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
22755 -1, Qnil);
22756
22757 face = get_char_face_and_encoding (s->f, c, face_id,
22758 s->char2b + i, 1);
22759 if (face)
22760 {
22761 if (! s->face)
22762 {
22763 s->face = face;
22764 s->font = s->face->font;
22765 }
22766 else if (s->face != face)
22767 break;
22768 }
22769 }
22770 ++s->nchars;
22771 }
22772 s->cmp_to = i;
22773
22774 if (s->face == NULL)
22775 {
22776 s->face = base_face->ascii_face;
22777 s->font = s->face->font;
22778 }
22779
22780 /* All glyph strings for the same composition has the same width,
22781 i.e. the width set for the first component of the composition. */
22782 s->width = s->first_glyph->pixel_width;
22783
22784 /* If the specified font could not be loaded, use the frame's
22785 default font, but record the fact that we couldn't load it in
22786 the glyph string so that we can draw rectangles for the
22787 characters of the glyph string. */
22788 if (s->font == NULL)
22789 {
22790 s->font_not_found_p = 1;
22791 s->font = FRAME_FONT (s->f);
22792 }
22793
22794 /* Adjust base line for subscript/superscript text. */
22795 s->ybase += s->first_glyph->voffset;
22796
22797 /* This glyph string must always be drawn with 16-bit functions. */
22798 s->two_byte_p = 1;
22799
22800 return s->cmp_to;
22801 }
22802
22803 static int
22804 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
22805 int start, int end, int overlaps)
22806 {
22807 struct glyph *glyph, *last;
22808 Lisp_Object lgstring;
22809 int i;
22810
22811 s->for_overlaps = overlaps;
22812 glyph = s->row->glyphs[s->area] + start;
22813 last = s->row->glyphs[s->area] + end;
22814 s->cmp_id = glyph->u.cmp.id;
22815 s->cmp_from = glyph->slice.cmp.from;
22816 s->cmp_to = glyph->slice.cmp.to + 1;
22817 s->face = FACE_FROM_ID (s->f, face_id);
22818 lgstring = composition_gstring_from_id (s->cmp_id);
22819 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
22820 glyph++;
22821 while (glyph < last
22822 && glyph->u.cmp.automatic
22823 && glyph->u.cmp.id == s->cmp_id
22824 && s->cmp_to == glyph->slice.cmp.from)
22825 s->cmp_to = (glyph++)->slice.cmp.to + 1;
22826
22827 for (i = s->cmp_from; i < s->cmp_to; i++)
22828 {
22829 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
22830 unsigned code = LGLYPH_CODE (lglyph);
22831
22832 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
22833 }
22834 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
22835 return glyph - s->row->glyphs[s->area];
22836 }
22837
22838
22839 /* Fill glyph string S from a sequence glyphs for glyphless characters.
22840 See the comment of fill_glyph_string for arguments.
22841 Value is the index of the first glyph not in S. */
22842
22843
22844 static int
22845 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
22846 int start, int end, int overlaps)
22847 {
22848 struct glyph *glyph, *last;
22849 int voffset;
22850
22851 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
22852 s->for_overlaps = overlaps;
22853 glyph = s->row->glyphs[s->area] + start;
22854 last = s->row->glyphs[s->area] + end;
22855 voffset = glyph->voffset;
22856 s->face = FACE_FROM_ID (s->f, face_id);
22857 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
22858 s->nchars = 1;
22859 s->width = glyph->pixel_width;
22860 glyph++;
22861 while (glyph < last
22862 && glyph->type == GLYPHLESS_GLYPH
22863 && glyph->voffset == voffset
22864 && glyph->face_id == face_id)
22865 {
22866 s->nchars++;
22867 s->width += glyph->pixel_width;
22868 glyph++;
22869 }
22870 s->ybase += voffset;
22871 return glyph - s->row->glyphs[s->area];
22872 }
22873
22874
22875 /* Fill glyph string S from a sequence of character glyphs.
22876
22877 FACE_ID is the face id of the string. START is the index of the
22878 first glyph to consider, END is the index of the last + 1.
22879 OVERLAPS non-zero means S should draw the foreground only, and use
22880 its physical height for clipping. See also draw_glyphs.
22881
22882 Value is the index of the first glyph not in S. */
22883
22884 static int
22885 fill_glyph_string (struct glyph_string *s, int face_id,
22886 int start, int end, int overlaps)
22887 {
22888 struct glyph *glyph, *last;
22889 int voffset;
22890 int glyph_not_available_p;
22891
22892 eassert (s->f == XFRAME (s->w->frame));
22893 eassert (s->nchars == 0);
22894 eassert (start >= 0 && end > start);
22895
22896 s->for_overlaps = overlaps;
22897 glyph = s->row->glyphs[s->area] + start;
22898 last = s->row->glyphs[s->area] + end;
22899 voffset = glyph->voffset;
22900 s->padding_p = glyph->padding_p;
22901 glyph_not_available_p = glyph->glyph_not_available_p;
22902
22903 while (glyph < last
22904 && glyph->type == CHAR_GLYPH
22905 && glyph->voffset == voffset
22906 /* Same face id implies same font, nowadays. */
22907 && glyph->face_id == face_id
22908 && glyph->glyph_not_available_p == glyph_not_available_p)
22909 {
22910 int two_byte_p;
22911
22912 s->face = get_glyph_face_and_encoding (s->f, glyph,
22913 s->char2b + s->nchars,
22914 &two_byte_p);
22915 s->two_byte_p = two_byte_p;
22916 ++s->nchars;
22917 eassert (s->nchars <= end - start);
22918 s->width += glyph->pixel_width;
22919 if (glyph++->padding_p != s->padding_p)
22920 break;
22921 }
22922
22923 s->font = s->face->font;
22924
22925 /* If the specified font could not be loaded, use the frame's font,
22926 but record the fact that we couldn't load it in
22927 S->font_not_found_p so that we can draw rectangles for the
22928 characters of the glyph string. */
22929 if (s->font == NULL || glyph_not_available_p)
22930 {
22931 s->font_not_found_p = 1;
22932 s->font = FRAME_FONT (s->f);
22933 }
22934
22935 /* Adjust base line for subscript/superscript text. */
22936 s->ybase += voffset;
22937
22938 eassert (s->face && s->face->gc);
22939 return glyph - s->row->glyphs[s->area];
22940 }
22941
22942
22943 /* Fill glyph string S from image glyph S->first_glyph. */
22944
22945 static void
22946 fill_image_glyph_string (struct glyph_string *s)
22947 {
22948 eassert (s->first_glyph->type == IMAGE_GLYPH);
22949 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
22950 eassert (s->img);
22951 s->slice = s->first_glyph->slice.img;
22952 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
22953 s->font = s->face->font;
22954 s->width = s->first_glyph->pixel_width;
22955
22956 /* Adjust base line for subscript/superscript text. */
22957 s->ybase += s->first_glyph->voffset;
22958 }
22959
22960
22961 /* Fill glyph string S from a sequence of stretch glyphs.
22962
22963 START is the index of the first glyph to consider,
22964 END is the index of the last + 1.
22965
22966 Value is the index of the first glyph not in S. */
22967
22968 static int
22969 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
22970 {
22971 struct glyph *glyph, *last;
22972 int voffset, face_id;
22973
22974 eassert (s->first_glyph->type == STRETCH_GLYPH);
22975
22976 glyph = s->row->glyphs[s->area] + start;
22977 last = s->row->glyphs[s->area] + end;
22978 face_id = glyph->face_id;
22979 s->face = FACE_FROM_ID (s->f, face_id);
22980 s->font = s->face->font;
22981 s->width = glyph->pixel_width;
22982 s->nchars = 1;
22983 voffset = glyph->voffset;
22984
22985 for (++glyph;
22986 (glyph < last
22987 && glyph->type == STRETCH_GLYPH
22988 && glyph->voffset == voffset
22989 && glyph->face_id == face_id);
22990 ++glyph)
22991 s->width += glyph->pixel_width;
22992
22993 /* Adjust base line for subscript/superscript text. */
22994 s->ybase += voffset;
22995
22996 /* The case that face->gc == 0 is handled when drawing the glyph
22997 string by calling PREPARE_FACE_FOR_DISPLAY. */
22998 eassert (s->face);
22999 return glyph - s->row->glyphs[s->area];
23000 }
23001
23002 static struct font_metrics *
23003 get_per_char_metric (struct font *font, XChar2b *char2b)
23004 {
23005 static struct font_metrics metrics;
23006 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
23007
23008 if (! font || code == FONT_INVALID_CODE)
23009 return NULL;
23010 font->driver->text_extents (font, &code, 1, &metrics);
23011 return &metrics;
23012 }
23013
23014 /* EXPORT for RIF:
23015 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
23016 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
23017 assumed to be zero. */
23018
23019 void
23020 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
23021 {
23022 *left = *right = 0;
23023
23024 if (glyph->type == CHAR_GLYPH)
23025 {
23026 struct face *face;
23027 XChar2b char2b;
23028 struct font_metrics *pcm;
23029
23030 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
23031 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
23032 {
23033 if (pcm->rbearing > pcm->width)
23034 *right = pcm->rbearing - pcm->width;
23035 if (pcm->lbearing < 0)
23036 *left = -pcm->lbearing;
23037 }
23038 }
23039 else if (glyph->type == COMPOSITE_GLYPH)
23040 {
23041 if (! glyph->u.cmp.automatic)
23042 {
23043 struct composition *cmp = composition_table[glyph->u.cmp.id];
23044
23045 if (cmp->rbearing > cmp->pixel_width)
23046 *right = cmp->rbearing - cmp->pixel_width;
23047 if (cmp->lbearing < 0)
23048 *left = - cmp->lbearing;
23049 }
23050 else
23051 {
23052 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
23053 struct font_metrics metrics;
23054
23055 composition_gstring_width (gstring, glyph->slice.cmp.from,
23056 glyph->slice.cmp.to + 1, &metrics);
23057 if (metrics.rbearing > metrics.width)
23058 *right = metrics.rbearing - metrics.width;
23059 if (metrics.lbearing < 0)
23060 *left = - metrics.lbearing;
23061 }
23062 }
23063 }
23064
23065
23066 /* Return the index of the first glyph preceding glyph string S that
23067 is overwritten by S because of S's left overhang. Value is -1
23068 if no glyphs are overwritten. */
23069
23070 static int
23071 left_overwritten (struct glyph_string *s)
23072 {
23073 int k;
23074
23075 if (s->left_overhang)
23076 {
23077 int x = 0, i;
23078 struct glyph *glyphs = s->row->glyphs[s->area];
23079 int first = s->first_glyph - glyphs;
23080
23081 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
23082 x -= glyphs[i].pixel_width;
23083
23084 k = i + 1;
23085 }
23086 else
23087 k = -1;
23088
23089 return k;
23090 }
23091
23092
23093 /* Return the index of the first glyph preceding glyph string S that
23094 is overwriting S because of its right overhang. Value is -1 if no
23095 glyph in front of S overwrites S. */
23096
23097 static int
23098 left_overwriting (struct glyph_string *s)
23099 {
23100 int i, k, x;
23101 struct glyph *glyphs = s->row->glyphs[s->area];
23102 int first = s->first_glyph - glyphs;
23103
23104 k = -1;
23105 x = 0;
23106 for (i = first - 1; i >= 0; --i)
23107 {
23108 int left, right;
23109 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23110 if (x + right > 0)
23111 k = i;
23112 x -= glyphs[i].pixel_width;
23113 }
23114
23115 return k;
23116 }
23117
23118
23119 /* Return the index of the last glyph following glyph string S that is
23120 overwritten by S because of S's right overhang. Value is -1 if
23121 no such glyph is found. */
23122
23123 static int
23124 right_overwritten (struct glyph_string *s)
23125 {
23126 int k = -1;
23127
23128 if (s->right_overhang)
23129 {
23130 int x = 0, i;
23131 struct glyph *glyphs = s->row->glyphs[s->area];
23132 int first = (s->first_glyph - glyphs
23133 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23134 int end = s->row->used[s->area];
23135
23136 for (i = first; i < end && s->right_overhang > x; ++i)
23137 x += glyphs[i].pixel_width;
23138
23139 k = i;
23140 }
23141
23142 return k;
23143 }
23144
23145
23146 /* Return the index of the last glyph following glyph string S that
23147 overwrites S because of its left overhang. Value is negative
23148 if no such glyph is found. */
23149
23150 static int
23151 right_overwriting (struct glyph_string *s)
23152 {
23153 int i, k, x;
23154 int end = s->row->used[s->area];
23155 struct glyph *glyphs = s->row->glyphs[s->area];
23156 int first = (s->first_glyph - glyphs
23157 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23158
23159 k = -1;
23160 x = 0;
23161 for (i = first; i < end; ++i)
23162 {
23163 int left, right;
23164 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23165 if (x - left < 0)
23166 k = i;
23167 x += glyphs[i].pixel_width;
23168 }
23169
23170 return k;
23171 }
23172
23173
23174 /* Set background width of glyph string S. START is the index of the
23175 first glyph following S. LAST_X is the right-most x-position + 1
23176 in the drawing area. */
23177
23178 static void
23179 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23180 {
23181 /* If the face of this glyph string has to be drawn to the end of
23182 the drawing area, set S->extends_to_end_of_line_p. */
23183
23184 if (start == s->row->used[s->area]
23185 && s->area == TEXT_AREA
23186 && ((s->row->fill_line_p
23187 && (s->hl == DRAW_NORMAL_TEXT
23188 || s->hl == DRAW_IMAGE_RAISED
23189 || s->hl == DRAW_IMAGE_SUNKEN))
23190 || s->hl == DRAW_MOUSE_FACE))
23191 s->extends_to_end_of_line_p = 1;
23192
23193 /* If S extends its face to the end of the line, set its
23194 background_width to the distance to the right edge of the drawing
23195 area. */
23196 if (s->extends_to_end_of_line_p)
23197 s->background_width = last_x - s->x + 1;
23198 else
23199 s->background_width = s->width;
23200 }
23201
23202
23203 /* Compute overhangs and x-positions for glyph string S and its
23204 predecessors, or successors. X is the starting x-position for S.
23205 BACKWARD_P non-zero means process predecessors. */
23206
23207 static void
23208 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23209 {
23210 if (backward_p)
23211 {
23212 while (s)
23213 {
23214 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23215 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23216 x -= s->width;
23217 s->x = x;
23218 s = s->prev;
23219 }
23220 }
23221 else
23222 {
23223 while (s)
23224 {
23225 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23226 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23227 s->x = x;
23228 x += s->width;
23229 s = s->next;
23230 }
23231 }
23232 }
23233
23234
23235
23236 /* The following macros are only called from draw_glyphs below.
23237 They reference the following parameters of that function directly:
23238 `w', `row', `area', and `overlap_p'
23239 as well as the following local variables:
23240 `s', `f', and `hdc' (in W32) */
23241
23242 #ifdef HAVE_NTGUI
23243 /* On W32, silently add local `hdc' variable to argument list of
23244 init_glyph_string. */
23245 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23246 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23247 #else
23248 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23249 init_glyph_string (s, char2b, w, row, area, start, hl)
23250 #endif
23251
23252 /* Add a glyph string for a stretch glyph to the list of strings
23253 between HEAD and TAIL. START is the index of the stretch glyph in
23254 row area AREA of glyph row ROW. END is the index of the last glyph
23255 in that glyph row area. X is the current output position assigned
23256 to the new glyph string constructed. HL overrides that face of the
23257 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23258 is the right-most x-position of the drawing area. */
23259
23260 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23261 and below -- keep them on one line. */
23262 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23263 do \
23264 { \
23265 s = alloca (sizeof *s); \
23266 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23267 START = fill_stretch_glyph_string (s, START, END); \
23268 append_glyph_string (&HEAD, &TAIL, s); \
23269 s->x = (X); \
23270 } \
23271 while (0)
23272
23273
23274 /* Add a glyph string for an image glyph to the list of strings
23275 between HEAD and TAIL. START is the index of the image glyph in
23276 row area AREA of glyph row ROW. END is the index of the last glyph
23277 in that glyph row area. X is the current output position assigned
23278 to the new glyph string constructed. HL overrides that face of the
23279 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23280 is the right-most x-position of the drawing area. */
23281
23282 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23283 do \
23284 { \
23285 s = alloca (sizeof *s); \
23286 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23287 fill_image_glyph_string (s); \
23288 append_glyph_string (&HEAD, &TAIL, s); \
23289 ++START; \
23290 s->x = (X); \
23291 } \
23292 while (0)
23293
23294
23295 /* Add a glyph string for a sequence of character glyphs to the list
23296 of strings between HEAD and TAIL. START is the index of the first
23297 glyph in row area AREA of glyph row ROW that is part of the new
23298 glyph string. END is the index of the last glyph in that glyph row
23299 area. X is the current output position assigned to the new glyph
23300 string constructed. HL overrides that face of the glyph; e.g. it
23301 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23302 right-most x-position of the drawing area. */
23303
23304 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23305 do \
23306 { \
23307 int face_id; \
23308 XChar2b *char2b; \
23309 \
23310 face_id = (row)->glyphs[area][START].face_id; \
23311 \
23312 s = alloca (sizeof *s); \
23313 char2b = alloca ((END - START) * sizeof *char2b); \
23314 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23315 append_glyph_string (&HEAD, &TAIL, s); \
23316 s->x = (X); \
23317 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23318 } \
23319 while (0)
23320
23321
23322 /* Add a glyph string for a composite sequence to the list of strings
23323 between HEAD and TAIL. START is the index of the first glyph in
23324 row area AREA of glyph row ROW that is part of the new glyph
23325 string. END is the index of the last glyph in that glyph row area.
23326 X is the current output position assigned to the new glyph string
23327 constructed. HL overrides that face of the glyph; e.g. it is
23328 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23329 x-position of the drawing area. */
23330
23331 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23332 do { \
23333 int face_id = (row)->glyphs[area][START].face_id; \
23334 struct face *base_face = FACE_FROM_ID (f, face_id); \
23335 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23336 struct composition *cmp = composition_table[cmp_id]; \
23337 XChar2b *char2b; \
23338 struct glyph_string *first_s = NULL; \
23339 int n; \
23340 \
23341 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23342 \
23343 /* Make glyph_strings for each glyph sequence that is drawable by \
23344 the same face, and append them to HEAD/TAIL. */ \
23345 for (n = 0; n < cmp->glyph_len;) \
23346 { \
23347 s = alloca (sizeof *s); \
23348 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23349 append_glyph_string (&(HEAD), &(TAIL), s); \
23350 s->cmp = cmp; \
23351 s->cmp_from = n; \
23352 s->x = (X); \
23353 if (n == 0) \
23354 first_s = s; \
23355 n = fill_composite_glyph_string (s, base_face, overlaps); \
23356 } \
23357 \
23358 ++START; \
23359 s = first_s; \
23360 } while (0)
23361
23362
23363 /* Add a glyph string for a glyph-string sequence to the list of strings
23364 between HEAD and TAIL. */
23365
23366 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23367 do { \
23368 int face_id; \
23369 XChar2b *char2b; \
23370 Lisp_Object gstring; \
23371 \
23372 face_id = (row)->glyphs[area][START].face_id; \
23373 gstring = (composition_gstring_from_id \
23374 ((row)->glyphs[area][START].u.cmp.id)); \
23375 s = alloca (sizeof *s); \
23376 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23377 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23378 append_glyph_string (&(HEAD), &(TAIL), s); \
23379 s->x = (X); \
23380 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23381 } while (0)
23382
23383
23384 /* Add a glyph string for a sequence of glyphless character's glyphs
23385 to the list of strings between HEAD and TAIL. The meanings of
23386 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23387
23388 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23389 do \
23390 { \
23391 int face_id; \
23392 \
23393 face_id = (row)->glyphs[area][START].face_id; \
23394 \
23395 s = alloca (sizeof *s); \
23396 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23397 append_glyph_string (&HEAD, &TAIL, s); \
23398 s->x = (X); \
23399 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23400 overlaps); \
23401 } \
23402 while (0)
23403
23404
23405 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23406 of AREA of glyph row ROW on window W between indices START and END.
23407 HL overrides the face for drawing glyph strings, e.g. it is
23408 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23409 x-positions of the drawing area.
23410
23411 This is an ugly monster macro construct because we must use alloca
23412 to allocate glyph strings (because draw_glyphs can be called
23413 asynchronously). */
23414
23415 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23416 do \
23417 { \
23418 HEAD = TAIL = NULL; \
23419 while (START < END) \
23420 { \
23421 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23422 switch (first_glyph->type) \
23423 { \
23424 case CHAR_GLYPH: \
23425 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23426 HL, X, LAST_X); \
23427 break; \
23428 \
23429 case COMPOSITE_GLYPH: \
23430 if (first_glyph->u.cmp.automatic) \
23431 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23432 HL, X, LAST_X); \
23433 else \
23434 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23435 HL, X, LAST_X); \
23436 break; \
23437 \
23438 case STRETCH_GLYPH: \
23439 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23440 HL, X, LAST_X); \
23441 break; \
23442 \
23443 case IMAGE_GLYPH: \
23444 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23445 HL, X, LAST_X); \
23446 break; \
23447 \
23448 case GLYPHLESS_GLYPH: \
23449 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23450 HL, X, LAST_X); \
23451 break; \
23452 \
23453 default: \
23454 emacs_abort (); \
23455 } \
23456 \
23457 if (s) \
23458 { \
23459 set_glyph_string_background_width (s, START, LAST_X); \
23460 (X) += s->width; \
23461 } \
23462 } \
23463 } while (0)
23464
23465
23466 /* Draw glyphs between START and END in AREA of ROW on window W,
23467 starting at x-position X. X is relative to AREA in W. HL is a
23468 face-override with the following meaning:
23469
23470 DRAW_NORMAL_TEXT draw normally
23471 DRAW_CURSOR draw in cursor face
23472 DRAW_MOUSE_FACE draw in mouse face.
23473 DRAW_INVERSE_VIDEO draw in mode line face
23474 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23475 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23476
23477 If OVERLAPS is non-zero, draw only the foreground of characters and
23478 clip to the physical height of ROW. Non-zero value also defines
23479 the overlapping part to be drawn:
23480
23481 OVERLAPS_PRED overlap with preceding rows
23482 OVERLAPS_SUCC overlap with succeeding rows
23483 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23484 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23485
23486 Value is the x-position reached, relative to AREA of W. */
23487
23488 static int
23489 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23490 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23491 enum draw_glyphs_face hl, int overlaps)
23492 {
23493 struct glyph_string *head, *tail;
23494 struct glyph_string *s;
23495 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23496 int i, j, x_reached, last_x, area_left = 0;
23497 struct frame *f = XFRAME (WINDOW_FRAME (w));
23498 DECLARE_HDC (hdc);
23499
23500 ALLOCATE_HDC (hdc, f);
23501
23502 /* Let's rather be paranoid than getting a SEGV. */
23503 end = min (end, row->used[area]);
23504 start = max (0, start);
23505 start = min (end, start);
23506
23507 /* Translate X to frame coordinates. Set last_x to the right
23508 end of the drawing area. */
23509 if (row->full_width_p)
23510 {
23511 /* X is relative to the left edge of W, without scroll bars
23512 or fringes. */
23513 area_left = WINDOW_LEFT_EDGE_X (w);
23514 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23515 }
23516 else
23517 {
23518 area_left = window_box_left (w, area);
23519 last_x = area_left + window_box_width (w, area);
23520 }
23521 x += area_left;
23522
23523 /* Build a doubly-linked list of glyph_string structures between
23524 head and tail from what we have to draw. Note that the macro
23525 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23526 the reason we use a separate variable `i'. */
23527 i = start;
23528 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23529 if (tail)
23530 x_reached = tail->x + tail->background_width;
23531 else
23532 x_reached = x;
23533
23534 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23535 the row, redraw some glyphs in front or following the glyph
23536 strings built above. */
23537 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23538 {
23539 struct glyph_string *h, *t;
23540 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23541 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23542 int check_mouse_face = 0;
23543 int dummy_x = 0;
23544
23545 /* If mouse highlighting is on, we may need to draw adjacent
23546 glyphs using mouse-face highlighting. */
23547 if (area == TEXT_AREA && row->mouse_face_p
23548 && hlinfo->mouse_face_beg_row >= 0
23549 && hlinfo->mouse_face_end_row >= 0)
23550 {
23551 struct glyph_row *mouse_beg_row, *mouse_end_row;
23552
23553 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
23554 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
23555
23556 if (row >= mouse_beg_row && row <= mouse_end_row)
23557 {
23558 check_mouse_face = 1;
23559 mouse_beg_col = (row == mouse_beg_row)
23560 ? hlinfo->mouse_face_beg_col : 0;
23561 mouse_end_col = (row == mouse_end_row)
23562 ? hlinfo->mouse_face_end_col
23563 : row->used[TEXT_AREA];
23564 }
23565 }
23566
23567 /* Compute overhangs for all glyph strings. */
23568 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23569 for (s = head; s; s = s->next)
23570 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23571
23572 /* Prepend glyph strings for glyphs in front of the first glyph
23573 string that are overwritten because of the first glyph
23574 string's left overhang. The background of all strings
23575 prepended must be drawn because the first glyph string
23576 draws over it. */
23577 i = left_overwritten (head);
23578 if (i >= 0)
23579 {
23580 enum draw_glyphs_face overlap_hl;
23581
23582 /* If this row contains mouse highlighting, attempt to draw
23583 the overlapped glyphs with the correct highlight. This
23584 code fails if the overlap encompasses more than one glyph
23585 and mouse-highlight spans only some of these glyphs.
23586 However, making it work perfectly involves a lot more
23587 code, and I don't know if the pathological case occurs in
23588 practice, so we'll stick to this for now. --- cyd */
23589 if (check_mouse_face
23590 && mouse_beg_col < start && mouse_end_col > i)
23591 overlap_hl = DRAW_MOUSE_FACE;
23592 else
23593 overlap_hl = DRAW_NORMAL_TEXT;
23594
23595 j = i;
23596 BUILD_GLYPH_STRINGS (j, start, h, t,
23597 overlap_hl, dummy_x, last_x);
23598 start = i;
23599 compute_overhangs_and_x (t, head->x, 1);
23600 prepend_glyph_string_lists (&head, &tail, h, t);
23601 clip_head = head;
23602 }
23603
23604 /* Prepend glyph strings for glyphs in front of the first glyph
23605 string that overwrite that glyph string because of their
23606 right overhang. For these strings, only the foreground must
23607 be drawn, because it draws over the glyph string at `head'.
23608 The background must not be drawn because this would overwrite
23609 right overhangs of preceding glyphs for which no glyph
23610 strings exist. */
23611 i = left_overwriting (head);
23612 if (i >= 0)
23613 {
23614 enum draw_glyphs_face overlap_hl;
23615
23616 if (check_mouse_face
23617 && mouse_beg_col < start && mouse_end_col > i)
23618 overlap_hl = DRAW_MOUSE_FACE;
23619 else
23620 overlap_hl = DRAW_NORMAL_TEXT;
23621
23622 clip_head = head;
23623 BUILD_GLYPH_STRINGS (i, start, h, t,
23624 overlap_hl, dummy_x, last_x);
23625 for (s = h; s; s = s->next)
23626 s->background_filled_p = 1;
23627 compute_overhangs_and_x (t, head->x, 1);
23628 prepend_glyph_string_lists (&head, &tail, h, t);
23629 }
23630
23631 /* Append glyphs strings for glyphs following the last glyph
23632 string tail that are overwritten by tail. The background of
23633 these strings has to be drawn because tail's foreground draws
23634 over it. */
23635 i = right_overwritten (tail);
23636 if (i >= 0)
23637 {
23638 enum draw_glyphs_face overlap_hl;
23639
23640 if (check_mouse_face
23641 && mouse_beg_col < i && mouse_end_col > end)
23642 overlap_hl = DRAW_MOUSE_FACE;
23643 else
23644 overlap_hl = DRAW_NORMAL_TEXT;
23645
23646 BUILD_GLYPH_STRINGS (end, i, h, t,
23647 overlap_hl, x, last_x);
23648 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23649 we don't have `end = i;' here. */
23650 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23651 append_glyph_string_lists (&head, &tail, h, t);
23652 clip_tail = tail;
23653 }
23654
23655 /* Append glyph strings for glyphs following the last glyph
23656 string tail that overwrite tail. The foreground of such
23657 glyphs has to be drawn because it writes into the background
23658 of tail. The background must not be drawn because it could
23659 paint over the foreground of following glyphs. */
23660 i = right_overwriting (tail);
23661 if (i >= 0)
23662 {
23663 enum draw_glyphs_face overlap_hl;
23664 if (check_mouse_face
23665 && mouse_beg_col < i && mouse_end_col > end)
23666 overlap_hl = DRAW_MOUSE_FACE;
23667 else
23668 overlap_hl = DRAW_NORMAL_TEXT;
23669
23670 clip_tail = tail;
23671 i++; /* We must include the Ith glyph. */
23672 BUILD_GLYPH_STRINGS (end, i, h, t,
23673 overlap_hl, x, last_x);
23674 for (s = h; s; s = s->next)
23675 s->background_filled_p = 1;
23676 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23677 append_glyph_string_lists (&head, &tail, h, t);
23678 }
23679 if (clip_head || clip_tail)
23680 for (s = head; s; s = s->next)
23681 {
23682 s->clip_head = clip_head;
23683 s->clip_tail = clip_tail;
23684 }
23685 }
23686
23687 /* Draw all strings. */
23688 for (s = head; s; s = s->next)
23689 FRAME_RIF (f)->draw_glyph_string (s);
23690
23691 #ifndef HAVE_NS
23692 /* When focus a sole frame and move horizontally, this sets on_p to 0
23693 causing a failure to erase prev cursor position. */
23694 if (area == TEXT_AREA
23695 && !row->full_width_p
23696 /* When drawing overlapping rows, only the glyph strings'
23697 foreground is drawn, which doesn't erase a cursor
23698 completely. */
23699 && !overlaps)
23700 {
23701 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23702 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23703 : (tail ? tail->x + tail->background_width : x));
23704 x0 -= area_left;
23705 x1 -= area_left;
23706
23707 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23708 row->y, MATRIX_ROW_BOTTOM_Y (row));
23709 }
23710 #endif
23711
23712 /* Value is the x-position up to which drawn, relative to AREA of W.
23713 This doesn't include parts drawn because of overhangs. */
23714 if (row->full_width_p)
23715 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23716 else
23717 x_reached -= area_left;
23718
23719 RELEASE_HDC (hdc, f);
23720
23721 return x_reached;
23722 }
23723
23724 /* Expand row matrix if too narrow. Don't expand if area
23725 is not present. */
23726
23727 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
23728 { \
23729 if (!fonts_changed_p \
23730 && (it->glyph_row->glyphs[area] \
23731 < it->glyph_row->glyphs[area + 1])) \
23732 { \
23733 it->w->ncols_scale_factor++; \
23734 fonts_changed_p = 1; \
23735 } \
23736 }
23737
23738 /* Store one glyph for IT->char_to_display in IT->glyph_row.
23739 Called from x_produce_glyphs when IT->glyph_row is non-null. */
23740
23741 static void
23742 append_glyph (struct it *it)
23743 {
23744 struct glyph *glyph;
23745 enum glyph_row_area area = it->area;
23746
23747 eassert (it->glyph_row);
23748 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
23749
23750 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23751 if (glyph < it->glyph_row->glyphs[area + 1])
23752 {
23753 /* If the glyph row is reversed, we need to prepend the glyph
23754 rather than append it. */
23755 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23756 {
23757 struct glyph *g;
23758
23759 /* Make room for the additional glyph. */
23760 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23761 g[1] = *g;
23762 glyph = it->glyph_row->glyphs[area];
23763 }
23764 glyph->charpos = CHARPOS (it->position);
23765 glyph->object = it->object;
23766 if (it->pixel_width > 0)
23767 {
23768 glyph->pixel_width = it->pixel_width;
23769 glyph->padding_p = 0;
23770 }
23771 else
23772 {
23773 /* Assure at least 1-pixel width. Otherwise, cursor can't
23774 be displayed correctly. */
23775 glyph->pixel_width = 1;
23776 glyph->padding_p = 1;
23777 }
23778 glyph->ascent = it->ascent;
23779 glyph->descent = it->descent;
23780 glyph->voffset = it->voffset;
23781 glyph->type = CHAR_GLYPH;
23782 glyph->avoid_cursor_p = it->avoid_cursor_p;
23783 glyph->multibyte_p = it->multibyte_p;
23784 glyph->left_box_line_p = it->start_of_box_run_p;
23785 glyph->right_box_line_p = it->end_of_box_run_p;
23786 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23787 || it->phys_descent > it->descent);
23788 glyph->glyph_not_available_p = it->glyph_not_available_p;
23789 glyph->face_id = it->face_id;
23790 glyph->u.ch = it->char_to_display;
23791 glyph->slice.img = null_glyph_slice;
23792 glyph->font_type = FONT_TYPE_UNKNOWN;
23793 if (it->bidi_p)
23794 {
23795 glyph->resolved_level = it->bidi_it.resolved_level;
23796 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23797 emacs_abort ();
23798 glyph->bidi_type = it->bidi_it.type;
23799 }
23800 else
23801 {
23802 glyph->resolved_level = 0;
23803 glyph->bidi_type = UNKNOWN_BT;
23804 }
23805 ++it->glyph_row->used[area];
23806 }
23807 else
23808 IT_EXPAND_MATRIX_WIDTH (it, area);
23809 }
23810
23811 /* Store one glyph for the composition IT->cmp_it.id in
23812 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
23813 non-null. */
23814
23815 static void
23816 append_composite_glyph (struct it *it)
23817 {
23818 struct glyph *glyph;
23819 enum glyph_row_area area = it->area;
23820
23821 eassert (it->glyph_row);
23822
23823 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23824 if (glyph < it->glyph_row->glyphs[area + 1])
23825 {
23826 /* If the glyph row is reversed, we need to prepend the glyph
23827 rather than append it. */
23828 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
23829 {
23830 struct glyph *g;
23831
23832 /* Make room for the new glyph. */
23833 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
23834 g[1] = *g;
23835 glyph = it->glyph_row->glyphs[it->area];
23836 }
23837 glyph->charpos = it->cmp_it.charpos;
23838 glyph->object = it->object;
23839 glyph->pixel_width = it->pixel_width;
23840 glyph->ascent = it->ascent;
23841 glyph->descent = it->descent;
23842 glyph->voffset = it->voffset;
23843 glyph->type = COMPOSITE_GLYPH;
23844 if (it->cmp_it.ch < 0)
23845 {
23846 glyph->u.cmp.automatic = 0;
23847 glyph->u.cmp.id = it->cmp_it.id;
23848 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
23849 }
23850 else
23851 {
23852 glyph->u.cmp.automatic = 1;
23853 glyph->u.cmp.id = it->cmp_it.id;
23854 glyph->slice.cmp.from = it->cmp_it.from;
23855 glyph->slice.cmp.to = it->cmp_it.to - 1;
23856 }
23857 glyph->avoid_cursor_p = it->avoid_cursor_p;
23858 glyph->multibyte_p = it->multibyte_p;
23859 glyph->left_box_line_p = it->start_of_box_run_p;
23860 glyph->right_box_line_p = it->end_of_box_run_p;
23861 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23862 || it->phys_descent > it->descent);
23863 glyph->padding_p = 0;
23864 glyph->glyph_not_available_p = 0;
23865 glyph->face_id = it->face_id;
23866 glyph->font_type = FONT_TYPE_UNKNOWN;
23867 if (it->bidi_p)
23868 {
23869 glyph->resolved_level = it->bidi_it.resolved_level;
23870 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23871 emacs_abort ();
23872 glyph->bidi_type = it->bidi_it.type;
23873 }
23874 ++it->glyph_row->used[area];
23875 }
23876 else
23877 IT_EXPAND_MATRIX_WIDTH (it, area);
23878 }
23879
23880
23881 /* Change IT->ascent and IT->height according to the setting of
23882 IT->voffset. */
23883
23884 static void
23885 take_vertical_position_into_account (struct it *it)
23886 {
23887 if (it->voffset)
23888 {
23889 if (it->voffset < 0)
23890 /* Increase the ascent so that we can display the text higher
23891 in the line. */
23892 it->ascent -= it->voffset;
23893 else
23894 /* Increase the descent so that we can display the text lower
23895 in the line. */
23896 it->descent += it->voffset;
23897 }
23898 }
23899
23900
23901 /* Produce glyphs/get display metrics for the image IT is loaded with.
23902 See the description of struct display_iterator in dispextern.h for
23903 an overview of struct display_iterator. */
23904
23905 static void
23906 produce_image_glyph (struct it *it)
23907 {
23908 struct image *img;
23909 struct face *face;
23910 int glyph_ascent, crop;
23911 struct glyph_slice slice;
23912
23913 eassert (it->what == IT_IMAGE);
23914
23915 face = FACE_FROM_ID (it->f, it->face_id);
23916 eassert (face);
23917 /* Make sure X resources of the face is loaded. */
23918 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23919
23920 if (it->image_id < 0)
23921 {
23922 /* Fringe bitmap. */
23923 it->ascent = it->phys_ascent = 0;
23924 it->descent = it->phys_descent = 0;
23925 it->pixel_width = 0;
23926 it->nglyphs = 0;
23927 return;
23928 }
23929
23930 img = IMAGE_FROM_ID (it->f, it->image_id);
23931 eassert (img);
23932 /* Make sure X resources of the image is loaded. */
23933 prepare_image_for_display (it->f, img);
23934
23935 slice.x = slice.y = 0;
23936 slice.width = img->width;
23937 slice.height = img->height;
23938
23939 if (INTEGERP (it->slice.x))
23940 slice.x = XINT (it->slice.x);
23941 else if (FLOATP (it->slice.x))
23942 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
23943
23944 if (INTEGERP (it->slice.y))
23945 slice.y = XINT (it->slice.y);
23946 else if (FLOATP (it->slice.y))
23947 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
23948
23949 if (INTEGERP (it->slice.width))
23950 slice.width = XINT (it->slice.width);
23951 else if (FLOATP (it->slice.width))
23952 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
23953
23954 if (INTEGERP (it->slice.height))
23955 slice.height = XINT (it->slice.height);
23956 else if (FLOATP (it->slice.height))
23957 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
23958
23959 if (slice.x >= img->width)
23960 slice.x = img->width;
23961 if (slice.y >= img->height)
23962 slice.y = img->height;
23963 if (slice.x + slice.width >= img->width)
23964 slice.width = img->width - slice.x;
23965 if (slice.y + slice.height > img->height)
23966 slice.height = img->height - slice.y;
23967
23968 if (slice.width == 0 || slice.height == 0)
23969 return;
23970
23971 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
23972
23973 it->descent = slice.height - glyph_ascent;
23974 if (slice.y == 0)
23975 it->descent += img->vmargin;
23976 if (slice.y + slice.height == img->height)
23977 it->descent += img->vmargin;
23978 it->phys_descent = it->descent;
23979
23980 it->pixel_width = slice.width;
23981 if (slice.x == 0)
23982 it->pixel_width += img->hmargin;
23983 if (slice.x + slice.width == img->width)
23984 it->pixel_width += img->hmargin;
23985
23986 /* It's quite possible for images to have an ascent greater than
23987 their height, so don't get confused in that case. */
23988 if (it->descent < 0)
23989 it->descent = 0;
23990
23991 it->nglyphs = 1;
23992
23993 if (face->box != FACE_NO_BOX)
23994 {
23995 if (face->box_line_width > 0)
23996 {
23997 if (slice.y == 0)
23998 it->ascent += face->box_line_width;
23999 if (slice.y + slice.height == img->height)
24000 it->descent += face->box_line_width;
24001 }
24002
24003 if (it->start_of_box_run_p && slice.x == 0)
24004 it->pixel_width += eabs (face->box_line_width);
24005 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
24006 it->pixel_width += eabs (face->box_line_width);
24007 }
24008
24009 take_vertical_position_into_account (it);
24010
24011 /* Automatically crop wide image glyphs at right edge so we can
24012 draw the cursor on same display row. */
24013 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
24014 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
24015 {
24016 it->pixel_width -= crop;
24017 slice.width -= crop;
24018 }
24019
24020 if (it->glyph_row)
24021 {
24022 struct glyph *glyph;
24023 enum glyph_row_area area = it->area;
24024
24025 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24026 if (glyph < it->glyph_row->glyphs[area + 1])
24027 {
24028 glyph->charpos = CHARPOS (it->position);
24029 glyph->object = it->object;
24030 glyph->pixel_width = it->pixel_width;
24031 glyph->ascent = glyph_ascent;
24032 glyph->descent = it->descent;
24033 glyph->voffset = it->voffset;
24034 glyph->type = IMAGE_GLYPH;
24035 glyph->avoid_cursor_p = it->avoid_cursor_p;
24036 glyph->multibyte_p = it->multibyte_p;
24037 glyph->left_box_line_p = it->start_of_box_run_p;
24038 glyph->right_box_line_p = it->end_of_box_run_p;
24039 glyph->overlaps_vertically_p = 0;
24040 glyph->padding_p = 0;
24041 glyph->glyph_not_available_p = 0;
24042 glyph->face_id = it->face_id;
24043 glyph->u.img_id = img->id;
24044 glyph->slice.img = slice;
24045 glyph->font_type = FONT_TYPE_UNKNOWN;
24046 if (it->bidi_p)
24047 {
24048 glyph->resolved_level = it->bidi_it.resolved_level;
24049 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24050 emacs_abort ();
24051 glyph->bidi_type = it->bidi_it.type;
24052 }
24053 ++it->glyph_row->used[area];
24054 }
24055 else
24056 IT_EXPAND_MATRIX_WIDTH (it, area);
24057 }
24058 }
24059
24060
24061 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
24062 of the glyph, WIDTH and HEIGHT are the width and height of the
24063 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
24064
24065 static void
24066 append_stretch_glyph (struct it *it, Lisp_Object object,
24067 int width, int height, int ascent)
24068 {
24069 struct glyph *glyph;
24070 enum glyph_row_area area = it->area;
24071
24072 eassert (ascent >= 0 && ascent <= height);
24073
24074 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24075 if (glyph < it->glyph_row->glyphs[area + 1])
24076 {
24077 /* If the glyph row is reversed, we need to prepend the glyph
24078 rather than append it. */
24079 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24080 {
24081 struct glyph *g;
24082
24083 /* Make room for the additional glyph. */
24084 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24085 g[1] = *g;
24086 glyph = it->glyph_row->glyphs[area];
24087 }
24088 glyph->charpos = CHARPOS (it->position);
24089 glyph->object = object;
24090 glyph->pixel_width = width;
24091 glyph->ascent = ascent;
24092 glyph->descent = height - ascent;
24093 glyph->voffset = it->voffset;
24094 glyph->type = STRETCH_GLYPH;
24095 glyph->avoid_cursor_p = it->avoid_cursor_p;
24096 glyph->multibyte_p = it->multibyte_p;
24097 glyph->left_box_line_p = it->start_of_box_run_p;
24098 glyph->right_box_line_p = it->end_of_box_run_p;
24099 glyph->overlaps_vertically_p = 0;
24100 glyph->padding_p = 0;
24101 glyph->glyph_not_available_p = 0;
24102 glyph->face_id = it->face_id;
24103 glyph->u.stretch.ascent = ascent;
24104 glyph->u.stretch.height = height;
24105 glyph->slice.img = null_glyph_slice;
24106 glyph->font_type = FONT_TYPE_UNKNOWN;
24107 if (it->bidi_p)
24108 {
24109 glyph->resolved_level = it->bidi_it.resolved_level;
24110 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24111 emacs_abort ();
24112 glyph->bidi_type = it->bidi_it.type;
24113 }
24114 else
24115 {
24116 glyph->resolved_level = 0;
24117 glyph->bidi_type = UNKNOWN_BT;
24118 }
24119 ++it->glyph_row->used[area];
24120 }
24121 else
24122 IT_EXPAND_MATRIX_WIDTH (it, area);
24123 }
24124
24125 #endif /* HAVE_WINDOW_SYSTEM */
24126
24127 /* Produce a stretch glyph for iterator IT. IT->object is the value
24128 of the glyph property displayed. The value must be a list
24129 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24130 being recognized:
24131
24132 1. `:width WIDTH' specifies that the space should be WIDTH *
24133 canonical char width wide. WIDTH may be an integer or floating
24134 point number.
24135
24136 2. `:relative-width FACTOR' specifies that the width of the stretch
24137 should be computed from the width of the first character having the
24138 `glyph' property, and should be FACTOR times that width.
24139
24140 3. `:align-to HPOS' specifies that the space should be wide enough
24141 to reach HPOS, a value in canonical character units.
24142
24143 Exactly one of the above pairs must be present.
24144
24145 4. `:height HEIGHT' specifies that the height of the stretch produced
24146 should be HEIGHT, measured in canonical character units.
24147
24148 5. `:relative-height FACTOR' specifies that the height of the
24149 stretch should be FACTOR times the height of the characters having
24150 the glyph property.
24151
24152 Either none or exactly one of 4 or 5 must be present.
24153
24154 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24155 of the stretch should be used for the ascent of the stretch.
24156 ASCENT must be in the range 0 <= ASCENT <= 100. */
24157
24158 void
24159 produce_stretch_glyph (struct it *it)
24160 {
24161 /* (space :width WIDTH :height HEIGHT ...) */
24162 Lisp_Object prop, plist;
24163 int width = 0, height = 0, align_to = -1;
24164 int zero_width_ok_p = 0;
24165 double tem;
24166 struct font *font = NULL;
24167
24168 #ifdef HAVE_WINDOW_SYSTEM
24169 int ascent = 0;
24170 int zero_height_ok_p = 0;
24171
24172 if (FRAME_WINDOW_P (it->f))
24173 {
24174 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24175 font = face->font ? face->font : FRAME_FONT (it->f);
24176 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24177 }
24178 #endif
24179
24180 /* List should start with `space'. */
24181 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24182 plist = XCDR (it->object);
24183
24184 /* Compute the width of the stretch. */
24185 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24186 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24187 {
24188 /* Absolute width `:width WIDTH' specified and valid. */
24189 zero_width_ok_p = 1;
24190 width = (int)tem;
24191 }
24192 #ifdef HAVE_WINDOW_SYSTEM
24193 else if (FRAME_WINDOW_P (it->f)
24194 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24195 {
24196 /* Relative width `:relative-width FACTOR' specified and valid.
24197 Compute the width of the characters having the `glyph'
24198 property. */
24199 struct it it2;
24200 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24201
24202 it2 = *it;
24203 if (it->multibyte_p)
24204 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24205 else
24206 {
24207 it2.c = it2.char_to_display = *p, it2.len = 1;
24208 if (! ASCII_CHAR_P (it2.c))
24209 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24210 }
24211
24212 it2.glyph_row = NULL;
24213 it2.what = IT_CHARACTER;
24214 x_produce_glyphs (&it2);
24215 width = NUMVAL (prop) * it2.pixel_width;
24216 }
24217 #endif /* HAVE_WINDOW_SYSTEM */
24218 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24219 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24220 {
24221 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24222 align_to = (align_to < 0
24223 ? 0
24224 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24225 else if (align_to < 0)
24226 align_to = window_box_left_offset (it->w, TEXT_AREA);
24227 width = max (0, (int)tem + align_to - it->current_x);
24228 zero_width_ok_p = 1;
24229 }
24230 else
24231 /* Nothing specified -> width defaults to canonical char width. */
24232 width = FRAME_COLUMN_WIDTH (it->f);
24233
24234 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24235 width = 1;
24236
24237 #ifdef HAVE_WINDOW_SYSTEM
24238 /* Compute height. */
24239 if (FRAME_WINDOW_P (it->f))
24240 {
24241 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24242 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24243 {
24244 height = (int)tem;
24245 zero_height_ok_p = 1;
24246 }
24247 else if (prop = Fplist_get (plist, QCrelative_height),
24248 NUMVAL (prop) > 0)
24249 height = FONT_HEIGHT (font) * NUMVAL (prop);
24250 else
24251 height = FONT_HEIGHT (font);
24252
24253 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24254 height = 1;
24255
24256 /* Compute percentage of height used for ascent. If
24257 `:ascent ASCENT' is present and valid, use that. Otherwise,
24258 derive the ascent from the font in use. */
24259 if (prop = Fplist_get (plist, QCascent),
24260 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24261 ascent = height * NUMVAL (prop) / 100.0;
24262 else if (!NILP (prop)
24263 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24264 ascent = min (max (0, (int)tem), height);
24265 else
24266 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24267 }
24268 else
24269 #endif /* HAVE_WINDOW_SYSTEM */
24270 height = 1;
24271
24272 if (width > 0 && it->line_wrap != TRUNCATE
24273 && it->current_x + width > it->last_visible_x)
24274 {
24275 width = it->last_visible_x - it->current_x;
24276 #ifdef HAVE_WINDOW_SYSTEM
24277 /* Subtract one more pixel from the stretch width, but only on
24278 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24279 width -= FRAME_WINDOW_P (it->f);
24280 #endif
24281 }
24282
24283 if (width > 0 && height > 0 && it->glyph_row)
24284 {
24285 Lisp_Object o_object = it->object;
24286 Lisp_Object object = it->stack[it->sp - 1].string;
24287 int n = width;
24288
24289 if (!STRINGP (object))
24290 object = it->w->buffer;
24291 #ifdef HAVE_WINDOW_SYSTEM
24292 if (FRAME_WINDOW_P (it->f))
24293 append_stretch_glyph (it, object, width, height, ascent);
24294 else
24295 #endif
24296 {
24297 it->object = object;
24298 it->char_to_display = ' ';
24299 it->pixel_width = it->len = 1;
24300 while (n--)
24301 tty_append_glyph (it);
24302 it->object = o_object;
24303 }
24304 }
24305
24306 it->pixel_width = width;
24307 #ifdef HAVE_WINDOW_SYSTEM
24308 if (FRAME_WINDOW_P (it->f))
24309 {
24310 it->ascent = it->phys_ascent = ascent;
24311 it->descent = it->phys_descent = height - it->ascent;
24312 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24313 take_vertical_position_into_account (it);
24314 }
24315 else
24316 #endif
24317 it->nglyphs = width;
24318 }
24319
24320 /* Get information about special display element WHAT in an
24321 environment described by IT. WHAT is one of IT_TRUNCATION or
24322 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24323 non-null glyph_row member. This function ensures that fields like
24324 face_id, c, len of IT are left untouched. */
24325
24326 static void
24327 produce_special_glyphs (struct it *it, enum display_element_type what)
24328 {
24329 struct it temp_it;
24330 Lisp_Object gc;
24331 GLYPH glyph;
24332
24333 temp_it = *it;
24334 temp_it.object = make_number (0);
24335 memset (&temp_it.current, 0, sizeof temp_it.current);
24336
24337 if (what == IT_CONTINUATION)
24338 {
24339 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24340 if (it->bidi_it.paragraph_dir == R2L)
24341 SET_GLYPH_FROM_CHAR (glyph, '/');
24342 else
24343 SET_GLYPH_FROM_CHAR (glyph, '\\');
24344 if (it->dp
24345 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24346 {
24347 /* FIXME: Should we mirror GC for R2L lines? */
24348 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24349 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24350 }
24351 }
24352 else if (what == IT_TRUNCATION)
24353 {
24354 /* Truncation glyph. */
24355 SET_GLYPH_FROM_CHAR (glyph, '$');
24356 if (it->dp
24357 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24358 {
24359 /* FIXME: Should we mirror GC for R2L lines? */
24360 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24361 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24362 }
24363 }
24364 else
24365 emacs_abort ();
24366
24367 #ifdef HAVE_WINDOW_SYSTEM
24368 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24369 is turned off, we precede the truncation/continuation glyphs by a
24370 stretch glyph whose width is computed such that these special
24371 glyphs are aligned at the window margin, even when very different
24372 fonts are used in different glyph rows. */
24373 if (FRAME_WINDOW_P (temp_it.f)
24374 /* init_iterator calls this with it->glyph_row == NULL, and it
24375 wants only the pixel width of the truncation/continuation
24376 glyphs. */
24377 && temp_it.glyph_row
24378 /* insert_left_trunc_glyphs calls us at the beginning of the
24379 row, and it has its own calculation of the stretch glyph
24380 width. */
24381 && temp_it.glyph_row->used[TEXT_AREA] > 0
24382 && (temp_it.glyph_row->reversed_p
24383 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24384 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24385 {
24386 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24387
24388 if (stretch_width > 0)
24389 {
24390 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24391 struct font *font =
24392 face->font ? face->font : FRAME_FONT (temp_it.f);
24393 int stretch_ascent =
24394 (((temp_it.ascent + temp_it.descent)
24395 * FONT_BASE (font)) / FONT_HEIGHT (font));
24396
24397 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
24398 temp_it.ascent + temp_it.descent,
24399 stretch_ascent);
24400 }
24401 }
24402 #endif
24403
24404 temp_it.dp = NULL;
24405 temp_it.what = IT_CHARACTER;
24406 temp_it.len = 1;
24407 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24408 temp_it.face_id = GLYPH_FACE (glyph);
24409 temp_it.len = CHAR_BYTES (temp_it.c);
24410
24411 PRODUCE_GLYPHS (&temp_it);
24412 it->pixel_width = temp_it.pixel_width;
24413 it->nglyphs = temp_it.pixel_width;
24414 }
24415
24416 #ifdef HAVE_WINDOW_SYSTEM
24417
24418 /* Calculate line-height and line-spacing properties.
24419 An integer value specifies explicit pixel value.
24420 A float value specifies relative value to current face height.
24421 A cons (float . face-name) specifies relative value to
24422 height of specified face font.
24423
24424 Returns height in pixels, or nil. */
24425
24426
24427 static Lisp_Object
24428 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24429 int boff, int override)
24430 {
24431 Lisp_Object face_name = Qnil;
24432 int ascent, descent, height;
24433
24434 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24435 return val;
24436
24437 if (CONSP (val))
24438 {
24439 face_name = XCAR (val);
24440 val = XCDR (val);
24441 if (!NUMBERP (val))
24442 val = make_number (1);
24443 if (NILP (face_name))
24444 {
24445 height = it->ascent + it->descent;
24446 goto scale;
24447 }
24448 }
24449
24450 if (NILP (face_name))
24451 {
24452 font = FRAME_FONT (it->f);
24453 boff = FRAME_BASELINE_OFFSET (it->f);
24454 }
24455 else if (EQ (face_name, Qt))
24456 {
24457 override = 0;
24458 }
24459 else
24460 {
24461 int face_id;
24462 struct face *face;
24463
24464 face_id = lookup_named_face (it->f, face_name, 0);
24465 if (face_id < 0)
24466 return make_number (-1);
24467
24468 face = FACE_FROM_ID (it->f, face_id);
24469 font = face->font;
24470 if (font == NULL)
24471 return make_number (-1);
24472 boff = font->baseline_offset;
24473 if (font->vertical_centering)
24474 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24475 }
24476
24477 ascent = FONT_BASE (font) + boff;
24478 descent = FONT_DESCENT (font) - boff;
24479
24480 if (override)
24481 {
24482 it->override_ascent = ascent;
24483 it->override_descent = descent;
24484 it->override_boff = boff;
24485 }
24486
24487 height = ascent + descent;
24488
24489 scale:
24490 if (FLOATP (val))
24491 height = (int)(XFLOAT_DATA (val) * height);
24492 else if (INTEGERP (val))
24493 height *= XINT (val);
24494
24495 return make_number (height);
24496 }
24497
24498
24499 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24500 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24501 and only if this is for a character for which no font was found.
24502
24503 If the display method (it->glyphless_method) is
24504 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24505 length of the acronym or the hexadecimal string, UPPER_XOFF and
24506 UPPER_YOFF are pixel offsets for the upper part of the string,
24507 LOWER_XOFF and LOWER_YOFF are for the lower part.
24508
24509 For the other display methods, LEN through LOWER_YOFF are zero. */
24510
24511 static void
24512 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24513 short upper_xoff, short upper_yoff,
24514 short lower_xoff, short lower_yoff)
24515 {
24516 struct glyph *glyph;
24517 enum glyph_row_area area = it->area;
24518
24519 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24520 if (glyph < it->glyph_row->glyphs[area + 1])
24521 {
24522 /* If the glyph row is reversed, we need to prepend the glyph
24523 rather than append it. */
24524 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24525 {
24526 struct glyph *g;
24527
24528 /* Make room for the additional glyph. */
24529 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24530 g[1] = *g;
24531 glyph = it->glyph_row->glyphs[area];
24532 }
24533 glyph->charpos = CHARPOS (it->position);
24534 glyph->object = it->object;
24535 glyph->pixel_width = it->pixel_width;
24536 glyph->ascent = it->ascent;
24537 glyph->descent = it->descent;
24538 glyph->voffset = it->voffset;
24539 glyph->type = GLYPHLESS_GLYPH;
24540 glyph->u.glyphless.method = it->glyphless_method;
24541 glyph->u.glyphless.for_no_font = for_no_font;
24542 glyph->u.glyphless.len = len;
24543 glyph->u.glyphless.ch = it->c;
24544 glyph->slice.glyphless.upper_xoff = upper_xoff;
24545 glyph->slice.glyphless.upper_yoff = upper_yoff;
24546 glyph->slice.glyphless.lower_xoff = lower_xoff;
24547 glyph->slice.glyphless.lower_yoff = lower_yoff;
24548 glyph->avoid_cursor_p = it->avoid_cursor_p;
24549 glyph->multibyte_p = it->multibyte_p;
24550 glyph->left_box_line_p = it->start_of_box_run_p;
24551 glyph->right_box_line_p = it->end_of_box_run_p;
24552 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24553 || it->phys_descent > it->descent);
24554 glyph->padding_p = 0;
24555 glyph->glyph_not_available_p = 0;
24556 glyph->face_id = face_id;
24557 glyph->font_type = FONT_TYPE_UNKNOWN;
24558 if (it->bidi_p)
24559 {
24560 glyph->resolved_level = it->bidi_it.resolved_level;
24561 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24562 emacs_abort ();
24563 glyph->bidi_type = it->bidi_it.type;
24564 }
24565 ++it->glyph_row->used[area];
24566 }
24567 else
24568 IT_EXPAND_MATRIX_WIDTH (it, area);
24569 }
24570
24571
24572 /* Produce a glyph for a glyphless character for iterator IT.
24573 IT->glyphless_method specifies which method to use for displaying
24574 the character. See the description of enum
24575 glyphless_display_method in dispextern.h for the detail.
24576
24577 FOR_NO_FONT is nonzero if and only if this is for a character for
24578 which no font was found. ACRONYM, if non-nil, is an acronym string
24579 for the character. */
24580
24581 static void
24582 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24583 {
24584 int face_id;
24585 struct face *face;
24586 struct font *font;
24587 int base_width, base_height, width, height;
24588 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24589 int len;
24590
24591 /* Get the metrics of the base font. We always refer to the current
24592 ASCII face. */
24593 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24594 font = face->font ? face->font : FRAME_FONT (it->f);
24595 it->ascent = FONT_BASE (font) + font->baseline_offset;
24596 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24597 base_height = it->ascent + it->descent;
24598 base_width = font->average_width;
24599
24600 /* Get a face ID for the glyph by utilizing a cache (the same way as
24601 done for `escape-glyph' in get_next_display_element). */
24602 if (it->f == last_glyphless_glyph_frame
24603 && it->face_id == last_glyphless_glyph_face_id)
24604 {
24605 face_id = last_glyphless_glyph_merged_face_id;
24606 }
24607 else
24608 {
24609 /* Merge the `glyphless-char' face into the current face. */
24610 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
24611 last_glyphless_glyph_frame = it->f;
24612 last_glyphless_glyph_face_id = it->face_id;
24613 last_glyphless_glyph_merged_face_id = face_id;
24614 }
24615
24616 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24617 {
24618 it->pixel_width = THIN_SPACE_WIDTH;
24619 len = 0;
24620 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24621 }
24622 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24623 {
24624 width = CHAR_WIDTH (it->c);
24625 if (width == 0)
24626 width = 1;
24627 else if (width > 4)
24628 width = 4;
24629 it->pixel_width = base_width * width;
24630 len = 0;
24631 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24632 }
24633 else
24634 {
24635 char buf[7];
24636 const char *str;
24637 unsigned int code[6];
24638 int upper_len;
24639 int ascent, descent;
24640 struct font_metrics metrics_upper, metrics_lower;
24641
24642 face = FACE_FROM_ID (it->f, face_id);
24643 font = face->font ? face->font : FRAME_FONT (it->f);
24644 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24645
24646 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24647 {
24648 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
24649 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
24650 if (CONSP (acronym))
24651 acronym = XCAR (acronym);
24652 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24653 }
24654 else
24655 {
24656 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
24657 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
24658 str = buf;
24659 }
24660 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
24661 code[len] = font->driver->encode_char (font, str[len]);
24662 upper_len = (len + 1) / 2;
24663 font->driver->text_extents (font, code, upper_len,
24664 &metrics_upper);
24665 font->driver->text_extents (font, code + upper_len, len - upper_len,
24666 &metrics_lower);
24667
24668
24669
24670 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
24671 width = max (metrics_upper.width, metrics_lower.width) + 4;
24672 upper_xoff = upper_yoff = 2; /* the typical case */
24673 if (base_width >= width)
24674 {
24675 /* Align the upper to the left, the lower to the right. */
24676 it->pixel_width = base_width;
24677 lower_xoff = base_width - 2 - metrics_lower.width;
24678 }
24679 else
24680 {
24681 /* Center the shorter one. */
24682 it->pixel_width = width;
24683 if (metrics_upper.width >= metrics_lower.width)
24684 lower_xoff = (width - metrics_lower.width) / 2;
24685 else
24686 {
24687 /* FIXME: This code doesn't look right. It formerly was
24688 missing the "lower_xoff = 0;", which couldn't have
24689 been right since it left lower_xoff uninitialized. */
24690 lower_xoff = 0;
24691 upper_xoff = (width - metrics_upper.width) / 2;
24692 }
24693 }
24694
24695 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
24696 top, bottom, and between upper and lower strings. */
24697 height = (metrics_upper.ascent + metrics_upper.descent
24698 + metrics_lower.ascent + metrics_lower.descent) + 5;
24699 /* Center vertically.
24700 H:base_height, D:base_descent
24701 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
24702
24703 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
24704 descent = D - H/2 + h/2;
24705 lower_yoff = descent - 2 - ld;
24706 upper_yoff = lower_yoff - la - 1 - ud; */
24707 ascent = - (it->descent - (base_height + height + 1) / 2);
24708 descent = it->descent - (base_height - height) / 2;
24709 lower_yoff = descent - 2 - metrics_lower.descent;
24710 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
24711 - metrics_upper.descent);
24712 /* Don't make the height shorter than the base height. */
24713 if (height > base_height)
24714 {
24715 it->ascent = ascent;
24716 it->descent = descent;
24717 }
24718 }
24719
24720 it->phys_ascent = it->ascent;
24721 it->phys_descent = it->descent;
24722 if (it->glyph_row)
24723 append_glyphless_glyph (it, face_id, for_no_font, len,
24724 upper_xoff, upper_yoff,
24725 lower_xoff, lower_yoff);
24726 it->nglyphs = 1;
24727 take_vertical_position_into_account (it);
24728 }
24729
24730
24731 /* RIF:
24732 Produce glyphs/get display metrics for the display element IT is
24733 loaded with. See the description of struct it in dispextern.h
24734 for an overview of struct it. */
24735
24736 void
24737 x_produce_glyphs (struct it *it)
24738 {
24739 int extra_line_spacing = it->extra_line_spacing;
24740
24741 it->glyph_not_available_p = 0;
24742
24743 if (it->what == IT_CHARACTER)
24744 {
24745 XChar2b char2b;
24746 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24747 struct font *font = face->font;
24748 struct font_metrics *pcm = NULL;
24749 int boff; /* baseline offset */
24750
24751 if (font == NULL)
24752 {
24753 /* When no suitable font is found, display this character by
24754 the method specified in the first extra slot of
24755 Vglyphless_char_display. */
24756 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
24757
24758 eassert (it->what == IT_GLYPHLESS);
24759 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
24760 goto done;
24761 }
24762
24763 boff = font->baseline_offset;
24764 if (font->vertical_centering)
24765 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24766
24767 if (it->char_to_display != '\n' && it->char_to_display != '\t')
24768 {
24769 int stretched_p;
24770
24771 it->nglyphs = 1;
24772
24773 if (it->override_ascent >= 0)
24774 {
24775 it->ascent = it->override_ascent;
24776 it->descent = it->override_descent;
24777 boff = it->override_boff;
24778 }
24779 else
24780 {
24781 it->ascent = FONT_BASE (font) + boff;
24782 it->descent = FONT_DESCENT (font) - boff;
24783 }
24784
24785 if (get_char_glyph_code (it->char_to_display, font, &char2b))
24786 {
24787 pcm = get_per_char_metric (font, &char2b);
24788 if (pcm->width == 0
24789 && pcm->rbearing == 0 && pcm->lbearing == 0)
24790 pcm = NULL;
24791 }
24792
24793 if (pcm)
24794 {
24795 it->phys_ascent = pcm->ascent + boff;
24796 it->phys_descent = pcm->descent - boff;
24797 it->pixel_width = pcm->width;
24798 }
24799 else
24800 {
24801 it->glyph_not_available_p = 1;
24802 it->phys_ascent = it->ascent;
24803 it->phys_descent = it->descent;
24804 it->pixel_width = font->space_width;
24805 }
24806
24807 if (it->constrain_row_ascent_descent_p)
24808 {
24809 if (it->descent > it->max_descent)
24810 {
24811 it->ascent += it->descent - it->max_descent;
24812 it->descent = it->max_descent;
24813 }
24814 if (it->ascent > it->max_ascent)
24815 {
24816 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24817 it->ascent = it->max_ascent;
24818 }
24819 it->phys_ascent = min (it->phys_ascent, it->ascent);
24820 it->phys_descent = min (it->phys_descent, it->descent);
24821 extra_line_spacing = 0;
24822 }
24823
24824 /* If this is a space inside a region of text with
24825 `space-width' property, change its width. */
24826 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
24827 if (stretched_p)
24828 it->pixel_width *= XFLOATINT (it->space_width);
24829
24830 /* If face has a box, add the box thickness to the character
24831 height. If character has a box line to the left and/or
24832 right, add the box line width to the character's width. */
24833 if (face->box != FACE_NO_BOX)
24834 {
24835 int thick = face->box_line_width;
24836
24837 if (thick > 0)
24838 {
24839 it->ascent += thick;
24840 it->descent += thick;
24841 }
24842 else
24843 thick = -thick;
24844
24845 if (it->start_of_box_run_p)
24846 it->pixel_width += thick;
24847 if (it->end_of_box_run_p)
24848 it->pixel_width += thick;
24849 }
24850
24851 /* If face has an overline, add the height of the overline
24852 (1 pixel) and a 1 pixel margin to the character height. */
24853 if (face->overline_p)
24854 it->ascent += overline_margin;
24855
24856 if (it->constrain_row_ascent_descent_p)
24857 {
24858 if (it->ascent > it->max_ascent)
24859 it->ascent = it->max_ascent;
24860 if (it->descent > it->max_descent)
24861 it->descent = it->max_descent;
24862 }
24863
24864 take_vertical_position_into_account (it);
24865
24866 /* If we have to actually produce glyphs, do it. */
24867 if (it->glyph_row)
24868 {
24869 if (stretched_p)
24870 {
24871 /* Translate a space with a `space-width' property
24872 into a stretch glyph. */
24873 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
24874 / FONT_HEIGHT (font));
24875 append_stretch_glyph (it, it->object, it->pixel_width,
24876 it->ascent + it->descent, ascent);
24877 }
24878 else
24879 append_glyph (it);
24880
24881 /* If characters with lbearing or rbearing are displayed
24882 in this line, record that fact in a flag of the
24883 glyph row. This is used to optimize X output code. */
24884 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
24885 it->glyph_row->contains_overlapping_glyphs_p = 1;
24886 }
24887 if (! stretched_p && it->pixel_width == 0)
24888 /* We assure that all visible glyphs have at least 1-pixel
24889 width. */
24890 it->pixel_width = 1;
24891 }
24892 else if (it->char_to_display == '\n')
24893 {
24894 /* A newline has no width, but we need the height of the
24895 line. But if previous part of the line sets a height,
24896 don't increase that height */
24897
24898 Lisp_Object height;
24899 Lisp_Object total_height = Qnil;
24900
24901 it->override_ascent = -1;
24902 it->pixel_width = 0;
24903 it->nglyphs = 0;
24904
24905 height = get_it_property (it, Qline_height);
24906 /* Split (line-height total-height) list */
24907 if (CONSP (height)
24908 && CONSP (XCDR (height))
24909 && NILP (XCDR (XCDR (height))))
24910 {
24911 total_height = XCAR (XCDR (height));
24912 height = XCAR (height);
24913 }
24914 height = calc_line_height_property (it, height, font, boff, 1);
24915
24916 if (it->override_ascent >= 0)
24917 {
24918 it->ascent = it->override_ascent;
24919 it->descent = it->override_descent;
24920 boff = it->override_boff;
24921 }
24922 else
24923 {
24924 it->ascent = FONT_BASE (font) + boff;
24925 it->descent = FONT_DESCENT (font) - boff;
24926 }
24927
24928 if (EQ (height, Qt))
24929 {
24930 if (it->descent > it->max_descent)
24931 {
24932 it->ascent += it->descent - it->max_descent;
24933 it->descent = it->max_descent;
24934 }
24935 if (it->ascent > it->max_ascent)
24936 {
24937 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24938 it->ascent = it->max_ascent;
24939 }
24940 it->phys_ascent = min (it->phys_ascent, it->ascent);
24941 it->phys_descent = min (it->phys_descent, it->descent);
24942 it->constrain_row_ascent_descent_p = 1;
24943 extra_line_spacing = 0;
24944 }
24945 else
24946 {
24947 Lisp_Object spacing;
24948
24949 it->phys_ascent = it->ascent;
24950 it->phys_descent = it->descent;
24951
24952 if ((it->max_ascent > 0 || it->max_descent > 0)
24953 && face->box != FACE_NO_BOX
24954 && face->box_line_width > 0)
24955 {
24956 it->ascent += face->box_line_width;
24957 it->descent += face->box_line_width;
24958 }
24959 if (!NILP (height)
24960 && XINT (height) > it->ascent + it->descent)
24961 it->ascent = XINT (height) - it->descent;
24962
24963 if (!NILP (total_height))
24964 spacing = calc_line_height_property (it, total_height, font, boff, 0);
24965 else
24966 {
24967 spacing = get_it_property (it, Qline_spacing);
24968 spacing = calc_line_height_property (it, spacing, font, boff, 0);
24969 }
24970 if (INTEGERP (spacing))
24971 {
24972 extra_line_spacing = XINT (spacing);
24973 if (!NILP (total_height))
24974 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
24975 }
24976 }
24977 }
24978 else /* i.e. (it->char_to_display == '\t') */
24979 {
24980 if (font->space_width > 0)
24981 {
24982 int tab_width = it->tab_width * font->space_width;
24983 int x = it->current_x + it->continuation_lines_width;
24984 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
24985
24986 /* If the distance from the current position to the next tab
24987 stop is less than a space character width, use the
24988 tab stop after that. */
24989 if (next_tab_x - x < font->space_width)
24990 next_tab_x += tab_width;
24991
24992 it->pixel_width = next_tab_x - x;
24993 it->nglyphs = 1;
24994 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
24995 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
24996
24997 if (it->glyph_row)
24998 {
24999 append_stretch_glyph (it, it->object, it->pixel_width,
25000 it->ascent + it->descent, it->ascent);
25001 }
25002 }
25003 else
25004 {
25005 it->pixel_width = 0;
25006 it->nglyphs = 1;
25007 }
25008 }
25009 }
25010 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
25011 {
25012 /* A static composition.
25013
25014 Note: A composition is represented as one glyph in the
25015 glyph matrix. There are no padding glyphs.
25016
25017 Important note: pixel_width, ascent, and descent are the
25018 values of what is drawn by draw_glyphs (i.e. the values of
25019 the overall glyphs composed). */
25020 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25021 int boff; /* baseline offset */
25022 struct composition *cmp = composition_table[it->cmp_it.id];
25023 int glyph_len = cmp->glyph_len;
25024 struct font *font = face->font;
25025
25026 it->nglyphs = 1;
25027
25028 /* If we have not yet calculated pixel size data of glyphs of
25029 the composition for the current face font, calculate them
25030 now. Theoretically, we have to check all fonts for the
25031 glyphs, but that requires much time and memory space. So,
25032 here we check only the font of the first glyph. This may
25033 lead to incorrect display, but it's very rare, and C-l
25034 (recenter-top-bottom) can correct the display anyway. */
25035 if (! cmp->font || cmp->font != font)
25036 {
25037 /* Ascent and descent of the font of the first character
25038 of this composition (adjusted by baseline offset).
25039 Ascent and descent of overall glyphs should not be less
25040 than these, respectively. */
25041 int font_ascent, font_descent, font_height;
25042 /* Bounding box of the overall glyphs. */
25043 int leftmost, rightmost, lowest, highest;
25044 int lbearing, rbearing;
25045 int i, width, ascent, descent;
25046 int left_padded = 0, right_padded = 0;
25047 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
25048 XChar2b char2b;
25049 struct font_metrics *pcm;
25050 int font_not_found_p;
25051 ptrdiff_t pos;
25052
25053 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
25054 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
25055 break;
25056 if (glyph_len < cmp->glyph_len)
25057 right_padded = 1;
25058 for (i = 0; i < glyph_len; i++)
25059 {
25060 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
25061 break;
25062 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25063 }
25064 if (i > 0)
25065 left_padded = 1;
25066
25067 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
25068 : IT_CHARPOS (*it));
25069 /* If no suitable font is found, use the default font. */
25070 font_not_found_p = font == NULL;
25071 if (font_not_found_p)
25072 {
25073 face = face->ascii_face;
25074 font = face->font;
25075 }
25076 boff = font->baseline_offset;
25077 if (font->vertical_centering)
25078 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25079 font_ascent = FONT_BASE (font) + boff;
25080 font_descent = FONT_DESCENT (font) - boff;
25081 font_height = FONT_HEIGHT (font);
25082
25083 cmp->font = font;
25084
25085 pcm = NULL;
25086 if (! font_not_found_p)
25087 {
25088 get_char_face_and_encoding (it->f, c, it->face_id,
25089 &char2b, 0);
25090 pcm = get_per_char_metric (font, &char2b);
25091 }
25092
25093 /* Initialize the bounding box. */
25094 if (pcm)
25095 {
25096 width = cmp->glyph_len > 0 ? pcm->width : 0;
25097 ascent = pcm->ascent;
25098 descent = pcm->descent;
25099 lbearing = pcm->lbearing;
25100 rbearing = pcm->rbearing;
25101 }
25102 else
25103 {
25104 width = cmp->glyph_len > 0 ? font->space_width : 0;
25105 ascent = FONT_BASE (font);
25106 descent = FONT_DESCENT (font);
25107 lbearing = 0;
25108 rbearing = width;
25109 }
25110
25111 rightmost = width;
25112 leftmost = 0;
25113 lowest = - descent + boff;
25114 highest = ascent + boff;
25115
25116 if (! font_not_found_p
25117 && font->default_ascent
25118 && CHAR_TABLE_P (Vuse_default_ascent)
25119 && !NILP (Faref (Vuse_default_ascent,
25120 make_number (it->char_to_display))))
25121 highest = font->default_ascent + boff;
25122
25123 /* Draw the first glyph at the normal position. It may be
25124 shifted to right later if some other glyphs are drawn
25125 at the left. */
25126 cmp->offsets[i * 2] = 0;
25127 cmp->offsets[i * 2 + 1] = boff;
25128 cmp->lbearing = lbearing;
25129 cmp->rbearing = rbearing;
25130
25131 /* Set cmp->offsets for the remaining glyphs. */
25132 for (i++; i < glyph_len; i++)
25133 {
25134 int left, right, btm, top;
25135 int ch = COMPOSITION_GLYPH (cmp, i);
25136 int face_id;
25137 struct face *this_face;
25138
25139 if (ch == '\t')
25140 ch = ' ';
25141 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25142 this_face = FACE_FROM_ID (it->f, face_id);
25143 font = this_face->font;
25144
25145 if (font == NULL)
25146 pcm = NULL;
25147 else
25148 {
25149 get_char_face_and_encoding (it->f, ch, face_id,
25150 &char2b, 0);
25151 pcm = get_per_char_metric (font, &char2b);
25152 }
25153 if (! pcm)
25154 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25155 else
25156 {
25157 width = pcm->width;
25158 ascent = pcm->ascent;
25159 descent = pcm->descent;
25160 lbearing = pcm->lbearing;
25161 rbearing = pcm->rbearing;
25162 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25163 {
25164 /* Relative composition with or without
25165 alternate chars. */
25166 left = (leftmost + rightmost - width) / 2;
25167 btm = - descent + boff;
25168 if (font->relative_compose
25169 && (! CHAR_TABLE_P (Vignore_relative_composition)
25170 || NILP (Faref (Vignore_relative_composition,
25171 make_number (ch)))))
25172 {
25173
25174 if (- descent >= font->relative_compose)
25175 /* One extra pixel between two glyphs. */
25176 btm = highest + 1;
25177 else if (ascent <= 0)
25178 /* One extra pixel between two glyphs. */
25179 btm = lowest - 1 - ascent - descent;
25180 }
25181 }
25182 else
25183 {
25184 /* A composition rule is specified by an integer
25185 value that encodes global and new reference
25186 points (GREF and NREF). GREF and NREF are
25187 specified by numbers as below:
25188
25189 0---1---2 -- ascent
25190 | |
25191 | |
25192 | |
25193 9--10--11 -- center
25194 | |
25195 ---3---4---5--- baseline
25196 | |
25197 6---7---8 -- descent
25198 */
25199 int rule = COMPOSITION_RULE (cmp, i);
25200 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25201
25202 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25203 grefx = gref % 3, nrefx = nref % 3;
25204 grefy = gref / 3, nrefy = nref / 3;
25205 if (xoff)
25206 xoff = font_height * (xoff - 128) / 256;
25207 if (yoff)
25208 yoff = font_height * (yoff - 128) / 256;
25209
25210 left = (leftmost
25211 + grefx * (rightmost - leftmost) / 2
25212 - nrefx * width / 2
25213 + xoff);
25214
25215 btm = ((grefy == 0 ? highest
25216 : grefy == 1 ? 0
25217 : grefy == 2 ? lowest
25218 : (highest + lowest) / 2)
25219 - (nrefy == 0 ? ascent + descent
25220 : nrefy == 1 ? descent - boff
25221 : nrefy == 2 ? 0
25222 : (ascent + descent) / 2)
25223 + yoff);
25224 }
25225
25226 cmp->offsets[i * 2] = left;
25227 cmp->offsets[i * 2 + 1] = btm + descent;
25228
25229 /* Update the bounding box of the overall glyphs. */
25230 if (width > 0)
25231 {
25232 right = left + width;
25233 if (left < leftmost)
25234 leftmost = left;
25235 if (right > rightmost)
25236 rightmost = right;
25237 }
25238 top = btm + descent + ascent;
25239 if (top > highest)
25240 highest = top;
25241 if (btm < lowest)
25242 lowest = btm;
25243
25244 if (cmp->lbearing > left + lbearing)
25245 cmp->lbearing = left + lbearing;
25246 if (cmp->rbearing < left + rbearing)
25247 cmp->rbearing = left + rbearing;
25248 }
25249 }
25250
25251 /* If there are glyphs whose x-offsets are negative,
25252 shift all glyphs to the right and make all x-offsets
25253 non-negative. */
25254 if (leftmost < 0)
25255 {
25256 for (i = 0; i < cmp->glyph_len; i++)
25257 cmp->offsets[i * 2] -= leftmost;
25258 rightmost -= leftmost;
25259 cmp->lbearing -= leftmost;
25260 cmp->rbearing -= leftmost;
25261 }
25262
25263 if (left_padded && cmp->lbearing < 0)
25264 {
25265 for (i = 0; i < cmp->glyph_len; i++)
25266 cmp->offsets[i * 2] -= cmp->lbearing;
25267 rightmost -= cmp->lbearing;
25268 cmp->rbearing -= cmp->lbearing;
25269 cmp->lbearing = 0;
25270 }
25271 if (right_padded && rightmost < cmp->rbearing)
25272 {
25273 rightmost = cmp->rbearing;
25274 }
25275
25276 cmp->pixel_width = rightmost;
25277 cmp->ascent = highest;
25278 cmp->descent = - lowest;
25279 if (cmp->ascent < font_ascent)
25280 cmp->ascent = font_ascent;
25281 if (cmp->descent < font_descent)
25282 cmp->descent = font_descent;
25283 }
25284
25285 if (it->glyph_row
25286 && (cmp->lbearing < 0
25287 || cmp->rbearing > cmp->pixel_width))
25288 it->glyph_row->contains_overlapping_glyphs_p = 1;
25289
25290 it->pixel_width = cmp->pixel_width;
25291 it->ascent = it->phys_ascent = cmp->ascent;
25292 it->descent = it->phys_descent = cmp->descent;
25293 if (face->box != FACE_NO_BOX)
25294 {
25295 int thick = face->box_line_width;
25296
25297 if (thick > 0)
25298 {
25299 it->ascent += thick;
25300 it->descent += thick;
25301 }
25302 else
25303 thick = - thick;
25304
25305 if (it->start_of_box_run_p)
25306 it->pixel_width += thick;
25307 if (it->end_of_box_run_p)
25308 it->pixel_width += thick;
25309 }
25310
25311 /* If face has an overline, add the height of the overline
25312 (1 pixel) and a 1 pixel margin to the character height. */
25313 if (face->overline_p)
25314 it->ascent += overline_margin;
25315
25316 take_vertical_position_into_account (it);
25317 if (it->ascent < 0)
25318 it->ascent = 0;
25319 if (it->descent < 0)
25320 it->descent = 0;
25321
25322 if (it->glyph_row && cmp->glyph_len > 0)
25323 append_composite_glyph (it);
25324 }
25325 else if (it->what == IT_COMPOSITION)
25326 {
25327 /* A dynamic (automatic) composition. */
25328 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25329 Lisp_Object gstring;
25330 struct font_metrics metrics;
25331
25332 it->nglyphs = 1;
25333
25334 gstring = composition_gstring_from_id (it->cmp_it.id);
25335 it->pixel_width
25336 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25337 &metrics);
25338 if (it->glyph_row
25339 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25340 it->glyph_row->contains_overlapping_glyphs_p = 1;
25341 it->ascent = it->phys_ascent = metrics.ascent;
25342 it->descent = it->phys_descent = metrics.descent;
25343 if (face->box != FACE_NO_BOX)
25344 {
25345 int thick = face->box_line_width;
25346
25347 if (thick > 0)
25348 {
25349 it->ascent += thick;
25350 it->descent += thick;
25351 }
25352 else
25353 thick = - thick;
25354
25355 if (it->start_of_box_run_p)
25356 it->pixel_width += thick;
25357 if (it->end_of_box_run_p)
25358 it->pixel_width += thick;
25359 }
25360 /* If face has an overline, add the height of the overline
25361 (1 pixel) and a 1 pixel margin to the character height. */
25362 if (face->overline_p)
25363 it->ascent += overline_margin;
25364 take_vertical_position_into_account (it);
25365 if (it->ascent < 0)
25366 it->ascent = 0;
25367 if (it->descent < 0)
25368 it->descent = 0;
25369
25370 if (it->glyph_row)
25371 append_composite_glyph (it);
25372 }
25373 else if (it->what == IT_GLYPHLESS)
25374 produce_glyphless_glyph (it, 0, Qnil);
25375 else if (it->what == IT_IMAGE)
25376 produce_image_glyph (it);
25377 else if (it->what == IT_STRETCH)
25378 produce_stretch_glyph (it);
25379
25380 done:
25381 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25382 because this isn't true for images with `:ascent 100'. */
25383 eassert (it->ascent >= 0 && it->descent >= 0);
25384 if (it->area == TEXT_AREA)
25385 it->current_x += it->pixel_width;
25386
25387 if (extra_line_spacing > 0)
25388 {
25389 it->descent += extra_line_spacing;
25390 if (extra_line_spacing > it->max_extra_line_spacing)
25391 it->max_extra_line_spacing = extra_line_spacing;
25392 }
25393
25394 it->max_ascent = max (it->max_ascent, it->ascent);
25395 it->max_descent = max (it->max_descent, it->descent);
25396 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25397 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25398 }
25399
25400 /* EXPORT for RIF:
25401 Output LEN glyphs starting at START at the nominal cursor position.
25402 Advance the nominal cursor over the text. The global variable
25403 updated_window contains the window being updated, updated_row is
25404 the glyph row being updated, and updated_area is the area of that
25405 row being updated. */
25406
25407 void
25408 x_write_glyphs (struct glyph *start, int len)
25409 {
25410 int x, hpos, chpos = updated_window->phys_cursor.hpos;
25411
25412 eassert (updated_window && updated_row);
25413 /* When the window is hscrolled, cursor hpos can legitimately be out
25414 of bounds, but we draw the cursor at the corresponding window
25415 margin in that case. */
25416 if (!updated_row->reversed_p && chpos < 0)
25417 chpos = 0;
25418 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25419 chpos = updated_row->used[TEXT_AREA] - 1;
25420
25421 block_input ();
25422
25423 /* Write glyphs. */
25424
25425 hpos = start - updated_row->glyphs[updated_area];
25426 x = draw_glyphs (updated_window, output_cursor.x,
25427 updated_row, updated_area,
25428 hpos, hpos + len,
25429 DRAW_NORMAL_TEXT, 0);
25430
25431 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25432 if (updated_area == TEXT_AREA
25433 && updated_window->phys_cursor_on_p
25434 && updated_window->phys_cursor.vpos == output_cursor.vpos
25435 && chpos >= hpos
25436 && chpos < hpos + len)
25437 updated_window->phys_cursor_on_p = 0;
25438
25439 unblock_input ();
25440
25441 /* Advance the output cursor. */
25442 output_cursor.hpos += len;
25443 output_cursor.x = x;
25444 }
25445
25446
25447 /* EXPORT for RIF:
25448 Insert LEN glyphs from START at the nominal cursor position. */
25449
25450 void
25451 x_insert_glyphs (struct glyph *start, int len)
25452 {
25453 struct frame *f;
25454 struct window *w;
25455 int line_height, shift_by_width, shifted_region_width;
25456 struct glyph_row *row;
25457 struct glyph *glyph;
25458 int frame_x, frame_y;
25459 ptrdiff_t hpos;
25460
25461 eassert (updated_window && updated_row);
25462 block_input ();
25463 w = updated_window;
25464 f = XFRAME (WINDOW_FRAME (w));
25465
25466 /* Get the height of the line we are in. */
25467 row = updated_row;
25468 line_height = row->height;
25469
25470 /* Get the width of the glyphs to insert. */
25471 shift_by_width = 0;
25472 for (glyph = start; glyph < start + len; ++glyph)
25473 shift_by_width += glyph->pixel_width;
25474
25475 /* Get the width of the region to shift right. */
25476 shifted_region_width = (window_box_width (w, updated_area)
25477 - output_cursor.x
25478 - shift_by_width);
25479
25480 /* Shift right. */
25481 frame_x = window_box_left (w, updated_area) + output_cursor.x;
25482 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
25483
25484 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25485 line_height, shift_by_width);
25486
25487 /* Write the glyphs. */
25488 hpos = start - row->glyphs[updated_area];
25489 draw_glyphs (w, output_cursor.x, row, updated_area,
25490 hpos, hpos + len,
25491 DRAW_NORMAL_TEXT, 0);
25492
25493 /* Advance the output cursor. */
25494 output_cursor.hpos += len;
25495 output_cursor.x += shift_by_width;
25496 unblock_input ();
25497 }
25498
25499
25500 /* EXPORT for RIF:
25501 Erase the current text line from the nominal cursor position
25502 (inclusive) to pixel column TO_X (exclusive). The idea is that
25503 everything from TO_X onward is already erased.
25504
25505 TO_X is a pixel position relative to updated_area of
25506 updated_window. TO_X == -1 means clear to the end of this area. */
25507
25508 void
25509 x_clear_end_of_line (int to_x)
25510 {
25511 struct frame *f;
25512 struct window *w = updated_window;
25513 int max_x, min_y, max_y;
25514 int from_x, from_y, to_y;
25515
25516 eassert (updated_window && updated_row);
25517 f = XFRAME (w->frame);
25518
25519 if (updated_row->full_width_p)
25520 max_x = WINDOW_TOTAL_WIDTH (w);
25521 else
25522 max_x = window_box_width (w, updated_area);
25523 max_y = window_text_bottom_y (w);
25524
25525 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25526 of window. For TO_X > 0, truncate to end of drawing area. */
25527 if (to_x == 0)
25528 return;
25529 else if (to_x < 0)
25530 to_x = max_x;
25531 else
25532 to_x = min (to_x, max_x);
25533
25534 to_y = min (max_y, output_cursor.y + updated_row->height);
25535
25536 /* Notice if the cursor will be cleared by this operation. */
25537 if (!updated_row->full_width_p)
25538 notice_overwritten_cursor (w, updated_area,
25539 output_cursor.x, -1,
25540 updated_row->y,
25541 MATRIX_ROW_BOTTOM_Y (updated_row));
25542
25543 from_x = output_cursor.x;
25544
25545 /* Translate to frame coordinates. */
25546 if (updated_row->full_width_p)
25547 {
25548 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25549 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25550 }
25551 else
25552 {
25553 int area_left = window_box_left (w, updated_area);
25554 from_x += area_left;
25555 to_x += area_left;
25556 }
25557
25558 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25559 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
25560 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25561
25562 /* Prevent inadvertently clearing to end of the X window. */
25563 if (to_x > from_x && to_y > from_y)
25564 {
25565 block_input ();
25566 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25567 to_x - from_x, to_y - from_y);
25568 unblock_input ();
25569 }
25570 }
25571
25572 #endif /* HAVE_WINDOW_SYSTEM */
25573
25574
25575 \f
25576 /***********************************************************************
25577 Cursor types
25578 ***********************************************************************/
25579
25580 /* Value is the internal representation of the specified cursor type
25581 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25582 of the bar cursor. */
25583
25584 static enum text_cursor_kinds
25585 get_specified_cursor_type (Lisp_Object arg, int *width)
25586 {
25587 enum text_cursor_kinds type;
25588
25589 if (NILP (arg))
25590 return NO_CURSOR;
25591
25592 if (EQ (arg, Qbox))
25593 return FILLED_BOX_CURSOR;
25594
25595 if (EQ (arg, Qhollow))
25596 return HOLLOW_BOX_CURSOR;
25597
25598 if (EQ (arg, Qbar))
25599 {
25600 *width = 2;
25601 return BAR_CURSOR;
25602 }
25603
25604 if (CONSP (arg)
25605 && EQ (XCAR (arg), Qbar)
25606 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25607 {
25608 *width = XINT (XCDR (arg));
25609 return BAR_CURSOR;
25610 }
25611
25612 if (EQ (arg, Qhbar))
25613 {
25614 *width = 2;
25615 return HBAR_CURSOR;
25616 }
25617
25618 if (CONSP (arg)
25619 && EQ (XCAR (arg), Qhbar)
25620 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25621 {
25622 *width = XINT (XCDR (arg));
25623 return HBAR_CURSOR;
25624 }
25625
25626 /* Treat anything unknown as "hollow box cursor".
25627 It was bad to signal an error; people have trouble fixing
25628 .Xdefaults with Emacs, when it has something bad in it. */
25629 type = HOLLOW_BOX_CURSOR;
25630
25631 return type;
25632 }
25633
25634 /* Set the default cursor types for specified frame. */
25635 void
25636 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25637 {
25638 int width = 1;
25639 Lisp_Object tem;
25640
25641 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25642 FRAME_CURSOR_WIDTH (f) = width;
25643
25644 /* By default, set up the blink-off state depending on the on-state. */
25645
25646 tem = Fassoc (arg, Vblink_cursor_alist);
25647 if (!NILP (tem))
25648 {
25649 FRAME_BLINK_OFF_CURSOR (f)
25650 = get_specified_cursor_type (XCDR (tem), &width);
25651 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25652 }
25653 else
25654 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25655 }
25656
25657
25658 #ifdef HAVE_WINDOW_SYSTEM
25659
25660 /* Return the cursor we want to be displayed in window W. Return
25661 width of bar/hbar cursor through WIDTH arg. Return with
25662 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
25663 (i.e. if the `system caret' should track this cursor).
25664
25665 In a mini-buffer window, we want the cursor only to appear if we
25666 are reading input from this window. For the selected window, we
25667 want the cursor type given by the frame parameter or buffer local
25668 setting of cursor-type. If explicitly marked off, draw no cursor.
25669 In all other cases, we want a hollow box cursor. */
25670
25671 static enum text_cursor_kinds
25672 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
25673 int *active_cursor)
25674 {
25675 struct frame *f = XFRAME (w->frame);
25676 struct buffer *b = XBUFFER (w->buffer);
25677 int cursor_type = DEFAULT_CURSOR;
25678 Lisp_Object alt_cursor;
25679 int non_selected = 0;
25680
25681 *active_cursor = 1;
25682
25683 /* Echo area */
25684 if (cursor_in_echo_area
25685 && FRAME_HAS_MINIBUF_P (f)
25686 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
25687 {
25688 if (w == XWINDOW (echo_area_window))
25689 {
25690 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
25691 {
25692 *width = FRAME_CURSOR_WIDTH (f);
25693 return FRAME_DESIRED_CURSOR (f);
25694 }
25695 else
25696 return get_specified_cursor_type (BVAR (b, cursor_type), width);
25697 }
25698
25699 *active_cursor = 0;
25700 non_selected = 1;
25701 }
25702
25703 /* Detect a nonselected window or nonselected frame. */
25704 else if (w != XWINDOW (f->selected_window)
25705 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
25706 {
25707 *active_cursor = 0;
25708
25709 if (MINI_WINDOW_P (w) && minibuf_level == 0)
25710 return NO_CURSOR;
25711
25712 non_selected = 1;
25713 }
25714
25715 /* Never display a cursor in a window in which cursor-type is nil. */
25716 if (NILP (BVAR (b, cursor_type)))
25717 return NO_CURSOR;
25718
25719 /* Get the normal cursor type for this window. */
25720 if (EQ (BVAR (b, cursor_type), Qt))
25721 {
25722 cursor_type = FRAME_DESIRED_CURSOR (f);
25723 *width = FRAME_CURSOR_WIDTH (f);
25724 }
25725 else
25726 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
25727
25728 /* Use cursor-in-non-selected-windows instead
25729 for non-selected window or frame. */
25730 if (non_selected)
25731 {
25732 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
25733 if (!EQ (Qt, alt_cursor))
25734 return get_specified_cursor_type (alt_cursor, width);
25735 /* t means modify the normal cursor type. */
25736 if (cursor_type == FILLED_BOX_CURSOR)
25737 cursor_type = HOLLOW_BOX_CURSOR;
25738 else if (cursor_type == BAR_CURSOR && *width > 1)
25739 --*width;
25740 return cursor_type;
25741 }
25742
25743 /* Use normal cursor if not blinked off. */
25744 if (!w->cursor_off_p)
25745 {
25746 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25747 {
25748 if (cursor_type == FILLED_BOX_CURSOR)
25749 {
25750 /* Using a block cursor on large images can be very annoying.
25751 So use a hollow cursor for "large" images.
25752 If image is not transparent (no mask), also use hollow cursor. */
25753 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25754 if (img != NULL && IMAGEP (img->spec))
25755 {
25756 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
25757 where N = size of default frame font size.
25758 This should cover most of the "tiny" icons people may use. */
25759 if (!img->mask
25760 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
25761 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
25762 cursor_type = HOLLOW_BOX_CURSOR;
25763 }
25764 }
25765 else if (cursor_type != NO_CURSOR)
25766 {
25767 /* Display current only supports BOX and HOLLOW cursors for images.
25768 So for now, unconditionally use a HOLLOW cursor when cursor is
25769 not a solid box cursor. */
25770 cursor_type = HOLLOW_BOX_CURSOR;
25771 }
25772 }
25773 return cursor_type;
25774 }
25775
25776 /* Cursor is blinked off, so determine how to "toggle" it. */
25777
25778 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
25779 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
25780 return get_specified_cursor_type (XCDR (alt_cursor), width);
25781
25782 /* Then see if frame has specified a specific blink off cursor type. */
25783 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
25784 {
25785 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
25786 return FRAME_BLINK_OFF_CURSOR (f);
25787 }
25788
25789 #if 0
25790 /* Some people liked having a permanently visible blinking cursor,
25791 while others had very strong opinions against it. So it was
25792 decided to remove it. KFS 2003-09-03 */
25793
25794 /* Finally perform built-in cursor blinking:
25795 filled box <-> hollow box
25796 wide [h]bar <-> narrow [h]bar
25797 narrow [h]bar <-> no cursor
25798 other type <-> no cursor */
25799
25800 if (cursor_type == FILLED_BOX_CURSOR)
25801 return HOLLOW_BOX_CURSOR;
25802
25803 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
25804 {
25805 *width = 1;
25806 return cursor_type;
25807 }
25808 #endif
25809
25810 return NO_CURSOR;
25811 }
25812
25813
25814 /* Notice when the text cursor of window W has been completely
25815 overwritten by a drawing operation that outputs glyphs in AREA
25816 starting at X0 and ending at X1 in the line starting at Y0 and
25817 ending at Y1. X coordinates are area-relative. X1 < 0 means all
25818 the rest of the line after X0 has been written. Y coordinates
25819 are window-relative. */
25820
25821 static void
25822 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
25823 int x0, int x1, int y0, int y1)
25824 {
25825 int cx0, cx1, cy0, cy1;
25826 struct glyph_row *row;
25827
25828 if (!w->phys_cursor_on_p)
25829 return;
25830 if (area != TEXT_AREA)
25831 return;
25832
25833 if (w->phys_cursor.vpos < 0
25834 || w->phys_cursor.vpos >= w->current_matrix->nrows
25835 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
25836 !(row->enabled_p && row->displays_text_p)))
25837 return;
25838
25839 if (row->cursor_in_fringe_p)
25840 {
25841 row->cursor_in_fringe_p = 0;
25842 draw_fringe_bitmap (w, row, row->reversed_p);
25843 w->phys_cursor_on_p = 0;
25844 return;
25845 }
25846
25847 cx0 = w->phys_cursor.x;
25848 cx1 = cx0 + w->phys_cursor_width;
25849 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
25850 return;
25851
25852 /* The cursor image will be completely removed from the
25853 screen if the output area intersects the cursor area in
25854 y-direction. When we draw in [y0 y1[, and some part of
25855 the cursor is at y < y0, that part must have been drawn
25856 before. When scrolling, the cursor is erased before
25857 actually scrolling, so we don't come here. When not
25858 scrolling, the rows above the old cursor row must have
25859 changed, and in this case these rows must have written
25860 over the cursor image.
25861
25862 Likewise if part of the cursor is below y1, with the
25863 exception of the cursor being in the first blank row at
25864 the buffer and window end because update_text_area
25865 doesn't draw that row. (Except when it does, but
25866 that's handled in update_text_area.) */
25867
25868 cy0 = w->phys_cursor.y;
25869 cy1 = cy0 + w->phys_cursor_height;
25870 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
25871 return;
25872
25873 w->phys_cursor_on_p = 0;
25874 }
25875
25876 #endif /* HAVE_WINDOW_SYSTEM */
25877
25878 \f
25879 /************************************************************************
25880 Mouse Face
25881 ************************************************************************/
25882
25883 #ifdef HAVE_WINDOW_SYSTEM
25884
25885 /* EXPORT for RIF:
25886 Fix the display of area AREA of overlapping row ROW in window W
25887 with respect to the overlapping part OVERLAPS. */
25888
25889 void
25890 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
25891 enum glyph_row_area area, int overlaps)
25892 {
25893 int i, x;
25894
25895 block_input ();
25896
25897 x = 0;
25898 for (i = 0; i < row->used[area];)
25899 {
25900 if (row->glyphs[area][i].overlaps_vertically_p)
25901 {
25902 int start = i, start_x = x;
25903
25904 do
25905 {
25906 x += row->glyphs[area][i].pixel_width;
25907 ++i;
25908 }
25909 while (i < row->used[area]
25910 && row->glyphs[area][i].overlaps_vertically_p);
25911
25912 draw_glyphs (w, start_x, row, area,
25913 start, i,
25914 DRAW_NORMAL_TEXT, overlaps);
25915 }
25916 else
25917 {
25918 x += row->glyphs[area][i].pixel_width;
25919 ++i;
25920 }
25921 }
25922
25923 unblock_input ();
25924 }
25925
25926
25927 /* EXPORT:
25928 Draw the cursor glyph of window W in glyph row ROW. See the
25929 comment of draw_glyphs for the meaning of HL. */
25930
25931 void
25932 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
25933 enum draw_glyphs_face hl)
25934 {
25935 /* If cursor hpos is out of bounds, don't draw garbage. This can
25936 happen in mini-buffer windows when switching between echo area
25937 glyphs and mini-buffer. */
25938 if ((row->reversed_p
25939 ? (w->phys_cursor.hpos >= 0)
25940 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
25941 {
25942 int on_p = w->phys_cursor_on_p;
25943 int x1;
25944 int hpos = w->phys_cursor.hpos;
25945
25946 /* When the window is hscrolled, cursor hpos can legitimately be
25947 out of bounds, but we draw the cursor at the corresponding
25948 window margin in that case. */
25949 if (!row->reversed_p && hpos < 0)
25950 hpos = 0;
25951 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25952 hpos = row->used[TEXT_AREA] - 1;
25953
25954 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
25955 hl, 0);
25956 w->phys_cursor_on_p = on_p;
25957
25958 if (hl == DRAW_CURSOR)
25959 w->phys_cursor_width = x1 - w->phys_cursor.x;
25960 /* When we erase the cursor, and ROW is overlapped by other
25961 rows, make sure that these overlapping parts of other rows
25962 are redrawn. */
25963 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
25964 {
25965 w->phys_cursor_width = x1 - w->phys_cursor.x;
25966
25967 if (row > w->current_matrix->rows
25968 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
25969 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
25970 OVERLAPS_ERASED_CURSOR);
25971
25972 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
25973 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
25974 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
25975 OVERLAPS_ERASED_CURSOR);
25976 }
25977 }
25978 }
25979
25980
25981 /* EXPORT:
25982 Erase the image of a cursor of window W from the screen. */
25983
25984 void
25985 erase_phys_cursor (struct window *w)
25986 {
25987 struct frame *f = XFRAME (w->frame);
25988 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25989 int hpos = w->phys_cursor.hpos;
25990 int vpos = w->phys_cursor.vpos;
25991 int mouse_face_here_p = 0;
25992 struct glyph_matrix *active_glyphs = w->current_matrix;
25993 struct glyph_row *cursor_row;
25994 struct glyph *cursor_glyph;
25995 enum draw_glyphs_face hl;
25996
25997 /* No cursor displayed or row invalidated => nothing to do on the
25998 screen. */
25999 if (w->phys_cursor_type == NO_CURSOR)
26000 goto mark_cursor_off;
26001
26002 /* VPOS >= active_glyphs->nrows means that window has been resized.
26003 Don't bother to erase the cursor. */
26004 if (vpos >= active_glyphs->nrows)
26005 goto mark_cursor_off;
26006
26007 /* If row containing cursor is marked invalid, there is nothing we
26008 can do. */
26009 cursor_row = MATRIX_ROW (active_glyphs, vpos);
26010 if (!cursor_row->enabled_p)
26011 goto mark_cursor_off;
26012
26013 /* If line spacing is > 0, old cursor may only be partially visible in
26014 window after split-window. So adjust visible height. */
26015 cursor_row->visible_height = min (cursor_row->visible_height,
26016 window_text_bottom_y (w) - cursor_row->y);
26017
26018 /* If row is completely invisible, don't attempt to delete a cursor which
26019 isn't there. This can happen if cursor is at top of a window, and
26020 we switch to a buffer with a header line in that window. */
26021 if (cursor_row->visible_height <= 0)
26022 goto mark_cursor_off;
26023
26024 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
26025 if (cursor_row->cursor_in_fringe_p)
26026 {
26027 cursor_row->cursor_in_fringe_p = 0;
26028 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
26029 goto mark_cursor_off;
26030 }
26031
26032 /* This can happen when the new row is shorter than the old one.
26033 In this case, either draw_glyphs or clear_end_of_line
26034 should have cleared the cursor. Note that we wouldn't be
26035 able to erase the cursor in this case because we don't have a
26036 cursor glyph at hand. */
26037 if ((cursor_row->reversed_p
26038 ? (w->phys_cursor.hpos < 0)
26039 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
26040 goto mark_cursor_off;
26041
26042 /* When the window is hscrolled, cursor hpos can legitimately be out
26043 of bounds, but we draw the cursor at the corresponding window
26044 margin in that case. */
26045 if (!cursor_row->reversed_p && hpos < 0)
26046 hpos = 0;
26047 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
26048 hpos = cursor_row->used[TEXT_AREA] - 1;
26049
26050 /* If the cursor is in the mouse face area, redisplay that when
26051 we clear the cursor. */
26052 if (! NILP (hlinfo->mouse_face_window)
26053 && coords_in_mouse_face_p (w, hpos, vpos)
26054 /* Don't redraw the cursor's spot in mouse face if it is at the
26055 end of a line (on a newline). The cursor appears there, but
26056 mouse highlighting does not. */
26057 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
26058 mouse_face_here_p = 1;
26059
26060 /* Maybe clear the display under the cursor. */
26061 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
26062 {
26063 int x, y, left_x;
26064 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
26065 int width;
26066
26067 cursor_glyph = get_phys_cursor_glyph (w);
26068 if (cursor_glyph == NULL)
26069 goto mark_cursor_off;
26070
26071 width = cursor_glyph->pixel_width;
26072 left_x = window_box_left_offset (w, TEXT_AREA);
26073 x = w->phys_cursor.x;
26074 if (x < left_x)
26075 width -= left_x - x;
26076 width = min (width, window_box_width (w, TEXT_AREA) - x);
26077 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
26078 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
26079
26080 if (width > 0)
26081 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
26082 }
26083
26084 /* Erase the cursor by redrawing the character underneath it. */
26085 if (mouse_face_here_p)
26086 hl = DRAW_MOUSE_FACE;
26087 else
26088 hl = DRAW_NORMAL_TEXT;
26089 draw_phys_cursor_glyph (w, cursor_row, hl);
26090
26091 mark_cursor_off:
26092 w->phys_cursor_on_p = 0;
26093 w->phys_cursor_type = NO_CURSOR;
26094 }
26095
26096
26097 /* EXPORT:
26098 Display or clear cursor of window W. If ON is zero, clear the
26099 cursor. If it is non-zero, display the cursor. If ON is nonzero,
26100 where to put the cursor is specified by HPOS, VPOS, X and Y. */
26101
26102 void
26103 display_and_set_cursor (struct window *w, int on,
26104 int hpos, int vpos, int x, int y)
26105 {
26106 struct frame *f = XFRAME (w->frame);
26107 int new_cursor_type;
26108 int new_cursor_width;
26109 int active_cursor;
26110 struct glyph_row *glyph_row;
26111 struct glyph *glyph;
26112
26113 /* This is pointless on invisible frames, and dangerous on garbaged
26114 windows and frames; in the latter case, the frame or window may
26115 be in the midst of changing its size, and x and y may be off the
26116 window. */
26117 if (! FRAME_VISIBLE_P (f)
26118 || FRAME_GARBAGED_P (f)
26119 || vpos >= w->current_matrix->nrows
26120 || hpos >= w->current_matrix->matrix_w)
26121 return;
26122
26123 /* If cursor is off and we want it off, return quickly. */
26124 if (!on && !w->phys_cursor_on_p)
26125 return;
26126
26127 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26128 /* If cursor row is not enabled, we don't really know where to
26129 display the cursor. */
26130 if (!glyph_row->enabled_p)
26131 {
26132 w->phys_cursor_on_p = 0;
26133 return;
26134 }
26135
26136 glyph = NULL;
26137 if (!glyph_row->exact_window_width_line_p
26138 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26139 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26140
26141 eassert (input_blocked_p ());
26142
26143 /* Set new_cursor_type to the cursor we want to be displayed. */
26144 new_cursor_type = get_window_cursor_type (w, glyph,
26145 &new_cursor_width, &active_cursor);
26146
26147 /* If cursor is currently being shown and we don't want it to be or
26148 it is in the wrong place, or the cursor type is not what we want,
26149 erase it. */
26150 if (w->phys_cursor_on_p
26151 && (!on
26152 || w->phys_cursor.x != x
26153 || w->phys_cursor.y != y
26154 || new_cursor_type != w->phys_cursor_type
26155 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26156 && new_cursor_width != w->phys_cursor_width)))
26157 erase_phys_cursor (w);
26158
26159 /* Don't check phys_cursor_on_p here because that flag is only set
26160 to zero in some cases where we know that the cursor has been
26161 completely erased, to avoid the extra work of erasing the cursor
26162 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26163 still not be visible, or it has only been partly erased. */
26164 if (on)
26165 {
26166 w->phys_cursor_ascent = glyph_row->ascent;
26167 w->phys_cursor_height = glyph_row->height;
26168
26169 /* Set phys_cursor_.* before x_draw_.* is called because some
26170 of them may need the information. */
26171 w->phys_cursor.x = x;
26172 w->phys_cursor.y = glyph_row->y;
26173 w->phys_cursor.hpos = hpos;
26174 w->phys_cursor.vpos = vpos;
26175 }
26176
26177 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26178 new_cursor_type, new_cursor_width,
26179 on, active_cursor);
26180 }
26181
26182
26183 /* Switch the display of W's cursor on or off, according to the value
26184 of ON. */
26185
26186 static void
26187 update_window_cursor (struct window *w, int on)
26188 {
26189 /* Don't update cursor in windows whose frame is in the process
26190 of being deleted. */
26191 if (w->current_matrix)
26192 {
26193 int hpos = w->phys_cursor.hpos;
26194 int vpos = w->phys_cursor.vpos;
26195 struct glyph_row *row;
26196
26197 if (vpos >= w->current_matrix->nrows
26198 || hpos >= w->current_matrix->matrix_w)
26199 return;
26200
26201 row = MATRIX_ROW (w->current_matrix, vpos);
26202
26203 /* When the window is hscrolled, cursor hpos can legitimately be
26204 out of bounds, but we draw the cursor at the corresponding
26205 window margin in that case. */
26206 if (!row->reversed_p && hpos < 0)
26207 hpos = 0;
26208 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26209 hpos = row->used[TEXT_AREA] - 1;
26210
26211 block_input ();
26212 display_and_set_cursor (w, on, hpos, vpos,
26213 w->phys_cursor.x, w->phys_cursor.y);
26214 unblock_input ();
26215 }
26216 }
26217
26218
26219 /* Call update_window_cursor with parameter ON_P on all leaf windows
26220 in the window tree rooted at W. */
26221
26222 static void
26223 update_cursor_in_window_tree (struct window *w, int on_p)
26224 {
26225 while (w)
26226 {
26227 if (!NILP (w->hchild))
26228 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
26229 else if (!NILP (w->vchild))
26230 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
26231 else
26232 update_window_cursor (w, on_p);
26233
26234 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26235 }
26236 }
26237
26238
26239 /* EXPORT:
26240 Display the cursor on window W, or clear it, according to ON_P.
26241 Don't change the cursor's position. */
26242
26243 void
26244 x_update_cursor (struct frame *f, int on_p)
26245 {
26246 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26247 }
26248
26249
26250 /* EXPORT:
26251 Clear the cursor of window W to background color, and mark the
26252 cursor as not shown. This is used when the text where the cursor
26253 is about to be rewritten. */
26254
26255 void
26256 x_clear_cursor (struct window *w)
26257 {
26258 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26259 update_window_cursor (w, 0);
26260 }
26261
26262 #endif /* HAVE_WINDOW_SYSTEM */
26263
26264 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26265 and MSDOS. */
26266 static void
26267 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26268 int start_hpos, int end_hpos,
26269 enum draw_glyphs_face draw)
26270 {
26271 #ifdef HAVE_WINDOW_SYSTEM
26272 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26273 {
26274 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26275 return;
26276 }
26277 #endif
26278 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26279 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26280 #endif
26281 }
26282
26283 /* Display the active region described by mouse_face_* according to DRAW. */
26284
26285 static void
26286 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26287 {
26288 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26289 struct frame *f = XFRAME (WINDOW_FRAME (w));
26290
26291 if (/* If window is in the process of being destroyed, don't bother
26292 to do anything. */
26293 w->current_matrix != NULL
26294 /* Don't update mouse highlight if hidden */
26295 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26296 /* Recognize when we are called to operate on rows that don't exist
26297 anymore. This can happen when a window is split. */
26298 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26299 {
26300 int phys_cursor_on_p = w->phys_cursor_on_p;
26301 struct glyph_row *row, *first, *last;
26302
26303 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26304 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26305
26306 for (row = first; row <= last && row->enabled_p; ++row)
26307 {
26308 int start_hpos, end_hpos, start_x;
26309
26310 /* For all but the first row, the highlight starts at column 0. */
26311 if (row == first)
26312 {
26313 /* R2L rows have BEG and END in reversed order, but the
26314 screen drawing geometry is always left to right. So
26315 we need to mirror the beginning and end of the
26316 highlighted area in R2L rows. */
26317 if (!row->reversed_p)
26318 {
26319 start_hpos = hlinfo->mouse_face_beg_col;
26320 start_x = hlinfo->mouse_face_beg_x;
26321 }
26322 else if (row == last)
26323 {
26324 start_hpos = hlinfo->mouse_face_end_col;
26325 start_x = hlinfo->mouse_face_end_x;
26326 }
26327 else
26328 {
26329 start_hpos = 0;
26330 start_x = 0;
26331 }
26332 }
26333 else if (row->reversed_p && row == last)
26334 {
26335 start_hpos = hlinfo->mouse_face_end_col;
26336 start_x = hlinfo->mouse_face_end_x;
26337 }
26338 else
26339 {
26340 start_hpos = 0;
26341 start_x = 0;
26342 }
26343
26344 if (row == last)
26345 {
26346 if (!row->reversed_p)
26347 end_hpos = hlinfo->mouse_face_end_col;
26348 else if (row == first)
26349 end_hpos = hlinfo->mouse_face_beg_col;
26350 else
26351 {
26352 end_hpos = row->used[TEXT_AREA];
26353 if (draw == DRAW_NORMAL_TEXT)
26354 row->fill_line_p = 1; /* Clear to end of line */
26355 }
26356 }
26357 else if (row->reversed_p && row == first)
26358 end_hpos = hlinfo->mouse_face_beg_col;
26359 else
26360 {
26361 end_hpos = row->used[TEXT_AREA];
26362 if (draw == DRAW_NORMAL_TEXT)
26363 row->fill_line_p = 1; /* Clear to end of line */
26364 }
26365
26366 if (end_hpos > start_hpos)
26367 {
26368 draw_row_with_mouse_face (w, start_x, row,
26369 start_hpos, end_hpos, draw);
26370
26371 row->mouse_face_p
26372 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26373 }
26374 }
26375
26376 #ifdef HAVE_WINDOW_SYSTEM
26377 /* When we've written over the cursor, arrange for it to
26378 be displayed again. */
26379 if (FRAME_WINDOW_P (f)
26380 && phys_cursor_on_p && !w->phys_cursor_on_p)
26381 {
26382 int hpos = w->phys_cursor.hpos;
26383
26384 /* When the window is hscrolled, cursor hpos can legitimately be
26385 out of bounds, but we draw the cursor at the corresponding
26386 window margin in that case. */
26387 if (!row->reversed_p && hpos < 0)
26388 hpos = 0;
26389 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26390 hpos = row->used[TEXT_AREA] - 1;
26391
26392 block_input ();
26393 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26394 w->phys_cursor.x, w->phys_cursor.y);
26395 unblock_input ();
26396 }
26397 #endif /* HAVE_WINDOW_SYSTEM */
26398 }
26399
26400 #ifdef HAVE_WINDOW_SYSTEM
26401 /* Change the mouse cursor. */
26402 if (FRAME_WINDOW_P (f))
26403 {
26404 if (draw == DRAW_NORMAL_TEXT
26405 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26406 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26407 else if (draw == DRAW_MOUSE_FACE)
26408 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26409 else
26410 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26411 }
26412 #endif /* HAVE_WINDOW_SYSTEM */
26413 }
26414
26415 /* EXPORT:
26416 Clear out the mouse-highlighted active region.
26417 Redraw it un-highlighted first. Value is non-zero if mouse
26418 face was actually drawn unhighlighted. */
26419
26420 int
26421 clear_mouse_face (Mouse_HLInfo *hlinfo)
26422 {
26423 int cleared = 0;
26424
26425 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26426 {
26427 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26428 cleared = 1;
26429 }
26430
26431 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26432 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26433 hlinfo->mouse_face_window = Qnil;
26434 hlinfo->mouse_face_overlay = Qnil;
26435 return cleared;
26436 }
26437
26438 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26439 within the mouse face on that window. */
26440 static int
26441 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26442 {
26443 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26444
26445 /* Quickly resolve the easy cases. */
26446 if (!(WINDOWP (hlinfo->mouse_face_window)
26447 && XWINDOW (hlinfo->mouse_face_window) == w))
26448 return 0;
26449 if (vpos < hlinfo->mouse_face_beg_row
26450 || vpos > hlinfo->mouse_face_end_row)
26451 return 0;
26452 if (vpos > hlinfo->mouse_face_beg_row
26453 && vpos < hlinfo->mouse_face_end_row)
26454 return 1;
26455
26456 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26457 {
26458 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26459 {
26460 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26461 return 1;
26462 }
26463 else if ((vpos == hlinfo->mouse_face_beg_row
26464 && hpos >= hlinfo->mouse_face_beg_col)
26465 || (vpos == hlinfo->mouse_face_end_row
26466 && hpos < hlinfo->mouse_face_end_col))
26467 return 1;
26468 }
26469 else
26470 {
26471 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26472 {
26473 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26474 return 1;
26475 }
26476 else if ((vpos == hlinfo->mouse_face_beg_row
26477 && hpos <= hlinfo->mouse_face_beg_col)
26478 || (vpos == hlinfo->mouse_face_end_row
26479 && hpos > hlinfo->mouse_face_end_col))
26480 return 1;
26481 }
26482 return 0;
26483 }
26484
26485
26486 /* EXPORT:
26487 Non-zero if physical cursor of window W is within mouse face. */
26488
26489 int
26490 cursor_in_mouse_face_p (struct window *w)
26491 {
26492 int hpos = w->phys_cursor.hpos;
26493 int vpos = w->phys_cursor.vpos;
26494 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26495
26496 /* When the window is hscrolled, cursor hpos can legitimately be out
26497 of bounds, but we draw the cursor at the corresponding window
26498 margin in that case. */
26499 if (!row->reversed_p && hpos < 0)
26500 hpos = 0;
26501 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26502 hpos = row->used[TEXT_AREA] - 1;
26503
26504 return coords_in_mouse_face_p (w, hpos, vpos);
26505 }
26506
26507
26508 \f
26509 /* Find the glyph rows START_ROW and END_ROW of window W that display
26510 characters between buffer positions START_CHARPOS and END_CHARPOS
26511 (excluding END_CHARPOS). DISP_STRING is a display string that
26512 covers these buffer positions. This is similar to
26513 row_containing_pos, but is more accurate when bidi reordering makes
26514 buffer positions change non-linearly with glyph rows. */
26515 static void
26516 rows_from_pos_range (struct window *w,
26517 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26518 Lisp_Object disp_string,
26519 struct glyph_row **start, struct glyph_row **end)
26520 {
26521 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26522 int last_y = window_text_bottom_y (w);
26523 struct glyph_row *row;
26524
26525 *start = NULL;
26526 *end = NULL;
26527
26528 while (!first->enabled_p
26529 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26530 first++;
26531
26532 /* Find the START row. */
26533 for (row = first;
26534 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26535 row++)
26536 {
26537 /* A row can potentially be the START row if the range of the
26538 characters it displays intersects the range
26539 [START_CHARPOS..END_CHARPOS). */
26540 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26541 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26542 /* See the commentary in row_containing_pos, for the
26543 explanation of the complicated way to check whether
26544 some position is beyond the end of the characters
26545 displayed by a row. */
26546 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26547 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26548 && !row->ends_at_zv_p
26549 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26550 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26551 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26552 && !row->ends_at_zv_p
26553 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26554 {
26555 /* Found a candidate row. Now make sure at least one of the
26556 glyphs it displays has a charpos from the range
26557 [START_CHARPOS..END_CHARPOS).
26558
26559 This is not obvious because bidi reordering could make
26560 buffer positions of a row be 1,2,3,102,101,100, and if we
26561 want to highlight characters in [50..60), we don't want
26562 this row, even though [50..60) does intersect [1..103),
26563 the range of character positions given by the row's start
26564 and end positions. */
26565 struct glyph *g = row->glyphs[TEXT_AREA];
26566 struct glyph *e = g + row->used[TEXT_AREA];
26567
26568 while (g < e)
26569 {
26570 if (((BUFFERP (g->object) || INTEGERP (g->object))
26571 && start_charpos <= g->charpos && g->charpos < end_charpos)
26572 /* A glyph that comes from DISP_STRING is by
26573 definition to be highlighted. */
26574 || EQ (g->object, disp_string))
26575 *start = row;
26576 g++;
26577 }
26578 if (*start)
26579 break;
26580 }
26581 }
26582
26583 /* Find the END row. */
26584 if (!*start
26585 /* If the last row is partially visible, start looking for END
26586 from that row, instead of starting from FIRST. */
26587 && !(row->enabled_p
26588 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26589 row = first;
26590 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26591 {
26592 struct glyph_row *next = row + 1;
26593 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26594
26595 if (!next->enabled_p
26596 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26597 /* The first row >= START whose range of displayed characters
26598 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26599 is the row END + 1. */
26600 || (start_charpos < next_start
26601 && end_charpos < next_start)
26602 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26603 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26604 && !next->ends_at_zv_p
26605 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26606 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26607 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26608 && !next->ends_at_zv_p
26609 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26610 {
26611 *end = row;
26612 break;
26613 }
26614 else
26615 {
26616 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26617 but none of the characters it displays are in the range, it is
26618 also END + 1. */
26619 struct glyph *g = next->glyphs[TEXT_AREA];
26620 struct glyph *s = g;
26621 struct glyph *e = g + next->used[TEXT_AREA];
26622
26623 while (g < e)
26624 {
26625 if (((BUFFERP (g->object) || INTEGERP (g->object))
26626 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26627 /* If the buffer position of the first glyph in
26628 the row is equal to END_CHARPOS, it means
26629 the last character to be highlighted is the
26630 newline of ROW, and we must consider NEXT as
26631 END, not END+1. */
26632 || (((!next->reversed_p && g == s)
26633 || (next->reversed_p && g == e - 1))
26634 && (g->charpos == end_charpos
26635 /* Special case for when NEXT is an
26636 empty line at ZV. */
26637 || (g->charpos == -1
26638 && !row->ends_at_zv_p
26639 && next_start == end_charpos)))))
26640 /* A glyph that comes from DISP_STRING is by
26641 definition to be highlighted. */
26642 || EQ (g->object, disp_string))
26643 break;
26644 g++;
26645 }
26646 if (g == e)
26647 {
26648 *end = row;
26649 break;
26650 }
26651 /* The first row that ends at ZV must be the last to be
26652 highlighted. */
26653 else if (next->ends_at_zv_p)
26654 {
26655 *end = next;
26656 break;
26657 }
26658 }
26659 }
26660 }
26661
26662 /* This function sets the mouse_face_* elements of HLINFO, assuming
26663 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
26664 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
26665 for the overlay or run of text properties specifying the mouse
26666 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
26667 before-string and after-string that must also be highlighted.
26668 DISP_STRING, if non-nil, is a display string that may cover some
26669 or all of the highlighted text. */
26670
26671 static void
26672 mouse_face_from_buffer_pos (Lisp_Object window,
26673 Mouse_HLInfo *hlinfo,
26674 ptrdiff_t mouse_charpos,
26675 ptrdiff_t start_charpos,
26676 ptrdiff_t end_charpos,
26677 Lisp_Object before_string,
26678 Lisp_Object after_string,
26679 Lisp_Object disp_string)
26680 {
26681 struct window *w = XWINDOW (window);
26682 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26683 struct glyph_row *r1, *r2;
26684 struct glyph *glyph, *end;
26685 ptrdiff_t ignore, pos;
26686 int x;
26687
26688 eassert (NILP (disp_string) || STRINGP (disp_string));
26689 eassert (NILP (before_string) || STRINGP (before_string));
26690 eassert (NILP (after_string) || STRINGP (after_string));
26691
26692 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
26693 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
26694 if (r1 == NULL)
26695 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26696 /* If the before-string or display-string contains newlines,
26697 rows_from_pos_range skips to its last row. Move back. */
26698 if (!NILP (before_string) || !NILP (disp_string))
26699 {
26700 struct glyph_row *prev;
26701 while ((prev = r1 - 1, prev >= first)
26702 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
26703 && prev->used[TEXT_AREA] > 0)
26704 {
26705 struct glyph *beg = prev->glyphs[TEXT_AREA];
26706 glyph = beg + prev->used[TEXT_AREA];
26707 while (--glyph >= beg && INTEGERP (glyph->object));
26708 if (glyph < beg
26709 || !(EQ (glyph->object, before_string)
26710 || EQ (glyph->object, disp_string)))
26711 break;
26712 r1 = prev;
26713 }
26714 }
26715 if (r2 == NULL)
26716 {
26717 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26718 hlinfo->mouse_face_past_end = 1;
26719 }
26720 else if (!NILP (after_string))
26721 {
26722 /* If the after-string has newlines, advance to its last row. */
26723 struct glyph_row *next;
26724 struct glyph_row *last
26725 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26726
26727 for (next = r2 + 1;
26728 next <= last
26729 && next->used[TEXT_AREA] > 0
26730 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
26731 ++next)
26732 r2 = next;
26733 }
26734 /* The rest of the display engine assumes that mouse_face_beg_row is
26735 either above mouse_face_end_row or identical to it. But with
26736 bidi-reordered continued lines, the row for START_CHARPOS could
26737 be below the row for END_CHARPOS. If so, swap the rows and store
26738 them in correct order. */
26739 if (r1->y > r2->y)
26740 {
26741 struct glyph_row *tem = r2;
26742
26743 r2 = r1;
26744 r1 = tem;
26745 }
26746
26747 hlinfo->mouse_face_beg_y = r1->y;
26748 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
26749 hlinfo->mouse_face_end_y = r2->y;
26750 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
26751
26752 /* For a bidi-reordered row, the positions of BEFORE_STRING,
26753 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
26754 could be anywhere in the row and in any order. The strategy
26755 below is to find the leftmost and the rightmost glyph that
26756 belongs to either of these 3 strings, or whose position is
26757 between START_CHARPOS and END_CHARPOS, and highlight all the
26758 glyphs between those two. This may cover more than just the text
26759 between START_CHARPOS and END_CHARPOS if the range of characters
26760 strides the bidi level boundary, e.g. if the beginning is in R2L
26761 text while the end is in L2R text or vice versa. */
26762 if (!r1->reversed_p)
26763 {
26764 /* This row is in a left to right paragraph. Scan it left to
26765 right. */
26766 glyph = r1->glyphs[TEXT_AREA];
26767 end = glyph + r1->used[TEXT_AREA];
26768 x = r1->x;
26769
26770 /* Skip truncation glyphs at the start of the glyph row. */
26771 if (r1->displays_text_p)
26772 for (; glyph < end
26773 && INTEGERP (glyph->object)
26774 && glyph->charpos < 0;
26775 ++glyph)
26776 x += glyph->pixel_width;
26777
26778 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26779 or DISP_STRING, and the first glyph from buffer whose
26780 position is between START_CHARPOS and END_CHARPOS. */
26781 for (; glyph < end
26782 && !INTEGERP (glyph->object)
26783 && !EQ (glyph->object, disp_string)
26784 && !(BUFFERP (glyph->object)
26785 && (glyph->charpos >= start_charpos
26786 && glyph->charpos < end_charpos));
26787 ++glyph)
26788 {
26789 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26790 are present at buffer positions between START_CHARPOS and
26791 END_CHARPOS, or if they come from an overlay. */
26792 if (EQ (glyph->object, before_string))
26793 {
26794 pos = string_buffer_position (before_string,
26795 start_charpos);
26796 /* If pos == 0, it means before_string came from an
26797 overlay, not from a buffer position. */
26798 if (!pos || (pos >= start_charpos && pos < end_charpos))
26799 break;
26800 }
26801 else if (EQ (glyph->object, after_string))
26802 {
26803 pos = string_buffer_position (after_string, end_charpos);
26804 if (!pos || (pos >= start_charpos && pos < end_charpos))
26805 break;
26806 }
26807 x += glyph->pixel_width;
26808 }
26809 hlinfo->mouse_face_beg_x = x;
26810 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26811 }
26812 else
26813 {
26814 /* This row is in a right to left paragraph. Scan it right to
26815 left. */
26816 struct glyph *g;
26817
26818 end = r1->glyphs[TEXT_AREA] - 1;
26819 glyph = end + r1->used[TEXT_AREA];
26820
26821 /* Skip truncation glyphs at the start of the glyph row. */
26822 if (r1->displays_text_p)
26823 for (; glyph > end
26824 && INTEGERP (glyph->object)
26825 && glyph->charpos < 0;
26826 --glyph)
26827 ;
26828
26829 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26830 or DISP_STRING, and the first glyph from buffer whose
26831 position is between START_CHARPOS and END_CHARPOS. */
26832 for (; glyph > end
26833 && !INTEGERP (glyph->object)
26834 && !EQ (glyph->object, disp_string)
26835 && !(BUFFERP (glyph->object)
26836 && (glyph->charpos >= start_charpos
26837 && glyph->charpos < end_charpos));
26838 --glyph)
26839 {
26840 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26841 are present at buffer positions between START_CHARPOS and
26842 END_CHARPOS, or if they come from an overlay. */
26843 if (EQ (glyph->object, before_string))
26844 {
26845 pos = string_buffer_position (before_string, start_charpos);
26846 /* If pos == 0, it means before_string came from an
26847 overlay, not from a buffer position. */
26848 if (!pos || (pos >= start_charpos && pos < end_charpos))
26849 break;
26850 }
26851 else if (EQ (glyph->object, after_string))
26852 {
26853 pos = string_buffer_position (after_string, end_charpos);
26854 if (!pos || (pos >= start_charpos && pos < end_charpos))
26855 break;
26856 }
26857 }
26858
26859 glyph++; /* first glyph to the right of the highlighted area */
26860 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
26861 x += g->pixel_width;
26862 hlinfo->mouse_face_beg_x = x;
26863 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26864 }
26865
26866 /* If the highlight ends in a different row, compute GLYPH and END
26867 for the end row. Otherwise, reuse the values computed above for
26868 the row where the highlight begins. */
26869 if (r2 != r1)
26870 {
26871 if (!r2->reversed_p)
26872 {
26873 glyph = r2->glyphs[TEXT_AREA];
26874 end = glyph + r2->used[TEXT_AREA];
26875 x = r2->x;
26876 }
26877 else
26878 {
26879 end = r2->glyphs[TEXT_AREA] - 1;
26880 glyph = end + r2->used[TEXT_AREA];
26881 }
26882 }
26883
26884 if (!r2->reversed_p)
26885 {
26886 /* Skip truncation and continuation glyphs near the end of the
26887 row, and also blanks and stretch glyphs inserted by
26888 extend_face_to_end_of_line. */
26889 while (end > glyph
26890 && INTEGERP ((end - 1)->object))
26891 --end;
26892 /* Scan the rest of the glyph row from the end, looking for the
26893 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26894 DISP_STRING, or whose position is between START_CHARPOS
26895 and END_CHARPOS */
26896 for (--end;
26897 end > glyph
26898 && !INTEGERP (end->object)
26899 && !EQ (end->object, disp_string)
26900 && !(BUFFERP (end->object)
26901 && (end->charpos >= start_charpos
26902 && end->charpos < end_charpos));
26903 --end)
26904 {
26905 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26906 are present at buffer positions between START_CHARPOS and
26907 END_CHARPOS, or if they come from an overlay. */
26908 if (EQ (end->object, before_string))
26909 {
26910 pos = string_buffer_position (before_string, start_charpos);
26911 if (!pos || (pos >= start_charpos && pos < end_charpos))
26912 break;
26913 }
26914 else if (EQ (end->object, after_string))
26915 {
26916 pos = string_buffer_position (after_string, end_charpos);
26917 if (!pos || (pos >= start_charpos && pos < end_charpos))
26918 break;
26919 }
26920 }
26921 /* Find the X coordinate of the last glyph to be highlighted. */
26922 for (; glyph <= end; ++glyph)
26923 x += glyph->pixel_width;
26924
26925 hlinfo->mouse_face_end_x = x;
26926 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
26927 }
26928 else
26929 {
26930 /* Skip truncation and continuation glyphs near the end of the
26931 row, and also blanks and stretch glyphs inserted by
26932 extend_face_to_end_of_line. */
26933 x = r2->x;
26934 end++;
26935 while (end < glyph
26936 && INTEGERP (end->object))
26937 {
26938 x += end->pixel_width;
26939 ++end;
26940 }
26941 /* Scan the rest of the glyph row from the end, looking for the
26942 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26943 DISP_STRING, or whose position is between START_CHARPOS
26944 and END_CHARPOS */
26945 for ( ;
26946 end < glyph
26947 && !INTEGERP (end->object)
26948 && !EQ (end->object, disp_string)
26949 && !(BUFFERP (end->object)
26950 && (end->charpos >= start_charpos
26951 && end->charpos < end_charpos));
26952 ++end)
26953 {
26954 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26955 are present at buffer positions between START_CHARPOS and
26956 END_CHARPOS, or if they come from an overlay. */
26957 if (EQ (end->object, before_string))
26958 {
26959 pos = string_buffer_position (before_string, start_charpos);
26960 if (!pos || (pos >= start_charpos && pos < end_charpos))
26961 break;
26962 }
26963 else if (EQ (end->object, after_string))
26964 {
26965 pos = string_buffer_position (after_string, end_charpos);
26966 if (!pos || (pos >= start_charpos && pos < end_charpos))
26967 break;
26968 }
26969 x += end->pixel_width;
26970 }
26971 /* If we exited the above loop because we arrived at the last
26972 glyph of the row, and its buffer position is still not in
26973 range, it means the last character in range is the preceding
26974 newline. Bump the end column and x values to get past the
26975 last glyph. */
26976 if (end == glyph
26977 && BUFFERP (end->object)
26978 && (end->charpos < start_charpos
26979 || end->charpos >= end_charpos))
26980 {
26981 x += end->pixel_width;
26982 ++end;
26983 }
26984 hlinfo->mouse_face_end_x = x;
26985 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
26986 }
26987
26988 hlinfo->mouse_face_window = window;
26989 hlinfo->mouse_face_face_id
26990 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
26991 mouse_charpos + 1,
26992 !hlinfo->mouse_face_hidden, -1);
26993 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26994 }
26995
26996 /* The following function is not used anymore (replaced with
26997 mouse_face_from_string_pos), but I leave it here for the time
26998 being, in case someone would. */
26999
27000 #if 0 /* not used */
27001
27002 /* Find the position of the glyph for position POS in OBJECT in
27003 window W's current matrix, and return in *X, *Y the pixel
27004 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
27005
27006 RIGHT_P non-zero means return the position of the right edge of the
27007 glyph, RIGHT_P zero means return the left edge position.
27008
27009 If no glyph for POS exists in the matrix, return the position of
27010 the glyph with the next smaller position that is in the matrix, if
27011 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
27012 exists in the matrix, return the position of the glyph with the
27013 next larger position in OBJECT.
27014
27015 Value is non-zero if a glyph was found. */
27016
27017 static int
27018 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
27019 int *hpos, int *vpos, int *x, int *y, int right_p)
27020 {
27021 int yb = window_text_bottom_y (w);
27022 struct glyph_row *r;
27023 struct glyph *best_glyph = NULL;
27024 struct glyph_row *best_row = NULL;
27025 int best_x = 0;
27026
27027 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27028 r->enabled_p && r->y < yb;
27029 ++r)
27030 {
27031 struct glyph *g = r->glyphs[TEXT_AREA];
27032 struct glyph *e = g + r->used[TEXT_AREA];
27033 int gx;
27034
27035 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27036 if (EQ (g->object, object))
27037 {
27038 if (g->charpos == pos)
27039 {
27040 best_glyph = g;
27041 best_x = gx;
27042 best_row = r;
27043 goto found;
27044 }
27045 else if (best_glyph == NULL
27046 || ((eabs (g->charpos - pos)
27047 < eabs (best_glyph->charpos - pos))
27048 && (right_p
27049 ? g->charpos < pos
27050 : g->charpos > pos)))
27051 {
27052 best_glyph = g;
27053 best_x = gx;
27054 best_row = r;
27055 }
27056 }
27057 }
27058
27059 found:
27060
27061 if (best_glyph)
27062 {
27063 *x = best_x;
27064 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
27065
27066 if (right_p)
27067 {
27068 *x += best_glyph->pixel_width;
27069 ++*hpos;
27070 }
27071
27072 *y = best_row->y;
27073 *vpos = best_row - w->current_matrix->rows;
27074 }
27075
27076 return best_glyph != NULL;
27077 }
27078 #endif /* not used */
27079
27080 /* Find the positions of the first and the last glyphs in window W's
27081 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
27082 (assumed to be a string), and return in HLINFO's mouse_face_*
27083 members the pixel and column/row coordinates of those glyphs. */
27084
27085 static void
27086 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
27087 Lisp_Object object,
27088 ptrdiff_t startpos, ptrdiff_t endpos)
27089 {
27090 int yb = window_text_bottom_y (w);
27091 struct glyph_row *r;
27092 struct glyph *g, *e;
27093 int gx;
27094 int found = 0;
27095
27096 /* Find the glyph row with at least one position in the range
27097 [STARTPOS..ENDPOS], and the first glyph in that row whose
27098 position belongs to that range. */
27099 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27100 r->enabled_p && r->y < yb;
27101 ++r)
27102 {
27103 if (!r->reversed_p)
27104 {
27105 g = r->glyphs[TEXT_AREA];
27106 e = g + r->used[TEXT_AREA];
27107 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27108 if (EQ (g->object, object)
27109 && startpos <= g->charpos && g->charpos <= endpos)
27110 {
27111 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
27112 hlinfo->mouse_face_beg_y = r->y;
27113 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27114 hlinfo->mouse_face_beg_x = gx;
27115 found = 1;
27116 break;
27117 }
27118 }
27119 else
27120 {
27121 struct glyph *g1;
27122
27123 e = r->glyphs[TEXT_AREA];
27124 g = e + r->used[TEXT_AREA];
27125 for ( ; g > e; --g)
27126 if (EQ ((g-1)->object, object)
27127 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
27128 {
27129 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
27130 hlinfo->mouse_face_beg_y = r->y;
27131 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27132 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27133 gx += g1->pixel_width;
27134 hlinfo->mouse_face_beg_x = gx;
27135 found = 1;
27136 break;
27137 }
27138 }
27139 if (found)
27140 break;
27141 }
27142
27143 if (!found)
27144 return;
27145
27146 /* Starting with the next row, look for the first row which does NOT
27147 include any glyphs whose positions are in the range. */
27148 for (++r; r->enabled_p && r->y < yb; ++r)
27149 {
27150 g = r->glyphs[TEXT_AREA];
27151 e = g + r->used[TEXT_AREA];
27152 found = 0;
27153 for ( ; g < e; ++g)
27154 if (EQ (g->object, object)
27155 && startpos <= g->charpos && g->charpos <= endpos)
27156 {
27157 found = 1;
27158 break;
27159 }
27160 if (!found)
27161 break;
27162 }
27163
27164 /* The highlighted region ends on the previous row. */
27165 r--;
27166
27167 /* Set the end row and its vertical pixel coordinate. */
27168 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
27169 hlinfo->mouse_face_end_y = r->y;
27170
27171 /* Compute and set the end column and the end column's horizontal
27172 pixel coordinate. */
27173 if (!r->reversed_p)
27174 {
27175 g = r->glyphs[TEXT_AREA];
27176 e = g + r->used[TEXT_AREA];
27177 for ( ; e > g; --e)
27178 if (EQ ((e-1)->object, object)
27179 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
27180 break;
27181 hlinfo->mouse_face_end_col = e - g;
27182
27183 for (gx = r->x; g < e; ++g)
27184 gx += g->pixel_width;
27185 hlinfo->mouse_face_end_x = gx;
27186 }
27187 else
27188 {
27189 e = r->glyphs[TEXT_AREA];
27190 g = e + r->used[TEXT_AREA];
27191 for (gx = r->x ; e < g; ++e)
27192 {
27193 if (EQ (e->object, object)
27194 && startpos <= e->charpos && e->charpos <= endpos)
27195 break;
27196 gx += e->pixel_width;
27197 }
27198 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27199 hlinfo->mouse_face_end_x = gx;
27200 }
27201 }
27202
27203 #ifdef HAVE_WINDOW_SYSTEM
27204
27205 /* See if position X, Y is within a hot-spot of an image. */
27206
27207 static int
27208 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27209 {
27210 if (!CONSP (hot_spot))
27211 return 0;
27212
27213 if (EQ (XCAR (hot_spot), Qrect))
27214 {
27215 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27216 Lisp_Object rect = XCDR (hot_spot);
27217 Lisp_Object tem;
27218 if (!CONSP (rect))
27219 return 0;
27220 if (!CONSP (XCAR (rect)))
27221 return 0;
27222 if (!CONSP (XCDR (rect)))
27223 return 0;
27224 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27225 return 0;
27226 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27227 return 0;
27228 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27229 return 0;
27230 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27231 return 0;
27232 return 1;
27233 }
27234 else if (EQ (XCAR (hot_spot), Qcircle))
27235 {
27236 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27237 Lisp_Object circ = XCDR (hot_spot);
27238 Lisp_Object lr, lx0, ly0;
27239 if (CONSP (circ)
27240 && CONSP (XCAR (circ))
27241 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27242 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27243 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27244 {
27245 double r = XFLOATINT (lr);
27246 double dx = XINT (lx0) - x;
27247 double dy = XINT (ly0) - y;
27248 return (dx * dx + dy * dy <= r * r);
27249 }
27250 }
27251 else if (EQ (XCAR (hot_spot), Qpoly))
27252 {
27253 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27254 if (VECTORP (XCDR (hot_spot)))
27255 {
27256 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27257 Lisp_Object *poly = v->contents;
27258 ptrdiff_t n = v->header.size;
27259 ptrdiff_t i;
27260 int inside = 0;
27261 Lisp_Object lx, ly;
27262 int x0, y0;
27263
27264 /* Need an even number of coordinates, and at least 3 edges. */
27265 if (n < 6 || n & 1)
27266 return 0;
27267
27268 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27269 If count is odd, we are inside polygon. Pixels on edges
27270 may or may not be included depending on actual geometry of the
27271 polygon. */
27272 if ((lx = poly[n-2], !INTEGERP (lx))
27273 || (ly = poly[n-1], !INTEGERP (lx)))
27274 return 0;
27275 x0 = XINT (lx), y0 = XINT (ly);
27276 for (i = 0; i < n; i += 2)
27277 {
27278 int x1 = x0, y1 = y0;
27279 if ((lx = poly[i], !INTEGERP (lx))
27280 || (ly = poly[i+1], !INTEGERP (ly)))
27281 return 0;
27282 x0 = XINT (lx), y0 = XINT (ly);
27283
27284 /* Does this segment cross the X line? */
27285 if (x0 >= x)
27286 {
27287 if (x1 >= x)
27288 continue;
27289 }
27290 else if (x1 < x)
27291 continue;
27292 if (y > y0 && y > y1)
27293 continue;
27294 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27295 inside = !inside;
27296 }
27297 return inside;
27298 }
27299 }
27300 return 0;
27301 }
27302
27303 Lisp_Object
27304 find_hot_spot (Lisp_Object map, int x, int y)
27305 {
27306 while (CONSP (map))
27307 {
27308 if (CONSP (XCAR (map))
27309 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27310 return XCAR (map);
27311 map = XCDR (map);
27312 }
27313
27314 return Qnil;
27315 }
27316
27317 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27318 3, 3, 0,
27319 doc: /* Lookup in image map MAP coordinates X and Y.
27320 An image map is an alist where each element has the format (AREA ID PLIST).
27321 An AREA is specified as either a rectangle, a circle, or a polygon:
27322 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27323 pixel coordinates of the upper left and bottom right corners.
27324 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27325 and the radius of the circle; r may be a float or integer.
27326 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27327 vector describes one corner in the polygon.
27328 Returns the alist element for the first matching AREA in MAP. */)
27329 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27330 {
27331 if (NILP (map))
27332 return Qnil;
27333
27334 CHECK_NUMBER (x);
27335 CHECK_NUMBER (y);
27336
27337 return find_hot_spot (map,
27338 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27339 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27340 }
27341
27342
27343 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27344 static void
27345 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27346 {
27347 /* Do not change cursor shape while dragging mouse. */
27348 if (!NILP (do_mouse_tracking))
27349 return;
27350
27351 if (!NILP (pointer))
27352 {
27353 if (EQ (pointer, Qarrow))
27354 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27355 else if (EQ (pointer, Qhand))
27356 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27357 else if (EQ (pointer, Qtext))
27358 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27359 else if (EQ (pointer, intern ("hdrag")))
27360 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27361 #ifdef HAVE_X_WINDOWS
27362 else if (EQ (pointer, intern ("vdrag")))
27363 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27364 #endif
27365 else if (EQ (pointer, intern ("hourglass")))
27366 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27367 else if (EQ (pointer, Qmodeline))
27368 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27369 else
27370 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27371 }
27372
27373 if (cursor != No_Cursor)
27374 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27375 }
27376
27377 #endif /* HAVE_WINDOW_SYSTEM */
27378
27379 /* Take proper action when mouse has moved to the mode or header line
27380 or marginal area AREA of window W, x-position X and y-position Y.
27381 X is relative to the start of the text display area of W, so the
27382 width of bitmap areas and scroll bars must be subtracted to get a
27383 position relative to the start of the mode line. */
27384
27385 static void
27386 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27387 enum window_part area)
27388 {
27389 struct window *w = XWINDOW (window);
27390 struct frame *f = XFRAME (w->frame);
27391 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27392 #ifdef HAVE_WINDOW_SYSTEM
27393 Display_Info *dpyinfo;
27394 #endif
27395 Cursor cursor = No_Cursor;
27396 Lisp_Object pointer = Qnil;
27397 int dx, dy, width, height;
27398 ptrdiff_t charpos;
27399 Lisp_Object string, object = Qnil;
27400 Lisp_Object pos IF_LINT (= Qnil), help;
27401
27402 Lisp_Object mouse_face;
27403 int original_x_pixel = x;
27404 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27405 struct glyph_row *row IF_LINT (= 0);
27406
27407 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27408 {
27409 int x0;
27410 struct glyph *end;
27411
27412 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27413 returns them in row/column units! */
27414 string = mode_line_string (w, area, &x, &y, &charpos,
27415 &object, &dx, &dy, &width, &height);
27416
27417 row = (area == ON_MODE_LINE
27418 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27419 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27420
27421 /* Find the glyph under the mouse pointer. */
27422 if (row->mode_line_p && row->enabled_p)
27423 {
27424 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27425 end = glyph + row->used[TEXT_AREA];
27426
27427 for (x0 = original_x_pixel;
27428 glyph < end && x0 >= glyph->pixel_width;
27429 ++glyph)
27430 x0 -= glyph->pixel_width;
27431
27432 if (glyph >= end)
27433 glyph = NULL;
27434 }
27435 }
27436 else
27437 {
27438 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27439 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27440 returns them in row/column units! */
27441 string = marginal_area_string (w, area, &x, &y, &charpos,
27442 &object, &dx, &dy, &width, &height);
27443 }
27444
27445 help = Qnil;
27446
27447 #ifdef HAVE_WINDOW_SYSTEM
27448 if (IMAGEP (object))
27449 {
27450 Lisp_Object image_map, hotspot;
27451 if ((image_map = Fplist_get (XCDR (object), QCmap),
27452 !NILP (image_map))
27453 && (hotspot = find_hot_spot (image_map, dx, dy),
27454 CONSP (hotspot))
27455 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27456 {
27457 Lisp_Object plist;
27458
27459 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27460 If so, we could look for mouse-enter, mouse-leave
27461 properties in PLIST (and do something...). */
27462 hotspot = XCDR (hotspot);
27463 if (CONSP (hotspot)
27464 && (plist = XCAR (hotspot), CONSP (plist)))
27465 {
27466 pointer = Fplist_get (plist, Qpointer);
27467 if (NILP (pointer))
27468 pointer = Qhand;
27469 help = Fplist_get (plist, Qhelp_echo);
27470 if (!NILP (help))
27471 {
27472 help_echo_string = help;
27473 XSETWINDOW (help_echo_window, w);
27474 help_echo_object = w->buffer;
27475 help_echo_pos = charpos;
27476 }
27477 }
27478 }
27479 if (NILP (pointer))
27480 pointer = Fplist_get (XCDR (object), QCpointer);
27481 }
27482 #endif /* HAVE_WINDOW_SYSTEM */
27483
27484 if (STRINGP (string))
27485 pos = make_number (charpos);
27486
27487 /* Set the help text and mouse pointer. If the mouse is on a part
27488 of the mode line without any text (e.g. past the right edge of
27489 the mode line text), use the default help text and pointer. */
27490 if (STRINGP (string) || area == ON_MODE_LINE)
27491 {
27492 /* Arrange to display the help by setting the global variables
27493 help_echo_string, help_echo_object, and help_echo_pos. */
27494 if (NILP (help))
27495 {
27496 if (STRINGP (string))
27497 help = Fget_text_property (pos, Qhelp_echo, string);
27498
27499 if (!NILP (help))
27500 {
27501 help_echo_string = help;
27502 XSETWINDOW (help_echo_window, w);
27503 help_echo_object = string;
27504 help_echo_pos = charpos;
27505 }
27506 else if (area == ON_MODE_LINE)
27507 {
27508 Lisp_Object default_help
27509 = buffer_local_value_1 (Qmode_line_default_help_echo,
27510 w->buffer);
27511
27512 if (STRINGP (default_help))
27513 {
27514 help_echo_string = default_help;
27515 XSETWINDOW (help_echo_window, w);
27516 help_echo_object = Qnil;
27517 help_echo_pos = -1;
27518 }
27519 }
27520 }
27521
27522 #ifdef HAVE_WINDOW_SYSTEM
27523 /* Change the mouse pointer according to what is under it. */
27524 if (FRAME_WINDOW_P (f))
27525 {
27526 dpyinfo = FRAME_X_DISPLAY_INFO (f);
27527 if (STRINGP (string))
27528 {
27529 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27530
27531 if (NILP (pointer))
27532 pointer = Fget_text_property (pos, Qpointer, string);
27533
27534 /* Change the mouse pointer according to what is under X/Y. */
27535 if (NILP (pointer)
27536 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27537 {
27538 Lisp_Object map;
27539 map = Fget_text_property (pos, Qlocal_map, string);
27540 if (!KEYMAPP (map))
27541 map = Fget_text_property (pos, Qkeymap, string);
27542 if (!KEYMAPP (map))
27543 cursor = dpyinfo->vertical_scroll_bar_cursor;
27544 }
27545 }
27546 else
27547 /* Default mode-line pointer. */
27548 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27549 }
27550 #endif
27551 }
27552
27553 /* Change the mouse face according to what is under X/Y. */
27554 if (STRINGP (string))
27555 {
27556 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27557 if (!NILP (mouse_face)
27558 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27559 && glyph)
27560 {
27561 Lisp_Object b, e;
27562
27563 struct glyph * tmp_glyph;
27564
27565 int gpos;
27566 int gseq_length;
27567 int total_pixel_width;
27568 ptrdiff_t begpos, endpos, ignore;
27569
27570 int vpos, hpos;
27571
27572 b = Fprevious_single_property_change (make_number (charpos + 1),
27573 Qmouse_face, string, Qnil);
27574 if (NILP (b))
27575 begpos = 0;
27576 else
27577 begpos = XINT (b);
27578
27579 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27580 if (NILP (e))
27581 endpos = SCHARS (string);
27582 else
27583 endpos = XINT (e);
27584
27585 /* Calculate the glyph position GPOS of GLYPH in the
27586 displayed string, relative to the beginning of the
27587 highlighted part of the string.
27588
27589 Note: GPOS is different from CHARPOS. CHARPOS is the
27590 position of GLYPH in the internal string object. A mode
27591 line string format has structures which are converted to
27592 a flattened string by the Emacs Lisp interpreter. The
27593 internal string is an element of those structures. The
27594 displayed string is the flattened string. */
27595 tmp_glyph = row_start_glyph;
27596 while (tmp_glyph < glyph
27597 && (!(EQ (tmp_glyph->object, glyph->object)
27598 && begpos <= tmp_glyph->charpos
27599 && tmp_glyph->charpos < endpos)))
27600 tmp_glyph++;
27601 gpos = glyph - tmp_glyph;
27602
27603 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27604 the highlighted part of the displayed string to which
27605 GLYPH belongs. Note: GSEQ_LENGTH is different from
27606 SCHARS (STRING), because the latter returns the length of
27607 the internal string. */
27608 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27609 tmp_glyph > glyph
27610 && (!(EQ (tmp_glyph->object, glyph->object)
27611 && begpos <= tmp_glyph->charpos
27612 && tmp_glyph->charpos < endpos));
27613 tmp_glyph--)
27614 ;
27615 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27616
27617 /* Calculate the total pixel width of all the glyphs between
27618 the beginning of the highlighted area and GLYPH. */
27619 total_pixel_width = 0;
27620 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27621 total_pixel_width += tmp_glyph->pixel_width;
27622
27623 /* Pre calculation of re-rendering position. Note: X is in
27624 column units here, after the call to mode_line_string or
27625 marginal_area_string. */
27626 hpos = x - gpos;
27627 vpos = (area == ON_MODE_LINE
27628 ? (w->current_matrix)->nrows - 1
27629 : 0);
27630
27631 /* If GLYPH's position is included in the region that is
27632 already drawn in mouse face, we have nothing to do. */
27633 if ( EQ (window, hlinfo->mouse_face_window)
27634 && (!row->reversed_p
27635 ? (hlinfo->mouse_face_beg_col <= hpos
27636 && hpos < hlinfo->mouse_face_end_col)
27637 /* In R2L rows we swap BEG and END, see below. */
27638 : (hlinfo->mouse_face_end_col <= hpos
27639 && hpos < hlinfo->mouse_face_beg_col))
27640 && hlinfo->mouse_face_beg_row == vpos )
27641 return;
27642
27643 if (clear_mouse_face (hlinfo))
27644 cursor = No_Cursor;
27645
27646 if (!row->reversed_p)
27647 {
27648 hlinfo->mouse_face_beg_col = hpos;
27649 hlinfo->mouse_face_beg_x = original_x_pixel
27650 - (total_pixel_width + dx);
27651 hlinfo->mouse_face_end_col = hpos + gseq_length;
27652 hlinfo->mouse_face_end_x = 0;
27653 }
27654 else
27655 {
27656 /* In R2L rows, show_mouse_face expects BEG and END
27657 coordinates to be swapped. */
27658 hlinfo->mouse_face_end_col = hpos;
27659 hlinfo->mouse_face_end_x = original_x_pixel
27660 - (total_pixel_width + dx);
27661 hlinfo->mouse_face_beg_col = hpos + gseq_length;
27662 hlinfo->mouse_face_beg_x = 0;
27663 }
27664
27665 hlinfo->mouse_face_beg_row = vpos;
27666 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
27667 hlinfo->mouse_face_beg_y = 0;
27668 hlinfo->mouse_face_end_y = 0;
27669 hlinfo->mouse_face_past_end = 0;
27670 hlinfo->mouse_face_window = window;
27671
27672 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
27673 charpos,
27674 0, 0, 0,
27675 &ignore,
27676 glyph->face_id,
27677 1);
27678 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27679
27680 if (NILP (pointer))
27681 pointer = Qhand;
27682 }
27683 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27684 clear_mouse_face (hlinfo);
27685 }
27686 #ifdef HAVE_WINDOW_SYSTEM
27687 if (FRAME_WINDOW_P (f))
27688 define_frame_cursor1 (f, cursor, pointer);
27689 #endif
27690 }
27691
27692
27693 /* EXPORT:
27694 Take proper action when the mouse has moved to position X, Y on
27695 frame F as regards highlighting characters that have mouse-face
27696 properties. Also de-highlighting chars where the mouse was before.
27697 X and Y can be negative or out of range. */
27698
27699 void
27700 note_mouse_highlight (struct frame *f, int x, int y)
27701 {
27702 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27703 enum window_part part = ON_NOTHING;
27704 Lisp_Object window;
27705 struct window *w;
27706 Cursor cursor = No_Cursor;
27707 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
27708 struct buffer *b;
27709
27710 /* When a menu is active, don't highlight because this looks odd. */
27711 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
27712 if (popup_activated ())
27713 return;
27714 #endif
27715
27716 if (NILP (Vmouse_highlight)
27717 || !f->glyphs_initialized_p
27718 || f->pointer_invisible)
27719 return;
27720
27721 hlinfo->mouse_face_mouse_x = x;
27722 hlinfo->mouse_face_mouse_y = y;
27723 hlinfo->mouse_face_mouse_frame = f;
27724
27725 if (hlinfo->mouse_face_defer)
27726 return;
27727
27728 if (gc_in_progress)
27729 {
27730 hlinfo->mouse_face_deferred_gc = 1;
27731 return;
27732 }
27733
27734 /* Which window is that in? */
27735 window = window_from_coordinates (f, x, y, &part, 1);
27736
27737 /* If displaying active text in another window, clear that. */
27738 if (! EQ (window, hlinfo->mouse_face_window)
27739 /* Also clear if we move out of text area in same window. */
27740 || (!NILP (hlinfo->mouse_face_window)
27741 && !NILP (window)
27742 && part != ON_TEXT
27743 && part != ON_MODE_LINE
27744 && part != ON_HEADER_LINE))
27745 clear_mouse_face (hlinfo);
27746
27747 /* Not on a window -> return. */
27748 if (!WINDOWP (window))
27749 return;
27750
27751 /* Reset help_echo_string. It will get recomputed below. */
27752 help_echo_string = Qnil;
27753
27754 /* Convert to window-relative pixel coordinates. */
27755 w = XWINDOW (window);
27756 frame_to_window_pixel_xy (w, &x, &y);
27757
27758 #ifdef HAVE_WINDOW_SYSTEM
27759 /* Handle tool-bar window differently since it doesn't display a
27760 buffer. */
27761 if (EQ (window, f->tool_bar_window))
27762 {
27763 note_tool_bar_highlight (f, x, y);
27764 return;
27765 }
27766 #endif
27767
27768 /* Mouse is on the mode, header line or margin? */
27769 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
27770 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
27771 {
27772 note_mode_line_or_margin_highlight (window, x, y, part);
27773 return;
27774 }
27775
27776 #ifdef HAVE_WINDOW_SYSTEM
27777 if (part == ON_VERTICAL_BORDER)
27778 {
27779 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27780 help_echo_string = build_string ("drag-mouse-1: resize");
27781 }
27782 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
27783 || part == ON_SCROLL_BAR)
27784 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27785 else
27786 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27787 #endif
27788
27789 /* Are we in a window whose display is up to date?
27790 And verify the buffer's text has not changed. */
27791 b = XBUFFER (w->buffer);
27792 if (part == ON_TEXT
27793 && EQ (w->window_end_valid, w->buffer)
27794 && w->last_modified == BUF_MODIFF (b)
27795 && w->last_overlay_modified == BUF_OVERLAY_MODIFF (b))
27796 {
27797 int hpos, vpos, dx, dy, area = LAST_AREA;
27798 ptrdiff_t pos;
27799 struct glyph *glyph;
27800 Lisp_Object object;
27801 Lisp_Object mouse_face = Qnil, position;
27802 Lisp_Object *overlay_vec = NULL;
27803 ptrdiff_t i, noverlays;
27804 struct buffer *obuf;
27805 ptrdiff_t obegv, ozv;
27806 int same_region;
27807
27808 /* Find the glyph under X/Y. */
27809 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
27810
27811 #ifdef HAVE_WINDOW_SYSTEM
27812 /* Look for :pointer property on image. */
27813 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27814 {
27815 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27816 if (img != NULL && IMAGEP (img->spec))
27817 {
27818 Lisp_Object image_map, hotspot;
27819 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
27820 !NILP (image_map))
27821 && (hotspot = find_hot_spot (image_map,
27822 glyph->slice.img.x + dx,
27823 glyph->slice.img.y + dy),
27824 CONSP (hotspot))
27825 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27826 {
27827 Lisp_Object plist;
27828
27829 /* Could check XCAR (hotspot) to see if we enter/leave
27830 this hot-spot.
27831 If so, we could look for mouse-enter, mouse-leave
27832 properties in PLIST (and do something...). */
27833 hotspot = XCDR (hotspot);
27834 if (CONSP (hotspot)
27835 && (plist = XCAR (hotspot), CONSP (plist)))
27836 {
27837 pointer = Fplist_get (plist, Qpointer);
27838 if (NILP (pointer))
27839 pointer = Qhand;
27840 help_echo_string = Fplist_get (plist, Qhelp_echo);
27841 if (!NILP (help_echo_string))
27842 {
27843 help_echo_window = window;
27844 help_echo_object = glyph->object;
27845 help_echo_pos = glyph->charpos;
27846 }
27847 }
27848 }
27849 if (NILP (pointer))
27850 pointer = Fplist_get (XCDR (img->spec), QCpointer);
27851 }
27852 }
27853 #endif /* HAVE_WINDOW_SYSTEM */
27854
27855 /* Clear mouse face if X/Y not over text. */
27856 if (glyph == NULL
27857 || area != TEXT_AREA
27858 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
27859 /* Glyph's OBJECT is an integer for glyphs inserted by the
27860 display engine for its internal purposes, like truncation
27861 and continuation glyphs and blanks beyond the end of
27862 line's text on text terminals. If we are over such a
27863 glyph, we are not over any text. */
27864 || INTEGERP (glyph->object)
27865 /* R2L rows have a stretch glyph at their front, which
27866 stands for no text, whereas L2R rows have no glyphs at
27867 all beyond the end of text. Treat such stretch glyphs
27868 like we do with NULL glyphs in L2R rows. */
27869 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
27870 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
27871 && glyph->type == STRETCH_GLYPH
27872 && glyph->avoid_cursor_p))
27873 {
27874 if (clear_mouse_face (hlinfo))
27875 cursor = No_Cursor;
27876 #ifdef HAVE_WINDOW_SYSTEM
27877 if (FRAME_WINDOW_P (f) && NILP (pointer))
27878 {
27879 if (area != TEXT_AREA)
27880 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27881 else
27882 pointer = Vvoid_text_area_pointer;
27883 }
27884 #endif
27885 goto set_cursor;
27886 }
27887
27888 pos = glyph->charpos;
27889 object = glyph->object;
27890 if (!STRINGP (object) && !BUFFERP (object))
27891 goto set_cursor;
27892
27893 /* If we get an out-of-range value, return now; avoid an error. */
27894 if (BUFFERP (object) && pos > BUF_Z (b))
27895 goto set_cursor;
27896
27897 /* Make the window's buffer temporarily current for
27898 overlays_at and compute_char_face. */
27899 obuf = current_buffer;
27900 current_buffer = b;
27901 obegv = BEGV;
27902 ozv = ZV;
27903 BEGV = BEG;
27904 ZV = Z;
27905
27906 /* Is this char mouse-active or does it have help-echo? */
27907 position = make_number (pos);
27908
27909 if (BUFFERP (object))
27910 {
27911 /* Put all the overlays we want in a vector in overlay_vec. */
27912 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
27913 /* Sort overlays into increasing priority order. */
27914 noverlays = sort_overlays (overlay_vec, noverlays, w);
27915 }
27916 else
27917 noverlays = 0;
27918
27919 same_region = coords_in_mouse_face_p (w, hpos, vpos);
27920
27921 if (same_region)
27922 cursor = No_Cursor;
27923
27924 /* Check mouse-face highlighting. */
27925 if (! same_region
27926 /* If there exists an overlay with mouse-face overlapping
27927 the one we are currently highlighting, we have to
27928 check if we enter the overlapping overlay, and then
27929 highlight only that. */
27930 || (OVERLAYP (hlinfo->mouse_face_overlay)
27931 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
27932 {
27933 /* Find the highest priority overlay with a mouse-face. */
27934 Lisp_Object overlay = Qnil;
27935 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
27936 {
27937 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
27938 if (!NILP (mouse_face))
27939 overlay = overlay_vec[i];
27940 }
27941
27942 /* If we're highlighting the same overlay as before, there's
27943 no need to do that again. */
27944 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
27945 goto check_help_echo;
27946 hlinfo->mouse_face_overlay = overlay;
27947
27948 /* Clear the display of the old active region, if any. */
27949 if (clear_mouse_face (hlinfo))
27950 cursor = No_Cursor;
27951
27952 /* If no overlay applies, get a text property. */
27953 if (NILP (overlay))
27954 mouse_face = Fget_text_property (position, Qmouse_face, object);
27955
27956 /* Next, compute the bounds of the mouse highlighting and
27957 display it. */
27958 if (!NILP (mouse_face) && STRINGP (object))
27959 {
27960 /* The mouse-highlighting comes from a display string
27961 with a mouse-face. */
27962 Lisp_Object s, e;
27963 ptrdiff_t ignore;
27964
27965 s = Fprevious_single_property_change
27966 (make_number (pos + 1), Qmouse_face, object, Qnil);
27967 e = Fnext_single_property_change
27968 (position, Qmouse_face, object, Qnil);
27969 if (NILP (s))
27970 s = make_number (0);
27971 if (NILP (e))
27972 e = make_number (SCHARS (object) - 1);
27973 mouse_face_from_string_pos (w, hlinfo, object,
27974 XINT (s), XINT (e));
27975 hlinfo->mouse_face_past_end = 0;
27976 hlinfo->mouse_face_window = window;
27977 hlinfo->mouse_face_face_id
27978 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
27979 glyph->face_id, 1);
27980 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27981 cursor = No_Cursor;
27982 }
27983 else
27984 {
27985 /* The mouse-highlighting, if any, comes from an overlay
27986 or text property in the buffer. */
27987 Lisp_Object buffer IF_LINT (= Qnil);
27988 Lisp_Object disp_string IF_LINT (= Qnil);
27989
27990 if (STRINGP (object))
27991 {
27992 /* If we are on a display string with no mouse-face,
27993 check if the text under it has one. */
27994 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
27995 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27996 pos = string_buffer_position (object, start);
27997 if (pos > 0)
27998 {
27999 mouse_face = get_char_property_and_overlay
28000 (make_number (pos), Qmouse_face, w->buffer, &overlay);
28001 buffer = w->buffer;
28002 disp_string = object;
28003 }
28004 }
28005 else
28006 {
28007 buffer = object;
28008 disp_string = Qnil;
28009 }
28010
28011 if (!NILP (mouse_face))
28012 {
28013 Lisp_Object before, after;
28014 Lisp_Object before_string, after_string;
28015 /* To correctly find the limits of mouse highlight
28016 in a bidi-reordered buffer, we must not use the
28017 optimization of limiting the search in
28018 previous-single-property-change and
28019 next-single-property-change, because
28020 rows_from_pos_range needs the real start and end
28021 positions to DTRT in this case. That's because
28022 the first row visible in a window does not
28023 necessarily display the character whose position
28024 is the smallest. */
28025 Lisp_Object lim1 =
28026 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28027 ? Fmarker_position (w->start)
28028 : Qnil;
28029 Lisp_Object lim2 =
28030 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28031 ? make_number (BUF_Z (XBUFFER (buffer))
28032 - XFASTINT (w->window_end_pos))
28033 : Qnil;
28034
28035 if (NILP (overlay))
28036 {
28037 /* Handle the text property case. */
28038 before = Fprevious_single_property_change
28039 (make_number (pos + 1), Qmouse_face, buffer, lim1);
28040 after = Fnext_single_property_change
28041 (make_number (pos), Qmouse_face, buffer, lim2);
28042 before_string = after_string = Qnil;
28043 }
28044 else
28045 {
28046 /* Handle the overlay case. */
28047 before = Foverlay_start (overlay);
28048 after = Foverlay_end (overlay);
28049 before_string = Foverlay_get (overlay, Qbefore_string);
28050 after_string = Foverlay_get (overlay, Qafter_string);
28051
28052 if (!STRINGP (before_string)) before_string = Qnil;
28053 if (!STRINGP (after_string)) after_string = Qnil;
28054 }
28055
28056 mouse_face_from_buffer_pos (window, hlinfo, pos,
28057 NILP (before)
28058 ? 1
28059 : XFASTINT (before),
28060 NILP (after)
28061 ? BUF_Z (XBUFFER (buffer))
28062 : XFASTINT (after),
28063 before_string, after_string,
28064 disp_string);
28065 cursor = No_Cursor;
28066 }
28067 }
28068 }
28069
28070 check_help_echo:
28071
28072 /* Look for a `help-echo' property. */
28073 if (NILP (help_echo_string)) {
28074 Lisp_Object help, overlay;
28075
28076 /* Check overlays first. */
28077 help = overlay = Qnil;
28078 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
28079 {
28080 overlay = overlay_vec[i];
28081 help = Foverlay_get (overlay, Qhelp_echo);
28082 }
28083
28084 if (!NILP (help))
28085 {
28086 help_echo_string = help;
28087 help_echo_window = window;
28088 help_echo_object = overlay;
28089 help_echo_pos = pos;
28090 }
28091 else
28092 {
28093 Lisp_Object obj = glyph->object;
28094 ptrdiff_t charpos = glyph->charpos;
28095
28096 /* Try text properties. */
28097 if (STRINGP (obj)
28098 && charpos >= 0
28099 && charpos < SCHARS (obj))
28100 {
28101 help = Fget_text_property (make_number (charpos),
28102 Qhelp_echo, obj);
28103 if (NILP (help))
28104 {
28105 /* If the string itself doesn't specify a help-echo,
28106 see if the buffer text ``under'' it does. */
28107 struct glyph_row *r
28108 = MATRIX_ROW (w->current_matrix, vpos);
28109 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28110 ptrdiff_t p = string_buffer_position (obj, start);
28111 if (p > 0)
28112 {
28113 help = Fget_char_property (make_number (p),
28114 Qhelp_echo, w->buffer);
28115 if (!NILP (help))
28116 {
28117 charpos = p;
28118 obj = w->buffer;
28119 }
28120 }
28121 }
28122 }
28123 else if (BUFFERP (obj)
28124 && charpos >= BEGV
28125 && charpos < ZV)
28126 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28127 obj);
28128
28129 if (!NILP (help))
28130 {
28131 help_echo_string = help;
28132 help_echo_window = window;
28133 help_echo_object = obj;
28134 help_echo_pos = charpos;
28135 }
28136 }
28137 }
28138
28139 #ifdef HAVE_WINDOW_SYSTEM
28140 /* Look for a `pointer' property. */
28141 if (FRAME_WINDOW_P (f) && NILP (pointer))
28142 {
28143 /* Check overlays first. */
28144 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28145 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28146
28147 if (NILP (pointer))
28148 {
28149 Lisp_Object obj = glyph->object;
28150 ptrdiff_t charpos = glyph->charpos;
28151
28152 /* Try text properties. */
28153 if (STRINGP (obj)
28154 && charpos >= 0
28155 && charpos < SCHARS (obj))
28156 {
28157 pointer = Fget_text_property (make_number (charpos),
28158 Qpointer, obj);
28159 if (NILP (pointer))
28160 {
28161 /* If the string itself doesn't specify a pointer,
28162 see if the buffer text ``under'' it does. */
28163 struct glyph_row *r
28164 = MATRIX_ROW (w->current_matrix, vpos);
28165 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28166 ptrdiff_t p = string_buffer_position (obj, start);
28167 if (p > 0)
28168 pointer = Fget_char_property (make_number (p),
28169 Qpointer, w->buffer);
28170 }
28171 }
28172 else if (BUFFERP (obj)
28173 && charpos >= BEGV
28174 && charpos < ZV)
28175 pointer = Fget_text_property (make_number (charpos),
28176 Qpointer, obj);
28177 }
28178 }
28179 #endif /* HAVE_WINDOW_SYSTEM */
28180
28181 BEGV = obegv;
28182 ZV = ozv;
28183 current_buffer = obuf;
28184 }
28185
28186 set_cursor:
28187
28188 #ifdef HAVE_WINDOW_SYSTEM
28189 if (FRAME_WINDOW_P (f))
28190 define_frame_cursor1 (f, cursor, pointer);
28191 #else
28192 /* This is here to prevent a compiler error, about "label at end of
28193 compound statement". */
28194 return;
28195 #endif
28196 }
28197
28198
28199 /* EXPORT for RIF:
28200 Clear any mouse-face on window W. This function is part of the
28201 redisplay interface, and is called from try_window_id and similar
28202 functions to ensure the mouse-highlight is off. */
28203
28204 void
28205 x_clear_window_mouse_face (struct window *w)
28206 {
28207 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28208 Lisp_Object window;
28209
28210 block_input ();
28211 XSETWINDOW (window, w);
28212 if (EQ (window, hlinfo->mouse_face_window))
28213 clear_mouse_face (hlinfo);
28214 unblock_input ();
28215 }
28216
28217
28218 /* EXPORT:
28219 Just discard the mouse face information for frame F, if any.
28220 This is used when the size of F is changed. */
28221
28222 void
28223 cancel_mouse_face (struct frame *f)
28224 {
28225 Lisp_Object window;
28226 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28227
28228 window = hlinfo->mouse_face_window;
28229 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28230 {
28231 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28232 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28233 hlinfo->mouse_face_window = Qnil;
28234 }
28235 }
28236
28237
28238 \f
28239 /***********************************************************************
28240 Exposure Events
28241 ***********************************************************************/
28242
28243 #ifdef HAVE_WINDOW_SYSTEM
28244
28245 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28246 which intersects rectangle R. R is in window-relative coordinates. */
28247
28248 static void
28249 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28250 enum glyph_row_area area)
28251 {
28252 struct glyph *first = row->glyphs[area];
28253 struct glyph *end = row->glyphs[area] + row->used[area];
28254 struct glyph *last;
28255 int first_x, start_x, x;
28256
28257 if (area == TEXT_AREA && row->fill_line_p)
28258 /* If row extends face to end of line write the whole line. */
28259 draw_glyphs (w, 0, row, area,
28260 0, row->used[area],
28261 DRAW_NORMAL_TEXT, 0);
28262 else
28263 {
28264 /* Set START_X to the window-relative start position for drawing glyphs of
28265 AREA. The first glyph of the text area can be partially visible.
28266 The first glyphs of other areas cannot. */
28267 start_x = window_box_left_offset (w, area);
28268 x = start_x;
28269 if (area == TEXT_AREA)
28270 x += row->x;
28271
28272 /* Find the first glyph that must be redrawn. */
28273 while (first < end
28274 && x + first->pixel_width < r->x)
28275 {
28276 x += first->pixel_width;
28277 ++first;
28278 }
28279
28280 /* Find the last one. */
28281 last = first;
28282 first_x = x;
28283 while (last < end
28284 && x < r->x + r->width)
28285 {
28286 x += last->pixel_width;
28287 ++last;
28288 }
28289
28290 /* Repaint. */
28291 if (last > first)
28292 draw_glyphs (w, first_x - start_x, row, area,
28293 first - row->glyphs[area], last - row->glyphs[area],
28294 DRAW_NORMAL_TEXT, 0);
28295 }
28296 }
28297
28298
28299 /* Redraw the parts of the glyph row ROW on window W intersecting
28300 rectangle R. R is in window-relative coordinates. Value is
28301 non-zero if mouse-face was overwritten. */
28302
28303 static int
28304 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28305 {
28306 eassert (row->enabled_p);
28307
28308 if (row->mode_line_p || w->pseudo_window_p)
28309 draw_glyphs (w, 0, row, TEXT_AREA,
28310 0, row->used[TEXT_AREA],
28311 DRAW_NORMAL_TEXT, 0);
28312 else
28313 {
28314 if (row->used[LEFT_MARGIN_AREA])
28315 expose_area (w, row, r, LEFT_MARGIN_AREA);
28316 if (row->used[TEXT_AREA])
28317 expose_area (w, row, r, TEXT_AREA);
28318 if (row->used[RIGHT_MARGIN_AREA])
28319 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28320 draw_row_fringe_bitmaps (w, row);
28321 }
28322
28323 return row->mouse_face_p;
28324 }
28325
28326
28327 /* Redraw those parts of glyphs rows during expose event handling that
28328 overlap other rows. Redrawing of an exposed line writes over parts
28329 of lines overlapping that exposed line; this function fixes that.
28330
28331 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28332 row in W's current matrix that is exposed and overlaps other rows.
28333 LAST_OVERLAPPING_ROW is the last such row. */
28334
28335 static void
28336 expose_overlaps (struct window *w,
28337 struct glyph_row *first_overlapping_row,
28338 struct glyph_row *last_overlapping_row,
28339 XRectangle *r)
28340 {
28341 struct glyph_row *row;
28342
28343 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28344 if (row->overlapping_p)
28345 {
28346 eassert (row->enabled_p && !row->mode_line_p);
28347
28348 row->clip = r;
28349 if (row->used[LEFT_MARGIN_AREA])
28350 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28351
28352 if (row->used[TEXT_AREA])
28353 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28354
28355 if (row->used[RIGHT_MARGIN_AREA])
28356 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28357 row->clip = NULL;
28358 }
28359 }
28360
28361
28362 /* Return non-zero if W's cursor intersects rectangle R. */
28363
28364 static int
28365 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28366 {
28367 XRectangle cr, result;
28368 struct glyph *cursor_glyph;
28369 struct glyph_row *row;
28370
28371 if (w->phys_cursor.vpos >= 0
28372 && w->phys_cursor.vpos < w->current_matrix->nrows
28373 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28374 row->enabled_p)
28375 && row->cursor_in_fringe_p)
28376 {
28377 /* Cursor is in the fringe. */
28378 cr.x = window_box_right_offset (w,
28379 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28380 ? RIGHT_MARGIN_AREA
28381 : TEXT_AREA));
28382 cr.y = row->y;
28383 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28384 cr.height = row->height;
28385 return x_intersect_rectangles (&cr, r, &result);
28386 }
28387
28388 cursor_glyph = get_phys_cursor_glyph (w);
28389 if (cursor_glyph)
28390 {
28391 /* r is relative to W's box, but w->phys_cursor.x is relative
28392 to left edge of W's TEXT area. Adjust it. */
28393 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28394 cr.y = w->phys_cursor.y;
28395 cr.width = cursor_glyph->pixel_width;
28396 cr.height = w->phys_cursor_height;
28397 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28398 I assume the effect is the same -- and this is portable. */
28399 return x_intersect_rectangles (&cr, r, &result);
28400 }
28401 /* If we don't understand the format, pretend we're not in the hot-spot. */
28402 return 0;
28403 }
28404
28405
28406 /* EXPORT:
28407 Draw a vertical window border to the right of window W if W doesn't
28408 have vertical scroll bars. */
28409
28410 void
28411 x_draw_vertical_border (struct window *w)
28412 {
28413 struct frame *f = XFRAME (WINDOW_FRAME (w));
28414
28415 /* We could do better, if we knew what type of scroll-bar the adjacent
28416 windows (on either side) have... But we don't :-(
28417 However, I think this works ok. ++KFS 2003-04-25 */
28418
28419 /* Redraw borders between horizontally adjacent windows. Don't
28420 do it for frames with vertical scroll bars because either the
28421 right scroll bar of a window, or the left scroll bar of its
28422 neighbor will suffice as a border. */
28423 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28424 return;
28425
28426 /* Note: It is necessary to redraw both the left and the right
28427 borders, for when only this single window W is being
28428 redisplayed. */
28429 if (!WINDOW_RIGHTMOST_P (w)
28430 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28431 {
28432 int x0, x1, y0, y1;
28433
28434 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28435 y1 -= 1;
28436
28437 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28438 x1 -= 1;
28439
28440 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28441 }
28442 if (!WINDOW_LEFTMOST_P (w)
28443 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28444 {
28445 int x0, x1, y0, y1;
28446
28447 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28448 y1 -= 1;
28449
28450 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28451 x0 -= 1;
28452
28453 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28454 }
28455 }
28456
28457
28458 /* Redraw the part of window W intersection rectangle FR. Pixel
28459 coordinates in FR are frame-relative. Call this function with
28460 input blocked. Value is non-zero if the exposure overwrites
28461 mouse-face. */
28462
28463 static int
28464 expose_window (struct window *w, XRectangle *fr)
28465 {
28466 struct frame *f = XFRAME (w->frame);
28467 XRectangle wr, r;
28468 int mouse_face_overwritten_p = 0;
28469
28470 /* If window is not yet fully initialized, do nothing. This can
28471 happen when toolkit scroll bars are used and a window is split.
28472 Reconfiguring the scroll bar will generate an expose for a newly
28473 created window. */
28474 if (w->current_matrix == NULL)
28475 return 0;
28476
28477 /* When we're currently updating the window, display and current
28478 matrix usually don't agree. Arrange for a thorough display
28479 later. */
28480 if (w == updated_window)
28481 {
28482 SET_FRAME_GARBAGED (f);
28483 return 0;
28484 }
28485
28486 /* Frame-relative pixel rectangle of W. */
28487 wr.x = WINDOW_LEFT_EDGE_X (w);
28488 wr.y = WINDOW_TOP_EDGE_Y (w);
28489 wr.width = WINDOW_TOTAL_WIDTH (w);
28490 wr.height = WINDOW_TOTAL_HEIGHT (w);
28491
28492 if (x_intersect_rectangles (fr, &wr, &r))
28493 {
28494 int yb = window_text_bottom_y (w);
28495 struct glyph_row *row;
28496 int cursor_cleared_p, phys_cursor_on_p;
28497 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28498
28499 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28500 r.x, r.y, r.width, r.height));
28501
28502 /* Convert to window coordinates. */
28503 r.x -= WINDOW_LEFT_EDGE_X (w);
28504 r.y -= WINDOW_TOP_EDGE_Y (w);
28505
28506 /* Turn off the cursor. */
28507 if (!w->pseudo_window_p
28508 && phys_cursor_in_rect_p (w, &r))
28509 {
28510 x_clear_cursor (w);
28511 cursor_cleared_p = 1;
28512 }
28513 else
28514 cursor_cleared_p = 0;
28515
28516 /* If the row containing the cursor extends face to end of line,
28517 then expose_area might overwrite the cursor outside the
28518 rectangle and thus notice_overwritten_cursor might clear
28519 w->phys_cursor_on_p. We remember the original value and
28520 check later if it is changed. */
28521 phys_cursor_on_p = w->phys_cursor_on_p;
28522
28523 /* Update lines intersecting rectangle R. */
28524 first_overlapping_row = last_overlapping_row = NULL;
28525 for (row = w->current_matrix->rows;
28526 row->enabled_p;
28527 ++row)
28528 {
28529 int y0 = row->y;
28530 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28531
28532 if ((y0 >= r.y && y0 < r.y + r.height)
28533 || (y1 > r.y && y1 < r.y + r.height)
28534 || (r.y >= y0 && r.y < y1)
28535 || (r.y + r.height > y0 && r.y + r.height < y1))
28536 {
28537 /* A header line may be overlapping, but there is no need
28538 to fix overlapping areas for them. KFS 2005-02-12 */
28539 if (row->overlapping_p && !row->mode_line_p)
28540 {
28541 if (first_overlapping_row == NULL)
28542 first_overlapping_row = row;
28543 last_overlapping_row = row;
28544 }
28545
28546 row->clip = fr;
28547 if (expose_line (w, row, &r))
28548 mouse_face_overwritten_p = 1;
28549 row->clip = NULL;
28550 }
28551 else if (row->overlapping_p)
28552 {
28553 /* We must redraw a row overlapping the exposed area. */
28554 if (y0 < r.y
28555 ? y0 + row->phys_height > r.y
28556 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28557 {
28558 if (first_overlapping_row == NULL)
28559 first_overlapping_row = row;
28560 last_overlapping_row = row;
28561 }
28562 }
28563
28564 if (y1 >= yb)
28565 break;
28566 }
28567
28568 /* Display the mode line if there is one. */
28569 if (WINDOW_WANTS_MODELINE_P (w)
28570 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28571 row->enabled_p)
28572 && row->y < r.y + r.height)
28573 {
28574 if (expose_line (w, row, &r))
28575 mouse_face_overwritten_p = 1;
28576 }
28577
28578 if (!w->pseudo_window_p)
28579 {
28580 /* Fix the display of overlapping rows. */
28581 if (first_overlapping_row)
28582 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28583 fr);
28584
28585 /* Draw border between windows. */
28586 x_draw_vertical_border (w);
28587
28588 /* Turn the cursor on again. */
28589 if (cursor_cleared_p
28590 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28591 update_window_cursor (w, 1);
28592 }
28593 }
28594
28595 return mouse_face_overwritten_p;
28596 }
28597
28598
28599
28600 /* Redraw (parts) of all windows in the window tree rooted at W that
28601 intersect R. R contains frame pixel coordinates. Value is
28602 non-zero if the exposure overwrites mouse-face. */
28603
28604 static int
28605 expose_window_tree (struct window *w, XRectangle *r)
28606 {
28607 struct frame *f = XFRAME (w->frame);
28608 int mouse_face_overwritten_p = 0;
28609
28610 while (w && !FRAME_GARBAGED_P (f))
28611 {
28612 if (!NILP (w->hchild))
28613 mouse_face_overwritten_p
28614 |= expose_window_tree (XWINDOW (w->hchild), r);
28615 else if (!NILP (w->vchild))
28616 mouse_face_overwritten_p
28617 |= expose_window_tree (XWINDOW (w->vchild), r);
28618 else
28619 mouse_face_overwritten_p |= expose_window (w, r);
28620
28621 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28622 }
28623
28624 return mouse_face_overwritten_p;
28625 }
28626
28627
28628 /* EXPORT:
28629 Redisplay an exposed area of frame F. X and Y are the upper-left
28630 corner of the exposed rectangle. W and H are width and height of
28631 the exposed area. All are pixel values. W or H zero means redraw
28632 the entire frame. */
28633
28634 void
28635 expose_frame (struct frame *f, int x, int y, int w, int h)
28636 {
28637 XRectangle r;
28638 int mouse_face_overwritten_p = 0;
28639
28640 TRACE ((stderr, "expose_frame "));
28641
28642 /* No need to redraw if frame will be redrawn soon. */
28643 if (FRAME_GARBAGED_P (f))
28644 {
28645 TRACE ((stderr, " garbaged\n"));
28646 return;
28647 }
28648
28649 /* If basic faces haven't been realized yet, there is no point in
28650 trying to redraw anything. This can happen when we get an expose
28651 event while Emacs is starting, e.g. by moving another window. */
28652 if (FRAME_FACE_CACHE (f) == NULL
28653 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28654 {
28655 TRACE ((stderr, " no faces\n"));
28656 return;
28657 }
28658
28659 if (w == 0 || h == 0)
28660 {
28661 r.x = r.y = 0;
28662 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
28663 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
28664 }
28665 else
28666 {
28667 r.x = x;
28668 r.y = y;
28669 r.width = w;
28670 r.height = h;
28671 }
28672
28673 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
28674 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
28675
28676 if (WINDOWP (f->tool_bar_window))
28677 mouse_face_overwritten_p
28678 |= expose_window (XWINDOW (f->tool_bar_window), &r);
28679
28680 #ifdef HAVE_X_WINDOWS
28681 #ifndef MSDOS
28682 #ifndef USE_X_TOOLKIT
28683 if (WINDOWP (f->menu_bar_window))
28684 mouse_face_overwritten_p
28685 |= expose_window (XWINDOW (f->menu_bar_window), &r);
28686 #endif /* not USE_X_TOOLKIT */
28687 #endif
28688 #endif
28689
28690 /* Some window managers support a focus-follows-mouse style with
28691 delayed raising of frames. Imagine a partially obscured frame,
28692 and moving the mouse into partially obscured mouse-face on that
28693 frame. The visible part of the mouse-face will be highlighted,
28694 then the WM raises the obscured frame. With at least one WM, KDE
28695 2.1, Emacs is not getting any event for the raising of the frame
28696 (even tried with SubstructureRedirectMask), only Expose events.
28697 These expose events will draw text normally, i.e. not
28698 highlighted. Which means we must redo the highlight here.
28699 Subsume it under ``we love X''. --gerd 2001-08-15 */
28700 /* Included in Windows version because Windows most likely does not
28701 do the right thing if any third party tool offers
28702 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
28703 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
28704 {
28705 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28706 if (f == hlinfo->mouse_face_mouse_frame)
28707 {
28708 int mouse_x = hlinfo->mouse_face_mouse_x;
28709 int mouse_y = hlinfo->mouse_face_mouse_y;
28710 clear_mouse_face (hlinfo);
28711 note_mouse_highlight (f, mouse_x, mouse_y);
28712 }
28713 }
28714 }
28715
28716
28717 /* EXPORT:
28718 Determine the intersection of two rectangles R1 and R2. Return
28719 the intersection in *RESULT. Value is non-zero if RESULT is not
28720 empty. */
28721
28722 int
28723 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
28724 {
28725 XRectangle *left, *right;
28726 XRectangle *upper, *lower;
28727 int intersection_p = 0;
28728
28729 /* Rearrange so that R1 is the left-most rectangle. */
28730 if (r1->x < r2->x)
28731 left = r1, right = r2;
28732 else
28733 left = r2, right = r1;
28734
28735 /* X0 of the intersection is right.x0, if this is inside R1,
28736 otherwise there is no intersection. */
28737 if (right->x <= left->x + left->width)
28738 {
28739 result->x = right->x;
28740
28741 /* The right end of the intersection is the minimum of
28742 the right ends of left and right. */
28743 result->width = (min (left->x + left->width, right->x + right->width)
28744 - result->x);
28745
28746 /* Same game for Y. */
28747 if (r1->y < r2->y)
28748 upper = r1, lower = r2;
28749 else
28750 upper = r2, lower = r1;
28751
28752 /* The upper end of the intersection is lower.y0, if this is inside
28753 of upper. Otherwise, there is no intersection. */
28754 if (lower->y <= upper->y + upper->height)
28755 {
28756 result->y = lower->y;
28757
28758 /* The lower end of the intersection is the minimum of the lower
28759 ends of upper and lower. */
28760 result->height = (min (lower->y + lower->height,
28761 upper->y + upper->height)
28762 - result->y);
28763 intersection_p = 1;
28764 }
28765 }
28766
28767 return intersection_p;
28768 }
28769
28770 #endif /* HAVE_WINDOW_SYSTEM */
28771
28772 \f
28773 /***********************************************************************
28774 Initialization
28775 ***********************************************************************/
28776
28777 void
28778 syms_of_xdisp (void)
28779 {
28780 Vwith_echo_area_save_vector = Qnil;
28781 staticpro (&Vwith_echo_area_save_vector);
28782
28783 Vmessage_stack = Qnil;
28784 staticpro (&Vmessage_stack);
28785
28786 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
28787 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
28788
28789 message_dolog_marker1 = Fmake_marker ();
28790 staticpro (&message_dolog_marker1);
28791 message_dolog_marker2 = Fmake_marker ();
28792 staticpro (&message_dolog_marker2);
28793 message_dolog_marker3 = Fmake_marker ();
28794 staticpro (&message_dolog_marker3);
28795
28796 #ifdef GLYPH_DEBUG
28797 defsubr (&Sdump_frame_glyph_matrix);
28798 defsubr (&Sdump_glyph_matrix);
28799 defsubr (&Sdump_glyph_row);
28800 defsubr (&Sdump_tool_bar_row);
28801 defsubr (&Strace_redisplay);
28802 defsubr (&Strace_to_stderr);
28803 #endif
28804 #ifdef HAVE_WINDOW_SYSTEM
28805 defsubr (&Stool_bar_lines_needed);
28806 defsubr (&Slookup_image_map);
28807 #endif
28808 defsubr (&Sformat_mode_line);
28809 defsubr (&Sinvisible_p);
28810 defsubr (&Scurrent_bidi_paragraph_direction);
28811
28812 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
28813 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
28814 DEFSYM (Qoverriding_local_map, "overriding-local-map");
28815 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
28816 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
28817 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
28818 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
28819 DEFSYM (Qeval, "eval");
28820 DEFSYM (QCdata, ":data");
28821 DEFSYM (Qdisplay, "display");
28822 DEFSYM (Qspace_width, "space-width");
28823 DEFSYM (Qraise, "raise");
28824 DEFSYM (Qslice, "slice");
28825 DEFSYM (Qspace, "space");
28826 DEFSYM (Qmargin, "margin");
28827 DEFSYM (Qpointer, "pointer");
28828 DEFSYM (Qleft_margin, "left-margin");
28829 DEFSYM (Qright_margin, "right-margin");
28830 DEFSYM (Qcenter, "center");
28831 DEFSYM (Qline_height, "line-height");
28832 DEFSYM (QCalign_to, ":align-to");
28833 DEFSYM (QCrelative_width, ":relative-width");
28834 DEFSYM (QCrelative_height, ":relative-height");
28835 DEFSYM (QCeval, ":eval");
28836 DEFSYM (QCpropertize, ":propertize");
28837 DEFSYM (QCfile, ":file");
28838 DEFSYM (Qfontified, "fontified");
28839 DEFSYM (Qfontification_functions, "fontification-functions");
28840 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
28841 DEFSYM (Qescape_glyph, "escape-glyph");
28842 DEFSYM (Qnobreak_space, "nobreak-space");
28843 DEFSYM (Qimage, "image");
28844 DEFSYM (Qtext, "text");
28845 DEFSYM (Qboth, "both");
28846 DEFSYM (Qboth_horiz, "both-horiz");
28847 DEFSYM (Qtext_image_horiz, "text-image-horiz");
28848 DEFSYM (QCmap, ":map");
28849 DEFSYM (QCpointer, ":pointer");
28850 DEFSYM (Qrect, "rect");
28851 DEFSYM (Qcircle, "circle");
28852 DEFSYM (Qpoly, "poly");
28853 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
28854 DEFSYM (Qgrow_only, "grow-only");
28855 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
28856 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
28857 DEFSYM (Qposition, "position");
28858 DEFSYM (Qbuffer_position, "buffer-position");
28859 DEFSYM (Qobject, "object");
28860 DEFSYM (Qbar, "bar");
28861 DEFSYM (Qhbar, "hbar");
28862 DEFSYM (Qbox, "box");
28863 DEFSYM (Qhollow, "hollow");
28864 DEFSYM (Qhand, "hand");
28865 DEFSYM (Qarrow, "arrow");
28866 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
28867
28868 list_of_error = Fcons (Fcons (intern_c_string ("error"),
28869 Fcons (intern_c_string ("void-variable"), Qnil)),
28870 Qnil);
28871 staticpro (&list_of_error);
28872
28873 DEFSYM (Qlast_arrow_position, "last-arrow-position");
28874 DEFSYM (Qlast_arrow_string, "last-arrow-string");
28875 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
28876 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
28877
28878 echo_buffer[0] = echo_buffer[1] = Qnil;
28879 staticpro (&echo_buffer[0]);
28880 staticpro (&echo_buffer[1]);
28881
28882 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
28883 staticpro (&echo_area_buffer[0]);
28884 staticpro (&echo_area_buffer[1]);
28885
28886 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
28887 staticpro (&Vmessages_buffer_name);
28888
28889 mode_line_proptrans_alist = Qnil;
28890 staticpro (&mode_line_proptrans_alist);
28891 mode_line_string_list = Qnil;
28892 staticpro (&mode_line_string_list);
28893 mode_line_string_face = Qnil;
28894 staticpro (&mode_line_string_face);
28895 mode_line_string_face_prop = Qnil;
28896 staticpro (&mode_line_string_face_prop);
28897 Vmode_line_unwind_vector = Qnil;
28898 staticpro (&Vmode_line_unwind_vector);
28899
28900 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
28901
28902 help_echo_string = Qnil;
28903 staticpro (&help_echo_string);
28904 help_echo_object = Qnil;
28905 staticpro (&help_echo_object);
28906 help_echo_window = Qnil;
28907 staticpro (&help_echo_window);
28908 previous_help_echo_string = Qnil;
28909 staticpro (&previous_help_echo_string);
28910 help_echo_pos = -1;
28911
28912 DEFSYM (Qright_to_left, "right-to-left");
28913 DEFSYM (Qleft_to_right, "left-to-right");
28914
28915 #ifdef HAVE_WINDOW_SYSTEM
28916 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
28917 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
28918 For example, if a block cursor is over a tab, it will be drawn as
28919 wide as that tab on the display. */);
28920 x_stretch_cursor_p = 0;
28921 #endif
28922
28923 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
28924 doc: /* Non-nil means highlight trailing whitespace.
28925 The face used for trailing whitespace is `trailing-whitespace'. */);
28926 Vshow_trailing_whitespace = Qnil;
28927
28928 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
28929 doc: /* Control highlighting of non-ASCII space and hyphen chars.
28930 If the value is t, Emacs highlights non-ASCII chars which have the
28931 same appearance as an ASCII space or hyphen, using the `nobreak-space'
28932 or `escape-glyph' face respectively.
28933
28934 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
28935 U+2011 (non-breaking hyphen) are affected.
28936
28937 Any other non-nil value means to display these characters as a escape
28938 glyph followed by an ordinary space or hyphen.
28939
28940 A value of nil means no special handling of these characters. */);
28941 Vnobreak_char_display = Qt;
28942
28943 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
28944 doc: /* The pointer shape to show in void text areas.
28945 A value of nil means to show the text pointer. Other options are `arrow',
28946 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
28947 Vvoid_text_area_pointer = Qarrow;
28948
28949 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
28950 doc: /* Non-nil means don't actually do any redisplay.
28951 This is used for internal purposes. */);
28952 Vinhibit_redisplay = Qnil;
28953
28954 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
28955 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
28956 Vglobal_mode_string = Qnil;
28957
28958 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
28959 doc: /* Marker for where to display an arrow on top of the buffer text.
28960 This must be the beginning of a line in order to work.
28961 See also `overlay-arrow-string'. */);
28962 Voverlay_arrow_position = Qnil;
28963
28964 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
28965 doc: /* String to display as an arrow in non-window frames.
28966 See also `overlay-arrow-position'. */);
28967 Voverlay_arrow_string = build_pure_c_string ("=>");
28968
28969 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
28970 doc: /* List of variables (symbols) which hold markers for overlay arrows.
28971 The symbols on this list are examined during redisplay to determine
28972 where to display overlay arrows. */);
28973 Voverlay_arrow_variable_list
28974 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
28975
28976 DEFVAR_INT ("scroll-step", emacs_scroll_step,
28977 doc: /* The number of lines to try scrolling a window by when point moves out.
28978 If that fails to bring point back on frame, point is centered instead.
28979 If this is zero, point is always centered after it moves off frame.
28980 If you want scrolling to always be a line at a time, you should set
28981 `scroll-conservatively' to a large value rather than set this to 1. */);
28982
28983 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
28984 doc: /* Scroll up to this many lines, to bring point back on screen.
28985 If point moves off-screen, redisplay will scroll by up to
28986 `scroll-conservatively' lines in order to bring point just barely
28987 onto the screen again. If that cannot be done, then redisplay
28988 recenters point as usual.
28989
28990 If the value is greater than 100, redisplay will never recenter point,
28991 but will always scroll just enough text to bring point into view, even
28992 if you move far away.
28993
28994 A value of zero means always recenter point if it moves off screen. */);
28995 scroll_conservatively = 0;
28996
28997 DEFVAR_INT ("scroll-margin", scroll_margin,
28998 doc: /* Number of lines of margin at the top and bottom of a window.
28999 Recenter the window whenever point gets within this many lines
29000 of the top or bottom of the window. */);
29001 scroll_margin = 0;
29002
29003 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
29004 doc: /* Pixels per inch value for non-window system displays.
29005 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
29006 Vdisplay_pixels_per_inch = make_float (72.0);
29007
29008 #ifdef GLYPH_DEBUG
29009 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
29010 #endif
29011
29012 DEFVAR_LISP ("truncate-partial-width-windows",
29013 Vtruncate_partial_width_windows,
29014 doc: /* Non-nil means truncate lines in windows narrower than the frame.
29015 For an integer value, truncate lines in each window narrower than the
29016 full frame width, provided the window width is less than that integer;
29017 otherwise, respect the value of `truncate-lines'.
29018
29019 For any other non-nil value, truncate lines in all windows that do
29020 not span the full frame width.
29021
29022 A value of nil means to respect the value of `truncate-lines'.
29023
29024 If `word-wrap' is enabled, you might want to reduce this. */);
29025 Vtruncate_partial_width_windows = make_number (50);
29026
29027 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
29028 doc: /* Maximum buffer size for which line number should be displayed.
29029 If the buffer is bigger than this, the line number does not appear
29030 in the mode line. A value of nil means no limit. */);
29031 Vline_number_display_limit = Qnil;
29032
29033 DEFVAR_INT ("line-number-display-limit-width",
29034 line_number_display_limit_width,
29035 doc: /* Maximum line width (in characters) for line number display.
29036 If the average length of the lines near point is bigger than this, then the
29037 line number may be omitted from the mode line. */);
29038 line_number_display_limit_width = 200;
29039
29040 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
29041 doc: /* Non-nil means highlight region even in nonselected windows. */);
29042 highlight_nonselected_windows = 0;
29043
29044 DEFVAR_BOOL ("multiple-frames", multiple_frames,
29045 doc: /* Non-nil if more than one frame is visible on this display.
29046 Minibuffer-only frames don't count, but iconified frames do.
29047 This variable is not guaranteed to be accurate except while processing
29048 `frame-title-format' and `icon-title-format'. */);
29049
29050 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
29051 doc: /* Template for displaying the title bar of visible frames.
29052 \(Assuming the window manager supports this feature.)
29053
29054 This variable has the same structure as `mode-line-format', except that
29055 the %c and %l constructs are ignored. It is used only on frames for
29056 which no explicit name has been set \(see `modify-frame-parameters'). */);
29057
29058 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
29059 doc: /* Template for displaying the title bar of an iconified frame.
29060 \(Assuming the window manager supports this feature.)
29061 This variable has the same structure as `mode-line-format' (which see),
29062 and is used only on frames for which no explicit name has been set
29063 \(see `modify-frame-parameters'). */);
29064 Vicon_title_format
29065 = Vframe_title_format
29066 = listn (CONSTYPE_PURE, 3,
29067 intern_c_string ("multiple-frames"),
29068 build_pure_c_string ("%b"),
29069 listn (CONSTYPE_PURE, 4,
29070 empty_unibyte_string,
29071 intern_c_string ("invocation-name"),
29072 build_pure_c_string ("@"),
29073 intern_c_string ("system-name")));
29074
29075 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
29076 doc: /* Maximum number of lines to keep in the message log buffer.
29077 If nil, disable message logging. If t, log messages but don't truncate
29078 the buffer when it becomes large. */);
29079 Vmessage_log_max = make_number (1000);
29080
29081 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
29082 doc: /* Functions called before redisplay, if window sizes have changed.
29083 The value should be a list of functions that take one argument.
29084 Just before redisplay, for each frame, if any of its windows have changed
29085 size since the last redisplay, or have been split or deleted,
29086 all the functions in the list are called, with the frame as argument. */);
29087 Vwindow_size_change_functions = Qnil;
29088
29089 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
29090 doc: /* List of functions to call before redisplaying a window with scrolling.
29091 Each function is called with two arguments, the window and its new
29092 display-start position. Note that these functions are also called by
29093 `set-window-buffer'. Also note that the value of `window-end' is not
29094 valid when these functions are called.
29095
29096 Warning: Do not use this feature to alter the way the window
29097 is scrolled. It is not designed for that, and such use probably won't
29098 work. */);
29099 Vwindow_scroll_functions = Qnil;
29100
29101 DEFVAR_LISP ("window-text-change-functions",
29102 Vwindow_text_change_functions,
29103 doc: /* Functions to call in redisplay when text in the window might change. */);
29104 Vwindow_text_change_functions = Qnil;
29105
29106 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
29107 doc: /* Functions called when redisplay of a window reaches the end trigger.
29108 Each function is called with two arguments, the window and the end trigger value.
29109 See `set-window-redisplay-end-trigger'. */);
29110 Vredisplay_end_trigger_functions = Qnil;
29111
29112 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
29113 doc: /* Non-nil means autoselect window with mouse pointer.
29114 If nil, do not autoselect windows.
29115 A positive number means delay autoselection by that many seconds: a
29116 window is autoselected only after the mouse has remained in that
29117 window for the duration of the delay.
29118 A negative number has a similar effect, but causes windows to be
29119 autoselected only after the mouse has stopped moving. \(Because of
29120 the way Emacs compares mouse events, you will occasionally wait twice
29121 that time before the window gets selected.\)
29122 Any other value means to autoselect window instantaneously when the
29123 mouse pointer enters it.
29124
29125 Autoselection selects the minibuffer only if it is active, and never
29126 unselects the minibuffer if it is active.
29127
29128 When customizing this variable make sure that the actual value of
29129 `focus-follows-mouse' matches the behavior of your window manager. */);
29130 Vmouse_autoselect_window = Qnil;
29131
29132 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29133 doc: /* Non-nil means automatically resize tool-bars.
29134 This dynamically changes the tool-bar's height to the minimum height
29135 that is needed to make all tool-bar items visible.
29136 If value is `grow-only', the tool-bar's height is only increased
29137 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29138 Vauto_resize_tool_bars = Qt;
29139
29140 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29141 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29142 auto_raise_tool_bar_buttons_p = 1;
29143
29144 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29145 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29146 make_cursor_line_fully_visible_p = 1;
29147
29148 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29149 doc: /* Border below tool-bar in pixels.
29150 If an integer, use it as the height of the border.
29151 If it is one of `internal-border-width' or `border-width', use the
29152 value of the corresponding frame parameter.
29153 Otherwise, no border is added below the tool-bar. */);
29154 Vtool_bar_border = Qinternal_border_width;
29155
29156 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29157 doc: /* Margin around tool-bar buttons in pixels.
29158 If an integer, use that for both horizontal and vertical margins.
29159 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29160 HORZ specifying the horizontal margin, and VERT specifying the
29161 vertical margin. */);
29162 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29163
29164 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29165 doc: /* Relief thickness of tool-bar buttons. */);
29166 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29167
29168 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29169 doc: /* Tool bar style to use.
29170 It can be one of
29171 image - show images only
29172 text - show text only
29173 both - show both, text below image
29174 both-horiz - show text to the right of the image
29175 text-image-horiz - show text to the left of the image
29176 any other - use system default or image if no system default.
29177
29178 This variable only affects the GTK+ toolkit version of Emacs. */);
29179 Vtool_bar_style = Qnil;
29180
29181 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29182 doc: /* Maximum number of characters a label can have to be shown.
29183 The tool bar style must also show labels for this to have any effect, see
29184 `tool-bar-style'. */);
29185 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29186
29187 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29188 doc: /* List of functions to call to fontify regions of text.
29189 Each function is called with one argument POS. Functions must
29190 fontify a region starting at POS in the current buffer, and give
29191 fontified regions the property `fontified'. */);
29192 Vfontification_functions = Qnil;
29193 Fmake_variable_buffer_local (Qfontification_functions);
29194
29195 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29196 unibyte_display_via_language_environment,
29197 doc: /* Non-nil means display unibyte text according to language environment.
29198 Specifically, this means that raw bytes in the range 160-255 decimal
29199 are displayed by converting them to the equivalent multibyte characters
29200 according to the current language environment. As a result, they are
29201 displayed according to the current fontset.
29202
29203 Note that this variable affects only how these bytes are displayed,
29204 but does not change the fact they are interpreted as raw bytes. */);
29205 unibyte_display_via_language_environment = 0;
29206
29207 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29208 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29209 If a float, it specifies a fraction of the mini-window frame's height.
29210 If an integer, it specifies a number of lines. */);
29211 Vmax_mini_window_height = make_float (0.25);
29212
29213 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29214 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29215 A value of nil means don't automatically resize mini-windows.
29216 A value of t means resize them to fit the text displayed in them.
29217 A value of `grow-only', the default, means let mini-windows grow only;
29218 they return to their normal size when the minibuffer is closed, or the
29219 echo area becomes empty. */);
29220 Vresize_mini_windows = Qgrow_only;
29221
29222 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29223 doc: /* Alist specifying how to blink the cursor off.
29224 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29225 `cursor-type' frame-parameter or variable equals ON-STATE,
29226 comparing using `equal', Emacs uses OFF-STATE to specify
29227 how to blink it off. ON-STATE and OFF-STATE are values for
29228 the `cursor-type' frame parameter.
29229
29230 If a frame's ON-STATE has no entry in this list,
29231 the frame's other specifications determine how to blink the cursor off. */);
29232 Vblink_cursor_alist = Qnil;
29233
29234 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29235 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29236 If non-nil, windows are automatically scrolled horizontally to make
29237 point visible. */);
29238 automatic_hscrolling_p = 1;
29239 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29240
29241 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29242 doc: /* How many columns away from the window edge point is allowed to get
29243 before automatic hscrolling will horizontally scroll the window. */);
29244 hscroll_margin = 5;
29245
29246 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29247 doc: /* How many columns to scroll the window when point gets too close to the edge.
29248 When point is less than `hscroll-margin' columns from the window
29249 edge, automatic hscrolling will scroll the window by the amount of columns
29250 determined by this variable. If its value is a positive integer, scroll that
29251 many columns. If it's a positive floating-point number, it specifies the
29252 fraction of the window's width to scroll. If it's nil or zero, point will be
29253 centered horizontally after the scroll. Any other value, including negative
29254 numbers, are treated as if the value were zero.
29255
29256 Automatic hscrolling always moves point outside the scroll margin, so if
29257 point was more than scroll step columns inside the margin, the window will
29258 scroll more than the value given by the scroll step.
29259
29260 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29261 and `scroll-right' overrides this variable's effect. */);
29262 Vhscroll_step = make_number (0);
29263
29264 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29265 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29266 Bind this around calls to `message' to let it take effect. */);
29267 message_truncate_lines = 0;
29268
29269 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29270 doc: /* Normal hook run to update the menu bar definitions.
29271 Redisplay runs this hook before it redisplays the menu bar.
29272 This is used to update submenus such as Buffers,
29273 whose contents depend on various data. */);
29274 Vmenu_bar_update_hook = Qnil;
29275
29276 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29277 doc: /* Frame for which we are updating a menu.
29278 The enable predicate for a menu binding should check this variable. */);
29279 Vmenu_updating_frame = Qnil;
29280
29281 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29282 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29283 inhibit_menubar_update = 0;
29284
29285 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29286 doc: /* Prefix prepended to all continuation lines at display time.
29287 The value may be a string, an image, or a stretch-glyph; it is
29288 interpreted in the same way as the value of a `display' text property.
29289
29290 This variable is overridden by any `wrap-prefix' text or overlay
29291 property.
29292
29293 To add a prefix to non-continuation lines, use `line-prefix'. */);
29294 Vwrap_prefix = Qnil;
29295 DEFSYM (Qwrap_prefix, "wrap-prefix");
29296 Fmake_variable_buffer_local (Qwrap_prefix);
29297
29298 DEFVAR_LISP ("line-prefix", Vline_prefix,
29299 doc: /* Prefix prepended to all non-continuation lines at display time.
29300 The value may be a string, an image, or a stretch-glyph; it is
29301 interpreted in the same way as the value of a `display' text property.
29302
29303 This variable is overridden by any `line-prefix' text or overlay
29304 property.
29305
29306 To add a prefix to continuation lines, use `wrap-prefix'. */);
29307 Vline_prefix = Qnil;
29308 DEFSYM (Qline_prefix, "line-prefix");
29309 Fmake_variable_buffer_local (Qline_prefix);
29310
29311 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29312 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29313 inhibit_eval_during_redisplay = 0;
29314
29315 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29316 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29317 inhibit_free_realized_faces = 0;
29318
29319 #ifdef GLYPH_DEBUG
29320 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29321 doc: /* Inhibit try_window_id display optimization. */);
29322 inhibit_try_window_id = 0;
29323
29324 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29325 doc: /* Inhibit try_window_reusing display optimization. */);
29326 inhibit_try_window_reusing = 0;
29327
29328 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29329 doc: /* Inhibit try_cursor_movement display optimization. */);
29330 inhibit_try_cursor_movement = 0;
29331 #endif /* GLYPH_DEBUG */
29332
29333 DEFVAR_INT ("overline-margin", overline_margin,
29334 doc: /* Space between overline and text, in pixels.
29335 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29336 margin to the character height. */);
29337 overline_margin = 2;
29338
29339 DEFVAR_INT ("underline-minimum-offset",
29340 underline_minimum_offset,
29341 doc: /* Minimum distance between baseline and underline.
29342 This can improve legibility of underlined text at small font sizes,
29343 particularly when using variable `x-use-underline-position-properties'
29344 with fonts that specify an UNDERLINE_POSITION relatively close to the
29345 baseline. The default value is 1. */);
29346 underline_minimum_offset = 1;
29347
29348 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29349 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29350 This feature only works when on a window system that can change
29351 cursor shapes. */);
29352 display_hourglass_p = 1;
29353
29354 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29355 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29356 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29357
29358 hourglass_atimer = NULL;
29359 hourglass_shown_p = 0;
29360
29361 DEFSYM (Qglyphless_char, "glyphless-char");
29362 DEFSYM (Qhex_code, "hex-code");
29363 DEFSYM (Qempty_box, "empty-box");
29364 DEFSYM (Qthin_space, "thin-space");
29365 DEFSYM (Qzero_width, "zero-width");
29366
29367 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29368 /* Intern this now in case it isn't already done.
29369 Setting this variable twice is harmless.
29370 But don't staticpro it here--that is done in alloc.c. */
29371 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
29372 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29373
29374 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29375 doc: /* Char-table defining glyphless characters.
29376 Each element, if non-nil, should be one of the following:
29377 an ASCII acronym string: display this string in a box
29378 `hex-code': display the hexadecimal code of a character in a box
29379 `empty-box': display as an empty box
29380 `thin-space': display as 1-pixel width space
29381 `zero-width': don't display
29382 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29383 display method for graphical terminals and text terminals respectively.
29384 GRAPHICAL and TEXT should each have one of the values listed above.
29385
29386 The char-table has one extra slot to control the display of a character for
29387 which no font is found. This slot only takes effect on graphical terminals.
29388 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29389 `thin-space'. The default is `empty-box'. */);
29390 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29391 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29392 Qempty_box);
29393
29394 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
29395 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
29396 Vdebug_on_message = Qnil;
29397 }
29398
29399
29400 /* Initialize this module when Emacs starts. */
29401
29402 void
29403 init_xdisp (void)
29404 {
29405 current_header_line_height = current_mode_line_height = -1;
29406
29407 CHARPOS (this_line_start_pos) = 0;
29408
29409 if (!noninteractive)
29410 {
29411 struct window *m = XWINDOW (minibuf_window);
29412 Lisp_Object frame = m->frame;
29413 struct frame *f = XFRAME (frame);
29414 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29415 struct window *r = XWINDOW (root);
29416 int i;
29417
29418 echo_area_window = minibuf_window;
29419
29420 wset_top_line (r, make_number (FRAME_TOP_MARGIN (f)));
29421 wset_total_lines
29422 (r, make_number (FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f)));
29423 wset_total_cols (r, make_number (FRAME_COLS (f)));
29424 wset_top_line (m, make_number (FRAME_LINES (f) - 1));
29425 wset_total_lines (m, make_number (1));
29426 wset_total_cols (m, make_number (FRAME_COLS (f)));
29427
29428 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29429 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29430 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29431
29432 /* The default ellipsis glyphs `...'. */
29433 for (i = 0; i < 3; ++i)
29434 default_invis_vector[i] = make_number ('.');
29435 }
29436
29437 {
29438 /* Allocate the buffer for frame titles.
29439 Also used for `format-mode-line'. */
29440 int size = 100;
29441 mode_line_noprop_buf = xmalloc (size);
29442 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29443 mode_line_noprop_ptr = mode_line_noprop_buf;
29444 mode_line_target = MODE_LINE_DISPLAY;
29445 }
29446
29447 help_echo_showing_p = 0;
29448 }
29449
29450 /* Platform-independent portion of hourglass implementation. */
29451
29452 /* Cancel a currently active hourglass timer, and start a new one. */
29453 void
29454 start_hourglass (void)
29455 {
29456 #if defined (HAVE_WINDOW_SYSTEM)
29457 EMACS_TIME delay;
29458
29459 cancel_hourglass ();
29460
29461 if (INTEGERP (Vhourglass_delay)
29462 && XINT (Vhourglass_delay) > 0)
29463 delay = make_emacs_time (min (XINT (Vhourglass_delay),
29464 TYPE_MAXIMUM (time_t)),
29465 0);
29466 else if (FLOATP (Vhourglass_delay)
29467 && XFLOAT_DATA (Vhourglass_delay) > 0)
29468 delay = EMACS_TIME_FROM_DOUBLE (XFLOAT_DATA (Vhourglass_delay));
29469 else
29470 delay = make_emacs_time (DEFAULT_HOURGLASS_DELAY, 0);
29471
29472 #ifdef HAVE_NTGUI
29473 {
29474 extern void w32_note_current_window (void);
29475 w32_note_current_window ();
29476 }
29477 #endif /* HAVE_NTGUI */
29478
29479 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29480 show_hourglass, NULL);
29481 #endif
29482 }
29483
29484
29485 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29486 shown. */
29487 void
29488 cancel_hourglass (void)
29489 {
29490 #if defined (HAVE_WINDOW_SYSTEM)
29491 if (hourglass_atimer)
29492 {
29493 cancel_atimer (hourglass_atimer);
29494 hourglass_atimer = NULL;
29495 }
29496
29497 if (hourglass_shown_p)
29498 hide_hourglass ();
29499 #endif
29500 }