Fix bug #15099 with 'box' face attribute in display tables.
[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 #ifndef FRAME_X_OUTPUT
317 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
318 #endif
319
320 #define INFINITY 10000000
321
322 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
323 Lisp_Object Qwindow_scroll_functions;
324 static Lisp_Object Qwindow_text_change_functions;
325 static Lisp_Object Qredisplay_end_trigger_functions;
326 Lisp_Object Qinhibit_point_motion_hooks;
327 static Lisp_Object QCeval, QCpropertize;
328 Lisp_Object QCfile, QCdata;
329 static Lisp_Object Qfontified;
330 static Lisp_Object Qgrow_only;
331 static Lisp_Object Qinhibit_eval_during_redisplay;
332 static Lisp_Object Qbuffer_position, Qposition, Qobject;
333 static Lisp_Object Qright_to_left, Qleft_to_right;
334
335 /* Cursor shapes. */
336 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
337
338 /* Pointer shapes. */
339 static Lisp_Object Qarrow, Qhand;
340 Lisp_Object Qtext;
341
342 /* Holds the list (error). */
343 static Lisp_Object list_of_error;
344
345 static Lisp_Object Qfontification_functions;
346
347 static Lisp_Object Qwrap_prefix;
348 static Lisp_Object Qline_prefix;
349 static Lisp_Object Qredisplay_internal;
350
351 /* Non-nil means don't actually do any redisplay. */
352
353 Lisp_Object Qinhibit_redisplay;
354
355 /* Names of text properties relevant for redisplay. */
356
357 Lisp_Object Qdisplay;
358
359 Lisp_Object Qspace, QCalign_to;
360 static Lisp_Object QCrelative_width, QCrelative_height;
361 Lisp_Object Qleft_margin, Qright_margin;
362 static Lisp_Object Qspace_width, Qraise;
363 static Lisp_Object Qslice;
364 Lisp_Object Qcenter;
365 static Lisp_Object Qmargin, Qpointer;
366 static Lisp_Object Qline_height;
367
368 #ifdef HAVE_WINDOW_SYSTEM
369
370 /* Test if overflow newline into fringe. Called with iterator IT
371 at or past right window margin, and with IT->current_x set. */
372
373 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
374 (!NILP (Voverflow_newline_into_fringe) \
375 && FRAME_WINDOW_P ((IT)->f) \
376 && ((IT)->bidi_it.paragraph_dir == R2L \
377 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
378 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
379 && (IT)->current_x == (IT)->last_visible_x)
380
381 #else /* !HAVE_WINDOW_SYSTEM */
382 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
383 #endif /* HAVE_WINDOW_SYSTEM */
384
385 /* Test if the display element loaded in IT, or the underlying buffer
386 or string character, is a space or a TAB character. This is used
387 to determine where word wrapping can occur. */
388
389 #define IT_DISPLAYING_WHITESPACE(it) \
390 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
391 || ((STRINGP (it->string) \
392 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
393 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
394 || (it->s \
395 && (it->s[IT_BYTEPOS (*it)] == ' ' \
396 || it->s[IT_BYTEPOS (*it)] == '\t')) \
397 || (IT_BYTEPOS (*it) < ZV_BYTE \
398 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
399 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
400
401 /* Name of the face used to highlight trailing whitespace. */
402
403 static Lisp_Object Qtrailing_whitespace;
404
405 /* Name and number of the face used to highlight escape glyphs. */
406
407 static Lisp_Object Qescape_glyph;
408
409 /* Name and number of the face used to highlight non-breaking spaces. */
410
411 static Lisp_Object Qnobreak_space;
412
413 /* The symbol `image' which is the car of the lists used to represent
414 images in Lisp. Also a tool bar style. */
415
416 Lisp_Object Qimage;
417
418 /* The image map types. */
419 Lisp_Object QCmap;
420 static Lisp_Object QCpointer;
421 static Lisp_Object Qrect, Qcircle, Qpoly;
422
423 /* Tool bar styles */
424 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
425
426 /* Non-zero means print newline to stdout before next mini-buffer
427 message. */
428
429 int noninteractive_need_newline;
430
431 /* Non-zero means print newline to message log before next message. */
432
433 static int message_log_need_newline;
434
435 /* Three markers that message_dolog uses.
436 It could allocate them itself, but that causes trouble
437 in handling memory-full errors. */
438 static Lisp_Object message_dolog_marker1;
439 static Lisp_Object message_dolog_marker2;
440 static Lisp_Object message_dolog_marker3;
441 \f
442 /* The buffer position of the first character appearing entirely or
443 partially on the line of the selected window which contains the
444 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
445 redisplay optimization in redisplay_internal. */
446
447 static struct text_pos this_line_start_pos;
448
449 /* Number of characters past the end of the line above, including the
450 terminating newline. */
451
452 static struct text_pos this_line_end_pos;
453
454 /* The vertical positions and the height of this line. */
455
456 static int this_line_vpos;
457 static int this_line_y;
458 static int this_line_pixel_height;
459
460 /* X position at which this display line starts. Usually zero;
461 negative if first character is partially visible. */
462
463 static int this_line_start_x;
464
465 /* The smallest character position seen by move_it_* functions as they
466 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
467 hscrolled lines, see display_line. */
468
469 static struct text_pos this_line_min_pos;
470
471 /* Buffer that this_line_.* variables are referring to. */
472
473 static struct buffer *this_line_buffer;
474
475
476 /* Values of those variables at last redisplay are stored as
477 properties on `overlay-arrow-position' symbol. However, if
478 Voverlay_arrow_position is a marker, last-arrow-position is its
479 numerical position. */
480
481 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
482
483 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
484 properties on a symbol in overlay-arrow-variable-list. */
485
486 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
487
488 Lisp_Object Qmenu_bar_update_hook;
489
490 /* Nonzero if an overlay arrow has been displayed in this window. */
491
492 static int overlay_arrow_seen;
493
494 /* Vector containing glyphs for an ellipsis `...'. */
495
496 static Lisp_Object default_invis_vector[3];
497
498 /* This is the window where the echo area message was displayed. It
499 is always a mini-buffer window, but it may not be the same window
500 currently active as a mini-buffer. */
501
502 Lisp_Object echo_area_window;
503
504 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
505 pushes the current message and the value of
506 message_enable_multibyte on the stack, the function restore_message
507 pops the stack and displays MESSAGE again. */
508
509 static Lisp_Object Vmessage_stack;
510
511 /* Nonzero means multibyte characters were enabled when the echo area
512 message was specified. */
513
514 static int message_enable_multibyte;
515
516 /* Nonzero if we should redraw the mode lines on the next redisplay. */
517
518 int update_mode_lines;
519
520 /* Nonzero if window sizes or contents have changed since last
521 redisplay that finished. */
522
523 int windows_or_buffers_changed;
524
525 /* Nonzero means a frame's cursor type has been changed. */
526
527 static int cursor_type_changed;
528
529 /* Nonzero after display_mode_line if %l was used and it displayed a
530 line number. */
531
532 static int line_number_displayed;
533
534 /* The name of the *Messages* buffer, a string. */
535
536 static Lisp_Object Vmessages_buffer_name;
537
538 /* Current, index 0, and last displayed echo area message. Either
539 buffers from echo_buffers, or nil to indicate no message. */
540
541 Lisp_Object echo_area_buffer[2];
542
543 /* The buffers referenced from echo_area_buffer. */
544
545 static Lisp_Object echo_buffer[2];
546
547 /* A vector saved used in with_area_buffer to reduce consing. */
548
549 static Lisp_Object Vwith_echo_area_save_vector;
550
551 /* Non-zero means display_echo_area should display the last echo area
552 message again. Set by redisplay_preserve_echo_area. */
553
554 static int display_last_displayed_message_p;
555
556 /* Nonzero if echo area is being used by print; zero if being used by
557 message. */
558
559 static int message_buf_print;
560
561 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
562
563 static Lisp_Object Qinhibit_menubar_update;
564 static Lisp_Object Qmessage_truncate_lines;
565
566 /* Set to 1 in clear_message to make redisplay_internal aware
567 of an emptied echo area. */
568
569 static int message_cleared_p;
570
571 /* A scratch glyph row with contents used for generating truncation
572 glyphs. Also used in direct_output_for_insert. */
573
574 #define MAX_SCRATCH_GLYPHS 100
575 static struct glyph_row scratch_glyph_row;
576 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
577
578 /* Ascent and height of the last line processed by move_it_to. */
579
580 static int last_height;
581
582 /* Non-zero if there's a help-echo in the echo area. */
583
584 int help_echo_showing_p;
585
586 /* If >= 0, computed, exact values of mode-line and header-line height
587 to use in the macros CURRENT_MODE_LINE_HEIGHT and
588 CURRENT_HEADER_LINE_HEIGHT. */
589
590 int current_mode_line_height, current_header_line_height;
591
592 /* The maximum distance to look ahead for text properties. Values
593 that are too small let us call compute_char_face and similar
594 functions too often which is expensive. Values that are too large
595 let us call compute_char_face and alike too often because we
596 might not be interested in text properties that far away. */
597
598 #define TEXT_PROP_DISTANCE_LIMIT 100
599
600 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
601 iterator state and later restore it. This is needed because the
602 bidi iterator on bidi.c keeps a stacked cache of its states, which
603 is really a singleton. When we use scratch iterator objects to
604 move around the buffer, we can cause the bidi cache to be pushed or
605 popped, and therefore we need to restore the cache state when we
606 return to the original iterator. */
607 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
608 do { \
609 if (CACHE) \
610 bidi_unshelve_cache (CACHE, 1); \
611 ITCOPY = ITORIG; \
612 CACHE = bidi_shelve_cache (); \
613 } while (0)
614
615 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
616 do { \
617 if (pITORIG != pITCOPY) \
618 *(pITORIG) = *(pITCOPY); \
619 bidi_unshelve_cache (CACHE, 0); \
620 CACHE = NULL; \
621 } while (0)
622
623 #ifdef GLYPH_DEBUG
624
625 /* Non-zero means print traces of redisplay if compiled with
626 GLYPH_DEBUG defined. */
627
628 int trace_redisplay_p;
629
630 #endif /* GLYPH_DEBUG */
631
632 #ifdef DEBUG_TRACE_MOVE
633 /* Non-zero means trace with TRACE_MOVE to stderr. */
634 int trace_move;
635
636 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
637 #else
638 #define TRACE_MOVE(x) (void) 0
639 #endif
640
641 static Lisp_Object Qauto_hscroll_mode;
642
643 /* Buffer being redisplayed -- for redisplay_window_error. */
644
645 static struct buffer *displayed_buffer;
646
647 /* Value returned from text property handlers (see below). */
648
649 enum prop_handled
650 {
651 HANDLED_NORMALLY,
652 HANDLED_RECOMPUTE_PROPS,
653 HANDLED_OVERLAY_STRING_CONSUMED,
654 HANDLED_RETURN
655 };
656
657 /* A description of text properties that redisplay is interested
658 in. */
659
660 struct props
661 {
662 /* The name of the property. */
663 Lisp_Object *name;
664
665 /* A unique index for the property. */
666 enum prop_idx idx;
667
668 /* A handler function called to set up iterator IT from the property
669 at IT's current position. Value is used to steer handle_stop. */
670 enum prop_handled (*handler) (struct it *it);
671 };
672
673 static enum prop_handled handle_face_prop (struct it *);
674 static enum prop_handled handle_invisible_prop (struct it *);
675 static enum prop_handled handle_display_prop (struct it *);
676 static enum prop_handled handle_composition_prop (struct it *);
677 static enum prop_handled handle_overlay_change (struct it *);
678 static enum prop_handled handle_fontified_prop (struct it *);
679
680 /* Properties handled by iterators. */
681
682 static struct props it_props[] =
683 {
684 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
685 /* Handle `face' before `display' because some sub-properties of
686 `display' need to know the face. */
687 {&Qface, FACE_PROP_IDX, handle_face_prop},
688 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
689 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
690 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
691 {NULL, 0, NULL}
692 };
693
694 /* Value is the position described by X. If X is a marker, value is
695 the marker_position of X. Otherwise, value is X. */
696
697 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
698
699 /* Enumeration returned by some move_it_.* functions internally. */
700
701 enum move_it_result
702 {
703 /* Not used. Undefined value. */
704 MOVE_UNDEFINED,
705
706 /* Move ended at the requested buffer position or ZV. */
707 MOVE_POS_MATCH_OR_ZV,
708
709 /* Move ended at the requested X pixel position. */
710 MOVE_X_REACHED,
711
712 /* Move within a line ended at the end of a line that must be
713 continued. */
714 MOVE_LINE_CONTINUED,
715
716 /* Move within a line ended at the end of a line that would
717 be displayed truncated. */
718 MOVE_LINE_TRUNCATED,
719
720 /* Move within a line ended at a line end. */
721 MOVE_NEWLINE_OR_CR
722 };
723
724 /* This counter is used to clear the face cache every once in a while
725 in redisplay_internal. It is incremented for each redisplay.
726 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
727 cleared. */
728
729 #define CLEAR_FACE_CACHE_COUNT 500
730 static int clear_face_cache_count;
731
732 /* Similarly for the image cache. */
733
734 #ifdef HAVE_WINDOW_SYSTEM
735 #define CLEAR_IMAGE_CACHE_COUNT 101
736 static int clear_image_cache_count;
737
738 /* Null glyph slice */
739 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
740 #endif
741
742 /* True while redisplay_internal is in progress. */
743
744 bool redisplaying_p;
745
746 static Lisp_Object Qinhibit_free_realized_faces;
747 static Lisp_Object Qmode_line_default_help_echo;
748
749 /* If a string, XTread_socket generates an event to display that string.
750 (The display is done in read_char.) */
751
752 Lisp_Object help_echo_string;
753 Lisp_Object help_echo_window;
754 Lisp_Object help_echo_object;
755 ptrdiff_t help_echo_pos;
756
757 /* Temporary variable for XTread_socket. */
758
759 Lisp_Object previous_help_echo_string;
760
761 /* Platform-independent portion of hourglass implementation. */
762
763 /* Non-zero means an hourglass cursor is currently shown. */
764 int hourglass_shown_p;
765
766 /* If non-null, an asynchronous timer that, when it expires, displays
767 an hourglass cursor on all frames. */
768 struct atimer *hourglass_atimer;
769
770 /* Name of the face used to display glyphless characters. */
771 Lisp_Object Qglyphless_char;
772
773 /* Symbol for the purpose of Vglyphless_char_display. */
774 static Lisp_Object Qglyphless_char_display;
775
776 /* Method symbols for Vglyphless_char_display. */
777 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
778
779 /* Default pixel width of `thin-space' display method. */
780 #define THIN_SPACE_WIDTH 1
781
782 /* Default number of seconds to wait before displaying an hourglass
783 cursor. */
784 #define DEFAULT_HOURGLASS_DELAY 1
785
786 \f
787 /* Function prototypes. */
788
789 static void setup_for_ellipsis (struct it *, int);
790 static void set_iterator_to_next (struct it *, int);
791 static void mark_window_display_accurate_1 (struct window *, int);
792 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
793 static int display_prop_string_p (Lisp_Object, Lisp_Object);
794 static int row_for_charpos_p (struct glyph_row *, ptrdiff_t);
795 static int cursor_row_p (struct glyph_row *);
796 static int redisplay_mode_lines (Lisp_Object, int);
797 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
798
799 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
800
801 static void handle_line_prefix (struct it *);
802
803 static void pint2str (char *, int, ptrdiff_t);
804 static void pint2hrstr (char *, int, ptrdiff_t);
805 static struct text_pos run_window_scroll_functions (Lisp_Object,
806 struct text_pos);
807 static int text_outside_line_unchanged_p (struct window *,
808 ptrdiff_t, ptrdiff_t);
809 static void store_mode_line_noprop_char (char);
810 static int store_mode_line_noprop (const char *, int, int);
811 static void handle_stop (struct it *);
812 static void handle_stop_backwards (struct it *, ptrdiff_t);
813 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
814 static void ensure_echo_area_buffers (void);
815 static void unwind_with_echo_area_buffer (Lisp_Object);
816 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
817 static int with_echo_area_buffer (struct window *, int,
818 int (*) (ptrdiff_t, Lisp_Object),
819 ptrdiff_t, Lisp_Object);
820 static void clear_garbaged_frames (void);
821 static int current_message_1 (ptrdiff_t, Lisp_Object);
822 static int truncate_message_1 (ptrdiff_t, Lisp_Object);
823 static void set_message (Lisp_Object);
824 static int set_message_1 (ptrdiff_t, Lisp_Object);
825 static int display_echo_area (struct window *);
826 static int display_echo_area_1 (ptrdiff_t, Lisp_Object);
827 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object);
828 static void unwind_redisplay (void);
829 static int string_char_and_length (const unsigned char *, int *);
830 static struct text_pos display_prop_end (struct it *, Lisp_Object,
831 struct text_pos);
832 static int compute_window_start_on_continuation_line (struct window *);
833 static void insert_left_trunc_glyphs (struct it *);
834 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
835 Lisp_Object);
836 static void extend_face_to_end_of_line (struct it *);
837 static int append_space_for_newline (struct it *, int);
838 static int cursor_row_fully_visible_p (struct window *, int, int);
839 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
840 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
841 static int trailing_whitespace_p (ptrdiff_t);
842 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
843 static void push_it (struct it *, struct text_pos *);
844 static void iterate_out_of_display_property (struct it *);
845 static void pop_it (struct it *);
846 static void sync_frame_with_window_matrix_rows (struct window *);
847 static void redisplay_internal (void);
848 static int echo_area_display (int);
849 static void redisplay_windows (Lisp_Object);
850 static void redisplay_window (Lisp_Object, int);
851 static Lisp_Object redisplay_window_error (Lisp_Object);
852 static Lisp_Object redisplay_window_0 (Lisp_Object);
853 static Lisp_Object redisplay_window_1 (Lisp_Object);
854 static int set_cursor_from_row (struct window *, struct glyph_row *,
855 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
856 int, int);
857 static int update_menu_bar (struct frame *, int, int);
858 static int try_window_reusing_current_matrix (struct window *);
859 static int try_window_id (struct window *);
860 static int display_line (struct it *);
861 static int display_mode_lines (struct window *);
862 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
863 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
864 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
865 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
866 static void display_menu_bar (struct window *);
867 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
868 ptrdiff_t *);
869 static int display_string (const char *, Lisp_Object, Lisp_Object,
870 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
871 static void compute_line_metrics (struct it *);
872 static void run_redisplay_end_trigger_hook (struct it *);
873 static int get_overlay_strings (struct it *, ptrdiff_t);
874 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
875 static void next_overlay_string (struct it *);
876 static void reseat (struct it *, struct text_pos, int);
877 static void reseat_1 (struct it *, struct text_pos, int);
878 static void back_to_previous_visible_line_start (struct it *);
879 static void reseat_at_next_visible_line_start (struct it *, int);
880 static int next_element_from_ellipsis (struct it *);
881 static int next_element_from_display_vector (struct it *);
882 static int next_element_from_string (struct it *);
883 static int next_element_from_c_string (struct it *);
884 static int next_element_from_buffer (struct it *);
885 static int next_element_from_composition (struct it *);
886 static int next_element_from_image (struct it *);
887 static int next_element_from_stretch (struct it *);
888 static void load_overlay_strings (struct it *, ptrdiff_t);
889 static int init_from_display_pos (struct it *, struct window *,
890 struct display_pos *);
891 static void reseat_to_string (struct it *, const char *,
892 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
893 static int get_next_display_element (struct it *);
894 static enum move_it_result
895 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
896 enum move_operation_enum);
897 static void get_visually_first_element (struct it *);
898 static void init_to_row_start (struct it *, struct window *,
899 struct glyph_row *);
900 static int init_to_row_end (struct it *, struct window *,
901 struct glyph_row *);
902 static void back_to_previous_line_start (struct it *);
903 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
904 static struct text_pos string_pos_nchars_ahead (struct text_pos,
905 Lisp_Object, ptrdiff_t);
906 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
907 static struct text_pos c_string_pos (ptrdiff_t, const char *, bool);
908 static ptrdiff_t number_of_chars (const char *, bool);
909 static void compute_stop_pos (struct it *);
910 static void compute_string_pos (struct text_pos *, struct text_pos,
911 Lisp_Object);
912 static int face_before_or_after_it_pos (struct it *, int);
913 static ptrdiff_t next_overlay_change (ptrdiff_t);
914 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
915 Lisp_Object, struct text_pos *, ptrdiff_t, int);
916 static int handle_single_display_spec (struct it *, Lisp_Object,
917 Lisp_Object, Lisp_Object,
918 struct text_pos *, ptrdiff_t, int, int);
919 static int underlying_face_id (struct it *);
920 static int in_ellipses_for_invisible_text_p (struct display_pos *,
921 struct window *);
922
923 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
924 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
925
926 #ifdef HAVE_WINDOW_SYSTEM
927
928 static void x_consider_frame_title (Lisp_Object);
929 static int tool_bar_lines_needed (struct frame *, int *);
930 static void update_tool_bar (struct frame *, int);
931 static void build_desired_tool_bar_string (struct frame *f);
932 static int redisplay_tool_bar (struct frame *);
933 static void display_tool_bar_line (struct it *, int);
934 static void notice_overwritten_cursor (struct window *,
935 enum glyph_row_area,
936 int, int, int, int);
937 static void append_stretch_glyph (struct it *, Lisp_Object,
938 int, int, int);
939
940
941 #endif /* HAVE_WINDOW_SYSTEM */
942
943 static void produce_special_glyphs (struct it *, enum display_element_type);
944 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
945 static int coords_in_mouse_face_p (struct window *, int, int);
946
947
948 \f
949 /***********************************************************************
950 Window display dimensions
951 ***********************************************************************/
952
953 /* Return the bottom boundary y-position for text lines in window W.
954 This is the first y position at which a line cannot start.
955 It is relative to the top of the window.
956
957 This is the height of W minus the height of a mode line, if any. */
958
959 int
960 window_text_bottom_y (struct window *w)
961 {
962 int height = WINDOW_TOTAL_HEIGHT (w);
963
964 if (WINDOW_WANTS_MODELINE_P (w))
965 height -= CURRENT_MODE_LINE_HEIGHT (w);
966 return height;
967 }
968
969 /* Return the pixel width of display area AREA of window W. AREA < 0
970 means return the total width of W, not including fringes to
971 the left and right of the window. */
972
973 int
974 window_box_width (struct window *w, int area)
975 {
976 int cols = w->total_cols;
977 int pixels = 0;
978
979 if (!w->pseudo_window_p)
980 {
981 cols -= WINDOW_SCROLL_BAR_COLS (w);
982
983 if (area == TEXT_AREA)
984 {
985 cols -= max (0, w->left_margin_cols);
986 cols -= max (0, w->right_margin_cols);
987 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
988 }
989 else if (area == LEFT_MARGIN_AREA)
990 {
991 cols = max (0, w->left_margin_cols);
992 pixels = 0;
993 }
994 else if (area == RIGHT_MARGIN_AREA)
995 {
996 cols = max (0, w->right_margin_cols);
997 pixels = 0;
998 }
999 }
1000
1001 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1002 }
1003
1004
1005 /* Return the pixel height of the display area of window W, not
1006 including mode lines of W, if any. */
1007
1008 int
1009 window_box_height (struct window *w)
1010 {
1011 struct frame *f = XFRAME (w->frame);
1012 int height = WINDOW_TOTAL_HEIGHT (w);
1013
1014 eassert (height >= 0);
1015
1016 /* Note: the code below that determines the mode-line/header-line
1017 height is essentially the same as that contained in the macro
1018 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1019 the appropriate glyph row has its `mode_line_p' flag set,
1020 and if it doesn't, uses estimate_mode_line_height instead. */
1021
1022 if (WINDOW_WANTS_MODELINE_P (w))
1023 {
1024 struct glyph_row *ml_row
1025 = (w->current_matrix && w->current_matrix->rows
1026 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1027 : 0);
1028 if (ml_row && ml_row->mode_line_p)
1029 height -= ml_row->height;
1030 else
1031 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1032 }
1033
1034 if (WINDOW_WANTS_HEADER_LINE_P (w))
1035 {
1036 struct glyph_row *hl_row
1037 = (w->current_matrix && w->current_matrix->rows
1038 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1039 : 0);
1040 if (hl_row && hl_row->mode_line_p)
1041 height -= hl_row->height;
1042 else
1043 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1044 }
1045
1046 /* With a very small font and a mode-line that's taller than
1047 default, we might end up with a negative height. */
1048 return max (0, height);
1049 }
1050
1051 /* Return the window-relative coordinate of the left edge of display
1052 area AREA of window W. AREA < 0 means return the left edge of the
1053 whole window, to the right of the left fringe of W. */
1054
1055 int
1056 window_box_left_offset (struct window *w, int area)
1057 {
1058 int x;
1059
1060 if (w->pseudo_window_p)
1061 return 0;
1062
1063 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1064
1065 if (area == TEXT_AREA)
1066 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1067 + window_box_width (w, LEFT_MARGIN_AREA));
1068 else if (area == RIGHT_MARGIN_AREA)
1069 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1070 + window_box_width (w, LEFT_MARGIN_AREA)
1071 + window_box_width (w, TEXT_AREA)
1072 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1073 ? 0
1074 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1075 else if (area == LEFT_MARGIN_AREA
1076 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1077 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1078
1079 return x;
1080 }
1081
1082
1083 /* Return the window-relative coordinate of the right edge of display
1084 area AREA of window W. AREA < 0 means return the right edge of the
1085 whole window, to the left of the right fringe of W. */
1086
1087 int
1088 window_box_right_offset (struct window *w, int area)
1089 {
1090 return window_box_left_offset (w, area) + window_box_width (w, area);
1091 }
1092
1093 /* Return the frame-relative coordinate of the left edge of display
1094 area AREA of window W. AREA < 0 means return the left edge of the
1095 whole window, to the right of the left fringe of W. */
1096
1097 int
1098 window_box_left (struct window *w, int area)
1099 {
1100 struct frame *f = XFRAME (w->frame);
1101 int x;
1102
1103 if (w->pseudo_window_p)
1104 return FRAME_INTERNAL_BORDER_WIDTH (f);
1105
1106 x = (WINDOW_LEFT_EDGE_X (w)
1107 + window_box_left_offset (w, area));
1108
1109 return x;
1110 }
1111
1112
1113 /* Return the frame-relative coordinate of the right edge of display
1114 area AREA of window W. AREA < 0 means return the right edge of the
1115 whole window, to the left of the right fringe of W. */
1116
1117 int
1118 window_box_right (struct window *w, int area)
1119 {
1120 return window_box_left (w, area) + window_box_width (w, area);
1121 }
1122
1123 /* Get the bounding box of the display area AREA of window W, without
1124 mode lines, in frame-relative coordinates. AREA < 0 means the
1125 whole window, not including the left and right fringes of
1126 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1127 coordinates of the upper-left corner of the box. Return in
1128 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1129
1130 void
1131 window_box (struct window *w, int area, int *box_x, int *box_y,
1132 int *box_width, int *box_height)
1133 {
1134 if (box_width)
1135 *box_width = window_box_width (w, area);
1136 if (box_height)
1137 *box_height = window_box_height (w);
1138 if (box_x)
1139 *box_x = window_box_left (w, area);
1140 if (box_y)
1141 {
1142 *box_y = WINDOW_TOP_EDGE_Y (w);
1143 if (WINDOW_WANTS_HEADER_LINE_P (w))
1144 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1145 }
1146 }
1147
1148
1149 /* Get the bounding box of the display area AREA of window W, without
1150 mode lines. AREA < 0 means the whole window, not including the
1151 left and right fringe of the window. Return in *TOP_LEFT_X
1152 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1153 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1154 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1155 box. */
1156
1157 static void
1158 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1159 int *bottom_right_x, int *bottom_right_y)
1160 {
1161 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1162 bottom_right_y);
1163 *bottom_right_x += *top_left_x;
1164 *bottom_right_y += *top_left_y;
1165 }
1166
1167
1168 \f
1169 /***********************************************************************
1170 Utilities
1171 ***********************************************************************/
1172
1173 /* Return the bottom y-position of the line the iterator IT is in.
1174 This can modify IT's settings. */
1175
1176 int
1177 line_bottom_y (struct it *it)
1178 {
1179 int line_height = it->max_ascent + it->max_descent;
1180 int line_top_y = it->current_y;
1181
1182 if (line_height == 0)
1183 {
1184 if (last_height)
1185 line_height = last_height;
1186 else if (IT_CHARPOS (*it) < ZV)
1187 {
1188 move_it_by_lines (it, 1);
1189 line_height = (it->max_ascent || it->max_descent
1190 ? it->max_ascent + it->max_descent
1191 : last_height);
1192 }
1193 else
1194 {
1195 struct glyph_row *row = it->glyph_row;
1196
1197 /* Use the default character height. */
1198 it->glyph_row = NULL;
1199 it->what = IT_CHARACTER;
1200 it->c = ' ';
1201 it->len = 1;
1202 PRODUCE_GLYPHS (it);
1203 line_height = it->ascent + it->descent;
1204 it->glyph_row = row;
1205 }
1206 }
1207
1208 return line_top_y + line_height;
1209 }
1210
1211 DEFUN ("line-pixel-height", Fline_pixel_height,
1212 Sline_pixel_height, 0, 0, 0,
1213 doc: /* Return height in pixels of text line in the selected window.
1214
1215 Value is the height in pixels of the line at point. */)
1216 (void)
1217 {
1218 struct it it;
1219 struct text_pos pt;
1220 struct window *w = XWINDOW (selected_window);
1221
1222 SET_TEXT_POS (pt, PT, PT_BYTE);
1223 start_display (&it, w, pt);
1224 it.vpos = it.current_y = 0;
1225 last_height = 0;
1226 return make_number (line_bottom_y (&it));
1227 }
1228
1229 /* Return the default pixel height of text lines in window W. The
1230 value is the canonical height of the W frame's default font, plus
1231 any extra space required by the line-spacing variable or frame
1232 parameter.
1233
1234 Implementation note: this ignores any line-spacing text properties
1235 put on the newline characters. This is because those properties
1236 only affect the _screen_ line ending in the newline (i.e., in a
1237 continued line, only the last screen line will be affected), which
1238 means only a small number of lines in a buffer can ever use this
1239 feature. Since this function is used to compute the default pixel
1240 equivalent of text lines in a window, we can safely ignore those
1241 few lines. For the same reasons, we ignore the line-height
1242 properties. */
1243 int
1244 default_line_pixel_height (struct window *w)
1245 {
1246 struct frame *f = WINDOW_XFRAME (w);
1247 int height = FRAME_LINE_HEIGHT (f);
1248
1249 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1250 {
1251 struct buffer *b = XBUFFER (w->contents);
1252 Lisp_Object val = BVAR (b, extra_line_spacing);
1253
1254 if (NILP (val))
1255 val = BVAR (&buffer_defaults, extra_line_spacing);
1256 if (!NILP (val))
1257 {
1258 if (RANGED_INTEGERP (0, val, INT_MAX))
1259 height += XFASTINT (val);
1260 else if (FLOATP (val))
1261 {
1262 int addon = XFLOAT_DATA (val) * height + 0.5;
1263
1264 if (addon >= 0)
1265 height += addon;
1266 }
1267 }
1268 else
1269 height += f->extra_line_spacing;
1270 }
1271
1272 return height;
1273 }
1274
1275 /* Subroutine of pos_visible_p below. Extracts a display string, if
1276 any, from the display spec given as its argument. */
1277 static Lisp_Object
1278 string_from_display_spec (Lisp_Object spec)
1279 {
1280 if (CONSP (spec))
1281 {
1282 while (CONSP (spec))
1283 {
1284 if (STRINGP (XCAR (spec)))
1285 return XCAR (spec);
1286 spec = XCDR (spec);
1287 }
1288 }
1289 else if (VECTORP (spec))
1290 {
1291 ptrdiff_t i;
1292
1293 for (i = 0; i < ASIZE (spec); i++)
1294 {
1295 if (STRINGP (AREF (spec, i)))
1296 return AREF (spec, i);
1297 }
1298 return Qnil;
1299 }
1300
1301 return spec;
1302 }
1303
1304
1305 /* Limit insanely large values of W->hscroll on frame F to the largest
1306 value that will still prevent first_visible_x and last_visible_x of
1307 'struct it' from overflowing an int. */
1308 static int
1309 window_hscroll_limited (struct window *w, struct frame *f)
1310 {
1311 ptrdiff_t window_hscroll = w->hscroll;
1312 int window_text_width = window_box_width (w, TEXT_AREA);
1313 int colwidth = FRAME_COLUMN_WIDTH (f);
1314
1315 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1316 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1317
1318 return window_hscroll;
1319 }
1320
1321 /* Return 1 if position CHARPOS is visible in window W.
1322 CHARPOS < 0 means return info about WINDOW_END position.
1323 If visible, set *X and *Y to pixel coordinates of top left corner.
1324 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1325 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1326
1327 int
1328 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1329 int *rtop, int *rbot, int *rowh, int *vpos)
1330 {
1331 struct it it;
1332 void *itdata = bidi_shelve_cache ();
1333 struct text_pos top;
1334 int visible_p = 0;
1335 struct buffer *old_buffer = NULL;
1336
1337 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1338 return visible_p;
1339
1340 if (XBUFFER (w->contents) != current_buffer)
1341 {
1342 old_buffer = current_buffer;
1343 set_buffer_internal_1 (XBUFFER (w->contents));
1344 }
1345
1346 SET_TEXT_POS_FROM_MARKER (top, w->start);
1347 /* Scrolling a minibuffer window via scroll bar when the echo area
1348 shows long text sometimes resets the minibuffer contents behind
1349 our backs. */
1350 if (CHARPOS (top) > ZV)
1351 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1352
1353 /* Compute exact mode line heights. */
1354 if (WINDOW_WANTS_MODELINE_P (w))
1355 current_mode_line_height
1356 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1357 BVAR (current_buffer, mode_line_format));
1358
1359 if (WINDOW_WANTS_HEADER_LINE_P (w))
1360 current_header_line_height
1361 = display_mode_line (w, HEADER_LINE_FACE_ID,
1362 BVAR (current_buffer, header_line_format));
1363
1364 start_display (&it, w, top);
1365 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1366 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1367
1368 if (charpos >= 0
1369 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1370 && IT_CHARPOS (it) >= charpos)
1371 /* When scanning backwards under bidi iteration, move_it_to
1372 stops at or _before_ CHARPOS, because it stops at or to
1373 the _right_ of the character at CHARPOS. */
1374 || (it.bidi_p && it.bidi_it.scan_dir == -1
1375 && IT_CHARPOS (it) <= charpos)))
1376 {
1377 /* We have reached CHARPOS, or passed it. How the call to
1378 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1379 or covered by a display property, move_it_to stops at the end
1380 of the invisible text, to the right of CHARPOS. (ii) If
1381 CHARPOS is in a display vector, move_it_to stops on its last
1382 glyph. */
1383 int top_x = it.current_x;
1384 int top_y = it.current_y;
1385 /* Calling line_bottom_y may change it.method, it.position, etc. */
1386 enum it_method it_method = it.method;
1387 int bottom_y = (last_height = 0, line_bottom_y (&it));
1388 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1389
1390 if (top_y < window_top_y)
1391 visible_p = bottom_y > window_top_y;
1392 else if (top_y < it.last_visible_y)
1393 visible_p = 1;
1394 if (bottom_y >= it.last_visible_y
1395 && it.bidi_p && it.bidi_it.scan_dir == -1
1396 && IT_CHARPOS (it) < charpos)
1397 {
1398 /* When the last line of the window is scanned backwards
1399 under bidi iteration, we could be duped into thinking
1400 that we have passed CHARPOS, when in fact move_it_to
1401 simply stopped short of CHARPOS because it reached
1402 last_visible_y. To see if that's what happened, we call
1403 move_it_to again with a slightly larger vertical limit,
1404 and see if it actually moved vertically; if it did, we
1405 didn't really reach CHARPOS, which is beyond window end. */
1406 struct it save_it = it;
1407 /* Why 10? because we don't know how many canonical lines
1408 will the height of the next line(s) be. So we guess. */
1409 int ten_more_lines = 10 * default_line_pixel_height (w);
1410
1411 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1412 MOVE_TO_POS | MOVE_TO_Y);
1413 if (it.current_y > top_y)
1414 visible_p = 0;
1415
1416 it = save_it;
1417 }
1418 if (visible_p)
1419 {
1420 if (it_method == GET_FROM_DISPLAY_VECTOR)
1421 {
1422 /* We stopped on the last glyph of a display vector.
1423 Try and recompute. Hack alert! */
1424 if (charpos < 2 || top.charpos >= charpos)
1425 top_x = it.glyph_row->x;
1426 else
1427 {
1428 struct it it2, it2_prev;
1429 /* The idea is to get to the previous buffer
1430 position, consume the character there, and use
1431 the pixel coordinates we get after that. But if
1432 the previous buffer position is also displayed
1433 from a display vector, we need to consume all of
1434 the glyphs from that display vector. */
1435 start_display (&it2, w, top);
1436 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1437 /* If we didn't get to CHARPOS - 1, there's some
1438 replacing display property at that position, and
1439 we stopped after it. That is exactly the place
1440 whose coordinates we want. */
1441 if (IT_CHARPOS (it2) != charpos - 1)
1442 it2_prev = it2;
1443 else
1444 {
1445 /* Iterate until we get out of the display
1446 vector that displays the character at
1447 CHARPOS - 1. */
1448 do {
1449 get_next_display_element (&it2);
1450 PRODUCE_GLYPHS (&it2);
1451 it2_prev = it2;
1452 set_iterator_to_next (&it2, 1);
1453 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1454 && IT_CHARPOS (it2) < charpos);
1455 }
1456 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1457 || it2_prev.current_x > it2_prev.last_visible_x)
1458 top_x = it.glyph_row->x;
1459 else
1460 {
1461 top_x = it2_prev.current_x;
1462 top_y = it2_prev.current_y;
1463 }
1464 }
1465 }
1466 else if (IT_CHARPOS (it) != charpos)
1467 {
1468 Lisp_Object cpos = make_number (charpos);
1469 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1470 Lisp_Object string = string_from_display_spec (spec);
1471 struct text_pos tpos;
1472 int replacing_spec_p;
1473 bool newline_in_string
1474 = (STRINGP (string)
1475 && memchr (SDATA (string), '\n', SBYTES (string)));
1476
1477 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1478 replacing_spec_p
1479 = (!NILP (spec)
1480 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1481 charpos, FRAME_WINDOW_P (it.f)));
1482 /* The tricky code below is needed because there's a
1483 discrepancy between move_it_to and how we set cursor
1484 when PT is at the beginning of a portion of text
1485 covered by a display property or an overlay with a
1486 display property, or the display line ends in a
1487 newline from a display string. move_it_to will stop
1488 _after_ such display strings, whereas
1489 set_cursor_from_row conspires with cursor_row_p to
1490 place the cursor on the first glyph produced from the
1491 display string. */
1492
1493 /* We have overshoot PT because it is covered by a
1494 display property that replaces the text it covers.
1495 If the string includes embedded newlines, we are also
1496 in the wrong display line. Backtrack to the correct
1497 line, where the display property begins. */
1498 if (replacing_spec_p)
1499 {
1500 Lisp_Object startpos, endpos;
1501 EMACS_INT start, end;
1502 struct it it3;
1503 int it3_moved;
1504
1505 /* Find the first and the last buffer positions
1506 covered by the display string. */
1507 endpos =
1508 Fnext_single_char_property_change (cpos, Qdisplay,
1509 Qnil, Qnil);
1510 startpos =
1511 Fprevious_single_char_property_change (endpos, Qdisplay,
1512 Qnil, Qnil);
1513 start = XFASTINT (startpos);
1514 end = XFASTINT (endpos);
1515 /* Move to the last buffer position before the
1516 display property. */
1517 start_display (&it3, w, top);
1518 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1519 /* Move forward one more line if the position before
1520 the display string is a newline or if it is the
1521 rightmost character on a line that is
1522 continued or word-wrapped. */
1523 if (it3.method == GET_FROM_BUFFER
1524 && (it3.c == '\n'
1525 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1526 move_it_by_lines (&it3, 1);
1527 else if (move_it_in_display_line_to (&it3, -1,
1528 it3.current_x
1529 + it3.pixel_width,
1530 MOVE_TO_X)
1531 == MOVE_LINE_CONTINUED)
1532 {
1533 move_it_by_lines (&it3, 1);
1534 /* When we are under word-wrap, the #$@%!
1535 move_it_by_lines moves 2 lines, so we need to
1536 fix that up. */
1537 if (it3.line_wrap == WORD_WRAP)
1538 move_it_by_lines (&it3, -1);
1539 }
1540
1541 /* Record the vertical coordinate of the display
1542 line where we wound up. */
1543 top_y = it3.current_y;
1544 if (it3.bidi_p)
1545 {
1546 /* When characters are reordered for display,
1547 the character displayed to the left of the
1548 display string could be _after_ the display
1549 property in the logical order. Use the
1550 smallest vertical position of these two. */
1551 start_display (&it3, w, top);
1552 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1553 if (it3.current_y < top_y)
1554 top_y = it3.current_y;
1555 }
1556 /* Move from the top of the window to the beginning
1557 of the display line where the display string
1558 begins. */
1559 start_display (&it3, w, top);
1560 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1561 /* If it3_moved stays zero after the 'while' loop
1562 below, that means we already were at a newline
1563 before the loop (e.g., the display string begins
1564 with a newline), so we don't need to (and cannot)
1565 inspect the glyphs of it3.glyph_row, because
1566 PRODUCE_GLYPHS will not produce anything for a
1567 newline, and thus it3.glyph_row stays at its
1568 stale content it got at top of the window. */
1569 it3_moved = 0;
1570 /* Finally, advance the iterator until we hit the
1571 first display element whose character position is
1572 CHARPOS, or until the first newline from the
1573 display string, which signals the end of the
1574 display line. */
1575 while (get_next_display_element (&it3))
1576 {
1577 PRODUCE_GLYPHS (&it3);
1578 if (IT_CHARPOS (it3) == charpos
1579 || ITERATOR_AT_END_OF_LINE_P (&it3))
1580 break;
1581 it3_moved = 1;
1582 set_iterator_to_next (&it3, 0);
1583 }
1584 top_x = it3.current_x - it3.pixel_width;
1585 /* Normally, we would exit the above loop because we
1586 found the display element whose character
1587 position is CHARPOS. For the contingency that we
1588 didn't, and stopped at the first newline from the
1589 display string, move back over the glyphs
1590 produced from the string, until we find the
1591 rightmost glyph not from the string. */
1592 if (it3_moved
1593 && newline_in_string
1594 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1595 {
1596 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1597 + it3.glyph_row->used[TEXT_AREA];
1598
1599 while (EQ ((g - 1)->object, string))
1600 {
1601 --g;
1602 top_x -= g->pixel_width;
1603 }
1604 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1605 + it3.glyph_row->used[TEXT_AREA]);
1606 }
1607 }
1608 }
1609
1610 *x = top_x;
1611 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1612 *rtop = max (0, window_top_y - top_y);
1613 *rbot = max (0, bottom_y - it.last_visible_y);
1614 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1615 - max (top_y, window_top_y)));
1616 *vpos = it.vpos;
1617 }
1618 }
1619 else
1620 {
1621 /* We were asked to provide info about WINDOW_END. */
1622 struct it it2;
1623 void *it2data = NULL;
1624
1625 SAVE_IT (it2, it, it2data);
1626 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1627 move_it_by_lines (&it, 1);
1628 if (charpos < IT_CHARPOS (it)
1629 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1630 {
1631 visible_p = 1;
1632 RESTORE_IT (&it2, &it2, it2data);
1633 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1634 *x = it2.current_x;
1635 *y = it2.current_y + it2.max_ascent - it2.ascent;
1636 *rtop = max (0, -it2.current_y);
1637 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1638 - it.last_visible_y));
1639 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1640 it.last_visible_y)
1641 - max (it2.current_y,
1642 WINDOW_HEADER_LINE_HEIGHT (w))));
1643 *vpos = it2.vpos;
1644 }
1645 else
1646 bidi_unshelve_cache (it2data, 1);
1647 }
1648 bidi_unshelve_cache (itdata, 0);
1649
1650 if (old_buffer)
1651 set_buffer_internal_1 (old_buffer);
1652
1653 current_header_line_height = current_mode_line_height = -1;
1654
1655 if (visible_p && w->hscroll > 0)
1656 *x -=
1657 window_hscroll_limited (w, WINDOW_XFRAME (w))
1658 * WINDOW_FRAME_COLUMN_WIDTH (w);
1659
1660 #if 0
1661 /* Debugging code. */
1662 if (visible_p)
1663 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1664 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1665 else
1666 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1667 #endif
1668
1669 return visible_p;
1670 }
1671
1672
1673 /* Return the next character from STR. Return in *LEN the length of
1674 the character. This is like STRING_CHAR_AND_LENGTH but never
1675 returns an invalid character. If we find one, we return a `?', but
1676 with the length of the invalid character. */
1677
1678 static int
1679 string_char_and_length (const unsigned char *str, int *len)
1680 {
1681 int c;
1682
1683 c = STRING_CHAR_AND_LENGTH (str, *len);
1684 if (!CHAR_VALID_P (c))
1685 /* We may not change the length here because other places in Emacs
1686 don't use this function, i.e. they silently accept invalid
1687 characters. */
1688 c = '?';
1689
1690 return c;
1691 }
1692
1693
1694
1695 /* Given a position POS containing a valid character and byte position
1696 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1697
1698 static struct text_pos
1699 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1700 {
1701 eassert (STRINGP (string) && nchars >= 0);
1702
1703 if (STRING_MULTIBYTE (string))
1704 {
1705 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1706 int len;
1707
1708 while (nchars--)
1709 {
1710 string_char_and_length (p, &len);
1711 p += len;
1712 CHARPOS (pos) += 1;
1713 BYTEPOS (pos) += len;
1714 }
1715 }
1716 else
1717 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1718
1719 return pos;
1720 }
1721
1722
1723 /* Value is the text position, i.e. character and byte position,
1724 for character position CHARPOS in STRING. */
1725
1726 static struct text_pos
1727 string_pos (ptrdiff_t charpos, Lisp_Object string)
1728 {
1729 struct text_pos pos;
1730 eassert (STRINGP (string));
1731 eassert (charpos >= 0);
1732 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1733 return pos;
1734 }
1735
1736
1737 /* Value is a text position, i.e. character and byte position, for
1738 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1739 means recognize multibyte characters. */
1740
1741 static struct text_pos
1742 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1743 {
1744 struct text_pos pos;
1745
1746 eassert (s != NULL);
1747 eassert (charpos >= 0);
1748
1749 if (multibyte_p)
1750 {
1751 int len;
1752
1753 SET_TEXT_POS (pos, 0, 0);
1754 while (charpos--)
1755 {
1756 string_char_and_length ((const unsigned char *) s, &len);
1757 s += len;
1758 CHARPOS (pos) += 1;
1759 BYTEPOS (pos) += len;
1760 }
1761 }
1762 else
1763 SET_TEXT_POS (pos, charpos, charpos);
1764
1765 return pos;
1766 }
1767
1768
1769 /* Value is the number of characters in C string S. MULTIBYTE_P
1770 non-zero means recognize multibyte characters. */
1771
1772 static ptrdiff_t
1773 number_of_chars (const char *s, bool multibyte_p)
1774 {
1775 ptrdiff_t nchars;
1776
1777 if (multibyte_p)
1778 {
1779 ptrdiff_t rest = strlen (s);
1780 int len;
1781 const unsigned char *p = (const unsigned char *) s;
1782
1783 for (nchars = 0; rest > 0; ++nchars)
1784 {
1785 string_char_and_length (p, &len);
1786 rest -= len, p += len;
1787 }
1788 }
1789 else
1790 nchars = strlen (s);
1791
1792 return nchars;
1793 }
1794
1795
1796 /* Compute byte position NEWPOS->bytepos corresponding to
1797 NEWPOS->charpos. POS is a known position in string STRING.
1798 NEWPOS->charpos must be >= POS.charpos. */
1799
1800 static void
1801 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1802 {
1803 eassert (STRINGP (string));
1804 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1805
1806 if (STRING_MULTIBYTE (string))
1807 *newpos = string_pos_nchars_ahead (pos, string,
1808 CHARPOS (*newpos) - CHARPOS (pos));
1809 else
1810 BYTEPOS (*newpos) = CHARPOS (*newpos);
1811 }
1812
1813 /* EXPORT:
1814 Return an estimation of the pixel height of mode or header lines on
1815 frame F. FACE_ID specifies what line's height to estimate. */
1816
1817 int
1818 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1819 {
1820 #ifdef HAVE_WINDOW_SYSTEM
1821 if (FRAME_WINDOW_P (f))
1822 {
1823 int height = FONT_HEIGHT (FRAME_FONT (f));
1824
1825 /* This function is called so early when Emacs starts that the face
1826 cache and mode line face are not yet initialized. */
1827 if (FRAME_FACE_CACHE (f))
1828 {
1829 struct face *face = FACE_FROM_ID (f, face_id);
1830 if (face)
1831 {
1832 if (face->font)
1833 height = FONT_HEIGHT (face->font);
1834 if (face->box_line_width > 0)
1835 height += 2 * face->box_line_width;
1836 }
1837 }
1838
1839 return height;
1840 }
1841 #endif
1842
1843 return 1;
1844 }
1845
1846 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1847 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1848 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1849 not force the value into range. */
1850
1851 void
1852 pixel_to_glyph_coords (struct frame *f, register int pix_x, register int pix_y,
1853 int *x, int *y, NativeRectangle *bounds, int noclip)
1854 {
1855
1856 #ifdef HAVE_WINDOW_SYSTEM
1857 if (FRAME_WINDOW_P (f))
1858 {
1859 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1860 even for negative values. */
1861 if (pix_x < 0)
1862 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1863 if (pix_y < 0)
1864 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1865
1866 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1867 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1868
1869 if (bounds)
1870 STORE_NATIVE_RECT (*bounds,
1871 FRAME_COL_TO_PIXEL_X (f, pix_x),
1872 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1873 FRAME_COLUMN_WIDTH (f) - 1,
1874 FRAME_LINE_HEIGHT (f) - 1);
1875
1876 if (!noclip)
1877 {
1878 if (pix_x < 0)
1879 pix_x = 0;
1880 else if (pix_x > FRAME_TOTAL_COLS (f))
1881 pix_x = FRAME_TOTAL_COLS (f);
1882
1883 if (pix_y < 0)
1884 pix_y = 0;
1885 else if (pix_y > FRAME_LINES (f))
1886 pix_y = FRAME_LINES (f);
1887 }
1888 }
1889 #endif
1890
1891 *x = pix_x;
1892 *y = pix_y;
1893 }
1894
1895
1896 /* Find the glyph under window-relative coordinates X/Y in window W.
1897 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1898 strings. Return in *HPOS and *VPOS the row and column number of
1899 the glyph found. Return in *AREA the glyph area containing X.
1900 Value is a pointer to the glyph found or null if X/Y is not on
1901 text, or we can't tell because W's current matrix is not up to
1902 date. */
1903
1904 static
1905 struct glyph *
1906 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1907 int *dx, int *dy, int *area)
1908 {
1909 struct glyph *glyph, *end;
1910 struct glyph_row *row = NULL;
1911 int x0, i;
1912
1913 /* Find row containing Y. Give up if some row is not enabled. */
1914 for (i = 0; i < w->current_matrix->nrows; ++i)
1915 {
1916 row = MATRIX_ROW (w->current_matrix, i);
1917 if (!row->enabled_p)
1918 return NULL;
1919 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1920 break;
1921 }
1922
1923 *vpos = i;
1924 *hpos = 0;
1925
1926 /* Give up if Y is not in the window. */
1927 if (i == w->current_matrix->nrows)
1928 return NULL;
1929
1930 /* Get the glyph area containing X. */
1931 if (w->pseudo_window_p)
1932 {
1933 *area = TEXT_AREA;
1934 x0 = 0;
1935 }
1936 else
1937 {
1938 if (x < window_box_left_offset (w, TEXT_AREA))
1939 {
1940 *area = LEFT_MARGIN_AREA;
1941 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1942 }
1943 else if (x < window_box_right_offset (w, TEXT_AREA))
1944 {
1945 *area = TEXT_AREA;
1946 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1947 }
1948 else
1949 {
1950 *area = RIGHT_MARGIN_AREA;
1951 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1952 }
1953 }
1954
1955 /* Find glyph containing X. */
1956 glyph = row->glyphs[*area];
1957 end = glyph + row->used[*area];
1958 x -= x0;
1959 while (glyph < end && x >= glyph->pixel_width)
1960 {
1961 x -= glyph->pixel_width;
1962 ++glyph;
1963 }
1964
1965 if (glyph == end)
1966 return NULL;
1967
1968 if (dx)
1969 {
1970 *dx = x;
1971 *dy = y - (row->y + row->ascent - glyph->ascent);
1972 }
1973
1974 *hpos = glyph - row->glyphs[*area];
1975 return glyph;
1976 }
1977
1978 /* Convert frame-relative x/y to coordinates relative to window W.
1979 Takes pseudo-windows into account. */
1980
1981 static void
1982 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1983 {
1984 if (w->pseudo_window_p)
1985 {
1986 /* A pseudo-window is always full-width, and starts at the
1987 left edge of the frame, plus a frame border. */
1988 struct frame *f = XFRAME (w->frame);
1989 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1990 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1991 }
1992 else
1993 {
1994 *x -= WINDOW_LEFT_EDGE_X (w);
1995 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1996 }
1997 }
1998
1999 #ifdef HAVE_WINDOW_SYSTEM
2000
2001 /* EXPORT:
2002 Return in RECTS[] at most N clipping rectangles for glyph string S.
2003 Return the number of stored rectangles. */
2004
2005 int
2006 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
2007 {
2008 XRectangle r;
2009
2010 if (n <= 0)
2011 return 0;
2012
2013 if (s->row->full_width_p)
2014 {
2015 /* Draw full-width. X coordinates are relative to S->w->left_col. */
2016 r.x = WINDOW_LEFT_EDGE_X (s->w);
2017 r.width = WINDOW_TOTAL_WIDTH (s->w);
2018
2019 /* Unless displaying a mode or menu bar line, which are always
2020 fully visible, clip to the visible part of the row. */
2021 if (s->w->pseudo_window_p)
2022 r.height = s->row->visible_height;
2023 else
2024 r.height = s->height;
2025 }
2026 else
2027 {
2028 /* This is a text line that may be partially visible. */
2029 r.x = window_box_left (s->w, s->area);
2030 r.width = window_box_width (s->w, s->area);
2031 r.height = s->row->visible_height;
2032 }
2033
2034 if (s->clip_head)
2035 if (r.x < s->clip_head->x)
2036 {
2037 if (r.width >= s->clip_head->x - r.x)
2038 r.width -= s->clip_head->x - r.x;
2039 else
2040 r.width = 0;
2041 r.x = s->clip_head->x;
2042 }
2043 if (s->clip_tail)
2044 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2045 {
2046 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2047 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2048 else
2049 r.width = 0;
2050 }
2051
2052 /* If S draws overlapping rows, it's sufficient to use the top and
2053 bottom of the window for clipping because this glyph string
2054 intentionally draws over other lines. */
2055 if (s->for_overlaps)
2056 {
2057 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2058 r.height = window_text_bottom_y (s->w) - r.y;
2059
2060 /* Alas, the above simple strategy does not work for the
2061 environments with anti-aliased text: if the same text is
2062 drawn onto the same place multiple times, it gets thicker.
2063 If the overlap we are processing is for the erased cursor, we
2064 take the intersection with the rectangle of the cursor. */
2065 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2066 {
2067 XRectangle rc, r_save = r;
2068
2069 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2070 rc.y = s->w->phys_cursor.y;
2071 rc.width = s->w->phys_cursor_width;
2072 rc.height = s->w->phys_cursor_height;
2073
2074 x_intersect_rectangles (&r_save, &rc, &r);
2075 }
2076 }
2077 else
2078 {
2079 /* Don't use S->y for clipping because it doesn't take partially
2080 visible lines into account. For example, it can be negative for
2081 partially visible lines at the top of a window. */
2082 if (!s->row->full_width_p
2083 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2084 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2085 else
2086 r.y = max (0, s->row->y);
2087 }
2088
2089 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2090
2091 /* If drawing the cursor, don't let glyph draw outside its
2092 advertised boundaries. Cleartype does this under some circumstances. */
2093 if (s->hl == DRAW_CURSOR)
2094 {
2095 struct glyph *glyph = s->first_glyph;
2096 int height, max_y;
2097
2098 if (s->x > r.x)
2099 {
2100 r.width -= s->x - r.x;
2101 r.x = s->x;
2102 }
2103 r.width = min (r.width, glyph->pixel_width);
2104
2105 /* If r.y is below window bottom, ensure that we still see a cursor. */
2106 height = min (glyph->ascent + glyph->descent,
2107 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2108 max_y = window_text_bottom_y (s->w) - height;
2109 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2110 if (s->ybase - glyph->ascent > max_y)
2111 {
2112 r.y = max_y;
2113 r.height = height;
2114 }
2115 else
2116 {
2117 /* Don't draw cursor glyph taller than our actual glyph. */
2118 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2119 if (height < r.height)
2120 {
2121 max_y = r.y + r.height;
2122 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2123 r.height = min (max_y - r.y, height);
2124 }
2125 }
2126 }
2127
2128 if (s->row->clip)
2129 {
2130 XRectangle r_save = r;
2131
2132 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2133 r.width = 0;
2134 }
2135
2136 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2137 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2138 {
2139 #ifdef CONVERT_FROM_XRECT
2140 CONVERT_FROM_XRECT (r, *rects);
2141 #else
2142 *rects = r;
2143 #endif
2144 return 1;
2145 }
2146 else
2147 {
2148 /* If we are processing overlapping and allowed to return
2149 multiple clipping rectangles, we exclude the row of the glyph
2150 string from the clipping rectangle. This is to avoid drawing
2151 the same text on the environment with anti-aliasing. */
2152 #ifdef CONVERT_FROM_XRECT
2153 XRectangle rs[2];
2154 #else
2155 XRectangle *rs = rects;
2156 #endif
2157 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2158
2159 if (s->for_overlaps & OVERLAPS_PRED)
2160 {
2161 rs[i] = r;
2162 if (r.y + r.height > row_y)
2163 {
2164 if (r.y < row_y)
2165 rs[i].height = row_y - r.y;
2166 else
2167 rs[i].height = 0;
2168 }
2169 i++;
2170 }
2171 if (s->for_overlaps & OVERLAPS_SUCC)
2172 {
2173 rs[i] = r;
2174 if (r.y < row_y + s->row->visible_height)
2175 {
2176 if (r.y + r.height > row_y + s->row->visible_height)
2177 {
2178 rs[i].y = row_y + s->row->visible_height;
2179 rs[i].height = r.y + r.height - rs[i].y;
2180 }
2181 else
2182 rs[i].height = 0;
2183 }
2184 i++;
2185 }
2186
2187 n = i;
2188 #ifdef CONVERT_FROM_XRECT
2189 for (i = 0; i < n; i++)
2190 CONVERT_FROM_XRECT (rs[i], rects[i]);
2191 #endif
2192 return n;
2193 }
2194 }
2195
2196 /* EXPORT:
2197 Return in *NR the clipping rectangle for glyph string S. */
2198
2199 void
2200 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2201 {
2202 get_glyph_string_clip_rects (s, nr, 1);
2203 }
2204
2205
2206 /* EXPORT:
2207 Return the position and height of the phys cursor in window W.
2208 Set w->phys_cursor_width to width of phys cursor.
2209 */
2210
2211 void
2212 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2213 struct glyph *glyph, int *xp, int *yp, int *heightp)
2214 {
2215 struct frame *f = XFRAME (WINDOW_FRAME (w));
2216 int x, y, wd, h, h0, y0;
2217
2218 /* Compute the width of the rectangle to draw. If on a stretch
2219 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2220 rectangle as wide as the glyph, but use a canonical character
2221 width instead. */
2222 wd = glyph->pixel_width - 1;
2223 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2224 wd++; /* Why? */
2225 #endif
2226
2227 x = w->phys_cursor.x;
2228 if (x < 0)
2229 {
2230 wd += x;
2231 x = 0;
2232 }
2233
2234 if (glyph->type == STRETCH_GLYPH
2235 && !x_stretch_cursor_p)
2236 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2237 w->phys_cursor_width = wd;
2238
2239 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2240
2241 /* If y is below window bottom, ensure that we still see a cursor. */
2242 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2243
2244 h = max (h0, glyph->ascent + glyph->descent);
2245 h0 = min (h0, glyph->ascent + glyph->descent);
2246
2247 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2248 if (y < y0)
2249 {
2250 h = max (h - (y0 - y) + 1, h0);
2251 y = y0 - 1;
2252 }
2253 else
2254 {
2255 y0 = window_text_bottom_y (w) - h0;
2256 if (y > y0)
2257 {
2258 h += y - y0;
2259 y = y0;
2260 }
2261 }
2262
2263 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2264 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2265 *heightp = h;
2266 }
2267
2268 /*
2269 * Remember which glyph the mouse is over.
2270 */
2271
2272 void
2273 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2274 {
2275 Lisp_Object window;
2276 struct window *w;
2277 struct glyph_row *r, *gr, *end_row;
2278 enum window_part part;
2279 enum glyph_row_area area;
2280 int x, y, width, height;
2281
2282 /* Try to determine frame pixel position and size of the glyph under
2283 frame pixel coordinates X/Y on frame F. */
2284
2285 if (!f->glyphs_initialized_p
2286 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2287 NILP (window)))
2288 {
2289 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2290 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2291 goto virtual_glyph;
2292 }
2293
2294 w = XWINDOW (window);
2295 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2296 height = WINDOW_FRAME_LINE_HEIGHT (w);
2297
2298 x = window_relative_x_coord (w, part, gx);
2299 y = gy - WINDOW_TOP_EDGE_Y (w);
2300
2301 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2302 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2303
2304 if (w->pseudo_window_p)
2305 {
2306 area = TEXT_AREA;
2307 part = ON_MODE_LINE; /* Don't adjust margin. */
2308 goto text_glyph;
2309 }
2310
2311 switch (part)
2312 {
2313 case ON_LEFT_MARGIN:
2314 area = LEFT_MARGIN_AREA;
2315 goto text_glyph;
2316
2317 case ON_RIGHT_MARGIN:
2318 area = RIGHT_MARGIN_AREA;
2319 goto text_glyph;
2320
2321 case ON_HEADER_LINE:
2322 case ON_MODE_LINE:
2323 gr = (part == ON_HEADER_LINE
2324 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2325 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2326 gy = gr->y;
2327 area = TEXT_AREA;
2328 goto text_glyph_row_found;
2329
2330 case ON_TEXT:
2331 area = TEXT_AREA;
2332
2333 text_glyph:
2334 gr = 0; gy = 0;
2335 for (; r <= end_row && r->enabled_p; ++r)
2336 if (r->y + r->height > y)
2337 {
2338 gr = r; gy = r->y;
2339 break;
2340 }
2341
2342 text_glyph_row_found:
2343 if (gr && gy <= y)
2344 {
2345 struct glyph *g = gr->glyphs[area];
2346 struct glyph *end = g + gr->used[area];
2347
2348 height = gr->height;
2349 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2350 if (gx + g->pixel_width > x)
2351 break;
2352
2353 if (g < end)
2354 {
2355 if (g->type == IMAGE_GLYPH)
2356 {
2357 /* Don't remember when mouse is over image, as
2358 image may have hot-spots. */
2359 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2360 return;
2361 }
2362 width = g->pixel_width;
2363 }
2364 else
2365 {
2366 /* Use nominal char spacing at end of line. */
2367 x -= gx;
2368 gx += (x / width) * width;
2369 }
2370
2371 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2372 gx += window_box_left_offset (w, area);
2373 }
2374 else
2375 {
2376 /* Use nominal line height at end of window. */
2377 gx = (x / width) * width;
2378 y -= gy;
2379 gy += (y / height) * height;
2380 }
2381 break;
2382
2383 case ON_LEFT_FRINGE:
2384 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2385 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2386 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2387 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2388 goto row_glyph;
2389
2390 case ON_RIGHT_FRINGE:
2391 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2392 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2393 : window_box_right_offset (w, TEXT_AREA));
2394 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2395 goto row_glyph;
2396
2397 case ON_SCROLL_BAR:
2398 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2399 ? 0
2400 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2401 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2402 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2403 : 0)));
2404 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2405
2406 row_glyph:
2407 gr = 0, gy = 0;
2408 for (; r <= end_row && r->enabled_p; ++r)
2409 if (r->y + r->height > y)
2410 {
2411 gr = r; gy = r->y;
2412 break;
2413 }
2414
2415 if (gr && gy <= y)
2416 height = gr->height;
2417 else
2418 {
2419 /* Use nominal line height at end of window. */
2420 y -= gy;
2421 gy += (y / height) * height;
2422 }
2423 break;
2424
2425 default:
2426 ;
2427 virtual_glyph:
2428 /* If there is no glyph under the mouse, then we divide the screen
2429 into a grid of the smallest glyph in the frame, and use that
2430 as our "glyph". */
2431
2432 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2433 round down even for negative values. */
2434 if (gx < 0)
2435 gx -= width - 1;
2436 if (gy < 0)
2437 gy -= height - 1;
2438
2439 gx = (gx / width) * width;
2440 gy = (gy / height) * height;
2441
2442 goto store_rect;
2443 }
2444
2445 gx += WINDOW_LEFT_EDGE_X (w);
2446 gy += WINDOW_TOP_EDGE_Y (w);
2447
2448 store_rect:
2449 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2450
2451 /* Visible feedback for debugging. */
2452 #if 0
2453 #if HAVE_X_WINDOWS
2454 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2455 f->output_data.x->normal_gc,
2456 gx, gy, width, height);
2457 #endif
2458 #endif
2459 }
2460
2461
2462 #endif /* HAVE_WINDOW_SYSTEM */
2463
2464 static void
2465 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2466 {
2467 eassert (w);
2468 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2469 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2470 w->window_end_vpos
2471 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2472 }
2473
2474 /***********************************************************************
2475 Lisp form evaluation
2476 ***********************************************************************/
2477
2478 /* Error handler for safe_eval and safe_call. */
2479
2480 static Lisp_Object
2481 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2482 {
2483 add_to_log ("Error during redisplay: %S signaled %S",
2484 Flist (nargs, args), arg);
2485 return Qnil;
2486 }
2487
2488 /* Call function FUNC with the rest of NARGS - 1 arguments
2489 following. Return the result, or nil if something went
2490 wrong. Prevent redisplay during the evaluation. */
2491
2492 Lisp_Object
2493 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2494 {
2495 Lisp_Object val;
2496
2497 if (inhibit_eval_during_redisplay)
2498 val = Qnil;
2499 else
2500 {
2501 va_list ap;
2502 ptrdiff_t i;
2503 ptrdiff_t count = SPECPDL_INDEX ();
2504 struct gcpro gcpro1;
2505 Lisp_Object *args = alloca (nargs * word_size);
2506
2507 args[0] = func;
2508 va_start (ap, func);
2509 for (i = 1; i < nargs; i++)
2510 args[i] = va_arg (ap, Lisp_Object);
2511 va_end (ap);
2512
2513 GCPRO1 (args[0]);
2514 gcpro1.nvars = nargs;
2515 specbind (Qinhibit_redisplay, Qt);
2516 /* Use Qt to ensure debugger does not run,
2517 so there is no possibility of wanting to redisplay. */
2518 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2519 safe_eval_handler);
2520 UNGCPRO;
2521 val = unbind_to (count, val);
2522 }
2523
2524 return val;
2525 }
2526
2527
2528 /* Call function FN with one argument ARG.
2529 Return the result, or nil if something went wrong. */
2530
2531 Lisp_Object
2532 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2533 {
2534 return safe_call (2, fn, arg);
2535 }
2536
2537 static Lisp_Object Qeval;
2538
2539 Lisp_Object
2540 safe_eval (Lisp_Object sexpr)
2541 {
2542 return safe_call1 (Qeval, sexpr);
2543 }
2544
2545 /* Call function FN with two arguments ARG1 and ARG2.
2546 Return the result, or nil if something went wrong. */
2547
2548 Lisp_Object
2549 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2550 {
2551 return safe_call (3, fn, arg1, arg2);
2552 }
2553
2554
2555 \f
2556 /***********************************************************************
2557 Debugging
2558 ***********************************************************************/
2559
2560 #if 0
2561
2562 /* Define CHECK_IT to perform sanity checks on iterators.
2563 This is for debugging. It is too slow to do unconditionally. */
2564
2565 static void
2566 check_it (struct it *it)
2567 {
2568 if (it->method == GET_FROM_STRING)
2569 {
2570 eassert (STRINGP (it->string));
2571 eassert (IT_STRING_CHARPOS (*it) >= 0);
2572 }
2573 else
2574 {
2575 eassert (IT_STRING_CHARPOS (*it) < 0);
2576 if (it->method == GET_FROM_BUFFER)
2577 {
2578 /* Check that character and byte positions agree. */
2579 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2580 }
2581 }
2582
2583 if (it->dpvec)
2584 eassert (it->current.dpvec_index >= 0);
2585 else
2586 eassert (it->current.dpvec_index < 0);
2587 }
2588
2589 #define CHECK_IT(IT) check_it ((IT))
2590
2591 #else /* not 0 */
2592
2593 #define CHECK_IT(IT) (void) 0
2594
2595 #endif /* not 0 */
2596
2597
2598 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2599
2600 /* Check that the window end of window W is what we expect it
2601 to be---the last row in the current matrix displaying text. */
2602
2603 static void
2604 check_window_end (struct window *w)
2605 {
2606 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2607 {
2608 struct glyph_row *row;
2609 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2610 !row->enabled_p
2611 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2612 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2613 }
2614 }
2615
2616 #define CHECK_WINDOW_END(W) check_window_end ((W))
2617
2618 #else
2619
2620 #define CHECK_WINDOW_END(W) (void) 0
2621
2622 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2623
2624 /* Return mark position if current buffer has the region of non-zero length,
2625 or -1 otherwise. */
2626
2627 static ptrdiff_t
2628 markpos_of_region (void)
2629 {
2630 if (!NILP (Vtransient_mark_mode)
2631 && !NILP (BVAR (current_buffer, mark_active))
2632 && XMARKER (BVAR (current_buffer, mark))->buffer != NULL)
2633 {
2634 ptrdiff_t markpos = XMARKER (BVAR (current_buffer, mark))->charpos;
2635
2636 if (markpos != PT)
2637 return markpos;
2638 }
2639 return -1;
2640 }
2641
2642 /***********************************************************************
2643 Iterator initialization
2644 ***********************************************************************/
2645
2646 /* Initialize IT for displaying current_buffer in window W, starting
2647 at character position CHARPOS. CHARPOS < 0 means that no buffer
2648 position is specified which is useful when the iterator is assigned
2649 a position later. BYTEPOS is the byte position corresponding to
2650 CHARPOS.
2651
2652 If ROW is not null, calls to produce_glyphs with IT as parameter
2653 will produce glyphs in that row.
2654
2655 BASE_FACE_ID is the id of a base face to use. It must be one of
2656 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2657 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2658 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2659
2660 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2661 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2662 will be initialized to use the corresponding mode line glyph row of
2663 the desired matrix of W. */
2664
2665 void
2666 init_iterator (struct it *it, struct window *w,
2667 ptrdiff_t charpos, ptrdiff_t bytepos,
2668 struct glyph_row *row, enum face_id base_face_id)
2669 {
2670 ptrdiff_t markpos;
2671 enum face_id remapped_base_face_id = base_face_id;
2672
2673 /* Some precondition checks. */
2674 eassert (w != NULL && it != NULL);
2675 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2676 && charpos <= ZV));
2677
2678 /* If face attributes have been changed since the last redisplay,
2679 free realized faces now because they depend on face definitions
2680 that might have changed. Don't free faces while there might be
2681 desired matrices pending which reference these faces. */
2682 if (face_change_count && !inhibit_free_realized_faces)
2683 {
2684 face_change_count = 0;
2685 free_all_realized_faces (Qnil);
2686 }
2687
2688 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2689 if (! NILP (Vface_remapping_alist))
2690 remapped_base_face_id
2691 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2692
2693 /* Use one of the mode line rows of W's desired matrix if
2694 appropriate. */
2695 if (row == NULL)
2696 {
2697 if (base_face_id == MODE_LINE_FACE_ID
2698 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2699 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2700 else if (base_face_id == HEADER_LINE_FACE_ID)
2701 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2702 }
2703
2704 /* Clear IT. */
2705 memset (it, 0, sizeof *it);
2706 it->current.overlay_string_index = -1;
2707 it->current.dpvec_index = -1;
2708 it->base_face_id = remapped_base_face_id;
2709 it->string = Qnil;
2710 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2711 it->paragraph_embedding = L2R;
2712 it->bidi_it.string.lstring = Qnil;
2713 it->bidi_it.string.s = NULL;
2714 it->bidi_it.string.bufpos = 0;
2715 it->bidi_it.w = w;
2716
2717 /* The window in which we iterate over current_buffer: */
2718 XSETWINDOW (it->window, w);
2719 it->w = w;
2720 it->f = XFRAME (w->frame);
2721
2722 it->cmp_it.id = -1;
2723
2724 /* Extra space between lines (on window systems only). */
2725 if (base_face_id == DEFAULT_FACE_ID
2726 && FRAME_WINDOW_P (it->f))
2727 {
2728 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2729 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2730 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2731 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2732 * FRAME_LINE_HEIGHT (it->f));
2733 else if (it->f->extra_line_spacing > 0)
2734 it->extra_line_spacing = it->f->extra_line_spacing;
2735 it->max_extra_line_spacing = 0;
2736 }
2737
2738 /* If realized faces have been removed, e.g. because of face
2739 attribute changes of named faces, recompute them. When running
2740 in batch mode, the face cache of the initial frame is null. If
2741 we happen to get called, make a dummy face cache. */
2742 if (FRAME_FACE_CACHE (it->f) == NULL)
2743 init_frame_faces (it->f);
2744 if (FRAME_FACE_CACHE (it->f)->used == 0)
2745 recompute_basic_faces (it->f);
2746
2747 /* Current value of the `slice', `space-width', and 'height' properties. */
2748 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2749 it->space_width = Qnil;
2750 it->font_height = Qnil;
2751 it->override_ascent = -1;
2752
2753 /* Are control characters displayed as `^C'? */
2754 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2755
2756 /* -1 means everything between a CR and the following line end
2757 is invisible. >0 means lines indented more than this value are
2758 invisible. */
2759 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2760 ? (clip_to_bounds
2761 (-1, XINT (BVAR (current_buffer, selective_display)),
2762 PTRDIFF_MAX))
2763 : (!NILP (BVAR (current_buffer, selective_display))
2764 ? -1 : 0));
2765 it->selective_display_ellipsis_p
2766 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2767
2768 /* Display table to use. */
2769 it->dp = window_display_table (w);
2770
2771 /* Are multibyte characters enabled in current_buffer? */
2772 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2773
2774 /* If visible region is of non-zero length, set IT->region_beg_charpos
2775 and IT->region_end_charpos to the start and end of a visible region
2776 in window IT->w. Set both to -1 to indicate no region. */
2777 markpos = markpos_of_region ();
2778 if (markpos >= 0
2779 /* Maybe highlight only in selected window. */
2780 && (/* Either show region everywhere. */
2781 highlight_nonselected_windows
2782 /* Or show region in the selected window. */
2783 || w == XWINDOW (selected_window)
2784 /* Or show the region if we are in the mini-buffer and W is
2785 the window the mini-buffer refers to. */
2786 || (MINI_WINDOW_P (XWINDOW (selected_window))
2787 && WINDOWP (minibuf_selected_window)
2788 && w == XWINDOW (minibuf_selected_window))))
2789 {
2790 it->region_beg_charpos = min (PT, markpos);
2791 it->region_end_charpos = max (PT, markpos);
2792 }
2793 else
2794 it->region_beg_charpos = it->region_end_charpos = -1;
2795
2796 /* Get the position at which the redisplay_end_trigger hook should
2797 be run, if it is to be run at all. */
2798 if (MARKERP (w->redisplay_end_trigger)
2799 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2800 it->redisplay_end_trigger_charpos
2801 = marker_position (w->redisplay_end_trigger);
2802 else if (INTEGERP (w->redisplay_end_trigger))
2803 it->redisplay_end_trigger_charpos =
2804 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2805
2806 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2807
2808 /* Are lines in the display truncated? */
2809 if (base_face_id != DEFAULT_FACE_ID
2810 || it->w->hscroll
2811 || (! WINDOW_FULL_WIDTH_P (it->w)
2812 && ((!NILP (Vtruncate_partial_width_windows)
2813 && !INTEGERP (Vtruncate_partial_width_windows))
2814 || (INTEGERP (Vtruncate_partial_width_windows)
2815 && (WINDOW_TOTAL_COLS (it->w)
2816 < XINT (Vtruncate_partial_width_windows))))))
2817 it->line_wrap = TRUNCATE;
2818 else if (NILP (BVAR (current_buffer, truncate_lines)))
2819 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2820 ? WINDOW_WRAP : WORD_WRAP;
2821 else
2822 it->line_wrap = TRUNCATE;
2823
2824 /* Get dimensions of truncation and continuation glyphs. These are
2825 displayed as fringe bitmaps under X, but we need them for such
2826 frames when the fringes are turned off. But leave the dimensions
2827 zero for tooltip frames, as these glyphs look ugly there and also
2828 sabotage calculations of tooltip dimensions in x-show-tip. */
2829 #ifdef HAVE_WINDOW_SYSTEM
2830 if (!(FRAME_WINDOW_P (it->f)
2831 && FRAMEP (tip_frame)
2832 && it->f == XFRAME (tip_frame)))
2833 #endif
2834 {
2835 if (it->line_wrap == TRUNCATE)
2836 {
2837 /* We will need the truncation glyph. */
2838 eassert (it->glyph_row == NULL);
2839 produce_special_glyphs (it, IT_TRUNCATION);
2840 it->truncation_pixel_width = it->pixel_width;
2841 }
2842 else
2843 {
2844 /* We will need the continuation glyph. */
2845 eassert (it->glyph_row == NULL);
2846 produce_special_glyphs (it, IT_CONTINUATION);
2847 it->continuation_pixel_width = it->pixel_width;
2848 }
2849 }
2850
2851 /* Reset these values to zero because the produce_special_glyphs
2852 above has changed them. */
2853 it->pixel_width = it->ascent = it->descent = 0;
2854 it->phys_ascent = it->phys_descent = 0;
2855
2856 /* Set this after getting the dimensions of truncation and
2857 continuation glyphs, so that we don't produce glyphs when calling
2858 produce_special_glyphs, above. */
2859 it->glyph_row = row;
2860 it->area = TEXT_AREA;
2861
2862 /* Forget any previous info about this row being reversed. */
2863 if (it->glyph_row)
2864 it->glyph_row->reversed_p = 0;
2865
2866 /* Get the dimensions of the display area. The display area
2867 consists of the visible window area plus a horizontally scrolled
2868 part to the left of the window. All x-values are relative to the
2869 start of this total display area. */
2870 if (base_face_id != DEFAULT_FACE_ID)
2871 {
2872 /* Mode lines, menu bar in terminal frames. */
2873 it->first_visible_x = 0;
2874 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2875 }
2876 else
2877 {
2878 it->first_visible_x =
2879 window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2880 it->last_visible_x = (it->first_visible_x
2881 + window_box_width (w, TEXT_AREA));
2882
2883 /* If we truncate lines, leave room for the truncation glyph(s) at
2884 the right margin. Otherwise, leave room for the continuation
2885 glyph(s). Done only if the window has no fringes. Since we
2886 don't know at this point whether there will be any R2L lines in
2887 the window, we reserve space for truncation/continuation glyphs
2888 even if only one of the fringes is absent. */
2889 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2890 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2891 {
2892 if (it->line_wrap == TRUNCATE)
2893 it->last_visible_x -= it->truncation_pixel_width;
2894 else
2895 it->last_visible_x -= it->continuation_pixel_width;
2896 }
2897
2898 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2899 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2900 }
2901
2902 /* Leave room for a border glyph. */
2903 if (!FRAME_WINDOW_P (it->f)
2904 && !WINDOW_RIGHTMOST_P (it->w))
2905 it->last_visible_x -= 1;
2906
2907 it->last_visible_y = window_text_bottom_y (w);
2908
2909 /* For mode lines and alike, arrange for the first glyph having a
2910 left box line if the face specifies a box. */
2911 if (base_face_id != DEFAULT_FACE_ID)
2912 {
2913 struct face *face;
2914
2915 it->face_id = remapped_base_face_id;
2916
2917 /* If we have a boxed mode line, make the first character appear
2918 with a left box line. */
2919 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2920 if (face->box != FACE_NO_BOX)
2921 it->start_of_box_run_p = 1;
2922 }
2923
2924 /* If a buffer position was specified, set the iterator there,
2925 getting overlays and face properties from that position. */
2926 if (charpos >= BUF_BEG (current_buffer))
2927 {
2928 it->end_charpos = ZV;
2929 eassert (charpos == BYTE_TO_CHAR (bytepos));
2930 IT_CHARPOS (*it) = charpos;
2931 IT_BYTEPOS (*it) = bytepos;
2932
2933 /* We will rely on `reseat' to set this up properly, via
2934 handle_face_prop. */
2935 it->face_id = it->base_face_id;
2936
2937 it->start = it->current;
2938 /* Do we need to reorder bidirectional text? Not if this is a
2939 unibyte buffer: by definition, none of the single-byte
2940 characters are strong R2L, so no reordering is needed. And
2941 bidi.c doesn't support unibyte buffers anyway. Also, don't
2942 reorder while we are loading loadup.el, since the tables of
2943 character properties needed for reordering are not yet
2944 available. */
2945 it->bidi_p =
2946 NILP (Vpurify_flag)
2947 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2948 && it->multibyte_p;
2949
2950 /* If we are to reorder bidirectional text, init the bidi
2951 iterator. */
2952 if (it->bidi_p)
2953 {
2954 /* Note the paragraph direction that this buffer wants to
2955 use. */
2956 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2957 Qleft_to_right))
2958 it->paragraph_embedding = L2R;
2959 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2960 Qright_to_left))
2961 it->paragraph_embedding = R2L;
2962 else
2963 it->paragraph_embedding = NEUTRAL_DIR;
2964 bidi_unshelve_cache (NULL, 0);
2965 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2966 &it->bidi_it);
2967 }
2968
2969 /* Compute faces etc. */
2970 reseat (it, it->current.pos, 1);
2971 }
2972
2973 CHECK_IT (it);
2974 }
2975
2976
2977 /* Initialize IT for the display of window W with window start POS. */
2978
2979 void
2980 start_display (struct it *it, struct window *w, struct text_pos pos)
2981 {
2982 struct glyph_row *row;
2983 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2984
2985 row = w->desired_matrix->rows + first_vpos;
2986 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2987 it->first_vpos = first_vpos;
2988
2989 /* Don't reseat to previous visible line start if current start
2990 position is in a string or image. */
2991 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2992 {
2993 int start_at_line_beg_p;
2994 int first_y = it->current_y;
2995
2996 /* If window start is not at a line start, skip forward to POS to
2997 get the correct continuation lines width. */
2998 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2999 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
3000 if (!start_at_line_beg_p)
3001 {
3002 int new_x;
3003
3004 reseat_at_previous_visible_line_start (it);
3005 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3006
3007 new_x = it->current_x + it->pixel_width;
3008
3009 /* If lines are continued, this line may end in the middle
3010 of a multi-glyph character (e.g. a control character
3011 displayed as \003, or in the middle of an overlay
3012 string). In this case move_it_to above will not have
3013 taken us to the start of the continuation line but to the
3014 end of the continued line. */
3015 if (it->current_x > 0
3016 && it->line_wrap != TRUNCATE /* Lines are continued. */
3017 && (/* And glyph doesn't fit on the line. */
3018 new_x > it->last_visible_x
3019 /* Or it fits exactly and we're on a window
3020 system frame. */
3021 || (new_x == it->last_visible_x
3022 && FRAME_WINDOW_P (it->f)
3023 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3024 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3025 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3026 {
3027 if ((it->current.dpvec_index >= 0
3028 || it->current.overlay_string_index >= 0)
3029 /* If we are on a newline from a display vector or
3030 overlay string, then we are already at the end of
3031 a screen line; no need to go to the next line in
3032 that case, as this line is not really continued.
3033 (If we do go to the next line, C-e will not DTRT.) */
3034 && it->c != '\n')
3035 {
3036 set_iterator_to_next (it, 1);
3037 move_it_in_display_line_to (it, -1, -1, 0);
3038 }
3039
3040 it->continuation_lines_width += it->current_x;
3041 }
3042 /* If the character at POS is displayed via a display
3043 vector, move_it_to above stops at the final glyph of
3044 IT->dpvec. To make the caller redisplay that character
3045 again (a.k.a. start at POS), we need to reset the
3046 dpvec_index to the beginning of IT->dpvec. */
3047 else if (it->current.dpvec_index >= 0)
3048 it->current.dpvec_index = 0;
3049
3050 /* We're starting a new display line, not affected by the
3051 height of the continued line, so clear the appropriate
3052 fields in the iterator structure. */
3053 it->max_ascent = it->max_descent = 0;
3054 it->max_phys_ascent = it->max_phys_descent = 0;
3055
3056 it->current_y = first_y;
3057 it->vpos = 0;
3058 it->current_x = it->hpos = 0;
3059 }
3060 }
3061 }
3062
3063
3064 /* Return 1 if POS is a position in ellipses displayed for invisible
3065 text. W is the window we display, for text property lookup. */
3066
3067 static int
3068 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3069 {
3070 Lisp_Object prop, window;
3071 int ellipses_p = 0;
3072 ptrdiff_t charpos = CHARPOS (pos->pos);
3073
3074 /* If POS specifies a position in a display vector, this might
3075 be for an ellipsis displayed for invisible text. We won't
3076 get the iterator set up for delivering that ellipsis unless
3077 we make sure that it gets aware of the invisible text. */
3078 if (pos->dpvec_index >= 0
3079 && pos->overlay_string_index < 0
3080 && CHARPOS (pos->string_pos) < 0
3081 && charpos > BEGV
3082 && (XSETWINDOW (window, w),
3083 prop = Fget_char_property (make_number (charpos),
3084 Qinvisible, window),
3085 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3086 {
3087 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3088 window);
3089 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3090 }
3091
3092 return ellipses_p;
3093 }
3094
3095
3096 /* Initialize IT for stepping through current_buffer in window W,
3097 starting at position POS that includes overlay string and display
3098 vector/ control character translation position information. Value
3099 is zero if there are overlay strings with newlines at POS. */
3100
3101 static int
3102 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3103 {
3104 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3105 int i, overlay_strings_with_newlines = 0;
3106
3107 /* If POS specifies a position in a display vector, this might
3108 be for an ellipsis displayed for invisible text. We won't
3109 get the iterator set up for delivering that ellipsis unless
3110 we make sure that it gets aware of the invisible text. */
3111 if (in_ellipses_for_invisible_text_p (pos, w))
3112 {
3113 --charpos;
3114 bytepos = 0;
3115 }
3116
3117 /* Keep in mind: the call to reseat in init_iterator skips invisible
3118 text, so we might end up at a position different from POS. This
3119 is only a problem when POS is a row start after a newline and an
3120 overlay starts there with an after-string, and the overlay has an
3121 invisible property. Since we don't skip invisible text in
3122 display_line and elsewhere immediately after consuming the
3123 newline before the row start, such a POS will not be in a string,
3124 but the call to init_iterator below will move us to the
3125 after-string. */
3126 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3127
3128 /* This only scans the current chunk -- it should scan all chunks.
3129 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3130 to 16 in 22.1 to make this a lesser problem. */
3131 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3132 {
3133 const char *s = SSDATA (it->overlay_strings[i]);
3134 const char *e = s + SBYTES (it->overlay_strings[i]);
3135
3136 while (s < e && *s != '\n')
3137 ++s;
3138
3139 if (s < e)
3140 {
3141 overlay_strings_with_newlines = 1;
3142 break;
3143 }
3144 }
3145
3146 /* If position is within an overlay string, set up IT to the right
3147 overlay string. */
3148 if (pos->overlay_string_index >= 0)
3149 {
3150 int relative_index;
3151
3152 /* If the first overlay string happens to have a `display'
3153 property for an image, the iterator will be set up for that
3154 image, and we have to undo that setup first before we can
3155 correct the overlay string index. */
3156 if (it->method == GET_FROM_IMAGE)
3157 pop_it (it);
3158
3159 /* We already have the first chunk of overlay strings in
3160 IT->overlay_strings. Load more until the one for
3161 pos->overlay_string_index is in IT->overlay_strings. */
3162 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3163 {
3164 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3165 it->current.overlay_string_index = 0;
3166 while (n--)
3167 {
3168 load_overlay_strings (it, 0);
3169 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3170 }
3171 }
3172
3173 it->current.overlay_string_index = pos->overlay_string_index;
3174 relative_index = (it->current.overlay_string_index
3175 % OVERLAY_STRING_CHUNK_SIZE);
3176 it->string = it->overlay_strings[relative_index];
3177 eassert (STRINGP (it->string));
3178 it->current.string_pos = pos->string_pos;
3179 it->method = GET_FROM_STRING;
3180 it->end_charpos = SCHARS (it->string);
3181 /* Set up the bidi iterator for this overlay string. */
3182 if (it->bidi_p)
3183 {
3184 it->bidi_it.string.lstring = it->string;
3185 it->bidi_it.string.s = NULL;
3186 it->bidi_it.string.schars = SCHARS (it->string);
3187 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3188 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3189 it->bidi_it.string.unibyte = !it->multibyte_p;
3190 it->bidi_it.w = it->w;
3191 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3192 FRAME_WINDOW_P (it->f), &it->bidi_it);
3193
3194 /* Synchronize the state of the bidi iterator with
3195 pos->string_pos. For any string position other than
3196 zero, this will be done automagically when we resume
3197 iteration over the string and get_visually_first_element
3198 is called. But if string_pos is zero, and the string is
3199 to be reordered for display, we need to resync manually,
3200 since it could be that the iteration state recorded in
3201 pos ended at string_pos of 0 moving backwards in string. */
3202 if (CHARPOS (pos->string_pos) == 0)
3203 {
3204 get_visually_first_element (it);
3205 if (IT_STRING_CHARPOS (*it) != 0)
3206 do {
3207 /* Paranoia. */
3208 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3209 bidi_move_to_visually_next (&it->bidi_it);
3210 } while (it->bidi_it.charpos != 0);
3211 }
3212 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3213 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3214 }
3215 }
3216
3217 if (CHARPOS (pos->string_pos) >= 0)
3218 {
3219 /* Recorded position is not in an overlay string, but in another
3220 string. This can only be a string from a `display' property.
3221 IT should already be filled with that string. */
3222 it->current.string_pos = pos->string_pos;
3223 eassert (STRINGP (it->string));
3224 if (it->bidi_p)
3225 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3226 FRAME_WINDOW_P (it->f), &it->bidi_it);
3227 }
3228
3229 /* Restore position in display vector translations, control
3230 character translations or ellipses. */
3231 if (pos->dpvec_index >= 0)
3232 {
3233 if (it->dpvec == NULL)
3234 get_next_display_element (it);
3235 eassert (it->dpvec && it->current.dpvec_index == 0);
3236 it->current.dpvec_index = pos->dpvec_index;
3237 }
3238
3239 CHECK_IT (it);
3240 return !overlay_strings_with_newlines;
3241 }
3242
3243
3244 /* Initialize IT for stepping through current_buffer in window W
3245 starting at ROW->start. */
3246
3247 static void
3248 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3249 {
3250 init_from_display_pos (it, w, &row->start);
3251 it->start = row->start;
3252 it->continuation_lines_width = row->continuation_lines_width;
3253 CHECK_IT (it);
3254 }
3255
3256
3257 /* Initialize IT for stepping through current_buffer in window W
3258 starting in the line following ROW, i.e. starting at ROW->end.
3259 Value is zero if there are overlay strings with newlines at ROW's
3260 end position. */
3261
3262 static int
3263 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3264 {
3265 int success = 0;
3266
3267 if (init_from_display_pos (it, w, &row->end))
3268 {
3269 if (row->continued_p)
3270 it->continuation_lines_width
3271 = row->continuation_lines_width + row->pixel_width;
3272 CHECK_IT (it);
3273 success = 1;
3274 }
3275
3276 return success;
3277 }
3278
3279
3280
3281 \f
3282 /***********************************************************************
3283 Text properties
3284 ***********************************************************************/
3285
3286 /* Called when IT reaches IT->stop_charpos. Handle text property and
3287 overlay changes. Set IT->stop_charpos to the next position where
3288 to stop. */
3289
3290 static void
3291 handle_stop (struct it *it)
3292 {
3293 enum prop_handled handled;
3294 int handle_overlay_change_p;
3295 struct props *p;
3296
3297 it->dpvec = NULL;
3298 it->current.dpvec_index = -1;
3299 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3300 it->ignore_overlay_strings_at_pos_p = 0;
3301 it->ellipsis_p = 0;
3302
3303 /* Use face of preceding text for ellipsis (if invisible) */
3304 if (it->selective_display_ellipsis_p)
3305 it->saved_face_id = it->face_id;
3306
3307 do
3308 {
3309 handled = HANDLED_NORMALLY;
3310
3311 /* Call text property handlers. */
3312 for (p = it_props; p->handler; ++p)
3313 {
3314 handled = p->handler (it);
3315
3316 if (handled == HANDLED_RECOMPUTE_PROPS)
3317 break;
3318 else if (handled == HANDLED_RETURN)
3319 {
3320 /* We still want to show before and after strings from
3321 overlays even if the actual buffer text is replaced. */
3322 if (!handle_overlay_change_p
3323 || it->sp > 1
3324 /* Don't call get_overlay_strings_1 if we already
3325 have overlay strings loaded, because doing so
3326 will load them again and push the iterator state
3327 onto the stack one more time, which is not
3328 expected by the rest of the code that processes
3329 overlay strings. */
3330 || (it->current.overlay_string_index < 0
3331 ? !get_overlay_strings_1 (it, 0, 0)
3332 : 0))
3333 {
3334 if (it->ellipsis_p)
3335 setup_for_ellipsis (it, 0);
3336 /* When handling a display spec, we might load an
3337 empty string. In that case, discard it here. We
3338 used to discard it in handle_single_display_spec,
3339 but that causes get_overlay_strings_1, above, to
3340 ignore overlay strings that we must check. */
3341 if (STRINGP (it->string) && !SCHARS (it->string))
3342 pop_it (it);
3343 return;
3344 }
3345 else if (STRINGP (it->string) && !SCHARS (it->string))
3346 pop_it (it);
3347 else
3348 {
3349 it->ignore_overlay_strings_at_pos_p = 1;
3350 it->string_from_display_prop_p = 0;
3351 it->from_disp_prop_p = 0;
3352 handle_overlay_change_p = 0;
3353 }
3354 handled = HANDLED_RECOMPUTE_PROPS;
3355 break;
3356 }
3357 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3358 handle_overlay_change_p = 0;
3359 }
3360
3361 if (handled != HANDLED_RECOMPUTE_PROPS)
3362 {
3363 /* Don't check for overlay strings below when set to deliver
3364 characters from a display vector. */
3365 if (it->method == GET_FROM_DISPLAY_VECTOR)
3366 handle_overlay_change_p = 0;
3367
3368 /* Handle overlay changes.
3369 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3370 if it finds overlays. */
3371 if (handle_overlay_change_p)
3372 handled = handle_overlay_change (it);
3373 }
3374
3375 if (it->ellipsis_p)
3376 {
3377 setup_for_ellipsis (it, 0);
3378 break;
3379 }
3380 }
3381 while (handled == HANDLED_RECOMPUTE_PROPS);
3382
3383 /* Determine where to stop next. */
3384 if (handled == HANDLED_NORMALLY)
3385 compute_stop_pos (it);
3386 }
3387
3388
3389 /* Compute IT->stop_charpos from text property and overlay change
3390 information for IT's current position. */
3391
3392 static void
3393 compute_stop_pos (struct it *it)
3394 {
3395 register INTERVAL iv, next_iv;
3396 Lisp_Object object, limit, position;
3397 ptrdiff_t charpos, bytepos;
3398
3399 if (STRINGP (it->string))
3400 {
3401 /* Strings are usually short, so don't limit the search for
3402 properties. */
3403 it->stop_charpos = it->end_charpos;
3404 object = it->string;
3405 limit = Qnil;
3406 charpos = IT_STRING_CHARPOS (*it);
3407 bytepos = IT_STRING_BYTEPOS (*it);
3408 }
3409 else
3410 {
3411 ptrdiff_t pos;
3412
3413 /* If end_charpos is out of range for some reason, such as a
3414 misbehaving display function, rationalize it (Bug#5984). */
3415 if (it->end_charpos > ZV)
3416 it->end_charpos = ZV;
3417 it->stop_charpos = it->end_charpos;
3418
3419 /* If next overlay change is in front of the current stop pos
3420 (which is IT->end_charpos), stop there. Note: value of
3421 next_overlay_change is point-max if no overlay change
3422 follows. */
3423 charpos = IT_CHARPOS (*it);
3424 bytepos = IT_BYTEPOS (*it);
3425 pos = next_overlay_change (charpos);
3426 if (pos < it->stop_charpos)
3427 it->stop_charpos = pos;
3428
3429 /* If showing the region, we have to stop at the region
3430 start or end because the face might change there. */
3431 if (it->region_beg_charpos > 0)
3432 {
3433 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3434 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3435 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3436 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3437 }
3438
3439 /* Set up variables for computing the stop position from text
3440 property changes. */
3441 XSETBUFFER (object, current_buffer);
3442 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3443 }
3444
3445 /* Get the interval containing IT's position. Value is a null
3446 interval if there isn't such an interval. */
3447 position = make_number (charpos);
3448 iv = validate_interval_range (object, &position, &position, 0);
3449 if (iv)
3450 {
3451 Lisp_Object values_here[LAST_PROP_IDX];
3452 struct props *p;
3453
3454 /* Get properties here. */
3455 for (p = it_props; p->handler; ++p)
3456 values_here[p->idx] = textget (iv->plist, *p->name);
3457
3458 /* Look for an interval following iv that has different
3459 properties. */
3460 for (next_iv = next_interval (iv);
3461 (next_iv
3462 && (NILP (limit)
3463 || XFASTINT (limit) > next_iv->position));
3464 next_iv = next_interval (next_iv))
3465 {
3466 for (p = it_props; p->handler; ++p)
3467 {
3468 Lisp_Object new_value;
3469
3470 new_value = textget (next_iv->plist, *p->name);
3471 if (!EQ (values_here[p->idx], new_value))
3472 break;
3473 }
3474
3475 if (p->handler)
3476 break;
3477 }
3478
3479 if (next_iv)
3480 {
3481 if (INTEGERP (limit)
3482 && next_iv->position >= XFASTINT (limit))
3483 /* No text property change up to limit. */
3484 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3485 else
3486 /* Text properties change in next_iv. */
3487 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3488 }
3489 }
3490
3491 if (it->cmp_it.id < 0)
3492 {
3493 ptrdiff_t stoppos = it->end_charpos;
3494
3495 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3496 stoppos = -1;
3497 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3498 stoppos, it->string);
3499 }
3500
3501 eassert (STRINGP (it->string)
3502 || (it->stop_charpos >= BEGV
3503 && it->stop_charpos >= IT_CHARPOS (*it)));
3504 }
3505
3506
3507 /* Return the position of the next overlay change after POS in
3508 current_buffer. Value is point-max if no overlay change
3509 follows. This is like `next-overlay-change' but doesn't use
3510 xmalloc. */
3511
3512 static ptrdiff_t
3513 next_overlay_change (ptrdiff_t pos)
3514 {
3515 ptrdiff_t i, noverlays;
3516 ptrdiff_t endpos;
3517 Lisp_Object *overlays;
3518
3519 /* Get all overlays at the given position. */
3520 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3521
3522 /* If any of these overlays ends before endpos,
3523 use its ending point instead. */
3524 for (i = 0; i < noverlays; ++i)
3525 {
3526 Lisp_Object oend;
3527 ptrdiff_t oendpos;
3528
3529 oend = OVERLAY_END (overlays[i]);
3530 oendpos = OVERLAY_POSITION (oend);
3531 endpos = min (endpos, oendpos);
3532 }
3533
3534 return endpos;
3535 }
3536
3537 /* How many characters forward to search for a display property or
3538 display string. Searching too far forward makes the bidi display
3539 sluggish, especially in small windows. */
3540 #define MAX_DISP_SCAN 250
3541
3542 /* Return the character position of a display string at or after
3543 position specified by POSITION. If no display string exists at or
3544 after POSITION, return ZV. A display string is either an overlay
3545 with `display' property whose value is a string, or a `display'
3546 text property whose value is a string. STRING is data about the
3547 string to iterate; if STRING->lstring is nil, we are iterating a
3548 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3549 on a GUI frame. DISP_PROP is set to zero if we searched
3550 MAX_DISP_SCAN characters forward without finding any display
3551 strings, non-zero otherwise. It is set to 2 if the display string
3552 uses any kind of `(space ...)' spec that will produce a stretch of
3553 white space in the text area. */
3554 ptrdiff_t
3555 compute_display_string_pos (struct text_pos *position,
3556 struct bidi_string_data *string,
3557 struct window *w,
3558 int frame_window_p, int *disp_prop)
3559 {
3560 /* OBJECT = nil means current buffer. */
3561 Lisp_Object object, object1;
3562 Lisp_Object pos, spec, limpos;
3563 int string_p = (string && (STRINGP (string->lstring) || string->s));
3564 ptrdiff_t eob = string_p ? string->schars : ZV;
3565 ptrdiff_t begb = string_p ? 0 : BEGV;
3566 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3567 ptrdiff_t lim =
3568 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3569 struct text_pos tpos;
3570 int rv = 0;
3571
3572 if (string && STRINGP (string->lstring))
3573 object1 = object = string->lstring;
3574 else if (w && !string_p)
3575 {
3576 XSETWINDOW (object, w);
3577 object1 = Qnil;
3578 }
3579 else
3580 object1 = object = Qnil;
3581
3582 *disp_prop = 1;
3583
3584 if (charpos >= eob
3585 /* We don't support display properties whose values are strings
3586 that have display string properties. */
3587 || string->from_disp_str
3588 /* C strings cannot have display properties. */
3589 || (string->s && !STRINGP (object)))
3590 {
3591 *disp_prop = 0;
3592 return eob;
3593 }
3594
3595 /* If the character at CHARPOS is where the display string begins,
3596 return CHARPOS. */
3597 pos = make_number (charpos);
3598 if (STRINGP (object))
3599 bufpos = string->bufpos;
3600 else
3601 bufpos = charpos;
3602 tpos = *position;
3603 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3604 && (charpos <= begb
3605 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3606 object),
3607 spec))
3608 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3609 frame_window_p)))
3610 {
3611 if (rv == 2)
3612 *disp_prop = 2;
3613 return charpos;
3614 }
3615
3616 /* Look forward for the first character with a `display' property
3617 that will replace the underlying text when displayed. */
3618 limpos = make_number (lim);
3619 do {
3620 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3621 CHARPOS (tpos) = XFASTINT (pos);
3622 if (CHARPOS (tpos) >= lim)
3623 {
3624 *disp_prop = 0;
3625 break;
3626 }
3627 if (STRINGP (object))
3628 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3629 else
3630 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3631 spec = Fget_char_property (pos, Qdisplay, object);
3632 if (!STRINGP (object))
3633 bufpos = CHARPOS (tpos);
3634 } while (NILP (spec)
3635 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3636 bufpos, frame_window_p)));
3637 if (rv == 2)
3638 *disp_prop = 2;
3639
3640 return CHARPOS (tpos);
3641 }
3642
3643 /* Return the character position of the end of the display string that
3644 started at CHARPOS. If there's no display string at CHARPOS,
3645 return -1. A display string is either an overlay with `display'
3646 property whose value is a string or a `display' text property whose
3647 value is a string. */
3648 ptrdiff_t
3649 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3650 {
3651 /* OBJECT = nil means current buffer. */
3652 Lisp_Object object =
3653 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3654 Lisp_Object pos = make_number (charpos);
3655 ptrdiff_t eob =
3656 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3657
3658 if (charpos >= eob || (string->s && !STRINGP (object)))
3659 return eob;
3660
3661 /* It could happen that the display property or overlay was removed
3662 since we found it in compute_display_string_pos above. One way
3663 this can happen is if JIT font-lock was called (through
3664 handle_fontified_prop), and jit-lock-functions remove text
3665 properties or overlays from the portion of buffer that includes
3666 CHARPOS. Muse mode is known to do that, for example. In this
3667 case, we return -1 to the caller, to signal that no display
3668 string is actually present at CHARPOS. See bidi_fetch_char for
3669 how this is handled.
3670
3671 An alternative would be to never look for display properties past
3672 it->stop_charpos. But neither compute_display_string_pos nor
3673 bidi_fetch_char that calls it know or care where the next
3674 stop_charpos is. */
3675 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3676 return -1;
3677
3678 /* Look forward for the first character where the `display' property
3679 changes. */
3680 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3681
3682 return XFASTINT (pos);
3683 }
3684
3685
3686 \f
3687 /***********************************************************************
3688 Fontification
3689 ***********************************************************************/
3690
3691 /* Handle changes in the `fontified' property of the current buffer by
3692 calling hook functions from Qfontification_functions to fontify
3693 regions of text. */
3694
3695 static enum prop_handled
3696 handle_fontified_prop (struct it *it)
3697 {
3698 Lisp_Object prop, pos;
3699 enum prop_handled handled = HANDLED_NORMALLY;
3700
3701 if (!NILP (Vmemory_full))
3702 return handled;
3703
3704 /* Get the value of the `fontified' property at IT's current buffer
3705 position. (The `fontified' property doesn't have a special
3706 meaning in strings.) If the value is nil, call functions from
3707 Qfontification_functions. */
3708 if (!STRINGP (it->string)
3709 && it->s == NULL
3710 && !NILP (Vfontification_functions)
3711 && !NILP (Vrun_hooks)
3712 && (pos = make_number (IT_CHARPOS (*it)),
3713 prop = Fget_char_property (pos, Qfontified, Qnil),
3714 /* Ignore the special cased nil value always present at EOB since
3715 no amount of fontifying will be able to change it. */
3716 NILP (prop) && IT_CHARPOS (*it) < Z))
3717 {
3718 ptrdiff_t count = SPECPDL_INDEX ();
3719 Lisp_Object val;
3720 struct buffer *obuf = current_buffer;
3721 int begv = BEGV, zv = ZV;
3722 int old_clip_changed = current_buffer->clip_changed;
3723
3724 val = Vfontification_functions;
3725 specbind (Qfontification_functions, Qnil);
3726
3727 eassert (it->end_charpos == ZV);
3728
3729 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3730 safe_call1 (val, pos);
3731 else
3732 {
3733 Lisp_Object fns, fn;
3734 struct gcpro gcpro1, gcpro2;
3735
3736 fns = Qnil;
3737 GCPRO2 (val, fns);
3738
3739 for (; CONSP (val); val = XCDR (val))
3740 {
3741 fn = XCAR (val);
3742
3743 if (EQ (fn, Qt))
3744 {
3745 /* A value of t indicates this hook has a local
3746 binding; it means to run the global binding too.
3747 In a global value, t should not occur. If it
3748 does, we must ignore it to avoid an endless
3749 loop. */
3750 for (fns = Fdefault_value (Qfontification_functions);
3751 CONSP (fns);
3752 fns = XCDR (fns))
3753 {
3754 fn = XCAR (fns);
3755 if (!EQ (fn, Qt))
3756 safe_call1 (fn, pos);
3757 }
3758 }
3759 else
3760 safe_call1 (fn, pos);
3761 }
3762
3763 UNGCPRO;
3764 }
3765
3766 unbind_to (count, Qnil);
3767
3768 /* Fontification functions routinely call `save-restriction'.
3769 Normally, this tags clip_changed, which can confuse redisplay
3770 (see discussion in Bug#6671). Since we don't perform any
3771 special handling of fontification changes in the case where
3772 `save-restriction' isn't called, there's no point doing so in
3773 this case either. So, if the buffer's restrictions are
3774 actually left unchanged, reset clip_changed. */
3775 if (obuf == current_buffer)
3776 {
3777 if (begv == BEGV && zv == ZV)
3778 current_buffer->clip_changed = old_clip_changed;
3779 }
3780 /* There isn't much we can reasonably do to protect against
3781 misbehaving fontification, but here's a fig leaf. */
3782 else if (BUFFER_LIVE_P (obuf))
3783 set_buffer_internal_1 (obuf);
3784
3785 /* The fontification code may have added/removed text.
3786 It could do even a lot worse, but let's at least protect against
3787 the most obvious case where only the text past `pos' gets changed',
3788 as is/was done in grep.el where some escapes sequences are turned
3789 into face properties (bug#7876). */
3790 it->end_charpos = ZV;
3791
3792 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3793 something. This avoids an endless loop if they failed to
3794 fontify the text for which reason ever. */
3795 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3796 handled = HANDLED_RECOMPUTE_PROPS;
3797 }
3798
3799 return handled;
3800 }
3801
3802
3803 \f
3804 /***********************************************************************
3805 Faces
3806 ***********************************************************************/
3807
3808 /* Set up iterator IT from face properties at its current position.
3809 Called from handle_stop. */
3810
3811 static enum prop_handled
3812 handle_face_prop (struct it *it)
3813 {
3814 int new_face_id;
3815 ptrdiff_t next_stop;
3816
3817 if (!STRINGP (it->string))
3818 {
3819 new_face_id
3820 = face_at_buffer_position (it->w,
3821 IT_CHARPOS (*it),
3822 it->region_beg_charpos,
3823 it->region_end_charpos,
3824 &next_stop,
3825 (IT_CHARPOS (*it)
3826 + TEXT_PROP_DISTANCE_LIMIT),
3827 0, it->base_face_id);
3828
3829 /* Is this a start of a run of characters with box face?
3830 Caveat: this can be called for a freshly initialized
3831 iterator; face_id is -1 in this case. We know that the new
3832 face will not change until limit, i.e. if the new face has a
3833 box, all characters up to limit will have one. But, as
3834 usual, we don't know whether limit is really the end. */
3835 if (new_face_id != it->face_id)
3836 {
3837 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3838 /* If it->face_id is -1, old_face below will be NULL, see
3839 the definition of FACE_FROM_ID. This will happen if this
3840 is the initial call that gets the face. */
3841 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3842
3843 /* If the value of face_id of the iterator is -1, we have to
3844 look in front of IT's position and see whether there is a
3845 face there that's different from new_face_id. */
3846 if (!old_face && IT_CHARPOS (*it) > BEG)
3847 {
3848 int prev_face_id = face_before_it_pos (it);
3849
3850 old_face = FACE_FROM_ID (it->f, prev_face_id);
3851 }
3852
3853 /* If the new face has a box, but the old face does not,
3854 this is the start of a run of characters with box face,
3855 i.e. this character has a shadow on the left side. */
3856 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3857 && (old_face == NULL || !old_face->box));
3858 it->face_box_p = new_face->box != FACE_NO_BOX;
3859 }
3860 }
3861 else
3862 {
3863 int base_face_id;
3864 ptrdiff_t bufpos;
3865 int i;
3866 Lisp_Object from_overlay
3867 = (it->current.overlay_string_index >= 0
3868 ? it->string_overlays[it->current.overlay_string_index
3869 % OVERLAY_STRING_CHUNK_SIZE]
3870 : Qnil);
3871
3872 /* See if we got to this string directly or indirectly from
3873 an overlay property. That includes the before-string or
3874 after-string of an overlay, strings in display properties
3875 provided by an overlay, their text properties, etc.
3876
3877 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3878 if (! NILP (from_overlay))
3879 for (i = it->sp - 1; i >= 0; i--)
3880 {
3881 if (it->stack[i].current.overlay_string_index >= 0)
3882 from_overlay
3883 = it->string_overlays[it->stack[i].current.overlay_string_index
3884 % OVERLAY_STRING_CHUNK_SIZE];
3885 else if (! NILP (it->stack[i].from_overlay))
3886 from_overlay = it->stack[i].from_overlay;
3887
3888 if (!NILP (from_overlay))
3889 break;
3890 }
3891
3892 if (! NILP (from_overlay))
3893 {
3894 bufpos = IT_CHARPOS (*it);
3895 /* For a string from an overlay, the base face depends
3896 only on text properties and ignores overlays. */
3897 base_face_id
3898 = face_for_overlay_string (it->w,
3899 IT_CHARPOS (*it),
3900 it->region_beg_charpos,
3901 it->region_end_charpos,
3902 &next_stop,
3903 (IT_CHARPOS (*it)
3904 + TEXT_PROP_DISTANCE_LIMIT),
3905 0,
3906 from_overlay);
3907 }
3908 else
3909 {
3910 bufpos = 0;
3911
3912 /* For strings from a `display' property, use the face at
3913 IT's current buffer position as the base face to merge
3914 with, so that overlay strings appear in the same face as
3915 surrounding text, unless they specify their own
3916 faces. */
3917 base_face_id = it->string_from_prefix_prop_p
3918 ? DEFAULT_FACE_ID
3919 : underlying_face_id (it);
3920 }
3921
3922 new_face_id = face_at_string_position (it->w,
3923 it->string,
3924 IT_STRING_CHARPOS (*it),
3925 bufpos,
3926 it->region_beg_charpos,
3927 it->region_end_charpos,
3928 &next_stop,
3929 base_face_id, 0);
3930
3931 /* Is this a start of a run of characters with box? Caveat:
3932 this can be called for a freshly allocated iterator; face_id
3933 is -1 is this case. We know that the new face will not
3934 change until the next check pos, i.e. if the new face has a
3935 box, all characters up to that position will have a
3936 box. But, as usual, we don't know whether that position
3937 is really the end. */
3938 if (new_face_id != it->face_id)
3939 {
3940 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3941 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3942
3943 /* If new face has a box but old face hasn't, this is the
3944 start of a run of characters with box, i.e. it has a
3945 shadow on the left side. */
3946 it->start_of_box_run_p
3947 = new_face->box && (old_face == NULL || !old_face->box);
3948 it->face_box_p = new_face->box != FACE_NO_BOX;
3949 }
3950 }
3951
3952 it->face_id = new_face_id;
3953 return HANDLED_NORMALLY;
3954 }
3955
3956
3957 /* Return the ID of the face ``underlying'' IT's current position,
3958 which is in a string. If the iterator is associated with a
3959 buffer, return the face at IT's current buffer position.
3960 Otherwise, use the iterator's base_face_id. */
3961
3962 static int
3963 underlying_face_id (struct it *it)
3964 {
3965 int face_id = it->base_face_id, i;
3966
3967 eassert (STRINGP (it->string));
3968
3969 for (i = it->sp - 1; i >= 0; --i)
3970 if (NILP (it->stack[i].string))
3971 face_id = it->stack[i].face_id;
3972
3973 return face_id;
3974 }
3975
3976
3977 /* Compute the face one character before or after the current position
3978 of IT, in the visual order. BEFORE_P non-zero means get the face
3979 in front (to the left in L2R paragraphs, to the right in R2L
3980 paragraphs) of IT's screen position. Value is the ID of the face. */
3981
3982 static int
3983 face_before_or_after_it_pos (struct it *it, int before_p)
3984 {
3985 int face_id, limit;
3986 ptrdiff_t next_check_charpos;
3987 struct it it_copy;
3988 void *it_copy_data = NULL;
3989
3990 eassert (it->s == NULL);
3991
3992 if (STRINGP (it->string))
3993 {
3994 ptrdiff_t bufpos, charpos;
3995 int base_face_id;
3996
3997 /* No face change past the end of the string (for the case
3998 we are padding with spaces). No face change before the
3999 string start. */
4000 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
4001 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
4002 return it->face_id;
4003
4004 if (!it->bidi_p)
4005 {
4006 /* Set charpos to the position before or after IT's current
4007 position, in the logical order, which in the non-bidi
4008 case is the same as the visual order. */
4009 if (before_p)
4010 charpos = IT_STRING_CHARPOS (*it) - 1;
4011 else if (it->what == IT_COMPOSITION)
4012 /* For composition, we must check the character after the
4013 composition. */
4014 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4015 else
4016 charpos = IT_STRING_CHARPOS (*it) + 1;
4017 }
4018 else
4019 {
4020 if (before_p)
4021 {
4022 /* With bidi iteration, the character before the current
4023 in the visual order cannot be found by simple
4024 iteration, because "reverse" reordering is not
4025 supported. Instead, we need to use the move_it_*
4026 family of functions. */
4027 /* Ignore face changes before the first visible
4028 character on this display line. */
4029 if (it->current_x <= it->first_visible_x)
4030 return it->face_id;
4031 SAVE_IT (it_copy, *it, it_copy_data);
4032 /* Implementation note: Since move_it_in_display_line
4033 works in the iterator geometry, and thinks the first
4034 character is always the leftmost, even in R2L lines,
4035 we don't need to distinguish between the R2L and L2R
4036 cases here. */
4037 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
4038 it_copy.current_x - 1, MOVE_TO_X);
4039 charpos = IT_STRING_CHARPOS (it_copy);
4040 RESTORE_IT (it, it, it_copy_data);
4041 }
4042 else
4043 {
4044 /* Set charpos to the string position of the character
4045 that comes after IT's current position in the visual
4046 order. */
4047 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4048
4049 it_copy = *it;
4050 while (n--)
4051 bidi_move_to_visually_next (&it_copy.bidi_it);
4052
4053 charpos = it_copy.bidi_it.charpos;
4054 }
4055 }
4056 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4057
4058 if (it->current.overlay_string_index >= 0)
4059 bufpos = IT_CHARPOS (*it);
4060 else
4061 bufpos = 0;
4062
4063 base_face_id = underlying_face_id (it);
4064
4065 /* Get the face for ASCII, or unibyte. */
4066 face_id = face_at_string_position (it->w,
4067 it->string,
4068 charpos,
4069 bufpos,
4070 it->region_beg_charpos,
4071 it->region_end_charpos,
4072 &next_check_charpos,
4073 base_face_id, 0);
4074
4075 /* Correct the face for charsets different from ASCII. Do it
4076 for the multibyte case only. The face returned above is
4077 suitable for unibyte text if IT->string is unibyte. */
4078 if (STRING_MULTIBYTE (it->string))
4079 {
4080 struct text_pos pos1 = string_pos (charpos, it->string);
4081 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4082 int c, len;
4083 struct face *face = FACE_FROM_ID (it->f, face_id);
4084
4085 c = string_char_and_length (p, &len);
4086 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4087 }
4088 }
4089 else
4090 {
4091 struct text_pos pos;
4092
4093 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4094 || (IT_CHARPOS (*it) <= BEGV && before_p))
4095 return it->face_id;
4096
4097 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4098 pos = it->current.pos;
4099
4100 if (!it->bidi_p)
4101 {
4102 if (before_p)
4103 DEC_TEXT_POS (pos, it->multibyte_p);
4104 else
4105 {
4106 if (it->what == IT_COMPOSITION)
4107 {
4108 /* For composition, we must check the position after
4109 the composition. */
4110 pos.charpos += it->cmp_it.nchars;
4111 pos.bytepos += it->len;
4112 }
4113 else
4114 INC_TEXT_POS (pos, it->multibyte_p);
4115 }
4116 }
4117 else
4118 {
4119 if (before_p)
4120 {
4121 /* With bidi iteration, the character before the current
4122 in the visual order cannot be found by simple
4123 iteration, because "reverse" reordering is not
4124 supported. Instead, we need to use the move_it_*
4125 family of functions. */
4126 /* Ignore face changes before the first visible
4127 character on this display line. */
4128 if (it->current_x <= it->first_visible_x)
4129 return it->face_id;
4130 SAVE_IT (it_copy, *it, it_copy_data);
4131 /* Implementation note: Since move_it_in_display_line
4132 works in the iterator geometry, and thinks the first
4133 character is always the leftmost, even in R2L lines,
4134 we don't need to distinguish between the R2L and L2R
4135 cases here. */
4136 move_it_in_display_line (&it_copy, ZV,
4137 it_copy.current_x - 1, MOVE_TO_X);
4138 pos = it_copy.current.pos;
4139 RESTORE_IT (it, it, it_copy_data);
4140 }
4141 else
4142 {
4143 /* Set charpos to the buffer position of the character
4144 that comes after IT's current position in the visual
4145 order. */
4146 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4147
4148 it_copy = *it;
4149 while (n--)
4150 bidi_move_to_visually_next (&it_copy.bidi_it);
4151
4152 SET_TEXT_POS (pos,
4153 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4154 }
4155 }
4156 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4157
4158 /* Determine face for CHARSET_ASCII, or unibyte. */
4159 face_id = face_at_buffer_position (it->w,
4160 CHARPOS (pos),
4161 it->region_beg_charpos,
4162 it->region_end_charpos,
4163 &next_check_charpos,
4164 limit, 0, -1);
4165
4166 /* Correct the face for charsets different from ASCII. Do it
4167 for the multibyte case only. The face returned above is
4168 suitable for unibyte text if current_buffer is unibyte. */
4169 if (it->multibyte_p)
4170 {
4171 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4172 struct face *face = FACE_FROM_ID (it->f, face_id);
4173 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4174 }
4175 }
4176
4177 return face_id;
4178 }
4179
4180
4181 \f
4182 /***********************************************************************
4183 Invisible text
4184 ***********************************************************************/
4185
4186 /* Set up iterator IT from invisible properties at its current
4187 position. Called from handle_stop. */
4188
4189 static enum prop_handled
4190 handle_invisible_prop (struct it *it)
4191 {
4192 enum prop_handled handled = HANDLED_NORMALLY;
4193 int invis_p;
4194 Lisp_Object prop;
4195
4196 if (STRINGP (it->string))
4197 {
4198 Lisp_Object end_charpos, limit, charpos;
4199
4200 /* Get the value of the invisible text property at the
4201 current position. Value will be nil if there is no such
4202 property. */
4203 charpos = make_number (IT_STRING_CHARPOS (*it));
4204 prop = Fget_text_property (charpos, Qinvisible, it->string);
4205 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4206
4207 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4208 {
4209 /* Record whether we have to display an ellipsis for the
4210 invisible text. */
4211 int display_ellipsis_p = (invis_p == 2);
4212 ptrdiff_t len, endpos;
4213
4214 handled = HANDLED_RECOMPUTE_PROPS;
4215
4216 /* Get the position at which the next visible text can be
4217 found in IT->string, if any. */
4218 endpos = len = SCHARS (it->string);
4219 XSETINT (limit, len);
4220 do
4221 {
4222 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4223 it->string, limit);
4224 if (INTEGERP (end_charpos))
4225 {
4226 endpos = XFASTINT (end_charpos);
4227 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4228 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4229 if (invis_p == 2)
4230 display_ellipsis_p = 1;
4231 }
4232 }
4233 while (invis_p && endpos < len);
4234
4235 if (display_ellipsis_p)
4236 it->ellipsis_p = 1;
4237
4238 if (endpos < len)
4239 {
4240 /* Text at END_CHARPOS is visible. Move IT there. */
4241 struct text_pos old;
4242 ptrdiff_t oldpos;
4243
4244 old = it->current.string_pos;
4245 oldpos = CHARPOS (old);
4246 if (it->bidi_p)
4247 {
4248 if (it->bidi_it.first_elt
4249 && it->bidi_it.charpos < SCHARS (it->string))
4250 bidi_paragraph_init (it->paragraph_embedding,
4251 &it->bidi_it, 1);
4252 /* Bidi-iterate out of the invisible text. */
4253 do
4254 {
4255 bidi_move_to_visually_next (&it->bidi_it);
4256 }
4257 while (oldpos <= it->bidi_it.charpos
4258 && it->bidi_it.charpos < endpos);
4259
4260 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4261 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4262 if (IT_CHARPOS (*it) >= endpos)
4263 it->prev_stop = endpos;
4264 }
4265 else
4266 {
4267 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4268 compute_string_pos (&it->current.string_pos, old, it->string);
4269 }
4270 }
4271 else
4272 {
4273 /* The rest of the string is invisible. If this is an
4274 overlay string, proceed with the next overlay string
4275 or whatever comes and return a character from there. */
4276 if (it->current.overlay_string_index >= 0
4277 && !display_ellipsis_p)
4278 {
4279 next_overlay_string (it);
4280 /* Don't check for overlay strings when we just
4281 finished processing them. */
4282 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4283 }
4284 else
4285 {
4286 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4287 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4288 }
4289 }
4290 }
4291 }
4292 else
4293 {
4294 ptrdiff_t newpos, next_stop, start_charpos, tem;
4295 Lisp_Object pos, overlay;
4296
4297 /* First of all, is there invisible text at this position? */
4298 tem = start_charpos = IT_CHARPOS (*it);
4299 pos = make_number (tem);
4300 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4301 &overlay);
4302 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4303
4304 /* If we are on invisible text, skip over it. */
4305 if (invis_p && start_charpos < it->end_charpos)
4306 {
4307 /* Record whether we have to display an ellipsis for the
4308 invisible text. */
4309 int display_ellipsis_p = invis_p == 2;
4310
4311 handled = HANDLED_RECOMPUTE_PROPS;
4312
4313 /* Loop skipping over invisible text. The loop is left at
4314 ZV or with IT on the first char being visible again. */
4315 do
4316 {
4317 /* Try to skip some invisible text. Return value is the
4318 position reached which can be equal to where we start
4319 if there is nothing invisible there. This skips both
4320 over invisible text properties and overlays with
4321 invisible property. */
4322 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4323
4324 /* If we skipped nothing at all we weren't at invisible
4325 text in the first place. If everything to the end of
4326 the buffer was skipped, end the loop. */
4327 if (newpos == tem || newpos >= ZV)
4328 invis_p = 0;
4329 else
4330 {
4331 /* We skipped some characters but not necessarily
4332 all there are. Check if we ended up on visible
4333 text. Fget_char_property returns the property of
4334 the char before the given position, i.e. if we
4335 get invis_p = 0, this means that the char at
4336 newpos is visible. */
4337 pos = make_number (newpos);
4338 prop = Fget_char_property (pos, Qinvisible, it->window);
4339 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4340 }
4341
4342 /* If we ended up on invisible text, proceed to
4343 skip starting with next_stop. */
4344 if (invis_p)
4345 tem = next_stop;
4346
4347 /* If there are adjacent invisible texts, don't lose the
4348 second one's ellipsis. */
4349 if (invis_p == 2)
4350 display_ellipsis_p = 1;
4351 }
4352 while (invis_p);
4353
4354 /* The position newpos is now either ZV or on visible text. */
4355 if (it->bidi_p)
4356 {
4357 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4358 int on_newline =
4359 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4360 int after_newline =
4361 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4362
4363 /* If the invisible text ends on a newline or on a
4364 character after a newline, we can avoid the costly,
4365 character by character, bidi iteration to NEWPOS, and
4366 instead simply reseat the iterator there. That's
4367 because all bidi reordering information is tossed at
4368 the newline. This is a big win for modes that hide
4369 complete lines, like Outline, Org, etc. */
4370 if (on_newline || after_newline)
4371 {
4372 struct text_pos tpos;
4373 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4374
4375 SET_TEXT_POS (tpos, newpos, bpos);
4376 reseat_1 (it, tpos, 0);
4377 /* If we reseat on a newline/ZV, we need to prep the
4378 bidi iterator for advancing to the next character
4379 after the newline/EOB, keeping the current paragraph
4380 direction (so that PRODUCE_GLYPHS does TRT wrt
4381 prepending/appending glyphs to a glyph row). */
4382 if (on_newline)
4383 {
4384 it->bidi_it.first_elt = 0;
4385 it->bidi_it.paragraph_dir = pdir;
4386 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4387 it->bidi_it.nchars = 1;
4388 it->bidi_it.ch_len = 1;
4389 }
4390 }
4391 else /* Must use the slow method. */
4392 {
4393 /* With bidi iteration, the region of invisible text
4394 could start and/or end in the middle of a
4395 non-base embedding level. Therefore, we need to
4396 skip invisible text using the bidi iterator,
4397 starting at IT's current position, until we find
4398 ourselves outside of the invisible text.
4399 Skipping invisible text _after_ bidi iteration
4400 avoids affecting the visual order of the
4401 displayed text when invisible properties are
4402 added or removed. */
4403 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4404 {
4405 /* If we were `reseat'ed to a new paragraph,
4406 determine the paragraph base direction. We
4407 need to do it now because
4408 next_element_from_buffer may not have a
4409 chance to do it, if we are going to skip any
4410 text at the beginning, which resets the
4411 FIRST_ELT flag. */
4412 bidi_paragraph_init (it->paragraph_embedding,
4413 &it->bidi_it, 1);
4414 }
4415 do
4416 {
4417 bidi_move_to_visually_next (&it->bidi_it);
4418 }
4419 while (it->stop_charpos <= it->bidi_it.charpos
4420 && it->bidi_it.charpos < newpos);
4421 IT_CHARPOS (*it) = it->bidi_it.charpos;
4422 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4423 /* If we overstepped NEWPOS, record its position in
4424 the iterator, so that we skip invisible text if
4425 later the bidi iteration lands us in the
4426 invisible region again. */
4427 if (IT_CHARPOS (*it) >= newpos)
4428 it->prev_stop = newpos;
4429 }
4430 }
4431 else
4432 {
4433 IT_CHARPOS (*it) = newpos;
4434 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4435 }
4436
4437 /* If there are before-strings at the start of invisible
4438 text, and the text is invisible because of a text
4439 property, arrange to show before-strings because 20.x did
4440 it that way. (If the text is invisible because of an
4441 overlay property instead of a text property, this is
4442 already handled in the overlay code.) */
4443 if (NILP (overlay)
4444 && get_overlay_strings (it, it->stop_charpos))
4445 {
4446 handled = HANDLED_RECOMPUTE_PROPS;
4447 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4448 }
4449 else if (display_ellipsis_p)
4450 {
4451 /* Make sure that the glyphs of the ellipsis will get
4452 correct `charpos' values. If we would not update
4453 it->position here, the glyphs would belong to the
4454 last visible character _before_ the invisible
4455 text, which confuses `set_cursor_from_row'.
4456
4457 We use the last invisible position instead of the
4458 first because this way the cursor is always drawn on
4459 the first "." of the ellipsis, whenever PT is inside
4460 the invisible text. Otherwise the cursor would be
4461 placed _after_ the ellipsis when the point is after the
4462 first invisible character. */
4463 if (!STRINGP (it->object))
4464 {
4465 it->position.charpos = newpos - 1;
4466 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4467 }
4468 it->ellipsis_p = 1;
4469 /* Let the ellipsis display before
4470 considering any properties of the following char.
4471 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4472 handled = HANDLED_RETURN;
4473 }
4474 }
4475 }
4476
4477 return handled;
4478 }
4479
4480
4481 /* Make iterator IT return `...' next.
4482 Replaces LEN characters from buffer. */
4483
4484 static void
4485 setup_for_ellipsis (struct it *it, int len)
4486 {
4487 /* Use the display table definition for `...'. Invalid glyphs
4488 will be handled by the method returning elements from dpvec. */
4489 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4490 {
4491 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4492 it->dpvec = v->contents;
4493 it->dpend = v->contents + v->header.size;
4494 }
4495 else
4496 {
4497 /* Default `...'. */
4498 it->dpvec = default_invis_vector;
4499 it->dpend = default_invis_vector + 3;
4500 }
4501
4502 it->dpvec_char_len = len;
4503 it->current.dpvec_index = 0;
4504 it->dpvec_face_id = -1;
4505
4506 /* Remember the current face id in case glyphs specify faces.
4507 IT's face is restored in set_iterator_to_next.
4508 saved_face_id was set to preceding char's face in handle_stop. */
4509 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4510 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4511
4512 it->method = GET_FROM_DISPLAY_VECTOR;
4513 it->ellipsis_p = 1;
4514 }
4515
4516
4517 \f
4518 /***********************************************************************
4519 'display' property
4520 ***********************************************************************/
4521
4522 /* Set up iterator IT from `display' property at its current position.
4523 Called from handle_stop.
4524 We return HANDLED_RETURN if some part of the display property
4525 overrides the display of the buffer text itself.
4526 Otherwise we return HANDLED_NORMALLY. */
4527
4528 static enum prop_handled
4529 handle_display_prop (struct it *it)
4530 {
4531 Lisp_Object propval, object, overlay;
4532 struct text_pos *position;
4533 ptrdiff_t bufpos;
4534 /* Nonzero if some property replaces the display of the text itself. */
4535 int display_replaced_p = 0;
4536
4537 if (STRINGP (it->string))
4538 {
4539 object = it->string;
4540 position = &it->current.string_pos;
4541 bufpos = CHARPOS (it->current.pos);
4542 }
4543 else
4544 {
4545 XSETWINDOW (object, it->w);
4546 position = &it->current.pos;
4547 bufpos = CHARPOS (*position);
4548 }
4549
4550 /* Reset those iterator values set from display property values. */
4551 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4552 it->space_width = Qnil;
4553 it->font_height = Qnil;
4554 it->voffset = 0;
4555
4556 /* We don't support recursive `display' properties, i.e. string
4557 values that have a string `display' property, that have a string
4558 `display' property etc. */
4559 if (!it->string_from_display_prop_p)
4560 it->area = TEXT_AREA;
4561
4562 propval = get_char_property_and_overlay (make_number (position->charpos),
4563 Qdisplay, object, &overlay);
4564 if (NILP (propval))
4565 return HANDLED_NORMALLY;
4566 /* Now OVERLAY is the overlay that gave us this property, or nil
4567 if it was a text property. */
4568
4569 if (!STRINGP (it->string))
4570 object = it->w->contents;
4571
4572 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4573 position, bufpos,
4574 FRAME_WINDOW_P (it->f));
4575
4576 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4577 }
4578
4579 /* Subroutine of handle_display_prop. Returns non-zero if the display
4580 specification in SPEC is a replacing specification, i.e. it would
4581 replace the text covered by `display' property with something else,
4582 such as an image or a display string. If SPEC includes any kind or
4583 `(space ...) specification, the value is 2; this is used by
4584 compute_display_string_pos, which see.
4585
4586 See handle_single_display_spec for documentation of arguments.
4587 frame_window_p is non-zero if the window being redisplayed is on a
4588 GUI frame; this argument is used only if IT is NULL, see below.
4589
4590 IT can be NULL, if this is called by the bidi reordering code
4591 through compute_display_string_pos, which see. In that case, this
4592 function only examines SPEC, but does not otherwise "handle" it, in
4593 the sense that it doesn't set up members of IT from the display
4594 spec. */
4595 static int
4596 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4597 Lisp_Object overlay, struct text_pos *position,
4598 ptrdiff_t bufpos, int frame_window_p)
4599 {
4600 int replacing_p = 0;
4601 int rv;
4602
4603 if (CONSP (spec)
4604 /* Simple specifications. */
4605 && !EQ (XCAR (spec), Qimage)
4606 && !EQ (XCAR (spec), Qspace)
4607 && !EQ (XCAR (spec), Qwhen)
4608 && !EQ (XCAR (spec), Qslice)
4609 && !EQ (XCAR (spec), Qspace_width)
4610 && !EQ (XCAR (spec), Qheight)
4611 && !EQ (XCAR (spec), Qraise)
4612 /* Marginal area specifications. */
4613 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4614 && !EQ (XCAR (spec), Qleft_fringe)
4615 && !EQ (XCAR (spec), Qright_fringe)
4616 && !NILP (XCAR (spec)))
4617 {
4618 for (; CONSP (spec); spec = XCDR (spec))
4619 {
4620 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4621 overlay, position, bufpos,
4622 replacing_p, frame_window_p)))
4623 {
4624 replacing_p = rv;
4625 /* If some text in a string is replaced, `position' no
4626 longer points to the position of `object'. */
4627 if (!it || STRINGP (object))
4628 break;
4629 }
4630 }
4631 }
4632 else if (VECTORP (spec))
4633 {
4634 ptrdiff_t i;
4635 for (i = 0; i < ASIZE (spec); ++i)
4636 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4637 overlay, position, bufpos,
4638 replacing_p, frame_window_p)))
4639 {
4640 replacing_p = rv;
4641 /* If some text in a string is replaced, `position' no
4642 longer points to the position of `object'. */
4643 if (!it || STRINGP (object))
4644 break;
4645 }
4646 }
4647 else
4648 {
4649 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4650 position, bufpos, 0,
4651 frame_window_p)))
4652 replacing_p = rv;
4653 }
4654
4655 return replacing_p;
4656 }
4657
4658 /* Value is the position of the end of the `display' property starting
4659 at START_POS in OBJECT. */
4660
4661 static struct text_pos
4662 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4663 {
4664 Lisp_Object end;
4665 struct text_pos end_pos;
4666
4667 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4668 Qdisplay, object, Qnil);
4669 CHARPOS (end_pos) = XFASTINT (end);
4670 if (STRINGP (object))
4671 compute_string_pos (&end_pos, start_pos, it->string);
4672 else
4673 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4674
4675 return end_pos;
4676 }
4677
4678
4679 /* Set up IT from a single `display' property specification SPEC. OBJECT
4680 is the object in which the `display' property was found. *POSITION
4681 is the position in OBJECT at which the `display' property was found.
4682 BUFPOS is the buffer position of OBJECT (different from POSITION if
4683 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4684 previously saw a display specification which already replaced text
4685 display with something else, for example an image; we ignore such
4686 properties after the first one has been processed.
4687
4688 OVERLAY is the overlay this `display' property came from,
4689 or nil if it was a text property.
4690
4691 If SPEC is a `space' or `image' specification, and in some other
4692 cases too, set *POSITION to the position where the `display'
4693 property ends.
4694
4695 If IT is NULL, only examine the property specification in SPEC, but
4696 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4697 is intended to be displayed in a window on a GUI frame.
4698
4699 Value is non-zero if something was found which replaces the display
4700 of buffer or string text. */
4701
4702 static int
4703 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4704 Lisp_Object overlay, struct text_pos *position,
4705 ptrdiff_t bufpos, int display_replaced_p,
4706 int frame_window_p)
4707 {
4708 Lisp_Object form;
4709 Lisp_Object location, value;
4710 struct text_pos start_pos = *position;
4711 int valid_p;
4712
4713 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4714 If the result is non-nil, use VALUE instead of SPEC. */
4715 form = Qt;
4716 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4717 {
4718 spec = XCDR (spec);
4719 if (!CONSP (spec))
4720 return 0;
4721 form = XCAR (spec);
4722 spec = XCDR (spec);
4723 }
4724
4725 if (!NILP (form) && !EQ (form, Qt))
4726 {
4727 ptrdiff_t count = SPECPDL_INDEX ();
4728 struct gcpro gcpro1;
4729
4730 /* Bind `object' to the object having the `display' property, a
4731 buffer or string. Bind `position' to the position in the
4732 object where the property was found, and `buffer-position'
4733 to the current position in the buffer. */
4734
4735 if (NILP (object))
4736 XSETBUFFER (object, current_buffer);
4737 specbind (Qobject, object);
4738 specbind (Qposition, make_number (CHARPOS (*position)));
4739 specbind (Qbuffer_position, make_number (bufpos));
4740 GCPRO1 (form);
4741 form = safe_eval (form);
4742 UNGCPRO;
4743 unbind_to (count, Qnil);
4744 }
4745
4746 if (NILP (form))
4747 return 0;
4748
4749 /* Handle `(height HEIGHT)' specifications. */
4750 if (CONSP (spec)
4751 && EQ (XCAR (spec), Qheight)
4752 && CONSP (XCDR (spec)))
4753 {
4754 if (it)
4755 {
4756 if (!FRAME_WINDOW_P (it->f))
4757 return 0;
4758
4759 it->font_height = XCAR (XCDR (spec));
4760 if (!NILP (it->font_height))
4761 {
4762 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4763 int new_height = -1;
4764
4765 if (CONSP (it->font_height)
4766 && (EQ (XCAR (it->font_height), Qplus)
4767 || EQ (XCAR (it->font_height), Qminus))
4768 && CONSP (XCDR (it->font_height))
4769 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4770 {
4771 /* `(+ N)' or `(- N)' where N is an integer. */
4772 int steps = XINT (XCAR (XCDR (it->font_height)));
4773 if (EQ (XCAR (it->font_height), Qplus))
4774 steps = - steps;
4775 it->face_id = smaller_face (it->f, it->face_id, steps);
4776 }
4777 else if (FUNCTIONP (it->font_height))
4778 {
4779 /* Call function with current height as argument.
4780 Value is the new height. */
4781 Lisp_Object height;
4782 height = safe_call1 (it->font_height,
4783 face->lface[LFACE_HEIGHT_INDEX]);
4784 if (NUMBERP (height))
4785 new_height = XFLOATINT (height);
4786 }
4787 else if (NUMBERP (it->font_height))
4788 {
4789 /* Value is a multiple of the canonical char height. */
4790 struct face *f;
4791
4792 f = FACE_FROM_ID (it->f,
4793 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4794 new_height = (XFLOATINT (it->font_height)
4795 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4796 }
4797 else
4798 {
4799 /* Evaluate IT->font_height with `height' bound to the
4800 current specified height to get the new height. */
4801 ptrdiff_t count = SPECPDL_INDEX ();
4802
4803 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4804 value = safe_eval (it->font_height);
4805 unbind_to (count, Qnil);
4806
4807 if (NUMBERP (value))
4808 new_height = XFLOATINT (value);
4809 }
4810
4811 if (new_height > 0)
4812 it->face_id = face_with_height (it->f, it->face_id, new_height);
4813 }
4814 }
4815
4816 return 0;
4817 }
4818
4819 /* Handle `(space-width WIDTH)'. */
4820 if (CONSP (spec)
4821 && EQ (XCAR (spec), Qspace_width)
4822 && CONSP (XCDR (spec)))
4823 {
4824 if (it)
4825 {
4826 if (!FRAME_WINDOW_P (it->f))
4827 return 0;
4828
4829 value = XCAR (XCDR (spec));
4830 if (NUMBERP (value) && XFLOATINT (value) > 0)
4831 it->space_width = value;
4832 }
4833
4834 return 0;
4835 }
4836
4837 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4838 if (CONSP (spec)
4839 && EQ (XCAR (spec), Qslice))
4840 {
4841 Lisp_Object tem;
4842
4843 if (it)
4844 {
4845 if (!FRAME_WINDOW_P (it->f))
4846 return 0;
4847
4848 if (tem = XCDR (spec), CONSP (tem))
4849 {
4850 it->slice.x = XCAR (tem);
4851 if (tem = XCDR (tem), CONSP (tem))
4852 {
4853 it->slice.y = XCAR (tem);
4854 if (tem = XCDR (tem), CONSP (tem))
4855 {
4856 it->slice.width = XCAR (tem);
4857 if (tem = XCDR (tem), CONSP (tem))
4858 it->slice.height = XCAR (tem);
4859 }
4860 }
4861 }
4862 }
4863
4864 return 0;
4865 }
4866
4867 /* Handle `(raise FACTOR)'. */
4868 if (CONSP (spec)
4869 && EQ (XCAR (spec), Qraise)
4870 && CONSP (XCDR (spec)))
4871 {
4872 if (it)
4873 {
4874 if (!FRAME_WINDOW_P (it->f))
4875 return 0;
4876
4877 #ifdef HAVE_WINDOW_SYSTEM
4878 value = XCAR (XCDR (spec));
4879 if (NUMBERP (value))
4880 {
4881 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4882 it->voffset = - (XFLOATINT (value)
4883 * (FONT_HEIGHT (face->font)));
4884 }
4885 #endif /* HAVE_WINDOW_SYSTEM */
4886 }
4887
4888 return 0;
4889 }
4890
4891 /* Don't handle the other kinds of display specifications
4892 inside a string that we got from a `display' property. */
4893 if (it && it->string_from_display_prop_p)
4894 return 0;
4895
4896 /* Characters having this form of property are not displayed, so
4897 we have to find the end of the property. */
4898 if (it)
4899 {
4900 start_pos = *position;
4901 *position = display_prop_end (it, object, start_pos);
4902 }
4903 value = Qnil;
4904
4905 /* Stop the scan at that end position--we assume that all
4906 text properties change there. */
4907 if (it)
4908 it->stop_charpos = position->charpos;
4909
4910 /* Handle `(left-fringe BITMAP [FACE])'
4911 and `(right-fringe BITMAP [FACE])'. */
4912 if (CONSP (spec)
4913 && (EQ (XCAR (spec), Qleft_fringe)
4914 || EQ (XCAR (spec), Qright_fringe))
4915 && CONSP (XCDR (spec)))
4916 {
4917 int fringe_bitmap;
4918
4919 if (it)
4920 {
4921 if (!FRAME_WINDOW_P (it->f))
4922 /* If we return here, POSITION has been advanced
4923 across the text with this property. */
4924 {
4925 /* Synchronize the bidi iterator with POSITION. This is
4926 needed because we are not going to push the iterator
4927 on behalf of this display property, so there will be
4928 no pop_it call to do this synchronization for us. */
4929 if (it->bidi_p)
4930 {
4931 it->position = *position;
4932 iterate_out_of_display_property (it);
4933 *position = it->position;
4934 }
4935 return 1;
4936 }
4937 }
4938 else if (!frame_window_p)
4939 return 1;
4940
4941 #ifdef HAVE_WINDOW_SYSTEM
4942 value = XCAR (XCDR (spec));
4943 if (!SYMBOLP (value)
4944 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4945 /* If we return here, POSITION has been advanced
4946 across the text with this property. */
4947 {
4948 if (it && it->bidi_p)
4949 {
4950 it->position = *position;
4951 iterate_out_of_display_property (it);
4952 *position = it->position;
4953 }
4954 return 1;
4955 }
4956
4957 if (it)
4958 {
4959 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4960
4961 if (CONSP (XCDR (XCDR (spec))))
4962 {
4963 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4964 int face_id2 = lookup_derived_face (it->f, face_name,
4965 FRINGE_FACE_ID, 0);
4966 if (face_id2 >= 0)
4967 face_id = face_id2;
4968 }
4969
4970 /* Save current settings of IT so that we can restore them
4971 when we are finished with the glyph property value. */
4972 push_it (it, position);
4973
4974 it->area = TEXT_AREA;
4975 it->what = IT_IMAGE;
4976 it->image_id = -1; /* no image */
4977 it->position = start_pos;
4978 it->object = NILP (object) ? it->w->contents : object;
4979 it->method = GET_FROM_IMAGE;
4980 it->from_overlay = Qnil;
4981 it->face_id = face_id;
4982 it->from_disp_prop_p = 1;
4983
4984 /* Say that we haven't consumed the characters with
4985 `display' property yet. The call to pop_it in
4986 set_iterator_to_next will clean this up. */
4987 *position = start_pos;
4988
4989 if (EQ (XCAR (spec), Qleft_fringe))
4990 {
4991 it->left_user_fringe_bitmap = fringe_bitmap;
4992 it->left_user_fringe_face_id = face_id;
4993 }
4994 else
4995 {
4996 it->right_user_fringe_bitmap = fringe_bitmap;
4997 it->right_user_fringe_face_id = face_id;
4998 }
4999 }
5000 #endif /* HAVE_WINDOW_SYSTEM */
5001 return 1;
5002 }
5003
5004 /* Prepare to handle `((margin left-margin) ...)',
5005 `((margin right-margin) ...)' and `((margin nil) ...)'
5006 prefixes for display specifications. */
5007 location = Qunbound;
5008 if (CONSP (spec) && CONSP (XCAR (spec)))
5009 {
5010 Lisp_Object tem;
5011
5012 value = XCDR (spec);
5013 if (CONSP (value))
5014 value = XCAR (value);
5015
5016 tem = XCAR (spec);
5017 if (EQ (XCAR (tem), Qmargin)
5018 && (tem = XCDR (tem),
5019 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5020 (NILP (tem)
5021 || EQ (tem, Qleft_margin)
5022 || EQ (tem, Qright_margin))))
5023 location = tem;
5024 }
5025
5026 if (EQ (location, Qunbound))
5027 {
5028 location = Qnil;
5029 value = spec;
5030 }
5031
5032 /* After this point, VALUE is the property after any
5033 margin prefix has been stripped. It must be a string,
5034 an image specification, or `(space ...)'.
5035
5036 LOCATION specifies where to display: `left-margin',
5037 `right-margin' or nil. */
5038
5039 valid_p = (STRINGP (value)
5040 #ifdef HAVE_WINDOW_SYSTEM
5041 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5042 && valid_image_p (value))
5043 #endif /* not HAVE_WINDOW_SYSTEM */
5044 || (CONSP (value) && EQ (XCAR (value), Qspace)));
5045
5046 if (valid_p && !display_replaced_p)
5047 {
5048 int retval = 1;
5049
5050 if (!it)
5051 {
5052 /* Callers need to know whether the display spec is any kind
5053 of `(space ...)' spec that is about to affect text-area
5054 display. */
5055 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5056 retval = 2;
5057 return retval;
5058 }
5059
5060 /* Save current settings of IT so that we can restore them
5061 when we are finished with the glyph property value. */
5062 push_it (it, position);
5063 it->from_overlay = overlay;
5064 it->from_disp_prop_p = 1;
5065
5066 if (NILP (location))
5067 it->area = TEXT_AREA;
5068 else if (EQ (location, Qleft_margin))
5069 it->area = LEFT_MARGIN_AREA;
5070 else
5071 it->area = RIGHT_MARGIN_AREA;
5072
5073 if (STRINGP (value))
5074 {
5075 it->string = value;
5076 it->multibyte_p = STRING_MULTIBYTE (it->string);
5077 it->current.overlay_string_index = -1;
5078 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5079 it->end_charpos = it->string_nchars = SCHARS (it->string);
5080 it->method = GET_FROM_STRING;
5081 it->stop_charpos = 0;
5082 it->prev_stop = 0;
5083 it->base_level_stop = 0;
5084 it->string_from_display_prop_p = 1;
5085 /* Say that we haven't consumed the characters with
5086 `display' property yet. The call to pop_it in
5087 set_iterator_to_next will clean this up. */
5088 if (BUFFERP (object))
5089 *position = start_pos;
5090
5091 /* Force paragraph direction to be that of the parent
5092 object. If the parent object's paragraph direction is
5093 not yet determined, default to L2R. */
5094 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5095 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5096 else
5097 it->paragraph_embedding = L2R;
5098
5099 /* Set up the bidi iterator for this display string. */
5100 if (it->bidi_p)
5101 {
5102 it->bidi_it.string.lstring = it->string;
5103 it->bidi_it.string.s = NULL;
5104 it->bidi_it.string.schars = it->end_charpos;
5105 it->bidi_it.string.bufpos = bufpos;
5106 it->bidi_it.string.from_disp_str = 1;
5107 it->bidi_it.string.unibyte = !it->multibyte_p;
5108 it->bidi_it.w = it->w;
5109 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5110 }
5111 }
5112 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5113 {
5114 it->method = GET_FROM_STRETCH;
5115 it->object = value;
5116 *position = it->position = start_pos;
5117 retval = 1 + (it->area == TEXT_AREA);
5118 }
5119 #ifdef HAVE_WINDOW_SYSTEM
5120 else
5121 {
5122 it->what = IT_IMAGE;
5123 it->image_id = lookup_image (it->f, value);
5124 it->position = start_pos;
5125 it->object = NILP (object) ? it->w->contents : object;
5126 it->method = GET_FROM_IMAGE;
5127
5128 /* Say that we haven't consumed the characters with
5129 `display' property yet. The call to pop_it in
5130 set_iterator_to_next will clean this up. */
5131 *position = start_pos;
5132 }
5133 #endif /* HAVE_WINDOW_SYSTEM */
5134
5135 return retval;
5136 }
5137
5138 /* Invalid property or property not supported. Restore
5139 POSITION to what it was before. */
5140 *position = start_pos;
5141 return 0;
5142 }
5143
5144 /* Check if PROP is a display property value whose text should be
5145 treated as intangible. OVERLAY is the overlay from which PROP
5146 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5147 specify the buffer position covered by PROP. */
5148
5149 int
5150 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5151 ptrdiff_t charpos, ptrdiff_t bytepos)
5152 {
5153 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5154 struct text_pos position;
5155
5156 SET_TEXT_POS (position, charpos, bytepos);
5157 return handle_display_spec (NULL, prop, Qnil, overlay,
5158 &position, charpos, frame_window_p);
5159 }
5160
5161
5162 /* Return 1 if PROP is a display sub-property value containing STRING.
5163
5164 Implementation note: this and the following function are really
5165 special cases of handle_display_spec and
5166 handle_single_display_spec, and should ideally use the same code.
5167 Until they do, these two pairs must be consistent and must be
5168 modified in sync. */
5169
5170 static int
5171 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5172 {
5173 if (EQ (string, prop))
5174 return 1;
5175
5176 /* Skip over `when FORM'. */
5177 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5178 {
5179 prop = XCDR (prop);
5180 if (!CONSP (prop))
5181 return 0;
5182 /* Actually, the condition following `when' should be eval'ed,
5183 like handle_single_display_spec does, and we should return
5184 zero if it evaluates to nil. However, this function is
5185 called only when the buffer was already displayed and some
5186 glyph in the glyph matrix was found to come from a display
5187 string. Therefore, the condition was already evaluated, and
5188 the result was non-nil, otherwise the display string wouldn't
5189 have been displayed and we would have never been called for
5190 this property. Thus, we can skip the evaluation and assume
5191 its result is non-nil. */
5192 prop = XCDR (prop);
5193 }
5194
5195 if (CONSP (prop))
5196 /* Skip over `margin LOCATION'. */
5197 if (EQ (XCAR (prop), Qmargin))
5198 {
5199 prop = XCDR (prop);
5200 if (!CONSP (prop))
5201 return 0;
5202
5203 prop = XCDR (prop);
5204 if (!CONSP (prop))
5205 return 0;
5206 }
5207
5208 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5209 }
5210
5211
5212 /* Return 1 if STRING appears in the `display' property PROP. */
5213
5214 static int
5215 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5216 {
5217 if (CONSP (prop)
5218 && !EQ (XCAR (prop), Qwhen)
5219 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5220 {
5221 /* A list of sub-properties. */
5222 while (CONSP (prop))
5223 {
5224 if (single_display_spec_string_p (XCAR (prop), string))
5225 return 1;
5226 prop = XCDR (prop);
5227 }
5228 }
5229 else if (VECTORP (prop))
5230 {
5231 /* A vector of sub-properties. */
5232 ptrdiff_t i;
5233 for (i = 0; i < ASIZE (prop); ++i)
5234 if (single_display_spec_string_p (AREF (prop, i), string))
5235 return 1;
5236 }
5237 else
5238 return single_display_spec_string_p (prop, string);
5239
5240 return 0;
5241 }
5242
5243 /* Look for STRING in overlays and text properties in the current
5244 buffer, between character positions FROM and TO (excluding TO).
5245 BACK_P non-zero means look back (in this case, TO is supposed to be
5246 less than FROM).
5247 Value is the first character position where STRING was found, or
5248 zero if it wasn't found before hitting TO.
5249
5250 This function may only use code that doesn't eval because it is
5251 called asynchronously from note_mouse_highlight. */
5252
5253 static ptrdiff_t
5254 string_buffer_position_lim (Lisp_Object string,
5255 ptrdiff_t from, ptrdiff_t to, int back_p)
5256 {
5257 Lisp_Object limit, prop, pos;
5258 int found = 0;
5259
5260 pos = make_number (max (from, BEGV));
5261
5262 if (!back_p) /* looking forward */
5263 {
5264 limit = make_number (min (to, ZV));
5265 while (!found && !EQ (pos, limit))
5266 {
5267 prop = Fget_char_property (pos, Qdisplay, Qnil);
5268 if (!NILP (prop) && display_prop_string_p (prop, string))
5269 found = 1;
5270 else
5271 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5272 limit);
5273 }
5274 }
5275 else /* looking back */
5276 {
5277 limit = make_number (max (to, BEGV));
5278 while (!found && !EQ (pos, limit))
5279 {
5280 prop = Fget_char_property (pos, Qdisplay, Qnil);
5281 if (!NILP (prop) && display_prop_string_p (prop, string))
5282 found = 1;
5283 else
5284 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5285 limit);
5286 }
5287 }
5288
5289 return found ? XINT (pos) : 0;
5290 }
5291
5292 /* Determine which buffer position in current buffer STRING comes from.
5293 AROUND_CHARPOS is an approximate position where it could come from.
5294 Value is the buffer position or 0 if it couldn't be determined.
5295
5296 This function is necessary because we don't record buffer positions
5297 in glyphs generated from strings (to keep struct glyph small).
5298 This function may only use code that doesn't eval because it is
5299 called asynchronously from note_mouse_highlight. */
5300
5301 static ptrdiff_t
5302 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5303 {
5304 const int MAX_DISTANCE = 1000;
5305 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5306 around_charpos + MAX_DISTANCE,
5307 0);
5308
5309 if (!found)
5310 found = string_buffer_position_lim (string, around_charpos,
5311 around_charpos - MAX_DISTANCE, 1);
5312 return found;
5313 }
5314
5315
5316 \f
5317 /***********************************************************************
5318 `composition' property
5319 ***********************************************************************/
5320
5321 /* Set up iterator IT from `composition' property at its current
5322 position. Called from handle_stop. */
5323
5324 static enum prop_handled
5325 handle_composition_prop (struct it *it)
5326 {
5327 Lisp_Object prop, string;
5328 ptrdiff_t pos, pos_byte, start, end;
5329
5330 if (STRINGP (it->string))
5331 {
5332 unsigned char *s;
5333
5334 pos = IT_STRING_CHARPOS (*it);
5335 pos_byte = IT_STRING_BYTEPOS (*it);
5336 string = it->string;
5337 s = SDATA (string) + pos_byte;
5338 it->c = STRING_CHAR (s);
5339 }
5340 else
5341 {
5342 pos = IT_CHARPOS (*it);
5343 pos_byte = IT_BYTEPOS (*it);
5344 string = Qnil;
5345 it->c = FETCH_CHAR (pos_byte);
5346 }
5347
5348 /* If there's a valid composition and point is not inside of the
5349 composition (in the case that the composition is from the current
5350 buffer), draw a glyph composed from the composition components. */
5351 if (find_composition (pos, -1, &start, &end, &prop, string)
5352 && composition_valid_p (start, end, prop)
5353 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5354 {
5355 if (start < pos)
5356 /* As we can't handle this situation (perhaps font-lock added
5357 a new composition), we just return here hoping that next
5358 redisplay will detect this composition much earlier. */
5359 return HANDLED_NORMALLY;
5360 if (start != pos)
5361 {
5362 if (STRINGP (it->string))
5363 pos_byte = string_char_to_byte (it->string, start);
5364 else
5365 pos_byte = CHAR_TO_BYTE (start);
5366 }
5367 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5368 prop, string);
5369
5370 if (it->cmp_it.id >= 0)
5371 {
5372 it->cmp_it.ch = -1;
5373 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5374 it->cmp_it.nglyphs = -1;
5375 }
5376 }
5377
5378 return HANDLED_NORMALLY;
5379 }
5380
5381
5382 \f
5383 /***********************************************************************
5384 Overlay strings
5385 ***********************************************************************/
5386
5387 /* The following structure is used to record overlay strings for
5388 later sorting in load_overlay_strings. */
5389
5390 struct overlay_entry
5391 {
5392 Lisp_Object overlay;
5393 Lisp_Object string;
5394 EMACS_INT priority;
5395 int after_string_p;
5396 };
5397
5398
5399 /* Set up iterator IT from overlay strings at its current position.
5400 Called from handle_stop. */
5401
5402 static enum prop_handled
5403 handle_overlay_change (struct it *it)
5404 {
5405 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5406 return HANDLED_RECOMPUTE_PROPS;
5407 else
5408 return HANDLED_NORMALLY;
5409 }
5410
5411
5412 /* Set up the next overlay string for delivery by IT, if there is an
5413 overlay string to deliver. Called by set_iterator_to_next when the
5414 end of the current overlay string is reached. If there are more
5415 overlay strings to display, IT->string and
5416 IT->current.overlay_string_index are set appropriately here.
5417 Otherwise IT->string is set to nil. */
5418
5419 static void
5420 next_overlay_string (struct it *it)
5421 {
5422 ++it->current.overlay_string_index;
5423 if (it->current.overlay_string_index == it->n_overlay_strings)
5424 {
5425 /* No more overlay strings. Restore IT's settings to what
5426 they were before overlay strings were processed, and
5427 continue to deliver from current_buffer. */
5428
5429 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5430 pop_it (it);
5431 eassert (it->sp > 0
5432 || (NILP (it->string)
5433 && it->method == GET_FROM_BUFFER
5434 && it->stop_charpos >= BEGV
5435 && it->stop_charpos <= it->end_charpos));
5436 it->current.overlay_string_index = -1;
5437 it->n_overlay_strings = 0;
5438 it->overlay_strings_charpos = -1;
5439 /* If there's an empty display string on the stack, pop the
5440 stack, to resync the bidi iterator with IT's position. Such
5441 empty strings are pushed onto the stack in
5442 get_overlay_strings_1. */
5443 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5444 pop_it (it);
5445
5446 /* If we're at the end of the buffer, record that we have
5447 processed the overlay strings there already, so that
5448 next_element_from_buffer doesn't try it again. */
5449 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5450 it->overlay_strings_at_end_processed_p = 1;
5451 }
5452 else
5453 {
5454 /* There are more overlay strings to process. If
5455 IT->current.overlay_string_index has advanced to a position
5456 where we must load IT->overlay_strings with more strings, do
5457 it. We must load at the IT->overlay_strings_charpos where
5458 IT->n_overlay_strings was originally computed; when invisible
5459 text is present, this might not be IT_CHARPOS (Bug#7016). */
5460 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5461
5462 if (it->current.overlay_string_index && i == 0)
5463 load_overlay_strings (it, it->overlay_strings_charpos);
5464
5465 /* Initialize IT to deliver display elements from the overlay
5466 string. */
5467 it->string = it->overlay_strings[i];
5468 it->multibyte_p = STRING_MULTIBYTE (it->string);
5469 SET_TEXT_POS (it->current.string_pos, 0, 0);
5470 it->method = GET_FROM_STRING;
5471 it->stop_charpos = 0;
5472 it->end_charpos = SCHARS (it->string);
5473 if (it->cmp_it.stop_pos >= 0)
5474 it->cmp_it.stop_pos = 0;
5475 it->prev_stop = 0;
5476 it->base_level_stop = 0;
5477
5478 /* Set up the bidi iterator for this overlay string. */
5479 if (it->bidi_p)
5480 {
5481 it->bidi_it.string.lstring = it->string;
5482 it->bidi_it.string.s = NULL;
5483 it->bidi_it.string.schars = SCHARS (it->string);
5484 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5485 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5486 it->bidi_it.string.unibyte = !it->multibyte_p;
5487 it->bidi_it.w = it->w;
5488 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5489 }
5490 }
5491
5492 CHECK_IT (it);
5493 }
5494
5495
5496 /* Compare two overlay_entry structures E1 and E2. Used as a
5497 comparison function for qsort in load_overlay_strings. Overlay
5498 strings for the same position are sorted so that
5499
5500 1. All after-strings come in front of before-strings, except
5501 when they come from the same overlay.
5502
5503 2. Within after-strings, strings are sorted so that overlay strings
5504 from overlays with higher priorities come first.
5505
5506 2. Within before-strings, strings are sorted so that overlay
5507 strings from overlays with higher priorities come last.
5508
5509 Value is analogous to strcmp. */
5510
5511
5512 static int
5513 compare_overlay_entries (const void *e1, const void *e2)
5514 {
5515 struct overlay_entry const *entry1 = e1;
5516 struct overlay_entry const *entry2 = e2;
5517 int result;
5518
5519 if (entry1->after_string_p != entry2->after_string_p)
5520 {
5521 /* Let after-strings appear in front of before-strings if
5522 they come from different overlays. */
5523 if (EQ (entry1->overlay, entry2->overlay))
5524 result = entry1->after_string_p ? 1 : -1;
5525 else
5526 result = entry1->after_string_p ? -1 : 1;
5527 }
5528 else if (entry1->priority != entry2->priority)
5529 {
5530 if (entry1->after_string_p)
5531 /* After-strings sorted in order of decreasing priority. */
5532 result = entry2->priority < entry1->priority ? -1 : 1;
5533 else
5534 /* Before-strings sorted in order of increasing priority. */
5535 result = entry1->priority < entry2->priority ? -1 : 1;
5536 }
5537 else
5538 result = 0;
5539
5540 return result;
5541 }
5542
5543
5544 /* Load the vector IT->overlay_strings with overlay strings from IT's
5545 current buffer position, or from CHARPOS if that is > 0. Set
5546 IT->n_overlays to the total number of overlay strings found.
5547
5548 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5549 a time. On entry into load_overlay_strings,
5550 IT->current.overlay_string_index gives the number of overlay
5551 strings that have already been loaded by previous calls to this
5552 function.
5553
5554 IT->add_overlay_start contains an additional overlay start
5555 position to consider for taking overlay strings from, if non-zero.
5556 This position comes into play when the overlay has an `invisible'
5557 property, and both before and after-strings. When we've skipped to
5558 the end of the overlay, because of its `invisible' property, we
5559 nevertheless want its before-string to appear.
5560 IT->add_overlay_start will contain the overlay start position
5561 in this case.
5562
5563 Overlay strings are sorted so that after-string strings come in
5564 front of before-string strings. Within before and after-strings,
5565 strings are sorted by overlay priority. See also function
5566 compare_overlay_entries. */
5567
5568 static void
5569 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5570 {
5571 Lisp_Object overlay, window, str, invisible;
5572 struct Lisp_Overlay *ov;
5573 ptrdiff_t start, end;
5574 ptrdiff_t size = 20;
5575 ptrdiff_t n = 0, i, j;
5576 int invis_p;
5577 struct overlay_entry *entries = alloca (size * sizeof *entries);
5578 USE_SAFE_ALLOCA;
5579
5580 if (charpos <= 0)
5581 charpos = IT_CHARPOS (*it);
5582
5583 /* Append the overlay string STRING of overlay OVERLAY to vector
5584 `entries' which has size `size' and currently contains `n'
5585 elements. AFTER_P non-zero means STRING is an after-string of
5586 OVERLAY. */
5587 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5588 do \
5589 { \
5590 Lisp_Object priority; \
5591 \
5592 if (n == size) \
5593 { \
5594 struct overlay_entry *old = entries; \
5595 SAFE_NALLOCA (entries, 2, size); \
5596 memcpy (entries, old, size * sizeof *entries); \
5597 size *= 2; \
5598 } \
5599 \
5600 entries[n].string = (STRING); \
5601 entries[n].overlay = (OVERLAY); \
5602 priority = Foverlay_get ((OVERLAY), Qpriority); \
5603 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5604 entries[n].after_string_p = (AFTER_P); \
5605 ++n; \
5606 } \
5607 while (0)
5608
5609 /* Process overlay before the overlay center. */
5610 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5611 {
5612 XSETMISC (overlay, ov);
5613 eassert (OVERLAYP (overlay));
5614 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5615 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5616
5617 if (end < charpos)
5618 break;
5619
5620 /* Skip this overlay if it doesn't start or end at IT's current
5621 position. */
5622 if (end != charpos && start != charpos)
5623 continue;
5624
5625 /* Skip this overlay if it doesn't apply to IT->w. */
5626 window = Foverlay_get (overlay, Qwindow);
5627 if (WINDOWP (window) && XWINDOW (window) != it->w)
5628 continue;
5629
5630 /* If the text ``under'' the overlay is invisible, both before-
5631 and after-strings from this overlay are visible; start and
5632 end position are indistinguishable. */
5633 invisible = Foverlay_get (overlay, Qinvisible);
5634 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5635
5636 /* If overlay has a non-empty before-string, record it. */
5637 if ((start == charpos || (end == charpos && invis_p))
5638 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5639 && SCHARS (str))
5640 RECORD_OVERLAY_STRING (overlay, str, 0);
5641
5642 /* If overlay has a non-empty after-string, record it. */
5643 if ((end == charpos || (start == charpos && invis_p))
5644 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5645 && SCHARS (str))
5646 RECORD_OVERLAY_STRING (overlay, str, 1);
5647 }
5648
5649 /* Process overlays after the overlay center. */
5650 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5651 {
5652 XSETMISC (overlay, ov);
5653 eassert (OVERLAYP (overlay));
5654 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5655 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5656
5657 if (start > charpos)
5658 break;
5659
5660 /* Skip this overlay if it doesn't start or end at IT's current
5661 position. */
5662 if (end != charpos && start != charpos)
5663 continue;
5664
5665 /* Skip this overlay if it doesn't apply to IT->w. */
5666 window = Foverlay_get (overlay, Qwindow);
5667 if (WINDOWP (window) && XWINDOW (window) != it->w)
5668 continue;
5669
5670 /* If the text ``under'' the overlay is invisible, it has a zero
5671 dimension, and both before- and after-strings apply. */
5672 invisible = Foverlay_get (overlay, Qinvisible);
5673 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5674
5675 /* If overlay has a non-empty before-string, record it. */
5676 if ((start == charpos || (end == charpos && invis_p))
5677 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5678 && SCHARS (str))
5679 RECORD_OVERLAY_STRING (overlay, str, 0);
5680
5681 /* If overlay has a non-empty after-string, record it. */
5682 if ((end == charpos || (start == charpos && invis_p))
5683 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5684 && SCHARS (str))
5685 RECORD_OVERLAY_STRING (overlay, str, 1);
5686 }
5687
5688 #undef RECORD_OVERLAY_STRING
5689
5690 /* Sort entries. */
5691 if (n > 1)
5692 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5693
5694 /* Record number of overlay strings, and where we computed it. */
5695 it->n_overlay_strings = n;
5696 it->overlay_strings_charpos = charpos;
5697
5698 /* IT->current.overlay_string_index is the number of overlay strings
5699 that have already been consumed by IT. Copy some of the
5700 remaining overlay strings to IT->overlay_strings. */
5701 i = 0;
5702 j = it->current.overlay_string_index;
5703 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5704 {
5705 it->overlay_strings[i] = entries[j].string;
5706 it->string_overlays[i++] = entries[j++].overlay;
5707 }
5708
5709 CHECK_IT (it);
5710 SAFE_FREE ();
5711 }
5712
5713
5714 /* Get the first chunk of overlay strings at IT's current buffer
5715 position, or at CHARPOS if that is > 0. Value is non-zero if at
5716 least one overlay string was found. */
5717
5718 static int
5719 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5720 {
5721 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5722 process. This fills IT->overlay_strings with strings, and sets
5723 IT->n_overlay_strings to the total number of strings to process.
5724 IT->pos.overlay_string_index has to be set temporarily to zero
5725 because load_overlay_strings needs this; it must be set to -1
5726 when no overlay strings are found because a zero value would
5727 indicate a position in the first overlay string. */
5728 it->current.overlay_string_index = 0;
5729 load_overlay_strings (it, charpos);
5730
5731 /* If we found overlay strings, set up IT to deliver display
5732 elements from the first one. Otherwise set up IT to deliver
5733 from current_buffer. */
5734 if (it->n_overlay_strings)
5735 {
5736 /* Make sure we know settings in current_buffer, so that we can
5737 restore meaningful values when we're done with the overlay
5738 strings. */
5739 if (compute_stop_p)
5740 compute_stop_pos (it);
5741 eassert (it->face_id >= 0);
5742
5743 /* Save IT's settings. They are restored after all overlay
5744 strings have been processed. */
5745 eassert (!compute_stop_p || it->sp == 0);
5746
5747 /* When called from handle_stop, there might be an empty display
5748 string loaded. In that case, don't bother saving it. But
5749 don't use this optimization with the bidi iterator, since we
5750 need the corresponding pop_it call to resync the bidi
5751 iterator's position with IT's position, after we are done
5752 with the overlay strings. (The corresponding call to pop_it
5753 in case of an empty display string is in
5754 next_overlay_string.) */
5755 if (!(!it->bidi_p
5756 && STRINGP (it->string) && !SCHARS (it->string)))
5757 push_it (it, NULL);
5758
5759 /* Set up IT to deliver display elements from the first overlay
5760 string. */
5761 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5762 it->string = it->overlay_strings[0];
5763 it->from_overlay = Qnil;
5764 it->stop_charpos = 0;
5765 eassert (STRINGP (it->string));
5766 it->end_charpos = SCHARS (it->string);
5767 it->prev_stop = 0;
5768 it->base_level_stop = 0;
5769 it->multibyte_p = STRING_MULTIBYTE (it->string);
5770 it->method = GET_FROM_STRING;
5771 it->from_disp_prop_p = 0;
5772
5773 /* Force paragraph direction to be that of the parent
5774 buffer. */
5775 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5776 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5777 else
5778 it->paragraph_embedding = L2R;
5779
5780 /* Set up the bidi iterator for this overlay string. */
5781 if (it->bidi_p)
5782 {
5783 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5784
5785 it->bidi_it.string.lstring = it->string;
5786 it->bidi_it.string.s = NULL;
5787 it->bidi_it.string.schars = SCHARS (it->string);
5788 it->bidi_it.string.bufpos = pos;
5789 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5790 it->bidi_it.string.unibyte = !it->multibyte_p;
5791 it->bidi_it.w = it->w;
5792 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5793 }
5794 return 1;
5795 }
5796
5797 it->current.overlay_string_index = -1;
5798 return 0;
5799 }
5800
5801 static int
5802 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5803 {
5804 it->string = Qnil;
5805 it->method = GET_FROM_BUFFER;
5806
5807 (void) get_overlay_strings_1 (it, charpos, 1);
5808
5809 CHECK_IT (it);
5810
5811 /* Value is non-zero if we found at least one overlay string. */
5812 return STRINGP (it->string);
5813 }
5814
5815
5816 \f
5817 /***********************************************************************
5818 Saving and restoring state
5819 ***********************************************************************/
5820
5821 /* Save current settings of IT on IT->stack. Called, for example,
5822 before setting up IT for an overlay string, to be able to restore
5823 IT's settings to what they were after the overlay string has been
5824 processed. If POSITION is non-NULL, it is the position to save on
5825 the stack instead of IT->position. */
5826
5827 static void
5828 push_it (struct it *it, struct text_pos *position)
5829 {
5830 struct iterator_stack_entry *p;
5831
5832 eassert (it->sp < IT_STACK_SIZE);
5833 p = it->stack + it->sp;
5834
5835 p->stop_charpos = it->stop_charpos;
5836 p->prev_stop = it->prev_stop;
5837 p->base_level_stop = it->base_level_stop;
5838 p->cmp_it = it->cmp_it;
5839 eassert (it->face_id >= 0);
5840 p->face_id = it->face_id;
5841 p->string = it->string;
5842 p->method = it->method;
5843 p->from_overlay = it->from_overlay;
5844 switch (p->method)
5845 {
5846 case GET_FROM_IMAGE:
5847 p->u.image.object = it->object;
5848 p->u.image.image_id = it->image_id;
5849 p->u.image.slice = it->slice;
5850 break;
5851 case GET_FROM_STRETCH:
5852 p->u.stretch.object = it->object;
5853 break;
5854 }
5855 p->position = position ? *position : it->position;
5856 p->current = it->current;
5857 p->end_charpos = it->end_charpos;
5858 p->string_nchars = it->string_nchars;
5859 p->area = it->area;
5860 p->multibyte_p = it->multibyte_p;
5861 p->avoid_cursor_p = it->avoid_cursor_p;
5862 p->space_width = it->space_width;
5863 p->font_height = it->font_height;
5864 p->voffset = it->voffset;
5865 p->string_from_display_prop_p = it->string_from_display_prop_p;
5866 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5867 p->display_ellipsis_p = 0;
5868 p->line_wrap = it->line_wrap;
5869 p->bidi_p = it->bidi_p;
5870 p->paragraph_embedding = it->paragraph_embedding;
5871 p->from_disp_prop_p = it->from_disp_prop_p;
5872 ++it->sp;
5873
5874 /* Save the state of the bidi iterator as well. */
5875 if (it->bidi_p)
5876 bidi_push_it (&it->bidi_it);
5877 }
5878
5879 static void
5880 iterate_out_of_display_property (struct it *it)
5881 {
5882 int buffer_p = !STRINGP (it->string);
5883 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5884 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5885
5886 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5887
5888 /* Maybe initialize paragraph direction. If we are at the beginning
5889 of a new paragraph, next_element_from_buffer may not have a
5890 chance to do that. */
5891 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5892 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5893 /* prev_stop can be zero, so check against BEGV as well. */
5894 while (it->bidi_it.charpos >= bob
5895 && it->prev_stop <= it->bidi_it.charpos
5896 && it->bidi_it.charpos < CHARPOS (it->position)
5897 && it->bidi_it.charpos < eob)
5898 bidi_move_to_visually_next (&it->bidi_it);
5899 /* Record the stop_pos we just crossed, for when we cross it
5900 back, maybe. */
5901 if (it->bidi_it.charpos > CHARPOS (it->position))
5902 it->prev_stop = CHARPOS (it->position);
5903 /* If we ended up not where pop_it put us, resync IT's
5904 positional members with the bidi iterator. */
5905 if (it->bidi_it.charpos != CHARPOS (it->position))
5906 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5907 if (buffer_p)
5908 it->current.pos = it->position;
5909 else
5910 it->current.string_pos = it->position;
5911 }
5912
5913 /* Restore IT's settings from IT->stack. Called, for example, when no
5914 more overlay strings must be processed, and we return to delivering
5915 display elements from a buffer, or when the end of a string from a
5916 `display' property is reached and we return to delivering display
5917 elements from an overlay string, or from a buffer. */
5918
5919 static void
5920 pop_it (struct it *it)
5921 {
5922 struct iterator_stack_entry *p;
5923 int from_display_prop = it->from_disp_prop_p;
5924
5925 eassert (it->sp > 0);
5926 --it->sp;
5927 p = it->stack + it->sp;
5928 it->stop_charpos = p->stop_charpos;
5929 it->prev_stop = p->prev_stop;
5930 it->base_level_stop = p->base_level_stop;
5931 it->cmp_it = p->cmp_it;
5932 it->face_id = p->face_id;
5933 it->current = p->current;
5934 it->position = p->position;
5935 it->string = p->string;
5936 it->from_overlay = p->from_overlay;
5937 if (NILP (it->string))
5938 SET_TEXT_POS (it->current.string_pos, -1, -1);
5939 it->method = p->method;
5940 switch (it->method)
5941 {
5942 case GET_FROM_IMAGE:
5943 it->image_id = p->u.image.image_id;
5944 it->object = p->u.image.object;
5945 it->slice = p->u.image.slice;
5946 break;
5947 case GET_FROM_STRETCH:
5948 it->object = p->u.stretch.object;
5949 break;
5950 case GET_FROM_BUFFER:
5951 it->object = it->w->contents;
5952 break;
5953 case GET_FROM_STRING:
5954 it->object = it->string;
5955 break;
5956 case GET_FROM_DISPLAY_VECTOR:
5957 if (it->s)
5958 it->method = GET_FROM_C_STRING;
5959 else if (STRINGP (it->string))
5960 it->method = GET_FROM_STRING;
5961 else
5962 {
5963 it->method = GET_FROM_BUFFER;
5964 it->object = it->w->contents;
5965 }
5966 }
5967 it->end_charpos = p->end_charpos;
5968 it->string_nchars = p->string_nchars;
5969 it->area = p->area;
5970 it->multibyte_p = p->multibyte_p;
5971 it->avoid_cursor_p = p->avoid_cursor_p;
5972 it->space_width = p->space_width;
5973 it->font_height = p->font_height;
5974 it->voffset = p->voffset;
5975 it->string_from_display_prop_p = p->string_from_display_prop_p;
5976 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5977 it->line_wrap = p->line_wrap;
5978 it->bidi_p = p->bidi_p;
5979 it->paragraph_embedding = p->paragraph_embedding;
5980 it->from_disp_prop_p = p->from_disp_prop_p;
5981 if (it->bidi_p)
5982 {
5983 bidi_pop_it (&it->bidi_it);
5984 /* Bidi-iterate until we get out of the portion of text, if any,
5985 covered by a `display' text property or by an overlay with
5986 `display' property. (We cannot just jump there, because the
5987 internal coherency of the bidi iterator state can not be
5988 preserved across such jumps.) We also must determine the
5989 paragraph base direction if the overlay we just processed is
5990 at the beginning of a new paragraph. */
5991 if (from_display_prop
5992 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5993 iterate_out_of_display_property (it);
5994
5995 eassert ((BUFFERP (it->object)
5996 && IT_CHARPOS (*it) == it->bidi_it.charpos
5997 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5998 || (STRINGP (it->object)
5999 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
6000 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
6001 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
6002 }
6003 }
6004
6005
6006 \f
6007 /***********************************************************************
6008 Moving over lines
6009 ***********************************************************************/
6010
6011 /* Set IT's current position to the previous line start. */
6012
6013 static void
6014 back_to_previous_line_start (struct it *it)
6015 {
6016 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6017
6018 DEC_BOTH (cp, bp);
6019 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6020 }
6021
6022
6023 /* Move IT to the next line start.
6024
6025 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
6026 we skipped over part of the text (as opposed to moving the iterator
6027 continuously over the text). Otherwise, don't change the value
6028 of *SKIPPED_P.
6029
6030 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6031 iterator on the newline, if it was found.
6032
6033 Newlines may come from buffer text, overlay strings, or strings
6034 displayed via the `display' property. That's the reason we can't
6035 simply use find_newline_no_quit.
6036
6037 Note that this function may not skip over invisible text that is so
6038 because of text properties and immediately follows a newline. If
6039 it would, function reseat_at_next_visible_line_start, when called
6040 from set_iterator_to_next, would effectively make invisible
6041 characters following a newline part of the wrong glyph row, which
6042 leads to wrong cursor motion. */
6043
6044 static int
6045 forward_to_next_line_start (struct it *it, int *skipped_p,
6046 struct bidi_it *bidi_it_prev)
6047 {
6048 ptrdiff_t old_selective;
6049 int newline_found_p, n;
6050 const int MAX_NEWLINE_DISTANCE = 500;
6051
6052 /* If already on a newline, just consume it to avoid unintended
6053 skipping over invisible text below. */
6054 if (it->what == IT_CHARACTER
6055 && it->c == '\n'
6056 && CHARPOS (it->position) == IT_CHARPOS (*it))
6057 {
6058 if (it->bidi_p && bidi_it_prev)
6059 *bidi_it_prev = it->bidi_it;
6060 set_iterator_to_next (it, 0);
6061 it->c = 0;
6062 return 1;
6063 }
6064
6065 /* Don't handle selective display in the following. It's (a)
6066 unnecessary because it's done by the caller, and (b) leads to an
6067 infinite recursion because next_element_from_ellipsis indirectly
6068 calls this function. */
6069 old_selective = it->selective;
6070 it->selective = 0;
6071
6072 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6073 from buffer text. */
6074 for (n = newline_found_p = 0;
6075 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6076 n += STRINGP (it->string) ? 0 : 1)
6077 {
6078 if (!get_next_display_element (it))
6079 return 0;
6080 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6081 if (newline_found_p && it->bidi_p && bidi_it_prev)
6082 *bidi_it_prev = it->bidi_it;
6083 set_iterator_to_next (it, 0);
6084 }
6085
6086 /* If we didn't find a newline near enough, see if we can use a
6087 short-cut. */
6088 if (!newline_found_p)
6089 {
6090 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6091 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6092 1, &bytepos);
6093 Lisp_Object pos;
6094
6095 eassert (!STRINGP (it->string));
6096
6097 /* If there isn't any `display' property in sight, and no
6098 overlays, we can just use the position of the newline in
6099 buffer text. */
6100 if (it->stop_charpos >= limit
6101 || ((pos = Fnext_single_property_change (make_number (start),
6102 Qdisplay, Qnil,
6103 make_number (limit)),
6104 NILP (pos))
6105 && next_overlay_change (start) == ZV))
6106 {
6107 if (!it->bidi_p)
6108 {
6109 IT_CHARPOS (*it) = limit;
6110 IT_BYTEPOS (*it) = bytepos;
6111 }
6112 else
6113 {
6114 struct bidi_it bprev;
6115
6116 /* Help bidi.c avoid expensive searches for display
6117 properties and overlays, by telling it that there are
6118 none up to `limit'. */
6119 if (it->bidi_it.disp_pos < limit)
6120 {
6121 it->bidi_it.disp_pos = limit;
6122 it->bidi_it.disp_prop = 0;
6123 }
6124 do {
6125 bprev = it->bidi_it;
6126 bidi_move_to_visually_next (&it->bidi_it);
6127 } while (it->bidi_it.charpos != limit);
6128 IT_CHARPOS (*it) = limit;
6129 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6130 if (bidi_it_prev)
6131 *bidi_it_prev = bprev;
6132 }
6133 *skipped_p = newline_found_p = 1;
6134 }
6135 else
6136 {
6137 while (get_next_display_element (it)
6138 && !newline_found_p)
6139 {
6140 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6141 if (newline_found_p && it->bidi_p && bidi_it_prev)
6142 *bidi_it_prev = it->bidi_it;
6143 set_iterator_to_next (it, 0);
6144 }
6145 }
6146 }
6147
6148 it->selective = old_selective;
6149 return newline_found_p;
6150 }
6151
6152
6153 /* Set IT's current position to the previous visible line start. Skip
6154 invisible text that is so either due to text properties or due to
6155 selective display. Caution: this does not change IT->current_x and
6156 IT->hpos. */
6157
6158 static void
6159 back_to_previous_visible_line_start (struct it *it)
6160 {
6161 while (IT_CHARPOS (*it) > BEGV)
6162 {
6163 back_to_previous_line_start (it);
6164
6165 if (IT_CHARPOS (*it) <= BEGV)
6166 break;
6167
6168 /* If selective > 0, then lines indented more than its value are
6169 invisible. */
6170 if (it->selective > 0
6171 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6172 it->selective))
6173 continue;
6174
6175 /* Check the newline before point for invisibility. */
6176 {
6177 Lisp_Object prop;
6178 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6179 Qinvisible, it->window);
6180 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6181 continue;
6182 }
6183
6184 if (IT_CHARPOS (*it) <= BEGV)
6185 break;
6186
6187 {
6188 struct it it2;
6189 void *it2data = NULL;
6190 ptrdiff_t pos;
6191 ptrdiff_t beg, end;
6192 Lisp_Object val, overlay;
6193
6194 SAVE_IT (it2, *it, it2data);
6195
6196 /* If newline is part of a composition, continue from start of composition */
6197 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6198 && beg < IT_CHARPOS (*it))
6199 goto replaced;
6200
6201 /* If newline is replaced by a display property, find start of overlay
6202 or interval and continue search from that point. */
6203 pos = --IT_CHARPOS (it2);
6204 --IT_BYTEPOS (it2);
6205 it2.sp = 0;
6206 bidi_unshelve_cache (NULL, 0);
6207 it2.string_from_display_prop_p = 0;
6208 it2.from_disp_prop_p = 0;
6209 if (handle_display_prop (&it2) == HANDLED_RETURN
6210 && !NILP (val = get_char_property_and_overlay
6211 (make_number (pos), Qdisplay, Qnil, &overlay))
6212 && (OVERLAYP (overlay)
6213 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6214 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6215 {
6216 RESTORE_IT (it, it, it2data);
6217 goto replaced;
6218 }
6219
6220 /* Newline is not replaced by anything -- so we are done. */
6221 RESTORE_IT (it, it, it2data);
6222 break;
6223
6224 replaced:
6225 if (beg < BEGV)
6226 beg = BEGV;
6227 IT_CHARPOS (*it) = beg;
6228 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6229 }
6230 }
6231
6232 it->continuation_lines_width = 0;
6233
6234 eassert (IT_CHARPOS (*it) >= BEGV);
6235 eassert (IT_CHARPOS (*it) == BEGV
6236 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6237 CHECK_IT (it);
6238 }
6239
6240
6241 /* Reseat iterator IT at the previous visible line start. Skip
6242 invisible text that is so either due to text properties or due to
6243 selective display. At the end, update IT's overlay information,
6244 face information etc. */
6245
6246 void
6247 reseat_at_previous_visible_line_start (struct it *it)
6248 {
6249 back_to_previous_visible_line_start (it);
6250 reseat (it, it->current.pos, 1);
6251 CHECK_IT (it);
6252 }
6253
6254
6255 /* Reseat iterator IT on the next visible line start in the current
6256 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6257 preceding the line start. Skip over invisible text that is so
6258 because of selective display. Compute faces, overlays etc at the
6259 new position. Note that this function does not skip over text that
6260 is invisible because of text properties. */
6261
6262 static void
6263 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6264 {
6265 int newline_found_p, skipped_p = 0;
6266 struct bidi_it bidi_it_prev;
6267
6268 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6269
6270 /* Skip over lines that are invisible because they are indented
6271 more than the value of IT->selective. */
6272 if (it->selective > 0)
6273 while (IT_CHARPOS (*it) < ZV
6274 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6275 it->selective))
6276 {
6277 eassert (IT_BYTEPOS (*it) == BEGV
6278 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6279 newline_found_p =
6280 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6281 }
6282
6283 /* Position on the newline if that's what's requested. */
6284 if (on_newline_p && newline_found_p)
6285 {
6286 if (STRINGP (it->string))
6287 {
6288 if (IT_STRING_CHARPOS (*it) > 0)
6289 {
6290 if (!it->bidi_p)
6291 {
6292 --IT_STRING_CHARPOS (*it);
6293 --IT_STRING_BYTEPOS (*it);
6294 }
6295 else
6296 {
6297 /* We need to restore the bidi iterator to the state
6298 it had on the newline, and resync the IT's
6299 position with that. */
6300 it->bidi_it = bidi_it_prev;
6301 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6302 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6303 }
6304 }
6305 }
6306 else if (IT_CHARPOS (*it) > BEGV)
6307 {
6308 if (!it->bidi_p)
6309 {
6310 --IT_CHARPOS (*it);
6311 --IT_BYTEPOS (*it);
6312 }
6313 else
6314 {
6315 /* We need to restore the bidi iterator to the state it
6316 had on the newline and resync IT with that. */
6317 it->bidi_it = bidi_it_prev;
6318 IT_CHARPOS (*it) = it->bidi_it.charpos;
6319 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6320 }
6321 reseat (it, it->current.pos, 0);
6322 }
6323 }
6324 else if (skipped_p)
6325 reseat (it, it->current.pos, 0);
6326
6327 CHECK_IT (it);
6328 }
6329
6330
6331 \f
6332 /***********************************************************************
6333 Changing an iterator's position
6334 ***********************************************************************/
6335
6336 /* Change IT's current position to POS in current_buffer. If FORCE_P
6337 is non-zero, always check for text properties at the new position.
6338 Otherwise, text properties are only looked up if POS >=
6339 IT->check_charpos of a property. */
6340
6341 static void
6342 reseat (struct it *it, struct text_pos pos, int force_p)
6343 {
6344 ptrdiff_t original_pos = IT_CHARPOS (*it);
6345
6346 reseat_1 (it, pos, 0);
6347
6348 /* Determine where to check text properties. Avoid doing it
6349 where possible because text property lookup is very expensive. */
6350 if (force_p
6351 || CHARPOS (pos) > it->stop_charpos
6352 || CHARPOS (pos) < original_pos)
6353 {
6354 if (it->bidi_p)
6355 {
6356 /* For bidi iteration, we need to prime prev_stop and
6357 base_level_stop with our best estimations. */
6358 /* Implementation note: Of course, POS is not necessarily a
6359 stop position, so assigning prev_pos to it is a lie; we
6360 should have called compute_stop_backwards. However, if
6361 the current buffer does not include any R2L characters,
6362 that call would be a waste of cycles, because the
6363 iterator will never move back, and thus never cross this
6364 "fake" stop position. So we delay that backward search
6365 until the time we really need it, in next_element_from_buffer. */
6366 if (CHARPOS (pos) != it->prev_stop)
6367 it->prev_stop = CHARPOS (pos);
6368 if (CHARPOS (pos) < it->base_level_stop)
6369 it->base_level_stop = 0; /* meaning it's unknown */
6370 handle_stop (it);
6371 }
6372 else
6373 {
6374 handle_stop (it);
6375 it->prev_stop = it->base_level_stop = 0;
6376 }
6377
6378 }
6379
6380 CHECK_IT (it);
6381 }
6382
6383
6384 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6385 IT->stop_pos to POS, also. */
6386
6387 static void
6388 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6389 {
6390 /* Don't call this function when scanning a C string. */
6391 eassert (it->s == NULL);
6392
6393 /* POS must be a reasonable value. */
6394 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6395
6396 it->current.pos = it->position = pos;
6397 it->end_charpos = ZV;
6398 it->dpvec = NULL;
6399 it->current.dpvec_index = -1;
6400 it->current.overlay_string_index = -1;
6401 IT_STRING_CHARPOS (*it) = -1;
6402 IT_STRING_BYTEPOS (*it) = -1;
6403 it->string = Qnil;
6404 it->method = GET_FROM_BUFFER;
6405 it->object = it->w->contents;
6406 it->area = TEXT_AREA;
6407 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6408 it->sp = 0;
6409 it->string_from_display_prop_p = 0;
6410 it->string_from_prefix_prop_p = 0;
6411
6412 it->from_disp_prop_p = 0;
6413 it->face_before_selective_p = 0;
6414 if (it->bidi_p)
6415 {
6416 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6417 &it->bidi_it);
6418 bidi_unshelve_cache (NULL, 0);
6419 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6420 it->bidi_it.string.s = NULL;
6421 it->bidi_it.string.lstring = Qnil;
6422 it->bidi_it.string.bufpos = 0;
6423 it->bidi_it.string.unibyte = 0;
6424 it->bidi_it.w = it->w;
6425 }
6426
6427 if (set_stop_p)
6428 {
6429 it->stop_charpos = CHARPOS (pos);
6430 it->base_level_stop = CHARPOS (pos);
6431 }
6432 /* This make the information stored in it->cmp_it invalidate. */
6433 it->cmp_it.id = -1;
6434 }
6435
6436
6437 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6438 If S is non-null, it is a C string to iterate over. Otherwise,
6439 STRING gives a Lisp string to iterate over.
6440
6441 If PRECISION > 0, don't return more then PRECISION number of
6442 characters from the string.
6443
6444 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6445 characters have been returned. FIELD_WIDTH < 0 means an infinite
6446 field width.
6447
6448 MULTIBYTE = 0 means disable processing of multibyte characters,
6449 MULTIBYTE > 0 means enable it,
6450 MULTIBYTE < 0 means use IT->multibyte_p.
6451
6452 IT must be initialized via a prior call to init_iterator before
6453 calling this function. */
6454
6455 static void
6456 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6457 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6458 int multibyte)
6459 {
6460 /* No region in strings. */
6461 it->region_beg_charpos = it->region_end_charpos = -1;
6462
6463 /* No text property checks performed by default, but see below. */
6464 it->stop_charpos = -1;
6465
6466 /* Set iterator position and end position. */
6467 memset (&it->current, 0, sizeof it->current);
6468 it->current.overlay_string_index = -1;
6469 it->current.dpvec_index = -1;
6470 eassert (charpos >= 0);
6471
6472 /* If STRING is specified, use its multibyteness, otherwise use the
6473 setting of MULTIBYTE, if specified. */
6474 if (multibyte >= 0)
6475 it->multibyte_p = multibyte > 0;
6476
6477 /* Bidirectional reordering of strings is controlled by the default
6478 value of bidi-display-reordering. Don't try to reorder while
6479 loading loadup.el, as the necessary character property tables are
6480 not yet available. */
6481 it->bidi_p =
6482 NILP (Vpurify_flag)
6483 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6484
6485 if (s == NULL)
6486 {
6487 eassert (STRINGP (string));
6488 it->string = string;
6489 it->s = NULL;
6490 it->end_charpos = it->string_nchars = SCHARS (string);
6491 it->method = GET_FROM_STRING;
6492 it->current.string_pos = string_pos (charpos, string);
6493
6494 if (it->bidi_p)
6495 {
6496 it->bidi_it.string.lstring = string;
6497 it->bidi_it.string.s = NULL;
6498 it->bidi_it.string.schars = it->end_charpos;
6499 it->bidi_it.string.bufpos = 0;
6500 it->bidi_it.string.from_disp_str = 0;
6501 it->bidi_it.string.unibyte = !it->multibyte_p;
6502 it->bidi_it.w = it->w;
6503 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6504 FRAME_WINDOW_P (it->f), &it->bidi_it);
6505 }
6506 }
6507 else
6508 {
6509 it->s = (const unsigned char *) s;
6510 it->string = Qnil;
6511
6512 /* Note that we use IT->current.pos, not it->current.string_pos,
6513 for displaying C strings. */
6514 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6515 if (it->multibyte_p)
6516 {
6517 it->current.pos = c_string_pos (charpos, s, 1);
6518 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6519 }
6520 else
6521 {
6522 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6523 it->end_charpos = it->string_nchars = strlen (s);
6524 }
6525
6526 if (it->bidi_p)
6527 {
6528 it->bidi_it.string.lstring = Qnil;
6529 it->bidi_it.string.s = (const unsigned char *) s;
6530 it->bidi_it.string.schars = it->end_charpos;
6531 it->bidi_it.string.bufpos = 0;
6532 it->bidi_it.string.from_disp_str = 0;
6533 it->bidi_it.string.unibyte = !it->multibyte_p;
6534 it->bidi_it.w = it->w;
6535 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6536 &it->bidi_it);
6537 }
6538 it->method = GET_FROM_C_STRING;
6539 }
6540
6541 /* PRECISION > 0 means don't return more than PRECISION characters
6542 from the string. */
6543 if (precision > 0 && it->end_charpos - charpos > precision)
6544 {
6545 it->end_charpos = it->string_nchars = charpos + precision;
6546 if (it->bidi_p)
6547 it->bidi_it.string.schars = it->end_charpos;
6548 }
6549
6550 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6551 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6552 FIELD_WIDTH < 0 means infinite field width. This is useful for
6553 padding with `-' at the end of a mode line. */
6554 if (field_width < 0)
6555 field_width = INFINITY;
6556 /* Implementation note: We deliberately don't enlarge
6557 it->bidi_it.string.schars here to fit it->end_charpos, because
6558 the bidi iterator cannot produce characters out of thin air. */
6559 if (field_width > it->end_charpos - charpos)
6560 it->end_charpos = charpos + field_width;
6561
6562 /* Use the standard display table for displaying strings. */
6563 if (DISP_TABLE_P (Vstandard_display_table))
6564 it->dp = XCHAR_TABLE (Vstandard_display_table);
6565
6566 it->stop_charpos = charpos;
6567 it->prev_stop = charpos;
6568 it->base_level_stop = 0;
6569 if (it->bidi_p)
6570 {
6571 it->bidi_it.first_elt = 1;
6572 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6573 it->bidi_it.disp_pos = -1;
6574 }
6575 if (s == NULL && it->multibyte_p)
6576 {
6577 ptrdiff_t endpos = SCHARS (it->string);
6578 if (endpos > it->end_charpos)
6579 endpos = it->end_charpos;
6580 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6581 it->string);
6582 }
6583 CHECK_IT (it);
6584 }
6585
6586
6587 \f
6588 /***********************************************************************
6589 Iteration
6590 ***********************************************************************/
6591
6592 /* Map enum it_method value to corresponding next_element_from_* function. */
6593
6594 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6595 {
6596 next_element_from_buffer,
6597 next_element_from_display_vector,
6598 next_element_from_string,
6599 next_element_from_c_string,
6600 next_element_from_image,
6601 next_element_from_stretch
6602 };
6603
6604 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6605
6606
6607 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6608 (possibly with the following characters). */
6609
6610 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6611 ((IT)->cmp_it.id >= 0 \
6612 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6613 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6614 END_CHARPOS, (IT)->w, \
6615 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6616 (IT)->string)))
6617
6618
6619 /* Lookup the char-table Vglyphless_char_display for character C (-1
6620 if we want information for no-font case), and return the display
6621 method symbol. By side-effect, update it->what and
6622 it->glyphless_method. This function is called from
6623 get_next_display_element for each character element, and from
6624 x_produce_glyphs when no suitable font was found. */
6625
6626 Lisp_Object
6627 lookup_glyphless_char_display (int c, struct it *it)
6628 {
6629 Lisp_Object glyphless_method = Qnil;
6630
6631 if (CHAR_TABLE_P (Vglyphless_char_display)
6632 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6633 {
6634 if (c >= 0)
6635 {
6636 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6637 if (CONSP (glyphless_method))
6638 glyphless_method = FRAME_WINDOW_P (it->f)
6639 ? XCAR (glyphless_method)
6640 : XCDR (glyphless_method);
6641 }
6642 else
6643 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6644 }
6645
6646 retry:
6647 if (NILP (glyphless_method))
6648 {
6649 if (c >= 0)
6650 /* The default is to display the character by a proper font. */
6651 return Qnil;
6652 /* The default for the no-font case is to display an empty box. */
6653 glyphless_method = Qempty_box;
6654 }
6655 if (EQ (glyphless_method, Qzero_width))
6656 {
6657 if (c >= 0)
6658 return glyphless_method;
6659 /* This method can't be used for the no-font case. */
6660 glyphless_method = Qempty_box;
6661 }
6662 if (EQ (glyphless_method, Qthin_space))
6663 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6664 else if (EQ (glyphless_method, Qempty_box))
6665 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6666 else if (EQ (glyphless_method, Qhex_code))
6667 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6668 else if (STRINGP (glyphless_method))
6669 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6670 else
6671 {
6672 /* Invalid value. We use the default method. */
6673 glyphless_method = Qnil;
6674 goto retry;
6675 }
6676 it->what = IT_GLYPHLESS;
6677 return glyphless_method;
6678 }
6679
6680 /* Load IT's display element fields with information about the next
6681 display element from the current position of IT. Value is zero if
6682 end of buffer (or C string) is reached. */
6683
6684 static struct frame *last_escape_glyph_frame = NULL;
6685 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6686 static int last_escape_glyph_merged_face_id = 0;
6687
6688 struct frame *last_glyphless_glyph_frame = NULL;
6689 int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6690 int last_glyphless_glyph_merged_face_id = 0;
6691
6692 static int
6693 get_next_display_element (struct it *it)
6694 {
6695 /* Non-zero means that we found a display element. Zero means that
6696 we hit the end of what we iterate over. Performance note: the
6697 function pointer `method' used here turns out to be faster than
6698 using a sequence of if-statements. */
6699 int success_p;
6700
6701 get_next:
6702 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6703
6704 if (it->what == IT_CHARACTER)
6705 {
6706 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6707 and only if (a) the resolved directionality of that character
6708 is R..." */
6709 /* FIXME: Do we need an exception for characters from display
6710 tables? */
6711 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6712 it->c = bidi_mirror_char (it->c);
6713 /* Map via display table or translate control characters.
6714 IT->c, IT->len etc. have been set to the next character by
6715 the function call above. If we have a display table, and it
6716 contains an entry for IT->c, translate it. Don't do this if
6717 IT->c itself comes from a display table, otherwise we could
6718 end up in an infinite recursion. (An alternative could be to
6719 count the recursion depth of this function and signal an
6720 error when a certain maximum depth is reached.) Is it worth
6721 it? */
6722 if (success_p && it->dpvec == NULL)
6723 {
6724 Lisp_Object dv;
6725 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6726 int nonascii_space_p = 0;
6727 int nonascii_hyphen_p = 0;
6728 int c = it->c; /* This is the character to display. */
6729
6730 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6731 {
6732 eassert (SINGLE_BYTE_CHAR_P (c));
6733 if (unibyte_display_via_language_environment)
6734 {
6735 c = DECODE_CHAR (unibyte, c);
6736 if (c < 0)
6737 c = BYTE8_TO_CHAR (it->c);
6738 }
6739 else
6740 c = BYTE8_TO_CHAR (it->c);
6741 }
6742
6743 if (it->dp
6744 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6745 VECTORP (dv)))
6746 {
6747 struct Lisp_Vector *v = XVECTOR (dv);
6748
6749 /* Return the first character from the display table
6750 entry, if not empty. If empty, don't display the
6751 current character. */
6752 if (v->header.size)
6753 {
6754 it->dpvec_char_len = it->len;
6755 it->dpvec = v->contents;
6756 it->dpend = v->contents + v->header.size;
6757 it->current.dpvec_index = 0;
6758 it->dpvec_face_id = -1;
6759 it->saved_face_id = it->face_id;
6760 it->method = GET_FROM_DISPLAY_VECTOR;
6761 it->ellipsis_p = 0;
6762 }
6763 else
6764 {
6765 set_iterator_to_next (it, 0);
6766 }
6767 goto get_next;
6768 }
6769
6770 if (! NILP (lookup_glyphless_char_display (c, it)))
6771 {
6772 if (it->what == IT_GLYPHLESS)
6773 goto done;
6774 /* Don't display this character. */
6775 set_iterator_to_next (it, 0);
6776 goto get_next;
6777 }
6778
6779 /* If `nobreak-char-display' is non-nil, we display
6780 non-ASCII spaces and hyphens specially. */
6781 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6782 {
6783 if (c == 0xA0)
6784 nonascii_space_p = 1;
6785 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6786 nonascii_hyphen_p = 1;
6787 }
6788
6789 /* Translate control characters into `\003' or `^C' form.
6790 Control characters coming from a display table entry are
6791 currently not translated because we use IT->dpvec to hold
6792 the translation. This could easily be changed but I
6793 don't believe that it is worth doing.
6794
6795 The characters handled by `nobreak-char-display' must be
6796 translated too.
6797
6798 Non-printable characters and raw-byte characters are also
6799 translated to octal form. */
6800 if (((c < ' ' || c == 127) /* ASCII control chars */
6801 ? (it->area != TEXT_AREA
6802 /* In mode line, treat \n, \t like other crl chars. */
6803 || (c != '\t'
6804 && it->glyph_row
6805 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6806 || (c != '\n' && c != '\t'))
6807 : (nonascii_space_p
6808 || nonascii_hyphen_p
6809 || CHAR_BYTE8_P (c)
6810 || ! CHAR_PRINTABLE_P (c))))
6811 {
6812 /* C is a control character, non-ASCII space/hyphen,
6813 raw-byte, or a non-printable character which must be
6814 displayed either as '\003' or as `^C' where the '\\'
6815 and '^' can be defined in the display table. Fill
6816 IT->ctl_chars with glyphs for what we have to
6817 display. Then, set IT->dpvec to these glyphs. */
6818 Lisp_Object gc;
6819 int ctl_len;
6820 int face_id;
6821 int lface_id = 0;
6822 int escape_glyph;
6823
6824 /* Handle control characters with ^. */
6825
6826 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6827 {
6828 int g;
6829
6830 g = '^'; /* default glyph for Control */
6831 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6832 if (it->dp
6833 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6834 {
6835 g = GLYPH_CODE_CHAR (gc);
6836 lface_id = GLYPH_CODE_FACE (gc);
6837 }
6838 if (lface_id)
6839 {
6840 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6841 }
6842 else if (it->f == last_escape_glyph_frame
6843 && it->face_id == last_escape_glyph_face_id)
6844 {
6845 face_id = last_escape_glyph_merged_face_id;
6846 }
6847 else
6848 {
6849 /* Merge the escape-glyph face into the current face. */
6850 face_id = merge_faces (it->f, Qescape_glyph, 0,
6851 it->face_id);
6852 last_escape_glyph_frame = it->f;
6853 last_escape_glyph_face_id = it->face_id;
6854 last_escape_glyph_merged_face_id = face_id;
6855 }
6856
6857 XSETINT (it->ctl_chars[0], g);
6858 XSETINT (it->ctl_chars[1], c ^ 0100);
6859 ctl_len = 2;
6860 goto display_control;
6861 }
6862
6863 /* Handle non-ascii space in the mode where it only gets
6864 highlighting. */
6865
6866 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6867 {
6868 /* Merge `nobreak-space' into the current face. */
6869 face_id = merge_faces (it->f, Qnobreak_space, 0,
6870 it->face_id);
6871 XSETINT (it->ctl_chars[0], ' ');
6872 ctl_len = 1;
6873 goto display_control;
6874 }
6875
6876 /* Handle sequences that start with the "escape glyph". */
6877
6878 /* the default escape glyph is \. */
6879 escape_glyph = '\\';
6880
6881 if (it->dp
6882 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6883 {
6884 escape_glyph = GLYPH_CODE_CHAR (gc);
6885 lface_id = GLYPH_CODE_FACE (gc);
6886 }
6887 if (lface_id)
6888 {
6889 /* The display table specified a face.
6890 Merge it into face_id and also into escape_glyph. */
6891 face_id = merge_faces (it->f, Qt, lface_id,
6892 it->face_id);
6893 }
6894 else if (it->f == last_escape_glyph_frame
6895 && it->face_id == last_escape_glyph_face_id)
6896 {
6897 face_id = last_escape_glyph_merged_face_id;
6898 }
6899 else
6900 {
6901 /* Merge the escape-glyph face into the current face. */
6902 face_id = merge_faces (it->f, Qescape_glyph, 0,
6903 it->face_id);
6904 last_escape_glyph_frame = it->f;
6905 last_escape_glyph_face_id = it->face_id;
6906 last_escape_glyph_merged_face_id = face_id;
6907 }
6908
6909 /* Draw non-ASCII hyphen with just highlighting: */
6910
6911 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6912 {
6913 XSETINT (it->ctl_chars[0], '-');
6914 ctl_len = 1;
6915 goto display_control;
6916 }
6917
6918 /* Draw non-ASCII space/hyphen with escape glyph: */
6919
6920 if (nonascii_space_p || nonascii_hyphen_p)
6921 {
6922 XSETINT (it->ctl_chars[0], escape_glyph);
6923 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6924 ctl_len = 2;
6925 goto display_control;
6926 }
6927
6928 {
6929 char str[10];
6930 int len, i;
6931
6932 if (CHAR_BYTE8_P (c))
6933 /* Display \200 instead of \17777600. */
6934 c = CHAR_TO_BYTE8 (c);
6935 len = sprintf (str, "%03o", c);
6936
6937 XSETINT (it->ctl_chars[0], escape_glyph);
6938 for (i = 0; i < len; i++)
6939 XSETINT (it->ctl_chars[i + 1], str[i]);
6940 ctl_len = len + 1;
6941 }
6942
6943 display_control:
6944 /* Set up IT->dpvec and return first character from it. */
6945 it->dpvec_char_len = it->len;
6946 it->dpvec = it->ctl_chars;
6947 it->dpend = it->dpvec + ctl_len;
6948 it->current.dpvec_index = 0;
6949 it->dpvec_face_id = face_id;
6950 it->saved_face_id = it->face_id;
6951 it->method = GET_FROM_DISPLAY_VECTOR;
6952 it->ellipsis_p = 0;
6953 goto get_next;
6954 }
6955 it->char_to_display = c;
6956 }
6957 else if (success_p)
6958 {
6959 it->char_to_display = it->c;
6960 }
6961 }
6962
6963 /* Adjust face id for a multibyte character. There are no multibyte
6964 character in unibyte text. */
6965 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6966 && it->multibyte_p
6967 && success_p
6968 && FRAME_WINDOW_P (it->f))
6969 {
6970 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6971
6972 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6973 {
6974 /* Automatic composition with glyph-string. */
6975 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6976
6977 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6978 }
6979 else
6980 {
6981 ptrdiff_t pos = (it->s ? -1
6982 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6983 : IT_CHARPOS (*it));
6984 int c;
6985
6986 if (it->what == IT_CHARACTER)
6987 c = it->char_to_display;
6988 else
6989 {
6990 struct composition *cmp = composition_table[it->cmp_it.id];
6991 int i;
6992
6993 c = ' ';
6994 for (i = 0; i < cmp->glyph_len; i++)
6995 /* TAB in a composition means display glyphs with
6996 padding space on the left or right. */
6997 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6998 break;
6999 }
7000 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7001 }
7002 }
7003
7004 done:
7005 /* Is this character the last one of a run of characters with
7006 box? If yes, set IT->end_of_box_run_p to 1. */
7007 if (it->face_box_p
7008 && it->s == NULL)
7009 {
7010 if (it->method == GET_FROM_STRING && it->sp)
7011 {
7012 int face_id = underlying_face_id (it);
7013 struct face *face = FACE_FROM_ID (it->f, face_id);
7014
7015 if (face)
7016 {
7017 if (face->box == FACE_NO_BOX)
7018 {
7019 /* If the box comes from face properties in a
7020 display string, check faces in that string. */
7021 int string_face_id = face_after_it_pos (it);
7022 it->end_of_box_run_p
7023 = (FACE_FROM_ID (it->f, string_face_id)->box
7024 == FACE_NO_BOX);
7025 }
7026 /* Otherwise, the box comes from the underlying face.
7027 If this is the last string character displayed, check
7028 the next buffer location. */
7029 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7030 && (it->current.overlay_string_index
7031 == it->n_overlay_strings - 1))
7032 {
7033 ptrdiff_t ignore;
7034 int next_face_id;
7035 struct text_pos pos = it->current.pos;
7036 INC_TEXT_POS (pos, it->multibyte_p);
7037
7038 next_face_id = face_at_buffer_position
7039 (it->w, CHARPOS (pos), it->region_beg_charpos,
7040 it->region_end_charpos, &ignore,
7041 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
7042 -1);
7043 it->end_of_box_run_p
7044 = (FACE_FROM_ID (it->f, next_face_id)->box
7045 == FACE_NO_BOX);
7046 }
7047 }
7048 }
7049 else
7050 {
7051 int face_id = face_after_it_pos (it);
7052 it->end_of_box_run_p
7053 = (face_id != it->face_id
7054 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7055 }
7056 }
7057 /* If we reached the end of the object we've been iterating (e.g., a
7058 display string or an overlay string), and there's something on
7059 IT->stack, proceed with what's on the stack. It doesn't make
7060 sense to return zero if there's unprocessed stuff on the stack,
7061 because otherwise that stuff will never be displayed. */
7062 if (!success_p && it->sp > 0)
7063 {
7064 set_iterator_to_next (it, 0);
7065 success_p = get_next_display_element (it);
7066 }
7067
7068 /* Value is 0 if end of buffer or string reached. */
7069 return success_p;
7070 }
7071
7072
7073 /* Move IT to the next display element.
7074
7075 RESEAT_P non-zero means if called on a newline in buffer text,
7076 skip to the next visible line start.
7077
7078 Functions get_next_display_element and set_iterator_to_next are
7079 separate because I find this arrangement easier to handle than a
7080 get_next_display_element function that also increments IT's
7081 position. The way it is we can first look at an iterator's current
7082 display element, decide whether it fits on a line, and if it does,
7083 increment the iterator position. The other way around we probably
7084 would either need a flag indicating whether the iterator has to be
7085 incremented the next time, or we would have to implement a
7086 decrement position function which would not be easy to write. */
7087
7088 void
7089 set_iterator_to_next (struct it *it, int reseat_p)
7090 {
7091 /* Reset flags indicating start and end of a sequence of characters
7092 with box. Reset them at the start of this function because
7093 moving the iterator to a new position might set them. */
7094 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7095
7096 switch (it->method)
7097 {
7098 case GET_FROM_BUFFER:
7099 /* The current display element of IT is a character from
7100 current_buffer. Advance in the buffer, and maybe skip over
7101 invisible lines that are so because of selective display. */
7102 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7103 reseat_at_next_visible_line_start (it, 0);
7104 else if (it->cmp_it.id >= 0)
7105 {
7106 /* We are currently getting glyphs from a composition. */
7107 int i;
7108
7109 if (! it->bidi_p)
7110 {
7111 IT_CHARPOS (*it) += it->cmp_it.nchars;
7112 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7113 if (it->cmp_it.to < it->cmp_it.nglyphs)
7114 {
7115 it->cmp_it.from = it->cmp_it.to;
7116 }
7117 else
7118 {
7119 it->cmp_it.id = -1;
7120 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7121 IT_BYTEPOS (*it),
7122 it->end_charpos, Qnil);
7123 }
7124 }
7125 else if (! it->cmp_it.reversed_p)
7126 {
7127 /* Composition created while scanning forward. */
7128 /* Update IT's char/byte positions to point to the first
7129 character of the next grapheme cluster, or to the
7130 character visually after the current composition. */
7131 for (i = 0; i < it->cmp_it.nchars; i++)
7132 bidi_move_to_visually_next (&it->bidi_it);
7133 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7134 IT_CHARPOS (*it) = it->bidi_it.charpos;
7135
7136 if (it->cmp_it.to < it->cmp_it.nglyphs)
7137 {
7138 /* Proceed to the next grapheme cluster. */
7139 it->cmp_it.from = it->cmp_it.to;
7140 }
7141 else
7142 {
7143 /* No more grapheme clusters in this composition.
7144 Find the next stop position. */
7145 ptrdiff_t stop = it->end_charpos;
7146 if (it->bidi_it.scan_dir < 0)
7147 /* Now we are scanning backward and don't know
7148 where to stop. */
7149 stop = -1;
7150 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7151 IT_BYTEPOS (*it), stop, Qnil);
7152 }
7153 }
7154 else
7155 {
7156 /* Composition created while scanning backward. */
7157 /* Update IT's char/byte positions to point to the last
7158 character of the previous grapheme cluster, or the
7159 character visually after the current composition. */
7160 for (i = 0; i < it->cmp_it.nchars; i++)
7161 bidi_move_to_visually_next (&it->bidi_it);
7162 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7163 IT_CHARPOS (*it) = it->bidi_it.charpos;
7164 if (it->cmp_it.from > 0)
7165 {
7166 /* Proceed to the previous grapheme cluster. */
7167 it->cmp_it.to = it->cmp_it.from;
7168 }
7169 else
7170 {
7171 /* No more grapheme clusters in this composition.
7172 Find the next stop position. */
7173 ptrdiff_t stop = it->end_charpos;
7174 if (it->bidi_it.scan_dir < 0)
7175 /* Now we are scanning backward and don't know
7176 where to stop. */
7177 stop = -1;
7178 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7179 IT_BYTEPOS (*it), stop, Qnil);
7180 }
7181 }
7182 }
7183 else
7184 {
7185 eassert (it->len != 0);
7186
7187 if (!it->bidi_p)
7188 {
7189 IT_BYTEPOS (*it) += it->len;
7190 IT_CHARPOS (*it) += 1;
7191 }
7192 else
7193 {
7194 int prev_scan_dir = it->bidi_it.scan_dir;
7195 /* If this is a new paragraph, determine its base
7196 direction (a.k.a. its base embedding level). */
7197 if (it->bidi_it.new_paragraph)
7198 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7199 bidi_move_to_visually_next (&it->bidi_it);
7200 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7201 IT_CHARPOS (*it) = it->bidi_it.charpos;
7202 if (prev_scan_dir != it->bidi_it.scan_dir)
7203 {
7204 /* As the scan direction was changed, we must
7205 re-compute the stop position for composition. */
7206 ptrdiff_t stop = it->end_charpos;
7207 if (it->bidi_it.scan_dir < 0)
7208 stop = -1;
7209 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7210 IT_BYTEPOS (*it), stop, Qnil);
7211 }
7212 }
7213 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7214 }
7215 break;
7216
7217 case GET_FROM_C_STRING:
7218 /* Current display element of IT is from a C string. */
7219 if (!it->bidi_p
7220 /* If the string position is beyond string's end, it means
7221 next_element_from_c_string is padding the string with
7222 blanks, in which case we bypass the bidi iterator,
7223 because it cannot deal with such virtual characters. */
7224 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7225 {
7226 IT_BYTEPOS (*it) += it->len;
7227 IT_CHARPOS (*it) += 1;
7228 }
7229 else
7230 {
7231 bidi_move_to_visually_next (&it->bidi_it);
7232 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7233 IT_CHARPOS (*it) = it->bidi_it.charpos;
7234 }
7235 break;
7236
7237 case GET_FROM_DISPLAY_VECTOR:
7238 /* Current display element of IT is from a display table entry.
7239 Advance in the display table definition. Reset it to null if
7240 end reached, and continue with characters from buffers/
7241 strings. */
7242 ++it->current.dpvec_index;
7243
7244 /* Restore face of the iterator to what they were before the
7245 display vector entry (these entries may contain faces). */
7246 it->face_id = it->saved_face_id;
7247
7248 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7249 {
7250 int recheck_faces = it->ellipsis_p;
7251
7252 if (it->s)
7253 it->method = GET_FROM_C_STRING;
7254 else if (STRINGP (it->string))
7255 it->method = GET_FROM_STRING;
7256 else
7257 {
7258 it->method = GET_FROM_BUFFER;
7259 it->object = it->w->contents;
7260 }
7261
7262 it->dpvec = NULL;
7263 it->current.dpvec_index = -1;
7264
7265 /* Skip over characters which were displayed via IT->dpvec. */
7266 if (it->dpvec_char_len < 0)
7267 reseat_at_next_visible_line_start (it, 1);
7268 else if (it->dpvec_char_len > 0)
7269 {
7270 if (it->method == GET_FROM_STRING
7271 && it->current.overlay_string_index >= 0
7272 && it->n_overlay_strings > 0)
7273 it->ignore_overlay_strings_at_pos_p = 1;
7274 it->len = it->dpvec_char_len;
7275 set_iterator_to_next (it, reseat_p);
7276 }
7277
7278 /* Maybe recheck faces after display vector */
7279 if (recheck_faces)
7280 it->stop_charpos = IT_CHARPOS (*it);
7281 }
7282 break;
7283
7284 case GET_FROM_STRING:
7285 /* Current display element is a character from a Lisp string. */
7286 eassert (it->s == NULL && STRINGP (it->string));
7287 /* Don't advance past string end. These conditions are true
7288 when set_iterator_to_next is called at the end of
7289 get_next_display_element, in which case the Lisp string is
7290 already exhausted, and all we want is pop the iterator
7291 stack. */
7292 if (it->current.overlay_string_index >= 0)
7293 {
7294 /* This is an overlay string, so there's no padding with
7295 spaces, and the number of characters in the string is
7296 where the string ends. */
7297 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7298 goto consider_string_end;
7299 }
7300 else
7301 {
7302 /* Not an overlay string. There could be padding, so test
7303 against it->end_charpos . */
7304 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7305 goto consider_string_end;
7306 }
7307 if (it->cmp_it.id >= 0)
7308 {
7309 int i;
7310
7311 if (! it->bidi_p)
7312 {
7313 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7314 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7315 if (it->cmp_it.to < it->cmp_it.nglyphs)
7316 it->cmp_it.from = it->cmp_it.to;
7317 else
7318 {
7319 it->cmp_it.id = -1;
7320 composition_compute_stop_pos (&it->cmp_it,
7321 IT_STRING_CHARPOS (*it),
7322 IT_STRING_BYTEPOS (*it),
7323 it->end_charpos, it->string);
7324 }
7325 }
7326 else if (! it->cmp_it.reversed_p)
7327 {
7328 for (i = 0; i < it->cmp_it.nchars; i++)
7329 bidi_move_to_visually_next (&it->bidi_it);
7330 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7331 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7332
7333 if (it->cmp_it.to < it->cmp_it.nglyphs)
7334 it->cmp_it.from = it->cmp_it.to;
7335 else
7336 {
7337 ptrdiff_t stop = it->end_charpos;
7338 if (it->bidi_it.scan_dir < 0)
7339 stop = -1;
7340 composition_compute_stop_pos (&it->cmp_it,
7341 IT_STRING_CHARPOS (*it),
7342 IT_STRING_BYTEPOS (*it), stop,
7343 it->string);
7344 }
7345 }
7346 else
7347 {
7348 for (i = 0; i < it->cmp_it.nchars; i++)
7349 bidi_move_to_visually_next (&it->bidi_it);
7350 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7351 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7352 if (it->cmp_it.from > 0)
7353 it->cmp_it.to = it->cmp_it.from;
7354 else
7355 {
7356 ptrdiff_t stop = it->end_charpos;
7357 if (it->bidi_it.scan_dir < 0)
7358 stop = -1;
7359 composition_compute_stop_pos (&it->cmp_it,
7360 IT_STRING_CHARPOS (*it),
7361 IT_STRING_BYTEPOS (*it), stop,
7362 it->string);
7363 }
7364 }
7365 }
7366 else
7367 {
7368 if (!it->bidi_p
7369 /* If the string position is beyond string's end, it
7370 means next_element_from_string is padding the string
7371 with blanks, in which case we bypass the bidi
7372 iterator, because it cannot deal with such virtual
7373 characters. */
7374 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7375 {
7376 IT_STRING_BYTEPOS (*it) += it->len;
7377 IT_STRING_CHARPOS (*it) += 1;
7378 }
7379 else
7380 {
7381 int prev_scan_dir = it->bidi_it.scan_dir;
7382
7383 bidi_move_to_visually_next (&it->bidi_it);
7384 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7385 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7386 if (prev_scan_dir != it->bidi_it.scan_dir)
7387 {
7388 ptrdiff_t stop = it->end_charpos;
7389
7390 if (it->bidi_it.scan_dir < 0)
7391 stop = -1;
7392 composition_compute_stop_pos (&it->cmp_it,
7393 IT_STRING_CHARPOS (*it),
7394 IT_STRING_BYTEPOS (*it), stop,
7395 it->string);
7396 }
7397 }
7398 }
7399
7400 consider_string_end:
7401
7402 if (it->current.overlay_string_index >= 0)
7403 {
7404 /* IT->string is an overlay string. Advance to the
7405 next, if there is one. */
7406 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7407 {
7408 it->ellipsis_p = 0;
7409 next_overlay_string (it);
7410 if (it->ellipsis_p)
7411 setup_for_ellipsis (it, 0);
7412 }
7413 }
7414 else
7415 {
7416 /* IT->string is not an overlay string. If we reached
7417 its end, and there is something on IT->stack, proceed
7418 with what is on the stack. This can be either another
7419 string, this time an overlay string, or a buffer. */
7420 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7421 && it->sp > 0)
7422 {
7423 pop_it (it);
7424 if (it->method == GET_FROM_STRING)
7425 goto consider_string_end;
7426 }
7427 }
7428 break;
7429
7430 case GET_FROM_IMAGE:
7431 case GET_FROM_STRETCH:
7432 /* The position etc with which we have to proceed are on
7433 the stack. The position may be at the end of a string,
7434 if the `display' property takes up the whole string. */
7435 eassert (it->sp > 0);
7436 pop_it (it);
7437 if (it->method == GET_FROM_STRING)
7438 goto consider_string_end;
7439 break;
7440
7441 default:
7442 /* There are no other methods defined, so this should be a bug. */
7443 emacs_abort ();
7444 }
7445
7446 eassert (it->method != GET_FROM_STRING
7447 || (STRINGP (it->string)
7448 && IT_STRING_CHARPOS (*it) >= 0));
7449 }
7450
7451 /* Load IT's display element fields with information about the next
7452 display element which comes from a display table entry or from the
7453 result of translating a control character to one of the forms `^C'
7454 or `\003'.
7455
7456 IT->dpvec holds the glyphs to return as characters.
7457 IT->saved_face_id holds the face id before the display vector--it
7458 is restored into IT->face_id in set_iterator_to_next. */
7459
7460 static int
7461 next_element_from_display_vector (struct it *it)
7462 {
7463 Lisp_Object gc;
7464 int prev_face_id = it->face_id;
7465 int next_face_id;
7466
7467 /* Precondition. */
7468 eassert (it->dpvec && it->current.dpvec_index >= 0);
7469
7470 it->face_id = it->saved_face_id;
7471
7472 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7473 That seemed totally bogus - so I changed it... */
7474 gc = it->dpvec[it->current.dpvec_index];
7475
7476 if (GLYPH_CODE_P (gc))
7477 {
7478 struct face *this_face, *prev_face, *next_face;
7479
7480 it->c = GLYPH_CODE_CHAR (gc);
7481 it->len = CHAR_BYTES (it->c);
7482
7483 /* The entry may contain a face id to use. Such a face id is
7484 the id of a Lisp face, not a realized face. A face id of
7485 zero means no face is specified. */
7486 if (it->dpvec_face_id >= 0)
7487 it->face_id = it->dpvec_face_id;
7488 else
7489 {
7490 int lface_id = GLYPH_CODE_FACE (gc);
7491 if (lface_id > 0)
7492 it->face_id = merge_faces (it->f, Qt, lface_id,
7493 it->saved_face_id);
7494 }
7495
7496 /* Glyphs in the display vector could have the box face, so we
7497 need to set the related flags in the iterator, as
7498 appropriate. */
7499 this_face = FACE_FROM_ID (it->f, it->face_id);
7500 prev_face = FACE_FROM_ID (it->f, prev_face_id);
7501
7502 /* Is this character the first character of a box-face run? */
7503 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7504 && (!prev_face
7505 || prev_face->box == FACE_NO_BOX));
7506
7507 /* For the last character of the box-face run, we need to look
7508 either at the next glyph from the display vector, or at the
7509 face we saw before the display vector. */
7510 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7511 {
7512 if (it->dpvec_face_id >= 0)
7513 next_face_id = it->dpvec_face_id;
7514 else
7515 {
7516 int lface_id =
7517 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7518
7519 if (lface_id > 0)
7520 next_face_id = merge_faces (it->f, Qt, lface_id,
7521 it->saved_face_id);
7522 }
7523 }
7524 else
7525 next_face_id = it->saved_face_id;
7526 next_face = FACE_FROM_ID (it->f, next_face_id);
7527 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7528 && (!next_face
7529 || next_face->box == FACE_NO_BOX));
7530 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7531 }
7532 else
7533 /* Display table entry is invalid. Return a space. */
7534 it->c = ' ', it->len = 1;
7535
7536 /* Don't change position and object of the iterator here. They are
7537 still the values of the character that had this display table
7538 entry or was translated, and that's what we want. */
7539 it->what = IT_CHARACTER;
7540 return 1;
7541 }
7542
7543 /* Get the first element of string/buffer in the visual order, after
7544 being reseated to a new position in a string or a buffer. */
7545 static void
7546 get_visually_first_element (struct it *it)
7547 {
7548 int string_p = STRINGP (it->string) || it->s;
7549 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7550 ptrdiff_t bob = (string_p ? 0 : BEGV);
7551
7552 if (STRINGP (it->string))
7553 {
7554 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7555 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7556 }
7557 else
7558 {
7559 it->bidi_it.charpos = IT_CHARPOS (*it);
7560 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7561 }
7562
7563 if (it->bidi_it.charpos == eob)
7564 {
7565 /* Nothing to do, but reset the FIRST_ELT flag, like
7566 bidi_paragraph_init does, because we are not going to
7567 call it. */
7568 it->bidi_it.first_elt = 0;
7569 }
7570 else if (it->bidi_it.charpos == bob
7571 || (!string_p
7572 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7573 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7574 {
7575 /* If we are at the beginning of a line/string, we can produce
7576 the next element right away. */
7577 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7578 bidi_move_to_visually_next (&it->bidi_it);
7579 }
7580 else
7581 {
7582 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7583
7584 /* We need to prime the bidi iterator starting at the line's or
7585 string's beginning, before we will be able to produce the
7586 next element. */
7587 if (string_p)
7588 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7589 else
7590 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7591 IT_BYTEPOS (*it), -1,
7592 &it->bidi_it.bytepos);
7593 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7594 do
7595 {
7596 /* Now return to buffer/string position where we were asked
7597 to get the next display element, and produce that. */
7598 bidi_move_to_visually_next (&it->bidi_it);
7599 }
7600 while (it->bidi_it.bytepos != orig_bytepos
7601 && it->bidi_it.charpos < eob);
7602 }
7603
7604 /* Adjust IT's position information to where we ended up. */
7605 if (STRINGP (it->string))
7606 {
7607 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7608 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7609 }
7610 else
7611 {
7612 IT_CHARPOS (*it) = it->bidi_it.charpos;
7613 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7614 }
7615
7616 if (STRINGP (it->string) || !it->s)
7617 {
7618 ptrdiff_t stop, charpos, bytepos;
7619
7620 if (STRINGP (it->string))
7621 {
7622 eassert (!it->s);
7623 stop = SCHARS (it->string);
7624 if (stop > it->end_charpos)
7625 stop = it->end_charpos;
7626 charpos = IT_STRING_CHARPOS (*it);
7627 bytepos = IT_STRING_BYTEPOS (*it);
7628 }
7629 else
7630 {
7631 stop = it->end_charpos;
7632 charpos = IT_CHARPOS (*it);
7633 bytepos = IT_BYTEPOS (*it);
7634 }
7635 if (it->bidi_it.scan_dir < 0)
7636 stop = -1;
7637 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7638 it->string);
7639 }
7640 }
7641
7642 /* Load IT with the next display element from Lisp string IT->string.
7643 IT->current.string_pos is the current position within the string.
7644 If IT->current.overlay_string_index >= 0, the Lisp string is an
7645 overlay string. */
7646
7647 static int
7648 next_element_from_string (struct it *it)
7649 {
7650 struct text_pos position;
7651
7652 eassert (STRINGP (it->string));
7653 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7654 eassert (IT_STRING_CHARPOS (*it) >= 0);
7655 position = it->current.string_pos;
7656
7657 /* With bidi reordering, the character to display might not be the
7658 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7659 that we were reseat()ed to a new string, whose paragraph
7660 direction is not known. */
7661 if (it->bidi_p && it->bidi_it.first_elt)
7662 {
7663 get_visually_first_element (it);
7664 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7665 }
7666
7667 /* Time to check for invisible text? */
7668 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7669 {
7670 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7671 {
7672 if (!(!it->bidi_p
7673 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7674 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7675 {
7676 /* With bidi non-linear iteration, we could find
7677 ourselves far beyond the last computed stop_charpos,
7678 with several other stop positions in between that we
7679 missed. Scan them all now, in buffer's logical
7680 order, until we find and handle the last stop_charpos
7681 that precedes our current position. */
7682 handle_stop_backwards (it, it->stop_charpos);
7683 return GET_NEXT_DISPLAY_ELEMENT (it);
7684 }
7685 else
7686 {
7687 if (it->bidi_p)
7688 {
7689 /* Take note of the stop position we just moved
7690 across, for when we will move back across it. */
7691 it->prev_stop = it->stop_charpos;
7692 /* If we are at base paragraph embedding level, take
7693 note of the last stop position seen at this
7694 level. */
7695 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7696 it->base_level_stop = it->stop_charpos;
7697 }
7698 handle_stop (it);
7699
7700 /* Since a handler may have changed IT->method, we must
7701 recurse here. */
7702 return GET_NEXT_DISPLAY_ELEMENT (it);
7703 }
7704 }
7705 else if (it->bidi_p
7706 /* If we are before prev_stop, we may have overstepped
7707 on our way backwards a stop_pos, and if so, we need
7708 to handle that stop_pos. */
7709 && IT_STRING_CHARPOS (*it) < it->prev_stop
7710 /* We can sometimes back up for reasons that have nothing
7711 to do with bidi reordering. E.g., compositions. The
7712 code below is only needed when we are above the base
7713 embedding level, so test for that explicitly. */
7714 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7715 {
7716 /* If we lost track of base_level_stop, we have no better
7717 place for handle_stop_backwards to start from than string
7718 beginning. This happens, e.g., when we were reseated to
7719 the previous screenful of text by vertical-motion. */
7720 if (it->base_level_stop <= 0
7721 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7722 it->base_level_stop = 0;
7723 handle_stop_backwards (it, it->base_level_stop);
7724 return GET_NEXT_DISPLAY_ELEMENT (it);
7725 }
7726 }
7727
7728 if (it->current.overlay_string_index >= 0)
7729 {
7730 /* Get the next character from an overlay string. In overlay
7731 strings, there is no field width or padding with spaces to
7732 do. */
7733 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7734 {
7735 it->what = IT_EOB;
7736 return 0;
7737 }
7738 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7739 IT_STRING_BYTEPOS (*it),
7740 it->bidi_it.scan_dir < 0
7741 ? -1
7742 : SCHARS (it->string))
7743 && next_element_from_composition (it))
7744 {
7745 return 1;
7746 }
7747 else if (STRING_MULTIBYTE (it->string))
7748 {
7749 const unsigned char *s = (SDATA (it->string)
7750 + IT_STRING_BYTEPOS (*it));
7751 it->c = string_char_and_length (s, &it->len);
7752 }
7753 else
7754 {
7755 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7756 it->len = 1;
7757 }
7758 }
7759 else
7760 {
7761 /* Get the next character from a Lisp string that is not an
7762 overlay string. Such strings come from the mode line, for
7763 example. We may have to pad with spaces, or truncate the
7764 string. See also next_element_from_c_string. */
7765 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7766 {
7767 it->what = IT_EOB;
7768 return 0;
7769 }
7770 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7771 {
7772 /* Pad with spaces. */
7773 it->c = ' ', it->len = 1;
7774 CHARPOS (position) = BYTEPOS (position) = -1;
7775 }
7776 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7777 IT_STRING_BYTEPOS (*it),
7778 it->bidi_it.scan_dir < 0
7779 ? -1
7780 : it->string_nchars)
7781 && next_element_from_composition (it))
7782 {
7783 return 1;
7784 }
7785 else if (STRING_MULTIBYTE (it->string))
7786 {
7787 const unsigned char *s = (SDATA (it->string)
7788 + IT_STRING_BYTEPOS (*it));
7789 it->c = string_char_and_length (s, &it->len);
7790 }
7791 else
7792 {
7793 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7794 it->len = 1;
7795 }
7796 }
7797
7798 /* Record what we have and where it came from. */
7799 it->what = IT_CHARACTER;
7800 it->object = it->string;
7801 it->position = position;
7802 return 1;
7803 }
7804
7805
7806 /* Load IT with next display element from C string IT->s.
7807 IT->string_nchars is the maximum number of characters to return
7808 from the string. IT->end_charpos may be greater than
7809 IT->string_nchars when this function is called, in which case we
7810 may have to return padding spaces. Value is zero if end of string
7811 reached, including padding spaces. */
7812
7813 static int
7814 next_element_from_c_string (struct it *it)
7815 {
7816 int success_p = 1;
7817
7818 eassert (it->s);
7819 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7820 it->what = IT_CHARACTER;
7821 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7822 it->object = Qnil;
7823
7824 /* With bidi reordering, the character to display might not be the
7825 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7826 we were reseated to a new string, whose paragraph direction is
7827 not known. */
7828 if (it->bidi_p && it->bidi_it.first_elt)
7829 get_visually_first_element (it);
7830
7831 /* IT's position can be greater than IT->string_nchars in case a
7832 field width or precision has been specified when the iterator was
7833 initialized. */
7834 if (IT_CHARPOS (*it) >= it->end_charpos)
7835 {
7836 /* End of the game. */
7837 it->what = IT_EOB;
7838 success_p = 0;
7839 }
7840 else if (IT_CHARPOS (*it) >= it->string_nchars)
7841 {
7842 /* Pad with spaces. */
7843 it->c = ' ', it->len = 1;
7844 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7845 }
7846 else if (it->multibyte_p)
7847 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7848 else
7849 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7850
7851 return success_p;
7852 }
7853
7854
7855 /* Set up IT to return characters from an ellipsis, if appropriate.
7856 The definition of the ellipsis glyphs may come from a display table
7857 entry. This function fills IT with the first glyph from the
7858 ellipsis if an ellipsis is to be displayed. */
7859
7860 static int
7861 next_element_from_ellipsis (struct it *it)
7862 {
7863 if (it->selective_display_ellipsis_p)
7864 setup_for_ellipsis (it, it->len);
7865 else
7866 {
7867 /* The face at the current position may be different from the
7868 face we find after the invisible text. Remember what it
7869 was in IT->saved_face_id, and signal that it's there by
7870 setting face_before_selective_p. */
7871 it->saved_face_id = it->face_id;
7872 it->method = GET_FROM_BUFFER;
7873 it->object = it->w->contents;
7874 reseat_at_next_visible_line_start (it, 1);
7875 it->face_before_selective_p = 1;
7876 }
7877
7878 return GET_NEXT_DISPLAY_ELEMENT (it);
7879 }
7880
7881
7882 /* Deliver an image display element. The iterator IT is already
7883 filled with image information (done in handle_display_prop). Value
7884 is always 1. */
7885
7886
7887 static int
7888 next_element_from_image (struct it *it)
7889 {
7890 it->what = IT_IMAGE;
7891 it->ignore_overlay_strings_at_pos_p = 0;
7892 return 1;
7893 }
7894
7895
7896 /* Fill iterator IT with next display element from a stretch glyph
7897 property. IT->object is the value of the text property. Value is
7898 always 1. */
7899
7900 static int
7901 next_element_from_stretch (struct it *it)
7902 {
7903 it->what = IT_STRETCH;
7904 return 1;
7905 }
7906
7907 /* Scan backwards from IT's current position until we find a stop
7908 position, or until BEGV. This is called when we find ourself
7909 before both the last known prev_stop and base_level_stop while
7910 reordering bidirectional text. */
7911
7912 static void
7913 compute_stop_pos_backwards (struct it *it)
7914 {
7915 const int SCAN_BACK_LIMIT = 1000;
7916 struct text_pos pos;
7917 struct display_pos save_current = it->current;
7918 struct text_pos save_position = it->position;
7919 ptrdiff_t charpos = IT_CHARPOS (*it);
7920 ptrdiff_t where_we_are = charpos;
7921 ptrdiff_t save_stop_pos = it->stop_charpos;
7922 ptrdiff_t save_end_pos = it->end_charpos;
7923
7924 eassert (NILP (it->string) && !it->s);
7925 eassert (it->bidi_p);
7926 it->bidi_p = 0;
7927 do
7928 {
7929 it->end_charpos = min (charpos + 1, ZV);
7930 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7931 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7932 reseat_1 (it, pos, 0);
7933 compute_stop_pos (it);
7934 /* We must advance forward, right? */
7935 if (it->stop_charpos <= charpos)
7936 emacs_abort ();
7937 }
7938 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7939
7940 if (it->stop_charpos <= where_we_are)
7941 it->prev_stop = it->stop_charpos;
7942 else
7943 it->prev_stop = BEGV;
7944 it->bidi_p = 1;
7945 it->current = save_current;
7946 it->position = save_position;
7947 it->stop_charpos = save_stop_pos;
7948 it->end_charpos = save_end_pos;
7949 }
7950
7951 /* Scan forward from CHARPOS in the current buffer/string, until we
7952 find a stop position > current IT's position. Then handle the stop
7953 position before that. This is called when we bump into a stop
7954 position while reordering bidirectional text. CHARPOS should be
7955 the last previously processed stop_pos (or BEGV/0, if none were
7956 processed yet) whose position is less that IT's current
7957 position. */
7958
7959 static void
7960 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7961 {
7962 int bufp = !STRINGP (it->string);
7963 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7964 struct display_pos save_current = it->current;
7965 struct text_pos save_position = it->position;
7966 struct text_pos pos1;
7967 ptrdiff_t next_stop;
7968
7969 /* Scan in strict logical order. */
7970 eassert (it->bidi_p);
7971 it->bidi_p = 0;
7972 do
7973 {
7974 it->prev_stop = charpos;
7975 if (bufp)
7976 {
7977 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7978 reseat_1 (it, pos1, 0);
7979 }
7980 else
7981 it->current.string_pos = string_pos (charpos, it->string);
7982 compute_stop_pos (it);
7983 /* We must advance forward, right? */
7984 if (it->stop_charpos <= it->prev_stop)
7985 emacs_abort ();
7986 charpos = it->stop_charpos;
7987 }
7988 while (charpos <= where_we_are);
7989
7990 it->bidi_p = 1;
7991 it->current = save_current;
7992 it->position = save_position;
7993 next_stop = it->stop_charpos;
7994 it->stop_charpos = it->prev_stop;
7995 handle_stop (it);
7996 it->stop_charpos = next_stop;
7997 }
7998
7999 /* Load IT with the next display element from current_buffer. Value
8000 is zero if end of buffer reached. IT->stop_charpos is the next
8001 position at which to stop and check for text properties or buffer
8002 end. */
8003
8004 static int
8005 next_element_from_buffer (struct it *it)
8006 {
8007 int success_p = 1;
8008
8009 eassert (IT_CHARPOS (*it) >= BEGV);
8010 eassert (NILP (it->string) && !it->s);
8011 eassert (!it->bidi_p
8012 || (EQ (it->bidi_it.string.lstring, Qnil)
8013 && it->bidi_it.string.s == NULL));
8014
8015 /* With bidi reordering, the character to display might not be the
8016 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
8017 we were reseat()ed to a new buffer position, which is potentially
8018 a different paragraph. */
8019 if (it->bidi_p && it->bidi_it.first_elt)
8020 {
8021 get_visually_first_element (it);
8022 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8023 }
8024
8025 if (IT_CHARPOS (*it) >= it->stop_charpos)
8026 {
8027 if (IT_CHARPOS (*it) >= it->end_charpos)
8028 {
8029 int overlay_strings_follow_p;
8030
8031 /* End of the game, except when overlay strings follow that
8032 haven't been returned yet. */
8033 if (it->overlay_strings_at_end_processed_p)
8034 overlay_strings_follow_p = 0;
8035 else
8036 {
8037 it->overlay_strings_at_end_processed_p = 1;
8038 overlay_strings_follow_p = get_overlay_strings (it, 0);
8039 }
8040
8041 if (overlay_strings_follow_p)
8042 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8043 else
8044 {
8045 it->what = IT_EOB;
8046 it->position = it->current.pos;
8047 success_p = 0;
8048 }
8049 }
8050 else if (!(!it->bidi_p
8051 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8052 || IT_CHARPOS (*it) == it->stop_charpos))
8053 {
8054 /* With bidi non-linear iteration, we could find ourselves
8055 far beyond the last computed stop_charpos, with several
8056 other stop positions in between that we missed. Scan
8057 them all now, in buffer's logical order, until we find
8058 and handle the last stop_charpos that precedes our
8059 current position. */
8060 handle_stop_backwards (it, it->stop_charpos);
8061 return GET_NEXT_DISPLAY_ELEMENT (it);
8062 }
8063 else
8064 {
8065 if (it->bidi_p)
8066 {
8067 /* Take note of the stop position we just moved across,
8068 for when we will move back across it. */
8069 it->prev_stop = it->stop_charpos;
8070 /* If we are at base paragraph embedding level, take
8071 note of the last stop position seen at this
8072 level. */
8073 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8074 it->base_level_stop = it->stop_charpos;
8075 }
8076 handle_stop (it);
8077 return GET_NEXT_DISPLAY_ELEMENT (it);
8078 }
8079 }
8080 else if (it->bidi_p
8081 /* If we are before prev_stop, we may have overstepped on
8082 our way backwards a stop_pos, and if so, we need to
8083 handle that stop_pos. */
8084 && IT_CHARPOS (*it) < it->prev_stop
8085 /* We can sometimes back up for reasons that have nothing
8086 to do with bidi reordering. E.g., compositions. The
8087 code below is only needed when we are above the base
8088 embedding level, so test for that explicitly. */
8089 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8090 {
8091 if (it->base_level_stop <= 0
8092 || IT_CHARPOS (*it) < it->base_level_stop)
8093 {
8094 /* If we lost track of base_level_stop, we need to find
8095 prev_stop by looking backwards. This happens, e.g., when
8096 we were reseated to the previous screenful of text by
8097 vertical-motion. */
8098 it->base_level_stop = BEGV;
8099 compute_stop_pos_backwards (it);
8100 handle_stop_backwards (it, it->prev_stop);
8101 }
8102 else
8103 handle_stop_backwards (it, it->base_level_stop);
8104 return GET_NEXT_DISPLAY_ELEMENT (it);
8105 }
8106 else
8107 {
8108 /* No face changes, overlays etc. in sight, so just return a
8109 character from current_buffer. */
8110 unsigned char *p;
8111 ptrdiff_t stop;
8112
8113 /* Maybe run the redisplay end trigger hook. Performance note:
8114 This doesn't seem to cost measurable time. */
8115 if (it->redisplay_end_trigger_charpos
8116 && it->glyph_row
8117 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8118 run_redisplay_end_trigger_hook (it);
8119
8120 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8121 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8122 stop)
8123 && next_element_from_composition (it))
8124 {
8125 return 1;
8126 }
8127
8128 /* Get the next character, maybe multibyte. */
8129 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8130 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8131 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8132 else
8133 it->c = *p, it->len = 1;
8134
8135 /* Record what we have and where it came from. */
8136 it->what = IT_CHARACTER;
8137 it->object = it->w->contents;
8138 it->position = it->current.pos;
8139
8140 /* Normally we return the character found above, except when we
8141 really want to return an ellipsis for selective display. */
8142 if (it->selective)
8143 {
8144 if (it->c == '\n')
8145 {
8146 /* A value of selective > 0 means hide lines indented more
8147 than that number of columns. */
8148 if (it->selective > 0
8149 && IT_CHARPOS (*it) + 1 < ZV
8150 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8151 IT_BYTEPOS (*it) + 1,
8152 it->selective))
8153 {
8154 success_p = next_element_from_ellipsis (it);
8155 it->dpvec_char_len = -1;
8156 }
8157 }
8158 else if (it->c == '\r' && it->selective == -1)
8159 {
8160 /* A value of selective == -1 means that everything from the
8161 CR to the end of the line is invisible, with maybe an
8162 ellipsis displayed for it. */
8163 success_p = next_element_from_ellipsis (it);
8164 it->dpvec_char_len = -1;
8165 }
8166 }
8167 }
8168
8169 /* Value is zero if end of buffer reached. */
8170 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8171 return success_p;
8172 }
8173
8174
8175 /* Run the redisplay end trigger hook for IT. */
8176
8177 static void
8178 run_redisplay_end_trigger_hook (struct it *it)
8179 {
8180 Lisp_Object args[3];
8181
8182 /* IT->glyph_row should be non-null, i.e. we should be actually
8183 displaying something, or otherwise we should not run the hook. */
8184 eassert (it->glyph_row);
8185
8186 /* Set up hook arguments. */
8187 args[0] = Qredisplay_end_trigger_functions;
8188 args[1] = it->window;
8189 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8190 it->redisplay_end_trigger_charpos = 0;
8191
8192 /* Since we are *trying* to run these functions, don't try to run
8193 them again, even if they get an error. */
8194 wset_redisplay_end_trigger (it->w, Qnil);
8195 Frun_hook_with_args (3, args);
8196
8197 /* Notice if it changed the face of the character we are on. */
8198 handle_face_prop (it);
8199 }
8200
8201
8202 /* Deliver a composition display element. Unlike the other
8203 next_element_from_XXX, this function is not registered in the array
8204 get_next_element[]. It is called from next_element_from_buffer and
8205 next_element_from_string when necessary. */
8206
8207 static int
8208 next_element_from_composition (struct it *it)
8209 {
8210 it->what = IT_COMPOSITION;
8211 it->len = it->cmp_it.nbytes;
8212 if (STRINGP (it->string))
8213 {
8214 if (it->c < 0)
8215 {
8216 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8217 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8218 return 0;
8219 }
8220 it->position = it->current.string_pos;
8221 it->object = it->string;
8222 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8223 IT_STRING_BYTEPOS (*it), it->string);
8224 }
8225 else
8226 {
8227 if (it->c < 0)
8228 {
8229 IT_CHARPOS (*it) += it->cmp_it.nchars;
8230 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8231 if (it->bidi_p)
8232 {
8233 if (it->bidi_it.new_paragraph)
8234 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8235 /* Resync the bidi iterator with IT's new position.
8236 FIXME: this doesn't support bidirectional text. */
8237 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8238 bidi_move_to_visually_next (&it->bidi_it);
8239 }
8240 return 0;
8241 }
8242 it->position = it->current.pos;
8243 it->object = it->w->contents;
8244 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8245 IT_BYTEPOS (*it), Qnil);
8246 }
8247 return 1;
8248 }
8249
8250
8251 \f
8252 /***********************************************************************
8253 Moving an iterator without producing glyphs
8254 ***********************************************************************/
8255
8256 /* Check if iterator is at a position corresponding to a valid buffer
8257 position after some move_it_ call. */
8258
8259 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8260 ((it)->method == GET_FROM_STRING \
8261 ? IT_STRING_CHARPOS (*it) == 0 \
8262 : 1)
8263
8264
8265 /* Move iterator IT to a specified buffer or X position within one
8266 line on the display without producing glyphs.
8267
8268 OP should be a bit mask including some or all of these bits:
8269 MOVE_TO_X: Stop upon reaching x-position TO_X.
8270 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8271 Regardless of OP's value, stop upon reaching the end of the display line.
8272
8273 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8274 This means, in particular, that TO_X includes window's horizontal
8275 scroll amount.
8276
8277 The return value has several possible values that
8278 say what condition caused the scan to stop:
8279
8280 MOVE_POS_MATCH_OR_ZV
8281 - when TO_POS or ZV was reached.
8282
8283 MOVE_X_REACHED
8284 -when TO_X was reached before TO_POS or ZV were reached.
8285
8286 MOVE_LINE_CONTINUED
8287 - when we reached the end of the display area and the line must
8288 be continued.
8289
8290 MOVE_LINE_TRUNCATED
8291 - when we reached the end of the display area and the line is
8292 truncated.
8293
8294 MOVE_NEWLINE_OR_CR
8295 - when we stopped at a line end, i.e. a newline or a CR and selective
8296 display is on. */
8297
8298 static enum move_it_result
8299 move_it_in_display_line_to (struct it *it,
8300 ptrdiff_t to_charpos, int to_x,
8301 enum move_operation_enum op)
8302 {
8303 enum move_it_result result = MOVE_UNDEFINED;
8304 struct glyph_row *saved_glyph_row;
8305 struct it wrap_it, atpos_it, atx_it, ppos_it;
8306 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8307 void *ppos_data = NULL;
8308 int may_wrap = 0;
8309 enum it_method prev_method = it->method;
8310 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8311 int saw_smaller_pos = prev_pos < to_charpos;
8312
8313 /* Don't produce glyphs in produce_glyphs. */
8314 saved_glyph_row = it->glyph_row;
8315 it->glyph_row = NULL;
8316
8317 /* Use wrap_it to save a copy of IT wherever a word wrap could
8318 occur. Use atpos_it to save a copy of IT at the desired buffer
8319 position, if found, so that we can scan ahead and check if the
8320 word later overshoots the window edge. Use atx_it similarly, for
8321 pixel positions. */
8322 wrap_it.sp = -1;
8323 atpos_it.sp = -1;
8324 atx_it.sp = -1;
8325
8326 /* Use ppos_it under bidi reordering to save a copy of IT for the
8327 position > CHARPOS that is the closest to CHARPOS. We restore
8328 that position in IT when we have scanned the entire display line
8329 without finding a match for CHARPOS and all the character
8330 positions are greater than CHARPOS. */
8331 if (it->bidi_p)
8332 {
8333 SAVE_IT (ppos_it, *it, ppos_data);
8334 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8335 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8336 SAVE_IT (ppos_it, *it, ppos_data);
8337 }
8338
8339 #define BUFFER_POS_REACHED_P() \
8340 ((op & MOVE_TO_POS) != 0 \
8341 && BUFFERP (it->object) \
8342 && (IT_CHARPOS (*it) == to_charpos \
8343 || ((!it->bidi_p \
8344 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8345 && IT_CHARPOS (*it) > to_charpos) \
8346 || (it->what == IT_COMPOSITION \
8347 && ((IT_CHARPOS (*it) > to_charpos \
8348 && to_charpos >= it->cmp_it.charpos) \
8349 || (IT_CHARPOS (*it) < to_charpos \
8350 && to_charpos <= it->cmp_it.charpos)))) \
8351 && (it->method == GET_FROM_BUFFER \
8352 || (it->method == GET_FROM_DISPLAY_VECTOR \
8353 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8354
8355 /* If there's a line-/wrap-prefix, handle it. */
8356 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8357 && it->current_y < it->last_visible_y)
8358 handle_line_prefix (it);
8359
8360 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8361 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8362
8363 while (1)
8364 {
8365 int x, i, ascent = 0, descent = 0;
8366
8367 /* Utility macro to reset an iterator with x, ascent, and descent. */
8368 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8369 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8370 (IT)->max_descent = descent)
8371
8372 /* Stop if we move beyond TO_CHARPOS (after an image or a
8373 display string or stretch glyph). */
8374 if ((op & MOVE_TO_POS) != 0
8375 && BUFFERP (it->object)
8376 && it->method == GET_FROM_BUFFER
8377 && (((!it->bidi_p
8378 /* When the iterator is at base embedding level, we
8379 are guaranteed that characters are delivered for
8380 display in strictly increasing order of their
8381 buffer positions. */
8382 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8383 && IT_CHARPOS (*it) > to_charpos)
8384 || (it->bidi_p
8385 && (prev_method == GET_FROM_IMAGE
8386 || prev_method == GET_FROM_STRETCH
8387 || prev_method == GET_FROM_STRING)
8388 /* Passed TO_CHARPOS from left to right. */
8389 && ((prev_pos < to_charpos
8390 && IT_CHARPOS (*it) > to_charpos)
8391 /* Passed TO_CHARPOS from right to left. */
8392 || (prev_pos > to_charpos
8393 && IT_CHARPOS (*it) < to_charpos)))))
8394 {
8395 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8396 {
8397 result = MOVE_POS_MATCH_OR_ZV;
8398 break;
8399 }
8400 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8401 /* If wrap_it is valid, the current position might be in a
8402 word that is wrapped. So, save the iterator in
8403 atpos_it and continue to see if wrapping happens. */
8404 SAVE_IT (atpos_it, *it, atpos_data);
8405 }
8406
8407 /* Stop when ZV reached.
8408 We used to stop here when TO_CHARPOS reached as well, but that is
8409 too soon if this glyph does not fit on this line. So we handle it
8410 explicitly below. */
8411 if (!get_next_display_element (it))
8412 {
8413 result = MOVE_POS_MATCH_OR_ZV;
8414 break;
8415 }
8416
8417 if (it->line_wrap == TRUNCATE)
8418 {
8419 if (BUFFER_POS_REACHED_P ())
8420 {
8421 result = MOVE_POS_MATCH_OR_ZV;
8422 break;
8423 }
8424 }
8425 else
8426 {
8427 if (it->line_wrap == WORD_WRAP)
8428 {
8429 if (IT_DISPLAYING_WHITESPACE (it))
8430 may_wrap = 1;
8431 else if (may_wrap)
8432 {
8433 /* We have reached a glyph that follows one or more
8434 whitespace characters. If the position is
8435 already found, we are done. */
8436 if (atpos_it.sp >= 0)
8437 {
8438 RESTORE_IT (it, &atpos_it, atpos_data);
8439 result = MOVE_POS_MATCH_OR_ZV;
8440 goto done;
8441 }
8442 if (atx_it.sp >= 0)
8443 {
8444 RESTORE_IT (it, &atx_it, atx_data);
8445 result = MOVE_X_REACHED;
8446 goto done;
8447 }
8448 /* Otherwise, we can wrap here. */
8449 SAVE_IT (wrap_it, *it, wrap_data);
8450 may_wrap = 0;
8451 }
8452 }
8453 }
8454
8455 /* Remember the line height for the current line, in case
8456 the next element doesn't fit on the line. */
8457 ascent = it->max_ascent;
8458 descent = it->max_descent;
8459
8460 /* The call to produce_glyphs will get the metrics of the
8461 display element IT is loaded with. Record the x-position
8462 before this display element, in case it doesn't fit on the
8463 line. */
8464 x = it->current_x;
8465
8466 PRODUCE_GLYPHS (it);
8467
8468 if (it->area != TEXT_AREA)
8469 {
8470 prev_method = it->method;
8471 if (it->method == GET_FROM_BUFFER)
8472 prev_pos = IT_CHARPOS (*it);
8473 set_iterator_to_next (it, 1);
8474 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8475 SET_TEXT_POS (this_line_min_pos,
8476 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8477 if (it->bidi_p
8478 && (op & MOVE_TO_POS)
8479 && IT_CHARPOS (*it) > to_charpos
8480 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8481 SAVE_IT (ppos_it, *it, ppos_data);
8482 continue;
8483 }
8484
8485 /* The number of glyphs we get back in IT->nglyphs will normally
8486 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8487 character on a terminal frame, or (iii) a line end. For the
8488 second case, IT->nglyphs - 1 padding glyphs will be present.
8489 (On X frames, there is only one glyph produced for a
8490 composite character.)
8491
8492 The behavior implemented below means, for continuation lines,
8493 that as many spaces of a TAB as fit on the current line are
8494 displayed there. For terminal frames, as many glyphs of a
8495 multi-glyph character are displayed in the current line, too.
8496 This is what the old redisplay code did, and we keep it that
8497 way. Under X, the whole shape of a complex character must
8498 fit on the line or it will be completely displayed in the
8499 next line.
8500
8501 Note that both for tabs and padding glyphs, all glyphs have
8502 the same width. */
8503 if (it->nglyphs)
8504 {
8505 /* More than one glyph or glyph doesn't fit on line. All
8506 glyphs have the same width. */
8507 int single_glyph_width = it->pixel_width / it->nglyphs;
8508 int new_x;
8509 int x_before_this_char = x;
8510 int hpos_before_this_char = it->hpos;
8511
8512 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8513 {
8514 new_x = x + single_glyph_width;
8515
8516 /* We want to leave anything reaching TO_X to the caller. */
8517 if ((op & MOVE_TO_X) && new_x > to_x)
8518 {
8519 if (BUFFER_POS_REACHED_P ())
8520 {
8521 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8522 goto buffer_pos_reached;
8523 if (atpos_it.sp < 0)
8524 {
8525 SAVE_IT (atpos_it, *it, atpos_data);
8526 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8527 }
8528 }
8529 else
8530 {
8531 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8532 {
8533 it->current_x = x;
8534 result = MOVE_X_REACHED;
8535 break;
8536 }
8537 if (atx_it.sp < 0)
8538 {
8539 SAVE_IT (atx_it, *it, atx_data);
8540 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8541 }
8542 }
8543 }
8544
8545 if (/* Lines are continued. */
8546 it->line_wrap != TRUNCATE
8547 && (/* And glyph doesn't fit on the line. */
8548 new_x > it->last_visible_x
8549 /* Or it fits exactly and we're on a window
8550 system frame. */
8551 || (new_x == it->last_visible_x
8552 && FRAME_WINDOW_P (it->f)
8553 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8554 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8555 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8556 {
8557 if (/* IT->hpos == 0 means the very first glyph
8558 doesn't fit on the line, e.g. a wide image. */
8559 it->hpos == 0
8560 || (new_x == it->last_visible_x
8561 && FRAME_WINDOW_P (it->f)))
8562 {
8563 ++it->hpos;
8564 it->current_x = new_x;
8565
8566 /* The character's last glyph just barely fits
8567 in this row. */
8568 if (i == it->nglyphs - 1)
8569 {
8570 /* If this is the destination position,
8571 return a position *before* it in this row,
8572 now that we know it fits in this row. */
8573 if (BUFFER_POS_REACHED_P ())
8574 {
8575 if (it->line_wrap != WORD_WRAP
8576 || wrap_it.sp < 0)
8577 {
8578 it->hpos = hpos_before_this_char;
8579 it->current_x = x_before_this_char;
8580 result = MOVE_POS_MATCH_OR_ZV;
8581 break;
8582 }
8583 if (it->line_wrap == WORD_WRAP
8584 && atpos_it.sp < 0)
8585 {
8586 SAVE_IT (atpos_it, *it, atpos_data);
8587 atpos_it.current_x = x_before_this_char;
8588 atpos_it.hpos = hpos_before_this_char;
8589 }
8590 }
8591
8592 prev_method = it->method;
8593 if (it->method == GET_FROM_BUFFER)
8594 prev_pos = IT_CHARPOS (*it);
8595 set_iterator_to_next (it, 1);
8596 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8597 SET_TEXT_POS (this_line_min_pos,
8598 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8599 /* On graphical terminals, newlines may
8600 "overflow" into the fringe if
8601 overflow-newline-into-fringe is non-nil.
8602 On text terminals, and on graphical
8603 terminals with no right margin, newlines
8604 may overflow into the last glyph on the
8605 display line.*/
8606 if (!FRAME_WINDOW_P (it->f)
8607 || ((it->bidi_p
8608 && it->bidi_it.paragraph_dir == R2L)
8609 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8610 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8611 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8612 {
8613 if (!get_next_display_element (it))
8614 {
8615 result = MOVE_POS_MATCH_OR_ZV;
8616 break;
8617 }
8618 if (BUFFER_POS_REACHED_P ())
8619 {
8620 if (ITERATOR_AT_END_OF_LINE_P (it))
8621 result = MOVE_POS_MATCH_OR_ZV;
8622 else
8623 result = MOVE_LINE_CONTINUED;
8624 break;
8625 }
8626 if (ITERATOR_AT_END_OF_LINE_P (it)
8627 && (it->line_wrap != WORD_WRAP
8628 || wrap_it.sp < 0))
8629 {
8630 result = MOVE_NEWLINE_OR_CR;
8631 break;
8632 }
8633 }
8634 }
8635 }
8636 else
8637 IT_RESET_X_ASCENT_DESCENT (it);
8638
8639 if (wrap_it.sp >= 0)
8640 {
8641 RESTORE_IT (it, &wrap_it, wrap_data);
8642 atpos_it.sp = -1;
8643 atx_it.sp = -1;
8644 }
8645
8646 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8647 IT_CHARPOS (*it)));
8648 result = MOVE_LINE_CONTINUED;
8649 break;
8650 }
8651
8652 if (BUFFER_POS_REACHED_P ())
8653 {
8654 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8655 goto buffer_pos_reached;
8656 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8657 {
8658 SAVE_IT (atpos_it, *it, atpos_data);
8659 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8660 }
8661 }
8662
8663 if (new_x > it->first_visible_x)
8664 {
8665 /* Glyph is visible. Increment number of glyphs that
8666 would be displayed. */
8667 ++it->hpos;
8668 }
8669 }
8670
8671 if (result != MOVE_UNDEFINED)
8672 break;
8673 }
8674 else if (BUFFER_POS_REACHED_P ())
8675 {
8676 buffer_pos_reached:
8677 IT_RESET_X_ASCENT_DESCENT (it);
8678 result = MOVE_POS_MATCH_OR_ZV;
8679 break;
8680 }
8681 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8682 {
8683 /* Stop when TO_X specified and reached. This check is
8684 necessary here because of lines consisting of a line end,
8685 only. The line end will not produce any glyphs and we
8686 would never get MOVE_X_REACHED. */
8687 eassert (it->nglyphs == 0);
8688 result = MOVE_X_REACHED;
8689 break;
8690 }
8691
8692 /* Is this a line end? If yes, we're done. */
8693 if (ITERATOR_AT_END_OF_LINE_P (it))
8694 {
8695 /* If we are past TO_CHARPOS, but never saw any character
8696 positions smaller than TO_CHARPOS, return
8697 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8698 did. */
8699 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8700 {
8701 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8702 {
8703 if (IT_CHARPOS (ppos_it) < ZV)
8704 {
8705 RESTORE_IT (it, &ppos_it, ppos_data);
8706 result = MOVE_POS_MATCH_OR_ZV;
8707 }
8708 else
8709 goto buffer_pos_reached;
8710 }
8711 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8712 && IT_CHARPOS (*it) > to_charpos)
8713 goto buffer_pos_reached;
8714 else
8715 result = MOVE_NEWLINE_OR_CR;
8716 }
8717 else
8718 result = MOVE_NEWLINE_OR_CR;
8719 break;
8720 }
8721
8722 prev_method = it->method;
8723 if (it->method == GET_FROM_BUFFER)
8724 prev_pos = IT_CHARPOS (*it);
8725 /* The current display element has been consumed. Advance
8726 to the next. */
8727 set_iterator_to_next (it, 1);
8728 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8729 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8730 if (IT_CHARPOS (*it) < to_charpos)
8731 saw_smaller_pos = 1;
8732 if (it->bidi_p
8733 && (op & MOVE_TO_POS)
8734 && IT_CHARPOS (*it) >= to_charpos
8735 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8736 SAVE_IT (ppos_it, *it, ppos_data);
8737
8738 /* Stop if lines are truncated and IT's current x-position is
8739 past the right edge of the window now. */
8740 if (it->line_wrap == TRUNCATE
8741 && it->current_x >= it->last_visible_x)
8742 {
8743 if (!FRAME_WINDOW_P (it->f)
8744 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8745 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8746 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8747 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8748 {
8749 int at_eob_p = 0;
8750
8751 if ((at_eob_p = !get_next_display_element (it))
8752 || BUFFER_POS_REACHED_P ()
8753 /* If we are past TO_CHARPOS, but never saw any
8754 character positions smaller than TO_CHARPOS,
8755 return MOVE_POS_MATCH_OR_ZV, like the
8756 unidirectional display did. */
8757 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8758 && !saw_smaller_pos
8759 && IT_CHARPOS (*it) > to_charpos))
8760 {
8761 if (it->bidi_p
8762 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8763 RESTORE_IT (it, &ppos_it, ppos_data);
8764 result = MOVE_POS_MATCH_OR_ZV;
8765 break;
8766 }
8767 if (ITERATOR_AT_END_OF_LINE_P (it))
8768 {
8769 result = MOVE_NEWLINE_OR_CR;
8770 break;
8771 }
8772 }
8773 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8774 && !saw_smaller_pos
8775 && IT_CHARPOS (*it) > to_charpos)
8776 {
8777 if (IT_CHARPOS (ppos_it) < ZV)
8778 RESTORE_IT (it, &ppos_it, ppos_data);
8779 result = MOVE_POS_MATCH_OR_ZV;
8780 break;
8781 }
8782 result = MOVE_LINE_TRUNCATED;
8783 break;
8784 }
8785 #undef IT_RESET_X_ASCENT_DESCENT
8786 }
8787
8788 #undef BUFFER_POS_REACHED_P
8789
8790 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8791 restore the saved iterator. */
8792 if (atpos_it.sp >= 0)
8793 RESTORE_IT (it, &atpos_it, atpos_data);
8794 else if (atx_it.sp >= 0)
8795 RESTORE_IT (it, &atx_it, atx_data);
8796
8797 done:
8798
8799 if (atpos_data)
8800 bidi_unshelve_cache (atpos_data, 1);
8801 if (atx_data)
8802 bidi_unshelve_cache (atx_data, 1);
8803 if (wrap_data)
8804 bidi_unshelve_cache (wrap_data, 1);
8805 if (ppos_data)
8806 bidi_unshelve_cache (ppos_data, 1);
8807
8808 /* Restore the iterator settings altered at the beginning of this
8809 function. */
8810 it->glyph_row = saved_glyph_row;
8811 return result;
8812 }
8813
8814 /* For external use. */
8815 void
8816 move_it_in_display_line (struct it *it,
8817 ptrdiff_t to_charpos, int to_x,
8818 enum move_operation_enum op)
8819 {
8820 if (it->line_wrap == WORD_WRAP
8821 && (op & MOVE_TO_X))
8822 {
8823 struct it save_it;
8824 void *save_data = NULL;
8825 int skip;
8826
8827 SAVE_IT (save_it, *it, save_data);
8828 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8829 /* When word-wrap is on, TO_X may lie past the end
8830 of a wrapped line. Then it->current is the
8831 character on the next line, so backtrack to the
8832 space before the wrap point. */
8833 if (skip == MOVE_LINE_CONTINUED)
8834 {
8835 int prev_x = max (it->current_x - 1, 0);
8836 RESTORE_IT (it, &save_it, save_data);
8837 move_it_in_display_line_to
8838 (it, -1, prev_x, MOVE_TO_X);
8839 }
8840 else
8841 bidi_unshelve_cache (save_data, 1);
8842 }
8843 else
8844 move_it_in_display_line_to (it, to_charpos, to_x, op);
8845 }
8846
8847
8848 /* Move IT forward until it satisfies one or more of the criteria in
8849 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8850
8851 OP is a bit-mask that specifies where to stop, and in particular,
8852 which of those four position arguments makes a difference. See the
8853 description of enum move_operation_enum.
8854
8855 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8856 screen line, this function will set IT to the next position that is
8857 displayed to the right of TO_CHARPOS on the screen. */
8858
8859 void
8860 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8861 {
8862 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8863 int line_height, line_start_x = 0, reached = 0;
8864 void *backup_data = NULL;
8865
8866 for (;;)
8867 {
8868 if (op & MOVE_TO_VPOS)
8869 {
8870 /* If no TO_CHARPOS and no TO_X specified, stop at the
8871 start of the line TO_VPOS. */
8872 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8873 {
8874 if (it->vpos == to_vpos)
8875 {
8876 reached = 1;
8877 break;
8878 }
8879 else
8880 skip = move_it_in_display_line_to (it, -1, -1, 0);
8881 }
8882 else
8883 {
8884 /* TO_VPOS >= 0 means stop at TO_X in the line at
8885 TO_VPOS, or at TO_POS, whichever comes first. */
8886 if (it->vpos == to_vpos)
8887 {
8888 reached = 2;
8889 break;
8890 }
8891
8892 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8893
8894 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8895 {
8896 reached = 3;
8897 break;
8898 }
8899 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8900 {
8901 /* We have reached TO_X but not in the line we want. */
8902 skip = move_it_in_display_line_to (it, to_charpos,
8903 -1, MOVE_TO_POS);
8904 if (skip == MOVE_POS_MATCH_OR_ZV)
8905 {
8906 reached = 4;
8907 break;
8908 }
8909 }
8910 }
8911 }
8912 else if (op & MOVE_TO_Y)
8913 {
8914 struct it it_backup;
8915
8916 if (it->line_wrap == WORD_WRAP)
8917 SAVE_IT (it_backup, *it, backup_data);
8918
8919 /* TO_Y specified means stop at TO_X in the line containing
8920 TO_Y---or at TO_CHARPOS if this is reached first. The
8921 problem is that we can't really tell whether the line
8922 contains TO_Y before we have completely scanned it, and
8923 this may skip past TO_X. What we do is to first scan to
8924 TO_X.
8925
8926 If TO_X is not specified, use a TO_X of zero. The reason
8927 is to make the outcome of this function more predictable.
8928 If we didn't use TO_X == 0, we would stop at the end of
8929 the line which is probably not what a caller would expect
8930 to happen. */
8931 skip = move_it_in_display_line_to
8932 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8933 (MOVE_TO_X | (op & MOVE_TO_POS)));
8934
8935 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8936 if (skip == MOVE_POS_MATCH_OR_ZV)
8937 reached = 5;
8938 else if (skip == MOVE_X_REACHED)
8939 {
8940 /* If TO_X was reached, we want to know whether TO_Y is
8941 in the line. We know this is the case if the already
8942 scanned glyphs make the line tall enough. Otherwise,
8943 we must check by scanning the rest of the line. */
8944 line_height = it->max_ascent + it->max_descent;
8945 if (to_y >= it->current_y
8946 && to_y < it->current_y + line_height)
8947 {
8948 reached = 6;
8949 break;
8950 }
8951 SAVE_IT (it_backup, *it, backup_data);
8952 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8953 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8954 op & MOVE_TO_POS);
8955 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8956 line_height = it->max_ascent + it->max_descent;
8957 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8958
8959 if (to_y >= it->current_y
8960 && to_y < it->current_y + line_height)
8961 {
8962 /* If TO_Y is in this line and TO_X was reached
8963 above, we scanned too far. We have to restore
8964 IT's settings to the ones before skipping. But
8965 keep the more accurate values of max_ascent and
8966 max_descent we've found while skipping the rest
8967 of the line, for the sake of callers, such as
8968 pos_visible_p, that need to know the line
8969 height. */
8970 int max_ascent = it->max_ascent;
8971 int max_descent = it->max_descent;
8972
8973 RESTORE_IT (it, &it_backup, backup_data);
8974 it->max_ascent = max_ascent;
8975 it->max_descent = max_descent;
8976 reached = 6;
8977 }
8978 else
8979 {
8980 skip = skip2;
8981 if (skip == MOVE_POS_MATCH_OR_ZV)
8982 reached = 7;
8983 }
8984 }
8985 else
8986 {
8987 /* Check whether TO_Y is in this line. */
8988 line_height = it->max_ascent + it->max_descent;
8989 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8990
8991 if (to_y >= it->current_y
8992 && to_y < it->current_y + line_height)
8993 {
8994 /* When word-wrap is on, TO_X may lie past the end
8995 of a wrapped line. Then it->current is the
8996 character on the next line, so backtrack to the
8997 space before the wrap point. */
8998 if (skip == MOVE_LINE_CONTINUED
8999 && it->line_wrap == WORD_WRAP)
9000 {
9001 int prev_x = max (it->current_x - 1, 0);
9002 RESTORE_IT (it, &it_backup, backup_data);
9003 skip = move_it_in_display_line_to
9004 (it, -1, prev_x, MOVE_TO_X);
9005 }
9006 reached = 6;
9007 }
9008 }
9009
9010 if (reached)
9011 break;
9012 }
9013 else if (BUFFERP (it->object)
9014 && (it->method == GET_FROM_BUFFER
9015 || it->method == GET_FROM_STRETCH)
9016 && IT_CHARPOS (*it) >= to_charpos
9017 /* Under bidi iteration, a call to set_iterator_to_next
9018 can scan far beyond to_charpos if the initial
9019 portion of the next line needs to be reordered. In
9020 that case, give move_it_in_display_line_to another
9021 chance below. */
9022 && !(it->bidi_p
9023 && it->bidi_it.scan_dir == -1))
9024 skip = MOVE_POS_MATCH_OR_ZV;
9025 else
9026 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9027
9028 switch (skip)
9029 {
9030 case MOVE_POS_MATCH_OR_ZV:
9031 reached = 8;
9032 goto out;
9033
9034 case MOVE_NEWLINE_OR_CR:
9035 set_iterator_to_next (it, 1);
9036 it->continuation_lines_width = 0;
9037 break;
9038
9039 case MOVE_LINE_TRUNCATED:
9040 it->continuation_lines_width = 0;
9041 reseat_at_next_visible_line_start (it, 0);
9042 if ((op & MOVE_TO_POS) != 0
9043 && IT_CHARPOS (*it) > to_charpos)
9044 {
9045 reached = 9;
9046 goto out;
9047 }
9048 break;
9049
9050 case MOVE_LINE_CONTINUED:
9051 /* For continued lines ending in a tab, some of the glyphs
9052 associated with the tab are displayed on the current
9053 line. Since it->current_x does not include these glyphs,
9054 we use it->last_visible_x instead. */
9055 if (it->c == '\t')
9056 {
9057 it->continuation_lines_width += it->last_visible_x;
9058 /* When moving by vpos, ensure that the iterator really
9059 advances to the next line (bug#847, bug#969). Fixme:
9060 do we need to do this in other circumstances? */
9061 if (it->current_x != it->last_visible_x
9062 && (op & MOVE_TO_VPOS)
9063 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9064 {
9065 line_start_x = it->current_x + it->pixel_width
9066 - it->last_visible_x;
9067 set_iterator_to_next (it, 0);
9068 }
9069 }
9070 else
9071 it->continuation_lines_width += it->current_x;
9072 break;
9073
9074 default:
9075 emacs_abort ();
9076 }
9077
9078 /* Reset/increment for the next run. */
9079 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9080 it->current_x = line_start_x;
9081 line_start_x = 0;
9082 it->hpos = 0;
9083 it->current_y += it->max_ascent + it->max_descent;
9084 ++it->vpos;
9085 last_height = it->max_ascent + it->max_descent;
9086 it->max_ascent = it->max_descent = 0;
9087 }
9088
9089 out:
9090
9091 /* On text terminals, we may stop at the end of a line in the middle
9092 of a multi-character glyph. If the glyph itself is continued,
9093 i.e. it is actually displayed on the next line, don't treat this
9094 stopping point as valid; move to the next line instead (unless
9095 that brings us offscreen). */
9096 if (!FRAME_WINDOW_P (it->f)
9097 && op & MOVE_TO_POS
9098 && IT_CHARPOS (*it) == to_charpos
9099 && it->what == IT_CHARACTER
9100 && it->nglyphs > 1
9101 && it->line_wrap == WINDOW_WRAP
9102 && it->current_x == it->last_visible_x - 1
9103 && it->c != '\n'
9104 && it->c != '\t'
9105 && it->vpos < it->w->window_end_vpos)
9106 {
9107 it->continuation_lines_width += it->current_x;
9108 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9109 it->current_y += it->max_ascent + it->max_descent;
9110 ++it->vpos;
9111 last_height = it->max_ascent + it->max_descent;
9112 }
9113
9114 if (backup_data)
9115 bidi_unshelve_cache (backup_data, 1);
9116
9117 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9118 }
9119
9120
9121 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9122
9123 If DY > 0, move IT backward at least that many pixels. DY = 0
9124 means move IT backward to the preceding line start or BEGV. This
9125 function may move over more than DY pixels if IT->current_y - DY
9126 ends up in the middle of a line; in this case IT->current_y will be
9127 set to the top of the line moved to. */
9128
9129 void
9130 move_it_vertically_backward (struct it *it, int dy)
9131 {
9132 int nlines, h;
9133 struct it it2, it3;
9134 void *it2data = NULL, *it3data = NULL;
9135 ptrdiff_t start_pos;
9136 int nchars_per_row
9137 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9138 ptrdiff_t pos_limit;
9139
9140 move_further_back:
9141 eassert (dy >= 0);
9142
9143 start_pos = IT_CHARPOS (*it);
9144
9145 /* Estimate how many newlines we must move back. */
9146 nlines = max (1, dy / default_line_pixel_height (it->w));
9147 if (it->line_wrap == TRUNCATE)
9148 pos_limit = BEGV;
9149 else
9150 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9151
9152 /* Set the iterator's position that many lines back. But don't go
9153 back more than NLINES full screen lines -- this wins a day with
9154 buffers which have very long lines. */
9155 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9156 back_to_previous_visible_line_start (it);
9157
9158 /* Reseat the iterator here. When moving backward, we don't want
9159 reseat to skip forward over invisible text, set up the iterator
9160 to deliver from overlay strings at the new position etc. So,
9161 use reseat_1 here. */
9162 reseat_1 (it, it->current.pos, 1);
9163
9164 /* We are now surely at a line start. */
9165 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9166 reordering is in effect. */
9167 it->continuation_lines_width = 0;
9168
9169 /* Move forward and see what y-distance we moved. First move to the
9170 start of the next line so that we get its height. We need this
9171 height to be able to tell whether we reached the specified
9172 y-distance. */
9173 SAVE_IT (it2, *it, it2data);
9174 it2.max_ascent = it2.max_descent = 0;
9175 do
9176 {
9177 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9178 MOVE_TO_POS | MOVE_TO_VPOS);
9179 }
9180 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9181 /* If we are in a display string which starts at START_POS,
9182 and that display string includes a newline, and we are
9183 right after that newline (i.e. at the beginning of a
9184 display line), exit the loop, because otherwise we will
9185 infloop, since move_it_to will see that it is already at
9186 START_POS and will not move. */
9187 || (it2.method == GET_FROM_STRING
9188 && IT_CHARPOS (it2) == start_pos
9189 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9190 eassert (IT_CHARPOS (*it) >= BEGV);
9191 SAVE_IT (it3, it2, it3data);
9192
9193 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9194 eassert (IT_CHARPOS (*it) >= BEGV);
9195 /* H is the actual vertical distance from the position in *IT
9196 and the starting position. */
9197 h = it2.current_y - it->current_y;
9198 /* NLINES is the distance in number of lines. */
9199 nlines = it2.vpos - it->vpos;
9200
9201 /* Correct IT's y and vpos position
9202 so that they are relative to the starting point. */
9203 it->vpos -= nlines;
9204 it->current_y -= h;
9205
9206 if (dy == 0)
9207 {
9208 /* DY == 0 means move to the start of the screen line. The
9209 value of nlines is > 0 if continuation lines were involved,
9210 or if the original IT position was at start of a line. */
9211 RESTORE_IT (it, it, it2data);
9212 if (nlines > 0)
9213 move_it_by_lines (it, nlines);
9214 /* The above code moves us to some position NLINES down,
9215 usually to its first glyph (leftmost in an L2R line), but
9216 that's not necessarily the start of the line, under bidi
9217 reordering. We want to get to the character position
9218 that is immediately after the newline of the previous
9219 line. */
9220 if (it->bidi_p
9221 && !it->continuation_lines_width
9222 && !STRINGP (it->string)
9223 && IT_CHARPOS (*it) > BEGV
9224 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9225 {
9226 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9227
9228 DEC_BOTH (cp, bp);
9229 cp = find_newline_no_quit (cp, bp, -1, NULL);
9230 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9231 }
9232 bidi_unshelve_cache (it3data, 1);
9233 }
9234 else
9235 {
9236 /* The y-position we try to reach, relative to *IT.
9237 Note that H has been subtracted in front of the if-statement. */
9238 int target_y = it->current_y + h - dy;
9239 int y0 = it3.current_y;
9240 int y1;
9241 int line_height;
9242
9243 RESTORE_IT (&it3, &it3, it3data);
9244 y1 = line_bottom_y (&it3);
9245 line_height = y1 - y0;
9246 RESTORE_IT (it, it, it2data);
9247 /* If we did not reach target_y, try to move further backward if
9248 we can. If we moved too far backward, try to move forward. */
9249 if (target_y < it->current_y
9250 /* This is heuristic. In a window that's 3 lines high, with
9251 a line height of 13 pixels each, recentering with point
9252 on the bottom line will try to move -39/2 = 19 pixels
9253 backward. Try to avoid moving into the first line. */
9254 && (it->current_y - target_y
9255 > min (window_box_height (it->w), line_height * 2 / 3))
9256 && IT_CHARPOS (*it) > BEGV)
9257 {
9258 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9259 target_y - it->current_y));
9260 dy = it->current_y - target_y;
9261 goto move_further_back;
9262 }
9263 else if (target_y >= it->current_y + line_height
9264 && IT_CHARPOS (*it) < ZV)
9265 {
9266 /* Should move forward by at least one line, maybe more.
9267
9268 Note: Calling move_it_by_lines can be expensive on
9269 terminal frames, where compute_motion is used (via
9270 vmotion) to do the job, when there are very long lines
9271 and truncate-lines is nil. That's the reason for
9272 treating terminal frames specially here. */
9273
9274 if (!FRAME_WINDOW_P (it->f))
9275 move_it_vertically (it, target_y - (it->current_y + line_height));
9276 else
9277 {
9278 do
9279 {
9280 move_it_by_lines (it, 1);
9281 }
9282 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9283 }
9284 }
9285 }
9286 }
9287
9288
9289 /* Move IT by a specified amount of pixel lines DY. DY negative means
9290 move backwards. DY = 0 means move to start of screen line. At the
9291 end, IT will be on the start of a screen line. */
9292
9293 void
9294 move_it_vertically (struct it *it, int dy)
9295 {
9296 if (dy <= 0)
9297 move_it_vertically_backward (it, -dy);
9298 else
9299 {
9300 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9301 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9302 MOVE_TO_POS | MOVE_TO_Y);
9303 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9304
9305 /* If buffer ends in ZV without a newline, move to the start of
9306 the line to satisfy the post-condition. */
9307 if (IT_CHARPOS (*it) == ZV
9308 && ZV > BEGV
9309 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9310 move_it_by_lines (it, 0);
9311 }
9312 }
9313
9314
9315 /* Move iterator IT past the end of the text line it is in. */
9316
9317 void
9318 move_it_past_eol (struct it *it)
9319 {
9320 enum move_it_result rc;
9321
9322 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9323 if (rc == MOVE_NEWLINE_OR_CR)
9324 set_iterator_to_next (it, 0);
9325 }
9326
9327
9328 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9329 negative means move up. DVPOS == 0 means move to the start of the
9330 screen line.
9331
9332 Optimization idea: If we would know that IT->f doesn't use
9333 a face with proportional font, we could be faster for
9334 truncate-lines nil. */
9335
9336 void
9337 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9338 {
9339
9340 /* The commented-out optimization uses vmotion on terminals. This
9341 gives bad results, because elements like it->what, on which
9342 callers such as pos_visible_p rely, aren't updated. */
9343 /* struct position pos;
9344 if (!FRAME_WINDOW_P (it->f))
9345 {
9346 struct text_pos textpos;
9347
9348 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9349 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9350 reseat (it, textpos, 1);
9351 it->vpos += pos.vpos;
9352 it->current_y += pos.vpos;
9353 }
9354 else */
9355
9356 if (dvpos == 0)
9357 {
9358 /* DVPOS == 0 means move to the start of the screen line. */
9359 move_it_vertically_backward (it, 0);
9360 /* Let next call to line_bottom_y calculate real line height */
9361 last_height = 0;
9362 }
9363 else if (dvpos > 0)
9364 {
9365 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9366 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9367 {
9368 /* Only move to the next buffer position if we ended up in a
9369 string from display property, not in an overlay string
9370 (before-string or after-string). That is because the
9371 latter don't conceal the underlying buffer position, so
9372 we can ask to move the iterator to the exact position we
9373 are interested in. Note that, even if we are already at
9374 IT_CHARPOS (*it), the call below is not a no-op, as it
9375 will detect that we are at the end of the string, pop the
9376 iterator, and compute it->current_x and it->hpos
9377 correctly. */
9378 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9379 -1, -1, -1, MOVE_TO_POS);
9380 }
9381 }
9382 else
9383 {
9384 struct it it2;
9385 void *it2data = NULL;
9386 ptrdiff_t start_charpos, i;
9387 int nchars_per_row
9388 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9389 ptrdiff_t pos_limit;
9390
9391 /* Start at the beginning of the screen line containing IT's
9392 position. This may actually move vertically backwards,
9393 in case of overlays, so adjust dvpos accordingly. */
9394 dvpos += it->vpos;
9395 move_it_vertically_backward (it, 0);
9396 dvpos -= it->vpos;
9397
9398 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9399 screen lines, and reseat the iterator there. */
9400 start_charpos = IT_CHARPOS (*it);
9401 if (it->line_wrap == TRUNCATE)
9402 pos_limit = BEGV;
9403 else
9404 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9405 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9406 back_to_previous_visible_line_start (it);
9407 reseat (it, it->current.pos, 1);
9408
9409 /* Move further back if we end up in a string or an image. */
9410 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9411 {
9412 /* First try to move to start of display line. */
9413 dvpos += it->vpos;
9414 move_it_vertically_backward (it, 0);
9415 dvpos -= it->vpos;
9416 if (IT_POS_VALID_AFTER_MOVE_P (it))
9417 break;
9418 /* If start of line is still in string or image,
9419 move further back. */
9420 back_to_previous_visible_line_start (it);
9421 reseat (it, it->current.pos, 1);
9422 dvpos--;
9423 }
9424
9425 it->current_x = it->hpos = 0;
9426
9427 /* Above call may have moved too far if continuation lines
9428 are involved. Scan forward and see if it did. */
9429 SAVE_IT (it2, *it, it2data);
9430 it2.vpos = it2.current_y = 0;
9431 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9432 it->vpos -= it2.vpos;
9433 it->current_y -= it2.current_y;
9434 it->current_x = it->hpos = 0;
9435
9436 /* If we moved too far back, move IT some lines forward. */
9437 if (it2.vpos > -dvpos)
9438 {
9439 int delta = it2.vpos + dvpos;
9440
9441 RESTORE_IT (&it2, &it2, it2data);
9442 SAVE_IT (it2, *it, it2data);
9443 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9444 /* Move back again if we got too far ahead. */
9445 if (IT_CHARPOS (*it) >= start_charpos)
9446 RESTORE_IT (it, &it2, it2data);
9447 else
9448 bidi_unshelve_cache (it2data, 1);
9449 }
9450 else
9451 RESTORE_IT (it, it, it2data);
9452 }
9453 }
9454
9455 /* Return 1 if IT points into the middle of a display vector. */
9456
9457 int
9458 in_display_vector_p (struct it *it)
9459 {
9460 return (it->method == GET_FROM_DISPLAY_VECTOR
9461 && it->current.dpvec_index > 0
9462 && it->dpvec + it->current.dpvec_index != it->dpend);
9463 }
9464
9465 \f
9466 /***********************************************************************
9467 Messages
9468 ***********************************************************************/
9469
9470
9471 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9472 to *Messages*. */
9473
9474 void
9475 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9476 {
9477 Lisp_Object args[3];
9478 Lisp_Object msg, fmt;
9479 char *buffer;
9480 ptrdiff_t len;
9481 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9482 USE_SAFE_ALLOCA;
9483
9484 fmt = msg = Qnil;
9485 GCPRO4 (fmt, msg, arg1, arg2);
9486
9487 args[0] = fmt = build_string (format);
9488 args[1] = arg1;
9489 args[2] = arg2;
9490 msg = Fformat (3, args);
9491
9492 len = SBYTES (msg) + 1;
9493 buffer = SAFE_ALLOCA (len);
9494 memcpy (buffer, SDATA (msg), len);
9495
9496 message_dolog (buffer, len - 1, 1, 0);
9497 SAFE_FREE ();
9498
9499 UNGCPRO;
9500 }
9501
9502
9503 /* Output a newline in the *Messages* buffer if "needs" one. */
9504
9505 void
9506 message_log_maybe_newline (void)
9507 {
9508 if (message_log_need_newline)
9509 message_dolog ("", 0, 1, 0);
9510 }
9511
9512
9513 /* Add a string M of length NBYTES to the message log, optionally
9514 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9515 true, means interpret the contents of M as multibyte. This
9516 function calls low-level routines in order to bypass text property
9517 hooks, etc. which might not be safe to run.
9518
9519 This may GC (insert may run before/after change hooks),
9520 so the buffer M must NOT point to a Lisp string. */
9521
9522 void
9523 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9524 {
9525 const unsigned char *msg = (const unsigned char *) m;
9526
9527 if (!NILP (Vmemory_full))
9528 return;
9529
9530 if (!NILP (Vmessage_log_max))
9531 {
9532 struct buffer *oldbuf;
9533 Lisp_Object oldpoint, oldbegv, oldzv;
9534 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9535 ptrdiff_t point_at_end = 0;
9536 ptrdiff_t zv_at_end = 0;
9537 Lisp_Object old_deactivate_mark;
9538 bool shown;
9539 struct gcpro gcpro1;
9540
9541 old_deactivate_mark = Vdeactivate_mark;
9542 oldbuf = current_buffer;
9543 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9544 bset_undo_list (current_buffer, Qt);
9545
9546 oldpoint = message_dolog_marker1;
9547 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9548 oldbegv = message_dolog_marker2;
9549 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9550 oldzv = message_dolog_marker3;
9551 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9552 GCPRO1 (old_deactivate_mark);
9553
9554 if (PT == Z)
9555 point_at_end = 1;
9556 if (ZV == Z)
9557 zv_at_end = 1;
9558
9559 BEGV = BEG;
9560 BEGV_BYTE = BEG_BYTE;
9561 ZV = Z;
9562 ZV_BYTE = Z_BYTE;
9563 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9564
9565 /* Insert the string--maybe converting multibyte to single byte
9566 or vice versa, so that all the text fits the buffer. */
9567 if (multibyte
9568 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9569 {
9570 ptrdiff_t i;
9571 int c, char_bytes;
9572 char work[1];
9573
9574 /* Convert a multibyte string to single-byte
9575 for the *Message* buffer. */
9576 for (i = 0; i < nbytes; i += char_bytes)
9577 {
9578 c = string_char_and_length (msg + i, &char_bytes);
9579 work[0] = (ASCII_CHAR_P (c)
9580 ? c
9581 : multibyte_char_to_unibyte (c));
9582 insert_1_both (work, 1, 1, 1, 0, 0);
9583 }
9584 }
9585 else if (! multibyte
9586 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9587 {
9588 ptrdiff_t i;
9589 int c, char_bytes;
9590 unsigned char str[MAX_MULTIBYTE_LENGTH];
9591 /* Convert a single-byte string to multibyte
9592 for the *Message* buffer. */
9593 for (i = 0; i < nbytes; i++)
9594 {
9595 c = msg[i];
9596 MAKE_CHAR_MULTIBYTE (c);
9597 char_bytes = CHAR_STRING (c, str);
9598 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9599 }
9600 }
9601 else if (nbytes)
9602 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9603
9604 if (nlflag)
9605 {
9606 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9607 printmax_t dups;
9608
9609 insert_1_both ("\n", 1, 1, 1, 0, 0);
9610
9611 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9612 this_bol = PT;
9613 this_bol_byte = PT_BYTE;
9614
9615 /* See if this line duplicates the previous one.
9616 If so, combine duplicates. */
9617 if (this_bol > BEG)
9618 {
9619 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9620 prev_bol = PT;
9621 prev_bol_byte = PT_BYTE;
9622
9623 dups = message_log_check_duplicate (prev_bol_byte,
9624 this_bol_byte);
9625 if (dups)
9626 {
9627 del_range_both (prev_bol, prev_bol_byte,
9628 this_bol, this_bol_byte, 0);
9629 if (dups > 1)
9630 {
9631 char dupstr[sizeof " [ times]"
9632 + INT_STRLEN_BOUND (printmax_t)];
9633
9634 /* If you change this format, don't forget to also
9635 change message_log_check_duplicate. */
9636 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9637 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9638 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9639 }
9640 }
9641 }
9642
9643 /* If we have more than the desired maximum number of lines
9644 in the *Messages* buffer now, delete the oldest ones.
9645 This is safe because we don't have undo in this buffer. */
9646
9647 if (NATNUMP (Vmessage_log_max))
9648 {
9649 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9650 -XFASTINT (Vmessage_log_max) - 1, 0);
9651 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9652 }
9653 }
9654 BEGV = marker_position (oldbegv);
9655 BEGV_BYTE = marker_byte_position (oldbegv);
9656
9657 if (zv_at_end)
9658 {
9659 ZV = Z;
9660 ZV_BYTE = Z_BYTE;
9661 }
9662 else
9663 {
9664 ZV = marker_position (oldzv);
9665 ZV_BYTE = marker_byte_position (oldzv);
9666 }
9667
9668 if (point_at_end)
9669 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9670 else
9671 /* We can't do Fgoto_char (oldpoint) because it will run some
9672 Lisp code. */
9673 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9674 marker_byte_position (oldpoint));
9675
9676 UNGCPRO;
9677 unchain_marker (XMARKER (oldpoint));
9678 unchain_marker (XMARKER (oldbegv));
9679 unchain_marker (XMARKER (oldzv));
9680
9681 shown = buffer_window_count (current_buffer) > 0;
9682 set_buffer_internal (oldbuf);
9683 /* We called insert_1_both above with its 5th argument (PREPARE)
9684 zero, which prevents insert_1_both from calling
9685 prepare_to_modify_buffer, which in turns prevents us from
9686 incrementing windows_or_buffers_changed even if *Messages* is
9687 shown in some window. So we must manually incrementing
9688 windows_or_buffers_changed here to make up for that. */
9689 if (shown)
9690 windows_or_buffers_changed++;
9691 else
9692 windows_or_buffers_changed = old_windows_or_buffers_changed;
9693 message_log_need_newline = !nlflag;
9694 Vdeactivate_mark = old_deactivate_mark;
9695 }
9696 }
9697
9698
9699 /* We are at the end of the buffer after just having inserted a newline.
9700 (Note: We depend on the fact we won't be crossing the gap.)
9701 Check to see if the most recent message looks a lot like the previous one.
9702 Return 0 if different, 1 if the new one should just replace it, or a
9703 value N > 1 if we should also append " [N times]". */
9704
9705 static intmax_t
9706 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9707 {
9708 ptrdiff_t i;
9709 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9710 int seen_dots = 0;
9711 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9712 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9713
9714 for (i = 0; i < len; i++)
9715 {
9716 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
9717 seen_dots = 1;
9718 if (p1[i] != p2[i])
9719 return seen_dots;
9720 }
9721 p1 += len;
9722 if (*p1 == '\n')
9723 return 2;
9724 if (*p1++ == ' ' && *p1++ == '[')
9725 {
9726 char *pend;
9727 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9728 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9729 return n + 1;
9730 }
9731 return 0;
9732 }
9733 \f
9734
9735 /* Display an echo area message M with a specified length of NBYTES
9736 bytes. The string may include null characters. If M is not a
9737 string, clear out any existing message, and let the mini-buffer
9738 text show through.
9739
9740 This function cancels echoing. */
9741
9742 void
9743 message3 (Lisp_Object m)
9744 {
9745 struct gcpro gcpro1;
9746
9747 GCPRO1 (m);
9748 clear_message (1,1);
9749 cancel_echoing ();
9750
9751 /* First flush out any partial line written with print. */
9752 message_log_maybe_newline ();
9753 if (STRINGP (m))
9754 {
9755 ptrdiff_t nbytes = SBYTES (m);
9756 bool multibyte = STRING_MULTIBYTE (m);
9757 USE_SAFE_ALLOCA;
9758 char *buffer = SAFE_ALLOCA (nbytes);
9759 memcpy (buffer, SDATA (m), nbytes);
9760 message_dolog (buffer, nbytes, 1, multibyte);
9761 SAFE_FREE ();
9762 }
9763 message3_nolog (m);
9764
9765 UNGCPRO;
9766 }
9767
9768
9769 /* The non-logging version of message3.
9770 This does not cancel echoing, because it is used for echoing.
9771 Perhaps we need to make a separate function for echoing
9772 and make this cancel echoing. */
9773
9774 void
9775 message3_nolog (Lisp_Object m)
9776 {
9777 struct frame *sf = SELECTED_FRAME ();
9778
9779 if (FRAME_INITIAL_P (sf))
9780 {
9781 if (noninteractive_need_newline)
9782 putc ('\n', stderr);
9783 noninteractive_need_newline = 0;
9784 if (STRINGP (m))
9785 fwrite (SDATA (m), SBYTES (m), 1, stderr);
9786 if (cursor_in_echo_area == 0)
9787 fprintf (stderr, "\n");
9788 fflush (stderr);
9789 }
9790 /* Error messages get reported properly by cmd_error, so this must be just an
9791 informative message; if the frame hasn't really been initialized yet, just
9792 toss it. */
9793 else if (INTERACTIVE && sf->glyphs_initialized_p)
9794 {
9795 /* Get the frame containing the mini-buffer
9796 that the selected frame is using. */
9797 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
9798 Lisp_Object frame = XWINDOW (mini_window)->frame;
9799 struct frame *f = XFRAME (frame);
9800
9801 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
9802 Fmake_frame_visible (frame);
9803
9804 if (STRINGP (m) && SCHARS (m) > 0)
9805 {
9806 set_message (m);
9807 if (minibuffer_auto_raise)
9808 Fraise_frame (frame);
9809 /* Assume we are not echoing.
9810 (If we are, echo_now will override this.) */
9811 echo_message_buffer = Qnil;
9812 }
9813 else
9814 clear_message (1, 1);
9815
9816 do_pending_window_change (0);
9817 echo_area_display (1);
9818 do_pending_window_change (0);
9819 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9820 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9821 }
9822 }
9823
9824
9825 /* Display a null-terminated echo area message M. If M is 0, clear
9826 out any existing message, and let the mini-buffer text show through.
9827
9828 The buffer M must continue to exist until after the echo area gets
9829 cleared or some other message gets displayed there. Do not pass
9830 text that is stored in a Lisp string. Do not pass text in a buffer
9831 that was alloca'd. */
9832
9833 void
9834 message1 (const char *m)
9835 {
9836 message3 (m ? build_unibyte_string (m) : Qnil);
9837 }
9838
9839
9840 /* The non-logging counterpart of message1. */
9841
9842 void
9843 message1_nolog (const char *m)
9844 {
9845 message3_nolog (m ? build_unibyte_string (m) : Qnil);
9846 }
9847
9848 /* Display a message M which contains a single %s
9849 which gets replaced with STRING. */
9850
9851 void
9852 message_with_string (const char *m, Lisp_Object string, int log)
9853 {
9854 CHECK_STRING (string);
9855
9856 if (noninteractive)
9857 {
9858 if (m)
9859 {
9860 if (noninteractive_need_newline)
9861 putc ('\n', stderr);
9862 noninteractive_need_newline = 0;
9863 fprintf (stderr, m, SDATA (string));
9864 if (!cursor_in_echo_area)
9865 fprintf (stderr, "\n");
9866 fflush (stderr);
9867 }
9868 }
9869 else if (INTERACTIVE)
9870 {
9871 /* The frame whose minibuffer we're going to display the message on.
9872 It may be larger than the selected frame, so we need
9873 to use its buffer, not the selected frame's buffer. */
9874 Lisp_Object mini_window;
9875 struct frame *f, *sf = SELECTED_FRAME ();
9876
9877 /* Get the frame containing the minibuffer
9878 that the selected frame is using. */
9879 mini_window = FRAME_MINIBUF_WINDOW (sf);
9880 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9881
9882 /* Error messages get reported properly by cmd_error, so this must be
9883 just an informative message; if the frame hasn't really been
9884 initialized yet, just toss it. */
9885 if (f->glyphs_initialized_p)
9886 {
9887 Lisp_Object args[2], msg;
9888 struct gcpro gcpro1, gcpro2;
9889
9890 args[0] = build_string (m);
9891 args[1] = msg = string;
9892 GCPRO2 (args[0], msg);
9893 gcpro1.nvars = 2;
9894
9895 msg = Fformat (2, args);
9896
9897 if (log)
9898 message3 (msg);
9899 else
9900 message3_nolog (msg);
9901
9902 UNGCPRO;
9903
9904 /* Print should start at the beginning of the message
9905 buffer next time. */
9906 message_buf_print = 0;
9907 }
9908 }
9909 }
9910
9911
9912 /* Dump an informative message to the minibuf. If M is 0, clear out
9913 any existing message, and let the mini-buffer text show through. */
9914
9915 static void
9916 vmessage (const char *m, va_list ap)
9917 {
9918 if (noninteractive)
9919 {
9920 if (m)
9921 {
9922 if (noninteractive_need_newline)
9923 putc ('\n', stderr);
9924 noninteractive_need_newline = 0;
9925 vfprintf (stderr, m, ap);
9926 if (cursor_in_echo_area == 0)
9927 fprintf (stderr, "\n");
9928 fflush (stderr);
9929 }
9930 }
9931 else if (INTERACTIVE)
9932 {
9933 /* The frame whose mini-buffer we're going to display the message
9934 on. It may be larger than the selected frame, so we need to
9935 use its buffer, not the selected frame's buffer. */
9936 Lisp_Object mini_window;
9937 struct frame *f, *sf = SELECTED_FRAME ();
9938
9939 /* Get the frame containing the mini-buffer
9940 that the selected frame is using. */
9941 mini_window = FRAME_MINIBUF_WINDOW (sf);
9942 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9943
9944 /* Error messages get reported properly by cmd_error, so this must be
9945 just an informative message; if the frame hasn't really been
9946 initialized yet, just toss it. */
9947 if (f->glyphs_initialized_p)
9948 {
9949 if (m)
9950 {
9951 ptrdiff_t len;
9952 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
9953 char *message_buf = alloca (maxsize + 1);
9954
9955 len = doprnt (message_buf, maxsize, m, 0, ap);
9956
9957 message3 (make_string (message_buf, len));
9958 }
9959 else
9960 message1 (0);
9961
9962 /* Print should start at the beginning of the message
9963 buffer next time. */
9964 message_buf_print = 0;
9965 }
9966 }
9967 }
9968
9969 void
9970 message (const char *m, ...)
9971 {
9972 va_list ap;
9973 va_start (ap, m);
9974 vmessage (m, ap);
9975 va_end (ap);
9976 }
9977
9978
9979 #if 0
9980 /* The non-logging version of message. */
9981
9982 void
9983 message_nolog (const char *m, ...)
9984 {
9985 Lisp_Object old_log_max;
9986 va_list ap;
9987 va_start (ap, m);
9988 old_log_max = Vmessage_log_max;
9989 Vmessage_log_max = Qnil;
9990 vmessage (m, ap);
9991 Vmessage_log_max = old_log_max;
9992 va_end (ap);
9993 }
9994 #endif
9995
9996
9997 /* Display the current message in the current mini-buffer. This is
9998 only called from error handlers in process.c, and is not time
9999 critical. */
10000
10001 void
10002 update_echo_area (void)
10003 {
10004 if (!NILP (echo_area_buffer[0]))
10005 {
10006 Lisp_Object string;
10007 string = Fcurrent_message ();
10008 message3 (string);
10009 }
10010 }
10011
10012
10013 /* Make sure echo area buffers in `echo_buffers' are live.
10014 If they aren't, make new ones. */
10015
10016 static void
10017 ensure_echo_area_buffers (void)
10018 {
10019 int i;
10020
10021 for (i = 0; i < 2; ++i)
10022 if (!BUFFERP (echo_buffer[i])
10023 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10024 {
10025 char name[30];
10026 Lisp_Object old_buffer;
10027 int j;
10028
10029 old_buffer = echo_buffer[i];
10030 echo_buffer[i] = Fget_buffer_create
10031 (make_formatted_string (name, " *Echo Area %d*", i));
10032 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10033 /* to force word wrap in echo area -
10034 it was decided to postpone this*/
10035 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10036
10037 for (j = 0; j < 2; ++j)
10038 if (EQ (old_buffer, echo_area_buffer[j]))
10039 echo_area_buffer[j] = echo_buffer[i];
10040 }
10041 }
10042
10043
10044 /* Call FN with args A1..A2 with either the current or last displayed
10045 echo_area_buffer as current buffer.
10046
10047 WHICH zero means use the current message buffer
10048 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10049 from echo_buffer[] and clear it.
10050
10051 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10052 suitable buffer from echo_buffer[] and clear it.
10053
10054 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10055 that the current message becomes the last displayed one, make
10056 choose a suitable buffer for echo_area_buffer[0], and clear it.
10057
10058 Value is what FN returns. */
10059
10060 static int
10061 with_echo_area_buffer (struct window *w, int which,
10062 int (*fn) (ptrdiff_t, Lisp_Object),
10063 ptrdiff_t a1, Lisp_Object a2)
10064 {
10065 Lisp_Object buffer;
10066 int this_one, the_other, clear_buffer_p, rc;
10067 ptrdiff_t count = SPECPDL_INDEX ();
10068
10069 /* If buffers aren't live, make new ones. */
10070 ensure_echo_area_buffers ();
10071
10072 clear_buffer_p = 0;
10073
10074 if (which == 0)
10075 this_one = 0, the_other = 1;
10076 else if (which > 0)
10077 this_one = 1, the_other = 0;
10078 else
10079 {
10080 this_one = 0, the_other = 1;
10081 clear_buffer_p = 1;
10082
10083 /* We need a fresh one in case the current echo buffer equals
10084 the one containing the last displayed echo area message. */
10085 if (!NILP (echo_area_buffer[this_one])
10086 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10087 echo_area_buffer[this_one] = Qnil;
10088 }
10089
10090 /* Choose a suitable buffer from echo_buffer[] is we don't
10091 have one. */
10092 if (NILP (echo_area_buffer[this_one]))
10093 {
10094 echo_area_buffer[this_one]
10095 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10096 ? echo_buffer[the_other]
10097 : echo_buffer[this_one]);
10098 clear_buffer_p = 1;
10099 }
10100
10101 buffer = echo_area_buffer[this_one];
10102
10103 /* Don't get confused by reusing the buffer used for echoing
10104 for a different purpose. */
10105 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10106 cancel_echoing ();
10107
10108 record_unwind_protect (unwind_with_echo_area_buffer,
10109 with_echo_area_buffer_unwind_data (w));
10110
10111 /* Make the echo area buffer current. Note that for display
10112 purposes, it is not necessary that the displayed window's buffer
10113 == current_buffer, except for text property lookup. So, let's
10114 only set that buffer temporarily here without doing a full
10115 Fset_window_buffer. We must also change w->pointm, though,
10116 because otherwise an assertions in unshow_buffer fails, and Emacs
10117 aborts. */
10118 set_buffer_internal_1 (XBUFFER (buffer));
10119 if (w)
10120 {
10121 wset_buffer (w, buffer);
10122 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10123 }
10124
10125 bset_undo_list (current_buffer, Qt);
10126 bset_read_only (current_buffer, Qnil);
10127 specbind (Qinhibit_read_only, Qt);
10128 specbind (Qinhibit_modification_hooks, Qt);
10129
10130 if (clear_buffer_p && Z > BEG)
10131 del_range (BEG, Z);
10132
10133 eassert (BEGV >= BEG);
10134 eassert (ZV <= Z && ZV >= BEGV);
10135
10136 rc = fn (a1, a2);
10137
10138 eassert (BEGV >= BEG);
10139 eassert (ZV <= Z && ZV >= BEGV);
10140
10141 unbind_to (count, Qnil);
10142 return rc;
10143 }
10144
10145
10146 /* Save state that should be preserved around the call to the function
10147 FN called in with_echo_area_buffer. */
10148
10149 static Lisp_Object
10150 with_echo_area_buffer_unwind_data (struct window *w)
10151 {
10152 int i = 0;
10153 Lisp_Object vector, tmp;
10154
10155 /* Reduce consing by keeping one vector in
10156 Vwith_echo_area_save_vector. */
10157 vector = Vwith_echo_area_save_vector;
10158 Vwith_echo_area_save_vector = Qnil;
10159
10160 if (NILP (vector))
10161 vector = Fmake_vector (make_number (9), Qnil);
10162
10163 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10164 ASET (vector, i, Vdeactivate_mark); ++i;
10165 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10166
10167 if (w)
10168 {
10169 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10170 ASET (vector, i, w->contents); ++i;
10171 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10172 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10173 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10174 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10175 }
10176 else
10177 {
10178 int end = i + 6;
10179 for (; i < end; ++i)
10180 ASET (vector, i, Qnil);
10181 }
10182
10183 eassert (i == ASIZE (vector));
10184 return vector;
10185 }
10186
10187
10188 /* Restore global state from VECTOR which was created by
10189 with_echo_area_buffer_unwind_data. */
10190
10191 static void
10192 unwind_with_echo_area_buffer (Lisp_Object vector)
10193 {
10194 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10195 Vdeactivate_mark = AREF (vector, 1);
10196 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10197
10198 if (WINDOWP (AREF (vector, 3)))
10199 {
10200 struct window *w;
10201 Lisp_Object buffer;
10202
10203 w = XWINDOW (AREF (vector, 3));
10204 buffer = AREF (vector, 4);
10205
10206 wset_buffer (w, buffer);
10207 set_marker_both (w->pointm, buffer,
10208 XFASTINT (AREF (vector, 5)),
10209 XFASTINT (AREF (vector, 6)));
10210 set_marker_both (w->start, buffer,
10211 XFASTINT (AREF (vector, 7)),
10212 XFASTINT (AREF (vector, 8)));
10213 }
10214
10215 Vwith_echo_area_save_vector = vector;
10216 }
10217
10218
10219 /* Set up the echo area for use by print functions. MULTIBYTE_P
10220 non-zero means we will print multibyte. */
10221
10222 void
10223 setup_echo_area_for_printing (int multibyte_p)
10224 {
10225 /* If we can't find an echo area any more, exit. */
10226 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10227 Fkill_emacs (Qnil);
10228
10229 ensure_echo_area_buffers ();
10230
10231 if (!message_buf_print)
10232 {
10233 /* A message has been output since the last time we printed.
10234 Choose a fresh echo area buffer. */
10235 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10236 echo_area_buffer[0] = echo_buffer[1];
10237 else
10238 echo_area_buffer[0] = echo_buffer[0];
10239
10240 /* Switch to that buffer and clear it. */
10241 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10242 bset_truncate_lines (current_buffer, Qnil);
10243
10244 if (Z > BEG)
10245 {
10246 ptrdiff_t count = SPECPDL_INDEX ();
10247 specbind (Qinhibit_read_only, Qt);
10248 /* Note that undo recording is always disabled. */
10249 del_range (BEG, Z);
10250 unbind_to (count, Qnil);
10251 }
10252 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10253
10254 /* Set up the buffer for the multibyteness we need. */
10255 if (multibyte_p
10256 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10257 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10258
10259 /* Raise the frame containing the echo area. */
10260 if (minibuffer_auto_raise)
10261 {
10262 struct frame *sf = SELECTED_FRAME ();
10263 Lisp_Object mini_window;
10264 mini_window = FRAME_MINIBUF_WINDOW (sf);
10265 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10266 }
10267
10268 message_log_maybe_newline ();
10269 message_buf_print = 1;
10270 }
10271 else
10272 {
10273 if (NILP (echo_area_buffer[0]))
10274 {
10275 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10276 echo_area_buffer[0] = echo_buffer[1];
10277 else
10278 echo_area_buffer[0] = echo_buffer[0];
10279 }
10280
10281 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10282 {
10283 /* Someone switched buffers between print requests. */
10284 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10285 bset_truncate_lines (current_buffer, Qnil);
10286 }
10287 }
10288 }
10289
10290
10291 /* Display an echo area message in window W. Value is non-zero if W's
10292 height is changed. If display_last_displayed_message_p is
10293 non-zero, display the message that was last displayed, otherwise
10294 display the current message. */
10295
10296 static int
10297 display_echo_area (struct window *w)
10298 {
10299 int i, no_message_p, window_height_changed_p;
10300
10301 /* Temporarily disable garbage collections while displaying the echo
10302 area. This is done because a GC can print a message itself.
10303 That message would modify the echo area buffer's contents while a
10304 redisplay of the buffer is going on, and seriously confuse
10305 redisplay. */
10306 ptrdiff_t count = inhibit_garbage_collection ();
10307
10308 /* If there is no message, we must call display_echo_area_1
10309 nevertheless because it resizes the window. But we will have to
10310 reset the echo_area_buffer in question to nil at the end because
10311 with_echo_area_buffer will sets it to an empty buffer. */
10312 i = display_last_displayed_message_p ? 1 : 0;
10313 no_message_p = NILP (echo_area_buffer[i]);
10314
10315 window_height_changed_p
10316 = with_echo_area_buffer (w, display_last_displayed_message_p,
10317 display_echo_area_1,
10318 (intptr_t) w, Qnil);
10319
10320 if (no_message_p)
10321 echo_area_buffer[i] = Qnil;
10322
10323 unbind_to (count, Qnil);
10324 return window_height_changed_p;
10325 }
10326
10327
10328 /* Helper for display_echo_area. Display the current buffer which
10329 contains the current echo area message in window W, a mini-window,
10330 a pointer to which is passed in A1. A2..A4 are currently not used.
10331 Change the height of W so that all of the message is displayed.
10332 Value is non-zero if height of W was changed. */
10333
10334 static int
10335 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10336 {
10337 intptr_t i1 = a1;
10338 struct window *w = (struct window *) i1;
10339 Lisp_Object window;
10340 struct text_pos start;
10341 int window_height_changed_p = 0;
10342
10343 /* Do this before displaying, so that we have a large enough glyph
10344 matrix for the display. If we can't get enough space for the
10345 whole text, display the last N lines. That works by setting w->start. */
10346 window_height_changed_p = resize_mini_window (w, 0);
10347
10348 /* Use the starting position chosen by resize_mini_window. */
10349 SET_TEXT_POS_FROM_MARKER (start, w->start);
10350
10351 /* Display. */
10352 clear_glyph_matrix (w->desired_matrix);
10353 XSETWINDOW (window, w);
10354 try_window (window, start, 0);
10355
10356 return window_height_changed_p;
10357 }
10358
10359
10360 /* Resize the echo area window to exactly the size needed for the
10361 currently displayed message, if there is one. If a mini-buffer
10362 is active, don't shrink it. */
10363
10364 void
10365 resize_echo_area_exactly (void)
10366 {
10367 if (BUFFERP (echo_area_buffer[0])
10368 && WINDOWP (echo_area_window))
10369 {
10370 struct window *w = XWINDOW (echo_area_window);
10371 int resized_p;
10372 Lisp_Object resize_exactly;
10373
10374 if (minibuf_level == 0)
10375 resize_exactly = Qt;
10376 else
10377 resize_exactly = Qnil;
10378
10379 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10380 (intptr_t) w, resize_exactly);
10381 if (resized_p)
10382 {
10383 ++windows_or_buffers_changed;
10384 ++update_mode_lines;
10385 redisplay_internal ();
10386 }
10387 }
10388 }
10389
10390
10391 /* Callback function for with_echo_area_buffer, when used from
10392 resize_echo_area_exactly. A1 contains a pointer to the window to
10393 resize, EXACTLY non-nil means resize the mini-window exactly to the
10394 size of the text displayed. A3 and A4 are not used. Value is what
10395 resize_mini_window returns. */
10396
10397 static int
10398 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10399 {
10400 intptr_t i1 = a1;
10401 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10402 }
10403
10404
10405 /* Resize mini-window W to fit the size of its contents. EXACT_P
10406 means size the window exactly to the size needed. Otherwise, it's
10407 only enlarged until W's buffer is empty.
10408
10409 Set W->start to the right place to begin display. If the whole
10410 contents fit, start at the beginning. Otherwise, start so as
10411 to make the end of the contents appear. This is particularly
10412 important for y-or-n-p, but seems desirable generally.
10413
10414 Value is non-zero if the window height has been changed. */
10415
10416 int
10417 resize_mini_window (struct window *w, int exact_p)
10418 {
10419 struct frame *f = XFRAME (w->frame);
10420 int window_height_changed_p = 0;
10421
10422 eassert (MINI_WINDOW_P (w));
10423
10424 /* By default, start display at the beginning. */
10425 set_marker_both (w->start, w->contents,
10426 BUF_BEGV (XBUFFER (w->contents)),
10427 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10428
10429 /* Don't resize windows while redisplaying a window; it would
10430 confuse redisplay functions when the size of the window they are
10431 displaying changes from under them. Such a resizing can happen,
10432 for instance, when which-func prints a long message while
10433 we are running fontification-functions. We're running these
10434 functions with safe_call which binds inhibit-redisplay to t. */
10435 if (!NILP (Vinhibit_redisplay))
10436 return 0;
10437
10438 /* Nil means don't try to resize. */
10439 if (NILP (Vresize_mini_windows)
10440 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10441 return 0;
10442
10443 if (!FRAME_MINIBUF_ONLY_P (f))
10444 {
10445 struct it it;
10446 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10447 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10448 int height;
10449 EMACS_INT max_height;
10450 int unit = FRAME_LINE_HEIGHT (f);
10451 struct text_pos start;
10452 struct buffer *old_current_buffer = NULL;
10453
10454 if (current_buffer != XBUFFER (w->contents))
10455 {
10456 old_current_buffer = current_buffer;
10457 set_buffer_internal (XBUFFER (w->contents));
10458 }
10459
10460 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10461
10462 /* Compute the max. number of lines specified by the user. */
10463 if (FLOATP (Vmax_mini_window_height))
10464 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10465 else if (INTEGERP (Vmax_mini_window_height))
10466 max_height = XINT (Vmax_mini_window_height);
10467 else
10468 max_height = total_height / 4;
10469
10470 /* Correct that max. height if it's bogus. */
10471 max_height = clip_to_bounds (1, max_height, total_height);
10472
10473 /* Find out the height of the text in the window. */
10474 if (it.line_wrap == TRUNCATE)
10475 height = 1;
10476 else
10477 {
10478 last_height = 0;
10479 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10480 if (it.max_ascent == 0 && it.max_descent == 0)
10481 height = it.current_y + last_height;
10482 else
10483 height = it.current_y + it.max_ascent + it.max_descent;
10484 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10485 height = (height + unit - 1) / unit;
10486 }
10487
10488 /* Compute a suitable window start. */
10489 if (height > max_height)
10490 {
10491 height = max_height;
10492 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10493 move_it_vertically_backward (&it, (height - 1) * unit);
10494 start = it.current.pos;
10495 }
10496 else
10497 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10498 SET_MARKER_FROM_TEXT_POS (w->start, start);
10499
10500 if (EQ (Vresize_mini_windows, Qgrow_only))
10501 {
10502 /* Let it grow only, until we display an empty message, in which
10503 case the window shrinks again. */
10504 if (height > WINDOW_TOTAL_LINES (w))
10505 {
10506 int old_height = WINDOW_TOTAL_LINES (w);
10507
10508 FRAME_WINDOWS_FROZEN (f) = 1;
10509 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10510 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10511 }
10512 else if (height < WINDOW_TOTAL_LINES (w)
10513 && (exact_p || BEGV == ZV))
10514 {
10515 int old_height = WINDOW_TOTAL_LINES (w);
10516
10517 FRAME_WINDOWS_FROZEN (f) = 0;
10518 shrink_mini_window (w);
10519 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10520 }
10521 }
10522 else
10523 {
10524 /* Always resize to exact size needed. */
10525 if (height > WINDOW_TOTAL_LINES (w))
10526 {
10527 int old_height = WINDOW_TOTAL_LINES (w);
10528
10529 FRAME_WINDOWS_FROZEN (f) = 1;
10530 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10531 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10532 }
10533 else if (height < WINDOW_TOTAL_LINES (w))
10534 {
10535 int old_height = WINDOW_TOTAL_LINES (w);
10536
10537 FRAME_WINDOWS_FROZEN (f) = 0;
10538 shrink_mini_window (w);
10539
10540 if (height)
10541 {
10542 FRAME_WINDOWS_FROZEN (f) = 1;
10543 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10544 }
10545
10546 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10547 }
10548 }
10549
10550 if (old_current_buffer)
10551 set_buffer_internal (old_current_buffer);
10552 }
10553
10554 return window_height_changed_p;
10555 }
10556
10557
10558 /* Value is the current message, a string, or nil if there is no
10559 current message. */
10560
10561 Lisp_Object
10562 current_message (void)
10563 {
10564 Lisp_Object msg;
10565
10566 if (!BUFFERP (echo_area_buffer[0]))
10567 msg = Qnil;
10568 else
10569 {
10570 with_echo_area_buffer (0, 0, current_message_1,
10571 (intptr_t) &msg, Qnil);
10572 if (NILP (msg))
10573 echo_area_buffer[0] = Qnil;
10574 }
10575
10576 return msg;
10577 }
10578
10579
10580 static int
10581 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10582 {
10583 intptr_t i1 = a1;
10584 Lisp_Object *msg = (Lisp_Object *) i1;
10585
10586 if (Z > BEG)
10587 *msg = make_buffer_string (BEG, Z, 1);
10588 else
10589 *msg = Qnil;
10590 return 0;
10591 }
10592
10593
10594 /* Push the current message on Vmessage_stack for later restoration
10595 by restore_message. Value is non-zero if the current message isn't
10596 empty. This is a relatively infrequent operation, so it's not
10597 worth optimizing. */
10598
10599 bool
10600 push_message (void)
10601 {
10602 Lisp_Object msg = current_message ();
10603 Vmessage_stack = Fcons (msg, Vmessage_stack);
10604 return STRINGP (msg);
10605 }
10606
10607
10608 /* Restore message display from the top of Vmessage_stack. */
10609
10610 void
10611 restore_message (void)
10612 {
10613 eassert (CONSP (Vmessage_stack));
10614 message3_nolog (XCAR (Vmessage_stack));
10615 }
10616
10617
10618 /* Handler for unwind-protect calling pop_message. */
10619
10620 void
10621 pop_message_unwind (void)
10622 {
10623 /* Pop the top-most entry off Vmessage_stack. */
10624 eassert (CONSP (Vmessage_stack));
10625 Vmessage_stack = XCDR (Vmessage_stack);
10626 }
10627
10628
10629 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10630 exits. If the stack is not empty, we have a missing pop_message
10631 somewhere. */
10632
10633 void
10634 check_message_stack (void)
10635 {
10636 if (!NILP (Vmessage_stack))
10637 emacs_abort ();
10638 }
10639
10640
10641 /* Truncate to NCHARS what will be displayed in the echo area the next
10642 time we display it---but don't redisplay it now. */
10643
10644 void
10645 truncate_echo_area (ptrdiff_t nchars)
10646 {
10647 if (nchars == 0)
10648 echo_area_buffer[0] = Qnil;
10649 else if (!noninteractive
10650 && INTERACTIVE
10651 && !NILP (echo_area_buffer[0]))
10652 {
10653 struct frame *sf = SELECTED_FRAME ();
10654 /* Error messages get reported properly by cmd_error, so this must be
10655 just an informative message; if the frame hasn't really been
10656 initialized yet, just toss it. */
10657 if (sf->glyphs_initialized_p)
10658 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10659 }
10660 }
10661
10662
10663 /* Helper function for truncate_echo_area. Truncate the current
10664 message to at most NCHARS characters. */
10665
10666 static int
10667 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10668 {
10669 if (BEG + nchars < Z)
10670 del_range (BEG + nchars, Z);
10671 if (Z == BEG)
10672 echo_area_buffer[0] = Qnil;
10673 return 0;
10674 }
10675
10676 /* Set the current message to STRING. */
10677
10678 static void
10679 set_message (Lisp_Object string)
10680 {
10681 eassert (STRINGP (string));
10682
10683 message_enable_multibyte = STRING_MULTIBYTE (string);
10684
10685 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10686 message_buf_print = 0;
10687 help_echo_showing_p = 0;
10688
10689 if (STRINGP (Vdebug_on_message)
10690 && STRINGP (string)
10691 && fast_string_match (Vdebug_on_message, string) >= 0)
10692 call_debugger (list2 (Qerror, string));
10693 }
10694
10695
10696 /* Helper function for set_message. First argument is ignored and second
10697 argument has the same meaning as for set_message.
10698 This function is called with the echo area buffer being current. */
10699
10700 static int
10701 set_message_1 (ptrdiff_t a1, Lisp_Object string)
10702 {
10703 eassert (STRINGP (string));
10704
10705 /* Change multibyteness of the echo buffer appropriately. */
10706 if (message_enable_multibyte
10707 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10708 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10709
10710 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10711 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10712 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10713
10714 /* Insert new message at BEG. */
10715 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10716
10717 /* This function takes care of single/multibyte conversion.
10718 We just have to ensure that the echo area buffer has the right
10719 setting of enable_multibyte_characters. */
10720 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
10721
10722 return 0;
10723 }
10724
10725
10726 /* Clear messages. CURRENT_P non-zero means clear the current
10727 message. LAST_DISPLAYED_P non-zero means clear the message
10728 last displayed. */
10729
10730 void
10731 clear_message (int current_p, int last_displayed_p)
10732 {
10733 if (current_p)
10734 {
10735 echo_area_buffer[0] = Qnil;
10736 message_cleared_p = 1;
10737 }
10738
10739 if (last_displayed_p)
10740 echo_area_buffer[1] = Qnil;
10741
10742 message_buf_print = 0;
10743 }
10744
10745 /* Clear garbaged frames.
10746
10747 This function is used where the old redisplay called
10748 redraw_garbaged_frames which in turn called redraw_frame which in
10749 turn called clear_frame. The call to clear_frame was a source of
10750 flickering. I believe a clear_frame is not necessary. It should
10751 suffice in the new redisplay to invalidate all current matrices,
10752 and ensure a complete redisplay of all windows. */
10753
10754 static void
10755 clear_garbaged_frames (void)
10756 {
10757 if (frame_garbaged)
10758 {
10759 Lisp_Object tail, frame;
10760 int changed_count = 0;
10761
10762 FOR_EACH_FRAME (tail, frame)
10763 {
10764 struct frame *f = XFRAME (frame);
10765
10766 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10767 {
10768 if (f->resized_p)
10769 {
10770 redraw_frame (f);
10771 f->force_flush_display_p = 1;
10772 }
10773 clear_current_matrices (f);
10774 changed_count++;
10775 f->garbaged = 0;
10776 f->resized_p = 0;
10777 }
10778 }
10779
10780 frame_garbaged = 0;
10781 if (changed_count)
10782 ++windows_or_buffers_changed;
10783 }
10784 }
10785
10786
10787 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10788 is non-zero update selected_frame. Value is non-zero if the
10789 mini-windows height has been changed. */
10790
10791 static int
10792 echo_area_display (int update_frame_p)
10793 {
10794 Lisp_Object mini_window;
10795 struct window *w;
10796 struct frame *f;
10797 int window_height_changed_p = 0;
10798 struct frame *sf = SELECTED_FRAME ();
10799
10800 mini_window = FRAME_MINIBUF_WINDOW (sf);
10801 w = XWINDOW (mini_window);
10802 f = XFRAME (WINDOW_FRAME (w));
10803
10804 /* Don't display if frame is invisible or not yet initialized. */
10805 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10806 return 0;
10807
10808 #ifdef HAVE_WINDOW_SYSTEM
10809 /* When Emacs starts, selected_frame may be the initial terminal
10810 frame. If we let this through, a message would be displayed on
10811 the terminal. */
10812 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10813 return 0;
10814 #endif /* HAVE_WINDOW_SYSTEM */
10815
10816 /* Redraw garbaged frames. */
10817 clear_garbaged_frames ();
10818
10819 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10820 {
10821 echo_area_window = mini_window;
10822 window_height_changed_p = display_echo_area (w);
10823 w->must_be_updated_p = 1;
10824
10825 /* Update the display, unless called from redisplay_internal.
10826 Also don't update the screen during redisplay itself. The
10827 update will happen at the end of redisplay, and an update
10828 here could cause confusion. */
10829 if (update_frame_p && !redisplaying_p)
10830 {
10831 int n = 0;
10832
10833 /* If the display update has been interrupted by pending
10834 input, update mode lines in the frame. Due to the
10835 pending input, it might have been that redisplay hasn't
10836 been called, so that mode lines above the echo area are
10837 garbaged. This looks odd, so we prevent it here. */
10838 if (!display_completed)
10839 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10840
10841 if (window_height_changed_p
10842 /* Don't do this if Emacs is shutting down. Redisplay
10843 needs to run hooks. */
10844 && !NILP (Vrun_hooks))
10845 {
10846 /* Must update other windows. Likewise as in other
10847 cases, don't let this update be interrupted by
10848 pending input. */
10849 ptrdiff_t count = SPECPDL_INDEX ();
10850 specbind (Qredisplay_dont_pause, Qt);
10851 windows_or_buffers_changed = 1;
10852 redisplay_internal ();
10853 unbind_to (count, Qnil);
10854 }
10855 else if (FRAME_WINDOW_P (f) && n == 0)
10856 {
10857 /* Window configuration is the same as before.
10858 Can do with a display update of the echo area,
10859 unless we displayed some mode lines. */
10860 update_single_window (w, 1);
10861 FRAME_RIF (f)->flush_display (f);
10862 }
10863 else
10864 update_frame (f, 1, 1);
10865
10866 /* If cursor is in the echo area, make sure that the next
10867 redisplay displays the minibuffer, so that the cursor will
10868 be replaced with what the minibuffer wants. */
10869 if (cursor_in_echo_area)
10870 ++windows_or_buffers_changed;
10871 }
10872 }
10873 else if (!EQ (mini_window, selected_window))
10874 windows_or_buffers_changed++;
10875
10876 /* Last displayed message is now the current message. */
10877 echo_area_buffer[1] = echo_area_buffer[0];
10878 /* Inform read_char that we're not echoing. */
10879 echo_message_buffer = Qnil;
10880
10881 /* Prevent redisplay optimization in redisplay_internal by resetting
10882 this_line_start_pos. This is done because the mini-buffer now
10883 displays the message instead of its buffer text. */
10884 if (EQ (mini_window, selected_window))
10885 CHARPOS (this_line_start_pos) = 0;
10886
10887 return window_height_changed_p;
10888 }
10889
10890 /* Nonzero if the current window's buffer is shown in more than one
10891 window and was modified since last redisplay. */
10892
10893 static int
10894 buffer_shared_and_changed (void)
10895 {
10896 return (buffer_window_count (current_buffer) > 1
10897 && UNCHANGED_MODIFIED < MODIFF);
10898 }
10899
10900 /* Nonzero if W's buffer was changed but not saved or Transient Mark mode
10901 is enabled and mark of W's buffer was changed since last W's update. */
10902
10903 static int
10904 window_buffer_changed (struct window *w)
10905 {
10906 struct buffer *b = XBUFFER (w->contents);
10907
10908 eassert (BUFFER_LIVE_P (b));
10909
10910 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star)
10911 || ((!NILP (Vtransient_mark_mode) && !NILP (BVAR (b, mark_active)))
10912 != (w->region_showing != 0)));
10913 }
10914
10915 /* Nonzero if W has %c in its mode line and mode line should be updated. */
10916
10917 static int
10918 mode_line_update_needed (struct window *w)
10919 {
10920 return (w->column_number_displayed != -1
10921 && !(PT == w->last_point && !window_outdated (w))
10922 && (w->column_number_displayed != current_column ()));
10923 }
10924
10925 /* Nonzero if window start of W is frozen and may not be changed during
10926 redisplay. */
10927
10928 static bool
10929 window_frozen_p (struct window *w)
10930 {
10931 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
10932 {
10933 Lisp_Object window;
10934
10935 XSETWINDOW (window, w);
10936 if (MINI_WINDOW_P (w))
10937 return 0;
10938 else if (EQ (window, selected_window))
10939 return 0;
10940 else if (MINI_WINDOW_P (XWINDOW (selected_window))
10941 && EQ (window, Vminibuf_scroll_window))
10942 /* This special window can't be frozen too. */
10943 return 0;
10944 else
10945 return 1;
10946 }
10947 return 0;
10948 }
10949
10950 /***********************************************************************
10951 Mode Lines and Frame Titles
10952 ***********************************************************************/
10953
10954 /* A buffer for constructing non-propertized mode-line strings and
10955 frame titles in it; allocated from the heap in init_xdisp and
10956 resized as needed in store_mode_line_noprop_char. */
10957
10958 static char *mode_line_noprop_buf;
10959
10960 /* The buffer's end, and a current output position in it. */
10961
10962 static char *mode_line_noprop_buf_end;
10963 static char *mode_line_noprop_ptr;
10964
10965 #define MODE_LINE_NOPROP_LEN(start) \
10966 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10967
10968 static enum {
10969 MODE_LINE_DISPLAY = 0,
10970 MODE_LINE_TITLE,
10971 MODE_LINE_NOPROP,
10972 MODE_LINE_STRING
10973 } mode_line_target;
10974
10975 /* Alist that caches the results of :propertize.
10976 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10977 static Lisp_Object mode_line_proptrans_alist;
10978
10979 /* List of strings making up the mode-line. */
10980 static Lisp_Object mode_line_string_list;
10981
10982 /* Base face property when building propertized mode line string. */
10983 static Lisp_Object mode_line_string_face;
10984 static Lisp_Object mode_line_string_face_prop;
10985
10986
10987 /* Unwind data for mode line strings */
10988
10989 static Lisp_Object Vmode_line_unwind_vector;
10990
10991 static Lisp_Object
10992 format_mode_line_unwind_data (struct frame *target_frame,
10993 struct buffer *obuf,
10994 Lisp_Object owin,
10995 int save_proptrans)
10996 {
10997 Lisp_Object vector, tmp;
10998
10999 /* Reduce consing by keeping one vector in
11000 Vwith_echo_area_save_vector. */
11001 vector = Vmode_line_unwind_vector;
11002 Vmode_line_unwind_vector = Qnil;
11003
11004 if (NILP (vector))
11005 vector = Fmake_vector (make_number (10), Qnil);
11006
11007 ASET (vector, 0, make_number (mode_line_target));
11008 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11009 ASET (vector, 2, mode_line_string_list);
11010 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11011 ASET (vector, 4, mode_line_string_face);
11012 ASET (vector, 5, mode_line_string_face_prop);
11013
11014 if (obuf)
11015 XSETBUFFER (tmp, obuf);
11016 else
11017 tmp = Qnil;
11018 ASET (vector, 6, tmp);
11019 ASET (vector, 7, owin);
11020 if (target_frame)
11021 {
11022 /* Similarly to `with-selected-window', if the operation selects
11023 a window on another frame, we must restore that frame's
11024 selected window, and (for a tty) the top-frame. */
11025 ASET (vector, 8, target_frame->selected_window);
11026 if (FRAME_TERMCAP_P (target_frame))
11027 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11028 }
11029
11030 return vector;
11031 }
11032
11033 static void
11034 unwind_format_mode_line (Lisp_Object vector)
11035 {
11036 Lisp_Object old_window = AREF (vector, 7);
11037 Lisp_Object target_frame_window = AREF (vector, 8);
11038 Lisp_Object old_top_frame = AREF (vector, 9);
11039
11040 mode_line_target = XINT (AREF (vector, 0));
11041 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11042 mode_line_string_list = AREF (vector, 2);
11043 if (! EQ (AREF (vector, 3), Qt))
11044 mode_line_proptrans_alist = AREF (vector, 3);
11045 mode_line_string_face = AREF (vector, 4);
11046 mode_line_string_face_prop = AREF (vector, 5);
11047
11048 /* Select window before buffer, since it may change the buffer. */
11049 if (!NILP (old_window))
11050 {
11051 /* If the operation that we are unwinding had selected a window
11052 on a different frame, reset its frame-selected-window. For a
11053 text terminal, reset its top-frame if necessary. */
11054 if (!NILP (target_frame_window))
11055 {
11056 Lisp_Object frame
11057 = WINDOW_FRAME (XWINDOW (target_frame_window));
11058
11059 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11060 Fselect_window (target_frame_window, Qt);
11061
11062 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11063 Fselect_frame (old_top_frame, Qt);
11064 }
11065
11066 Fselect_window (old_window, Qt);
11067 }
11068
11069 if (!NILP (AREF (vector, 6)))
11070 {
11071 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11072 ASET (vector, 6, Qnil);
11073 }
11074
11075 Vmode_line_unwind_vector = vector;
11076 }
11077
11078
11079 /* Store a single character C for the frame title in mode_line_noprop_buf.
11080 Re-allocate mode_line_noprop_buf if necessary. */
11081
11082 static void
11083 store_mode_line_noprop_char (char c)
11084 {
11085 /* If output position has reached the end of the allocated buffer,
11086 increase the buffer's size. */
11087 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11088 {
11089 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11090 ptrdiff_t size = len;
11091 mode_line_noprop_buf =
11092 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11093 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11094 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11095 }
11096
11097 *mode_line_noprop_ptr++ = c;
11098 }
11099
11100
11101 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11102 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11103 characters that yield more columns than PRECISION; PRECISION <= 0
11104 means copy the whole string. Pad with spaces until FIELD_WIDTH
11105 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11106 pad. Called from display_mode_element when it is used to build a
11107 frame title. */
11108
11109 static int
11110 store_mode_line_noprop (const char *string, int field_width, int precision)
11111 {
11112 const unsigned char *str = (const unsigned char *) string;
11113 int n = 0;
11114 ptrdiff_t dummy, nbytes;
11115
11116 /* Copy at most PRECISION chars from STR. */
11117 nbytes = strlen (string);
11118 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11119 while (nbytes--)
11120 store_mode_line_noprop_char (*str++);
11121
11122 /* Fill up with spaces until FIELD_WIDTH reached. */
11123 while (field_width > 0
11124 && n < field_width)
11125 {
11126 store_mode_line_noprop_char (' ');
11127 ++n;
11128 }
11129
11130 return n;
11131 }
11132
11133 /***********************************************************************
11134 Frame Titles
11135 ***********************************************************************/
11136
11137 #ifdef HAVE_WINDOW_SYSTEM
11138
11139 /* Set the title of FRAME, if it has changed. The title format is
11140 Vicon_title_format if FRAME is iconified, otherwise it is
11141 frame_title_format. */
11142
11143 static void
11144 x_consider_frame_title (Lisp_Object frame)
11145 {
11146 struct frame *f = XFRAME (frame);
11147
11148 if (FRAME_WINDOW_P (f)
11149 || FRAME_MINIBUF_ONLY_P (f)
11150 || f->explicit_name)
11151 {
11152 /* Do we have more than one visible frame on this X display? */
11153 Lisp_Object tail, other_frame, fmt;
11154 ptrdiff_t title_start;
11155 char *title;
11156 ptrdiff_t len;
11157 struct it it;
11158 ptrdiff_t count = SPECPDL_INDEX ();
11159
11160 FOR_EACH_FRAME (tail, other_frame)
11161 {
11162 struct frame *tf = XFRAME (other_frame);
11163
11164 if (tf != f
11165 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11166 && !FRAME_MINIBUF_ONLY_P (tf)
11167 && !EQ (other_frame, tip_frame)
11168 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11169 break;
11170 }
11171
11172 /* Set global variable indicating that multiple frames exist. */
11173 multiple_frames = CONSP (tail);
11174
11175 /* Switch to the buffer of selected window of the frame. Set up
11176 mode_line_target so that display_mode_element will output into
11177 mode_line_noprop_buf; then display the title. */
11178 record_unwind_protect (unwind_format_mode_line,
11179 format_mode_line_unwind_data
11180 (f, current_buffer, selected_window, 0));
11181
11182 Fselect_window (f->selected_window, Qt);
11183 set_buffer_internal_1
11184 (XBUFFER (XWINDOW (f->selected_window)->contents));
11185 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11186
11187 mode_line_target = MODE_LINE_TITLE;
11188 title_start = MODE_LINE_NOPROP_LEN (0);
11189 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11190 NULL, DEFAULT_FACE_ID);
11191 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11192 len = MODE_LINE_NOPROP_LEN (title_start);
11193 title = mode_line_noprop_buf + title_start;
11194 unbind_to (count, Qnil);
11195
11196 /* Set the title only if it's changed. This avoids consing in
11197 the common case where it hasn't. (If it turns out that we've
11198 already wasted too much time by walking through the list with
11199 display_mode_element, then we might need to optimize at a
11200 higher level than this.) */
11201 if (! STRINGP (f->name)
11202 || SBYTES (f->name) != len
11203 || memcmp (title, SDATA (f->name), len) != 0)
11204 x_implicitly_set_name (f, make_string (title, len), Qnil);
11205 }
11206 }
11207
11208 #endif /* not HAVE_WINDOW_SYSTEM */
11209
11210 \f
11211 /***********************************************************************
11212 Menu Bars
11213 ***********************************************************************/
11214
11215
11216 /* Prepare for redisplay by updating menu-bar item lists when
11217 appropriate. This can call eval. */
11218
11219 void
11220 prepare_menu_bars (void)
11221 {
11222 int all_windows;
11223 struct gcpro gcpro1, gcpro2;
11224 struct frame *f;
11225 Lisp_Object tooltip_frame;
11226
11227 #ifdef HAVE_WINDOW_SYSTEM
11228 tooltip_frame = tip_frame;
11229 #else
11230 tooltip_frame = Qnil;
11231 #endif
11232
11233 /* Update all frame titles based on their buffer names, etc. We do
11234 this before the menu bars so that the buffer-menu will show the
11235 up-to-date frame titles. */
11236 #ifdef HAVE_WINDOW_SYSTEM
11237 if (windows_or_buffers_changed || update_mode_lines)
11238 {
11239 Lisp_Object tail, frame;
11240
11241 FOR_EACH_FRAME (tail, frame)
11242 {
11243 f = XFRAME (frame);
11244 if (!EQ (frame, tooltip_frame)
11245 && (FRAME_ICONIFIED_P (f)
11246 || FRAME_VISIBLE_P (f) == 1
11247 /* Exclude TTY frames that are obscured because they
11248 are not the top frame on their console. This is
11249 because x_consider_frame_title actually switches
11250 to the frame, which for TTY frames means it is
11251 marked as garbaged, and will be completely
11252 redrawn on the next redisplay cycle. This causes
11253 TTY frames to be completely redrawn, when there
11254 are more than one of them, even though nothing
11255 should be changed on display. */
11256 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11257 x_consider_frame_title (frame);
11258 }
11259 }
11260 #endif /* HAVE_WINDOW_SYSTEM */
11261
11262 /* Update the menu bar item lists, if appropriate. This has to be
11263 done before any actual redisplay or generation of display lines. */
11264 all_windows = (update_mode_lines
11265 || buffer_shared_and_changed ()
11266 || windows_or_buffers_changed);
11267 if (all_windows)
11268 {
11269 Lisp_Object tail, frame;
11270 ptrdiff_t count = SPECPDL_INDEX ();
11271 /* 1 means that update_menu_bar has run its hooks
11272 so any further calls to update_menu_bar shouldn't do so again. */
11273 int menu_bar_hooks_run = 0;
11274
11275 record_unwind_save_match_data ();
11276
11277 FOR_EACH_FRAME (tail, frame)
11278 {
11279 f = XFRAME (frame);
11280
11281 /* Ignore tooltip frame. */
11282 if (EQ (frame, tooltip_frame))
11283 continue;
11284
11285 /* If a window on this frame changed size, report that to
11286 the user and clear the size-change flag. */
11287 if (FRAME_WINDOW_SIZES_CHANGED (f))
11288 {
11289 Lisp_Object functions;
11290
11291 /* Clear flag first in case we get an error below. */
11292 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11293 functions = Vwindow_size_change_functions;
11294 GCPRO2 (tail, functions);
11295
11296 while (CONSP (functions))
11297 {
11298 if (!EQ (XCAR (functions), Qt))
11299 call1 (XCAR (functions), frame);
11300 functions = XCDR (functions);
11301 }
11302 UNGCPRO;
11303 }
11304
11305 GCPRO1 (tail);
11306 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11307 #ifdef HAVE_WINDOW_SYSTEM
11308 update_tool_bar (f, 0);
11309 #endif
11310 #ifdef HAVE_NS
11311 if (windows_or_buffers_changed
11312 && FRAME_NS_P (f))
11313 ns_set_doc_edited
11314 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->contents));
11315 #endif
11316 UNGCPRO;
11317 }
11318
11319 unbind_to (count, Qnil);
11320 }
11321 else
11322 {
11323 struct frame *sf = SELECTED_FRAME ();
11324 update_menu_bar (sf, 1, 0);
11325 #ifdef HAVE_WINDOW_SYSTEM
11326 update_tool_bar (sf, 1);
11327 #endif
11328 }
11329 }
11330
11331
11332 /* Update the menu bar item list for frame F. This has to be done
11333 before we start to fill in any display lines, because it can call
11334 eval.
11335
11336 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11337
11338 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11339 already ran the menu bar hooks for this redisplay, so there
11340 is no need to run them again. The return value is the
11341 updated value of this flag, to pass to the next call. */
11342
11343 static int
11344 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11345 {
11346 Lisp_Object window;
11347 register struct window *w;
11348
11349 /* If called recursively during a menu update, do nothing. This can
11350 happen when, for instance, an activate-menubar-hook causes a
11351 redisplay. */
11352 if (inhibit_menubar_update)
11353 return hooks_run;
11354
11355 window = FRAME_SELECTED_WINDOW (f);
11356 w = XWINDOW (window);
11357
11358 if (FRAME_WINDOW_P (f)
11359 ?
11360 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11361 || defined (HAVE_NS) || defined (USE_GTK)
11362 FRAME_EXTERNAL_MENU_BAR (f)
11363 #else
11364 FRAME_MENU_BAR_LINES (f) > 0
11365 #endif
11366 : FRAME_MENU_BAR_LINES (f) > 0)
11367 {
11368 /* If the user has switched buffers or windows, we need to
11369 recompute to reflect the new bindings. But we'll
11370 recompute when update_mode_lines is set too; that means
11371 that people can use force-mode-line-update to request
11372 that the menu bar be recomputed. The adverse effect on
11373 the rest of the redisplay algorithm is about the same as
11374 windows_or_buffers_changed anyway. */
11375 if (windows_or_buffers_changed
11376 /* This used to test w->update_mode_line, but we believe
11377 there is no need to recompute the menu in that case. */
11378 || update_mode_lines
11379 || window_buffer_changed (w))
11380 {
11381 struct buffer *prev = current_buffer;
11382 ptrdiff_t count = SPECPDL_INDEX ();
11383
11384 specbind (Qinhibit_menubar_update, Qt);
11385
11386 set_buffer_internal_1 (XBUFFER (w->contents));
11387 if (save_match_data)
11388 record_unwind_save_match_data ();
11389 if (NILP (Voverriding_local_map_menu_flag))
11390 {
11391 specbind (Qoverriding_terminal_local_map, Qnil);
11392 specbind (Qoverriding_local_map, Qnil);
11393 }
11394
11395 if (!hooks_run)
11396 {
11397 /* Run the Lucid hook. */
11398 safe_run_hooks (Qactivate_menubar_hook);
11399
11400 /* If it has changed current-menubar from previous value,
11401 really recompute the menu-bar from the value. */
11402 if (! NILP (Vlucid_menu_bar_dirty_flag))
11403 call0 (Qrecompute_lucid_menubar);
11404
11405 safe_run_hooks (Qmenu_bar_update_hook);
11406
11407 hooks_run = 1;
11408 }
11409
11410 XSETFRAME (Vmenu_updating_frame, f);
11411 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11412
11413 /* Redisplay the menu bar in case we changed it. */
11414 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11415 || defined (HAVE_NS) || defined (USE_GTK)
11416 if (FRAME_WINDOW_P (f))
11417 {
11418 #if defined (HAVE_NS)
11419 /* All frames on Mac OS share the same menubar. So only
11420 the selected frame should be allowed to set it. */
11421 if (f == SELECTED_FRAME ())
11422 #endif
11423 set_frame_menubar (f, 0, 0);
11424 }
11425 else
11426 /* On a terminal screen, the menu bar is an ordinary screen
11427 line, and this makes it get updated. */
11428 w->update_mode_line = 1;
11429 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11430 /* In the non-toolkit version, the menu bar is an ordinary screen
11431 line, and this makes it get updated. */
11432 w->update_mode_line = 1;
11433 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11434
11435 unbind_to (count, Qnil);
11436 set_buffer_internal_1 (prev);
11437 }
11438 }
11439
11440 return hooks_run;
11441 }
11442
11443
11444 \f
11445 /***********************************************************************
11446 Output Cursor
11447 ***********************************************************************/
11448
11449 #ifdef HAVE_WINDOW_SYSTEM
11450
11451 /* EXPORT:
11452 Nominal cursor position -- where to draw output.
11453 HPOS and VPOS are window relative glyph matrix coordinates.
11454 X and Y are window relative pixel coordinates. */
11455
11456 struct cursor_pos output_cursor;
11457
11458
11459 /* EXPORT:
11460 Set the global variable output_cursor to CURSOR. All cursor
11461 positions are relative to currently updated window. */
11462
11463 void
11464 set_output_cursor (struct cursor_pos *cursor)
11465 {
11466 output_cursor.hpos = cursor->hpos;
11467 output_cursor.vpos = cursor->vpos;
11468 output_cursor.x = cursor->x;
11469 output_cursor.y = cursor->y;
11470 }
11471
11472
11473 /* EXPORT for RIF:
11474 Set a nominal cursor position.
11475
11476 HPOS and VPOS are column/row positions in a window glyph matrix.
11477 X and Y are window text area relative pixel positions.
11478
11479 This is always done during window update, so the position is the
11480 future output cursor position for currently updated window W.
11481 NOTE: W is used only to check whether this function is called
11482 in a consistent manner via the redisplay interface. */
11483
11484 void
11485 x_cursor_to (struct window *w, int vpos, int hpos, int y, int x)
11486 {
11487 eassert (w);
11488
11489 /* Set the output cursor. */
11490 output_cursor.hpos = hpos;
11491 output_cursor.vpos = vpos;
11492 output_cursor.x = x;
11493 output_cursor.y = y;
11494 }
11495
11496 #endif /* HAVE_WINDOW_SYSTEM */
11497
11498 \f
11499 /***********************************************************************
11500 Tool-bars
11501 ***********************************************************************/
11502
11503 #ifdef HAVE_WINDOW_SYSTEM
11504
11505 /* Where the mouse was last time we reported a mouse event. */
11506
11507 struct frame *last_mouse_frame;
11508
11509 /* Tool-bar item index of the item on which a mouse button was pressed
11510 or -1. */
11511
11512 int last_tool_bar_item;
11513
11514 /* Select `frame' temporarily without running all the code in
11515 do_switch_frame.
11516 FIXME: Maybe do_switch_frame should be trimmed down similarly
11517 when `norecord' is set. */
11518 static void
11519 fast_set_selected_frame (Lisp_Object frame)
11520 {
11521 if (!EQ (selected_frame, frame))
11522 {
11523 selected_frame = frame;
11524 selected_window = XFRAME (frame)->selected_window;
11525 }
11526 }
11527
11528 /* Update the tool-bar item list for frame F. This has to be done
11529 before we start to fill in any display lines. Called from
11530 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11531 and restore it here. */
11532
11533 static void
11534 update_tool_bar (struct frame *f, int save_match_data)
11535 {
11536 #if defined (USE_GTK) || defined (HAVE_NS)
11537 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11538 #else
11539 int do_update = WINDOWP (f->tool_bar_window)
11540 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11541 #endif
11542
11543 if (do_update)
11544 {
11545 Lisp_Object window;
11546 struct window *w;
11547
11548 window = FRAME_SELECTED_WINDOW (f);
11549 w = XWINDOW (window);
11550
11551 /* If the user has switched buffers or windows, we need to
11552 recompute to reflect the new bindings. But we'll
11553 recompute when update_mode_lines is set too; that means
11554 that people can use force-mode-line-update to request
11555 that the menu bar be recomputed. The adverse effect on
11556 the rest of the redisplay algorithm is about the same as
11557 windows_or_buffers_changed anyway. */
11558 if (windows_or_buffers_changed
11559 || w->update_mode_line
11560 || update_mode_lines
11561 || window_buffer_changed (w))
11562 {
11563 struct buffer *prev = current_buffer;
11564 ptrdiff_t count = SPECPDL_INDEX ();
11565 Lisp_Object frame, new_tool_bar;
11566 int new_n_tool_bar;
11567 struct gcpro gcpro1;
11568
11569 /* Set current_buffer to the buffer of the selected
11570 window of the frame, so that we get the right local
11571 keymaps. */
11572 set_buffer_internal_1 (XBUFFER (w->contents));
11573
11574 /* Save match data, if we must. */
11575 if (save_match_data)
11576 record_unwind_save_match_data ();
11577
11578 /* Make sure that we don't accidentally use bogus keymaps. */
11579 if (NILP (Voverriding_local_map_menu_flag))
11580 {
11581 specbind (Qoverriding_terminal_local_map, Qnil);
11582 specbind (Qoverriding_local_map, Qnil);
11583 }
11584
11585 GCPRO1 (new_tool_bar);
11586
11587 /* We must temporarily set the selected frame to this frame
11588 before calling tool_bar_items, because the calculation of
11589 the tool-bar keymap uses the selected frame (see
11590 `tool-bar-make-keymap' in tool-bar.el). */
11591 eassert (EQ (selected_window,
11592 /* Since we only explicitly preserve selected_frame,
11593 check that selected_window would be redundant. */
11594 XFRAME (selected_frame)->selected_window));
11595 record_unwind_protect (fast_set_selected_frame, selected_frame);
11596 XSETFRAME (frame, f);
11597 fast_set_selected_frame (frame);
11598
11599 /* Build desired tool-bar items from keymaps. */
11600 new_tool_bar
11601 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11602 &new_n_tool_bar);
11603
11604 /* Redisplay the tool-bar if we changed it. */
11605 if (new_n_tool_bar != f->n_tool_bar_items
11606 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11607 {
11608 /* Redisplay that happens asynchronously due to an expose event
11609 may access f->tool_bar_items. Make sure we update both
11610 variables within BLOCK_INPUT so no such event interrupts. */
11611 block_input ();
11612 fset_tool_bar_items (f, new_tool_bar);
11613 f->n_tool_bar_items = new_n_tool_bar;
11614 w->update_mode_line = 1;
11615 unblock_input ();
11616 }
11617
11618 UNGCPRO;
11619
11620 unbind_to (count, Qnil);
11621 set_buffer_internal_1 (prev);
11622 }
11623 }
11624 }
11625
11626
11627 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11628 F's desired tool-bar contents. F->tool_bar_items must have
11629 been set up previously by calling prepare_menu_bars. */
11630
11631 static void
11632 build_desired_tool_bar_string (struct frame *f)
11633 {
11634 int i, size, size_needed;
11635 struct gcpro gcpro1, gcpro2, gcpro3;
11636 Lisp_Object image, plist, props;
11637
11638 image = plist = props = Qnil;
11639 GCPRO3 (image, plist, props);
11640
11641 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11642 Otherwise, make a new string. */
11643
11644 /* The size of the string we might be able to reuse. */
11645 size = (STRINGP (f->desired_tool_bar_string)
11646 ? SCHARS (f->desired_tool_bar_string)
11647 : 0);
11648
11649 /* We need one space in the string for each image. */
11650 size_needed = f->n_tool_bar_items;
11651
11652 /* Reuse f->desired_tool_bar_string, if possible. */
11653 if (size < size_needed || NILP (f->desired_tool_bar_string))
11654 fset_desired_tool_bar_string
11655 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11656 else
11657 {
11658 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11659 Fremove_text_properties (make_number (0), make_number (size),
11660 props, f->desired_tool_bar_string);
11661 }
11662
11663 /* Put a `display' property on the string for the images to display,
11664 put a `menu_item' property on tool-bar items with a value that
11665 is the index of the item in F's tool-bar item vector. */
11666 for (i = 0; i < f->n_tool_bar_items; ++i)
11667 {
11668 #define PROP(IDX) \
11669 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11670
11671 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11672 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11673 int hmargin, vmargin, relief, idx, end;
11674
11675 /* If image is a vector, choose the image according to the
11676 button state. */
11677 image = PROP (TOOL_BAR_ITEM_IMAGES);
11678 if (VECTORP (image))
11679 {
11680 if (enabled_p)
11681 idx = (selected_p
11682 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11683 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11684 else
11685 idx = (selected_p
11686 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11687 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11688
11689 eassert (ASIZE (image) >= idx);
11690 image = AREF (image, idx);
11691 }
11692 else
11693 idx = -1;
11694
11695 /* Ignore invalid image specifications. */
11696 if (!valid_image_p (image))
11697 continue;
11698
11699 /* Display the tool-bar button pressed, or depressed. */
11700 plist = Fcopy_sequence (XCDR (image));
11701
11702 /* Compute margin and relief to draw. */
11703 relief = (tool_bar_button_relief >= 0
11704 ? tool_bar_button_relief
11705 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11706 hmargin = vmargin = relief;
11707
11708 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11709 INT_MAX - max (hmargin, vmargin)))
11710 {
11711 hmargin += XFASTINT (Vtool_bar_button_margin);
11712 vmargin += XFASTINT (Vtool_bar_button_margin);
11713 }
11714 else if (CONSP (Vtool_bar_button_margin))
11715 {
11716 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11717 INT_MAX - hmargin))
11718 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11719
11720 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11721 INT_MAX - vmargin))
11722 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11723 }
11724
11725 if (auto_raise_tool_bar_buttons_p)
11726 {
11727 /* Add a `:relief' property to the image spec if the item is
11728 selected. */
11729 if (selected_p)
11730 {
11731 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11732 hmargin -= relief;
11733 vmargin -= relief;
11734 }
11735 }
11736 else
11737 {
11738 /* If image is selected, display it pressed, i.e. with a
11739 negative relief. If it's not selected, display it with a
11740 raised relief. */
11741 plist = Fplist_put (plist, QCrelief,
11742 (selected_p
11743 ? make_number (-relief)
11744 : make_number (relief)));
11745 hmargin -= relief;
11746 vmargin -= relief;
11747 }
11748
11749 /* Put a margin around the image. */
11750 if (hmargin || vmargin)
11751 {
11752 if (hmargin == vmargin)
11753 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11754 else
11755 plist = Fplist_put (plist, QCmargin,
11756 Fcons (make_number (hmargin),
11757 make_number (vmargin)));
11758 }
11759
11760 /* If button is not enabled, and we don't have special images
11761 for the disabled state, make the image appear disabled by
11762 applying an appropriate algorithm to it. */
11763 if (!enabled_p && idx < 0)
11764 plist = Fplist_put (plist, QCconversion, Qdisabled);
11765
11766 /* Put a `display' text property on the string for the image to
11767 display. Put a `menu-item' property on the string that gives
11768 the start of this item's properties in the tool-bar items
11769 vector. */
11770 image = Fcons (Qimage, plist);
11771 props = list4 (Qdisplay, image,
11772 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11773
11774 /* Let the last image hide all remaining spaces in the tool bar
11775 string. The string can be longer than needed when we reuse a
11776 previous string. */
11777 if (i + 1 == f->n_tool_bar_items)
11778 end = SCHARS (f->desired_tool_bar_string);
11779 else
11780 end = i + 1;
11781 Fadd_text_properties (make_number (i), make_number (end),
11782 props, f->desired_tool_bar_string);
11783 #undef PROP
11784 }
11785
11786 UNGCPRO;
11787 }
11788
11789
11790 /* Display one line of the tool-bar of frame IT->f.
11791
11792 HEIGHT specifies the desired height of the tool-bar line.
11793 If the actual height of the glyph row is less than HEIGHT, the
11794 row's height is increased to HEIGHT, and the icons are centered
11795 vertically in the new height.
11796
11797 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11798 count a final empty row in case the tool-bar width exactly matches
11799 the window width.
11800 */
11801
11802 static void
11803 display_tool_bar_line (struct it *it, int height)
11804 {
11805 struct glyph_row *row = it->glyph_row;
11806 int max_x = it->last_visible_x;
11807 struct glyph *last;
11808
11809 prepare_desired_row (row);
11810 row->y = it->current_y;
11811
11812 /* Note that this isn't made use of if the face hasn't a box,
11813 so there's no need to check the face here. */
11814 it->start_of_box_run_p = 1;
11815
11816 while (it->current_x < max_x)
11817 {
11818 int x, n_glyphs_before, i, nglyphs;
11819 struct it it_before;
11820
11821 /* Get the next display element. */
11822 if (!get_next_display_element (it))
11823 {
11824 /* Don't count empty row if we are counting needed tool-bar lines. */
11825 if (height < 0 && !it->hpos)
11826 return;
11827 break;
11828 }
11829
11830 /* Produce glyphs. */
11831 n_glyphs_before = row->used[TEXT_AREA];
11832 it_before = *it;
11833
11834 PRODUCE_GLYPHS (it);
11835
11836 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11837 i = 0;
11838 x = it_before.current_x;
11839 while (i < nglyphs)
11840 {
11841 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11842
11843 if (x + glyph->pixel_width > max_x)
11844 {
11845 /* Glyph doesn't fit on line. Backtrack. */
11846 row->used[TEXT_AREA] = n_glyphs_before;
11847 *it = it_before;
11848 /* If this is the only glyph on this line, it will never fit on the
11849 tool-bar, so skip it. But ensure there is at least one glyph,
11850 so we don't accidentally disable the tool-bar. */
11851 if (n_glyphs_before == 0
11852 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11853 break;
11854 goto out;
11855 }
11856
11857 ++it->hpos;
11858 x += glyph->pixel_width;
11859 ++i;
11860 }
11861
11862 /* Stop at line end. */
11863 if (ITERATOR_AT_END_OF_LINE_P (it))
11864 break;
11865
11866 set_iterator_to_next (it, 1);
11867 }
11868
11869 out:;
11870
11871 row->displays_text_p = row->used[TEXT_AREA] != 0;
11872
11873 /* Use default face for the border below the tool bar.
11874
11875 FIXME: When auto-resize-tool-bars is grow-only, there is
11876 no additional border below the possibly empty tool-bar lines.
11877 So to make the extra empty lines look "normal", we have to
11878 use the tool-bar face for the border too. */
11879 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
11880 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11881 it->face_id = DEFAULT_FACE_ID;
11882
11883 extend_face_to_end_of_line (it);
11884 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11885 last->right_box_line_p = 1;
11886 if (last == row->glyphs[TEXT_AREA])
11887 last->left_box_line_p = 1;
11888
11889 /* Make line the desired height and center it vertically. */
11890 if ((height -= it->max_ascent + it->max_descent) > 0)
11891 {
11892 /* Don't add more than one line height. */
11893 height %= FRAME_LINE_HEIGHT (it->f);
11894 it->max_ascent += height / 2;
11895 it->max_descent += (height + 1) / 2;
11896 }
11897
11898 compute_line_metrics (it);
11899
11900 /* If line is empty, make it occupy the rest of the tool-bar. */
11901 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
11902 {
11903 row->height = row->phys_height = it->last_visible_y - row->y;
11904 row->visible_height = row->height;
11905 row->ascent = row->phys_ascent = 0;
11906 row->extra_line_spacing = 0;
11907 }
11908
11909 row->full_width_p = 1;
11910 row->continued_p = 0;
11911 row->truncated_on_left_p = 0;
11912 row->truncated_on_right_p = 0;
11913
11914 it->current_x = it->hpos = 0;
11915 it->current_y += row->height;
11916 ++it->vpos;
11917 ++it->glyph_row;
11918 }
11919
11920
11921 /* Max tool-bar height. */
11922
11923 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11924 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11925
11926 /* Value is the number of screen lines needed to make all tool-bar
11927 items of frame F visible. The number of actual rows needed is
11928 returned in *N_ROWS if non-NULL. */
11929
11930 static int
11931 tool_bar_lines_needed (struct frame *f, int *n_rows)
11932 {
11933 struct window *w = XWINDOW (f->tool_bar_window);
11934 struct it it;
11935 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11936 the desired matrix, so use (unused) mode-line row as temporary row to
11937 avoid destroying the first tool-bar row. */
11938 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11939
11940 /* Initialize an iterator for iteration over
11941 F->desired_tool_bar_string in the tool-bar window of frame F. */
11942 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11943 it.first_visible_x = 0;
11944 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11945 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11946 it.paragraph_embedding = L2R;
11947
11948 while (!ITERATOR_AT_END_P (&it))
11949 {
11950 clear_glyph_row (temp_row);
11951 it.glyph_row = temp_row;
11952 display_tool_bar_line (&it, -1);
11953 }
11954 clear_glyph_row (temp_row);
11955
11956 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11957 if (n_rows)
11958 *n_rows = it.vpos > 0 ? it.vpos : -1;
11959
11960 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11961 }
11962
11963
11964 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11965 0, 1, 0,
11966 doc: /* Return the number of lines occupied by the tool bar of FRAME.
11967 If FRAME is nil or omitted, use the selected frame. */)
11968 (Lisp_Object frame)
11969 {
11970 struct frame *f = decode_any_frame (frame);
11971 struct window *w;
11972 int nlines = 0;
11973
11974 if (WINDOWP (f->tool_bar_window)
11975 && (w = XWINDOW (f->tool_bar_window),
11976 WINDOW_TOTAL_LINES (w) > 0))
11977 {
11978 update_tool_bar (f, 1);
11979 if (f->n_tool_bar_items)
11980 {
11981 build_desired_tool_bar_string (f);
11982 nlines = tool_bar_lines_needed (f, NULL);
11983 }
11984 }
11985
11986 return make_number (nlines);
11987 }
11988
11989
11990 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11991 height should be changed. */
11992
11993 static int
11994 redisplay_tool_bar (struct frame *f)
11995 {
11996 struct window *w;
11997 struct it it;
11998 struct glyph_row *row;
11999
12000 #if defined (USE_GTK) || defined (HAVE_NS)
12001 if (FRAME_EXTERNAL_TOOL_BAR (f))
12002 update_frame_tool_bar (f);
12003 return 0;
12004 #endif
12005
12006 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12007 do anything. This means you must start with tool-bar-lines
12008 non-zero to get the auto-sizing effect. Or in other words, you
12009 can turn off tool-bars by specifying tool-bar-lines zero. */
12010 if (!WINDOWP (f->tool_bar_window)
12011 || (w = XWINDOW (f->tool_bar_window),
12012 WINDOW_TOTAL_LINES (w) == 0))
12013 return 0;
12014
12015 /* Set up an iterator for the tool-bar window. */
12016 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12017 it.first_visible_x = 0;
12018 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
12019 row = it.glyph_row;
12020
12021 /* Build a string that represents the contents of the tool-bar. */
12022 build_desired_tool_bar_string (f);
12023 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12024 /* FIXME: This should be controlled by a user option. But it
12025 doesn't make sense to have an R2L tool bar if the menu bar cannot
12026 be drawn also R2L, and making the menu bar R2L is tricky due
12027 toolkit-specific code that implements it. If an R2L tool bar is
12028 ever supported, display_tool_bar_line should also be augmented to
12029 call unproduce_glyphs like display_line and display_string
12030 do. */
12031 it.paragraph_embedding = L2R;
12032
12033 if (f->n_tool_bar_rows == 0)
12034 {
12035 int nlines;
12036
12037 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
12038 nlines != WINDOW_TOTAL_LINES (w)))
12039 {
12040 Lisp_Object frame;
12041 int old_height = WINDOW_TOTAL_LINES (w);
12042
12043 XSETFRAME (frame, f);
12044 Fmodify_frame_parameters (frame,
12045 list1 (Fcons (Qtool_bar_lines,
12046 make_number (nlines))));
12047 if (WINDOW_TOTAL_LINES (w) != old_height)
12048 {
12049 clear_glyph_matrix (w->desired_matrix);
12050 fonts_changed_p = 1;
12051 return 1;
12052 }
12053 }
12054 }
12055
12056 /* Display as many lines as needed to display all tool-bar items. */
12057
12058 if (f->n_tool_bar_rows > 0)
12059 {
12060 int border, rows, height, extra;
12061
12062 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12063 border = XINT (Vtool_bar_border);
12064 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12065 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12066 else if (EQ (Vtool_bar_border, Qborder_width))
12067 border = f->border_width;
12068 else
12069 border = 0;
12070 if (border < 0)
12071 border = 0;
12072
12073 rows = f->n_tool_bar_rows;
12074 height = max (1, (it.last_visible_y - border) / rows);
12075 extra = it.last_visible_y - border - height * rows;
12076
12077 while (it.current_y < it.last_visible_y)
12078 {
12079 int h = 0;
12080 if (extra > 0 && rows-- > 0)
12081 {
12082 h = (extra + rows - 1) / rows;
12083 extra -= h;
12084 }
12085 display_tool_bar_line (&it, height + h);
12086 }
12087 }
12088 else
12089 {
12090 while (it.current_y < it.last_visible_y)
12091 display_tool_bar_line (&it, 0);
12092 }
12093
12094 /* It doesn't make much sense to try scrolling in the tool-bar
12095 window, so don't do it. */
12096 w->desired_matrix->no_scrolling_p = 1;
12097 w->must_be_updated_p = 1;
12098
12099 if (!NILP (Vauto_resize_tool_bars))
12100 {
12101 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12102 int change_height_p = 0;
12103
12104 /* If we couldn't display everything, change the tool-bar's
12105 height if there is room for more. */
12106 if (IT_STRING_CHARPOS (it) < it.end_charpos
12107 && it.current_y < max_tool_bar_height)
12108 change_height_p = 1;
12109
12110 row = it.glyph_row - 1;
12111
12112 /* If there are blank lines at the end, except for a partially
12113 visible blank line at the end that is smaller than
12114 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12115 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12116 && row->height >= FRAME_LINE_HEIGHT (f))
12117 change_height_p = 1;
12118
12119 /* If row displays tool-bar items, but is partially visible,
12120 change the tool-bar's height. */
12121 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12122 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12123 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12124 change_height_p = 1;
12125
12126 /* Resize windows as needed by changing the `tool-bar-lines'
12127 frame parameter. */
12128 if (change_height_p)
12129 {
12130 Lisp_Object frame;
12131 int old_height = WINDOW_TOTAL_LINES (w);
12132 int nrows;
12133 int nlines = tool_bar_lines_needed (f, &nrows);
12134
12135 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12136 && !f->minimize_tool_bar_window_p)
12137 ? (nlines > old_height)
12138 : (nlines != old_height));
12139 f->minimize_tool_bar_window_p = 0;
12140
12141 if (change_height_p)
12142 {
12143 XSETFRAME (frame, f);
12144 Fmodify_frame_parameters (frame,
12145 list1 (Fcons (Qtool_bar_lines,
12146 make_number (nlines))));
12147 if (WINDOW_TOTAL_LINES (w) != old_height)
12148 {
12149 clear_glyph_matrix (w->desired_matrix);
12150 f->n_tool_bar_rows = nrows;
12151 fonts_changed_p = 1;
12152 return 1;
12153 }
12154 }
12155 }
12156 }
12157
12158 f->minimize_tool_bar_window_p = 0;
12159 return 0;
12160 }
12161
12162
12163 /* Get information about the tool-bar item which is displayed in GLYPH
12164 on frame F. Return in *PROP_IDX the index where tool-bar item
12165 properties start in F->tool_bar_items. Value is zero if
12166 GLYPH doesn't display a tool-bar item. */
12167
12168 static int
12169 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12170 {
12171 Lisp_Object prop;
12172 int success_p;
12173 int charpos;
12174
12175 /* This function can be called asynchronously, which means we must
12176 exclude any possibility that Fget_text_property signals an
12177 error. */
12178 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12179 charpos = max (0, charpos);
12180
12181 /* Get the text property `menu-item' at pos. The value of that
12182 property is the start index of this item's properties in
12183 F->tool_bar_items. */
12184 prop = Fget_text_property (make_number (charpos),
12185 Qmenu_item, f->current_tool_bar_string);
12186 if (INTEGERP (prop))
12187 {
12188 *prop_idx = XINT (prop);
12189 success_p = 1;
12190 }
12191 else
12192 success_p = 0;
12193
12194 return success_p;
12195 }
12196
12197 \f
12198 /* Get information about the tool-bar item at position X/Y on frame F.
12199 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12200 the current matrix of the tool-bar window of F, or NULL if not
12201 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12202 item in F->tool_bar_items. Value is
12203
12204 -1 if X/Y is not on a tool-bar item
12205 0 if X/Y is on the same item that was highlighted before.
12206 1 otherwise. */
12207
12208 static int
12209 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12210 int *hpos, int *vpos, int *prop_idx)
12211 {
12212 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12213 struct window *w = XWINDOW (f->tool_bar_window);
12214 int area;
12215
12216 /* Find the glyph under X/Y. */
12217 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12218 if (*glyph == NULL)
12219 return -1;
12220
12221 /* Get the start of this tool-bar item's properties in
12222 f->tool_bar_items. */
12223 if (!tool_bar_item_info (f, *glyph, prop_idx))
12224 return -1;
12225
12226 /* Is mouse on the highlighted item? */
12227 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12228 && *vpos >= hlinfo->mouse_face_beg_row
12229 && *vpos <= hlinfo->mouse_face_end_row
12230 && (*vpos > hlinfo->mouse_face_beg_row
12231 || *hpos >= hlinfo->mouse_face_beg_col)
12232 && (*vpos < hlinfo->mouse_face_end_row
12233 || *hpos < hlinfo->mouse_face_end_col
12234 || hlinfo->mouse_face_past_end))
12235 return 0;
12236
12237 return 1;
12238 }
12239
12240
12241 /* EXPORT:
12242 Handle mouse button event on the tool-bar of frame F, at
12243 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12244 0 for button release. MODIFIERS is event modifiers for button
12245 release. */
12246
12247 void
12248 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12249 int modifiers)
12250 {
12251 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12252 struct window *w = XWINDOW (f->tool_bar_window);
12253 int hpos, vpos, prop_idx;
12254 struct glyph *glyph;
12255 Lisp_Object enabled_p;
12256 int ts;
12257
12258 /* If not on the highlighted tool-bar item, and mouse-highlight is
12259 non-nil, return. This is so we generate the tool-bar button
12260 click only when the mouse button is released on the same item as
12261 where it was pressed. However, when mouse-highlight is disabled,
12262 generate the click when the button is released regardless of the
12263 highlight, since tool-bar items are not highlighted in that
12264 case. */
12265 frame_to_window_pixel_xy (w, &x, &y);
12266 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12267 if (ts == -1
12268 || (ts != 0 && !NILP (Vmouse_highlight)))
12269 return;
12270
12271 /* When mouse-highlight is off, generate the click for the item
12272 where the button was pressed, disregarding where it was
12273 released. */
12274 if (NILP (Vmouse_highlight) && !down_p)
12275 prop_idx = last_tool_bar_item;
12276
12277 /* If item is disabled, do nothing. */
12278 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12279 if (NILP (enabled_p))
12280 return;
12281
12282 if (down_p)
12283 {
12284 /* Show item in pressed state. */
12285 if (!NILP (Vmouse_highlight))
12286 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12287 last_tool_bar_item = prop_idx;
12288 }
12289 else
12290 {
12291 Lisp_Object key, frame;
12292 struct input_event event;
12293 EVENT_INIT (event);
12294
12295 /* Show item in released state. */
12296 if (!NILP (Vmouse_highlight))
12297 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12298
12299 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12300
12301 XSETFRAME (frame, f);
12302 event.kind = TOOL_BAR_EVENT;
12303 event.frame_or_window = frame;
12304 event.arg = frame;
12305 kbd_buffer_store_event (&event);
12306
12307 event.kind = TOOL_BAR_EVENT;
12308 event.frame_or_window = frame;
12309 event.arg = key;
12310 event.modifiers = modifiers;
12311 kbd_buffer_store_event (&event);
12312 last_tool_bar_item = -1;
12313 }
12314 }
12315
12316
12317 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12318 tool-bar window-relative coordinates X/Y. Called from
12319 note_mouse_highlight. */
12320
12321 static void
12322 note_tool_bar_highlight (struct frame *f, int x, int y)
12323 {
12324 Lisp_Object window = f->tool_bar_window;
12325 struct window *w = XWINDOW (window);
12326 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12327 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12328 int hpos, vpos;
12329 struct glyph *glyph;
12330 struct glyph_row *row;
12331 int i;
12332 Lisp_Object enabled_p;
12333 int prop_idx;
12334 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12335 int mouse_down_p, rc;
12336
12337 /* Function note_mouse_highlight is called with negative X/Y
12338 values when mouse moves outside of the frame. */
12339 if (x <= 0 || y <= 0)
12340 {
12341 clear_mouse_face (hlinfo);
12342 return;
12343 }
12344
12345 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12346 if (rc < 0)
12347 {
12348 /* Not on tool-bar item. */
12349 clear_mouse_face (hlinfo);
12350 return;
12351 }
12352 else if (rc == 0)
12353 /* On same tool-bar item as before. */
12354 goto set_help_echo;
12355
12356 clear_mouse_face (hlinfo);
12357
12358 /* Mouse is down, but on different tool-bar item? */
12359 mouse_down_p = (dpyinfo->grabbed
12360 && f == last_mouse_frame
12361 && FRAME_LIVE_P (f));
12362 if (mouse_down_p
12363 && last_tool_bar_item != prop_idx)
12364 return;
12365
12366 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12367
12368 /* If tool-bar item is not enabled, don't highlight it. */
12369 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12370 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12371 {
12372 /* Compute the x-position of the glyph. In front and past the
12373 image is a space. We include this in the highlighted area. */
12374 row = MATRIX_ROW (w->current_matrix, vpos);
12375 for (i = x = 0; i < hpos; ++i)
12376 x += row->glyphs[TEXT_AREA][i].pixel_width;
12377
12378 /* Record this as the current active region. */
12379 hlinfo->mouse_face_beg_col = hpos;
12380 hlinfo->mouse_face_beg_row = vpos;
12381 hlinfo->mouse_face_beg_x = x;
12382 hlinfo->mouse_face_beg_y = row->y;
12383 hlinfo->mouse_face_past_end = 0;
12384
12385 hlinfo->mouse_face_end_col = hpos + 1;
12386 hlinfo->mouse_face_end_row = vpos;
12387 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12388 hlinfo->mouse_face_end_y = row->y;
12389 hlinfo->mouse_face_window = window;
12390 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12391
12392 /* Display it as active. */
12393 show_mouse_face (hlinfo, draw);
12394 }
12395
12396 set_help_echo:
12397
12398 /* Set help_echo_string to a help string to display for this tool-bar item.
12399 XTread_socket does the rest. */
12400 help_echo_object = help_echo_window = Qnil;
12401 help_echo_pos = -1;
12402 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12403 if (NILP (help_echo_string))
12404 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12405 }
12406
12407 #endif /* HAVE_WINDOW_SYSTEM */
12408
12409
12410 \f
12411 /************************************************************************
12412 Horizontal scrolling
12413 ************************************************************************/
12414
12415 static int hscroll_window_tree (Lisp_Object);
12416 static int hscroll_windows (Lisp_Object);
12417
12418 /* For all leaf windows in the window tree rooted at WINDOW, set their
12419 hscroll value so that PT is (i) visible in the window, and (ii) so
12420 that it is not within a certain margin at the window's left and
12421 right border. Value is non-zero if any window's hscroll has been
12422 changed. */
12423
12424 static int
12425 hscroll_window_tree (Lisp_Object window)
12426 {
12427 int hscrolled_p = 0;
12428 int hscroll_relative_p = FLOATP (Vhscroll_step);
12429 int hscroll_step_abs = 0;
12430 double hscroll_step_rel = 0;
12431
12432 if (hscroll_relative_p)
12433 {
12434 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12435 if (hscroll_step_rel < 0)
12436 {
12437 hscroll_relative_p = 0;
12438 hscroll_step_abs = 0;
12439 }
12440 }
12441 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12442 {
12443 hscroll_step_abs = XINT (Vhscroll_step);
12444 if (hscroll_step_abs < 0)
12445 hscroll_step_abs = 0;
12446 }
12447 else
12448 hscroll_step_abs = 0;
12449
12450 while (WINDOWP (window))
12451 {
12452 struct window *w = XWINDOW (window);
12453
12454 if (WINDOWP (w->contents))
12455 hscrolled_p |= hscroll_window_tree (w->contents);
12456 else if (w->cursor.vpos >= 0)
12457 {
12458 int h_margin;
12459 int text_area_width;
12460 struct glyph_row *current_cursor_row
12461 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12462 struct glyph_row *desired_cursor_row
12463 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12464 struct glyph_row *cursor_row
12465 = (desired_cursor_row->enabled_p
12466 ? desired_cursor_row
12467 : current_cursor_row);
12468 int row_r2l_p = cursor_row->reversed_p;
12469
12470 text_area_width = window_box_width (w, TEXT_AREA);
12471
12472 /* Scroll when cursor is inside this scroll margin. */
12473 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12474
12475 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12476 /* For left-to-right rows, hscroll when cursor is either
12477 (i) inside the right hscroll margin, or (ii) if it is
12478 inside the left margin and the window is already
12479 hscrolled. */
12480 && ((!row_r2l_p
12481 && ((w->hscroll
12482 && w->cursor.x <= h_margin)
12483 || (cursor_row->enabled_p
12484 && cursor_row->truncated_on_right_p
12485 && (w->cursor.x >= text_area_width - h_margin))))
12486 /* For right-to-left rows, the logic is similar,
12487 except that rules for scrolling to left and right
12488 are reversed. E.g., if cursor.x <= h_margin, we
12489 need to hscroll "to the right" unconditionally,
12490 and that will scroll the screen to the left so as
12491 to reveal the next portion of the row. */
12492 || (row_r2l_p
12493 && ((cursor_row->enabled_p
12494 /* FIXME: It is confusing to set the
12495 truncated_on_right_p flag when R2L rows
12496 are actually truncated on the left. */
12497 && cursor_row->truncated_on_right_p
12498 && w->cursor.x <= h_margin)
12499 || (w->hscroll
12500 && (w->cursor.x >= text_area_width - h_margin))))))
12501 {
12502 struct it it;
12503 ptrdiff_t hscroll;
12504 struct buffer *saved_current_buffer;
12505 ptrdiff_t pt;
12506 int wanted_x;
12507
12508 /* Find point in a display of infinite width. */
12509 saved_current_buffer = current_buffer;
12510 current_buffer = XBUFFER (w->contents);
12511
12512 if (w == XWINDOW (selected_window))
12513 pt = PT;
12514 else
12515 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12516
12517 /* Move iterator to pt starting at cursor_row->start in
12518 a line with infinite width. */
12519 init_to_row_start (&it, w, cursor_row);
12520 it.last_visible_x = INFINITY;
12521 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12522 current_buffer = saved_current_buffer;
12523
12524 /* Position cursor in window. */
12525 if (!hscroll_relative_p && hscroll_step_abs == 0)
12526 hscroll = max (0, (it.current_x
12527 - (ITERATOR_AT_END_OF_LINE_P (&it)
12528 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12529 : (text_area_width / 2))))
12530 / FRAME_COLUMN_WIDTH (it.f);
12531 else if ((!row_r2l_p
12532 && w->cursor.x >= text_area_width - h_margin)
12533 || (row_r2l_p && w->cursor.x <= h_margin))
12534 {
12535 if (hscroll_relative_p)
12536 wanted_x = text_area_width * (1 - hscroll_step_rel)
12537 - h_margin;
12538 else
12539 wanted_x = text_area_width
12540 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12541 - h_margin;
12542 hscroll
12543 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12544 }
12545 else
12546 {
12547 if (hscroll_relative_p)
12548 wanted_x = text_area_width * hscroll_step_rel
12549 + h_margin;
12550 else
12551 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12552 + h_margin;
12553 hscroll
12554 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12555 }
12556 hscroll = max (hscroll, w->min_hscroll);
12557
12558 /* Don't prevent redisplay optimizations if hscroll
12559 hasn't changed, as it will unnecessarily slow down
12560 redisplay. */
12561 if (w->hscroll != hscroll)
12562 {
12563 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12564 w->hscroll = hscroll;
12565 hscrolled_p = 1;
12566 }
12567 }
12568 }
12569
12570 window = w->next;
12571 }
12572
12573 /* Value is non-zero if hscroll of any leaf window has been changed. */
12574 return hscrolled_p;
12575 }
12576
12577
12578 /* Set hscroll so that cursor is visible and not inside horizontal
12579 scroll margins for all windows in the tree rooted at WINDOW. See
12580 also hscroll_window_tree above. Value is non-zero if any window's
12581 hscroll has been changed. If it has, desired matrices on the frame
12582 of WINDOW are cleared. */
12583
12584 static int
12585 hscroll_windows (Lisp_Object window)
12586 {
12587 int hscrolled_p = hscroll_window_tree (window);
12588 if (hscrolled_p)
12589 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12590 return hscrolled_p;
12591 }
12592
12593
12594 \f
12595 /************************************************************************
12596 Redisplay
12597 ************************************************************************/
12598
12599 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12600 to a non-zero value. This is sometimes handy to have in a debugger
12601 session. */
12602
12603 #ifdef GLYPH_DEBUG
12604
12605 /* First and last unchanged row for try_window_id. */
12606
12607 static int debug_first_unchanged_at_end_vpos;
12608 static int debug_last_unchanged_at_beg_vpos;
12609
12610 /* Delta vpos and y. */
12611
12612 static int debug_dvpos, debug_dy;
12613
12614 /* Delta in characters and bytes for try_window_id. */
12615
12616 static ptrdiff_t debug_delta, debug_delta_bytes;
12617
12618 /* Values of window_end_pos and window_end_vpos at the end of
12619 try_window_id. */
12620
12621 static ptrdiff_t debug_end_vpos;
12622
12623 /* Append a string to W->desired_matrix->method. FMT is a printf
12624 format string. If trace_redisplay_p is non-zero also printf the
12625 resulting string to stderr. */
12626
12627 static void debug_method_add (struct window *, char const *, ...)
12628 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12629
12630 static void
12631 debug_method_add (struct window *w, char const *fmt, ...)
12632 {
12633 void *ptr = w;
12634 char *method = w->desired_matrix->method;
12635 int len = strlen (method);
12636 int size = sizeof w->desired_matrix->method;
12637 int remaining = size - len - 1;
12638 va_list ap;
12639
12640 if (len && remaining)
12641 {
12642 method[len] = '|';
12643 --remaining, ++len;
12644 }
12645
12646 va_start (ap, fmt);
12647 vsnprintf (method + len, remaining + 1, fmt, ap);
12648 va_end (ap);
12649
12650 if (trace_redisplay_p)
12651 fprintf (stderr, "%p (%s): %s\n",
12652 ptr,
12653 ((BUFFERP (w->contents)
12654 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12655 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12656 : "no buffer"),
12657 method + len);
12658 }
12659
12660 #endif /* GLYPH_DEBUG */
12661
12662
12663 /* Value is non-zero if all changes in window W, which displays
12664 current_buffer, are in the text between START and END. START is a
12665 buffer position, END is given as a distance from Z. Used in
12666 redisplay_internal for display optimization. */
12667
12668 static int
12669 text_outside_line_unchanged_p (struct window *w,
12670 ptrdiff_t start, ptrdiff_t end)
12671 {
12672 int unchanged_p = 1;
12673
12674 /* If text or overlays have changed, see where. */
12675 if (window_outdated (w))
12676 {
12677 /* Gap in the line? */
12678 if (GPT < start || Z - GPT < end)
12679 unchanged_p = 0;
12680
12681 /* Changes start in front of the line, or end after it? */
12682 if (unchanged_p
12683 && (BEG_UNCHANGED < start - 1
12684 || END_UNCHANGED < end))
12685 unchanged_p = 0;
12686
12687 /* If selective display, can't optimize if changes start at the
12688 beginning of the line. */
12689 if (unchanged_p
12690 && INTEGERP (BVAR (current_buffer, selective_display))
12691 && XINT (BVAR (current_buffer, selective_display)) > 0
12692 && (BEG_UNCHANGED < start || GPT <= start))
12693 unchanged_p = 0;
12694
12695 /* If there are overlays at the start or end of the line, these
12696 may have overlay strings with newlines in them. A change at
12697 START, for instance, may actually concern the display of such
12698 overlay strings as well, and they are displayed on different
12699 lines. So, quickly rule out this case. (For the future, it
12700 might be desirable to implement something more telling than
12701 just BEG/END_UNCHANGED.) */
12702 if (unchanged_p)
12703 {
12704 if (BEG + BEG_UNCHANGED == start
12705 && overlay_touches_p (start))
12706 unchanged_p = 0;
12707 if (END_UNCHANGED == end
12708 && overlay_touches_p (Z - end))
12709 unchanged_p = 0;
12710 }
12711
12712 /* Under bidi reordering, adding or deleting a character in the
12713 beginning of a paragraph, before the first strong directional
12714 character, can change the base direction of the paragraph (unless
12715 the buffer specifies a fixed paragraph direction), which will
12716 require to redisplay the whole paragraph. It might be worthwhile
12717 to find the paragraph limits and widen the range of redisplayed
12718 lines to that, but for now just give up this optimization. */
12719 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
12720 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
12721 unchanged_p = 0;
12722 }
12723
12724 return unchanged_p;
12725 }
12726
12727
12728 /* Do a frame update, taking possible shortcuts into account. This is
12729 the main external entry point for redisplay.
12730
12731 If the last redisplay displayed an echo area message and that message
12732 is no longer requested, we clear the echo area or bring back the
12733 mini-buffer if that is in use. */
12734
12735 void
12736 redisplay (void)
12737 {
12738 redisplay_internal ();
12739 }
12740
12741
12742 static Lisp_Object
12743 overlay_arrow_string_or_property (Lisp_Object var)
12744 {
12745 Lisp_Object val;
12746
12747 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12748 return val;
12749
12750 return Voverlay_arrow_string;
12751 }
12752
12753 /* Return 1 if there are any overlay-arrows in current_buffer. */
12754 static int
12755 overlay_arrow_in_current_buffer_p (void)
12756 {
12757 Lisp_Object vlist;
12758
12759 for (vlist = Voverlay_arrow_variable_list;
12760 CONSP (vlist);
12761 vlist = XCDR (vlist))
12762 {
12763 Lisp_Object var = XCAR (vlist);
12764 Lisp_Object val;
12765
12766 if (!SYMBOLP (var))
12767 continue;
12768 val = find_symbol_value (var);
12769 if (MARKERP (val)
12770 && current_buffer == XMARKER (val)->buffer)
12771 return 1;
12772 }
12773 return 0;
12774 }
12775
12776
12777 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12778 has changed. */
12779
12780 static int
12781 overlay_arrows_changed_p (void)
12782 {
12783 Lisp_Object vlist;
12784
12785 for (vlist = Voverlay_arrow_variable_list;
12786 CONSP (vlist);
12787 vlist = XCDR (vlist))
12788 {
12789 Lisp_Object var = XCAR (vlist);
12790 Lisp_Object val, pstr;
12791
12792 if (!SYMBOLP (var))
12793 continue;
12794 val = find_symbol_value (var);
12795 if (!MARKERP (val))
12796 continue;
12797 if (! EQ (COERCE_MARKER (val),
12798 Fget (var, Qlast_arrow_position))
12799 || ! (pstr = overlay_arrow_string_or_property (var),
12800 EQ (pstr, Fget (var, Qlast_arrow_string))))
12801 return 1;
12802 }
12803 return 0;
12804 }
12805
12806 /* Mark overlay arrows to be updated on next redisplay. */
12807
12808 static void
12809 update_overlay_arrows (int up_to_date)
12810 {
12811 Lisp_Object vlist;
12812
12813 for (vlist = Voverlay_arrow_variable_list;
12814 CONSP (vlist);
12815 vlist = XCDR (vlist))
12816 {
12817 Lisp_Object var = XCAR (vlist);
12818
12819 if (!SYMBOLP (var))
12820 continue;
12821
12822 if (up_to_date > 0)
12823 {
12824 Lisp_Object val = find_symbol_value (var);
12825 Fput (var, Qlast_arrow_position,
12826 COERCE_MARKER (val));
12827 Fput (var, Qlast_arrow_string,
12828 overlay_arrow_string_or_property (var));
12829 }
12830 else if (up_to_date < 0
12831 || !NILP (Fget (var, Qlast_arrow_position)))
12832 {
12833 Fput (var, Qlast_arrow_position, Qt);
12834 Fput (var, Qlast_arrow_string, Qt);
12835 }
12836 }
12837 }
12838
12839
12840 /* Return overlay arrow string to display at row.
12841 Return integer (bitmap number) for arrow bitmap in left fringe.
12842 Return nil if no overlay arrow. */
12843
12844 static Lisp_Object
12845 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12846 {
12847 Lisp_Object vlist;
12848
12849 for (vlist = Voverlay_arrow_variable_list;
12850 CONSP (vlist);
12851 vlist = XCDR (vlist))
12852 {
12853 Lisp_Object var = XCAR (vlist);
12854 Lisp_Object val;
12855
12856 if (!SYMBOLP (var))
12857 continue;
12858
12859 val = find_symbol_value (var);
12860
12861 if (MARKERP (val)
12862 && current_buffer == XMARKER (val)->buffer
12863 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12864 {
12865 if (FRAME_WINDOW_P (it->f)
12866 /* FIXME: if ROW->reversed_p is set, this should test
12867 the right fringe, not the left one. */
12868 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12869 {
12870 #ifdef HAVE_WINDOW_SYSTEM
12871 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12872 {
12873 int fringe_bitmap;
12874 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12875 return make_number (fringe_bitmap);
12876 }
12877 #endif
12878 return make_number (-1); /* Use default arrow bitmap. */
12879 }
12880 return overlay_arrow_string_or_property (var);
12881 }
12882 }
12883
12884 return Qnil;
12885 }
12886
12887 /* Return 1 if point moved out of or into a composition. Otherwise
12888 return 0. PREV_BUF and PREV_PT are the last point buffer and
12889 position. BUF and PT are the current point buffer and position. */
12890
12891 static int
12892 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12893 struct buffer *buf, ptrdiff_t pt)
12894 {
12895 ptrdiff_t start, end;
12896 Lisp_Object prop;
12897 Lisp_Object buffer;
12898
12899 XSETBUFFER (buffer, buf);
12900 /* Check a composition at the last point if point moved within the
12901 same buffer. */
12902 if (prev_buf == buf)
12903 {
12904 if (prev_pt == pt)
12905 /* Point didn't move. */
12906 return 0;
12907
12908 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12909 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12910 && composition_valid_p (start, end, prop)
12911 && start < prev_pt && end > prev_pt)
12912 /* The last point was within the composition. Return 1 iff
12913 point moved out of the composition. */
12914 return (pt <= start || pt >= end);
12915 }
12916
12917 /* Check a composition at the current point. */
12918 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12919 && find_composition (pt, -1, &start, &end, &prop, buffer)
12920 && composition_valid_p (start, end, prop)
12921 && start < pt && end > pt);
12922 }
12923
12924 /* Reconsider the clip changes of buffer which is displayed in W. */
12925
12926 static void
12927 reconsider_clip_changes (struct window *w)
12928 {
12929 struct buffer *b = XBUFFER (w->contents);
12930
12931 if (b->clip_changed
12932 && w->window_end_valid
12933 && w->current_matrix->buffer == b
12934 && w->current_matrix->zv == BUF_ZV (b)
12935 && w->current_matrix->begv == BUF_BEGV (b))
12936 b->clip_changed = 0;
12937
12938 /* If display wasn't paused, and W is not a tool bar window, see if
12939 point has been moved into or out of a composition. In that case,
12940 we set b->clip_changed to 1 to force updating the screen. If
12941 b->clip_changed has already been set to 1, we can skip this
12942 check. */
12943 if (!b->clip_changed && w->window_end_valid)
12944 {
12945 ptrdiff_t pt = (w == XWINDOW (selected_window)
12946 ? PT : marker_position (w->pointm));
12947
12948 if ((w->current_matrix->buffer != b || pt != w->last_point)
12949 && check_point_in_composition (w->current_matrix->buffer,
12950 w->last_point, b, pt))
12951 b->clip_changed = 1;
12952 }
12953 }
12954
12955 #define STOP_POLLING \
12956 do { if (! polling_stopped_here) stop_polling (); \
12957 polling_stopped_here = 1; } while (0)
12958
12959 #define RESUME_POLLING \
12960 do { if (polling_stopped_here) start_polling (); \
12961 polling_stopped_here = 0; } while (0)
12962
12963
12964 /* Perhaps in the future avoid recentering windows if it
12965 is not necessary; currently that causes some problems. */
12966
12967 static void
12968 redisplay_internal (void)
12969 {
12970 struct window *w = XWINDOW (selected_window);
12971 struct window *sw;
12972 struct frame *fr;
12973 int pending;
12974 bool must_finish = 0, match_p;
12975 struct text_pos tlbufpos, tlendpos;
12976 int number_of_visible_frames;
12977 ptrdiff_t count;
12978 struct frame *sf;
12979 int polling_stopped_here = 0;
12980 Lisp_Object tail, frame;
12981
12982 /* Non-zero means redisplay has to consider all windows on all
12983 frames. Zero means, only selected_window is considered. */
12984 int consider_all_windows_p;
12985
12986 /* Non-zero means redisplay has to redisplay the miniwindow. */
12987 int update_miniwindow_p = 0;
12988
12989 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12990
12991 /* No redisplay if running in batch mode or frame is not yet fully
12992 initialized, or redisplay is explicitly turned off by setting
12993 Vinhibit_redisplay. */
12994 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12995 || !NILP (Vinhibit_redisplay))
12996 return;
12997
12998 /* Don't examine these until after testing Vinhibit_redisplay.
12999 When Emacs is shutting down, perhaps because its connection to
13000 X has dropped, we should not look at them at all. */
13001 fr = XFRAME (w->frame);
13002 sf = SELECTED_FRAME ();
13003
13004 if (!fr->glyphs_initialized_p)
13005 return;
13006
13007 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13008 if (popup_activated ())
13009 return;
13010 #endif
13011
13012 /* I don't think this happens but let's be paranoid. */
13013 if (redisplaying_p)
13014 return;
13015
13016 /* Record a function that clears redisplaying_p
13017 when we leave this function. */
13018 count = SPECPDL_INDEX ();
13019 record_unwind_protect_void (unwind_redisplay);
13020 redisplaying_p = 1;
13021 specbind (Qinhibit_free_realized_faces, Qnil);
13022
13023 /* Record this function, so it appears on the profiler's backtraces. */
13024 record_in_backtrace (Qredisplay_internal, &Qnil, 0);
13025
13026 FOR_EACH_FRAME (tail, frame)
13027 XFRAME (frame)->already_hscrolled_p = 0;
13028
13029 retry:
13030 /* Remember the currently selected window. */
13031 sw = w;
13032
13033 pending = 0;
13034 last_escape_glyph_frame = NULL;
13035 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
13036 last_glyphless_glyph_frame = NULL;
13037 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
13038
13039 /* If new fonts have been loaded that make a glyph matrix adjustment
13040 necessary, do it. */
13041 if (fonts_changed_p)
13042 {
13043 adjust_glyphs (NULL);
13044 ++windows_or_buffers_changed;
13045 fonts_changed_p = 0;
13046 }
13047
13048 /* If face_change_count is non-zero, init_iterator will free all
13049 realized faces, which includes the faces referenced from current
13050 matrices. So, we can't reuse current matrices in this case. */
13051 if (face_change_count)
13052 ++windows_or_buffers_changed;
13053
13054 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13055 && FRAME_TTY (sf)->previous_frame != sf)
13056 {
13057 /* Since frames on a single ASCII terminal share the same
13058 display area, displaying a different frame means redisplay
13059 the whole thing. */
13060 windows_or_buffers_changed++;
13061 SET_FRAME_GARBAGED (sf);
13062 #ifndef DOS_NT
13063 set_tty_color_mode (FRAME_TTY (sf), sf);
13064 #endif
13065 FRAME_TTY (sf)->previous_frame = sf;
13066 }
13067
13068 /* Set the visible flags for all frames. Do this before checking for
13069 resized or garbaged frames; they want to know if their frames are
13070 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13071 number_of_visible_frames = 0;
13072
13073 FOR_EACH_FRAME (tail, frame)
13074 {
13075 struct frame *f = XFRAME (frame);
13076
13077 if (FRAME_VISIBLE_P (f))
13078 ++number_of_visible_frames;
13079 clear_desired_matrices (f);
13080 }
13081
13082 /* Notice any pending interrupt request to change frame size. */
13083 do_pending_window_change (1);
13084
13085 /* do_pending_window_change could change the selected_window due to
13086 frame resizing which makes the selected window too small. */
13087 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13088 sw = w;
13089
13090 /* Clear frames marked as garbaged. */
13091 clear_garbaged_frames ();
13092
13093 /* Build menubar and tool-bar items. */
13094 if (NILP (Vmemory_full))
13095 prepare_menu_bars ();
13096
13097 if (windows_or_buffers_changed)
13098 update_mode_lines++;
13099
13100 reconsider_clip_changes (w);
13101
13102 /* In most cases selected window displays current buffer. */
13103 match_p = XBUFFER (w->contents) == current_buffer;
13104 if (match_p)
13105 {
13106 ptrdiff_t count1;
13107
13108 /* Detect case that we need to write or remove a star in the mode line. */
13109 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13110 {
13111 w->update_mode_line = 1;
13112 if (buffer_shared_and_changed ())
13113 update_mode_lines++;
13114 }
13115
13116 /* Avoid invocation of point motion hooks by `current_column' below. */
13117 count1 = SPECPDL_INDEX ();
13118 specbind (Qinhibit_point_motion_hooks, Qt);
13119
13120 if (mode_line_update_needed (w))
13121 w->update_mode_line = 1;
13122
13123 unbind_to (count1, Qnil);
13124 }
13125
13126 consider_all_windows_p = (update_mode_lines
13127 || buffer_shared_and_changed ()
13128 || cursor_type_changed);
13129
13130 /* If specs for an arrow have changed, do thorough redisplay
13131 to ensure we remove any arrow that should no longer exist. */
13132 if (overlay_arrows_changed_p ())
13133 consider_all_windows_p = windows_or_buffers_changed = 1;
13134
13135 /* Normally the message* functions will have already displayed and
13136 updated the echo area, but the frame may have been trashed, or
13137 the update may have been preempted, so display the echo area
13138 again here. Checking message_cleared_p captures the case that
13139 the echo area should be cleared. */
13140 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13141 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13142 || (message_cleared_p
13143 && minibuf_level == 0
13144 /* If the mini-window is currently selected, this means the
13145 echo-area doesn't show through. */
13146 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13147 {
13148 int window_height_changed_p = echo_area_display (0);
13149
13150 if (message_cleared_p)
13151 update_miniwindow_p = 1;
13152
13153 must_finish = 1;
13154
13155 /* If we don't display the current message, don't clear the
13156 message_cleared_p flag, because, if we did, we wouldn't clear
13157 the echo area in the next redisplay which doesn't preserve
13158 the echo area. */
13159 if (!display_last_displayed_message_p)
13160 message_cleared_p = 0;
13161
13162 if (fonts_changed_p)
13163 goto retry;
13164 else if (window_height_changed_p)
13165 {
13166 consider_all_windows_p = 1;
13167 ++update_mode_lines;
13168 ++windows_or_buffers_changed;
13169
13170 /* If window configuration was changed, frames may have been
13171 marked garbaged. Clear them or we will experience
13172 surprises wrt scrolling. */
13173 clear_garbaged_frames ();
13174 }
13175 }
13176 else if (EQ (selected_window, minibuf_window)
13177 && (current_buffer->clip_changed || window_outdated (w))
13178 && resize_mini_window (w, 0))
13179 {
13180 /* Resized active mini-window to fit the size of what it is
13181 showing if its contents might have changed. */
13182 must_finish = 1;
13183 /* FIXME: this causes all frames to be updated, which seems unnecessary
13184 since only the current frame needs to be considered. This function
13185 needs to be rewritten with two variables, consider_all_windows and
13186 consider_all_frames. */
13187 consider_all_windows_p = 1;
13188 ++windows_or_buffers_changed;
13189 ++update_mode_lines;
13190
13191 /* If window configuration was changed, frames may have been
13192 marked garbaged. Clear them or we will experience
13193 surprises wrt scrolling. */
13194 clear_garbaged_frames ();
13195 }
13196
13197 /* If showing the region, and mark has changed, we must redisplay
13198 the whole window. The assignment to this_line_start_pos prevents
13199 the optimization directly below this if-statement. */
13200 if (((!NILP (Vtransient_mark_mode)
13201 && !NILP (BVAR (XBUFFER (w->contents), mark_active)))
13202 != (w->region_showing > 0))
13203 || (w->region_showing
13204 && w->region_showing
13205 != XINT (Fmarker_position (BVAR (XBUFFER (w->contents), mark)))))
13206 CHARPOS (this_line_start_pos) = 0;
13207
13208 /* Optimize the case that only the line containing the cursor in the
13209 selected window has changed. Variables starting with this_ are
13210 set in display_line and record information about the line
13211 containing the cursor. */
13212 tlbufpos = this_line_start_pos;
13213 tlendpos = this_line_end_pos;
13214 if (!consider_all_windows_p
13215 && CHARPOS (tlbufpos) > 0
13216 && !w->update_mode_line
13217 && !current_buffer->clip_changed
13218 && !current_buffer->prevent_redisplay_optimizations_p
13219 && FRAME_VISIBLE_P (XFRAME (w->frame))
13220 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13221 /* Make sure recorded data applies to current buffer, etc. */
13222 && this_line_buffer == current_buffer
13223 && match_p
13224 && !w->force_start
13225 && !w->optional_new_start
13226 /* Point must be on the line that we have info recorded about. */
13227 && PT >= CHARPOS (tlbufpos)
13228 && PT <= Z - CHARPOS (tlendpos)
13229 /* All text outside that line, including its final newline,
13230 must be unchanged. */
13231 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13232 CHARPOS (tlendpos)))
13233 {
13234 if (CHARPOS (tlbufpos) > BEGV
13235 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13236 && (CHARPOS (tlbufpos) == ZV
13237 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13238 /* Former continuation line has disappeared by becoming empty. */
13239 goto cancel;
13240 else if (window_outdated (w) || MINI_WINDOW_P (w))
13241 {
13242 /* We have to handle the case of continuation around a
13243 wide-column character (see the comment in indent.c around
13244 line 1340).
13245
13246 For instance, in the following case:
13247
13248 -------- Insert --------
13249 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13250 J_I_ ==> J_I_ `^^' are cursors.
13251 ^^ ^^
13252 -------- --------
13253
13254 As we have to redraw the line above, we cannot use this
13255 optimization. */
13256
13257 struct it it;
13258 int line_height_before = this_line_pixel_height;
13259
13260 /* Note that start_display will handle the case that the
13261 line starting at tlbufpos is a continuation line. */
13262 start_display (&it, w, tlbufpos);
13263
13264 /* Implementation note: It this still necessary? */
13265 if (it.current_x != this_line_start_x)
13266 goto cancel;
13267
13268 TRACE ((stderr, "trying display optimization 1\n"));
13269 w->cursor.vpos = -1;
13270 overlay_arrow_seen = 0;
13271 it.vpos = this_line_vpos;
13272 it.current_y = this_line_y;
13273 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13274 display_line (&it);
13275
13276 /* If line contains point, is not continued,
13277 and ends at same distance from eob as before, we win. */
13278 if (w->cursor.vpos >= 0
13279 /* Line is not continued, otherwise this_line_start_pos
13280 would have been set to 0 in display_line. */
13281 && CHARPOS (this_line_start_pos)
13282 /* Line ends as before. */
13283 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13284 /* Line has same height as before. Otherwise other lines
13285 would have to be shifted up or down. */
13286 && this_line_pixel_height == line_height_before)
13287 {
13288 /* If this is not the window's last line, we must adjust
13289 the charstarts of the lines below. */
13290 if (it.current_y < it.last_visible_y)
13291 {
13292 struct glyph_row *row
13293 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13294 ptrdiff_t delta, delta_bytes;
13295
13296 /* We used to distinguish between two cases here,
13297 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13298 when the line ends in a newline or the end of the
13299 buffer's accessible portion. But both cases did
13300 the same, so they were collapsed. */
13301 delta = (Z
13302 - CHARPOS (tlendpos)
13303 - MATRIX_ROW_START_CHARPOS (row));
13304 delta_bytes = (Z_BYTE
13305 - BYTEPOS (tlendpos)
13306 - MATRIX_ROW_START_BYTEPOS (row));
13307
13308 increment_matrix_positions (w->current_matrix,
13309 this_line_vpos + 1,
13310 w->current_matrix->nrows,
13311 delta, delta_bytes);
13312 }
13313
13314 /* If this row displays text now but previously didn't,
13315 or vice versa, w->window_end_vpos may have to be
13316 adjusted. */
13317 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13318 {
13319 if (w->window_end_vpos < this_line_vpos)
13320 w->window_end_vpos = this_line_vpos;
13321 }
13322 else if (w->window_end_vpos == this_line_vpos
13323 && this_line_vpos > 0)
13324 w->window_end_vpos = this_line_vpos - 1;
13325 w->window_end_valid = 0;
13326
13327 /* Update hint: No need to try to scroll in update_window. */
13328 w->desired_matrix->no_scrolling_p = 1;
13329
13330 #ifdef GLYPH_DEBUG
13331 *w->desired_matrix->method = 0;
13332 debug_method_add (w, "optimization 1");
13333 #endif
13334 #ifdef HAVE_WINDOW_SYSTEM
13335 update_window_fringes (w, 0);
13336 #endif
13337 goto update;
13338 }
13339 else
13340 goto cancel;
13341 }
13342 else if (/* Cursor position hasn't changed. */
13343 PT == w->last_point
13344 /* Make sure the cursor was last displayed
13345 in this window. Otherwise we have to reposition it. */
13346 && 0 <= w->cursor.vpos
13347 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13348 {
13349 if (!must_finish)
13350 {
13351 do_pending_window_change (1);
13352 /* If selected_window changed, redisplay again. */
13353 if (WINDOWP (selected_window)
13354 && (w = XWINDOW (selected_window)) != sw)
13355 goto retry;
13356
13357 /* We used to always goto end_of_redisplay here, but this
13358 isn't enough if we have a blinking cursor. */
13359 if (w->cursor_off_p == w->last_cursor_off_p)
13360 goto end_of_redisplay;
13361 }
13362 goto update;
13363 }
13364 /* If highlighting the region, or if the cursor is in the echo area,
13365 then we can't just move the cursor. */
13366 else if (! (!NILP (Vtransient_mark_mode)
13367 && !NILP (BVAR (current_buffer, mark_active)))
13368 && (EQ (selected_window,
13369 BVAR (current_buffer, last_selected_window))
13370 || highlight_nonselected_windows)
13371 && !w->region_showing
13372 && NILP (Vshow_trailing_whitespace)
13373 && !cursor_in_echo_area)
13374 {
13375 struct it it;
13376 struct glyph_row *row;
13377
13378 /* Skip from tlbufpos to PT and see where it is. Note that
13379 PT may be in invisible text. If so, we will end at the
13380 next visible position. */
13381 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13382 NULL, DEFAULT_FACE_ID);
13383 it.current_x = this_line_start_x;
13384 it.current_y = this_line_y;
13385 it.vpos = this_line_vpos;
13386
13387 /* The call to move_it_to stops in front of PT, but
13388 moves over before-strings. */
13389 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13390
13391 if (it.vpos == this_line_vpos
13392 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13393 row->enabled_p))
13394 {
13395 eassert (this_line_vpos == it.vpos);
13396 eassert (this_line_y == it.current_y);
13397 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13398 #ifdef GLYPH_DEBUG
13399 *w->desired_matrix->method = 0;
13400 debug_method_add (w, "optimization 3");
13401 #endif
13402 goto update;
13403 }
13404 else
13405 goto cancel;
13406 }
13407
13408 cancel:
13409 /* Text changed drastically or point moved off of line. */
13410 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13411 }
13412
13413 CHARPOS (this_line_start_pos) = 0;
13414 consider_all_windows_p |= buffer_shared_and_changed ();
13415 ++clear_face_cache_count;
13416 #ifdef HAVE_WINDOW_SYSTEM
13417 ++clear_image_cache_count;
13418 #endif
13419
13420 /* Build desired matrices, and update the display. If
13421 consider_all_windows_p is non-zero, do it for all windows on all
13422 frames. Otherwise do it for selected_window, only. */
13423
13424 if (consider_all_windows_p)
13425 {
13426 FOR_EACH_FRAME (tail, frame)
13427 XFRAME (frame)->updated_p = 0;
13428
13429 FOR_EACH_FRAME (tail, frame)
13430 {
13431 struct frame *f = XFRAME (frame);
13432
13433 /* We don't have to do anything for unselected terminal
13434 frames. */
13435 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13436 && !EQ (FRAME_TTY (f)->top_frame, frame))
13437 continue;
13438
13439 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13440 {
13441 /* Mark all the scroll bars to be removed; we'll redeem
13442 the ones we want when we redisplay their windows. */
13443 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13444 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13445
13446 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13447 redisplay_windows (FRAME_ROOT_WINDOW (f));
13448
13449 /* The X error handler may have deleted that frame. */
13450 if (!FRAME_LIVE_P (f))
13451 continue;
13452
13453 /* Any scroll bars which redisplay_windows should have
13454 nuked should now go away. */
13455 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13456 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13457
13458 /* If fonts changed, display again. */
13459 /* ??? rms: I suspect it is a mistake to jump all the way
13460 back to retry here. It should just retry this frame. */
13461 if (fonts_changed_p)
13462 goto retry;
13463
13464 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13465 {
13466 /* See if we have to hscroll. */
13467 if (!f->already_hscrolled_p)
13468 {
13469 f->already_hscrolled_p = 1;
13470 if (hscroll_windows (f->root_window))
13471 goto retry;
13472 }
13473
13474 /* Prevent various kinds of signals during display
13475 update. stdio is not robust about handling
13476 signals, which can cause an apparent I/O
13477 error. */
13478 if (interrupt_input)
13479 unrequest_sigio ();
13480 STOP_POLLING;
13481
13482 /* Update the display. */
13483 set_window_update_flags (XWINDOW (f->root_window), 1);
13484 pending |= update_frame (f, 0, 0);
13485 f->updated_p = 1;
13486 }
13487 }
13488 }
13489
13490 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13491
13492 if (!pending)
13493 {
13494 /* Do the mark_window_display_accurate after all windows have
13495 been redisplayed because this call resets flags in buffers
13496 which are needed for proper redisplay. */
13497 FOR_EACH_FRAME (tail, frame)
13498 {
13499 struct frame *f = XFRAME (frame);
13500 if (f->updated_p)
13501 {
13502 mark_window_display_accurate (f->root_window, 1);
13503 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13504 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13505 }
13506 }
13507 }
13508 }
13509 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13510 {
13511 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13512 struct frame *mini_frame;
13513
13514 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13515 /* Use list_of_error, not Qerror, so that
13516 we catch only errors and don't run the debugger. */
13517 internal_condition_case_1 (redisplay_window_1, selected_window,
13518 list_of_error,
13519 redisplay_window_error);
13520 if (update_miniwindow_p)
13521 internal_condition_case_1 (redisplay_window_1, mini_window,
13522 list_of_error,
13523 redisplay_window_error);
13524
13525 /* Compare desired and current matrices, perform output. */
13526
13527 update:
13528 /* If fonts changed, display again. */
13529 if (fonts_changed_p)
13530 goto retry;
13531
13532 /* Prevent various kinds of signals during display update.
13533 stdio is not robust about handling signals,
13534 which can cause an apparent I/O error. */
13535 if (interrupt_input)
13536 unrequest_sigio ();
13537 STOP_POLLING;
13538
13539 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13540 {
13541 if (hscroll_windows (selected_window))
13542 goto retry;
13543
13544 XWINDOW (selected_window)->must_be_updated_p = 1;
13545 pending = update_frame (sf, 0, 0);
13546 }
13547
13548 /* We may have called echo_area_display at the top of this
13549 function. If the echo area is on another frame, that may
13550 have put text on a frame other than the selected one, so the
13551 above call to update_frame would not have caught it. Catch
13552 it here. */
13553 mini_window = FRAME_MINIBUF_WINDOW (sf);
13554 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13555
13556 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13557 {
13558 XWINDOW (mini_window)->must_be_updated_p = 1;
13559 pending |= update_frame (mini_frame, 0, 0);
13560 if (!pending && hscroll_windows (mini_window))
13561 goto retry;
13562 }
13563 }
13564
13565 /* If display was paused because of pending input, make sure we do a
13566 thorough update the next time. */
13567 if (pending)
13568 {
13569 /* Prevent the optimization at the beginning of
13570 redisplay_internal that tries a single-line update of the
13571 line containing the cursor in the selected window. */
13572 CHARPOS (this_line_start_pos) = 0;
13573
13574 /* Let the overlay arrow be updated the next time. */
13575 update_overlay_arrows (0);
13576
13577 /* If we pause after scrolling, some rows in the current
13578 matrices of some windows are not valid. */
13579 if (!WINDOW_FULL_WIDTH_P (w)
13580 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13581 update_mode_lines = 1;
13582 }
13583 else
13584 {
13585 if (!consider_all_windows_p)
13586 {
13587 /* This has already been done above if
13588 consider_all_windows_p is set. */
13589 mark_window_display_accurate_1 (w, 1);
13590
13591 /* Say overlay arrows are up to date. */
13592 update_overlay_arrows (1);
13593
13594 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13595 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13596 }
13597
13598 update_mode_lines = 0;
13599 windows_or_buffers_changed = 0;
13600 cursor_type_changed = 0;
13601 }
13602
13603 /* Start SIGIO interrupts coming again. Having them off during the
13604 code above makes it less likely one will discard output, but not
13605 impossible, since there might be stuff in the system buffer here.
13606 But it is much hairier to try to do anything about that. */
13607 if (interrupt_input)
13608 request_sigio ();
13609 RESUME_POLLING;
13610
13611 /* If a frame has become visible which was not before, redisplay
13612 again, so that we display it. Expose events for such a frame
13613 (which it gets when becoming visible) don't call the parts of
13614 redisplay constructing glyphs, so simply exposing a frame won't
13615 display anything in this case. So, we have to display these
13616 frames here explicitly. */
13617 if (!pending)
13618 {
13619 int new_count = 0;
13620
13621 FOR_EACH_FRAME (tail, frame)
13622 {
13623 int this_is_visible = 0;
13624
13625 if (XFRAME (frame)->visible)
13626 this_is_visible = 1;
13627
13628 if (this_is_visible)
13629 new_count++;
13630 }
13631
13632 if (new_count != number_of_visible_frames)
13633 windows_or_buffers_changed++;
13634 }
13635
13636 /* Change frame size now if a change is pending. */
13637 do_pending_window_change (1);
13638
13639 /* If we just did a pending size change, or have additional
13640 visible frames, or selected_window changed, redisplay again. */
13641 if ((windows_or_buffers_changed && !pending)
13642 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13643 goto retry;
13644
13645 /* Clear the face and image caches.
13646
13647 We used to do this only if consider_all_windows_p. But the cache
13648 needs to be cleared if a timer creates images in the current
13649 buffer (e.g. the test case in Bug#6230). */
13650
13651 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13652 {
13653 clear_face_cache (0);
13654 clear_face_cache_count = 0;
13655 }
13656
13657 #ifdef HAVE_WINDOW_SYSTEM
13658 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13659 {
13660 clear_image_caches (Qnil);
13661 clear_image_cache_count = 0;
13662 }
13663 #endif /* HAVE_WINDOW_SYSTEM */
13664
13665 end_of_redisplay:
13666 unbind_to (count, Qnil);
13667 RESUME_POLLING;
13668 }
13669
13670
13671 /* Redisplay, but leave alone any recent echo area message unless
13672 another message has been requested in its place.
13673
13674 This is useful in situations where you need to redisplay but no
13675 user action has occurred, making it inappropriate for the message
13676 area to be cleared. See tracking_off and
13677 wait_reading_process_output for examples of these situations.
13678
13679 FROM_WHERE is an integer saying from where this function was
13680 called. This is useful for debugging. */
13681
13682 void
13683 redisplay_preserve_echo_area (int from_where)
13684 {
13685 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13686
13687 if (!NILP (echo_area_buffer[1]))
13688 {
13689 /* We have a previously displayed message, but no current
13690 message. Redisplay the previous message. */
13691 display_last_displayed_message_p = 1;
13692 redisplay_internal ();
13693 display_last_displayed_message_p = 0;
13694 }
13695 else
13696 redisplay_internal ();
13697
13698 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13699 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13700 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13701 }
13702
13703
13704 /* Function registered with record_unwind_protect in redisplay_internal. */
13705
13706 static void
13707 unwind_redisplay (void)
13708 {
13709 redisplaying_p = 0;
13710 }
13711
13712
13713 /* Mark the display of leaf window W as accurate or inaccurate.
13714 If ACCURATE_P is non-zero mark display of W as accurate. If
13715 ACCURATE_P is zero, arrange for W to be redisplayed the next
13716 time redisplay_internal is called. */
13717
13718 static void
13719 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13720 {
13721 struct buffer *b = XBUFFER (w->contents);
13722
13723 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
13724 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
13725 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13726
13727 if (accurate_p)
13728 {
13729 b->clip_changed = 0;
13730 b->prevent_redisplay_optimizations_p = 0;
13731
13732 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13733 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13734 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13735 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13736
13737 w->current_matrix->buffer = b;
13738 w->current_matrix->begv = BUF_BEGV (b);
13739 w->current_matrix->zv = BUF_ZV (b);
13740
13741 w->last_cursor = w->cursor;
13742 w->last_cursor_off_p = w->cursor_off_p;
13743
13744 if (w == XWINDOW (selected_window))
13745 w->last_point = BUF_PT (b);
13746 else
13747 w->last_point = marker_position (w->pointm);
13748
13749 w->window_end_valid = 1;
13750 w->update_mode_line = 0;
13751 }
13752 }
13753
13754
13755 /* Mark the display of windows in the window tree rooted at WINDOW as
13756 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13757 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13758 be redisplayed the next time redisplay_internal is called. */
13759
13760 void
13761 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13762 {
13763 struct window *w;
13764
13765 for (; !NILP (window); window = w->next)
13766 {
13767 w = XWINDOW (window);
13768 if (WINDOWP (w->contents))
13769 mark_window_display_accurate (w->contents, accurate_p);
13770 else
13771 mark_window_display_accurate_1 (w, accurate_p);
13772 }
13773
13774 if (accurate_p)
13775 update_overlay_arrows (1);
13776 else
13777 /* Force a thorough redisplay the next time by setting
13778 last_arrow_position and last_arrow_string to t, which is
13779 unequal to any useful value of Voverlay_arrow_... */
13780 update_overlay_arrows (-1);
13781 }
13782
13783
13784 /* Return value in display table DP (Lisp_Char_Table *) for character
13785 C. Since a display table doesn't have any parent, we don't have to
13786 follow parent. Do not call this function directly but use the
13787 macro DISP_CHAR_VECTOR. */
13788
13789 Lisp_Object
13790 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13791 {
13792 Lisp_Object val;
13793
13794 if (ASCII_CHAR_P (c))
13795 {
13796 val = dp->ascii;
13797 if (SUB_CHAR_TABLE_P (val))
13798 val = XSUB_CHAR_TABLE (val)->contents[c];
13799 }
13800 else
13801 {
13802 Lisp_Object table;
13803
13804 XSETCHAR_TABLE (table, dp);
13805 val = char_table_ref (table, c);
13806 }
13807 if (NILP (val))
13808 val = dp->defalt;
13809 return val;
13810 }
13811
13812
13813 \f
13814 /***********************************************************************
13815 Window Redisplay
13816 ***********************************************************************/
13817
13818 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13819
13820 static void
13821 redisplay_windows (Lisp_Object window)
13822 {
13823 while (!NILP (window))
13824 {
13825 struct window *w = XWINDOW (window);
13826
13827 if (WINDOWP (w->contents))
13828 redisplay_windows (w->contents);
13829 else if (BUFFERP (w->contents))
13830 {
13831 displayed_buffer = XBUFFER (w->contents);
13832 /* Use list_of_error, not Qerror, so that
13833 we catch only errors and don't run the debugger. */
13834 internal_condition_case_1 (redisplay_window_0, window,
13835 list_of_error,
13836 redisplay_window_error);
13837 }
13838
13839 window = w->next;
13840 }
13841 }
13842
13843 static Lisp_Object
13844 redisplay_window_error (Lisp_Object ignore)
13845 {
13846 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13847 return Qnil;
13848 }
13849
13850 static Lisp_Object
13851 redisplay_window_0 (Lisp_Object window)
13852 {
13853 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13854 redisplay_window (window, 0);
13855 return Qnil;
13856 }
13857
13858 static Lisp_Object
13859 redisplay_window_1 (Lisp_Object window)
13860 {
13861 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13862 redisplay_window (window, 1);
13863 return Qnil;
13864 }
13865 \f
13866
13867 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13868 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13869 which positions recorded in ROW differ from current buffer
13870 positions.
13871
13872 Return 0 if cursor is not on this row, 1 otherwise. */
13873
13874 static int
13875 set_cursor_from_row (struct window *w, struct glyph_row *row,
13876 struct glyph_matrix *matrix,
13877 ptrdiff_t delta, ptrdiff_t delta_bytes,
13878 int dy, int dvpos)
13879 {
13880 struct glyph *glyph = row->glyphs[TEXT_AREA];
13881 struct glyph *end = glyph + row->used[TEXT_AREA];
13882 struct glyph *cursor = NULL;
13883 /* The last known character position in row. */
13884 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13885 int x = row->x;
13886 ptrdiff_t pt_old = PT - delta;
13887 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13888 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13889 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13890 /* A glyph beyond the edge of TEXT_AREA which we should never
13891 touch. */
13892 struct glyph *glyphs_end = end;
13893 /* Non-zero means we've found a match for cursor position, but that
13894 glyph has the avoid_cursor_p flag set. */
13895 int match_with_avoid_cursor = 0;
13896 /* Non-zero means we've seen at least one glyph that came from a
13897 display string. */
13898 int string_seen = 0;
13899 /* Largest and smallest buffer positions seen so far during scan of
13900 glyph row. */
13901 ptrdiff_t bpos_max = pos_before;
13902 ptrdiff_t bpos_min = pos_after;
13903 /* Last buffer position covered by an overlay string with an integer
13904 `cursor' property. */
13905 ptrdiff_t bpos_covered = 0;
13906 /* Non-zero means the display string on which to display the cursor
13907 comes from a text property, not from an overlay. */
13908 int string_from_text_prop = 0;
13909
13910 /* Don't even try doing anything if called for a mode-line or
13911 header-line row, since the rest of the code isn't prepared to
13912 deal with such calamities. */
13913 eassert (!row->mode_line_p);
13914 if (row->mode_line_p)
13915 return 0;
13916
13917 /* Skip over glyphs not having an object at the start and the end of
13918 the row. These are special glyphs like truncation marks on
13919 terminal frames. */
13920 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13921 {
13922 if (!row->reversed_p)
13923 {
13924 while (glyph < end
13925 && INTEGERP (glyph->object)
13926 && glyph->charpos < 0)
13927 {
13928 x += glyph->pixel_width;
13929 ++glyph;
13930 }
13931 while (end > glyph
13932 && INTEGERP ((end - 1)->object)
13933 /* CHARPOS is zero for blanks and stretch glyphs
13934 inserted by extend_face_to_end_of_line. */
13935 && (end - 1)->charpos <= 0)
13936 --end;
13937 glyph_before = glyph - 1;
13938 glyph_after = end;
13939 }
13940 else
13941 {
13942 struct glyph *g;
13943
13944 /* If the glyph row is reversed, we need to process it from back
13945 to front, so swap the edge pointers. */
13946 glyphs_end = end = glyph - 1;
13947 glyph += row->used[TEXT_AREA] - 1;
13948
13949 while (glyph > end + 1
13950 && INTEGERP (glyph->object)
13951 && glyph->charpos < 0)
13952 {
13953 --glyph;
13954 x -= glyph->pixel_width;
13955 }
13956 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13957 --glyph;
13958 /* By default, in reversed rows we put the cursor on the
13959 rightmost (first in the reading order) glyph. */
13960 for (g = end + 1; g < glyph; g++)
13961 x += g->pixel_width;
13962 while (end < glyph
13963 && INTEGERP ((end + 1)->object)
13964 && (end + 1)->charpos <= 0)
13965 ++end;
13966 glyph_before = glyph + 1;
13967 glyph_after = end;
13968 }
13969 }
13970 else if (row->reversed_p)
13971 {
13972 /* In R2L rows that don't display text, put the cursor on the
13973 rightmost glyph. Case in point: an empty last line that is
13974 part of an R2L paragraph. */
13975 cursor = end - 1;
13976 /* Avoid placing the cursor on the last glyph of the row, where
13977 on terminal frames we hold the vertical border between
13978 adjacent windows. */
13979 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13980 && !WINDOW_RIGHTMOST_P (w)
13981 && cursor == row->glyphs[LAST_AREA] - 1)
13982 cursor--;
13983 x = -1; /* will be computed below, at label compute_x */
13984 }
13985
13986 /* Step 1: Try to find the glyph whose character position
13987 corresponds to point. If that's not possible, find 2 glyphs
13988 whose character positions are the closest to point, one before
13989 point, the other after it. */
13990 if (!row->reversed_p)
13991 while (/* not marched to end of glyph row */
13992 glyph < end
13993 /* glyph was not inserted by redisplay for internal purposes */
13994 && !INTEGERP (glyph->object))
13995 {
13996 if (BUFFERP (glyph->object))
13997 {
13998 ptrdiff_t dpos = glyph->charpos - pt_old;
13999
14000 if (glyph->charpos > bpos_max)
14001 bpos_max = glyph->charpos;
14002 if (glyph->charpos < bpos_min)
14003 bpos_min = glyph->charpos;
14004 if (!glyph->avoid_cursor_p)
14005 {
14006 /* If we hit point, we've found the glyph on which to
14007 display the cursor. */
14008 if (dpos == 0)
14009 {
14010 match_with_avoid_cursor = 0;
14011 break;
14012 }
14013 /* See if we've found a better approximation to
14014 POS_BEFORE or to POS_AFTER. */
14015 if (0 > dpos && dpos > pos_before - pt_old)
14016 {
14017 pos_before = glyph->charpos;
14018 glyph_before = glyph;
14019 }
14020 else if (0 < dpos && dpos < pos_after - pt_old)
14021 {
14022 pos_after = glyph->charpos;
14023 glyph_after = glyph;
14024 }
14025 }
14026 else if (dpos == 0)
14027 match_with_avoid_cursor = 1;
14028 }
14029 else if (STRINGP (glyph->object))
14030 {
14031 Lisp_Object chprop;
14032 ptrdiff_t glyph_pos = glyph->charpos;
14033
14034 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14035 glyph->object);
14036 if (!NILP (chprop))
14037 {
14038 /* If the string came from a `display' text property,
14039 look up the buffer position of that property and
14040 use that position to update bpos_max, as if we
14041 actually saw such a position in one of the row's
14042 glyphs. This helps with supporting integer values
14043 of `cursor' property on the display string in
14044 situations where most or all of the row's buffer
14045 text is completely covered by display properties,
14046 so that no glyph with valid buffer positions is
14047 ever seen in the row. */
14048 ptrdiff_t prop_pos =
14049 string_buffer_position_lim (glyph->object, pos_before,
14050 pos_after, 0);
14051
14052 if (prop_pos >= pos_before)
14053 bpos_max = prop_pos - 1;
14054 }
14055 if (INTEGERP (chprop))
14056 {
14057 bpos_covered = bpos_max + XINT (chprop);
14058 /* If the `cursor' property covers buffer positions up
14059 to and including point, we should display cursor on
14060 this glyph. Note that, if a `cursor' property on one
14061 of the string's characters has an integer value, we
14062 will break out of the loop below _before_ we get to
14063 the position match above. IOW, integer values of
14064 the `cursor' property override the "exact match for
14065 point" strategy of positioning the cursor. */
14066 /* Implementation note: bpos_max == pt_old when, e.g.,
14067 we are in an empty line, where bpos_max is set to
14068 MATRIX_ROW_START_CHARPOS, see above. */
14069 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14070 {
14071 cursor = glyph;
14072 break;
14073 }
14074 }
14075
14076 string_seen = 1;
14077 }
14078 x += glyph->pixel_width;
14079 ++glyph;
14080 }
14081 else if (glyph > end) /* row is reversed */
14082 while (!INTEGERP (glyph->object))
14083 {
14084 if (BUFFERP (glyph->object))
14085 {
14086 ptrdiff_t dpos = glyph->charpos - pt_old;
14087
14088 if (glyph->charpos > bpos_max)
14089 bpos_max = glyph->charpos;
14090 if (glyph->charpos < bpos_min)
14091 bpos_min = glyph->charpos;
14092 if (!glyph->avoid_cursor_p)
14093 {
14094 if (dpos == 0)
14095 {
14096 match_with_avoid_cursor = 0;
14097 break;
14098 }
14099 if (0 > dpos && dpos > pos_before - pt_old)
14100 {
14101 pos_before = glyph->charpos;
14102 glyph_before = glyph;
14103 }
14104 else if (0 < dpos && dpos < pos_after - pt_old)
14105 {
14106 pos_after = glyph->charpos;
14107 glyph_after = glyph;
14108 }
14109 }
14110 else if (dpos == 0)
14111 match_with_avoid_cursor = 1;
14112 }
14113 else if (STRINGP (glyph->object))
14114 {
14115 Lisp_Object chprop;
14116 ptrdiff_t glyph_pos = glyph->charpos;
14117
14118 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14119 glyph->object);
14120 if (!NILP (chprop))
14121 {
14122 ptrdiff_t prop_pos =
14123 string_buffer_position_lim (glyph->object, pos_before,
14124 pos_after, 0);
14125
14126 if (prop_pos >= pos_before)
14127 bpos_max = prop_pos - 1;
14128 }
14129 if (INTEGERP (chprop))
14130 {
14131 bpos_covered = bpos_max + XINT (chprop);
14132 /* If the `cursor' property covers buffer positions up
14133 to and including point, we should display cursor on
14134 this glyph. */
14135 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14136 {
14137 cursor = glyph;
14138 break;
14139 }
14140 }
14141 string_seen = 1;
14142 }
14143 --glyph;
14144 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14145 {
14146 x--; /* can't use any pixel_width */
14147 break;
14148 }
14149 x -= glyph->pixel_width;
14150 }
14151
14152 /* Step 2: If we didn't find an exact match for point, we need to
14153 look for a proper place to put the cursor among glyphs between
14154 GLYPH_BEFORE and GLYPH_AFTER. */
14155 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14156 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14157 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14158 {
14159 /* An empty line has a single glyph whose OBJECT is zero and
14160 whose CHARPOS is the position of a newline on that line.
14161 Note that on a TTY, there are more glyphs after that, which
14162 were produced by extend_face_to_end_of_line, but their
14163 CHARPOS is zero or negative. */
14164 int empty_line_p =
14165 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14166 && INTEGERP (glyph->object) && glyph->charpos > 0
14167 /* On a TTY, continued and truncated rows also have a glyph at
14168 their end whose OBJECT is zero and whose CHARPOS is
14169 positive (the continuation and truncation glyphs), but such
14170 rows are obviously not "empty". */
14171 && !(row->continued_p || row->truncated_on_right_p);
14172
14173 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14174 {
14175 ptrdiff_t ellipsis_pos;
14176
14177 /* Scan back over the ellipsis glyphs. */
14178 if (!row->reversed_p)
14179 {
14180 ellipsis_pos = (glyph - 1)->charpos;
14181 while (glyph > row->glyphs[TEXT_AREA]
14182 && (glyph - 1)->charpos == ellipsis_pos)
14183 glyph--, x -= glyph->pixel_width;
14184 /* That loop always goes one position too far, including
14185 the glyph before the ellipsis. So scan forward over
14186 that one. */
14187 x += glyph->pixel_width;
14188 glyph++;
14189 }
14190 else /* row is reversed */
14191 {
14192 ellipsis_pos = (glyph + 1)->charpos;
14193 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14194 && (glyph + 1)->charpos == ellipsis_pos)
14195 glyph++, x += glyph->pixel_width;
14196 x -= glyph->pixel_width;
14197 glyph--;
14198 }
14199 }
14200 else if (match_with_avoid_cursor)
14201 {
14202 cursor = glyph_after;
14203 x = -1;
14204 }
14205 else if (string_seen)
14206 {
14207 int incr = row->reversed_p ? -1 : +1;
14208
14209 /* Need to find the glyph that came out of a string which is
14210 present at point. That glyph is somewhere between
14211 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14212 positioned between POS_BEFORE and POS_AFTER in the
14213 buffer. */
14214 struct glyph *start, *stop;
14215 ptrdiff_t pos = pos_before;
14216
14217 x = -1;
14218
14219 /* If the row ends in a newline from a display string,
14220 reordering could have moved the glyphs belonging to the
14221 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14222 in this case we extend the search to the last glyph in
14223 the row that was not inserted by redisplay. */
14224 if (row->ends_in_newline_from_string_p)
14225 {
14226 glyph_after = end;
14227 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14228 }
14229
14230 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14231 correspond to POS_BEFORE and POS_AFTER, respectively. We
14232 need START and STOP in the order that corresponds to the
14233 row's direction as given by its reversed_p flag. If the
14234 directionality of characters between POS_BEFORE and
14235 POS_AFTER is the opposite of the row's base direction,
14236 these characters will have been reordered for display,
14237 and we need to reverse START and STOP. */
14238 if (!row->reversed_p)
14239 {
14240 start = min (glyph_before, glyph_after);
14241 stop = max (glyph_before, glyph_after);
14242 }
14243 else
14244 {
14245 start = max (glyph_before, glyph_after);
14246 stop = min (glyph_before, glyph_after);
14247 }
14248 for (glyph = start + incr;
14249 row->reversed_p ? glyph > stop : glyph < stop; )
14250 {
14251
14252 /* Any glyphs that come from the buffer are here because
14253 of bidi reordering. Skip them, and only pay
14254 attention to glyphs that came from some string. */
14255 if (STRINGP (glyph->object))
14256 {
14257 Lisp_Object str;
14258 ptrdiff_t tem;
14259 /* If the display property covers the newline, we
14260 need to search for it one position farther. */
14261 ptrdiff_t lim = pos_after
14262 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14263
14264 string_from_text_prop = 0;
14265 str = glyph->object;
14266 tem = string_buffer_position_lim (str, pos, lim, 0);
14267 if (tem == 0 /* from overlay */
14268 || pos <= tem)
14269 {
14270 /* If the string from which this glyph came is
14271 found in the buffer at point, or at position
14272 that is closer to point than pos_after, then
14273 we've found the glyph we've been looking for.
14274 If it comes from an overlay (tem == 0), and
14275 it has the `cursor' property on one of its
14276 glyphs, record that glyph as a candidate for
14277 displaying the cursor. (As in the
14278 unidirectional version, we will display the
14279 cursor on the last candidate we find.) */
14280 if (tem == 0
14281 || tem == pt_old
14282 || (tem - pt_old > 0 && tem < pos_after))
14283 {
14284 /* The glyphs from this string could have
14285 been reordered. Find the one with the
14286 smallest string position. Or there could
14287 be a character in the string with the
14288 `cursor' property, which means display
14289 cursor on that character's glyph. */
14290 ptrdiff_t strpos = glyph->charpos;
14291
14292 if (tem)
14293 {
14294 cursor = glyph;
14295 string_from_text_prop = 1;
14296 }
14297 for ( ;
14298 (row->reversed_p ? glyph > stop : glyph < stop)
14299 && EQ (glyph->object, str);
14300 glyph += incr)
14301 {
14302 Lisp_Object cprop;
14303 ptrdiff_t gpos = glyph->charpos;
14304
14305 cprop = Fget_char_property (make_number (gpos),
14306 Qcursor,
14307 glyph->object);
14308 if (!NILP (cprop))
14309 {
14310 cursor = glyph;
14311 break;
14312 }
14313 if (tem && glyph->charpos < strpos)
14314 {
14315 strpos = glyph->charpos;
14316 cursor = glyph;
14317 }
14318 }
14319
14320 if (tem == pt_old
14321 || (tem - pt_old > 0 && tem < pos_after))
14322 goto compute_x;
14323 }
14324 if (tem)
14325 pos = tem + 1; /* don't find previous instances */
14326 }
14327 /* This string is not what we want; skip all of the
14328 glyphs that came from it. */
14329 while ((row->reversed_p ? glyph > stop : glyph < stop)
14330 && EQ (glyph->object, str))
14331 glyph += incr;
14332 }
14333 else
14334 glyph += incr;
14335 }
14336
14337 /* If we reached the end of the line, and END was from a string,
14338 the cursor is not on this line. */
14339 if (cursor == NULL
14340 && (row->reversed_p ? glyph <= end : glyph >= end)
14341 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14342 && STRINGP (end->object)
14343 && row->continued_p)
14344 return 0;
14345 }
14346 /* A truncated row may not include PT among its character positions.
14347 Setting the cursor inside the scroll margin will trigger
14348 recalculation of hscroll in hscroll_window_tree. But if a
14349 display string covers point, defer to the string-handling
14350 code below to figure this out. */
14351 else if (row->truncated_on_left_p && pt_old < bpos_min)
14352 {
14353 cursor = glyph_before;
14354 x = -1;
14355 }
14356 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14357 /* Zero-width characters produce no glyphs. */
14358 || (!empty_line_p
14359 && (row->reversed_p
14360 ? glyph_after > glyphs_end
14361 : glyph_after < glyphs_end)))
14362 {
14363 cursor = glyph_after;
14364 x = -1;
14365 }
14366 }
14367
14368 compute_x:
14369 if (cursor != NULL)
14370 glyph = cursor;
14371 else if (glyph == glyphs_end
14372 && pos_before == pos_after
14373 && STRINGP ((row->reversed_p
14374 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14375 : row->glyphs[TEXT_AREA])->object))
14376 {
14377 /* If all the glyphs of this row came from strings, put the
14378 cursor on the first glyph of the row. This avoids having the
14379 cursor outside of the text area in this very rare and hard
14380 use case. */
14381 glyph =
14382 row->reversed_p
14383 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14384 : row->glyphs[TEXT_AREA];
14385 }
14386 if (x < 0)
14387 {
14388 struct glyph *g;
14389
14390 /* Need to compute x that corresponds to GLYPH. */
14391 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14392 {
14393 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14394 emacs_abort ();
14395 x += g->pixel_width;
14396 }
14397 }
14398
14399 /* ROW could be part of a continued line, which, under bidi
14400 reordering, might have other rows whose start and end charpos
14401 occlude point. Only set w->cursor if we found a better
14402 approximation to the cursor position than we have from previously
14403 examined candidate rows belonging to the same continued line. */
14404 if (/* we already have a candidate row */
14405 w->cursor.vpos >= 0
14406 /* that candidate is not the row we are processing */
14407 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14408 /* Make sure cursor.vpos specifies a row whose start and end
14409 charpos occlude point, and it is valid candidate for being a
14410 cursor-row. This is because some callers of this function
14411 leave cursor.vpos at the row where the cursor was displayed
14412 during the last redisplay cycle. */
14413 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14414 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14415 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14416 {
14417 struct glyph *g1 =
14418 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14419
14420 /* Don't consider glyphs that are outside TEXT_AREA. */
14421 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14422 return 0;
14423 /* Keep the candidate whose buffer position is the closest to
14424 point or has the `cursor' property. */
14425 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14426 w->cursor.hpos >= 0
14427 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14428 && ((BUFFERP (g1->object)
14429 && (g1->charpos == pt_old /* an exact match always wins */
14430 || (BUFFERP (glyph->object)
14431 && eabs (g1->charpos - pt_old)
14432 < eabs (glyph->charpos - pt_old))))
14433 /* previous candidate is a glyph from a string that has
14434 a non-nil `cursor' property */
14435 || (STRINGP (g1->object)
14436 && (!NILP (Fget_char_property (make_number (g1->charpos),
14437 Qcursor, g1->object))
14438 /* previous candidate is from the same display
14439 string as this one, and the display string
14440 came from a text property */
14441 || (EQ (g1->object, glyph->object)
14442 && string_from_text_prop)
14443 /* this candidate is from newline and its
14444 position is not an exact match */
14445 || (INTEGERP (glyph->object)
14446 && glyph->charpos != pt_old)))))
14447 return 0;
14448 /* If this candidate gives an exact match, use that. */
14449 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14450 /* If this candidate is a glyph created for the
14451 terminating newline of a line, and point is on that
14452 newline, it wins because it's an exact match. */
14453 || (!row->continued_p
14454 && INTEGERP (glyph->object)
14455 && glyph->charpos == 0
14456 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14457 /* Otherwise, keep the candidate that comes from a row
14458 spanning less buffer positions. This may win when one or
14459 both candidate positions are on glyphs that came from
14460 display strings, for which we cannot compare buffer
14461 positions. */
14462 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14463 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14464 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14465 return 0;
14466 }
14467 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14468 w->cursor.x = x;
14469 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14470 w->cursor.y = row->y + dy;
14471
14472 if (w == XWINDOW (selected_window))
14473 {
14474 if (!row->continued_p
14475 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14476 && row->x == 0)
14477 {
14478 this_line_buffer = XBUFFER (w->contents);
14479
14480 CHARPOS (this_line_start_pos)
14481 = MATRIX_ROW_START_CHARPOS (row) + delta;
14482 BYTEPOS (this_line_start_pos)
14483 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14484
14485 CHARPOS (this_line_end_pos)
14486 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14487 BYTEPOS (this_line_end_pos)
14488 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14489
14490 this_line_y = w->cursor.y;
14491 this_line_pixel_height = row->height;
14492 this_line_vpos = w->cursor.vpos;
14493 this_line_start_x = row->x;
14494 }
14495 else
14496 CHARPOS (this_line_start_pos) = 0;
14497 }
14498
14499 return 1;
14500 }
14501
14502
14503 /* Run window scroll functions, if any, for WINDOW with new window
14504 start STARTP. Sets the window start of WINDOW to that position.
14505
14506 We assume that the window's buffer is really current. */
14507
14508 static struct text_pos
14509 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14510 {
14511 struct window *w = XWINDOW (window);
14512 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14513
14514 eassert (current_buffer == XBUFFER (w->contents));
14515
14516 if (!NILP (Vwindow_scroll_functions))
14517 {
14518 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14519 make_number (CHARPOS (startp)));
14520 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14521 /* In case the hook functions switch buffers. */
14522 set_buffer_internal (XBUFFER (w->contents));
14523 }
14524
14525 return startp;
14526 }
14527
14528
14529 /* Make sure the line containing the cursor is fully visible.
14530 A value of 1 means there is nothing to be done.
14531 (Either the line is fully visible, or it cannot be made so,
14532 or we cannot tell.)
14533
14534 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14535 is higher than window.
14536
14537 A value of 0 means the caller should do scrolling
14538 as if point had gone off the screen. */
14539
14540 static int
14541 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14542 {
14543 struct glyph_matrix *matrix;
14544 struct glyph_row *row;
14545 int window_height;
14546
14547 if (!make_cursor_line_fully_visible_p)
14548 return 1;
14549
14550 /* It's not always possible to find the cursor, e.g, when a window
14551 is full of overlay strings. Don't do anything in that case. */
14552 if (w->cursor.vpos < 0)
14553 return 1;
14554
14555 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14556 row = MATRIX_ROW (matrix, w->cursor.vpos);
14557
14558 /* If the cursor row is not partially visible, there's nothing to do. */
14559 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14560 return 1;
14561
14562 /* If the row the cursor is in is taller than the window's height,
14563 it's not clear what to do, so do nothing. */
14564 window_height = window_box_height (w);
14565 if (row->height >= window_height)
14566 {
14567 if (!force_p || MINI_WINDOW_P (w)
14568 || w->vscroll || w->cursor.vpos == 0)
14569 return 1;
14570 }
14571 return 0;
14572 }
14573
14574
14575 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14576 non-zero means only WINDOW is redisplayed in redisplay_internal.
14577 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14578 in redisplay_window to bring a partially visible line into view in
14579 the case that only the cursor has moved.
14580
14581 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14582 last screen line's vertical height extends past the end of the screen.
14583
14584 Value is
14585
14586 1 if scrolling succeeded
14587
14588 0 if scrolling didn't find point.
14589
14590 -1 if new fonts have been loaded so that we must interrupt
14591 redisplay, adjust glyph matrices, and try again. */
14592
14593 enum
14594 {
14595 SCROLLING_SUCCESS,
14596 SCROLLING_FAILED,
14597 SCROLLING_NEED_LARGER_MATRICES
14598 };
14599
14600 /* If scroll-conservatively is more than this, never recenter.
14601
14602 If you change this, don't forget to update the doc string of
14603 `scroll-conservatively' and the Emacs manual. */
14604 #define SCROLL_LIMIT 100
14605
14606 static int
14607 try_scrolling (Lisp_Object window, int just_this_one_p,
14608 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14609 int temp_scroll_step, int last_line_misfit)
14610 {
14611 struct window *w = XWINDOW (window);
14612 struct frame *f = XFRAME (w->frame);
14613 struct text_pos pos, startp;
14614 struct it it;
14615 int this_scroll_margin, scroll_max, rc, height;
14616 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14617 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14618 Lisp_Object aggressive;
14619 /* We will never try scrolling more than this number of lines. */
14620 int scroll_limit = SCROLL_LIMIT;
14621 int frame_line_height = default_line_pixel_height (w);
14622 int window_total_lines
14623 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14624
14625 #ifdef GLYPH_DEBUG
14626 debug_method_add (w, "try_scrolling");
14627 #endif
14628
14629 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14630
14631 /* Compute scroll margin height in pixels. We scroll when point is
14632 within this distance from the top or bottom of the window. */
14633 if (scroll_margin > 0)
14634 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
14635 * frame_line_height;
14636 else
14637 this_scroll_margin = 0;
14638
14639 /* Force arg_scroll_conservatively to have a reasonable value, to
14640 avoid scrolling too far away with slow move_it_* functions. Note
14641 that the user can supply scroll-conservatively equal to
14642 `most-positive-fixnum', which can be larger than INT_MAX. */
14643 if (arg_scroll_conservatively > scroll_limit)
14644 {
14645 arg_scroll_conservatively = scroll_limit + 1;
14646 scroll_max = scroll_limit * frame_line_height;
14647 }
14648 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14649 /* Compute how much we should try to scroll maximally to bring
14650 point into view. */
14651 scroll_max = (max (scroll_step,
14652 max (arg_scroll_conservatively, temp_scroll_step))
14653 * frame_line_height);
14654 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14655 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14656 /* We're trying to scroll because of aggressive scrolling but no
14657 scroll_step is set. Choose an arbitrary one. */
14658 scroll_max = 10 * frame_line_height;
14659 else
14660 scroll_max = 0;
14661
14662 too_near_end:
14663
14664 /* Decide whether to scroll down. */
14665 if (PT > CHARPOS (startp))
14666 {
14667 int scroll_margin_y;
14668
14669 /* Compute the pixel ypos of the scroll margin, then move IT to
14670 either that ypos or PT, whichever comes first. */
14671 start_display (&it, w, startp);
14672 scroll_margin_y = it.last_visible_y - this_scroll_margin
14673 - frame_line_height * extra_scroll_margin_lines;
14674 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14675 (MOVE_TO_POS | MOVE_TO_Y));
14676
14677 if (PT > CHARPOS (it.current.pos))
14678 {
14679 int y0 = line_bottom_y (&it);
14680 /* Compute how many pixels below window bottom to stop searching
14681 for PT. This avoids costly search for PT that is far away if
14682 the user limited scrolling by a small number of lines, but
14683 always finds PT if scroll_conservatively is set to a large
14684 number, such as most-positive-fixnum. */
14685 int slack = max (scroll_max, 10 * frame_line_height);
14686 int y_to_move = it.last_visible_y + slack;
14687
14688 /* Compute the distance from the scroll margin to PT or to
14689 the scroll limit, whichever comes first. This should
14690 include the height of the cursor line, to make that line
14691 fully visible. */
14692 move_it_to (&it, PT, -1, y_to_move,
14693 -1, MOVE_TO_POS | MOVE_TO_Y);
14694 dy = line_bottom_y (&it) - y0;
14695
14696 if (dy > scroll_max)
14697 return SCROLLING_FAILED;
14698
14699 if (dy > 0)
14700 scroll_down_p = 1;
14701 }
14702 }
14703
14704 if (scroll_down_p)
14705 {
14706 /* Point is in or below the bottom scroll margin, so move the
14707 window start down. If scrolling conservatively, move it just
14708 enough down to make point visible. If scroll_step is set,
14709 move it down by scroll_step. */
14710 if (arg_scroll_conservatively)
14711 amount_to_scroll
14712 = min (max (dy, frame_line_height),
14713 frame_line_height * arg_scroll_conservatively);
14714 else if (scroll_step || temp_scroll_step)
14715 amount_to_scroll = scroll_max;
14716 else
14717 {
14718 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14719 height = WINDOW_BOX_TEXT_HEIGHT (w);
14720 if (NUMBERP (aggressive))
14721 {
14722 double float_amount = XFLOATINT (aggressive) * height;
14723 int aggressive_scroll = float_amount;
14724 if (aggressive_scroll == 0 && float_amount > 0)
14725 aggressive_scroll = 1;
14726 /* Don't let point enter the scroll margin near top of
14727 the window. This could happen if the value of
14728 scroll_up_aggressively is too large and there are
14729 non-zero margins, because scroll_up_aggressively
14730 means put point that fraction of window height
14731 _from_the_bottom_margin_. */
14732 if (aggressive_scroll + 2*this_scroll_margin > height)
14733 aggressive_scroll = height - 2*this_scroll_margin;
14734 amount_to_scroll = dy + aggressive_scroll;
14735 }
14736 }
14737
14738 if (amount_to_scroll <= 0)
14739 return SCROLLING_FAILED;
14740
14741 start_display (&it, w, startp);
14742 if (arg_scroll_conservatively <= scroll_limit)
14743 move_it_vertically (&it, amount_to_scroll);
14744 else
14745 {
14746 /* Extra precision for users who set scroll-conservatively
14747 to a large number: make sure the amount we scroll
14748 the window start is never less than amount_to_scroll,
14749 which was computed as distance from window bottom to
14750 point. This matters when lines at window top and lines
14751 below window bottom have different height. */
14752 struct it it1;
14753 void *it1data = NULL;
14754 /* We use a temporary it1 because line_bottom_y can modify
14755 its argument, if it moves one line down; see there. */
14756 int start_y;
14757
14758 SAVE_IT (it1, it, it1data);
14759 start_y = line_bottom_y (&it1);
14760 do {
14761 RESTORE_IT (&it, &it, it1data);
14762 move_it_by_lines (&it, 1);
14763 SAVE_IT (it1, it, it1data);
14764 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14765 }
14766
14767 /* If STARTP is unchanged, move it down another screen line. */
14768 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14769 move_it_by_lines (&it, 1);
14770 startp = it.current.pos;
14771 }
14772 else
14773 {
14774 struct text_pos scroll_margin_pos = startp;
14775 int y_offset = 0;
14776
14777 /* See if point is inside the scroll margin at the top of the
14778 window. */
14779 if (this_scroll_margin)
14780 {
14781 int y_start;
14782
14783 start_display (&it, w, startp);
14784 y_start = it.current_y;
14785 move_it_vertically (&it, this_scroll_margin);
14786 scroll_margin_pos = it.current.pos;
14787 /* If we didn't move enough before hitting ZV, request
14788 additional amount of scroll, to move point out of the
14789 scroll margin. */
14790 if (IT_CHARPOS (it) == ZV
14791 && it.current_y - y_start < this_scroll_margin)
14792 y_offset = this_scroll_margin - (it.current_y - y_start);
14793 }
14794
14795 if (PT < CHARPOS (scroll_margin_pos))
14796 {
14797 /* Point is in the scroll margin at the top of the window or
14798 above what is displayed in the window. */
14799 int y0, y_to_move;
14800
14801 /* Compute the vertical distance from PT to the scroll
14802 margin position. Move as far as scroll_max allows, or
14803 one screenful, or 10 screen lines, whichever is largest.
14804 Give up if distance is greater than scroll_max or if we
14805 didn't reach the scroll margin position. */
14806 SET_TEXT_POS (pos, PT, PT_BYTE);
14807 start_display (&it, w, pos);
14808 y0 = it.current_y;
14809 y_to_move = max (it.last_visible_y,
14810 max (scroll_max, 10 * frame_line_height));
14811 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14812 y_to_move, -1,
14813 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14814 dy = it.current_y - y0;
14815 if (dy > scroll_max
14816 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
14817 return SCROLLING_FAILED;
14818
14819 /* Additional scroll for when ZV was too close to point. */
14820 dy += y_offset;
14821
14822 /* Compute new window start. */
14823 start_display (&it, w, startp);
14824
14825 if (arg_scroll_conservatively)
14826 amount_to_scroll = max (dy, frame_line_height *
14827 max (scroll_step, temp_scroll_step));
14828 else if (scroll_step || temp_scroll_step)
14829 amount_to_scroll = scroll_max;
14830 else
14831 {
14832 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14833 height = WINDOW_BOX_TEXT_HEIGHT (w);
14834 if (NUMBERP (aggressive))
14835 {
14836 double float_amount = XFLOATINT (aggressive) * height;
14837 int aggressive_scroll = float_amount;
14838 if (aggressive_scroll == 0 && float_amount > 0)
14839 aggressive_scroll = 1;
14840 /* Don't let point enter the scroll margin near
14841 bottom of the window, if the value of
14842 scroll_down_aggressively happens to be too
14843 large. */
14844 if (aggressive_scroll + 2*this_scroll_margin > height)
14845 aggressive_scroll = height - 2*this_scroll_margin;
14846 amount_to_scroll = dy + aggressive_scroll;
14847 }
14848 }
14849
14850 if (amount_to_scroll <= 0)
14851 return SCROLLING_FAILED;
14852
14853 move_it_vertically_backward (&it, amount_to_scroll);
14854 startp = it.current.pos;
14855 }
14856 }
14857
14858 /* Run window scroll functions. */
14859 startp = run_window_scroll_functions (window, startp);
14860
14861 /* Display the window. Give up if new fonts are loaded, or if point
14862 doesn't appear. */
14863 if (!try_window (window, startp, 0))
14864 rc = SCROLLING_NEED_LARGER_MATRICES;
14865 else if (w->cursor.vpos < 0)
14866 {
14867 clear_glyph_matrix (w->desired_matrix);
14868 rc = SCROLLING_FAILED;
14869 }
14870 else
14871 {
14872 /* Maybe forget recorded base line for line number display. */
14873 if (!just_this_one_p
14874 || current_buffer->clip_changed
14875 || BEG_UNCHANGED < CHARPOS (startp))
14876 w->base_line_number = 0;
14877
14878 /* If cursor ends up on a partially visible line,
14879 treat that as being off the bottom of the screen. */
14880 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14881 /* It's possible that the cursor is on the first line of the
14882 buffer, which is partially obscured due to a vscroll
14883 (Bug#7537). In that case, avoid looping forever . */
14884 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14885 {
14886 clear_glyph_matrix (w->desired_matrix);
14887 ++extra_scroll_margin_lines;
14888 goto too_near_end;
14889 }
14890 rc = SCROLLING_SUCCESS;
14891 }
14892
14893 return rc;
14894 }
14895
14896
14897 /* Compute a suitable window start for window W if display of W starts
14898 on a continuation line. Value is non-zero if a new window start
14899 was computed.
14900
14901 The new window start will be computed, based on W's width, starting
14902 from the start of the continued line. It is the start of the
14903 screen line with the minimum distance from the old start W->start. */
14904
14905 static int
14906 compute_window_start_on_continuation_line (struct window *w)
14907 {
14908 struct text_pos pos, start_pos;
14909 int window_start_changed_p = 0;
14910
14911 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14912
14913 /* If window start is on a continuation line... Window start may be
14914 < BEGV in case there's invisible text at the start of the
14915 buffer (M-x rmail, for example). */
14916 if (CHARPOS (start_pos) > BEGV
14917 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14918 {
14919 struct it it;
14920 struct glyph_row *row;
14921
14922 /* Handle the case that the window start is out of range. */
14923 if (CHARPOS (start_pos) < BEGV)
14924 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14925 else if (CHARPOS (start_pos) > ZV)
14926 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14927
14928 /* Find the start of the continued line. This should be fast
14929 because find_newline is fast (newline cache). */
14930 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14931 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14932 row, DEFAULT_FACE_ID);
14933 reseat_at_previous_visible_line_start (&it);
14934
14935 /* If the line start is "too far" away from the window start,
14936 say it takes too much time to compute a new window start. */
14937 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14938 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14939 {
14940 int min_distance, distance;
14941
14942 /* Move forward by display lines to find the new window
14943 start. If window width was enlarged, the new start can
14944 be expected to be > the old start. If window width was
14945 decreased, the new window start will be < the old start.
14946 So, we're looking for the display line start with the
14947 minimum distance from the old window start. */
14948 pos = it.current.pos;
14949 min_distance = INFINITY;
14950 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14951 distance < min_distance)
14952 {
14953 min_distance = distance;
14954 pos = it.current.pos;
14955 if (it.line_wrap == WORD_WRAP)
14956 {
14957 /* Under WORD_WRAP, move_it_by_lines is likely to
14958 overshoot and stop not at the first, but the
14959 second character from the left margin. So in
14960 that case, we need a more tight control on the X
14961 coordinate of the iterator than move_it_by_lines
14962 promises in its contract. The method is to first
14963 go to the last (rightmost) visible character of a
14964 line, then move to the leftmost character on the
14965 next line in a separate call. */
14966 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
14967 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14968 move_it_to (&it, ZV, 0,
14969 it.current_y + it.max_ascent + it.max_descent, -1,
14970 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14971 }
14972 else
14973 move_it_by_lines (&it, 1);
14974 }
14975
14976 /* Set the window start there. */
14977 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14978 window_start_changed_p = 1;
14979 }
14980 }
14981
14982 return window_start_changed_p;
14983 }
14984
14985
14986 /* Try cursor movement in case text has not changed in window WINDOW,
14987 with window start STARTP. Value is
14988
14989 CURSOR_MOVEMENT_SUCCESS if successful
14990
14991 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14992
14993 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14994 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14995 we want to scroll as if scroll-step were set to 1. See the code.
14996
14997 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14998 which case we have to abort this redisplay, and adjust matrices
14999 first. */
15000
15001 enum
15002 {
15003 CURSOR_MOVEMENT_SUCCESS,
15004 CURSOR_MOVEMENT_CANNOT_BE_USED,
15005 CURSOR_MOVEMENT_MUST_SCROLL,
15006 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15007 };
15008
15009 static int
15010 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
15011 {
15012 struct window *w = XWINDOW (window);
15013 struct frame *f = XFRAME (w->frame);
15014 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15015
15016 #ifdef GLYPH_DEBUG
15017 if (inhibit_try_cursor_movement)
15018 return rc;
15019 #endif
15020
15021 /* Previously, there was a check for Lisp integer in the
15022 if-statement below. Now, this field is converted to
15023 ptrdiff_t, thus zero means invalid position in a buffer. */
15024 eassert (w->last_point > 0);
15025 /* Likewise there was a check whether window_end_vpos is nil or larger
15026 than the window. Now window_end_vpos is int and so never nil, but
15027 let's leave eassert to check whether it fits in the window. */
15028 eassert (w->window_end_vpos < w->current_matrix->nrows);
15029
15030 /* Handle case where text has not changed, only point, and it has
15031 not moved off the frame. */
15032 if (/* Point may be in this window. */
15033 PT >= CHARPOS (startp)
15034 /* Selective display hasn't changed. */
15035 && !current_buffer->clip_changed
15036 /* Function force-mode-line-update is used to force a thorough
15037 redisplay. It sets either windows_or_buffers_changed or
15038 update_mode_lines. So don't take a shortcut here for these
15039 cases. */
15040 && !update_mode_lines
15041 && !windows_or_buffers_changed
15042 && !cursor_type_changed
15043 /* Can't use this case if highlighting a region. When a
15044 region exists, cursor movement has to do more than just
15045 set the cursor. */
15046 && markpos_of_region () < 0
15047 && !w->region_showing
15048 && NILP (Vshow_trailing_whitespace)
15049 /* This code is not used for mini-buffer for the sake of the case
15050 of redisplaying to replace an echo area message; since in
15051 that case the mini-buffer contents per se are usually
15052 unchanged. This code is of no real use in the mini-buffer
15053 since the handling of this_line_start_pos, etc., in redisplay
15054 handles the same cases. */
15055 && !EQ (window, minibuf_window)
15056 && (FRAME_WINDOW_P (f)
15057 || !overlay_arrow_in_current_buffer_p ()))
15058 {
15059 int this_scroll_margin, top_scroll_margin;
15060 struct glyph_row *row = NULL;
15061 int frame_line_height = default_line_pixel_height (w);
15062 int window_total_lines
15063 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15064
15065 #ifdef GLYPH_DEBUG
15066 debug_method_add (w, "cursor movement");
15067 #endif
15068
15069 /* Scroll if point within this distance from the top or bottom
15070 of the window. This is a pixel value. */
15071 if (scroll_margin > 0)
15072 {
15073 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15074 this_scroll_margin *= frame_line_height;
15075 }
15076 else
15077 this_scroll_margin = 0;
15078
15079 top_scroll_margin = this_scroll_margin;
15080 if (WINDOW_WANTS_HEADER_LINE_P (w))
15081 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15082
15083 /* Start with the row the cursor was displayed during the last
15084 not paused redisplay. Give up if that row is not valid. */
15085 if (w->last_cursor.vpos < 0
15086 || w->last_cursor.vpos >= w->current_matrix->nrows)
15087 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15088 else
15089 {
15090 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
15091 if (row->mode_line_p)
15092 ++row;
15093 if (!row->enabled_p)
15094 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15095 }
15096
15097 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15098 {
15099 int scroll_p = 0, must_scroll = 0;
15100 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15101
15102 if (PT > w->last_point)
15103 {
15104 /* Point has moved forward. */
15105 while (MATRIX_ROW_END_CHARPOS (row) < PT
15106 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15107 {
15108 eassert (row->enabled_p);
15109 ++row;
15110 }
15111
15112 /* If the end position of a row equals the start
15113 position of the next row, and PT is at that position,
15114 we would rather display cursor in the next line. */
15115 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15116 && MATRIX_ROW_END_CHARPOS (row) == PT
15117 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15118 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15119 && !cursor_row_p (row))
15120 ++row;
15121
15122 /* If within the scroll margin, scroll. Note that
15123 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15124 the next line would be drawn, and that
15125 this_scroll_margin can be zero. */
15126 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15127 || PT > MATRIX_ROW_END_CHARPOS (row)
15128 /* Line is completely visible last line in window
15129 and PT is to be set in the next line. */
15130 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15131 && PT == MATRIX_ROW_END_CHARPOS (row)
15132 && !row->ends_at_zv_p
15133 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15134 scroll_p = 1;
15135 }
15136 else if (PT < w->last_point)
15137 {
15138 /* Cursor has to be moved backward. Note that PT >=
15139 CHARPOS (startp) because of the outer if-statement. */
15140 while (!row->mode_line_p
15141 && (MATRIX_ROW_START_CHARPOS (row) > PT
15142 || (MATRIX_ROW_START_CHARPOS (row) == PT
15143 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15144 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15145 row > w->current_matrix->rows
15146 && (row-1)->ends_in_newline_from_string_p))))
15147 && (row->y > top_scroll_margin
15148 || CHARPOS (startp) == BEGV))
15149 {
15150 eassert (row->enabled_p);
15151 --row;
15152 }
15153
15154 /* Consider the following case: Window starts at BEGV,
15155 there is invisible, intangible text at BEGV, so that
15156 display starts at some point START > BEGV. It can
15157 happen that we are called with PT somewhere between
15158 BEGV and START. Try to handle that case. */
15159 if (row < w->current_matrix->rows
15160 || row->mode_line_p)
15161 {
15162 row = w->current_matrix->rows;
15163 if (row->mode_line_p)
15164 ++row;
15165 }
15166
15167 /* Due to newlines in overlay strings, we may have to
15168 skip forward over overlay strings. */
15169 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15170 && MATRIX_ROW_END_CHARPOS (row) == PT
15171 && !cursor_row_p (row))
15172 ++row;
15173
15174 /* If within the scroll margin, scroll. */
15175 if (row->y < top_scroll_margin
15176 && CHARPOS (startp) != BEGV)
15177 scroll_p = 1;
15178 }
15179 else
15180 {
15181 /* Cursor did not move. So don't scroll even if cursor line
15182 is partially visible, as it was so before. */
15183 rc = CURSOR_MOVEMENT_SUCCESS;
15184 }
15185
15186 if (PT < MATRIX_ROW_START_CHARPOS (row)
15187 || PT > MATRIX_ROW_END_CHARPOS (row))
15188 {
15189 /* if PT is not in the glyph row, give up. */
15190 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15191 must_scroll = 1;
15192 }
15193 else if (rc != CURSOR_MOVEMENT_SUCCESS
15194 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15195 {
15196 struct glyph_row *row1;
15197
15198 /* If rows are bidi-reordered and point moved, back up
15199 until we find a row that does not belong to a
15200 continuation line. This is because we must consider
15201 all rows of a continued line as candidates for the
15202 new cursor positioning, since row start and end
15203 positions change non-linearly with vertical position
15204 in such rows. */
15205 /* FIXME: Revisit this when glyph ``spilling'' in
15206 continuation lines' rows is implemented for
15207 bidi-reordered rows. */
15208 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15209 MATRIX_ROW_CONTINUATION_LINE_P (row);
15210 --row)
15211 {
15212 /* If we hit the beginning of the displayed portion
15213 without finding the first row of a continued
15214 line, give up. */
15215 if (row <= row1)
15216 {
15217 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15218 break;
15219 }
15220 eassert (row->enabled_p);
15221 }
15222 }
15223 if (must_scroll)
15224 ;
15225 else if (rc != CURSOR_MOVEMENT_SUCCESS
15226 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15227 /* Make sure this isn't a header line by any chance, since
15228 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15229 && !row->mode_line_p
15230 && make_cursor_line_fully_visible_p)
15231 {
15232 if (PT == MATRIX_ROW_END_CHARPOS (row)
15233 && !row->ends_at_zv_p
15234 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15235 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15236 else if (row->height > window_box_height (w))
15237 {
15238 /* If we end up in a partially visible line, let's
15239 make it fully visible, except when it's taller
15240 than the window, in which case we can't do much
15241 about it. */
15242 *scroll_step = 1;
15243 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15244 }
15245 else
15246 {
15247 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15248 if (!cursor_row_fully_visible_p (w, 0, 1))
15249 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15250 else
15251 rc = CURSOR_MOVEMENT_SUCCESS;
15252 }
15253 }
15254 else if (scroll_p)
15255 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15256 else if (rc != CURSOR_MOVEMENT_SUCCESS
15257 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15258 {
15259 /* With bidi-reordered rows, there could be more than
15260 one candidate row whose start and end positions
15261 occlude point. We need to let set_cursor_from_row
15262 find the best candidate. */
15263 /* FIXME: Revisit this when glyph ``spilling'' in
15264 continuation lines' rows is implemented for
15265 bidi-reordered rows. */
15266 int rv = 0;
15267
15268 do
15269 {
15270 int at_zv_p = 0, exact_match_p = 0;
15271
15272 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15273 && PT <= MATRIX_ROW_END_CHARPOS (row)
15274 && cursor_row_p (row))
15275 rv |= set_cursor_from_row (w, row, w->current_matrix,
15276 0, 0, 0, 0);
15277 /* As soon as we've found the exact match for point,
15278 or the first suitable row whose ends_at_zv_p flag
15279 is set, we are done. */
15280 at_zv_p =
15281 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15282 if (rv && !at_zv_p
15283 && w->cursor.hpos >= 0
15284 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15285 w->cursor.vpos))
15286 {
15287 struct glyph_row *candidate =
15288 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15289 struct glyph *g =
15290 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15291 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15292
15293 exact_match_p =
15294 (BUFFERP (g->object) && g->charpos == PT)
15295 || (INTEGERP (g->object)
15296 && (g->charpos == PT
15297 || (g->charpos == 0 && endpos - 1 == PT)));
15298 }
15299 if (rv && (at_zv_p || exact_match_p))
15300 {
15301 rc = CURSOR_MOVEMENT_SUCCESS;
15302 break;
15303 }
15304 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15305 break;
15306 ++row;
15307 }
15308 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15309 || row->continued_p)
15310 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15311 || (MATRIX_ROW_START_CHARPOS (row) == PT
15312 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15313 /* If we didn't find any candidate rows, or exited the
15314 loop before all the candidates were examined, signal
15315 to the caller that this method failed. */
15316 if (rc != CURSOR_MOVEMENT_SUCCESS
15317 && !(rv
15318 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15319 && !row->continued_p))
15320 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15321 else if (rv)
15322 rc = CURSOR_MOVEMENT_SUCCESS;
15323 }
15324 else
15325 {
15326 do
15327 {
15328 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15329 {
15330 rc = CURSOR_MOVEMENT_SUCCESS;
15331 break;
15332 }
15333 ++row;
15334 }
15335 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15336 && MATRIX_ROW_START_CHARPOS (row) == PT
15337 && cursor_row_p (row));
15338 }
15339 }
15340 }
15341
15342 return rc;
15343 }
15344
15345 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15346 static
15347 #endif
15348 void
15349 set_vertical_scroll_bar (struct window *w)
15350 {
15351 ptrdiff_t start, end, whole;
15352
15353 /* Calculate the start and end positions for the current window.
15354 At some point, it would be nice to choose between scrollbars
15355 which reflect the whole buffer size, with special markers
15356 indicating narrowing, and scrollbars which reflect only the
15357 visible region.
15358
15359 Note that mini-buffers sometimes aren't displaying any text. */
15360 if (!MINI_WINDOW_P (w)
15361 || (w == XWINDOW (minibuf_window)
15362 && NILP (echo_area_buffer[0])))
15363 {
15364 struct buffer *buf = XBUFFER (w->contents);
15365 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15366 start = marker_position (w->start) - BUF_BEGV (buf);
15367 /* I don't think this is guaranteed to be right. For the
15368 moment, we'll pretend it is. */
15369 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15370
15371 if (end < start)
15372 end = start;
15373 if (whole < (end - start))
15374 whole = end - start;
15375 }
15376 else
15377 start = end = whole = 0;
15378
15379 /* Indicate what this scroll bar ought to be displaying now. */
15380 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15381 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15382 (w, end - start, whole, start);
15383 }
15384
15385
15386 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15387 selected_window is redisplayed.
15388
15389 We can return without actually redisplaying the window if
15390 fonts_changed_p. In that case, redisplay_internal will
15391 retry. */
15392
15393 static void
15394 redisplay_window (Lisp_Object window, int just_this_one_p)
15395 {
15396 struct window *w = XWINDOW (window);
15397 struct frame *f = XFRAME (w->frame);
15398 struct buffer *buffer = XBUFFER (w->contents);
15399 struct buffer *old = current_buffer;
15400 struct text_pos lpoint, opoint, startp;
15401 int update_mode_line;
15402 int tem;
15403 struct it it;
15404 /* Record it now because it's overwritten. */
15405 int current_matrix_up_to_date_p = 0;
15406 int used_current_matrix_p = 0;
15407 /* This is less strict than current_matrix_up_to_date_p.
15408 It indicates that the buffer contents and narrowing are unchanged. */
15409 int buffer_unchanged_p = 0;
15410 int temp_scroll_step = 0;
15411 ptrdiff_t count = SPECPDL_INDEX ();
15412 int rc;
15413 int centering_position = -1;
15414 int last_line_misfit = 0;
15415 ptrdiff_t beg_unchanged, end_unchanged;
15416 int frame_line_height;
15417
15418 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15419 opoint = lpoint;
15420
15421 #ifdef GLYPH_DEBUG
15422 *w->desired_matrix->method = 0;
15423 #endif
15424
15425 /* Make sure that both W's markers are valid. */
15426 eassert (XMARKER (w->start)->buffer == buffer);
15427 eassert (XMARKER (w->pointm)->buffer == buffer);
15428
15429 restart:
15430 reconsider_clip_changes (w);
15431 frame_line_height = default_line_pixel_height (w);
15432
15433 /* Has the mode line to be updated? */
15434 update_mode_line = (w->update_mode_line
15435 || update_mode_lines
15436 || buffer->clip_changed
15437 || buffer->prevent_redisplay_optimizations_p);
15438
15439 if (MINI_WINDOW_P (w))
15440 {
15441 if (w == XWINDOW (echo_area_window)
15442 && !NILP (echo_area_buffer[0]))
15443 {
15444 if (update_mode_line)
15445 /* We may have to update a tty frame's menu bar or a
15446 tool-bar. Example `M-x C-h C-h C-g'. */
15447 goto finish_menu_bars;
15448 else
15449 /* We've already displayed the echo area glyphs in this window. */
15450 goto finish_scroll_bars;
15451 }
15452 else if ((w != XWINDOW (minibuf_window)
15453 || minibuf_level == 0)
15454 /* When buffer is nonempty, redisplay window normally. */
15455 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15456 /* Quail displays non-mini buffers in minibuffer window.
15457 In that case, redisplay the window normally. */
15458 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15459 {
15460 /* W is a mini-buffer window, but it's not active, so clear
15461 it. */
15462 int yb = window_text_bottom_y (w);
15463 struct glyph_row *row;
15464 int y;
15465
15466 for (y = 0, row = w->desired_matrix->rows;
15467 y < yb;
15468 y += row->height, ++row)
15469 blank_row (w, row, y);
15470 goto finish_scroll_bars;
15471 }
15472
15473 clear_glyph_matrix (w->desired_matrix);
15474 }
15475
15476 /* Otherwise set up data on this window; select its buffer and point
15477 value. */
15478 /* Really select the buffer, for the sake of buffer-local
15479 variables. */
15480 set_buffer_internal_1 (XBUFFER (w->contents));
15481
15482 current_matrix_up_to_date_p
15483 = (w->window_end_valid
15484 && !current_buffer->clip_changed
15485 && !current_buffer->prevent_redisplay_optimizations_p
15486 && !window_outdated (w));
15487
15488 /* Run the window-bottom-change-functions
15489 if it is possible that the text on the screen has changed
15490 (either due to modification of the text, or any other reason). */
15491 if (!current_matrix_up_to_date_p
15492 && !NILP (Vwindow_text_change_functions))
15493 {
15494 safe_run_hooks (Qwindow_text_change_functions);
15495 goto restart;
15496 }
15497
15498 beg_unchanged = BEG_UNCHANGED;
15499 end_unchanged = END_UNCHANGED;
15500
15501 SET_TEXT_POS (opoint, PT, PT_BYTE);
15502
15503 specbind (Qinhibit_point_motion_hooks, Qt);
15504
15505 buffer_unchanged_p
15506 = (w->window_end_valid
15507 && !current_buffer->clip_changed
15508 && !window_outdated (w));
15509
15510 /* When windows_or_buffers_changed is non-zero, we can't rely
15511 on the window end being valid, so set it to zero there. */
15512 if (windows_or_buffers_changed)
15513 {
15514 /* If window starts on a continuation line, maybe adjust the
15515 window start in case the window's width changed. */
15516 if (XMARKER (w->start)->buffer == current_buffer)
15517 compute_window_start_on_continuation_line (w);
15518
15519 w->window_end_valid = 0;
15520 /* If so, we also can't rely on current matrix
15521 and should not fool try_cursor_movement below. */
15522 current_matrix_up_to_date_p = 0;
15523 }
15524
15525 /* Some sanity checks. */
15526 CHECK_WINDOW_END (w);
15527 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15528 emacs_abort ();
15529 if (BYTEPOS (opoint) < CHARPOS (opoint))
15530 emacs_abort ();
15531
15532 if (mode_line_update_needed (w))
15533 update_mode_line = 1;
15534
15535 /* Point refers normally to the selected window. For any other
15536 window, set up appropriate value. */
15537 if (!EQ (window, selected_window))
15538 {
15539 ptrdiff_t new_pt = marker_position (w->pointm);
15540 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15541 if (new_pt < BEGV)
15542 {
15543 new_pt = BEGV;
15544 new_pt_byte = BEGV_BYTE;
15545 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15546 }
15547 else if (new_pt > (ZV - 1))
15548 {
15549 new_pt = ZV;
15550 new_pt_byte = ZV_BYTE;
15551 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15552 }
15553
15554 /* We don't use SET_PT so that the point-motion hooks don't run. */
15555 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15556 }
15557
15558 /* If any of the character widths specified in the display table
15559 have changed, invalidate the width run cache. It's true that
15560 this may be a bit late to catch such changes, but the rest of
15561 redisplay goes (non-fatally) haywire when the display table is
15562 changed, so why should we worry about doing any better? */
15563 if (current_buffer->width_run_cache)
15564 {
15565 struct Lisp_Char_Table *disptab = buffer_display_table ();
15566
15567 if (! disptab_matches_widthtab
15568 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15569 {
15570 invalidate_region_cache (current_buffer,
15571 current_buffer->width_run_cache,
15572 BEG, Z);
15573 recompute_width_table (current_buffer, disptab);
15574 }
15575 }
15576
15577 /* If window-start is screwed up, choose a new one. */
15578 if (XMARKER (w->start)->buffer != current_buffer)
15579 goto recenter;
15580
15581 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15582
15583 /* If someone specified a new starting point but did not insist,
15584 check whether it can be used. */
15585 if (w->optional_new_start
15586 && CHARPOS (startp) >= BEGV
15587 && CHARPOS (startp) <= ZV)
15588 {
15589 w->optional_new_start = 0;
15590 start_display (&it, w, startp);
15591 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15592 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15593 if (IT_CHARPOS (it) == PT)
15594 w->force_start = 1;
15595 /* IT may overshoot PT if text at PT is invisible. */
15596 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15597 w->force_start = 1;
15598 }
15599
15600 force_start:
15601
15602 /* Handle case where place to start displaying has been specified,
15603 unless the specified location is outside the accessible range. */
15604 if (w->force_start || window_frozen_p (w))
15605 {
15606 /* We set this later on if we have to adjust point. */
15607 int new_vpos = -1;
15608
15609 w->force_start = 0;
15610 w->vscroll = 0;
15611 w->window_end_valid = 0;
15612
15613 /* Forget any recorded base line for line number display. */
15614 if (!buffer_unchanged_p)
15615 w->base_line_number = 0;
15616
15617 /* Redisplay the mode line. Select the buffer properly for that.
15618 Also, run the hook window-scroll-functions
15619 because we have scrolled. */
15620 /* Note, we do this after clearing force_start because
15621 if there's an error, it is better to forget about force_start
15622 than to get into an infinite loop calling the hook functions
15623 and having them get more errors. */
15624 if (!update_mode_line
15625 || ! NILP (Vwindow_scroll_functions))
15626 {
15627 update_mode_line = 1;
15628 w->update_mode_line = 1;
15629 startp = run_window_scroll_functions (window, startp);
15630 }
15631
15632 if (CHARPOS (startp) < BEGV)
15633 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15634 else if (CHARPOS (startp) > ZV)
15635 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15636
15637 /* Redisplay, then check if cursor has been set during the
15638 redisplay. Give up if new fonts were loaded. */
15639 /* We used to issue a CHECK_MARGINS argument to try_window here,
15640 but this causes scrolling to fail when point begins inside
15641 the scroll margin (bug#148) -- cyd */
15642 if (!try_window (window, startp, 0))
15643 {
15644 w->force_start = 1;
15645 clear_glyph_matrix (w->desired_matrix);
15646 goto need_larger_matrices;
15647 }
15648
15649 if (w->cursor.vpos < 0 && !window_frozen_p (w))
15650 {
15651 /* If point does not appear, try to move point so it does
15652 appear. The desired matrix has been built above, so we
15653 can use it here. */
15654 new_vpos = window_box_height (w) / 2;
15655 }
15656
15657 if (!cursor_row_fully_visible_p (w, 0, 0))
15658 {
15659 /* Point does appear, but on a line partly visible at end of window.
15660 Move it back to a fully-visible line. */
15661 new_vpos = window_box_height (w);
15662 }
15663 else if (w->cursor.vpos >=0)
15664 {
15665 /* Some people insist on not letting point enter the scroll
15666 margin, even though this part handles windows that didn't
15667 scroll at all. */
15668 int window_total_lines
15669 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15670 int margin = min (scroll_margin, window_total_lines / 4);
15671 int pixel_margin = margin * frame_line_height;
15672 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15673
15674 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15675 below, which finds the row to move point to, advances by
15676 the Y coordinate of the _next_ row, see the definition of
15677 MATRIX_ROW_BOTTOM_Y. */
15678 if (w->cursor.vpos < margin + header_line)
15679 {
15680 w->cursor.vpos = -1;
15681 clear_glyph_matrix (w->desired_matrix);
15682 goto try_to_scroll;
15683 }
15684 else
15685 {
15686 int window_height = window_box_height (w);
15687
15688 if (header_line)
15689 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15690 if (w->cursor.y >= window_height - pixel_margin)
15691 {
15692 w->cursor.vpos = -1;
15693 clear_glyph_matrix (w->desired_matrix);
15694 goto try_to_scroll;
15695 }
15696 }
15697 }
15698
15699 /* If we need to move point for either of the above reasons,
15700 now actually do it. */
15701 if (new_vpos >= 0)
15702 {
15703 struct glyph_row *row;
15704
15705 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15706 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15707 ++row;
15708
15709 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15710 MATRIX_ROW_START_BYTEPOS (row));
15711
15712 if (w != XWINDOW (selected_window))
15713 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15714 else if (current_buffer == old)
15715 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15716
15717 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15718
15719 /* If we are highlighting the region, then we just changed
15720 the region, so redisplay to show it. */
15721 if (markpos_of_region () >= 0)
15722 {
15723 clear_glyph_matrix (w->desired_matrix);
15724 if (!try_window (window, startp, 0))
15725 goto need_larger_matrices;
15726 }
15727 }
15728
15729 #ifdef GLYPH_DEBUG
15730 debug_method_add (w, "forced window start");
15731 #endif
15732 goto done;
15733 }
15734
15735 /* Handle case where text has not changed, only point, and it has
15736 not moved off the frame, and we are not retrying after hscroll.
15737 (current_matrix_up_to_date_p is nonzero when retrying.) */
15738 if (current_matrix_up_to_date_p
15739 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15740 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15741 {
15742 switch (rc)
15743 {
15744 case CURSOR_MOVEMENT_SUCCESS:
15745 used_current_matrix_p = 1;
15746 goto done;
15747
15748 case CURSOR_MOVEMENT_MUST_SCROLL:
15749 goto try_to_scroll;
15750
15751 default:
15752 emacs_abort ();
15753 }
15754 }
15755 /* If current starting point was originally the beginning of a line
15756 but no longer is, find a new starting point. */
15757 else if (w->start_at_line_beg
15758 && !(CHARPOS (startp) <= BEGV
15759 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15760 {
15761 #ifdef GLYPH_DEBUG
15762 debug_method_add (w, "recenter 1");
15763 #endif
15764 goto recenter;
15765 }
15766
15767 /* Try scrolling with try_window_id. Value is > 0 if update has
15768 been done, it is -1 if we know that the same window start will
15769 not work. It is 0 if unsuccessful for some other reason. */
15770 else if ((tem = try_window_id (w)) != 0)
15771 {
15772 #ifdef GLYPH_DEBUG
15773 debug_method_add (w, "try_window_id %d", tem);
15774 #endif
15775
15776 if (fonts_changed_p)
15777 goto need_larger_matrices;
15778 if (tem > 0)
15779 goto done;
15780
15781 /* Otherwise try_window_id has returned -1 which means that we
15782 don't want the alternative below this comment to execute. */
15783 }
15784 else if (CHARPOS (startp) >= BEGV
15785 && CHARPOS (startp) <= ZV
15786 && PT >= CHARPOS (startp)
15787 && (CHARPOS (startp) < ZV
15788 /* Avoid starting at end of buffer. */
15789 || CHARPOS (startp) == BEGV
15790 || !window_outdated (w)))
15791 {
15792 int d1, d2, d3, d4, d5, d6;
15793
15794 /* If first window line is a continuation line, and window start
15795 is inside the modified region, but the first change is before
15796 current window start, we must select a new window start.
15797
15798 However, if this is the result of a down-mouse event (e.g. by
15799 extending the mouse-drag-overlay), we don't want to select a
15800 new window start, since that would change the position under
15801 the mouse, resulting in an unwanted mouse-movement rather
15802 than a simple mouse-click. */
15803 if (!w->start_at_line_beg
15804 && NILP (do_mouse_tracking)
15805 && CHARPOS (startp) > BEGV
15806 && CHARPOS (startp) > BEG + beg_unchanged
15807 && CHARPOS (startp) <= Z - end_unchanged
15808 /* Even if w->start_at_line_beg is nil, a new window may
15809 start at a line_beg, since that's how set_buffer_window
15810 sets it. So, we need to check the return value of
15811 compute_window_start_on_continuation_line. (See also
15812 bug#197). */
15813 && XMARKER (w->start)->buffer == current_buffer
15814 && compute_window_start_on_continuation_line (w)
15815 /* It doesn't make sense to force the window start like we
15816 do at label force_start if it is already known that point
15817 will not be visible in the resulting window, because
15818 doing so will move point from its correct position
15819 instead of scrolling the window to bring point into view.
15820 See bug#9324. */
15821 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15822 {
15823 w->force_start = 1;
15824 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15825 goto force_start;
15826 }
15827
15828 #ifdef GLYPH_DEBUG
15829 debug_method_add (w, "same window start");
15830 #endif
15831
15832 /* Try to redisplay starting at same place as before.
15833 If point has not moved off frame, accept the results. */
15834 if (!current_matrix_up_to_date_p
15835 /* Don't use try_window_reusing_current_matrix in this case
15836 because a window scroll function can have changed the
15837 buffer. */
15838 || !NILP (Vwindow_scroll_functions)
15839 || MINI_WINDOW_P (w)
15840 || !(used_current_matrix_p
15841 = try_window_reusing_current_matrix (w)))
15842 {
15843 IF_DEBUG (debug_method_add (w, "1"));
15844 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15845 /* -1 means we need to scroll.
15846 0 means we need new matrices, but fonts_changed_p
15847 is set in that case, so we will detect it below. */
15848 goto try_to_scroll;
15849 }
15850
15851 if (fonts_changed_p)
15852 goto need_larger_matrices;
15853
15854 if (w->cursor.vpos >= 0)
15855 {
15856 if (!just_this_one_p
15857 || current_buffer->clip_changed
15858 || BEG_UNCHANGED < CHARPOS (startp))
15859 /* Forget any recorded base line for line number display. */
15860 w->base_line_number = 0;
15861
15862 if (!cursor_row_fully_visible_p (w, 1, 0))
15863 {
15864 clear_glyph_matrix (w->desired_matrix);
15865 last_line_misfit = 1;
15866 }
15867 /* Drop through and scroll. */
15868 else
15869 goto done;
15870 }
15871 else
15872 clear_glyph_matrix (w->desired_matrix);
15873 }
15874
15875 try_to_scroll:
15876
15877 /* Redisplay the mode line. Select the buffer properly for that. */
15878 if (!update_mode_line)
15879 {
15880 update_mode_line = 1;
15881 w->update_mode_line = 1;
15882 }
15883
15884 /* Try to scroll by specified few lines. */
15885 if ((scroll_conservatively
15886 || emacs_scroll_step
15887 || temp_scroll_step
15888 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15889 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15890 && CHARPOS (startp) >= BEGV
15891 && CHARPOS (startp) <= ZV)
15892 {
15893 /* The function returns -1 if new fonts were loaded, 1 if
15894 successful, 0 if not successful. */
15895 int ss = try_scrolling (window, just_this_one_p,
15896 scroll_conservatively,
15897 emacs_scroll_step,
15898 temp_scroll_step, last_line_misfit);
15899 switch (ss)
15900 {
15901 case SCROLLING_SUCCESS:
15902 goto done;
15903
15904 case SCROLLING_NEED_LARGER_MATRICES:
15905 goto need_larger_matrices;
15906
15907 case SCROLLING_FAILED:
15908 break;
15909
15910 default:
15911 emacs_abort ();
15912 }
15913 }
15914
15915 /* Finally, just choose a place to start which positions point
15916 according to user preferences. */
15917
15918 recenter:
15919
15920 #ifdef GLYPH_DEBUG
15921 debug_method_add (w, "recenter");
15922 #endif
15923
15924 /* Forget any previously recorded base line for line number display. */
15925 if (!buffer_unchanged_p)
15926 w->base_line_number = 0;
15927
15928 /* Determine the window start relative to point. */
15929 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15930 it.current_y = it.last_visible_y;
15931 if (centering_position < 0)
15932 {
15933 int window_total_lines
15934 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15935 int margin =
15936 scroll_margin > 0
15937 ? min (scroll_margin, window_total_lines / 4)
15938 : 0;
15939 ptrdiff_t margin_pos = CHARPOS (startp);
15940 Lisp_Object aggressive;
15941 int scrolling_up;
15942
15943 /* If there is a scroll margin at the top of the window, find
15944 its character position. */
15945 if (margin
15946 /* Cannot call start_display if startp is not in the
15947 accessible region of the buffer. This can happen when we
15948 have just switched to a different buffer and/or changed
15949 its restriction. In that case, startp is initialized to
15950 the character position 1 (BEGV) because we did not yet
15951 have chance to display the buffer even once. */
15952 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15953 {
15954 struct it it1;
15955 void *it1data = NULL;
15956
15957 SAVE_IT (it1, it, it1data);
15958 start_display (&it1, w, startp);
15959 move_it_vertically (&it1, margin * frame_line_height);
15960 margin_pos = IT_CHARPOS (it1);
15961 RESTORE_IT (&it, &it, it1data);
15962 }
15963 scrolling_up = PT > margin_pos;
15964 aggressive =
15965 scrolling_up
15966 ? BVAR (current_buffer, scroll_up_aggressively)
15967 : BVAR (current_buffer, scroll_down_aggressively);
15968
15969 if (!MINI_WINDOW_P (w)
15970 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15971 {
15972 int pt_offset = 0;
15973
15974 /* Setting scroll-conservatively overrides
15975 scroll-*-aggressively. */
15976 if (!scroll_conservatively && NUMBERP (aggressive))
15977 {
15978 double float_amount = XFLOATINT (aggressive);
15979
15980 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15981 if (pt_offset == 0 && float_amount > 0)
15982 pt_offset = 1;
15983 if (pt_offset && margin > 0)
15984 margin -= 1;
15985 }
15986 /* Compute how much to move the window start backward from
15987 point so that point will be displayed where the user
15988 wants it. */
15989 if (scrolling_up)
15990 {
15991 centering_position = it.last_visible_y;
15992 if (pt_offset)
15993 centering_position -= pt_offset;
15994 centering_position -=
15995 frame_line_height * (1 + margin + (last_line_misfit != 0))
15996 + WINDOW_HEADER_LINE_HEIGHT (w);
15997 /* Don't let point enter the scroll margin near top of
15998 the window. */
15999 if (centering_position < margin * frame_line_height)
16000 centering_position = margin * frame_line_height;
16001 }
16002 else
16003 centering_position = margin * frame_line_height + pt_offset;
16004 }
16005 else
16006 /* Set the window start half the height of the window backward
16007 from point. */
16008 centering_position = window_box_height (w) / 2;
16009 }
16010 move_it_vertically_backward (&it, centering_position);
16011
16012 eassert (IT_CHARPOS (it) >= BEGV);
16013
16014 /* The function move_it_vertically_backward may move over more
16015 than the specified y-distance. If it->w is small, e.g. a
16016 mini-buffer window, we may end up in front of the window's
16017 display area. Start displaying at the start of the line
16018 containing PT in this case. */
16019 if (it.current_y <= 0)
16020 {
16021 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16022 move_it_vertically_backward (&it, 0);
16023 it.current_y = 0;
16024 }
16025
16026 it.current_x = it.hpos = 0;
16027
16028 /* Set the window start position here explicitly, to avoid an
16029 infinite loop in case the functions in window-scroll-functions
16030 get errors. */
16031 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16032
16033 /* Run scroll hooks. */
16034 startp = run_window_scroll_functions (window, it.current.pos);
16035
16036 /* Redisplay the window. */
16037 if (!current_matrix_up_to_date_p
16038 || windows_or_buffers_changed
16039 || cursor_type_changed
16040 /* Don't use try_window_reusing_current_matrix in this case
16041 because it can have changed the buffer. */
16042 || !NILP (Vwindow_scroll_functions)
16043 || !just_this_one_p
16044 || MINI_WINDOW_P (w)
16045 || !(used_current_matrix_p
16046 = try_window_reusing_current_matrix (w)))
16047 try_window (window, startp, 0);
16048
16049 /* If new fonts have been loaded (due to fontsets), give up. We
16050 have to start a new redisplay since we need to re-adjust glyph
16051 matrices. */
16052 if (fonts_changed_p)
16053 goto need_larger_matrices;
16054
16055 /* If cursor did not appear assume that the middle of the window is
16056 in the first line of the window. Do it again with the next line.
16057 (Imagine a window of height 100, displaying two lines of height
16058 60. Moving back 50 from it->last_visible_y will end in the first
16059 line.) */
16060 if (w->cursor.vpos < 0)
16061 {
16062 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16063 {
16064 clear_glyph_matrix (w->desired_matrix);
16065 move_it_by_lines (&it, 1);
16066 try_window (window, it.current.pos, 0);
16067 }
16068 else if (PT < IT_CHARPOS (it))
16069 {
16070 clear_glyph_matrix (w->desired_matrix);
16071 move_it_by_lines (&it, -1);
16072 try_window (window, it.current.pos, 0);
16073 }
16074 else
16075 {
16076 /* Not much we can do about it. */
16077 }
16078 }
16079
16080 /* Consider the following case: Window starts at BEGV, there is
16081 invisible, intangible text at BEGV, so that display starts at
16082 some point START > BEGV. It can happen that we are called with
16083 PT somewhere between BEGV and START. Try to handle that case. */
16084 if (w->cursor.vpos < 0)
16085 {
16086 struct glyph_row *row = w->current_matrix->rows;
16087 if (row->mode_line_p)
16088 ++row;
16089 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16090 }
16091
16092 if (!cursor_row_fully_visible_p (w, 0, 0))
16093 {
16094 /* If vscroll is enabled, disable it and try again. */
16095 if (w->vscroll)
16096 {
16097 w->vscroll = 0;
16098 clear_glyph_matrix (w->desired_matrix);
16099 goto recenter;
16100 }
16101
16102 /* Users who set scroll-conservatively to a large number want
16103 point just above/below the scroll margin. If we ended up
16104 with point's row partially visible, move the window start to
16105 make that row fully visible and out of the margin. */
16106 if (scroll_conservatively > SCROLL_LIMIT)
16107 {
16108 int window_total_lines
16109 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16110 int margin =
16111 scroll_margin > 0
16112 ? min (scroll_margin, window_total_lines / 4)
16113 : 0;
16114 int move_down = w->cursor.vpos >= window_total_lines / 2;
16115
16116 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16117 clear_glyph_matrix (w->desired_matrix);
16118 if (1 == try_window (window, it.current.pos,
16119 TRY_WINDOW_CHECK_MARGINS))
16120 goto done;
16121 }
16122
16123 /* If centering point failed to make the whole line visible,
16124 put point at the top instead. That has to make the whole line
16125 visible, if it can be done. */
16126 if (centering_position == 0)
16127 goto done;
16128
16129 clear_glyph_matrix (w->desired_matrix);
16130 centering_position = 0;
16131 goto recenter;
16132 }
16133
16134 done:
16135
16136 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16137 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16138 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16139
16140 /* Display the mode line, if we must. */
16141 if ((update_mode_line
16142 /* If window not full width, must redo its mode line
16143 if (a) the window to its side is being redone and
16144 (b) we do a frame-based redisplay. This is a consequence
16145 of how inverted lines are drawn in frame-based redisplay. */
16146 || (!just_this_one_p
16147 && !FRAME_WINDOW_P (f)
16148 && !WINDOW_FULL_WIDTH_P (w))
16149 /* Line number to display. */
16150 || w->base_line_pos > 0
16151 /* Column number is displayed and different from the one displayed. */
16152 || (w->column_number_displayed != -1
16153 && (w->column_number_displayed != current_column ())))
16154 /* This means that the window has a mode line. */
16155 && (WINDOW_WANTS_MODELINE_P (w)
16156 || WINDOW_WANTS_HEADER_LINE_P (w)))
16157 {
16158 display_mode_lines (w);
16159
16160 /* If mode line height has changed, arrange for a thorough
16161 immediate redisplay using the correct mode line height. */
16162 if (WINDOW_WANTS_MODELINE_P (w)
16163 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16164 {
16165 fonts_changed_p = 1;
16166 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16167 = DESIRED_MODE_LINE_HEIGHT (w);
16168 }
16169
16170 /* If header line height has changed, arrange for a thorough
16171 immediate redisplay using the correct header line height. */
16172 if (WINDOW_WANTS_HEADER_LINE_P (w)
16173 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16174 {
16175 fonts_changed_p = 1;
16176 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16177 = DESIRED_HEADER_LINE_HEIGHT (w);
16178 }
16179
16180 if (fonts_changed_p)
16181 goto need_larger_matrices;
16182 }
16183
16184 if (!line_number_displayed && w->base_line_pos != -1)
16185 {
16186 w->base_line_pos = 0;
16187 w->base_line_number = 0;
16188 }
16189
16190 finish_menu_bars:
16191
16192 /* When we reach a frame's selected window, redo the frame's menu bar. */
16193 if (update_mode_line
16194 && EQ (FRAME_SELECTED_WINDOW (f), window))
16195 {
16196 int redisplay_menu_p = 0;
16197
16198 if (FRAME_WINDOW_P (f))
16199 {
16200 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16201 || defined (HAVE_NS) || defined (USE_GTK)
16202 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16203 #else
16204 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16205 #endif
16206 }
16207 else
16208 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16209
16210 if (redisplay_menu_p)
16211 display_menu_bar (w);
16212
16213 #ifdef HAVE_WINDOW_SYSTEM
16214 if (FRAME_WINDOW_P (f))
16215 {
16216 #if defined (USE_GTK) || defined (HAVE_NS)
16217 if (FRAME_EXTERNAL_TOOL_BAR (f))
16218 redisplay_tool_bar (f);
16219 #else
16220 if (WINDOWP (f->tool_bar_window)
16221 && (FRAME_TOOL_BAR_LINES (f) > 0
16222 || !NILP (Vauto_resize_tool_bars))
16223 && redisplay_tool_bar (f))
16224 ignore_mouse_drag_p = 1;
16225 #endif
16226 }
16227 #endif
16228 }
16229
16230 #ifdef HAVE_WINDOW_SYSTEM
16231 if (FRAME_WINDOW_P (f)
16232 && update_window_fringes (w, (just_this_one_p
16233 || (!used_current_matrix_p && !overlay_arrow_seen)
16234 || w->pseudo_window_p)))
16235 {
16236 update_begin (f);
16237 block_input ();
16238 if (draw_window_fringes (w, 1))
16239 x_draw_vertical_border (w);
16240 unblock_input ();
16241 update_end (f);
16242 }
16243 #endif /* HAVE_WINDOW_SYSTEM */
16244
16245 /* We go to this label, with fonts_changed_p set,
16246 if it is necessary to try again using larger glyph matrices.
16247 We have to redeem the scroll bar even in this case,
16248 because the loop in redisplay_internal expects that. */
16249 need_larger_matrices:
16250 ;
16251 finish_scroll_bars:
16252
16253 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16254 {
16255 /* Set the thumb's position and size. */
16256 set_vertical_scroll_bar (w);
16257
16258 /* Note that we actually used the scroll bar attached to this
16259 window, so it shouldn't be deleted at the end of redisplay. */
16260 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16261 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16262 }
16263
16264 /* Restore current_buffer and value of point in it. The window
16265 update may have changed the buffer, so first make sure `opoint'
16266 is still valid (Bug#6177). */
16267 if (CHARPOS (opoint) < BEGV)
16268 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16269 else if (CHARPOS (opoint) > ZV)
16270 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16271 else
16272 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16273
16274 set_buffer_internal_1 (old);
16275 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16276 shorter. This can be caused by log truncation in *Messages*. */
16277 if (CHARPOS (lpoint) <= ZV)
16278 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16279
16280 unbind_to (count, Qnil);
16281 }
16282
16283
16284 /* Build the complete desired matrix of WINDOW with a window start
16285 buffer position POS.
16286
16287 Value is 1 if successful. It is zero if fonts were loaded during
16288 redisplay which makes re-adjusting glyph matrices necessary, and -1
16289 if point would appear in the scroll margins.
16290 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16291 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16292 set in FLAGS.) */
16293
16294 int
16295 try_window (Lisp_Object window, struct text_pos pos, int flags)
16296 {
16297 struct window *w = XWINDOW (window);
16298 struct it it;
16299 struct glyph_row *last_text_row = NULL;
16300 struct frame *f = XFRAME (w->frame);
16301 int frame_line_height = default_line_pixel_height (w);
16302
16303 /* Make POS the new window start. */
16304 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16305
16306 /* Mark cursor position as unknown. No overlay arrow seen. */
16307 w->cursor.vpos = -1;
16308 overlay_arrow_seen = 0;
16309
16310 /* Initialize iterator and info to start at POS. */
16311 start_display (&it, w, pos);
16312
16313 /* Display all lines of W. */
16314 while (it.current_y < it.last_visible_y)
16315 {
16316 if (display_line (&it))
16317 last_text_row = it.glyph_row - 1;
16318 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16319 return 0;
16320 }
16321
16322 /* Don't let the cursor end in the scroll margins. */
16323 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16324 && !MINI_WINDOW_P (w))
16325 {
16326 int this_scroll_margin;
16327 int window_total_lines
16328 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16329
16330 if (scroll_margin > 0)
16331 {
16332 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16333 this_scroll_margin *= frame_line_height;
16334 }
16335 else
16336 this_scroll_margin = 0;
16337
16338 if ((w->cursor.y >= 0 /* not vscrolled */
16339 && w->cursor.y < this_scroll_margin
16340 && CHARPOS (pos) > BEGV
16341 && IT_CHARPOS (it) < ZV)
16342 /* rms: considering make_cursor_line_fully_visible_p here
16343 seems to give wrong results. We don't want to recenter
16344 when the last line is partly visible, we want to allow
16345 that case to be handled in the usual way. */
16346 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16347 {
16348 w->cursor.vpos = -1;
16349 clear_glyph_matrix (w->desired_matrix);
16350 return -1;
16351 }
16352 }
16353
16354 /* If bottom moved off end of frame, change mode line percentage. */
16355 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
16356 w->update_mode_line = 1;
16357
16358 /* Set window_end_pos to the offset of the last character displayed
16359 on the window from the end of current_buffer. Set
16360 window_end_vpos to its row number. */
16361 if (last_text_row)
16362 {
16363 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16364 adjust_window_ends (w, last_text_row, 0);
16365 eassert
16366 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16367 w->window_end_vpos)));
16368 }
16369 else
16370 {
16371 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16372 w->window_end_pos = Z - ZV;
16373 w->window_end_vpos = 0;
16374 }
16375
16376 /* But that is not valid info until redisplay finishes. */
16377 w->window_end_valid = 0;
16378 return 1;
16379 }
16380
16381
16382 \f
16383 /************************************************************************
16384 Window redisplay reusing current matrix when buffer has not changed
16385 ************************************************************************/
16386
16387 /* Try redisplay of window W showing an unchanged buffer with a
16388 different window start than the last time it was displayed by
16389 reusing its current matrix. Value is non-zero if successful.
16390 W->start is the new window start. */
16391
16392 static int
16393 try_window_reusing_current_matrix (struct window *w)
16394 {
16395 struct frame *f = XFRAME (w->frame);
16396 struct glyph_row *bottom_row;
16397 struct it it;
16398 struct run run;
16399 struct text_pos start, new_start;
16400 int nrows_scrolled, i;
16401 struct glyph_row *last_text_row;
16402 struct glyph_row *last_reused_text_row;
16403 struct glyph_row *start_row;
16404 int start_vpos, min_y, max_y;
16405
16406 #ifdef GLYPH_DEBUG
16407 if (inhibit_try_window_reusing)
16408 return 0;
16409 #endif
16410
16411 if (/* This function doesn't handle terminal frames. */
16412 !FRAME_WINDOW_P (f)
16413 /* Don't try to reuse the display if windows have been split
16414 or such. */
16415 || windows_or_buffers_changed
16416 || cursor_type_changed)
16417 return 0;
16418
16419 /* Can't do this if region may have changed. */
16420 if (markpos_of_region () >= 0
16421 || w->region_showing
16422 || !NILP (Vshow_trailing_whitespace))
16423 return 0;
16424
16425 /* If top-line visibility has changed, give up. */
16426 if (WINDOW_WANTS_HEADER_LINE_P (w)
16427 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16428 return 0;
16429
16430 /* Give up if old or new display is scrolled vertically. We could
16431 make this function handle this, but right now it doesn't. */
16432 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16433 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16434 return 0;
16435
16436 /* The variable new_start now holds the new window start. The old
16437 start `start' can be determined from the current matrix. */
16438 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16439 start = start_row->minpos;
16440 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16441
16442 /* Clear the desired matrix for the display below. */
16443 clear_glyph_matrix (w->desired_matrix);
16444
16445 if (CHARPOS (new_start) <= CHARPOS (start))
16446 {
16447 /* Don't use this method if the display starts with an ellipsis
16448 displayed for invisible text. It's not easy to handle that case
16449 below, and it's certainly not worth the effort since this is
16450 not a frequent case. */
16451 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16452 return 0;
16453
16454 IF_DEBUG (debug_method_add (w, "twu1"));
16455
16456 /* Display up to a row that can be reused. The variable
16457 last_text_row is set to the last row displayed that displays
16458 text. Note that it.vpos == 0 if or if not there is a
16459 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16460 start_display (&it, w, new_start);
16461 w->cursor.vpos = -1;
16462 last_text_row = last_reused_text_row = NULL;
16463
16464 while (it.current_y < it.last_visible_y
16465 && !fonts_changed_p)
16466 {
16467 /* If we have reached into the characters in the START row,
16468 that means the line boundaries have changed. So we
16469 can't start copying with the row START. Maybe it will
16470 work to start copying with the following row. */
16471 while (IT_CHARPOS (it) > CHARPOS (start))
16472 {
16473 /* Advance to the next row as the "start". */
16474 start_row++;
16475 start = start_row->minpos;
16476 /* If there are no more rows to try, or just one, give up. */
16477 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16478 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16479 || CHARPOS (start) == ZV)
16480 {
16481 clear_glyph_matrix (w->desired_matrix);
16482 return 0;
16483 }
16484
16485 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16486 }
16487 /* If we have reached alignment, we can copy the rest of the
16488 rows. */
16489 if (IT_CHARPOS (it) == CHARPOS (start)
16490 /* Don't accept "alignment" inside a display vector,
16491 since start_row could have started in the middle of
16492 that same display vector (thus their character
16493 positions match), and we have no way of telling if
16494 that is the case. */
16495 && it.current.dpvec_index < 0)
16496 break;
16497
16498 if (display_line (&it))
16499 last_text_row = it.glyph_row - 1;
16500
16501 }
16502
16503 /* A value of current_y < last_visible_y means that we stopped
16504 at the previous window start, which in turn means that we
16505 have at least one reusable row. */
16506 if (it.current_y < it.last_visible_y)
16507 {
16508 struct glyph_row *row;
16509
16510 /* IT.vpos always starts from 0; it counts text lines. */
16511 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16512
16513 /* Find PT if not already found in the lines displayed. */
16514 if (w->cursor.vpos < 0)
16515 {
16516 int dy = it.current_y - start_row->y;
16517
16518 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16519 row = row_containing_pos (w, PT, row, NULL, dy);
16520 if (row)
16521 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16522 dy, nrows_scrolled);
16523 else
16524 {
16525 clear_glyph_matrix (w->desired_matrix);
16526 return 0;
16527 }
16528 }
16529
16530 /* Scroll the display. Do it before the current matrix is
16531 changed. The problem here is that update has not yet
16532 run, i.e. part of the current matrix is not up to date.
16533 scroll_run_hook will clear the cursor, and use the
16534 current matrix to get the height of the row the cursor is
16535 in. */
16536 run.current_y = start_row->y;
16537 run.desired_y = it.current_y;
16538 run.height = it.last_visible_y - it.current_y;
16539
16540 if (run.height > 0 && run.current_y != run.desired_y)
16541 {
16542 update_begin (f);
16543 FRAME_RIF (f)->update_window_begin_hook (w);
16544 FRAME_RIF (f)->clear_window_mouse_face (w);
16545 FRAME_RIF (f)->scroll_run_hook (w, &run);
16546 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16547 update_end (f);
16548 }
16549
16550 /* Shift current matrix down by nrows_scrolled lines. */
16551 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16552 rotate_matrix (w->current_matrix,
16553 start_vpos,
16554 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16555 nrows_scrolled);
16556
16557 /* Disable lines that must be updated. */
16558 for (i = 0; i < nrows_scrolled; ++i)
16559 (start_row + i)->enabled_p = 0;
16560
16561 /* Re-compute Y positions. */
16562 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16563 max_y = it.last_visible_y;
16564 for (row = start_row + nrows_scrolled;
16565 row < bottom_row;
16566 ++row)
16567 {
16568 row->y = it.current_y;
16569 row->visible_height = row->height;
16570
16571 if (row->y < min_y)
16572 row->visible_height -= min_y - row->y;
16573 if (row->y + row->height > max_y)
16574 row->visible_height -= row->y + row->height - max_y;
16575 if (row->fringe_bitmap_periodic_p)
16576 row->redraw_fringe_bitmaps_p = 1;
16577
16578 it.current_y += row->height;
16579
16580 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16581 last_reused_text_row = row;
16582 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16583 break;
16584 }
16585
16586 /* Disable lines in the current matrix which are now
16587 below the window. */
16588 for (++row; row < bottom_row; ++row)
16589 row->enabled_p = row->mode_line_p = 0;
16590 }
16591
16592 /* Update window_end_pos etc.; last_reused_text_row is the last
16593 reused row from the current matrix containing text, if any.
16594 The value of last_text_row is the last displayed line
16595 containing text. */
16596 if (last_reused_text_row)
16597 adjust_window_ends (w, last_reused_text_row, 1);
16598 else if (last_text_row)
16599 adjust_window_ends (w, last_text_row, 0);
16600 else
16601 {
16602 /* This window must be completely empty. */
16603 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16604 w->window_end_pos = Z - ZV;
16605 w->window_end_vpos = 0;
16606 }
16607 w->window_end_valid = 0;
16608
16609 /* Update hint: don't try scrolling again in update_window. */
16610 w->desired_matrix->no_scrolling_p = 1;
16611
16612 #ifdef GLYPH_DEBUG
16613 debug_method_add (w, "try_window_reusing_current_matrix 1");
16614 #endif
16615 return 1;
16616 }
16617 else if (CHARPOS (new_start) > CHARPOS (start))
16618 {
16619 struct glyph_row *pt_row, *row;
16620 struct glyph_row *first_reusable_row;
16621 struct glyph_row *first_row_to_display;
16622 int dy;
16623 int yb = window_text_bottom_y (w);
16624
16625 /* Find the row starting at new_start, if there is one. Don't
16626 reuse a partially visible line at the end. */
16627 first_reusable_row = start_row;
16628 while (first_reusable_row->enabled_p
16629 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16630 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16631 < CHARPOS (new_start)))
16632 ++first_reusable_row;
16633
16634 /* Give up if there is no row to reuse. */
16635 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16636 || !first_reusable_row->enabled_p
16637 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16638 != CHARPOS (new_start)))
16639 return 0;
16640
16641 /* We can reuse fully visible rows beginning with
16642 first_reusable_row to the end of the window. Set
16643 first_row_to_display to the first row that cannot be reused.
16644 Set pt_row to the row containing point, if there is any. */
16645 pt_row = NULL;
16646 for (first_row_to_display = first_reusable_row;
16647 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16648 ++first_row_to_display)
16649 {
16650 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16651 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16652 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16653 && first_row_to_display->ends_at_zv_p
16654 && pt_row == NULL)))
16655 pt_row = first_row_to_display;
16656 }
16657
16658 /* Start displaying at the start of first_row_to_display. */
16659 eassert (first_row_to_display->y < yb);
16660 init_to_row_start (&it, w, first_row_to_display);
16661
16662 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16663 - start_vpos);
16664 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16665 - nrows_scrolled);
16666 it.current_y = (first_row_to_display->y - first_reusable_row->y
16667 + WINDOW_HEADER_LINE_HEIGHT (w));
16668
16669 /* Display lines beginning with first_row_to_display in the
16670 desired matrix. Set last_text_row to the last row displayed
16671 that displays text. */
16672 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16673 if (pt_row == NULL)
16674 w->cursor.vpos = -1;
16675 last_text_row = NULL;
16676 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16677 if (display_line (&it))
16678 last_text_row = it.glyph_row - 1;
16679
16680 /* If point is in a reused row, adjust y and vpos of the cursor
16681 position. */
16682 if (pt_row)
16683 {
16684 w->cursor.vpos -= nrows_scrolled;
16685 w->cursor.y -= first_reusable_row->y - start_row->y;
16686 }
16687
16688 /* Give up if point isn't in a row displayed or reused. (This
16689 also handles the case where w->cursor.vpos < nrows_scrolled
16690 after the calls to display_line, which can happen with scroll
16691 margins. See bug#1295.) */
16692 if (w->cursor.vpos < 0)
16693 {
16694 clear_glyph_matrix (w->desired_matrix);
16695 return 0;
16696 }
16697
16698 /* Scroll the display. */
16699 run.current_y = first_reusable_row->y;
16700 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16701 run.height = it.last_visible_y - run.current_y;
16702 dy = run.current_y - run.desired_y;
16703
16704 if (run.height)
16705 {
16706 update_begin (f);
16707 FRAME_RIF (f)->update_window_begin_hook (w);
16708 FRAME_RIF (f)->clear_window_mouse_face (w);
16709 FRAME_RIF (f)->scroll_run_hook (w, &run);
16710 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16711 update_end (f);
16712 }
16713
16714 /* Adjust Y positions of reused rows. */
16715 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16716 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16717 max_y = it.last_visible_y;
16718 for (row = first_reusable_row; row < first_row_to_display; ++row)
16719 {
16720 row->y -= dy;
16721 row->visible_height = row->height;
16722 if (row->y < min_y)
16723 row->visible_height -= min_y - row->y;
16724 if (row->y + row->height > max_y)
16725 row->visible_height -= row->y + row->height - max_y;
16726 if (row->fringe_bitmap_periodic_p)
16727 row->redraw_fringe_bitmaps_p = 1;
16728 }
16729
16730 /* Scroll the current matrix. */
16731 eassert (nrows_scrolled > 0);
16732 rotate_matrix (w->current_matrix,
16733 start_vpos,
16734 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16735 -nrows_scrolled);
16736
16737 /* Disable rows not reused. */
16738 for (row -= nrows_scrolled; row < bottom_row; ++row)
16739 row->enabled_p = 0;
16740
16741 /* Point may have moved to a different line, so we cannot assume that
16742 the previous cursor position is valid; locate the correct row. */
16743 if (pt_row)
16744 {
16745 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16746 row < bottom_row
16747 && PT >= MATRIX_ROW_END_CHARPOS (row)
16748 && !row->ends_at_zv_p;
16749 row++)
16750 {
16751 w->cursor.vpos++;
16752 w->cursor.y = row->y;
16753 }
16754 if (row < bottom_row)
16755 {
16756 /* Can't simply scan the row for point with
16757 bidi-reordered glyph rows. Let set_cursor_from_row
16758 figure out where to put the cursor, and if it fails,
16759 give up. */
16760 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16761 {
16762 if (!set_cursor_from_row (w, row, w->current_matrix,
16763 0, 0, 0, 0))
16764 {
16765 clear_glyph_matrix (w->desired_matrix);
16766 return 0;
16767 }
16768 }
16769 else
16770 {
16771 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16772 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16773
16774 for (; glyph < end
16775 && (!BUFFERP (glyph->object)
16776 || glyph->charpos < PT);
16777 glyph++)
16778 {
16779 w->cursor.hpos++;
16780 w->cursor.x += glyph->pixel_width;
16781 }
16782 }
16783 }
16784 }
16785
16786 /* Adjust window end. A null value of last_text_row means that
16787 the window end is in reused rows which in turn means that
16788 only its vpos can have changed. */
16789 if (last_text_row)
16790 adjust_window_ends (w, last_text_row, 0);
16791 else
16792 w->window_end_vpos -= nrows_scrolled;
16793
16794 w->window_end_valid = 0;
16795 w->desired_matrix->no_scrolling_p = 1;
16796
16797 #ifdef GLYPH_DEBUG
16798 debug_method_add (w, "try_window_reusing_current_matrix 2");
16799 #endif
16800 return 1;
16801 }
16802
16803 return 0;
16804 }
16805
16806
16807 \f
16808 /************************************************************************
16809 Window redisplay reusing current matrix when buffer has changed
16810 ************************************************************************/
16811
16812 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16813 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16814 ptrdiff_t *, ptrdiff_t *);
16815 static struct glyph_row *
16816 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16817 struct glyph_row *);
16818
16819
16820 /* Return the last row in MATRIX displaying text. If row START is
16821 non-null, start searching with that row. IT gives the dimensions
16822 of the display. Value is null if matrix is empty; otherwise it is
16823 a pointer to the row found. */
16824
16825 static struct glyph_row *
16826 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16827 struct glyph_row *start)
16828 {
16829 struct glyph_row *row, *row_found;
16830
16831 /* Set row_found to the last row in IT->w's current matrix
16832 displaying text. The loop looks funny but think of partially
16833 visible lines. */
16834 row_found = NULL;
16835 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16836 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16837 {
16838 eassert (row->enabled_p);
16839 row_found = row;
16840 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16841 break;
16842 ++row;
16843 }
16844
16845 return row_found;
16846 }
16847
16848
16849 /* Return the last row in the current matrix of W that is not affected
16850 by changes at the start of current_buffer that occurred since W's
16851 current matrix was built. Value is null if no such row exists.
16852
16853 BEG_UNCHANGED us the number of characters unchanged at the start of
16854 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16855 first changed character in current_buffer. Characters at positions <
16856 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16857 when the current matrix was built. */
16858
16859 static struct glyph_row *
16860 find_last_unchanged_at_beg_row (struct window *w)
16861 {
16862 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16863 struct glyph_row *row;
16864 struct glyph_row *row_found = NULL;
16865 int yb = window_text_bottom_y (w);
16866
16867 /* Find the last row displaying unchanged text. */
16868 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16869 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16870 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16871 ++row)
16872 {
16873 if (/* If row ends before first_changed_pos, it is unchanged,
16874 except in some case. */
16875 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16876 /* When row ends in ZV and we write at ZV it is not
16877 unchanged. */
16878 && !row->ends_at_zv_p
16879 /* When first_changed_pos is the end of a continued line,
16880 row is not unchanged because it may be no longer
16881 continued. */
16882 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16883 && (row->continued_p
16884 || row->exact_window_width_line_p))
16885 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16886 needs to be recomputed, so don't consider this row as
16887 unchanged. This happens when the last line was
16888 bidi-reordered and was killed immediately before this
16889 redisplay cycle. In that case, ROW->end stores the
16890 buffer position of the first visual-order character of
16891 the killed text, which is now beyond ZV. */
16892 && CHARPOS (row->end.pos) <= ZV)
16893 row_found = row;
16894
16895 /* Stop if last visible row. */
16896 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16897 break;
16898 }
16899
16900 return row_found;
16901 }
16902
16903
16904 /* Find the first glyph row in the current matrix of W that is not
16905 affected by changes at the end of current_buffer since the
16906 time W's current matrix was built.
16907
16908 Return in *DELTA the number of chars by which buffer positions in
16909 unchanged text at the end of current_buffer must be adjusted.
16910
16911 Return in *DELTA_BYTES the corresponding number of bytes.
16912
16913 Value is null if no such row exists, i.e. all rows are affected by
16914 changes. */
16915
16916 static struct glyph_row *
16917 find_first_unchanged_at_end_row (struct window *w,
16918 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16919 {
16920 struct glyph_row *row;
16921 struct glyph_row *row_found = NULL;
16922
16923 *delta = *delta_bytes = 0;
16924
16925 /* Display must not have been paused, otherwise the current matrix
16926 is not up to date. */
16927 eassert (w->window_end_valid);
16928
16929 /* A value of window_end_pos >= END_UNCHANGED means that the window
16930 end is in the range of changed text. If so, there is no
16931 unchanged row at the end of W's current matrix. */
16932 if (w->window_end_pos >= END_UNCHANGED)
16933 return NULL;
16934
16935 /* Set row to the last row in W's current matrix displaying text. */
16936 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
16937
16938 /* If matrix is entirely empty, no unchanged row exists. */
16939 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16940 {
16941 /* The value of row is the last glyph row in the matrix having a
16942 meaningful buffer position in it. The end position of row
16943 corresponds to window_end_pos. This allows us to translate
16944 buffer positions in the current matrix to current buffer
16945 positions for characters not in changed text. */
16946 ptrdiff_t Z_old =
16947 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
16948 ptrdiff_t Z_BYTE_old =
16949 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16950 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16951 struct glyph_row *first_text_row
16952 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16953
16954 *delta = Z - Z_old;
16955 *delta_bytes = Z_BYTE - Z_BYTE_old;
16956
16957 /* Set last_unchanged_pos to the buffer position of the last
16958 character in the buffer that has not been changed. Z is the
16959 index + 1 of the last character in current_buffer, i.e. by
16960 subtracting END_UNCHANGED we get the index of the last
16961 unchanged character, and we have to add BEG to get its buffer
16962 position. */
16963 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16964 last_unchanged_pos_old = last_unchanged_pos - *delta;
16965
16966 /* Search backward from ROW for a row displaying a line that
16967 starts at a minimum position >= last_unchanged_pos_old. */
16968 for (; row > first_text_row; --row)
16969 {
16970 /* This used to abort, but it can happen.
16971 It is ok to just stop the search instead here. KFS. */
16972 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16973 break;
16974
16975 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16976 row_found = row;
16977 }
16978 }
16979
16980 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16981
16982 return row_found;
16983 }
16984
16985
16986 /* Make sure that glyph rows in the current matrix of window W
16987 reference the same glyph memory as corresponding rows in the
16988 frame's frame matrix. This function is called after scrolling W's
16989 current matrix on a terminal frame in try_window_id and
16990 try_window_reusing_current_matrix. */
16991
16992 static void
16993 sync_frame_with_window_matrix_rows (struct window *w)
16994 {
16995 struct frame *f = XFRAME (w->frame);
16996 struct glyph_row *window_row, *window_row_end, *frame_row;
16997
16998 /* Preconditions: W must be a leaf window and full-width. Its frame
16999 must have a frame matrix. */
17000 eassert (BUFFERP (w->contents));
17001 eassert (WINDOW_FULL_WIDTH_P (w));
17002 eassert (!FRAME_WINDOW_P (f));
17003
17004 /* If W is a full-width window, glyph pointers in W's current matrix
17005 have, by definition, to be the same as glyph pointers in the
17006 corresponding frame matrix. Note that frame matrices have no
17007 marginal areas (see build_frame_matrix). */
17008 window_row = w->current_matrix->rows;
17009 window_row_end = window_row + w->current_matrix->nrows;
17010 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17011 while (window_row < window_row_end)
17012 {
17013 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17014 struct glyph *end = window_row->glyphs[LAST_AREA];
17015
17016 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17017 frame_row->glyphs[TEXT_AREA] = start;
17018 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17019 frame_row->glyphs[LAST_AREA] = end;
17020
17021 /* Disable frame rows whose corresponding window rows have
17022 been disabled in try_window_id. */
17023 if (!window_row->enabled_p)
17024 frame_row->enabled_p = 0;
17025
17026 ++window_row, ++frame_row;
17027 }
17028 }
17029
17030
17031 /* Find the glyph row in window W containing CHARPOS. Consider all
17032 rows between START and END (not inclusive). END null means search
17033 all rows to the end of the display area of W. Value is the row
17034 containing CHARPOS or null. */
17035
17036 struct glyph_row *
17037 row_containing_pos (struct window *w, ptrdiff_t charpos,
17038 struct glyph_row *start, struct glyph_row *end, int dy)
17039 {
17040 struct glyph_row *row = start;
17041 struct glyph_row *best_row = NULL;
17042 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
17043 int last_y;
17044
17045 /* If we happen to start on a header-line, skip that. */
17046 if (row->mode_line_p)
17047 ++row;
17048
17049 if ((end && row >= end) || !row->enabled_p)
17050 return NULL;
17051
17052 last_y = window_text_bottom_y (w) - dy;
17053
17054 while (1)
17055 {
17056 /* Give up if we have gone too far. */
17057 if (end && row >= end)
17058 return NULL;
17059 /* This formerly returned if they were equal.
17060 I think that both quantities are of a "last plus one" type;
17061 if so, when they are equal, the row is within the screen. -- rms. */
17062 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17063 return NULL;
17064
17065 /* If it is in this row, return this row. */
17066 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17067 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17068 /* The end position of a row equals the start
17069 position of the next row. If CHARPOS is there, we
17070 would rather consider it displayed in the next
17071 line, except when this line ends in ZV. */
17072 && !row_for_charpos_p (row, charpos)))
17073 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17074 {
17075 struct glyph *g;
17076
17077 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17078 || (!best_row && !row->continued_p))
17079 return row;
17080 /* In bidi-reordered rows, there could be several rows whose
17081 edges surround CHARPOS, all of these rows belonging to
17082 the same continued line. We need to find the row which
17083 fits CHARPOS the best. */
17084 for (g = row->glyphs[TEXT_AREA];
17085 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17086 g++)
17087 {
17088 if (!STRINGP (g->object))
17089 {
17090 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17091 {
17092 mindif = eabs (g->charpos - charpos);
17093 best_row = row;
17094 /* Exact match always wins. */
17095 if (mindif == 0)
17096 return best_row;
17097 }
17098 }
17099 }
17100 }
17101 else if (best_row && !row->continued_p)
17102 return best_row;
17103 ++row;
17104 }
17105 }
17106
17107
17108 /* Try to redisplay window W by reusing its existing display. W's
17109 current matrix must be up to date when this function is called,
17110 i.e. window_end_valid must be nonzero.
17111
17112 Value is
17113
17114 1 if display has been updated
17115 0 if otherwise unsuccessful
17116 -1 if redisplay with same window start is known not to succeed
17117
17118 The following steps are performed:
17119
17120 1. Find the last row in the current matrix of W that is not
17121 affected by changes at the start of current_buffer. If no such row
17122 is found, give up.
17123
17124 2. Find the first row in W's current matrix that is not affected by
17125 changes at the end of current_buffer. Maybe there is no such row.
17126
17127 3. Display lines beginning with the row + 1 found in step 1 to the
17128 row found in step 2 or, if step 2 didn't find a row, to the end of
17129 the window.
17130
17131 4. If cursor is not known to appear on the window, give up.
17132
17133 5. If display stopped at the row found in step 2, scroll the
17134 display and current matrix as needed.
17135
17136 6. Maybe display some lines at the end of W, if we must. This can
17137 happen under various circumstances, like a partially visible line
17138 becoming fully visible, or because newly displayed lines are displayed
17139 in smaller font sizes.
17140
17141 7. Update W's window end information. */
17142
17143 static int
17144 try_window_id (struct window *w)
17145 {
17146 struct frame *f = XFRAME (w->frame);
17147 struct glyph_matrix *current_matrix = w->current_matrix;
17148 struct glyph_matrix *desired_matrix = w->desired_matrix;
17149 struct glyph_row *last_unchanged_at_beg_row;
17150 struct glyph_row *first_unchanged_at_end_row;
17151 struct glyph_row *row;
17152 struct glyph_row *bottom_row;
17153 int bottom_vpos;
17154 struct it it;
17155 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17156 int dvpos, dy;
17157 struct text_pos start_pos;
17158 struct run run;
17159 int first_unchanged_at_end_vpos = 0;
17160 struct glyph_row *last_text_row, *last_text_row_at_end;
17161 struct text_pos start;
17162 ptrdiff_t first_changed_charpos, last_changed_charpos;
17163
17164 #ifdef GLYPH_DEBUG
17165 if (inhibit_try_window_id)
17166 return 0;
17167 #endif
17168
17169 /* This is handy for debugging. */
17170 #if 0
17171 #define GIVE_UP(X) \
17172 do { \
17173 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17174 return 0; \
17175 } while (0)
17176 #else
17177 #define GIVE_UP(X) return 0
17178 #endif
17179
17180 SET_TEXT_POS_FROM_MARKER (start, w->start);
17181
17182 /* Don't use this for mini-windows because these can show
17183 messages and mini-buffers, and we don't handle that here. */
17184 if (MINI_WINDOW_P (w))
17185 GIVE_UP (1);
17186
17187 /* This flag is used to prevent redisplay optimizations. */
17188 if (windows_or_buffers_changed || cursor_type_changed)
17189 GIVE_UP (2);
17190
17191 /* Verify that narrowing has not changed.
17192 Also verify that we were not told to prevent redisplay optimizations.
17193 It would be nice to further
17194 reduce the number of cases where this prevents try_window_id. */
17195 if (current_buffer->clip_changed
17196 || current_buffer->prevent_redisplay_optimizations_p)
17197 GIVE_UP (3);
17198
17199 /* Window must either use window-based redisplay or be full width. */
17200 if (!FRAME_WINDOW_P (f)
17201 && (!FRAME_LINE_INS_DEL_OK (f)
17202 || !WINDOW_FULL_WIDTH_P (w)))
17203 GIVE_UP (4);
17204
17205 /* Give up if point is known NOT to appear in W. */
17206 if (PT < CHARPOS (start))
17207 GIVE_UP (5);
17208
17209 /* Another way to prevent redisplay optimizations. */
17210 if (w->last_modified == 0)
17211 GIVE_UP (6);
17212
17213 /* Verify that window is not hscrolled. */
17214 if (w->hscroll != 0)
17215 GIVE_UP (7);
17216
17217 /* Verify that display wasn't paused. */
17218 if (!w->window_end_valid)
17219 GIVE_UP (8);
17220
17221 /* Can't use this if highlighting a region because a cursor movement
17222 will do more than just set the cursor. */
17223 if (markpos_of_region () >= 0)
17224 GIVE_UP (9);
17225
17226 /* Likewise if highlighting trailing whitespace. */
17227 if (!NILP (Vshow_trailing_whitespace))
17228 GIVE_UP (11);
17229
17230 /* Likewise if showing a region. */
17231 if (w->region_showing)
17232 GIVE_UP (10);
17233
17234 /* Can't use this if overlay arrow position and/or string have
17235 changed. */
17236 if (overlay_arrows_changed_p ())
17237 GIVE_UP (12);
17238
17239 /* When word-wrap is on, adding a space to the first word of a
17240 wrapped line can change the wrap position, altering the line
17241 above it. It might be worthwhile to handle this more
17242 intelligently, but for now just redisplay from scratch. */
17243 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17244 GIVE_UP (21);
17245
17246 /* Under bidi reordering, adding or deleting a character in the
17247 beginning of a paragraph, before the first strong directional
17248 character, can change the base direction of the paragraph (unless
17249 the buffer specifies a fixed paragraph direction), which will
17250 require to redisplay the whole paragraph. It might be worthwhile
17251 to find the paragraph limits and widen the range of redisplayed
17252 lines to that, but for now just give up this optimization and
17253 redisplay from scratch. */
17254 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17255 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17256 GIVE_UP (22);
17257
17258 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17259 only if buffer has really changed. The reason is that the gap is
17260 initially at Z for freshly visited files. The code below would
17261 set end_unchanged to 0 in that case. */
17262 if (MODIFF > SAVE_MODIFF
17263 /* This seems to happen sometimes after saving a buffer. */
17264 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17265 {
17266 if (GPT - BEG < BEG_UNCHANGED)
17267 BEG_UNCHANGED = GPT - BEG;
17268 if (Z - GPT < END_UNCHANGED)
17269 END_UNCHANGED = Z - GPT;
17270 }
17271
17272 /* The position of the first and last character that has been changed. */
17273 first_changed_charpos = BEG + BEG_UNCHANGED;
17274 last_changed_charpos = Z - END_UNCHANGED;
17275
17276 /* If window starts after a line end, and the last change is in
17277 front of that newline, then changes don't affect the display.
17278 This case happens with stealth-fontification. Note that although
17279 the display is unchanged, glyph positions in the matrix have to
17280 be adjusted, of course. */
17281 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17282 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17283 && ((last_changed_charpos < CHARPOS (start)
17284 && CHARPOS (start) == BEGV)
17285 || (last_changed_charpos < CHARPOS (start) - 1
17286 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17287 {
17288 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17289 struct glyph_row *r0;
17290
17291 /* Compute how many chars/bytes have been added to or removed
17292 from the buffer. */
17293 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17294 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17295 Z_delta = Z - Z_old;
17296 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17297
17298 /* Give up if PT is not in the window. Note that it already has
17299 been checked at the start of try_window_id that PT is not in
17300 front of the window start. */
17301 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17302 GIVE_UP (13);
17303
17304 /* If window start is unchanged, we can reuse the whole matrix
17305 as is, after adjusting glyph positions. No need to compute
17306 the window end again, since its offset from Z hasn't changed. */
17307 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17308 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17309 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17310 /* PT must not be in a partially visible line. */
17311 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17312 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17313 {
17314 /* Adjust positions in the glyph matrix. */
17315 if (Z_delta || Z_delta_bytes)
17316 {
17317 struct glyph_row *r1
17318 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17319 increment_matrix_positions (w->current_matrix,
17320 MATRIX_ROW_VPOS (r0, current_matrix),
17321 MATRIX_ROW_VPOS (r1, current_matrix),
17322 Z_delta, Z_delta_bytes);
17323 }
17324
17325 /* Set the cursor. */
17326 row = row_containing_pos (w, PT, r0, NULL, 0);
17327 if (row)
17328 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17329 else
17330 emacs_abort ();
17331 return 1;
17332 }
17333 }
17334
17335 /* Handle the case that changes are all below what is displayed in
17336 the window, and that PT is in the window. This shortcut cannot
17337 be taken if ZV is visible in the window, and text has been added
17338 there that is visible in the window. */
17339 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17340 /* ZV is not visible in the window, or there are no
17341 changes at ZV, actually. */
17342 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17343 || first_changed_charpos == last_changed_charpos))
17344 {
17345 struct glyph_row *r0;
17346
17347 /* Give up if PT is not in the window. Note that it already has
17348 been checked at the start of try_window_id that PT is not in
17349 front of the window start. */
17350 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17351 GIVE_UP (14);
17352
17353 /* If window start is unchanged, we can reuse the whole matrix
17354 as is, without changing glyph positions since no text has
17355 been added/removed in front of the window end. */
17356 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17357 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17358 /* PT must not be in a partially visible line. */
17359 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17360 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17361 {
17362 /* We have to compute the window end anew since text
17363 could have been added/removed after it. */
17364 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17365 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17366
17367 /* Set the cursor. */
17368 row = row_containing_pos (w, PT, r0, NULL, 0);
17369 if (row)
17370 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17371 else
17372 emacs_abort ();
17373 return 2;
17374 }
17375 }
17376
17377 /* Give up if window start is in the changed area.
17378
17379 The condition used to read
17380
17381 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17382
17383 but why that was tested escapes me at the moment. */
17384 if (CHARPOS (start) >= first_changed_charpos
17385 && CHARPOS (start) <= last_changed_charpos)
17386 GIVE_UP (15);
17387
17388 /* Check that window start agrees with the start of the first glyph
17389 row in its current matrix. Check this after we know the window
17390 start is not in changed text, otherwise positions would not be
17391 comparable. */
17392 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17393 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17394 GIVE_UP (16);
17395
17396 /* Give up if the window ends in strings. Overlay strings
17397 at the end are difficult to handle, so don't try. */
17398 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
17399 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17400 GIVE_UP (20);
17401
17402 /* Compute the position at which we have to start displaying new
17403 lines. Some of the lines at the top of the window might be
17404 reusable because they are not displaying changed text. Find the
17405 last row in W's current matrix not affected by changes at the
17406 start of current_buffer. Value is null if changes start in the
17407 first line of window. */
17408 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17409 if (last_unchanged_at_beg_row)
17410 {
17411 /* Avoid starting to display in the middle of a character, a TAB
17412 for instance. This is easier than to set up the iterator
17413 exactly, and it's not a frequent case, so the additional
17414 effort wouldn't really pay off. */
17415 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17416 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17417 && last_unchanged_at_beg_row > w->current_matrix->rows)
17418 --last_unchanged_at_beg_row;
17419
17420 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17421 GIVE_UP (17);
17422
17423 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17424 GIVE_UP (18);
17425 start_pos = it.current.pos;
17426
17427 /* Start displaying new lines in the desired matrix at the same
17428 vpos we would use in the current matrix, i.e. below
17429 last_unchanged_at_beg_row. */
17430 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17431 current_matrix);
17432 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17433 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17434
17435 eassert (it.hpos == 0 && it.current_x == 0);
17436 }
17437 else
17438 {
17439 /* There are no reusable lines at the start of the window.
17440 Start displaying in the first text line. */
17441 start_display (&it, w, start);
17442 it.vpos = it.first_vpos;
17443 start_pos = it.current.pos;
17444 }
17445
17446 /* Find the first row that is not affected by changes at the end of
17447 the buffer. Value will be null if there is no unchanged row, in
17448 which case we must redisplay to the end of the window. delta
17449 will be set to the value by which buffer positions beginning with
17450 first_unchanged_at_end_row have to be adjusted due to text
17451 changes. */
17452 first_unchanged_at_end_row
17453 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17454 IF_DEBUG (debug_delta = delta);
17455 IF_DEBUG (debug_delta_bytes = delta_bytes);
17456
17457 /* Set stop_pos to the buffer position up to which we will have to
17458 display new lines. If first_unchanged_at_end_row != NULL, this
17459 is the buffer position of the start of the line displayed in that
17460 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17461 that we don't stop at a buffer position. */
17462 stop_pos = 0;
17463 if (first_unchanged_at_end_row)
17464 {
17465 eassert (last_unchanged_at_beg_row == NULL
17466 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17467
17468 /* If this is a continuation line, move forward to the next one
17469 that isn't. Changes in lines above affect this line.
17470 Caution: this may move first_unchanged_at_end_row to a row
17471 not displaying text. */
17472 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17473 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17474 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17475 < it.last_visible_y))
17476 ++first_unchanged_at_end_row;
17477
17478 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17479 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17480 >= it.last_visible_y))
17481 first_unchanged_at_end_row = NULL;
17482 else
17483 {
17484 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17485 + delta);
17486 first_unchanged_at_end_vpos
17487 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17488 eassert (stop_pos >= Z - END_UNCHANGED);
17489 }
17490 }
17491 else if (last_unchanged_at_beg_row == NULL)
17492 GIVE_UP (19);
17493
17494
17495 #ifdef GLYPH_DEBUG
17496
17497 /* Either there is no unchanged row at the end, or the one we have
17498 now displays text. This is a necessary condition for the window
17499 end pos calculation at the end of this function. */
17500 eassert (first_unchanged_at_end_row == NULL
17501 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17502
17503 debug_last_unchanged_at_beg_vpos
17504 = (last_unchanged_at_beg_row
17505 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17506 : -1);
17507 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17508
17509 #endif /* GLYPH_DEBUG */
17510
17511
17512 /* Display new lines. Set last_text_row to the last new line
17513 displayed which has text on it, i.e. might end up as being the
17514 line where the window_end_vpos is. */
17515 w->cursor.vpos = -1;
17516 last_text_row = NULL;
17517 overlay_arrow_seen = 0;
17518 while (it.current_y < it.last_visible_y
17519 && !fonts_changed_p
17520 && (first_unchanged_at_end_row == NULL
17521 || IT_CHARPOS (it) < stop_pos))
17522 {
17523 if (display_line (&it))
17524 last_text_row = it.glyph_row - 1;
17525 }
17526
17527 if (fonts_changed_p)
17528 return -1;
17529
17530
17531 /* Compute differences in buffer positions, y-positions etc. for
17532 lines reused at the bottom of the window. Compute what we can
17533 scroll. */
17534 if (first_unchanged_at_end_row
17535 /* No lines reused because we displayed everything up to the
17536 bottom of the window. */
17537 && it.current_y < it.last_visible_y)
17538 {
17539 dvpos = (it.vpos
17540 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17541 current_matrix));
17542 dy = it.current_y - first_unchanged_at_end_row->y;
17543 run.current_y = first_unchanged_at_end_row->y;
17544 run.desired_y = run.current_y + dy;
17545 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17546 }
17547 else
17548 {
17549 delta = delta_bytes = dvpos = dy
17550 = run.current_y = run.desired_y = run.height = 0;
17551 first_unchanged_at_end_row = NULL;
17552 }
17553 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17554
17555
17556 /* Find the cursor if not already found. We have to decide whether
17557 PT will appear on this window (it sometimes doesn't, but this is
17558 not a very frequent case.) This decision has to be made before
17559 the current matrix is altered. A value of cursor.vpos < 0 means
17560 that PT is either in one of the lines beginning at
17561 first_unchanged_at_end_row or below the window. Don't care for
17562 lines that might be displayed later at the window end; as
17563 mentioned, this is not a frequent case. */
17564 if (w->cursor.vpos < 0)
17565 {
17566 /* Cursor in unchanged rows at the top? */
17567 if (PT < CHARPOS (start_pos)
17568 && last_unchanged_at_beg_row)
17569 {
17570 row = row_containing_pos (w, PT,
17571 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17572 last_unchanged_at_beg_row + 1, 0);
17573 if (row)
17574 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17575 }
17576
17577 /* Start from first_unchanged_at_end_row looking for PT. */
17578 else if (first_unchanged_at_end_row)
17579 {
17580 row = row_containing_pos (w, PT - delta,
17581 first_unchanged_at_end_row, NULL, 0);
17582 if (row)
17583 set_cursor_from_row (w, row, w->current_matrix, delta,
17584 delta_bytes, dy, dvpos);
17585 }
17586
17587 /* Give up if cursor was not found. */
17588 if (w->cursor.vpos < 0)
17589 {
17590 clear_glyph_matrix (w->desired_matrix);
17591 return -1;
17592 }
17593 }
17594
17595 /* Don't let the cursor end in the scroll margins. */
17596 {
17597 int this_scroll_margin, cursor_height;
17598 int frame_line_height = default_line_pixel_height (w);
17599 int window_total_lines
17600 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
17601
17602 this_scroll_margin =
17603 max (0, min (scroll_margin, window_total_lines / 4));
17604 this_scroll_margin *= frame_line_height;
17605 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17606
17607 if ((w->cursor.y < this_scroll_margin
17608 && CHARPOS (start) > BEGV)
17609 /* Old redisplay didn't take scroll margin into account at the bottom,
17610 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17611 || (w->cursor.y + (make_cursor_line_fully_visible_p
17612 ? cursor_height + this_scroll_margin
17613 : 1)) > it.last_visible_y)
17614 {
17615 w->cursor.vpos = -1;
17616 clear_glyph_matrix (w->desired_matrix);
17617 return -1;
17618 }
17619 }
17620
17621 /* Scroll the display. Do it before changing the current matrix so
17622 that xterm.c doesn't get confused about where the cursor glyph is
17623 found. */
17624 if (dy && run.height)
17625 {
17626 update_begin (f);
17627
17628 if (FRAME_WINDOW_P (f))
17629 {
17630 FRAME_RIF (f)->update_window_begin_hook (w);
17631 FRAME_RIF (f)->clear_window_mouse_face (w);
17632 FRAME_RIF (f)->scroll_run_hook (w, &run);
17633 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17634 }
17635 else
17636 {
17637 /* Terminal frame. In this case, dvpos gives the number of
17638 lines to scroll by; dvpos < 0 means scroll up. */
17639 int from_vpos
17640 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17641 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17642 int end = (WINDOW_TOP_EDGE_LINE (w)
17643 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17644 + window_internal_height (w));
17645
17646 #if defined (HAVE_GPM) || defined (MSDOS)
17647 x_clear_window_mouse_face (w);
17648 #endif
17649 /* Perform the operation on the screen. */
17650 if (dvpos > 0)
17651 {
17652 /* Scroll last_unchanged_at_beg_row to the end of the
17653 window down dvpos lines. */
17654 set_terminal_window (f, end);
17655
17656 /* On dumb terminals delete dvpos lines at the end
17657 before inserting dvpos empty lines. */
17658 if (!FRAME_SCROLL_REGION_OK (f))
17659 ins_del_lines (f, end - dvpos, -dvpos);
17660
17661 /* Insert dvpos empty lines in front of
17662 last_unchanged_at_beg_row. */
17663 ins_del_lines (f, from, dvpos);
17664 }
17665 else if (dvpos < 0)
17666 {
17667 /* Scroll up last_unchanged_at_beg_vpos to the end of
17668 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17669 set_terminal_window (f, end);
17670
17671 /* Delete dvpos lines in front of
17672 last_unchanged_at_beg_vpos. ins_del_lines will set
17673 the cursor to the given vpos and emit |dvpos| delete
17674 line sequences. */
17675 ins_del_lines (f, from + dvpos, dvpos);
17676
17677 /* On a dumb terminal insert dvpos empty lines at the
17678 end. */
17679 if (!FRAME_SCROLL_REGION_OK (f))
17680 ins_del_lines (f, end + dvpos, -dvpos);
17681 }
17682
17683 set_terminal_window (f, 0);
17684 }
17685
17686 update_end (f);
17687 }
17688
17689 /* Shift reused rows of the current matrix to the right position.
17690 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17691 text. */
17692 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17693 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17694 if (dvpos < 0)
17695 {
17696 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17697 bottom_vpos, dvpos);
17698 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17699 bottom_vpos);
17700 }
17701 else if (dvpos > 0)
17702 {
17703 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17704 bottom_vpos, dvpos);
17705 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17706 first_unchanged_at_end_vpos + dvpos);
17707 }
17708
17709 /* For frame-based redisplay, make sure that current frame and window
17710 matrix are in sync with respect to glyph memory. */
17711 if (!FRAME_WINDOW_P (f))
17712 sync_frame_with_window_matrix_rows (w);
17713
17714 /* Adjust buffer positions in reused rows. */
17715 if (delta || delta_bytes)
17716 increment_matrix_positions (current_matrix,
17717 first_unchanged_at_end_vpos + dvpos,
17718 bottom_vpos, delta, delta_bytes);
17719
17720 /* Adjust Y positions. */
17721 if (dy)
17722 shift_glyph_matrix (w, current_matrix,
17723 first_unchanged_at_end_vpos + dvpos,
17724 bottom_vpos, dy);
17725
17726 if (first_unchanged_at_end_row)
17727 {
17728 first_unchanged_at_end_row += dvpos;
17729 if (first_unchanged_at_end_row->y >= it.last_visible_y
17730 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17731 first_unchanged_at_end_row = NULL;
17732 }
17733
17734 /* If scrolling up, there may be some lines to display at the end of
17735 the window. */
17736 last_text_row_at_end = NULL;
17737 if (dy < 0)
17738 {
17739 /* Scrolling up can leave for example a partially visible line
17740 at the end of the window to be redisplayed. */
17741 /* Set last_row to the glyph row in the current matrix where the
17742 window end line is found. It has been moved up or down in
17743 the matrix by dvpos. */
17744 int last_vpos = w->window_end_vpos + dvpos;
17745 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17746
17747 /* If last_row is the window end line, it should display text. */
17748 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
17749
17750 /* If window end line was partially visible before, begin
17751 displaying at that line. Otherwise begin displaying with the
17752 line following it. */
17753 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17754 {
17755 init_to_row_start (&it, w, last_row);
17756 it.vpos = last_vpos;
17757 it.current_y = last_row->y;
17758 }
17759 else
17760 {
17761 init_to_row_end (&it, w, last_row);
17762 it.vpos = 1 + last_vpos;
17763 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17764 ++last_row;
17765 }
17766
17767 /* We may start in a continuation line. If so, we have to
17768 get the right continuation_lines_width and current_x. */
17769 it.continuation_lines_width = last_row->continuation_lines_width;
17770 it.hpos = it.current_x = 0;
17771
17772 /* Display the rest of the lines at the window end. */
17773 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17774 while (it.current_y < it.last_visible_y
17775 && !fonts_changed_p)
17776 {
17777 /* Is it always sure that the display agrees with lines in
17778 the current matrix? I don't think so, so we mark rows
17779 displayed invalid in the current matrix by setting their
17780 enabled_p flag to zero. */
17781 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17782 if (display_line (&it))
17783 last_text_row_at_end = it.glyph_row - 1;
17784 }
17785 }
17786
17787 /* Update window_end_pos and window_end_vpos. */
17788 if (first_unchanged_at_end_row && !last_text_row_at_end)
17789 {
17790 /* Window end line if one of the preserved rows from the current
17791 matrix. Set row to the last row displaying text in current
17792 matrix starting at first_unchanged_at_end_row, after
17793 scrolling. */
17794 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17795 row = find_last_row_displaying_text (w->current_matrix, &it,
17796 first_unchanged_at_end_row);
17797 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17798 adjust_window_ends (w, row, 1);
17799 eassert (w->window_end_bytepos >= 0);
17800 IF_DEBUG (debug_method_add (w, "A"));
17801 }
17802 else if (last_text_row_at_end)
17803 {
17804 adjust_window_ends (w, last_text_row_at_end, 0);
17805 eassert (w->window_end_bytepos >= 0);
17806 IF_DEBUG (debug_method_add (w, "B"));
17807 }
17808 else if (last_text_row)
17809 {
17810 /* We have displayed either to the end of the window or at the
17811 end of the window, i.e. the last row with text is to be found
17812 in the desired matrix. */
17813 adjust_window_ends (w, last_text_row, 0);
17814 eassert (w->window_end_bytepos >= 0);
17815 }
17816 else if (first_unchanged_at_end_row == NULL
17817 && last_text_row == NULL
17818 && last_text_row_at_end == NULL)
17819 {
17820 /* Displayed to end of window, but no line containing text was
17821 displayed. Lines were deleted at the end of the window. */
17822 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17823 int vpos = w->window_end_vpos;
17824 struct glyph_row *current_row = current_matrix->rows + vpos;
17825 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17826
17827 for (row = NULL;
17828 row == NULL && vpos >= first_vpos;
17829 --vpos, --current_row, --desired_row)
17830 {
17831 if (desired_row->enabled_p)
17832 {
17833 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
17834 row = desired_row;
17835 }
17836 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
17837 row = current_row;
17838 }
17839
17840 eassert (row != NULL);
17841 w->window_end_vpos = vpos + 1;
17842 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17843 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17844 eassert (w->window_end_bytepos >= 0);
17845 IF_DEBUG (debug_method_add (w, "C"));
17846 }
17847 else
17848 emacs_abort ();
17849
17850 IF_DEBUG (debug_end_pos = w->window_end_pos;
17851 debug_end_vpos = w->window_end_vpos);
17852
17853 /* Record that display has not been completed. */
17854 w->window_end_valid = 0;
17855 w->desired_matrix->no_scrolling_p = 1;
17856 return 3;
17857
17858 #undef GIVE_UP
17859 }
17860
17861
17862 \f
17863 /***********************************************************************
17864 More debugging support
17865 ***********************************************************************/
17866
17867 #ifdef GLYPH_DEBUG
17868
17869 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17870 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17871 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17872
17873
17874 /* Dump the contents of glyph matrix MATRIX on stderr.
17875
17876 GLYPHS 0 means don't show glyph contents.
17877 GLYPHS 1 means show glyphs in short form
17878 GLYPHS > 1 means show glyphs in long form. */
17879
17880 void
17881 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17882 {
17883 int i;
17884 for (i = 0; i < matrix->nrows; ++i)
17885 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17886 }
17887
17888
17889 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17890 the glyph row and area where the glyph comes from. */
17891
17892 void
17893 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17894 {
17895 if (glyph->type == CHAR_GLYPH
17896 || glyph->type == GLYPHLESS_GLYPH)
17897 {
17898 fprintf (stderr,
17899 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17900 glyph - row->glyphs[TEXT_AREA],
17901 (glyph->type == CHAR_GLYPH
17902 ? 'C'
17903 : 'G'),
17904 glyph->charpos,
17905 (BUFFERP (glyph->object)
17906 ? 'B'
17907 : (STRINGP (glyph->object)
17908 ? 'S'
17909 : (INTEGERP (glyph->object)
17910 ? '0'
17911 : '-'))),
17912 glyph->pixel_width,
17913 glyph->u.ch,
17914 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17915 ? glyph->u.ch
17916 : '.'),
17917 glyph->face_id,
17918 glyph->left_box_line_p,
17919 glyph->right_box_line_p);
17920 }
17921 else if (glyph->type == STRETCH_GLYPH)
17922 {
17923 fprintf (stderr,
17924 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17925 glyph - row->glyphs[TEXT_AREA],
17926 'S',
17927 glyph->charpos,
17928 (BUFFERP (glyph->object)
17929 ? 'B'
17930 : (STRINGP (glyph->object)
17931 ? 'S'
17932 : (INTEGERP (glyph->object)
17933 ? '0'
17934 : '-'))),
17935 glyph->pixel_width,
17936 0,
17937 ' ',
17938 glyph->face_id,
17939 glyph->left_box_line_p,
17940 glyph->right_box_line_p);
17941 }
17942 else if (glyph->type == IMAGE_GLYPH)
17943 {
17944 fprintf (stderr,
17945 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17946 glyph - row->glyphs[TEXT_AREA],
17947 'I',
17948 glyph->charpos,
17949 (BUFFERP (glyph->object)
17950 ? 'B'
17951 : (STRINGP (glyph->object)
17952 ? 'S'
17953 : (INTEGERP (glyph->object)
17954 ? '0'
17955 : '-'))),
17956 glyph->pixel_width,
17957 glyph->u.img_id,
17958 '.',
17959 glyph->face_id,
17960 glyph->left_box_line_p,
17961 glyph->right_box_line_p);
17962 }
17963 else if (glyph->type == COMPOSITE_GLYPH)
17964 {
17965 fprintf (stderr,
17966 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
17967 glyph - row->glyphs[TEXT_AREA],
17968 '+',
17969 glyph->charpos,
17970 (BUFFERP (glyph->object)
17971 ? 'B'
17972 : (STRINGP (glyph->object)
17973 ? 'S'
17974 : (INTEGERP (glyph->object)
17975 ? '0'
17976 : '-'))),
17977 glyph->pixel_width,
17978 glyph->u.cmp.id);
17979 if (glyph->u.cmp.automatic)
17980 fprintf (stderr,
17981 "[%d-%d]",
17982 glyph->slice.cmp.from, glyph->slice.cmp.to);
17983 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17984 glyph->face_id,
17985 glyph->left_box_line_p,
17986 glyph->right_box_line_p);
17987 }
17988 }
17989
17990
17991 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17992 GLYPHS 0 means don't show glyph contents.
17993 GLYPHS 1 means show glyphs in short form
17994 GLYPHS > 1 means show glyphs in long form. */
17995
17996 void
17997 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17998 {
17999 if (glyphs != 1)
18000 {
18001 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
18002 fprintf (stderr, "==============================================================================\n");
18003
18004 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18005 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18006 vpos,
18007 MATRIX_ROW_START_CHARPOS (row),
18008 MATRIX_ROW_END_CHARPOS (row),
18009 row->used[TEXT_AREA],
18010 row->contains_overlapping_glyphs_p,
18011 row->enabled_p,
18012 row->truncated_on_left_p,
18013 row->truncated_on_right_p,
18014 row->continued_p,
18015 MATRIX_ROW_CONTINUATION_LINE_P (row),
18016 MATRIX_ROW_DISPLAYS_TEXT_P (row),
18017 row->ends_at_zv_p,
18018 row->fill_line_p,
18019 row->ends_in_middle_of_char_p,
18020 row->starts_in_middle_of_char_p,
18021 row->mouse_face_p,
18022 row->x,
18023 row->y,
18024 row->pixel_width,
18025 row->height,
18026 row->visible_height,
18027 row->ascent,
18028 row->phys_ascent);
18029 /* The next 3 lines should align to "Start" in the header. */
18030 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
18031 row->end.overlay_string_index,
18032 row->continuation_lines_width);
18033 fprintf (stderr, " %9"pI"d %9"pI"d\n",
18034 CHARPOS (row->start.string_pos),
18035 CHARPOS (row->end.string_pos));
18036 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18037 row->end.dpvec_index);
18038 }
18039
18040 if (glyphs > 1)
18041 {
18042 int area;
18043
18044 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18045 {
18046 struct glyph *glyph = row->glyphs[area];
18047 struct glyph *glyph_end = glyph + row->used[area];
18048
18049 /* Glyph for a line end in text. */
18050 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18051 ++glyph_end;
18052
18053 if (glyph < glyph_end)
18054 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18055
18056 for (; glyph < glyph_end; ++glyph)
18057 dump_glyph (row, glyph, area);
18058 }
18059 }
18060 else if (glyphs == 1)
18061 {
18062 int area;
18063
18064 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18065 {
18066 char *s = alloca (row->used[area] + 4);
18067 int i;
18068
18069 for (i = 0; i < row->used[area]; ++i)
18070 {
18071 struct glyph *glyph = row->glyphs[area] + i;
18072 if (i == row->used[area] - 1
18073 && area == TEXT_AREA
18074 && INTEGERP (glyph->object)
18075 && glyph->type == CHAR_GLYPH
18076 && glyph->u.ch == ' ')
18077 {
18078 strcpy (&s[i], "[\\n]");
18079 i += 4;
18080 }
18081 else if (glyph->type == CHAR_GLYPH
18082 && glyph->u.ch < 0x80
18083 && glyph->u.ch >= ' ')
18084 s[i] = glyph->u.ch;
18085 else
18086 s[i] = '.';
18087 }
18088
18089 s[i] = '\0';
18090 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18091 }
18092 }
18093 }
18094
18095
18096 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18097 Sdump_glyph_matrix, 0, 1, "p",
18098 doc: /* Dump the current matrix of the selected window to stderr.
18099 Shows contents of glyph row structures. With non-nil
18100 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18101 glyphs in short form, otherwise show glyphs in long form. */)
18102 (Lisp_Object glyphs)
18103 {
18104 struct window *w = XWINDOW (selected_window);
18105 struct buffer *buffer = XBUFFER (w->contents);
18106
18107 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18108 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18109 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18110 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18111 fprintf (stderr, "=============================================\n");
18112 dump_glyph_matrix (w->current_matrix,
18113 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18114 return Qnil;
18115 }
18116
18117
18118 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18119 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18120 (void)
18121 {
18122 struct frame *f = XFRAME (selected_frame);
18123 dump_glyph_matrix (f->current_matrix, 1);
18124 return Qnil;
18125 }
18126
18127
18128 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18129 doc: /* Dump glyph row ROW to stderr.
18130 GLYPH 0 means don't dump glyphs.
18131 GLYPH 1 means dump glyphs in short form.
18132 GLYPH > 1 or omitted means dump glyphs in long form. */)
18133 (Lisp_Object row, Lisp_Object glyphs)
18134 {
18135 struct glyph_matrix *matrix;
18136 EMACS_INT vpos;
18137
18138 CHECK_NUMBER (row);
18139 matrix = XWINDOW (selected_window)->current_matrix;
18140 vpos = XINT (row);
18141 if (vpos >= 0 && vpos < matrix->nrows)
18142 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18143 vpos,
18144 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18145 return Qnil;
18146 }
18147
18148
18149 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18150 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18151 GLYPH 0 means don't dump glyphs.
18152 GLYPH 1 means dump glyphs in short form.
18153 GLYPH > 1 or omitted means dump glyphs in long form. */)
18154 (Lisp_Object row, Lisp_Object glyphs)
18155 {
18156 struct frame *sf = SELECTED_FRAME ();
18157 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18158 EMACS_INT vpos;
18159
18160 CHECK_NUMBER (row);
18161 vpos = XINT (row);
18162 if (vpos >= 0 && vpos < m->nrows)
18163 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18164 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18165 return Qnil;
18166 }
18167
18168
18169 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18170 doc: /* Toggle tracing of redisplay.
18171 With ARG, turn tracing on if and only if ARG is positive. */)
18172 (Lisp_Object arg)
18173 {
18174 if (NILP (arg))
18175 trace_redisplay_p = !trace_redisplay_p;
18176 else
18177 {
18178 arg = Fprefix_numeric_value (arg);
18179 trace_redisplay_p = XINT (arg) > 0;
18180 }
18181
18182 return Qnil;
18183 }
18184
18185
18186 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18187 doc: /* Like `format', but print result to stderr.
18188 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18189 (ptrdiff_t nargs, Lisp_Object *args)
18190 {
18191 Lisp_Object s = Fformat (nargs, args);
18192 fprintf (stderr, "%s", SDATA (s));
18193 return Qnil;
18194 }
18195
18196 #endif /* GLYPH_DEBUG */
18197
18198
18199 \f
18200 /***********************************************************************
18201 Building Desired Matrix Rows
18202 ***********************************************************************/
18203
18204 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18205 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18206
18207 static struct glyph_row *
18208 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18209 {
18210 struct frame *f = XFRAME (WINDOW_FRAME (w));
18211 struct buffer *buffer = XBUFFER (w->contents);
18212 struct buffer *old = current_buffer;
18213 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18214 int arrow_len = SCHARS (overlay_arrow_string);
18215 const unsigned char *arrow_end = arrow_string + arrow_len;
18216 const unsigned char *p;
18217 struct it it;
18218 bool multibyte_p;
18219 int n_glyphs_before;
18220
18221 set_buffer_temp (buffer);
18222 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18223 it.glyph_row->used[TEXT_AREA] = 0;
18224 SET_TEXT_POS (it.position, 0, 0);
18225
18226 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18227 p = arrow_string;
18228 while (p < arrow_end)
18229 {
18230 Lisp_Object face, ilisp;
18231
18232 /* Get the next character. */
18233 if (multibyte_p)
18234 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18235 else
18236 {
18237 it.c = it.char_to_display = *p, it.len = 1;
18238 if (! ASCII_CHAR_P (it.c))
18239 it.char_to_display = BYTE8_TO_CHAR (it.c);
18240 }
18241 p += it.len;
18242
18243 /* Get its face. */
18244 ilisp = make_number (p - arrow_string);
18245 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18246 it.face_id = compute_char_face (f, it.char_to_display, face);
18247
18248 /* Compute its width, get its glyphs. */
18249 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18250 SET_TEXT_POS (it.position, -1, -1);
18251 PRODUCE_GLYPHS (&it);
18252
18253 /* If this character doesn't fit any more in the line, we have
18254 to remove some glyphs. */
18255 if (it.current_x > it.last_visible_x)
18256 {
18257 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18258 break;
18259 }
18260 }
18261
18262 set_buffer_temp (old);
18263 return it.glyph_row;
18264 }
18265
18266
18267 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18268 glyphs to insert is determined by produce_special_glyphs. */
18269
18270 static void
18271 insert_left_trunc_glyphs (struct it *it)
18272 {
18273 struct it truncate_it;
18274 struct glyph *from, *end, *to, *toend;
18275
18276 eassert (!FRAME_WINDOW_P (it->f)
18277 || (!it->glyph_row->reversed_p
18278 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18279 || (it->glyph_row->reversed_p
18280 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18281
18282 /* Get the truncation glyphs. */
18283 truncate_it = *it;
18284 truncate_it.current_x = 0;
18285 truncate_it.face_id = DEFAULT_FACE_ID;
18286 truncate_it.glyph_row = &scratch_glyph_row;
18287 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18288 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18289 truncate_it.object = make_number (0);
18290 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18291
18292 /* Overwrite glyphs from IT with truncation glyphs. */
18293 if (!it->glyph_row->reversed_p)
18294 {
18295 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18296
18297 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18298 end = from + tused;
18299 to = it->glyph_row->glyphs[TEXT_AREA];
18300 toend = to + it->glyph_row->used[TEXT_AREA];
18301 if (FRAME_WINDOW_P (it->f))
18302 {
18303 /* On GUI frames, when variable-size fonts are displayed,
18304 the truncation glyphs may need more pixels than the row's
18305 glyphs they overwrite. We overwrite more glyphs to free
18306 enough screen real estate, and enlarge the stretch glyph
18307 on the right (see display_line), if there is one, to
18308 preserve the screen position of the truncation glyphs on
18309 the right. */
18310 int w = 0;
18311 struct glyph *g = to;
18312 short used;
18313
18314 /* The first glyph could be partially visible, in which case
18315 it->glyph_row->x will be negative. But we want the left
18316 truncation glyphs to be aligned at the left margin of the
18317 window, so we override the x coordinate at which the row
18318 will begin. */
18319 it->glyph_row->x = 0;
18320 while (g < toend && w < it->truncation_pixel_width)
18321 {
18322 w += g->pixel_width;
18323 ++g;
18324 }
18325 if (g - to - tused > 0)
18326 {
18327 memmove (to + tused, g, (toend - g) * sizeof(*g));
18328 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18329 }
18330 used = it->glyph_row->used[TEXT_AREA];
18331 if (it->glyph_row->truncated_on_right_p
18332 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18333 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18334 == STRETCH_GLYPH)
18335 {
18336 int extra = w - it->truncation_pixel_width;
18337
18338 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18339 }
18340 }
18341
18342 while (from < end)
18343 *to++ = *from++;
18344
18345 /* There may be padding glyphs left over. Overwrite them too. */
18346 if (!FRAME_WINDOW_P (it->f))
18347 {
18348 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18349 {
18350 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18351 while (from < end)
18352 *to++ = *from++;
18353 }
18354 }
18355
18356 if (to > toend)
18357 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18358 }
18359 else
18360 {
18361 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18362
18363 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18364 that back to front. */
18365 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18366 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18367 toend = it->glyph_row->glyphs[TEXT_AREA];
18368 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18369 if (FRAME_WINDOW_P (it->f))
18370 {
18371 int w = 0;
18372 struct glyph *g = to;
18373
18374 while (g >= toend && w < it->truncation_pixel_width)
18375 {
18376 w += g->pixel_width;
18377 --g;
18378 }
18379 if (to - g - tused > 0)
18380 to = g + tused;
18381 if (it->glyph_row->truncated_on_right_p
18382 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18383 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18384 {
18385 int extra = w - it->truncation_pixel_width;
18386
18387 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18388 }
18389 }
18390
18391 while (from >= end && to >= toend)
18392 *to-- = *from--;
18393 if (!FRAME_WINDOW_P (it->f))
18394 {
18395 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18396 {
18397 from =
18398 truncate_it.glyph_row->glyphs[TEXT_AREA]
18399 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18400 while (from >= end && to >= toend)
18401 *to-- = *from--;
18402 }
18403 }
18404 if (from >= end)
18405 {
18406 /* Need to free some room before prepending additional
18407 glyphs. */
18408 int move_by = from - end + 1;
18409 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18410 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18411
18412 for ( ; g >= g0; g--)
18413 g[move_by] = *g;
18414 while (from >= end)
18415 *to-- = *from--;
18416 it->glyph_row->used[TEXT_AREA] += move_by;
18417 }
18418 }
18419 }
18420
18421 /* Compute the hash code for ROW. */
18422 unsigned
18423 row_hash (struct glyph_row *row)
18424 {
18425 int area, k;
18426 unsigned hashval = 0;
18427
18428 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18429 for (k = 0; k < row->used[area]; ++k)
18430 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18431 + row->glyphs[area][k].u.val
18432 + row->glyphs[area][k].face_id
18433 + row->glyphs[area][k].padding_p
18434 + (row->glyphs[area][k].type << 2));
18435
18436 return hashval;
18437 }
18438
18439 /* Compute the pixel height and width of IT->glyph_row.
18440
18441 Most of the time, ascent and height of a display line will be equal
18442 to the max_ascent and max_height values of the display iterator
18443 structure. This is not the case if
18444
18445 1. We hit ZV without displaying anything. In this case, max_ascent
18446 and max_height will be zero.
18447
18448 2. We have some glyphs that don't contribute to the line height.
18449 (The glyph row flag contributes_to_line_height_p is for future
18450 pixmap extensions).
18451
18452 The first case is easily covered by using default values because in
18453 these cases, the line height does not really matter, except that it
18454 must not be zero. */
18455
18456 static void
18457 compute_line_metrics (struct it *it)
18458 {
18459 struct glyph_row *row = it->glyph_row;
18460
18461 if (FRAME_WINDOW_P (it->f))
18462 {
18463 int i, min_y, max_y;
18464
18465 /* The line may consist of one space only, that was added to
18466 place the cursor on it. If so, the row's height hasn't been
18467 computed yet. */
18468 if (row->height == 0)
18469 {
18470 if (it->max_ascent + it->max_descent == 0)
18471 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18472 row->ascent = it->max_ascent;
18473 row->height = it->max_ascent + it->max_descent;
18474 row->phys_ascent = it->max_phys_ascent;
18475 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18476 row->extra_line_spacing = it->max_extra_line_spacing;
18477 }
18478
18479 /* Compute the width of this line. */
18480 row->pixel_width = row->x;
18481 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18482 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18483
18484 eassert (row->pixel_width >= 0);
18485 eassert (row->ascent >= 0 && row->height > 0);
18486
18487 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18488 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18489
18490 /* If first line's physical ascent is larger than its logical
18491 ascent, use the physical ascent, and make the row taller.
18492 This makes accented characters fully visible. */
18493 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18494 && row->phys_ascent > row->ascent)
18495 {
18496 row->height += row->phys_ascent - row->ascent;
18497 row->ascent = row->phys_ascent;
18498 }
18499
18500 /* Compute how much of the line is visible. */
18501 row->visible_height = row->height;
18502
18503 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18504 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18505
18506 if (row->y < min_y)
18507 row->visible_height -= min_y - row->y;
18508 if (row->y + row->height > max_y)
18509 row->visible_height -= row->y + row->height - max_y;
18510 }
18511 else
18512 {
18513 row->pixel_width = row->used[TEXT_AREA];
18514 if (row->continued_p)
18515 row->pixel_width -= it->continuation_pixel_width;
18516 else if (row->truncated_on_right_p)
18517 row->pixel_width -= it->truncation_pixel_width;
18518 row->ascent = row->phys_ascent = 0;
18519 row->height = row->phys_height = row->visible_height = 1;
18520 row->extra_line_spacing = 0;
18521 }
18522
18523 /* Compute a hash code for this row. */
18524 row->hash = row_hash (row);
18525
18526 it->max_ascent = it->max_descent = 0;
18527 it->max_phys_ascent = it->max_phys_descent = 0;
18528 }
18529
18530
18531 /* Append one space to the glyph row of iterator IT if doing a
18532 window-based redisplay. The space has the same face as
18533 IT->face_id. Value is non-zero if a space was added.
18534
18535 This function is called to make sure that there is always one glyph
18536 at the end of a glyph row that the cursor can be set on under
18537 window-systems. (If there weren't such a glyph we would not know
18538 how wide and tall a box cursor should be displayed).
18539
18540 At the same time this space let's a nicely handle clearing to the
18541 end of the line if the row ends in italic text. */
18542
18543 static int
18544 append_space_for_newline (struct it *it, int default_face_p)
18545 {
18546 if (FRAME_WINDOW_P (it->f))
18547 {
18548 int n = it->glyph_row->used[TEXT_AREA];
18549
18550 if (it->glyph_row->glyphs[TEXT_AREA] + n
18551 < it->glyph_row->glyphs[1 + TEXT_AREA])
18552 {
18553 /* Save some values that must not be changed.
18554 Must save IT->c and IT->len because otherwise
18555 ITERATOR_AT_END_P wouldn't work anymore after
18556 append_space_for_newline has been called. */
18557 enum display_element_type saved_what = it->what;
18558 int saved_c = it->c, saved_len = it->len;
18559 int saved_char_to_display = it->char_to_display;
18560 int saved_x = it->current_x;
18561 int saved_face_id = it->face_id;
18562 int saved_box_end = it->end_of_box_run_p;
18563 struct text_pos saved_pos;
18564 Lisp_Object saved_object;
18565 struct face *face;
18566
18567 saved_object = it->object;
18568 saved_pos = it->position;
18569
18570 it->what = IT_CHARACTER;
18571 memset (&it->position, 0, sizeof it->position);
18572 it->object = make_number (0);
18573 it->c = it->char_to_display = ' ';
18574 it->len = 1;
18575
18576 /* If the default face was remapped, be sure to use the
18577 remapped face for the appended newline. */
18578 if (default_face_p)
18579 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18580 else if (it->face_before_selective_p)
18581 it->face_id = it->saved_face_id;
18582 face = FACE_FROM_ID (it->f, it->face_id);
18583 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18584 /* In R2L rows, we will prepend a stretch glyph that will
18585 have the end_of_box_run_p flag set for it, so there's no
18586 need for the appended newline glyph to have that flag
18587 set. */
18588 if (it->glyph_row->reversed_p
18589 /* But if the appended newline glyph goes all the way to
18590 the end of the row, there will be no stretch glyph,
18591 so leave the box flag set. */
18592 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18593 it->end_of_box_run_p = 0;
18594
18595 PRODUCE_GLYPHS (it);
18596
18597 it->override_ascent = -1;
18598 it->constrain_row_ascent_descent_p = 0;
18599 it->current_x = saved_x;
18600 it->object = saved_object;
18601 it->position = saved_pos;
18602 it->what = saved_what;
18603 it->face_id = saved_face_id;
18604 it->len = saved_len;
18605 it->c = saved_c;
18606 it->char_to_display = saved_char_to_display;
18607 it->end_of_box_run_p = saved_box_end;
18608 return 1;
18609 }
18610 }
18611
18612 return 0;
18613 }
18614
18615
18616 /* Extend the face of the last glyph in the text area of IT->glyph_row
18617 to the end of the display line. Called from display_line. If the
18618 glyph row is empty, add a space glyph to it so that we know the
18619 face to draw. Set the glyph row flag fill_line_p. If the glyph
18620 row is R2L, prepend a stretch glyph to cover the empty space to the
18621 left of the leftmost glyph. */
18622
18623 static void
18624 extend_face_to_end_of_line (struct it *it)
18625 {
18626 struct face *face, *default_face;
18627 struct frame *f = it->f;
18628
18629 /* If line is already filled, do nothing. Non window-system frames
18630 get a grace of one more ``pixel'' because their characters are
18631 1-``pixel'' wide, so they hit the equality too early. This grace
18632 is needed only for R2L rows that are not continued, to produce
18633 one extra blank where we could display the cursor. */
18634 if (it->current_x >= it->last_visible_x
18635 + (!FRAME_WINDOW_P (f)
18636 && it->glyph_row->reversed_p
18637 && !it->glyph_row->continued_p))
18638 return;
18639
18640 /* The default face, possibly remapped. */
18641 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18642
18643 /* Face extension extends the background and box of IT->face_id
18644 to the end of the line. If the background equals the background
18645 of the frame, we don't have to do anything. */
18646 if (it->face_before_selective_p)
18647 face = FACE_FROM_ID (f, it->saved_face_id);
18648 else
18649 face = FACE_FROM_ID (f, it->face_id);
18650
18651 if (FRAME_WINDOW_P (f)
18652 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
18653 && face->box == FACE_NO_BOX
18654 && face->background == FRAME_BACKGROUND_PIXEL (f)
18655 && !face->stipple
18656 && !it->glyph_row->reversed_p)
18657 return;
18658
18659 /* Set the glyph row flag indicating that the face of the last glyph
18660 in the text area has to be drawn to the end of the text area. */
18661 it->glyph_row->fill_line_p = 1;
18662
18663 /* If current character of IT is not ASCII, make sure we have the
18664 ASCII face. This will be automatically undone the next time
18665 get_next_display_element returns a multibyte character. Note
18666 that the character will always be single byte in unibyte
18667 text. */
18668 if (!ASCII_CHAR_P (it->c))
18669 {
18670 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18671 }
18672
18673 if (FRAME_WINDOW_P (f))
18674 {
18675 /* If the row is empty, add a space with the current face of IT,
18676 so that we know which face to draw. */
18677 if (it->glyph_row->used[TEXT_AREA] == 0)
18678 {
18679 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18680 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18681 it->glyph_row->used[TEXT_AREA] = 1;
18682 }
18683 #ifdef HAVE_WINDOW_SYSTEM
18684 if (it->glyph_row->reversed_p)
18685 {
18686 /* Prepend a stretch glyph to the row, such that the
18687 rightmost glyph will be drawn flushed all the way to the
18688 right margin of the window. The stretch glyph that will
18689 occupy the empty space, if any, to the left of the
18690 glyphs. */
18691 struct font *font = face->font ? face->font : FRAME_FONT (f);
18692 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18693 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18694 struct glyph *g;
18695 int row_width, stretch_ascent, stretch_width;
18696 struct text_pos saved_pos;
18697 int saved_face_id, saved_avoid_cursor, saved_box_start;
18698
18699 for (row_width = 0, g = row_start; g < row_end; g++)
18700 row_width += g->pixel_width;
18701 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18702 if (stretch_width > 0)
18703 {
18704 stretch_ascent =
18705 (((it->ascent + it->descent)
18706 * FONT_BASE (font)) / FONT_HEIGHT (font));
18707 saved_pos = it->position;
18708 memset (&it->position, 0, sizeof it->position);
18709 saved_avoid_cursor = it->avoid_cursor_p;
18710 it->avoid_cursor_p = 1;
18711 saved_face_id = it->face_id;
18712 saved_box_start = it->start_of_box_run_p;
18713 /* The last row's stretch glyph should get the default
18714 face, to avoid painting the rest of the window with
18715 the region face, if the region ends at ZV. */
18716 if (it->glyph_row->ends_at_zv_p)
18717 it->face_id = default_face->id;
18718 else
18719 it->face_id = face->id;
18720 it->start_of_box_run_p = 0;
18721 append_stretch_glyph (it, make_number (0), stretch_width,
18722 it->ascent + it->descent, stretch_ascent);
18723 it->position = saved_pos;
18724 it->avoid_cursor_p = saved_avoid_cursor;
18725 it->face_id = saved_face_id;
18726 it->start_of_box_run_p = saved_box_start;
18727 }
18728 }
18729 #endif /* HAVE_WINDOW_SYSTEM */
18730 }
18731 else
18732 {
18733 /* Save some values that must not be changed. */
18734 int saved_x = it->current_x;
18735 struct text_pos saved_pos;
18736 Lisp_Object saved_object;
18737 enum display_element_type saved_what = it->what;
18738 int saved_face_id = it->face_id;
18739
18740 saved_object = it->object;
18741 saved_pos = it->position;
18742
18743 it->what = IT_CHARACTER;
18744 memset (&it->position, 0, sizeof it->position);
18745 it->object = make_number (0);
18746 it->c = it->char_to_display = ' ';
18747 it->len = 1;
18748 /* The last row's blank glyphs should get the default face, to
18749 avoid painting the rest of the window with the region face,
18750 if the region ends at ZV. */
18751 if (it->glyph_row->ends_at_zv_p)
18752 it->face_id = default_face->id;
18753 else
18754 it->face_id = face->id;
18755
18756 PRODUCE_GLYPHS (it);
18757
18758 while (it->current_x <= it->last_visible_x)
18759 PRODUCE_GLYPHS (it);
18760
18761 /* Don't count these blanks really. It would let us insert a left
18762 truncation glyph below and make us set the cursor on them, maybe. */
18763 it->current_x = saved_x;
18764 it->object = saved_object;
18765 it->position = saved_pos;
18766 it->what = saved_what;
18767 it->face_id = saved_face_id;
18768 }
18769 }
18770
18771
18772 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18773 trailing whitespace. */
18774
18775 static int
18776 trailing_whitespace_p (ptrdiff_t charpos)
18777 {
18778 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18779 int c = 0;
18780
18781 while (bytepos < ZV_BYTE
18782 && (c = FETCH_CHAR (bytepos),
18783 c == ' ' || c == '\t'))
18784 ++bytepos;
18785
18786 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18787 {
18788 if (bytepos != PT_BYTE)
18789 return 1;
18790 }
18791 return 0;
18792 }
18793
18794
18795 /* Highlight trailing whitespace, if any, in ROW. */
18796
18797 static void
18798 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18799 {
18800 int used = row->used[TEXT_AREA];
18801
18802 if (used)
18803 {
18804 struct glyph *start = row->glyphs[TEXT_AREA];
18805 struct glyph *glyph = start + used - 1;
18806
18807 if (row->reversed_p)
18808 {
18809 /* Right-to-left rows need to be processed in the opposite
18810 direction, so swap the edge pointers. */
18811 glyph = start;
18812 start = row->glyphs[TEXT_AREA] + used - 1;
18813 }
18814
18815 /* Skip over glyphs inserted to display the cursor at the
18816 end of a line, for extending the face of the last glyph
18817 to the end of the line on terminals, and for truncation
18818 and continuation glyphs. */
18819 if (!row->reversed_p)
18820 {
18821 while (glyph >= start
18822 && glyph->type == CHAR_GLYPH
18823 && INTEGERP (glyph->object))
18824 --glyph;
18825 }
18826 else
18827 {
18828 while (glyph <= start
18829 && glyph->type == CHAR_GLYPH
18830 && INTEGERP (glyph->object))
18831 ++glyph;
18832 }
18833
18834 /* If last glyph is a space or stretch, and it's trailing
18835 whitespace, set the face of all trailing whitespace glyphs in
18836 IT->glyph_row to `trailing-whitespace'. */
18837 if ((row->reversed_p ? glyph <= start : glyph >= start)
18838 && BUFFERP (glyph->object)
18839 && (glyph->type == STRETCH_GLYPH
18840 || (glyph->type == CHAR_GLYPH
18841 && glyph->u.ch == ' '))
18842 && trailing_whitespace_p (glyph->charpos))
18843 {
18844 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18845 if (face_id < 0)
18846 return;
18847
18848 if (!row->reversed_p)
18849 {
18850 while (glyph >= start
18851 && BUFFERP (glyph->object)
18852 && (glyph->type == STRETCH_GLYPH
18853 || (glyph->type == CHAR_GLYPH
18854 && glyph->u.ch == ' ')))
18855 (glyph--)->face_id = face_id;
18856 }
18857 else
18858 {
18859 while (glyph <= start
18860 && BUFFERP (glyph->object)
18861 && (glyph->type == STRETCH_GLYPH
18862 || (glyph->type == CHAR_GLYPH
18863 && glyph->u.ch == ' ')))
18864 (glyph++)->face_id = face_id;
18865 }
18866 }
18867 }
18868 }
18869
18870
18871 /* Value is non-zero if glyph row ROW should be
18872 considered to hold the buffer position CHARPOS. */
18873
18874 static int
18875 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
18876 {
18877 int result = 1;
18878
18879 if (charpos == CHARPOS (row->end.pos)
18880 || charpos == MATRIX_ROW_END_CHARPOS (row))
18881 {
18882 /* Suppose the row ends on a string.
18883 Unless the row is continued, that means it ends on a newline
18884 in the string. If it's anything other than a display string
18885 (e.g., a before-string from an overlay), we don't want the
18886 cursor there. (This heuristic seems to give the optimal
18887 behavior for the various types of multi-line strings.)
18888 One exception: if the string has `cursor' property on one of
18889 its characters, we _do_ want the cursor there. */
18890 if (CHARPOS (row->end.string_pos) >= 0)
18891 {
18892 if (row->continued_p)
18893 result = 1;
18894 else
18895 {
18896 /* Check for `display' property. */
18897 struct glyph *beg = row->glyphs[TEXT_AREA];
18898 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18899 struct glyph *glyph;
18900
18901 result = 0;
18902 for (glyph = end; glyph >= beg; --glyph)
18903 if (STRINGP (glyph->object))
18904 {
18905 Lisp_Object prop
18906 = Fget_char_property (make_number (charpos),
18907 Qdisplay, Qnil);
18908 result =
18909 (!NILP (prop)
18910 && display_prop_string_p (prop, glyph->object));
18911 /* If there's a `cursor' property on one of the
18912 string's characters, this row is a cursor row,
18913 even though this is not a display string. */
18914 if (!result)
18915 {
18916 Lisp_Object s = glyph->object;
18917
18918 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18919 {
18920 ptrdiff_t gpos = glyph->charpos;
18921
18922 if (!NILP (Fget_char_property (make_number (gpos),
18923 Qcursor, s)))
18924 {
18925 result = 1;
18926 break;
18927 }
18928 }
18929 }
18930 break;
18931 }
18932 }
18933 }
18934 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18935 {
18936 /* If the row ends in middle of a real character,
18937 and the line is continued, we want the cursor here.
18938 That's because CHARPOS (ROW->end.pos) would equal
18939 PT if PT is before the character. */
18940 if (!row->ends_in_ellipsis_p)
18941 result = row->continued_p;
18942 else
18943 /* If the row ends in an ellipsis, then
18944 CHARPOS (ROW->end.pos) will equal point after the
18945 invisible text. We want that position to be displayed
18946 after the ellipsis. */
18947 result = 0;
18948 }
18949 /* If the row ends at ZV, display the cursor at the end of that
18950 row instead of at the start of the row below. */
18951 else if (row->ends_at_zv_p)
18952 result = 1;
18953 else
18954 result = 0;
18955 }
18956
18957 return result;
18958 }
18959
18960 /* Value is non-zero if glyph row ROW should be
18961 used to hold the cursor. */
18962
18963 static int
18964 cursor_row_p (struct glyph_row *row)
18965 {
18966 return row_for_charpos_p (row, PT);
18967 }
18968
18969 \f
18970
18971 /* Push the property PROP so that it will be rendered at the current
18972 position in IT. Return 1 if PROP was successfully pushed, 0
18973 otherwise. Called from handle_line_prefix to handle the
18974 `line-prefix' and `wrap-prefix' properties. */
18975
18976 static int
18977 push_prefix_prop (struct it *it, Lisp_Object prop)
18978 {
18979 struct text_pos pos =
18980 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18981
18982 eassert (it->method == GET_FROM_BUFFER
18983 || it->method == GET_FROM_DISPLAY_VECTOR
18984 || it->method == GET_FROM_STRING);
18985
18986 /* We need to save the current buffer/string position, so it will be
18987 restored by pop_it, because iterate_out_of_display_property
18988 depends on that being set correctly, but some situations leave
18989 it->position not yet set when this function is called. */
18990 push_it (it, &pos);
18991
18992 if (STRINGP (prop))
18993 {
18994 if (SCHARS (prop) == 0)
18995 {
18996 pop_it (it);
18997 return 0;
18998 }
18999
19000 it->string = prop;
19001 it->string_from_prefix_prop_p = 1;
19002 it->multibyte_p = STRING_MULTIBYTE (it->string);
19003 it->current.overlay_string_index = -1;
19004 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
19005 it->end_charpos = it->string_nchars = SCHARS (it->string);
19006 it->method = GET_FROM_STRING;
19007 it->stop_charpos = 0;
19008 it->prev_stop = 0;
19009 it->base_level_stop = 0;
19010
19011 /* Force paragraph direction to be that of the parent
19012 buffer/string. */
19013 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
19014 it->paragraph_embedding = it->bidi_it.paragraph_dir;
19015 else
19016 it->paragraph_embedding = L2R;
19017
19018 /* Set up the bidi iterator for this display string. */
19019 if (it->bidi_p)
19020 {
19021 it->bidi_it.string.lstring = it->string;
19022 it->bidi_it.string.s = NULL;
19023 it->bidi_it.string.schars = it->end_charpos;
19024 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
19025 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19026 it->bidi_it.string.unibyte = !it->multibyte_p;
19027 it->bidi_it.w = it->w;
19028 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19029 }
19030 }
19031 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19032 {
19033 it->method = GET_FROM_STRETCH;
19034 it->object = prop;
19035 }
19036 #ifdef HAVE_WINDOW_SYSTEM
19037 else if (IMAGEP (prop))
19038 {
19039 it->what = IT_IMAGE;
19040 it->image_id = lookup_image (it->f, prop);
19041 it->method = GET_FROM_IMAGE;
19042 }
19043 #endif /* HAVE_WINDOW_SYSTEM */
19044 else
19045 {
19046 pop_it (it); /* bogus display property, give up */
19047 return 0;
19048 }
19049
19050 return 1;
19051 }
19052
19053 /* Return the character-property PROP at the current position in IT. */
19054
19055 static Lisp_Object
19056 get_it_property (struct it *it, Lisp_Object prop)
19057 {
19058 Lisp_Object position, object = it->object;
19059
19060 if (STRINGP (object))
19061 position = make_number (IT_STRING_CHARPOS (*it));
19062 else if (BUFFERP (object))
19063 {
19064 position = make_number (IT_CHARPOS (*it));
19065 object = it->window;
19066 }
19067 else
19068 return Qnil;
19069
19070 return Fget_char_property (position, prop, object);
19071 }
19072
19073 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19074
19075 static void
19076 handle_line_prefix (struct it *it)
19077 {
19078 Lisp_Object prefix;
19079
19080 if (it->continuation_lines_width > 0)
19081 {
19082 prefix = get_it_property (it, Qwrap_prefix);
19083 if (NILP (prefix))
19084 prefix = Vwrap_prefix;
19085 }
19086 else
19087 {
19088 prefix = get_it_property (it, Qline_prefix);
19089 if (NILP (prefix))
19090 prefix = Vline_prefix;
19091 }
19092 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19093 {
19094 /* If the prefix is wider than the window, and we try to wrap
19095 it, it would acquire its own wrap prefix, and so on till the
19096 iterator stack overflows. So, don't wrap the prefix. */
19097 it->line_wrap = TRUNCATE;
19098 it->avoid_cursor_p = 1;
19099 }
19100 }
19101
19102 \f
19103
19104 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19105 only for R2L lines from display_line and display_string, when they
19106 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19107 the line/string needs to be continued on the next glyph row. */
19108 static void
19109 unproduce_glyphs (struct it *it, int n)
19110 {
19111 struct glyph *glyph, *end;
19112
19113 eassert (it->glyph_row);
19114 eassert (it->glyph_row->reversed_p);
19115 eassert (it->area == TEXT_AREA);
19116 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19117
19118 if (n > it->glyph_row->used[TEXT_AREA])
19119 n = it->glyph_row->used[TEXT_AREA];
19120 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19121 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19122 for ( ; glyph < end; glyph++)
19123 glyph[-n] = *glyph;
19124 }
19125
19126 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19127 and ROW->maxpos. */
19128 static void
19129 find_row_edges (struct it *it, struct glyph_row *row,
19130 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19131 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19132 {
19133 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19134 lines' rows is implemented for bidi-reordered rows. */
19135
19136 /* ROW->minpos is the value of min_pos, the minimal buffer position
19137 we have in ROW, or ROW->start.pos if that is smaller. */
19138 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19139 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19140 else
19141 /* We didn't find buffer positions smaller than ROW->start, or
19142 didn't find _any_ valid buffer positions in any of the glyphs,
19143 so we must trust the iterator's computed positions. */
19144 row->minpos = row->start.pos;
19145 if (max_pos <= 0)
19146 {
19147 max_pos = CHARPOS (it->current.pos);
19148 max_bpos = BYTEPOS (it->current.pos);
19149 }
19150
19151 /* Here are the various use-cases for ending the row, and the
19152 corresponding values for ROW->maxpos:
19153
19154 Line ends in a newline from buffer eol_pos + 1
19155 Line is continued from buffer max_pos + 1
19156 Line is truncated on right it->current.pos
19157 Line ends in a newline from string max_pos + 1(*)
19158 (*) + 1 only when line ends in a forward scan
19159 Line is continued from string max_pos
19160 Line is continued from display vector max_pos
19161 Line is entirely from a string min_pos == max_pos
19162 Line is entirely from a display vector min_pos == max_pos
19163 Line that ends at ZV ZV
19164
19165 If you discover other use-cases, please add them here as
19166 appropriate. */
19167 if (row->ends_at_zv_p)
19168 row->maxpos = it->current.pos;
19169 else if (row->used[TEXT_AREA])
19170 {
19171 int seen_this_string = 0;
19172 struct glyph_row *r1 = row - 1;
19173
19174 /* Did we see the same display string on the previous row? */
19175 if (STRINGP (it->object)
19176 /* this is not the first row */
19177 && row > it->w->desired_matrix->rows
19178 /* previous row is not the header line */
19179 && !r1->mode_line_p
19180 /* previous row also ends in a newline from a string */
19181 && r1->ends_in_newline_from_string_p)
19182 {
19183 struct glyph *start, *end;
19184
19185 /* Search for the last glyph of the previous row that came
19186 from buffer or string. Depending on whether the row is
19187 L2R or R2L, we need to process it front to back or the
19188 other way round. */
19189 if (!r1->reversed_p)
19190 {
19191 start = r1->glyphs[TEXT_AREA];
19192 end = start + r1->used[TEXT_AREA];
19193 /* Glyphs inserted by redisplay have an integer (zero)
19194 as their object. */
19195 while (end > start
19196 && INTEGERP ((end - 1)->object)
19197 && (end - 1)->charpos <= 0)
19198 --end;
19199 if (end > start)
19200 {
19201 if (EQ ((end - 1)->object, it->object))
19202 seen_this_string = 1;
19203 }
19204 else
19205 /* If all the glyphs of the previous row were inserted
19206 by redisplay, it means the previous row was
19207 produced from a single newline, which is only
19208 possible if that newline came from the same string
19209 as the one which produced this ROW. */
19210 seen_this_string = 1;
19211 }
19212 else
19213 {
19214 end = r1->glyphs[TEXT_AREA] - 1;
19215 start = end + r1->used[TEXT_AREA];
19216 while (end < start
19217 && INTEGERP ((end + 1)->object)
19218 && (end + 1)->charpos <= 0)
19219 ++end;
19220 if (end < start)
19221 {
19222 if (EQ ((end + 1)->object, it->object))
19223 seen_this_string = 1;
19224 }
19225 else
19226 seen_this_string = 1;
19227 }
19228 }
19229 /* Take note of each display string that covers a newline only
19230 once, the first time we see it. This is for when a display
19231 string includes more than one newline in it. */
19232 if (row->ends_in_newline_from_string_p && !seen_this_string)
19233 {
19234 /* If we were scanning the buffer forward when we displayed
19235 the string, we want to account for at least one buffer
19236 position that belongs to this row (position covered by
19237 the display string), so that cursor positioning will
19238 consider this row as a candidate when point is at the end
19239 of the visual line represented by this row. This is not
19240 required when scanning back, because max_pos will already
19241 have a much larger value. */
19242 if (CHARPOS (row->end.pos) > max_pos)
19243 INC_BOTH (max_pos, max_bpos);
19244 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19245 }
19246 else if (CHARPOS (it->eol_pos) > 0)
19247 SET_TEXT_POS (row->maxpos,
19248 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19249 else if (row->continued_p)
19250 {
19251 /* If max_pos is different from IT's current position, it
19252 means IT->method does not belong to the display element
19253 at max_pos. However, it also means that the display
19254 element at max_pos was displayed in its entirety on this
19255 line, which is equivalent to saying that the next line
19256 starts at the next buffer position. */
19257 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19258 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19259 else
19260 {
19261 INC_BOTH (max_pos, max_bpos);
19262 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19263 }
19264 }
19265 else if (row->truncated_on_right_p)
19266 /* display_line already called reseat_at_next_visible_line_start,
19267 which puts the iterator at the beginning of the next line, in
19268 the logical order. */
19269 row->maxpos = it->current.pos;
19270 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19271 /* A line that is entirely from a string/image/stretch... */
19272 row->maxpos = row->minpos;
19273 else
19274 emacs_abort ();
19275 }
19276 else
19277 row->maxpos = it->current.pos;
19278 }
19279
19280 /* Construct the glyph row IT->glyph_row in the desired matrix of
19281 IT->w from text at the current position of IT. See dispextern.h
19282 for an overview of struct it. Value is non-zero if
19283 IT->glyph_row displays text, as opposed to a line displaying ZV
19284 only. */
19285
19286 static int
19287 display_line (struct it *it)
19288 {
19289 struct glyph_row *row = it->glyph_row;
19290 Lisp_Object overlay_arrow_string;
19291 struct it wrap_it;
19292 void *wrap_data = NULL;
19293 int may_wrap = 0, wrap_x IF_LINT (= 0);
19294 int wrap_row_used = -1;
19295 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19296 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19297 int wrap_row_extra_line_spacing IF_LINT (= 0);
19298 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19299 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19300 int cvpos;
19301 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19302 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19303
19304 /* We always start displaying at hpos zero even if hscrolled. */
19305 eassert (it->hpos == 0 && it->current_x == 0);
19306
19307 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19308 >= it->w->desired_matrix->nrows)
19309 {
19310 it->w->nrows_scale_factor++;
19311 fonts_changed_p = 1;
19312 return 0;
19313 }
19314
19315 /* Is IT->w showing the region? */
19316 it->w->region_showing = it->region_beg_charpos > 0 ? it->region_beg_charpos : 0;
19317
19318 /* Clear the result glyph row and enable it. */
19319 prepare_desired_row (row);
19320
19321 row->y = it->current_y;
19322 row->start = it->start;
19323 row->continuation_lines_width = it->continuation_lines_width;
19324 row->displays_text_p = 1;
19325 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19326 it->starts_in_middle_of_char_p = 0;
19327
19328 /* Arrange the overlays nicely for our purposes. Usually, we call
19329 display_line on only one line at a time, in which case this
19330 can't really hurt too much, or we call it on lines which appear
19331 one after another in the buffer, in which case all calls to
19332 recenter_overlay_lists but the first will be pretty cheap. */
19333 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19334
19335 /* Move over display elements that are not visible because we are
19336 hscrolled. This may stop at an x-position < IT->first_visible_x
19337 if the first glyph is partially visible or if we hit a line end. */
19338 if (it->current_x < it->first_visible_x)
19339 {
19340 enum move_it_result move_result;
19341
19342 this_line_min_pos = row->start.pos;
19343 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19344 MOVE_TO_POS | MOVE_TO_X);
19345 /* If we are under a large hscroll, move_it_in_display_line_to
19346 could hit the end of the line without reaching
19347 it->first_visible_x. Pretend that we did reach it. This is
19348 especially important on a TTY, where we will call
19349 extend_face_to_end_of_line, which needs to know how many
19350 blank glyphs to produce. */
19351 if (it->current_x < it->first_visible_x
19352 && (move_result == MOVE_NEWLINE_OR_CR
19353 || move_result == MOVE_POS_MATCH_OR_ZV))
19354 it->current_x = it->first_visible_x;
19355
19356 /* Record the smallest positions seen while we moved over
19357 display elements that are not visible. This is needed by
19358 redisplay_internal for optimizing the case where the cursor
19359 stays inside the same line. The rest of this function only
19360 considers positions that are actually displayed, so
19361 RECORD_MAX_MIN_POS will not otherwise record positions that
19362 are hscrolled to the left of the left edge of the window. */
19363 min_pos = CHARPOS (this_line_min_pos);
19364 min_bpos = BYTEPOS (this_line_min_pos);
19365 }
19366 else
19367 {
19368 /* We only do this when not calling `move_it_in_display_line_to'
19369 above, because move_it_in_display_line_to calls
19370 handle_line_prefix itself. */
19371 handle_line_prefix (it);
19372 }
19373
19374 /* Get the initial row height. This is either the height of the
19375 text hscrolled, if there is any, or zero. */
19376 row->ascent = it->max_ascent;
19377 row->height = it->max_ascent + it->max_descent;
19378 row->phys_ascent = it->max_phys_ascent;
19379 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19380 row->extra_line_spacing = it->max_extra_line_spacing;
19381
19382 /* Utility macro to record max and min buffer positions seen until now. */
19383 #define RECORD_MAX_MIN_POS(IT) \
19384 do \
19385 { \
19386 int composition_p = !STRINGP ((IT)->string) \
19387 && ((IT)->what == IT_COMPOSITION); \
19388 ptrdiff_t current_pos = \
19389 composition_p ? (IT)->cmp_it.charpos \
19390 : IT_CHARPOS (*(IT)); \
19391 ptrdiff_t current_bpos = \
19392 composition_p ? CHAR_TO_BYTE (current_pos) \
19393 : IT_BYTEPOS (*(IT)); \
19394 if (current_pos < min_pos) \
19395 { \
19396 min_pos = current_pos; \
19397 min_bpos = current_bpos; \
19398 } \
19399 if (IT_CHARPOS (*it) > max_pos) \
19400 { \
19401 max_pos = IT_CHARPOS (*it); \
19402 max_bpos = IT_BYTEPOS (*it); \
19403 } \
19404 } \
19405 while (0)
19406
19407 /* Loop generating characters. The loop is left with IT on the next
19408 character to display. */
19409 while (1)
19410 {
19411 int n_glyphs_before, hpos_before, x_before;
19412 int x, nglyphs;
19413 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19414
19415 /* Retrieve the next thing to display. Value is zero if end of
19416 buffer reached. */
19417 if (!get_next_display_element (it))
19418 {
19419 /* Maybe add a space at the end of this line that is used to
19420 display the cursor there under X. Set the charpos of the
19421 first glyph of blank lines not corresponding to any text
19422 to -1. */
19423 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19424 row->exact_window_width_line_p = 1;
19425 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19426 || row->used[TEXT_AREA] == 0)
19427 {
19428 row->glyphs[TEXT_AREA]->charpos = -1;
19429 row->displays_text_p = 0;
19430
19431 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
19432 && (!MINI_WINDOW_P (it->w)
19433 || (minibuf_level && EQ (it->window, minibuf_window))))
19434 row->indicate_empty_line_p = 1;
19435 }
19436
19437 it->continuation_lines_width = 0;
19438 row->ends_at_zv_p = 1;
19439 /* A row that displays right-to-left text must always have
19440 its last face extended all the way to the end of line,
19441 even if this row ends in ZV, because we still write to
19442 the screen left to right. We also need to extend the
19443 last face if the default face is remapped to some
19444 different face, otherwise the functions that clear
19445 portions of the screen will clear with the default face's
19446 background color. */
19447 if (row->reversed_p
19448 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19449 extend_face_to_end_of_line (it);
19450 break;
19451 }
19452
19453 /* Now, get the metrics of what we want to display. This also
19454 generates glyphs in `row' (which is IT->glyph_row). */
19455 n_glyphs_before = row->used[TEXT_AREA];
19456 x = it->current_x;
19457
19458 /* Remember the line height so far in case the next element doesn't
19459 fit on the line. */
19460 if (it->line_wrap != TRUNCATE)
19461 {
19462 ascent = it->max_ascent;
19463 descent = it->max_descent;
19464 phys_ascent = it->max_phys_ascent;
19465 phys_descent = it->max_phys_descent;
19466
19467 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19468 {
19469 if (IT_DISPLAYING_WHITESPACE (it))
19470 may_wrap = 1;
19471 else if (may_wrap)
19472 {
19473 SAVE_IT (wrap_it, *it, wrap_data);
19474 wrap_x = x;
19475 wrap_row_used = row->used[TEXT_AREA];
19476 wrap_row_ascent = row->ascent;
19477 wrap_row_height = row->height;
19478 wrap_row_phys_ascent = row->phys_ascent;
19479 wrap_row_phys_height = row->phys_height;
19480 wrap_row_extra_line_spacing = row->extra_line_spacing;
19481 wrap_row_min_pos = min_pos;
19482 wrap_row_min_bpos = min_bpos;
19483 wrap_row_max_pos = max_pos;
19484 wrap_row_max_bpos = max_bpos;
19485 may_wrap = 0;
19486 }
19487 }
19488 }
19489
19490 PRODUCE_GLYPHS (it);
19491
19492 /* If this display element was in marginal areas, continue with
19493 the next one. */
19494 if (it->area != TEXT_AREA)
19495 {
19496 row->ascent = max (row->ascent, it->max_ascent);
19497 row->height = max (row->height, it->max_ascent + it->max_descent);
19498 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19499 row->phys_height = max (row->phys_height,
19500 it->max_phys_ascent + it->max_phys_descent);
19501 row->extra_line_spacing = max (row->extra_line_spacing,
19502 it->max_extra_line_spacing);
19503 set_iterator_to_next (it, 1);
19504 continue;
19505 }
19506
19507 /* Does the display element fit on the line? If we truncate
19508 lines, we should draw past the right edge of the window. If
19509 we don't truncate, we want to stop so that we can display the
19510 continuation glyph before the right margin. If lines are
19511 continued, there are two possible strategies for characters
19512 resulting in more than 1 glyph (e.g. tabs): Display as many
19513 glyphs as possible in this line and leave the rest for the
19514 continuation line, or display the whole element in the next
19515 line. Original redisplay did the former, so we do it also. */
19516 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19517 hpos_before = it->hpos;
19518 x_before = x;
19519
19520 if (/* Not a newline. */
19521 nglyphs > 0
19522 /* Glyphs produced fit entirely in the line. */
19523 && it->current_x < it->last_visible_x)
19524 {
19525 it->hpos += nglyphs;
19526 row->ascent = max (row->ascent, it->max_ascent);
19527 row->height = max (row->height, it->max_ascent + it->max_descent);
19528 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19529 row->phys_height = max (row->phys_height,
19530 it->max_phys_ascent + it->max_phys_descent);
19531 row->extra_line_spacing = max (row->extra_line_spacing,
19532 it->max_extra_line_spacing);
19533 if (it->current_x - it->pixel_width < it->first_visible_x)
19534 row->x = x - it->first_visible_x;
19535 /* Record the maximum and minimum buffer positions seen so
19536 far in glyphs that will be displayed by this row. */
19537 if (it->bidi_p)
19538 RECORD_MAX_MIN_POS (it);
19539 }
19540 else
19541 {
19542 int i, new_x;
19543 struct glyph *glyph;
19544
19545 for (i = 0; i < nglyphs; ++i, x = new_x)
19546 {
19547 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19548 new_x = x + glyph->pixel_width;
19549
19550 if (/* Lines are continued. */
19551 it->line_wrap != TRUNCATE
19552 && (/* Glyph doesn't fit on the line. */
19553 new_x > it->last_visible_x
19554 /* Or it fits exactly on a window system frame. */
19555 || (new_x == it->last_visible_x
19556 && FRAME_WINDOW_P (it->f)
19557 && (row->reversed_p
19558 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19559 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19560 {
19561 /* End of a continued line. */
19562
19563 if (it->hpos == 0
19564 || (new_x == it->last_visible_x
19565 && FRAME_WINDOW_P (it->f)
19566 && (row->reversed_p
19567 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19568 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19569 {
19570 /* Current glyph is the only one on the line or
19571 fits exactly on the line. We must continue
19572 the line because we can't draw the cursor
19573 after the glyph. */
19574 row->continued_p = 1;
19575 it->current_x = new_x;
19576 it->continuation_lines_width += new_x;
19577 ++it->hpos;
19578 if (i == nglyphs - 1)
19579 {
19580 /* If line-wrap is on, check if a previous
19581 wrap point was found. */
19582 if (wrap_row_used > 0
19583 /* Even if there is a previous wrap
19584 point, continue the line here as
19585 usual, if (i) the previous character
19586 was a space or tab AND (ii) the
19587 current character is not. */
19588 && (!may_wrap
19589 || IT_DISPLAYING_WHITESPACE (it)))
19590 goto back_to_wrap;
19591
19592 /* Record the maximum and minimum buffer
19593 positions seen so far in glyphs that will be
19594 displayed by this row. */
19595 if (it->bidi_p)
19596 RECORD_MAX_MIN_POS (it);
19597 set_iterator_to_next (it, 1);
19598 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19599 {
19600 if (!get_next_display_element (it))
19601 {
19602 row->exact_window_width_line_p = 1;
19603 it->continuation_lines_width = 0;
19604 row->continued_p = 0;
19605 row->ends_at_zv_p = 1;
19606 }
19607 else if (ITERATOR_AT_END_OF_LINE_P (it))
19608 {
19609 row->continued_p = 0;
19610 row->exact_window_width_line_p = 1;
19611 }
19612 }
19613 }
19614 else if (it->bidi_p)
19615 RECORD_MAX_MIN_POS (it);
19616 }
19617 else if (CHAR_GLYPH_PADDING_P (*glyph)
19618 && !FRAME_WINDOW_P (it->f))
19619 {
19620 /* A padding glyph that doesn't fit on this line.
19621 This means the whole character doesn't fit
19622 on the line. */
19623 if (row->reversed_p)
19624 unproduce_glyphs (it, row->used[TEXT_AREA]
19625 - n_glyphs_before);
19626 row->used[TEXT_AREA] = n_glyphs_before;
19627
19628 /* Fill the rest of the row with continuation
19629 glyphs like in 20.x. */
19630 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19631 < row->glyphs[1 + TEXT_AREA])
19632 produce_special_glyphs (it, IT_CONTINUATION);
19633
19634 row->continued_p = 1;
19635 it->current_x = x_before;
19636 it->continuation_lines_width += x_before;
19637
19638 /* Restore the height to what it was before the
19639 element not fitting on the line. */
19640 it->max_ascent = ascent;
19641 it->max_descent = descent;
19642 it->max_phys_ascent = phys_ascent;
19643 it->max_phys_descent = phys_descent;
19644 }
19645 else if (wrap_row_used > 0)
19646 {
19647 back_to_wrap:
19648 if (row->reversed_p)
19649 unproduce_glyphs (it,
19650 row->used[TEXT_AREA] - wrap_row_used);
19651 RESTORE_IT (it, &wrap_it, wrap_data);
19652 it->continuation_lines_width += wrap_x;
19653 row->used[TEXT_AREA] = wrap_row_used;
19654 row->ascent = wrap_row_ascent;
19655 row->height = wrap_row_height;
19656 row->phys_ascent = wrap_row_phys_ascent;
19657 row->phys_height = wrap_row_phys_height;
19658 row->extra_line_spacing = wrap_row_extra_line_spacing;
19659 min_pos = wrap_row_min_pos;
19660 min_bpos = wrap_row_min_bpos;
19661 max_pos = wrap_row_max_pos;
19662 max_bpos = wrap_row_max_bpos;
19663 row->continued_p = 1;
19664 row->ends_at_zv_p = 0;
19665 row->exact_window_width_line_p = 0;
19666 it->continuation_lines_width += x;
19667
19668 /* Make sure that a non-default face is extended
19669 up to the right margin of the window. */
19670 extend_face_to_end_of_line (it);
19671 }
19672 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19673 {
19674 /* A TAB that extends past the right edge of the
19675 window. This produces a single glyph on
19676 window system frames. We leave the glyph in
19677 this row and let it fill the row, but don't
19678 consume the TAB. */
19679 if ((row->reversed_p
19680 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19681 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19682 produce_special_glyphs (it, IT_CONTINUATION);
19683 it->continuation_lines_width += it->last_visible_x;
19684 row->ends_in_middle_of_char_p = 1;
19685 row->continued_p = 1;
19686 glyph->pixel_width = it->last_visible_x - x;
19687 it->starts_in_middle_of_char_p = 1;
19688 }
19689 else
19690 {
19691 /* Something other than a TAB that draws past
19692 the right edge of the window. Restore
19693 positions to values before the element. */
19694 if (row->reversed_p)
19695 unproduce_glyphs (it, row->used[TEXT_AREA]
19696 - (n_glyphs_before + i));
19697 row->used[TEXT_AREA] = n_glyphs_before + i;
19698
19699 /* Display continuation glyphs. */
19700 it->current_x = x_before;
19701 it->continuation_lines_width += x;
19702 if (!FRAME_WINDOW_P (it->f)
19703 || (row->reversed_p
19704 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19705 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19706 produce_special_glyphs (it, IT_CONTINUATION);
19707 row->continued_p = 1;
19708
19709 extend_face_to_end_of_line (it);
19710
19711 if (nglyphs > 1 && i > 0)
19712 {
19713 row->ends_in_middle_of_char_p = 1;
19714 it->starts_in_middle_of_char_p = 1;
19715 }
19716
19717 /* Restore the height to what it was before the
19718 element not fitting on the line. */
19719 it->max_ascent = ascent;
19720 it->max_descent = descent;
19721 it->max_phys_ascent = phys_ascent;
19722 it->max_phys_descent = phys_descent;
19723 }
19724
19725 break;
19726 }
19727 else if (new_x > it->first_visible_x)
19728 {
19729 /* Increment number of glyphs actually displayed. */
19730 ++it->hpos;
19731
19732 /* Record the maximum and minimum buffer positions
19733 seen so far in glyphs that will be displayed by
19734 this row. */
19735 if (it->bidi_p)
19736 RECORD_MAX_MIN_POS (it);
19737
19738 if (x < it->first_visible_x)
19739 /* Glyph is partially visible, i.e. row starts at
19740 negative X position. */
19741 row->x = x - it->first_visible_x;
19742 }
19743 else
19744 {
19745 /* Glyph is completely off the left margin of the
19746 window. This should not happen because of the
19747 move_it_in_display_line at the start of this
19748 function, unless the text display area of the
19749 window is empty. */
19750 eassert (it->first_visible_x <= it->last_visible_x);
19751 }
19752 }
19753 /* Even if this display element produced no glyphs at all,
19754 we want to record its position. */
19755 if (it->bidi_p && nglyphs == 0)
19756 RECORD_MAX_MIN_POS (it);
19757
19758 row->ascent = max (row->ascent, it->max_ascent);
19759 row->height = max (row->height, it->max_ascent + it->max_descent);
19760 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19761 row->phys_height = max (row->phys_height,
19762 it->max_phys_ascent + it->max_phys_descent);
19763 row->extra_line_spacing = max (row->extra_line_spacing,
19764 it->max_extra_line_spacing);
19765
19766 /* End of this display line if row is continued. */
19767 if (row->continued_p || row->ends_at_zv_p)
19768 break;
19769 }
19770
19771 at_end_of_line:
19772 /* Is this a line end? If yes, we're also done, after making
19773 sure that a non-default face is extended up to the right
19774 margin of the window. */
19775 if (ITERATOR_AT_END_OF_LINE_P (it))
19776 {
19777 int used_before = row->used[TEXT_AREA];
19778
19779 row->ends_in_newline_from_string_p = STRINGP (it->object);
19780
19781 /* Add a space at the end of the line that is used to
19782 display the cursor there. */
19783 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19784 append_space_for_newline (it, 0);
19785
19786 /* Extend the face to the end of the line. */
19787 extend_face_to_end_of_line (it);
19788
19789 /* Make sure we have the position. */
19790 if (used_before == 0)
19791 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19792
19793 /* Record the position of the newline, for use in
19794 find_row_edges. */
19795 it->eol_pos = it->current.pos;
19796
19797 /* Consume the line end. This skips over invisible lines. */
19798 set_iterator_to_next (it, 1);
19799 it->continuation_lines_width = 0;
19800 break;
19801 }
19802
19803 /* Proceed with next display element. Note that this skips
19804 over lines invisible because of selective display. */
19805 set_iterator_to_next (it, 1);
19806
19807 /* If we truncate lines, we are done when the last displayed
19808 glyphs reach past the right margin of the window. */
19809 if (it->line_wrap == TRUNCATE
19810 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19811 ? (it->current_x >= it->last_visible_x)
19812 : (it->current_x > it->last_visible_x)))
19813 {
19814 /* Maybe add truncation glyphs. */
19815 if (!FRAME_WINDOW_P (it->f)
19816 || (row->reversed_p
19817 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19818 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19819 {
19820 int i, n;
19821
19822 if (!row->reversed_p)
19823 {
19824 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19825 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19826 break;
19827 }
19828 else
19829 {
19830 for (i = 0; i < row->used[TEXT_AREA]; i++)
19831 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19832 break;
19833 /* Remove any padding glyphs at the front of ROW, to
19834 make room for the truncation glyphs we will be
19835 adding below. The loop below always inserts at
19836 least one truncation glyph, so also remove the
19837 last glyph added to ROW. */
19838 unproduce_glyphs (it, i + 1);
19839 /* Adjust i for the loop below. */
19840 i = row->used[TEXT_AREA] - (i + 1);
19841 }
19842
19843 it->current_x = x_before;
19844 if (!FRAME_WINDOW_P (it->f))
19845 {
19846 for (n = row->used[TEXT_AREA]; i < n; ++i)
19847 {
19848 row->used[TEXT_AREA] = i;
19849 produce_special_glyphs (it, IT_TRUNCATION);
19850 }
19851 }
19852 else
19853 {
19854 row->used[TEXT_AREA] = i;
19855 produce_special_glyphs (it, IT_TRUNCATION);
19856 }
19857 }
19858 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19859 {
19860 /* Don't truncate if we can overflow newline into fringe. */
19861 if (!get_next_display_element (it))
19862 {
19863 it->continuation_lines_width = 0;
19864 row->ends_at_zv_p = 1;
19865 row->exact_window_width_line_p = 1;
19866 break;
19867 }
19868 if (ITERATOR_AT_END_OF_LINE_P (it))
19869 {
19870 row->exact_window_width_line_p = 1;
19871 goto at_end_of_line;
19872 }
19873 it->current_x = x_before;
19874 }
19875
19876 row->truncated_on_right_p = 1;
19877 it->continuation_lines_width = 0;
19878 reseat_at_next_visible_line_start (it, 0);
19879 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19880 it->hpos = hpos_before;
19881 break;
19882 }
19883 }
19884
19885 if (wrap_data)
19886 bidi_unshelve_cache (wrap_data, 1);
19887
19888 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19889 at the left window margin. */
19890 if (it->first_visible_x
19891 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19892 {
19893 if (!FRAME_WINDOW_P (it->f)
19894 || (row->reversed_p
19895 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19896 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19897 insert_left_trunc_glyphs (it);
19898 row->truncated_on_left_p = 1;
19899 }
19900
19901 /* Remember the position at which this line ends.
19902
19903 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19904 cannot be before the call to find_row_edges below, since that is
19905 where these positions are determined. */
19906 row->end = it->current;
19907 if (!it->bidi_p)
19908 {
19909 row->minpos = row->start.pos;
19910 row->maxpos = row->end.pos;
19911 }
19912 else
19913 {
19914 /* ROW->minpos and ROW->maxpos must be the smallest and
19915 `1 + the largest' buffer positions in ROW. But if ROW was
19916 bidi-reordered, these two positions can be anywhere in the
19917 row, so we must determine them now. */
19918 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19919 }
19920
19921 /* If the start of this line is the overlay arrow-position, then
19922 mark this glyph row as the one containing the overlay arrow.
19923 This is clearly a mess with variable size fonts. It would be
19924 better to let it be displayed like cursors under X. */
19925 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
19926 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19927 !NILP (overlay_arrow_string)))
19928 {
19929 /* Overlay arrow in window redisplay is a fringe bitmap. */
19930 if (STRINGP (overlay_arrow_string))
19931 {
19932 struct glyph_row *arrow_row
19933 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19934 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19935 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19936 struct glyph *p = row->glyphs[TEXT_AREA];
19937 struct glyph *p2, *end;
19938
19939 /* Copy the arrow glyphs. */
19940 while (glyph < arrow_end)
19941 *p++ = *glyph++;
19942
19943 /* Throw away padding glyphs. */
19944 p2 = p;
19945 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19946 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19947 ++p2;
19948 if (p2 > p)
19949 {
19950 while (p2 < end)
19951 *p++ = *p2++;
19952 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19953 }
19954 }
19955 else
19956 {
19957 eassert (INTEGERP (overlay_arrow_string));
19958 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19959 }
19960 overlay_arrow_seen = 1;
19961 }
19962
19963 /* Highlight trailing whitespace. */
19964 if (!NILP (Vshow_trailing_whitespace))
19965 highlight_trailing_whitespace (it->f, it->glyph_row);
19966
19967 /* Compute pixel dimensions of this line. */
19968 compute_line_metrics (it);
19969
19970 /* Implementation note: No changes in the glyphs of ROW or in their
19971 faces can be done past this point, because compute_line_metrics
19972 computes ROW's hash value and stores it within the glyph_row
19973 structure. */
19974
19975 /* Record whether this row ends inside an ellipsis. */
19976 row->ends_in_ellipsis_p
19977 = (it->method == GET_FROM_DISPLAY_VECTOR
19978 && it->ellipsis_p);
19979
19980 /* Save fringe bitmaps in this row. */
19981 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19982 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19983 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19984 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19985
19986 it->left_user_fringe_bitmap = 0;
19987 it->left_user_fringe_face_id = 0;
19988 it->right_user_fringe_bitmap = 0;
19989 it->right_user_fringe_face_id = 0;
19990
19991 /* Maybe set the cursor. */
19992 cvpos = it->w->cursor.vpos;
19993 if ((cvpos < 0
19994 /* In bidi-reordered rows, keep checking for proper cursor
19995 position even if one has been found already, because buffer
19996 positions in such rows change non-linearly with ROW->VPOS,
19997 when a line is continued. One exception: when we are at ZV,
19998 display cursor on the first suitable glyph row, since all
19999 the empty rows after that also have their position set to ZV. */
20000 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20001 lines' rows is implemented for bidi-reordered rows. */
20002 || (it->bidi_p
20003 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
20004 && PT >= MATRIX_ROW_START_CHARPOS (row)
20005 && PT <= MATRIX_ROW_END_CHARPOS (row)
20006 && cursor_row_p (row))
20007 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
20008
20009 /* Prepare for the next line. This line starts horizontally at (X
20010 HPOS) = (0 0). Vertical positions are incremented. As a
20011 convenience for the caller, IT->glyph_row is set to the next
20012 row to be used. */
20013 it->current_x = it->hpos = 0;
20014 it->current_y += row->height;
20015 SET_TEXT_POS (it->eol_pos, 0, 0);
20016 ++it->vpos;
20017 ++it->glyph_row;
20018 /* The next row should by default use the same value of the
20019 reversed_p flag as this one. set_iterator_to_next decides when
20020 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
20021 the flag accordingly. */
20022 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
20023 it->glyph_row->reversed_p = row->reversed_p;
20024 it->start = row->end;
20025 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
20026
20027 #undef RECORD_MAX_MIN_POS
20028 }
20029
20030 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
20031 Scurrent_bidi_paragraph_direction, 0, 1, 0,
20032 doc: /* Return paragraph direction at point in BUFFER.
20033 Value is either `left-to-right' or `right-to-left'.
20034 If BUFFER is omitted or nil, it defaults to the current buffer.
20035
20036 Paragraph direction determines how the text in the paragraph is displayed.
20037 In left-to-right paragraphs, text begins at the left margin of the window
20038 and the reading direction is generally left to right. In right-to-left
20039 paragraphs, text begins at the right margin and is read from right to left.
20040
20041 See also `bidi-paragraph-direction'. */)
20042 (Lisp_Object buffer)
20043 {
20044 struct buffer *buf = current_buffer;
20045 struct buffer *old = buf;
20046
20047 if (! NILP (buffer))
20048 {
20049 CHECK_BUFFER (buffer);
20050 buf = XBUFFER (buffer);
20051 }
20052
20053 if (NILP (BVAR (buf, bidi_display_reordering))
20054 || NILP (BVAR (buf, enable_multibyte_characters))
20055 /* When we are loading loadup.el, the character property tables
20056 needed for bidi iteration are not yet available. */
20057 || !NILP (Vpurify_flag))
20058 return Qleft_to_right;
20059 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20060 return BVAR (buf, bidi_paragraph_direction);
20061 else
20062 {
20063 /* Determine the direction from buffer text. We could try to
20064 use current_matrix if it is up to date, but this seems fast
20065 enough as it is. */
20066 struct bidi_it itb;
20067 ptrdiff_t pos = BUF_PT (buf);
20068 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20069 int c;
20070 void *itb_data = bidi_shelve_cache ();
20071
20072 set_buffer_temp (buf);
20073 /* bidi_paragraph_init finds the base direction of the paragraph
20074 by searching forward from paragraph start. We need the base
20075 direction of the current or _previous_ paragraph, so we need
20076 to make sure we are within that paragraph. To that end, find
20077 the previous non-empty line. */
20078 if (pos >= ZV && pos > BEGV)
20079 DEC_BOTH (pos, bytepos);
20080 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20081 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20082 {
20083 while ((c = FETCH_BYTE (bytepos)) == '\n'
20084 || c == ' ' || c == '\t' || c == '\f')
20085 {
20086 if (bytepos <= BEGV_BYTE)
20087 break;
20088 bytepos--;
20089 pos--;
20090 }
20091 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20092 bytepos--;
20093 }
20094 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20095 itb.paragraph_dir = NEUTRAL_DIR;
20096 itb.string.s = NULL;
20097 itb.string.lstring = Qnil;
20098 itb.string.bufpos = 0;
20099 itb.string.unibyte = 0;
20100 /* We have no window to use here for ignoring window-specific
20101 overlays. Using NULL for window pointer will cause
20102 compute_display_string_pos to use the current buffer. */
20103 itb.w = NULL;
20104 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20105 bidi_unshelve_cache (itb_data, 0);
20106 set_buffer_temp (old);
20107 switch (itb.paragraph_dir)
20108 {
20109 case L2R:
20110 return Qleft_to_right;
20111 break;
20112 case R2L:
20113 return Qright_to_left;
20114 break;
20115 default:
20116 emacs_abort ();
20117 }
20118 }
20119 }
20120
20121 DEFUN ("move-point-visually", Fmove_point_visually,
20122 Smove_point_visually, 1, 1, 0,
20123 doc: /* Move point in the visual order in the specified DIRECTION.
20124 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
20125 left.
20126
20127 Value is the new character position of point. */)
20128 (Lisp_Object direction)
20129 {
20130 struct window *w = XWINDOW (selected_window);
20131 struct buffer *b = XBUFFER (w->contents);
20132 struct glyph_row *row;
20133 int dir;
20134 Lisp_Object paragraph_dir;
20135
20136 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
20137 (!(ROW)->continued_p \
20138 && INTEGERP ((GLYPH)->object) \
20139 && (GLYPH)->type == CHAR_GLYPH \
20140 && (GLYPH)->u.ch == ' ' \
20141 && (GLYPH)->charpos >= 0 \
20142 && !(GLYPH)->avoid_cursor_p)
20143
20144 CHECK_NUMBER (direction);
20145 dir = XINT (direction);
20146 if (dir > 0)
20147 dir = 1;
20148 else
20149 dir = -1;
20150
20151 /* If current matrix is up-to-date, we can use the information
20152 recorded in the glyphs, at least as long as the goal is on the
20153 screen. */
20154 if (w->window_end_valid
20155 && !windows_or_buffers_changed
20156 && b
20157 && !b->clip_changed
20158 && !b->prevent_redisplay_optimizations_p
20159 && !window_outdated (w)
20160 && w->cursor.vpos >= 0
20161 && w->cursor.vpos < w->current_matrix->nrows
20162 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
20163 {
20164 struct glyph *g = row->glyphs[TEXT_AREA];
20165 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
20166 struct glyph *gpt = g + w->cursor.hpos;
20167
20168 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
20169 {
20170 if (BUFFERP (g->object) && g->charpos != PT)
20171 {
20172 SET_PT (g->charpos);
20173 w->cursor.vpos = -1;
20174 return make_number (PT);
20175 }
20176 else if (!INTEGERP (g->object) && !EQ (g->object, gpt->object))
20177 {
20178 ptrdiff_t new_pos;
20179
20180 if (BUFFERP (gpt->object))
20181 {
20182 new_pos = PT;
20183 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
20184 new_pos += (row->reversed_p ? -dir : dir);
20185 else
20186 new_pos -= (row->reversed_p ? -dir : dir);;
20187 }
20188 else if (BUFFERP (g->object))
20189 new_pos = g->charpos;
20190 else
20191 break;
20192 SET_PT (new_pos);
20193 w->cursor.vpos = -1;
20194 return make_number (PT);
20195 }
20196 else if (ROW_GLYPH_NEWLINE_P (row, g))
20197 {
20198 /* Glyphs inserted at the end of a non-empty line for
20199 positioning the cursor have zero charpos, so we must
20200 deduce the value of point by other means. */
20201 if (g->charpos > 0)
20202 SET_PT (g->charpos);
20203 else if (row->ends_at_zv_p && PT != ZV)
20204 SET_PT (ZV);
20205 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
20206 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20207 else
20208 break;
20209 w->cursor.vpos = -1;
20210 return make_number (PT);
20211 }
20212 }
20213 if (g == e || INTEGERP (g->object))
20214 {
20215 if (row->truncated_on_left_p || row->truncated_on_right_p)
20216 goto simulate_display;
20217 if (!row->reversed_p)
20218 row += dir;
20219 else
20220 row -= dir;
20221 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
20222 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
20223 goto simulate_display;
20224
20225 if (dir > 0)
20226 {
20227 if (row->reversed_p && !row->continued_p)
20228 {
20229 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20230 w->cursor.vpos = -1;
20231 return make_number (PT);
20232 }
20233 g = row->glyphs[TEXT_AREA];
20234 e = g + row->used[TEXT_AREA];
20235 for ( ; g < e; g++)
20236 {
20237 if (BUFFERP (g->object)
20238 /* Empty lines have only one glyph, which stands
20239 for the newline, and whose charpos is the
20240 buffer position of the newline. */
20241 || ROW_GLYPH_NEWLINE_P (row, g)
20242 /* When the buffer ends in a newline, the line at
20243 EOB also has one glyph, but its charpos is -1. */
20244 || (row->ends_at_zv_p
20245 && !row->reversed_p
20246 && INTEGERP (g->object)
20247 && g->type == CHAR_GLYPH
20248 && g->u.ch == ' '))
20249 {
20250 if (g->charpos > 0)
20251 SET_PT (g->charpos);
20252 else if (!row->reversed_p
20253 && row->ends_at_zv_p
20254 && PT != ZV)
20255 SET_PT (ZV);
20256 else
20257 continue;
20258 w->cursor.vpos = -1;
20259 return make_number (PT);
20260 }
20261 }
20262 }
20263 else
20264 {
20265 if (!row->reversed_p && !row->continued_p)
20266 {
20267 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20268 w->cursor.vpos = -1;
20269 return make_number (PT);
20270 }
20271 e = row->glyphs[TEXT_AREA];
20272 g = e + row->used[TEXT_AREA] - 1;
20273 for ( ; g >= e; g--)
20274 {
20275 if (BUFFERP (g->object)
20276 || (ROW_GLYPH_NEWLINE_P (row, g)
20277 && g->charpos > 0)
20278 /* Empty R2L lines on GUI frames have the buffer
20279 position of the newline stored in the stretch
20280 glyph. */
20281 || g->type == STRETCH_GLYPH
20282 || (row->ends_at_zv_p
20283 && row->reversed_p
20284 && INTEGERP (g->object)
20285 && g->type == CHAR_GLYPH
20286 && g->u.ch == ' '))
20287 {
20288 if (g->charpos > 0)
20289 SET_PT (g->charpos);
20290 else if (row->reversed_p
20291 && row->ends_at_zv_p
20292 && PT != ZV)
20293 SET_PT (ZV);
20294 else
20295 continue;
20296 w->cursor.vpos = -1;
20297 return make_number (PT);
20298 }
20299 }
20300 }
20301 }
20302 }
20303
20304 simulate_display:
20305
20306 /* If we wind up here, we failed to move by using the glyphs, so we
20307 need to simulate display instead. */
20308
20309 if (b)
20310 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
20311 else
20312 paragraph_dir = Qleft_to_right;
20313 if (EQ (paragraph_dir, Qright_to_left))
20314 dir = -dir;
20315 if (PT <= BEGV && dir < 0)
20316 xsignal0 (Qbeginning_of_buffer);
20317 else if (PT >= ZV && dir > 0)
20318 xsignal0 (Qend_of_buffer);
20319 else
20320 {
20321 struct text_pos pt;
20322 struct it it;
20323 int pt_x, target_x, pixel_width, pt_vpos;
20324 bool at_eol_p;
20325 bool overshoot_expected = false;
20326 bool target_is_eol_p = false;
20327
20328 /* Setup the arena. */
20329 SET_TEXT_POS (pt, PT, PT_BYTE);
20330 start_display (&it, w, pt);
20331
20332 if (it.cmp_it.id < 0
20333 && it.method == GET_FROM_STRING
20334 && it.area == TEXT_AREA
20335 && it.string_from_display_prop_p
20336 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
20337 overshoot_expected = true;
20338
20339 /* Find the X coordinate of point. We start from the beginning
20340 of this or previous line to make sure we are before point in
20341 the logical order (since the move_it_* functions can only
20342 move forward). */
20343 reseat_at_previous_visible_line_start (&it);
20344 it.current_x = it.hpos = it.current_y = it.vpos = 0;
20345 if (IT_CHARPOS (it) != PT)
20346 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
20347 -1, -1, -1, MOVE_TO_POS);
20348 pt_x = it.current_x;
20349 pt_vpos = it.vpos;
20350 if (dir > 0 || overshoot_expected)
20351 {
20352 struct glyph_row *row = it.glyph_row;
20353
20354 /* When point is at beginning of line, we don't have
20355 information about the glyph there loaded into struct
20356 it. Calling get_next_display_element fixes that. */
20357 if (pt_x == 0)
20358 get_next_display_element (&it);
20359 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20360 it.glyph_row = NULL;
20361 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
20362 it.glyph_row = row;
20363 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
20364 it, lest it will become out of sync with it's buffer
20365 position. */
20366 it.current_x = pt_x;
20367 }
20368 else
20369 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20370 pixel_width = it.pixel_width;
20371 if (overshoot_expected && at_eol_p)
20372 pixel_width = 0;
20373 else if (pixel_width <= 0)
20374 pixel_width = 1;
20375
20376 /* If there's a display string at point, we are actually at the
20377 glyph to the left of point, so we need to correct the X
20378 coordinate. */
20379 if (overshoot_expected)
20380 pt_x += pixel_width;
20381
20382 /* Compute target X coordinate, either to the left or to the
20383 right of point. On TTY frames, all characters have the same
20384 pixel width of 1, so we can use that. On GUI frames we don't
20385 have an easy way of getting at the pixel width of the
20386 character to the left of point, so we use a different method
20387 of getting to that place. */
20388 if (dir > 0)
20389 target_x = pt_x + pixel_width;
20390 else
20391 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
20392
20393 /* Target X coordinate could be one line above or below the line
20394 of point, in which case we need to adjust the target X
20395 coordinate. Also, if moving to the left, we need to begin at
20396 the left edge of the point's screen line. */
20397 if (dir < 0)
20398 {
20399 if (pt_x > 0)
20400 {
20401 start_display (&it, w, pt);
20402 reseat_at_previous_visible_line_start (&it);
20403 it.current_x = it.current_y = it.hpos = 0;
20404 if (pt_vpos != 0)
20405 move_it_by_lines (&it, pt_vpos);
20406 }
20407 else
20408 {
20409 move_it_by_lines (&it, -1);
20410 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
20411 target_is_eol_p = true;
20412 }
20413 }
20414 else
20415 {
20416 if (at_eol_p
20417 || (target_x >= it.last_visible_x
20418 && it.line_wrap != TRUNCATE))
20419 {
20420 if (pt_x > 0)
20421 move_it_by_lines (&it, 0);
20422 move_it_by_lines (&it, 1);
20423 target_x = 0;
20424 }
20425 }
20426
20427 /* Move to the target X coordinate. */
20428 #ifdef HAVE_WINDOW_SYSTEM
20429 /* On GUI frames, as we don't know the X coordinate of the
20430 character to the left of point, moving point to the left
20431 requires walking, one grapheme cluster at a time, until we
20432 find ourself at a place immediately to the left of the
20433 character at point. */
20434 if (FRAME_WINDOW_P (it.f) && dir < 0)
20435 {
20436 struct text_pos new_pos = it.current.pos;
20437 enum move_it_result rc = MOVE_X_REACHED;
20438
20439 while (it.current_x + it.pixel_width <= target_x
20440 && rc == MOVE_X_REACHED)
20441 {
20442 int new_x = it.current_x + it.pixel_width;
20443
20444 new_pos = it.current.pos;
20445 if (new_x == it.current_x)
20446 new_x++;
20447 rc = move_it_in_display_line_to (&it, ZV, new_x,
20448 MOVE_TO_POS | MOVE_TO_X);
20449 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
20450 break;
20451 }
20452 /* If we ended up on a composed character inside
20453 bidi-reordered text (e.g., Hebrew text with diacritics),
20454 the iterator gives us the buffer position of the last (in
20455 logical order) character of the composed grapheme cluster,
20456 which is not what we want. So we cheat: we compute the
20457 character position of the character that follows (in the
20458 logical order) the one where the above loop stopped. That
20459 character will appear on display to the left of point. */
20460 if (it.bidi_p
20461 && it.bidi_it.scan_dir == -1
20462 && new_pos.charpos - IT_CHARPOS (it) > 1)
20463 {
20464 new_pos.charpos = IT_CHARPOS (it) + 1;
20465 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
20466 }
20467 it.current.pos = new_pos;
20468 }
20469 else
20470 #endif
20471 if (it.current_x != target_x)
20472 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
20473
20474 /* When lines are truncated, the above loop will stop at the
20475 window edge. But we want to get to the end of line, even if
20476 it is beyond the window edge; automatic hscroll will then
20477 scroll the window to show point as appropriate. */
20478 if (target_is_eol_p && it.line_wrap == TRUNCATE
20479 && get_next_display_element (&it))
20480 {
20481 struct text_pos new_pos = it.current.pos;
20482
20483 while (!ITERATOR_AT_END_OF_LINE_P (&it))
20484 {
20485 set_iterator_to_next (&it, 0);
20486 if (it.method == GET_FROM_BUFFER)
20487 new_pos = it.current.pos;
20488 if (!get_next_display_element (&it))
20489 break;
20490 }
20491
20492 it.current.pos = new_pos;
20493 }
20494
20495 /* If we ended up in a display string that covers point, move to
20496 buffer position to the right in the visual order. */
20497 if (dir > 0)
20498 {
20499 while (IT_CHARPOS (it) == PT)
20500 {
20501 set_iterator_to_next (&it, 0);
20502 if (!get_next_display_element (&it))
20503 break;
20504 }
20505 }
20506
20507 /* Move point to that position. */
20508 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
20509 }
20510
20511 return make_number (PT);
20512
20513 #undef ROW_GLYPH_NEWLINE_P
20514 }
20515
20516 \f
20517 /***********************************************************************
20518 Menu Bar
20519 ***********************************************************************/
20520
20521 /* Redisplay the menu bar in the frame for window W.
20522
20523 The menu bar of X frames that don't have X toolkit support is
20524 displayed in a special window W->frame->menu_bar_window.
20525
20526 The menu bar of terminal frames is treated specially as far as
20527 glyph matrices are concerned. Menu bar lines are not part of
20528 windows, so the update is done directly on the frame matrix rows
20529 for the menu bar. */
20530
20531 static void
20532 display_menu_bar (struct window *w)
20533 {
20534 struct frame *f = XFRAME (WINDOW_FRAME (w));
20535 struct it it;
20536 Lisp_Object items;
20537 int i;
20538
20539 /* Don't do all this for graphical frames. */
20540 #ifdef HAVE_NTGUI
20541 if (FRAME_W32_P (f))
20542 return;
20543 #endif
20544 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20545 if (FRAME_X_P (f))
20546 return;
20547 #endif
20548
20549 #ifdef HAVE_NS
20550 if (FRAME_NS_P (f))
20551 return;
20552 #endif /* HAVE_NS */
20553
20554 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20555 eassert (!FRAME_WINDOW_P (f));
20556 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20557 it.first_visible_x = 0;
20558 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20559 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
20560 if (FRAME_WINDOW_P (f))
20561 {
20562 /* Menu bar lines are displayed in the desired matrix of the
20563 dummy window menu_bar_window. */
20564 struct window *menu_w;
20565 menu_w = XWINDOW (f->menu_bar_window);
20566 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20567 MENU_FACE_ID);
20568 it.first_visible_x = 0;
20569 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20570 }
20571 else
20572 #endif /* not USE_X_TOOLKIT and not USE_GTK */
20573 {
20574 /* This is a TTY frame, i.e. character hpos/vpos are used as
20575 pixel x/y. */
20576 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20577 MENU_FACE_ID);
20578 it.first_visible_x = 0;
20579 it.last_visible_x = FRAME_COLS (f);
20580 }
20581
20582 /* FIXME: This should be controlled by a user option. See the
20583 comments in redisplay_tool_bar and display_mode_line about
20584 this. */
20585 it.paragraph_embedding = L2R;
20586
20587 /* Clear all rows of the menu bar. */
20588 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20589 {
20590 struct glyph_row *row = it.glyph_row + i;
20591 clear_glyph_row (row);
20592 row->enabled_p = 1;
20593 row->full_width_p = 1;
20594 }
20595
20596 /* Display all items of the menu bar. */
20597 items = FRAME_MENU_BAR_ITEMS (it.f);
20598 for (i = 0; i < ASIZE (items); i += 4)
20599 {
20600 Lisp_Object string;
20601
20602 /* Stop at nil string. */
20603 string = AREF (items, i + 1);
20604 if (NILP (string))
20605 break;
20606
20607 /* Remember where item was displayed. */
20608 ASET (items, i + 3, make_number (it.hpos));
20609
20610 /* Display the item, pad with one space. */
20611 if (it.current_x < it.last_visible_x)
20612 display_string (NULL, string, Qnil, 0, 0, &it,
20613 SCHARS (string) + 1, 0, 0, -1);
20614 }
20615
20616 /* Fill out the line with spaces. */
20617 if (it.current_x < it.last_visible_x)
20618 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20619
20620 /* Compute the total height of the lines. */
20621 compute_line_metrics (&it);
20622 }
20623
20624
20625 \f
20626 /***********************************************************************
20627 Mode Line
20628 ***********************************************************************/
20629
20630 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20631 FORCE is non-zero, redisplay mode lines unconditionally.
20632 Otherwise, redisplay only mode lines that are garbaged. Value is
20633 the number of windows whose mode lines were redisplayed. */
20634
20635 static int
20636 redisplay_mode_lines (Lisp_Object window, int force)
20637 {
20638 int nwindows = 0;
20639
20640 while (!NILP (window))
20641 {
20642 struct window *w = XWINDOW (window);
20643
20644 if (WINDOWP (w->contents))
20645 nwindows += redisplay_mode_lines (w->contents, force);
20646 else if (force
20647 || FRAME_GARBAGED_P (XFRAME (w->frame))
20648 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20649 {
20650 struct text_pos lpoint;
20651 struct buffer *old = current_buffer;
20652
20653 /* Set the window's buffer for the mode line display. */
20654 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20655 set_buffer_internal_1 (XBUFFER (w->contents));
20656
20657 /* Point refers normally to the selected window. For any
20658 other window, set up appropriate value. */
20659 if (!EQ (window, selected_window))
20660 {
20661 struct text_pos pt;
20662
20663 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
20664 if (CHARPOS (pt) < BEGV)
20665 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
20666 else if (CHARPOS (pt) > (ZV - 1))
20667 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
20668 else
20669 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20670 }
20671
20672 /* Display mode lines. */
20673 clear_glyph_matrix (w->desired_matrix);
20674 if (display_mode_lines (w))
20675 {
20676 ++nwindows;
20677 w->must_be_updated_p = 1;
20678 }
20679
20680 /* Restore old settings. */
20681 set_buffer_internal_1 (old);
20682 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20683 }
20684
20685 window = w->next;
20686 }
20687
20688 return nwindows;
20689 }
20690
20691
20692 /* Display the mode and/or header line of window W. Value is the
20693 sum number of mode lines and header lines displayed. */
20694
20695 static int
20696 display_mode_lines (struct window *w)
20697 {
20698 Lisp_Object old_selected_window = selected_window;
20699 Lisp_Object old_selected_frame = selected_frame;
20700 Lisp_Object new_frame = w->frame;
20701 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
20702 int n = 0;
20703
20704 selected_frame = new_frame;
20705 /* FIXME: If we were to allow the mode-line's computation changing the buffer
20706 or window's point, then we'd need select_window_1 here as well. */
20707 XSETWINDOW (selected_window, w);
20708 XFRAME (new_frame)->selected_window = selected_window;
20709
20710 /* These will be set while the mode line specs are processed. */
20711 line_number_displayed = 0;
20712 w->column_number_displayed = -1;
20713
20714 if (WINDOW_WANTS_MODELINE_P (w))
20715 {
20716 struct window *sel_w = XWINDOW (old_selected_window);
20717
20718 /* Select mode line face based on the real selected window. */
20719 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20720 BVAR (current_buffer, mode_line_format));
20721 ++n;
20722 }
20723
20724 if (WINDOW_WANTS_HEADER_LINE_P (w))
20725 {
20726 display_mode_line (w, HEADER_LINE_FACE_ID,
20727 BVAR (current_buffer, header_line_format));
20728 ++n;
20729 }
20730
20731 XFRAME (new_frame)->selected_window = old_frame_selected_window;
20732 selected_frame = old_selected_frame;
20733 selected_window = old_selected_window;
20734 return n;
20735 }
20736
20737
20738 /* Display mode or header line of window W. FACE_ID specifies which
20739 line to display; it is either MODE_LINE_FACE_ID or
20740 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20741 display. Value is the pixel height of the mode/header line
20742 displayed. */
20743
20744 static int
20745 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20746 {
20747 struct it it;
20748 struct face *face;
20749 ptrdiff_t count = SPECPDL_INDEX ();
20750
20751 init_iterator (&it, w, -1, -1, NULL, face_id);
20752 /* Don't extend on a previously drawn mode-line.
20753 This may happen if called from pos_visible_p. */
20754 it.glyph_row->enabled_p = 0;
20755 prepare_desired_row (it.glyph_row);
20756
20757 it.glyph_row->mode_line_p = 1;
20758
20759 /* FIXME: This should be controlled by a user option. But
20760 supporting such an option is not trivial, since the mode line is
20761 made up of many separate strings. */
20762 it.paragraph_embedding = L2R;
20763
20764 record_unwind_protect (unwind_format_mode_line,
20765 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20766
20767 mode_line_target = MODE_LINE_DISPLAY;
20768
20769 /* Temporarily make frame's keyboard the current kboard so that
20770 kboard-local variables in the mode_line_format will get the right
20771 values. */
20772 push_kboard (FRAME_KBOARD (it.f));
20773 record_unwind_save_match_data ();
20774 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20775 pop_kboard ();
20776
20777 unbind_to (count, Qnil);
20778
20779 /* Fill up with spaces. */
20780 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20781
20782 compute_line_metrics (&it);
20783 it.glyph_row->full_width_p = 1;
20784 it.glyph_row->continued_p = 0;
20785 it.glyph_row->truncated_on_left_p = 0;
20786 it.glyph_row->truncated_on_right_p = 0;
20787
20788 /* Make a 3D mode-line have a shadow at its right end. */
20789 face = FACE_FROM_ID (it.f, face_id);
20790 extend_face_to_end_of_line (&it);
20791 if (face->box != FACE_NO_BOX)
20792 {
20793 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20794 + it.glyph_row->used[TEXT_AREA] - 1);
20795 last->right_box_line_p = 1;
20796 }
20797
20798 return it.glyph_row->height;
20799 }
20800
20801 /* Move element ELT in LIST to the front of LIST.
20802 Return the updated list. */
20803
20804 static Lisp_Object
20805 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20806 {
20807 register Lisp_Object tail, prev;
20808 register Lisp_Object tem;
20809
20810 tail = list;
20811 prev = Qnil;
20812 while (CONSP (tail))
20813 {
20814 tem = XCAR (tail);
20815
20816 if (EQ (elt, tem))
20817 {
20818 /* Splice out the link TAIL. */
20819 if (NILP (prev))
20820 list = XCDR (tail);
20821 else
20822 Fsetcdr (prev, XCDR (tail));
20823
20824 /* Now make it the first. */
20825 Fsetcdr (tail, list);
20826 return tail;
20827 }
20828 else
20829 prev = tail;
20830 tail = XCDR (tail);
20831 QUIT;
20832 }
20833
20834 /* Not found--return unchanged LIST. */
20835 return list;
20836 }
20837
20838 /* Contribute ELT to the mode line for window IT->w. How it
20839 translates into text depends on its data type.
20840
20841 IT describes the display environment in which we display, as usual.
20842
20843 DEPTH is the depth in recursion. It is used to prevent
20844 infinite recursion here.
20845
20846 FIELD_WIDTH is the number of characters the display of ELT should
20847 occupy in the mode line, and PRECISION is the maximum number of
20848 characters to display from ELT's representation. See
20849 display_string for details.
20850
20851 Returns the hpos of the end of the text generated by ELT.
20852
20853 PROPS is a property list to add to any string we encounter.
20854
20855 If RISKY is nonzero, remove (disregard) any properties in any string
20856 we encounter, and ignore :eval and :propertize.
20857
20858 The global variable `mode_line_target' determines whether the
20859 output is passed to `store_mode_line_noprop',
20860 `store_mode_line_string', or `display_string'. */
20861
20862 static int
20863 display_mode_element (struct it *it, int depth, int field_width, int precision,
20864 Lisp_Object elt, Lisp_Object props, int risky)
20865 {
20866 int n = 0, field, prec;
20867 int literal = 0;
20868
20869 tail_recurse:
20870 if (depth > 100)
20871 elt = build_string ("*too-deep*");
20872
20873 depth++;
20874
20875 switch (XTYPE (elt))
20876 {
20877 case Lisp_String:
20878 {
20879 /* A string: output it and check for %-constructs within it. */
20880 unsigned char c;
20881 ptrdiff_t offset = 0;
20882
20883 if (SCHARS (elt) > 0
20884 && (!NILP (props) || risky))
20885 {
20886 Lisp_Object oprops, aelt;
20887 oprops = Ftext_properties_at (make_number (0), elt);
20888
20889 /* If the starting string's properties are not what
20890 we want, translate the string. Also, if the string
20891 is risky, do that anyway. */
20892
20893 if (NILP (Fequal (props, oprops)) || risky)
20894 {
20895 /* If the starting string has properties,
20896 merge the specified ones onto the existing ones. */
20897 if (! NILP (oprops) && !risky)
20898 {
20899 Lisp_Object tem;
20900
20901 oprops = Fcopy_sequence (oprops);
20902 tem = props;
20903 while (CONSP (tem))
20904 {
20905 oprops = Fplist_put (oprops, XCAR (tem),
20906 XCAR (XCDR (tem)));
20907 tem = XCDR (XCDR (tem));
20908 }
20909 props = oprops;
20910 }
20911
20912 aelt = Fassoc (elt, mode_line_proptrans_alist);
20913 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20914 {
20915 /* AELT is what we want. Move it to the front
20916 without consing. */
20917 elt = XCAR (aelt);
20918 mode_line_proptrans_alist
20919 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20920 }
20921 else
20922 {
20923 Lisp_Object tem;
20924
20925 /* If AELT has the wrong props, it is useless.
20926 so get rid of it. */
20927 if (! NILP (aelt))
20928 mode_line_proptrans_alist
20929 = Fdelq (aelt, mode_line_proptrans_alist);
20930
20931 elt = Fcopy_sequence (elt);
20932 Fset_text_properties (make_number (0), Flength (elt),
20933 props, elt);
20934 /* Add this item to mode_line_proptrans_alist. */
20935 mode_line_proptrans_alist
20936 = Fcons (Fcons (elt, props),
20937 mode_line_proptrans_alist);
20938 /* Truncate mode_line_proptrans_alist
20939 to at most 50 elements. */
20940 tem = Fnthcdr (make_number (50),
20941 mode_line_proptrans_alist);
20942 if (! NILP (tem))
20943 XSETCDR (tem, Qnil);
20944 }
20945 }
20946 }
20947
20948 offset = 0;
20949
20950 if (literal)
20951 {
20952 prec = precision - n;
20953 switch (mode_line_target)
20954 {
20955 case MODE_LINE_NOPROP:
20956 case MODE_LINE_TITLE:
20957 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20958 break;
20959 case MODE_LINE_STRING:
20960 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20961 break;
20962 case MODE_LINE_DISPLAY:
20963 n += display_string (NULL, elt, Qnil, 0, 0, it,
20964 0, prec, 0, STRING_MULTIBYTE (elt));
20965 break;
20966 }
20967
20968 break;
20969 }
20970
20971 /* Handle the non-literal case. */
20972
20973 while ((precision <= 0 || n < precision)
20974 && SREF (elt, offset) != 0
20975 && (mode_line_target != MODE_LINE_DISPLAY
20976 || it->current_x < it->last_visible_x))
20977 {
20978 ptrdiff_t last_offset = offset;
20979
20980 /* Advance to end of string or next format specifier. */
20981 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20982 ;
20983
20984 if (offset - 1 != last_offset)
20985 {
20986 ptrdiff_t nchars, nbytes;
20987
20988 /* Output to end of string or up to '%'. Field width
20989 is length of string. Don't output more than
20990 PRECISION allows us. */
20991 offset--;
20992
20993 prec = c_string_width (SDATA (elt) + last_offset,
20994 offset - last_offset, precision - n,
20995 &nchars, &nbytes);
20996
20997 switch (mode_line_target)
20998 {
20999 case MODE_LINE_NOPROP:
21000 case MODE_LINE_TITLE:
21001 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
21002 break;
21003 case MODE_LINE_STRING:
21004 {
21005 ptrdiff_t bytepos = last_offset;
21006 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21007 ptrdiff_t endpos = (precision <= 0
21008 ? string_byte_to_char (elt, offset)
21009 : charpos + nchars);
21010
21011 n += store_mode_line_string (NULL,
21012 Fsubstring (elt, make_number (charpos),
21013 make_number (endpos)),
21014 0, 0, 0, Qnil);
21015 }
21016 break;
21017 case MODE_LINE_DISPLAY:
21018 {
21019 ptrdiff_t bytepos = last_offset;
21020 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21021
21022 if (precision <= 0)
21023 nchars = string_byte_to_char (elt, offset) - charpos;
21024 n += display_string (NULL, elt, Qnil, 0, charpos,
21025 it, 0, nchars, 0,
21026 STRING_MULTIBYTE (elt));
21027 }
21028 break;
21029 }
21030 }
21031 else /* c == '%' */
21032 {
21033 ptrdiff_t percent_position = offset;
21034
21035 /* Get the specified minimum width. Zero means
21036 don't pad. */
21037 field = 0;
21038 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
21039 field = field * 10 + c - '0';
21040
21041 /* Don't pad beyond the total padding allowed. */
21042 if (field_width - n > 0 && field > field_width - n)
21043 field = field_width - n;
21044
21045 /* Note that either PRECISION <= 0 or N < PRECISION. */
21046 prec = precision - n;
21047
21048 if (c == 'M')
21049 n += display_mode_element (it, depth, field, prec,
21050 Vglobal_mode_string, props,
21051 risky);
21052 else if (c != 0)
21053 {
21054 bool multibyte;
21055 ptrdiff_t bytepos, charpos;
21056 const char *spec;
21057 Lisp_Object string;
21058
21059 bytepos = percent_position;
21060 charpos = (STRING_MULTIBYTE (elt)
21061 ? string_byte_to_char (elt, bytepos)
21062 : bytepos);
21063 spec = decode_mode_spec (it->w, c, field, &string);
21064 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
21065
21066 switch (mode_line_target)
21067 {
21068 case MODE_LINE_NOPROP:
21069 case MODE_LINE_TITLE:
21070 n += store_mode_line_noprop (spec, field, prec);
21071 break;
21072 case MODE_LINE_STRING:
21073 {
21074 Lisp_Object tem = build_string (spec);
21075 props = Ftext_properties_at (make_number (charpos), elt);
21076 /* Should only keep face property in props */
21077 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
21078 }
21079 break;
21080 case MODE_LINE_DISPLAY:
21081 {
21082 int nglyphs_before, nwritten;
21083
21084 nglyphs_before = it->glyph_row->used[TEXT_AREA];
21085 nwritten = display_string (spec, string, elt,
21086 charpos, 0, it,
21087 field, prec, 0,
21088 multibyte);
21089
21090 /* Assign to the glyphs written above the
21091 string where the `%x' came from, position
21092 of the `%'. */
21093 if (nwritten > 0)
21094 {
21095 struct glyph *glyph
21096 = (it->glyph_row->glyphs[TEXT_AREA]
21097 + nglyphs_before);
21098 int i;
21099
21100 for (i = 0; i < nwritten; ++i)
21101 {
21102 glyph[i].object = elt;
21103 glyph[i].charpos = charpos;
21104 }
21105
21106 n += nwritten;
21107 }
21108 }
21109 break;
21110 }
21111 }
21112 else /* c == 0 */
21113 break;
21114 }
21115 }
21116 }
21117 break;
21118
21119 case Lisp_Symbol:
21120 /* A symbol: process the value of the symbol recursively
21121 as if it appeared here directly. Avoid error if symbol void.
21122 Special case: if value of symbol is a string, output the string
21123 literally. */
21124 {
21125 register Lisp_Object tem;
21126
21127 /* If the variable is not marked as risky to set
21128 then its contents are risky to use. */
21129 if (NILP (Fget (elt, Qrisky_local_variable)))
21130 risky = 1;
21131
21132 tem = Fboundp (elt);
21133 if (!NILP (tem))
21134 {
21135 tem = Fsymbol_value (elt);
21136 /* If value is a string, output that string literally:
21137 don't check for % within it. */
21138 if (STRINGP (tem))
21139 literal = 1;
21140
21141 if (!EQ (tem, elt))
21142 {
21143 /* Give up right away for nil or t. */
21144 elt = tem;
21145 goto tail_recurse;
21146 }
21147 }
21148 }
21149 break;
21150
21151 case Lisp_Cons:
21152 {
21153 register Lisp_Object car, tem;
21154
21155 /* A cons cell: five distinct cases.
21156 If first element is :eval or :propertize, do something special.
21157 If first element is a string or a cons, process all the elements
21158 and effectively concatenate them.
21159 If first element is a negative number, truncate displaying cdr to
21160 at most that many characters. If positive, pad (with spaces)
21161 to at least that many characters.
21162 If first element is a symbol, process the cadr or caddr recursively
21163 according to whether the symbol's value is non-nil or nil. */
21164 car = XCAR (elt);
21165 if (EQ (car, QCeval))
21166 {
21167 /* An element of the form (:eval FORM) means evaluate FORM
21168 and use the result as mode line elements. */
21169
21170 if (risky)
21171 break;
21172
21173 if (CONSP (XCDR (elt)))
21174 {
21175 Lisp_Object spec;
21176 spec = safe_eval (XCAR (XCDR (elt)));
21177 n += display_mode_element (it, depth, field_width - n,
21178 precision - n, spec, props,
21179 risky);
21180 }
21181 }
21182 else if (EQ (car, QCpropertize))
21183 {
21184 /* An element of the form (:propertize ELT PROPS...)
21185 means display ELT but applying properties PROPS. */
21186
21187 if (risky)
21188 break;
21189
21190 if (CONSP (XCDR (elt)))
21191 n += display_mode_element (it, depth, field_width - n,
21192 precision - n, XCAR (XCDR (elt)),
21193 XCDR (XCDR (elt)), risky);
21194 }
21195 else if (SYMBOLP (car))
21196 {
21197 tem = Fboundp (car);
21198 elt = XCDR (elt);
21199 if (!CONSP (elt))
21200 goto invalid;
21201 /* elt is now the cdr, and we know it is a cons cell.
21202 Use its car if CAR has a non-nil value. */
21203 if (!NILP (tem))
21204 {
21205 tem = Fsymbol_value (car);
21206 if (!NILP (tem))
21207 {
21208 elt = XCAR (elt);
21209 goto tail_recurse;
21210 }
21211 }
21212 /* Symbol's value is nil (or symbol is unbound)
21213 Get the cddr of the original list
21214 and if possible find the caddr and use that. */
21215 elt = XCDR (elt);
21216 if (NILP (elt))
21217 break;
21218 else if (!CONSP (elt))
21219 goto invalid;
21220 elt = XCAR (elt);
21221 goto tail_recurse;
21222 }
21223 else if (INTEGERP (car))
21224 {
21225 register int lim = XINT (car);
21226 elt = XCDR (elt);
21227 if (lim < 0)
21228 {
21229 /* Negative int means reduce maximum width. */
21230 if (precision <= 0)
21231 precision = -lim;
21232 else
21233 precision = min (precision, -lim);
21234 }
21235 else if (lim > 0)
21236 {
21237 /* Padding specified. Don't let it be more than
21238 current maximum. */
21239 if (precision > 0)
21240 lim = min (precision, lim);
21241
21242 /* If that's more padding than already wanted, queue it.
21243 But don't reduce padding already specified even if
21244 that is beyond the current truncation point. */
21245 field_width = max (lim, field_width);
21246 }
21247 goto tail_recurse;
21248 }
21249 else if (STRINGP (car) || CONSP (car))
21250 {
21251 Lisp_Object halftail = elt;
21252 int len = 0;
21253
21254 while (CONSP (elt)
21255 && (precision <= 0 || n < precision))
21256 {
21257 n += display_mode_element (it, depth,
21258 /* Do padding only after the last
21259 element in the list. */
21260 (! CONSP (XCDR (elt))
21261 ? field_width - n
21262 : 0),
21263 precision - n, XCAR (elt),
21264 props, risky);
21265 elt = XCDR (elt);
21266 len++;
21267 if ((len & 1) == 0)
21268 halftail = XCDR (halftail);
21269 /* Check for cycle. */
21270 if (EQ (halftail, elt))
21271 break;
21272 }
21273 }
21274 }
21275 break;
21276
21277 default:
21278 invalid:
21279 elt = build_string ("*invalid*");
21280 goto tail_recurse;
21281 }
21282
21283 /* Pad to FIELD_WIDTH. */
21284 if (field_width > 0 && n < field_width)
21285 {
21286 switch (mode_line_target)
21287 {
21288 case MODE_LINE_NOPROP:
21289 case MODE_LINE_TITLE:
21290 n += store_mode_line_noprop ("", field_width - n, 0);
21291 break;
21292 case MODE_LINE_STRING:
21293 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
21294 break;
21295 case MODE_LINE_DISPLAY:
21296 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
21297 0, 0, 0);
21298 break;
21299 }
21300 }
21301
21302 return n;
21303 }
21304
21305 /* Store a mode-line string element in mode_line_string_list.
21306
21307 If STRING is non-null, display that C string. Otherwise, the Lisp
21308 string LISP_STRING is displayed.
21309
21310 FIELD_WIDTH is the minimum number of output glyphs to produce.
21311 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21312 with spaces. FIELD_WIDTH <= 0 means don't pad.
21313
21314 PRECISION is the maximum number of characters to output from
21315 STRING. PRECISION <= 0 means don't truncate the string.
21316
21317 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
21318 properties to the string.
21319
21320 PROPS are the properties to add to the string.
21321 The mode_line_string_face face property is always added to the string.
21322 */
21323
21324 static int
21325 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
21326 int field_width, int precision, Lisp_Object props)
21327 {
21328 ptrdiff_t len;
21329 int n = 0;
21330
21331 if (string != NULL)
21332 {
21333 len = strlen (string);
21334 if (precision > 0 && len > precision)
21335 len = precision;
21336 lisp_string = make_string (string, len);
21337 if (NILP (props))
21338 props = mode_line_string_face_prop;
21339 else if (!NILP (mode_line_string_face))
21340 {
21341 Lisp_Object face = Fplist_get (props, Qface);
21342 props = Fcopy_sequence (props);
21343 if (NILP (face))
21344 face = mode_line_string_face;
21345 else
21346 face = list2 (face, mode_line_string_face);
21347 props = Fplist_put (props, Qface, face);
21348 }
21349 Fadd_text_properties (make_number (0), make_number (len),
21350 props, lisp_string);
21351 }
21352 else
21353 {
21354 len = XFASTINT (Flength (lisp_string));
21355 if (precision > 0 && len > precision)
21356 {
21357 len = precision;
21358 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
21359 precision = -1;
21360 }
21361 if (!NILP (mode_line_string_face))
21362 {
21363 Lisp_Object face;
21364 if (NILP (props))
21365 props = Ftext_properties_at (make_number (0), lisp_string);
21366 face = Fplist_get (props, Qface);
21367 if (NILP (face))
21368 face = mode_line_string_face;
21369 else
21370 face = list2 (face, mode_line_string_face);
21371 props = list2 (Qface, face);
21372 if (copy_string)
21373 lisp_string = Fcopy_sequence (lisp_string);
21374 }
21375 if (!NILP (props))
21376 Fadd_text_properties (make_number (0), make_number (len),
21377 props, lisp_string);
21378 }
21379
21380 if (len > 0)
21381 {
21382 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21383 n += len;
21384 }
21385
21386 if (field_width > len)
21387 {
21388 field_width -= len;
21389 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
21390 if (!NILP (props))
21391 Fadd_text_properties (make_number (0), make_number (field_width),
21392 props, lisp_string);
21393 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21394 n += field_width;
21395 }
21396
21397 return n;
21398 }
21399
21400
21401 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
21402 1, 4, 0,
21403 doc: /* Format a string out of a mode line format specification.
21404 First arg FORMAT specifies the mode line format (see `mode-line-format'
21405 for details) to use.
21406
21407 By default, the format is evaluated for the currently selected window.
21408
21409 Optional second arg FACE specifies the face property to put on all
21410 characters for which no face is specified. The value nil means the
21411 default face. The value t means whatever face the window's mode line
21412 currently uses (either `mode-line' or `mode-line-inactive',
21413 depending on whether the window is the selected window or not).
21414 An integer value means the value string has no text
21415 properties.
21416
21417 Optional third and fourth args WINDOW and BUFFER specify the window
21418 and buffer to use as the context for the formatting (defaults
21419 are the selected window and the WINDOW's buffer). */)
21420 (Lisp_Object format, Lisp_Object face,
21421 Lisp_Object window, Lisp_Object buffer)
21422 {
21423 struct it it;
21424 int len;
21425 struct window *w;
21426 struct buffer *old_buffer = NULL;
21427 int face_id;
21428 int no_props = INTEGERP (face);
21429 ptrdiff_t count = SPECPDL_INDEX ();
21430 Lisp_Object str;
21431 int string_start = 0;
21432
21433 w = decode_any_window (window);
21434 XSETWINDOW (window, w);
21435
21436 if (NILP (buffer))
21437 buffer = w->contents;
21438 CHECK_BUFFER (buffer);
21439
21440 /* Make formatting the modeline a non-op when noninteractive, otherwise
21441 there will be problems later caused by a partially initialized frame. */
21442 if (NILP (format) || noninteractive)
21443 return empty_unibyte_string;
21444
21445 if (no_props)
21446 face = Qnil;
21447
21448 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
21449 : EQ (face, Qt) ? (EQ (window, selected_window)
21450 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21451 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21452 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21453 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21454 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21455 : DEFAULT_FACE_ID;
21456
21457 old_buffer = current_buffer;
21458
21459 /* Save things including mode_line_proptrans_alist,
21460 and set that to nil so that we don't alter the outer value. */
21461 record_unwind_protect (unwind_format_mode_line,
21462 format_mode_line_unwind_data
21463 (XFRAME (WINDOW_FRAME (w)),
21464 old_buffer, selected_window, 1));
21465 mode_line_proptrans_alist = Qnil;
21466
21467 Fselect_window (window, Qt);
21468 set_buffer_internal_1 (XBUFFER (buffer));
21469
21470 init_iterator (&it, w, -1, -1, NULL, face_id);
21471
21472 if (no_props)
21473 {
21474 mode_line_target = MODE_LINE_NOPROP;
21475 mode_line_string_face_prop = Qnil;
21476 mode_line_string_list = Qnil;
21477 string_start = MODE_LINE_NOPROP_LEN (0);
21478 }
21479 else
21480 {
21481 mode_line_target = MODE_LINE_STRING;
21482 mode_line_string_list = Qnil;
21483 mode_line_string_face = face;
21484 mode_line_string_face_prop
21485 = NILP (face) ? Qnil : list2 (Qface, face);
21486 }
21487
21488 push_kboard (FRAME_KBOARD (it.f));
21489 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21490 pop_kboard ();
21491
21492 if (no_props)
21493 {
21494 len = MODE_LINE_NOPROP_LEN (string_start);
21495 str = make_string (mode_line_noprop_buf + string_start, len);
21496 }
21497 else
21498 {
21499 mode_line_string_list = Fnreverse (mode_line_string_list);
21500 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21501 empty_unibyte_string);
21502 }
21503
21504 unbind_to (count, Qnil);
21505 return str;
21506 }
21507
21508 /* Write a null-terminated, right justified decimal representation of
21509 the positive integer D to BUF using a minimal field width WIDTH. */
21510
21511 static void
21512 pint2str (register char *buf, register int width, register ptrdiff_t d)
21513 {
21514 register char *p = buf;
21515
21516 if (d <= 0)
21517 *p++ = '0';
21518 else
21519 {
21520 while (d > 0)
21521 {
21522 *p++ = d % 10 + '0';
21523 d /= 10;
21524 }
21525 }
21526
21527 for (width -= (int) (p - buf); width > 0; --width)
21528 *p++ = ' ';
21529 *p-- = '\0';
21530 while (p > buf)
21531 {
21532 d = *buf;
21533 *buf++ = *p;
21534 *p-- = d;
21535 }
21536 }
21537
21538 /* Write a null-terminated, right justified decimal and "human
21539 readable" representation of the nonnegative integer D to BUF using
21540 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21541
21542 static const char power_letter[] =
21543 {
21544 0, /* no letter */
21545 'k', /* kilo */
21546 'M', /* mega */
21547 'G', /* giga */
21548 'T', /* tera */
21549 'P', /* peta */
21550 'E', /* exa */
21551 'Z', /* zetta */
21552 'Y' /* yotta */
21553 };
21554
21555 static void
21556 pint2hrstr (char *buf, int width, ptrdiff_t d)
21557 {
21558 /* We aim to represent the nonnegative integer D as
21559 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21560 ptrdiff_t quotient = d;
21561 int remainder = 0;
21562 /* -1 means: do not use TENTHS. */
21563 int tenths = -1;
21564 int exponent = 0;
21565
21566 /* Length of QUOTIENT.TENTHS as a string. */
21567 int length;
21568
21569 char * psuffix;
21570 char * p;
21571
21572 if (quotient >= 1000)
21573 {
21574 /* Scale to the appropriate EXPONENT. */
21575 do
21576 {
21577 remainder = quotient % 1000;
21578 quotient /= 1000;
21579 exponent++;
21580 }
21581 while (quotient >= 1000);
21582
21583 /* Round to nearest and decide whether to use TENTHS or not. */
21584 if (quotient <= 9)
21585 {
21586 tenths = remainder / 100;
21587 if (remainder % 100 >= 50)
21588 {
21589 if (tenths < 9)
21590 tenths++;
21591 else
21592 {
21593 quotient++;
21594 if (quotient == 10)
21595 tenths = -1;
21596 else
21597 tenths = 0;
21598 }
21599 }
21600 }
21601 else
21602 if (remainder >= 500)
21603 {
21604 if (quotient < 999)
21605 quotient++;
21606 else
21607 {
21608 quotient = 1;
21609 exponent++;
21610 tenths = 0;
21611 }
21612 }
21613 }
21614
21615 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21616 if (tenths == -1 && quotient <= 99)
21617 if (quotient <= 9)
21618 length = 1;
21619 else
21620 length = 2;
21621 else
21622 length = 3;
21623 p = psuffix = buf + max (width, length);
21624
21625 /* Print EXPONENT. */
21626 *psuffix++ = power_letter[exponent];
21627 *psuffix = '\0';
21628
21629 /* Print TENTHS. */
21630 if (tenths >= 0)
21631 {
21632 *--p = '0' + tenths;
21633 *--p = '.';
21634 }
21635
21636 /* Print QUOTIENT. */
21637 do
21638 {
21639 int digit = quotient % 10;
21640 *--p = '0' + digit;
21641 }
21642 while ((quotient /= 10) != 0);
21643
21644 /* Print leading spaces. */
21645 while (buf < p)
21646 *--p = ' ';
21647 }
21648
21649 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21650 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21651 type of CODING_SYSTEM. Return updated pointer into BUF. */
21652
21653 static unsigned char invalid_eol_type[] = "(*invalid*)";
21654
21655 static char *
21656 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21657 {
21658 Lisp_Object val;
21659 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21660 const unsigned char *eol_str;
21661 int eol_str_len;
21662 /* The EOL conversion we are using. */
21663 Lisp_Object eoltype;
21664
21665 val = CODING_SYSTEM_SPEC (coding_system);
21666 eoltype = Qnil;
21667
21668 if (!VECTORP (val)) /* Not yet decided. */
21669 {
21670 *buf++ = multibyte ? '-' : ' ';
21671 if (eol_flag)
21672 eoltype = eol_mnemonic_undecided;
21673 /* Don't mention EOL conversion if it isn't decided. */
21674 }
21675 else
21676 {
21677 Lisp_Object attrs;
21678 Lisp_Object eolvalue;
21679
21680 attrs = AREF (val, 0);
21681 eolvalue = AREF (val, 2);
21682
21683 *buf++ = multibyte
21684 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21685 : ' ';
21686
21687 if (eol_flag)
21688 {
21689 /* The EOL conversion that is normal on this system. */
21690
21691 if (NILP (eolvalue)) /* Not yet decided. */
21692 eoltype = eol_mnemonic_undecided;
21693 else if (VECTORP (eolvalue)) /* Not yet decided. */
21694 eoltype = eol_mnemonic_undecided;
21695 else /* eolvalue is Qunix, Qdos, or Qmac. */
21696 eoltype = (EQ (eolvalue, Qunix)
21697 ? eol_mnemonic_unix
21698 : (EQ (eolvalue, Qdos) == 1
21699 ? eol_mnemonic_dos : eol_mnemonic_mac));
21700 }
21701 }
21702
21703 if (eol_flag)
21704 {
21705 /* Mention the EOL conversion if it is not the usual one. */
21706 if (STRINGP (eoltype))
21707 {
21708 eol_str = SDATA (eoltype);
21709 eol_str_len = SBYTES (eoltype);
21710 }
21711 else if (CHARACTERP (eoltype))
21712 {
21713 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21714 int c = XFASTINT (eoltype);
21715 eol_str_len = CHAR_STRING (c, tmp);
21716 eol_str = tmp;
21717 }
21718 else
21719 {
21720 eol_str = invalid_eol_type;
21721 eol_str_len = sizeof (invalid_eol_type) - 1;
21722 }
21723 memcpy (buf, eol_str, eol_str_len);
21724 buf += eol_str_len;
21725 }
21726
21727 return buf;
21728 }
21729
21730 /* Return a string for the output of a mode line %-spec for window W,
21731 generated by character C. FIELD_WIDTH > 0 means pad the string
21732 returned with spaces to that value. Return a Lisp string in
21733 *STRING if the resulting string is taken from that Lisp string.
21734
21735 Note we operate on the current buffer for most purposes. */
21736
21737 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21738
21739 static const char *
21740 decode_mode_spec (struct window *w, register int c, int field_width,
21741 Lisp_Object *string)
21742 {
21743 Lisp_Object obj;
21744 struct frame *f = XFRAME (WINDOW_FRAME (w));
21745 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21746 /* We are going to use f->decode_mode_spec_buffer as the buffer to
21747 produce strings from numerical values, so limit preposterously
21748 large values of FIELD_WIDTH to avoid overrunning the buffer's
21749 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
21750 bytes plus the terminating null. */
21751 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
21752 struct buffer *b = current_buffer;
21753
21754 obj = Qnil;
21755 *string = Qnil;
21756
21757 switch (c)
21758 {
21759 case '*':
21760 if (!NILP (BVAR (b, read_only)))
21761 return "%";
21762 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21763 return "*";
21764 return "-";
21765
21766 case '+':
21767 /* This differs from %* only for a modified read-only buffer. */
21768 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21769 return "*";
21770 if (!NILP (BVAR (b, read_only)))
21771 return "%";
21772 return "-";
21773
21774 case '&':
21775 /* This differs from %* in ignoring read-only-ness. */
21776 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21777 return "*";
21778 return "-";
21779
21780 case '%':
21781 return "%";
21782
21783 case '[':
21784 {
21785 int i;
21786 char *p;
21787
21788 if (command_loop_level > 5)
21789 return "[[[... ";
21790 p = decode_mode_spec_buf;
21791 for (i = 0; i < command_loop_level; i++)
21792 *p++ = '[';
21793 *p = 0;
21794 return decode_mode_spec_buf;
21795 }
21796
21797 case ']':
21798 {
21799 int i;
21800 char *p;
21801
21802 if (command_loop_level > 5)
21803 return " ...]]]";
21804 p = decode_mode_spec_buf;
21805 for (i = 0; i < command_loop_level; i++)
21806 *p++ = ']';
21807 *p = 0;
21808 return decode_mode_spec_buf;
21809 }
21810
21811 case '-':
21812 {
21813 register int i;
21814
21815 /* Let lots_of_dashes be a string of infinite length. */
21816 if (mode_line_target == MODE_LINE_NOPROP
21817 || mode_line_target == MODE_LINE_STRING)
21818 return "--";
21819 if (field_width <= 0
21820 || field_width > sizeof (lots_of_dashes))
21821 {
21822 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21823 decode_mode_spec_buf[i] = '-';
21824 decode_mode_spec_buf[i] = '\0';
21825 return decode_mode_spec_buf;
21826 }
21827 else
21828 return lots_of_dashes;
21829 }
21830
21831 case 'b':
21832 obj = BVAR (b, name);
21833 break;
21834
21835 case 'c':
21836 /* %c and %l are ignored in `frame-title-format'.
21837 (In redisplay_internal, the frame title is drawn _before_ the
21838 windows are updated, so the stuff which depends on actual
21839 window contents (such as %l) may fail to render properly, or
21840 even crash emacs.) */
21841 if (mode_line_target == MODE_LINE_TITLE)
21842 return "";
21843 else
21844 {
21845 ptrdiff_t col = current_column ();
21846 w->column_number_displayed = col;
21847 pint2str (decode_mode_spec_buf, width, col);
21848 return decode_mode_spec_buf;
21849 }
21850
21851 case 'e':
21852 #ifndef SYSTEM_MALLOC
21853 {
21854 if (NILP (Vmemory_full))
21855 return "";
21856 else
21857 return "!MEM FULL! ";
21858 }
21859 #else
21860 return "";
21861 #endif
21862
21863 case 'F':
21864 /* %F displays the frame name. */
21865 if (!NILP (f->title))
21866 return SSDATA (f->title);
21867 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21868 return SSDATA (f->name);
21869 return "Emacs";
21870
21871 case 'f':
21872 obj = BVAR (b, filename);
21873 break;
21874
21875 case 'i':
21876 {
21877 ptrdiff_t size = ZV - BEGV;
21878 pint2str (decode_mode_spec_buf, width, size);
21879 return decode_mode_spec_buf;
21880 }
21881
21882 case 'I':
21883 {
21884 ptrdiff_t size = ZV - BEGV;
21885 pint2hrstr (decode_mode_spec_buf, width, size);
21886 return decode_mode_spec_buf;
21887 }
21888
21889 case 'l':
21890 {
21891 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21892 ptrdiff_t topline, nlines, height;
21893 ptrdiff_t junk;
21894
21895 /* %c and %l are ignored in `frame-title-format'. */
21896 if (mode_line_target == MODE_LINE_TITLE)
21897 return "";
21898
21899 startpos = marker_position (w->start);
21900 startpos_byte = marker_byte_position (w->start);
21901 height = WINDOW_TOTAL_LINES (w);
21902
21903 /* If we decided that this buffer isn't suitable for line numbers,
21904 don't forget that too fast. */
21905 if (w->base_line_pos == -1)
21906 goto no_value;
21907
21908 /* If the buffer is very big, don't waste time. */
21909 if (INTEGERP (Vline_number_display_limit)
21910 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21911 {
21912 w->base_line_pos = 0;
21913 w->base_line_number = 0;
21914 goto no_value;
21915 }
21916
21917 if (w->base_line_number > 0
21918 && w->base_line_pos > 0
21919 && w->base_line_pos <= startpos)
21920 {
21921 line = w->base_line_number;
21922 linepos = w->base_line_pos;
21923 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21924 }
21925 else
21926 {
21927 line = 1;
21928 linepos = BUF_BEGV (b);
21929 linepos_byte = BUF_BEGV_BYTE (b);
21930 }
21931
21932 /* Count lines from base line to window start position. */
21933 nlines = display_count_lines (linepos_byte,
21934 startpos_byte,
21935 startpos, &junk);
21936
21937 topline = nlines + line;
21938
21939 /* Determine a new base line, if the old one is too close
21940 or too far away, or if we did not have one.
21941 "Too close" means it's plausible a scroll-down would
21942 go back past it. */
21943 if (startpos == BUF_BEGV (b))
21944 {
21945 w->base_line_number = topline;
21946 w->base_line_pos = BUF_BEGV (b);
21947 }
21948 else if (nlines < height + 25 || nlines > height * 3 + 50
21949 || linepos == BUF_BEGV (b))
21950 {
21951 ptrdiff_t limit = BUF_BEGV (b);
21952 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21953 ptrdiff_t position;
21954 ptrdiff_t distance =
21955 (height * 2 + 30) * line_number_display_limit_width;
21956
21957 if (startpos - distance > limit)
21958 {
21959 limit = startpos - distance;
21960 limit_byte = CHAR_TO_BYTE (limit);
21961 }
21962
21963 nlines = display_count_lines (startpos_byte,
21964 limit_byte,
21965 - (height * 2 + 30),
21966 &position);
21967 /* If we couldn't find the lines we wanted within
21968 line_number_display_limit_width chars per line,
21969 give up on line numbers for this window. */
21970 if (position == limit_byte && limit == startpos - distance)
21971 {
21972 w->base_line_pos = -1;
21973 w->base_line_number = 0;
21974 goto no_value;
21975 }
21976
21977 w->base_line_number = topline - nlines;
21978 w->base_line_pos = BYTE_TO_CHAR (position);
21979 }
21980
21981 /* Now count lines from the start pos to point. */
21982 nlines = display_count_lines (startpos_byte,
21983 PT_BYTE, PT, &junk);
21984
21985 /* Record that we did display the line number. */
21986 line_number_displayed = 1;
21987
21988 /* Make the string to show. */
21989 pint2str (decode_mode_spec_buf, width, topline + nlines);
21990 return decode_mode_spec_buf;
21991 no_value:
21992 {
21993 char* p = decode_mode_spec_buf;
21994 int pad = width - 2;
21995 while (pad-- > 0)
21996 *p++ = ' ';
21997 *p++ = '?';
21998 *p++ = '?';
21999 *p = '\0';
22000 return decode_mode_spec_buf;
22001 }
22002 }
22003 break;
22004
22005 case 'm':
22006 obj = BVAR (b, mode_name);
22007 break;
22008
22009 case 'n':
22010 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
22011 return " Narrow";
22012 break;
22013
22014 case 'p':
22015 {
22016 ptrdiff_t pos = marker_position (w->start);
22017 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22018
22019 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
22020 {
22021 if (pos <= BUF_BEGV (b))
22022 return "All";
22023 else
22024 return "Bottom";
22025 }
22026 else if (pos <= BUF_BEGV (b))
22027 return "Top";
22028 else
22029 {
22030 if (total > 1000000)
22031 /* Do it differently for a large value, to avoid overflow. */
22032 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22033 else
22034 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
22035 /* We can't normally display a 3-digit number,
22036 so get us a 2-digit number that is close. */
22037 if (total == 100)
22038 total = 99;
22039 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22040 return decode_mode_spec_buf;
22041 }
22042 }
22043
22044 /* Display percentage of size above the bottom of the screen. */
22045 case 'P':
22046 {
22047 ptrdiff_t toppos = marker_position (w->start);
22048 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
22049 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22050
22051 if (botpos >= BUF_ZV (b))
22052 {
22053 if (toppos <= BUF_BEGV (b))
22054 return "All";
22055 else
22056 return "Bottom";
22057 }
22058 else
22059 {
22060 if (total > 1000000)
22061 /* Do it differently for a large value, to avoid overflow. */
22062 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22063 else
22064 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
22065 /* We can't normally display a 3-digit number,
22066 so get us a 2-digit number that is close. */
22067 if (total == 100)
22068 total = 99;
22069 if (toppos <= BUF_BEGV (b))
22070 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
22071 else
22072 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22073 return decode_mode_spec_buf;
22074 }
22075 }
22076
22077 case 's':
22078 /* status of process */
22079 obj = Fget_buffer_process (Fcurrent_buffer ());
22080 if (NILP (obj))
22081 return "no process";
22082 #ifndef MSDOS
22083 obj = Fsymbol_name (Fprocess_status (obj));
22084 #endif
22085 break;
22086
22087 case '@':
22088 {
22089 ptrdiff_t count = inhibit_garbage_collection ();
22090 Lisp_Object val = call1 (intern ("file-remote-p"),
22091 BVAR (current_buffer, directory));
22092 unbind_to (count, Qnil);
22093
22094 if (NILP (val))
22095 return "-";
22096 else
22097 return "@";
22098 }
22099
22100 case 'z':
22101 /* coding-system (not including end-of-line format) */
22102 case 'Z':
22103 /* coding-system (including end-of-line type) */
22104 {
22105 int eol_flag = (c == 'Z');
22106 char *p = decode_mode_spec_buf;
22107
22108 if (! FRAME_WINDOW_P (f))
22109 {
22110 /* No need to mention EOL here--the terminal never needs
22111 to do EOL conversion. */
22112 p = decode_mode_spec_coding (CODING_ID_NAME
22113 (FRAME_KEYBOARD_CODING (f)->id),
22114 p, 0);
22115 p = decode_mode_spec_coding (CODING_ID_NAME
22116 (FRAME_TERMINAL_CODING (f)->id),
22117 p, 0);
22118 }
22119 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
22120 p, eol_flag);
22121
22122 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
22123 #ifdef subprocesses
22124 obj = Fget_buffer_process (Fcurrent_buffer ());
22125 if (PROCESSP (obj))
22126 {
22127 p = decode_mode_spec_coding
22128 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
22129 p = decode_mode_spec_coding
22130 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
22131 }
22132 #endif /* subprocesses */
22133 #endif /* 0 */
22134 *p = 0;
22135 return decode_mode_spec_buf;
22136 }
22137 }
22138
22139 if (STRINGP (obj))
22140 {
22141 *string = obj;
22142 return SSDATA (obj);
22143 }
22144 else
22145 return "";
22146 }
22147
22148
22149 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
22150 means count lines back from START_BYTE. But don't go beyond
22151 LIMIT_BYTE. Return the number of lines thus found (always
22152 nonnegative).
22153
22154 Set *BYTE_POS_PTR to the byte position where we stopped. This is
22155 either the position COUNT lines after/before START_BYTE, if we
22156 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
22157 COUNT lines. */
22158
22159 static ptrdiff_t
22160 display_count_lines (ptrdiff_t start_byte,
22161 ptrdiff_t limit_byte, ptrdiff_t count,
22162 ptrdiff_t *byte_pos_ptr)
22163 {
22164 register unsigned char *cursor;
22165 unsigned char *base;
22166
22167 register ptrdiff_t ceiling;
22168 register unsigned char *ceiling_addr;
22169 ptrdiff_t orig_count = count;
22170
22171 /* If we are not in selective display mode,
22172 check only for newlines. */
22173 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
22174 && !INTEGERP (BVAR (current_buffer, selective_display)));
22175
22176 if (count > 0)
22177 {
22178 while (start_byte < limit_byte)
22179 {
22180 ceiling = BUFFER_CEILING_OF (start_byte);
22181 ceiling = min (limit_byte - 1, ceiling);
22182 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
22183 base = (cursor = BYTE_POS_ADDR (start_byte));
22184
22185 do
22186 {
22187 if (selective_display)
22188 {
22189 while (*cursor != '\n' && *cursor != 015
22190 && ++cursor != ceiling_addr)
22191 continue;
22192 if (cursor == ceiling_addr)
22193 break;
22194 }
22195 else
22196 {
22197 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
22198 if (! cursor)
22199 break;
22200 }
22201
22202 cursor++;
22203
22204 if (--count == 0)
22205 {
22206 start_byte += cursor - base;
22207 *byte_pos_ptr = start_byte;
22208 return orig_count;
22209 }
22210 }
22211 while (cursor < ceiling_addr);
22212
22213 start_byte += ceiling_addr - base;
22214 }
22215 }
22216 else
22217 {
22218 while (start_byte > limit_byte)
22219 {
22220 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
22221 ceiling = max (limit_byte, ceiling);
22222 ceiling_addr = BYTE_POS_ADDR (ceiling);
22223 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
22224 while (1)
22225 {
22226 if (selective_display)
22227 {
22228 while (--cursor >= ceiling_addr
22229 && *cursor != '\n' && *cursor != 015)
22230 continue;
22231 if (cursor < ceiling_addr)
22232 break;
22233 }
22234 else
22235 {
22236 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
22237 if (! cursor)
22238 break;
22239 }
22240
22241 if (++count == 0)
22242 {
22243 start_byte += cursor - base + 1;
22244 *byte_pos_ptr = start_byte;
22245 /* When scanning backwards, we should
22246 not count the newline posterior to which we stop. */
22247 return - orig_count - 1;
22248 }
22249 }
22250 start_byte += ceiling_addr - base;
22251 }
22252 }
22253
22254 *byte_pos_ptr = limit_byte;
22255
22256 if (count < 0)
22257 return - orig_count + count;
22258 return orig_count - count;
22259
22260 }
22261
22262
22263 \f
22264 /***********************************************************************
22265 Displaying strings
22266 ***********************************************************************/
22267
22268 /* Display a NUL-terminated string, starting with index START.
22269
22270 If STRING is non-null, display that C string. Otherwise, the Lisp
22271 string LISP_STRING is displayed. There's a case that STRING is
22272 non-null and LISP_STRING is not nil. It means STRING is a string
22273 data of LISP_STRING. In that case, we display LISP_STRING while
22274 ignoring its text properties.
22275
22276 If FACE_STRING is not nil, FACE_STRING_POS is a position in
22277 FACE_STRING. Display STRING or LISP_STRING with the face at
22278 FACE_STRING_POS in FACE_STRING:
22279
22280 Display the string in the environment given by IT, but use the
22281 standard display table, temporarily.
22282
22283 FIELD_WIDTH is the minimum number of output glyphs to produce.
22284 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22285 with spaces. If STRING has more characters, more than FIELD_WIDTH
22286 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
22287
22288 PRECISION is the maximum number of characters to output from
22289 STRING. PRECISION < 0 means don't truncate the string.
22290
22291 This is roughly equivalent to printf format specifiers:
22292
22293 FIELD_WIDTH PRECISION PRINTF
22294 ----------------------------------------
22295 -1 -1 %s
22296 -1 10 %.10s
22297 10 -1 %10s
22298 20 10 %20.10s
22299
22300 MULTIBYTE zero means do not display multibyte chars, > 0 means do
22301 display them, and < 0 means obey the current buffer's value of
22302 enable_multibyte_characters.
22303
22304 Value is the number of columns displayed. */
22305
22306 static int
22307 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
22308 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
22309 int field_width, int precision, int max_x, int multibyte)
22310 {
22311 int hpos_at_start = it->hpos;
22312 int saved_face_id = it->face_id;
22313 struct glyph_row *row = it->glyph_row;
22314 ptrdiff_t it_charpos;
22315
22316 /* Initialize the iterator IT for iteration over STRING beginning
22317 with index START. */
22318 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
22319 precision, field_width, multibyte);
22320 if (string && STRINGP (lisp_string))
22321 /* LISP_STRING is the one returned by decode_mode_spec. We should
22322 ignore its text properties. */
22323 it->stop_charpos = it->end_charpos;
22324
22325 /* If displaying STRING, set up the face of the iterator from
22326 FACE_STRING, if that's given. */
22327 if (STRINGP (face_string))
22328 {
22329 ptrdiff_t endptr;
22330 struct face *face;
22331
22332 it->face_id
22333 = face_at_string_position (it->w, face_string, face_string_pos,
22334 0, it->region_beg_charpos,
22335 it->region_end_charpos,
22336 &endptr, it->base_face_id, 0);
22337 face = FACE_FROM_ID (it->f, it->face_id);
22338 it->face_box_p = face->box != FACE_NO_BOX;
22339 }
22340
22341 /* Set max_x to the maximum allowed X position. Don't let it go
22342 beyond the right edge of the window. */
22343 if (max_x <= 0)
22344 max_x = it->last_visible_x;
22345 else
22346 max_x = min (max_x, it->last_visible_x);
22347
22348 /* Skip over display elements that are not visible. because IT->w is
22349 hscrolled. */
22350 if (it->current_x < it->first_visible_x)
22351 move_it_in_display_line_to (it, 100000, it->first_visible_x,
22352 MOVE_TO_POS | MOVE_TO_X);
22353
22354 row->ascent = it->max_ascent;
22355 row->height = it->max_ascent + it->max_descent;
22356 row->phys_ascent = it->max_phys_ascent;
22357 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
22358 row->extra_line_spacing = it->max_extra_line_spacing;
22359
22360 if (STRINGP (it->string))
22361 it_charpos = IT_STRING_CHARPOS (*it);
22362 else
22363 it_charpos = IT_CHARPOS (*it);
22364
22365 /* This condition is for the case that we are called with current_x
22366 past last_visible_x. */
22367 while (it->current_x < max_x)
22368 {
22369 int x_before, x, n_glyphs_before, i, nglyphs;
22370
22371 /* Get the next display element. */
22372 if (!get_next_display_element (it))
22373 break;
22374
22375 /* Produce glyphs. */
22376 x_before = it->current_x;
22377 n_glyphs_before = row->used[TEXT_AREA];
22378 PRODUCE_GLYPHS (it);
22379
22380 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
22381 i = 0;
22382 x = x_before;
22383 while (i < nglyphs)
22384 {
22385 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
22386
22387 if (it->line_wrap != TRUNCATE
22388 && x + glyph->pixel_width > max_x)
22389 {
22390 /* End of continued line or max_x reached. */
22391 if (CHAR_GLYPH_PADDING_P (*glyph))
22392 {
22393 /* A wide character is unbreakable. */
22394 if (row->reversed_p)
22395 unproduce_glyphs (it, row->used[TEXT_AREA]
22396 - n_glyphs_before);
22397 row->used[TEXT_AREA] = n_glyphs_before;
22398 it->current_x = x_before;
22399 }
22400 else
22401 {
22402 if (row->reversed_p)
22403 unproduce_glyphs (it, row->used[TEXT_AREA]
22404 - (n_glyphs_before + i));
22405 row->used[TEXT_AREA] = n_glyphs_before + i;
22406 it->current_x = x;
22407 }
22408 break;
22409 }
22410 else if (x + glyph->pixel_width >= it->first_visible_x)
22411 {
22412 /* Glyph is at least partially visible. */
22413 ++it->hpos;
22414 if (x < it->first_visible_x)
22415 row->x = x - it->first_visible_x;
22416 }
22417 else
22418 {
22419 /* Glyph is off the left margin of the display area.
22420 Should not happen. */
22421 emacs_abort ();
22422 }
22423
22424 row->ascent = max (row->ascent, it->max_ascent);
22425 row->height = max (row->height, it->max_ascent + it->max_descent);
22426 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
22427 row->phys_height = max (row->phys_height,
22428 it->max_phys_ascent + it->max_phys_descent);
22429 row->extra_line_spacing = max (row->extra_line_spacing,
22430 it->max_extra_line_spacing);
22431 x += glyph->pixel_width;
22432 ++i;
22433 }
22434
22435 /* Stop if max_x reached. */
22436 if (i < nglyphs)
22437 break;
22438
22439 /* Stop at line ends. */
22440 if (ITERATOR_AT_END_OF_LINE_P (it))
22441 {
22442 it->continuation_lines_width = 0;
22443 break;
22444 }
22445
22446 set_iterator_to_next (it, 1);
22447 if (STRINGP (it->string))
22448 it_charpos = IT_STRING_CHARPOS (*it);
22449 else
22450 it_charpos = IT_CHARPOS (*it);
22451
22452 /* Stop if truncating at the right edge. */
22453 if (it->line_wrap == TRUNCATE
22454 && it->current_x >= it->last_visible_x)
22455 {
22456 /* Add truncation mark, but don't do it if the line is
22457 truncated at a padding space. */
22458 if (it_charpos < it->string_nchars)
22459 {
22460 if (!FRAME_WINDOW_P (it->f))
22461 {
22462 int ii, n;
22463
22464 if (it->current_x > it->last_visible_x)
22465 {
22466 if (!row->reversed_p)
22467 {
22468 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22469 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22470 break;
22471 }
22472 else
22473 {
22474 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22475 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22476 break;
22477 unproduce_glyphs (it, ii + 1);
22478 ii = row->used[TEXT_AREA] - (ii + 1);
22479 }
22480 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22481 {
22482 row->used[TEXT_AREA] = ii;
22483 produce_special_glyphs (it, IT_TRUNCATION);
22484 }
22485 }
22486 produce_special_glyphs (it, IT_TRUNCATION);
22487 }
22488 row->truncated_on_right_p = 1;
22489 }
22490 break;
22491 }
22492 }
22493
22494 /* Maybe insert a truncation at the left. */
22495 if (it->first_visible_x
22496 && it_charpos > 0)
22497 {
22498 if (!FRAME_WINDOW_P (it->f)
22499 || (row->reversed_p
22500 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22501 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22502 insert_left_trunc_glyphs (it);
22503 row->truncated_on_left_p = 1;
22504 }
22505
22506 it->face_id = saved_face_id;
22507
22508 /* Value is number of columns displayed. */
22509 return it->hpos - hpos_at_start;
22510 }
22511
22512
22513 \f
22514 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22515 appears as an element of LIST or as the car of an element of LIST.
22516 If PROPVAL is a list, compare each element against LIST in that
22517 way, and return 1/2 if any element of PROPVAL is found in LIST.
22518 Otherwise return 0. This function cannot quit.
22519 The return value is 2 if the text is invisible but with an ellipsis
22520 and 1 if it's invisible and without an ellipsis. */
22521
22522 int
22523 invisible_p (register Lisp_Object propval, Lisp_Object list)
22524 {
22525 register Lisp_Object tail, proptail;
22526
22527 for (tail = list; CONSP (tail); tail = XCDR (tail))
22528 {
22529 register Lisp_Object tem;
22530 tem = XCAR (tail);
22531 if (EQ (propval, tem))
22532 return 1;
22533 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22534 return NILP (XCDR (tem)) ? 1 : 2;
22535 }
22536
22537 if (CONSP (propval))
22538 {
22539 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22540 {
22541 Lisp_Object propelt;
22542 propelt = XCAR (proptail);
22543 for (tail = list; CONSP (tail); tail = XCDR (tail))
22544 {
22545 register Lisp_Object tem;
22546 tem = XCAR (tail);
22547 if (EQ (propelt, tem))
22548 return 1;
22549 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22550 return NILP (XCDR (tem)) ? 1 : 2;
22551 }
22552 }
22553 }
22554
22555 return 0;
22556 }
22557
22558 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22559 doc: /* Non-nil if the property makes the text invisible.
22560 POS-OR-PROP can be a marker or number, in which case it is taken to be
22561 a position in the current buffer and the value of the `invisible' property
22562 is checked; or it can be some other value, which is then presumed to be the
22563 value of the `invisible' property of the text of interest.
22564 The non-nil value returned can be t for truly invisible text or something
22565 else if the text is replaced by an ellipsis. */)
22566 (Lisp_Object pos_or_prop)
22567 {
22568 Lisp_Object prop
22569 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22570 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22571 : pos_or_prop);
22572 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22573 return (invis == 0 ? Qnil
22574 : invis == 1 ? Qt
22575 : make_number (invis));
22576 }
22577
22578 /* Calculate a width or height in pixels from a specification using
22579 the following elements:
22580
22581 SPEC ::=
22582 NUM - a (fractional) multiple of the default font width/height
22583 (NUM) - specifies exactly NUM pixels
22584 UNIT - a fixed number of pixels, see below.
22585 ELEMENT - size of a display element in pixels, see below.
22586 (NUM . SPEC) - equals NUM * SPEC
22587 (+ SPEC SPEC ...) - add pixel values
22588 (- SPEC SPEC ...) - subtract pixel values
22589 (- SPEC) - negate pixel value
22590
22591 NUM ::=
22592 INT or FLOAT - a number constant
22593 SYMBOL - use symbol's (buffer local) variable binding.
22594
22595 UNIT ::=
22596 in - pixels per inch *)
22597 mm - pixels per 1/1000 meter *)
22598 cm - pixels per 1/100 meter *)
22599 width - width of current font in pixels.
22600 height - height of current font in pixels.
22601
22602 *) using the ratio(s) defined in display-pixels-per-inch.
22603
22604 ELEMENT ::=
22605
22606 left-fringe - left fringe width in pixels
22607 right-fringe - right fringe width in pixels
22608
22609 left-margin - left margin width in pixels
22610 right-margin - right margin width in pixels
22611
22612 scroll-bar - scroll-bar area width in pixels
22613
22614 Examples:
22615
22616 Pixels corresponding to 5 inches:
22617 (5 . in)
22618
22619 Total width of non-text areas on left side of window (if scroll-bar is on left):
22620 '(space :width (+ left-fringe left-margin scroll-bar))
22621
22622 Align to first text column (in header line):
22623 '(space :align-to 0)
22624
22625 Align to middle of text area minus half the width of variable `my-image'
22626 containing a loaded image:
22627 '(space :align-to (0.5 . (- text my-image)))
22628
22629 Width of left margin minus width of 1 character in the default font:
22630 '(space :width (- left-margin 1))
22631
22632 Width of left margin minus width of 2 characters in the current font:
22633 '(space :width (- left-margin (2 . width)))
22634
22635 Center 1 character over left-margin (in header line):
22636 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22637
22638 Different ways to express width of left fringe plus left margin minus one pixel:
22639 '(space :width (- (+ left-fringe left-margin) (1)))
22640 '(space :width (+ left-fringe left-margin (- (1))))
22641 '(space :width (+ left-fringe left-margin (-1)))
22642
22643 */
22644
22645 static int
22646 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22647 struct font *font, int width_p, int *align_to)
22648 {
22649 double pixels;
22650
22651 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22652 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22653
22654 if (NILP (prop))
22655 return OK_PIXELS (0);
22656
22657 eassert (FRAME_LIVE_P (it->f));
22658
22659 if (SYMBOLP (prop))
22660 {
22661 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22662 {
22663 char *unit = SSDATA (SYMBOL_NAME (prop));
22664
22665 if (unit[0] == 'i' && unit[1] == 'n')
22666 pixels = 1.0;
22667 else if (unit[0] == 'm' && unit[1] == 'm')
22668 pixels = 25.4;
22669 else if (unit[0] == 'c' && unit[1] == 'm')
22670 pixels = 2.54;
22671 else
22672 pixels = 0;
22673 if (pixels > 0)
22674 {
22675 double ppi = (width_p ? FRAME_RES_X (it->f)
22676 : FRAME_RES_Y (it->f));
22677
22678 if (ppi > 0)
22679 return OK_PIXELS (ppi / pixels);
22680 return 0;
22681 }
22682 }
22683
22684 #ifdef HAVE_WINDOW_SYSTEM
22685 if (EQ (prop, Qheight))
22686 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22687 if (EQ (prop, Qwidth))
22688 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22689 #else
22690 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22691 return OK_PIXELS (1);
22692 #endif
22693
22694 if (EQ (prop, Qtext))
22695 return OK_PIXELS (width_p
22696 ? window_box_width (it->w, TEXT_AREA)
22697 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22698
22699 if (align_to && *align_to < 0)
22700 {
22701 *res = 0;
22702 if (EQ (prop, Qleft))
22703 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22704 if (EQ (prop, Qright))
22705 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22706 if (EQ (prop, Qcenter))
22707 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22708 + window_box_width (it->w, TEXT_AREA) / 2);
22709 if (EQ (prop, Qleft_fringe))
22710 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22711 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22712 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22713 if (EQ (prop, Qright_fringe))
22714 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22715 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22716 : window_box_right_offset (it->w, TEXT_AREA));
22717 if (EQ (prop, Qleft_margin))
22718 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22719 if (EQ (prop, Qright_margin))
22720 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22721 if (EQ (prop, Qscroll_bar))
22722 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22723 ? 0
22724 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22725 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22726 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22727 : 0)));
22728 }
22729 else
22730 {
22731 if (EQ (prop, Qleft_fringe))
22732 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22733 if (EQ (prop, Qright_fringe))
22734 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22735 if (EQ (prop, Qleft_margin))
22736 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22737 if (EQ (prop, Qright_margin))
22738 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22739 if (EQ (prop, Qscroll_bar))
22740 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22741 }
22742
22743 prop = buffer_local_value_1 (prop, it->w->contents);
22744 if (EQ (prop, Qunbound))
22745 prop = Qnil;
22746 }
22747
22748 if (INTEGERP (prop) || FLOATP (prop))
22749 {
22750 int base_unit = (width_p
22751 ? FRAME_COLUMN_WIDTH (it->f)
22752 : FRAME_LINE_HEIGHT (it->f));
22753 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22754 }
22755
22756 if (CONSP (prop))
22757 {
22758 Lisp_Object car = XCAR (prop);
22759 Lisp_Object cdr = XCDR (prop);
22760
22761 if (SYMBOLP (car))
22762 {
22763 #ifdef HAVE_WINDOW_SYSTEM
22764 if (FRAME_WINDOW_P (it->f)
22765 && valid_image_p (prop))
22766 {
22767 ptrdiff_t id = lookup_image (it->f, prop);
22768 struct image *img = IMAGE_FROM_ID (it->f, id);
22769
22770 return OK_PIXELS (width_p ? img->width : img->height);
22771 }
22772 #endif
22773 if (EQ (car, Qplus) || EQ (car, Qminus))
22774 {
22775 int first = 1;
22776 double px;
22777
22778 pixels = 0;
22779 while (CONSP (cdr))
22780 {
22781 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22782 font, width_p, align_to))
22783 return 0;
22784 if (first)
22785 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22786 else
22787 pixels += px;
22788 cdr = XCDR (cdr);
22789 }
22790 if (EQ (car, Qminus))
22791 pixels = -pixels;
22792 return OK_PIXELS (pixels);
22793 }
22794
22795 car = buffer_local_value_1 (car, it->w->contents);
22796 if (EQ (car, Qunbound))
22797 car = Qnil;
22798 }
22799
22800 if (INTEGERP (car) || FLOATP (car))
22801 {
22802 double fact;
22803 pixels = XFLOATINT (car);
22804 if (NILP (cdr))
22805 return OK_PIXELS (pixels);
22806 if (calc_pixel_width_or_height (&fact, it, cdr,
22807 font, width_p, align_to))
22808 return OK_PIXELS (pixels * fact);
22809 return 0;
22810 }
22811
22812 return 0;
22813 }
22814
22815 return 0;
22816 }
22817
22818 \f
22819 /***********************************************************************
22820 Glyph Display
22821 ***********************************************************************/
22822
22823 #ifdef HAVE_WINDOW_SYSTEM
22824
22825 #ifdef GLYPH_DEBUG
22826
22827 void
22828 dump_glyph_string (struct glyph_string *s)
22829 {
22830 fprintf (stderr, "glyph string\n");
22831 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22832 s->x, s->y, s->width, s->height);
22833 fprintf (stderr, " ybase = %d\n", s->ybase);
22834 fprintf (stderr, " hl = %d\n", s->hl);
22835 fprintf (stderr, " left overhang = %d, right = %d\n",
22836 s->left_overhang, s->right_overhang);
22837 fprintf (stderr, " nchars = %d\n", s->nchars);
22838 fprintf (stderr, " extends to end of line = %d\n",
22839 s->extends_to_end_of_line_p);
22840 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22841 fprintf (stderr, " bg width = %d\n", s->background_width);
22842 }
22843
22844 #endif /* GLYPH_DEBUG */
22845
22846 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22847 of XChar2b structures for S; it can't be allocated in
22848 init_glyph_string because it must be allocated via `alloca'. W
22849 is the window on which S is drawn. ROW and AREA are the glyph row
22850 and area within the row from which S is constructed. START is the
22851 index of the first glyph structure covered by S. HL is a
22852 face-override for drawing S. */
22853
22854 #ifdef HAVE_NTGUI
22855 #define OPTIONAL_HDC(hdc) HDC hdc,
22856 #define DECLARE_HDC(hdc) HDC hdc;
22857 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22858 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22859 #endif
22860
22861 #ifndef OPTIONAL_HDC
22862 #define OPTIONAL_HDC(hdc)
22863 #define DECLARE_HDC(hdc)
22864 #define ALLOCATE_HDC(hdc, f)
22865 #define RELEASE_HDC(hdc, f)
22866 #endif
22867
22868 static void
22869 init_glyph_string (struct glyph_string *s,
22870 OPTIONAL_HDC (hdc)
22871 XChar2b *char2b, struct window *w, struct glyph_row *row,
22872 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22873 {
22874 memset (s, 0, sizeof *s);
22875 s->w = w;
22876 s->f = XFRAME (w->frame);
22877 #ifdef HAVE_NTGUI
22878 s->hdc = hdc;
22879 #endif
22880 s->display = FRAME_X_DISPLAY (s->f);
22881 s->window = FRAME_X_WINDOW (s->f);
22882 s->char2b = char2b;
22883 s->hl = hl;
22884 s->row = row;
22885 s->area = area;
22886 s->first_glyph = row->glyphs[area] + start;
22887 s->height = row->height;
22888 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22889 s->ybase = s->y + row->ascent;
22890 }
22891
22892
22893 /* Append the list of glyph strings with head H and tail T to the list
22894 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22895
22896 static void
22897 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22898 struct glyph_string *h, struct glyph_string *t)
22899 {
22900 if (h)
22901 {
22902 if (*head)
22903 (*tail)->next = h;
22904 else
22905 *head = h;
22906 h->prev = *tail;
22907 *tail = t;
22908 }
22909 }
22910
22911
22912 /* Prepend the list of glyph strings with head H and tail T to the
22913 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22914 result. */
22915
22916 static void
22917 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22918 struct glyph_string *h, struct glyph_string *t)
22919 {
22920 if (h)
22921 {
22922 if (*head)
22923 (*head)->prev = t;
22924 else
22925 *tail = t;
22926 t->next = *head;
22927 *head = h;
22928 }
22929 }
22930
22931
22932 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22933 Set *HEAD and *TAIL to the resulting list. */
22934
22935 static void
22936 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22937 struct glyph_string *s)
22938 {
22939 s->next = s->prev = NULL;
22940 append_glyph_string_lists (head, tail, s, s);
22941 }
22942
22943
22944 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22945 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22946 make sure that X resources for the face returned are allocated.
22947 Value is a pointer to a realized face that is ready for display if
22948 DISPLAY_P is non-zero. */
22949
22950 static struct face *
22951 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22952 XChar2b *char2b, int display_p)
22953 {
22954 struct face *face = FACE_FROM_ID (f, face_id);
22955 unsigned code = 0;
22956
22957 if (face->font)
22958 {
22959 code = face->font->driver->encode_char (face->font, c);
22960
22961 if (code == FONT_INVALID_CODE)
22962 code = 0;
22963 }
22964 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22965
22966 /* Make sure X resources of the face are allocated. */
22967 #ifdef HAVE_X_WINDOWS
22968 if (display_p)
22969 #endif
22970 {
22971 eassert (face != NULL);
22972 PREPARE_FACE_FOR_DISPLAY (f, face);
22973 }
22974
22975 return face;
22976 }
22977
22978
22979 /* Get face and two-byte form of character glyph GLYPH on frame F.
22980 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22981 a pointer to a realized face that is ready for display. */
22982
22983 static struct face *
22984 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22985 XChar2b *char2b, int *two_byte_p)
22986 {
22987 struct face *face;
22988 unsigned code = 0;
22989
22990 eassert (glyph->type == CHAR_GLYPH);
22991 face = FACE_FROM_ID (f, glyph->face_id);
22992
22993 /* Make sure X resources of the face are allocated. */
22994 eassert (face != NULL);
22995 PREPARE_FACE_FOR_DISPLAY (f, face);
22996
22997 if (two_byte_p)
22998 *two_byte_p = 0;
22999
23000 if (face->font)
23001 {
23002 if (CHAR_BYTE8_P (glyph->u.ch))
23003 code = CHAR_TO_BYTE8 (glyph->u.ch);
23004 else
23005 code = face->font->driver->encode_char (face->font, glyph->u.ch);
23006
23007 if (code == FONT_INVALID_CODE)
23008 code = 0;
23009 }
23010
23011 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23012 return face;
23013 }
23014
23015
23016 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
23017 Return 1 if FONT has a glyph for C, otherwise return 0. */
23018
23019 static int
23020 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
23021 {
23022 unsigned code;
23023
23024 if (CHAR_BYTE8_P (c))
23025 code = CHAR_TO_BYTE8 (c);
23026 else
23027 code = font->driver->encode_char (font, c);
23028
23029 if (code == FONT_INVALID_CODE)
23030 return 0;
23031 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23032 return 1;
23033 }
23034
23035
23036 /* Fill glyph string S with composition components specified by S->cmp.
23037
23038 BASE_FACE is the base face of the composition.
23039 S->cmp_from is the index of the first component for S.
23040
23041 OVERLAPS non-zero means S should draw the foreground only, and use
23042 its physical height for clipping. See also draw_glyphs.
23043
23044 Value is the index of a component not in S. */
23045
23046 static int
23047 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
23048 int overlaps)
23049 {
23050 int i;
23051 /* For all glyphs of this composition, starting at the offset
23052 S->cmp_from, until we reach the end of the definition or encounter a
23053 glyph that requires the different face, add it to S. */
23054 struct face *face;
23055
23056 eassert (s);
23057
23058 s->for_overlaps = overlaps;
23059 s->face = NULL;
23060 s->font = NULL;
23061 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
23062 {
23063 int c = COMPOSITION_GLYPH (s->cmp, i);
23064
23065 /* TAB in a composition means display glyphs with padding space
23066 on the left or right. */
23067 if (c != '\t')
23068 {
23069 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
23070 -1, Qnil);
23071
23072 face = get_char_face_and_encoding (s->f, c, face_id,
23073 s->char2b + i, 1);
23074 if (face)
23075 {
23076 if (! s->face)
23077 {
23078 s->face = face;
23079 s->font = s->face->font;
23080 }
23081 else if (s->face != face)
23082 break;
23083 }
23084 }
23085 ++s->nchars;
23086 }
23087 s->cmp_to = i;
23088
23089 if (s->face == NULL)
23090 {
23091 s->face = base_face->ascii_face;
23092 s->font = s->face->font;
23093 }
23094
23095 /* All glyph strings for the same composition has the same width,
23096 i.e. the width set for the first component of the composition. */
23097 s->width = s->first_glyph->pixel_width;
23098
23099 /* If the specified font could not be loaded, use the frame's
23100 default font, but record the fact that we couldn't load it in
23101 the glyph string so that we can draw rectangles for the
23102 characters of the glyph string. */
23103 if (s->font == NULL)
23104 {
23105 s->font_not_found_p = 1;
23106 s->font = FRAME_FONT (s->f);
23107 }
23108
23109 /* Adjust base line for subscript/superscript text. */
23110 s->ybase += s->first_glyph->voffset;
23111
23112 /* This glyph string must always be drawn with 16-bit functions. */
23113 s->two_byte_p = 1;
23114
23115 return s->cmp_to;
23116 }
23117
23118 static int
23119 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
23120 int start, int end, int overlaps)
23121 {
23122 struct glyph *glyph, *last;
23123 Lisp_Object lgstring;
23124 int i;
23125
23126 s->for_overlaps = overlaps;
23127 glyph = s->row->glyphs[s->area] + start;
23128 last = s->row->glyphs[s->area] + end;
23129 s->cmp_id = glyph->u.cmp.id;
23130 s->cmp_from = glyph->slice.cmp.from;
23131 s->cmp_to = glyph->slice.cmp.to + 1;
23132 s->face = FACE_FROM_ID (s->f, face_id);
23133 lgstring = composition_gstring_from_id (s->cmp_id);
23134 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
23135 glyph++;
23136 while (glyph < last
23137 && glyph->u.cmp.automatic
23138 && glyph->u.cmp.id == s->cmp_id
23139 && s->cmp_to == glyph->slice.cmp.from)
23140 s->cmp_to = (glyph++)->slice.cmp.to + 1;
23141
23142 for (i = s->cmp_from; i < s->cmp_to; i++)
23143 {
23144 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
23145 unsigned code = LGLYPH_CODE (lglyph);
23146
23147 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
23148 }
23149 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
23150 return glyph - s->row->glyphs[s->area];
23151 }
23152
23153
23154 /* Fill glyph string S from a sequence glyphs for glyphless characters.
23155 See the comment of fill_glyph_string for arguments.
23156 Value is the index of the first glyph not in S. */
23157
23158
23159 static int
23160 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
23161 int start, int end, int overlaps)
23162 {
23163 struct glyph *glyph, *last;
23164 int voffset;
23165
23166 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
23167 s->for_overlaps = overlaps;
23168 glyph = s->row->glyphs[s->area] + start;
23169 last = s->row->glyphs[s->area] + end;
23170 voffset = glyph->voffset;
23171 s->face = FACE_FROM_ID (s->f, face_id);
23172 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
23173 s->nchars = 1;
23174 s->width = glyph->pixel_width;
23175 glyph++;
23176 while (glyph < last
23177 && glyph->type == GLYPHLESS_GLYPH
23178 && glyph->voffset == voffset
23179 && glyph->face_id == face_id)
23180 {
23181 s->nchars++;
23182 s->width += glyph->pixel_width;
23183 glyph++;
23184 }
23185 s->ybase += voffset;
23186 return glyph - s->row->glyphs[s->area];
23187 }
23188
23189
23190 /* Fill glyph string S from a sequence of character glyphs.
23191
23192 FACE_ID is the face id of the string. START is the index of the
23193 first glyph to consider, END is the index of the last + 1.
23194 OVERLAPS non-zero means S should draw the foreground only, and use
23195 its physical height for clipping. See also draw_glyphs.
23196
23197 Value is the index of the first glyph not in S. */
23198
23199 static int
23200 fill_glyph_string (struct glyph_string *s, int face_id,
23201 int start, int end, int overlaps)
23202 {
23203 struct glyph *glyph, *last;
23204 int voffset;
23205 int glyph_not_available_p;
23206
23207 eassert (s->f == XFRAME (s->w->frame));
23208 eassert (s->nchars == 0);
23209 eassert (start >= 0 && end > start);
23210
23211 s->for_overlaps = overlaps;
23212 glyph = s->row->glyphs[s->area] + start;
23213 last = s->row->glyphs[s->area] + end;
23214 voffset = glyph->voffset;
23215 s->padding_p = glyph->padding_p;
23216 glyph_not_available_p = glyph->glyph_not_available_p;
23217
23218 while (glyph < last
23219 && glyph->type == CHAR_GLYPH
23220 && glyph->voffset == voffset
23221 /* Same face id implies same font, nowadays. */
23222 && glyph->face_id == face_id
23223 && glyph->glyph_not_available_p == glyph_not_available_p)
23224 {
23225 int two_byte_p;
23226
23227 s->face = get_glyph_face_and_encoding (s->f, glyph,
23228 s->char2b + s->nchars,
23229 &two_byte_p);
23230 s->two_byte_p = two_byte_p;
23231 ++s->nchars;
23232 eassert (s->nchars <= end - start);
23233 s->width += glyph->pixel_width;
23234 if (glyph++->padding_p != s->padding_p)
23235 break;
23236 }
23237
23238 s->font = s->face->font;
23239
23240 /* If the specified font could not be loaded, use the frame's font,
23241 but record the fact that we couldn't load it in
23242 S->font_not_found_p so that we can draw rectangles for the
23243 characters of the glyph string. */
23244 if (s->font == NULL || glyph_not_available_p)
23245 {
23246 s->font_not_found_p = 1;
23247 s->font = FRAME_FONT (s->f);
23248 }
23249
23250 /* Adjust base line for subscript/superscript text. */
23251 s->ybase += voffset;
23252
23253 eassert (s->face && s->face->gc);
23254 return glyph - s->row->glyphs[s->area];
23255 }
23256
23257
23258 /* Fill glyph string S from image glyph S->first_glyph. */
23259
23260 static void
23261 fill_image_glyph_string (struct glyph_string *s)
23262 {
23263 eassert (s->first_glyph->type == IMAGE_GLYPH);
23264 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
23265 eassert (s->img);
23266 s->slice = s->first_glyph->slice.img;
23267 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
23268 s->font = s->face->font;
23269 s->width = s->first_glyph->pixel_width;
23270
23271 /* Adjust base line for subscript/superscript text. */
23272 s->ybase += s->first_glyph->voffset;
23273 }
23274
23275
23276 /* Fill glyph string S from a sequence of stretch glyphs.
23277
23278 START is the index of the first glyph to consider,
23279 END is the index of the last + 1.
23280
23281 Value is the index of the first glyph not in S. */
23282
23283 static int
23284 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
23285 {
23286 struct glyph *glyph, *last;
23287 int voffset, face_id;
23288
23289 eassert (s->first_glyph->type == STRETCH_GLYPH);
23290
23291 glyph = s->row->glyphs[s->area] + start;
23292 last = s->row->glyphs[s->area] + end;
23293 face_id = glyph->face_id;
23294 s->face = FACE_FROM_ID (s->f, face_id);
23295 s->font = s->face->font;
23296 s->width = glyph->pixel_width;
23297 s->nchars = 1;
23298 voffset = glyph->voffset;
23299
23300 for (++glyph;
23301 (glyph < last
23302 && glyph->type == STRETCH_GLYPH
23303 && glyph->voffset == voffset
23304 && glyph->face_id == face_id);
23305 ++glyph)
23306 s->width += glyph->pixel_width;
23307
23308 /* Adjust base line for subscript/superscript text. */
23309 s->ybase += voffset;
23310
23311 /* The case that face->gc == 0 is handled when drawing the glyph
23312 string by calling PREPARE_FACE_FOR_DISPLAY. */
23313 eassert (s->face);
23314 return glyph - s->row->glyphs[s->area];
23315 }
23316
23317 static struct font_metrics *
23318 get_per_char_metric (struct font *font, XChar2b *char2b)
23319 {
23320 static struct font_metrics metrics;
23321 unsigned code;
23322
23323 if (! font)
23324 return NULL;
23325 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
23326 if (code == FONT_INVALID_CODE)
23327 return NULL;
23328 font->driver->text_extents (font, &code, 1, &metrics);
23329 return &metrics;
23330 }
23331
23332 /* EXPORT for RIF:
23333 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
23334 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
23335 assumed to be zero. */
23336
23337 void
23338 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
23339 {
23340 *left = *right = 0;
23341
23342 if (glyph->type == CHAR_GLYPH)
23343 {
23344 struct face *face;
23345 XChar2b char2b;
23346 struct font_metrics *pcm;
23347
23348 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
23349 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
23350 {
23351 if (pcm->rbearing > pcm->width)
23352 *right = pcm->rbearing - pcm->width;
23353 if (pcm->lbearing < 0)
23354 *left = -pcm->lbearing;
23355 }
23356 }
23357 else if (glyph->type == COMPOSITE_GLYPH)
23358 {
23359 if (! glyph->u.cmp.automatic)
23360 {
23361 struct composition *cmp = composition_table[glyph->u.cmp.id];
23362
23363 if (cmp->rbearing > cmp->pixel_width)
23364 *right = cmp->rbearing - cmp->pixel_width;
23365 if (cmp->lbearing < 0)
23366 *left = - cmp->lbearing;
23367 }
23368 else
23369 {
23370 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
23371 struct font_metrics metrics;
23372
23373 composition_gstring_width (gstring, glyph->slice.cmp.from,
23374 glyph->slice.cmp.to + 1, &metrics);
23375 if (metrics.rbearing > metrics.width)
23376 *right = metrics.rbearing - metrics.width;
23377 if (metrics.lbearing < 0)
23378 *left = - metrics.lbearing;
23379 }
23380 }
23381 }
23382
23383
23384 /* Return the index of the first glyph preceding glyph string S that
23385 is overwritten by S because of S's left overhang. Value is -1
23386 if no glyphs are overwritten. */
23387
23388 static int
23389 left_overwritten (struct glyph_string *s)
23390 {
23391 int k;
23392
23393 if (s->left_overhang)
23394 {
23395 int x = 0, i;
23396 struct glyph *glyphs = s->row->glyphs[s->area];
23397 int first = s->first_glyph - glyphs;
23398
23399 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
23400 x -= glyphs[i].pixel_width;
23401
23402 k = i + 1;
23403 }
23404 else
23405 k = -1;
23406
23407 return k;
23408 }
23409
23410
23411 /* Return the index of the first glyph preceding glyph string S that
23412 is overwriting S because of its right overhang. Value is -1 if no
23413 glyph in front of S overwrites S. */
23414
23415 static int
23416 left_overwriting (struct glyph_string *s)
23417 {
23418 int i, k, x;
23419 struct glyph *glyphs = s->row->glyphs[s->area];
23420 int first = s->first_glyph - glyphs;
23421
23422 k = -1;
23423 x = 0;
23424 for (i = first - 1; i >= 0; --i)
23425 {
23426 int left, right;
23427 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23428 if (x + right > 0)
23429 k = i;
23430 x -= glyphs[i].pixel_width;
23431 }
23432
23433 return k;
23434 }
23435
23436
23437 /* Return the index of the last glyph following glyph string S that is
23438 overwritten by S because of S's right overhang. Value is -1 if
23439 no such glyph is found. */
23440
23441 static int
23442 right_overwritten (struct glyph_string *s)
23443 {
23444 int k = -1;
23445
23446 if (s->right_overhang)
23447 {
23448 int x = 0, i;
23449 struct glyph *glyphs = s->row->glyphs[s->area];
23450 int first = (s->first_glyph - glyphs
23451 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23452 int end = s->row->used[s->area];
23453
23454 for (i = first; i < end && s->right_overhang > x; ++i)
23455 x += glyphs[i].pixel_width;
23456
23457 k = i;
23458 }
23459
23460 return k;
23461 }
23462
23463
23464 /* Return the index of the last glyph following glyph string S that
23465 overwrites S because of its left overhang. Value is negative
23466 if no such glyph is found. */
23467
23468 static int
23469 right_overwriting (struct glyph_string *s)
23470 {
23471 int i, k, x;
23472 int end = s->row->used[s->area];
23473 struct glyph *glyphs = s->row->glyphs[s->area];
23474 int first = (s->first_glyph - glyphs
23475 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23476
23477 k = -1;
23478 x = 0;
23479 for (i = first; i < end; ++i)
23480 {
23481 int left, right;
23482 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23483 if (x - left < 0)
23484 k = i;
23485 x += glyphs[i].pixel_width;
23486 }
23487
23488 return k;
23489 }
23490
23491
23492 /* Set background width of glyph string S. START is the index of the
23493 first glyph following S. LAST_X is the right-most x-position + 1
23494 in the drawing area. */
23495
23496 static void
23497 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23498 {
23499 /* If the face of this glyph string has to be drawn to the end of
23500 the drawing area, set S->extends_to_end_of_line_p. */
23501
23502 if (start == s->row->used[s->area]
23503 && s->area == TEXT_AREA
23504 && ((s->row->fill_line_p
23505 && (s->hl == DRAW_NORMAL_TEXT
23506 || s->hl == DRAW_IMAGE_RAISED
23507 || s->hl == DRAW_IMAGE_SUNKEN))
23508 || s->hl == DRAW_MOUSE_FACE))
23509 s->extends_to_end_of_line_p = 1;
23510
23511 /* If S extends its face to the end of the line, set its
23512 background_width to the distance to the right edge of the drawing
23513 area. */
23514 if (s->extends_to_end_of_line_p)
23515 s->background_width = last_x - s->x + 1;
23516 else
23517 s->background_width = s->width;
23518 }
23519
23520
23521 /* Compute overhangs and x-positions for glyph string S and its
23522 predecessors, or successors. X is the starting x-position for S.
23523 BACKWARD_P non-zero means process predecessors. */
23524
23525 static void
23526 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23527 {
23528 if (backward_p)
23529 {
23530 while (s)
23531 {
23532 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23533 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23534 x -= s->width;
23535 s->x = x;
23536 s = s->prev;
23537 }
23538 }
23539 else
23540 {
23541 while (s)
23542 {
23543 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23544 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23545 s->x = x;
23546 x += s->width;
23547 s = s->next;
23548 }
23549 }
23550 }
23551
23552
23553
23554 /* The following macros are only called from draw_glyphs below.
23555 They reference the following parameters of that function directly:
23556 `w', `row', `area', and `overlap_p'
23557 as well as the following local variables:
23558 `s', `f', and `hdc' (in W32) */
23559
23560 #ifdef HAVE_NTGUI
23561 /* On W32, silently add local `hdc' variable to argument list of
23562 init_glyph_string. */
23563 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23564 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23565 #else
23566 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23567 init_glyph_string (s, char2b, w, row, area, start, hl)
23568 #endif
23569
23570 /* Add a glyph string for a stretch glyph to the list of strings
23571 between HEAD and TAIL. START is the index of the stretch glyph in
23572 row area AREA of glyph row ROW. END is the index of the last glyph
23573 in that glyph row area. X is the current output position assigned
23574 to the new glyph string constructed. HL overrides that face of the
23575 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23576 is the right-most x-position of the drawing area. */
23577
23578 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23579 and below -- keep them on one line. */
23580 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23581 do \
23582 { \
23583 s = alloca (sizeof *s); \
23584 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23585 START = fill_stretch_glyph_string (s, START, END); \
23586 append_glyph_string (&HEAD, &TAIL, s); \
23587 s->x = (X); \
23588 } \
23589 while (0)
23590
23591
23592 /* Add a glyph string for an image glyph to the list of strings
23593 between HEAD and TAIL. START is the index of the image glyph in
23594 row area AREA of glyph row ROW. END is the index of the last glyph
23595 in that glyph row area. X is the current output position assigned
23596 to the new glyph string constructed. HL overrides that face of the
23597 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23598 is the right-most x-position of the drawing area. */
23599
23600 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23601 do \
23602 { \
23603 s = alloca (sizeof *s); \
23604 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23605 fill_image_glyph_string (s); \
23606 append_glyph_string (&HEAD, &TAIL, s); \
23607 ++START; \
23608 s->x = (X); \
23609 } \
23610 while (0)
23611
23612
23613 /* Add a glyph string for a sequence of character glyphs to the list
23614 of strings between HEAD and TAIL. START is the index of the first
23615 glyph in row area AREA of glyph row ROW that is part of the new
23616 glyph string. END is the index of the last glyph in that glyph row
23617 area. X is the current output position assigned to the new glyph
23618 string constructed. HL overrides that face of the glyph; e.g. it
23619 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23620 right-most x-position of the drawing area. */
23621
23622 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23623 do \
23624 { \
23625 int face_id; \
23626 XChar2b *char2b; \
23627 \
23628 face_id = (row)->glyphs[area][START].face_id; \
23629 \
23630 s = alloca (sizeof *s); \
23631 char2b = alloca ((END - START) * sizeof *char2b); \
23632 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23633 append_glyph_string (&HEAD, &TAIL, s); \
23634 s->x = (X); \
23635 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23636 } \
23637 while (0)
23638
23639
23640 /* Add a glyph string for a composite sequence to the list of strings
23641 between HEAD and TAIL. START is the index of the first glyph in
23642 row area AREA of glyph row ROW that is part of the new glyph
23643 string. END is the index of the last glyph in that glyph row area.
23644 X is the current output position assigned to the new glyph string
23645 constructed. HL overrides that face of the glyph; e.g. it is
23646 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23647 x-position of the drawing area. */
23648
23649 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23650 do { \
23651 int face_id = (row)->glyphs[area][START].face_id; \
23652 struct face *base_face = FACE_FROM_ID (f, face_id); \
23653 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23654 struct composition *cmp = composition_table[cmp_id]; \
23655 XChar2b *char2b; \
23656 struct glyph_string *first_s = NULL; \
23657 int n; \
23658 \
23659 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23660 \
23661 /* Make glyph_strings for each glyph sequence that is drawable by \
23662 the same face, and append them to HEAD/TAIL. */ \
23663 for (n = 0; n < cmp->glyph_len;) \
23664 { \
23665 s = alloca (sizeof *s); \
23666 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23667 append_glyph_string (&(HEAD), &(TAIL), s); \
23668 s->cmp = cmp; \
23669 s->cmp_from = n; \
23670 s->x = (X); \
23671 if (n == 0) \
23672 first_s = s; \
23673 n = fill_composite_glyph_string (s, base_face, overlaps); \
23674 } \
23675 \
23676 ++START; \
23677 s = first_s; \
23678 } while (0)
23679
23680
23681 /* Add a glyph string for a glyph-string sequence to the list of strings
23682 between HEAD and TAIL. */
23683
23684 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23685 do { \
23686 int face_id; \
23687 XChar2b *char2b; \
23688 Lisp_Object gstring; \
23689 \
23690 face_id = (row)->glyphs[area][START].face_id; \
23691 gstring = (composition_gstring_from_id \
23692 ((row)->glyphs[area][START].u.cmp.id)); \
23693 s = alloca (sizeof *s); \
23694 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23695 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23696 append_glyph_string (&(HEAD), &(TAIL), s); \
23697 s->x = (X); \
23698 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23699 } while (0)
23700
23701
23702 /* Add a glyph string for a sequence of glyphless character's glyphs
23703 to the list of strings between HEAD and TAIL. The meanings of
23704 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23705
23706 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23707 do \
23708 { \
23709 int face_id; \
23710 \
23711 face_id = (row)->glyphs[area][START].face_id; \
23712 \
23713 s = alloca (sizeof *s); \
23714 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23715 append_glyph_string (&HEAD, &TAIL, s); \
23716 s->x = (X); \
23717 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23718 overlaps); \
23719 } \
23720 while (0)
23721
23722
23723 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23724 of AREA of glyph row ROW on window W between indices START and END.
23725 HL overrides the face for drawing glyph strings, e.g. it is
23726 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23727 x-positions of the drawing area.
23728
23729 This is an ugly monster macro construct because we must use alloca
23730 to allocate glyph strings (because draw_glyphs can be called
23731 asynchronously). */
23732
23733 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23734 do \
23735 { \
23736 HEAD = TAIL = NULL; \
23737 while (START < END) \
23738 { \
23739 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23740 switch (first_glyph->type) \
23741 { \
23742 case CHAR_GLYPH: \
23743 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23744 HL, X, LAST_X); \
23745 break; \
23746 \
23747 case COMPOSITE_GLYPH: \
23748 if (first_glyph->u.cmp.automatic) \
23749 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23750 HL, X, LAST_X); \
23751 else \
23752 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23753 HL, X, LAST_X); \
23754 break; \
23755 \
23756 case STRETCH_GLYPH: \
23757 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23758 HL, X, LAST_X); \
23759 break; \
23760 \
23761 case IMAGE_GLYPH: \
23762 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23763 HL, X, LAST_X); \
23764 break; \
23765 \
23766 case GLYPHLESS_GLYPH: \
23767 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23768 HL, X, LAST_X); \
23769 break; \
23770 \
23771 default: \
23772 emacs_abort (); \
23773 } \
23774 \
23775 if (s) \
23776 { \
23777 set_glyph_string_background_width (s, START, LAST_X); \
23778 (X) += s->width; \
23779 } \
23780 } \
23781 } while (0)
23782
23783
23784 /* Draw glyphs between START and END in AREA of ROW on window W,
23785 starting at x-position X. X is relative to AREA in W. HL is a
23786 face-override with the following meaning:
23787
23788 DRAW_NORMAL_TEXT draw normally
23789 DRAW_CURSOR draw in cursor face
23790 DRAW_MOUSE_FACE draw in mouse face.
23791 DRAW_INVERSE_VIDEO draw in mode line face
23792 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23793 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23794
23795 If OVERLAPS is non-zero, draw only the foreground of characters and
23796 clip to the physical height of ROW. Non-zero value also defines
23797 the overlapping part to be drawn:
23798
23799 OVERLAPS_PRED overlap with preceding rows
23800 OVERLAPS_SUCC overlap with succeeding rows
23801 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23802 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23803
23804 Value is the x-position reached, relative to AREA of W. */
23805
23806 static int
23807 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23808 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23809 enum draw_glyphs_face hl, int overlaps)
23810 {
23811 struct glyph_string *head, *tail;
23812 struct glyph_string *s;
23813 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23814 int i, j, x_reached, last_x, area_left = 0;
23815 struct frame *f = XFRAME (WINDOW_FRAME (w));
23816 DECLARE_HDC (hdc);
23817
23818 ALLOCATE_HDC (hdc, f);
23819
23820 /* Let's rather be paranoid than getting a SEGV. */
23821 end = min (end, row->used[area]);
23822 start = clip_to_bounds (0, start, end);
23823
23824 /* Translate X to frame coordinates. Set last_x to the right
23825 end of the drawing area. */
23826 if (row->full_width_p)
23827 {
23828 /* X is relative to the left edge of W, without scroll bars
23829 or fringes. */
23830 area_left = WINDOW_LEFT_EDGE_X (w);
23831 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23832 }
23833 else
23834 {
23835 area_left = window_box_left (w, area);
23836 last_x = area_left + window_box_width (w, area);
23837 }
23838 x += area_left;
23839
23840 /* Build a doubly-linked list of glyph_string structures between
23841 head and tail from what we have to draw. Note that the macro
23842 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23843 the reason we use a separate variable `i'. */
23844 i = start;
23845 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23846 if (tail)
23847 x_reached = tail->x + tail->background_width;
23848 else
23849 x_reached = x;
23850
23851 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23852 the row, redraw some glyphs in front or following the glyph
23853 strings built above. */
23854 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23855 {
23856 struct glyph_string *h, *t;
23857 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23858 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23859 int check_mouse_face = 0;
23860 int dummy_x = 0;
23861
23862 /* If mouse highlighting is on, we may need to draw adjacent
23863 glyphs using mouse-face highlighting. */
23864 if (area == TEXT_AREA && row->mouse_face_p
23865 && hlinfo->mouse_face_beg_row >= 0
23866 && hlinfo->mouse_face_end_row >= 0)
23867 {
23868 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
23869
23870 if (row_vpos >= hlinfo->mouse_face_beg_row
23871 && row_vpos <= hlinfo->mouse_face_end_row)
23872 {
23873 check_mouse_face = 1;
23874 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
23875 ? hlinfo->mouse_face_beg_col : 0;
23876 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
23877 ? hlinfo->mouse_face_end_col
23878 : row->used[TEXT_AREA];
23879 }
23880 }
23881
23882 /* Compute overhangs for all glyph strings. */
23883 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23884 for (s = head; s; s = s->next)
23885 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23886
23887 /* Prepend glyph strings for glyphs in front of the first glyph
23888 string that are overwritten because of the first glyph
23889 string's left overhang. The background of all strings
23890 prepended must be drawn because the first glyph string
23891 draws over it. */
23892 i = left_overwritten (head);
23893 if (i >= 0)
23894 {
23895 enum draw_glyphs_face overlap_hl;
23896
23897 /* If this row contains mouse highlighting, attempt to draw
23898 the overlapped glyphs with the correct highlight. This
23899 code fails if the overlap encompasses more than one glyph
23900 and mouse-highlight spans only some of these glyphs.
23901 However, making it work perfectly involves a lot more
23902 code, and I don't know if the pathological case occurs in
23903 practice, so we'll stick to this for now. --- cyd */
23904 if (check_mouse_face
23905 && mouse_beg_col < start && mouse_end_col > i)
23906 overlap_hl = DRAW_MOUSE_FACE;
23907 else
23908 overlap_hl = DRAW_NORMAL_TEXT;
23909
23910 j = i;
23911 BUILD_GLYPH_STRINGS (j, start, h, t,
23912 overlap_hl, dummy_x, last_x);
23913 start = i;
23914 compute_overhangs_and_x (t, head->x, 1);
23915 prepend_glyph_string_lists (&head, &tail, h, t);
23916 clip_head = head;
23917 }
23918
23919 /* Prepend glyph strings for glyphs in front of the first glyph
23920 string that overwrite that glyph string because of their
23921 right overhang. For these strings, only the foreground must
23922 be drawn, because it draws over the glyph string at `head'.
23923 The background must not be drawn because this would overwrite
23924 right overhangs of preceding glyphs for which no glyph
23925 strings exist. */
23926 i = left_overwriting (head);
23927 if (i >= 0)
23928 {
23929 enum draw_glyphs_face overlap_hl;
23930
23931 if (check_mouse_face
23932 && mouse_beg_col < start && mouse_end_col > i)
23933 overlap_hl = DRAW_MOUSE_FACE;
23934 else
23935 overlap_hl = DRAW_NORMAL_TEXT;
23936
23937 clip_head = head;
23938 BUILD_GLYPH_STRINGS (i, start, h, t,
23939 overlap_hl, dummy_x, last_x);
23940 for (s = h; s; s = s->next)
23941 s->background_filled_p = 1;
23942 compute_overhangs_and_x (t, head->x, 1);
23943 prepend_glyph_string_lists (&head, &tail, h, t);
23944 }
23945
23946 /* Append glyphs strings for glyphs following the last glyph
23947 string tail that are overwritten by tail. The background of
23948 these strings has to be drawn because tail's foreground draws
23949 over it. */
23950 i = right_overwritten (tail);
23951 if (i >= 0)
23952 {
23953 enum draw_glyphs_face overlap_hl;
23954
23955 if (check_mouse_face
23956 && mouse_beg_col < i && mouse_end_col > end)
23957 overlap_hl = DRAW_MOUSE_FACE;
23958 else
23959 overlap_hl = DRAW_NORMAL_TEXT;
23960
23961 BUILD_GLYPH_STRINGS (end, i, h, t,
23962 overlap_hl, x, last_x);
23963 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23964 we don't have `end = i;' here. */
23965 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23966 append_glyph_string_lists (&head, &tail, h, t);
23967 clip_tail = tail;
23968 }
23969
23970 /* Append glyph strings for glyphs following the last glyph
23971 string tail that overwrite tail. The foreground of such
23972 glyphs has to be drawn because it writes into the background
23973 of tail. The background must not be drawn because it could
23974 paint over the foreground of following glyphs. */
23975 i = right_overwriting (tail);
23976 if (i >= 0)
23977 {
23978 enum draw_glyphs_face overlap_hl;
23979 if (check_mouse_face
23980 && mouse_beg_col < i && mouse_end_col > end)
23981 overlap_hl = DRAW_MOUSE_FACE;
23982 else
23983 overlap_hl = DRAW_NORMAL_TEXT;
23984
23985 clip_tail = tail;
23986 i++; /* We must include the Ith glyph. */
23987 BUILD_GLYPH_STRINGS (end, i, h, t,
23988 overlap_hl, x, last_x);
23989 for (s = h; s; s = s->next)
23990 s->background_filled_p = 1;
23991 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23992 append_glyph_string_lists (&head, &tail, h, t);
23993 }
23994 if (clip_head || clip_tail)
23995 for (s = head; s; s = s->next)
23996 {
23997 s->clip_head = clip_head;
23998 s->clip_tail = clip_tail;
23999 }
24000 }
24001
24002 /* Draw all strings. */
24003 for (s = head; s; s = s->next)
24004 FRAME_RIF (f)->draw_glyph_string (s);
24005
24006 #ifndef HAVE_NS
24007 /* When focus a sole frame and move horizontally, this sets on_p to 0
24008 causing a failure to erase prev cursor position. */
24009 if (area == TEXT_AREA
24010 && !row->full_width_p
24011 /* When drawing overlapping rows, only the glyph strings'
24012 foreground is drawn, which doesn't erase a cursor
24013 completely. */
24014 && !overlaps)
24015 {
24016 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
24017 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
24018 : (tail ? tail->x + tail->background_width : x));
24019 x0 -= area_left;
24020 x1 -= area_left;
24021
24022 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
24023 row->y, MATRIX_ROW_BOTTOM_Y (row));
24024 }
24025 #endif
24026
24027 /* Value is the x-position up to which drawn, relative to AREA of W.
24028 This doesn't include parts drawn because of overhangs. */
24029 if (row->full_width_p)
24030 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
24031 else
24032 x_reached -= area_left;
24033
24034 RELEASE_HDC (hdc, f);
24035
24036 return x_reached;
24037 }
24038
24039 /* Expand row matrix if too narrow. Don't expand if area
24040 is not present. */
24041
24042 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
24043 { \
24044 if (!fonts_changed_p \
24045 && (it->glyph_row->glyphs[area] \
24046 < it->glyph_row->glyphs[area + 1])) \
24047 { \
24048 it->w->ncols_scale_factor++; \
24049 fonts_changed_p = 1; \
24050 } \
24051 }
24052
24053 /* Store one glyph for IT->char_to_display in IT->glyph_row.
24054 Called from x_produce_glyphs when IT->glyph_row is non-null. */
24055
24056 static void
24057 append_glyph (struct it *it)
24058 {
24059 struct glyph *glyph;
24060 enum glyph_row_area area = it->area;
24061
24062 eassert (it->glyph_row);
24063 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
24064
24065 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24066 if (glyph < it->glyph_row->glyphs[area + 1])
24067 {
24068 /* If the glyph row is reversed, we need to prepend the glyph
24069 rather than append it. */
24070 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24071 {
24072 struct glyph *g;
24073
24074 /* Make room for the additional glyph. */
24075 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24076 g[1] = *g;
24077 glyph = it->glyph_row->glyphs[area];
24078 }
24079 glyph->charpos = CHARPOS (it->position);
24080 glyph->object = it->object;
24081 if (it->pixel_width > 0)
24082 {
24083 glyph->pixel_width = it->pixel_width;
24084 glyph->padding_p = 0;
24085 }
24086 else
24087 {
24088 /* Assure at least 1-pixel width. Otherwise, cursor can't
24089 be displayed correctly. */
24090 glyph->pixel_width = 1;
24091 glyph->padding_p = 1;
24092 }
24093 glyph->ascent = it->ascent;
24094 glyph->descent = it->descent;
24095 glyph->voffset = it->voffset;
24096 glyph->type = CHAR_GLYPH;
24097 glyph->avoid_cursor_p = it->avoid_cursor_p;
24098 glyph->multibyte_p = it->multibyte_p;
24099 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24100 {
24101 /* In R2L rows, the left and the right box edges need to be
24102 drawn in reverse direction. */
24103 glyph->right_box_line_p = it->start_of_box_run_p;
24104 glyph->left_box_line_p = it->end_of_box_run_p;
24105 }
24106 else
24107 {
24108 glyph->left_box_line_p = it->start_of_box_run_p;
24109 glyph->right_box_line_p = it->end_of_box_run_p;
24110 }
24111 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24112 || it->phys_descent > it->descent);
24113 glyph->glyph_not_available_p = it->glyph_not_available_p;
24114 glyph->face_id = it->face_id;
24115 glyph->u.ch = it->char_to_display;
24116 glyph->slice.img = null_glyph_slice;
24117 glyph->font_type = FONT_TYPE_UNKNOWN;
24118 if (it->bidi_p)
24119 {
24120 glyph->resolved_level = it->bidi_it.resolved_level;
24121 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24122 emacs_abort ();
24123 glyph->bidi_type = it->bidi_it.type;
24124 }
24125 else
24126 {
24127 glyph->resolved_level = 0;
24128 glyph->bidi_type = UNKNOWN_BT;
24129 }
24130 ++it->glyph_row->used[area];
24131 }
24132 else
24133 IT_EXPAND_MATRIX_WIDTH (it, area);
24134 }
24135
24136 /* Store one glyph for the composition IT->cmp_it.id in
24137 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
24138 non-null. */
24139
24140 static void
24141 append_composite_glyph (struct it *it)
24142 {
24143 struct glyph *glyph;
24144 enum glyph_row_area area = it->area;
24145
24146 eassert (it->glyph_row);
24147
24148 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24149 if (glyph < it->glyph_row->glyphs[area + 1])
24150 {
24151 /* If the glyph row is reversed, we need to prepend the glyph
24152 rather than append it. */
24153 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
24154 {
24155 struct glyph *g;
24156
24157 /* Make room for the new glyph. */
24158 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
24159 g[1] = *g;
24160 glyph = it->glyph_row->glyphs[it->area];
24161 }
24162 glyph->charpos = it->cmp_it.charpos;
24163 glyph->object = it->object;
24164 glyph->pixel_width = it->pixel_width;
24165 glyph->ascent = it->ascent;
24166 glyph->descent = it->descent;
24167 glyph->voffset = it->voffset;
24168 glyph->type = COMPOSITE_GLYPH;
24169 if (it->cmp_it.ch < 0)
24170 {
24171 glyph->u.cmp.automatic = 0;
24172 glyph->u.cmp.id = it->cmp_it.id;
24173 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
24174 }
24175 else
24176 {
24177 glyph->u.cmp.automatic = 1;
24178 glyph->u.cmp.id = it->cmp_it.id;
24179 glyph->slice.cmp.from = it->cmp_it.from;
24180 glyph->slice.cmp.to = it->cmp_it.to - 1;
24181 }
24182 glyph->avoid_cursor_p = it->avoid_cursor_p;
24183 glyph->multibyte_p = it->multibyte_p;
24184 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24185 {
24186 /* In R2L rows, the left and the right box edges need to be
24187 drawn in reverse direction. */
24188 glyph->right_box_line_p = it->start_of_box_run_p;
24189 glyph->left_box_line_p = it->end_of_box_run_p;
24190 }
24191 else
24192 {
24193 glyph->left_box_line_p = it->start_of_box_run_p;
24194 glyph->right_box_line_p = it->end_of_box_run_p;
24195 }
24196 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24197 || it->phys_descent > it->descent);
24198 glyph->padding_p = 0;
24199 glyph->glyph_not_available_p = 0;
24200 glyph->face_id = it->face_id;
24201 glyph->font_type = FONT_TYPE_UNKNOWN;
24202 if (it->bidi_p)
24203 {
24204 glyph->resolved_level = it->bidi_it.resolved_level;
24205 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24206 emacs_abort ();
24207 glyph->bidi_type = it->bidi_it.type;
24208 }
24209 ++it->glyph_row->used[area];
24210 }
24211 else
24212 IT_EXPAND_MATRIX_WIDTH (it, area);
24213 }
24214
24215
24216 /* Change IT->ascent and IT->height according to the setting of
24217 IT->voffset. */
24218
24219 static void
24220 take_vertical_position_into_account (struct it *it)
24221 {
24222 if (it->voffset)
24223 {
24224 if (it->voffset < 0)
24225 /* Increase the ascent so that we can display the text higher
24226 in the line. */
24227 it->ascent -= it->voffset;
24228 else
24229 /* Increase the descent so that we can display the text lower
24230 in the line. */
24231 it->descent += it->voffset;
24232 }
24233 }
24234
24235
24236 /* Produce glyphs/get display metrics for the image IT is loaded with.
24237 See the description of struct display_iterator in dispextern.h for
24238 an overview of struct display_iterator. */
24239
24240 static void
24241 produce_image_glyph (struct it *it)
24242 {
24243 struct image *img;
24244 struct face *face;
24245 int glyph_ascent, crop;
24246 struct glyph_slice slice;
24247
24248 eassert (it->what == IT_IMAGE);
24249
24250 face = FACE_FROM_ID (it->f, it->face_id);
24251 eassert (face);
24252 /* Make sure X resources of the face is loaded. */
24253 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24254
24255 if (it->image_id < 0)
24256 {
24257 /* Fringe bitmap. */
24258 it->ascent = it->phys_ascent = 0;
24259 it->descent = it->phys_descent = 0;
24260 it->pixel_width = 0;
24261 it->nglyphs = 0;
24262 return;
24263 }
24264
24265 img = IMAGE_FROM_ID (it->f, it->image_id);
24266 eassert (img);
24267 /* Make sure X resources of the image is loaded. */
24268 prepare_image_for_display (it->f, img);
24269
24270 slice.x = slice.y = 0;
24271 slice.width = img->width;
24272 slice.height = img->height;
24273
24274 if (INTEGERP (it->slice.x))
24275 slice.x = XINT (it->slice.x);
24276 else if (FLOATP (it->slice.x))
24277 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
24278
24279 if (INTEGERP (it->slice.y))
24280 slice.y = XINT (it->slice.y);
24281 else if (FLOATP (it->slice.y))
24282 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
24283
24284 if (INTEGERP (it->slice.width))
24285 slice.width = XINT (it->slice.width);
24286 else if (FLOATP (it->slice.width))
24287 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
24288
24289 if (INTEGERP (it->slice.height))
24290 slice.height = XINT (it->slice.height);
24291 else if (FLOATP (it->slice.height))
24292 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
24293
24294 if (slice.x >= img->width)
24295 slice.x = img->width;
24296 if (slice.y >= img->height)
24297 slice.y = img->height;
24298 if (slice.x + slice.width >= img->width)
24299 slice.width = img->width - slice.x;
24300 if (slice.y + slice.height > img->height)
24301 slice.height = img->height - slice.y;
24302
24303 if (slice.width == 0 || slice.height == 0)
24304 return;
24305
24306 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
24307
24308 it->descent = slice.height - glyph_ascent;
24309 if (slice.y == 0)
24310 it->descent += img->vmargin;
24311 if (slice.y + slice.height == img->height)
24312 it->descent += img->vmargin;
24313 it->phys_descent = it->descent;
24314
24315 it->pixel_width = slice.width;
24316 if (slice.x == 0)
24317 it->pixel_width += img->hmargin;
24318 if (slice.x + slice.width == img->width)
24319 it->pixel_width += img->hmargin;
24320
24321 /* It's quite possible for images to have an ascent greater than
24322 their height, so don't get confused in that case. */
24323 if (it->descent < 0)
24324 it->descent = 0;
24325
24326 it->nglyphs = 1;
24327
24328 if (face->box != FACE_NO_BOX)
24329 {
24330 if (face->box_line_width > 0)
24331 {
24332 if (slice.y == 0)
24333 it->ascent += face->box_line_width;
24334 if (slice.y + slice.height == img->height)
24335 it->descent += face->box_line_width;
24336 }
24337
24338 if (it->start_of_box_run_p && slice.x == 0)
24339 it->pixel_width += eabs (face->box_line_width);
24340 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
24341 it->pixel_width += eabs (face->box_line_width);
24342 }
24343
24344 take_vertical_position_into_account (it);
24345
24346 /* Automatically crop wide image glyphs at right edge so we can
24347 draw the cursor on same display row. */
24348 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
24349 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
24350 {
24351 it->pixel_width -= crop;
24352 slice.width -= crop;
24353 }
24354
24355 if (it->glyph_row)
24356 {
24357 struct glyph *glyph;
24358 enum glyph_row_area area = it->area;
24359
24360 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24361 if (glyph < it->glyph_row->glyphs[area + 1])
24362 {
24363 glyph->charpos = CHARPOS (it->position);
24364 glyph->object = it->object;
24365 glyph->pixel_width = it->pixel_width;
24366 glyph->ascent = glyph_ascent;
24367 glyph->descent = it->descent;
24368 glyph->voffset = it->voffset;
24369 glyph->type = IMAGE_GLYPH;
24370 glyph->avoid_cursor_p = it->avoid_cursor_p;
24371 glyph->multibyte_p = it->multibyte_p;
24372 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24373 {
24374 /* In R2L rows, the left and the right box edges need to be
24375 drawn in reverse direction. */
24376 glyph->right_box_line_p = it->start_of_box_run_p;
24377 glyph->left_box_line_p = it->end_of_box_run_p;
24378 }
24379 else
24380 {
24381 glyph->left_box_line_p = it->start_of_box_run_p;
24382 glyph->right_box_line_p = it->end_of_box_run_p;
24383 }
24384 glyph->overlaps_vertically_p = 0;
24385 glyph->padding_p = 0;
24386 glyph->glyph_not_available_p = 0;
24387 glyph->face_id = it->face_id;
24388 glyph->u.img_id = img->id;
24389 glyph->slice.img = slice;
24390 glyph->font_type = FONT_TYPE_UNKNOWN;
24391 if (it->bidi_p)
24392 {
24393 glyph->resolved_level = it->bidi_it.resolved_level;
24394 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24395 emacs_abort ();
24396 glyph->bidi_type = it->bidi_it.type;
24397 }
24398 ++it->glyph_row->used[area];
24399 }
24400 else
24401 IT_EXPAND_MATRIX_WIDTH (it, area);
24402 }
24403 }
24404
24405
24406 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
24407 of the glyph, WIDTH and HEIGHT are the width and height of the
24408 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
24409
24410 static void
24411 append_stretch_glyph (struct it *it, Lisp_Object object,
24412 int width, int height, int ascent)
24413 {
24414 struct glyph *glyph;
24415 enum glyph_row_area area = it->area;
24416
24417 eassert (ascent >= 0 && ascent <= height);
24418
24419 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24420 if (glyph < it->glyph_row->glyphs[area + 1])
24421 {
24422 /* If the glyph row is reversed, we need to prepend the glyph
24423 rather than append it. */
24424 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24425 {
24426 struct glyph *g;
24427
24428 /* Make room for the additional glyph. */
24429 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24430 g[1] = *g;
24431 glyph = it->glyph_row->glyphs[area];
24432 }
24433 glyph->charpos = CHARPOS (it->position);
24434 glyph->object = object;
24435 glyph->pixel_width = width;
24436 glyph->ascent = ascent;
24437 glyph->descent = height - ascent;
24438 glyph->voffset = it->voffset;
24439 glyph->type = STRETCH_GLYPH;
24440 glyph->avoid_cursor_p = it->avoid_cursor_p;
24441 glyph->multibyte_p = it->multibyte_p;
24442 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24443 {
24444 /* In R2L rows, the left and the right box edges need to be
24445 drawn in reverse direction. */
24446 glyph->right_box_line_p = it->start_of_box_run_p;
24447 glyph->left_box_line_p = it->end_of_box_run_p;
24448 }
24449 else
24450 {
24451 glyph->left_box_line_p = it->start_of_box_run_p;
24452 glyph->right_box_line_p = it->end_of_box_run_p;
24453 }
24454 glyph->overlaps_vertically_p = 0;
24455 glyph->padding_p = 0;
24456 glyph->glyph_not_available_p = 0;
24457 glyph->face_id = it->face_id;
24458 glyph->u.stretch.ascent = ascent;
24459 glyph->u.stretch.height = height;
24460 glyph->slice.img = null_glyph_slice;
24461 glyph->font_type = FONT_TYPE_UNKNOWN;
24462 if (it->bidi_p)
24463 {
24464 glyph->resolved_level = it->bidi_it.resolved_level;
24465 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24466 emacs_abort ();
24467 glyph->bidi_type = it->bidi_it.type;
24468 }
24469 else
24470 {
24471 glyph->resolved_level = 0;
24472 glyph->bidi_type = UNKNOWN_BT;
24473 }
24474 ++it->glyph_row->used[area];
24475 }
24476 else
24477 IT_EXPAND_MATRIX_WIDTH (it, area);
24478 }
24479
24480 #endif /* HAVE_WINDOW_SYSTEM */
24481
24482 /* Produce a stretch glyph for iterator IT. IT->object is the value
24483 of the glyph property displayed. The value must be a list
24484 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24485 being recognized:
24486
24487 1. `:width WIDTH' specifies that the space should be WIDTH *
24488 canonical char width wide. WIDTH may be an integer or floating
24489 point number.
24490
24491 2. `:relative-width FACTOR' specifies that the width of the stretch
24492 should be computed from the width of the first character having the
24493 `glyph' property, and should be FACTOR times that width.
24494
24495 3. `:align-to HPOS' specifies that the space should be wide enough
24496 to reach HPOS, a value in canonical character units.
24497
24498 Exactly one of the above pairs must be present.
24499
24500 4. `:height HEIGHT' specifies that the height of the stretch produced
24501 should be HEIGHT, measured in canonical character units.
24502
24503 5. `:relative-height FACTOR' specifies that the height of the
24504 stretch should be FACTOR times the height of the characters having
24505 the glyph property.
24506
24507 Either none or exactly one of 4 or 5 must be present.
24508
24509 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24510 of the stretch should be used for the ascent of the stretch.
24511 ASCENT must be in the range 0 <= ASCENT <= 100. */
24512
24513 void
24514 produce_stretch_glyph (struct it *it)
24515 {
24516 /* (space :width WIDTH :height HEIGHT ...) */
24517 Lisp_Object prop, plist;
24518 int width = 0, height = 0, align_to = -1;
24519 int zero_width_ok_p = 0;
24520 double tem;
24521 struct font *font = NULL;
24522
24523 #ifdef HAVE_WINDOW_SYSTEM
24524 int ascent = 0;
24525 int zero_height_ok_p = 0;
24526
24527 if (FRAME_WINDOW_P (it->f))
24528 {
24529 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24530 font = face->font ? face->font : FRAME_FONT (it->f);
24531 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24532 }
24533 #endif
24534
24535 /* List should start with `space'. */
24536 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24537 plist = XCDR (it->object);
24538
24539 /* Compute the width of the stretch. */
24540 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24541 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24542 {
24543 /* Absolute width `:width WIDTH' specified and valid. */
24544 zero_width_ok_p = 1;
24545 width = (int)tem;
24546 }
24547 #ifdef HAVE_WINDOW_SYSTEM
24548 else if (FRAME_WINDOW_P (it->f)
24549 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24550 {
24551 /* Relative width `:relative-width FACTOR' specified and valid.
24552 Compute the width of the characters having the `glyph'
24553 property. */
24554 struct it it2;
24555 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24556
24557 it2 = *it;
24558 if (it->multibyte_p)
24559 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24560 else
24561 {
24562 it2.c = it2.char_to_display = *p, it2.len = 1;
24563 if (! ASCII_CHAR_P (it2.c))
24564 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24565 }
24566
24567 it2.glyph_row = NULL;
24568 it2.what = IT_CHARACTER;
24569 x_produce_glyphs (&it2);
24570 width = NUMVAL (prop) * it2.pixel_width;
24571 }
24572 #endif /* HAVE_WINDOW_SYSTEM */
24573 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24574 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24575 {
24576 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24577 align_to = (align_to < 0
24578 ? 0
24579 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24580 else if (align_to < 0)
24581 align_to = window_box_left_offset (it->w, TEXT_AREA);
24582 width = max (0, (int)tem + align_to - it->current_x);
24583 zero_width_ok_p = 1;
24584 }
24585 else
24586 /* Nothing specified -> width defaults to canonical char width. */
24587 width = FRAME_COLUMN_WIDTH (it->f);
24588
24589 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24590 width = 1;
24591
24592 #ifdef HAVE_WINDOW_SYSTEM
24593 /* Compute height. */
24594 if (FRAME_WINDOW_P (it->f))
24595 {
24596 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24597 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24598 {
24599 height = (int)tem;
24600 zero_height_ok_p = 1;
24601 }
24602 else if (prop = Fplist_get (plist, QCrelative_height),
24603 NUMVAL (prop) > 0)
24604 height = FONT_HEIGHT (font) * NUMVAL (prop);
24605 else
24606 height = FONT_HEIGHT (font);
24607
24608 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24609 height = 1;
24610
24611 /* Compute percentage of height used for ascent. If
24612 `:ascent ASCENT' is present and valid, use that. Otherwise,
24613 derive the ascent from the font in use. */
24614 if (prop = Fplist_get (plist, QCascent),
24615 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24616 ascent = height * NUMVAL (prop) / 100.0;
24617 else if (!NILP (prop)
24618 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24619 ascent = min (max (0, (int)tem), height);
24620 else
24621 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24622 }
24623 else
24624 #endif /* HAVE_WINDOW_SYSTEM */
24625 height = 1;
24626
24627 if (width > 0 && it->line_wrap != TRUNCATE
24628 && it->current_x + width > it->last_visible_x)
24629 {
24630 width = it->last_visible_x - it->current_x;
24631 #ifdef HAVE_WINDOW_SYSTEM
24632 /* Subtract one more pixel from the stretch width, but only on
24633 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24634 width -= FRAME_WINDOW_P (it->f);
24635 #endif
24636 }
24637
24638 if (width > 0 && height > 0 && it->glyph_row)
24639 {
24640 Lisp_Object o_object = it->object;
24641 Lisp_Object object = it->stack[it->sp - 1].string;
24642 int n = width;
24643
24644 if (!STRINGP (object))
24645 object = it->w->contents;
24646 #ifdef HAVE_WINDOW_SYSTEM
24647 if (FRAME_WINDOW_P (it->f))
24648 append_stretch_glyph (it, object, width, height, ascent);
24649 else
24650 #endif
24651 {
24652 it->object = object;
24653 it->char_to_display = ' ';
24654 it->pixel_width = it->len = 1;
24655 while (n--)
24656 tty_append_glyph (it);
24657 it->object = o_object;
24658 }
24659 }
24660
24661 it->pixel_width = width;
24662 #ifdef HAVE_WINDOW_SYSTEM
24663 if (FRAME_WINDOW_P (it->f))
24664 {
24665 it->ascent = it->phys_ascent = ascent;
24666 it->descent = it->phys_descent = height - it->ascent;
24667 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24668 take_vertical_position_into_account (it);
24669 }
24670 else
24671 #endif
24672 it->nglyphs = width;
24673 }
24674
24675 /* Get information about special display element WHAT in an
24676 environment described by IT. WHAT is one of IT_TRUNCATION or
24677 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24678 non-null glyph_row member. This function ensures that fields like
24679 face_id, c, len of IT are left untouched. */
24680
24681 static void
24682 produce_special_glyphs (struct it *it, enum display_element_type what)
24683 {
24684 struct it temp_it;
24685 Lisp_Object gc;
24686 GLYPH glyph;
24687
24688 temp_it = *it;
24689 temp_it.object = make_number (0);
24690 memset (&temp_it.current, 0, sizeof temp_it.current);
24691
24692 if (what == IT_CONTINUATION)
24693 {
24694 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24695 if (it->bidi_it.paragraph_dir == R2L)
24696 SET_GLYPH_FROM_CHAR (glyph, '/');
24697 else
24698 SET_GLYPH_FROM_CHAR (glyph, '\\');
24699 if (it->dp
24700 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24701 {
24702 /* FIXME: Should we mirror GC for R2L lines? */
24703 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24704 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24705 }
24706 }
24707 else if (what == IT_TRUNCATION)
24708 {
24709 /* Truncation glyph. */
24710 SET_GLYPH_FROM_CHAR (glyph, '$');
24711 if (it->dp
24712 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24713 {
24714 /* FIXME: Should we mirror GC for R2L lines? */
24715 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24716 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24717 }
24718 }
24719 else
24720 emacs_abort ();
24721
24722 #ifdef HAVE_WINDOW_SYSTEM
24723 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24724 is turned off, we precede the truncation/continuation glyphs by a
24725 stretch glyph whose width is computed such that these special
24726 glyphs are aligned at the window margin, even when very different
24727 fonts are used in different glyph rows. */
24728 if (FRAME_WINDOW_P (temp_it.f)
24729 /* init_iterator calls this with it->glyph_row == NULL, and it
24730 wants only the pixel width of the truncation/continuation
24731 glyphs. */
24732 && temp_it.glyph_row
24733 /* insert_left_trunc_glyphs calls us at the beginning of the
24734 row, and it has its own calculation of the stretch glyph
24735 width. */
24736 && temp_it.glyph_row->used[TEXT_AREA] > 0
24737 && (temp_it.glyph_row->reversed_p
24738 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24739 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24740 {
24741 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24742
24743 if (stretch_width > 0)
24744 {
24745 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24746 struct font *font =
24747 face->font ? face->font : FRAME_FONT (temp_it.f);
24748 int stretch_ascent =
24749 (((temp_it.ascent + temp_it.descent)
24750 * FONT_BASE (font)) / FONT_HEIGHT (font));
24751
24752 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
24753 temp_it.ascent + temp_it.descent,
24754 stretch_ascent);
24755 }
24756 }
24757 #endif
24758
24759 temp_it.dp = NULL;
24760 temp_it.what = IT_CHARACTER;
24761 temp_it.len = 1;
24762 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24763 temp_it.face_id = GLYPH_FACE (glyph);
24764 temp_it.len = CHAR_BYTES (temp_it.c);
24765
24766 PRODUCE_GLYPHS (&temp_it);
24767 it->pixel_width = temp_it.pixel_width;
24768 it->nglyphs = temp_it.pixel_width;
24769 }
24770
24771 #ifdef HAVE_WINDOW_SYSTEM
24772
24773 /* Calculate line-height and line-spacing properties.
24774 An integer value specifies explicit pixel value.
24775 A float value specifies relative value to current face height.
24776 A cons (float . face-name) specifies relative value to
24777 height of specified face font.
24778
24779 Returns height in pixels, or nil. */
24780
24781
24782 static Lisp_Object
24783 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24784 int boff, int override)
24785 {
24786 Lisp_Object face_name = Qnil;
24787 int ascent, descent, height;
24788
24789 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24790 return val;
24791
24792 if (CONSP (val))
24793 {
24794 face_name = XCAR (val);
24795 val = XCDR (val);
24796 if (!NUMBERP (val))
24797 val = make_number (1);
24798 if (NILP (face_name))
24799 {
24800 height = it->ascent + it->descent;
24801 goto scale;
24802 }
24803 }
24804
24805 if (NILP (face_name))
24806 {
24807 font = FRAME_FONT (it->f);
24808 boff = FRAME_BASELINE_OFFSET (it->f);
24809 }
24810 else if (EQ (face_name, Qt))
24811 {
24812 override = 0;
24813 }
24814 else
24815 {
24816 int face_id;
24817 struct face *face;
24818
24819 face_id = lookup_named_face (it->f, face_name, 0);
24820 if (face_id < 0)
24821 return make_number (-1);
24822
24823 face = FACE_FROM_ID (it->f, face_id);
24824 font = face->font;
24825 if (font == NULL)
24826 return make_number (-1);
24827 boff = font->baseline_offset;
24828 if (font->vertical_centering)
24829 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24830 }
24831
24832 ascent = FONT_BASE (font) + boff;
24833 descent = FONT_DESCENT (font) - boff;
24834
24835 if (override)
24836 {
24837 it->override_ascent = ascent;
24838 it->override_descent = descent;
24839 it->override_boff = boff;
24840 }
24841
24842 height = ascent + descent;
24843
24844 scale:
24845 if (FLOATP (val))
24846 height = (int)(XFLOAT_DATA (val) * height);
24847 else if (INTEGERP (val))
24848 height *= XINT (val);
24849
24850 return make_number (height);
24851 }
24852
24853
24854 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24855 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24856 and only if this is for a character for which no font was found.
24857
24858 If the display method (it->glyphless_method) is
24859 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24860 length of the acronym or the hexadecimal string, UPPER_XOFF and
24861 UPPER_YOFF are pixel offsets for the upper part of the string,
24862 LOWER_XOFF and LOWER_YOFF are for the lower part.
24863
24864 For the other display methods, LEN through LOWER_YOFF are zero. */
24865
24866 static void
24867 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24868 short upper_xoff, short upper_yoff,
24869 short lower_xoff, short lower_yoff)
24870 {
24871 struct glyph *glyph;
24872 enum glyph_row_area area = it->area;
24873
24874 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24875 if (glyph < it->glyph_row->glyphs[area + 1])
24876 {
24877 /* If the glyph row is reversed, we need to prepend the glyph
24878 rather than append it. */
24879 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24880 {
24881 struct glyph *g;
24882
24883 /* Make room for the additional glyph. */
24884 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24885 g[1] = *g;
24886 glyph = it->glyph_row->glyphs[area];
24887 }
24888 glyph->charpos = CHARPOS (it->position);
24889 glyph->object = it->object;
24890 glyph->pixel_width = it->pixel_width;
24891 glyph->ascent = it->ascent;
24892 glyph->descent = it->descent;
24893 glyph->voffset = it->voffset;
24894 glyph->type = GLYPHLESS_GLYPH;
24895 glyph->u.glyphless.method = it->glyphless_method;
24896 glyph->u.glyphless.for_no_font = for_no_font;
24897 glyph->u.glyphless.len = len;
24898 glyph->u.glyphless.ch = it->c;
24899 glyph->slice.glyphless.upper_xoff = upper_xoff;
24900 glyph->slice.glyphless.upper_yoff = upper_yoff;
24901 glyph->slice.glyphless.lower_xoff = lower_xoff;
24902 glyph->slice.glyphless.lower_yoff = lower_yoff;
24903 glyph->avoid_cursor_p = it->avoid_cursor_p;
24904 glyph->multibyte_p = it->multibyte_p;
24905 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24906 {
24907 /* In R2L rows, the left and the right box edges need to be
24908 drawn in reverse direction. */
24909 glyph->right_box_line_p = it->start_of_box_run_p;
24910 glyph->left_box_line_p = it->end_of_box_run_p;
24911 }
24912 else
24913 {
24914 glyph->left_box_line_p = it->start_of_box_run_p;
24915 glyph->right_box_line_p = it->end_of_box_run_p;
24916 }
24917 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24918 || it->phys_descent > it->descent);
24919 glyph->padding_p = 0;
24920 glyph->glyph_not_available_p = 0;
24921 glyph->face_id = face_id;
24922 glyph->font_type = FONT_TYPE_UNKNOWN;
24923 if (it->bidi_p)
24924 {
24925 glyph->resolved_level = it->bidi_it.resolved_level;
24926 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24927 emacs_abort ();
24928 glyph->bidi_type = it->bidi_it.type;
24929 }
24930 ++it->glyph_row->used[area];
24931 }
24932 else
24933 IT_EXPAND_MATRIX_WIDTH (it, area);
24934 }
24935
24936
24937 /* Produce a glyph for a glyphless character for iterator IT.
24938 IT->glyphless_method specifies which method to use for displaying
24939 the character. See the description of enum
24940 glyphless_display_method in dispextern.h for the detail.
24941
24942 FOR_NO_FONT is nonzero if and only if this is for a character for
24943 which no font was found. ACRONYM, if non-nil, is an acronym string
24944 for the character. */
24945
24946 static void
24947 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24948 {
24949 int face_id;
24950 struct face *face;
24951 struct font *font;
24952 int base_width, base_height, width, height;
24953 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24954 int len;
24955
24956 /* Get the metrics of the base font. We always refer to the current
24957 ASCII face. */
24958 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24959 font = face->font ? face->font : FRAME_FONT (it->f);
24960 it->ascent = FONT_BASE (font) + font->baseline_offset;
24961 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24962 base_height = it->ascent + it->descent;
24963 base_width = font->average_width;
24964
24965 /* Get a face ID for the glyph by utilizing a cache (the same way as
24966 done for `escape-glyph' in get_next_display_element). */
24967 if (it->f == last_glyphless_glyph_frame
24968 && it->face_id == last_glyphless_glyph_face_id)
24969 {
24970 face_id = last_glyphless_glyph_merged_face_id;
24971 }
24972 else
24973 {
24974 /* Merge the `glyphless-char' face into the current face. */
24975 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
24976 last_glyphless_glyph_frame = it->f;
24977 last_glyphless_glyph_face_id = it->face_id;
24978 last_glyphless_glyph_merged_face_id = face_id;
24979 }
24980
24981 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24982 {
24983 it->pixel_width = THIN_SPACE_WIDTH;
24984 len = 0;
24985 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24986 }
24987 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24988 {
24989 width = CHAR_WIDTH (it->c);
24990 if (width == 0)
24991 width = 1;
24992 else if (width > 4)
24993 width = 4;
24994 it->pixel_width = base_width * width;
24995 len = 0;
24996 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24997 }
24998 else
24999 {
25000 char buf[7];
25001 const char *str;
25002 unsigned int code[6];
25003 int upper_len;
25004 int ascent, descent;
25005 struct font_metrics metrics_upper, metrics_lower;
25006
25007 face = FACE_FROM_ID (it->f, face_id);
25008 font = face->font ? face->font : FRAME_FONT (it->f);
25009 PREPARE_FACE_FOR_DISPLAY (it->f, face);
25010
25011 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
25012 {
25013 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
25014 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
25015 if (CONSP (acronym))
25016 acronym = XCAR (acronym);
25017 str = STRINGP (acronym) ? SSDATA (acronym) : "";
25018 }
25019 else
25020 {
25021 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
25022 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
25023 str = buf;
25024 }
25025 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
25026 code[len] = font->driver->encode_char (font, str[len]);
25027 upper_len = (len + 1) / 2;
25028 font->driver->text_extents (font, code, upper_len,
25029 &metrics_upper);
25030 font->driver->text_extents (font, code + upper_len, len - upper_len,
25031 &metrics_lower);
25032
25033
25034
25035 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
25036 width = max (metrics_upper.width, metrics_lower.width) + 4;
25037 upper_xoff = upper_yoff = 2; /* the typical case */
25038 if (base_width >= width)
25039 {
25040 /* Align the upper to the left, the lower to the right. */
25041 it->pixel_width = base_width;
25042 lower_xoff = base_width - 2 - metrics_lower.width;
25043 }
25044 else
25045 {
25046 /* Center the shorter one. */
25047 it->pixel_width = width;
25048 if (metrics_upper.width >= metrics_lower.width)
25049 lower_xoff = (width - metrics_lower.width) / 2;
25050 else
25051 {
25052 /* FIXME: This code doesn't look right. It formerly was
25053 missing the "lower_xoff = 0;", which couldn't have
25054 been right since it left lower_xoff uninitialized. */
25055 lower_xoff = 0;
25056 upper_xoff = (width - metrics_upper.width) / 2;
25057 }
25058 }
25059
25060 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
25061 top, bottom, and between upper and lower strings. */
25062 height = (metrics_upper.ascent + metrics_upper.descent
25063 + metrics_lower.ascent + metrics_lower.descent) + 5;
25064 /* Center vertically.
25065 H:base_height, D:base_descent
25066 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
25067
25068 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
25069 descent = D - H/2 + h/2;
25070 lower_yoff = descent - 2 - ld;
25071 upper_yoff = lower_yoff - la - 1 - ud; */
25072 ascent = - (it->descent - (base_height + height + 1) / 2);
25073 descent = it->descent - (base_height - height) / 2;
25074 lower_yoff = descent - 2 - metrics_lower.descent;
25075 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
25076 - metrics_upper.descent);
25077 /* Don't make the height shorter than the base height. */
25078 if (height > base_height)
25079 {
25080 it->ascent = ascent;
25081 it->descent = descent;
25082 }
25083 }
25084
25085 it->phys_ascent = it->ascent;
25086 it->phys_descent = it->descent;
25087 if (it->glyph_row)
25088 append_glyphless_glyph (it, face_id, for_no_font, len,
25089 upper_xoff, upper_yoff,
25090 lower_xoff, lower_yoff);
25091 it->nglyphs = 1;
25092 take_vertical_position_into_account (it);
25093 }
25094
25095
25096 /* RIF:
25097 Produce glyphs/get display metrics for the display element IT is
25098 loaded with. See the description of struct it in dispextern.h
25099 for an overview of struct it. */
25100
25101 void
25102 x_produce_glyphs (struct it *it)
25103 {
25104 int extra_line_spacing = it->extra_line_spacing;
25105
25106 it->glyph_not_available_p = 0;
25107
25108 if (it->what == IT_CHARACTER)
25109 {
25110 XChar2b char2b;
25111 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25112 struct font *font = face->font;
25113 struct font_metrics *pcm = NULL;
25114 int boff; /* baseline offset */
25115
25116 if (font == NULL)
25117 {
25118 /* When no suitable font is found, display this character by
25119 the method specified in the first extra slot of
25120 Vglyphless_char_display. */
25121 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
25122
25123 eassert (it->what == IT_GLYPHLESS);
25124 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
25125 goto done;
25126 }
25127
25128 boff = font->baseline_offset;
25129 if (font->vertical_centering)
25130 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25131
25132 if (it->char_to_display != '\n' && it->char_to_display != '\t')
25133 {
25134 int stretched_p;
25135
25136 it->nglyphs = 1;
25137
25138 if (it->override_ascent >= 0)
25139 {
25140 it->ascent = it->override_ascent;
25141 it->descent = it->override_descent;
25142 boff = it->override_boff;
25143 }
25144 else
25145 {
25146 it->ascent = FONT_BASE (font) + boff;
25147 it->descent = FONT_DESCENT (font) - boff;
25148 }
25149
25150 if (get_char_glyph_code (it->char_to_display, font, &char2b))
25151 {
25152 pcm = get_per_char_metric (font, &char2b);
25153 if (pcm->width == 0
25154 && pcm->rbearing == 0 && pcm->lbearing == 0)
25155 pcm = NULL;
25156 }
25157
25158 if (pcm)
25159 {
25160 it->phys_ascent = pcm->ascent + boff;
25161 it->phys_descent = pcm->descent - boff;
25162 it->pixel_width = pcm->width;
25163 }
25164 else
25165 {
25166 it->glyph_not_available_p = 1;
25167 it->phys_ascent = it->ascent;
25168 it->phys_descent = it->descent;
25169 it->pixel_width = font->space_width;
25170 }
25171
25172 if (it->constrain_row_ascent_descent_p)
25173 {
25174 if (it->descent > it->max_descent)
25175 {
25176 it->ascent += it->descent - it->max_descent;
25177 it->descent = it->max_descent;
25178 }
25179 if (it->ascent > it->max_ascent)
25180 {
25181 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25182 it->ascent = it->max_ascent;
25183 }
25184 it->phys_ascent = min (it->phys_ascent, it->ascent);
25185 it->phys_descent = min (it->phys_descent, it->descent);
25186 extra_line_spacing = 0;
25187 }
25188
25189 /* If this is a space inside a region of text with
25190 `space-width' property, change its width. */
25191 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
25192 if (stretched_p)
25193 it->pixel_width *= XFLOATINT (it->space_width);
25194
25195 /* If face has a box, add the box thickness to the character
25196 height. If character has a box line to the left and/or
25197 right, add the box line width to the character's width. */
25198 if (face->box != FACE_NO_BOX)
25199 {
25200 int thick = face->box_line_width;
25201
25202 if (thick > 0)
25203 {
25204 it->ascent += thick;
25205 it->descent += thick;
25206 }
25207 else
25208 thick = -thick;
25209
25210 if (it->start_of_box_run_p)
25211 it->pixel_width += thick;
25212 if (it->end_of_box_run_p)
25213 it->pixel_width += thick;
25214 }
25215
25216 /* If face has an overline, add the height of the overline
25217 (1 pixel) and a 1 pixel margin to the character height. */
25218 if (face->overline_p)
25219 it->ascent += overline_margin;
25220
25221 if (it->constrain_row_ascent_descent_p)
25222 {
25223 if (it->ascent > it->max_ascent)
25224 it->ascent = it->max_ascent;
25225 if (it->descent > it->max_descent)
25226 it->descent = it->max_descent;
25227 }
25228
25229 take_vertical_position_into_account (it);
25230
25231 /* If we have to actually produce glyphs, do it. */
25232 if (it->glyph_row)
25233 {
25234 if (stretched_p)
25235 {
25236 /* Translate a space with a `space-width' property
25237 into a stretch glyph. */
25238 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
25239 / FONT_HEIGHT (font));
25240 append_stretch_glyph (it, it->object, it->pixel_width,
25241 it->ascent + it->descent, ascent);
25242 }
25243 else
25244 append_glyph (it);
25245
25246 /* If characters with lbearing or rbearing are displayed
25247 in this line, record that fact in a flag of the
25248 glyph row. This is used to optimize X output code. */
25249 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
25250 it->glyph_row->contains_overlapping_glyphs_p = 1;
25251 }
25252 if (! stretched_p && it->pixel_width == 0)
25253 /* We assure that all visible glyphs have at least 1-pixel
25254 width. */
25255 it->pixel_width = 1;
25256 }
25257 else if (it->char_to_display == '\n')
25258 {
25259 /* A newline has no width, but we need the height of the
25260 line. But if previous part of the line sets a height,
25261 don't increase that height */
25262
25263 Lisp_Object height;
25264 Lisp_Object total_height = Qnil;
25265
25266 it->override_ascent = -1;
25267 it->pixel_width = 0;
25268 it->nglyphs = 0;
25269
25270 height = get_it_property (it, Qline_height);
25271 /* Split (line-height total-height) list */
25272 if (CONSP (height)
25273 && CONSP (XCDR (height))
25274 && NILP (XCDR (XCDR (height))))
25275 {
25276 total_height = XCAR (XCDR (height));
25277 height = XCAR (height);
25278 }
25279 height = calc_line_height_property (it, height, font, boff, 1);
25280
25281 if (it->override_ascent >= 0)
25282 {
25283 it->ascent = it->override_ascent;
25284 it->descent = it->override_descent;
25285 boff = it->override_boff;
25286 }
25287 else
25288 {
25289 it->ascent = FONT_BASE (font) + boff;
25290 it->descent = FONT_DESCENT (font) - boff;
25291 }
25292
25293 if (EQ (height, Qt))
25294 {
25295 if (it->descent > it->max_descent)
25296 {
25297 it->ascent += it->descent - it->max_descent;
25298 it->descent = it->max_descent;
25299 }
25300 if (it->ascent > it->max_ascent)
25301 {
25302 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25303 it->ascent = it->max_ascent;
25304 }
25305 it->phys_ascent = min (it->phys_ascent, it->ascent);
25306 it->phys_descent = min (it->phys_descent, it->descent);
25307 it->constrain_row_ascent_descent_p = 1;
25308 extra_line_spacing = 0;
25309 }
25310 else
25311 {
25312 Lisp_Object spacing;
25313
25314 it->phys_ascent = it->ascent;
25315 it->phys_descent = it->descent;
25316
25317 if ((it->max_ascent > 0 || it->max_descent > 0)
25318 && face->box != FACE_NO_BOX
25319 && face->box_line_width > 0)
25320 {
25321 it->ascent += face->box_line_width;
25322 it->descent += face->box_line_width;
25323 }
25324 if (!NILP (height)
25325 && XINT (height) > it->ascent + it->descent)
25326 it->ascent = XINT (height) - it->descent;
25327
25328 if (!NILP (total_height))
25329 spacing = calc_line_height_property (it, total_height, font, boff, 0);
25330 else
25331 {
25332 spacing = get_it_property (it, Qline_spacing);
25333 spacing = calc_line_height_property (it, spacing, font, boff, 0);
25334 }
25335 if (INTEGERP (spacing))
25336 {
25337 extra_line_spacing = XINT (spacing);
25338 if (!NILP (total_height))
25339 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
25340 }
25341 }
25342 }
25343 else /* i.e. (it->char_to_display == '\t') */
25344 {
25345 if (font->space_width > 0)
25346 {
25347 int tab_width = it->tab_width * font->space_width;
25348 int x = it->current_x + it->continuation_lines_width;
25349 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
25350
25351 /* If the distance from the current position to the next tab
25352 stop is less than a space character width, use the
25353 tab stop after that. */
25354 if (next_tab_x - x < font->space_width)
25355 next_tab_x += tab_width;
25356
25357 it->pixel_width = next_tab_x - x;
25358 it->nglyphs = 1;
25359 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
25360 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
25361
25362 if (it->glyph_row)
25363 {
25364 append_stretch_glyph (it, it->object, it->pixel_width,
25365 it->ascent + it->descent, it->ascent);
25366 }
25367 }
25368 else
25369 {
25370 it->pixel_width = 0;
25371 it->nglyphs = 1;
25372 }
25373 }
25374 }
25375 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
25376 {
25377 /* A static composition.
25378
25379 Note: A composition is represented as one glyph in the
25380 glyph matrix. There are no padding glyphs.
25381
25382 Important note: pixel_width, ascent, and descent are the
25383 values of what is drawn by draw_glyphs (i.e. the values of
25384 the overall glyphs composed). */
25385 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25386 int boff; /* baseline offset */
25387 struct composition *cmp = composition_table[it->cmp_it.id];
25388 int glyph_len = cmp->glyph_len;
25389 struct font *font = face->font;
25390
25391 it->nglyphs = 1;
25392
25393 /* If we have not yet calculated pixel size data of glyphs of
25394 the composition for the current face font, calculate them
25395 now. Theoretically, we have to check all fonts for the
25396 glyphs, but that requires much time and memory space. So,
25397 here we check only the font of the first glyph. This may
25398 lead to incorrect display, but it's very rare, and C-l
25399 (recenter-top-bottom) can correct the display anyway. */
25400 if (! cmp->font || cmp->font != font)
25401 {
25402 /* Ascent and descent of the font of the first character
25403 of this composition (adjusted by baseline offset).
25404 Ascent and descent of overall glyphs should not be less
25405 than these, respectively. */
25406 int font_ascent, font_descent, font_height;
25407 /* Bounding box of the overall glyphs. */
25408 int leftmost, rightmost, lowest, highest;
25409 int lbearing, rbearing;
25410 int i, width, ascent, descent;
25411 int left_padded = 0, right_padded = 0;
25412 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
25413 XChar2b char2b;
25414 struct font_metrics *pcm;
25415 int font_not_found_p;
25416 ptrdiff_t pos;
25417
25418 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
25419 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
25420 break;
25421 if (glyph_len < cmp->glyph_len)
25422 right_padded = 1;
25423 for (i = 0; i < glyph_len; i++)
25424 {
25425 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
25426 break;
25427 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25428 }
25429 if (i > 0)
25430 left_padded = 1;
25431
25432 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
25433 : IT_CHARPOS (*it));
25434 /* If no suitable font is found, use the default font. */
25435 font_not_found_p = font == NULL;
25436 if (font_not_found_p)
25437 {
25438 face = face->ascii_face;
25439 font = face->font;
25440 }
25441 boff = font->baseline_offset;
25442 if (font->vertical_centering)
25443 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25444 font_ascent = FONT_BASE (font) + boff;
25445 font_descent = FONT_DESCENT (font) - boff;
25446 font_height = FONT_HEIGHT (font);
25447
25448 cmp->font = font;
25449
25450 pcm = NULL;
25451 if (! font_not_found_p)
25452 {
25453 get_char_face_and_encoding (it->f, c, it->face_id,
25454 &char2b, 0);
25455 pcm = get_per_char_metric (font, &char2b);
25456 }
25457
25458 /* Initialize the bounding box. */
25459 if (pcm)
25460 {
25461 width = cmp->glyph_len > 0 ? pcm->width : 0;
25462 ascent = pcm->ascent;
25463 descent = pcm->descent;
25464 lbearing = pcm->lbearing;
25465 rbearing = pcm->rbearing;
25466 }
25467 else
25468 {
25469 width = cmp->glyph_len > 0 ? font->space_width : 0;
25470 ascent = FONT_BASE (font);
25471 descent = FONT_DESCENT (font);
25472 lbearing = 0;
25473 rbearing = width;
25474 }
25475
25476 rightmost = width;
25477 leftmost = 0;
25478 lowest = - descent + boff;
25479 highest = ascent + boff;
25480
25481 if (! font_not_found_p
25482 && font->default_ascent
25483 && CHAR_TABLE_P (Vuse_default_ascent)
25484 && !NILP (Faref (Vuse_default_ascent,
25485 make_number (it->char_to_display))))
25486 highest = font->default_ascent + boff;
25487
25488 /* Draw the first glyph at the normal position. It may be
25489 shifted to right later if some other glyphs are drawn
25490 at the left. */
25491 cmp->offsets[i * 2] = 0;
25492 cmp->offsets[i * 2 + 1] = boff;
25493 cmp->lbearing = lbearing;
25494 cmp->rbearing = rbearing;
25495
25496 /* Set cmp->offsets for the remaining glyphs. */
25497 for (i++; i < glyph_len; i++)
25498 {
25499 int left, right, btm, top;
25500 int ch = COMPOSITION_GLYPH (cmp, i);
25501 int face_id;
25502 struct face *this_face;
25503
25504 if (ch == '\t')
25505 ch = ' ';
25506 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25507 this_face = FACE_FROM_ID (it->f, face_id);
25508 font = this_face->font;
25509
25510 if (font == NULL)
25511 pcm = NULL;
25512 else
25513 {
25514 get_char_face_and_encoding (it->f, ch, face_id,
25515 &char2b, 0);
25516 pcm = get_per_char_metric (font, &char2b);
25517 }
25518 if (! pcm)
25519 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25520 else
25521 {
25522 width = pcm->width;
25523 ascent = pcm->ascent;
25524 descent = pcm->descent;
25525 lbearing = pcm->lbearing;
25526 rbearing = pcm->rbearing;
25527 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25528 {
25529 /* Relative composition with or without
25530 alternate chars. */
25531 left = (leftmost + rightmost - width) / 2;
25532 btm = - descent + boff;
25533 if (font->relative_compose
25534 && (! CHAR_TABLE_P (Vignore_relative_composition)
25535 || NILP (Faref (Vignore_relative_composition,
25536 make_number (ch)))))
25537 {
25538
25539 if (- descent >= font->relative_compose)
25540 /* One extra pixel between two glyphs. */
25541 btm = highest + 1;
25542 else if (ascent <= 0)
25543 /* One extra pixel between two glyphs. */
25544 btm = lowest - 1 - ascent - descent;
25545 }
25546 }
25547 else
25548 {
25549 /* A composition rule is specified by an integer
25550 value that encodes global and new reference
25551 points (GREF and NREF). GREF and NREF are
25552 specified by numbers as below:
25553
25554 0---1---2 -- ascent
25555 | |
25556 | |
25557 | |
25558 9--10--11 -- center
25559 | |
25560 ---3---4---5--- baseline
25561 | |
25562 6---7---8 -- descent
25563 */
25564 int rule = COMPOSITION_RULE (cmp, i);
25565 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25566
25567 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25568 grefx = gref % 3, nrefx = nref % 3;
25569 grefy = gref / 3, nrefy = nref / 3;
25570 if (xoff)
25571 xoff = font_height * (xoff - 128) / 256;
25572 if (yoff)
25573 yoff = font_height * (yoff - 128) / 256;
25574
25575 left = (leftmost
25576 + grefx * (rightmost - leftmost) / 2
25577 - nrefx * width / 2
25578 + xoff);
25579
25580 btm = ((grefy == 0 ? highest
25581 : grefy == 1 ? 0
25582 : grefy == 2 ? lowest
25583 : (highest + lowest) / 2)
25584 - (nrefy == 0 ? ascent + descent
25585 : nrefy == 1 ? descent - boff
25586 : nrefy == 2 ? 0
25587 : (ascent + descent) / 2)
25588 + yoff);
25589 }
25590
25591 cmp->offsets[i * 2] = left;
25592 cmp->offsets[i * 2 + 1] = btm + descent;
25593
25594 /* Update the bounding box of the overall glyphs. */
25595 if (width > 0)
25596 {
25597 right = left + width;
25598 if (left < leftmost)
25599 leftmost = left;
25600 if (right > rightmost)
25601 rightmost = right;
25602 }
25603 top = btm + descent + ascent;
25604 if (top > highest)
25605 highest = top;
25606 if (btm < lowest)
25607 lowest = btm;
25608
25609 if (cmp->lbearing > left + lbearing)
25610 cmp->lbearing = left + lbearing;
25611 if (cmp->rbearing < left + rbearing)
25612 cmp->rbearing = left + rbearing;
25613 }
25614 }
25615
25616 /* If there are glyphs whose x-offsets are negative,
25617 shift all glyphs to the right and make all x-offsets
25618 non-negative. */
25619 if (leftmost < 0)
25620 {
25621 for (i = 0; i < cmp->glyph_len; i++)
25622 cmp->offsets[i * 2] -= leftmost;
25623 rightmost -= leftmost;
25624 cmp->lbearing -= leftmost;
25625 cmp->rbearing -= leftmost;
25626 }
25627
25628 if (left_padded && cmp->lbearing < 0)
25629 {
25630 for (i = 0; i < cmp->glyph_len; i++)
25631 cmp->offsets[i * 2] -= cmp->lbearing;
25632 rightmost -= cmp->lbearing;
25633 cmp->rbearing -= cmp->lbearing;
25634 cmp->lbearing = 0;
25635 }
25636 if (right_padded && rightmost < cmp->rbearing)
25637 {
25638 rightmost = cmp->rbearing;
25639 }
25640
25641 cmp->pixel_width = rightmost;
25642 cmp->ascent = highest;
25643 cmp->descent = - lowest;
25644 if (cmp->ascent < font_ascent)
25645 cmp->ascent = font_ascent;
25646 if (cmp->descent < font_descent)
25647 cmp->descent = font_descent;
25648 }
25649
25650 if (it->glyph_row
25651 && (cmp->lbearing < 0
25652 || cmp->rbearing > cmp->pixel_width))
25653 it->glyph_row->contains_overlapping_glyphs_p = 1;
25654
25655 it->pixel_width = cmp->pixel_width;
25656 it->ascent = it->phys_ascent = cmp->ascent;
25657 it->descent = it->phys_descent = cmp->descent;
25658 if (face->box != FACE_NO_BOX)
25659 {
25660 int thick = face->box_line_width;
25661
25662 if (thick > 0)
25663 {
25664 it->ascent += thick;
25665 it->descent += thick;
25666 }
25667 else
25668 thick = - thick;
25669
25670 if (it->start_of_box_run_p)
25671 it->pixel_width += thick;
25672 if (it->end_of_box_run_p)
25673 it->pixel_width += thick;
25674 }
25675
25676 /* If face has an overline, add the height of the overline
25677 (1 pixel) and a 1 pixel margin to the character height. */
25678 if (face->overline_p)
25679 it->ascent += overline_margin;
25680
25681 take_vertical_position_into_account (it);
25682 if (it->ascent < 0)
25683 it->ascent = 0;
25684 if (it->descent < 0)
25685 it->descent = 0;
25686
25687 if (it->glyph_row && cmp->glyph_len > 0)
25688 append_composite_glyph (it);
25689 }
25690 else if (it->what == IT_COMPOSITION)
25691 {
25692 /* A dynamic (automatic) composition. */
25693 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25694 Lisp_Object gstring;
25695 struct font_metrics metrics;
25696
25697 it->nglyphs = 1;
25698
25699 gstring = composition_gstring_from_id (it->cmp_it.id);
25700 it->pixel_width
25701 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25702 &metrics);
25703 if (it->glyph_row
25704 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25705 it->glyph_row->contains_overlapping_glyphs_p = 1;
25706 it->ascent = it->phys_ascent = metrics.ascent;
25707 it->descent = it->phys_descent = metrics.descent;
25708 if (face->box != FACE_NO_BOX)
25709 {
25710 int thick = face->box_line_width;
25711
25712 if (thick > 0)
25713 {
25714 it->ascent += thick;
25715 it->descent += thick;
25716 }
25717 else
25718 thick = - thick;
25719
25720 if (it->start_of_box_run_p)
25721 it->pixel_width += thick;
25722 if (it->end_of_box_run_p)
25723 it->pixel_width += thick;
25724 }
25725 /* If face has an overline, add the height of the overline
25726 (1 pixel) and a 1 pixel margin to the character height. */
25727 if (face->overline_p)
25728 it->ascent += overline_margin;
25729 take_vertical_position_into_account (it);
25730 if (it->ascent < 0)
25731 it->ascent = 0;
25732 if (it->descent < 0)
25733 it->descent = 0;
25734
25735 if (it->glyph_row)
25736 append_composite_glyph (it);
25737 }
25738 else if (it->what == IT_GLYPHLESS)
25739 produce_glyphless_glyph (it, 0, Qnil);
25740 else if (it->what == IT_IMAGE)
25741 produce_image_glyph (it);
25742 else if (it->what == IT_STRETCH)
25743 produce_stretch_glyph (it);
25744
25745 done:
25746 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25747 because this isn't true for images with `:ascent 100'. */
25748 eassert (it->ascent >= 0 && it->descent >= 0);
25749 if (it->area == TEXT_AREA)
25750 it->current_x += it->pixel_width;
25751
25752 if (extra_line_spacing > 0)
25753 {
25754 it->descent += extra_line_spacing;
25755 if (extra_line_spacing > it->max_extra_line_spacing)
25756 it->max_extra_line_spacing = extra_line_spacing;
25757 }
25758
25759 it->max_ascent = max (it->max_ascent, it->ascent);
25760 it->max_descent = max (it->max_descent, it->descent);
25761 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25762 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25763 }
25764
25765 /* EXPORT for RIF:
25766 Output LEN glyphs starting at START at the nominal cursor position.
25767 Advance the nominal cursor over the text. The global variable
25768 updated_row is the glyph row being updated, and updated_area is the
25769 area of that row being updated. */
25770
25771 void
25772 x_write_glyphs (struct window *w, struct glyph *start, int len)
25773 {
25774 int x, hpos, chpos = w->phys_cursor.hpos;
25775
25776 eassert (updated_row);
25777 /* When the window is hscrolled, cursor hpos can legitimately be out
25778 of bounds, but we draw the cursor at the corresponding window
25779 margin in that case. */
25780 if (!updated_row->reversed_p && chpos < 0)
25781 chpos = 0;
25782 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25783 chpos = updated_row->used[TEXT_AREA] - 1;
25784
25785 block_input ();
25786
25787 /* Write glyphs. */
25788
25789 hpos = start - updated_row->glyphs[updated_area];
25790 x = draw_glyphs (w, output_cursor.x,
25791 updated_row, updated_area,
25792 hpos, hpos + len,
25793 DRAW_NORMAL_TEXT, 0);
25794
25795 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25796 if (updated_area == TEXT_AREA
25797 && w->phys_cursor_on_p
25798 && w->phys_cursor.vpos == output_cursor.vpos
25799 && chpos >= hpos
25800 && chpos < hpos + len)
25801 w->phys_cursor_on_p = 0;
25802
25803 unblock_input ();
25804
25805 /* Advance the output cursor. */
25806 output_cursor.hpos += len;
25807 output_cursor.x = x;
25808 }
25809
25810
25811 /* EXPORT for RIF:
25812 Insert LEN glyphs from START at the nominal cursor position. */
25813
25814 void
25815 x_insert_glyphs (struct window *w, struct glyph *start, int len)
25816 {
25817 struct frame *f;
25818 int line_height, shift_by_width, shifted_region_width;
25819 struct glyph_row *row;
25820 struct glyph *glyph;
25821 int frame_x, frame_y;
25822 ptrdiff_t hpos;
25823
25824 eassert (updated_row);
25825 block_input ();
25826 f = XFRAME (WINDOW_FRAME (w));
25827
25828 /* Get the height of the line we are in. */
25829 row = updated_row;
25830 line_height = row->height;
25831
25832 /* Get the width of the glyphs to insert. */
25833 shift_by_width = 0;
25834 for (glyph = start; glyph < start + len; ++glyph)
25835 shift_by_width += glyph->pixel_width;
25836
25837 /* Get the width of the region to shift right. */
25838 shifted_region_width = (window_box_width (w, updated_area)
25839 - output_cursor.x
25840 - shift_by_width);
25841
25842 /* Shift right. */
25843 frame_x = window_box_left (w, updated_area) + output_cursor.x;
25844 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
25845
25846 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25847 line_height, shift_by_width);
25848
25849 /* Write the glyphs. */
25850 hpos = start - row->glyphs[updated_area];
25851 draw_glyphs (w, output_cursor.x, row, updated_area,
25852 hpos, hpos + len,
25853 DRAW_NORMAL_TEXT, 0);
25854
25855 /* Advance the output cursor. */
25856 output_cursor.hpos += len;
25857 output_cursor.x += shift_by_width;
25858 unblock_input ();
25859 }
25860
25861
25862 /* EXPORT for RIF:
25863 Erase the current text line from the nominal cursor position
25864 (inclusive) to pixel column TO_X (exclusive). The idea is that
25865 everything from TO_X onward is already erased.
25866
25867 TO_X is a pixel position relative to updated_area of currently
25868 updated window W. TO_X == -1 means clear to the end of this area. */
25869
25870 void
25871 x_clear_end_of_line (struct window *w, int to_x)
25872 {
25873 struct frame *f;
25874 int max_x, min_y, max_y;
25875 int from_x, from_y, to_y;
25876
25877 eassert (updated_row);
25878 f = XFRAME (w->frame);
25879
25880 if (updated_row->full_width_p)
25881 max_x = WINDOW_TOTAL_WIDTH (w);
25882 else
25883 max_x = window_box_width (w, updated_area);
25884 max_y = window_text_bottom_y (w);
25885
25886 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25887 of window. For TO_X > 0, truncate to end of drawing area. */
25888 if (to_x == 0)
25889 return;
25890 else if (to_x < 0)
25891 to_x = max_x;
25892 else
25893 to_x = min (to_x, max_x);
25894
25895 to_y = min (max_y, output_cursor.y + updated_row->height);
25896
25897 /* Notice if the cursor will be cleared by this operation. */
25898 if (!updated_row->full_width_p)
25899 notice_overwritten_cursor (w, updated_area,
25900 output_cursor.x, -1,
25901 updated_row->y,
25902 MATRIX_ROW_BOTTOM_Y (updated_row));
25903
25904 from_x = output_cursor.x;
25905
25906 /* Translate to frame coordinates. */
25907 if (updated_row->full_width_p)
25908 {
25909 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25910 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25911 }
25912 else
25913 {
25914 int area_left = window_box_left (w, updated_area);
25915 from_x += area_left;
25916 to_x += area_left;
25917 }
25918
25919 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25920 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
25921 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25922
25923 /* Prevent inadvertently clearing to end of the X window. */
25924 if (to_x > from_x && to_y > from_y)
25925 {
25926 block_input ();
25927 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25928 to_x - from_x, to_y - from_y);
25929 unblock_input ();
25930 }
25931 }
25932
25933 #endif /* HAVE_WINDOW_SYSTEM */
25934
25935
25936 \f
25937 /***********************************************************************
25938 Cursor types
25939 ***********************************************************************/
25940
25941 /* Value is the internal representation of the specified cursor type
25942 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25943 of the bar cursor. */
25944
25945 static enum text_cursor_kinds
25946 get_specified_cursor_type (Lisp_Object arg, int *width)
25947 {
25948 enum text_cursor_kinds type;
25949
25950 if (NILP (arg))
25951 return NO_CURSOR;
25952
25953 if (EQ (arg, Qbox))
25954 return FILLED_BOX_CURSOR;
25955
25956 if (EQ (arg, Qhollow))
25957 return HOLLOW_BOX_CURSOR;
25958
25959 if (EQ (arg, Qbar))
25960 {
25961 *width = 2;
25962 return BAR_CURSOR;
25963 }
25964
25965 if (CONSP (arg)
25966 && EQ (XCAR (arg), Qbar)
25967 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25968 {
25969 *width = XINT (XCDR (arg));
25970 return BAR_CURSOR;
25971 }
25972
25973 if (EQ (arg, Qhbar))
25974 {
25975 *width = 2;
25976 return HBAR_CURSOR;
25977 }
25978
25979 if (CONSP (arg)
25980 && EQ (XCAR (arg), Qhbar)
25981 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25982 {
25983 *width = XINT (XCDR (arg));
25984 return HBAR_CURSOR;
25985 }
25986
25987 /* Treat anything unknown as "hollow box cursor".
25988 It was bad to signal an error; people have trouble fixing
25989 .Xdefaults with Emacs, when it has something bad in it. */
25990 type = HOLLOW_BOX_CURSOR;
25991
25992 return type;
25993 }
25994
25995 /* Set the default cursor types for specified frame. */
25996 void
25997 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25998 {
25999 int width = 1;
26000 Lisp_Object tem;
26001
26002 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
26003 FRAME_CURSOR_WIDTH (f) = width;
26004
26005 /* By default, set up the blink-off state depending on the on-state. */
26006
26007 tem = Fassoc (arg, Vblink_cursor_alist);
26008 if (!NILP (tem))
26009 {
26010 FRAME_BLINK_OFF_CURSOR (f)
26011 = get_specified_cursor_type (XCDR (tem), &width);
26012 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
26013 }
26014 else
26015 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
26016
26017 /* Make sure the cursor gets redrawn. */
26018 cursor_type_changed = 1;
26019 }
26020
26021
26022 #ifdef HAVE_WINDOW_SYSTEM
26023
26024 /* Return the cursor we want to be displayed in window W. Return
26025 width of bar/hbar cursor through WIDTH arg. Return with
26026 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
26027 (i.e. if the `system caret' should track this cursor).
26028
26029 In a mini-buffer window, we want the cursor only to appear if we
26030 are reading input from this window. For the selected window, we
26031 want the cursor type given by the frame parameter or buffer local
26032 setting of cursor-type. If explicitly marked off, draw no cursor.
26033 In all other cases, we want a hollow box cursor. */
26034
26035 static enum text_cursor_kinds
26036 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
26037 int *active_cursor)
26038 {
26039 struct frame *f = XFRAME (w->frame);
26040 struct buffer *b = XBUFFER (w->contents);
26041 int cursor_type = DEFAULT_CURSOR;
26042 Lisp_Object alt_cursor;
26043 int non_selected = 0;
26044
26045 *active_cursor = 1;
26046
26047 /* Echo area */
26048 if (cursor_in_echo_area
26049 && FRAME_HAS_MINIBUF_P (f)
26050 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
26051 {
26052 if (w == XWINDOW (echo_area_window))
26053 {
26054 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
26055 {
26056 *width = FRAME_CURSOR_WIDTH (f);
26057 return FRAME_DESIRED_CURSOR (f);
26058 }
26059 else
26060 return get_specified_cursor_type (BVAR (b, cursor_type), width);
26061 }
26062
26063 *active_cursor = 0;
26064 non_selected = 1;
26065 }
26066
26067 /* Detect a nonselected window or nonselected frame. */
26068 else if (w != XWINDOW (f->selected_window)
26069 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
26070 {
26071 *active_cursor = 0;
26072
26073 if (MINI_WINDOW_P (w) && minibuf_level == 0)
26074 return NO_CURSOR;
26075
26076 non_selected = 1;
26077 }
26078
26079 /* Never display a cursor in a window in which cursor-type is nil. */
26080 if (NILP (BVAR (b, cursor_type)))
26081 return NO_CURSOR;
26082
26083 /* Get the normal cursor type for this window. */
26084 if (EQ (BVAR (b, cursor_type), Qt))
26085 {
26086 cursor_type = FRAME_DESIRED_CURSOR (f);
26087 *width = FRAME_CURSOR_WIDTH (f);
26088 }
26089 else
26090 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
26091
26092 /* Use cursor-in-non-selected-windows instead
26093 for non-selected window or frame. */
26094 if (non_selected)
26095 {
26096 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
26097 if (!EQ (Qt, alt_cursor))
26098 return get_specified_cursor_type (alt_cursor, width);
26099 /* t means modify the normal cursor type. */
26100 if (cursor_type == FILLED_BOX_CURSOR)
26101 cursor_type = HOLLOW_BOX_CURSOR;
26102 else if (cursor_type == BAR_CURSOR && *width > 1)
26103 --*width;
26104 return cursor_type;
26105 }
26106
26107 /* Use normal cursor if not blinked off. */
26108 if (!w->cursor_off_p)
26109 {
26110 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26111 {
26112 if (cursor_type == FILLED_BOX_CURSOR)
26113 {
26114 /* Using a block cursor on large images can be very annoying.
26115 So use a hollow cursor for "large" images.
26116 If image is not transparent (no mask), also use hollow cursor. */
26117 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26118 if (img != NULL && IMAGEP (img->spec))
26119 {
26120 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
26121 where N = size of default frame font size.
26122 This should cover most of the "tiny" icons people may use. */
26123 if (!img->mask
26124 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
26125 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
26126 cursor_type = HOLLOW_BOX_CURSOR;
26127 }
26128 }
26129 else if (cursor_type != NO_CURSOR)
26130 {
26131 /* Display current only supports BOX and HOLLOW cursors for images.
26132 So for now, unconditionally use a HOLLOW cursor when cursor is
26133 not a solid box cursor. */
26134 cursor_type = HOLLOW_BOX_CURSOR;
26135 }
26136 }
26137 return cursor_type;
26138 }
26139
26140 /* Cursor is blinked off, so determine how to "toggle" it. */
26141
26142 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
26143 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
26144 return get_specified_cursor_type (XCDR (alt_cursor), width);
26145
26146 /* Then see if frame has specified a specific blink off cursor type. */
26147 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
26148 {
26149 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
26150 return FRAME_BLINK_OFF_CURSOR (f);
26151 }
26152
26153 #if 0
26154 /* Some people liked having a permanently visible blinking cursor,
26155 while others had very strong opinions against it. So it was
26156 decided to remove it. KFS 2003-09-03 */
26157
26158 /* Finally perform built-in cursor blinking:
26159 filled box <-> hollow box
26160 wide [h]bar <-> narrow [h]bar
26161 narrow [h]bar <-> no cursor
26162 other type <-> no cursor */
26163
26164 if (cursor_type == FILLED_BOX_CURSOR)
26165 return HOLLOW_BOX_CURSOR;
26166
26167 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
26168 {
26169 *width = 1;
26170 return cursor_type;
26171 }
26172 #endif
26173
26174 return NO_CURSOR;
26175 }
26176
26177
26178 /* Notice when the text cursor of window W has been completely
26179 overwritten by a drawing operation that outputs glyphs in AREA
26180 starting at X0 and ending at X1 in the line starting at Y0 and
26181 ending at Y1. X coordinates are area-relative. X1 < 0 means all
26182 the rest of the line after X0 has been written. Y coordinates
26183 are window-relative. */
26184
26185 static void
26186 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
26187 int x0, int x1, int y0, int y1)
26188 {
26189 int cx0, cx1, cy0, cy1;
26190 struct glyph_row *row;
26191
26192 if (!w->phys_cursor_on_p)
26193 return;
26194 if (area != TEXT_AREA)
26195 return;
26196
26197 if (w->phys_cursor.vpos < 0
26198 || w->phys_cursor.vpos >= w->current_matrix->nrows
26199 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
26200 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
26201 return;
26202
26203 if (row->cursor_in_fringe_p)
26204 {
26205 row->cursor_in_fringe_p = 0;
26206 draw_fringe_bitmap (w, row, row->reversed_p);
26207 w->phys_cursor_on_p = 0;
26208 return;
26209 }
26210
26211 cx0 = w->phys_cursor.x;
26212 cx1 = cx0 + w->phys_cursor_width;
26213 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
26214 return;
26215
26216 /* The cursor image will be completely removed from the
26217 screen if the output area intersects the cursor area in
26218 y-direction. When we draw in [y0 y1[, and some part of
26219 the cursor is at y < y0, that part must have been drawn
26220 before. When scrolling, the cursor is erased before
26221 actually scrolling, so we don't come here. When not
26222 scrolling, the rows above the old cursor row must have
26223 changed, and in this case these rows must have written
26224 over the cursor image.
26225
26226 Likewise if part of the cursor is below y1, with the
26227 exception of the cursor being in the first blank row at
26228 the buffer and window end because update_text_area
26229 doesn't draw that row. (Except when it does, but
26230 that's handled in update_text_area.) */
26231
26232 cy0 = w->phys_cursor.y;
26233 cy1 = cy0 + w->phys_cursor_height;
26234 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
26235 return;
26236
26237 w->phys_cursor_on_p = 0;
26238 }
26239
26240 #endif /* HAVE_WINDOW_SYSTEM */
26241
26242 \f
26243 /************************************************************************
26244 Mouse Face
26245 ************************************************************************/
26246
26247 #ifdef HAVE_WINDOW_SYSTEM
26248
26249 /* EXPORT for RIF:
26250 Fix the display of area AREA of overlapping row ROW in window W
26251 with respect to the overlapping part OVERLAPS. */
26252
26253 void
26254 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
26255 enum glyph_row_area area, int overlaps)
26256 {
26257 int i, x;
26258
26259 block_input ();
26260
26261 x = 0;
26262 for (i = 0; i < row->used[area];)
26263 {
26264 if (row->glyphs[area][i].overlaps_vertically_p)
26265 {
26266 int start = i, start_x = x;
26267
26268 do
26269 {
26270 x += row->glyphs[area][i].pixel_width;
26271 ++i;
26272 }
26273 while (i < row->used[area]
26274 && row->glyphs[area][i].overlaps_vertically_p);
26275
26276 draw_glyphs (w, start_x, row, area,
26277 start, i,
26278 DRAW_NORMAL_TEXT, overlaps);
26279 }
26280 else
26281 {
26282 x += row->glyphs[area][i].pixel_width;
26283 ++i;
26284 }
26285 }
26286
26287 unblock_input ();
26288 }
26289
26290
26291 /* EXPORT:
26292 Draw the cursor glyph of window W in glyph row ROW. See the
26293 comment of draw_glyphs for the meaning of HL. */
26294
26295 void
26296 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
26297 enum draw_glyphs_face hl)
26298 {
26299 /* If cursor hpos is out of bounds, don't draw garbage. This can
26300 happen in mini-buffer windows when switching between echo area
26301 glyphs and mini-buffer. */
26302 if ((row->reversed_p
26303 ? (w->phys_cursor.hpos >= 0)
26304 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
26305 {
26306 int on_p = w->phys_cursor_on_p;
26307 int x1;
26308 int hpos = w->phys_cursor.hpos;
26309
26310 /* When the window is hscrolled, cursor hpos can legitimately be
26311 out of bounds, but we draw the cursor at the corresponding
26312 window margin in that case. */
26313 if (!row->reversed_p && hpos < 0)
26314 hpos = 0;
26315 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26316 hpos = row->used[TEXT_AREA] - 1;
26317
26318 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
26319 hl, 0);
26320 w->phys_cursor_on_p = on_p;
26321
26322 if (hl == DRAW_CURSOR)
26323 w->phys_cursor_width = x1 - w->phys_cursor.x;
26324 /* When we erase the cursor, and ROW is overlapped by other
26325 rows, make sure that these overlapping parts of other rows
26326 are redrawn. */
26327 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
26328 {
26329 w->phys_cursor_width = x1 - w->phys_cursor.x;
26330
26331 if (row > w->current_matrix->rows
26332 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
26333 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
26334 OVERLAPS_ERASED_CURSOR);
26335
26336 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
26337 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
26338 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
26339 OVERLAPS_ERASED_CURSOR);
26340 }
26341 }
26342 }
26343
26344
26345 /* EXPORT:
26346 Erase the image of a cursor of window W from the screen. */
26347
26348 void
26349 erase_phys_cursor (struct window *w)
26350 {
26351 struct frame *f = XFRAME (w->frame);
26352 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26353 int hpos = w->phys_cursor.hpos;
26354 int vpos = w->phys_cursor.vpos;
26355 int mouse_face_here_p = 0;
26356 struct glyph_matrix *active_glyphs = w->current_matrix;
26357 struct glyph_row *cursor_row;
26358 struct glyph *cursor_glyph;
26359 enum draw_glyphs_face hl;
26360
26361 /* No cursor displayed or row invalidated => nothing to do on the
26362 screen. */
26363 if (w->phys_cursor_type == NO_CURSOR)
26364 goto mark_cursor_off;
26365
26366 /* VPOS >= active_glyphs->nrows means that window has been resized.
26367 Don't bother to erase the cursor. */
26368 if (vpos >= active_glyphs->nrows)
26369 goto mark_cursor_off;
26370
26371 /* If row containing cursor is marked invalid, there is nothing we
26372 can do. */
26373 cursor_row = MATRIX_ROW (active_glyphs, vpos);
26374 if (!cursor_row->enabled_p)
26375 goto mark_cursor_off;
26376
26377 /* If line spacing is > 0, old cursor may only be partially visible in
26378 window after split-window. So adjust visible height. */
26379 cursor_row->visible_height = min (cursor_row->visible_height,
26380 window_text_bottom_y (w) - cursor_row->y);
26381
26382 /* If row is completely invisible, don't attempt to delete a cursor which
26383 isn't there. This can happen if cursor is at top of a window, and
26384 we switch to a buffer with a header line in that window. */
26385 if (cursor_row->visible_height <= 0)
26386 goto mark_cursor_off;
26387
26388 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
26389 if (cursor_row->cursor_in_fringe_p)
26390 {
26391 cursor_row->cursor_in_fringe_p = 0;
26392 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
26393 goto mark_cursor_off;
26394 }
26395
26396 /* This can happen when the new row is shorter than the old one.
26397 In this case, either draw_glyphs or clear_end_of_line
26398 should have cleared the cursor. Note that we wouldn't be
26399 able to erase the cursor in this case because we don't have a
26400 cursor glyph at hand. */
26401 if ((cursor_row->reversed_p
26402 ? (w->phys_cursor.hpos < 0)
26403 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
26404 goto mark_cursor_off;
26405
26406 /* When the window is hscrolled, cursor hpos can legitimately be out
26407 of bounds, but we draw the cursor at the corresponding window
26408 margin in that case. */
26409 if (!cursor_row->reversed_p && hpos < 0)
26410 hpos = 0;
26411 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
26412 hpos = cursor_row->used[TEXT_AREA] - 1;
26413
26414 /* If the cursor is in the mouse face area, redisplay that when
26415 we clear the cursor. */
26416 if (! NILP (hlinfo->mouse_face_window)
26417 && coords_in_mouse_face_p (w, hpos, vpos)
26418 /* Don't redraw the cursor's spot in mouse face if it is at the
26419 end of a line (on a newline). The cursor appears there, but
26420 mouse highlighting does not. */
26421 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
26422 mouse_face_here_p = 1;
26423
26424 /* Maybe clear the display under the cursor. */
26425 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
26426 {
26427 int x, y, left_x;
26428 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
26429 int width;
26430
26431 cursor_glyph = get_phys_cursor_glyph (w);
26432 if (cursor_glyph == NULL)
26433 goto mark_cursor_off;
26434
26435 width = cursor_glyph->pixel_width;
26436 left_x = window_box_left_offset (w, TEXT_AREA);
26437 x = w->phys_cursor.x;
26438 if (x < left_x)
26439 width -= left_x - x;
26440 width = min (width, window_box_width (w, TEXT_AREA) - x);
26441 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
26442 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
26443
26444 if (width > 0)
26445 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
26446 }
26447
26448 /* Erase the cursor by redrawing the character underneath it. */
26449 if (mouse_face_here_p)
26450 hl = DRAW_MOUSE_FACE;
26451 else
26452 hl = DRAW_NORMAL_TEXT;
26453 draw_phys_cursor_glyph (w, cursor_row, hl);
26454
26455 mark_cursor_off:
26456 w->phys_cursor_on_p = 0;
26457 w->phys_cursor_type = NO_CURSOR;
26458 }
26459
26460
26461 /* EXPORT:
26462 Display or clear cursor of window W. If ON is zero, clear the
26463 cursor. If it is non-zero, display the cursor. If ON is nonzero,
26464 where to put the cursor is specified by HPOS, VPOS, X and Y. */
26465
26466 void
26467 display_and_set_cursor (struct window *w, int on,
26468 int hpos, int vpos, int x, int y)
26469 {
26470 struct frame *f = XFRAME (w->frame);
26471 int new_cursor_type;
26472 int new_cursor_width;
26473 int active_cursor;
26474 struct glyph_row *glyph_row;
26475 struct glyph *glyph;
26476
26477 /* This is pointless on invisible frames, and dangerous on garbaged
26478 windows and frames; in the latter case, the frame or window may
26479 be in the midst of changing its size, and x and y may be off the
26480 window. */
26481 if (! FRAME_VISIBLE_P (f)
26482 || FRAME_GARBAGED_P (f)
26483 || vpos >= w->current_matrix->nrows
26484 || hpos >= w->current_matrix->matrix_w)
26485 return;
26486
26487 /* If cursor is off and we want it off, return quickly. */
26488 if (!on && !w->phys_cursor_on_p)
26489 return;
26490
26491 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26492 /* If cursor row is not enabled, we don't really know where to
26493 display the cursor. */
26494 if (!glyph_row->enabled_p)
26495 {
26496 w->phys_cursor_on_p = 0;
26497 return;
26498 }
26499
26500 glyph = NULL;
26501 if (!glyph_row->exact_window_width_line_p
26502 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26503 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26504
26505 eassert (input_blocked_p ());
26506
26507 /* Set new_cursor_type to the cursor we want to be displayed. */
26508 new_cursor_type = get_window_cursor_type (w, glyph,
26509 &new_cursor_width, &active_cursor);
26510
26511 /* If cursor is currently being shown and we don't want it to be or
26512 it is in the wrong place, or the cursor type is not what we want,
26513 erase it. */
26514 if (w->phys_cursor_on_p
26515 && (!on
26516 || w->phys_cursor.x != x
26517 || w->phys_cursor.y != y
26518 || new_cursor_type != w->phys_cursor_type
26519 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26520 && new_cursor_width != w->phys_cursor_width)))
26521 erase_phys_cursor (w);
26522
26523 /* Don't check phys_cursor_on_p here because that flag is only set
26524 to zero in some cases where we know that the cursor has been
26525 completely erased, to avoid the extra work of erasing the cursor
26526 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26527 still not be visible, or it has only been partly erased. */
26528 if (on)
26529 {
26530 w->phys_cursor_ascent = glyph_row->ascent;
26531 w->phys_cursor_height = glyph_row->height;
26532
26533 /* Set phys_cursor_.* before x_draw_.* is called because some
26534 of them may need the information. */
26535 w->phys_cursor.x = x;
26536 w->phys_cursor.y = glyph_row->y;
26537 w->phys_cursor.hpos = hpos;
26538 w->phys_cursor.vpos = vpos;
26539 }
26540
26541 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26542 new_cursor_type, new_cursor_width,
26543 on, active_cursor);
26544 }
26545
26546
26547 /* Switch the display of W's cursor on or off, according to the value
26548 of ON. */
26549
26550 static void
26551 update_window_cursor (struct window *w, int on)
26552 {
26553 /* Don't update cursor in windows whose frame is in the process
26554 of being deleted. */
26555 if (w->current_matrix)
26556 {
26557 int hpos = w->phys_cursor.hpos;
26558 int vpos = w->phys_cursor.vpos;
26559 struct glyph_row *row;
26560
26561 if (vpos >= w->current_matrix->nrows
26562 || hpos >= w->current_matrix->matrix_w)
26563 return;
26564
26565 row = MATRIX_ROW (w->current_matrix, vpos);
26566
26567 /* When the window is hscrolled, cursor hpos can legitimately be
26568 out of bounds, but we draw the cursor at the corresponding
26569 window margin in that case. */
26570 if (!row->reversed_p && hpos < 0)
26571 hpos = 0;
26572 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26573 hpos = row->used[TEXT_AREA] - 1;
26574
26575 block_input ();
26576 display_and_set_cursor (w, on, hpos, vpos,
26577 w->phys_cursor.x, w->phys_cursor.y);
26578 unblock_input ();
26579 }
26580 }
26581
26582
26583 /* Call update_window_cursor with parameter ON_P on all leaf windows
26584 in the window tree rooted at W. */
26585
26586 static void
26587 update_cursor_in_window_tree (struct window *w, int on_p)
26588 {
26589 while (w)
26590 {
26591 if (WINDOWP (w->contents))
26592 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
26593 else
26594 update_window_cursor (w, on_p);
26595
26596 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26597 }
26598 }
26599
26600
26601 /* EXPORT:
26602 Display the cursor on window W, or clear it, according to ON_P.
26603 Don't change the cursor's position. */
26604
26605 void
26606 x_update_cursor (struct frame *f, int on_p)
26607 {
26608 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26609 }
26610
26611
26612 /* EXPORT:
26613 Clear the cursor of window W to background color, and mark the
26614 cursor as not shown. This is used when the text where the cursor
26615 is about to be rewritten. */
26616
26617 void
26618 x_clear_cursor (struct window *w)
26619 {
26620 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26621 update_window_cursor (w, 0);
26622 }
26623
26624 #endif /* HAVE_WINDOW_SYSTEM */
26625
26626 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26627 and MSDOS. */
26628 static void
26629 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26630 int start_hpos, int end_hpos,
26631 enum draw_glyphs_face draw)
26632 {
26633 #ifdef HAVE_WINDOW_SYSTEM
26634 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26635 {
26636 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26637 return;
26638 }
26639 #endif
26640 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26641 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26642 #endif
26643 }
26644
26645 /* Display the active region described by mouse_face_* according to DRAW. */
26646
26647 static void
26648 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26649 {
26650 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26651 struct frame *f = XFRAME (WINDOW_FRAME (w));
26652
26653 if (/* If window is in the process of being destroyed, don't bother
26654 to do anything. */
26655 w->current_matrix != NULL
26656 /* Don't update mouse highlight if hidden */
26657 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26658 /* Recognize when we are called to operate on rows that don't exist
26659 anymore. This can happen when a window is split. */
26660 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26661 {
26662 int phys_cursor_on_p = w->phys_cursor_on_p;
26663 struct glyph_row *row, *first, *last;
26664
26665 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26666 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26667
26668 for (row = first; row <= last && row->enabled_p; ++row)
26669 {
26670 int start_hpos, end_hpos, start_x;
26671
26672 /* For all but the first row, the highlight starts at column 0. */
26673 if (row == first)
26674 {
26675 /* R2L rows have BEG and END in reversed order, but the
26676 screen drawing geometry is always left to right. So
26677 we need to mirror the beginning and end of the
26678 highlighted area in R2L rows. */
26679 if (!row->reversed_p)
26680 {
26681 start_hpos = hlinfo->mouse_face_beg_col;
26682 start_x = hlinfo->mouse_face_beg_x;
26683 }
26684 else if (row == last)
26685 {
26686 start_hpos = hlinfo->mouse_face_end_col;
26687 start_x = hlinfo->mouse_face_end_x;
26688 }
26689 else
26690 {
26691 start_hpos = 0;
26692 start_x = 0;
26693 }
26694 }
26695 else if (row->reversed_p && row == last)
26696 {
26697 start_hpos = hlinfo->mouse_face_end_col;
26698 start_x = hlinfo->mouse_face_end_x;
26699 }
26700 else
26701 {
26702 start_hpos = 0;
26703 start_x = 0;
26704 }
26705
26706 if (row == last)
26707 {
26708 if (!row->reversed_p)
26709 end_hpos = hlinfo->mouse_face_end_col;
26710 else if (row == first)
26711 end_hpos = hlinfo->mouse_face_beg_col;
26712 else
26713 {
26714 end_hpos = row->used[TEXT_AREA];
26715 if (draw == DRAW_NORMAL_TEXT)
26716 row->fill_line_p = 1; /* Clear to end of line */
26717 }
26718 }
26719 else if (row->reversed_p && row == first)
26720 end_hpos = hlinfo->mouse_face_beg_col;
26721 else
26722 {
26723 end_hpos = row->used[TEXT_AREA];
26724 if (draw == DRAW_NORMAL_TEXT)
26725 row->fill_line_p = 1; /* Clear to end of line */
26726 }
26727
26728 if (end_hpos > start_hpos)
26729 {
26730 draw_row_with_mouse_face (w, start_x, row,
26731 start_hpos, end_hpos, draw);
26732
26733 row->mouse_face_p
26734 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26735 }
26736 }
26737
26738 #ifdef HAVE_WINDOW_SYSTEM
26739 /* When we've written over the cursor, arrange for it to
26740 be displayed again. */
26741 if (FRAME_WINDOW_P (f)
26742 && phys_cursor_on_p && !w->phys_cursor_on_p)
26743 {
26744 int hpos = w->phys_cursor.hpos;
26745
26746 /* When the window is hscrolled, cursor hpos can legitimately be
26747 out of bounds, but we draw the cursor at the corresponding
26748 window margin in that case. */
26749 if (!row->reversed_p && hpos < 0)
26750 hpos = 0;
26751 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26752 hpos = row->used[TEXT_AREA] - 1;
26753
26754 block_input ();
26755 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26756 w->phys_cursor.x, w->phys_cursor.y);
26757 unblock_input ();
26758 }
26759 #endif /* HAVE_WINDOW_SYSTEM */
26760 }
26761
26762 #ifdef HAVE_WINDOW_SYSTEM
26763 /* Change the mouse cursor. */
26764 if (FRAME_WINDOW_P (f))
26765 {
26766 if (draw == DRAW_NORMAL_TEXT
26767 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26768 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26769 else if (draw == DRAW_MOUSE_FACE)
26770 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26771 else
26772 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26773 }
26774 #endif /* HAVE_WINDOW_SYSTEM */
26775 }
26776
26777 /* EXPORT:
26778 Clear out the mouse-highlighted active region.
26779 Redraw it un-highlighted first. Value is non-zero if mouse
26780 face was actually drawn unhighlighted. */
26781
26782 int
26783 clear_mouse_face (Mouse_HLInfo *hlinfo)
26784 {
26785 int cleared = 0;
26786
26787 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26788 {
26789 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26790 cleared = 1;
26791 }
26792
26793 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26794 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26795 hlinfo->mouse_face_window = Qnil;
26796 hlinfo->mouse_face_overlay = Qnil;
26797 return cleared;
26798 }
26799
26800 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26801 within the mouse face on that window. */
26802 static int
26803 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26804 {
26805 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26806
26807 /* Quickly resolve the easy cases. */
26808 if (!(WINDOWP (hlinfo->mouse_face_window)
26809 && XWINDOW (hlinfo->mouse_face_window) == w))
26810 return 0;
26811 if (vpos < hlinfo->mouse_face_beg_row
26812 || vpos > hlinfo->mouse_face_end_row)
26813 return 0;
26814 if (vpos > hlinfo->mouse_face_beg_row
26815 && vpos < hlinfo->mouse_face_end_row)
26816 return 1;
26817
26818 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26819 {
26820 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26821 {
26822 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26823 return 1;
26824 }
26825 else if ((vpos == hlinfo->mouse_face_beg_row
26826 && hpos >= hlinfo->mouse_face_beg_col)
26827 || (vpos == hlinfo->mouse_face_end_row
26828 && hpos < hlinfo->mouse_face_end_col))
26829 return 1;
26830 }
26831 else
26832 {
26833 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26834 {
26835 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26836 return 1;
26837 }
26838 else if ((vpos == hlinfo->mouse_face_beg_row
26839 && hpos <= hlinfo->mouse_face_beg_col)
26840 || (vpos == hlinfo->mouse_face_end_row
26841 && hpos > hlinfo->mouse_face_end_col))
26842 return 1;
26843 }
26844 return 0;
26845 }
26846
26847
26848 /* EXPORT:
26849 Non-zero if physical cursor of window W is within mouse face. */
26850
26851 int
26852 cursor_in_mouse_face_p (struct window *w)
26853 {
26854 int hpos = w->phys_cursor.hpos;
26855 int vpos = w->phys_cursor.vpos;
26856 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26857
26858 /* When the window is hscrolled, cursor hpos can legitimately be out
26859 of bounds, but we draw the cursor at the corresponding window
26860 margin in that case. */
26861 if (!row->reversed_p && hpos < 0)
26862 hpos = 0;
26863 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26864 hpos = row->used[TEXT_AREA] - 1;
26865
26866 return coords_in_mouse_face_p (w, hpos, vpos);
26867 }
26868
26869
26870 \f
26871 /* Find the glyph rows START_ROW and END_ROW of window W that display
26872 characters between buffer positions START_CHARPOS and END_CHARPOS
26873 (excluding END_CHARPOS). DISP_STRING is a display string that
26874 covers these buffer positions. This is similar to
26875 row_containing_pos, but is more accurate when bidi reordering makes
26876 buffer positions change non-linearly with glyph rows. */
26877 static void
26878 rows_from_pos_range (struct window *w,
26879 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26880 Lisp_Object disp_string,
26881 struct glyph_row **start, struct glyph_row **end)
26882 {
26883 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26884 int last_y = window_text_bottom_y (w);
26885 struct glyph_row *row;
26886
26887 *start = NULL;
26888 *end = NULL;
26889
26890 while (!first->enabled_p
26891 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26892 first++;
26893
26894 /* Find the START row. */
26895 for (row = first;
26896 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26897 row++)
26898 {
26899 /* A row can potentially be the START row if the range of the
26900 characters it displays intersects the range
26901 [START_CHARPOS..END_CHARPOS). */
26902 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26903 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26904 /* See the commentary in row_containing_pos, for the
26905 explanation of the complicated way to check whether
26906 some position is beyond the end of the characters
26907 displayed by a row. */
26908 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26909 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26910 && !row->ends_at_zv_p
26911 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26912 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26913 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26914 && !row->ends_at_zv_p
26915 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26916 {
26917 /* Found a candidate row. Now make sure at least one of the
26918 glyphs it displays has a charpos from the range
26919 [START_CHARPOS..END_CHARPOS).
26920
26921 This is not obvious because bidi reordering could make
26922 buffer positions of a row be 1,2,3,102,101,100, and if we
26923 want to highlight characters in [50..60), we don't want
26924 this row, even though [50..60) does intersect [1..103),
26925 the range of character positions given by the row's start
26926 and end positions. */
26927 struct glyph *g = row->glyphs[TEXT_AREA];
26928 struct glyph *e = g + row->used[TEXT_AREA];
26929
26930 while (g < e)
26931 {
26932 if (((BUFFERP (g->object) || INTEGERP (g->object))
26933 && start_charpos <= g->charpos && g->charpos < end_charpos)
26934 /* A glyph that comes from DISP_STRING is by
26935 definition to be highlighted. */
26936 || EQ (g->object, disp_string))
26937 *start = row;
26938 g++;
26939 }
26940 if (*start)
26941 break;
26942 }
26943 }
26944
26945 /* Find the END row. */
26946 if (!*start
26947 /* If the last row is partially visible, start looking for END
26948 from that row, instead of starting from FIRST. */
26949 && !(row->enabled_p
26950 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26951 row = first;
26952 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26953 {
26954 struct glyph_row *next = row + 1;
26955 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26956
26957 if (!next->enabled_p
26958 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26959 /* The first row >= START whose range of displayed characters
26960 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26961 is the row END + 1. */
26962 || (start_charpos < next_start
26963 && end_charpos < next_start)
26964 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26965 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26966 && !next->ends_at_zv_p
26967 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26968 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26969 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26970 && !next->ends_at_zv_p
26971 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26972 {
26973 *end = row;
26974 break;
26975 }
26976 else
26977 {
26978 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26979 but none of the characters it displays are in the range, it is
26980 also END + 1. */
26981 struct glyph *g = next->glyphs[TEXT_AREA];
26982 struct glyph *s = g;
26983 struct glyph *e = g + next->used[TEXT_AREA];
26984
26985 while (g < e)
26986 {
26987 if (((BUFFERP (g->object) || INTEGERP (g->object))
26988 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26989 /* If the buffer position of the first glyph in
26990 the row is equal to END_CHARPOS, it means
26991 the last character to be highlighted is the
26992 newline of ROW, and we must consider NEXT as
26993 END, not END+1. */
26994 || (((!next->reversed_p && g == s)
26995 || (next->reversed_p && g == e - 1))
26996 && (g->charpos == end_charpos
26997 /* Special case for when NEXT is an
26998 empty line at ZV. */
26999 || (g->charpos == -1
27000 && !row->ends_at_zv_p
27001 && next_start == end_charpos)))))
27002 /* A glyph that comes from DISP_STRING is by
27003 definition to be highlighted. */
27004 || EQ (g->object, disp_string))
27005 break;
27006 g++;
27007 }
27008 if (g == e)
27009 {
27010 *end = row;
27011 break;
27012 }
27013 /* The first row that ends at ZV must be the last to be
27014 highlighted. */
27015 else if (next->ends_at_zv_p)
27016 {
27017 *end = next;
27018 break;
27019 }
27020 }
27021 }
27022 }
27023
27024 /* This function sets the mouse_face_* elements of HLINFO, assuming
27025 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
27026 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
27027 for the overlay or run of text properties specifying the mouse
27028 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
27029 before-string and after-string that must also be highlighted.
27030 DISP_STRING, if non-nil, is a display string that may cover some
27031 or all of the highlighted text. */
27032
27033 static void
27034 mouse_face_from_buffer_pos (Lisp_Object window,
27035 Mouse_HLInfo *hlinfo,
27036 ptrdiff_t mouse_charpos,
27037 ptrdiff_t start_charpos,
27038 ptrdiff_t end_charpos,
27039 Lisp_Object before_string,
27040 Lisp_Object after_string,
27041 Lisp_Object disp_string)
27042 {
27043 struct window *w = XWINDOW (window);
27044 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27045 struct glyph_row *r1, *r2;
27046 struct glyph *glyph, *end;
27047 ptrdiff_t ignore, pos;
27048 int x;
27049
27050 eassert (NILP (disp_string) || STRINGP (disp_string));
27051 eassert (NILP (before_string) || STRINGP (before_string));
27052 eassert (NILP (after_string) || STRINGP (after_string));
27053
27054 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
27055 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
27056 if (r1 == NULL)
27057 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27058 /* If the before-string or display-string contains newlines,
27059 rows_from_pos_range skips to its last row. Move back. */
27060 if (!NILP (before_string) || !NILP (disp_string))
27061 {
27062 struct glyph_row *prev;
27063 while ((prev = r1 - 1, prev >= first)
27064 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
27065 && prev->used[TEXT_AREA] > 0)
27066 {
27067 struct glyph *beg = prev->glyphs[TEXT_AREA];
27068 glyph = beg + prev->used[TEXT_AREA];
27069 while (--glyph >= beg && INTEGERP (glyph->object));
27070 if (glyph < beg
27071 || !(EQ (glyph->object, before_string)
27072 || EQ (glyph->object, disp_string)))
27073 break;
27074 r1 = prev;
27075 }
27076 }
27077 if (r2 == NULL)
27078 {
27079 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27080 hlinfo->mouse_face_past_end = 1;
27081 }
27082 else if (!NILP (after_string))
27083 {
27084 /* If the after-string has newlines, advance to its last row. */
27085 struct glyph_row *next;
27086 struct glyph_row *last
27087 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27088
27089 for (next = r2 + 1;
27090 next <= last
27091 && next->used[TEXT_AREA] > 0
27092 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
27093 ++next)
27094 r2 = next;
27095 }
27096 /* The rest of the display engine assumes that mouse_face_beg_row is
27097 either above mouse_face_end_row or identical to it. But with
27098 bidi-reordered continued lines, the row for START_CHARPOS could
27099 be below the row for END_CHARPOS. If so, swap the rows and store
27100 them in correct order. */
27101 if (r1->y > r2->y)
27102 {
27103 struct glyph_row *tem = r2;
27104
27105 r2 = r1;
27106 r1 = tem;
27107 }
27108
27109 hlinfo->mouse_face_beg_y = r1->y;
27110 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
27111 hlinfo->mouse_face_end_y = r2->y;
27112 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
27113
27114 /* For a bidi-reordered row, the positions of BEFORE_STRING,
27115 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
27116 could be anywhere in the row and in any order. The strategy
27117 below is to find the leftmost and the rightmost glyph that
27118 belongs to either of these 3 strings, or whose position is
27119 between START_CHARPOS and END_CHARPOS, and highlight all the
27120 glyphs between those two. This may cover more than just the text
27121 between START_CHARPOS and END_CHARPOS if the range of characters
27122 strides the bidi level boundary, e.g. if the beginning is in R2L
27123 text while the end is in L2R text or vice versa. */
27124 if (!r1->reversed_p)
27125 {
27126 /* This row is in a left to right paragraph. Scan it left to
27127 right. */
27128 glyph = r1->glyphs[TEXT_AREA];
27129 end = glyph + r1->used[TEXT_AREA];
27130 x = r1->x;
27131
27132 /* Skip truncation glyphs at the start of the glyph row. */
27133 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27134 for (; glyph < end
27135 && INTEGERP (glyph->object)
27136 && glyph->charpos < 0;
27137 ++glyph)
27138 x += glyph->pixel_width;
27139
27140 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27141 or DISP_STRING, and the first glyph from buffer whose
27142 position is between START_CHARPOS and END_CHARPOS. */
27143 for (; glyph < end
27144 && !INTEGERP (glyph->object)
27145 && !EQ (glyph->object, disp_string)
27146 && !(BUFFERP (glyph->object)
27147 && (glyph->charpos >= start_charpos
27148 && glyph->charpos < end_charpos));
27149 ++glyph)
27150 {
27151 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27152 are present at buffer positions between START_CHARPOS and
27153 END_CHARPOS, or if they come from an overlay. */
27154 if (EQ (glyph->object, before_string))
27155 {
27156 pos = string_buffer_position (before_string,
27157 start_charpos);
27158 /* If pos == 0, it means before_string came from an
27159 overlay, not from a buffer position. */
27160 if (!pos || (pos >= start_charpos && pos < end_charpos))
27161 break;
27162 }
27163 else if (EQ (glyph->object, after_string))
27164 {
27165 pos = string_buffer_position (after_string, end_charpos);
27166 if (!pos || (pos >= start_charpos && pos < end_charpos))
27167 break;
27168 }
27169 x += glyph->pixel_width;
27170 }
27171 hlinfo->mouse_face_beg_x = x;
27172 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27173 }
27174 else
27175 {
27176 /* This row is in a right to left paragraph. Scan it right to
27177 left. */
27178 struct glyph *g;
27179
27180 end = r1->glyphs[TEXT_AREA] - 1;
27181 glyph = end + r1->used[TEXT_AREA];
27182
27183 /* Skip truncation glyphs at the start of the glyph row. */
27184 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27185 for (; glyph > end
27186 && INTEGERP (glyph->object)
27187 && glyph->charpos < 0;
27188 --glyph)
27189 ;
27190
27191 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27192 or DISP_STRING, and the first glyph from buffer whose
27193 position is between START_CHARPOS and END_CHARPOS. */
27194 for (; glyph > end
27195 && !INTEGERP (glyph->object)
27196 && !EQ (glyph->object, disp_string)
27197 && !(BUFFERP (glyph->object)
27198 && (glyph->charpos >= start_charpos
27199 && glyph->charpos < end_charpos));
27200 --glyph)
27201 {
27202 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27203 are present at buffer positions between START_CHARPOS and
27204 END_CHARPOS, or if they come from an overlay. */
27205 if (EQ (glyph->object, before_string))
27206 {
27207 pos = string_buffer_position (before_string, start_charpos);
27208 /* If pos == 0, it means before_string came from an
27209 overlay, not from a buffer position. */
27210 if (!pos || (pos >= start_charpos && pos < end_charpos))
27211 break;
27212 }
27213 else if (EQ (glyph->object, after_string))
27214 {
27215 pos = string_buffer_position (after_string, end_charpos);
27216 if (!pos || (pos >= start_charpos && pos < end_charpos))
27217 break;
27218 }
27219 }
27220
27221 glyph++; /* first glyph to the right of the highlighted area */
27222 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
27223 x += g->pixel_width;
27224 hlinfo->mouse_face_beg_x = x;
27225 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27226 }
27227
27228 /* If the highlight ends in a different row, compute GLYPH and END
27229 for the end row. Otherwise, reuse the values computed above for
27230 the row where the highlight begins. */
27231 if (r2 != r1)
27232 {
27233 if (!r2->reversed_p)
27234 {
27235 glyph = r2->glyphs[TEXT_AREA];
27236 end = glyph + r2->used[TEXT_AREA];
27237 x = r2->x;
27238 }
27239 else
27240 {
27241 end = r2->glyphs[TEXT_AREA] - 1;
27242 glyph = end + r2->used[TEXT_AREA];
27243 }
27244 }
27245
27246 if (!r2->reversed_p)
27247 {
27248 /* Skip truncation and continuation glyphs near the end of the
27249 row, and also blanks and stretch glyphs inserted by
27250 extend_face_to_end_of_line. */
27251 while (end > glyph
27252 && INTEGERP ((end - 1)->object))
27253 --end;
27254 /* Scan the rest of the glyph row from the end, looking for the
27255 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27256 DISP_STRING, or whose position is between START_CHARPOS
27257 and END_CHARPOS */
27258 for (--end;
27259 end > glyph
27260 && !INTEGERP (end->object)
27261 && !EQ (end->object, disp_string)
27262 && !(BUFFERP (end->object)
27263 && (end->charpos >= start_charpos
27264 && end->charpos < end_charpos));
27265 --end)
27266 {
27267 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27268 are present at buffer positions between START_CHARPOS and
27269 END_CHARPOS, or if they come from an overlay. */
27270 if (EQ (end->object, before_string))
27271 {
27272 pos = string_buffer_position (before_string, start_charpos);
27273 if (!pos || (pos >= start_charpos && pos < end_charpos))
27274 break;
27275 }
27276 else if (EQ (end->object, after_string))
27277 {
27278 pos = string_buffer_position (after_string, end_charpos);
27279 if (!pos || (pos >= start_charpos && pos < end_charpos))
27280 break;
27281 }
27282 }
27283 /* Find the X coordinate of the last glyph to be highlighted. */
27284 for (; glyph <= end; ++glyph)
27285 x += glyph->pixel_width;
27286
27287 hlinfo->mouse_face_end_x = x;
27288 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
27289 }
27290 else
27291 {
27292 /* Skip truncation and continuation glyphs near the end of the
27293 row, and also blanks and stretch glyphs inserted by
27294 extend_face_to_end_of_line. */
27295 x = r2->x;
27296 end++;
27297 while (end < glyph
27298 && INTEGERP (end->object))
27299 {
27300 x += end->pixel_width;
27301 ++end;
27302 }
27303 /* Scan the rest of the glyph row from the end, looking for the
27304 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27305 DISP_STRING, or whose position is between START_CHARPOS
27306 and END_CHARPOS */
27307 for ( ;
27308 end < glyph
27309 && !INTEGERP (end->object)
27310 && !EQ (end->object, disp_string)
27311 && !(BUFFERP (end->object)
27312 && (end->charpos >= start_charpos
27313 && end->charpos < end_charpos));
27314 ++end)
27315 {
27316 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27317 are present at buffer positions between START_CHARPOS and
27318 END_CHARPOS, or if they come from an overlay. */
27319 if (EQ (end->object, before_string))
27320 {
27321 pos = string_buffer_position (before_string, start_charpos);
27322 if (!pos || (pos >= start_charpos && pos < end_charpos))
27323 break;
27324 }
27325 else if (EQ (end->object, after_string))
27326 {
27327 pos = string_buffer_position (after_string, end_charpos);
27328 if (!pos || (pos >= start_charpos && pos < end_charpos))
27329 break;
27330 }
27331 x += end->pixel_width;
27332 }
27333 /* If we exited the above loop because we arrived at the last
27334 glyph of the row, and its buffer position is still not in
27335 range, it means the last character in range is the preceding
27336 newline. Bump the end column and x values to get past the
27337 last glyph. */
27338 if (end == glyph
27339 && BUFFERP (end->object)
27340 && (end->charpos < start_charpos
27341 || end->charpos >= end_charpos))
27342 {
27343 x += end->pixel_width;
27344 ++end;
27345 }
27346 hlinfo->mouse_face_end_x = x;
27347 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
27348 }
27349
27350 hlinfo->mouse_face_window = window;
27351 hlinfo->mouse_face_face_id
27352 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
27353 mouse_charpos + 1,
27354 !hlinfo->mouse_face_hidden, -1);
27355 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27356 }
27357
27358 /* The following function is not used anymore (replaced with
27359 mouse_face_from_string_pos), but I leave it here for the time
27360 being, in case someone would. */
27361
27362 #if 0 /* not used */
27363
27364 /* Find the position of the glyph for position POS in OBJECT in
27365 window W's current matrix, and return in *X, *Y the pixel
27366 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
27367
27368 RIGHT_P non-zero means return the position of the right edge of the
27369 glyph, RIGHT_P zero means return the left edge position.
27370
27371 If no glyph for POS exists in the matrix, return the position of
27372 the glyph with the next smaller position that is in the matrix, if
27373 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
27374 exists in the matrix, return the position of the glyph with the
27375 next larger position in OBJECT.
27376
27377 Value is non-zero if a glyph was found. */
27378
27379 static int
27380 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
27381 int *hpos, int *vpos, int *x, int *y, int right_p)
27382 {
27383 int yb = window_text_bottom_y (w);
27384 struct glyph_row *r;
27385 struct glyph *best_glyph = NULL;
27386 struct glyph_row *best_row = NULL;
27387 int best_x = 0;
27388
27389 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27390 r->enabled_p && r->y < yb;
27391 ++r)
27392 {
27393 struct glyph *g = r->glyphs[TEXT_AREA];
27394 struct glyph *e = g + r->used[TEXT_AREA];
27395 int gx;
27396
27397 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27398 if (EQ (g->object, object))
27399 {
27400 if (g->charpos == pos)
27401 {
27402 best_glyph = g;
27403 best_x = gx;
27404 best_row = r;
27405 goto found;
27406 }
27407 else if (best_glyph == NULL
27408 || ((eabs (g->charpos - pos)
27409 < eabs (best_glyph->charpos - pos))
27410 && (right_p
27411 ? g->charpos < pos
27412 : g->charpos > pos)))
27413 {
27414 best_glyph = g;
27415 best_x = gx;
27416 best_row = r;
27417 }
27418 }
27419 }
27420
27421 found:
27422
27423 if (best_glyph)
27424 {
27425 *x = best_x;
27426 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
27427
27428 if (right_p)
27429 {
27430 *x += best_glyph->pixel_width;
27431 ++*hpos;
27432 }
27433
27434 *y = best_row->y;
27435 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
27436 }
27437
27438 return best_glyph != NULL;
27439 }
27440 #endif /* not used */
27441
27442 /* Find the positions of the first and the last glyphs in window W's
27443 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
27444 (assumed to be a string), and return in HLINFO's mouse_face_*
27445 members the pixel and column/row coordinates of those glyphs. */
27446
27447 static void
27448 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
27449 Lisp_Object object,
27450 ptrdiff_t startpos, ptrdiff_t endpos)
27451 {
27452 int yb = window_text_bottom_y (w);
27453 struct glyph_row *r;
27454 struct glyph *g, *e;
27455 int gx;
27456 int found = 0;
27457
27458 /* Find the glyph row with at least one position in the range
27459 [STARTPOS..ENDPOS], and the first glyph in that row whose
27460 position belongs to that range. */
27461 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27462 r->enabled_p && r->y < yb;
27463 ++r)
27464 {
27465 if (!r->reversed_p)
27466 {
27467 g = r->glyphs[TEXT_AREA];
27468 e = g + r->used[TEXT_AREA];
27469 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27470 if (EQ (g->object, object)
27471 && startpos <= g->charpos && g->charpos <= endpos)
27472 {
27473 hlinfo->mouse_face_beg_row
27474 = MATRIX_ROW_VPOS (r, w->current_matrix);
27475 hlinfo->mouse_face_beg_y = r->y;
27476 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27477 hlinfo->mouse_face_beg_x = gx;
27478 found = 1;
27479 break;
27480 }
27481 }
27482 else
27483 {
27484 struct glyph *g1;
27485
27486 e = r->glyphs[TEXT_AREA];
27487 g = e + r->used[TEXT_AREA];
27488 for ( ; g > e; --g)
27489 if (EQ ((g-1)->object, object)
27490 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
27491 {
27492 hlinfo->mouse_face_beg_row
27493 = MATRIX_ROW_VPOS (r, w->current_matrix);
27494 hlinfo->mouse_face_beg_y = r->y;
27495 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27496 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27497 gx += g1->pixel_width;
27498 hlinfo->mouse_face_beg_x = gx;
27499 found = 1;
27500 break;
27501 }
27502 }
27503 if (found)
27504 break;
27505 }
27506
27507 if (!found)
27508 return;
27509
27510 /* Starting with the next row, look for the first row which does NOT
27511 include any glyphs whose positions are in the range. */
27512 for (++r; r->enabled_p && r->y < yb; ++r)
27513 {
27514 g = r->glyphs[TEXT_AREA];
27515 e = g + r->used[TEXT_AREA];
27516 found = 0;
27517 for ( ; g < e; ++g)
27518 if (EQ (g->object, object)
27519 && startpos <= g->charpos && g->charpos <= endpos)
27520 {
27521 found = 1;
27522 break;
27523 }
27524 if (!found)
27525 break;
27526 }
27527
27528 /* The highlighted region ends on the previous row. */
27529 r--;
27530
27531 /* Set the end row and its vertical pixel coordinate. */
27532 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
27533 hlinfo->mouse_face_end_y = r->y;
27534
27535 /* Compute and set the end column and the end column's horizontal
27536 pixel coordinate. */
27537 if (!r->reversed_p)
27538 {
27539 g = r->glyphs[TEXT_AREA];
27540 e = g + r->used[TEXT_AREA];
27541 for ( ; e > g; --e)
27542 if (EQ ((e-1)->object, object)
27543 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
27544 break;
27545 hlinfo->mouse_face_end_col = e - g;
27546
27547 for (gx = r->x; g < e; ++g)
27548 gx += g->pixel_width;
27549 hlinfo->mouse_face_end_x = gx;
27550 }
27551 else
27552 {
27553 e = r->glyphs[TEXT_AREA];
27554 g = e + r->used[TEXT_AREA];
27555 for (gx = r->x ; e < g; ++e)
27556 {
27557 if (EQ (e->object, object)
27558 && startpos <= e->charpos && e->charpos <= endpos)
27559 break;
27560 gx += e->pixel_width;
27561 }
27562 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27563 hlinfo->mouse_face_end_x = gx;
27564 }
27565 }
27566
27567 #ifdef HAVE_WINDOW_SYSTEM
27568
27569 /* See if position X, Y is within a hot-spot of an image. */
27570
27571 static int
27572 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27573 {
27574 if (!CONSP (hot_spot))
27575 return 0;
27576
27577 if (EQ (XCAR (hot_spot), Qrect))
27578 {
27579 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27580 Lisp_Object rect = XCDR (hot_spot);
27581 Lisp_Object tem;
27582 if (!CONSP (rect))
27583 return 0;
27584 if (!CONSP (XCAR (rect)))
27585 return 0;
27586 if (!CONSP (XCDR (rect)))
27587 return 0;
27588 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27589 return 0;
27590 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27591 return 0;
27592 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27593 return 0;
27594 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27595 return 0;
27596 return 1;
27597 }
27598 else if (EQ (XCAR (hot_spot), Qcircle))
27599 {
27600 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27601 Lisp_Object circ = XCDR (hot_spot);
27602 Lisp_Object lr, lx0, ly0;
27603 if (CONSP (circ)
27604 && CONSP (XCAR (circ))
27605 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27606 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27607 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27608 {
27609 double r = XFLOATINT (lr);
27610 double dx = XINT (lx0) - x;
27611 double dy = XINT (ly0) - y;
27612 return (dx * dx + dy * dy <= r * r);
27613 }
27614 }
27615 else if (EQ (XCAR (hot_spot), Qpoly))
27616 {
27617 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27618 if (VECTORP (XCDR (hot_spot)))
27619 {
27620 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27621 Lisp_Object *poly = v->contents;
27622 ptrdiff_t n = v->header.size;
27623 ptrdiff_t i;
27624 int inside = 0;
27625 Lisp_Object lx, ly;
27626 int x0, y0;
27627
27628 /* Need an even number of coordinates, and at least 3 edges. */
27629 if (n < 6 || n & 1)
27630 return 0;
27631
27632 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27633 If count is odd, we are inside polygon. Pixels on edges
27634 may or may not be included depending on actual geometry of the
27635 polygon. */
27636 if ((lx = poly[n-2], !INTEGERP (lx))
27637 || (ly = poly[n-1], !INTEGERP (lx)))
27638 return 0;
27639 x0 = XINT (lx), y0 = XINT (ly);
27640 for (i = 0; i < n; i += 2)
27641 {
27642 int x1 = x0, y1 = y0;
27643 if ((lx = poly[i], !INTEGERP (lx))
27644 || (ly = poly[i+1], !INTEGERP (ly)))
27645 return 0;
27646 x0 = XINT (lx), y0 = XINT (ly);
27647
27648 /* Does this segment cross the X line? */
27649 if (x0 >= x)
27650 {
27651 if (x1 >= x)
27652 continue;
27653 }
27654 else if (x1 < x)
27655 continue;
27656 if (y > y0 && y > y1)
27657 continue;
27658 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27659 inside = !inside;
27660 }
27661 return inside;
27662 }
27663 }
27664 return 0;
27665 }
27666
27667 Lisp_Object
27668 find_hot_spot (Lisp_Object map, int x, int y)
27669 {
27670 while (CONSP (map))
27671 {
27672 if (CONSP (XCAR (map))
27673 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27674 return XCAR (map);
27675 map = XCDR (map);
27676 }
27677
27678 return Qnil;
27679 }
27680
27681 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27682 3, 3, 0,
27683 doc: /* Lookup in image map MAP coordinates X and Y.
27684 An image map is an alist where each element has the format (AREA ID PLIST).
27685 An AREA is specified as either a rectangle, a circle, or a polygon:
27686 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27687 pixel coordinates of the upper left and bottom right corners.
27688 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27689 and the radius of the circle; r may be a float or integer.
27690 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27691 vector describes one corner in the polygon.
27692 Returns the alist element for the first matching AREA in MAP. */)
27693 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27694 {
27695 if (NILP (map))
27696 return Qnil;
27697
27698 CHECK_NUMBER (x);
27699 CHECK_NUMBER (y);
27700
27701 return find_hot_spot (map,
27702 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27703 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27704 }
27705
27706
27707 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27708 static void
27709 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27710 {
27711 /* Do not change cursor shape while dragging mouse. */
27712 if (!NILP (do_mouse_tracking))
27713 return;
27714
27715 if (!NILP (pointer))
27716 {
27717 if (EQ (pointer, Qarrow))
27718 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27719 else if (EQ (pointer, Qhand))
27720 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27721 else if (EQ (pointer, Qtext))
27722 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27723 else if (EQ (pointer, intern ("hdrag")))
27724 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27725 #ifdef HAVE_X_WINDOWS
27726 else if (EQ (pointer, intern ("vdrag")))
27727 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27728 #endif
27729 else if (EQ (pointer, intern ("hourglass")))
27730 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27731 else if (EQ (pointer, Qmodeline))
27732 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27733 else
27734 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27735 }
27736
27737 if (cursor != No_Cursor)
27738 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27739 }
27740
27741 #endif /* HAVE_WINDOW_SYSTEM */
27742
27743 /* Take proper action when mouse has moved to the mode or header line
27744 or marginal area AREA of window W, x-position X and y-position Y.
27745 X is relative to the start of the text display area of W, so the
27746 width of bitmap areas and scroll bars must be subtracted to get a
27747 position relative to the start of the mode line. */
27748
27749 static void
27750 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27751 enum window_part area)
27752 {
27753 struct window *w = XWINDOW (window);
27754 struct frame *f = XFRAME (w->frame);
27755 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27756 #ifdef HAVE_WINDOW_SYSTEM
27757 Display_Info *dpyinfo;
27758 #endif
27759 Cursor cursor = No_Cursor;
27760 Lisp_Object pointer = Qnil;
27761 int dx, dy, width, height;
27762 ptrdiff_t charpos;
27763 Lisp_Object string, object = Qnil;
27764 Lisp_Object pos IF_LINT (= Qnil), help;
27765
27766 Lisp_Object mouse_face;
27767 int original_x_pixel = x;
27768 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27769 struct glyph_row *row IF_LINT (= 0);
27770
27771 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27772 {
27773 int x0;
27774 struct glyph *end;
27775
27776 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27777 returns them in row/column units! */
27778 string = mode_line_string (w, area, &x, &y, &charpos,
27779 &object, &dx, &dy, &width, &height);
27780
27781 row = (area == ON_MODE_LINE
27782 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27783 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27784
27785 /* Find the glyph under the mouse pointer. */
27786 if (row->mode_line_p && row->enabled_p)
27787 {
27788 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27789 end = glyph + row->used[TEXT_AREA];
27790
27791 for (x0 = original_x_pixel;
27792 glyph < end && x0 >= glyph->pixel_width;
27793 ++glyph)
27794 x0 -= glyph->pixel_width;
27795
27796 if (glyph >= end)
27797 glyph = NULL;
27798 }
27799 }
27800 else
27801 {
27802 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27803 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27804 returns them in row/column units! */
27805 string = marginal_area_string (w, area, &x, &y, &charpos,
27806 &object, &dx, &dy, &width, &height);
27807 }
27808
27809 help = Qnil;
27810
27811 #ifdef HAVE_WINDOW_SYSTEM
27812 if (IMAGEP (object))
27813 {
27814 Lisp_Object image_map, hotspot;
27815 if ((image_map = Fplist_get (XCDR (object), QCmap),
27816 !NILP (image_map))
27817 && (hotspot = find_hot_spot (image_map, dx, dy),
27818 CONSP (hotspot))
27819 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27820 {
27821 Lisp_Object plist;
27822
27823 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27824 If so, we could look for mouse-enter, mouse-leave
27825 properties in PLIST (and do something...). */
27826 hotspot = XCDR (hotspot);
27827 if (CONSP (hotspot)
27828 && (plist = XCAR (hotspot), CONSP (plist)))
27829 {
27830 pointer = Fplist_get (plist, Qpointer);
27831 if (NILP (pointer))
27832 pointer = Qhand;
27833 help = Fplist_get (plist, Qhelp_echo);
27834 if (!NILP (help))
27835 {
27836 help_echo_string = help;
27837 XSETWINDOW (help_echo_window, w);
27838 help_echo_object = w->contents;
27839 help_echo_pos = charpos;
27840 }
27841 }
27842 }
27843 if (NILP (pointer))
27844 pointer = Fplist_get (XCDR (object), QCpointer);
27845 }
27846 #endif /* HAVE_WINDOW_SYSTEM */
27847
27848 if (STRINGP (string))
27849 pos = make_number (charpos);
27850
27851 /* Set the help text and mouse pointer. If the mouse is on a part
27852 of the mode line without any text (e.g. past the right edge of
27853 the mode line text), use the default help text and pointer. */
27854 if (STRINGP (string) || area == ON_MODE_LINE)
27855 {
27856 /* Arrange to display the help by setting the global variables
27857 help_echo_string, help_echo_object, and help_echo_pos. */
27858 if (NILP (help))
27859 {
27860 if (STRINGP (string))
27861 help = Fget_text_property (pos, Qhelp_echo, string);
27862
27863 if (!NILP (help))
27864 {
27865 help_echo_string = help;
27866 XSETWINDOW (help_echo_window, w);
27867 help_echo_object = string;
27868 help_echo_pos = charpos;
27869 }
27870 else if (area == ON_MODE_LINE)
27871 {
27872 Lisp_Object default_help
27873 = buffer_local_value_1 (Qmode_line_default_help_echo,
27874 w->contents);
27875
27876 if (STRINGP (default_help))
27877 {
27878 help_echo_string = default_help;
27879 XSETWINDOW (help_echo_window, w);
27880 help_echo_object = Qnil;
27881 help_echo_pos = -1;
27882 }
27883 }
27884 }
27885
27886 #ifdef HAVE_WINDOW_SYSTEM
27887 /* Change the mouse pointer according to what is under it. */
27888 if (FRAME_WINDOW_P (f))
27889 {
27890 dpyinfo = FRAME_X_DISPLAY_INFO (f);
27891 if (STRINGP (string))
27892 {
27893 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27894
27895 if (NILP (pointer))
27896 pointer = Fget_text_property (pos, Qpointer, string);
27897
27898 /* Change the mouse pointer according to what is under X/Y. */
27899 if (NILP (pointer)
27900 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27901 {
27902 Lisp_Object map;
27903 map = Fget_text_property (pos, Qlocal_map, string);
27904 if (!KEYMAPP (map))
27905 map = Fget_text_property (pos, Qkeymap, string);
27906 if (!KEYMAPP (map))
27907 cursor = dpyinfo->vertical_scroll_bar_cursor;
27908 }
27909 }
27910 else
27911 /* Default mode-line pointer. */
27912 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27913 }
27914 #endif
27915 }
27916
27917 /* Change the mouse face according to what is under X/Y. */
27918 if (STRINGP (string))
27919 {
27920 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27921 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
27922 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27923 && glyph)
27924 {
27925 Lisp_Object b, e;
27926
27927 struct glyph * tmp_glyph;
27928
27929 int gpos;
27930 int gseq_length;
27931 int total_pixel_width;
27932 ptrdiff_t begpos, endpos, ignore;
27933
27934 int vpos, hpos;
27935
27936 b = Fprevious_single_property_change (make_number (charpos + 1),
27937 Qmouse_face, string, Qnil);
27938 if (NILP (b))
27939 begpos = 0;
27940 else
27941 begpos = XINT (b);
27942
27943 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27944 if (NILP (e))
27945 endpos = SCHARS (string);
27946 else
27947 endpos = XINT (e);
27948
27949 /* Calculate the glyph position GPOS of GLYPH in the
27950 displayed string, relative to the beginning of the
27951 highlighted part of the string.
27952
27953 Note: GPOS is different from CHARPOS. CHARPOS is the
27954 position of GLYPH in the internal string object. A mode
27955 line string format has structures which are converted to
27956 a flattened string by the Emacs Lisp interpreter. The
27957 internal string is an element of those structures. The
27958 displayed string is the flattened string. */
27959 tmp_glyph = row_start_glyph;
27960 while (tmp_glyph < glyph
27961 && (!(EQ (tmp_glyph->object, glyph->object)
27962 && begpos <= tmp_glyph->charpos
27963 && tmp_glyph->charpos < endpos)))
27964 tmp_glyph++;
27965 gpos = glyph - tmp_glyph;
27966
27967 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27968 the highlighted part of the displayed string to which
27969 GLYPH belongs. Note: GSEQ_LENGTH is different from
27970 SCHARS (STRING), because the latter returns the length of
27971 the internal string. */
27972 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27973 tmp_glyph > glyph
27974 && (!(EQ (tmp_glyph->object, glyph->object)
27975 && begpos <= tmp_glyph->charpos
27976 && tmp_glyph->charpos < endpos));
27977 tmp_glyph--)
27978 ;
27979 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27980
27981 /* Calculate the total pixel width of all the glyphs between
27982 the beginning of the highlighted area and GLYPH. */
27983 total_pixel_width = 0;
27984 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27985 total_pixel_width += tmp_glyph->pixel_width;
27986
27987 /* Pre calculation of re-rendering position. Note: X is in
27988 column units here, after the call to mode_line_string or
27989 marginal_area_string. */
27990 hpos = x - gpos;
27991 vpos = (area == ON_MODE_LINE
27992 ? (w->current_matrix)->nrows - 1
27993 : 0);
27994
27995 /* If GLYPH's position is included in the region that is
27996 already drawn in mouse face, we have nothing to do. */
27997 if ( EQ (window, hlinfo->mouse_face_window)
27998 && (!row->reversed_p
27999 ? (hlinfo->mouse_face_beg_col <= hpos
28000 && hpos < hlinfo->mouse_face_end_col)
28001 /* In R2L rows we swap BEG and END, see below. */
28002 : (hlinfo->mouse_face_end_col <= hpos
28003 && hpos < hlinfo->mouse_face_beg_col))
28004 && hlinfo->mouse_face_beg_row == vpos )
28005 return;
28006
28007 if (clear_mouse_face (hlinfo))
28008 cursor = No_Cursor;
28009
28010 if (!row->reversed_p)
28011 {
28012 hlinfo->mouse_face_beg_col = hpos;
28013 hlinfo->mouse_face_beg_x = original_x_pixel
28014 - (total_pixel_width + dx);
28015 hlinfo->mouse_face_end_col = hpos + gseq_length;
28016 hlinfo->mouse_face_end_x = 0;
28017 }
28018 else
28019 {
28020 /* In R2L rows, show_mouse_face expects BEG and END
28021 coordinates to be swapped. */
28022 hlinfo->mouse_face_end_col = hpos;
28023 hlinfo->mouse_face_end_x = original_x_pixel
28024 - (total_pixel_width + dx);
28025 hlinfo->mouse_face_beg_col = hpos + gseq_length;
28026 hlinfo->mouse_face_beg_x = 0;
28027 }
28028
28029 hlinfo->mouse_face_beg_row = vpos;
28030 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
28031 hlinfo->mouse_face_beg_y = 0;
28032 hlinfo->mouse_face_end_y = 0;
28033 hlinfo->mouse_face_past_end = 0;
28034 hlinfo->mouse_face_window = window;
28035
28036 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
28037 charpos,
28038 0, 0, 0,
28039 &ignore,
28040 glyph->face_id,
28041 1);
28042 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28043
28044 if (NILP (pointer))
28045 pointer = Qhand;
28046 }
28047 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28048 clear_mouse_face (hlinfo);
28049 }
28050 #ifdef HAVE_WINDOW_SYSTEM
28051 if (FRAME_WINDOW_P (f))
28052 define_frame_cursor1 (f, cursor, pointer);
28053 #endif
28054 }
28055
28056
28057 /* EXPORT:
28058 Take proper action when the mouse has moved to position X, Y on
28059 frame F with regards to highlighting portions of display that have
28060 mouse-face properties. Also de-highlight portions of display where
28061 the mouse was before, set the mouse pointer shape as appropriate
28062 for the mouse coordinates, and activate help echo (tooltips).
28063 X and Y can be negative or out of range. */
28064
28065 void
28066 note_mouse_highlight (struct frame *f, int x, int y)
28067 {
28068 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28069 enum window_part part = ON_NOTHING;
28070 Lisp_Object window;
28071 struct window *w;
28072 Cursor cursor = No_Cursor;
28073 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
28074 struct buffer *b;
28075
28076 /* When a menu is active, don't highlight because this looks odd. */
28077 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
28078 if (popup_activated ())
28079 return;
28080 #endif
28081
28082 if (!f->glyphs_initialized_p
28083 || f->pointer_invisible)
28084 return;
28085
28086 hlinfo->mouse_face_mouse_x = x;
28087 hlinfo->mouse_face_mouse_y = y;
28088 hlinfo->mouse_face_mouse_frame = f;
28089
28090 if (hlinfo->mouse_face_defer)
28091 return;
28092
28093 /* Which window is that in? */
28094 window = window_from_coordinates (f, x, y, &part, 1);
28095
28096 /* If displaying active text in another window, clear that. */
28097 if (! EQ (window, hlinfo->mouse_face_window)
28098 /* Also clear if we move out of text area in same window. */
28099 || (!NILP (hlinfo->mouse_face_window)
28100 && !NILP (window)
28101 && part != ON_TEXT
28102 && part != ON_MODE_LINE
28103 && part != ON_HEADER_LINE))
28104 clear_mouse_face (hlinfo);
28105
28106 /* Not on a window -> return. */
28107 if (!WINDOWP (window))
28108 return;
28109
28110 /* Reset help_echo_string. It will get recomputed below. */
28111 help_echo_string = Qnil;
28112
28113 /* Convert to window-relative pixel coordinates. */
28114 w = XWINDOW (window);
28115 frame_to_window_pixel_xy (w, &x, &y);
28116
28117 #ifdef HAVE_WINDOW_SYSTEM
28118 /* Handle tool-bar window differently since it doesn't display a
28119 buffer. */
28120 if (EQ (window, f->tool_bar_window))
28121 {
28122 note_tool_bar_highlight (f, x, y);
28123 return;
28124 }
28125 #endif
28126
28127 /* Mouse is on the mode, header line or margin? */
28128 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
28129 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
28130 {
28131 note_mode_line_or_margin_highlight (window, x, y, part);
28132 return;
28133 }
28134
28135 #ifdef HAVE_WINDOW_SYSTEM
28136 if (part == ON_VERTICAL_BORDER)
28137 {
28138 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28139 help_echo_string = build_string ("drag-mouse-1: resize");
28140 }
28141 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
28142 || part == ON_SCROLL_BAR)
28143 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28144 else
28145 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28146 #endif
28147
28148 /* Are we in a window whose display is up to date?
28149 And verify the buffer's text has not changed. */
28150 b = XBUFFER (w->contents);
28151 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
28152 {
28153 int hpos, vpos, dx, dy, area = LAST_AREA;
28154 ptrdiff_t pos;
28155 struct glyph *glyph;
28156 Lisp_Object object;
28157 Lisp_Object mouse_face = Qnil, position;
28158 Lisp_Object *overlay_vec = NULL;
28159 ptrdiff_t i, noverlays;
28160 struct buffer *obuf;
28161 ptrdiff_t obegv, ozv;
28162 int same_region;
28163
28164 /* Find the glyph under X/Y. */
28165 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
28166
28167 #ifdef HAVE_WINDOW_SYSTEM
28168 /* Look for :pointer property on image. */
28169 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28170 {
28171 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
28172 if (img != NULL && IMAGEP (img->spec))
28173 {
28174 Lisp_Object image_map, hotspot;
28175 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
28176 !NILP (image_map))
28177 && (hotspot = find_hot_spot (image_map,
28178 glyph->slice.img.x + dx,
28179 glyph->slice.img.y + dy),
28180 CONSP (hotspot))
28181 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28182 {
28183 Lisp_Object plist;
28184
28185 /* Could check XCAR (hotspot) to see if we enter/leave
28186 this hot-spot.
28187 If so, we could look for mouse-enter, mouse-leave
28188 properties in PLIST (and do something...). */
28189 hotspot = XCDR (hotspot);
28190 if (CONSP (hotspot)
28191 && (plist = XCAR (hotspot), CONSP (plist)))
28192 {
28193 pointer = Fplist_get (plist, Qpointer);
28194 if (NILP (pointer))
28195 pointer = Qhand;
28196 help_echo_string = Fplist_get (plist, Qhelp_echo);
28197 if (!NILP (help_echo_string))
28198 {
28199 help_echo_window = window;
28200 help_echo_object = glyph->object;
28201 help_echo_pos = glyph->charpos;
28202 }
28203 }
28204 }
28205 if (NILP (pointer))
28206 pointer = Fplist_get (XCDR (img->spec), QCpointer);
28207 }
28208 }
28209 #endif /* HAVE_WINDOW_SYSTEM */
28210
28211 /* Clear mouse face if X/Y not over text. */
28212 if (glyph == NULL
28213 || area != TEXT_AREA
28214 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
28215 /* Glyph's OBJECT is an integer for glyphs inserted by the
28216 display engine for its internal purposes, like truncation
28217 and continuation glyphs and blanks beyond the end of
28218 line's text on text terminals. If we are over such a
28219 glyph, we are not over any text. */
28220 || INTEGERP (glyph->object)
28221 /* R2L rows have a stretch glyph at their front, which
28222 stands for no text, whereas L2R rows have no glyphs at
28223 all beyond the end of text. Treat such stretch glyphs
28224 like we do with NULL glyphs in L2R rows. */
28225 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
28226 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
28227 && glyph->type == STRETCH_GLYPH
28228 && glyph->avoid_cursor_p))
28229 {
28230 if (clear_mouse_face (hlinfo))
28231 cursor = No_Cursor;
28232 #ifdef HAVE_WINDOW_SYSTEM
28233 if (FRAME_WINDOW_P (f) && NILP (pointer))
28234 {
28235 if (area != TEXT_AREA)
28236 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28237 else
28238 pointer = Vvoid_text_area_pointer;
28239 }
28240 #endif
28241 goto set_cursor;
28242 }
28243
28244 pos = glyph->charpos;
28245 object = glyph->object;
28246 if (!STRINGP (object) && !BUFFERP (object))
28247 goto set_cursor;
28248
28249 /* If we get an out-of-range value, return now; avoid an error. */
28250 if (BUFFERP (object) && pos > BUF_Z (b))
28251 goto set_cursor;
28252
28253 /* Make the window's buffer temporarily current for
28254 overlays_at and compute_char_face. */
28255 obuf = current_buffer;
28256 current_buffer = b;
28257 obegv = BEGV;
28258 ozv = ZV;
28259 BEGV = BEG;
28260 ZV = Z;
28261
28262 /* Is this char mouse-active or does it have help-echo? */
28263 position = make_number (pos);
28264
28265 if (BUFFERP (object))
28266 {
28267 /* Put all the overlays we want in a vector in overlay_vec. */
28268 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
28269 /* Sort overlays into increasing priority order. */
28270 noverlays = sort_overlays (overlay_vec, noverlays, w);
28271 }
28272 else
28273 noverlays = 0;
28274
28275 if (NILP (Vmouse_highlight))
28276 {
28277 clear_mouse_face (hlinfo);
28278 goto check_help_echo;
28279 }
28280
28281 same_region = coords_in_mouse_face_p (w, hpos, vpos);
28282
28283 if (same_region)
28284 cursor = No_Cursor;
28285
28286 /* Check mouse-face highlighting. */
28287 if (! same_region
28288 /* If there exists an overlay with mouse-face overlapping
28289 the one we are currently highlighting, we have to
28290 check if we enter the overlapping overlay, and then
28291 highlight only that. */
28292 || (OVERLAYP (hlinfo->mouse_face_overlay)
28293 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
28294 {
28295 /* Find the highest priority overlay with a mouse-face. */
28296 Lisp_Object overlay = Qnil;
28297 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
28298 {
28299 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
28300 if (!NILP (mouse_face))
28301 overlay = overlay_vec[i];
28302 }
28303
28304 /* If we're highlighting the same overlay as before, there's
28305 no need to do that again. */
28306 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
28307 goto check_help_echo;
28308 hlinfo->mouse_face_overlay = overlay;
28309
28310 /* Clear the display of the old active region, if any. */
28311 if (clear_mouse_face (hlinfo))
28312 cursor = No_Cursor;
28313
28314 /* If no overlay applies, get a text property. */
28315 if (NILP (overlay))
28316 mouse_face = Fget_text_property (position, Qmouse_face, object);
28317
28318 /* Next, compute the bounds of the mouse highlighting and
28319 display it. */
28320 if (!NILP (mouse_face) && STRINGP (object))
28321 {
28322 /* The mouse-highlighting comes from a display string
28323 with a mouse-face. */
28324 Lisp_Object s, e;
28325 ptrdiff_t ignore;
28326
28327 s = Fprevious_single_property_change
28328 (make_number (pos + 1), Qmouse_face, object, Qnil);
28329 e = Fnext_single_property_change
28330 (position, Qmouse_face, object, Qnil);
28331 if (NILP (s))
28332 s = make_number (0);
28333 if (NILP (e))
28334 e = make_number (SCHARS (object) - 1);
28335 mouse_face_from_string_pos (w, hlinfo, object,
28336 XINT (s), XINT (e));
28337 hlinfo->mouse_face_past_end = 0;
28338 hlinfo->mouse_face_window = window;
28339 hlinfo->mouse_face_face_id
28340 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
28341 glyph->face_id, 1);
28342 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28343 cursor = No_Cursor;
28344 }
28345 else
28346 {
28347 /* The mouse-highlighting, if any, comes from an overlay
28348 or text property in the buffer. */
28349 Lisp_Object buffer IF_LINT (= Qnil);
28350 Lisp_Object disp_string IF_LINT (= Qnil);
28351
28352 if (STRINGP (object))
28353 {
28354 /* If we are on a display string with no mouse-face,
28355 check if the text under it has one. */
28356 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
28357 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28358 pos = string_buffer_position (object, start);
28359 if (pos > 0)
28360 {
28361 mouse_face = get_char_property_and_overlay
28362 (make_number (pos), Qmouse_face, w->contents, &overlay);
28363 buffer = w->contents;
28364 disp_string = object;
28365 }
28366 }
28367 else
28368 {
28369 buffer = object;
28370 disp_string = Qnil;
28371 }
28372
28373 if (!NILP (mouse_face))
28374 {
28375 Lisp_Object before, after;
28376 Lisp_Object before_string, after_string;
28377 /* To correctly find the limits of mouse highlight
28378 in a bidi-reordered buffer, we must not use the
28379 optimization of limiting the search in
28380 previous-single-property-change and
28381 next-single-property-change, because
28382 rows_from_pos_range needs the real start and end
28383 positions to DTRT in this case. That's because
28384 the first row visible in a window does not
28385 necessarily display the character whose position
28386 is the smallest. */
28387 Lisp_Object lim1 =
28388 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28389 ? Fmarker_position (w->start)
28390 : Qnil;
28391 Lisp_Object lim2 =
28392 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28393 ? make_number (BUF_Z (XBUFFER (buffer)) - w->window_end_pos)
28394 : Qnil;
28395
28396 if (NILP (overlay))
28397 {
28398 /* Handle the text property case. */
28399 before = Fprevious_single_property_change
28400 (make_number (pos + 1), Qmouse_face, buffer, lim1);
28401 after = Fnext_single_property_change
28402 (make_number (pos), Qmouse_face, buffer, lim2);
28403 before_string = after_string = Qnil;
28404 }
28405 else
28406 {
28407 /* Handle the overlay case. */
28408 before = Foverlay_start (overlay);
28409 after = Foverlay_end (overlay);
28410 before_string = Foverlay_get (overlay, Qbefore_string);
28411 after_string = Foverlay_get (overlay, Qafter_string);
28412
28413 if (!STRINGP (before_string)) before_string = Qnil;
28414 if (!STRINGP (after_string)) after_string = Qnil;
28415 }
28416
28417 mouse_face_from_buffer_pos (window, hlinfo, pos,
28418 NILP (before)
28419 ? 1
28420 : XFASTINT (before),
28421 NILP (after)
28422 ? BUF_Z (XBUFFER (buffer))
28423 : XFASTINT (after),
28424 before_string, after_string,
28425 disp_string);
28426 cursor = No_Cursor;
28427 }
28428 }
28429 }
28430
28431 check_help_echo:
28432
28433 /* Look for a `help-echo' property. */
28434 if (NILP (help_echo_string)) {
28435 Lisp_Object help, overlay;
28436
28437 /* Check overlays first. */
28438 help = overlay = Qnil;
28439 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
28440 {
28441 overlay = overlay_vec[i];
28442 help = Foverlay_get (overlay, Qhelp_echo);
28443 }
28444
28445 if (!NILP (help))
28446 {
28447 help_echo_string = help;
28448 help_echo_window = window;
28449 help_echo_object = overlay;
28450 help_echo_pos = pos;
28451 }
28452 else
28453 {
28454 Lisp_Object obj = glyph->object;
28455 ptrdiff_t charpos = glyph->charpos;
28456
28457 /* Try text properties. */
28458 if (STRINGP (obj)
28459 && charpos >= 0
28460 && charpos < SCHARS (obj))
28461 {
28462 help = Fget_text_property (make_number (charpos),
28463 Qhelp_echo, obj);
28464 if (NILP (help))
28465 {
28466 /* If the string itself doesn't specify a help-echo,
28467 see if the buffer text ``under'' it does. */
28468 struct glyph_row *r
28469 = MATRIX_ROW (w->current_matrix, vpos);
28470 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28471 ptrdiff_t p = string_buffer_position (obj, start);
28472 if (p > 0)
28473 {
28474 help = Fget_char_property (make_number (p),
28475 Qhelp_echo, w->contents);
28476 if (!NILP (help))
28477 {
28478 charpos = p;
28479 obj = w->contents;
28480 }
28481 }
28482 }
28483 }
28484 else if (BUFFERP (obj)
28485 && charpos >= BEGV
28486 && charpos < ZV)
28487 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28488 obj);
28489
28490 if (!NILP (help))
28491 {
28492 help_echo_string = help;
28493 help_echo_window = window;
28494 help_echo_object = obj;
28495 help_echo_pos = charpos;
28496 }
28497 }
28498 }
28499
28500 #ifdef HAVE_WINDOW_SYSTEM
28501 /* Look for a `pointer' property. */
28502 if (FRAME_WINDOW_P (f) && NILP (pointer))
28503 {
28504 /* Check overlays first. */
28505 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28506 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28507
28508 if (NILP (pointer))
28509 {
28510 Lisp_Object obj = glyph->object;
28511 ptrdiff_t charpos = glyph->charpos;
28512
28513 /* Try text properties. */
28514 if (STRINGP (obj)
28515 && charpos >= 0
28516 && charpos < SCHARS (obj))
28517 {
28518 pointer = Fget_text_property (make_number (charpos),
28519 Qpointer, obj);
28520 if (NILP (pointer))
28521 {
28522 /* If the string itself doesn't specify a pointer,
28523 see if the buffer text ``under'' it does. */
28524 struct glyph_row *r
28525 = MATRIX_ROW (w->current_matrix, vpos);
28526 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28527 ptrdiff_t p = string_buffer_position (obj, start);
28528 if (p > 0)
28529 pointer = Fget_char_property (make_number (p),
28530 Qpointer, w->contents);
28531 }
28532 }
28533 else if (BUFFERP (obj)
28534 && charpos >= BEGV
28535 && charpos < ZV)
28536 pointer = Fget_text_property (make_number (charpos),
28537 Qpointer, obj);
28538 }
28539 }
28540 #endif /* HAVE_WINDOW_SYSTEM */
28541
28542 BEGV = obegv;
28543 ZV = ozv;
28544 current_buffer = obuf;
28545 }
28546
28547 set_cursor:
28548
28549 #ifdef HAVE_WINDOW_SYSTEM
28550 if (FRAME_WINDOW_P (f))
28551 define_frame_cursor1 (f, cursor, pointer);
28552 #else
28553 /* This is here to prevent a compiler error, about "label at end of
28554 compound statement". */
28555 return;
28556 #endif
28557 }
28558
28559
28560 /* EXPORT for RIF:
28561 Clear any mouse-face on window W. This function is part of the
28562 redisplay interface, and is called from try_window_id and similar
28563 functions to ensure the mouse-highlight is off. */
28564
28565 void
28566 x_clear_window_mouse_face (struct window *w)
28567 {
28568 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28569 Lisp_Object window;
28570
28571 block_input ();
28572 XSETWINDOW (window, w);
28573 if (EQ (window, hlinfo->mouse_face_window))
28574 clear_mouse_face (hlinfo);
28575 unblock_input ();
28576 }
28577
28578
28579 /* EXPORT:
28580 Just discard the mouse face information for frame F, if any.
28581 This is used when the size of F is changed. */
28582
28583 void
28584 cancel_mouse_face (struct frame *f)
28585 {
28586 Lisp_Object window;
28587 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28588
28589 window = hlinfo->mouse_face_window;
28590 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28591 {
28592 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28593 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28594 hlinfo->mouse_face_window = Qnil;
28595 }
28596 }
28597
28598
28599 \f
28600 /***********************************************************************
28601 Exposure Events
28602 ***********************************************************************/
28603
28604 #ifdef HAVE_WINDOW_SYSTEM
28605
28606 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28607 which intersects rectangle R. R is in window-relative coordinates. */
28608
28609 static void
28610 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28611 enum glyph_row_area area)
28612 {
28613 struct glyph *first = row->glyphs[area];
28614 struct glyph *end = row->glyphs[area] + row->used[area];
28615 struct glyph *last;
28616 int first_x, start_x, x;
28617
28618 if (area == TEXT_AREA && row->fill_line_p)
28619 /* If row extends face to end of line write the whole line. */
28620 draw_glyphs (w, 0, row, area,
28621 0, row->used[area],
28622 DRAW_NORMAL_TEXT, 0);
28623 else
28624 {
28625 /* Set START_X to the window-relative start position for drawing glyphs of
28626 AREA. The first glyph of the text area can be partially visible.
28627 The first glyphs of other areas cannot. */
28628 start_x = window_box_left_offset (w, area);
28629 x = start_x;
28630 if (area == TEXT_AREA)
28631 x += row->x;
28632
28633 /* Find the first glyph that must be redrawn. */
28634 while (first < end
28635 && x + first->pixel_width < r->x)
28636 {
28637 x += first->pixel_width;
28638 ++first;
28639 }
28640
28641 /* Find the last one. */
28642 last = first;
28643 first_x = x;
28644 while (last < end
28645 && x < r->x + r->width)
28646 {
28647 x += last->pixel_width;
28648 ++last;
28649 }
28650
28651 /* Repaint. */
28652 if (last > first)
28653 draw_glyphs (w, first_x - start_x, row, area,
28654 first - row->glyphs[area], last - row->glyphs[area],
28655 DRAW_NORMAL_TEXT, 0);
28656 }
28657 }
28658
28659
28660 /* Redraw the parts of the glyph row ROW on window W intersecting
28661 rectangle R. R is in window-relative coordinates. Value is
28662 non-zero if mouse-face was overwritten. */
28663
28664 static int
28665 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28666 {
28667 eassert (row->enabled_p);
28668
28669 if (row->mode_line_p || w->pseudo_window_p)
28670 draw_glyphs (w, 0, row, TEXT_AREA,
28671 0, row->used[TEXT_AREA],
28672 DRAW_NORMAL_TEXT, 0);
28673 else
28674 {
28675 if (row->used[LEFT_MARGIN_AREA])
28676 expose_area (w, row, r, LEFT_MARGIN_AREA);
28677 if (row->used[TEXT_AREA])
28678 expose_area (w, row, r, TEXT_AREA);
28679 if (row->used[RIGHT_MARGIN_AREA])
28680 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28681 draw_row_fringe_bitmaps (w, row);
28682 }
28683
28684 return row->mouse_face_p;
28685 }
28686
28687
28688 /* Redraw those parts of glyphs rows during expose event handling that
28689 overlap other rows. Redrawing of an exposed line writes over parts
28690 of lines overlapping that exposed line; this function fixes that.
28691
28692 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28693 row in W's current matrix that is exposed and overlaps other rows.
28694 LAST_OVERLAPPING_ROW is the last such row. */
28695
28696 static void
28697 expose_overlaps (struct window *w,
28698 struct glyph_row *first_overlapping_row,
28699 struct glyph_row *last_overlapping_row,
28700 XRectangle *r)
28701 {
28702 struct glyph_row *row;
28703
28704 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28705 if (row->overlapping_p)
28706 {
28707 eassert (row->enabled_p && !row->mode_line_p);
28708
28709 row->clip = r;
28710 if (row->used[LEFT_MARGIN_AREA])
28711 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28712
28713 if (row->used[TEXT_AREA])
28714 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28715
28716 if (row->used[RIGHT_MARGIN_AREA])
28717 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28718 row->clip = NULL;
28719 }
28720 }
28721
28722
28723 /* Return non-zero if W's cursor intersects rectangle R. */
28724
28725 static int
28726 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28727 {
28728 XRectangle cr, result;
28729 struct glyph *cursor_glyph;
28730 struct glyph_row *row;
28731
28732 if (w->phys_cursor.vpos >= 0
28733 && w->phys_cursor.vpos < w->current_matrix->nrows
28734 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28735 row->enabled_p)
28736 && row->cursor_in_fringe_p)
28737 {
28738 /* Cursor is in the fringe. */
28739 cr.x = window_box_right_offset (w,
28740 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28741 ? RIGHT_MARGIN_AREA
28742 : TEXT_AREA));
28743 cr.y = row->y;
28744 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28745 cr.height = row->height;
28746 return x_intersect_rectangles (&cr, r, &result);
28747 }
28748
28749 cursor_glyph = get_phys_cursor_glyph (w);
28750 if (cursor_glyph)
28751 {
28752 /* r is relative to W's box, but w->phys_cursor.x is relative
28753 to left edge of W's TEXT area. Adjust it. */
28754 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28755 cr.y = w->phys_cursor.y;
28756 cr.width = cursor_glyph->pixel_width;
28757 cr.height = w->phys_cursor_height;
28758 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28759 I assume the effect is the same -- and this is portable. */
28760 return x_intersect_rectangles (&cr, r, &result);
28761 }
28762 /* If we don't understand the format, pretend we're not in the hot-spot. */
28763 return 0;
28764 }
28765
28766
28767 /* EXPORT:
28768 Draw a vertical window border to the right of window W if W doesn't
28769 have vertical scroll bars. */
28770
28771 void
28772 x_draw_vertical_border (struct window *w)
28773 {
28774 struct frame *f = XFRAME (WINDOW_FRAME (w));
28775
28776 /* We could do better, if we knew what type of scroll-bar the adjacent
28777 windows (on either side) have... But we don't :-(
28778 However, I think this works ok. ++KFS 2003-04-25 */
28779
28780 /* Redraw borders between horizontally adjacent windows. Don't
28781 do it for frames with vertical scroll bars because either the
28782 right scroll bar of a window, or the left scroll bar of its
28783 neighbor will suffice as a border. */
28784 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28785 return;
28786
28787 /* Note: It is necessary to redraw both the left and the right
28788 borders, for when only this single window W is being
28789 redisplayed. */
28790 if (!WINDOW_RIGHTMOST_P (w)
28791 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28792 {
28793 int x0, x1, y0, y1;
28794
28795 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28796 y1 -= 1;
28797
28798 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28799 x1 -= 1;
28800
28801 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28802 }
28803 if (!WINDOW_LEFTMOST_P (w)
28804 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28805 {
28806 int x0, x1, y0, y1;
28807
28808 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28809 y1 -= 1;
28810
28811 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28812 x0 -= 1;
28813
28814 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28815 }
28816 }
28817
28818
28819 /* Redraw the part of window W intersection rectangle FR. Pixel
28820 coordinates in FR are frame-relative. Call this function with
28821 input blocked. Value is non-zero if the exposure overwrites
28822 mouse-face. */
28823
28824 static int
28825 expose_window (struct window *w, XRectangle *fr)
28826 {
28827 struct frame *f = XFRAME (w->frame);
28828 XRectangle wr, r;
28829 int mouse_face_overwritten_p = 0;
28830
28831 /* If window is not yet fully initialized, do nothing. This can
28832 happen when toolkit scroll bars are used and a window is split.
28833 Reconfiguring the scroll bar will generate an expose for a newly
28834 created window. */
28835 if (w->current_matrix == NULL)
28836 return 0;
28837
28838 /* When we're currently updating the window, display and current
28839 matrix usually don't agree. Arrange for a thorough display
28840 later. */
28841 if (w->must_be_updated_p)
28842 {
28843 SET_FRAME_GARBAGED (f);
28844 return 0;
28845 }
28846
28847 /* Frame-relative pixel rectangle of W. */
28848 wr.x = WINDOW_LEFT_EDGE_X (w);
28849 wr.y = WINDOW_TOP_EDGE_Y (w);
28850 wr.width = WINDOW_TOTAL_WIDTH (w);
28851 wr.height = WINDOW_TOTAL_HEIGHT (w);
28852
28853 if (x_intersect_rectangles (fr, &wr, &r))
28854 {
28855 int yb = window_text_bottom_y (w);
28856 struct glyph_row *row;
28857 int cursor_cleared_p, phys_cursor_on_p;
28858 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28859
28860 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28861 r.x, r.y, r.width, r.height));
28862
28863 /* Convert to window coordinates. */
28864 r.x -= WINDOW_LEFT_EDGE_X (w);
28865 r.y -= WINDOW_TOP_EDGE_Y (w);
28866
28867 /* Turn off the cursor. */
28868 if (!w->pseudo_window_p
28869 && phys_cursor_in_rect_p (w, &r))
28870 {
28871 x_clear_cursor (w);
28872 cursor_cleared_p = 1;
28873 }
28874 else
28875 cursor_cleared_p = 0;
28876
28877 /* If the row containing the cursor extends face to end of line,
28878 then expose_area might overwrite the cursor outside the
28879 rectangle and thus notice_overwritten_cursor might clear
28880 w->phys_cursor_on_p. We remember the original value and
28881 check later if it is changed. */
28882 phys_cursor_on_p = w->phys_cursor_on_p;
28883
28884 /* Update lines intersecting rectangle R. */
28885 first_overlapping_row = last_overlapping_row = NULL;
28886 for (row = w->current_matrix->rows;
28887 row->enabled_p;
28888 ++row)
28889 {
28890 int y0 = row->y;
28891 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28892
28893 if ((y0 >= r.y && y0 < r.y + r.height)
28894 || (y1 > r.y && y1 < r.y + r.height)
28895 || (r.y >= y0 && r.y < y1)
28896 || (r.y + r.height > y0 && r.y + r.height < y1))
28897 {
28898 /* A header line may be overlapping, but there is no need
28899 to fix overlapping areas for them. KFS 2005-02-12 */
28900 if (row->overlapping_p && !row->mode_line_p)
28901 {
28902 if (first_overlapping_row == NULL)
28903 first_overlapping_row = row;
28904 last_overlapping_row = row;
28905 }
28906
28907 row->clip = fr;
28908 if (expose_line (w, row, &r))
28909 mouse_face_overwritten_p = 1;
28910 row->clip = NULL;
28911 }
28912 else if (row->overlapping_p)
28913 {
28914 /* We must redraw a row overlapping the exposed area. */
28915 if (y0 < r.y
28916 ? y0 + row->phys_height > r.y
28917 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28918 {
28919 if (first_overlapping_row == NULL)
28920 first_overlapping_row = row;
28921 last_overlapping_row = row;
28922 }
28923 }
28924
28925 if (y1 >= yb)
28926 break;
28927 }
28928
28929 /* Display the mode line if there is one. */
28930 if (WINDOW_WANTS_MODELINE_P (w)
28931 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28932 row->enabled_p)
28933 && row->y < r.y + r.height)
28934 {
28935 if (expose_line (w, row, &r))
28936 mouse_face_overwritten_p = 1;
28937 }
28938
28939 if (!w->pseudo_window_p)
28940 {
28941 /* Fix the display of overlapping rows. */
28942 if (first_overlapping_row)
28943 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28944 fr);
28945
28946 /* Draw border between windows. */
28947 x_draw_vertical_border (w);
28948
28949 /* Turn the cursor on again. */
28950 if (cursor_cleared_p
28951 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28952 update_window_cursor (w, 1);
28953 }
28954 }
28955
28956 return mouse_face_overwritten_p;
28957 }
28958
28959
28960
28961 /* Redraw (parts) of all windows in the window tree rooted at W that
28962 intersect R. R contains frame pixel coordinates. Value is
28963 non-zero if the exposure overwrites mouse-face. */
28964
28965 static int
28966 expose_window_tree (struct window *w, XRectangle *r)
28967 {
28968 struct frame *f = XFRAME (w->frame);
28969 int mouse_face_overwritten_p = 0;
28970
28971 while (w && !FRAME_GARBAGED_P (f))
28972 {
28973 if (WINDOWP (w->contents))
28974 mouse_face_overwritten_p
28975 |= expose_window_tree (XWINDOW (w->contents), r);
28976 else
28977 mouse_face_overwritten_p |= expose_window (w, r);
28978
28979 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28980 }
28981
28982 return mouse_face_overwritten_p;
28983 }
28984
28985
28986 /* EXPORT:
28987 Redisplay an exposed area of frame F. X and Y are the upper-left
28988 corner of the exposed rectangle. W and H are width and height of
28989 the exposed area. All are pixel values. W or H zero means redraw
28990 the entire frame. */
28991
28992 void
28993 expose_frame (struct frame *f, int x, int y, int w, int h)
28994 {
28995 XRectangle r;
28996 int mouse_face_overwritten_p = 0;
28997
28998 TRACE ((stderr, "expose_frame "));
28999
29000 /* No need to redraw if frame will be redrawn soon. */
29001 if (FRAME_GARBAGED_P (f))
29002 {
29003 TRACE ((stderr, " garbaged\n"));
29004 return;
29005 }
29006
29007 /* If basic faces haven't been realized yet, there is no point in
29008 trying to redraw anything. This can happen when we get an expose
29009 event while Emacs is starting, e.g. by moving another window. */
29010 if (FRAME_FACE_CACHE (f) == NULL
29011 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
29012 {
29013 TRACE ((stderr, " no faces\n"));
29014 return;
29015 }
29016
29017 if (w == 0 || h == 0)
29018 {
29019 r.x = r.y = 0;
29020 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
29021 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
29022 }
29023 else
29024 {
29025 r.x = x;
29026 r.y = y;
29027 r.width = w;
29028 r.height = h;
29029 }
29030
29031 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
29032 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
29033
29034 if (WINDOWP (f->tool_bar_window))
29035 mouse_face_overwritten_p
29036 |= expose_window (XWINDOW (f->tool_bar_window), &r);
29037
29038 #ifdef HAVE_X_WINDOWS
29039 #ifndef MSDOS
29040 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
29041 if (WINDOWP (f->menu_bar_window))
29042 mouse_face_overwritten_p
29043 |= expose_window (XWINDOW (f->menu_bar_window), &r);
29044 #endif /* not USE_X_TOOLKIT and not USE_GTK */
29045 #endif
29046 #endif
29047
29048 /* Some window managers support a focus-follows-mouse style with
29049 delayed raising of frames. Imagine a partially obscured frame,
29050 and moving the mouse into partially obscured mouse-face on that
29051 frame. The visible part of the mouse-face will be highlighted,
29052 then the WM raises the obscured frame. With at least one WM, KDE
29053 2.1, Emacs is not getting any event for the raising of the frame
29054 (even tried with SubstructureRedirectMask), only Expose events.
29055 These expose events will draw text normally, i.e. not
29056 highlighted. Which means we must redo the highlight here.
29057 Subsume it under ``we love X''. --gerd 2001-08-15 */
29058 /* Included in Windows version because Windows most likely does not
29059 do the right thing if any third party tool offers
29060 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
29061 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
29062 {
29063 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29064 if (f == hlinfo->mouse_face_mouse_frame)
29065 {
29066 int mouse_x = hlinfo->mouse_face_mouse_x;
29067 int mouse_y = hlinfo->mouse_face_mouse_y;
29068 clear_mouse_face (hlinfo);
29069 note_mouse_highlight (f, mouse_x, mouse_y);
29070 }
29071 }
29072 }
29073
29074
29075 /* EXPORT:
29076 Determine the intersection of two rectangles R1 and R2. Return
29077 the intersection in *RESULT. Value is non-zero if RESULT is not
29078 empty. */
29079
29080 int
29081 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
29082 {
29083 XRectangle *left, *right;
29084 XRectangle *upper, *lower;
29085 int intersection_p = 0;
29086
29087 /* Rearrange so that R1 is the left-most rectangle. */
29088 if (r1->x < r2->x)
29089 left = r1, right = r2;
29090 else
29091 left = r2, right = r1;
29092
29093 /* X0 of the intersection is right.x0, if this is inside R1,
29094 otherwise there is no intersection. */
29095 if (right->x <= left->x + left->width)
29096 {
29097 result->x = right->x;
29098
29099 /* The right end of the intersection is the minimum of
29100 the right ends of left and right. */
29101 result->width = (min (left->x + left->width, right->x + right->width)
29102 - result->x);
29103
29104 /* Same game for Y. */
29105 if (r1->y < r2->y)
29106 upper = r1, lower = r2;
29107 else
29108 upper = r2, lower = r1;
29109
29110 /* The upper end of the intersection is lower.y0, if this is inside
29111 of upper. Otherwise, there is no intersection. */
29112 if (lower->y <= upper->y + upper->height)
29113 {
29114 result->y = lower->y;
29115
29116 /* The lower end of the intersection is the minimum of the lower
29117 ends of upper and lower. */
29118 result->height = (min (lower->y + lower->height,
29119 upper->y + upper->height)
29120 - result->y);
29121 intersection_p = 1;
29122 }
29123 }
29124
29125 return intersection_p;
29126 }
29127
29128 #endif /* HAVE_WINDOW_SYSTEM */
29129
29130 \f
29131 /***********************************************************************
29132 Initialization
29133 ***********************************************************************/
29134
29135 void
29136 syms_of_xdisp (void)
29137 {
29138 Vwith_echo_area_save_vector = Qnil;
29139 staticpro (&Vwith_echo_area_save_vector);
29140
29141 Vmessage_stack = Qnil;
29142 staticpro (&Vmessage_stack);
29143
29144 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
29145 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
29146
29147 message_dolog_marker1 = Fmake_marker ();
29148 staticpro (&message_dolog_marker1);
29149 message_dolog_marker2 = Fmake_marker ();
29150 staticpro (&message_dolog_marker2);
29151 message_dolog_marker3 = Fmake_marker ();
29152 staticpro (&message_dolog_marker3);
29153
29154 #ifdef GLYPH_DEBUG
29155 defsubr (&Sdump_frame_glyph_matrix);
29156 defsubr (&Sdump_glyph_matrix);
29157 defsubr (&Sdump_glyph_row);
29158 defsubr (&Sdump_tool_bar_row);
29159 defsubr (&Strace_redisplay);
29160 defsubr (&Strace_to_stderr);
29161 #endif
29162 #ifdef HAVE_WINDOW_SYSTEM
29163 defsubr (&Stool_bar_lines_needed);
29164 defsubr (&Slookup_image_map);
29165 #endif
29166 defsubr (&Sline_pixel_height);
29167 defsubr (&Sformat_mode_line);
29168 defsubr (&Sinvisible_p);
29169 defsubr (&Scurrent_bidi_paragraph_direction);
29170 defsubr (&Smove_point_visually);
29171
29172 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
29173 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
29174 DEFSYM (Qoverriding_local_map, "overriding-local-map");
29175 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
29176 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
29177 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
29178 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
29179 DEFSYM (Qeval, "eval");
29180 DEFSYM (QCdata, ":data");
29181 DEFSYM (Qdisplay, "display");
29182 DEFSYM (Qspace_width, "space-width");
29183 DEFSYM (Qraise, "raise");
29184 DEFSYM (Qslice, "slice");
29185 DEFSYM (Qspace, "space");
29186 DEFSYM (Qmargin, "margin");
29187 DEFSYM (Qpointer, "pointer");
29188 DEFSYM (Qleft_margin, "left-margin");
29189 DEFSYM (Qright_margin, "right-margin");
29190 DEFSYM (Qcenter, "center");
29191 DEFSYM (Qline_height, "line-height");
29192 DEFSYM (QCalign_to, ":align-to");
29193 DEFSYM (QCrelative_width, ":relative-width");
29194 DEFSYM (QCrelative_height, ":relative-height");
29195 DEFSYM (QCeval, ":eval");
29196 DEFSYM (QCpropertize, ":propertize");
29197 DEFSYM (QCfile, ":file");
29198 DEFSYM (Qfontified, "fontified");
29199 DEFSYM (Qfontification_functions, "fontification-functions");
29200 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
29201 DEFSYM (Qescape_glyph, "escape-glyph");
29202 DEFSYM (Qnobreak_space, "nobreak-space");
29203 DEFSYM (Qimage, "image");
29204 DEFSYM (Qtext, "text");
29205 DEFSYM (Qboth, "both");
29206 DEFSYM (Qboth_horiz, "both-horiz");
29207 DEFSYM (Qtext_image_horiz, "text-image-horiz");
29208 DEFSYM (QCmap, ":map");
29209 DEFSYM (QCpointer, ":pointer");
29210 DEFSYM (Qrect, "rect");
29211 DEFSYM (Qcircle, "circle");
29212 DEFSYM (Qpoly, "poly");
29213 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
29214 DEFSYM (Qgrow_only, "grow-only");
29215 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
29216 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
29217 DEFSYM (Qposition, "position");
29218 DEFSYM (Qbuffer_position, "buffer-position");
29219 DEFSYM (Qobject, "object");
29220 DEFSYM (Qbar, "bar");
29221 DEFSYM (Qhbar, "hbar");
29222 DEFSYM (Qbox, "box");
29223 DEFSYM (Qhollow, "hollow");
29224 DEFSYM (Qhand, "hand");
29225 DEFSYM (Qarrow, "arrow");
29226 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
29227
29228 list_of_error = list1 (list2 (intern_c_string ("error"),
29229 intern_c_string ("void-variable")));
29230 staticpro (&list_of_error);
29231
29232 DEFSYM (Qlast_arrow_position, "last-arrow-position");
29233 DEFSYM (Qlast_arrow_string, "last-arrow-string");
29234 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
29235 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
29236
29237 echo_buffer[0] = echo_buffer[1] = Qnil;
29238 staticpro (&echo_buffer[0]);
29239 staticpro (&echo_buffer[1]);
29240
29241 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
29242 staticpro (&echo_area_buffer[0]);
29243 staticpro (&echo_area_buffer[1]);
29244
29245 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
29246 staticpro (&Vmessages_buffer_name);
29247
29248 mode_line_proptrans_alist = Qnil;
29249 staticpro (&mode_line_proptrans_alist);
29250 mode_line_string_list = Qnil;
29251 staticpro (&mode_line_string_list);
29252 mode_line_string_face = Qnil;
29253 staticpro (&mode_line_string_face);
29254 mode_line_string_face_prop = Qnil;
29255 staticpro (&mode_line_string_face_prop);
29256 Vmode_line_unwind_vector = Qnil;
29257 staticpro (&Vmode_line_unwind_vector);
29258
29259 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
29260
29261 help_echo_string = Qnil;
29262 staticpro (&help_echo_string);
29263 help_echo_object = Qnil;
29264 staticpro (&help_echo_object);
29265 help_echo_window = Qnil;
29266 staticpro (&help_echo_window);
29267 previous_help_echo_string = Qnil;
29268 staticpro (&previous_help_echo_string);
29269 help_echo_pos = -1;
29270
29271 DEFSYM (Qright_to_left, "right-to-left");
29272 DEFSYM (Qleft_to_right, "left-to-right");
29273
29274 #ifdef HAVE_WINDOW_SYSTEM
29275 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
29276 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
29277 For example, if a block cursor is over a tab, it will be drawn as
29278 wide as that tab on the display. */);
29279 x_stretch_cursor_p = 0;
29280 #endif
29281
29282 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
29283 doc: /* Non-nil means highlight trailing whitespace.
29284 The face used for trailing whitespace is `trailing-whitespace'. */);
29285 Vshow_trailing_whitespace = Qnil;
29286
29287 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
29288 doc: /* Control highlighting of non-ASCII space and hyphen chars.
29289 If the value is t, Emacs highlights non-ASCII chars which have the
29290 same appearance as an ASCII space or hyphen, using the `nobreak-space'
29291 or `escape-glyph' face respectively.
29292
29293 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
29294 U+2011 (non-breaking hyphen) are affected.
29295
29296 Any other non-nil value means to display these characters as a escape
29297 glyph followed by an ordinary space or hyphen.
29298
29299 A value of nil means no special handling of these characters. */);
29300 Vnobreak_char_display = Qt;
29301
29302 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
29303 doc: /* The pointer shape to show in void text areas.
29304 A value of nil means to show the text pointer. Other options are `arrow',
29305 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
29306 Vvoid_text_area_pointer = Qarrow;
29307
29308 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
29309 doc: /* Non-nil means don't actually do any redisplay.
29310 This is used for internal purposes. */);
29311 Vinhibit_redisplay = Qnil;
29312
29313 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
29314 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
29315 Vglobal_mode_string = Qnil;
29316
29317 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
29318 doc: /* Marker for where to display an arrow on top of the buffer text.
29319 This must be the beginning of a line in order to work.
29320 See also `overlay-arrow-string'. */);
29321 Voverlay_arrow_position = Qnil;
29322
29323 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
29324 doc: /* String to display as an arrow in non-window frames.
29325 See also `overlay-arrow-position'. */);
29326 Voverlay_arrow_string = build_pure_c_string ("=>");
29327
29328 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
29329 doc: /* List of variables (symbols) which hold markers for overlay arrows.
29330 The symbols on this list are examined during redisplay to determine
29331 where to display overlay arrows. */);
29332 Voverlay_arrow_variable_list
29333 = list1 (intern_c_string ("overlay-arrow-position"));
29334
29335 DEFVAR_INT ("scroll-step", emacs_scroll_step,
29336 doc: /* The number of lines to try scrolling a window by when point moves out.
29337 If that fails to bring point back on frame, point is centered instead.
29338 If this is zero, point is always centered after it moves off frame.
29339 If you want scrolling to always be a line at a time, you should set
29340 `scroll-conservatively' to a large value rather than set this to 1. */);
29341
29342 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
29343 doc: /* Scroll up to this many lines, to bring point back on screen.
29344 If point moves off-screen, redisplay will scroll by up to
29345 `scroll-conservatively' lines in order to bring point just barely
29346 onto the screen again. If that cannot be done, then redisplay
29347 recenters point as usual.
29348
29349 If the value is greater than 100, redisplay will never recenter point,
29350 but will always scroll just enough text to bring point into view, even
29351 if you move far away.
29352
29353 A value of zero means always recenter point if it moves off screen. */);
29354 scroll_conservatively = 0;
29355
29356 DEFVAR_INT ("scroll-margin", scroll_margin,
29357 doc: /* Number of lines of margin at the top and bottom of a window.
29358 Recenter the window whenever point gets within this many lines
29359 of the top or bottom of the window. */);
29360 scroll_margin = 0;
29361
29362 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
29363 doc: /* Pixels per inch value for non-window system displays.
29364 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
29365 Vdisplay_pixels_per_inch = make_float (72.0);
29366
29367 #ifdef GLYPH_DEBUG
29368 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
29369 #endif
29370
29371 DEFVAR_LISP ("truncate-partial-width-windows",
29372 Vtruncate_partial_width_windows,
29373 doc: /* Non-nil means truncate lines in windows narrower than the frame.
29374 For an integer value, truncate lines in each window narrower than the
29375 full frame width, provided the window width is less than that integer;
29376 otherwise, respect the value of `truncate-lines'.
29377
29378 For any other non-nil value, truncate lines in all windows that do
29379 not span the full frame width.
29380
29381 A value of nil means to respect the value of `truncate-lines'.
29382
29383 If `word-wrap' is enabled, you might want to reduce this. */);
29384 Vtruncate_partial_width_windows = make_number (50);
29385
29386 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
29387 doc: /* Maximum buffer size for which line number should be displayed.
29388 If the buffer is bigger than this, the line number does not appear
29389 in the mode line. A value of nil means no limit. */);
29390 Vline_number_display_limit = Qnil;
29391
29392 DEFVAR_INT ("line-number-display-limit-width",
29393 line_number_display_limit_width,
29394 doc: /* Maximum line width (in characters) for line number display.
29395 If the average length of the lines near point is bigger than this, then the
29396 line number may be omitted from the mode line. */);
29397 line_number_display_limit_width = 200;
29398
29399 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
29400 doc: /* Non-nil means highlight region even in nonselected windows. */);
29401 highlight_nonselected_windows = 0;
29402
29403 DEFVAR_BOOL ("multiple-frames", multiple_frames,
29404 doc: /* Non-nil if more than one frame is visible on this display.
29405 Minibuffer-only frames don't count, but iconified frames do.
29406 This variable is not guaranteed to be accurate except while processing
29407 `frame-title-format' and `icon-title-format'. */);
29408
29409 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
29410 doc: /* Template for displaying the title bar of visible frames.
29411 \(Assuming the window manager supports this feature.)
29412
29413 This variable has the same structure as `mode-line-format', except that
29414 the %c and %l constructs are ignored. It is used only on frames for
29415 which no explicit name has been set \(see `modify-frame-parameters'). */);
29416
29417 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
29418 doc: /* Template for displaying the title bar of an iconified frame.
29419 \(Assuming the window manager supports this feature.)
29420 This variable has the same structure as `mode-line-format' (which see),
29421 and is used only on frames for which no explicit name has been set
29422 \(see `modify-frame-parameters'). */);
29423 Vicon_title_format
29424 = Vframe_title_format
29425 = listn (CONSTYPE_PURE, 3,
29426 intern_c_string ("multiple-frames"),
29427 build_pure_c_string ("%b"),
29428 listn (CONSTYPE_PURE, 4,
29429 empty_unibyte_string,
29430 intern_c_string ("invocation-name"),
29431 build_pure_c_string ("@"),
29432 intern_c_string ("system-name")));
29433
29434 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
29435 doc: /* Maximum number of lines to keep in the message log buffer.
29436 If nil, disable message logging. If t, log messages but don't truncate
29437 the buffer when it becomes large. */);
29438 Vmessage_log_max = make_number (1000);
29439
29440 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
29441 doc: /* Functions called before redisplay, if window sizes have changed.
29442 The value should be a list of functions that take one argument.
29443 Just before redisplay, for each frame, if any of its windows have changed
29444 size since the last redisplay, or have been split or deleted,
29445 all the functions in the list are called, with the frame as argument. */);
29446 Vwindow_size_change_functions = Qnil;
29447
29448 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
29449 doc: /* List of functions to call before redisplaying a window with scrolling.
29450 Each function is called with two arguments, the window and its new
29451 display-start position. Note that these functions are also called by
29452 `set-window-buffer'. Also note that the value of `window-end' is not
29453 valid when these functions are called.
29454
29455 Warning: Do not use this feature to alter the way the window
29456 is scrolled. It is not designed for that, and such use probably won't
29457 work. */);
29458 Vwindow_scroll_functions = Qnil;
29459
29460 DEFVAR_LISP ("window-text-change-functions",
29461 Vwindow_text_change_functions,
29462 doc: /* Functions to call in redisplay when text in the window might change. */);
29463 Vwindow_text_change_functions = Qnil;
29464
29465 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
29466 doc: /* Functions called when redisplay of a window reaches the end trigger.
29467 Each function is called with two arguments, the window and the end trigger value.
29468 See `set-window-redisplay-end-trigger'. */);
29469 Vredisplay_end_trigger_functions = Qnil;
29470
29471 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
29472 doc: /* Non-nil means autoselect window with mouse pointer.
29473 If nil, do not autoselect windows.
29474 A positive number means delay autoselection by that many seconds: a
29475 window is autoselected only after the mouse has remained in that
29476 window for the duration of the delay.
29477 A negative number has a similar effect, but causes windows to be
29478 autoselected only after the mouse has stopped moving. \(Because of
29479 the way Emacs compares mouse events, you will occasionally wait twice
29480 that time before the window gets selected.\)
29481 Any other value means to autoselect window instantaneously when the
29482 mouse pointer enters it.
29483
29484 Autoselection selects the minibuffer only if it is active, and never
29485 unselects the minibuffer if it is active.
29486
29487 When customizing this variable make sure that the actual value of
29488 `focus-follows-mouse' matches the behavior of your window manager. */);
29489 Vmouse_autoselect_window = Qnil;
29490
29491 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29492 doc: /* Non-nil means automatically resize tool-bars.
29493 This dynamically changes the tool-bar's height to the minimum height
29494 that is needed to make all tool-bar items visible.
29495 If value is `grow-only', the tool-bar's height is only increased
29496 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29497 Vauto_resize_tool_bars = Qt;
29498
29499 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29500 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29501 auto_raise_tool_bar_buttons_p = 1;
29502
29503 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29504 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29505 make_cursor_line_fully_visible_p = 1;
29506
29507 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29508 doc: /* Border below tool-bar in pixels.
29509 If an integer, use it as the height of the border.
29510 If it is one of `internal-border-width' or `border-width', use the
29511 value of the corresponding frame parameter.
29512 Otherwise, no border is added below the tool-bar. */);
29513 Vtool_bar_border = Qinternal_border_width;
29514
29515 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29516 doc: /* Margin around tool-bar buttons in pixels.
29517 If an integer, use that for both horizontal and vertical margins.
29518 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29519 HORZ specifying the horizontal margin, and VERT specifying the
29520 vertical margin. */);
29521 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29522
29523 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29524 doc: /* Relief thickness of tool-bar buttons. */);
29525 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29526
29527 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29528 doc: /* Tool bar style to use.
29529 It can be one of
29530 image - show images only
29531 text - show text only
29532 both - show both, text below image
29533 both-horiz - show text to the right of the image
29534 text-image-horiz - show text to the left of the image
29535 any other - use system default or image if no system default.
29536
29537 This variable only affects the GTK+ toolkit version of Emacs. */);
29538 Vtool_bar_style = Qnil;
29539
29540 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29541 doc: /* Maximum number of characters a label can have to be shown.
29542 The tool bar style must also show labels for this to have any effect, see
29543 `tool-bar-style'. */);
29544 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29545
29546 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29547 doc: /* List of functions to call to fontify regions of text.
29548 Each function is called with one argument POS. Functions must
29549 fontify a region starting at POS in the current buffer, and give
29550 fontified regions the property `fontified'. */);
29551 Vfontification_functions = Qnil;
29552 Fmake_variable_buffer_local (Qfontification_functions);
29553
29554 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29555 unibyte_display_via_language_environment,
29556 doc: /* Non-nil means display unibyte text according to language environment.
29557 Specifically, this means that raw bytes in the range 160-255 decimal
29558 are displayed by converting them to the equivalent multibyte characters
29559 according to the current language environment. As a result, they are
29560 displayed according to the current fontset.
29561
29562 Note that this variable affects only how these bytes are displayed,
29563 but does not change the fact they are interpreted as raw bytes. */);
29564 unibyte_display_via_language_environment = 0;
29565
29566 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29567 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29568 If a float, it specifies a fraction of the mini-window frame's height.
29569 If an integer, it specifies a number of lines. */);
29570 Vmax_mini_window_height = make_float (0.25);
29571
29572 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29573 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29574 A value of nil means don't automatically resize mini-windows.
29575 A value of t means resize them to fit the text displayed in them.
29576 A value of `grow-only', the default, means let mini-windows grow only;
29577 they return to their normal size when the minibuffer is closed, or the
29578 echo area becomes empty. */);
29579 Vresize_mini_windows = Qgrow_only;
29580
29581 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29582 doc: /* Alist specifying how to blink the cursor off.
29583 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29584 `cursor-type' frame-parameter or variable equals ON-STATE,
29585 comparing using `equal', Emacs uses OFF-STATE to specify
29586 how to blink it off. ON-STATE and OFF-STATE are values for
29587 the `cursor-type' frame parameter.
29588
29589 If a frame's ON-STATE has no entry in this list,
29590 the frame's other specifications determine how to blink the cursor off. */);
29591 Vblink_cursor_alist = Qnil;
29592
29593 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29594 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29595 If non-nil, windows are automatically scrolled horizontally to make
29596 point visible. */);
29597 automatic_hscrolling_p = 1;
29598 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29599
29600 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29601 doc: /* How many columns away from the window edge point is allowed to get
29602 before automatic hscrolling will horizontally scroll the window. */);
29603 hscroll_margin = 5;
29604
29605 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29606 doc: /* How many columns to scroll the window when point gets too close to the edge.
29607 When point is less than `hscroll-margin' columns from the window
29608 edge, automatic hscrolling will scroll the window by the amount of columns
29609 determined by this variable. If its value is a positive integer, scroll that
29610 many columns. If it's a positive floating-point number, it specifies the
29611 fraction of the window's width to scroll. If it's nil or zero, point will be
29612 centered horizontally after the scroll. Any other value, including negative
29613 numbers, are treated as if the value were zero.
29614
29615 Automatic hscrolling always moves point outside the scroll margin, so if
29616 point was more than scroll step columns inside the margin, the window will
29617 scroll more than the value given by the scroll step.
29618
29619 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29620 and `scroll-right' overrides this variable's effect. */);
29621 Vhscroll_step = make_number (0);
29622
29623 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29624 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29625 Bind this around calls to `message' to let it take effect. */);
29626 message_truncate_lines = 0;
29627
29628 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29629 doc: /* Normal hook run to update the menu bar definitions.
29630 Redisplay runs this hook before it redisplays the menu bar.
29631 This is used to update submenus such as Buffers,
29632 whose contents depend on various data. */);
29633 Vmenu_bar_update_hook = Qnil;
29634
29635 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29636 doc: /* Frame for which we are updating a menu.
29637 The enable predicate for a menu binding should check this variable. */);
29638 Vmenu_updating_frame = Qnil;
29639
29640 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29641 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29642 inhibit_menubar_update = 0;
29643
29644 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29645 doc: /* Prefix prepended to all continuation lines at display time.
29646 The value may be a string, an image, or a stretch-glyph; it is
29647 interpreted in the same way as the value of a `display' text property.
29648
29649 This variable is overridden by any `wrap-prefix' text or overlay
29650 property.
29651
29652 To add a prefix to non-continuation lines, use `line-prefix'. */);
29653 Vwrap_prefix = Qnil;
29654 DEFSYM (Qwrap_prefix, "wrap-prefix");
29655 Fmake_variable_buffer_local (Qwrap_prefix);
29656
29657 DEFVAR_LISP ("line-prefix", Vline_prefix,
29658 doc: /* Prefix prepended to all non-continuation lines at display time.
29659 The value may be a string, an image, or a stretch-glyph; it is
29660 interpreted in the same way as the value of a `display' text property.
29661
29662 This variable is overridden by any `line-prefix' text or overlay
29663 property.
29664
29665 To add a prefix to continuation lines, use `wrap-prefix'. */);
29666 Vline_prefix = Qnil;
29667 DEFSYM (Qline_prefix, "line-prefix");
29668 Fmake_variable_buffer_local (Qline_prefix);
29669
29670 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29671 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29672 inhibit_eval_during_redisplay = 0;
29673
29674 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29675 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29676 inhibit_free_realized_faces = 0;
29677
29678 #ifdef GLYPH_DEBUG
29679 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29680 doc: /* Inhibit try_window_id display optimization. */);
29681 inhibit_try_window_id = 0;
29682
29683 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29684 doc: /* Inhibit try_window_reusing display optimization. */);
29685 inhibit_try_window_reusing = 0;
29686
29687 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29688 doc: /* Inhibit try_cursor_movement display optimization. */);
29689 inhibit_try_cursor_movement = 0;
29690 #endif /* GLYPH_DEBUG */
29691
29692 DEFVAR_INT ("overline-margin", overline_margin,
29693 doc: /* Space between overline and text, in pixels.
29694 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29695 margin to the character height. */);
29696 overline_margin = 2;
29697
29698 DEFVAR_INT ("underline-minimum-offset",
29699 underline_minimum_offset,
29700 doc: /* Minimum distance between baseline and underline.
29701 This can improve legibility of underlined text at small font sizes,
29702 particularly when using variable `x-use-underline-position-properties'
29703 with fonts that specify an UNDERLINE_POSITION relatively close to the
29704 baseline. The default value is 1. */);
29705 underline_minimum_offset = 1;
29706
29707 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29708 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29709 This feature only works when on a window system that can change
29710 cursor shapes. */);
29711 display_hourglass_p = 1;
29712
29713 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29714 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29715 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29716
29717 hourglass_atimer = NULL;
29718 hourglass_shown_p = 0;
29719
29720 DEFSYM (Qglyphless_char, "glyphless-char");
29721 DEFSYM (Qhex_code, "hex-code");
29722 DEFSYM (Qempty_box, "empty-box");
29723 DEFSYM (Qthin_space, "thin-space");
29724 DEFSYM (Qzero_width, "zero-width");
29725
29726 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29727 /* Intern this now in case it isn't already done.
29728 Setting this variable twice is harmless.
29729 But don't staticpro it here--that is done in alloc.c. */
29730 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
29731 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29732
29733 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29734 doc: /* Char-table defining glyphless characters.
29735 Each element, if non-nil, should be one of the following:
29736 an ASCII acronym string: display this string in a box
29737 `hex-code': display the hexadecimal code of a character in a box
29738 `empty-box': display as an empty box
29739 `thin-space': display as 1-pixel width space
29740 `zero-width': don't display
29741 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29742 display method for graphical terminals and text terminals respectively.
29743 GRAPHICAL and TEXT should each have one of the values listed above.
29744
29745 The char-table has one extra slot to control the display of a character for
29746 which no font is found. This slot only takes effect on graphical terminals.
29747 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29748 `thin-space'. The default is `empty-box'. */);
29749 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29750 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29751 Qempty_box);
29752
29753 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
29754 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
29755 Vdebug_on_message = Qnil;
29756 }
29757
29758
29759 /* Initialize this module when Emacs starts. */
29760
29761 void
29762 init_xdisp (void)
29763 {
29764 current_header_line_height = current_mode_line_height = -1;
29765
29766 CHARPOS (this_line_start_pos) = 0;
29767
29768 if (!noninteractive)
29769 {
29770 struct window *m = XWINDOW (minibuf_window);
29771 Lisp_Object frame = m->frame;
29772 struct frame *f = XFRAME (frame);
29773 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29774 struct window *r = XWINDOW (root);
29775 int i;
29776
29777 echo_area_window = minibuf_window;
29778
29779 r->top_line = FRAME_TOP_MARGIN (f);
29780 r->total_lines = FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
29781 r->total_cols = FRAME_COLS (f);
29782
29783 m->top_line = FRAME_LINES (f) - 1;
29784 m->total_lines = 1;
29785 m->total_cols = FRAME_COLS (f);
29786
29787 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29788 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29789 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29790
29791 /* The default ellipsis glyphs `...'. */
29792 for (i = 0; i < 3; ++i)
29793 default_invis_vector[i] = make_number ('.');
29794 }
29795
29796 {
29797 /* Allocate the buffer for frame titles.
29798 Also used for `format-mode-line'. */
29799 int size = 100;
29800 mode_line_noprop_buf = xmalloc (size);
29801 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29802 mode_line_noprop_ptr = mode_line_noprop_buf;
29803 mode_line_target = MODE_LINE_DISPLAY;
29804 }
29805
29806 help_echo_showing_p = 0;
29807 }
29808
29809 /* Platform-independent portion of hourglass implementation. */
29810
29811 /* Cancel a currently active hourglass timer, and start a new one. */
29812 void
29813 start_hourglass (void)
29814 {
29815 #if defined (HAVE_WINDOW_SYSTEM)
29816 EMACS_TIME delay;
29817
29818 cancel_hourglass ();
29819
29820 if (INTEGERP (Vhourglass_delay)
29821 && XINT (Vhourglass_delay) > 0)
29822 delay = make_emacs_time (min (XINT (Vhourglass_delay),
29823 TYPE_MAXIMUM (time_t)),
29824 0);
29825 else if (FLOATP (Vhourglass_delay)
29826 && XFLOAT_DATA (Vhourglass_delay) > 0)
29827 delay = EMACS_TIME_FROM_DOUBLE (XFLOAT_DATA (Vhourglass_delay));
29828 else
29829 delay = make_emacs_time (DEFAULT_HOURGLASS_DELAY, 0);
29830
29831 #ifdef HAVE_NTGUI
29832 {
29833 extern void w32_note_current_window (void);
29834 w32_note_current_window ();
29835 }
29836 #endif /* HAVE_NTGUI */
29837
29838 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29839 show_hourglass, NULL);
29840 #endif
29841 }
29842
29843
29844 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29845 shown. */
29846 void
29847 cancel_hourglass (void)
29848 {
29849 #if defined (HAVE_WINDOW_SYSTEM)
29850 if (hourglass_atimer)
29851 {
29852 cancel_atimer (hourglass_atimer);
29853 hourglass_atimer = NULL;
29854 }
29855
29856 if (hourglass_shown_p)
29857 hide_hourglass ();
29858 #endif
29859 }