Redesign redisplay interface to drop global output_cursor.
[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 faces.
3916 For strings from wrap-prefix and line-prefix properties,
3917 use the default face, possibly remapped via
3918 Vface_remapping_alist. */
3919 base_face_id = it->string_from_prefix_prop_p
3920 ? (!NILP (Vface_remapping_alist)
3921 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3922 : DEFAULT_FACE_ID)
3923 : underlying_face_id (it);
3924 }
3925
3926 new_face_id = face_at_string_position (it->w,
3927 it->string,
3928 IT_STRING_CHARPOS (*it),
3929 bufpos,
3930 it->region_beg_charpos,
3931 it->region_end_charpos,
3932 &next_stop,
3933 base_face_id, 0);
3934
3935 /* Is this a start of a run of characters with box? Caveat:
3936 this can be called for a freshly allocated iterator; face_id
3937 is -1 is this case. We know that the new face will not
3938 change until the next check pos, i.e. if the new face has a
3939 box, all characters up to that position will have a
3940 box. But, as usual, we don't know whether that position
3941 is really the end. */
3942 if (new_face_id != it->face_id)
3943 {
3944 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3945 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3946
3947 /* If new face has a box but old face hasn't, this is the
3948 start of a run of characters with box, i.e. it has a
3949 shadow on the left side. */
3950 it->start_of_box_run_p
3951 = new_face->box && (old_face == NULL || !old_face->box);
3952 it->face_box_p = new_face->box != FACE_NO_BOX;
3953 }
3954 }
3955
3956 it->face_id = new_face_id;
3957 return HANDLED_NORMALLY;
3958 }
3959
3960
3961 /* Return the ID of the face ``underlying'' IT's current position,
3962 which is in a string. If the iterator is associated with a
3963 buffer, return the face at IT's current buffer position.
3964 Otherwise, use the iterator's base_face_id. */
3965
3966 static int
3967 underlying_face_id (struct it *it)
3968 {
3969 int face_id = it->base_face_id, i;
3970
3971 eassert (STRINGP (it->string));
3972
3973 for (i = it->sp - 1; i >= 0; --i)
3974 if (NILP (it->stack[i].string))
3975 face_id = it->stack[i].face_id;
3976
3977 return face_id;
3978 }
3979
3980
3981 /* Compute the face one character before or after the current position
3982 of IT, in the visual order. BEFORE_P non-zero means get the face
3983 in front (to the left in L2R paragraphs, to the right in R2L
3984 paragraphs) of IT's screen position. Value is the ID of the face. */
3985
3986 static int
3987 face_before_or_after_it_pos (struct it *it, int before_p)
3988 {
3989 int face_id, limit;
3990 ptrdiff_t next_check_charpos;
3991 struct it it_copy;
3992 void *it_copy_data = NULL;
3993
3994 eassert (it->s == NULL);
3995
3996 if (STRINGP (it->string))
3997 {
3998 ptrdiff_t bufpos, charpos;
3999 int base_face_id;
4000
4001 /* No face change past the end of the string (for the case
4002 we are padding with spaces). No face change before the
4003 string start. */
4004 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
4005 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
4006 return it->face_id;
4007
4008 if (!it->bidi_p)
4009 {
4010 /* Set charpos to the position before or after IT's current
4011 position, in the logical order, which in the non-bidi
4012 case is the same as the visual order. */
4013 if (before_p)
4014 charpos = IT_STRING_CHARPOS (*it) - 1;
4015 else if (it->what == IT_COMPOSITION)
4016 /* For composition, we must check the character after the
4017 composition. */
4018 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4019 else
4020 charpos = IT_STRING_CHARPOS (*it) + 1;
4021 }
4022 else
4023 {
4024 if (before_p)
4025 {
4026 /* With bidi iteration, the character before the current
4027 in the visual order cannot be found by simple
4028 iteration, because "reverse" reordering is not
4029 supported. Instead, we need to use the move_it_*
4030 family of functions. */
4031 /* Ignore face changes before the first visible
4032 character on this display line. */
4033 if (it->current_x <= it->first_visible_x)
4034 return it->face_id;
4035 SAVE_IT (it_copy, *it, it_copy_data);
4036 /* Implementation note: Since move_it_in_display_line
4037 works in the iterator geometry, and thinks the first
4038 character is always the leftmost, even in R2L lines,
4039 we don't need to distinguish between the R2L and L2R
4040 cases here. */
4041 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
4042 it_copy.current_x - 1, MOVE_TO_X);
4043 charpos = IT_STRING_CHARPOS (it_copy);
4044 RESTORE_IT (it, it, it_copy_data);
4045 }
4046 else
4047 {
4048 /* Set charpos to the string position of the character
4049 that comes after IT's current position in the visual
4050 order. */
4051 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4052
4053 it_copy = *it;
4054 while (n--)
4055 bidi_move_to_visually_next (&it_copy.bidi_it);
4056
4057 charpos = it_copy.bidi_it.charpos;
4058 }
4059 }
4060 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4061
4062 if (it->current.overlay_string_index >= 0)
4063 bufpos = IT_CHARPOS (*it);
4064 else
4065 bufpos = 0;
4066
4067 base_face_id = underlying_face_id (it);
4068
4069 /* Get the face for ASCII, or unibyte. */
4070 face_id = face_at_string_position (it->w,
4071 it->string,
4072 charpos,
4073 bufpos,
4074 it->region_beg_charpos,
4075 it->region_end_charpos,
4076 &next_check_charpos,
4077 base_face_id, 0);
4078
4079 /* Correct the face for charsets different from ASCII. Do it
4080 for the multibyte case only. The face returned above is
4081 suitable for unibyte text if IT->string is unibyte. */
4082 if (STRING_MULTIBYTE (it->string))
4083 {
4084 struct text_pos pos1 = string_pos (charpos, it->string);
4085 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4086 int c, len;
4087 struct face *face = FACE_FROM_ID (it->f, face_id);
4088
4089 c = string_char_and_length (p, &len);
4090 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4091 }
4092 }
4093 else
4094 {
4095 struct text_pos pos;
4096
4097 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4098 || (IT_CHARPOS (*it) <= BEGV && before_p))
4099 return it->face_id;
4100
4101 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4102 pos = it->current.pos;
4103
4104 if (!it->bidi_p)
4105 {
4106 if (before_p)
4107 DEC_TEXT_POS (pos, it->multibyte_p);
4108 else
4109 {
4110 if (it->what == IT_COMPOSITION)
4111 {
4112 /* For composition, we must check the position after
4113 the composition. */
4114 pos.charpos += it->cmp_it.nchars;
4115 pos.bytepos += it->len;
4116 }
4117 else
4118 INC_TEXT_POS (pos, it->multibyte_p);
4119 }
4120 }
4121 else
4122 {
4123 if (before_p)
4124 {
4125 /* With bidi iteration, the character before the current
4126 in the visual order cannot be found by simple
4127 iteration, because "reverse" reordering is not
4128 supported. Instead, we need to use the move_it_*
4129 family of functions. */
4130 /* Ignore face changes before the first visible
4131 character on this display line. */
4132 if (it->current_x <= it->first_visible_x)
4133 return it->face_id;
4134 SAVE_IT (it_copy, *it, it_copy_data);
4135 /* Implementation note: Since move_it_in_display_line
4136 works in the iterator geometry, and thinks the first
4137 character is always the leftmost, even in R2L lines,
4138 we don't need to distinguish between the R2L and L2R
4139 cases here. */
4140 move_it_in_display_line (&it_copy, ZV,
4141 it_copy.current_x - 1, MOVE_TO_X);
4142 pos = it_copy.current.pos;
4143 RESTORE_IT (it, it, it_copy_data);
4144 }
4145 else
4146 {
4147 /* Set charpos to the buffer position of the character
4148 that comes after IT's current position in the visual
4149 order. */
4150 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4151
4152 it_copy = *it;
4153 while (n--)
4154 bidi_move_to_visually_next (&it_copy.bidi_it);
4155
4156 SET_TEXT_POS (pos,
4157 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4158 }
4159 }
4160 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4161
4162 /* Determine face for CHARSET_ASCII, or unibyte. */
4163 face_id = face_at_buffer_position (it->w,
4164 CHARPOS (pos),
4165 it->region_beg_charpos,
4166 it->region_end_charpos,
4167 &next_check_charpos,
4168 limit, 0, -1);
4169
4170 /* Correct the face for charsets different from ASCII. Do it
4171 for the multibyte case only. The face returned above is
4172 suitable for unibyte text if current_buffer is unibyte. */
4173 if (it->multibyte_p)
4174 {
4175 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4176 struct face *face = FACE_FROM_ID (it->f, face_id);
4177 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4178 }
4179 }
4180
4181 return face_id;
4182 }
4183
4184
4185 \f
4186 /***********************************************************************
4187 Invisible text
4188 ***********************************************************************/
4189
4190 /* Set up iterator IT from invisible properties at its current
4191 position. Called from handle_stop. */
4192
4193 static enum prop_handled
4194 handle_invisible_prop (struct it *it)
4195 {
4196 enum prop_handled handled = HANDLED_NORMALLY;
4197 int invis_p;
4198 Lisp_Object prop;
4199
4200 if (STRINGP (it->string))
4201 {
4202 Lisp_Object end_charpos, limit, charpos;
4203
4204 /* Get the value of the invisible text property at the
4205 current position. Value will be nil if there is no such
4206 property. */
4207 charpos = make_number (IT_STRING_CHARPOS (*it));
4208 prop = Fget_text_property (charpos, Qinvisible, it->string);
4209 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4210
4211 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4212 {
4213 /* Record whether we have to display an ellipsis for the
4214 invisible text. */
4215 int display_ellipsis_p = (invis_p == 2);
4216 ptrdiff_t len, endpos;
4217
4218 handled = HANDLED_RECOMPUTE_PROPS;
4219
4220 /* Get the position at which the next visible text can be
4221 found in IT->string, if any. */
4222 endpos = len = SCHARS (it->string);
4223 XSETINT (limit, len);
4224 do
4225 {
4226 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4227 it->string, limit);
4228 if (INTEGERP (end_charpos))
4229 {
4230 endpos = XFASTINT (end_charpos);
4231 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4232 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4233 if (invis_p == 2)
4234 display_ellipsis_p = 1;
4235 }
4236 }
4237 while (invis_p && endpos < len);
4238
4239 if (display_ellipsis_p)
4240 it->ellipsis_p = 1;
4241
4242 if (endpos < len)
4243 {
4244 /* Text at END_CHARPOS is visible. Move IT there. */
4245 struct text_pos old;
4246 ptrdiff_t oldpos;
4247
4248 old = it->current.string_pos;
4249 oldpos = CHARPOS (old);
4250 if (it->bidi_p)
4251 {
4252 if (it->bidi_it.first_elt
4253 && it->bidi_it.charpos < SCHARS (it->string))
4254 bidi_paragraph_init (it->paragraph_embedding,
4255 &it->bidi_it, 1);
4256 /* Bidi-iterate out of the invisible text. */
4257 do
4258 {
4259 bidi_move_to_visually_next (&it->bidi_it);
4260 }
4261 while (oldpos <= it->bidi_it.charpos
4262 && it->bidi_it.charpos < endpos);
4263
4264 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4265 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4266 if (IT_CHARPOS (*it) >= endpos)
4267 it->prev_stop = endpos;
4268 }
4269 else
4270 {
4271 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4272 compute_string_pos (&it->current.string_pos, old, it->string);
4273 }
4274 }
4275 else
4276 {
4277 /* The rest of the string is invisible. If this is an
4278 overlay string, proceed with the next overlay string
4279 or whatever comes and return a character from there. */
4280 if (it->current.overlay_string_index >= 0
4281 && !display_ellipsis_p)
4282 {
4283 next_overlay_string (it);
4284 /* Don't check for overlay strings when we just
4285 finished processing them. */
4286 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4287 }
4288 else
4289 {
4290 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4291 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4292 }
4293 }
4294 }
4295 }
4296 else
4297 {
4298 ptrdiff_t newpos, next_stop, start_charpos, tem;
4299 Lisp_Object pos, overlay;
4300
4301 /* First of all, is there invisible text at this position? */
4302 tem = start_charpos = IT_CHARPOS (*it);
4303 pos = make_number (tem);
4304 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4305 &overlay);
4306 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4307
4308 /* If we are on invisible text, skip over it. */
4309 if (invis_p && start_charpos < it->end_charpos)
4310 {
4311 /* Record whether we have to display an ellipsis for the
4312 invisible text. */
4313 int display_ellipsis_p = invis_p == 2;
4314
4315 handled = HANDLED_RECOMPUTE_PROPS;
4316
4317 /* Loop skipping over invisible text. The loop is left at
4318 ZV or with IT on the first char being visible again. */
4319 do
4320 {
4321 /* Try to skip some invisible text. Return value is the
4322 position reached which can be equal to where we start
4323 if there is nothing invisible there. This skips both
4324 over invisible text properties and overlays with
4325 invisible property. */
4326 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4327
4328 /* If we skipped nothing at all we weren't at invisible
4329 text in the first place. If everything to the end of
4330 the buffer was skipped, end the loop. */
4331 if (newpos == tem || newpos >= ZV)
4332 invis_p = 0;
4333 else
4334 {
4335 /* We skipped some characters but not necessarily
4336 all there are. Check if we ended up on visible
4337 text. Fget_char_property returns the property of
4338 the char before the given position, i.e. if we
4339 get invis_p = 0, this means that the char at
4340 newpos is visible. */
4341 pos = make_number (newpos);
4342 prop = Fget_char_property (pos, Qinvisible, it->window);
4343 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4344 }
4345
4346 /* If we ended up on invisible text, proceed to
4347 skip starting with next_stop. */
4348 if (invis_p)
4349 tem = next_stop;
4350
4351 /* If there are adjacent invisible texts, don't lose the
4352 second one's ellipsis. */
4353 if (invis_p == 2)
4354 display_ellipsis_p = 1;
4355 }
4356 while (invis_p);
4357
4358 /* The position newpos is now either ZV or on visible text. */
4359 if (it->bidi_p)
4360 {
4361 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4362 int on_newline =
4363 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4364 int after_newline =
4365 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4366
4367 /* If the invisible text ends on a newline or on a
4368 character after a newline, we can avoid the costly,
4369 character by character, bidi iteration to NEWPOS, and
4370 instead simply reseat the iterator there. That's
4371 because all bidi reordering information is tossed at
4372 the newline. This is a big win for modes that hide
4373 complete lines, like Outline, Org, etc. */
4374 if (on_newline || after_newline)
4375 {
4376 struct text_pos tpos;
4377 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4378
4379 SET_TEXT_POS (tpos, newpos, bpos);
4380 reseat_1 (it, tpos, 0);
4381 /* If we reseat on a newline/ZV, we need to prep the
4382 bidi iterator for advancing to the next character
4383 after the newline/EOB, keeping the current paragraph
4384 direction (so that PRODUCE_GLYPHS does TRT wrt
4385 prepending/appending glyphs to a glyph row). */
4386 if (on_newline)
4387 {
4388 it->bidi_it.first_elt = 0;
4389 it->bidi_it.paragraph_dir = pdir;
4390 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4391 it->bidi_it.nchars = 1;
4392 it->bidi_it.ch_len = 1;
4393 }
4394 }
4395 else /* Must use the slow method. */
4396 {
4397 /* With bidi iteration, the region of invisible text
4398 could start and/or end in the middle of a
4399 non-base embedding level. Therefore, we need to
4400 skip invisible text using the bidi iterator,
4401 starting at IT's current position, until we find
4402 ourselves outside of the invisible text.
4403 Skipping invisible text _after_ bidi iteration
4404 avoids affecting the visual order of the
4405 displayed text when invisible properties are
4406 added or removed. */
4407 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4408 {
4409 /* If we were `reseat'ed to a new paragraph,
4410 determine the paragraph base direction. We
4411 need to do it now because
4412 next_element_from_buffer may not have a
4413 chance to do it, if we are going to skip any
4414 text at the beginning, which resets the
4415 FIRST_ELT flag. */
4416 bidi_paragraph_init (it->paragraph_embedding,
4417 &it->bidi_it, 1);
4418 }
4419 do
4420 {
4421 bidi_move_to_visually_next (&it->bidi_it);
4422 }
4423 while (it->stop_charpos <= it->bidi_it.charpos
4424 && it->bidi_it.charpos < newpos);
4425 IT_CHARPOS (*it) = it->bidi_it.charpos;
4426 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4427 /* If we overstepped NEWPOS, record its position in
4428 the iterator, so that we skip invisible text if
4429 later the bidi iteration lands us in the
4430 invisible region again. */
4431 if (IT_CHARPOS (*it) >= newpos)
4432 it->prev_stop = newpos;
4433 }
4434 }
4435 else
4436 {
4437 IT_CHARPOS (*it) = newpos;
4438 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4439 }
4440
4441 /* If there are before-strings at the start of invisible
4442 text, and the text is invisible because of a text
4443 property, arrange to show before-strings because 20.x did
4444 it that way. (If the text is invisible because of an
4445 overlay property instead of a text property, this is
4446 already handled in the overlay code.) */
4447 if (NILP (overlay)
4448 && get_overlay_strings (it, it->stop_charpos))
4449 {
4450 handled = HANDLED_RECOMPUTE_PROPS;
4451 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4452 }
4453 else if (display_ellipsis_p)
4454 {
4455 /* Make sure that the glyphs of the ellipsis will get
4456 correct `charpos' values. If we would not update
4457 it->position here, the glyphs would belong to the
4458 last visible character _before_ the invisible
4459 text, which confuses `set_cursor_from_row'.
4460
4461 We use the last invisible position instead of the
4462 first because this way the cursor is always drawn on
4463 the first "." of the ellipsis, whenever PT is inside
4464 the invisible text. Otherwise the cursor would be
4465 placed _after_ the ellipsis when the point is after the
4466 first invisible character. */
4467 if (!STRINGP (it->object))
4468 {
4469 it->position.charpos = newpos - 1;
4470 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4471 }
4472 it->ellipsis_p = 1;
4473 /* Let the ellipsis display before
4474 considering any properties of the following char.
4475 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4476 handled = HANDLED_RETURN;
4477 }
4478 }
4479 }
4480
4481 return handled;
4482 }
4483
4484
4485 /* Make iterator IT return `...' next.
4486 Replaces LEN characters from buffer. */
4487
4488 static void
4489 setup_for_ellipsis (struct it *it, int len)
4490 {
4491 /* Use the display table definition for `...'. Invalid glyphs
4492 will be handled by the method returning elements from dpvec. */
4493 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4494 {
4495 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4496 it->dpvec = v->contents;
4497 it->dpend = v->contents + v->header.size;
4498 }
4499 else
4500 {
4501 /* Default `...'. */
4502 it->dpvec = default_invis_vector;
4503 it->dpend = default_invis_vector + 3;
4504 }
4505
4506 it->dpvec_char_len = len;
4507 it->current.dpvec_index = 0;
4508 it->dpvec_face_id = -1;
4509
4510 /* Remember the current face id in case glyphs specify faces.
4511 IT's face is restored in set_iterator_to_next.
4512 saved_face_id was set to preceding char's face in handle_stop. */
4513 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4514 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4515
4516 it->method = GET_FROM_DISPLAY_VECTOR;
4517 it->ellipsis_p = 1;
4518 }
4519
4520
4521 \f
4522 /***********************************************************************
4523 'display' property
4524 ***********************************************************************/
4525
4526 /* Set up iterator IT from `display' property at its current position.
4527 Called from handle_stop.
4528 We return HANDLED_RETURN if some part of the display property
4529 overrides the display of the buffer text itself.
4530 Otherwise we return HANDLED_NORMALLY. */
4531
4532 static enum prop_handled
4533 handle_display_prop (struct it *it)
4534 {
4535 Lisp_Object propval, object, overlay;
4536 struct text_pos *position;
4537 ptrdiff_t bufpos;
4538 /* Nonzero if some property replaces the display of the text itself. */
4539 int display_replaced_p = 0;
4540
4541 if (STRINGP (it->string))
4542 {
4543 object = it->string;
4544 position = &it->current.string_pos;
4545 bufpos = CHARPOS (it->current.pos);
4546 }
4547 else
4548 {
4549 XSETWINDOW (object, it->w);
4550 position = &it->current.pos;
4551 bufpos = CHARPOS (*position);
4552 }
4553
4554 /* Reset those iterator values set from display property values. */
4555 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4556 it->space_width = Qnil;
4557 it->font_height = Qnil;
4558 it->voffset = 0;
4559
4560 /* We don't support recursive `display' properties, i.e. string
4561 values that have a string `display' property, that have a string
4562 `display' property etc. */
4563 if (!it->string_from_display_prop_p)
4564 it->area = TEXT_AREA;
4565
4566 propval = get_char_property_and_overlay (make_number (position->charpos),
4567 Qdisplay, object, &overlay);
4568 if (NILP (propval))
4569 return HANDLED_NORMALLY;
4570 /* Now OVERLAY is the overlay that gave us this property, or nil
4571 if it was a text property. */
4572
4573 if (!STRINGP (it->string))
4574 object = it->w->contents;
4575
4576 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4577 position, bufpos,
4578 FRAME_WINDOW_P (it->f));
4579
4580 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4581 }
4582
4583 /* Subroutine of handle_display_prop. Returns non-zero if the display
4584 specification in SPEC is a replacing specification, i.e. it would
4585 replace the text covered by `display' property with something else,
4586 such as an image or a display string. If SPEC includes any kind or
4587 `(space ...) specification, the value is 2; this is used by
4588 compute_display_string_pos, which see.
4589
4590 See handle_single_display_spec for documentation of arguments.
4591 frame_window_p is non-zero if the window being redisplayed is on a
4592 GUI frame; this argument is used only if IT is NULL, see below.
4593
4594 IT can be NULL, if this is called by the bidi reordering code
4595 through compute_display_string_pos, which see. In that case, this
4596 function only examines SPEC, but does not otherwise "handle" it, in
4597 the sense that it doesn't set up members of IT from the display
4598 spec. */
4599 static int
4600 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4601 Lisp_Object overlay, struct text_pos *position,
4602 ptrdiff_t bufpos, int frame_window_p)
4603 {
4604 int replacing_p = 0;
4605 int rv;
4606
4607 if (CONSP (spec)
4608 /* Simple specifications. */
4609 && !EQ (XCAR (spec), Qimage)
4610 && !EQ (XCAR (spec), Qspace)
4611 && !EQ (XCAR (spec), Qwhen)
4612 && !EQ (XCAR (spec), Qslice)
4613 && !EQ (XCAR (spec), Qspace_width)
4614 && !EQ (XCAR (spec), Qheight)
4615 && !EQ (XCAR (spec), Qraise)
4616 /* Marginal area specifications. */
4617 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4618 && !EQ (XCAR (spec), Qleft_fringe)
4619 && !EQ (XCAR (spec), Qright_fringe)
4620 && !NILP (XCAR (spec)))
4621 {
4622 for (; CONSP (spec); spec = XCDR (spec))
4623 {
4624 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4625 overlay, position, bufpos,
4626 replacing_p, frame_window_p)))
4627 {
4628 replacing_p = rv;
4629 /* If some text in a string is replaced, `position' no
4630 longer points to the position of `object'. */
4631 if (!it || STRINGP (object))
4632 break;
4633 }
4634 }
4635 }
4636 else if (VECTORP (spec))
4637 {
4638 ptrdiff_t i;
4639 for (i = 0; i < ASIZE (spec); ++i)
4640 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4641 overlay, position, bufpos,
4642 replacing_p, frame_window_p)))
4643 {
4644 replacing_p = rv;
4645 /* If some text in a string is replaced, `position' no
4646 longer points to the position of `object'. */
4647 if (!it || STRINGP (object))
4648 break;
4649 }
4650 }
4651 else
4652 {
4653 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4654 position, bufpos, 0,
4655 frame_window_p)))
4656 replacing_p = rv;
4657 }
4658
4659 return replacing_p;
4660 }
4661
4662 /* Value is the position of the end of the `display' property starting
4663 at START_POS in OBJECT. */
4664
4665 static struct text_pos
4666 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4667 {
4668 Lisp_Object end;
4669 struct text_pos end_pos;
4670
4671 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4672 Qdisplay, object, Qnil);
4673 CHARPOS (end_pos) = XFASTINT (end);
4674 if (STRINGP (object))
4675 compute_string_pos (&end_pos, start_pos, it->string);
4676 else
4677 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4678
4679 return end_pos;
4680 }
4681
4682
4683 /* Set up IT from a single `display' property specification SPEC. OBJECT
4684 is the object in which the `display' property was found. *POSITION
4685 is the position in OBJECT at which the `display' property was found.
4686 BUFPOS is the buffer position of OBJECT (different from POSITION if
4687 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4688 previously saw a display specification which already replaced text
4689 display with something else, for example an image; we ignore such
4690 properties after the first one has been processed.
4691
4692 OVERLAY is the overlay this `display' property came from,
4693 or nil if it was a text property.
4694
4695 If SPEC is a `space' or `image' specification, and in some other
4696 cases too, set *POSITION to the position where the `display'
4697 property ends.
4698
4699 If IT is NULL, only examine the property specification in SPEC, but
4700 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4701 is intended to be displayed in a window on a GUI frame.
4702
4703 Value is non-zero if something was found which replaces the display
4704 of buffer or string text. */
4705
4706 static int
4707 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4708 Lisp_Object overlay, struct text_pos *position,
4709 ptrdiff_t bufpos, int display_replaced_p,
4710 int frame_window_p)
4711 {
4712 Lisp_Object form;
4713 Lisp_Object location, value;
4714 struct text_pos start_pos = *position;
4715 int valid_p;
4716
4717 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4718 If the result is non-nil, use VALUE instead of SPEC. */
4719 form = Qt;
4720 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4721 {
4722 spec = XCDR (spec);
4723 if (!CONSP (spec))
4724 return 0;
4725 form = XCAR (spec);
4726 spec = XCDR (spec);
4727 }
4728
4729 if (!NILP (form) && !EQ (form, Qt))
4730 {
4731 ptrdiff_t count = SPECPDL_INDEX ();
4732 struct gcpro gcpro1;
4733
4734 /* Bind `object' to the object having the `display' property, a
4735 buffer or string. Bind `position' to the position in the
4736 object where the property was found, and `buffer-position'
4737 to the current position in the buffer. */
4738
4739 if (NILP (object))
4740 XSETBUFFER (object, current_buffer);
4741 specbind (Qobject, object);
4742 specbind (Qposition, make_number (CHARPOS (*position)));
4743 specbind (Qbuffer_position, make_number (bufpos));
4744 GCPRO1 (form);
4745 form = safe_eval (form);
4746 UNGCPRO;
4747 unbind_to (count, Qnil);
4748 }
4749
4750 if (NILP (form))
4751 return 0;
4752
4753 /* Handle `(height HEIGHT)' specifications. */
4754 if (CONSP (spec)
4755 && EQ (XCAR (spec), Qheight)
4756 && CONSP (XCDR (spec)))
4757 {
4758 if (it)
4759 {
4760 if (!FRAME_WINDOW_P (it->f))
4761 return 0;
4762
4763 it->font_height = XCAR (XCDR (spec));
4764 if (!NILP (it->font_height))
4765 {
4766 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4767 int new_height = -1;
4768
4769 if (CONSP (it->font_height)
4770 && (EQ (XCAR (it->font_height), Qplus)
4771 || EQ (XCAR (it->font_height), Qminus))
4772 && CONSP (XCDR (it->font_height))
4773 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4774 {
4775 /* `(+ N)' or `(- N)' where N is an integer. */
4776 int steps = XINT (XCAR (XCDR (it->font_height)));
4777 if (EQ (XCAR (it->font_height), Qplus))
4778 steps = - steps;
4779 it->face_id = smaller_face (it->f, it->face_id, steps);
4780 }
4781 else if (FUNCTIONP (it->font_height))
4782 {
4783 /* Call function with current height as argument.
4784 Value is the new height. */
4785 Lisp_Object height;
4786 height = safe_call1 (it->font_height,
4787 face->lface[LFACE_HEIGHT_INDEX]);
4788 if (NUMBERP (height))
4789 new_height = XFLOATINT (height);
4790 }
4791 else if (NUMBERP (it->font_height))
4792 {
4793 /* Value is a multiple of the canonical char height. */
4794 struct face *f;
4795
4796 f = FACE_FROM_ID (it->f,
4797 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4798 new_height = (XFLOATINT (it->font_height)
4799 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4800 }
4801 else
4802 {
4803 /* Evaluate IT->font_height with `height' bound to the
4804 current specified height to get the new height. */
4805 ptrdiff_t count = SPECPDL_INDEX ();
4806
4807 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4808 value = safe_eval (it->font_height);
4809 unbind_to (count, Qnil);
4810
4811 if (NUMBERP (value))
4812 new_height = XFLOATINT (value);
4813 }
4814
4815 if (new_height > 0)
4816 it->face_id = face_with_height (it->f, it->face_id, new_height);
4817 }
4818 }
4819
4820 return 0;
4821 }
4822
4823 /* Handle `(space-width WIDTH)'. */
4824 if (CONSP (spec)
4825 && EQ (XCAR (spec), Qspace_width)
4826 && CONSP (XCDR (spec)))
4827 {
4828 if (it)
4829 {
4830 if (!FRAME_WINDOW_P (it->f))
4831 return 0;
4832
4833 value = XCAR (XCDR (spec));
4834 if (NUMBERP (value) && XFLOATINT (value) > 0)
4835 it->space_width = value;
4836 }
4837
4838 return 0;
4839 }
4840
4841 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4842 if (CONSP (spec)
4843 && EQ (XCAR (spec), Qslice))
4844 {
4845 Lisp_Object tem;
4846
4847 if (it)
4848 {
4849 if (!FRAME_WINDOW_P (it->f))
4850 return 0;
4851
4852 if (tem = XCDR (spec), CONSP (tem))
4853 {
4854 it->slice.x = XCAR (tem);
4855 if (tem = XCDR (tem), CONSP (tem))
4856 {
4857 it->slice.y = XCAR (tem);
4858 if (tem = XCDR (tem), CONSP (tem))
4859 {
4860 it->slice.width = XCAR (tem);
4861 if (tem = XCDR (tem), CONSP (tem))
4862 it->slice.height = XCAR (tem);
4863 }
4864 }
4865 }
4866 }
4867
4868 return 0;
4869 }
4870
4871 /* Handle `(raise FACTOR)'. */
4872 if (CONSP (spec)
4873 && EQ (XCAR (spec), Qraise)
4874 && CONSP (XCDR (spec)))
4875 {
4876 if (it)
4877 {
4878 if (!FRAME_WINDOW_P (it->f))
4879 return 0;
4880
4881 #ifdef HAVE_WINDOW_SYSTEM
4882 value = XCAR (XCDR (spec));
4883 if (NUMBERP (value))
4884 {
4885 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4886 it->voffset = - (XFLOATINT (value)
4887 * (FONT_HEIGHT (face->font)));
4888 }
4889 #endif /* HAVE_WINDOW_SYSTEM */
4890 }
4891
4892 return 0;
4893 }
4894
4895 /* Don't handle the other kinds of display specifications
4896 inside a string that we got from a `display' property. */
4897 if (it && it->string_from_display_prop_p)
4898 return 0;
4899
4900 /* Characters having this form of property are not displayed, so
4901 we have to find the end of the property. */
4902 if (it)
4903 {
4904 start_pos = *position;
4905 *position = display_prop_end (it, object, start_pos);
4906 }
4907 value = Qnil;
4908
4909 /* Stop the scan at that end position--we assume that all
4910 text properties change there. */
4911 if (it)
4912 it->stop_charpos = position->charpos;
4913
4914 /* Handle `(left-fringe BITMAP [FACE])'
4915 and `(right-fringe BITMAP [FACE])'. */
4916 if (CONSP (spec)
4917 && (EQ (XCAR (spec), Qleft_fringe)
4918 || EQ (XCAR (spec), Qright_fringe))
4919 && CONSP (XCDR (spec)))
4920 {
4921 int fringe_bitmap;
4922
4923 if (it)
4924 {
4925 if (!FRAME_WINDOW_P (it->f))
4926 /* If we return here, POSITION has been advanced
4927 across the text with this property. */
4928 {
4929 /* Synchronize the bidi iterator with POSITION. This is
4930 needed because we are not going to push the iterator
4931 on behalf of this display property, so there will be
4932 no pop_it call to do this synchronization for us. */
4933 if (it->bidi_p)
4934 {
4935 it->position = *position;
4936 iterate_out_of_display_property (it);
4937 *position = it->position;
4938 }
4939 return 1;
4940 }
4941 }
4942 else if (!frame_window_p)
4943 return 1;
4944
4945 #ifdef HAVE_WINDOW_SYSTEM
4946 value = XCAR (XCDR (spec));
4947 if (!SYMBOLP (value)
4948 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4949 /* If we return here, POSITION has been advanced
4950 across the text with this property. */
4951 {
4952 if (it && it->bidi_p)
4953 {
4954 it->position = *position;
4955 iterate_out_of_display_property (it);
4956 *position = it->position;
4957 }
4958 return 1;
4959 }
4960
4961 if (it)
4962 {
4963 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4964
4965 if (CONSP (XCDR (XCDR (spec))))
4966 {
4967 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4968 int face_id2 = lookup_derived_face (it->f, face_name,
4969 FRINGE_FACE_ID, 0);
4970 if (face_id2 >= 0)
4971 face_id = face_id2;
4972 }
4973
4974 /* Save current settings of IT so that we can restore them
4975 when we are finished with the glyph property value. */
4976 push_it (it, position);
4977
4978 it->area = TEXT_AREA;
4979 it->what = IT_IMAGE;
4980 it->image_id = -1; /* no image */
4981 it->position = start_pos;
4982 it->object = NILP (object) ? it->w->contents : object;
4983 it->method = GET_FROM_IMAGE;
4984 it->from_overlay = Qnil;
4985 it->face_id = face_id;
4986 it->from_disp_prop_p = 1;
4987
4988 /* Say that we haven't consumed the characters with
4989 `display' property yet. The call to pop_it in
4990 set_iterator_to_next will clean this up. */
4991 *position = start_pos;
4992
4993 if (EQ (XCAR (spec), Qleft_fringe))
4994 {
4995 it->left_user_fringe_bitmap = fringe_bitmap;
4996 it->left_user_fringe_face_id = face_id;
4997 }
4998 else
4999 {
5000 it->right_user_fringe_bitmap = fringe_bitmap;
5001 it->right_user_fringe_face_id = face_id;
5002 }
5003 }
5004 #endif /* HAVE_WINDOW_SYSTEM */
5005 return 1;
5006 }
5007
5008 /* Prepare to handle `((margin left-margin) ...)',
5009 `((margin right-margin) ...)' and `((margin nil) ...)'
5010 prefixes for display specifications. */
5011 location = Qunbound;
5012 if (CONSP (spec) && CONSP (XCAR (spec)))
5013 {
5014 Lisp_Object tem;
5015
5016 value = XCDR (spec);
5017 if (CONSP (value))
5018 value = XCAR (value);
5019
5020 tem = XCAR (spec);
5021 if (EQ (XCAR (tem), Qmargin)
5022 && (tem = XCDR (tem),
5023 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5024 (NILP (tem)
5025 || EQ (tem, Qleft_margin)
5026 || EQ (tem, Qright_margin))))
5027 location = tem;
5028 }
5029
5030 if (EQ (location, Qunbound))
5031 {
5032 location = Qnil;
5033 value = spec;
5034 }
5035
5036 /* After this point, VALUE is the property after any
5037 margin prefix has been stripped. It must be a string,
5038 an image specification, or `(space ...)'.
5039
5040 LOCATION specifies where to display: `left-margin',
5041 `right-margin' or nil. */
5042
5043 valid_p = (STRINGP (value)
5044 #ifdef HAVE_WINDOW_SYSTEM
5045 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5046 && valid_image_p (value))
5047 #endif /* not HAVE_WINDOW_SYSTEM */
5048 || (CONSP (value) && EQ (XCAR (value), Qspace)));
5049
5050 if (valid_p && !display_replaced_p)
5051 {
5052 int retval = 1;
5053
5054 if (!it)
5055 {
5056 /* Callers need to know whether the display spec is any kind
5057 of `(space ...)' spec that is about to affect text-area
5058 display. */
5059 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5060 retval = 2;
5061 return retval;
5062 }
5063
5064 /* Save current settings of IT so that we can restore them
5065 when we are finished with the glyph property value. */
5066 push_it (it, position);
5067 it->from_overlay = overlay;
5068 it->from_disp_prop_p = 1;
5069
5070 if (NILP (location))
5071 it->area = TEXT_AREA;
5072 else if (EQ (location, Qleft_margin))
5073 it->area = LEFT_MARGIN_AREA;
5074 else
5075 it->area = RIGHT_MARGIN_AREA;
5076
5077 if (STRINGP (value))
5078 {
5079 it->string = value;
5080 it->multibyte_p = STRING_MULTIBYTE (it->string);
5081 it->current.overlay_string_index = -1;
5082 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5083 it->end_charpos = it->string_nchars = SCHARS (it->string);
5084 it->method = GET_FROM_STRING;
5085 it->stop_charpos = 0;
5086 it->prev_stop = 0;
5087 it->base_level_stop = 0;
5088 it->string_from_display_prop_p = 1;
5089 /* Say that we haven't consumed the characters with
5090 `display' property yet. The call to pop_it in
5091 set_iterator_to_next will clean this up. */
5092 if (BUFFERP (object))
5093 *position = start_pos;
5094
5095 /* Force paragraph direction to be that of the parent
5096 object. If the parent object's paragraph direction is
5097 not yet determined, default to L2R. */
5098 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5099 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5100 else
5101 it->paragraph_embedding = L2R;
5102
5103 /* Set up the bidi iterator for this display string. */
5104 if (it->bidi_p)
5105 {
5106 it->bidi_it.string.lstring = it->string;
5107 it->bidi_it.string.s = NULL;
5108 it->bidi_it.string.schars = it->end_charpos;
5109 it->bidi_it.string.bufpos = bufpos;
5110 it->bidi_it.string.from_disp_str = 1;
5111 it->bidi_it.string.unibyte = !it->multibyte_p;
5112 it->bidi_it.w = it->w;
5113 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5114 }
5115 }
5116 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5117 {
5118 it->method = GET_FROM_STRETCH;
5119 it->object = value;
5120 *position = it->position = start_pos;
5121 retval = 1 + (it->area == TEXT_AREA);
5122 }
5123 #ifdef HAVE_WINDOW_SYSTEM
5124 else
5125 {
5126 it->what = IT_IMAGE;
5127 it->image_id = lookup_image (it->f, value);
5128 it->position = start_pos;
5129 it->object = NILP (object) ? it->w->contents : object;
5130 it->method = GET_FROM_IMAGE;
5131
5132 /* Say that we haven't consumed the characters with
5133 `display' property yet. The call to pop_it in
5134 set_iterator_to_next will clean this up. */
5135 *position = start_pos;
5136 }
5137 #endif /* HAVE_WINDOW_SYSTEM */
5138
5139 return retval;
5140 }
5141
5142 /* Invalid property or property not supported. Restore
5143 POSITION to what it was before. */
5144 *position = start_pos;
5145 return 0;
5146 }
5147
5148 /* Check if PROP is a display property value whose text should be
5149 treated as intangible. OVERLAY is the overlay from which PROP
5150 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5151 specify the buffer position covered by PROP. */
5152
5153 int
5154 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5155 ptrdiff_t charpos, ptrdiff_t bytepos)
5156 {
5157 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5158 struct text_pos position;
5159
5160 SET_TEXT_POS (position, charpos, bytepos);
5161 return handle_display_spec (NULL, prop, Qnil, overlay,
5162 &position, charpos, frame_window_p);
5163 }
5164
5165
5166 /* Return 1 if PROP is a display sub-property value containing STRING.
5167
5168 Implementation note: this and the following function are really
5169 special cases of handle_display_spec and
5170 handle_single_display_spec, and should ideally use the same code.
5171 Until they do, these two pairs must be consistent and must be
5172 modified in sync. */
5173
5174 static int
5175 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5176 {
5177 if (EQ (string, prop))
5178 return 1;
5179
5180 /* Skip over `when FORM'. */
5181 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5182 {
5183 prop = XCDR (prop);
5184 if (!CONSP (prop))
5185 return 0;
5186 /* Actually, the condition following `when' should be eval'ed,
5187 like handle_single_display_spec does, and we should return
5188 zero if it evaluates to nil. However, this function is
5189 called only when the buffer was already displayed and some
5190 glyph in the glyph matrix was found to come from a display
5191 string. Therefore, the condition was already evaluated, and
5192 the result was non-nil, otherwise the display string wouldn't
5193 have been displayed and we would have never been called for
5194 this property. Thus, we can skip the evaluation and assume
5195 its result is non-nil. */
5196 prop = XCDR (prop);
5197 }
5198
5199 if (CONSP (prop))
5200 /* Skip over `margin LOCATION'. */
5201 if (EQ (XCAR (prop), Qmargin))
5202 {
5203 prop = XCDR (prop);
5204 if (!CONSP (prop))
5205 return 0;
5206
5207 prop = XCDR (prop);
5208 if (!CONSP (prop))
5209 return 0;
5210 }
5211
5212 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5213 }
5214
5215
5216 /* Return 1 if STRING appears in the `display' property PROP. */
5217
5218 static int
5219 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5220 {
5221 if (CONSP (prop)
5222 && !EQ (XCAR (prop), Qwhen)
5223 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5224 {
5225 /* A list of sub-properties. */
5226 while (CONSP (prop))
5227 {
5228 if (single_display_spec_string_p (XCAR (prop), string))
5229 return 1;
5230 prop = XCDR (prop);
5231 }
5232 }
5233 else if (VECTORP (prop))
5234 {
5235 /* A vector of sub-properties. */
5236 ptrdiff_t i;
5237 for (i = 0; i < ASIZE (prop); ++i)
5238 if (single_display_spec_string_p (AREF (prop, i), string))
5239 return 1;
5240 }
5241 else
5242 return single_display_spec_string_p (prop, string);
5243
5244 return 0;
5245 }
5246
5247 /* Look for STRING in overlays and text properties in the current
5248 buffer, between character positions FROM and TO (excluding TO).
5249 BACK_P non-zero means look back (in this case, TO is supposed to be
5250 less than FROM).
5251 Value is the first character position where STRING was found, or
5252 zero if it wasn't found before hitting TO.
5253
5254 This function may only use code that doesn't eval because it is
5255 called asynchronously from note_mouse_highlight. */
5256
5257 static ptrdiff_t
5258 string_buffer_position_lim (Lisp_Object string,
5259 ptrdiff_t from, ptrdiff_t to, int back_p)
5260 {
5261 Lisp_Object limit, prop, pos;
5262 int found = 0;
5263
5264 pos = make_number (max (from, BEGV));
5265
5266 if (!back_p) /* looking forward */
5267 {
5268 limit = make_number (min (to, ZV));
5269 while (!found && !EQ (pos, limit))
5270 {
5271 prop = Fget_char_property (pos, Qdisplay, Qnil);
5272 if (!NILP (prop) && display_prop_string_p (prop, string))
5273 found = 1;
5274 else
5275 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5276 limit);
5277 }
5278 }
5279 else /* looking back */
5280 {
5281 limit = make_number (max (to, BEGV));
5282 while (!found && !EQ (pos, limit))
5283 {
5284 prop = Fget_char_property (pos, Qdisplay, Qnil);
5285 if (!NILP (prop) && display_prop_string_p (prop, string))
5286 found = 1;
5287 else
5288 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5289 limit);
5290 }
5291 }
5292
5293 return found ? XINT (pos) : 0;
5294 }
5295
5296 /* Determine which buffer position in current buffer STRING comes from.
5297 AROUND_CHARPOS is an approximate position where it could come from.
5298 Value is the buffer position or 0 if it couldn't be determined.
5299
5300 This function is necessary because we don't record buffer positions
5301 in glyphs generated from strings (to keep struct glyph small).
5302 This function may only use code that doesn't eval because it is
5303 called asynchronously from note_mouse_highlight. */
5304
5305 static ptrdiff_t
5306 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5307 {
5308 const int MAX_DISTANCE = 1000;
5309 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5310 around_charpos + MAX_DISTANCE,
5311 0);
5312
5313 if (!found)
5314 found = string_buffer_position_lim (string, around_charpos,
5315 around_charpos - MAX_DISTANCE, 1);
5316 return found;
5317 }
5318
5319
5320 \f
5321 /***********************************************************************
5322 `composition' property
5323 ***********************************************************************/
5324
5325 /* Set up iterator IT from `composition' property at its current
5326 position. Called from handle_stop. */
5327
5328 static enum prop_handled
5329 handle_composition_prop (struct it *it)
5330 {
5331 Lisp_Object prop, string;
5332 ptrdiff_t pos, pos_byte, start, end;
5333
5334 if (STRINGP (it->string))
5335 {
5336 unsigned char *s;
5337
5338 pos = IT_STRING_CHARPOS (*it);
5339 pos_byte = IT_STRING_BYTEPOS (*it);
5340 string = it->string;
5341 s = SDATA (string) + pos_byte;
5342 it->c = STRING_CHAR (s);
5343 }
5344 else
5345 {
5346 pos = IT_CHARPOS (*it);
5347 pos_byte = IT_BYTEPOS (*it);
5348 string = Qnil;
5349 it->c = FETCH_CHAR (pos_byte);
5350 }
5351
5352 /* If there's a valid composition and point is not inside of the
5353 composition (in the case that the composition is from the current
5354 buffer), draw a glyph composed from the composition components. */
5355 if (find_composition (pos, -1, &start, &end, &prop, string)
5356 && composition_valid_p (start, end, prop)
5357 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5358 {
5359 if (start < pos)
5360 /* As we can't handle this situation (perhaps font-lock added
5361 a new composition), we just return here hoping that next
5362 redisplay will detect this composition much earlier. */
5363 return HANDLED_NORMALLY;
5364 if (start != pos)
5365 {
5366 if (STRINGP (it->string))
5367 pos_byte = string_char_to_byte (it->string, start);
5368 else
5369 pos_byte = CHAR_TO_BYTE (start);
5370 }
5371 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5372 prop, string);
5373
5374 if (it->cmp_it.id >= 0)
5375 {
5376 it->cmp_it.ch = -1;
5377 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5378 it->cmp_it.nglyphs = -1;
5379 }
5380 }
5381
5382 return HANDLED_NORMALLY;
5383 }
5384
5385
5386 \f
5387 /***********************************************************************
5388 Overlay strings
5389 ***********************************************************************/
5390
5391 /* The following structure is used to record overlay strings for
5392 later sorting in load_overlay_strings. */
5393
5394 struct overlay_entry
5395 {
5396 Lisp_Object overlay;
5397 Lisp_Object string;
5398 EMACS_INT priority;
5399 int after_string_p;
5400 };
5401
5402
5403 /* Set up iterator IT from overlay strings at its current position.
5404 Called from handle_stop. */
5405
5406 static enum prop_handled
5407 handle_overlay_change (struct it *it)
5408 {
5409 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5410 return HANDLED_RECOMPUTE_PROPS;
5411 else
5412 return HANDLED_NORMALLY;
5413 }
5414
5415
5416 /* Set up the next overlay string for delivery by IT, if there is an
5417 overlay string to deliver. Called by set_iterator_to_next when the
5418 end of the current overlay string is reached. If there are more
5419 overlay strings to display, IT->string and
5420 IT->current.overlay_string_index are set appropriately here.
5421 Otherwise IT->string is set to nil. */
5422
5423 static void
5424 next_overlay_string (struct it *it)
5425 {
5426 ++it->current.overlay_string_index;
5427 if (it->current.overlay_string_index == it->n_overlay_strings)
5428 {
5429 /* No more overlay strings. Restore IT's settings to what
5430 they were before overlay strings were processed, and
5431 continue to deliver from current_buffer. */
5432
5433 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5434 pop_it (it);
5435 eassert (it->sp > 0
5436 || (NILP (it->string)
5437 && it->method == GET_FROM_BUFFER
5438 && it->stop_charpos >= BEGV
5439 && it->stop_charpos <= it->end_charpos));
5440 it->current.overlay_string_index = -1;
5441 it->n_overlay_strings = 0;
5442 it->overlay_strings_charpos = -1;
5443 /* If there's an empty display string on the stack, pop the
5444 stack, to resync the bidi iterator with IT's position. Such
5445 empty strings are pushed onto the stack in
5446 get_overlay_strings_1. */
5447 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5448 pop_it (it);
5449
5450 /* If we're at the end of the buffer, record that we have
5451 processed the overlay strings there already, so that
5452 next_element_from_buffer doesn't try it again. */
5453 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5454 it->overlay_strings_at_end_processed_p = 1;
5455 }
5456 else
5457 {
5458 /* There are more overlay strings to process. If
5459 IT->current.overlay_string_index has advanced to a position
5460 where we must load IT->overlay_strings with more strings, do
5461 it. We must load at the IT->overlay_strings_charpos where
5462 IT->n_overlay_strings was originally computed; when invisible
5463 text is present, this might not be IT_CHARPOS (Bug#7016). */
5464 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5465
5466 if (it->current.overlay_string_index && i == 0)
5467 load_overlay_strings (it, it->overlay_strings_charpos);
5468
5469 /* Initialize IT to deliver display elements from the overlay
5470 string. */
5471 it->string = it->overlay_strings[i];
5472 it->multibyte_p = STRING_MULTIBYTE (it->string);
5473 SET_TEXT_POS (it->current.string_pos, 0, 0);
5474 it->method = GET_FROM_STRING;
5475 it->stop_charpos = 0;
5476 it->end_charpos = SCHARS (it->string);
5477 if (it->cmp_it.stop_pos >= 0)
5478 it->cmp_it.stop_pos = 0;
5479 it->prev_stop = 0;
5480 it->base_level_stop = 0;
5481
5482 /* Set up the bidi iterator for this overlay string. */
5483 if (it->bidi_p)
5484 {
5485 it->bidi_it.string.lstring = it->string;
5486 it->bidi_it.string.s = NULL;
5487 it->bidi_it.string.schars = SCHARS (it->string);
5488 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5489 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5490 it->bidi_it.string.unibyte = !it->multibyte_p;
5491 it->bidi_it.w = it->w;
5492 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5493 }
5494 }
5495
5496 CHECK_IT (it);
5497 }
5498
5499
5500 /* Compare two overlay_entry structures E1 and E2. Used as a
5501 comparison function for qsort in load_overlay_strings. Overlay
5502 strings for the same position are sorted so that
5503
5504 1. All after-strings come in front of before-strings, except
5505 when they come from the same overlay.
5506
5507 2. Within after-strings, strings are sorted so that overlay strings
5508 from overlays with higher priorities come first.
5509
5510 2. Within before-strings, strings are sorted so that overlay
5511 strings from overlays with higher priorities come last.
5512
5513 Value is analogous to strcmp. */
5514
5515
5516 static int
5517 compare_overlay_entries (const void *e1, const void *e2)
5518 {
5519 struct overlay_entry const *entry1 = e1;
5520 struct overlay_entry const *entry2 = e2;
5521 int result;
5522
5523 if (entry1->after_string_p != entry2->after_string_p)
5524 {
5525 /* Let after-strings appear in front of before-strings if
5526 they come from different overlays. */
5527 if (EQ (entry1->overlay, entry2->overlay))
5528 result = entry1->after_string_p ? 1 : -1;
5529 else
5530 result = entry1->after_string_p ? -1 : 1;
5531 }
5532 else if (entry1->priority != entry2->priority)
5533 {
5534 if (entry1->after_string_p)
5535 /* After-strings sorted in order of decreasing priority. */
5536 result = entry2->priority < entry1->priority ? -1 : 1;
5537 else
5538 /* Before-strings sorted in order of increasing priority. */
5539 result = entry1->priority < entry2->priority ? -1 : 1;
5540 }
5541 else
5542 result = 0;
5543
5544 return result;
5545 }
5546
5547
5548 /* Load the vector IT->overlay_strings with overlay strings from IT's
5549 current buffer position, or from CHARPOS if that is > 0. Set
5550 IT->n_overlays to the total number of overlay strings found.
5551
5552 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5553 a time. On entry into load_overlay_strings,
5554 IT->current.overlay_string_index gives the number of overlay
5555 strings that have already been loaded by previous calls to this
5556 function.
5557
5558 IT->add_overlay_start contains an additional overlay start
5559 position to consider for taking overlay strings from, if non-zero.
5560 This position comes into play when the overlay has an `invisible'
5561 property, and both before and after-strings. When we've skipped to
5562 the end of the overlay, because of its `invisible' property, we
5563 nevertheless want its before-string to appear.
5564 IT->add_overlay_start will contain the overlay start position
5565 in this case.
5566
5567 Overlay strings are sorted so that after-string strings come in
5568 front of before-string strings. Within before and after-strings,
5569 strings are sorted by overlay priority. See also function
5570 compare_overlay_entries. */
5571
5572 static void
5573 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5574 {
5575 Lisp_Object overlay, window, str, invisible;
5576 struct Lisp_Overlay *ov;
5577 ptrdiff_t start, end;
5578 ptrdiff_t size = 20;
5579 ptrdiff_t n = 0, i, j;
5580 int invis_p;
5581 struct overlay_entry *entries = alloca (size * sizeof *entries);
5582 USE_SAFE_ALLOCA;
5583
5584 if (charpos <= 0)
5585 charpos = IT_CHARPOS (*it);
5586
5587 /* Append the overlay string STRING of overlay OVERLAY to vector
5588 `entries' which has size `size' and currently contains `n'
5589 elements. AFTER_P non-zero means STRING is an after-string of
5590 OVERLAY. */
5591 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5592 do \
5593 { \
5594 Lisp_Object priority; \
5595 \
5596 if (n == size) \
5597 { \
5598 struct overlay_entry *old = entries; \
5599 SAFE_NALLOCA (entries, 2, size); \
5600 memcpy (entries, old, size * sizeof *entries); \
5601 size *= 2; \
5602 } \
5603 \
5604 entries[n].string = (STRING); \
5605 entries[n].overlay = (OVERLAY); \
5606 priority = Foverlay_get ((OVERLAY), Qpriority); \
5607 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5608 entries[n].after_string_p = (AFTER_P); \
5609 ++n; \
5610 } \
5611 while (0)
5612
5613 /* Process overlay before the overlay center. */
5614 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5615 {
5616 XSETMISC (overlay, ov);
5617 eassert (OVERLAYP (overlay));
5618 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5619 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5620
5621 if (end < charpos)
5622 break;
5623
5624 /* Skip this overlay if it doesn't start or end at IT's current
5625 position. */
5626 if (end != charpos && start != charpos)
5627 continue;
5628
5629 /* Skip this overlay if it doesn't apply to IT->w. */
5630 window = Foverlay_get (overlay, Qwindow);
5631 if (WINDOWP (window) && XWINDOW (window) != it->w)
5632 continue;
5633
5634 /* If the text ``under'' the overlay is invisible, both before-
5635 and after-strings from this overlay are visible; start and
5636 end position are indistinguishable. */
5637 invisible = Foverlay_get (overlay, Qinvisible);
5638 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5639
5640 /* If overlay has a non-empty before-string, record it. */
5641 if ((start == charpos || (end == charpos && invis_p))
5642 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5643 && SCHARS (str))
5644 RECORD_OVERLAY_STRING (overlay, str, 0);
5645
5646 /* If overlay has a non-empty after-string, record it. */
5647 if ((end == charpos || (start == charpos && invis_p))
5648 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5649 && SCHARS (str))
5650 RECORD_OVERLAY_STRING (overlay, str, 1);
5651 }
5652
5653 /* Process overlays after the overlay center. */
5654 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5655 {
5656 XSETMISC (overlay, ov);
5657 eassert (OVERLAYP (overlay));
5658 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5659 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5660
5661 if (start > charpos)
5662 break;
5663
5664 /* Skip this overlay if it doesn't start or end at IT's current
5665 position. */
5666 if (end != charpos && start != charpos)
5667 continue;
5668
5669 /* Skip this overlay if it doesn't apply to IT->w. */
5670 window = Foverlay_get (overlay, Qwindow);
5671 if (WINDOWP (window) && XWINDOW (window) != it->w)
5672 continue;
5673
5674 /* If the text ``under'' the overlay is invisible, it has a zero
5675 dimension, and both before- and after-strings apply. */
5676 invisible = Foverlay_get (overlay, Qinvisible);
5677 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5678
5679 /* If overlay has a non-empty before-string, record it. */
5680 if ((start == charpos || (end == charpos && invis_p))
5681 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5682 && SCHARS (str))
5683 RECORD_OVERLAY_STRING (overlay, str, 0);
5684
5685 /* If overlay has a non-empty after-string, record it. */
5686 if ((end == charpos || (start == charpos && invis_p))
5687 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5688 && SCHARS (str))
5689 RECORD_OVERLAY_STRING (overlay, str, 1);
5690 }
5691
5692 #undef RECORD_OVERLAY_STRING
5693
5694 /* Sort entries. */
5695 if (n > 1)
5696 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5697
5698 /* Record number of overlay strings, and where we computed it. */
5699 it->n_overlay_strings = n;
5700 it->overlay_strings_charpos = charpos;
5701
5702 /* IT->current.overlay_string_index is the number of overlay strings
5703 that have already been consumed by IT. Copy some of the
5704 remaining overlay strings to IT->overlay_strings. */
5705 i = 0;
5706 j = it->current.overlay_string_index;
5707 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5708 {
5709 it->overlay_strings[i] = entries[j].string;
5710 it->string_overlays[i++] = entries[j++].overlay;
5711 }
5712
5713 CHECK_IT (it);
5714 SAFE_FREE ();
5715 }
5716
5717
5718 /* Get the first chunk of overlay strings at IT's current buffer
5719 position, or at CHARPOS if that is > 0. Value is non-zero if at
5720 least one overlay string was found. */
5721
5722 static int
5723 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5724 {
5725 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5726 process. This fills IT->overlay_strings with strings, and sets
5727 IT->n_overlay_strings to the total number of strings to process.
5728 IT->pos.overlay_string_index has to be set temporarily to zero
5729 because load_overlay_strings needs this; it must be set to -1
5730 when no overlay strings are found because a zero value would
5731 indicate a position in the first overlay string. */
5732 it->current.overlay_string_index = 0;
5733 load_overlay_strings (it, charpos);
5734
5735 /* If we found overlay strings, set up IT to deliver display
5736 elements from the first one. Otherwise set up IT to deliver
5737 from current_buffer. */
5738 if (it->n_overlay_strings)
5739 {
5740 /* Make sure we know settings in current_buffer, so that we can
5741 restore meaningful values when we're done with the overlay
5742 strings. */
5743 if (compute_stop_p)
5744 compute_stop_pos (it);
5745 eassert (it->face_id >= 0);
5746
5747 /* Save IT's settings. They are restored after all overlay
5748 strings have been processed. */
5749 eassert (!compute_stop_p || it->sp == 0);
5750
5751 /* When called from handle_stop, there might be an empty display
5752 string loaded. In that case, don't bother saving it. But
5753 don't use this optimization with the bidi iterator, since we
5754 need the corresponding pop_it call to resync the bidi
5755 iterator's position with IT's position, after we are done
5756 with the overlay strings. (The corresponding call to pop_it
5757 in case of an empty display string is in
5758 next_overlay_string.) */
5759 if (!(!it->bidi_p
5760 && STRINGP (it->string) && !SCHARS (it->string)))
5761 push_it (it, NULL);
5762
5763 /* Set up IT to deliver display elements from the first overlay
5764 string. */
5765 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5766 it->string = it->overlay_strings[0];
5767 it->from_overlay = Qnil;
5768 it->stop_charpos = 0;
5769 eassert (STRINGP (it->string));
5770 it->end_charpos = SCHARS (it->string);
5771 it->prev_stop = 0;
5772 it->base_level_stop = 0;
5773 it->multibyte_p = STRING_MULTIBYTE (it->string);
5774 it->method = GET_FROM_STRING;
5775 it->from_disp_prop_p = 0;
5776
5777 /* Force paragraph direction to be that of the parent
5778 buffer. */
5779 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5780 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5781 else
5782 it->paragraph_embedding = L2R;
5783
5784 /* Set up the bidi iterator for this overlay string. */
5785 if (it->bidi_p)
5786 {
5787 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5788
5789 it->bidi_it.string.lstring = it->string;
5790 it->bidi_it.string.s = NULL;
5791 it->bidi_it.string.schars = SCHARS (it->string);
5792 it->bidi_it.string.bufpos = pos;
5793 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5794 it->bidi_it.string.unibyte = !it->multibyte_p;
5795 it->bidi_it.w = it->w;
5796 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5797 }
5798 return 1;
5799 }
5800
5801 it->current.overlay_string_index = -1;
5802 return 0;
5803 }
5804
5805 static int
5806 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5807 {
5808 it->string = Qnil;
5809 it->method = GET_FROM_BUFFER;
5810
5811 (void) get_overlay_strings_1 (it, charpos, 1);
5812
5813 CHECK_IT (it);
5814
5815 /* Value is non-zero if we found at least one overlay string. */
5816 return STRINGP (it->string);
5817 }
5818
5819
5820 \f
5821 /***********************************************************************
5822 Saving and restoring state
5823 ***********************************************************************/
5824
5825 /* Save current settings of IT on IT->stack. Called, for example,
5826 before setting up IT for an overlay string, to be able to restore
5827 IT's settings to what they were after the overlay string has been
5828 processed. If POSITION is non-NULL, it is the position to save on
5829 the stack instead of IT->position. */
5830
5831 static void
5832 push_it (struct it *it, struct text_pos *position)
5833 {
5834 struct iterator_stack_entry *p;
5835
5836 eassert (it->sp < IT_STACK_SIZE);
5837 p = it->stack + it->sp;
5838
5839 p->stop_charpos = it->stop_charpos;
5840 p->prev_stop = it->prev_stop;
5841 p->base_level_stop = it->base_level_stop;
5842 p->cmp_it = it->cmp_it;
5843 eassert (it->face_id >= 0);
5844 p->face_id = it->face_id;
5845 p->string = it->string;
5846 p->method = it->method;
5847 p->from_overlay = it->from_overlay;
5848 switch (p->method)
5849 {
5850 case GET_FROM_IMAGE:
5851 p->u.image.object = it->object;
5852 p->u.image.image_id = it->image_id;
5853 p->u.image.slice = it->slice;
5854 break;
5855 case GET_FROM_STRETCH:
5856 p->u.stretch.object = it->object;
5857 break;
5858 }
5859 p->position = position ? *position : it->position;
5860 p->current = it->current;
5861 p->end_charpos = it->end_charpos;
5862 p->string_nchars = it->string_nchars;
5863 p->area = it->area;
5864 p->multibyte_p = it->multibyte_p;
5865 p->avoid_cursor_p = it->avoid_cursor_p;
5866 p->space_width = it->space_width;
5867 p->font_height = it->font_height;
5868 p->voffset = it->voffset;
5869 p->string_from_display_prop_p = it->string_from_display_prop_p;
5870 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5871 p->display_ellipsis_p = 0;
5872 p->line_wrap = it->line_wrap;
5873 p->bidi_p = it->bidi_p;
5874 p->paragraph_embedding = it->paragraph_embedding;
5875 p->from_disp_prop_p = it->from_disp_prop_p;
5876 ++it->sp;
5877
5878 /* Save the state of the bidi iterator as well. */
5879 if (it->bidi_p)
5880 bidi_push_it (&it->bidi_it);
5881 }
5882
5883 static void
5884 iterate_out_of_display_property (struct it *it)
5885 {
5886 int buffer_p = !STRINGP (it->string);
5887 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5888 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5889
5890 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5891
5892 /* Maybe initialize paragraph direction. If we are at the beginning
5893 of a new paragraph, next_element_from_buffer may not have a
5894 chance to do that. */
5895 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5896 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5897 /* prev_stop can be zero, so check against BEGV as well. */
5898 while (it->bidi_it.charpos >= bob
5899 && it->prev_stop <= it->bidi_it.charpos
5900 && it->bidi_it.charpos < CHARPOS (it->position)
5901 && it->bidi_it.charpos < eob)
5902 bidi_move_to_visually_next (&it->bidi_it);
5903 /* Record the stop_pos we just crossed, for when we cross it
5904 back, maybe. */
5905 if (it->bidi_it.charpos > CHARPOS (it->position))
5906 it->prev_stop = CHARPOS (it->position);
5907 /* If we ended up not where pop_it put us, resync IT's
5908 positional members with the bidi iterator. */
5909 if (it->bidi_it.charpos != CHARPOS (it->position))
5910 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5911 if (buffer_p)
5912 it->current.pos = it->position;
5913 else
5914 it->current.string_pos = it->position;
5915 }
5916
5917 /* Restore IT's settings from IT->stack. Called, for example, when no
5918 more overlay strings must be processed, and we return to delivering
5919 display elements from a buffer, or when the end of a string from a
5920 `display' property is reached and we return to delivering display
5921 elements from an overlay string, or from a buffer. */
5922
5923 static void
5924 pop_it (struct it *it)
5925 {
5926 struct iterator_stack_entry *p;
5927 int from_display_prop = it->from_disp_prop_p;
5928
5929 eassert (it->sp > 0);
5930 --it->sp;
5931 p = it->stack + it->sp;
5932 it->stop_charpos = p->stop_charpos;
5933 it->prev_stop = p->prev_stop;
5934 it->base_level_stop = p->base_level_stop;
5935 it->cmp_it = p->cmp_it;
5936 it->face_id = p->face_id;
5937 it->current = p->current;
5938 it->position = p->position;
5939 it->string = p->string;
5940 it->from_overlay = p->from_overlay;
5941 if (NILP (it->string))
5942 SET_TEXT_POS (it->current.string_pos, -1, -1);
5943 it->method = p->method;
5944 switch (it->method)
5945 {
5946 case GET_FROM_IMAGE:
5947 it->image_id = p->u.image.image_id;
5948 it->object = p->u.image.object;
5949 it->slice = p->u.image.slice;
5950 break;
5951 case GET_FROM_STRETCH:
5952 it->object = p->u.stretch.object;
5953 break;
5954 case GET_FROM_BUFFER:
5955 it->object = it->w->contents;
5956 break;
5957 case GET_FROM_STRING:
5958 it->object = it->string;
5959 break;
5960 case GET_FROM_DISPLAY_VECTOR:
5961 if (it->s)
5962 it->method = GET_FROM_C_STRING;
5963 else if (STRINGP (it->string))
5964 it->method = GET_FROM_STRING;
5965 else
5966 {
5967 it->method = GET_FROM_BUFFER;
5968 it->object = it->w->contents;
5969 }
5970 }
5971 it->end_charpos = p->end_charpos;
5972 it->string_nchars = p->string_nchars;
5973 it->area = p->area;
5974 it->multibyte_p = p->multibyte_p;
5975 it->avoid_cursor_p = p->avoid_cursor_p;
5976 it->space_width = p->space_width;
5977 it->font_height = p->font_height;
5978 it->voffset = p->voffset;
5979 it->string_from_display_prop_p = p->string_from_display_prop_p;
5980 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5981 it->line_wrap = p->line_wrap;
5982 it->bidi_p = p->bidi_p;
5983 it->paragraph_embedding = p->paragraph_embedding;
5984 it->from_disp_prop_p = p->from_disp_prop_p;
5985 if (it->bidi_p)
5986 {
5987 bidi_pop_it (&it->bidi_it);
5988 /* Bidi-iterate until we get out of the portion of text, if any,
5989 covered by a `display' text property or by an overlay with
5990 `display' property. (We cannot just jump there, because the
5991 internal coherency of the bidi iterator state can not be
5992 preserved across such jumps.) We also must determine the
5993 paragraph base direction if the overlay we just processed is
5994 at the beginning of a new paragraph. */
5995 if (from_display_prop
5996 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5997 iterate_out_of_display_property (it);
5998
5999 eassert ((BUFFERP (it->object)
6000 && IT_CHARPOS (*it) == it->bidi_it.charpos
6001 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
6002 || (STRINGP (it->object)
6003 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
6004 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
6005 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
6006 }
6007 }
6008
6009
6010 \f
6011 /***********************************************************************
6012 Moving over lines
6013 ***********************************************************************/
6014
6015 /* Set IT's current position to the previous line start. */
6016
6017 static void
6018 back_to_previous_line_start (struct it *it)
6019 {
6020 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6021
6022 DEC_BOTH (cp, bp);
6023 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6024 }
6025
6026
6027 /* Move IT to the next line start.
6028
6029 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
6030 we skipped over part of the text (as opposed to moving the iterator
6031 continuously over the text). Otherwise, don't change the value
6032 of *SKIPPED_P.
6033
6034 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6035 iterator on the newline, if it was found.
6036
6037 Newlines may come from buffer text, overlay strings, or strings
6038 displayed via the `display' property. That's the reason we can't
6039 simply use find_newline_no_quit.
6040
6041 Note that this function may not skip over invisible text that is so
6042 because of text properties and immediately follows a newline. If
6043 it would, function reseat_at_next_visible_line_start, when called
6044 from set_iterator_to_next, would effectively make invisible
6045 characters following a newline part of the wrong glyph row, which
6046 leads to wrong cursor motion. */
6047
6048 static int
6049 forward_to_next_line_start (struct it *it, int *skipped_p,
6050 struct bidi_it *bidi_it_prev)
6051 {
6052 ptrdiff_t old_selective;
6053 int newline_found_p, n;
6054 const int MAX_NEWLINE_DISTANCE = 500;
6055
6056 /* If already on a newline, just consume it to avoid unintended
6057 skipping over invisible text below. */
6058 if (it->what == IT_CHARACTER
6059 && it->c == '\n'
6060 && CHARPOS (it->position) == IT_CHARPOS (*it))
6061 {
6062 if (it->bidi_p && bidi_it_prev)
6063 *bidi_it_prev = it->bidi_it;
6064 set_iterator_to_next (it, 0);
6065 it->c = 0;
6066 return 1;
6067 }
6068
6069 /* Don't handle selective display in the following. It's (a)
6070 unnecessary because it's done by the caller, and (b) leads to an
6071 infinite recursion because next_element_from_ellipsis indirectly
6072 calls this function. */
6073 old_selective = it->selective;
6074 it->selective = 0;
6075
6076 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6077 from buffer text. */
6078 for (n = newline_found_p = 0;
6079 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6080 n += STRINGP (it->string) ? 0 : 1)
6081 {
6082 if (!get_next_display_element (it))
6083 return 0;
6084 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6085 if (newline_found_p && it->bidi_p && bidi_it_prev)
6086 *bidi_it_prev = it->bidi_it;
6087 set_iterator_to_next (it, 0);
6088 }
6089
6090 /* If we didn't find a newline near enough, see if we can use a
6091 short-cut. */
6092 if (!newline_found_p)
6093 {
6094 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6095 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6096 1, &bytepos);
6097 Lisp_Object pos;
6098
6099 eassert (!STRINGP (it->string));
6100
6101 /* If there isn't any `display' property in sight, and no
6102 overlays, we can just use the position of the newline in
6103 buffer text. */
6104 if (it->stop_charpos >= limit
6105 || ((pos = Fnext_single_property_change (make_number (start),
6106 Qdisplay, Qnil,
6107 make_number (limit)),
6108 NILP (pos))
6109 && next_overlay_change (start) == ZV))
6110 {
6111 if (!it->bidi_p)
6112 {
6113 IT_CHARPOS (*it) = limit;
6114 IT_BYTEPOS (*it) = bytepos;
6115 }
6116 else
6117 {
6118 struct bidi_it bprev;
6119
6120 /* Help bidi.c avoid expensive searches for display
6121 properties and overlays, by telling it that there are
6122 none up to `limit'. */
6123 if (it->bidi_it.disp_pos < limit)
6124 {
6125 it->bidi_it.disp_pos = limit;
6126 it->bidi_it.disp_prop = 0;
6127 }
6128 do {
6129 bprev = it->bidi_it;
6130 bidi_move_to_visually_next (&it->bidi_it);
6131 } while (it->bidi_it.charpos != limit);
6132 IT_CHARPOS (*it) = limit;
6133 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6134 if (bidi_it_prev)
6135 *bidi_it_prev = bprev;
6136 }
6137 *skipped_p = newline_found_p = 1;
6138 }
6139 else
6140 {
6141 while (get_next_display_element (it)
6142 && !newline_found_p)
6143 {
6144 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6145 if (newline_found_p && it->bidi_p && bidi_it_prev)
6146 *bidi_it_prev = it->bidi_it;
6147 set_iterator_to_next (it, 0);
6148 }
6149 }
6150 }
6151
6152 it->selective = old_selective;
6153 return newline_found_p;
6154 }
6155
6156
6157 /* Set IT's current position to the previous visible line start. Skip
6158 invisible text that is so either due to text properties or due to
6159 selective display. Caution: this does not change IT->current_x and
6160 IT->hpos. */
6161
6162 static void
6163 back_to_previous_visible_line_start (struct it *it)
6164 {
6165 while (IT_CHARPOS (*it) > BEGV)
6166 {
6167 back_to_previous_line_start (it);
6168
6169 if (IT_CHARPOS (*it) <= BEGV)
6170 break;
6171
6172 /* If selective > 0, then lines indented more than its value are
6173 invisible. */
6174 if (it->selective > 0
6175 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6176 it->selective))
6177 continue;
6178
6179 /* Check the newline before point for invisibility. */
6180 {
6181 Lisp_Object prop;
6182 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6183 Qinvisible, it->window);
6184 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6185 continue;
6186 }
6187
6188 if (IT_CHARPOS (*it) <= BEGV)
6189 break;
6190
6191 {
6192 struct it it2;
6193 void *it2data = NULL;
6194 ptrdiff_t pos;
6195 ptrdiff_t beg, end;
6196 Lisp_Object val, overlay;
6197
6198 SAVE_IT (it2, *it, it2data);
6199
6200 /* If newline is part of a composition, continue from start of composition */
6201 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6202 && beg < IT_CHARPOS (*it))
6203 goto replaced;
6204
6205 /* If newline is replaced by a display property, find start of overlay
6206 or interval and continue search from that point. */
6207 pos = --IT_CHARPOS (it2);
6208 --IT_BYTEPOS (it2);
6209 it2.sp = 0;
6210 bidi_unshelve_cache (NULL, 0);
6211 it2.string_from_display_prop_p = 0;
6212 it2.from_disp_prop_p = 0;
6213 if (handle_display_prop (&it2) == HANDLED_RETURN
6214 && !NILP (val = get_char_property_and_overlay
6215 (make_number (pos), Qdisplay, Qnil, &overlay))
6216 && (OVERLAYP (overlay)
6217 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6218 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6219 {
6220 RESTORE_IT (it, it, it2data);
6221 goto replaced;
6222 }
6223
6224 /* Newline is not replaced by anything -- so we are done. */
6225 RESTORE_IT (it, it, it2data);
6226 break;
6227
6228 replaced:
6229 if (beg < BEGV)
6230 beg = BEGV;
6231 IT_CHARPOS (*it) = beg;
6232 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6233 }
6234 }
6235
6236 it->continuation_lines_width = 0;
6237
6238 eassert (IT_CHARPOS (*it) >= BEGV);
6239 eassert (IT_CHARPOS (*it) == BEGV
6240 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6241 CHECK_IT (it);
6242 }
6243
6244
6245 /* Reseat iterator IT at the previous visible line start. Skip
6246 invisible text that is so either due to text properties or due to
6247 selective display. At the end, update IT's overlay information,
6248 face information etc. */
6249
6250 void
6251 reseat_at_previous_visible_line_start (struct it *it)
6252 {
6253 back_to_previous_visible_line_start (it);
6254 reseat (it, it->current.pos, 1);
6255 CHECK_IT (it);
6256 }
6257
6258
6259 /* Reseat iterator IT on the next visible line start in the current
6260 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6261 preceding the line start. Skip over invisible text that is so
6262 because of selective display. Compute faces, overlays etc at the
6263 new position. Note that this function does not skip over text that
6264 is invisible because of text properties. */
6265
6266 static void
6267 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6268 {
6269 int newline_found_p, skipped_p = 0;
6270 struct bidi_it bidi_it_prev;
6271
6272 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6273
6274 /* Skip over lines that are invisible because they are indented
6275 more than the value of IT->selective. */
6276 if (it->selective > 0)
6277 while (IT_CHARPOS (*it) < ZV
6278 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6279 it->selective))
6280 {
6281 eassert (IT_BYTEPOS (*it) == BEGV
6282 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6283 newline_found_p =
6284 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6285 }
6286
6287 /* Position on the newline if that's what's requested. */
6288 if (on_newline_p && newline_found_p)
6289 {
6290 if (STRINGP (it->string))
6291 {
6292 if (IT_STRING_CHARPOS (*it) > 0)
6293 {
6294 if (!it->bidi_p)
6295 {
6296 --IT_STRING_CHARPOS (*it);
6297 --IT_STRING_BYTEPOS (*it);
6298 }
6299 else
6300 {
6301 /* We need to restore the bidi iterator to the state
6302 it had on the newline, and resync the IT's
6303 position with that. */
6304 it->bidi_it = bidi_it_prev;
6305 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6306 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6307 }
6308 }
6309 }
6310 else if (IT_CHARPOS (*it) > BEGV)
6311 {
6312 if (!it->bidi_p)
6313 {
6314 --IT_CHARPOS (*it);
6315 --IT_BYTEPOS (*it);
6316 }
6317 else
6318 {
6319 /* We need to restore the bidi iterator to the state it
6320 had on the newline and resync IT with that. */
6321 it->bidi_it = bidi_it_prev;
6322 IT_CHARPOS (*it) = it->bidi_it.charpos;
6323 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6324 }
6325 reseat (it, it->current.pos, 0);
6326 }
6327 }
6328 else if (skipped_p)
6329 reseat (it, it->current.pos, 0);
6330
6331 CHECK_IT (it);
6332 }
6333
6334
6335 \f
6336 /***********************************************************************
6337 Changing an iterator's position
6338 ***********************************************************************/
6339
6340 /* Change IT's current position to POS in current_buffer. If FORCE_P
6341 is non-zero, always check for text properties at the new position.
6342 Otherwise, text properties are only looked up if POS >=
6343 IT->check_charpos of a property. */
6344
6345 static void
6346 reseat (struct it *it, struct text_pos pos, int force_p)
6347 {
6348 ptrdiff_t original_pos = IT_CHARPOS (*it);
6349
6350 reseat_1 (it, pos, 0);
6351
6352 /* Determine where to check text properties. Avoid doing it
6353 where possible because text property lookup is very expensive. */
6354 if (force_p
6355 || CHARPOS (pos) > it->stop_charpos
6356 || CHARPOS (pos) < original_pos)
6357 {
6358 if (it->bidi_p)
6359 {
6360 /* For bidi iteration, we need to prime prev_stop and
6361 base_level_stop with our best estimations. */
6362 /* Implementation note: Of course, POS is not necessarily a
6363 stop position, so assigning prev_pos to it is a lie; we
6364 should have called compute_stop_backwards. However, if
6365 the current buffer does not include any R2L characters,
6366 that call would be a waste of cycles, because the
6367 iterator will never move back, and thus never cross this
6368 "fake" stop position. So we delay that backward search
6369 until the time we really need it, in next_element_from_buffer. */
6370 if (CHARPOS (pos) != it->prev_stop)
6371 it->prev_stop = CHARPOS (pos);
6372 if (CHARPOS (pos) < it->base_level_stop)
6373 it->base_level_stop = 0; /* meaning it's unknown */
6374 handle_stop (it);
6375 }
6376 else
6377 {
6378 handle_stop (it);
6379 it->prev_stop = it->base_level_stop = 0;
6380 }
6381
6382 }
6383
6384 CHECK_IT (it);
6385 }
6386
6387
6388 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6389 IT->stop_pos to POS, also. */
6390
6391 static void
6392 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6393 {
6394 /* Don't call this function when scanning a C string. */
6395 eassert (it->s == NULL);
6396
6397 /* POS must be a reasonable value. */
6398 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6399
6400 it->current.pos = it->position = pos;
6401 it->end_charpos = ZV;
6402 it->dpvec = NULL;
6403 it->current.dpvec_index = -1;
6404 it->current.overlay_string_index = -1;
6405 IT_STRING_CHARPOS (*it) = -1;
6406 IT_STRING_BYTEPOS (*it) = -1;
6407 it->string = Qnil;
6408 it->method = GET_FROM_BUFFER;
6409 it->object = it->w->contents;
6410 it->area = TEXT_AREA;
6411 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6412 it->sp = 0;
6413 it->string_from_display_prop_p = 0;
6414 it->string_from_prefix_prop_p = 0;
6415
6416 it->from_disp_prop_p = 0;
6417 it->face_before_selective_p = 0;
6418 if (it->bidi_p)
6419 {
6420 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6421 &it->bidi_it);
6422 bidi_unshelve_cache (NULL, 0);
6423 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6424 it->bidi_it.string.s = NULL;
6425 it->bidi_it.string.lstring = Qnil;
6426 it->bidi_it.string.bufpos = 0;
6427 it->bidi_it.string.unibyte = 0;
6428 it->bidi_it.w = it->w;
6429 }
6430
6431 if (set_stop_p)
6432 {
6433 it->stop_charpos = CHARPOS (pos);
6434 it->base_level_stop = CHARPOS (pos);
6435 }
6436 /* This make the information stored in it->cmp_it invalidate. */
6437 it->cmp_it.id = -1;
6438 }
6439
6440
6441 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6442 If S is non-null, it is a C string to iterate over. Otherwise,
6443 STRING gives a Lisp string to iterate over.
6444
6445 If PRECISION > 0, don't return more then PRECISION number of
6446 characters from the string.
6447
6448 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6449 characters have been returned. FIELD_WIDTH < 0 means an infinite
6450 field width.
6451
6452 MULTIBYTE = 0 means disable processing of multibyte characters,
6453 MULTIBYTE > 0 means enable it,
6454 MULTIBYTE < 0 means use IT->multibyte_p.
6455
6456 IT must be initialized via a prior call to init_iterator before
6457 calling this function. */
6458
6459 static void
6460 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6461 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6462 int multibyte)
6463 {
6464 /* No region in strings. */
6465 it->region_beg_charpos = it->region_end_charpos = -1;
6466
6467 /* No text property checks performed by default, but see below. */
6468 it->stop_charpos = -1;
6469
6470 /* Set iterator position and end position. */
6471 memset (&it->current, 0, sizeof it->current);
6472 it->current.overlay_string_index = -1;
6473 it->current.dpvec_index = -1;
6474 eassert (charpos >= 0);
6475
6476 /* If STRING is specified, use its multibyteness, otherwise use the
6477 setting of MULTIBYTE, if specified. */
6478 if (multibyte >= 0)
6479 it->multibyte_p = multibyte > 0;
6480
6481 /* Bidirectional reordering of strings is controlled by the default
6482 value of bidi-display-reordering. Don't try to reorder while
6483 loading loadup.el, as the necessary character property tables are
6484 not yet available. */
6485 it->bidi_p =
6486 NILP (Vpurify_flag)
6487 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6488
6489 if (s == NULL)
6490 {
6491 eassert (STRINGP (string));
6492 it->string = string;
6493 it->s = NULL;
6494 it->end_charpos = it->string_nchars = SCHARS (string);
6495 it->method = GET_FROM_STRING;
6496 it->current.string_pos = string_pos (charpos, string);
6497
6498 if (it->bidi_p)
6499 {
6500 it->bidi_it.string.lstring = string;
6501 it->bidi_it.string.s = NULL;
6502 it->bidi_it.string.schars = it->end_charpos;
6503 it->bidi_it.string.bufpos = 0;
6504 it->bidi_it.string.from_disp_str = 0;
6505 it->bidi_it.string.unibyte = !it->multibyte_p;
6506 it->bidi_it.w = it->w;
6507 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6508 FRAME_WINDOW_P (it->f), &it->bidi_it);
6509 }
6510 }
6511 else
6512 {
6513 it->s = (const unsigned char *) s;
6514 it->string = Qnil;
6515
6516 /* Note that we use IT->current.pos, not it->current.string_pos,
6517 for displaying C strings. */
6518 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6519 if (it->multibyte_p)
6520 {
6521 it->current.pos = c_string_pos (charpos, s, 1);
6522 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6523 }
6524 else
6525 {
6526 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6527 it->end_charpos = it->string_nchars = strlen (s);
6528 }
6529
6530 if (it->bidi_p)
6531 {
6532 it->bidi_it.string.lstring = Qnil;
6533 it->bidi_it.string.s = (const unsigned char *) s;
6534 it->bidi_it.string.schars = it->end_charpos;
6535 it->bidi_it.string.bufpos = 0;
6536 it->bidi_it.string.from_disp_str = 0;
6537 it->bidi_it.string.unibyte = !it->multibyte_p;
6538 it->bidi_it.w = it->w;
6539 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6540 &it->bidi_it);
6541 }
6542 it->method = GET_FROM_C_STRING;
6543 }
6544
6545 /* PRECISION > 0 means don't return more than PRECISION characters
6546 from the string. */
6547 if (precision > 0 && it->end_charpos - charpos > precision)
6548 {
6549 it->end_charpos = it->string_nchars = charpos + precision;
6550 if (it->bidi_p)
6551 it->bidi_it.string.schars = it->end_charpos;
6552 }
6553
6554 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6555 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6556 FIELD_WIDTH < 0 means infinite field width. This is useful for
6557 padding with `-' at the end of a mode line. */
6558 if (field_width < 0)
6559 field_width = INFINITY;
6560 /* Implementation note: We deliberately don't enlarge
6561 it->bidi_it.string.schars here to fit it->end_charpos, because
6562 the bidi iterator cannot produce characters out of thin air. */
6563 if (field_width > it->end_charpos - charpos)
6564 it->end_charpos = charpos + field_width;
6565
6566 /* Use the standard display table for displaying strings. */
6567 if (DISP_TABLE_P (Vstandard_display_table))
6568 it->dp = XCHAR_TABLE (Vstandard_display_table);
6569
6570 it->stop_charpos = charpos;
6571 it->prev_stop = charpos;
6572 it->base_level_stop = 0;
6573 if (it->bidi_p)
6574 {
6575 it->bidi_it.first_elt = 1;
6576 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6577 it->bidi_it.disp_pos = -1;
6578 }
6579 if (s == NULL && it->multibyte_p)
6580 {
6581 ptrdiff_t endpos = SCHARS (it->string);
6582 if (endpos > it->end_charpos)
6583 endpos = it->end_charpos;
6584 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6585 it->string);
6586 }
6587 CHECK_IT (it);
6588 }
6589
6590
6591 \f
6592 /***********************************************************************
6593 Iteration
6594 ***********************************************************************/
6595
6596 /* Map enum it_method value to corresponding next_element_from_* function. */
6597
6598 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6599 {
6600 next_element_from_buffer,
6601 next_element_from_display_vector,
6602 next_element_from_string,
6603 next_element_from_c_string,
6604 next_element_from_image,
6605 next_element_from_stretch
6606 };
6607
6608 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6609
6610
6611 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6612 (possibly with the following characters). */
6613
6614 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6615 ((IT)->cmp_it.id >= 0 \
6616 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6617 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6618 END_CHARPOS, (IT)->w, \
6619 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6620 (IT)->string)))
6621
6622
6623 /* Lookup the char-table Vglyphless_char_display for character C (-1
6624 if we want information for no-font case), and return the display
6625 method symbol. By side-effect, update it->what and
6626 it->glyphless_method. This function is called from
6627 get_next_display_element for each character element, and from
6628 x_produce_glyphs when no suitable font was found. */
6629
6630 Lisp_Object
6631 lookup_glyphless_char_display (int c, struct it *it)
6632 {
6633 Lisp_Object glyphless_method = Qnil;
6634
6635 if (CHAR_TABLE_P (Vglyphless_char_display)
6636 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6637 {
6638 if (c >= 0)
6639 {
6640 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6641 if (CONSP (glyphless_method))
6642 glyphless_method = FRAME_WINDOW_P (it->f)
6643 ? XCAR (glyphless_method)
6644 : XCDR (glyphless_method);
6645 }
6646 else
6647 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6648 }
6649
6650 retry:
6651 if (NILP (glyphless_method))
6652 {
6653 if (c >= 0)
6654 /* The default is to display the character by a proper font. */
6655 return Qnil;
6656 /* The default for the no-font case is to display an empty box. */
6657 glyphless_method = Qempty_box;
6658 }
6659 if (EQ (glyphless_method, Qzero_width))
6660 {
6661 if (c >= 0)
6662 return glyphless_method;
6663 /* This method can't be used for the no-font case. */
6664 glyphless_method = Qempty_box;
6665 }
6666 if (EQ (glyphless_method, Qthin_space))
6667 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6668 else if (EQ (glyphless_method, Qempty_box))
6669 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6670 else if (EQ (glyphless_method, Qhex_code))
6671 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6672 else if (STRINGP (glyphless_method))
6673 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6674 else
6675 {
6676 /* Invalid value. We use the default method. */
6677 glyphless_method = Qnil;
6678 goto retry;
6679 }
6680 it->what = IT_GLYPHLESS;
6681 return glyphless_method;
6682 }
6683
6684 /* Load IT's display element fields with information about the next
6685 display element from the current position of IT. Value is zero if
6686 end of buffer (or C string) is reached. */
6687
6688 static struct frame *last_escape_glyph_frame = NULL;
6689 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6690 static int last_escape_glyph_merged_face_id = 0;
6691
6692 struct frame *last_glyphless_glyph_frame = NULL;
6693 int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6694 int last_glyphless_glyph_merged_face_id = 0;
6695
6696 static int
6697 get_next_display_element (struct it *it)
6698 {
6699 /* Non-zero means that we found a display element. Zero means that
6700 we hit the end of what we iterate over. Performance note: the
6701 function pointer `method' used here turns out to be faster than
6702 using a sequence of if-statements. */
6703 int success_p;
6704
6705 get_next:
6706 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6707
6708 if (it->what == IT_CHARACTER)
6709 {
6710 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6711 and only if (a) the resolved directionality of that character
6712 is R..." */
6713 /* FIXME: Do we need an exception for characters from display
6714 tables? */
6715 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6716 it->c = bidi_mirror_char (it->c);
6717 /* Map via display table or translate control characters.
6718 IT->c, IT->len etc. have been set to the next character by
6719 the function call above. If we have a display table, and it
6720 contains an entry for IT->c, translate it. Don't do this if
6721 IT->c itself comes from a display table, otherwise we could
6722 end up in an infinite recursion. (An alternative could be to
6723 count the recursion depth of this function and signal an
6724 error when a certain maximum depth is reached.) Is it worth
6725 it? */
6726 if (success_p && it->dpvec == NULL)
6727 {
6728 Lisp_Object dv;
6729 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6730 int nonascii_space_p = 0;
6731 int nonascii_hyphen_p = 0;
6732 int c = it->c; /* This is the character to display. */
6733
6734 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6735 {
6736 eassert (SINGLE_BYTE_CHAR_P (c));
6737 if (unibyte_display_via_language_environment)
6738 {
6739 c = DECODE_CHAR (unibyte, c);
6740 if (c < 0)
6741 c = BYTE8_TO_CHAR (it->c);
6742 }
6743 else
6744 c = BYTE8_TO_CHAR (it->c);
6745 }
6746
6747 if (it->dp
6748 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6749 VECTORP (dv)))
6750 {
6751 struct Lisp_Vector *v = XVECTOR (dv);
6752
6753 /* Return the first character from the display table
6754 entry, if not empty. If empty, don't display the
6755 current character. */
6756 if (v->header.size)
6757 {
6758 it->dpvec_char_len = it->len;
6759 it->dpvec = v->contents;
6760 it->dpend = v->contents + v->header.size;
6761 it->current.dpvec_index = 0;
6762 it->dpvec_face_id = -1;
6763 it->saved_face_id = it->face_id;
6764 it->method = GET_FROM_DISPLAY_VECTOR;
6765 it->ellipsis_p = 0;
6766 }
6767 else
6768 {
6769 set_iterator_to_next (it, 0);
6770 }
6771 goto get_next;
6772 }
6773
6774 if (! NILP (lookup_glyphless_char_display (c, it)))
6775 {
6776 if (it->what == IT_GLYPHLESS)
6777 goto done;
6778 /* Don't display this character. */
6779 set_iterator_to_next (it, 0);
6780 goto get_next;
6781 }
6782
6783 /* If `nobreak-char-display' is non-nil, we display
6784 non-ASCII spaces and hyphens specially. */
6785 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6786 {
6787 if (c == 0xA0)
6788 nonascii_space_p = 1;
6789 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6790 nonascii_hyphen_p = 1;
6791 }
6792
6793 /* Translate control characters into `\003' or `^C' form.
6794 Control characters coming from a display table entry are
6795 currently not translated because we use IT->dpvec to hold
6796 the translation. This could easily be changed but I
6797 don't believe that it is worth doing.
6798
6799 The characters handled by `nobreak-char-display' must be
6800 translated too.
6801
6802 Non-printable characters and raw-byte characters are also
6803 translated to octal form. */
6804 if (((c < ' ' || c == 127) /* ASCII control chars */
6805 ? (it->area != TEXT_AREA
6806 /* In mode line, treat \n, \t like other crl chars. */
6807 || (c != '\t'
6808 && it->glyph_row
6809 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6810 || (c != '\n' && c != '\t'))
6811 : (nonascii_space_p
6812 || nonascii_hyphen_p
6813 || CHAR_BYTE8_P (c)
6814 || ! CHAR_PRINTABLE_P (c))))
6815 {
6816 /* C is a control character, non-ASCII space/hyphen,
6817 raw-byte, or a non-printable character which must be
6818 displayed either as '\003' or as `^C' where the '\\'
6819 and '^' can be defined in the display table. Fill
6820 IT->ctl_chars with glyphs for what we have to
6821 display. Then, set IT->dpvec to these glyphs. */
6822 Lisp_Object gc;
6823 int ctl_len;
6824 int face_id;
6825 int lface_id = 0;
6826 int escape_glyph;
6827
6828 /* Handle control characters with ^. */
6829
6830 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6831 {
6832 int g;
6833
6834 g = '^'; /* default glyph for Control */
6835 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6836 if (it->dp
6837 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6838 {
6839 g = GLYPH_CODE_CHAR (gc);
6840 lface_id = GLYPH_CODE_FACE (gc);
6841 }
6842 if (lface_id)
6843 {
6844 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6845 }
6846 else if (it->f == last_escape_glyph_frame
6847 && it->face_id == last_escape_glyph_face_id)
6848 {
6849 face_id = last_escape_glyph_merged_face_id;
6850 }
6851 else
6852 {
6853 /* Merge the escape-glyph face into the current face. */
6854 face_id = merge_faces (it->f, Qescape_glyph, 0,
6855 it->face_id);
6856 last_escape_glyph_frame = it->f;
6857 last_escape_glyph_face_id = it->face_id;
6858 last_escape_glyph_merged_face_id = face_id;
6859 }
6860
6861 XSETINT (it->ctl_chars[0], g);
6862 XSETINT (it->ctl_chars[1], c ^ 0100);
6863 ctl_len = 2;
6864 goto display_control;
6865 }
6866
6867 /* Handle non-ascii space in the mode where it only gets
6868 highlighting. */
6869
6870 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6871 {
6872 /* Merge `nobreak-space' into the current face. */
6873 face_id = merge_faces (it->f, Qnobreak_space, 0,
6874 it->face_id);
6875 XSETINT (it->ctl_chars[0], ' ');
6876 ctl_len = 1;
6877 goto display_control;
6878 }
6879
6880 /* Handle sequences that start with the "escape glyph". */
6881
6882 /* the default escape glyph is \. */
6883 escape_glyph = '\\';
6884
6885 if (it->dp
6886 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6887 {
6888 escape_glyph = GLYPH_CODE_CHAR (gc);
6889 lface_id = GLYPH_CODE_FACE (gc);
6890 }
6891 if (lface_id)
6892 {
6893 /* The display table specified a face.
6894 Merge it into face_id and also into escape_glyph. */
6895 face_id = merge_faces (it->f, Qt, lface_id,
6896 it->face_id);
6897 }
6898 else if (it->f == last_escape_glyph_frame
6899 && it->face_id == last_escape_glyph_face_id)
6900 {
6901 face_id = last_escape_glyph_merged_face_id;
6902 }
6903 else
6904 {
6905 /* Merge the escape-glyph face into the current face. */
6906 face_id = merge_faces (it->f, Qescape_glyph, 0,
6907 it->face_id);
6908 last_escape_glyph_frame = it->f;
6909 last_escape_glyph_face_id = it->face_id;
6910 last_escape_glyph_merged_face_id = face_id;
6911 }
6912
6913 /* Draw non-ASCII hyphen with just highlighting: */
6914
6915 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6916 {
6917 XSETINT (it->ctl_chars[0], '-');
6918 ctl_len = 1;
6919 goto display_control;
6920 }
6921
6922 /* Draw non-ASCII space/hyphen with escape glyph: */
6923
6924 if (nonascii_space_p || nonascii_hyphen_p)
6925 {
6926 XSETINT (it->ctl_chars[0], escape_glyph);
6927 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6928 ctl_len = 2;
6929 goto display_control;
6930 }
6931
6932 {
6933 char str[10];
6934 int len, i;
6935
6936 if (CHAR_BYTE8_P (c))
6937 /* Display \200 instead of \17777600. */
6938 c = CHAR_TO_BYTE8 (c);
6939 len = sprintf (str, "%03o", c);
6940
6941 XSETINT (it->ctl_chars[0], escape_glyph);
6942 for (i = 0; i < len; i++)
6943 XSETINT (it->ctl_chars[i + 1], str[i]);
6944 ctl_len = len + 1;
6945 }
6946
6947 display_control:
6948 /* Set up IT->dpvec and return first character from it. */
6949 it->dpvec_char_len = it->len;
6950 it->dpvec = it->ctl_chars;
6951 it->dpend = it->dpvec + ctl_len;
6952 it->current.dpvec_index = 0;
6953 it->dpvec_face_id = face_id;
6954 it->saved_face_id = it->face_id;
6955 it->method = GET_FROM_DISPLAY_VECTOR;
6956 it->ellipsis_p = 0;
6957 goto get_next;
6958 }
6959 it->char_to_display = c;
6960 }
6961 else if (success_p)
6962 {
6963 it->char_to_display = it->c;
6964 }
6965 }
6966
6967 /* Adjust face id for a multibyte character. There are no multibyte
6968 character in unibyte text. */
6969 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6970 && it->multibyte_p
6971 && success_p
6972 && FRAME_WINDOW_P (it->f))
6973 {
6974 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6975
6976 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6977 {
6978 /* Automatic composition with glyph-string. */
6979 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6980
6981 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6982 }
6983 else
6984 {
6985 ptrdiff_t pos = (it->s ? -1
6986 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6987 : IT_CHARPOS (*it));
6988 int c;
6989
6990 if (it->what == IT_CHARACTER)
6991 c = it->char_to_display;
6992 else
6993 {
6994 struct composition *cmp = composition_table[it->cmp_it.id];
6995 int i;
6996
6997 c = ' ';
6998 for (i = 0; i < cmp->glyph_len; i++)
6999 /* TAB in a composition means display glyphs with
7000 padding space on the left or right. */
7001 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
7002 break;
7003 }
7004 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7005 }
7006 }
7007
7008 done:
7009 /* Is this character the last one of a run of characters with
7010 box? If yes, set IT->end_of_box_run_p to 1. */
7011 if (it->face_box_p
7012 && it->s == NULL)
7013 {
7014 if (it->method == GET_FROM_STRING && it->sp)
7015 {
7016 int face_id = underlying_face_id (it);
7017 struct face *face = FACE_FROM_ID (it->f, face_id);
7018
7019 if (face)
7020 {
7021 if (face->box == FACE_NO_BOX)
7022 {
7023 /* If the box comes from face properties in a
7024 display string, check faces in that string. */
7025 int string_face_id = face_after_it_pos (it);
7026 it->end_of_box_run_p
7027 = (FACE_FROM_ID (it->f, string_face_id)->box
7028 == FACE_NO_BOX);
7029 }
7030 /* Otherwise, the box comes from the underlying face.
7031 If this is the last string character displayed, check
7032 the next buffer location. */
7033 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7034 && (it->current.overlay_string_index
7035 == it->n_overlay_strings - 1))
7036 {
7037 ptrdiff_t ignore;
7038 int next_face_id;
7039 struct text_pos pos = it->current.pos;
7040 INC_TEXT_POS (pos, it->multibyte_p);
7041
7042 next_face_id = face_at_buffer_position
7043 (it->w, CHARPOS (pos), it->region_beg_charpos,
7044 it->region_end_charpos, &ignore,
7045 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
7046 -1);
7047 it->end_of_box_run_p
7048 = (FACE_FROM_ID (it->f, next_face_id)->box
7049 == FACE_NO_BOX);
7050 }
7051 }
7052 }
7053 /* next_element_from_display_vector sets this flag according to
7054 faces of the display vector glyphs, see there. */
7055 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7056 {
7057 int face_id = face_after_it_pos (it);
7058 it->end_of_box_run_p
7059 = (face_id != it->face_id
7060 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7061 }
7062 }
7063 /* If we reached the end of the object we've been iterating (e.g., a
7064 display string or an overlay string), and there's something on
7065 IT->stack, proceed with what's on the stack. It doesn't make
7066 sense to return zero if there's unprocessed stuff on the stack,
7067 because otherwise that stuff will never be displayed. */
7068 if (!success_p && it->sp > 0)
7069 {
7070 set_iterator_to_next (it, 0);
7071 success_p = get_next_display_element (it);
7072 }
7073
7074 /* Value is 0 if end of buffer or string reached. */
7075 return success_p;
7076 }
7077
7078
7079 /* Move IT to the next display element.
7080
7081 RESEAT_P non-zero means if called on a newline in buffer text,
7082 skip to the next visible line start.
7083
7084 Functions get_next_display_element and set_iterator_to_next are
7085 separate because I find this arrangement easier to handle than a
7086 get_next_display_element function that also increments IT's
7087 position. The way it is we can first look at an iterator's current
7088 display element, decide whether it fits on a line, and if it does,
7089 increment the iterator position. The other way around we probably
7090 would either need a flag indicating whether the iterator has to be
7091 incremented the next time, or we would have to implement a
7092 decrement position function which would not be easy to write. */
7093
7094 void
7095 set_iterator_to_next (struct it *it, int reseat_p)
7096 {
7097 /* Reset flags indicating start and end of a sequence of characters
7098 with box. Reset them at the start of this function because
7099 moving the iterator to a new position might set them. */
7100 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7101
7102 switch (it->method)
7103 {
7104 case GET_FROM_BUFFER:
7105 /* The current display element of IT is a character from
7106 current_buffer. Advance in the buffer, and maybe skip over
7107 invisible lines that are so because of selective display. */
7108 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7109 reseat_at_next_visible_line_start (it, 0);
7110 else if (it->cmp_it.id >= 0)
7111 {
7112 /* We are currently getting glyphs from a composition. */
7113 int i;
7114
7115 if (! it->bidi_p)
7116 {
7117 IT_CHARPOS (*it) += it->cmp_it.nchars;
7118 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7119 if (it->cmp_it.to < it->cmp_it.nglyphs)
7120 {
7121 it->cmp_it.from = it->cmp_it.to;
7122 }
7123 else
7124 {
7125 it->cmp_it.id = -1;
7126 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7127 IT_BYTEPOS (*it),
7128 it->end_charpos, Qnil);
7129 }
7130 }
7131 else if (! it->cmp_it.reversed_p)
7132 {
7133 /* Composition created while scanning forward. */
7134 /* Update IT's char/byte positions to point to the first
7135 character of the next grapheme cluster, or to the
7136 character visually after the current composition. */
7137 for (i = 0; i < it->cmp_it.nchars; i++)
7138 bidi_move_to_visually_next (&it->bidi_it);
7139 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7140 IT_CHARPOS (*it) = it->bidi_it.charpos;
7141
7142 if (it->cmp_it.to < it->cmp_it.nglyphs)
7143 {
7144 /* Proceed to the next grapheme cluster. */
7145 it->cmp_it.from = it->cmp_it.to;
7146 }
7147 else
7148 {
7149 /* No more grapheme clusters in this composition.
7150 Find the next stop position. */
7151 ptrdiff_t stop = it->end_charpos;
7152 if (it->bidi_it.scan_dir < 0)
7153 /* Now we are scanning backward and don't know
7154 where to stop. */
7155 stop = -1;
7156 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7157 IT_BYTEPOS (*it), stop, Qnil);
7158 }
7159 }
7160 else
7161 {
7162 /* Composition created while scanning backward. */
7163 /* Update IT's char/byte positions to point to the last
7164 character of the previous grapheme cluster, or the
7165 character visually after the current composition. */
7166 for (i = 0; i < it->cmp_it.nchars; i++)
7167 bidi_move_to_visually_next (&it->bidi_it);
7168 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7169 IT_CHARPOS (*it) = it->bidi_it.charpos;
7170 if (it->cmp_it.from > 0)
7171 {
7172 /* Proceed to the previous grapheme cluster. */
7173 it->cmp_it.to = it->cmp_it.from;
7174 }
7175 else
7176 {
7177 /* No more grapheme clusters in this composition.
7178 Find the next stop position. */
7179 ptrdiff_t stop = it->end_charpos;
7180 if (it->bidi_it.scan_dir < 0)
7181 /* Now we are scanning backward and don't know
7182 where to stop. */
7183 stop = -1;
7184 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7185 IT_BYTEPOS (*it), stop, Qnil);
7186 }
7187 }
7188 }
7189 else
7190 {
7191 eassert (it->len != 0);
7192
7193 if (!it->bidi_p)
7194 {
7195 IT_BYTEPOS (*it) += it->len;
7196 IT_CHARPOS (*it) += 1;
7197 }
7198 else
7199 {
7200 int prev_scan_dir = it->bidi_it.scan_dir;
7201 /* If this is a new paragraph, determine its base
7202 direction (a.k.a. its base embedding level). */
7203 if (it->bidi_it.new_paragraph)
7204 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7205 bidi_move_to_visually_next (&it->bidi_it);
7206 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7207 IT_CHARPOS (*it) = it->bidi_it.charpos;
7208 if (prev_scan_dir != it->bidi_it.scan_dir)
7209 {
7210 /* As the scan direction was changed, we must
7211 re-compute the stop position for composition. */
7212 ptrdiff_t stop = it->end_charpos;
7213 if (it->bidi_it.scan_dir < 0)
7214 stop = -1;
7215 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7216 IT_BYTEPOS (*it), stop, Qnil);
7217 }
7218 }
7219 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7220 }
7221 break;
7222
7223 case GET_FROM_C_STRING:
7224 /* Current display element of IT is from a C string. */
7225 if (!it->bidi_p
7226 /* If the string position is beyond string's end, it means
7227 next_element_from_c_string is padding the string with
7228 blanks, in which case we bypass the bidi iterator,
7229 because it cannot deal with such virtual characters. */
7230 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7231 {
7232 IT_BYTEPOS (*it) += it->len;
7233 IT_CHARPOS (*it) += 1;
7234 }
7235 else
7236 {
7237 bidi_move_to_visually_next (&it->bidi_it);
7238 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7239 IT_CHARPOS (*it) = it->bidi_it.charpos;
7240 }
7241 break;
7242
7243 case GET_FROM_DISPLAY_VECTOR:
7244 /* Current display element of IT is from a display table entry.
7245 Advance in the display table definition. Reset it to null if
7246 end reached, and continue with characters from buffers/
7247 strings. */
7248 ++it->current.dpvec_index;
7249
7250 /* Restore face of the iterator to what they were before the
7251 display vector entry (these entries may contain faces). */
7252 it->face_id = it->saved_face_id;
7253
7254 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7255 {
7256 int recheck_faces = it->ellipsis_p;
7257
7258 if (it->s)
7259 it->method = GET_FROM_C_STRING;
7260 else if (STRINGP (it->string))
7261 it->method = GET_FROM_STRING;
7262 else
7263 {
7264 it->method = GET_FROM_BUFFER;
7265 it->object = it->w->contents;
7266 }
7267
7268 it->dpvec = NULL;
7269 it->current.dpvec_index = -1;
7270
7271 /* Skip over characters which were displayed via IT->dpvec. */
7272 if (it->dpvec_char_len < 0)
7273 reseat_at_next_visible_line_start (it, 1);
7274 else if (it->dpvec_char_len > 0)
7275 {
7276 if (it->method == GET_FROM_STRING
7277 && it->current.overlay_string_index >= 0
7278 && it->n_overlay_strings > 0)
7279 it->ignore_overlay_strings_at_pos_p = 1;
7280 it->len = it->dpvec_char_len;
7281 set_iterator_to_next (it, reseat_p);
7282 }
7283
7284 /* Maybe recheck faces after display vector */
7285 if (recheck_faces)
7286 it->stop_charpos = IT_CHARPOS (*it);
7287 }
7288 break;
7289
7290 case GET_FROM_STRING:
7291 /* Current display element is a character from a Lisp string. */
7292 eassert (it->s == NULL && STRINGP (it->string));
7293 /* Don't advance past string end. These conditions are true
7294 when set_iterator_to_next is called at the end of
7295 get_next_display_element, in which case the Lisp string is
7296 already exhausted, and all we want is pop the iterator
7297 stack. */
7298 if (it->current.overlay_string_index >= 0)
7299 {
7300 /* This is an overlay string, so there's no padding with
7301 spaces, and the number of characters in the string is
7302 where the string ends. */
7303 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7304 goto consider_string_end;
7305 }
7306 else
7307 {
7308 /* Not an overlay string. There could be padding, so test
7309 against it->end_charpos . */
7310 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7311 goto consider_string_end;
7312 }
7313 if (it->cmp_it.id >= 0)
7314 {
7315 int i;
7316
7317 if (! it->bidi_p)
7318 {
7319 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7320 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7321 if (it->cmp_it.to < it->cmp_it.nglyphs)
7322 it->cmp_it.from = it->cmp_it.to;
7323 else
7324 {
7325 it->cmp_it.id = -1;
7326 composition_compute_stop_pos (&it->cmp_it,
7327 IT_STRING_CHARPOS (*it),
7328 IT_STRING_BYTEPOS (*it),
7329 it->end_charpos, it->string);
7330 }
7331 }
7332 else if (! it->cmp_it.reversed_p)
7333 {
7334 for (i = 0; i < it->cmp_it.nchars; i++)
7335 bidi_move_to_visually_next (&it->bidi_it);
7336 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7337 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7338
7339 if (it->cmp_it.to < it->cmp_it.nglyphs)
7340 it->cmp_it.from = it->cmp_it.to;
7341 else
7342 {
7343 ptrdiff_t stop = it->end_charpos;
7344 if (it->bidi_it.scan_dir < 0)
7345 stop = -1;
7346 composition_compute_stop_pos (&it->cmp_it,
7347 IT_STRING_CHARPOS (*it),
7348 IT_STRING_BYTEPOS (*it), stop,
7349 it->string);
7350 }
7351 }
7352 else
7353 {
7354 for (i = 0; i < it->cmp_it.nchars; i++)
7355 bidi_move_to_visually_next (&it->bidi_it);
7356 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7357 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7358 if (it->cmp_it.from > 0)
7359 it->cmp_it.to = it->cmp_it.from;
7360 else
7361 {
7362 ptrdiff_t stop = it->end_charpos;
7363 if (it->bidi_it.scan_dir < 0)
7364 stop = -1;
7365 composition_compute_stop_pos (&it->cmp_it,
7366 IT_STRING_CHARPOS (*it),
7367 IT_STRING_BYTEPOS (*it), stop,
7368 it->string);
7369 }
7370 }
7371 }
7372 else
7373 {
7374 if (!it->bidi_p
7375 /* If the string position is beyond string's end, it
7376 means next_element_from_string is padding the string
7377 with blanks, in which case we bypass the bidi
7378 iterator, because it cannot deal with such virtual
7379 characters. */
7380 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7381 {
7382 IT_STRING_BYTEPOS (*it) += it->len;
7383 IT_STRING_CHARPOS (*it) += 1;
7384 }
7385 else
7386 {
7387 int prev_scan_dir = it->bidi_it.scan_dir;
7388
7389 bidi_move_to_visually_next (&it->bidi_it);
7390 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7391 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7392 if (prev_scan_dir != it->bidi_it.scan_dir)
7393 {
7394 ptrdiff_t stop = it->end_charpos;
7395
7396 if (it->bidi_it.scan_dir < 0)
7397 stop = -1;
7398 composition_compute_stop_pos (&it->cmp_it,
7399 IT_STRING_CHARPOS (*it),
7400 IT_STRING_BYTEPOS (*it), stop,
7401 it->string);
7402 }
7403 }
7404 }
7405
7406 consider_string_end:
7407
7408 if (it->current.overlay_string_index >= 0)
7409 {
7410 /* IT->string is an overlay string. Advance to the
7411 next, if there is one. */
7412 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7413 {
7414 it->ellipsis_p = 0;
7415 next_overlay_string (it);
7416 if (it->ellipsis_p)
7417 setup_for_ellipsis (it, 0);
7418 }
7419 }
7420 else
7421 {
7422 /* IT->string is not an overlay string. If we reached
7423 its end, and there is something on IT->stack, proceed
7424 with what is on the stack. This can be either another
7425 string, this time an overlay string, or a buffer. */
7426 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7427 && it->sp > 0)
7428 {
7429 pop_it (it);
7430 if (it->method == GET_FROM_STRING)
7431 goto consider_string_end;
7432 }
7433 }
7434 break;
7435
7436 case GET_FROM_IMAGE:
7437 case GET_FROM_STRETCH:
7438 /* The position etc with which we have to proceed are on
7439 the stack. The position may be at the end of a string,
7440 if the `display' property takes up the whole string. */
7441 eassert (it->sp > 0);
7442 pop_it (it);
7443 if (it->method == GET_FROM_STRING)
7444 goto consider_string_end;
7445 break;
7446
7447 default:
7448 /* There are no other methods defined, so this should be a bug. */
7449 emacs_abort ();
7450 }
7451
7452 eassert (it->method != GET_FROM_STRING
7453 || (STRINGP (it->string)
7454 && IT_STRING_CHARPOS (*it) >= 0));
7455 }
7456
7457 /* Load IT's display element fields with information about the next
7458 display element which comes from a display table entry or from the
7459 result of translating a control character to one of the forms `^C'
7460 or `\003'.
7461
7462 IT->dpvec holds the glyphs to return as characters.
7463 IT->saved_face_id holds the face id before the display vector--it
7464 is restored into IT->face_id in set_iterator_to_next. */
7465
7466 static int
7467 next_element_from_display_vector (struct it *it)
7468 {
7469 Lisp_Object gc;
7470 int prev_face_id = it->face_id;
7471 int next_face_id;
7472
7473 /* Precondition. */
7474 eassert (it->dpvec && it->current.dpvec_index >= 0);
7475
7476 it->face_id = it->saved_face_id;
7477
7478 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7479 That seemed totally bogus - so I changed it... */
7480 gc = it->dpvec[it->current.dpvec_index];
7481
7482 if (GLYPH_CODE_P (gc))
7483 {
7484 struct face *this_face, *prev_face, *next_face;
7485
7486 it->c = GLYPH_CODE_CHAR (gc);
7487 it->len = CHAR_BYTES (it->c);
7488
7489 /* The entry may contain a face id to use. Such a face id is
7490 the id of a Lisp face, not a realized face. A face id of
7491 zero means no face is specified. */
7492 if (it->dpvec_face_id >= 0)
7493 it->face_id = it->dpvec_face_id;
7494 else
7495 {
7496 int lface_id = GLYPH_CODE_FACE (gc);
7497 if (lface_id > 0)
7498 it->face_id = merge_faces (it->f, Qt, lface_id,
7499 it->saved_face_id);
7500 }
7501
7502 /* Glyphs in the display vector could have the box face, so we
7503 need to set the related flags in the iterator, as
7504 appropriate. */
7505 this_face = FACE_FROM_ID (it->f, it->face_id);
7506 prev_face = FACE_FROM_ID (it->f, prev_face_id);
7507
7508 /* Is this character the first character of a box-face run? */
7509 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7510 && (!prev_face
7511 || prev_face->box == FACE_NO_BOX));
7512
7513 /* For the last character of the box-face run, we need to look
7514 either at the next glyph from the display vector, or at the
7515 face we saw before the display vector. */
7516 next_face_id = it->saved_face_id;
7517 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7518 {
7519 if (it->dpvec_face_id >= 0)
7520 next_face_id = it->dpvec_face_id;
7521 else
7522 {
7523 int lface_id =
7524 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7525
7526 if (lface_id > 0)
7527 next_face_id = merge_faces (it->f, Qt, lface_id,
7528 it->saved_face_id);
7529 }
7530 }
7531 next_face = FACE_FROM_ID (it->f, next_face_id);
7532 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7533 && (!next_face
7534 || next_face->box == FACE_NO_BOX));
7535 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7536 }
7537 else
7538 /* Display table entry is invalid. Return a space. */
7539 it->c = ' ', it->len = 1;
7540
7541 /* Don't change position and object of the iterator here. They are
7542 still the values of the character that had this display table
7543 entry or was translated, and that's what we want. */
7544 it->what = IT_CHARACTER;
7545 return 1;
7546 }
7547
7548 /* Get the first element of string/buffer in the visual order, after
7549 being reseated to a new position in a string or a buffer. */
7550 static void
7551 get_visually_first_element (struct it *it)
7552 {
7553 int string_p = STRINGP (it->string) || it->s;
7554 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7555 ptrdiff_t bob = (string_p ? 0 : BEGV);
7556
7557 if (STRINGP (it->string))
7558 {
7559 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7560 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7561 }
7562 else
7563 {
7564 it->bidi_it.charpos = IT_CHARPOS (*it);
7565 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7566 }
7567
7568 if (it->bidi_it.charpos == eob)
7569 {
7570 /* Nothing to do, but reset the FIRST_ELT flag, like
7571 bidi_paragraph_init does, because we are not going to
7572 call it. */
7573 it->bidi_it.first_elt = 0;
7574 }
7575 else if (it->bidi_it.charpos == bob
7576 || (!string_p
7577 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7578 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7579 {
7580 /* If we are at the beginning of a line/string, we can produce
7581 the next element right away. */
7582 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7583 bidi_move_to_visually_next (&it->bidi_it);
7584 }
7585 else
7586 {
7587 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7588
7589 /* We need to prime the bidi iterator starting at the line's or
7590 string's beginning, before we will be able to produce the
7591 next element. */
7592 if (string_p)
7593 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7594 else
7595 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7596 IT_BYTEPOS (*it), -1,
7597 &it->bidi_it.bytepos);
7598 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7599 do
7600 {
7601 /* Now return to buffer/string position where we were asked
7602 to get the next display element, and produce that. */
7603 bidi_move_to_visually_next (&it->bidi_it);
7604 }
7605 while (it->bidi_it.bytepos != orig_bytepos
7606 && it->bidi_it.charpos < eob);
7607 }
7608
7609 /* Adjust IT's position information to where we ended up. */
7610 if (STRINGP (it->string))
7611 {
7612 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7613 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7614 }
7615 else
7616 {
7617 IT_CHARPOS (*it) = it->bidi_it.charpos;
7618 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7619 }
7620
7621 if (STRINGP (it->string) || !it->s)
7622 {
7623 ptrdiff_t stop, charpos, bytepos;
7624
7625 if (STRINGP (it->string))
7626 {
7627 eassert (!it->s);
7628 stop = SCHARS (it->string);
7629 if (stop > it->end_charpos)
7630 stop = it->end_charpos;
7631 charpos = IT_STRING_CHARPOS (*it);
7632 bytepos = IT_STRING_BYTEPOS (*it);
7633 }
7634 else
7635 {
7636 stop = it->end_charpos;
7637 charpos = IT_CHARPOS (*it);
7638 bytepos = IT_BYTEPOS (*it);
7639 }
7640 if (it->bidi_it.scan_dir < 0)
7641 stop = -1;
7642 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7643 it->string);
7644 }
7645 }
7646
7647 /* Load IT with the next display element from Lisp string IT->string.
7648 IT->current.string_pos is the current position within the string.
7649 If IT->current.overlay_string_index >= 0, the Lisp string is an
7650 overlay string. */
7651
7652 static int
7653 next_element_from_string (struct it *it)
7654 {
7655 struct text_pos position;
7656
7657 eassert (STRINGP (it->string));
7658 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7659 eassert (IT_STRING_CHARPOS (*it) >= 0);
7660 position = it->current.string_pos;
7661
7662 /* With bidi reordering, the character to display might not be the
7663 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7664 that we were reseat()ed to a new string, whose paragraph
7665 direction is not known. */
7666 if (it->bidi_p && it->bidi_it.first_elt)
7667 {
7668 get_visually_first_element (it);
7669 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7670 }
7671
7672 /* Time to check for invisible text? */
7673 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7674 {
7675 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7676 {
7677 if (!(!it->bidi_p
7678 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7679 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7680 {
7681 /* With bidi non-linear iteration, we could find
7682 ourselves far beyond the last computed stop_charpos,
7683 with several other stop positions in between that we
7684 missed. Scan them all now, in buffer's logical
7685 order, until we find and handle the last stop_charpos
7686 that precedes our current position. */
7687 handle_stop_backwards (it, it->stop_charpos);
7688 return GET_NEXT_DISPLAY_ELEMENT (it);
7689 }
7690 else
7691 {
7692 if (it->bidi_p)
7693 {
7694 /* Take note of the stop position we just moved
7695 across, for when we will move back across it. */
7696 it->prev_stop = it->stop_charpos;
7697 /* If we are at base paragraph embedding level, take
7698 note of the last stop position seen at this
7699 level. */
7700 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7701 it->base_level_stop = it->stop_charpos;
7702 }
7703 handle_stop (it);
7704
7705 /* Since a handler may have changed IT->method, we must
7706 recurse here. */
7707 return GET_NEXT_DISPLAY_ELEMENT (it);
7708 }
7709 }
7710 else if (it->bidi_p
7711 /* If we are before prev_stop, we may have overstepped
7712 on our way backwards a stop_pos, and if so, we need
7713 to handle that stop_pos. */
7714 && IT_STRING_CHARPOS (*it) < it->prev_stop
7715 /* We can sometimes back up for reasons that have nothing
7716 to do with bidi reordering. E.g., compositions. The
7717 code below is only needed when we are above the base
7718 embedding level, so test for that explicitly. */
7719 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7720 {
7721 /* If we lost track of base_level_stop, we have no better
7722 place for handle_stop_backwards to start from than string
7723 beginning. This happens, e.g., when we were reseated to
7724 the previous screenful of text by vertical-motion. */
7725 if (it->base_level_stop <= 0
7726 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7727 it->base_level_stop = 0;
7728 handle_stop_backwards (it, it->base_level_stop);
7729 return GET_NEXT_DISPLAY_ELEMENT (it);
7730 }
7731 }
7732
7733 if (it->current.overlay_string_index >= 0)
7734 {
7735 /* Get the next character from an overlay string. In overlay
7736 strings, there is no field width or padding with spaces to
7737 do. */
7738 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7739 {
7740 it->what = IT_EOB;
7741 return 0;
7742 }
7743 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7744 IT_STRING_BYTEPOS (*it),
7745 it->bidi_it.scan_dir < 0
7746 ? -1
7747 : SCHARS (it->string))
7748 && next_element_from_composition (it))
7749 {
7750 return 1;
7751 }
7752 else if (STRING_MULTIBYTE (it->string))
7753 {
7754 const unsigned char *s = (SDATA (it->string)
7755 + IT_STRING_BYTEPOS (*it));
7756 it->c = string_char_and_length (s, &it->len);
7757 }
7758 else
7759 {
7760 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7761 it->len = 1;
7762 }
7763 }
7764 else
7765 {
7766 /* Get the next character from a Lisp string that is not an
7767 overlay string. Such strings come from the mode line, for
7768 example. We may have to pad with spaces, or truncate the
7769 string. See also next_element_from_c_string. */
7770 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7771 {
7772 it->what = IT_EOB;
7773 return 0;
7774 }
7775 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7776 {
7777 /* Pad with spaces. */
7778 it->c = ' ', it->len = 1;
7779 CHARPOS (position) = BYTEPOS (position) = -1;
7780 }
7781 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7782 IT_STRING_BYTEPOS (*it),
7783 it->bidi_it.scan_dir < 0
7784 ? -1
7785 : it->string_nchars)
7786 && next_element_from_composition (it))
7787 {
7788 return 1;
7789 }
7790 else if (STRING_MULTIBYTE (it->string))
7791 {
7792 const unsigned char *s = (SDATA (it->string)
7793 + IT_STRING_BYTEPOS (*it));
7794 it->c = string_char_and_length (s, &it->len);
7795 }
7796 else
7797 {
7798 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7799 it->len = 1;
7800 }
7801 }
7802
7803 /* Record what we have and where it came from. */
7804 it->what = IT_CHARACTER;
7805 it->object = it->string;
7806 it->position = position;
7807 return 1;
7808 }
7809
7810
7811 /* Load IT with next display element from C string IT->s.
7812 IT->string_nchars is the maximum number of characters to return
7813 from the string. IT->end_charpos may be greater than
7814 IT->string_nchars when this function is called, in which case we
7815 may have to return padding spaces. Value is zero if end of string
7816 reached, including padding spaces. */
7817
7818 static int
7819 next_element_from_c_string (struct it *it)
7820 {
7821 int success_p = 1;
7822
7823 eassert (it->s);
7824 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7825 it->what = IT_CHARACTER;
7826 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7827 it->object = Qnil;
7828
7829 /* With bidi reordering, the character to display might not be the
7830 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7831 we were reseated to a new string, whose paragraph direction is
7832 not known. */
7833 if (it->bidi_p && it->bidi_it.first_elt)
7834 get_visually_first_element (it);
7835
7836 /* IT's position can be greater than IT->string_nchars in case a
7837 field width or precision has been specified when the iterator was
7838 initialized. */
7839 if (IT_CHARPOS (*it) >= it->end_charpos)
7840 {
7841 /* End of the game. */
7842 it->what = IT_EOB;
7843 success_p = 0;
7844 }
7845 else if (IT_CHARPOS (*it) >= it->string_nchars)
7846 {
7847 /* Pad with spaces. */
7848 it->c = ' ', it->len = 1;
7849 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7850 }
7851 else if (it->multibyte_p)
7852 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7853 else
7854 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7855
7856 return success_p;
7857 }
7858
7859
7860 /* Set up IT to return characters from an ellipsis, if appropriate.
7861 The definition of the ellipsis glyphs may come from a display table
7862 entry. This function fills IT with the first glyph from the
7863 ellipsis if an ellipsis is to be displayed. */
7864
7865 static int
7866 next_element_from_ellipsis (struct it *it)
7867 {
7868 if (it->selective_display_ellipsis_p)
7869 setup_for_ellipsis (it, it->len);
7870 else
7871 {
7872 /* The face at the current position may be different from the
7873 face we find after the invisible text. Remember what it
7874 was in IT->saved_face_id, and signal that it's there by
7875 setting face_before_selective_p. */
7876 it->saved_face_id = it->face_id;
7877 it->method = GET_FROM_BUFFER;
7878 it->object = it->w->contents;
7879 reseat_at_next_visible_line_start (it, 1);
7880 it->face_before_selective_p = 1;
7881 }
7882
7883 return GET_NEXT_DISPLAY_ELEMENT (it);
7884 }
7885
7886
7887 /* Deliver an image display element. The iterator IT is already
7888 filled with image information (done in handle_display_prop). Value
7889 is always 1. */
7890
7891
7892 static int
7893 next_element_from_image (struct it *it)
7894 {
7895 it->what = IT_IMAGE;
7896 it->ignore_overlay_strings_at_pos_p = 0;
7897 return 1;
7898 }
7899
7900
7901 /* Fill iterator IT with next display element from a stretch glyph
7902 property. IT->object is the value of the text property. Value is
7903 always 1. */
7904
7905 static int
7906 next_element_from_stretch (struct it *it)
7907 {
7908 it->what = IT_STRETCH;
7909 return 1;
7910 }
7911
7912 /* Scan backwards from IT's current position until we find a stop
7913 position, or until BEGV. This is called when we find ourself
7914 before both the last known prev_stop and base_level_stop while
7915 reordering bidirectional text. */
7916
7917 static void
7918 compute_stop_pos_backwards (struct it *it)
7919 {
7920 const int SCAN_BACK_LIMIT = 1000;
7921 struct text_pos pos;
7922 struct display_pos save_current = it->current;
7923 struct text_pos save_position = it->position;
7924 ptrdiff_t charpos = IT_CHARPOS (*it);
7925 ptrdiff_t where_we_are = charpos;
7926 ptrdiff_t save_stop_pos = it->stop_charpos;
7927 ptrdiff_t save_end_pos = it->end_charpos;
7928
7929 eassert (NILP (it->string) && !it->s);
7930 eassert (it->bidi_p);
7931 it->bidi_p = 0;
7932 do
7933 {
7934 it->end_charpos = min (charpos + 1, ZV);
7935 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7936 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7937 reseat_1 (it, pos, 0);
7938 compute_stop_pos (it);
7939 /* We must advance forward, right? */
7940 if (it->stop_charpos <= charpos)
7941 emacs_abort ();
7942 }
7943 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7944
7945 if (it->stop_charpos <= where_we_are)
7946 it->prev_stop = it->stop_charpos;
7947 else
7948 it->prev_stop = BEGV;
7949 it->bidi_p = 1;
7950 it->current = save_current;
7951 it->position = save_position;
7952 it->stop_charpos = save_stop_pos;
7953 it->end_charpos = save_end_pos;
7954 }
7955
7956 /* Scan forward from CHARPOS in the current buffer/string, until we
7957 find a stop position > current IT's position. Then handle the stop
7958 position before that. This is called when we bump into a stop
7959 position while reordering bidirectional text. CHARPOS should be
7960 the last previously processed stop_pos (or BEGV/0, if none were
7961 processed yet) whose position is less that IT's current
7962 position. */
7963
7964 static void
7965 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7966 {
7967 int bufp = !STRINGP (it->string);
7968 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7969 struct display_pos save_current = it->current;
7970 struct text_pos save_position = it->position;
7971 struct text_pos pos1;
7972 ptrdiff_t next_stop;
7973
7974 /* Scan in strict logical order. */
7975 eassert (it->bidi_p);
7976 it->bidi_p = 0;
7977 do
7978 {
7979 it->prev_stop = charpos;
7980 if (bufp)
7981 {
7982 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7983 reseat_1 (it, pos1, 0);
7984 }
7985 else
7986 it->current.string_pos = string_pos (charpos, it->string);
7987 compute_stop_pos (it);
7988 /* We must advance forward, right? */
7989 if (it->stop_charpos <= it->prev_stop)
7990 emacs_abort ();
7991 charpos = it->stop_charpos;
7992 }
7993 while (charpos <= where_we_are);
7994
7995 it->bidi_p = 1;
7996 it->current = save_current;
7997 it->position = save_position;
7998 next_stop = it->stop_charpos;
7999 it->stop_charpos = it->prev_stop;
8000 handle_stop (it);
8001 it->stop_charpos = next_stop;
8002 }
8003
8004 /* Load IT with the next display element from current_buffer. Value
8005 is zero if end of buffer reached. IT->stop_charpos is the next
8006 position at which to stop and check for text properties or buffer
8007 end. */
8008
8009 static int
8010 next_element_from_buffer (struct it *it)
8011 {
8012 int success_p = 1;
8013
8014 eassert (IT_CHARPOS (*it) >= BEGV);
8015 eassert (NILP (it->string) && !it->s);
8016 eassert (!it->bidi_p
8017 || (EQ (it->bidi_it.string.lstring, Qnil)
8018 && it->bidi_it.string.s == NULL));
8019
8020 /* With bidi reordering, the character to display might not be the
8021 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
8022 we were reseat()ed to a new buffer position, which is potentially
8023 a different paragraph. */
8024 if (it->bidi_p && it->bidi_it.first_elt)
8025 {
8026 get_visually_first_element (it);
8027 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8028 }
8029
8030 if (IT_CHARPOS (*it) >= it->stop_charpos)
8031 {
8032 if (IT_CHARPOS (*it) >= it->end_charpos)
8033 {
8034 int overlay_strings_follow_p;
8035
8036 /* End of the game, except when overlay strings follow that
8037 haven't been returned yet. */
8038 if (it->overlay_strings_at_end_processed_p)
8039 overlay_strings_follow_p = 0;
8040 else
8041 {
8042 it->overlay_strings_at_end_processed_p = 1;
8043 overlay_strings_follow_p = get_overlay_strings (it, 0);
8044 }
8045
8046 if (overlay_strings_follow_p)
8047 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8048 else
8049 {
8050 it->what = IT_EOB;
8051 it->position = it->current.pos;
8052 success_p = 0;
8053 }
8054 }
8055 else if (!(!it->bidi_p
8056 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8057 || IT_CHARPOS (*it) == it->stop_charpos))
8058 {
8059 /* With bidi non-linear iteration, we could find ourselves
8060 far beyond the last computed stop_charpos, with several
8061 other stop positions in between that we missed. Scan
8062 them all now, in buffer's logical order, until we find
8063 and handle the last stop_charpos that precedes our
8064 current position. */
8065 handle_stop_backwards (it, it->stop_charpos);
8066 return GET_NEXT_DISPLAY_ELEMENT (it);
8067 }
8068 else
8069 {
8070 if (it->bidi_p)
8071 {
8072 /* Take note of the stop position we just moved across,
8073 for when we will move back across it. */
8074 it->prev_stop = it->stop_charpos;
8075 /* If we are at base paragraph embedding level, take
8076 note of the last stop position seen at this
8077 level. */
8078 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8079 it->base_level_stop = it->stop_charpos;
8080 }
8081 handle_stop (it);
8082 return GET_NEXT_DISPLAY_ELEMENT (it);
8083 }
8084 }
8085 else if (it->bidi_p
8086 /* If we are before prev_stop, we may have overstepped on
8087 our way backwards a stop_pos, and if so, we need to
8088 handle that stop_pos. */
8089 && IT_CHARPOS (*it) < it->prev_stop
8090 /* We can sometimes back up for reasons that have nothing
8091 to do with bidi reordering. E.g., compositions. The
8092 code below is only needed when we are above the base
8093 embedding level, so test for that explicitly. */
8094 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8095 {
8096 if (it->base_level_stop <= 0
8097 || IT_CHARPOS (*it) < it->base_level_stop)
8098 {
8099 /* If we lost track of base_level_stop, we need to find
8100 prev_stop by looking backwards. This happens, e.g., when
8101 we were reseated to the previous screenful of text by
8102 vertical-motion. */
8103 it->base_level_stop = BEGV;
8104 compute_stop_pos_backwards (it);
8105 handle_stop_backwards (it, it->prev_stop);
8106 }
8107 else
8108 handle_stop_backwards (it, it->base_level_stop);
8109 return GET_NEXT_DISPLAY_ELEMENT (it);
8110 }
8111 else
8112 {
8113 /* No face changes, overlays etc. in sight, so just return a
8114 character from current_buffer. */
8115 unsigned char *p;
8116 ptrdiff_t stop;
8117
8118 /* Maybe run the redisplay end trigger hook. Performance note:
8119 This doesn't seem to cost measurable time. */
8120 if (it->redisplay_end_trigger_charpos
8121 && it->glyph_row
8122 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8123 run_redisplay_end_trigger_hook (it);
8124
8125 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8126 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8127 stop)
8128 && next_element_from_composition (it))
8129 {
8130 return 1;
8131 }
8132
8133 /* Get the next character, maybe multibyte. */
8134 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8135 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8136 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8137 else
8138 it->c = *p, it->len = 1;
8139
8140 /* Record what we have and where it came from. */
8141 it->what = IT_CHARACTER;
8142 it->object = it->w->contents;
8143 it->position = it->current.pos;
8144
8145 /* Normally we return the character found above, except when we
8146 really want to return an ellipsis for selective display. */
8147 if (it->selective)
8148 {
8149 if (it->c == '\n')
8150 {
8151 /* A value of selective > 0 means hide lines indented more
8152 than that number of columns. */
8153 if (it->selective > 0
8154 && IT_CHARPOS (*it) + 1 < ZV
8155 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8156 IT_BYTEPOS (*it) + 1,
8157 it->selective))
8158 {
8159 success_p = next_element_from_ellipsis (it);
8160 it->dpvec_char_len = -1;
8161 }
8162 }
8163 else if (it->c == '\r' && it->selective == -1)
8164 {
8165 /* A value of selective == -1 means that everything from the
8166 CR to the end of the line is invisible, with maybe an
8167 ellipsis displayed for it. */
8168 success_p = next_element_from_ellipsis (it);
8169 it->dpvec_char_len = -1;
8170 }
8171 }
8172 }
8173
8174 /* Value is zero if end of buffer reached. */
8175 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8176 return success_p;
8177 }
8178
8179
8180 /* Run the redisplay end trigger hook for IT. */
8181
8182 static void
8183 run_redisplay_end_trigger_hook (struct it *it)
8184 {
8185 Lisp_Object args[3];
8186
8187 /* IT->glyph_row should be non-null, i.e. we should be actually
8188 displaying something, or otherwise we should not run the hook. */
8189 eassert (it->glyph_row);
8190
8191 /* Set up hook arguments. */
8192 args[0] = Qredisplay_end_trigger_functions;
8193 args[1] = it->window;
8194 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8195 it->redisplay_end_trigger_charpos = 0;
8196
8197 /* Since we are *trying* to run these functions, don't try to run
8198 them again, even if they get an error. */
8199 wset_redisplay_end_trigger (it->w, Qnil);
8200 Frun_hook_with_args (3, args);
8201
8202 /* Notice if it changed the face of the character we are on. */
8203 handle_face_prop (it);
8204 }
8205
8206
8207 /* Deliver a composition display element. Unlike the other
8208 next_element_from_XXX, this function is not registered in the array
8209 get_next_element[]. It is called from next_element_from_buffer and
8210 next_element_from_string when necessary. */
8211
8212 static int
8213 next_element_from_composition (struct it *it)
8214 {
8215 it->what = IT_COMPOSITION;
8216 it->len = it->cmp_it.nbytes;
8217 if (STRINGP (it->string))
8218 {
8219 if (it->c < 0)
8220 {
8221 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8222 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8223 return 0;
8224 }
8225 it->position = it->current.string_pos;
8226 it->object = it->string;
8227 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8228 IT_STRING_BYTEPOS (*it), it->string);
8229 }
8230 else
8231 {
8232 if (it->c < 0)
8233 {
8234 IT_CHARPOS (*it) += it->cmp_it.nchars;
8235 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8236 if (it->bidi_p)
8237 {
8238 if (it->bidi_it.new_paragraph)
8239 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8240 /* Resync the bidi iterator with IT's new position.
8241 FIXME: this doesn't support bidirectional text. */
8242 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8243 bidi_move_to_visually_next (&it->bidi_it);
8244 }
8245 return 0;
8246 }
8247 it->position = it->current.pos;
8248 it->object = it->w->contents;
8249 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8250 IT_BYTEPOS (*it), Qnil);
8251 }
8252 return 1;
8253 }
8254
8255
8256 \f
8257 /***********************************************************************
8258 Moving an iterator without producing glyphs
8259 ***********************************************************************/
8260
8261 /* Check if iterator is at a position corresponding to a valid buffer
8262 position after some move_it_ call. */
8263
8264 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8265 ((it)->method == GET_FROM_STRING \
8266 ? IT_STRING_CHARPOS (*it) == 0 \
8267 : 1)
8268
8269
8270 /* Move iterator IT to a specified buffer or X position within one
8271 line on the display without producing glyphs.
8272
8273 OP should be a bit mask including some or all of these bits:
8274 MOVE_TO_X: Stop upon reaching x-position TO_X.
8275 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8276 Regardless of OP's value, stop upon reaching the end of the display line.
8277
8278 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8279 This means, in particular, that TO_X includes window's horizontal
8280 scroll amount.
8281
8282 The return value has several possible values that
8283 say what condition caused the scan to stop:
8284
8285 MOVE_POS_MATCH_OR_ZV
8286 - when TO_POS or ZV was reached.
8287
8288 MOVE_X_REACHED
8289 -when TO_X was reached before TO_POS or ZV were reached.
8290
8291 MOVE_LINE_CONTINUED
8292 - when we reached the end of the display area and the line must
8293 be continued.
8294
8295 MOVE_LINE_TRUNCATED
8296 - when we reached the end of the display area and the line is
8297 truncated.
8298
8299 MOVE_NEWLINE_OR_CR
8300 - when we stopped at a line end, i.e. a newline or a CR and selective
8301 display is on. */
8302
8303 static enum move_it_result
8304 move_it_in_display_line_to (struct it *it,
8305 ptrdiff_t to_charpos, int to_x,
8306 enum move_operation_enum op)
8307 {
8308 enum move_it_result result = MOVE_UNDEFINED;
8309 struct glyph_row *saved_glyph_row;
8310 struct it wrap_it, atpos_it, atx_it, ppos_it;
8311 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8312 void *ppos_data = NULL;
8313 int may_wrap = 0;
8314 enum it_method prev_method = it->method;
8315 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8316 int saw_smaller_pos = prev_pos < to_charpos;
8317
8318 /* Don't produce glyphs in produce_glyphs. */
8319 saved_glyph_row = it->glyph_row;
8320 it->glyph_row = NULL;
8321
8322 /* Use wrap_it to save a copy of IT wherever a word wrap could
8323 occur. Use atpos_it to save a copy of IT at the desired buffer
8324 position, if found, so that we can scan ahead and check if the
8325 word later overshoots the window edge. Use atx_it similarly, for
8326 pixel positions. */
8327 wrap_it.sp = -1;
8328 atpos_it.sp = -1;
8329 atx_it.sp = -1;
8330
8331 /* Use ppos_it under bidi reordering to save a copy of IT for the
8332 position > CHARPOS that is the closest to CHARPOS. We restore
8333 that position in IT when we have scanned the entire display line
8334 without finding a match for CHARPOS and all the character
8335 positions are greater than CHARPOS. */
8336 if (it->bidi_p)
8337 {
8338 SAVE_IT (ppos_it, *it, ppos_data);
8339 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8340 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8341 SAVE_IT (ppos_it, *it, ppos_data);
8342 }
8343
8344 #define BUFFER_POS_REACHED_P() \
8345 ((op & MOVE_TO_POS) != 0 \
8346 && BUFFERP (it->object) \
8347 && (IT_CHARPOS (*it) == to_charpos \
8348 || ((!it->bidi_p \
8349 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8350 && IT_CHARPOS (*it) > to_charpos) \
8351 || (it->what == IT_COMPOSITION \
8352 && ((IT_CHARPOS (*it) > to_charpos \
8353 && to_charpos >= it->cmp_it.charpos) \
8354 || (IT_CHARPOS (*it) < to_charpos \
8355 && to_charpos <= it->cmp_it.charpos)))) \
8356 && (it->method == GET_FROM_BUFFER \
8357 || (it->method == GET_FROM_DISPLAY_VECTOR \
8358 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8359
8360 /* If there's a line-/wrap-prefix, handle it. */
8361 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8362 && it->current_y < it->last_visible_y)
8363 handle_line_prefix (it);
8364
8365 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8366 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8367
8368 while (1)
8369 {
8370 int x, i, ascent = 0, descent = 0;
8371
8372 /* Utility macro to reset an iterator with x, ascent, and descent. */
8373 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8374 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8375 (IT)->max_descent = descent)
8376
8377 /* Stop if we move beyond TO_CHARPOS (after an image or a
8378 display string or stretch glyph). */
8379 if ((op & MOVE_TO_POS) != 0
8380 && BUFFERP (it->object)
8381 && it->method == GET_FROM_BUFFER
8382 && (((!it->bidi_p
8383 /* When the iterator is at base embedding level, we
8384 are guaranteed that characters are delivered for
8385 display in strictly increasing order of their
8386 buffer positions. */
8387 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8388 && IT_CHARPOS (*it) > to_charpos)
8389 || (it->bidi_p
8390 && (prev_method == GET_FROM_IMAGE
8391 || prev_method == GET_FROM_STRETCH
8392 || prev_method == GET_FROM_STRING)
8393 /* Passed TO_CHARPOS from left to right. */
8394 && ((prev_pos < to_charpos
8395 && IT_CHARPOS (*it) > to_charpos)
8396 /* Passed TO_CHARPOS from right to left. */
8397 || (prev_pos > to_charpos
8398 && IT_CHARPOS (*it) < to_charpos)))))
8399 {
8400 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8401 {
8402 result = MOVE_POS_MATCH_OR_ZV;
8403 break;
8404 }
8405 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8406 /* If wrap_it is valid, the current position might be in a
8407 word that is wrapped. So, save the iterator in
8408 atpos_it and continue to see if wrapping happens. */
8409 SAVE_IT (atpos_it, *it, atpos_data);
8410 }
8411
8412 /* Stop when ZV reached.
8413 We used to stop here when TO_CHARPOS reached as well, but that is
8414 too soon if this glyph does not fit on this line. So we handle it
8415 explicitly below. */
8416 if (!get_next_display_element (it))
8417 {
8418 result = MOVE_POS_MATCH_OR_ZV;
8419 break;
8420 }
8421
8422 if (it->line_wrap == TRUNCATE)
8423 {
8424 if (BUFFER_POS_REACHED_P ())
8425 {
8426 result = MOVE_POS_MATCH_OR_ZV;
8427 break;
8428 }
8429 }
8430 else
8431 {
8432 if (it->line_wrap == WORD_WRAP)
8433 {
8434 if (IT_DISPLAYING_WHITESPACE (it))
8435 may_wrap = 1;
8436 else if (may_wrap)
8437 {
8438 /* We have reached a glyph that follows one or more
8439 whitespace characters. If the position is
8440 already found, we are done. */
8441 if (atpos_it.sp >= 0)
8442 {
8443 RESTORE_IT (it, &atpos_it, atpos_data);
8444 result = MOVE_POS_MATCH_OR_ZV;
8445 goto done;
8446 }
8447 if (atx_it.sp >= 0)
8448 {
8449 RESTORE_IT (it, &atx_it, atx_data);
8450 result = MOVE_X_REACHED;
8451 goto done;
8452 }
8453 /* Otherwise, we can wrap here. */
8454 SAVE_IT (wrap_it, *it, wrap_data);
8455 may_wrap = 0;
8456 }
8457 }
8458 }
8459
8460 /* Remember the line height for the current line, in case
8461 the next element doesn't fit on the line. */
8462 ascent = it->max_ascent;
8463 descent = it->max_descent;
8464
8465 /* The call to produce_glyphs will get the metrics of the
8466 display element IT is loaded with. Record the x-position
8467 before this display element, in case it doesn't fit on the
8468 line. */
8469 x = it->current_x;
8470
8471 PRODUCE_GLYPHS (it);
8472
8473 if (it->area != TEXT_AREA)
8474 {
8475 prev_method = it->method;
8476 if (it->method == GET_FROM_BUFFER)
8477 prev_pos = IT_CHARPOS (*it);
8478 set_iterator_to_next (it, 1);
8479 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8480 SET_TEXT_POS (this_line_min_pos,
8481 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8482 if (it->bidi_p
8483 && (op & MOVE_TO_POS)
8484 && IT_CHARPOS (*it) > to_charpos
8485 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8486 SAVE_IT (ppos_it, *it, ppos_data);
8487 continue;
8488 }
8489
8490 /* The number of glyphs we get back in IT->nglyphs will normally
8491 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8492 character on a terminal frame, or (iii) a line end. For the
8493 second case, IT->nglyphs - 1 padding glyphs will be present.
8494 (On X frames, there is only one glyph produced for a
8495 composite character.)
8496
8497 The behavior implemented below means, for continuation lines,
8498 that as many spaces of a TAB as fit on the current line are
8499 displayed there. For terminal frames, as many glyphs of a
8500 multi-glyph character are displayed in the current line, too.
8501 This is what the old redisplay code did, and we keep it that
8502 way. Under X, the whole shape of a complex character must
8503 fit on the line or it will be completely displayed in the
8504 next line.
8505
8506 Note that both for tabs and padding glyphs, all glyphs have
8507 the same width. */
8508 if (it->nglyphs)
8509 {
8510 /* More than one glyph or glyph doesn't fit on line. All
8511 glyphs have the same width. */
8512 int single_glyph_width = it->pixel_width / it->nglyphs;
8513 int new_x;
8514 int x_before_this_char = x;
8515 int hpos_before_this_char = it->hpos;
8516
8517 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8518 {
8519 new_x = x + single_glyph_width;
8520
8521 /* We want to leave anything reaching TO_X to the caller. */
8522 if ((op & MOVE_TO_X) && new_x > to_x)
8523 {
8524 if (BUFFER_POS_REACHED_P ())
8525 {
8526 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8527 goto buffer_pos_reached;
8528 if (atpos_it.sp < 0)
8529 {
8530 SAVE_IT (atpos_it, *it, atpos_data);
8531 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8532 }
8533 }
8534 else
8535 {
8536 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8537 {
8538 it->current_x = x;
8539 result = MOVE_X_REACHED;
8540 break;
8541 }
8542 if (atx_it.sp < 0)
8543 {
8544 SAVE_IT (atx_it, *it, atx_data);
8545 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8546 }
8547 }
8548 }
8549
8550 if (/* Lines are continued. */
8551 it->line_wrap != TRUNCATE
8552 && (/* And glyph doesn't fit on the line. */
8553 new_x > it->last_visible_x
8554 /* Or it fits exactly and we're on a window
8555 system frame. */
8556 || (new_x == it->last_visible_x
8557 && FRAME_WINDOW_P (it->f)
8558 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8559 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8560 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8561 {
8562 if (/* IT->hpos == 0 means the very first glyph
8563 doesn't fit on the line, e.g. a wide image. */
8564 it->hpos == 0
8565 || (new_x == it->last_visible_x
8566 && FRAME_WINDOW_P (it->f)))
8567 {
8568 ++it->hpos;
8569 it->current_x = new_x;
8570
8571 /* The character's last glyph just barely fits
8572 in this row. */
8573 if (i == it->nglyphs - 1)
8574 {
8575 /* If this is the destination position,
8576 return a position *before* it in this row,
8577 now that we know it fits in this row. */
8578 if (BUFFER_POS_REACHED_P ())
8579 {
8580 if (it->line_wrap != WORD_WRAP
8581 || wrap_it.sp < 0)
8582 {
8583 it->hpos = hpos_before_this_char;
8584 it->current_x = x_before_this_char;
8585 result = MOVE_POS_MATCH_OR_ZV;
8586 break;
8587 }
8588 if (it->line_wrap == WORD_WRAP
8589 && atpos_it.sp < 0)
8590 {
8591 SAVE_IT (atpos_it, *it, atpos_data);
8592 atpos_it.current_x = x_before_this_char;
8593 atpos_it.hpos = hpos_before_this_char;
8594 }
8595 }
8596
8597 prev_method = it->method;
8598 if (it->method == GET_FROM_BUFFER)
8599 prev_pos = IT_CHARPOS (*it);
8600 set_iterator_to_next (it, 1);
8601 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8602 SET_TEXT_POS (this_line_min_pos,
8603 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8604 /* On graphical terminals, newlines may
8605 "overflow" into the fringe if
8606 overflow-newline-into-fringe is non-nil.
8607 On text terminals, and on graphical
8608 terminals with no right margin, newlines
8609 may overflow into the last glyph on the
8610 display line.*/
8611 if (!FRAME_WINDOW_P (it->f)
8612 || ((it->bidi_p
8613 && it->bidi_it.paragraph_dir == R2L)
8614 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8615 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8616 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8617 {
8618 if (!get_next_display_element (it))
8619 {
8620 result = MOVE_POS_MATCH_OR_ZV;
8621 break;
8622 }
8623 if (BUFFER_POS_REACHED_P ())
8624 {
8625 if (ITERATOR_AT_END_OF_LINE_P (it))
8626 result = MOVE_POS_MATCH_OR_ZV;
8627 else
8628 result = MOVE_LINE_CONTINUED;
8629 break;
8630 }
8631 if (ITERATOR_AT_END_OF_LINE_P (it)
8632 && (it->line_wrap != WORD_WRAP
8633 || wrap_it.sp < 0))
8634 {
8635 result = MOVE_NEWLINE_OR_CR;
8636 break;
8637 }
8638 }
8639 }
8640 }
8641 else
8642 IT_RESET_X_ASCENT_DESCENT (it);
8643
8644 if (wrap_it.sp >= 0)
8645 {
8646 RESTORE_IT (it, &wrap_it, wrap_data);
8647 atpos_it.sp = -1;
8648 atx_it.sp = -1;
8649 }
8650
8651 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8652 IT_CHARPOS (*it)));
8653 result = MOVE_LINE_CONTINUED;
8654 break;
8655 }
8656
8657 if (BUFFER_POS_REACHED_P ())
8658 {
8659 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8660 goto buffer_pos_reached;
8661 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8662 {
8663 SAVE_IT (atpos_it, *it, atpos_data);
8664 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8665 }
8666 }
8667
8668 if (new_x > it->first_visible_x)
8669 {
8670 /* Glyph is visible. Increment number of glyphs that
8671 would be displayed. */
8672 ++it->hpos;
8673 }
8674 }
8675
8676 if (result != MOVE_UNDEFINED)
8677 break;
8678 }
8679 else if (BUFFER_POS_REACHED_P ())
8680 {
8681 buffer_pos_reached:
8682 IT_RESET_X_ASCENT_DESCENT (it);
8683 result = MOVE_POS_MATCH_OR_ZV;
8684 break;
8685 }
8686 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8687 {
8688 /* Stop when TO_X specified and reached. This check is
8689 necessary here because of lines consisting of a line end,
8690 only. The line end will not produce any glyphs and we
8691 would never get MOVE_X_REACHED. */
8692 eassert (it->nglyphs == 0);
8693 result = MOVE_X_REACHED;
8694 break;
8695 }
8696
8697 /* Is this a line end? If yes, we're done. */
8698 if (ITERATOR_AT_END_OF_LINE_P (it))
8699 {
8700 /* If we are past TO_CHARPOS, but never saw any character
8701 positions smaller than TO_CHARPOS, return
8702 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8703 did. */
8704 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8705 {
8706 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8707 {
8708 if (IT_CHARPOS (ppos_it) < ZV)
8709 {
8710 RESTORE_IT (it, &ppos_it, ppos_data);
8711 result = MOVE_POS_MATCH_OR_ZV;
8712 }
8713 else
8714 goto buffer_pos_reached;
8715 }
8716 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8717 && IT_CHARPOS (*it) > to_charpos)
8718 goto buffer_pos_reached;
8719 else
8720 result = MOVE_NEWLINE_OR_CR;
8721 }
8722 else
8723 result = MOVE_NEWLINE_OR_CR;
8724 break;
8725 }
8726
8727 prev_method = it->method;
8728 if (it->method == GET_FROM_BUFFER)
8729 prev_pos = IT_CHARPOS (*it);
8730 /* The current display element has been consumed. Advance
8731 to the next. */
8732 set_iterator_to_next (it, 1);
8733 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8734 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8735 if (IT_CHARPOS (*it) < to_charpos)
8736 saw_smaller_pos = 1;
8737 if (it->bidi_p
8738 && (op & MOVE_TO_POS)
8739 && IT_CHARPOS (*it) >= to_charpos
8740 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8741 SAVE_IT (ppos_it, *it, ppos_data);
8742
8743 /* Stop if lines are truncated and IT's current x-position is
8744 past the right edge of the window now. */
8745 if (it->line_wrap == TRUNCATE
8746 && it->current_x >= it->last_visible_x)
8747 {
8748 if (!FRAME_WINDOW_P (it->f)
8749 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8750 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8751 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8752 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8753 {
8754 int at_eob_p = 0;
8755
8756 if ((at_eob_p = !get_next_display_element (it))
8757 || BUFFER_POS_REACHED_P ()
8758 /* If we are past TO_CHARPOS, but never saw any
8759 character positions smaller than TO_CHARPOS,
8760 return MOVE_POS_MATCH_OR_ZV, like the
8761 unidirectional display did. */
8762 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8763 && !saw_smaller_pos
8764 && IT_CHARPOS (*it) > to_charpos))
8765 {
8766 if (it->bidi_p
8767 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8768 RESTORE_IT (it, &ppos_it, ppos_data);
8769 result = MOVE_POS_MATCH_OR_ZV;
8770 break;
8771 }
8772 if (ITERATOR_AT_END_OF_LINE_P (it))
8773 {
8774 result = MOVE_NEWLINE_OR_CR;
8775 break;
8776 }
8777 }
8778 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8779 && !saw_smaller_pos
8780 && IT_CHARPOS (*it) > to_charpos)
8781 {
8782 if (IT_CHARPOS (ppos_it) < ZV)
8783 RESTORE_IT (it, &ppos_it, ppos_data);
8784 result = MOVE_POS_MATCH_OR_ZV;
8785 break;
8786 }
8787 result = MOVE_LINE_TRUNCATED;
8788 break;
8789 }
8790 #undef IT_RESET_X_ASCENT_DESCENT
8791 }
8792
8793 #undef BUFFER_POS_REACHED_P
8794
8795 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8796 restore the saved iterator. */
8797 if (atpos_it.sp >= 0)
8798 RESTORE_IT (it, &atpos_it, atpos_data);
8799 else if (atx_it.sp >= 0)
8800 RESTORE_IT (it, &atx_it, atx_data);
8801
8802 done:
8803
8804 if (atpos_data)
8805 bidi_unshelve_cache (atpos_data, 1);
8806 if (atx_data)
8807 bidi_unshelve_cache (atx_data, 1);
8808 if (wrap_data)
8809 bidi_unshelve_cache (wrap_data, 1);
8810 if (ppos_data)
8811 bidi_unshelve_cache (ppos_data, 1);
8812
8813 /* Restore the iterator settings altered at the beginning of this
8814 function. */
8815 it->glyph_row = saved_glyph_row;
8816 return result;
8817 }
8818
8819 /* For external use. */
8820 void
8821 move_it_in_display_line (struct it *it,
8822 ptrdiff_t to_charpos, int to_x,
8823 enum move_operation_enum op)
8824 {
8825 if (it->line_wrap == WORD_WRAP
8826 && (op & MOVE_TO_X))
8827 {
8828 struct it save_it;
8829 void *save_data = NULL;
8830 int skip;
8831
8832 SAVE_IT (save_it, *it, save_data);
8833 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8834 /* When word-wrap is on, TO_X may lie past the end
8835 of a wrapped line. Then it->current is the
8836 character on the next line, so backtrack to the
8837 space before the wrap point. */
8838 if (skip == MOVE_LINE_CONTINUED)
8839 {
8840 int prev_x = max (it->current_x - 1, 0);
8841 RESTORE_IT (it, &save_it, save_data);
8842 move_it_in_display_line_to
8843 (it, -1, prev_x, MOVE_TO_X);
8844 }
8845 else
8846 bidi_unshelve_cache (save_data, 1);
8847 }
8848 else
8849 move_it_in_display_line_to (it, to_charpos, to_x, op);
8850 }
8851
8852
8853 /* Move IT forward until it satisfies one or more of the criteria in
8854 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8855
8856 OP is a bit-mask that specifies where to stop, and in particular,
8857 which of those four position arguments makes a difference. See the
8858 description of enum move_operation_enum.
8859
8860 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8861 screen line, this function will set IT to the next position that is
8862 displayed to the right of TO_CHARPOS on the screen. */
8863
8864 void
8865 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8866 {
8867 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8868 int line_height, line_start_x = 0, reached = 0;
8869 void *backup_data = NULL;
8870
8871 for (;;)
8872 {
8873 if (op & MOVE_TO_VPOS)
8874 {
8875 /* If no TO_CHARPOS and no TO_X specified, stop at the
8876 start of the line TO_VPOS. */
8877 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8878 {
8879 if (it->vpos == to_vpos)
8880 {
8881 reached = 1;
8882 break;
8883 }
8884 else
8885 skip = move_it_in_display_line_to (it, -1, -1, 0);
8886 }
8887 else
8888 {
8889 /* TO_VPOS >= 0 means stop at TO_X in the line at
8890 TO_VPOS, or at TO_POS, whichever comes first. */
8891 if (it->vpos == to_vpos)
8892 {
8893 reached = 2;
8894 break;
8895 }
8896
8897 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8898
8899 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8900 {
8901 reached = 3;
8902 break;
8903 }
8904 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8905 {
8906 /* We have reached TO_X but not in the line we want. */
8907 skip = move_it_in_display_line_to (it, to_charpos,
8908 -1, MOVE_TO_POS);
8909 if (skip == MOVE_POS_MATCH_OR_ZV)
8910 {
8911 reached = 4;
8912 break;
8913 }
8914 }
8915 }
8916 }
8917 else if (op & MOVE_TO_Y)
8918 {
8919 struct it it_backup;
8920
8921 if (it->line_wrap == WORD_WRAP)
8922 SAVE_IT (it_backup, *it, backup_data);
8923
8924 /* TO_Y specified means stop at TO_X in the line containing
8925 TO_Y---or at TO_CHARPOS if this is reached first. The
8926 problem is that we can't really tell whether the line
8927 contains TO_Y before we have completely scanned it, and
8928 this may skip past TO_X. What we do is to first scan to
8929 TO_X.
8930
8931 If TO_X is not specified, use a TO_X of zero. The reason
8932 is to make the outcome of this function more predictable.
8933 If we didn't use TO_X == 0, we would stop at the end of
8934 the line which is probably not what a caller would expect
8935 to happen. */
8936 skip = move_it_in_display_line_to
8937 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8938 (MOVE_TO_X | (op & MOVE_TO_POS)));
8939
8940 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8941 if (skip == MOVE_POS_MATCH_OR_ZV)
8942 reached = 5;
8943 else if (skip == MOVE_X_REACHED)
8944 {
8945 /* If TO_X was reached, we want to know whether TO_Y is
8946 in the line. We know this is the case if the already
8947 scanned glyphs make the line tall enough. Otherwise,
8948 we must check by scanning the rest of the line. */
8949 line_height = it->max_ascent + it->max_descent;
8950 if (to_y >= it->current_y
8951 && to_y < it->current_y + line_height)
8952 {
8953 reached = 6;
8954 break;
8955 }
8956 SAVE_IT (it_backup, *it, backup_data);
8957 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8958 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8959 op & MOVE_TO_POS);
8960 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8961 line_height = it->max_ascent + it->max_descent;
8962 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8963
8964 if (to_y >= it->current_y
8965 && to_y < it->current_y + line_height)
8966 {
8967 /* If TO_Y is in this line and TO_X was reached
8968 above, we scanned too far. We have to restore
8969 IT's settings to the ones before skipping. But
8970 keep the more accurate values of max_ascent and
8971 max_descent we've found while skipping the rest
8972 of the line, for the sake of callers, such as
8973 pos_visible_p, that need to know the line
8974 height. */
8975 int max_ascent = it->max_ascent;
8976 int max_descent = it->max_descent;
8977
8978 RESTORE_IT (it, &it_backup, backup_data);
8979 it->max_ascent = max_ascent;
8980 it->max_descent = max_descent;
8981 reached = 6;
8982 }
8983 else
8984 {
8985 skip = skip2;
8986 if (skip == MOVE_POS_MATCH_OR_ZV)
8987 reached = 7;
8988 }
8989 }
8990 else
8991 {
8992 /* Check whether TO_Y is in this line. */
8993 line_height = it->max_ascent + it->max_descent;
8994 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8995
8996 if (to_y >= it->current_y
8997 && to_y < it->current_y + line_height)
8998 {
8999 /* When word-wrap is on, TO_X may lie past the end
9000 of a wrapped line. Then it->current is the
9001 character on the next line, so backtrack to the
9002 space before the wrap point. */
9003 if (skip == MOVE_LINE_CONTINUED
9004 && it->line_wrap == WORD_WRAP)
9005 {
9006 int prev_x = max (it->current_x - 1, 0);
9007 RESTORE_IT (it, &it_backup, backup_data);
9008 skip = move_it_in_display_line_to
9009 (it, -1, prev_x, MOVE_TO_X);
9010 }
9011 reached = 6;
9012 }
9013 }
9014
9015 if (reached)
9016 break;
9017 }
9018 else if (BUFFERP (it->object)
9019 && (it->method == GET_FROM_BUFFER
9020 || it->method == GET_FROM_STRETCH)
9021 && IT_CHARPOS (*it) >= to_charpos
9022 /* Under bidi iteration, a call to set_iterator_to_next
9023 can scan far beyond to_charpos if the initial
9024 portion of the next line needs to be reordered. In
9025 that case, give move_it_in_display_line_to another
9026 chance below. */
9027 && !(it->bidi_p
9028 && it->bidi_it.scan_dir == -1))
9029 skip = MOVE_POS_MATCH_OR_ZV;
9030 else
9031 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9032
9033 switch (skip)
9034 {
9035 case MOVE_POS_MATCH_OR_ZV:
9036 reached = 8;
9037 goto out;
9038
9039 case MOVE_NEWLINE_OR_CR:
9040 set_iterator_to_next (it, 1);
9041 it->continuation_lines_width = 0;
9042 break;
9043
9044 case MOVE_LINE_TRUNCATED:
9045 it->continuation_lines_width = 0;
9046 reseat_at_next_visible_line_start (it, 0);
9047 if ((op & MOVE_TO_POS) != 0
9048 && IT_CHARPOS (*it) > to_charpos)
9049 {
9050 reached = 9;
9051 goto out;
9052 }
9053 break;
9054
9055 case MOVE_LINE_CONTINUED:
9056 /* For continued lines ending in a tab, some of the glyphs
9057 associated with the tab are displayed on the current
9058 line. Since it->current_x does not include these glyphs,
9059 we use it->last_visible_x instead. */
9060 if (it->c == '\t')
9061 {
9062 it->continuation_lines_width += it->last_visible_x;
9063 /* When moving by vpos, ensure that the iterator really
9064 advances to the next line (bug#847, bug#969). Fixme:
9065 do we need to do this in other circumstances? */
9066 if (it->current_x != it->last_visible_x
9067 && (op & MOVE_TO_VPOS)
9068 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9069 {
9070 line_start_x = it->current_x + it->pixel_width
9071 - it->last_visible_x;
9072 set_iterator_to_next (it, 0);
9073 }
9074 }
9075 else
9076 it->continuation_lines_width += it->current_x;
9077 break;
9078
9079 default:
9080 emacs_abort ();
9081 }
9082
9083 /* Reset/increment for the next run. */
9084 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9085 it->current_x = line_start_x;
9086 line_start_x = 0;
9087 it->hpos = 0;
9088 it->current_y += it->max_ascent + it->max_descent;
9089 ++it->vpos;
9090 last_height = it->max_ascent + it->max_descent;
9091 it->max_ascent = it->max_descent = 0;
9092 }
9093
9094 out:
9095
9096 /* On text terminals, we may stop at the end of a line in the middle
9097 of a multi-character glyph. If the glyph itself is continued,
9098 i.e. it is actually displayed on the next line, don't treat this
9099 stopping point as valid; move to the next line instead (unless
9100 that brings us offscreen). */
9101 if (!FRAME_WINDOW_P (it->f)
9102 && op & MOVE_TO_POS
9103 && IT_CHARPOS (*it) == to_charpos
9104 && it->what == IT_CHARACTER
9105 && it->nglyphs > 1
9106 && it->line_wrap == WINDOW_WRAP
9107 && it->current_x == it->last_visible_x - 1
9108 && it->c != '\n'
9109 && it->c != '\t'
9110 && it->vpos < it->w->window_end_vpos)
9111 {
9112 it->continuation_lines_width += it->current_x;
9113 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9114 it->current_y += it->max_ascent + it->max_descent;
9115 ++it->vpos;
9116 last_height = it->max_ascent + it->max_descent;
9117 }
9118
9119 if (backup_data)
9120 bidi_unshelve_cache (backup_data, 1);
9121
9122 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9123 }
9124
9125
9126 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9127
9128 If DY > 0, move IT backward at least that many pixels. DY = 0
9129 means move IT backward to the preceding line start or BEGV. This
9130 function may move over more than DY pixels if IT->current_y - DY
9131 ends up in the middle of a line; in this case IT->current_y will be
9132 set to the top of the line moved to. */
9133
9134 void
9135 move_it_vertically_backward (struct it *it, int dy)
9136 {
9137 int nlines, h;
9138 struct it it2, it3;
9139 void *it2data = NULL, *it3data = NULL;
9140 ptrdiff_t start_pos;
9141 int nchars_per_row
9142 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9143 ptrdiff_t pos_limit;
9144
9145 move_further_back:
9146 eassert (dy >= 0);
9147
9148 start_pos = IT_CHARPOS (*it);
9149
9150 /* Estimate how many newlines we must move back. */
9151 nlines = max (1, dy / default_line_pixel_height (it->w));
9152 if (it->line_wrap == TRUNCATE)
9153 pos_limit = BEGV;
9154 else
9155 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9156
9157 /* Set the iterator's position that many lines back. But don't go
9158 back more than NLINES full screen lines -- this wins a day with
9159 buffers which have very long lines. */
9160 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9161 back_to_previous_visible_line_start (it);
9162
9163 /* Reseat the iterator here. When moving backward, we don't want
9164 reseat to skip forward over invisible text, set up the iterator
9165 to deliver from overlay strings at the new position etc. So,
9166 use reseat_1 here. */
9167 reseat_1 (it, it->current.pos, 1);
9168
9169 /* We are now surely at a line start. */
9170 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9171 reordering is in effect. */
9172 it->continuation_lines_width = 0;
9173
9174 /* Move forward and see what y-distance we moved. First move to the
9175 start of the next line so that we get its height. We need this
9176 height to be able to tell whether we reached the specified
9177 y-distance. */
9178 SAVE_IT (it2, *it, it2data);
9179 it2.max_ascent = it2.max_descent = 0;
9180 do
9181 {
9182 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9183 MOVE_TO_POS | MOVE_TO_VPOS);
9184 }
9185 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9186 /* If we are in a display string which starts at START_POS,
9187 and that display string includes a newline, and we are
9188 right after that newline (i.e. at the beginning of a
9189 display line), exit the loop, because otherwise we will
9190 infloop, since move_it_to will see that it is already at
9191 START_POS and will not move. */
9192 || (it2.method == GET_FROM_STRING
9193 && IT_CHARPOS (it2) == start_pos
9194 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9195 eassert (IT_CHARPOS (*it) >= BEGV);
9196 SAVE_IT (it3, it2, it3data);
9197
9198 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9199 eassert (IT_CHARPOS (*it) >= BEGV);
9200 /* H is the actual vertical distance from the position in *IT
9201 and the starting position. */
9202 h = it2.current_y - it->current_y;
9203 /* NLINES is the distance in number of lines. */
9204 nlines = it2.vpos - it->vpos;
9205
9206 /* Correct IT's y and vpos position
9207 so that they are relative to the starting point. */
9208 it->vpos -= nlines;
9209 it->current_y -= h;
9210
9211 if (dy == 0)
9212 {
9213 /* DY == 0 means move to the start of the screen line. The
9214 value of nlines is > 0 if continuation lines were involved,
9215 or if the original IT position was at start of a line. */
9216 RESTORE_IT (it, it, it2data);
9217 if (nlines > 0)
9218 move_it_by_lines (it, nlines);
9219 /* The above code moves us to some position NLINES down,
9220 usually to its first glyph (leftmost in an L2R line), but
9221 that's not necessarily the start of the line, under bidi
9222 reordering. We want to get to the character position
9223 that is immediately after the newline of the previous
9224 line. */
9225 if (it->bidi_p
9226 && !it->continuation_lines_width
9227 && !STRINGP (it->string)
9228 && IT_CHARPOS (*it) > BEGV
9229 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9230 {
9231 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9232
9233 DEC_BOTH (cp, bp);
9234 cp = find_newline_no_quit (cp, bp, -1, NULL);
9235 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9236 }
9237 bidi_unshelve_cache (it3data, 1);
9238 }
9239 else
9240 {
9241 /* The y-position we try to reach, relative to *IT.
9242 Note that H has been subtracted in front of the if-statement. */
9243 int target_y = it->current_y + h - dy;
9244 int y0 = it3.current_y;
9245 int y1;
9246 int line_height;
9247
9248 RESTORE_IT (&it3, &it3, it3data);
9249 y1 = line_bottom_y (&it3);
9250 line_height = y1 - y0;
9251 RESTORE_IT (it, it, it2data);
9252 /* If we did not reach target_y, try to move further backward if
9253 we can. If we moved too far backward, try to move forward. */
9254 if (target_y < it->current_y
9255 /* This is heuristic. In a window that's 3 lines high, with
9256 a line height of 13 pixels each, recentering with point
9257 on the bottom line will try to move -39/2 = 19 pixels
9258 backward. Try to avoid moving into the first line. */
9259 && (it->current_y - target_y
9260 > min (window_box_height (it->w), line_height * 2 / 3))
9261 && IT_CHARPOS (*it) > BEGV)
9262 {
9263 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9264 target_y - it->current_y));
9265 dy = it->current_y - target_y;
9266 goto move_further_back;
9267 }
9268 else if (target_y >= it->current_y + line_height
9269 && IT_CHARPOS (*it) < ZV)
9270 {
9271 /* Should move forward by at least one line, maybe more.
9272
9273 Note: Calling move_it_by_lines can be expensive on
9274 terminal frames, where compute_motion is used (via
9275 vmotion) to do the job, when there are very long lines
9276 and truncate-lines is nil. That's the reason for
9277 treating terminal frames specially here. */
9278
9279 if (!FRAME_WINDOW_P (it->f))
9280 move_it_vertically (it, target_y - (it->current_y + line_height));
9281 else
9282 {
9283 do
9284 {
9285 move_it_by_lines (it, 1);
9286 }
9287 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9288 }
9289 }
9290 }
9291 }
9292
9293
9294 /* Move IT by a specified amount of pixel lines DY. DY negative means
9295 move backwards. DY = 0 means move to start of screen line. At the
9296 end, IT will be on the start of a screen line. */
9297
9298 void
9299 move_it_vertically (struct it *it, int dy)
9300 {
9301 if (dy <= 0)
9302 move_it_vertically_backward (it, -dy);
9303 else
9304 {
9305 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9306 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9307 MOVE_TO_POS | MOVE_TO_Y);
9308 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9309
9310 /* If buffer ends in ZV without a newline, move to the start of
9311 the line to satisfy the post-condition. */
9312 if (IT_CHARPOS (*it) == ZV
9313 && ZV > BEGV
9314 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9315 move_it_by_lines (it, 0);
9316 }
9317 }
9318
9319
9320 /* Move iterator IT past the end of the text line it is in. */
9321
9322 void
9323 move_it_past_eol (struct it *it)
9324 {
9325 enum move_it_result rc;
9326
9327 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9328 if (rc == MOVE_NEWLINE_OR_CR)
9329 set_iterator_to_next (it, 0);
9330 }
9331
9332
9333 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9334 negative means move up. DVPOS == 0 means move to the start of the
9335 screen line.
9336
9337 Optimization idea: If we would know that IT->f doesn't use
9338 a face with proportional font, we could be faster for
9339 truncate-lines nil. */
9340
9341 void
9342 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9343 {
9344
9345 /* The commented-out optimization uses vmotion on terminals. This
9346 gives bad results, because elements like it->what, on which
9347 callers such as pos_visible_p rely, aren't updated. */
9348 /* struct position pos;
9349 if (!FRAME_WINDOW_P (it->f))
9350 {
9351 struct text_pos textpos;
9352
9353 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9354 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9355 reseat (it, textpos, 1);
9356 it->vpos += pos.vpos;
9357 it->current_y += pos.vpos;
9358 }
9359 else */
9360
9361 if (dvpos == 0)
9362 {
9363 /* DVPOS == 0 means move to the start of the screen line. */
9364 move_it_vertically_backward (it, 0);
9365 /* Let next call to line_bottom_y calculate real line height */
9366 last_height = 0;
9367 }
9368 else if (dvpos > 0)
9369 {
9370 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9371 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9372 {
9373 /* Only move to the next buffer position if we ended up in a
9374 string from display property, not in an overlay string
9375 (before-string or after-string). That is because the
9376 latter don't conceal the underlying buffer position, so
9377 we can ask to move the iterator to the exact position we
9378 are interested in. Note that, even if we are already at
9379 IT_CHARPOS (*it), the call below is not a no-op, as it
9380 will detect that we are at the end of the string, pop the
9381 iterator, and compute it->current_x and it->hpos
9382 correctly. */
9383 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9384 -1, -1, -1, MOVE_TO_POS);
9385 }
9386 }
9387 else
9388 {
9389 struct it it2;
9390 void *it2data = NULL;
9391 ptrdiff_t start_charpos, i;
9392 int nchars_per_row
9393 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9394 ptrdiff_t pos_limit;
9395
9396 /* Start at the beginning of the screen line containing IT's
9397 position. This may actually move vertically backwards,
9398 in case of overlays, so adjust dvpos accordingly. */
9399 dvpos += it->vpos;
9400 move_it_vertically_backward (it, 0);
9401 dvpos -= it->vpos;
9402
9403 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9404 screen lines, and reseat the iterator there. */
9405 start_charpos = IT_CHARPOS (*it);
9406 if (it->line_wrap == TRUNCATE)
9407 pos_limit = BEGV;
9408 else
9409 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9410 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9411 back_to_previous_visible_line_start (it);
9412 reseat (it, it->current.pos, 1);
9413
9414 /* Move further back if we end up in a string or an image. */
9415 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9416 {
9417 /* First try to move to start of display line. */
9418 dvpos += it->vpos;
9419 move_it_vertically_backward (it, 0);
9420 dvpos -= it->vpos;
9421 if (IT_POS_VALID_AFTER_MOVE_P (it))
9422 break;
9423 /* If start of line is still in string or image,
9424 move further back. */
9425 back_to_previous_visible_line_start (it);
9426 reseat (it, it->current.pos, 1);
9427 dvpos--;
9428 }
9429
9430 it->current_x = it->hpos = 0;
9431
9432 /* Above call may have moved too far if continuation lines
9433 are involved. Scan forward and see if it did. */
9434 SAVE_IT (it2, *it, it2data);
9435 it2.vpos = it2.current_y = 0;
9436 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9437 it->vpos -= it2.vpos;
9438 it->current_y -= it2.current_y;
9439 it->current_x = it->hpos = 0;
9440
9441 /* If we moved too far back, move IT some lines forward. */
9442 if (it2.vpos > -dvpos)
9443 {
9444 int delta = it2.vpos + dvpos;
9445
9446 RESTORE_IT (&it2, &it2, it2data);
9447 SAVE_IT (it2, *it, it2data);
9448 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9449 /* Move back again if we got too far ahead. */
9450 if (IT_CHARPOS (*it) >= start_charpos)
9451 RESTORE_IT (it, &it2, it2data);
9452 else
9453 bidi_unshelve_cache (it2data, 1);
9454 }
9455 else
9456 RESTORE_IT (it, it, it2data);
9457 }
9458 }
9459
9460 /* Return 1 if IT points into the middle of a display vector. */
9461
9462 int
9463 in_display_vector_p (struct it *it)
9464 {
9465 return (it->method == GET_FROM_DISPLAY_VECTOR
9466 && it->current.dpvec_index > 0
9467 && it->dpvec + it->current.dpvec_index != it->dpend);
9468 }
9469
9470 \f
9471 /***********************************************************************
9472 Messages
9473 ***********************************************************************/
9474
9475
9476 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9477 to *Messages*. */
9478
9479 void
9480 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9481 {
9482 Lisp_Object args[3];
9483 Lisp_Object msg, fmt;
9484 char *buffer;
9485 ptrdiff_t len;
9486 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9487 USE_SAFE_ALLOCA;
9488
9489 fmt = msg = Qnil;
9490 GCPRO4 (fmt, msg, arg1, arg2);
9491
9492 args[0] = fmt = build_string (format);
9493 args[1] = arg1;
9494 args[2] = arg2;
9495 msg = Fformat (3, args);
9496
9497 len = SBYTES (msg) + 1;
9498 buffer = SAFE_ALLOCA (len);
9499 memcpy (buffer, SDATA (msg), len);
9500
9501 message_dolog (buffer, len - 1, 1, 0);
9502 SAFE_FREE ();
9503
9504 UNGCPRO;
9505 }
9506
9507
9508 /* Output a newline in the *Messages* buffer if "needs" one. */
9509
9510 void
9511 message_log_maybe_newline (void)
9512 {
9513 if (message_log_need_newline)
9514 message_dolog ("", 0, 1, 0);
9515 }
9516
9517
9518 /* Add a string M of length NBYTES to the message log, optionally
9519 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9520 true, means interpret the contents of M as multibyte. This
9521 function calls low-level routines in order to bypass text property
9522 hooks, etc. which might not be safe to run.
9523
9524 This may GC (insert may run before/after change hooks),
9525 so the buffer M must NOT point to a Lisp string. */
9526
9527 void
9528 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9529 {
9530 const unsigned char *msg = (const unsigned char *) m;
9531
9532 if (!NILP (Vmemory_full))
9533 return;
9534
9535 if (!NILP (Vmessage_log_max))
9536 {
9537 struct buffer *oldbuf;
9538 Lisp_Object oldpoint, oldbegv, oldzv;
9539 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9540 ptrdiff_t point_at_end = 0;
9541 ptrdiff_t zv_at_end = 0;
9542 Lisp_Object old_deactivate_mark;
9543 bool shown;
9544 struct gcpro gcpro1;
9545
9546 old_deactivate_mark = Vdeactivate_mark;
9547 oldbuf = current_buffer;
9548 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9549 bset_undo_list (current_buffer, Qt);
9550
9551 oldpoint = message_dolog_marker1;
9552 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9553 oldbegv = message_dolog_marker2;
9554 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9555 oldzv = message_dolog_marker3;
9556 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9557 GCPRO1 (old_deactivate_mark);
9558
9559 if (PT == Z)
9560 point_at_end = 1;
9561 if (ZV == Z)
9562 zv_at_end = 1;
9563
9564 BEGV = BEG;
9565 BEGV_BYTE = BEG_BYTE;
9566 ZV = Z;
9567 ZV_BYTE = Z_BYTE;
9568 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9569
9570 /* Insert the string--maybe converting multibyte to single byte
9571 or vice versa, so that all the text fits the buffer. */
9572 if (multibyte
9573 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9574 {
9575 ptrdiff_t i;
9576 int c, char_bytes;
9577 char work[1];
9578
9579 /* Convert a multibyte string to single-byte
9580 for the *Message* buffer. */
9581 for (i = 0; i < nbytes; i += char_bytes)
9582 {
9583 c = string_char_and_length (msg + i, &char_bytes);
9584 work[0] = (ASCII_CHAR_P (c)
9585 ? c
9586 : multibyte_char_to_unibyte (c));
9587 insert_1_both (work, 1, 1, 1, 0, 0);
9588 }
9589 }
9590 else if (! multibyte
9591 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9592 {
9593 ptrdiff_t i;
9594 int c, char_bytes;
9595 unsigned char str[MAX_MULTIBYTE_LENGTH];
9596 /* Convert a single-byte string to multibyte
9597 for the *Message* buffer. */
9598 for (i = 0; i < nbytes; i++)
9599 {
9600 c = msg[i];
9601 MAKE_CHAR_MULTIBYTE (c);
9602 char_bytes = CHAR_STRING (c, str);
9603 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9604 }
9605 }
9606 else if (nbytes)
9607 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9608
9609 if (nlflag)
9610 {
9611 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9612 printmax_t dups;
9613
9614 insert_1_both ("\n", 1, 1, 1, 0, 0);
9615
9616 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9617 this_bol = PT;
9618 this_bol_byte = PT_BYTE;
9619
9620 /* See if this line duplicates the previous one.
9621 If so, combine duplicates. */
9622 if (this_bol > BEG)
9623 {
9624 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9625 prev_bol = PT;
9626 prev_bol_byte = PT_BYTE;
9627
9628 dups = message_log_check_duplicate (prev_bol_byte,
9629 this_bol_byte);
9630 if (dups)
9631 {
9632 del_range_both (prev_bol, prev_bol_byte,
9633 this_bol, this_bol_byte, 0);
9634 if (dups > 1)
9635 {
9636 char dupstr[sizeof " [ times]"
9637 + INT_STRLEN_BOUND (printmax_t)];
9638
9639 /* If you change this format, don't forget to also
9640 change message_log_check_duplicate. */
9641 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9642 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9643 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9644 }
9645 }
9646 }
9647
9648 /* If we have more than the desired maximum number of lines
9649 in the *Messages* buffer now, delete the oldest ones.
9650 This is safe because we don't have undo in this buffer. */
9651
9652 if (NATNUMP (Vmessage_log_max))
9653 {
9654 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9655 -XFASTINT (Vmessage_log_max) - 1, 0);
9656 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9657 }
9658 }
9659 BEGV = marker_position (oldbegv);
9660 BEGV_BYTE = marker_byte_position (oldbegv);
9661
9662 if (zv_at_end)
9663 {
9664 ZV = Z;
9665 ZV_BYTE = Z_BYTE;
9666 }
9667 else
9668 {
9669 ZV = marker_position (oldzv);
9670 ZV_BYTE = marker_byte_position (oldzv);
9671 }
9672
9673 if (point_at_end)
9674 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9675 else
9676 /* We can't do Fgoto_char (oldpoint) because it will run some
9677 Lisp code. */
9678 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9679 marker_byte_position (oldpoint));
9680
9681 UNGCPRO;
9682 unchain_marker (XMARKER (oldpoint));
9683 unchain_marker (XMARKER (oldbegv));
9684 unchain_marker (XMARKER (oldzv));
9685
9686 shown = buffer_window_count (current_buffer) > 0;
9687 set_buffer_internal (oldbuf);
9688 /* We called insert_1_both above with its 5th argument (PREPARE)
9689 zero, which prevents insert_1_both from calling
9690 prepare_to_modify_buffer, which in turns prevents us from
9691 incrementing windows_or_buffers_changed even if *Messages* is
9692 shown in some window. So we must manually incrementing
9693 windows_or_buffers_changed here to make up for that. */
9694 if (shown)
9695 windows_or_buffers_changed++;
9696 else
9697 windows_or_buffers_changed = old_windows_or_buffers_changed;
9698 message_log_need_newline = !nlflag;
9699 Vdeactivate_mark = old_deactivate_mark;
9700 }
9701 }
9702
9703
9704 /* We are at the end of the buffer after just having inserted a newline.
9705 (Note: We depend on the fact we won't be crossing the gap.)
9706 Check to see if the most recent message looks a lot like the previous one.
9707 Return 0 if different, 1 if the new one should just replace it, or a
9708 value N > 1 if we should also append " [N times]". */
9709
9710 static intmax_t
9711 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9712 {
9713 ptrdiff_t i;
9714 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9715 int seen_dots = 0;
9716 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9717 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9718
9719 for (i = 0; i < len; i++)
9720 {
9721 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
9722 seen_dots = 1;
9723 if (p1[i] != p2[i])
9724 return seen_dots;
9725 }
9726 p1 += len;
9727 if (*p1 == '\n')
9728 return 2;
9729 if (*p1++ == ' ' && *p1++ == '[')
9730 {
9731 char *pend;
9732 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9733 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9734 return n + 1;
9735 }
9736 return 0;
9737 }
9738 \f
9739
9740 /* Display an echo area message M with a specified length of NBYTES
9741 bytes. The string may include null characters. If M is not a
9742 string, clear out any existing message, and let the mini-buffer
9743 text show through.
9744
9745 This function cancels echoing. */
9746
9747 void
9748 message3 (Lisp_Object m)
9749 {
9750 struct gcpro gcpro1;
9751
9752 GCPRO1 (m);
9753 clear_message (1,1);
9754 cancel_echoing ();
9755
9756 /* First flush out any partial line written with print. */
9757 message_log_maybe_newline ();
9758 if (STRINGP (m))
9759 {
9760 ptrdiff_t nbytes = SBYTES (m);
9761 bool multibyte = STRING_MULTIBYTE (m);
9762 USE_SAFE_ALLOCA;
9763 char *buffer = SAFE_ALLOCA (nbytes);
9764 memcpy (buffer, SDATA (m), nbytes);
9765 message_dolog (buffer, nbytes, 1, multibyte);
9766 SAFE_FREE ();
9767 }
9768 message3_nolog (m);
9769
9770 UNGCPRO;
9771 }
9772
9773
9774 /* The non-logging version of message3.
9775 This does not cancel echoing, because it is used for echoing.
9776 Perhaps we need to make a separate function for echoing
9777 and make this cancel echoing. */
9778
9779 void
9780 message3_nolog (Lisp_Object m)
9781 {
9782 struct frame *sf = SELECTED_FRAME ();
9783
9784 if (FRAME_INITIAL_P (sf))
9785 {
9786 if (noninteractive_need_newline)
9787 putc ('\n', stderr);
9788 noninteractive_need_newline = 0;
9789 if (STRINGP (m))
9790 fwrite (SDATA (m), SBYTES (m), 1, stderr);
9791 if (cursor_in_echo_area == 0)
9792 fprintf (stderr, "\n");
9793 fflush (stderr);
9794 }
9795 /* Error messages get reported properly by cmd_error, so this must be just an
9796 informative message; if the frame hasn't really been initialized yet, just
9797 toss it. */
9798 else if (INTERACTIVE && sf->glyphs_initialized_p)
9799 {
9800 /* Get the frame containing the mini-buffer
9801 that the selected frame is using. */
9802 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
9803 Lisp_Object frame = XWINDOW (mini_window)->frame;
9804 struct frame *f = XFRAME (frame);
9805
9806 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
9807 Fmake_frame_visible (frame);
9808
9809 if (STRINGP (m) && SCHARS (m) > 0)
9810 {
9811 set_message (m);
9812 if (minibuffer_auto_raise)
9813 Fraise_frame (frame);
9814 /* Assume we are not echoing.
9815 (If we are, echo_now will override this.) */
9816 echo_message_buffer = Qnil;
9817 }
9818 else
9819 clear_message (1, 1);
9820
9821 do_pending_window_change (0);
9822 echo_area_display (1);
9823 do_pending_window_change (0);
9824 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9825 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9826 }
9827 }
9828
9829
9830 /* Display a null-terminated echo area message M. If M is 0, clear
9831 out any existing message, and let the mini-buffer text show through.
9832
9833 The buffer M must continue to exist until after the echo area gets
9834 cleared or some other message gets displayed there. Do not pass
9835 text that is stored in a Lisp string. Do not pass text in a buffer
9836 that was alloca'd. */
9837
9838 void
9839 message1 (const char *m)
9840 {
9841 message3 (m ? build_unibyte_string (m) : Qnil);
9842 }
9843
9844
9845 /* The non-logging counterpart of message1. */
9846
9847 void
9848 message1_nolog (const char *m)
9849 {
9850 message3_nolog (m ? build_unibyte_string (m) : Qnil);
9851 }
9852
9853 /* Display a message M which contains a single %s
9854 which gets replaced with STRING. */
9855
9856 void
9857 message_with_string (const char *m, Lisp_Object string, int log)
9858 {
9859 CHECK_STRING (string);
9860
9861 if (noninteractive)
9862 {
9863 if (m)
9864 {
9865 if (noninteractive_need_newline)
9866 putc ('\n', stderr);
9867 noninteractive_need_newline = 0;
9868 fprintf (stderr, m, SDATA (string));
9869 if (!cursor_in_echo_area)
9870 fprintf (stderr, "\n");
9871 fflush (stderr);
9872 }
9873 }
9874 else if (INTERACTIVE)
9875 {
9876 /* The frame whose minibuffer we're going to display the message on.
9877 It may be larger than the selected frame, so we need
9878 to use its buffer, not the selected frame's buffer. */
9879 Lisp_Object mini_window;
9880 struct frame *f, *sf = SELECTED_FRAME ();
9881
9882 /* Get the frame containing the minibuffer
9883 that the selected frame is using. */
9884 mini_window = FRAME_MINIBUF_WINDOW (sf);
9885 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9886
9887 /* Error messages get reported properly by cmd_error, so this must be
9888 just an informative message; if the frame hasn't really been
9889 initialized yet, just toss it. */
9890 if (f->glyphs_initialized_p)
9891 {
9892 Lisp_Object args[2], msg;
9893 struct gcpro gcpro1, gcpro2;
9894
9895 args[0] = build_string (m);
9896 args[1] = msg = string;
9897 GCPRO2 (args[0], msg);
9898 gcpro1.nvars = 2;
9899
9900 msg = Fformat (2, args);
9901
9902 if (log)
9903 message3 (msg);
9904 else
9905 message3_nolog (msg);
9906
9907 UNGCPRO;
9908
9909 /* Print should start at the beginning of the message
9910 buffer next time. */
9911 message_buf_print = 0;
9912 }
9913 }
9914 }
9915
9916
9917 /* Dump an informative message to the minibuf. If M is 0, clear out
9918 any existing message, and let the mini-buffer text show through. */
9919
9920 static void
9921 vmessage (const char *m, va_list ap)
9922 {
9923 if (noninteractive)
9924 {
9925 if (m)
9926 {
9927 if (noninteractive_need_newline)
9928 putc ('\n', stderr);
9929 noninteractive_need_newline = 0;
9930 vfprintf (stderr, m, ap);
9931 if (cursor_in_echo_area == 0)
9932 fprintf (stderr, "\n");
9933 fflush (stderr);
9934 }
9935 }
9936 else if (INTERACTIVE)
9937 {
9938 /* The frame whose mini-buffer we're going to display the message
9939 on. It may be larger than the selected frame, so we need to
9940 use its buffer, not the selected frame's buffer. */
9941 Lisp_Object mini_window;
9942 struct frame *f, *sf = SELECTED_FRAME ();
9943
9944 /* Get the frame containing the mini-buffer
9945 that the selected frame is using. */
9946 mini_window = FRAME_MINIBUF_WINDOW (sf);
9947 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9948
9949 /* Error messages get reported properly by cmd_error, so this must be
9950 just an informative message; if the frame hasn't really been
9951 initialized yet, just toss it. */
9952 if (f->glyphs_initialized_p)
9953 {
9954 if (m)
9955 {
9956 ptrdiff_t len;
9957 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
9958 char *message_buf = alloca (maxsize + 1);
9959
9960 len = doprnt (message_buf, maxsize, m, 0, ap);
9961
9962 message3 (make_string (message_buf, len));
9963 }
9964 else
9965 message1 (0);
9966
9967 /* Print should start at the beginning of the message
9968 buffer next time. */
9969 message_buf_print = 0;
9970 }
9971 }
9972 }
9973
9974 void
9975 message (const char *m, ...)
9976 {
9977 va_list ap;
9978 va_start (ap, m);
9979 vmessage (m, ap);
9980 va_end (ap);
9981 }
9982
9983
9984 #if 0
9985 /* The non-logging version of message. */
9986
9987 void
9988 message_nolog (const char *m, ...)
9989 {
9990 Lisp_Object old_log_max;
9991 va_list ap;
9992 va_start (ap, m);
9993 old_log_max = Vmessage_log_max;
9994 Vmessage_log_max = Qnil;
9995 vmessage (m, ap);
9996 Vmessage_log_max = old_log_max;
9997 va_end (ap);
9998 }
9999 #endif
10000
10001
10002 /* Display the current message in the current mini-buffer. This is
10003 only called from error handlers in process.c, and is not time
10004 critical. */
10005
10006 void
10007 update_echo_area (void)
10008 {
10009 if (!NILP (echo_area_buffer[0]))
10010 {
10011 Lisp_Object string;
10012 string = Fcurrent_message ();
10013 message3 (string);
10014 }
10015 }
10016
10017
10018 /* Make sure echo area buffers in `echo_buffers' are live.
10019 If they aren't, make new ones. */
10020
10021 static void
10022 ensure_echo_area_buffers (void)
10023 {
10024 int i;
10025
10026 for (i = 0; i < 2; ++i)
10027 if (!BUFFERP (echo_buffer[i])
10028 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10029 {
10030 char name[30];
10031 Lisp_Object old_buffer;
10032 int j;
10033
10034 old_buffer = echo_buffer[i];
10035 echo_buffer[i] = Fget_buffer_create
10036 (make_formatted_string (name, " *Echo Area %d*", i));
10037 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10038 /* to force word wrap in echo area -
10039 it was decided to postpone this*/
10040 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10041
10042 for (j = 0; j < 2; ++j)
10043 if (EQ (old_buffer, echo_area_buffer[j]))
10044 echo_area_buffer[j] = echo_buffer[i];
10045 }
10046 }
10047
10048
10049 /* Call FN with args A1..A2 with either the current or last displayed
10050 echo_area_buffer as current buffer.
10051
10052 WHICH zero means use the current message buffer
10053 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10054 from echo_buffer[] and clear it.
10055
10056 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10057 suitable buffer from echo_buffer[] and clear it.
10058
10059 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10060 that the current message becomes the last displayed one, make
10061 choose a suitable buffer for echo_area_buffer[0], and clear it.
10062
10063 Value is what FN returns. */
10064
10065 static int
10066 with_echo_area_buffer (struct window *w, int which,
10067 int (*fn) (ptrdiff_t, Lisp_Object),
10068 ptrdiff_t a1, Lisp_Object a2)
10069 {
10070 Lisp_Object buffer;
10071 int this_one, the_other, clear_buffer_p, rc;
10072 ptrdiff_t count = SPECPDL_INDEX ();
10073
10074 /* If buffers aren't live, make new ones. */
10075 ensure_echo_area_buffers ();
10076
10077 clear_buffer_p = 0;
10078
10079 if (which == 0)
10080 this_one = 0, the_other = 1;
10081 else if (which > 0)
10082 this_one = 1, the_other = 0;
10083 else
10084 {
10085 this_one = 0, the_other = 1;
10086 clear_buffer_p = 1;
10087
10088 /* We need a fresh one in case the current echo buffer equals
10089 the one containing the last displayed echo area message. */
10090 if (!NILP (echo_area_buffer[this_one])
10091 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10092 echo_area_buffer[this_one] = Qnil;
10093 }
10094
10095 /* Choose a suitable buffer from echo_buffer[] is we don't
10096 have one. */
10097 if (NILP (echo_area_buffer[this_one]))
10098 {
10099 echo_area_buffer[this_one]
10100 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10101 ? echo_buffer[the_other]
10102 : echo_buffer[this_one]);
10103 clear_buffer_p = 1;
10104 }
10105
10106 buffer = echo_area_buffer[this_one];
10107
10108 /* Don't get confused by reusing the buffer used for echoing
10109 for a different purpose. */
10110 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10111 cancel_echoing ();
10112
10113 record_unwind_protect (unwind_with_echo_area_buffer,
10114 with_echo_area_buffer_unwind_data (w));
10115
10116 /* Make the echo area buffer current. Note that for display
10117 purposes, it is not necessary that the displayed window's buffer
10118 == current_buffer, except for text property lookup. So, let's
10119 only set that buffer temporarily here without doing a full
10120 Fset_window_buffer. We must also change w->pointm, though,
10121 because otherwise an assertions in unshow_buffer fails, and Emacs
10122 aborts. */
10123 set_buffer_internal_1 (XBUFFER (buffer));
10124 if (w)
10125 {
10126 wset_buffer (w, buffer);
10127 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10128 }
10129
10130 bset_undo_list (current_buffer, Qt);
10131 bset_read_only (current_buffer, Qnil);
10132 specbind (Qinhibit_read_only, Qt);
10133 specbind (Qinhibit_modification_hooks, Qt);
10134
10135 if (clear_buffer_p && Z > BEG)
10136 del_range (BEG, Z);
10137
10138 eassert (BEGV >= BEG);
10139 eassert (ZV <= Z && ZV >= BEGV);
10140
10141 rc = fn (a1, a2);
10142
10143 eassert (BEGV >= BEG);
10144 eassert (ZV <= Z && ZV >= BEGV);
10145
10146 unbind_to (count, Qnil);
10147 return rc;
10148 }
10149
10150
10151 /* Save state that should be preserved around the call to the function
10152 FN called in with_echo_area_buffer. */
10153
10154 static Lisp_Object
10155 with_echo_area_buffer_unwind_data (struct window *w)
10156 {
10157 int i = 0;
10158 Lisp_Object vector, tmp;
10159
10160 /* Reduce consing by keeping one vector in
10161 Vwith_echo_area_save_vector. */
10162 vector = Vwith_echo_area_save_vector;
10163 Vwith_echo_area_save_vector = Qnil;
10164
10165 if (NILP (vector))
10166 vector = Fmake_vector (make_number (9), Qnil);
10167
10168 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10169 ASET (vector, i, Vdeactivate_mark); ++i;
10170 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10171
10172 if (w)
10173 {
10174 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10175 ASET (vector, i, w->contents); ++i;
10176 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10177 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10178 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10179 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10180 }
10181 else
10182 {
10183 int end = i + 6;
10184 for (; i < end; ++i)
10185 ASET (vector, i, Qnil);
10186 }
10187
10188 eassert (i == ASIZE (vector));
10189 return vector;
10190 }
10191
10192
10193 /* Restore global state from VECTOR which was created by
10194 with_echo_area_buffer_unwind_data. */
10195
10196 static void
10197 unwind_with_echo_area_buffer (Lisp_Object vector)
10198 {
10199 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10200 Vdeactivate_mark = AREF (vector, 1);
10201 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10202
10203 if (WINDOWP (AREF (vector, 3)))
10204 {
10205 struct window *w;
10206 Lisp_Object buffer;
10207
10208 w = XWINDOW (AREF (vector, 3));
10209 buffer = AREF (vector, 4);
10210
10211 wset_buffer (w, buffer);
10212 set_marker_both (w->pointm, buffer,
10213 XFASTINT (AREF (vector, 5)),
10214 XFASTINT (AREF (vector, 6)));
10215 set_marker_both (w->start, buffer,
10216 XFASTINT (AREF (vector, 7)),
10217 XFASTINT (AREF (vector, 8)));
10218 }
10219
10220 Vwith_echo_area_save_vector = vector;
10221 }
10222
10223
10224 /* Set up the echo area for use by print functions. MULTIBYTE_P
10225 non-zero means we will print multibyte. */
10226
10227 void
10228 setup_echo_area_for_printing (int multibyte_p)
10229 {
10230 /* If we can't find an echo area any more, exit. */
10231 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10232 Fkill_emacs (Qnil);
10233
10234 ensure_echo_area_buffers ();
10235
10236 if (!message_buf_print)
10237 {
10238 /* A message has been output since the last time we printed.
10239 Choose a fresh echo area buffer. */
10240 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10241 echo_area_buffer[0] = echo_buffer[1];
10242 else
10243 echo_area_buffer[0] = echo_buffer[0];
10244
10245 /* Switch to that buffer and clear it. */
10246 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10247 bset_truncate_lines (current_buffer, Qnil);
10248
10249 if (Z > BEG)
10250 {
10251 ptrdiff_t count = SPECPDL_INDEX ();
10252 specbind (Qinhibit_read_only, Qt);
10253 /* Note that undo recording is always disabled. */
10254 del_range (BEG, Z);
10255 unbind_to (count, Qnil);
10256 }
10257 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10258
10259 /* Set up the buffer for the multibyteness we need. */
10260 if (multibyte_p
10261 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10262 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10263
10264 /* Raise the frame containing the echo area. */
10265 if (minibuffer_auto_raise)
10266 {
10267 struct frame *sf = SELECTED_FRAME ();
10268 Lisp_Object mini_window;
10269 mini_window = FRAME_MINIBUF_WINDOW (sf);
10270 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10271 }
10272
10273 message_log_maybe_newline ();
10274 message_buf_print = 1;
10275 }
10276 else
10277 {
10278 if (NILP (echo_area_buffer[0]))
10279 {
10280 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10281 echo_area_buffer[0] = echo_buffer[1];
10282 else
10283 echo_area_buffer[0] = echo_buffer[0];
10284 }
10285
10286 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10287 {
10288 /* Someone switched buffers between print requests. */
10289 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10290 bset_truncate_lines (current_buffer, Qnil);
10291 }
10292 }
10293 }
10294
10295
10296 /* Display an echo area message in window W. Value is non-zero if W's
10297 height is changed. If display_last_displayed_message_p is
10298 non-zero, display the message that was last displayed, otherwise
10299 display the current message. */
10300
10301 static int
10302 display_echo_area (struct window *w)
10303 {
10304 int i, no_message_p, window_height_changed_p;
10305
10306 /* Temporarily disable garbage collections while displaying the echo
10307 area. This is done because a GC can print a message itself.
10308 That message would modify the echo area buffer's contents while a
10309 redisplay of the buffer is going on, and seriously confuse
10310 redisplay. */
10311 ptrdiff_t count = inhibit_garbage_collection ();
10312
10313 /* If there is no message, we must call display_echo_area_1
10314 nevertheless because it resizes the window. But we will have to
10315 reset the echo_area_buffer in question to nil at the end because
10316 with_echo_area_buffer will sets it to an empty buffer. */
10317 i = display_last_displayed_message_p ? 1 : 0;
10318 no_message_p = NILP (echo_area_buffer[i]);
10319
10320 window_height_changed_p
10321 = with_echo_area_buffer (w, display_last_displayed_message_p,
10322 display_echo_area_1,
10323 (intptr_t) w, Qnil);
10324
10325 if (no_message_p)
10326 echo_area_buffer[i] = Qnil;
10327
10328 unbind_to (count, Qnil);
10329 return window_height_changed_p;
10330 }
10331
10332
10333 /* Helper for display_echo_area. Display the current buffer which
10334 contains the current echo area message in window W, a mini-window,
10335 a pointer to which is passed in A1. A2..A4 are currently not used.
10336 Change the height of W so that all of the message is displayed.
10337 Value is non-zero if height of W was changed. */
10338
10339 static int
10340 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10341 {
10342 intptr_t i1 = a1;
10343 struct window *w = (struct window *) i1;
10344 Lisp_Object window;
10345 struct text_pos start;
10346 int window_height_changed_p = 0;
10347
10348 /* Do this before displaying, so that we have a large enough glyph
10349 matrix for the display. If we can't get enough space for the
10350 whole text, display the last N lines. That works by setting w->start. */
10351 window_height_changed_p = resize_mini_window (w, 0);
10352
10353 /* Use the starting position chosen by resize_mini_window. */
10354 SET_TEXT_POS_FROM_MARKER (start, w->start);
10355
10356 /* Display. */
10357 clear_glyph_matrix (w->desired_matrix);
10358 XSETWINDOW (window, w);
10359 try_window (window, start, 0);
10360
10361 return window_height_changed_p;
10362 }
10363
10364
10365 /* Resize the echo area window to exactly the size needed for the
10366 currently displayed message, if there is one. If a mini-buffer
10367 is active, don't shrink it. */
10368
10369 void
10370 resize_echo_area_exactly (void)
10371 {
10372 if (BUFFERP (echo_area_buffer[0])
10373 && WINDOWP (echo_area_window))
10374 {
10375 struct window *w = XWINDOW (echo_area_window);
10376 int resized_p;
10377 Lisp_Object resize_exactly;
10378
10379 if (minibuf_level == 0)
10380 resize_exactly = Qt;
10381 else
10382 resize_exactly = Qnil;
10383
10384 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10385 (intptr_t) w, resize_exactly);
10386 if (resized_p)
10387 {
10388 ++windows_or_buffers_changed;
10389 ++update_mode_lines;
10390 redisplay_internal ();
10391 }
10392 }
10393 }
10394
10395
10396 /* Callback function for with_echo_area_buffer, when used from
10397 resize_echo_area_exactly. A1 contains a pointer to the window to
10398 resize, EXACTLY non-nil means resize the mini-window exactly to the
10399 size of the text displayed. A3 and A4 are not used. Value is what
10400 resize_mini_window returns. */
10401
10402 static int
10403 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10404 {
10405 intptr_t i1 = a1;
10406 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10407 }
10408
10409
10410 /* Resize mini-window W to fit the size of its contents. EXACT_P
10411 means size the window exactly to the size needed. Otherwise, it's
10412 only enlarged until W's buffer is empty.
10413
10414 Set W->start to the right place to begin display. If the whole
10415 contents fit, start at the beginning. Otherwise, start so as
10416 to make the end of the contents appear. This is particularly
10417 important for y-or-n-p, but seems desirable generally.
10418
10419 Value is non-zero if the window height has been changed. */
10420
10421 int
10422 resize_mini_window (struct window *w, int exact_p)
10423 {
10424 struct frame *f = XFRAME (w->frame);
10425 int window_height_changed_p = 0;
10426
10427 eassert (MINI_WINDOW_P (w));
10428
10429 /* By default, start display at the beginning. */
10430 set_marker_both (w->start, w->contents,
10431 BUF_BEGV (XBUFFER (w->contents)),
10432 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10433
10434 /* Don't resize windows while redisplaying a window; it would
10435 confuse redisplay functions when the size of the window they are
10436 displaying changes from under them. Such a resizing can happen,
10437 for instance, when which-func prints a long message while
10438 we are running fontification-functions. We're running these
10439 functions with safe_call which binds inhibit-redisplay to t. */
10440 if (!NILP (Vinhibit_redisplay))
10441 return 0;
10442
10443 /* Nil means don't try to resize. */
10444 if (NILP (Vresize_mini_windows)
10445 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10446 return 0;
10447
10448 if (!FRAME_MINIBUF_ONLY_P (f))
10449 {
10450 struct it it;
10451 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10452 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10453 int height;
10454 EMACS_INT max_height;
10455 int unit = FRAME_LINE_HEIGHT (f);
10456 struct text_pos start;
10457 struct buffer *old_current_buffer = NULL;
10458
10459 if (current_buffer != XBUFFER (w->contents))
10460 {
10461 old_current_buffer = current_buffer;
10462 set_buffer_internal (XBUFFER (w->contents));
10463 }
10464
10465 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10466
10467 /* Compute the max. number of lines specified by the user. */
10468 if (FLOATP (Vmax_mini_window_height))
10469 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10470 else if (INTEGERP (Vmax_mini_window_height))
10471 max_height = XINT (Vmax_mini_window_height);
10472 else
10473 max_height = total_height / 4;
10474
10475 /* Correct that max. height if it's bogus. */
10476 max_height = clip_to_bounds (1, max_height, total_height);
10477
10478 /* Find out the height of the text in the window. */
10479 if (it.line_wrap == TRUNCATE)
10480 height = 1;
10481 else
10482 {
10483 last_height = 0;
10484 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10485 if (it.max_ascent == 0 && it.max_descent == 0)
10486 height = it.current_y + last_height;
10487 else
10488 height = it.current_y + it.max_ascent + it.max_descent;
10489 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10490 height = (height + unit - 1) / unit;
10491 }
10492
10493 /* Compute a suitable window start. */
10494 if (height > max_height)
10495 {
10496 height = max_height;
10497 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10498 move_it_vertically_backward (&it, (height - 1) * unit);
10499 start = it.current.pos;
10500 }
10501 else
10502 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10503 SET_MARKER_FROM_TEXT_POS (w->start, start);
10504
10505 if (EQ (Vresize_mini_windows, Qgrow_only))
10506 {
10507 /* Let it grow only, until we display an empty message, in which
10508 case the window shrinks again. */
10509 if (height > WINDOW_TOTAL_LINES (w))
10510 {
10511 int old_height = WINDOW_TOTAL_LINES (w);
10512
10513 FRAME_WINDOWS_FROZEN (f) = 1;
10514 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10515 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10516 }
10517 else if (height < WINDOW_TOTAL_LINES (w)
10518 && (exact_p || BEGV == ZV))
10519 {
10520 int old_height = WINDOW_TOTAL_LINES (w);
10521
10522 FRAME_WINDOWS_FROZEN (f) = 0;
10523 shrink_mini_window (w);
10524 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10525 }
10526 }
10527 else
10528 {
10529 /* Always resize to exact size needed. */
10530 if (height > WINDOW_TOTAL_LINES (w))
10531 {
10532 int old_height = WINDOW_TOTAL_LINES (w);
10533
10534 FRAME_WINDOWS_FROZEN (f) = 1;
10535 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10536 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10537 }
10538 else if (height < WINDOW_TOTAL_LINES (w))
10539 {
10540 int old_height = WINDOW_TOTAL_LINES (w);
10541
10542 FRAME_WINDOWS_FROZEN (f) = 0;
10543 shrink_mini_window (w);
10544
10545 if (height)
10546 {
10547 FRAME_WINDOWS_FROZEN (f) = 1;
10548 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10549 }
10550
10551 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10552 }
10553 }
10554
10555 if (old_current_buffer)
10556 set_buffer_internal (old_current_buffer);
10557 }
10558
10559 return window_height_changed_p;
10560 }
10561
10562
10563 /* Value is the current message, a string, or nil if there is no
10564 current message. */
10565
10566 Lisp_Object
10567 current_message (void)
10568 {
10569 Lisp_Object msg;
10570
10571 if (!BUFFERP (echo_area_buffer[0]))
10572 msg = Qnil;
10573 else
10574 {
10575 with_echo_area_buffer (0, 0, current_message_1,
10576 (intptr_t) &msg, Qnil);
10577 if (NILP (msg))
10578 echo_area_buffer[0] = Qnil;
10579 }
10580
10581 return msg;
10582 }
10583
10584
10585 static int
10586 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10587 {
10588 intptr_t i1 = a1;
10589 Lisp_Object *msg = (Lisp_Object *) i1;
10590
10591 if (Z > BEG)
10592 *msg = make_buffer_string (BEG, Z, 1);
10593 else
10594 *msg = Qnil;
10595 return 0;
10596 }
10597
10598
10599 /* Push the current message on Vmessage_stack for later restoration
10600 by restore_message. Value is non-zero if the current message isn't
10601 empty. This is a relatively infrequent operation, so it's not
10602 worth optimizing. */
10603
10604 bool
10605 push_message (void)
10606 {
10607 Lisp_Object msg = current_message ();
10608 Vmessage_stack = Fcons (msg, Vmessage_stack);
10609 return STRINGP (msg);
10610 }
10611
10612
10613 /* Restore message display from the top of Vmessage_stack. */
10614
10615 void
10616 restore_message (void)
10617 {
10618 eassert (CONSP (Vmessage_stack));
10619 message3_nolog (XCAR (Vmessage_stack));
10620 }
10621
10622
10623 /* Handler for unwind-protect calling pop_message. */
10624
10625 void
10626 pop_message_unwind (void)
10627 {
10628 /* Pop the top-most entry off Vmessage_stack. */
10629 eassert (CONSP (Vmessage_stack));
10630 Vmessage_stack = XCDR (Vmessage_stack);
10631 }
10632
10633
10634 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10635 exits. If the stack is not empty, we have a missing pop_message
10636 somewhere. */
10637
10638 void
10639 check_message_stack (void)
10640 {
10641 if (!NILP (Vmessage_stack))
10642 emacs_abort ();
10643 }
10644
10645
10646 /* Truncate to NCHARS what will be displayed in the echo area the next
10647 time we display it---but don't redisplay it now. */
10648
10649 void
10650 truncate_echo_area (ptrdiff_t nchars)
10651 {
10652 if (nchars == 0)
10653 echo_area_buffer[0] = Qnil;
10654 else if (!noninteractive
10655 && INTERACTIVE
10656 && !NILP (echo_area_buffer[0]))
10657 {
10658 struct frame *sf = SELECTED_FRAME ();
10659 /* Error messages get reported properly by cmd_error, so this must be
10660 just an informative message; if the frame hasn't really been
10661 initialized yet, just toss it. */
10662 if (sf->glyphs_initialized_p)
10663 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10664 }
10665 }
10666
10667
10668 /* Helper function for truncate_echo_area. Truncate the current
10669 message to at most NCHARS characters. */
10670
10671 static int
10672 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10673 {
10674 if (BEG + nchars < Z)
10675 del_range (BEG + nchars, Z);
10676 if (Z == BEG)
10677 echo_area_buffer[0] = Qnil;
10678 return 0;
10679 }
10680
10681 /* Set the current message to STRING. */
10682
10683 static void
10684 set_message (Lisp_Object string)
10685 {
10686 eassert (STRINGP (string));
10687
10688 message_enable_multibyte = STRING_MULTIBYTE (string);
10689
10690 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10691 message_buf_print = 0;
10692 help_echo_showing_p = 0;
10693
10694 if (STRINGP (Vdebug_on_message)
10695 && STRINGP (string)
10696 && fast_string_match (Vdebug_on_message, string) >= 0)
10697 call_debugger (list2 (Qerror, string));
10698 }
10699
10700
10701 /* Helper function for set_message. First argument is ignored and second
10702 argument has the same meaning as for set_message.
10703 This function is called with the echo area buffer being current. */
10704
10705 static int
10706 set_message_1 (ptrdiff_t a1, Lisp_Object string)
10707 {
10708 eassert (STRINGP (string));
10709
10710 /* Change multibyteness of the echo buffer appropriately. */
10711 if (message_enable_multibyte
10712 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10713 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10714
10715 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10716 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10717 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10718
10719 /* Insert new message at BEG. */
10720 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10721
10722 /* This function takes care of single/multibyte conversion.
10723 We just have to ensure that the echo area buffer has the right
10724 setting of enable_multibyte_characters. */
10725 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
10726
10727 return 0;
10728 }
10729
10730
10731 /* Clear messages. CURRENT_P non-zero means clear the current
10732 message. LAST_DISPLAYED_P non-zero means clear the message
10733 last displayed. */
10734
10735 void
10736 clear_message (int current_p, int last_displayed_p)
10737 {
10738 if (current_p)
10739 {
10740 echo_area_buffer[0] = Qnil;
10741 message_cleared_p = 1;
10742 }
10743
10744 if (last_displayed_p)
10745 echo_area_buffer[1] = Qnil;
10746
10747 message_buf_print = 0;
10748 }
10749
10750 /* Clear garbaged frames.
10751
10752 This function is used where the old redisplay called
10753 redraw_garbaged_frames which in turn called redraw_frame which in
10754 turn called clear_frame. The call to clear_frame was a source of
10755 flickering. I believe a clear_frame is not necessary. It should
10756 suffice in the new redisplay to invalidate all current matrices,
10757 and ensure a complete redisplay of all windows. */
10758
10759 static void
10760 clear_garbaged_frames (void)
10761 {
10762 if (frame_garbaged)
10763 {
10764 Lisp_Object tail, frame;
10765 int changed_count = 0;
10766
10767 FOR_EACH_FRAME (tail, frame)
10768 {
10769 struct frame *f = XFRAME (frame);
10770
10771 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10772 {
10773 if (f->resized_p)
10774 {
10775 redraw_frame (f);
10776 f->force_flush_display_p = 1;
10777 }
10778 clear_current_matrices (f);
10779 changed_count++;
10780 f->garbaged = 0;
10781 f->resized_p = 0;
10782 }
10783 }
10784
10785 frame_garbaged = 0;
10786 if (changed_count)
10787 ++windows_or_buffers_changed;
10788 }
10789 }
10790
10791
10792 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10793 is non-zero update selected_frame. Value is non-zero if the
10794 mini-windows height has been changed. */
10795
10796 static int
10797 echo_area_display (int update_frame_p)
10798 {
10799 Lisp_Object mini_window;
10800 struct window *w;
10801 struct frame *f;
10802 int window_height_changed_p = 0;
10803 struct frame *sf = SELECTED_FRAME ();
10804
10805 mini_window = FRAME_MINIBUF_WINDOW (sf);
10806 w = XWINDOW (mini_window);
10807 f = XFRAME (WINDOW_FRAME (w));
10808
10809 /* Don't display if frame is invisible or not yet initialized. */
10810 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10811 return 0;
10812
10813 #ifdef HAVE_WINDOW_SYSTEM
10814 /* When Emacs starts, selected_frame may be the initial terminal
10815 frame. If we let this through, a message would be displayed on
10816 the terminal. */
10817 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10818 return 0;
10819 #endif /* HAVE_WINDOW_SYSTEM */
10820
10821 /* Redraw garbaged frames. */
10822 clear_garbaged_frames ();
10823
10824 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10825 {
10826 echo_area_window = mini_window;
10827 window_height_changed_p = display_echo_area (w);
10828 w->must_be_updated_p = 1;
10829
10830 /* Update the display, unless called from redisplay_internal.
10831 Also don't update the screen during redisplay itself. The
10832 update will happen at the end of redisplay, and an update
10833 here could cause confusion. */
10834 if (update_frame_p && !redisplaying_p)
10835 {
10836 int n = 0;
10837
10838 /* If the display update has been interrupted by pending
10839 input, update mode lines in the frame. Due to the
10840 pending input, it might have been that redisplay hasn't
10841 been called, so that mode lines above the echo area are
10842 garbaged. This looks odd, so we prevent it here. */
10843 if (!display_completed)
10844 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10845
10846 if (window_height_changed_p
10847 /* Don't do this if Emacs is shutting down. Redisplay
10848 needs to run hooks. */
10849 && !NILP (Vrun_hooks))
10850 {
10851 /* Must update other windows. Likewise as in other
10852 cases, don't let this update be interrupted by
10853 pending input. */
10854 ptrdiff_t count = SPECPDL_INDEX ();
10855 specbind (Qredisplay_dont_pause, Qt);
10856 windows_or_buffers_changed = 1;
10857 redisplay_internal ();
10858 unbind_to (count, Qnil);
10859 }
10860 else if (FRAME_WINDOW_P (f) && n == 0)
10861 {
10862 /* Window configuration is the same as before.
10863 Can do with a display update of the echo area,
10864 unless we displayed some mode lines. */
10865 update_single_window (w, 1);
10866 FRAME_RIF (f)->flush_display (f);
10867 }
10868 else
10869 update_frame (f, 1, 1);
10870
10871 /* If cursor is in the echo area, make sure that the next
10872 redisplay displays the minibuffer, so that the cursor will
10873 be replaced with what the minibuffer wants. */
10874 if (cursor_in_echo_area)
10875 ++windows_or_buffers_changed;
10876 }
10877 }
10878 else if (!EQ (mini_window, selected_window))
10879 windows_or_buffers_changed++;
10880
10881 /* Last displayed message is now the current message. */
10882 echo_area_buffer[1] = echo_area_buffer[0];
10883 /* Inform read_char that we're not echoing. */
10884 echo_message_buffer = Qnil;
10885
10886 /* Prevent redisplay optimization in redisplay_internal by resetting
10887 this_line_start_pos. This is done because the mini-buffer now
10888 displays the message instead of its buffer text. */
10889 if (EQ (mini_window, selected_window))
10890 CHARPOS (this_line_start_pos) = 0;
10891
10892 return window_height_changed_p;
10893 }
10894
10895 /* Nonzero if the current window's buffer is shown in more than one
10896 window and was modified since last redisplay. */
10897
10898 static int
10899 buffer_shared_and_changed (void)
10900 {
10901 return (buffer_window_count (current_buffer) > 1
10902 && UNCHANGED_MODIFIED < MODIFF);
10903 }
10904
10905 /* Nonzero if W's buffer was changed but not saved or Transient Mark mode
10906 is enabled and mark of W's buffer was changed since last W's update. */
10907
10908 static int
10909 window_buffer_changed (struct window *w)
10910 {
10911 struct buffer *b = XBUFFER (w->contents);
10912
10913 eassert (BUFFER_LIVE_P (b));
10914
10915 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star)
10916 || ((!NILP (Vtransient_mark_mode) && !NILP (BVAR (b, mark_active)))
10917 != (w->region_showing != 0)));
10918 }
10919
10920 /* Nonzero if W has %c in its mode line and mode line should be updated. */
10921
10922 static int
10923 mode_line_update_needed (struct window *w)
10924 {
10925 return (w->column_number_displayed != -1
10926 && !(PT == w->last_point && !window_outdated (w))
10927 && (w->column_number_displayed != current_column ()));
10928 }
10929
10930 /* Nonzero if window start of W is frozen and may not be changed during
10931 redisplay. */
10932
10933 static bool
10934 window_frozen_p (struct window *w)
10935 {
10936 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
10937 {
10938 Lisp_Object window;
10939
10940 XSETWINDOW (window, w);
10941 if (MINI_WINDOW_P (w))
10942 return 0;
10943 else if (EQ (window, selected_window))
10944 return 0;
10945 else if (MINI_WINDOW_P (XWINDOW (selected_window))
10946 && EQ (window, Vminibuf_scroll_window))
10947 /* This special window can't be frozen too. */
10948 return 0;
10949 else
10950 return 1;
10951 }
10952 return 0;
10953 }
10954
10955 /***********************************************************************
10956 Mode Lines and Frame Titles
10957 ***********************************************************************/
10958
10959 /* A buffer for constructing non-propertized mode-line strings and
10960 frame titles in it; allocated from the heap in init_xdisp and
10961 resized as needed in store_mode_line_noprop_char. */
10962
10963 static char *mode_line_noprop_buf;
10964
10965 /* The buffer's end, and a current output position in it. */
10966
10967 static char *mode_line_noprop_buf_end;
10968 static char *mode_line_noprop_ptr;
10969
10970 #define MODE_LINE_NOPROP_LEN(start) \
10971 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10972
10973 static enum {
10974 MODE_LINE_DISPLAY = 0,
10975 MODE_LINE_TITLE,
10976 MODE_LINE_NOPROP,
10977 MODE_LINE_STRING
10978 } mode_line_target;
10979
10980 /* Alist that caches the results of :propertize.
10981 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10982 static Lisp_Object mode_line_proptrans_alist;
10983
10984 /* List of strings making up the mode-line. */
10985 static Lisp_Object mode_line_string_list;
10986
10987 /* Base face property when building propertized mode line string. */
10988 static Lisp_Object mode_line_string_face;
10989 static Lisp_Object mode_line_string_face_prop;
10990
10991
10992 /* Unwind data for mode line strings */
10993
10994 static Lisp_Object Vmode_line_unwind_vector;
10995
10996 static Lisp_Object
10997 format_mode_line_unwind_data (struct frame *target_frame,
10998 struct buffer *obuf,
10999 Lisp_Object owin,
11000 int save_proptrans)
11001 {
11002 Lisp_Object vector, tmp;
11003
11004 /* Reduce consing by keeping one vector in
11005 Vwith_echo_area_save_vector. */
11006 vector = Vmode_line_unwind_vector;
11007 Vmode_line_unwind_vector = Qnil;
11008
11009 if (NILP (vector))
11010 vector = Fmake_vector (make_number (10), Qnil);
11011
11012 ASET (vector, 0, make_number (mode_line_target));
11013 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11014 ASET (vector, 2, mode_line_string_list);
11015 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11016 ASET (vector, 4, mode_line_string_face);
11017 ASET (vector, 5, mode_line_string_face_prop);
11018
11019 if (obuf)
11020 XSETBUFFER (tmp, obuf);
11021 else
11022 tmp = Qnil;
11023 ASET (vector, 6, tmp);
11024 ASET (vector, 7, owin);
11025 if (target_frame)
11026 {
11027 /* Similarly to `with-selected-window', if the operation selects
11028 a window on another frame, we must restore that frame's
11029 selected window, and (for a tty) the top-frame. */
11030 ASET (vector, 8, target_frame->selected_window);
11031 if (FRAME_TERMCAP_P (target_frame))
11032 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11033 }
11034
11035 return vector;
11036 }
11037
11038 static void
11039 unwind_format_mode_line (Lisp_Object vector)
11040 {
11041 Lisp_Object old_window = AREF (vector, 7);
11042 Lisp_Object target_frame_window = AREF (vector, 8);
11043 Lisp_Object old_top_frame = AREF (vector, 9);
11044
11045 mode_line_target = XINT (AREF (vector, 0));
11046 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11047 mode_line_string_list = AREF (vector, 2);
11048 if (! EQ (AREF (vector, 3), Qt))
11049 mode_line_proptrans_alist = AREF (vector, 3);
11050 mode_line_string_face = AREF (vector, 4);
11051 mode_line_string_face_prop = AREF (vector, 5);
11052
11053 /* Select window before buffer, since it may change the buffer. */
11054 if (!NILP (old_window))
11055 {
11056 /* If the operation that we are unwinding had selected a window
11057 on a different frame, reset its frame-selected-window. For a
11058 text terminal, reset its top-frame if necessary. */
11059 if (!NILP (target_frame_window))
11060 {
11061 Lisp_Object frame
11062 = WINDOW_FRAME (XWINDOW (target_frame_window));
11063
11064 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11065 Fselect_window (target_frame_window, Qt);
11066
11067 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11068 Fselect_frame (old_top_frame, Qt);
11069 }
11070
11071 Fselect_window (old_window, Qt);
11072 }
11073
11074 if (!NILP (AREF (vector, 6)))
11075 {
11076 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11077 ASET (vector, 6, Qnil);
11078 }
11079
11080 Vmode_line_unwind_vector = vector;
11081 }
11082
11083
11084 /* Store a single character C for the frame title in mode_line_noprop_buf.
11085 Re-allocate mode_line_noprop_buf if necessary. */
11086
11087 static void
11088 store_mode_line_noprop_char (char c)
11089 {
11090 /* If output position has reached the end of the allocated buffer,
11091 increase the buffer's size. */
11092 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11093 {
11094 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11095 ptrdiff_t size = len;
11096 mode_line_noprop_buf =
11097 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11098 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11099 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11100 }
11101
11102 *mode_line_noprop_ptr++ = c;
11103 }
11104
11105
11106 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11107 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11108 characters that yield more columns than PRECISION; PRECISION <= 0
11109 means copy the whole string. Pad with spaces until FIELD_WIDTH
11110 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11111 pad. Called from display_mode_element when it is used to build a
11112 frame title. */
11113
11114 static int
11115 store_mode_line_noprop (const char *string, int field_width, int precision)
11116 {
11117 const unsigned char *str = (const unsigned char *) string;
11118 int n = 0;
11119 ptrdiff_t dummy, nbytes;
11120
11121 /* Copy at most PRECISION chars from STR. */
11122 nbytes = strlen (string);
11123 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11124 while (nbytes--)
11125 store_mode_line_noprop_char (*str++);
11126
11127 /* Fill up with spaces until FIELD_WIDTH reached. */
11128 while (field_width > 0
11129 && n < field_width)
11130 {
11131 store_mode_line_noprop_char (' ');
11132 ++n;
11133 }
11134
11135 return n;
11136 }
11137
11138 /***********************************************************************
11139 Frame Titles
11140 ***********************************************************************/
11141
11142 #ifdef HAVE_WINDOW_SYSTEM
11143
11144 /* Set the title of FRAME, if it has changed. The title format is
11145 Vicon_title_format if FRAME is iconified, otherwise it is
11146 frame_title_format. */
11147
11148 static void
11149 x_consider_frame_title (Lisp_Object frame)
11150 {
11151 struct frame *f = XFRAME (frame);
11152
11153 if (FRAME_WINDOW_P (f)
11154 || FRAME_MINIBUF_ONLY_P (f)
11155 || f->explicit_name)
11156 {
11157 /* Do we have more than one visible frame on this X display? */
11158 Lisp_Object tail, other_frame, fmt;
11159 ptrdiff_t title_start;
11160 char *title;
11161 ptrdiff_t len;
11162 struct it it;
11163 ptrdiff_t count = SPECPDL_INDEX ();
11164
11165 FOR_EACH_FRAME (tail, other_frame)
11166 {
11167 struct frame *tf = XFRAME (other_frame);
11168
11169 if (tf != f
11170 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11171 && !FRAME_MINIBUF_ONLY_P (tf)
11172 && !EQ (other_frame, tip_frame)
11173 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11174 break;
11175 }
11176
11177 /* Set global variable indicating that multiple frames exist. */
11178 multiple_frames = CONSP (tail);
11179
11180 /* Switch to the buffer of selected window of the frame. Set up
11181 mode_line_target so that display_mode_element will output into
11182 mode_line_noprop_buf; then display the title. */
11183 record_unwind_protect (unwind_format_mode_line,
11184 format_mode_line_unwind_data
11185 (f, current_buffer, selected_window, 0));
11186
11187 Fselect_window (f->selected_window, Qt);
11188 set_buffer_internal_1
11189 (XBUFFER (XWINDOW (f->selected_window)->contents));
11190 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11191
11192 mode_line_target = MODE_LINE_TITLE;
11193 title_start = MODE_LINE_NOPROP_LEN (0);
11194 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11195 NULL, DEFAULT_FACE_ID);
11196 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11197 len = MODE_LINE_NOPROP_LEN (title_start);
11198 title = mode_line_noprop_buf + title_start;
11199 unbind_to (count, Qnil);
11200
11201 /* Set the title only if it's changed. This avoids consing in
11202 the common case where it hasn't. (If it turns out that we've
11203 already wasted too much time by walking through the list with
11204 display_mode_element, then we might need to optimize at a
11205 higher level than this.) */
11206 if (! STRINGP (f->name)
11207 || SBYTES (f->name) != len
11208 || memcmp (title, SDATA (f->name), len) != 0)
11209 x_implicitly_set_name (f, make_string (title, len), Qnil);
11210 }
11211 }
11212
11213 #endif /* not HAVE_WINDOW_SYSTEM */
11214
11215 \f
11216 /***********************************************************************
11217 Menu Bars
11218 ***********************************************************************/
11219
11220
11221 /* Prepare for redisplay by updating menu-bar item lists when
11222 appropriate. This can call eval. */
11223
11224 void
11225 prepare_menu_bars (void)
11226 {
11227 int all_windows;
11228 struct gcpro gcpro1, gcpro2;
11229 struct frame *f;
11230 Lisp_Object tooltip_frame;
11231
11232 #ifdef HAVE_WINDOW_SYSTEM
11233 tooltip_frame = tip_frame;
11234 #else
11235 tooltip_frame = Qnil;
11236 #endif
11237
11238 /* Update all frame titles based on their buffer names, etc. We do
11239 this before the menu bars so that the buffer-menu will show the
11240 up-to-date frame titles. */
11241 #ifdef HAVE_WINDOW_SYSTEM
11242 if (windows_or_buffers_changed || update_mode_lines)
11243 {
11244 Lisp_Object tail, frame;
11245
11246 FOR_EACH_FRAME (tail, frame)
11247 {
11248 f = XFRAME (frame);
11249 if (!EQ (frame, tooltip_frame)
11250 && (FRAME_ICONIFIED_P (f)
11251 || FRAME_VISIBLE_P (f) == 1
11252 /* Exclude TTY frames that are obscured because they
11253 are not the top frame on their console. This is
11254 because x_consider_frame_title actually switches
11255 to the frame, which for TTY frames means it is
11256 marked as garbaged, and will be completely
11257 redrawn on the next redisplay cycle. This causes
11258 TTY frames to be completely redrawn, when there
11259 are more than one of them, even though nothing
11260 should be changed on display. */
11261 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11262 x_consider_frame_title (frame);
11263 }
11264 }
11265 #endif /* HAVE_WINDOW_SYSTEM */
11266
11267 /* Update the menu bar item lists, if appropriate. This has to be
11268 done before any actual redisplay or generation of display lines. */
11269 all_windows = (update_mode_lines
11270 || buffer_shared_and_changed ()
11271 || windows_or_buffers_changed);
11272 if (all_windows)
11273 {
11274 Lisp_Object tail, frame;
11275 ptrdiff_t count = SPECPDL_INDEX ();
11276 /* 1 means that update_menu_bar has run its hooks
11277 so any further calls to update_menu_bar shouldn't do so again. */
11278 int menu_bar_hooks_run = 0;
11279
11280 record_unwind_save_match_data ();
11281
11282 FOR_EACH_FRAME (tail, frame)
11283 {
11284 f = XFRAME (frame);
11285
11286 /* Ignore tooltip frame. */
11287 if (EQ (frame, tooltip_frame))
11288 continue;
11289
11290 /* If a window on this frame changed size, report that to
11291 the user and clear the size-change flag. */
11292 if (FRAME_WINDOW_SIZES_CHANGED (f))
11293 {
11294 Lisp_Object functions;
11295
11296 /* Clear flag first in case we get an error below. */
11297 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11298 functions = Vwindow_size_change_functions;
11299 GCPRO2 (tail, functions);
11300
11301 while (CONSP (functions))
11302 {
11303 if (!EQ (XCAR (functions), Qt))
11304 call1 (XCAR (functions), frame);
11305 functions = XCDR (functions);
11306 }
11307 UNGCPRO;
11308 }
11309
11310 GCPRO1 (tail);
11311 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11312 #ifdef HAVE_WINDOW_SYSTEM
11313 update_tool_bar (f, 0);
11314 #endif
11315 #ifdef HAVE_NS
11316 if (windows_or_buffers_changed
11317 && FRAME_NS_P (f))
11318 ns_set_doc_edited
11319 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->contents));
11320 #endif
11321 UNGCPRO;
11322 }
11323
11324 unbind_to (count, Qnil);
11325 }
11326 else
11327 {
11328 struct frame *sf = SELECTED_FRAME ();
11329 update_menu_bar (sf, 1, 0);
11330 #ifdef HAVE_WINDOW_SYSTEM
11331 update_tool_bar (sf, 1);
11332 #endif
11333 }
11334 }
11335
11336
11337 /* Update the menu bar item list for frame F. This has to be done
11338 before we start to fill in any display lines, because it can call
11339 eval.
11340
11341 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11342
11343 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11344 already ran the menu bar hooks for this redisplay, so there
11345 is no need to run them again. The return value is the
11346 updated value of this flag, to pass to the next call. */
11347
11348 static int
11349 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11350 {
11351 Lisp_Object window;
11352 register struct window *w;
11353
11354 /* If called recursively during a menu update, do nothing. This can
11355 happen when, for instance, an activate-menubar-hook causes a
11356 redisplay. */
11357 if (inhibit_menubar_update)
11358 return hooks_run;
11359
11360 window = FRAME_SELECTED_WINDOW (f);
11361 w = XWINDOW (window);
11362
11363 if (FRAME_WINDOW_P (f)
11364 ?
11365 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11366 || defined (HAVE_NS) || defined (USE_GTK)
11367 FRAME_EXTERNAL_MENU_BAR (f)
11368 #else
11369 FRAME_MENU_BAR_LINES (f) > 0
11370 #endif
11371 : FRAME_MENU_BAR_LINES (f) > 0)
11372 {
11373 /* If the user has switched buffers or windows, we need to
11374 recompute to reflect the new bindings. But we'll
11375 recompute when update_mode_lines is set too; that means
11376 that people can use force-mode-line-update to request
11377 that the menu bar be recomputed. The adverse effect on
11378 the rest of the redisplay algorithm is about the same as
11379 windows_or_buffers_changed anyway. */
11380 if (windows_or_buffers_changed
11381 /* This used to test w->update_mode_line, but we believe
11382 there is no need to recompute the menu in that case. */
11383 || update_mode_lines
11384 || window_buffer_changed (w))
11385 {
11386 struct buffer *prev = current_buffer;
11387 ptrdiff_t count = SPECPDL_INDEX ();
11388
11389 specbind (Qinhibit_menubar_update, Qt);
11390
11391 set_buffer_internal_1 (XBUFFER (w->contents));
11392 if (save_match_data)
11393 record_unwind_save_match_data ();
11394 if (NILP (Voverriding_local_map_menu_flag))
11395 {
11396 specbind (Qoverriding_terminal_local_map, Qnil);
11397 specbind (Qoverriding_local_map, Qnil);
11398 }
11399
11400 if (!hooks_run)
11401 {
11402 /* Run the Lucid hook. */
11403 safe_run_hooks (Qactivate_menubar_hook);
11404
11405 /* If it has changed current-menubar from previous value,
11406 really recompute the menu-bar from the value. */
11407 if (! NILP (Vlucid_menu_bar_dirty_flag))
11408 call0 (Qrecompute_lucid_menubar);
11409
11410 safe_run_hooks (Qmenu_bar_update_hook);
11411
11412 hooks_run = 1;
11413 }
11414
11415 XSETFRAME (Vmenu_updating_frame, f);
11416 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11417
11418 /* Redisplay the menu bar in case we changed it. */
11419 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11420 || defined (HAVE_NS) || defined (USE_GTK)
11421 if (FRAME_WINDOW_P (f))
11422 {
11423 #if defined (HAVE_NS)
11424 /* All frames on Mac OS share the same menubar. So only
11425 the selected frame should be allowed to set it. */
11426 if (f == SELECTED_FRAME ())
11427 #endif
11428 set_frame_menubar (f, 0, 0);
11429 }
11430 else
11431 /* On a terminal screen, the menu bar is an ordinary screen
11432 line, and this makes it get updated. */
11433 w->update_mode_line = 1;
11434 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11435 /* In the non-toolkit version, the menu bar is an ordinary screen
11436 line, and this makes it get updated. */
11437 w->update_mode_line = 1;
11438 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11439
11440 unbind_to (count, Qnil);
11441 set_buffer_internal_1 (prev);
11442 }
11443 }
11444
11445 return hooks_run;
11446 }
11447
11448 /***********************************************************************
11449 Tool-bars
11450 ***********************************************************************/
11451
11452 #ifdef HAVE_WINDOW_SYSTEM
11453
11454 /* Where the mouse was last time we reported a mouse event. */
11455
11456 struct frame *last_mouse_frame;
11457
11458 /* Tool-bar item index of the item on which a mouse button was pressed
11459 or -1. */
11460
11461 int last_tool_bar_item;
11462
11463 /* Select `frame' temporarily without running all the code in
11464 do_switch_frame.
11465 FIXME: Maybe do_switch_frame should be trimmed down similarly
11466 when `norecord' is set. */
11467 static void
11468 fast_set_selected_frame (Lisp_Object frame)
11469 {
11470 if (!EQ (selected_frame, frame))
11471 {
11472 selected_frame = frame;
11473 selected_window = XFRAME (frame)->selected_window;
11474 }
11475 }
11476
11477 /* Update the tool-bar item list for frame F. This has to be done
11478 before we start to fill in any display lines. Called from
11479 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11480 and restore it here. */
11481
11482 static void
11483 update_tool_bar (struct frame *f, int save_match_data)
11484 {
11485 #if defined (USE_GTK) || defined (HAVE_NS)
11486 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11487 #else
11488 int do_update = WINDOWP (f->tool_bar_window)
11489 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11490 #endif
11491
11492 if (do_update)
11493 {
11494 Lisp_Object window;
11495 struct window *w;
11496
11497 window = FRAME_SELECTED_WINDOW (f);
11498 w = XWINDOW (window);
11499
11500 /* If the user has switched buffers or windows, we need to
11501 recompute to reflect the new bindings. But we'll
11502 recompute when update_mode_lines is set too; that means
11503 that people can use force-mode-line-update to request
11504 that the menu bar be recomputed. The adverse effect on
11505 the rest of the redisplay algorithm is about the same as
11506 windows_or_buffers_changed anyway. */
11507 if (windows_or_buffers_changed
11508 || w->update_mode_line
11509 || update_mode_lines
11510 || window_buffer_changed (w))
11511 {
11512 struct buffer *prev = current_buffer;
11513 ptrdiff_t count = SPECPDL_INDEX ();
11514 Lisp_Object frame, new_tool_bar;
11515 int new_n_tool_bar;
11516 struct gcpro gcpro1;
11517
11518 /* Set current_buffer to the buffer of the selected
11519 window of the frame, so that we get the right local
11520 keymaps. */
11521 set_buffer_internal_1 (XBUFFER (w->contents));
11522
11523 /* Save match data, if we must. */
11524 if (save_match_data)
11525 record_unwind_save_match_data ();
11526
11527 /* Make sure that we don't accidentally use bogus keymaps. */
11528 if (NILP (Voverriding_local_map_menu_flag))
11529 {
11530 specbind (Qoverriding_terminal_local_map, Qnil);
11531 specbind (Qoverriding_local_map, Qnil);
11532 }
11533
11534 GCPRO1 (new_tool_bar);
11535
11536 /* We must temporarily set the selected frame to this frame
11537 before calling tool_bar_items, because the calculation of
11538 the tool-bar keymap uses the selected frame (see
11539 `tool-bar-make-keymap' in tool-bar.el). */
11540 eassert (EQ (selected_window,
11541 /* Since we only explicitly preserve selected_frame,
11542 check that selected_window would be redundant. */
11543 XFRAME (selected_frame)->selected_window));
11544 record_unwind_protect (fast_set_selected_frame, selected_frame);
11545 XSETFRAME (frame, f);
11546 fast_set_selected_frame (frame);
11547
11548 /* Build desired tool-bar items from keymaps. */
11549 new_tool_bar
11550 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11551 &new_n_tool_bar);
11552
11553 /* Redisplay the tool-bar if we changed it. */
11554 if (new_n_tool_bar != f->n_tool_bar_items
11555 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11556 {
11557 /* Redisplay that happens asynchronously due to an expose event
11558 may access f->tool_bar_items. Make sure we update both
11559 variables within BLOCK_INPUT so no such event interrupts. */
11560 block_input ();
11561 fset_tool_bar_items (f, new_tool_bar);
11562 f->n_tool_bar_items = new_n_tool_bar;
11563 w->update_mode_line = 1;
11564 unblock_input ();
11565 }
11566
11567 UNGCPRO;
11568
11569 unbind_to (count, Qnil);
11570 set_buffer_internal_1 (prev);
11571 }
11572 }
11573 }
11574
11575
11576 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11577 F's desired tool-bar contents. F->tool_bar_items must have
11578 been set up previously by calling prepare_menu_bars. */
11579
11580 static void
11581 build_desired_tool_bar_string (struct frame *f)
11582 {
11583 int i, size, size_needed;
11584 struct gcpro gcpro1, gcpro2, gcpro3;
11585 Lisp_Object image, plist, props;
11586
11587 image = plist = props = Qnil;
11588 GCPRO3 (image, plist, props);
11589
11590 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11591 Otherwise, make a new string. */
11592
11593 /* The size of the string we might be able to reuse. */
11594 size = (STRINGP (f->desired_tool_bar_string)
11595 ? SCHARS (f->desired_tool_bar_string)
11596 : 0);
11597
11598 /* We need one space in the string for each image. */
11599 size_needed = f->n_tool_bar_items;
11600
11601 /* Reuse f->desired_tool_bar_string, if possible. */
11602 if (size < size_needed || NILP (f->desired_tool_bar_string))
11603 fset_desired_tool_bar_string
11604 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11605 else
11606 {
11607 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11608 Fremove_text_properties (make_number (0), make_number (size),
11609 props, f->desired_tool_bar_string);
11610 }
11611
11612 /* Put a `display' property on the string for the images to display,
11613 put a `menu_item' property on tool-bar items with a value that
11614 is the index of the item in F's tool-bar item vector. */
11615 for (i = 0; i < f->n_tool_bar_items; ++i)
11616 {
11617 #define PROP(IDX) \
11618 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11619
11620 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11621 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11622 int hmargin, vmargin, relief, idx, end;
11623
11624 /* If image is a vector, choose the image according to the
11625 button state. */
11626 image = PROP (TOOL_BAR_ITEM_IMAGES);
11627 if (VECTORP (image))
11628 {
11629 if (enabled_p)
11630 idx = (selected_p
11631 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11632 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11633 else
11634 idx = (selected_p
11635 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11636 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11637
11638 eassert (ASIZE (image) >= idx);
11639 image = AREF (image, idx);
11640 }
11641 else
11642 idx = -1;
11643
11644 /* Ignore invalid image specifications. */
11645 if (!valid_image_p (image))
11646 continue;
11647
11648 /* Display the tool-bar button pressed, or depressed. */
11649 plist = Fcopy_sequence (XCDR (image));
11650
11651 /* Compute margin and relief to draw. */
11652 relief = (tool_bar_button_relief >= 0
11653 ? tool_bar_button_relief
11654 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11655 hmargin = vmargin = relief;
11656
11657 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11658 INT_MAX - max (hmargin, vmargin)))
11659 {
11660 hmargin += XFASTINT (Vtool_bar_button_margin);
11661 vmargin += XFASTINT (Vtool_bar_button_margin);
11662 }
11663 else if (CONSP (Vtool_bar_button_margin))
11664 {
11665 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11666 INT_MAX - hmargin))
11667 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11668
11669 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11670 INT_MAX - vmargin))
11671 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11672 }
11673
11674 if (auto_raise_tool_bar_buttons_p)
11675 {
11676 /* Add a `:relief' property to the image spec if the item is
11677 selected. */
11678 if (selected_p)
11679 {
11680 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11681 hmargin -= relief;
11682 vmargin -= relief;
11683 }
11684 }
11685 else
11686 {
11687 /* If image is selected, display it pressed, i.e. with a
11688 negative relief. If it's not selected, display it with a
11689 raised relief. */
11690 plist = Fplist_put (plist, QCrelief,
11691 (selected_p
11692 ? make_number (-relief)
11693 : make_number (relief)));
11694 hmargin -= relief;
11695 vmargin -= relief;
11696 }
11697
11698 /* Put a margin around the image. */
11699 if (hmargin || vmargin)
11700 {
11701 if (hmargin == vmargin)
11702 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11703 else
11704 plist = Fplist_put (plist, QCmargin,
11705 Fcons (make_number (hmargin),
11706 make_number (vmargin)));
11707 }
11708
11709 /* If button is not enabled, and we don't have special images
11710 for the disabled state, make the image appear disabled by
11711 applying an appropriate algorithm to it. */
11712 if (!enabled_p && idx < 0)
11713 plist = Fplist_put (plist, QCconversion, Qdisabled);
11714
11715 /* Put a `display' text property on the string for the image to
11716 display. Put a `menu-item' property on the string that gives
11717 the start of this item's properties in the tool-bar items
11718 vector. */
11719 image = Fcons (Qimage, plist);
11720 props = list4 (Qdisplay, image,
11721 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11722
11723 /* Let the last image hide all remaining spaces in the tool bar
11724 string. The string can be longer than needed when we reuse a
11725 previous string. */
11726 if (i + 1 == f->n_tool_bar_items)
11727 end = SCHARS (f->desired_tool_bar_string);
11728 else
11729 end = i + 1;
11730 Fadd_text_properties (make_number (i), make_number (end),
11731 props, f->desired_tool_bar_string);
11732 #undef PROP
11733 }
11734
11735 UNGCPRO;
11736 }
11737
11738
11739 /* Display one line of the tool-bar of frame IT->f.
11740
11741 HEIGHT specifies the desired height of the tool-bar line.
11742 If the actual height of the glyph row is less than HEIGHT, the
11743 row's height is increased to HEIGHT, and the icons are centered
11744 vertically in the new height.
11745
11746 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11747 count a final empty row in case the tool-bar width exactly matches
11748 the window width.
11749 */
11750
11751 static void
11752 display_tool_bar_line (struct it *it, int height)
11753 {
11754 struct glyph_row *row = it->glyph_row;
11755 int max_x = it->last_visible_x;
11756 struct glyph *last;
11757
11758 prepare_desired_row (row);
11759 row->y = it->current_y;
11760
11761 /* Note that this isn't made use of if the face hasn't a box,
11762 so there's no need to check the face here. */
11763 it->start_of_box_run_p = 1;
11764
11765 while (it->current_x < max_x)
11766 {
11767 int x, n_glyphs_before, i, nglyphs;
11768 struct it it_before;
11769
11770 /* Get the next display element. */
11771 if (!get_next_display_element (it))
11772 {
11773 /* Don't count empty row if we are counting needed tool-bar lines. */
11774 if (height < 0 && !it->hpos)
11775 return;
11776 break;
11777 }
11778
11779 /* Produce glyphs. */
11780 n_glyphs_before = row->used[TEXT_AREA];
11781 it_before = *it;
11782
11783 PRODUCE_GLYPHS (it);
11784
11785 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11786 i = 0;
11787 x = it_before.current_x;
11788 while (i < nglyphs)
11789 {
11790 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11791
11792 if (x + glyph->pixel_width > max_x)
11793 {
11794 /* Glyph doesn't fit on line. Backtrack. */
11795 row->used[TEXT_AREA] = n_glyphs_before;
11796 *it = it_before;
11797 /* If this is the only glyph on this line, it will never fit on the
11798 tool-bar, so skip it. But ensure there is at least one glyph,
11799 so we don't accidentally disable the tool-bar. */
11800 if (n_glyphs_before == 0
11801 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11802 break;
11803 goto out;
11804 }
11805
11806 ++it->hpos;
11807 x += glyph->pixel_width;
11808 ++i;
11809 }
11810
11811 /* Stop at line end. */
11812 if (ITERATOR_AT_END_OF_LINE_P (it))
11813 break;
11814
11815 set_iterator_to_next (it, 1);
11816 }
11817
11818 out:;
11819
11820 row->displays_text_p = row->used[TEXT_AREA] != 0;
11821
11822 /* Use default face for the border below the tool bar.
11823
11824 FIXME: When auto-resize-tool-bars is grow-only, there is
11825 no additional border below the possibly empty tool-bar lines.
11826 So to make the extra empty lines look "normal", we have to
11827 use the tool-bar face for the border too. */
11828 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
11829 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11830 it->face_id = DEFAULT_FACE_ID;
11831
11832 extend_face_to_end_of_line (it);
11833 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11834 last->right_box_line_p = 1;
11835 if (last == row->glyphs[TEXT_AREA])
11836 last->left_box_line_p = 1;
11837
11838 /* Make line the desired height and center it vertically. */
11839 if ((height -= it->max_ascent + it->max_descent) > 0)
11840 {
11841 /* Don't add more than one line height. */
11842 height %= FRAME_LINE_HEIGHT (it->f);
11843 it->max_ascent += height / 2;
11844 it->max_descent += (height + 1) / 2;
11845 }
11846
11847 compute_line_metrics (it);
11848
11849 /* If line is empty, make it occupy the rest of the tool-bar. */
11850 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
11851 {
11852 row->height = row->phys_height = it->last_visible_y - row->y;
11853 row->visible_height = row->height;
11854 row->ascent = row->phys_ascent = 0;
11855 row->extra_line_spacing = 0;
11856 }
11857
11858 row->full_width_p = 1;
11859 row->continued_p = 0;
11860 row->truncated_on_left_p = 0;
11861 row->truncated_on_right_p = 0;
11862
11863 it->current_x = it->hpos = 0;
11864 it->current_y += row->height;
11865 ++it->vpos;
11866 ++it->glyph_row;
11867 }
11868
11869
11870 /* Max tool-bar height. */
11871
11872 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11873 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11874
11875 /* Value is the number of screen lines needed to make all tool-bar
11876 items of frame F visible. The number of actual rows needed is
11877 returned in *N_ROWS if non-NULL. */
11878
11879 static int
11880 tool_bar_lines_needed (struct frame *f, int *n_rows)
11881 {
11882 struct window *w = XWINDOW (f->tool_bar_window);
11883 struct it it;
11884 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11885 the desired matrix, so use (unused) mode-line row as temporary row to
11886 avoid destroying the first tool-bar row. */
11887 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11888
11889 /* Initialize an iterator for iteration over
11890 F->desired_tool_bar_string in the tool-bar window of frame F. */
11891 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11892 it.first_visible_x = 0;
11893 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11894 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11895 it.paragraph_embedding = L2R;
11896
11897 while (!ITERATOR_AT_END_P (&it))
11898 {
11899 clear_glyph_row (temp_row);
11900 it.glyph_row = temp_row;
11901 display_tool_bar_line (&it, -1);
11902 }
11903 clear_glyph_row (temp_row);
11904
11905 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11906 if (n_rows)
11907 *n_rows = it.vpos > 0 ? it.vpos : -1;
11908
11909 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11910 }
11911
11912
11913 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11914 0, 1, 0,
11915 doc: /* Return the number of lines occupied by the tool bar of FRAME.
11916 If FRAME is nil or omitted, use the selected frame. */)
11917 (Lisp_Object frame)
11918 {
11919 struct frame *f = decode_any_frame (frame);
11920 struct window *w;
11921 int nlines = 0;
11922
11923 if (WINDOWP (f->tool_bar_window)
11924 && (w = XWINDOW (f->tool_bar_window),
11925 WINDOW_TOTAL_LINES (w) > 0))
11926 {
11927 update_tool_bar (f, 1);
11928 if (f->n_tool_bar_items)
11929 {
11930 build_desired_tool_bar_string (f);
11931 nlines = tool_bar_lines_needed (f, NULL);
11932 }
11933 }
11934
11935 return make_number (nlines);
11936 }
11937
11938
11939 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11940 height should be changed. */
11941
11942 static int
11943 redisplay_tool_bar (struct frame *f)
11944 {
11945 struct window *w;
11946 struct it it;
11947 struct glyph_row *row;
11948
11949 #if defined (USE_GTK) || defined (HAVE_NS)
11950 if (FRAME_EXTERNAL_TOOL_BAR (f))
11951 update_frame_tool_bar (f);
11952 return 0;
11953 #endif
11954
11955 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11956 do anything. This means you must start with tool-bar-lines
11957 non-zero to get the auto-sizing effect. Or in other words, you
11958 can turn off tool-bars by specifying tool-bar-lines zero. */
11959 if (!WINDOWP (f->tool_bar_window)
11960 || (w = XWINDOW (f->tool_bar_window),
11961 WINDOW_TOTAL_LINES (w) == 0))
11962 return 0;
11963
11964 /* Set up an iterator for the tool-bar window. */
11965 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11966 it.first_visible_x = 0;
11967 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11968 row = it.glyph_row;
11969
11970 /* Build a string that represents the contents of the tool-bar. */
11971 build_desired_tool_bar_string (f);
11972 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11973 /* FIXME: This should be controlled by a user option. But it
11974 doesn't make sense to have an R2L tool bar if the menu bar cannot
11975 be drawn also R2L, and making the menu bar R2L is tricky due
11976 toolkit-specific code that implements it. If an R2L tool bar is
11977 ever supported, display_tool_bar_line should also be augmented to
11978 call unproduce_glyphs like display_line and display_string
11979 do. */
11980 it.paragraph_embedding = L2R;
11981
11982 if (f->n_tool_bar_rows == 0)
11983 {
11984 int nlines;
11985
11986 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11987 nlines != WINDOW_TOTAL_LINES (w)))
11988 {
11989 Lisp_Object frame;
11990 int old_height = WINDOW_TOTAL_LINES (w);
11991
11992 XSETFRAME (frame, f);
11993 Fmodify_frame_parameters (frame,
11994 list1 (Fcons (Qtool_bar_lines,
11995 make_number (nlines))));
11996 if (WINDOW_TOTAL_LINES (w) != old_height)
11997 {
11998 clear_glyph_matrix (w->desired_matrix);
11999 fonts_changed_p = 1;
12000 return 1;
12001 }
12002 }
12003 }
12004
12005 /* Display as many lines as needed to display all tool-bar items. */
12006
12007 if (f->n_tool_bar_rows > 0)
12008 {
12009 int border, rows, height, extra;
12010
12011 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12012 border = XINT (Vtool_bar_border);
12013 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12014 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12015 else if (EQ (Vtool_bar_border, Qborder_width))
12016 border = f->border_width;
12017 else
12018 border = 0;
12019 if (border < 0)
12020 border = 0;
12021
12022 rows = f->n_tool_bar_rows;
12023 height = max (1, (it.last_visible_y - border) / rows);
12024 extra = it.last_visible_y - border - height * rows;
12025
12026 while (it.current_y < it.last_visible_y)
12027 {
12028 int h = 0;
12029 if (extra > 0 && rows-- > 0)
12030 {
12031 h = (extra + rows - 1) / rows;
12032 extra -= h;
12033 }
12034 display_tool_bar_line (&it, height + h);
12035 }
12036 }
12037 else
12038 {
12039 while (it.current_y < it.last_visible_y)
12040 display_tool_bar_line (&it, 0);
12041 }
12042
12043 /* It doesn't make much sense to try scrolling in the tool-bar
12044 window, so don't do it. */
12045 w->desired_matrix->no_scrolling_p = 1;
12046 w->must_be_updated_p = 1;
12047
12048 if (!NILP (Vauto_resize_tool_bars))
12049 {
12050 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12051 int change_height_p = 0;
12052
12053 /* If we couldn't display everything, change the tool-bar's
12054 height if there is room for more. */
12055 if (IT_STRING_CHARPOS (it) < it.end_charpos
12056 && it.current_y < max_tool_bar_height)
12057 change_height_p = 1;
12058
12059 row = it.glyph_row - 1;
12060
12061 /* If there are blank lines at the end, except for a partially
12062 visible blank line at the end that is smaller than
12063 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12064 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12065 && row->height >= FRAME_LINE_HEIGHT (f))
12066 change_height_p = 1;
12067
12068 /* If row displays tool-bar items, but is partially visible,
12069 change the tool-bar's height. */
12070 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12071 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12072 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12073 change_height_p = 1;
12074
12075 /* Resize windows as needed by changing the `tool-bar-lines'
12076 frame parameter. */
12077 if (change_height_p)
12078 {
12079 Lisp_Object frame;
12080 int old_height = WINDOW_TOTAL_LINES (w);
12081 int nrows;
12082 int nlines = tool_bar_lines_needed (f, &nrows);
12083
12084 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12085 && !f->minimize_tool_bar_window_p)
12086 ? (nlines > old_height)
12087 : (nlines != old_height));
12088 f->minimize_tool_bar_window_p = 0;
12089
12090 if (change_height_p)
12091 {
12092 XSETFRAME (frame, f);
12093 Fmodify_frame_parameters (frame,
12094 list1 (Fcons (Qtool_bar_lines,
12095 make_number (nlines))));
12096 if (WINDOW_TOTAL_LINES (w) != old_height)
12097 {
12098 clear_glyph_matrix (w->desired_matrix);
12099 f->n_tool_bar_rows = nrows;
12100 fonts_changed_p = 1;
12101 return 1;
12102 }
12103 }
12104 }
12105 }
12106
12107 f->minimize_tool_bar_window_p = 0;
12108 return 0;
12109 }
12110
12111
12112 /* Get information about the tool-bar item which is displayed in GLYPH
12113 on frame F. Return in *PROP_IDX the index where tool-bar item
12114 properties start in F->tool_bar_items. Value is zero if
12115 GLYPH doesn't display a tool-bar item. */
12116
12117 static int
12118 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12119 {
12120 Lisp_Object prop;
12121 int success_p;
12122 int charpos;
12123
12124 /* This function can be called asynchronously, which means we must
12125 exclude any possibility that Fget_text_property signals an
12126 error. */
12127 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12128 charpos = max (0, charpos);
12129
12130 /* Get the text property `menu-item' at pos. The value of that
12131 property is the start index of this item's properties in
12132 F->tool_bar_items. */
12133 prop = Fget_text_property (make_number (charpos),
12134 Qmenu_item, f->current_tool_bar_string);
12135 if (INTEGERP (prop))
12136 {
12137 *prop_idx = XINT (prop);
12138 success_p = 1;
12139 }
12140 else
12141 success_p = 0;
12142
12143 return success_p;
12144 }
12145
12146 \f
12147 /* Get information about the tool-bar item at position X/Y on frame F.
12148 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12149 the current matrix of the tool-bar window of F, or NULL if not
12150 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12151 item in F->tool_bar_items. Value is
12152
12153 -1 if X/Y is not on a tool-bar item
12154 0 if X/Y is on the same item that was highlighted before.
12155 1 otherwise. */
12156
12157 static int
12158 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12159 int *hpos, int *vpos, int *prop_idx)
12160 {
12161 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12162 struct window *w = XWINDOW (f->tool_bar_window);
12163 int area;
12164
12165 /* Find the glyph under X/Y. */
12166 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12167 if (*glyph == NULL)
12168 return -1;
12169
12170 /* Get the start of this tool-bar item's properties in
12171 f->tool_bar_items. */
12172 if (!tool_bar_item_info (f, *glyph, prop_idx))
12173 return -1;
12174
12175 /* Is mouse on the highlighted item? */
12176 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12177 && *vpos >= hlinfo->mouse_face_beg_row
12178 && *vpos <= hlinfo->mouse_face_end_row
12179 && (*vpos > hlinfo->mouse_face_beg_row
12180 || *hpos >= hlinfo->mouse_face_beg_col)
12181 && (*vpos < hlinfo->mouse_face_end_row
12182 || *hpos < hlinfo->mouse_face_end_col
12183 || hlinfo->mouse_face_past_end))
12184 return 0;
12185
12186 return 1;
12187 }
12188
12189
12190 /* EXPORT:
12191 Handle mouse button event on the tool-bar of frame F, at
12192 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12193 0 for button release. MODIFIERS is event modifiers for button
12194 release. */
12195
12196 void
12197 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12198 int modifiers)
12199 {
12200 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12201 struct window *w = XWINDOW (f->tool_bar_window);
12202 int hpos, vpos, prop_idx;
12203 struct glyph *glyph;
12204 Lisp_Object enabled_p;
12205 int ts;
12206
12207 /* If not on the highlighted tool-bar item, and mouse-highlight is
12208 non-nil, return. This is so we generate the tool-bar button
12209 click only when the mouse button is released on the same item as
12210 where it was pressed. However, when mouse-highlight is disabled,
12211 generate the click when the button is released regardless of the
12212 highlight, since tool-bar items are not highlighted in that
12213 case. */
12214 frame_to_window_pixel_xy (w, &x, &y);
12215 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12216 if (ts == -1
12217 || (ts != 0 && !NILP (Vmouse_highlight)))
12218 return;
12219
12220 /* When mouse-highlight is off, generate the click for the item
12221 where the button was pressed, disregarding where it was
12222 released. */
12223 if (NILP (Vmouse_highlight) && !down_p)
12224 prop_idx = last_tool_bar_item;
12225
12226 /* If item is disabled, do nothing. */
12227 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12228 if (NILP (enabled_p))
12229 return;
12230
12231 if (down_p)
12232 {
12233 /* Show item in pressed state. */
12234 if (!NILP (Vmouse_highlight))
12235 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12236 last_tool_bar_item = prop_idx;
12237 }
12238 else
12239 {
12240 Lisp_Object key, frame;
12241 struct input_event event;
12242 EVENT_INIT (event);
12243
12244 /* Show item in released state. */
12245 if (!NILP (Vmouse_highlight))
12246 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12247
12248 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12249
12250 XSETFRAME (frame, f);
12251 event.kind = TOOL_BAR_EVENT;
12252 event.frame_or_window = frame;
12253 event.arg = frame;
12254 kbd_buffer_store_event (&event);
12255
12256 event.kind = TOOL_BAR_EVENT;
12257 event.frame_or_window = frame;
12258 event.arg = key;
12259 event.modifiers = modifiers;
12260 kbd_buffer_store_event (&event);
12261 last_tool_bar_item = -1;
12262 }
12263 }
12264
12265
12266 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12267 tool-bar window-relative coordinates X/Y. Called from
12268 note_mouse_highlight. */
12269
12270 static void
12271 note_tool_bar_highlight (struct frame *f, int x, int y)
12272 {
12273 Lisp_Object window = f->tool_bar_window;
12274 struct window *w = XWINDOW (window);
12275 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12276 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12277 int hpos, vpos;
12278 struct glyph *glyph;
12279 struct glyph_row *row;
12280 int i;
12281 Lisp_Object enabled_p;
12282 int prop_idx;
12283 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12284 int mouse_down_p, rc;
12285
12286 /* Function note_mouse_highlight is called with negative X/Y
12287 values when mouse moves outside of the frame. */
12288 if (x <= 0 || y <= 0)
12289 {
12290 clear_mouse_face (hlinfo);
12291 return;
12292 }
12293
12294 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12295 if (rc < 0)
12296 {
12297 /* Not on tool-bar item. */
12298 clear_mouse_face (hlinfo);
12299 return;
12300 }
12301 else if (rc == 0)
12302 /* On same tool-bar item as before. */
12303 goto set_help_echo;
12304
12305 clear_mouse_face (hlinfo);
12306
12307 /* Mouse is down, but on different tool-bar item? */
12308 mouse_down_p = (dpyinfo->grabbed
12309 && f == last_mouse_frame
12310 && FRAME_LIVE_P (f));
12311 if (mouse_down_p
12312 && last_tool_bar_item != prop_idx)
12313 return;
12314
12315 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12316
12317 /* If tool-bar item is not enabled, don't highlight it. */
12318 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12319 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12320 {
12321 /* Compute the x-position of the glyph. In front and past the
12322 image is a space. We include this in the highlighted area. */
12323 row = MATRIX_ROW (w->current_matrix, vpos);
12324 for (i = x = 0; i < hpos; ++i)
12325 x += row->glyphs[TEXT_AREA][i].pixel_width;
12326
12327 /* Record this as the current active region. */
12328 hlinfo->mouse_face_beg_col = hpos;
12329 hlinfo->mouse_face_beg_row = vpos;
12330 hlinfo->mouse_face_beg_x = x;
12331 hlinfo->mouse_face_past_end = 0;
12332
12333 hlinfo->mouse_face_end_col = hpos + 1;
12334 hlinfo->mouse_face_end_row = vpos;
12335 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12336 hlinfo->mouse_face_window = window;
12337 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12338
12339 /* Display it as active. */
12340 show_mouse_face (hlinfo, draw);
12341 }
12342
12343 set_help_echo:
12344
12345 /* Set help_echo_string to a help string to display for this tool-bar item.
12346 XTread_socket does the rest. */
12347 help_echo_object = help_echo_window = Qnil;
12348 help_echo_pos = -1;
12349 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12350 if (NILP (help_echo_string))
12351 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12352 }
12353
12354 #endif /* HAVE_WINDOW_SYSTEM */
12355
12356
12357 \f
12358 /************************************************************************
12359 Horizontal scrolling
12360 ************************************************************************/
12361
12362 static int hscroll_window_tree (Lisp_Object);
12363 static int hscroll_windows (Lisp_Object);
12364
12365 /* For all leaf windows in the window tree rooted at WINDOW, set their
12366 hscroll value so that PT is (i) visible in the window, and (ii) so
12367 that it is not within a certain margin at the window's left and
12368 right border. Value is non-zero if any window's hscroll has been
12369 changed. */
12370
12371 static int
12372 hscroll_window_tree (Lisp_Object window)
12373 {
12374 int hscrolled_p = 0;
12375 int hscroll_relative_p = FLOATP (Vhscroll_step);
12376 int hscroll_step_abs = 0;
12377 double hscroll_step_rel = 0;
12378
12379 if (hscroll_relative_p)
12380 {
12381 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12382 if (hscroll_step_rel < 0)
12383 {
12384 hscroll_relative_p = 0;
12385 hscroll_step_abs = 0;
12386 }
12387 }
12388 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12389 {
12390 hscroll_step_abs = XINT (Vhscroll_step);
12391 if (hscroll_step_abs < 0)
12392 hscroll_step_abs = 0;
12393 }
12394 else
12395 hscroll_step_abs = 0;
12396
12397 while (WINDOWP (window))
12398 {
12399 struct window *w = XWINDOW (window);
12400
12401 if (WINDOWP (w->contents))
12402 hscrolled_p |= hscroll_window_tree (w->contents);
12403 else if (w->cursor.vpos >= 0)
12404 {
12405 int h_margin;
12406 int text_area_width;
12407 struct glyph_row *current_cursor_row
12408 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12409 struct glyph_row *desired_cursor_row
12410 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12411 struct glyph_row *cursor_row
12412 = (desired_cursor_row->enabled_p
12413 ? desired_cursor_row
12414 : current_cursor_row);
12415 int row_r2l_p = cursor_row->reversed_p;
12416
12417 text_area_width = window_box_width (w, TEXT_AREA);
12418
12419 /* Scroll when cursor is inside this scroll margin. */
12420 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12421
12422 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12423 /* For left-to-right rows, hscroll when cursor is either
12424 (i) inside the right hscroll margin, or (ii) if it is
12425 inside the left margin and the window is already
12426 hscrolled. */
12427 && ((!row_r2l_p
12428 && ((w->hscroll
12429 && w->cursor.x <= h_margin)
12430 || (cursor_row->enabled_p
12431 && cursor_row->truncated_on_right_p
12432 && (w->cursor.x >= text_area_width - h_margin))))
12433 /* For right-to-left rows, the logic is similar,
12434 except that rules for scrolling to left and right
12435 are reversed. E.g., if cursor.x <= h_margin, we
12436 need to hscroll "to the right" unconditionally,
12437 and that will scroll the screen to the left so as
12438 to reveal the next portion of the row. */
12439 || (row_r2l_p
12440 && ((cursor_row->enabled_p
12441 /* FIXME: It is confusing to set the
12442 truncated_on_right_p flag when R2L rows
12443 are actually truncated on the left. */
12444 && cursor_row->truncated_on_right_p
12445 && w->cursor.x <= h_margin)
12446 || (w->hscroll
12447 && (w->cursor.x >= text_area_width - h_margin))))))
12448 {
12449 struct it it;
12450 ptrdiff_t hscroll;
12451 struct buffer *saved_current_buffer;
12452 ptrdiff_t pt;
12453 int wanted_x;
12454
12455 /* Find point in a display of infinite width. */
12456 saved_current_buffer = current_buffer;
12457 current_buffer = XBUFFER (w->contents);
12458
12459 if (w == XWINDOW (selected_window))
12460 pt = PT;
12461 else
12462 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12463
12464 /* Move iterator to pt starting at cursor_row->start in
12465 a line with infinite width. */
12466 init_to_row_start (&it, w, cursor_row);
12467 it.last_visible_x = INFINITY;
12468 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12469 current_buffer = saved_current_buffer;
12470
12471 /* Position cursor in window. */
12472 if (!hscroll_relative_p && hscroll_step_abs == 0)
12473 hscroll = max (0, (it.current_x
12474 - (ITERATOR_AT_END_OF_LINE_P (&it)
12475 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12476 : (text_area_width / 2))))
12477 / FRAME_COLUMN_WIDTH (it.f);
12478 else if ((!row_r2l_p
12479 && w->cursor.x >= text_area_width - h_margin)
12480 || (row_r2l_p && w->cursor.x <= h_margin))
12481 {
12482 if (hscroll_relative_p)
12483 wanted_x = text_area_width * (1 - hscroll_step_rel)
12484 - h_margin;
12485 else
12486 wanted_x = text_area_width
12487 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12488 - h_margin;
12489 hscroll
12490 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12491 }
12492 else
12493 {
12494 if (hscroll_relative_p)
12495 wanted_x = text_area_width * hscroll_step_rel
12496 + h_margin;
12497 else
12498 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12499 + h_margin;
12500 hscroll
12501 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12502 }
12503 hscroll = max (hscroll, w->min_hscroll);
12504
12505 /* Don't prevent redisplay optimizations if hscroll
12506 hasn't changed, as it will unnecessarily slow down
12507 redisplay. */
12508 if (w->hscroll != hscroll)
12509 {
12510 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12511 w->hscroll = hscroll;
12512 hscrolled_p = 1;
12513 }
12514 }
12515 }
12516
12517 window = w->next;
12518 }
12519
12520 /* Value is non-zero if hscroll of any leaf window has been changed. */
12521 return hscrolled_p;
12522 }
12523
12524
12525 /* Set hscroll so that cursor is visible and not inside horizontal
12526 scroll margins for all windows in the tree rooted at WINDOW. See
12527 also hscroll_window_tree above. Value is non-zero if any window's
12528 hscroll has been changed. If it has, desired matrices on the frame
12529 of WINDOW are cleared. */
12530
12531 static int
12532 hscroll_windows (Lisp_Object window)
12533 {
12534 int hscrolled_p = hscroll_window_tree (window);
12535 if (hscrolled_p)
12536 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12537 return hscrolled_p;
12538 }
12539
12540
12541 \f
12542 /************************************************************************
12543 Redisplay
12544 ************************************************************************/
12545
12546 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12547 to a non-zero value. This is sometimes handy to have in a debugger
12548 session. */
12549
12550 #ifdef GLYPH_DEBUG
12551
12552 /* First and last unchanged row for try_window_id. */
12553
12554 static int debug_first_unchanged_at_end_vpos;
12555 static int debug_last_unchanged_at_beg_vpos;
12556
12557 /* Delta vpos and y. */
12558
12559 static int debug_dvpos, debug_dy;
12560
12561 /* Delta in characters and bytes for try_window_id. */
12562
12563 static ptrdiff_t debug_delta, debug_delta_bytes;
12564
12565 /* Values of window_end_pos and window_end_vpos at the end of
12566 try_window_id. */
12567
12568 static ptrdiff_t debug_end_vpos;
12569
12570 /* Append a string to W->desired_matrix->method. FMT is a printf
12571 format string. If trace_redisplay_p is non-zero also printf the
12572 resulting string to stderr. */
12573
12574 static void debug_method_add (struct window *, char const *, ...)
12575 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12576
12577 static void
12578 debug_method_add (struct window *w, char const *fmt, ...)
12579 {
12580 void *ptr = w;
12581 char *method = w->desired_matrix->method;
12582 int len = strlen (method);
12583 int size = sizeof w->desired_matrix->method;
12584 int remaining = size - len - 1;
12585 va_list ap;
12586
12587 if (len && remaining)
12588 {
12589 method[len] = '|';
12590 --remaining, ++len;
12591 }
12592
12593 va_start (ap, fmt);
12594 vsnprintf (method + len, remaining + 1, fmt, ap);
12595 va_end (ap);
12596
12597 if (trace_redisplay_p)
12598 fprintf (stderr, "%p (%s): %s\n",
12599 ptr,
12600 ((BUFFERP (w->contents)
12601 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12602 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12603 : "no buffer"),
12604 method + len);
12605 }
12606
12607 #endif /* GLYPH_DEBUG */
12608
12609
12610 /* Value is non-zero if all changes in window W, which displays
12611 current_buffer, are in the text between START and END. START is a
12612 buffer position, END is given as a distance from Z. Used in
12613 redisplay_internal for display optimization. */
12614
12615 static int
12616 text_outside_line_unchanged_p (struct window *w,
12617 ptrdiff_t start, ptrdiff_t end)
12618 {
12619 int unchanged_p = 1;
12620
12621 /* If text or overlays have changed, see where. */
12622 if (window_outdated (w))
12623 {
12624 /* Gap in the line? */
12625 if (GPT < start || Z - GPT < end)
12626 unchanged_p = 0;
12627
12628 /* Changes start in front of the line, or end after it? */
12629 if (unchanged_p
12630 && (BEG_UNCHANGED < start - 1
12631 || END_UNCHANGED < end))
12632 unchanged_p = 0;
12633
12634 /* If selective display, can't optimize if changes start at the
12635 beginning of the line. */
12636 if (unchanged_p
12637 && INTEGERP (BVAR (current_buffer, selective_display))
12638 && XINT (BVAR (current_buffer, selective_display)) > 0
12639 && (BEG_UNCHANGED < start || GPT <= start))
12640 unchanged_p = 0;
12641
12642 /* If there are overlays at the start or end of the line, these
12643 may have overlay strings with newlines in them. A change at
12644 START, for instance, may actually concern the display of such
12645 overlay strings as well, and they are displayed on different
12646 lines. So, quickly rule out this case. (For the future, it
12647 might be desirable to implement something more telling than
12648 just BEG/END_UNCHANGED.) */
12649 if (unchanged_p)
12650 {
12651 if (BEG + BEG_UNCHANGED == start
12652 && overlay_touches_p (start))
12653 unchanged_p = 0;
12654 if (END_UNCHANGED == end
12655 && overlay_touches_p (Z - end))
12656 unchanged_p = 0;
12657 }
12658
12659 /* Under bidi reordering, adding or deleting a character in the
12660 beginning of a paragraph, before the first strong directional
12661 character, can change the base direction of the paragraph (unless
12662 the buffer specifies a fixed paragraph direction), which will
12663 require to redisplay the whole paragraph. It might be worthwhile
12664 to find the paragraph limits and widen the range of redisplayed
12665 lines to that, but for now just give up this optimization. */
12666 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
12667 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
12668 unchanged_p = 0;
12669 }
12670
12671 return unchanged_p;
12672 }
12673
12674
12675 /* Do a frame update, taking possible shortcuts into account. This is
12676 the main external entry point for redisplay.
12677
12678 If the last redisplay displayed an echo area message and that message
12679 is no longer requested, we clear the echo area or bring back the
12680 mini-buffer if that is in use. */
12681
12682 void
12683 redisplay (void)
12684 {
12685 redisplay_internal ();
12686 }
12687
12688
12689 static Lisp_Object
12690 overlay_arrow_string_or_property (Lisp_Object var)
12691 {
12692 Lisp_Object val;
12693
12694 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12695 return val;
12696
12697 return Voverlay_arrow_string;
12698 }
12699
12700 /* Return 1 if there are any overlay-arrows in current_buffer. */
12701 static int
12702 overlay_arrow_in_current_buffer_p (void)
12703 {
12704 Lisp_Object vlist;
12705
12706 for (vlist = Voverlay_arrow_variable_list;
12707 CONSP (vlist);
12708 vlist = XCDR (vlist))
12709 {
12710 Lisp_Object var = XCAR (vlist);
12711 Lisp_Object val;
12712
12713 if (!SYMBOLP (var))
12714 continue;
12715 val = find_symbol_value (var);
12716 if (MARKERP (val)
12717 && current_buffer == XMARKER (val)->buffer)
12718 return 1;
12719 }
12720 return 0;
12721 }
12722
12723
12724 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12725 has changed. */
12726
12727 static int
12728 overlay_arrows_changed_p (void)
12729 {
12730 Lisp_Object vlist;
12731
12732 for (vlist = Voverlay_arrow_variable_list;
12733 CONSP (vlist);
12734 vlist = XCDR (vlist))
12735 {
12736 Lisp_Object var = XCAR (vlist);
12737 Lisp_Object val, pstr;
12738
12739 if (!SYMBOLP (var))
12740 continue;
12741 val = find_symbol_value (var);
12742 if (!MARKERP (val))
12743 continue;
12744 if (! EQ (COERCE_MARKER (val),
12745 Fget (var, Qlast_arrow_position))
12746 || ! (pstr = overlay_arrow_string_or_property (var),
12747 EQ (pstr, Fget (var, Qlast_arrow_string))))
12748 return 1;
12749 }
12750 return 0;
12751 }
12752
12753 /* Mark overlay arrows to be updated on next redisplay. */
12754
12755 static void
12756 update_overlay_arrows (int up_to_date)
12757 {
12758 Lisp_Object vlist;
12759
12760 for (vlist = Voverlay_arrow_variable_list;
12761 CONSP (vlist);
12762 vlist = XCDR (vlist))
12763 {
12764 Lisp_Object var = XCAR (vlist);
12765
12766 if (!SYMBOLP (var))
12767 continue;
12768
12769 if (up_to_date > 0)
12770 {
12771 Lisp_Object val = find_symbol_value (var);
12772 Fput (var, Qlast_arrow_position,
12773 COERCE_MARKER (val));
12774 Fput (var, Qlast_arrow_string,
12775 overlay_arrow_string_or_property (var));
12776 }
12777 else if (up_to_date < 0
12778 || !NILP (Fget (var, Qlast_arrow_position)))
12779 {
12780 Fput (var, Qlast_arrow_position, Qt);
12781 Fput (var, Qlast_arrow_string, Qt);
12782 }
12783 }
12784 }
12785
12786
12787 /* Return overlay arrow string to display at row.
12788 Return integer (bitmap number) for arrow bitmap in left fringe.
12789 Return nil if no overlay arrow. */
12790
12791 static Lisp_Object
12792 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12793 {
12794 Lisp_Object vlist;
12795
12796 for (vlist = Voverlay_arrow_variable_list;
12797 CONSP (vlist);
12798 vlist = XCDR (vlist))
12799 {
12800 Lisp_Object var = XCAR (vlist);
12801 Lisp_Object val;
12802
12803 if (!SYMBOLP (var))
12804 continue;
12805
12806 val = find_symbol_value (var);
12807
12808 if (MARKERP (val)
12809 && current_buffer == XMARKER (val)->buffer
12810 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12811 {
12812 if (FRAME_WINDOW_P (it->f)
12813 /* FIXME: if ROW->reversed_p is set, this should test
12814 the right fringe, not the left one. */
12815 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12816 {
12817 #ifdef HAVE_WINDOW_SYSTEM
12818 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12819 {
12820 int fringe_bitmap;
12821 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12822 return make_number (fringe_bitmap);
12823 }
12824 #endif
12825 return make_number (-1); /* Use default arrow bitmap. */
12826 }
12827 return overlay_arrow_string_or_property (var);
12828 }
12829 }
12830
12831 return Qnil;
12832 }
12833
12834 /* Return 1 if point moved out of or into a composition. Otherwise
12835 return 0. PREV_BUF and PREV_PT are the last point buffer and
12836 position. BUF and PT are the current point buffer and position. */
12837
12838 static int
12839 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12840 struct buffer *buf, ptrdiff_t pt)
12841 {
12842 ptrdiff_t start, end;
12843 Lisp_Object prop;
12844 Lisp_Object buffer;
12845
12846 XSETBUFFER (buffer, buf);
12847 /* Check a composition at the last point if point moved within the
12848 same buffer. */
12849 if (prev_buf == buf)
12850 {
12851 if (prev_pt == pt)
12852 /* Point didn't move. */
12853 return 0;
12854
12855 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12856 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12857 && composition_valid_p (start, end, prop)
12858 && start < prev_pt && end > prev_pt)
12859 /* The last point was within the composition. Return 1 iff
12860 point moved out of the composition. */
12861 return (pt <= start || pt >= end);
12862 }
12863
12864 /* Check a composition at the current point. */
12865 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12866 && find_composition (pt, -1, &start, &end, &prop, buffer)
12867 && composition_valid_p (start, end, prop)
12868 && start < pt && end > pt);
12869 }
12870
12871 /* Reconsider the clip changes of buffer which is displayed in W. */
12872
12873 static void
12874 reconsider_clip_changes (struct window *w)
12875 {
12876 struct buffer *b = XBUFFER (w->contents);
12877
12878 if (b->clip_changed
12879 && w->window_end_valid
12880 && w->current_matrix->buffer == b
12881 && w->current_matrix->zv == BUF_ZV (b)
12882 && w->current_matrix->begv == BUF_BEGV (b))
12883 b->clip_changed = 0;
12884
12885 /* If display wasn't paused, and W is not a tool bar window, see if
12886 point has been moved into or out of a composition. In that case,
12887 we set b->clip_changed to 1 to force updating the screen. If
12888 b->clip_changed has already been set to 1, we can skip this
12889 check. */
12890 if (!b->clip_changed && w->window_end_valid)
12891 {
12892 ptrdiff_t pt = (w == XWINDOW (selected_window)
12893 ? PT : marker_position (w->pointm));
12894
12895 if ((w->current_matrix->buffer != b || pt != w->last_point)
12896 && check_point_in_composition (w->current_matrix->buffer,
12897 w->last_point, b, pt))
12898 b->clip_changed = 1;
12899 }
12900 }
12901
12902 #define STOP_POLLING \
12903 do { if (! polling_stopped_here) stop_polling (); \
12904 polling_stopped_here = 1; } while (0)
12905
12906 #define RESUME_POLLING \
12907 do { if (polling_stopped_here) start_polling (); \
12908 polling_stopped_here = 0; } while (0)
12909
12910
12911 /* Perhaps in the future avoid recentering windows if it
12912 is not necessary; currently that causes some problems. */
12913
12914 static void
12915 redisplay_internal (void)
12916 {
12917 struct window *w = XWINDOW (selected_window);
12918 struct window *sw;
12919 struct frame *fr;
12920 int pending;
12921 bool must_finish = 0, match_p;
12922 struct text_pos tlbufpos, tlendpos;
12923 int number_of_visible_frames;
12924 ptrdiff_t count;
12925 struct frame *sf;
12926 int polling_stopped_here = 0;
12927 Lisp_Object tail, frame;
12928
12929 /* Non-zero means redisplay has to consider all windows on all
12930 frames. Zero means, only selected_window is considered. */
12931 int consider_all_windows_p;
12932
12933 /* Non-zero means redisplay has to redisplay the miniwindow. */
12934 int update_miniwindow_p = 0;
12935
12936 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12937
12938 /* No redisplay if running in batch mode or frame is not yet fully
12939 initialized, or redisplay is explicitly turned off by setting
12940 Vinhibit_redisplay. */
12941 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12942 || !NILP (Vinhibit_redisplay))
12943 return;
12944
12945 /* Don't examine these until after testing Vinhibit_redisplay.
12946 When Emacs is shutting down, perhaps because its connection to
12947 X has dropped, we should not look at them at all. */
12948 fr = XFRAME (w->frame);
12949 sf = SELECTED_FRAME ();
12950
12951 if (!fr->glyphs_initialized_p)
12952 return;
12953
12954 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12955 if (popup_activated ())
12956 return;
12957 #endif
12958
12959 /* I don't think this happens but let's be paranoid. */
12960 if (redisplaying_p)
12961 return;
12962
12963 /* Record a function that clears redisplaying_p
12964 when we leave this function. */
12965 count = SPECPDL_INDEX ();
12966 record_unwind_protect_void (unwind_redisplay);
12967 redisplaying_p = 1;
12968 specbind (Qinhibit_free_realized_faces, Qnil);
12969
12970 /* Record this function, so it appears on the profiler's backtraces. */
12971 record_in_backtrace (Qredisplay_internal, &Qnil, 0);
12972
12973 FOR_EACH_FRAME (tail, frame)
12974 XFRAME (frame)->already_hscrolled_p = 0;
12975
12976 retry:
12977 /* Remember the currently selected window. */
12978 sw = w;
12979
12980 pending = 0;
12981 last_escape_glyph_frame = NULL;
12982 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12983 last_glyphless_glyph_frame = NULL;
12984 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12985
12986 /* If new fonts have been loaded that make a glyph matrix adjustment
12987 necessary, do it. */
12988 if (fonts_changed_p)
12989 {
12990 adjust_glyphs (NULL);
12991 ++windows_or_buffers_changed;
12992 fonts_changed_p = 0;
12993 }
12994
12995 /* If face_change_count is non-zero, init_iterator will free all
12996 realized faces, which includes the faces referenced from current
12997 matrices. So, we can't reuse current matrices in this case. */
12998 if (face_change_count)
12999 ++windows_or_buffers_changed;
13000
13001 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13002 && FRAME_TTY (sf)->previous_frame != sf)
13003 {
13004 /* Since frames on a single ASCII terminal share the same
13005 display area, displaying a different frame means redisplay
13006 the whole thing. */
13007 windows_or_buffers_changed++;
13008 SET_FRAME_GARBAGED (sf);
13009 #ifndef DOS_NT
13010 set_tty_color_mode (FRAME_TTY (sf), sf);
13011 #endif
13012 FRAME_TTY (sf)->previous_frame = sf;
13013 }
13014
13015 /* Set the visible flags for all frames. Do this before checking for
13016 resized or garbaged frames; they want to know if their frames are
13017 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13018 number_of_visible_frames = 0;
13019
13020 FOR_EACH_FRAME (tail, frame)
13021 {
13022 struct frame *f = XFRAME (frame);
13023
13024 if (FRAME_VISIBLE_P (f))
13025 ++number_of_visible_frames;
13026 clear_desired_matrices (f);
13027 }
13028
13029 /* Notice any pending interrupt request to change frame size. */
13030 do_pending_window_change (1);
13031
13032 /* do_pending_window_change could change the selected_window due to
13033 frame resizing which makes the selected window too small. */
13034 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13035 sw = w;
13036
13037 /* Clear frames marked as garbaged. */
13038 clear_garbaged_frames ();
13039
13040 /* Build menubar and tool-bar items. */
13041 if (NILP (Vmemory_full))
13042 prepare_menu_bars ();
13043
13044 if (windows_or_buffers_changed)
13045 update_mode_lines++;
13046
13047 reconsider_clip_changes (w);
13048
13049 /* In most cases selected window displays current buffer. */
13050 match_p = XBUFFER (w->contents) == current_buffer;
13051 if (match_p)
13052 {
13053 ptrdiff_t count1;
13054
13055 /* Detect case that we need to write or remove a star in the mode line. */
13056 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13057 {
13058 w->update_mode_line = 1;
13059 if (buffer_shared_and_changed ())
13060 update_mode_lines++;
13061 }
13062
13063 /* Avoid invocation of point motion hooks by `current_column' below. */
13064 count1 = SPECPDL_INDEX ();
13065 specbind (Qinhibit_point_motion_hooks, Qt);
13066
13067 if (mode_line_update_needed (w))
13068 w->update_mode_line = 1;
13069
13070 unbind_to (count1, Qnil);
13071 }
13072
13073 consider_all_windows_p = (update_mode_lines
13074 || buffer_shared_and_changed ()
13075 || cursor_type_changed);
13076
13077 /* If specs for an arrow have changed, do thorough redisplay
13078 to ensure we remove any arrow that should no longer exist. */
13079 if (overlay_arrows_changed_p ())
13080 consider_all_windows_p = windows_or_buffers_changed = 1;
13081
13082 /* Normally the message* functions will have already displayed and
13083 updated the echo area, but the frame may have been trashed, or
13084 the update may have been preempted, so display the echo area
13085 again here. Checking message_cleared_p captures the case that
13086 the echo area should be cleared. */
13087 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13088 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13089 || (message_cleared_p
13090 && minibuf_level == 0
13091 /* If the mini-window is currently selected, this means the
13092 echo-area doesn't show through. */
13093 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13094 {
13095 int window_height_changed_p = echo_area_display (0);
13096
13097 if (message_cleared_p)
13098 update_miniwindow_p = 1;
13099
13100 must_finish = 1;
13101
13102 /* If we don't display the current message, don't clear the
13103 message_cleared_p flag, because, if we did, we wouldn't clear
13104 the echo area in the next redisplay which doesn't preserve
13105 the echo area. */
13106 if (!display_last_displayed_message_p)
13107 message_cleared_p = 0;
13108
13109 if (fonts_changed_p)
13110 goto retry;
13111 else if (window_height_changed_p)
13112 {
13113 consider_all_windows_p = 1;
13114 ++update_mode_lines;
13115 ++windows_or_buffers_changed;
13116
13117 /* If window configuration was changed, frames may have been
13118 marked garbaged. Clear them or we will experience
13119 surprises wrt scrolling. */
13120 clear_garbaged_frames ();
13121 }
13122 }
13123 else if (EQ (selected_window, minibuf_window)
13124 && (current_buffer->clip_changed || window_outdated (w))
13125 && resize_mini_window (w, 0))
13126 {
13127 /* Resized active mini-window to fit the size of what it is
13128 showing if its contents might have changed. */
13129 must_finish = 1;
13130 /* FIXME: this causes all frames to be updated, which seems unnecessary
13131 since only the current frame needs to be considered. This function
13132 needs to be rewritten with two variables, consider_all_windows and
13133 consider_all_frames. */
13134 consider_all_windows_p = 1;
13135 ++windows_or_buffers_changed;
13136 ++update_mode_lines;
13137
13138 /* If window configuration was changed, frames may have been
13139 marked garbaged. Clear them or we will experience
13140 surprises wrt scrolling. */
13141 clear_garbaged_frames ();
13142 }
13143
13144 /* If showing the region, and mark has changed, we must redisplay
13145 the whole window. The assignment to this_line_start_pos prevents
13146 the optimization directly below this if-statement. */
13147 if (((!NILP (Vtransient_mark_mode)
13148 && !NILP (BVAR (XBUFFER (w->contents), mark_active)))
13149 != (w->region_showing > 0))
13150 || (w->region_showing
13151 && w->region_showing
13152 != XINT (Fmarker_position (BVAR (XBUFFER (w->contents), mark)))))
13153 CHARPOS (this_line_start_pos) = 0;
13154
13155 /* Optimize the case that only the line containing the cursor in the
13156 selected window has changed. Variables starting with this_ are
13157 set in display_line and record information about the line
13158 containing the cursor. */
13159 tlbufpos = this_line_start_pos;
13160 tlendpos = this_line_end_pos;
13161 if (!consider_all_windows_p
13162 && CHARPOS (tlbufpos) > 0
13163 && !w->update_mode_line
13164 && !current_buffer->clip_changed
13165 && !current_buffer->prevent_redisplay_optimizations_p
13166 && FRAME_VISIBLE_P (XFRAME (w->frame))
13167 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13168 /* Make sure recorded data applies to current buffer, etc. */
13169 && this_line_buffer == current_buffer
13170 && match_p
13171 && !w->force_start
13172 && !w->optional_new_start
13173 /* Point must be on the line that we have info recorded about. */
13174 && PT >= CHARPOS (tlbufpos)
13175 && PT <= Z - CHARPOS (tlendpos)
13176 /* All text outside that line, including its final newline,
13177 must be unchanged. */
13178 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13179 CHARPOS (tlendpos)))
13180 {
13181 if (CHARPOS (tlbufpos) > BEGV
13182 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13183 && (CHARPOS (tlbufpos) == ZV
13184 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13185 /* Former continuation line has disappeared by becoming empty. */
13186 goto cancel;
13187 else if (window_outdated (w) || MINI_WINDOW_P (w))
13188 {
13189 /* We have to handle the case of continuation around a
13190 wide-column character (see the comment in indent.c around
13191 line 1340).
13192
13193 For instance, in the following case:
13194
13195 -------- Insert --------
13196 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13197 J_I_ ==> J_I_ `^^' are cursors.
13198 ^^ ^^
13199 -------- --------
13200
13201 As we have to redraw the line above, we cannot use this
13202 optimization. */
13203
13204 struct it it;
13205 int line_height_before = this_line_pixel_height;
13206
13207 /* Note that start_display will handle the case that the
13208 line starting at tlbufpos is a continuation line. */
13209 start_display (&it, w, tlbufpos);
13210
13211 /* Implementation note: It this still necessary? */
13212 if (it.current_x != this_line_start_x)
13213 goto cancel;
13214
13215 TRACE ((stderr, "trying display optimization 1\n"));
13216 w->cursor.vpos = -1;
13217 overlay_arrow_seen = 0;
13218 it.vpos = this_line_vpos;
13219 it.current_y = this_line_y;
13220 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13221 display_line (&it);
13222
13223 /* If line contains point, is not continued,
13224 and ends at same distance from eob as before, we win. */
13225 if (w->cursor.vpos >= 0
13226 /* Line is not continued, otherwise this_line_start_pos
13227 would have been set to 0 in display_line. */
13228 && CHARPOS (this_line_start_pos)
13229 /* Line ends as before. */
13230 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13231 /* Line has same height as before. Otherwise other lines
13232 would have to be shifted up or down. */
13233 && this_line_pixel_height == line_height_before)
13234 {
13235 /* If this is not the window's last line, we must adjust
13236 the charstarts of the lines below. */
13237 if (it.current_y < it.last_visible_y)
13238 {
13239 struct glyph_row *row
13240 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13241 ptrdiff_t delta, delta_bytes;
13242
13243 /* We used to distinguish between two cases here,
13244 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13245 when the line ends in a newline or the end of the
13246 buffer's accessible portion. But both cases did
13247 the same, so they were collapsed. */
13248 delta = (Z
13249 - CHARPOS (tlendpos)
13250 - MATRIX_ROW_START_CHARPOS (row));
13251 delta_bytes = (Z_BYTE
13252 - BYTEPOS (tlendpos)
13253 - MATRIX_ROW_START_BYTEPOS (row));
13254
13255 increment_matrix_positions (w->current_matrix,
13256 this_line_vpos + 1,
13257 w->current_matrix->nrows,
13258 delta, delta_bytes);
13259 }
13260
13261 /* If this row displays text now but previously didn't,
13262 or vice versa, w->window_end_vpos may have to be
13263 adjusted. */
13264 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13265 {
13266 if (w->window_end_vpos < this_line_vpos)
13267 w->window_end_vpos = this_line_vpos;
13268 }
13269 else if (w->window_end_vpos == this_line_vpos
13270 && this_line_vpos > 0)
13271 w->window_end_vpos = this_line_vpos - 1;
13272 w->window_end_valid = 0;
13273
13274 /* Update hint: No need to try to scroll in update_window. */
13275 w->desired_matrix->no_scrolling_p = 1;
13276
13277 #ifdef GLYPH_DEBUG
13278 *w->desired_matrix->method = 0;
13279 debug_method_add (w, "optimization 1");
13280 #endif
13281 #ifdef HAVE_WINDOW_SYSTEM
13282 update_window_fringes (w, 0);
13283 #endif
13284 goto update;
13285 }
13286 else
13287 goto cancel;
13288 }
13289 else if (/* Cursor position hasn't changed. */
13290 PT == w->last_point
13291 /* Make sure the cursor was last displayed
13292 in this window. Otherwise we have to reposition it. */
13293 && 0 <= w->cursor.vpos
13294 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13295 {
13296 if (!must_finish)
13297 {
13298 do_pending_window_change (1);
13299 /* If selected_window changed, redisplay again. */
13300 if (WINDOWP (selected_window)
13301 && (w = XWINDOW (selected_window)) != sw)
13302 goto retry;
13303
13304 /* We used to always goto end_of_redisplay here, but this
13305 isn't enough if we have a blinking cursor. */
13306 if (w->cursor_off_p == w->last_cursor_off_p)
13307 goto end_of_redisplay;
13308 }
13309 goto update;
13310 }
13311 /* If highlighting the region, or if the cursor is in the echo area,
13312 then we can't just move the cursor. */
13313 else if (! (!NILP (Vtransient_mark_mode)
13314 && !NILP (BVAR (current_buffer, mark_active)))
13315 && (EQ (selected_window,
13316 BVAR (current_buffer, last_selected_window))
13317 || highlight_nonselected_windows)
13318 && !w->region_showing
13319 && NILP (Vshow_trailing_whitespace)
13320 && !cursor_in_echo_area)
13321 {
13322 struct it it;
13323 struct glyph_row *row;
13324
13325 /* Skip from tlbufpos to PT and see where it is. Note that
13326 PT may be in invisible text. If so, we will end at the
13327 next visible position. */
13328 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13329 NULL, DEFAULT_FACE_ID);
13330 it.current_x = this_line_start_x;
13331 it.current_y = this_line_y;
13332 it.vpos = this_line_vpos;
13333
13334 /* The call to move_it_to stops in front of PT, but
13335 moves over before-strings. */
13336 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13337
13338 if (it.vpos == this_line_vpos
13339 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13340 row->enabled_p))
13341 {
13342 eassert (this_line_vpos == it.vpos);
13343 eassert (this_line_y == it.current_y);
13344 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13345 #ifdef GLYPH_DEBUG
13346 *w->desired_matrix->method = 0;
13347 debug_method_add (w, "optimization 3");
13348 #endif
13349 goto update;
13350 }
13351 else
13352 goto cancel;
13353 }
13354
13355 cancel:
13356 /* Text changed drastically or point moved off of line. */
13357 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13358 }
13359
13360 CHARPOS (this_line_start_pos) = 0;
13361 consider_all_windows_p |= buffer_shared_and_changed ();
13362 ++clear_face_cache_count;
13363 #ifdef HAVE_WINDOW_SYSTEM
13364 ++clear_image_cache_count;
13365 #endif
13366
13367 /* Build desired matrices, and update the display. If
13368 consider_all_windows_p is non-zero, do it for all windows on all
13369 frames. Otherwise do it for selected_window, only. */
13370
13371 if (consider_all_windows_p)
13372 {
13373 FOR_EACH_FRAME (tail, frame)
13374 XFRAME (frame)->updated_p = 0;
13375
13376 FOR_EACH_FRAME (tail, frame)
13377 {
13378 struct frame *f = XFRAME (frame);
13379
13380 /* We don't have to do anything for unselected terminal
13381 frames. */
13382 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13383 && !EQ (FRAME_TTY (f)->top_frame, frame))
13384 continue;
13385
13386 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13387 {
13388 /* Mark all the scroll bars to be removed; we'll redeem
13389 the ones we want when we redisplay their windows. */
13390 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13391 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13392
13393 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13394 redisplay_windows (FRAME_ROOT_WINDOW (f));
13395
13396 /* The X error handler may have deleted that frame. */
13397 if (!FRAME_LIVE_P (f))
13398 continue;
13399
13400 /* Any scroll bars which redisplay_windows should have
13401 nuked should now go away. */
13402 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13403 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13404
13405 /* If fonts changed, display again. */
13406 /* ??? rms: I suspect it is a mistake to jump all the way
13407 back to retry here. It should just retry this frame. */
13408 if (fonts_changed_p)
13409 goto retry;
13410
13411 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13412 {
13413 /* See if we have to hscroll. */
13414 if (!f->already_hscrolled_p)
13415 {
13416 f->already_hscrolled_p = 1;
13417 if (hscroll_windows (f->root_window))
13418 goto retry;
13419 }
13420
13421 /* Prevent various kinds of signals during display
13422 update. stdio is not robust about handling
13423 signals, which can cause an apparent I/O
13424 error. */
13425 if (interrupt_input)
13426 unrequest_sigio ();
13427 STOP_POLLING;
13428
13429 /* Update the display. */
13430 set_window_update_flags (XWINDOW (f->root_window), 1);
13431 pending |= update_frame (f, 0, 0);
13432 f->updated_p = 1;
13433 }
13434 }
13435 }
13436
13437 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13438
13439 if (!pending)
13440 {
13441 /* Do the mark_window_display_accurate after all windows have
13442 been redisplayed because this call resets flags in buffers
13443 which are needed for proper redisplay. */
13444 FOR_EACH_FRAME (tail, frame)
13445 {
13446 struct frame *f = XFRAME (frame);
13447 if (f->updated_p)
13448 {
13449 mark_window_display_accurate (f->root_window, 1);
13450 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13451 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13452 }
13453 }
13454 }
13455 }
13456 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13457 {
13458 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13459 struct frame *mini_frame;
13460
13461 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13462 /* Use list_of_error, not Qerror, so that
13463 we catch only errors and don't run the debugger. */
13464 internal_condition_case_1 (redisplay_window_1, selected_window,
13465 list_of_error,
13466 redisplay_window_error);
13467 if (update_miniwindow_p)
13468 internal_condition_case_1 (redisplay_window_1, mini_window,
13469 list_of_error,
13470 redisplay_window_error);
13471
13472 /* Compare desired and current matrices, perform output. */
13473
13474 update:
13475 /* If fonts changed, display again. */
13476 if (fonts_changed_p)
13477 goto retry;
13478
13479 /* Prevent various kinds of signals during display update.
13480 stdio is not robust about handling signals,
13481 which can cause an apparent I/O error. */
13482 if (interrupt_input)
13483 unrequest_sigio ();
13484 STOP_POLLING;
13485
13486 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13487 {
13488 if (hscroll_windows (selected_window))
13489 goto retry;
13490
13491 XWINDOW (selected_window)->must_be_updated_p = 1;
13492 pending = update_frame (sf, 0, 0);
13493 }
13494
13495 /* We may have called echo_area_display at the top of this
13496 function. If the echo area is on another frame, that may
13497 have put text on a frame other than the selected one, so the
13498 above call to update_frame would not have caught it. Catch
13499 it here. */
13500 mini_window = FRAME_MINIBUF_WINDOW (sf);
13501 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13502
13503 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13504 {
13505 XWINDOW (mini_window)->must_be_updated_p = 1;
13506 pending |= update_frame (mini_frame, 0, 0);
13507 if (!pending && hscroll_windows (mini_window))
13508 goto retry;
13509 }
13510 }
13511
13512 /* If display was paused because of pending input, make sure we do a
13513 thorough update the next time. */
13514 if (pending)
13515 {
13516 /* Prevent the optimization at the beginning of
13517 redisplay_internal that tries a single-line update of the
13518 line containing the cursor in the selected window. */
13519 CHARPOS (this_line_start_pos) = 0;
13520
13521 /* Let the overlay arrow be updated the next time. */
13522 update_overlay_arrows (0);
13523
13524 /* If we pause after scrolling, some rows in the current
13525 matrices of some windows are not valid. */
13526 if (!WINDOW_FULL_WIDTH_P (w)
13527 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13528 update_mode_lines = 1;
13529 }
13530 else
13531 {
13532 if (!consider_all_windows_p)
13533 {
13534 /* This has already been done above if
13535 consider_all_windows_p is set. */
13536 mark_window_display_accurate_1 (w, 1);
13537
13538 /* Say overlay arrows are up to date. */
13539 update_overlay_arrows (1);
13540
13541 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13542 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13543 }
13544
13545 update_mode_lines = 0;
13546 windows_or_buffers_changed = 0;
13547 cursor_type_changed = 0;
13548 }
13549
13550 /* Start SIGIO interrupts coming again. Having them off during the
13551 code above makes it less likely one will discard output, but not
13552 impossible, since there might be stuff in the system buffer here.
13553 But it is much hairier to try to do anything about that. */
13554 if (interrupt_input)
13555 request_sigio ();
13556 RESUME_POLLING;
13557
13558 /* If a frame has become visible which was not before, redisplay
13559 again, so that we display it. Expose events for such a frame
13560 (which it gets when becoming visible) don't call the parts of
13561 redisplay constructing glyphs, so simply exposing a frame won't
13562 display anything in this case. So, we have to display these
13563 frames here explicitly. */
13564 if (!pending)
13565 {
13566 int new_count = 0;
13567
13568 FOR_EACH_FRAME (tail, frame)
13569 {
13570 int this_is_visible = 0;
13571
13572 if (XFRAME (frame)->visible)
13573 this_is_visible = 1;
13574
13575 if (this_is_visible)
13576 new_count++;
13577 }
13578
13579 if (new_count != number_of_visible_frames)
13580 windows_or_buffers_changed++;
13581 }
13582
13583 /* Change frame size now if a change is pending. */
13584 do_pending_window_change (1);
13585
13586 /* If we just did a pending size change, or have additional
13587 visible frames, or selected_window changed, redisplay again. */
13588 if ((windows_or_buffers_changed && !pending)
13589 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13590 goto retry;
13591
13592 /* Clear the face and image caches.
13593
13594 We used to do this only if consider_all_windows_p. But the cache
13595 needs to be cleared if a timer creates images in the current
13596 buffer (e.g. the test case in Bug#6230). */
13597
13598 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13599 {
13600 clear_face_cache (0);
13601 clear_face_cache_count = 0;
13602 }
13603
13604 #ifdef HAVE_WINDOW_SYSTEM
13605 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13606 {
13607 clear_image_caches (Qnil);
13608 clear_image_cache_count = 0;
13609 }
13610 #endif /* HAVE_WINDOW_SYSTEM */
13611
13612 end_of_redisplay:
13613 unbind_to (count, Qnil);
13614 RESUME_POLLING;
13615 }
13616
13617
13618 /* Redisplay, but leave alone any recent echo area message unless
13619 another message has been requested in its place.
13620
13621 This is useful in situations where you need to redisplay but no
13622 user action has occurred, making it inappropriate for the message
13623 area to be cleared. See tracking_off and
13624 wait_reading_process_output for examples of these situations.
13625
13626 FROM_WHERE is an integer saying from where this function was
13627 called. This is useful for debugging. */
13628
13629 void
13630 redisplay_preserve_echo_area (int from_where)
13631 {
13632 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13633
13634 if (!NILP (echo_area_buffer[1]))
13635 {
13636 /* We have a previously displayed message, but no current
13637 message. Redisplay the previous message. */
13638 display_last_displayed_message_p = 1;
13639 redisplay_internal ();
13640 display_last_displayed_message_p = 0;
13641 }
13642 else
13643 redisplay_internal ();
13644
13645 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13646 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13647 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13648 }
13649
13650
13651 /* Function registered with record_unwind_protect in redisplay_internal. */
13652
13653 static void
13654 unwind_redisplay (void)
13655 {
13656 redisplaying_p = 0;
13657 }
13658
13659
13660 /* Mark the display of leaf window W as accurate or inaccurate.
13661 If ACCURATE_P is non-zero mark display of W as accurate. If
13662 ACCURATE_P is zero, arrange for W to be redisplayed the next
13663 time redisplay_internal is called. */
13664
13665 static void
13666 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13667 {
13668 struct buffer *b = XBUFFER (w->contents);
13669
13670 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
13671 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
13672 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13673
13674 if (accurate_p)
13675 {
13676 b->clip_changed = 0;
13677 b->prevent_redisplay_optimizations_p = 0;
13678
13679 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13680 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13681 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13682 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13683
13684 w->current_matrix->buffer = b;
13685 w->current_matrix->begv = BUF_BEGV (b);
13686 w->current_matrix->zv = BUF_ZV (b);
13687
13688 w->last_cursor_vpos = w->cursor.vpos;
13689 w->last_cursor_off_p = w->cursor_off_p;
13690
13691 if (w == XWINDOW (selected_window))
13692 w->last_point = BUF_PT (b);
13693 else
13694 w->last_point = marker_position (w->pointm);
13695
13696 w->window_end_valid = 1;
13697 w->update_mode_line = 0;
13698 }
13699 }
13700
13701
13702 /* Mark the display of windows in the window tree rooted at WINDOW as
13703 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13704 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13705 be redisplayed the next time redisplay_internal is called. */
13706
13707 void
13708 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13709 {
13710 struct window *w;
13711
13712 for (; !NILP (window); window = w->next)
13713 {
13714 w = XWINDOW (window);
13715 if (WINDOWP (w->contents))
13716 mark_window_display_accurate (w->contents, accurate_p);
13717 else
13718 mark_window_display_accurate_1 (w, accurate_p);
13719 }
13720
13721 if (accurate_p)
13722 update_overlay_arrows (1);
13723 else
13724 /* Force a thorough redisplay the next time by setting
13725 last_arrow_position and last_arrow_string to t, which is
13726 unequal to any useful value of Voverlay_arrow_... */
13727 update_overlay_arrows (-1);
13728 }
13729
13730
13731 /* Return value in display table DP (Lisp_Char_Table *) for character
13732 C. Since a display table doesn't have any parent, we don't have to
13733 follow parent. Do not call this function directly but use the
13734 macro DISP_CHAR_VECTOR. */
13735
13736 Lisp_Object
13737 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13738 {
13739 Lisp_Object val;
13740
13741 if (ASCII_CHAR_P (c))
13742 {
13743 val = dp->ascii;
13744 if (SUB_CHAR_TABLE_P (val))
13745 val = XSUB_CHAR_TABLE (val)->contents[c];
13746 }
13747 else
13748 {
13749 Lisp_Object table;
13750
13751 XSETCHAR_TABLE (table, dp);
13752 val = char_table_ref (table, c);
13753 }
13754 if (NILP (val))
13755 val = dp->defalt;
13756 return val;
13757 }
13758
13759
13760 \f
13761 /***********************************************************************
13762 Window Redisplay
13763 ***********************************************************************/
13764
13765 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13766
13767 static void
13768 redisplay_windows (Lisp_Object window)
13769 {
13770 while (!NILP (window))
13771 {
13772 struct window *w = XWINDOW (window);
13773
13774 if (WINDOWP (w->contents))
13775 redisplay_windows (w->contents);
13776 else if (BUFFERP (w->contents))
13777 {
13778 displayed_buffer = XBUFFER (w->contents);
13779 /* Use list_of_error, not Qerror, so that
13780 we catch only errors and don't run the debugger. */
13781 internal_condition_case_1 (redisplay_window_0, window,
13782 list_of_error,
13783 redisplay_window_error);
13784 }
13785
13786 window = w->next;
13787 }
13788 }
13789
13790 static Lisp_Object
13791 redisplay_window_error (Lisp_Object ignore)
13792 {
13793 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13794 return Qnil;
13795 }
13796
13797 static Lisp_Object
13798 redisplay_window_0 (Lisp_Object window)
13799 {
13800 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13801 redisplay_window (window, 0);
13802 return Qnil;
13803 }
13804
13805 static Lisp_Object
13806 redisplay_window_1 (Lisp_Object window)
13807 {
13808 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13809 redisplay_window (window, 1);
13810 return Qnil;
13811 }
13812 \f
13813
13814 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13815 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13816 which positions recorded in ROW differ from current buffer
13817 positions.
13818
13819 Return 0 if cursor is not on this row, 1 otherwise. */
13820
13821 static int
13822 set_cursor_from_row (struct window *w, struct glyph_row *row,
13823 struct glyph_matrix *matrix,
13824 ptrdiff_t delta, ptrdiff_t delta_bytes,
13825 int dy, int dvpos)
13826 {
13827 struct glyph *glyph = row->glyphs[TEXT_AREA];
13828 struct glyph *end = glyph + row->used[TEXT_AREA];
13829 struct glyph *cursor = NULL;
13830 /* The last known character position in row. */
13831 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13832 int x = row->x;
13833 ptrdiff_t pt_old = PT - delta;
13834 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13835 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13836 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13837 /* A glyph beyond the edge of TEXT_AREA which we should never
13838 touch. */
13839 struct glyph *glyphs_end = end;
13840 /* Non-zero means we've found a match for cursor position, but that
13841 glyph has the avoid_cursor_p flag set. */
13842 int match_with_avoid_cursor = 0;
13843 /* Non-zero means we've seen at least one glyph that came from a
13844 display string. */
13845 int string_seen = 0;
13846 /* Largest and smallest buffer positions seen so far during scan of
13847 glyph row. */
13848 ptrdiff_t bpos_max = pos_before;
13849 ptrdiff_t bpos_min = pos_after;
13850 /* Last buffer position covered by an overlay string with an integer
13851 `cursor' property. */
13852 ptrdiff_t bpos_covered = 0;
13853 /* Non-zero means the display string on which to display the cursor
13854 comes from a text property, not from an overlay. */
13855 int string_from_text_prop = 0;
13856
13857 /* Don't even try doing anything if called for a mode-line or
13858 header-line row, since the rest of the code isn't prepared to
13859 deal with such calamities. */
13860 eassert (!row->mode_line_p);
13861 if (row->mode_line_p)
13862 return 0;
13863
13864 /* Skip over glyphs not having an object at the start and the end of
13865 the row. These are special glyphs like truncation marks on
13866 terminal frames. */
13867 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13868 {
13869 if (!row->reversed_p)
13870 {
13871 while (glyph < end
13872 && INTEGERP (glyph->object)
13873 && glyph->charpos < 0)
13874 {
13875 x += glyph->pixel_width;
13876 ++glyph;
13877 }
13878 while (end > glyph
13879 && INTEGERP ((end - 1)->object)
13880 /* CHARPOS is zero for blanks and stretch glyphs
13881 inserted by extend_face_to_end_of_line. */
13882 && (end - 1)->charpos <= 0)
13883 --end;
13884 glyph_before = glyph - 1;
13885 glyph_after = end;
13886 }
13887 else
13888 {
13889 struct glyph *g;
13890
13891 /* If the glyph row is reversed, we need to process it from back
13892 to front, so swap the edge pointers. */
13893 glyphs_end = end = glyph - 1;
13894 glyph += row->used[TEXT_AREA] - 1;
13895
13896 while (glyph > end + 1
13897 && INTEGERP (glyph->object)
13898 && glyph->charpos < 0)
13899 {
13900 --glyph;
13901 x -= glyph->pixel_width;
13902 }
13903 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13904 --glyph;
13905 /* By default, in reversed rows we put the cursor on the
13906 rightmost (first in the reading order) glyph. */
13907 for (g = end + 1; g < glyph; g++)
13908 x += g->pixel_width;
13909 while (end < glyph
13910 && INTEGERP ((end + 1)->object)
13911 && (end + 1)->charpos <= 0)
13912 ++end;
13913 glyph_before = glyph + 1;
13914 glyph_after = end;
13915 }
13916 }
13917 else if (row->reversed_p)
13918 {
13919 /* In R2L rows that don't display text, put the cursor on the
13920 rightmost glyph. Case in point: an empty last line that is
13921 part of an R2L paragraph. */
13922 cursor = end - 1;
13923 /* Avoid placing the cursor on the last glyph of the row, where
13924 on terminal frames we hold the vertical border between
13925 adjacent windows. */
13926 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13927 && !WINDOW_RIGHTMOST_P (w)
13928 && cursor == row->glyphs[LAST_AREA] - 1)
13929 cursor--;
13930 x = -1; /* will be computed below, at label compute_x */
13931 }
13932
13933 /* Step 1: Try to find the glyph whose character position
13934 corresponds to point. If that's not possible, find 2 glyphs
13935 whose character positions are the closest to point, one before
13936 point, the other after it. */
13937 if (!row->reversed_p)
13938 while (/* not marched to end of glyph row */
13939 glyph < end
13940 /* glyph was not inserted by redisplay for internal purposes */
13941 && !INTEGERP (glyph->object))
13942 {
13943 if (BUFFERP (glyph->object))
13944 {
13945 ptrdiff_t dpos = glyph->charpos - pt_old;
13946
13947 if (glyph->charpos > bpos_max)
13948 bpos_max = glyph->charpos;
13949 if (glyph->charpos < bpos_min)
13950 bpos_min = glyph->charpos;
13951 if (!glyph->avoid_cursor_p)
13952 {
13953 /* If we hit point, we've found the glyph on which to
13954 display the cursor. */
13955 if (dpos == 0)
13956 {
13957 match_with_avoid_cursor = 0;
13958 break;
13959 }
13960 /* See if we've found a better approximation to
13961 POS_BEFORE or to POS_AFTER. */
13962 if (0 > dpos && dpos > pos_before - pt_old)
13963 {
13964 pos_before = glyph->charpos;
13965 glyph_before = glyph;
13966 }
13967 else if (0 < dpos && dpos < pos_after - pt_old)
13968 {
13969 pos_after = glyph->charpos;
13970 glyph_after = glyph;
13971 }
13972 }
13973 else if (dpos == 0)
13974 match_with_avoid_cursor = 1;
13975 }
13976 else if (STRINGP (glyph->object))
13977 {
13978 Lisp_Object chprop;
13979 ptrdiff_t glyph_pos = glyph->charpos;
13980
13981 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13982 glyph->object);
13983 if (!NILP (chprop))
13984 {
13985 /* If the string came from a `display' text property,
13986 look up the buffer position of that property and
13987 use that position to update bpos_max, as if we
13988 actually saw such a position in one of the row's
13989 glyphs. This helps with supporting integer values
13990 of `cursor' property on the display string in
13991 situations where most or all of the row's buffer
13992 text is completely covered by display properties,
13993 so that no glyph with valid buffer positions is
13994 ever seen in the row. */
13995 ptrdiff_t prop_pos =
13996 string_buffer_position_lim (glyph->object, pos_before,
13997 pos_after, 0);
13998
13999 if (prop_pos >= pos_before)
14000 bpos_max = prop_pos - 1;
14001 }
14002 if (INTEGERP (chprop))
14003 {
14004 bpos_covered = bpos_max + XINT (chprop);
14005 /* If the `cursor' property covers buffer positions up
14006 to and including point, we should display cursor on
14007 this glyph. Note that, if a `cursor' property on one
14008 of the string's characters has an integer value, we
14009 will break out of the loop below _before_ we get to
14010 the position match above. IOW, integer values of
14011 the `cursor' property override the "exact match for
14012 point" strategy of positioning the cursor. */
14013 /* Implementation note: bpos_max == pt_old when, e.g.,
14014 we are in an empty line, where bpos_max is set to
14015 MATRIX_ROW_START_CHARPOS, see above. */
14016 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14017 {
14018 cursor = glyph;
14019 break;
14020 }
14021 }
14022
14023 string_seen = 1;
14024 }
14025 x += glyph->pixel_width;
14026 ++glyph;
14027 }
14028 else if (glyph > end) /* row is reversed */
14029 while (!INTEGERP (glyph->object))
14030 {
14031 if (BUFFERP (glyph->object))
14032 {
14033 ptrdiff_t dpos = glyph->charpos - pt_old;
14034
14035 if (glyph->charpos > bpos_max)
14036 bpos_max = glyph->charpos;
14037 if (glyph->charpos < bpos_min)
14038 bpos_min = glyph->charpos;
14039 if (!glyph->avoid_cursor_p)
14040 {
14041 if (dpos == 0)
14042 {
14043 match_with_avoid_cursor = 0;
14044 break;
14045 }
14046 if (0 > dpos && dpos > pos_before - pt_old)
14047 {
14048 pos_before = glyph->charpos;
14049 glyph_before = glyph;
14050 }
14051 else if (0 < dpos && dpos < pos_after - pt_old)
14052 {
14053 pos_after = glyph->charpos;
14054 glyph_after = glyph;
14055 }
14056 }
14057 else if (dpos == 0)
14058 match_with_avoid_cursor = 1;
14059 }
14060 else if (STRINGP (glyph->object))
14061 {
14062 Lisp_Object chprop;
14063 ptrdiff_t glyph_pos = glyph->charpos;
14064
14065 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14066 glyph->object);
14067 if (!NILP (chprop))
14068 {
14069 ptrdiff_t prop_pos =
14070 string_buffer_position_lim (glyph->object, pos_before,
14071 pos_after, 0);
14072
14073 if (prop_pos >= pos_before)
14074 bpos_max = prop_pos - 1;
14075 }
14076 if (INTEGERP (chprop))
14077 {
14078 bpos_covered = bpos_max + XINT (chprop);
14079 /* If the `cursor' property covers buffer positions up
14080 to and including point, we should display cursor on
14081 this glyph. */
14082 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14083 {
14084 cursor = glyph;
14085 break;
14086 }
14087 }
14088 string_seen = 1;
14089 }
14090 --glyph;
14091 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14092 {
14093 x--; /* can't use any pixel_width */
14094 break;
14095 }
14096 x -= glyph->pixel_width;
14097 }
14098
14099 /* Step 2: If we didn't find an exact match for point, we need to
14100 look for a proper place to put the cursor among glyphs between
14101 GLYPH_BEFORE and GLYPH_AFTER. */
14102 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14103 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14104 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14105 {
14106 /* An empty line has a single glyph whose OBJECT is zero and
14107 whose CHARPOS is the position of a newline on that line.
14108 Note that on a TTY, there are more glyphs after that, which
14109 were produced by extend_face_to_end_of_line, but their
14110 CHARPOS is zero or negative. */
14111 int empty_line_p =
14112 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14113 && INTEGERP (glyph->object) && glyph->charpos > 0
14114 /* On a TTY, continued and truncated rows also have a glyph at
14115 their end whose OBJECT is zero and whose CHARPOS is
14116 positive (the continuation and truncation glyphs), but such
14117 rows are obviously not "empty". */
14118 && !(row->continued_p || row->truncated_on_right_p);
14119
14120 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14121 {
14122 ptrdiff_t ellipsis_pos;
14123
14124 /* Scan back over the ellipsis glyphs. */
14125 if (!row->reversed_p)
14126 {
14127 ellipsis_pos = (glyph - 1)->charpos;
14128 while (glyph > row->glyphs[TEXT_AREA]
14129 && (glyph - 1)->charpos == ellipsis_pos)
14130 glyph--, x -= glyph->pixel_width;
14131 /* That loop always goes one position too far, including
14132 the glyph before the ellipsis. So scan forward over
14133 that one. */
14134 x += glyph->pixel_width;
14135 glyph++;
14136 }
14137 else /* row is reversed */
14138 {
14139 ellipsis_pos = (glyph + 1)->charpos;
14140 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14141 && (glyph + 1)->charpos == ellipsis_pos)
14142 glyph++, x += glyph->pixel_width;
14143 x -= glyph->pixel_width;
14144 glyph--;
14145 }
14146 }
14147 else if (match_with_avoid_cursor)
14148 {
14149 cursor = glyph_after;
14150 x = -1;
14151 }
14152 else if (string_seen)
14153 {
14154 int incr = row->reversed_p ? -1 : +1;
14155
14156 /* Need to find the glyph that came out of a string which is
14157 present at point. That glyph is somewhere between
14158 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14159 positioned between POS_BEFORE and POS_AFTER in the
14160 buffer. */
14161 struct glyph *start, *stop;
14162 ptrdiff_t pos = pos_before;
14163
14164 x = -1;
14165
14166 /* If the row ends in a newline from a display string,
14167 reordering could have moved the glyphs belonging to the
14168 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14169 in this case we extend the search to the last glyph in
14170 the row that was not inserted by redisplay. */
14171 if (row->ends_in_newline_from_string_p)
14172 {
14173 glyph_after = end;
14174 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14175 }
14176
14177 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14178 correspond to POS_BEFORE and POS_AFTER, respectively. We
14179 need START and STOP in the order that corresponds to the
14180 row's direction as given by its reversed_p flag. If the
14181 directionality of characters between POS_BEFORE and
14182 POS_AFTER is the opposite of the row's base direction,
14183 these characters will have been reordered for display,
14184 and we need to reverse START and STOP. */
14185 if (!row->reversed_p)
14186 {
14187 start = min (glyph_before, glyph_after);
14188 stop = max (glyph_before, glyph_after);
14189 }
14190 else
14191 {
14192 start = max (glyph_before, glyph_after);
14193 stop = min (glyph_before, glyph_after);
14194 }
14195 for (glyph = start + incr;
14196 row->reversed_p ? glyph > stop : glyph < stop; )
14197 {
14198
14199 /* Any glyphs that come from the buffer are here because
14200 of bidi reordering. Skip them, and only pay
14201 attention to glyphs that came from some string. */
14202 if (STRINGP (glyph->object))
14203 {
14204 Lisp_Object str;
14205 ptrdiff_t tem;
14206 /* If the display property covers the newline, we
14207 need to search for it one position farther. */
14208 ptrdiff_t lim = pos_after
14209 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14210
14211 string_from_text_prop = 0;
14212 str = glyph->object;
14213 tem = string_buffer_position_lim (str, pos, lim, 0);
14214 if (tem == 0 /* from overlay */
14215 || pos <= tem)
14216 {
14217 /* If the string from which this glyph came is
14218 found in the buffer at point, or at position
14219 that is closer to point than pos_after, then
14220 we've found the glyph we've been looking for.
14221 If it comes from an overlay (tem == 0), and
14222 it has the `cursor' property on one of its
14223 glyphs, record that glyph as a candidate for
14224 displaying the cursor. (As in the
14225 unidirectional version, we will display the
14226 cursor on the last candidate we find.) */
14227 if (tem == 0
14228 || tem == pt_old
14229 || (tem - pt_old > 0 && tem < pos_after))
14230 {
14231 /* The glyphs from this string could have
14232 been reordered. Find the one with the
14233 smallest string position. Or there could
14234 be a character in the string with the
14235 `cursor' property, which means display
14236 cursor on that character's glyph. */
14237 ptrdiff_t strpos = glyph->charpos;
14238
14239 if (tem)
14240 {
14241 cursor = glyph;
14242 string_from_text_prop = 1;
14243 }
14244 for ( ;
14245 (row->reversed_p ? glyph > stop : glyph < stop)
14246 && EQ (glyph->object, str);
14247 glyph += incr)
14248 {
14249 Lisp_Object cprop;
14250 ptrdiff_t gpos = glyph->charpos;
14251
14252 cprop = Fget_char_property (make_number (gpos),
14253 Qcursor,
14254 glyph->object);
14255 if (!NILP (cprop))
14256 {
14257 cursor = glyph;
14258 break;
14259 }
14260 if (tem && glyph->charpos < strpos)
14261 {
14262 strpos = glyph->charpos;
14263 cursor = glyph;
14264 }
14265 }
14266
14267 if (tem == pt_old
14268 || (tem - pt_old > 0 && tem < pos_after))
14269 goto compute_x;
14270 }
14271 if (tem)
14272 pos = tem + 1; /* don't find previous instances */
14273 }
14274 /* This string is not what we want; skip all of the
14275 glyphs that came from it. */
14276 while ((row->reversed_p ? glyph > stop : glyph < stop)
14277 && EQ (glyph->object, str))
14278 glyph += incr;
14279 }
14280 else
14281 glyph += incr;
14282 }
14283
14284 /* If we reached the end of the line, and END was from a string,
14285 the cursor is not on this line. */
14286 if (cursor == NULL
14287 && (row->reversed_p ? glyph <= end : glyph >= end)
14288 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14289 && STRINGP (end->object)
14290 && row->continued_p)
14291 return 0;
14292 }
14293 /* A truncated row may not include PT among its character positions.
14294 Setting the cursor inside the scroll margin will trigger
14295 recalculation of hscroll in hscroll_window_tree. But if a
14296 display string covers point, defer to the string-handling
14297 code below to figure this out. */
14298 else if (row->truncated_on_left_p && pt_old < bpos_min)
14299 {
14300 cursor = glyph_before;
14301 x = -1;
14302 }
14303 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14304 /* Zero-width characters produce no glyphs. */
14305 || (!empty_line_p
14306 && (row->reversed_p
14307 ? glyph_after > glyphs_end
14308 : glyph_after < glyphs_end)))
14309 {
14310 cursor = glyph_after;
14311 x = -1;
14312 }
14313 }
14314
14315 compute_x:
14316 if (cursor != NULL)
14317 glyph = cursor;
14318 else if (glyph == glyphs_end
14319 && pos_before == pos_after
14320 && STRINGP ((row->reversed_p
14321 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14322 : row->glyphs[TEXT_AREA])->object))
14323 {
14324 /* If all the glyphs of this row came from strings, put the
14325 cursor on the first glyph of the row. This avoids having the
14326 cursor outside of the text area in this very rare and hard
14327 use case. */
14328 glyph =
14329 row->reversed_p
14330 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14331 : row->glyphs[TEXT_AREA];
14332 }
14333 if (x < 0)
14334 {
14335 struct glyph *g;
14336
14337 /* Need to compute x that corresponds to GLYPH. */
14338 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14339 {
14340 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14341 emacs_abort ();
14342 x += g->pixel_width;
14343 }
14344 }
14345
14346 /* ROW could be part of a continued line, which, under bidi
14347 reordering, might have other rows whose start and end charpos
14348 occlude point. Only set w->cursor if we found a better
14349 approximation to the cursor position than we have from previously
14350 examined candidate rows belonging to the same continued line. */
14351 if (/* we already have a candidate row */
14352 w->cursor.vpos >= 0
14353 /* that candidate is not the row we are processing */
14354 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14355 /* Make sure cursor.vpos specifies a row whose start and end
14356 charpos occlude point, and it is valid candidate for being a
14357 cursor-row. This is because some callers of this function
14358 leave cursor.vpos at the row where the cursor was displayed
14359 during the last redisplay cycle. */
14360 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14361 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14362 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14363 {
14364 struct glyph *g1 =
14365 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14366
14367 /* Don't consider glyphs that are outside TEXT_AREA. */
14368 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14369 return 0;
14370 /* Keep the candidate whose buffer position is the closest to
14371 point or has the `cursor' property. */
14372 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14373 w->cursor.hpos >= 0
14374 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14375 && ((BUFFERP (g1->object)
14376 && (g1->charpos == pt_old /* an exact match always wins */
14377 || (BUFFERP (glyph->object)
14378 && eabs (g1->charpos - pt_old)
14379 < eabs (glyph->charpos - pt_old))))
14380 /* previous candidate is a glyph from a string that has
14381 a non-nil `cursor' property */
14382 || (STRINGP (g1->object)
14383 && (!NILP (Fget_char_property (make_number (g1->charpos),
14384 Qcursor, g1->object))
14385 /* previous candidate is from the same display
14386 string as this one, and the display string
14387 came from a text property */
14388 || (EQ (g1->object, glyph->object)
14389 && string_from_text_prop)
14390 /* this candidate is from newline and its
14391 position is not an exact match */
14392 || (INTEGERP (glyph->object)
14393 && glyph->charpos != pt_old)))))
14394 return 0;
14395 /* If this candidate gives an exact match, use that. */
14396 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14397 /* If this candidate is a glyph created for the
14398 terminating newline of a line, and point is on that
14399 newline, it wins because it's an exact match. */
14400 || (!row->continued_p
14401 && INTEGERP (glyph->object)
14402 && glyph->charpos == 0
14403 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14404 /* Otherwise, keep the candidate that comes from a row
14405 spanning less buffer positions. This may win when one or
14406 both candidate positions are on glyphs that came from
14407 display strings, for which we cannot compare buffer
14408 positions. */
14409 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14410 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14411 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14412 return 0;
14413 }
14414 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14415 w->cursor.x = x;
14416 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14417 w->cursor.y = row->y + dy;
14418
14419 if (w == XWINDOW (selected_window))
14420 {
14421 if (!row->continued_p
14422 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14423 && row->x == 0)
14424 {
14425 this_line_buffer = XBUFFER (w->contents);
14426
14427 CHARPOS (this_line_start_pos)
14428 = MATRIX_ROW_START_CHARPOS (row) + delta;
14429 BYTEPOS (this_line_start_pos)
14430 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14431
14432 CHARPOS (this_line_end_pos)
14433 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14434 BYTEPOS (this_line_end_pos)
14435 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14436
14437 this_line_y = w->cursor.y;
14438 this_line_pixel_height = row->height;
14439 this_line_vpos = w->cursor.vpos;
14440 this_line_start_x = row->x;
14441 }
14442 else
14443 CHARPOS (this_line_start_pos) = 0;
14444 }
14445
14446 return 1;
14447 }
14448
14449
14450 /* Run window scroll functions, if any, for WINDOW with new window
14451 start STARTP. Sets the window start of WINDOW to that position.
14452
14453 We assume that the window's buffer is really current. */
14454
14455 static struct text_pos
14456 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14457 {
14458 struct window *w = XWINDOW (window);
14459 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14460
14461 eassert (current_buffer == XBUFFER (w->contents));
14462
14463 if (!NILP (Vwindow_scroll_functions))
14464 {
14465 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14466 make_number (CHARPOS (startp)));
14467 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14468 /* In case the hook functions switch buffers. */
14469 set_buffer_internal (XBUFFER (w->contents));
14470 }
14471
14472 return startp;
14473 }
14474
14475
14476 /* Make sure the line containing the cursor is fully visible.
14477 A value of 1 means there is nothing to be done.
14478 (Either the line is fully visible, or it cannot be made so,
14479 or we cannot tell.)
14480
14481 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14482 is higher than window.
14483
14484 A value of 0 means the caller should do scrolling
14485 as if point had gone off the screen. */
14486
14487 static int
14488 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14489 {
14490 struct glyph_matrix *matrix;
14491 struct glyph_row *row;
14492 int window_height;
14493
14494 if (!make_cursor_line_fully_visible_p)
14495 return 1;
14496
14497 /* It's not always possible to find the cursor, e.g, when a window
14498 is full of overlay strings. Don't do anything in that case. */
14499 if (w->cursor.vpos < 0)
14500 return 1;
14501
14502 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14503 row = MATRIX_ROW (matrix, w->cursor.vpos);
14504
14505 /* If the cursor row is not partially visible, there's nothing to do. */
14506 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14507 return 1;
14508
14509 /* If the row the cursor is in is taller than the window's height,
14510 it's not clear what to do, so do nothing. */
14511 window_height = window_box_height (w);
14512 if (row->height >= window_height)
14513 {
14514 if (!force_p || MINI_WINDOW_P (w)
14515 || w->vscroll || w->cursor.vpos == 0)
14516 return 1;
14517 }
14518 return 0;
14519 }
14520
14521
14522 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14523 non-zero means only WINDOW is redisplayed in redisplay_internal.
14524 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14525 in redisplay_window to bring a partially visible line into view in
14526 the case that only the cursor has moved.
14527
14528 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14529 last screen line's vertical height extends past the end of the screen.
14530
14531 Value is
14532
14533 1 if scrolling succeeded
14534
14535 0 if scrolling didn't find point.
14536
14537 -1 if new fonts have been loaded so that we must interrupt
14538 redisplay, adjust glyph matrices, and try again. */
14539
14540 enum
14541 {
14542 SCROLLING_SUCCESS,
14543 SCROLLING_FAILED,
14544 SCROLLING_NEED_LARGER_MATRICES
14545 };
14546
14547 /* If scroll-conservatively is more than this, never recenter.
14548
14549 If you change this, don't forget to update the doc string of
14550 `scroll-conservatively' and the Emacs manual. */
14551 #define SCROLL_LIMIT 100
14552
14553 static int
14554 try_scrolling (Lisp_Object window, int just_this_one_p,
14555 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14556 int temp_scroll_step, int last_line_misfit)
14557 {
14558 struct window *w = XWINDOW (window);
14559 struct frame *f = XFRAME (w->frame);
14560 struct text_pos pos, startp;
14561 struct it it;
14562 int this_scroll_margin, scroll_max, rc, height;
14563 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14564 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14565 Lisp_Object aggressive;
14566 /* We will never try scrolling more than this number of lines. */
14567 int scroll_limit = SCROLL_LIMIT;
14568 int frame_line_height = default_line_pixel_height (w);
14569 int window_total_lines
14570 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14571
14572 #ifdef GLYPH_DEBUG
14573 debug_method_add (w, "try_scrolling");
14574 #endif
14575
14576 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14577
14578 /* Compute scroll margin height in pixels. We scroll when point is
14579 within this distance from the top or bottom of the window. */
14580 if (scroll_margin > 0)
14581 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
14582 * frame_line_height;
14583 else
14584 this_scroll_margin = 0;
14585
14586 /* Force arg_scroll_conservatively to have a reasonable value, to
14587 avoid scrolling too far away with slow move_it_* functions. Note
14588 that the user can supply scroll-conservatively equal to
14589 `most-positive-fixnum', which can be larger than INT_MAX. */
14590 if (arg_scroll_conservatively > scroll_limit)
14591 {
14592 arg_scroll_conservatively = scroll_limit + 1;
14593 scroll_max = scroll_limit * frame_line_height;
14594 }
14595 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14596 /* Compute how much we should try to scroll maximally to bring
14597 point into view. */
14598 scroll_max = (max (scroll_step,
14599 max (arg_scroll_conservatively, temp_scroll_step))
14600 * frame_line_height);
14601 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14602 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14603 /* We're trying to scroll because of aggressive scrolling but no
14604 scroll_step is set. Choose an arbitrary one. */
14605 scroll_max = 10 * frame_line_height;
14606 else
14607 scroll_max = 0;
14608
14609 too_near_end:
14610
14611 /* Decide whether to scroll down. */
14612 if (PT > CHARPOS (startp))
14613 {
14614 int scroll_margin_y;
14615
14616 /* Compute the pixel ypos of the scroll margin, then move IT to
14617 either that ypos or PT, whichever comes first. */
14618 start_display (&it, w, startp);
14619 scroll_margin_y = it.last_visible_y - this_scroll_margin
14620 - frame_line_height * extra_scroll_margin_lines;
14621 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14622 (MOVE_TO_POS | MOVE_TO_Y));
14623
14624 if (PT > CHARPOS (it.current.pos))
14625 {
14626 int y0 = line_bottom_y (&it);
14627 /* Compute how many pixels below window bottom to stop searching
14628 for PT. This avoids costly search for PT that is far away if
14629 the user limited scrolling by a small number of lines, but
14630 always finds PT if scroll_conservatively is set to a large
14631 number, such as most-positive-fixnum. */
14632 int slack = max (scroll_max, 10 * frame_line_height);
14633 int y_to_move = it.last_visible_y + slack;
14634
14635 /* Compute the distance from the scroll margin to PT or to
14636 the scroll limit, whichever comes first. This should
14637 include the height of the cursor line, to make that line
14638 fully visible. */
14639 move_it_to (&it, PT, -1, y_to_move,
14640 -1, MOVE_TO_POS | MOVE_TO_Y);
14641 dy = line_bottom_y (&it) - y0;
14642
14643 if (dy > scroll_max)
14644 return SCROLLING_FAILED;
14645
14646 if (dy > 0)
14647 scroll_down_p = 1;
14648 }
14649 }
14650
14651 if (scroll_down_p)
14652 {
14653 /* Point is in or below the bottom scroll margin, so move the
14654 window start down. If scrolling conservatively, move it just
14655 enough down to make point visible. If scroll_step is set,
14656 move it down by scroll_step. */
14657 if (arg_scroll_conservatively)
14658 amount_to_scroll
14659 = min (max (dy, frame_line_height),
14660 frame_line_height * arg_scroll_conservatively);
14661 else if (scroll_step || temp_scroll_step)
14662 amount_to_scroll = scroll_max;
14663 else
14664 {
14665 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14666 height = WINDOW_BOX_TEXT_HEIGHT (w);
14667 if (NUMBERP (aggressive))
14668 {
14669 double float_amount = XFLOATINT (aggressive) * height;
14670 int aggressive_scroll = float_amount;
14671 if (aggressive_scroll == 0 && float_amount > 0)
14672 aggressive_scroll = 1;
14673 /* Don't let point enter the scroll margin near top of
14674 the window. This could happen if the value of
14675 scroll_up_aggressively is too large and there are
14676 non-zero margins, because scroll_up_aggressively
14677 means put point that fraction of window height
14678 _from_the_bottom_margin_. */
14679 if (aggressive_scroll + 2*this_scroll_margin > height)
14680 aggressive_scroll = height - 2*this_scroll_margin;
14681 amount_to_scroll = dy + aggressive_scroll;
14682 }
14683 }
14684
14685 if (amount_to_scroll <= 0)
14686 return SCROLLING_FAILED;
14687
14688 start_display (&it, w, startp);
14689 if (arg_scroll_conservatively <= scroll_limit)
14690 move_it_vertically (&it, amount_to_scroll);
14691 else
14692 {
14693 /* Extra precision for users who set scroll-conservatively
14694 to a large number: make sure the amount we scroll
14695 the window start is never less than amount_to_scroll,
14696 which was computed as distance from window bottom to
14697 point. This matters when lines at window top and lines
14698 below window bottom have different height. */
14699 struct it it1;
14700 void *it1data = NULL;
14701 /* We use a temporary it1 because line_bottom_y can modify
14702 its argument, if it moves one line down; see there. */
14703 int start_y;
14704
14705 SAVE_IT (it1, it, it1data);
14706 start_y = line_bottom_y (&it1);
14707 do {
14708 RESTORE_IT (&it, &it, it1data);
14709 move_it_by_lines (&it, 1);
14710 SAVE_IT (it1, it, it1data);
14711 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14712 }
14713
14714 /* If STARTP is unchanged, move it down another screen line. */
14715 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14716 move_it_by_lines (&it, 1);
14717 startp = it.current.pos;
14718 }
14719 else
14720 {
14721 struct text_pos scroll_margin_pos = startp;
14722 int y_offset = 0;
14723
14724 /* See if point is inside the scroll margin at the top of the
14725 window. */
14726 if (this_scroll_margin)
14727 {
14728 int y_start;
14729
14730 start_display (&it, w, startp);
14731 y_start = it.current_y;
14732 move_it_vertically (&it, this_scroll_margin);
14733 scroll_margin_pos = it.current.pos;
14734 /* If we didn't move enough before hitting ZV, request
14735 additional amount of scroll, to move point out of the
14736 scroll margin. */
14737 if (IT_CHARPOS (it) == ZV
14738 && it.current_y - y_start < this_scroll_margin)
14739 y_offset = this_scroll_margin - (it.current_y - y_start);
14740 }
14741
14742 if (PT < CHARPOS (scroll_margin_pos))
14743 {
14744 /* Point is in the scroll margin at the top of the window or
14745 above what is displayed in the window. */
14746 int y0, y_to_move;
14747
14748 /* Compute the vertical distance from PT to the scroll
14749 margin position. Move as far as scroll_max allows, or
14750 one screenful, or 10 screen lines, whichever is largest.
14751 Give up if distance is greater than scroll_max or if we
14752 didn't reach the scroll margin position. */
14753 SET_TEXT_POS (pos, PT, PT_BYTE);
14754 start_display (&it, w, pos);
14755 y0 = it.current_y;
14756 y_to_move = max (it.last_visible_y,
14757 max (scroll_max, 10 * frame_line_height));
14758 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14759 y_to_move, -1,
14760 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14761 dy = it.current_y - y0;
14762 if (dy > scroll_max
14763 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
14764 return SCROLLING_FAILED;
14765
14766 /* Additional scroll for when ZV was too close to point. */
14767 dy += y_offset;
14768
14769 /* Compute new window start. */
14770 start_display (&it, w, startp);
14771
14772 if (arg_scroll_conservatively)
14773 amount_to_scroll = max (dy, frame_line_height *
14774 max (scroll_step, temp_scroll_step));
14775 else if (scroll_step || temp_scroll_step)
14776 amount_to_scroll = scroll_max;
14777 else
14778 {
14779 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14780 height = WINDOW_BOX_TEXT_HEIGHT (w);
14781 if (NUMBERP (aggressive))
14782 {
14783 double float_amount = XFLOATINT (aggressive) * height;
14784 int aggressive_scroll = float_amount;
14785 if (aggressive_scroll == 0 && float_amount > 0)
14786 aggressive_scroll = 1;
14787 /* Don't let point enter the scroll margin near
14788 bottom of the window, if the value of
14789 scroll_down_aggressively happens to be too
14790 large. */
14791 if (aggressive_scroll + 2*this_scroll_margin > height)
14792 aggressive_scroll = height - 2*this_scroll_margin;
14793 amount_to_scroll = dy + aggressive_scroll;
14794 }
14795 }
14796
14797 if (amount_to_scroll <= 0)
14798 return SCROLLING_FAILED;
14799
14800 move_it_vertically_backward (&it, amount_to_scroll);
14801 startp = it.current.pos;
14802 }
14803 }
14804
14805 /* Run window scroll functions. */
14806 startp = run_window_scroll_functions (window, startp);
14807
14808 /* Display the window. Give up if new fonts are loaded, or if point
14809 doesn't appear. */
14810 if (!try_window (window, startp, 0))
14811 rc = SCROLLING_NEED_LARGER_MATRICES;
14812 else if (w->cursor.vpos < 0)
14813 {
14814 clear_glyph_matrix (w->desired_matrix);
14815 rc = SCROLLING_FAILED;
14816 }
14817 else
14818 {
14819 /* Maybe forget recorded base line for line number display. */
14820 if (!just_this_one_p
14821 || current_buffer->clip_changed
14822 || BEG_UNCHANGED < CHARPOS (startp))
14823 w->base_line_number = 0;
14824
14825 /* If cursor ends up on a partially visible line,
14826 treat that as being off the bottom of the screen. */
14827 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14828 /* It's possible that the cursor is on the first line of the
14829 buffer, which is partially obscured due to a vscroll
14830 (Bug#7537). In that case, avoid looping forever . */
14831 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14832 {
14833 clear_glyph_matrix (w->desired_matrix);
14834 ++extra_scroll_margin_lines;
14835 goto too_near_end;
14836 }
14837 rc = SCROLLING_SUCCESS;
14838 }
14839
14840 return rc;
14841 }
14842
14843
14844 /* Compute a suitable window start for window W if display of W starts
14845 on a continuation line. Value is non-zero if a new window start
14846 was computed.
14847
14848 The new window start will be computed, based on W's width, starting
14849 from the start of the continued line. It is the start of the
14850 screen line with the minimum distance from the old start W->start. */
14851
14852 static int
14853 compute_window_start_on_continuation_line (struct window *w)
14854 {
14855 struct text_pos pos, start_pos;
14856 int window_start_changed_p = 0;
14857
14858 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14859
14860 /* If window start is on a continuation line... Window start may be
14861 < BEGV in case there's invisible text at the start of the
14862 buffer (M-x rmail, for example). */
14863 if (CHARPOS (start_pos) > BEGV
14864 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14865 {
14866 struct it it;
14867 struct glyph_row *row;
14868
14869 /* Handle the case that the window start is out of range. */
14870 if (CHARPOS (start_pos) < BEGV)
14871 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14872 else if (CHARPOS (start_pos) > ZV)
14873 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14874
14875 /* Find the start of the continued line. This should be fast
14876 because find_newline is fast (newline cache). */
14877 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14878 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14879 row, DEFAULT_FACE_ID);
14880 reseat_at_previous_visible_line_start (&it);
14881
14882 /* If the line start is "too far" away from the window start,
14883 say it takes too much time to compute a new window start. */
14884 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14885 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14886 {
14887 int min_distance, distance;
14888
14889 /* Move forward by display lines to find the new window
14890 start. If window width was enlarged, the new start can
14891 be expected to be > the old start. If window width was
14892 decreased, the new window start will be < the old start.
14893 So, we're looking for the display line start with the
14894 minimum distance from the old window start. */
14895 pos = it.current.pos;
14896 min_distance = INFINITY;
14897 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14898 distance < min_distance)
14899 {
14900 min_distance = distance;
14901 pos = it.current.pos;
14902 if (it.line_wrap == WORD_WRAP)
14903 {
14904 /* Under WORD_WRAP, move_it_by_lines is likely to
14905 overshoot and stop not at the first, but the
14906 second character from the left margin. So in
14907 that case, we need a more tight control on the X
14908 coordinate of the iterator than move_it_by_lines
14909 promises in its contract. The method is to first
14910 go to the last (rightmost) visible character of a
14911 line, then move to the leftmost character on the
14912 next line in a separate call. */
14913 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
14914 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14915 move_it_to (&it, ZV, 0,
14916 it.current_y + it.max_ascent + it.max_descent, -1,
14917 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14918 }
14919 else
14920 move_it_by_lines (&it, 1);
14921 }
14922
14923 /* Set the window start there. */
14924 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14925 window_start_changed_p = 1;
14926 }
14927 }
14928
14929 return window_start_changed_p;
14930 }
14931
14932
14933 /* Try cursor movement in case text has not changed in window WINDOW,
14934 with window start STARTP. Value is
14935
14936 CURSOR_MOVEMENT_SUCCESS if successful
14937
14938 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14939
14940 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14941 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14942 we want to scroll as if scroll-step were set to 1. See the code.
14943
14944 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14945 which case we have to abort this redisplay, and adjust matrices
14946 first. */
14947
14948 enum
14949 {
14950 CURSOR_MOVEMENT_SUCCESS,
14951 CURSOR_MOVEMENT_CANNOT_BE_USED,
14952 CURSOR_MOVEMENT_MUST_SCROLL,
14953 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14954 };
14955
14956 static int
14957 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14958 {
14959 struct window *w = XWINDOW (window);
14960 struct frame *f = XFRAME (w->frame);
14961 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14962
14963 #ifdef GLYPH_DEBUG
14964 if (inhibit_try_cursor_movement)
14965 return rc;
14966 #endif
14967
14968 /* Previously, there was a check for Lisp integer in the
14969 if-statement below. Now, this field is converted to
14970 ptrdiff_t, thus zero means invalid position in a buffer. */
14971 eassert (w->last_point > 0);
14972 /* Likewise there was a check whether window_end_vpos is nil or larger
14973 than the window. Now window_end_vpos is int and so never nil, but
14974 let's leave eassert to check whether it fits in the window. */
14975 eassert (w->window_end_vpos < w->current_matrix->nrows);
14976
14977 /* Handle case where text has not changed, only point, and it has
14978 not moved off the frame. */
14979 if (/* Point may be in this window. */
14980 PT >= CHARPOS (startp)
14981 /* Selective display hasn't changed. */
14982 && !current_buffer->clip_changed
14983 /* Function force-mode-line-update is used to force a thorough
14984 redisplay. It sets either windows_or_buffers_changed or
14985 update_mode_lines. So don't take a shortcut here for these
14986 cases. */
14987 && !update_mode_lines
14988 && !windows_or_buffers_changed
14989 && !cursor_type_changed
14990 /* Can't use this case if highlighting a region. When a
14991 region exists, cursor movement has to do more than just
14992 set the cursor. */
14993 && markpos_of_region () < 0
14994 && !w->region_showing
14995 && NILP (Vshow_trailing_whitespace)
14996 /* This code is not used for mini-buffer for the sake of the case
14997 of redisplaying to replace an echo area message; since in
14998 that case the mini-buffer contents per se are usually
14999 unchanged. This code is of no real use in the mini-buffer
15000 since the handling of this_line_start_pos, etc., in redisplay
15001 handles the same cases. */
15002 && !EQ (window, minibuf_window)
15003 && (FRAME_WINDOW_P (f)
15004 || !overlay_arrow_in_current_buffer_p ()))
15005 {
15006 int this_scroll_margin, top_scroll_margin;
15007 struct glyph_row *row = NULL;
15008 int frame_line_height = default_line_pixel_height (w);
15009 int window_total_lines
15010 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15011
15012 #ifdef GLYPH_DEBUG
15013 debug_method_add (w, "cursor movement");
15014 #endif
15015
15016 /* Scroll if point within this distance from the top or bottom
15017 of the window. This is a pixel value. */
15018 if (scroll_margin > 0)
15019 {
15020 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15021 this_scroll_margin *= frame_line_height;
15022 }
15023 else
15024 this_scroll_margin = 0;
15025
15026 top_scroll_margin = this_scroll_margin;
15027 if (WINDOW_WANTS_HEADER_LINE_P (w))
15028 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15029
15030 /* Start with the row the cursor was displayed during the last
15031 not paused redisplay. Give up if that row is not valid. */
15032 if (w->last_cursor_vpos < 0
15033 || w->last_cursor_vpos >= w->current_matrix->nrows)
15034 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15035 else
15036 {
15037 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15038 if (row->mode_line_p)
15039 ++row;
15040 if (!row->enabled_p)
15041 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15042 }
15043
15044 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15045 {
15046 int scroll_p = 0, must_scroll = 0;
15047 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15048
15049 if (PT > w->last_point)
15050 {
15051 /* Point has moved forward. */
15052 while (MATRIX_ROW_END_CHARPOS (row) < PT
15053 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15054 {
15055 eassert (row->enabled_p);
15056 ++row;
15057 }
15058
15059 /* If the end position of a row equals the start
15060 position of the next row, and PT is at that position,
15061 we would rather display cursor in the next line. */
15062 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15063 && MATRIX_ROW_END_CHARPOS (row) == PT
15064 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15065 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15066 && !cursor_row_p (row))
15067 ++row;
15068
15069 /* If within the scroll margin, scroll. Note that
15070 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15071 the next line would be drawn, and that
15072 this_scroll_margin can be zero. */
15073 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15074 || PT > MATRIX_ROW_END_CHARPOS (row)
15075 /* Line is completely visible last line in window
15076 and PT is to be set in the next line. */
15077 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15078 && PT == MATRIX_ROW_END_CHARPOS (row)
15079 && !row->ends_at_zv_p
15080 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15081 scroll_p = 1;
15082 }
15083 else if (PT < w->last_point)
15084 {
15085 /* Cursor has to be moved backward. Note that PT >=
15086 CHARPOS (startp) because of the outer if-statement. */
15087 while (!row->mode_line_p
15088 && (MATRIX_ROW_START_CHARPOS (row) > PT
15089 || (MATRIX_ROW_START_CHARPOS (row) == PT
15090 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15091 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15092 row > w->current_matrix->rows
15093 && (row-1)->ends_in_newline_from_string_p))))
15094 && (row->y > top_scroll_margin
15095 || CHARPOS (startp) == BEGV))
15096 {
15097 eassert (row->enabled_p);
15098 --row;
15099 }
15100
15101 /* Consider the following case: Window starts at BEGV,
15102 there is invisible, intangible text at BEGV, so that
15103 display starts at some point START > BEGV. It can
15104 happen that we are called with PT somewhere between
15105 BEGV and START. Try to handle that case. */
15106 if (row < w->current_matrix->rows
15107 || row->mode_line_p)
15108 {
15109 row = w->current_matrix->rows;
15110 if (row->mode_line_p)
15111 ++row;
15112 }
15113
15114 /* Due to newlines in overlay strings, we may have to
15115 skip forward over overlay strings. */
15116 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15117 && MATRIX_ROW_END_CHARPOS (row) == PT
15118 && !cursor_row_p (row))
15119 ++row;
15120
15121 /* If within the scroll margin, scroll. */
15122 if (row->y < top_scroll_margin
15123 && CHARPOS (startp) != BEGV)
15124 scroll_p = 1;
15125 }
15126 else
15127 {
15128 /* Cursor did not move. So don't scroll even if cursor line
15129 is partially visible, as it was so before. */
15130 rc = CURSOR_MOVEMENT_SUCCESS;
15131 }
15132
15133 if (PT < MATRIX_ROW_START_CHARPOS (row)
15134 || PT > MATRIX_ROW_END_CHARPOS (row))
15135 {
15136 /* if PT is not in the glyph row, give up. */
15137 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15138 must_scroll = 1;
15139 }
15140 else if (rc != CURSOR_MOVEMENT_SUCCESS
15141 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15142 {
15143 struct glyph_row *row1;
15144
15145 /* If rows are bidi-reordered and point moved, back up
15146 until we find a row that does not belong to a
15147 continuation line. This is because we must consider
15148 all rows of a continued line as candidates for the
15149 new cursor positioning, since row start and end
15150 positions change non-linearly with vertical position
15151 in such rows. */
15152 /* FIXME: Revisit this when glyph ``spilling'' in
15153 continuation lines' rows is implemented for
15154 bidi-reordered rows. */
15155 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15156 MATRIX_ROW_CONTINUATION_LINE_P (row);
15157 --row)
15158 {
15159 /* If we hit the beginning of the displayed portion
15160 without finding the first row of a continued
15161 line, give up. */
15162 if (row <= row1)
15163 {
15164 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15165 break;
15166 }
15167 eassert (row->enabled_p);
15168 }
15169 }
15170 if (must_scroll)
15171 ;
15172 else if (rc != CURSOR_MOVEMENT_SUCCESS
15173 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15174 /* Make sure this isn't a header line by any chance, since
15175 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15176 && !row->mode_line_p
15177 && make_cursor_line_fully_visible_p)
15178 {
15179 if (PT == MATRIX_ROW_END_CHARPOS (row)
15180 && !row->ends_at_zv_p
15181 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15182 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15183 else if (row->height > window_box_height (w))
15184 {
15185 /* If we end up in a partially visible line, let's
15186 make it fully visible, except when it's taller
15187 than the window, in which case we can't do much
15188 about it. */
15189 *scroll_step = 1;
15190 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15191 }
15192 else
15193 {
15194 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15195 if (!cursor_row_fully_visible_p (w, 0, 1))
15196 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15197 else
15198 rc = CURSOR_MOVEMENT_SUCCESS;
15199 }
15200 }
15201 else if (scroll_p)
15202 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15203 else if (rc != CURSOR_MOVEMENT_SUCCESS
15204 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15205 {
15206 /* With bidi-reordered rows, there could be more than
15207 one candidate row whose start and end positions
15208 occlude point. We need to let set_cursor_from_row
15209 find the best candidate. */
15210 /* FIXME: Revisit this when glyph ``spilling'' in
15211 continuation lines' rows is implemented for
15212 bidi-reordered rows. */
15213 int rv = 0;
15214
15215 do
15216 {
15217 int at_zv_p = 0, exact_match_p = 0;
15218
15219 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15220 && PT <= MATRIX_ROW_END_CHARPOS (row)
15221 && cursor_row_p (row))
15222 rv |= set_cursor_from_row (w, row, w->current_matrix,
15223 0, 0, 0, 0);
15224 /* As soon as we've found the exact match for point,
15225 or the first suitable row whose ends_at_zv_p flag
15226 is set, we are done. */
15227 at_zv_p =
15228 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15229 if (rv && !at_zv_p
15230 && w->cursor.hpos >= 0
15231 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15232 w->cursor.vpos))
15233 {
15234 struct glyph_row *candidate =
15235 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15236 struct glyph *g =
15237 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15238 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15239
15240 exact_match_p =
15241 (BUFFERP (g->object) && g->charpos == PT)
15242 || (INTEGERP (g->object)
15243 && (g->charpos == PT
15244 || (g->charpos == 0 && endpos - 1 == PT)));
15245 }
15246 if (rv && (at_zv_p || exact_match_p))
15247 {
15248 rc = CURSOR_MOVEMENT_SUCCESS;
15249 break;
15250 }
15251 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15252 break;
15253 ++row;
15254 }
15255 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15256 || row->continued_p)
15257 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15258 || (MATRIX_ROW_START_CHARPOS (row) == PT
15259 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15260 /* If we didn't find any candidate rows, or exited the
15261 loop before all the candidates were examined, signal
15262 to the caller that this method failed. */
15263 if (rc != CURSOR_MOVEMENT_SUCCESS
15264 && !(rv
15265 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15266 && !row->continued_p))
15267 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15268 else if (rv)
15269 rc = CURSOR_MOVEMENT_SUCCESS;
15270 }
15271 else
15272 {
15273 do
15274 {
15275 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15276 {
15277 rc = CURSOR_MOVEMENT_SUCCESS;
15278 break;
15279 }
15280 ++row;
15281 }
15282 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15283 && MATRIX_ROW_START_CHARPOS (row) == PT
15284 && cursor_row_p (row));
15285 }
15286 }
15287 }
15288
15289 return rc;
15290 }
15291
15292 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15293 static
15294 #endif
15295 void
15296 set_vertical_scroll_bar (struct window *w)
15297 {
15298 ptrdiff_t start, end, whole;
15299
15300 /* Calculate the start and end positions for the current window.
15301 At some point, it would be nice to choose between scrollbars
15302 which reflect the whole buffer size, with special markers
15303 indicating narrowing, and scrollbars which reflect only the
15304 visible region.
15305
15306 Note that mini-buffers sometimes aren't displaying any text. */
15307 if (!MINI_WINDOW_P (w)
15308 || (w == XWINDOW (minibuf_window)
15309 && NILP (echo_area_buffer[0])))
15310 {
15311 struct buffer *buf = XBUFFER (w->contents);
15312 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15313 start = marker_position (w->start) - BUF_BEGV (buf);
15314 /* I don't think this is guaranteed to be right. For the
15315 moment, we'll pretend it is. */
15316 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15317
15318 if (end < start)
15319 end = start;
15320 if (whole < (end - start))
15321 whole = end - start;
15322 }
15323 else
15324 start = end = whole = 0;
15325
15326 /* Indicate what this scroll bar ought to be displaying now. */
15327 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15328 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15329 (w, end - start, whole, start);
15330 }
15331
15332
15333 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15334 selected_window is redisplayed.
15335
15336 We can return without actually redisplaying the window if
15337 fonts_changed_p. In that case, redisplay_internal will
15338 retry. */
15339
15340 static void
15341 redisplay_window (Lisp_Object window, int just_this_one_p)
15342 {
15343 struct window *w = XWINDOW (window);
15344 struct frame *f = XFRAME (w->frame);
15345 struct buffer *buffer = XBUFFER (w->contents);
15346 struct buffer *old = current_buffer;
15347 struct text_pos lpoint, opoint, startp;
15348 int update_mode_line;
15349 int tem;
15350 struct it it;
15351 /* Record it now because it's overwritten. */
15352 int current_matrix_up_to_date_p = 0;
15353 int used_current_matrix_p = 0;
15354 /* This is less strict than current_matrix_up_to_date_p.
15355 It indicates that the buffer contents and narrowing are unchanged. */
15356 int buffer_unchanged_p = 0;
15357 int temp_scroll_step = 0;
15358 ptrdiff_t count = SPECPDL_INDEX ();
15359 int rc;
15360 int centering_position = -1;
15361 int last_line_misfit = 0;
15362 ptrdiff_t beg_unchanged, end_unchanged;
15363 int frame_line_height;
15364
15365 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15366 opoint = lpoint;
15367
15368 #ifdef GLYPH_DEBUG
15369 *w->desired_matrix->method = 0;
15370 #endif
15371
15372 /* Make sure that both W's markers are valid. */
15373 eassert (XMARKER (w->start)->buffer == buffer);
15374 eassert (XMARKER (w->pointm)->buffer == buffer);
15375
15376 restart:
15377 reconsider_clip_changes (w);
15378 frame_line_height = default_line_pixel_height (w);
15379
15380 /* Has the mode line to be updated? */
15381 update_mode_line = (w->update_mode_line
15382 || update_mode_lines
15383 || buffer->clip_changed
15384 || buffer->prevent_redisplay_optimizations_p);
15385
15386 if (MINI_WINDOW_P (w))
15387 {
15388 if (w == XWINDOW (echo_area_window)
15389 && !NILP (echo_area_buffer[0]))
15390 {
15391 if (update_mode_line)
15392 /* We may have to update a tty frame's menu bar or a
15393 tool-bar. Example `M-x C-h C-h C-g'. */
15394 goto finish_menu_bars;
15395 else
15396 /* We've already displayed the echo area glyphs in this window. */
15397 goto finish_scroll_bars;
15398 }
15399 else if ((w != XWINDOW (minibuf_window)
15400 || minibuf_level == 0)
15401 /* When buffer is nonempty, redisplay window normally. */
15402 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15403 /* Quail displays non-mini buffers in minibuffer window.
15404 In that case, redisplay the window normally. */
15405 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15406 {
15407 /* W is a mini-buffer window, but it's not active, so clear
15408 it. */
15409 int yb = window_text_bottom_y (w);
15410 struct glyph_row *row;
15411 int y;
15412
15413 for (y = 0, row = w->desired_matrix->rows;
15414 y < yb;
15415 y += row->height, ++row)
15416 blank_row (w, row, y);
15417 goto finish_scroll_bars;
15418 }
15419
15420 clear_glyph_matrix (w->desired_matrix);
15421 }
15422
15423 /* Otherwise set up data on this window; select its buffer and point
15424 value. */
15425 /* Really select the buffer, for the sake of buffer-local
15426 variables. */
15427 set_buffer_internal_1 (XBUFFER (w->contents));
15428
15429 current_matrix_up_to_date_p
15430 = (w->window_end_valid
15431 && !current_buffer->clip_changed
15432 && !current_buffer->prevent_redisplay_optimizations_p
15433 && !window_outdated (w));
15434
15435 /* Run the window-bottom-change-functions
15436 if it is possible that the text on the screen has changed
15437 (either due to modification of the text, or any other reason). */
15438 if (!current_matrix_up_to_date_p
15439 && !NILP (Vwindow_text_change_functions))
15440 {
15441 safe_run_hooks (Qwindow_text_change_functions);
15442 goto restart;
15443 }
15444
15445 beg_unchanged = BEG_UNCHANGED;
15446 end_unchanged = END_UNCHANGED;
15447
15448 SET_TEXT_POS (opoint, PT, PT_BYTE);
15449
15450 specbind (Qinhibit_point_motion_hooks, Qt);
15451
15452 buffer_unchanged_p
15453 = (w->window_end_valid
15454 && !current_buffer->clip_changed
15455 && !window_outdated (w));
15456
15457 /* When windows_or_buffers_changed is non-zero, we can't rely
15458 on the window end being valid, so set it to zero there. */
15459 if (windows_or_buffers_changed)
15460 {
15461 /* If window starts on a continuation line, maybe adjust the
15462 window start in case the window's width changed. */
15463 if (XMARKER (w->start)->buffer == current_buffer)
15464 compute_window_start_on_continuation_line (w);
15465
15466 w->window_end_valid = 0;
15467 /* If so, we also can't rely on current matrix
15468 and should not fool try_cursor_movement below. */
15469 current_matrix_up_to_date_p = 0;
15470 }
15471
15472 /* Some sanity checks. */
15473 CHECK_WINDOW_END (w);
15474 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15475 emacs_abort ();
15476 if (BYTEPOS (opoint) < CHARPOS (opoint))
15477 emacs_abort ();
15478
15479 if (mode_line_update_needed (w))
15480 update_mode_line = 1;
15481
15482 /* Point refers normally to the selected window. For any other
15483 window, set up appropriate value. */
15484 if (!EQ (window, selected_window))
15485 {
15486 ptrdiff_t new_pt = marker_position (w->pointm);
15487 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15488 if (new_pt < BEGV)
15489 {
15490 new_pt = BEGV;
15491 new_pt_byte = BEGV_BYTE;
15492 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15493 }
15494 else if (new_pt > (ZV - 1))
15495 {
15496 new_pt = ZV;
15497 new_pt_byte = ZV_BYTE;
15498 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15499 }
15500
15501 /* We don't use SET_PT so that the point-motion hooks don't run. */
15502 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15503 }
15504
15505 /* If any of the character widths specified in the display table
15506 have changed, invalidate the width run cache. It's true that
15507 this may be a bit late to catch such changes, but the rest of
15508 redisplay goes (non-fatally) haywire when the display table is
15509 changed, so why should we worry about doing any better? */
15510 if (current_buffer->width_run_cache)
15511 {
15512 struct Lisp_Char_Table *disptab = buffer_display_table ();
15513
15514 if (! disptab_matches_widthtab
15515 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15516 {
15517 invalidate_region_cache (current_buffer,
15518 current_buffer->width_run_cache,
15519 BEG, Z);
15520 recompute_width_table (current_buffer, disptab);
15521 }
15522 }
15523
15524 /* If window-start is screwed up, choose a new one. */
15525 if (XMARKER (w->start)->buffer != current_buffer)
15526 goto recenter;
15527
15528 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15529
15530 /* If someone specified a new starting point but did not insist,
15531 check whether it can be used. */
15532 if (w->optional_new_start
15533 && CHARPOS (startp) >= BEGV
15534 && CHARPOS (startp) <= ZV)
15535 {
15536 w->optional_new_start = 0;
15537 start_display (&it, w, startp);
15538 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15539 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15540 if (IT_CHARPOS (it) == PT)
15541 w->force_start = 1;
15542 /* IT may overshoot PT if text at PT is invisible. */
15543 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15544 w->force_start = 1;
15545 }
15546
15547 force_start:
15548
15549 /* Handle case where place to start displaying has been specified,
15550 unless the specified location is outside the accessible range. */
15551 if (w->force_start || window_frozen_p (w))
15552 {
15553 /* We set this later on if we have to adjust point. */
15554 int new_vpos = -1;
15555
15556 w->force_start = 0;
15557 w->vscroll = 0;
15558 w->window_end_valid = 0;
15559
15560 /* Forget any recorded base line for line number display. */
15561 if (!buffer_unchanged_p)
15562 w->base_line_number = 0;
15563
15564 /* Redisplay the mode line. Select the buffer properly for that.
15565 Also, run the hook window-scroll-functions
15566 because we have scrolled. */
15567 /* Note, we do this after clearing force_start because
15568 if there's an error, it is better to forget about force_start
15569 than to get into an infinite loop calling the hook functions
15570 and having them get more errors. */
15571 if (!update_mode_line
15572 || ! NILP (Vwindow_scroll_functions))
15573 {
15574 update_mode_line = 1;
15575 w->update_mode_line = 1;
15576 startp = run_window_scroll_functions (window, startp);
15577 }
15578
15579 if (CHARPOS (startp) < BEGV)
15580 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15581 else if (CHARPOS (startp) > ZV)
15582 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15583
15584 /* Redisplay, then check if cursor has been set during the
15585 redisplay. Give up if new fonts were loaded. */
15586 /* We used to issue a CHECK_MARGINS argument to try_window here,
15587 but this causes scrolling to fail when point begins inside
15588 the scroll margin (bug#148) -- cyd */
15589 if (!try_window (window, startp, 0))
15590 {
15591 w->force_start = 1;
15592 clear_glyph_matrix (w->desired_matrix);
15593 goto need_larger_matrices;
15594 }
15595
15596 if (w->cursor.vpos < 0 && !window_frozen_p (w))
15597 {
15598 /* If point does not appear, try to move point so it does
15599 appear. The desired matrix has been built above, so we
15600 can use it here. */
15601 new_vpos = window_box_height (w) / 2;
15602 }
15603
15604 if (!cursor_row_fully_visible_p (w, 0, 0))
15605 {
15606 /* Point does appear, but on a line partly visible at end of window.
15607 Move it back to a fully-visible line. */
15608 new_vpos = window_box_height (w);
15609 }
15610 else if (w->cursor.vpos >=0)
15611 {
15612 /* Some people insist on not letting point enter the scroll
15613 margin, even though this part handles windows that didn't
15614 scroll at all. */
15615 int window_total_lines
15616 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15617 int margin = min (scroll_margin, window_total_lines / 4);
15618 int pixel_margin = margin * frame_line_height;
15619 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15620
15621 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15622 below, which finds the row to move point to, advances by
15623 the Y coordinate of the _next_ row, see the definition of
15624 MATRIX_ROW_BOTTOM_Y. */
15625 if (w->cursor.vpos < margin + header_line)
15626 {
15627 w->cursor.vpos = -1;
15628 clear_glyph_matrix (w->desired_matrix);
15629 goto try_to_scroll;
15630 }
15631 else
15632 {
15633 int window_height = window_box_height (w);
15634
15635 if (header_line)
15636 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15637 if (w->cursor.y >= window_height - pixel_margin)
15638 {
15639 w->cursor.vpos = -1;
15640 clear_glyph_matrix (w->desired_matrix);
15641 goto try_to_scroll;
15642 }
15643 }
15644 }
15645
15646 /* If we need to move point for either of the above reasons,
15647 now actually do it. */
15648 if (new_vpos >= 0)
15649 {
15650 struct glyph_row *row;
15651
15652 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15653 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15654 ++row;
15655
15656 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15657 MATRIX_ROW_START_BYTEPOS (row));
15658
15659 if (w != XWINDOW (selected_window))
15660 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15661 else if (current_buffer == old)
15662 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15663
15664 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15665
15666 /* If we are highlighting the region, then we just changed
15667 the region, so redisplay to show it. */
15668 if (markpos_of_region () >= 0)
15669 {
15670 clear_glyph_matrix (w->desired_matrix);
15671 if (!try_window (window, startp, 0))
15672 goto need_larger_matrices;
15673 }
15674 }
15675
15676 #ifdef GLYPH_DEBUG
15677 debug_method_add (w, "forced window start");
15678 #endif
15679 goto done;
15680 }
15681
15682 /* Handle case where text has not changed, only point, and it has
15683 not moved off the frame, and we are not retrying after hscroll.
15684 (current_matrix_up_to_date_p is nonzero when retrying.) */
15685 if (current_matrix_up_to_date_p
15686 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15687 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15688 {
15689 switch (rc)
15690 {
15691 case CURSOR_MOVEMENT_SUCCESS:
15692 used_current_matrix_p = 1;
15693 goto done;
15694
15695 case CURSOR_MOVEMENT_MUST_SCROLL:
15696 goto try_to_scroll;
15697
15698 default:
15699 emacs_abort ();
15700 }
15701 }
15702 /* If current starting point was originally the beginning of a line
15703 but no longer is, find a new starting point. */
15704 else if (w->start_at_line_beg
15705 && !(CHARPOS (startp) <= BEGV
15706 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15707 {
15708 #ifdef GLYPH_DEBUG
15709 debug_method_add (w, "recenter 1");
15710 #endif
15711 goto recenter;
15712 }
15713
15714 /* Try scrolling with try_window_id. Value is > 0 if update has
15715 been done, it is -1 if we know that the same window start will
15716 not work. It is 0 if unsuccessful for some other reason. */
15717 else if ((tem = try_window_id (w)) != 0)
15718 {
15719 #ifdef GLYPH_DEBUG
15720 debug_method_add (w, "try_window_id %d", tem);
15721 #endif
15722
15723 if (fonts_changed_p)
15724 goto need_larger_matrices;
15725 if (tem > 0)
15726 goto done;
15727
15728 /* Otherwise try_window_id has returned -1 which means that we
15729 don't want the alternative below this comment to execute. */
15730 }
15731 else if (CHARPOS (startp) >= BEGV
15732 && CHARPOS (startp) <= ZV
15733 && PT >= CHARPOS (startp)
15734 && (CHARPOS (startp) < ZV
15735 /* Avoid starting at end of buffer. */
15736 || CHARPOS (startp) == BEGV
15737 || !window_outdated (w)))
15738 {
15739 int d1, d2, d3, d4, d5, d6;
15740
15741 /* If first window line is a continuation line, and window start
15742 is inside the modified region, but the first change is before
15743 current window start, we must select a new window start.
15744
15745 However, if this is the result of a down-mouse event (e.g. by
15746 extending the mouse-drag-overlay), we don't want to select a
15747 new window start, since that would change the position under
15748 the mouse, resulting in an unwanted mouse-movement rather
15749 than a simple mouse-click. */
15750 if (!w->start_at_line_beg
15751 && NILP (do_mouse_tracking)
15752 && CHARPOS (startp) > BEGV
15753 && CHARPOS (startp) > BEG + beg_unchanged
15754 && CHARPOS (startp) <= Z - end_unchanged
15755 /* Even if w->start_at_line_beg is nil, a new window may
15756 start at a line_beg, since that's how set_buffer_window
15757 sets it. So, we need to check the return value of
15758 compute_window_start_on_continuation_line. (See also
15759 bug#197). */
15760 && XMARKER (w->start)->buffer == current_buffer
15761 && compute_window_start_on_continuation_line (w)
15762 /* It doesn't make sense to force the window start like we
15763 do at label force_start if it is already known that point
15764 will not be visible in the resulting window, because
15765 doing so will move point from its correct position
15766 instead of scrolling the window to bring point into view.
15767 See bug#9324. */
15768 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15769 {
15770 w->force_start = 1;
15771 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15772 goto force_start;
15773 }
15774
15775 #ifdef GLYPH_DEBUG
15776 debug_method_add (w, "same window start");
15777 #endif
15778
15779 /* Try to redisplay starting at same place as before.
15780 If point has not moved off frame, accept the results. */
15781 if (!current_matrix_up_to_date_p
15782 /* Don't use try_window_reusing_current_matrix in this case
15783 because a window scroll function can have changed the
15784 buffer. */
15785 || !NILP (Vwindow_scroll_functions)
15786 || MINI_WINDOW_P (w)
15787 || !(used_current_matrix_p
15788 = try_window_reusing_current_matrix (w)))
15789 {
15790 IF_DEBUG (debug_method_add (w, "1"));
15791 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15792 /* -1 means we need to scroll.
15793 0 means we need new matrices, but fonts_changed_p
15794 is set in that case, so we will detect it below. */
15795 goto try_to_scroll;
15796 }
15797
15798 if (fonts_changed_p)
15799 goto need_larger_matrices;
15800
15801 if (w->cursor.vpos >= 0)
15802 {
15803 if (!just_this_one_p
15804 || current_buffer->clip_changed
15805 || BEG_UNCHANGED < CHARPOS (startp))
15806 /* Forget any recorded base line for line number display. */
15807 w->base_line_number = 0;
15808
15809 if (!cursor_row_fully_visible_p (w, 1, 0))
15810 {
15811 clear_glyph_matrix (w->desired_matrix);
15812 last_line_misfit = 1;
15813 }
15814 /* Drop through and scroll. */
15815 else
15816 goto done;
15817 }
15818 else
15819 clear_glyph_matrix (w->desired_matrix);
15820 }
15821
15822 try_to_scroll:
15823
15824 /* Redisplay the mode line. Select the buffer properly for that. */
15825 if (!update_mode_line)
15826 {
15827 update_mode_line = 1;
15828 w->update_mode_line = 1;
15829 }
15830
15831 /* Try to scroll by specified few lines. */
15832 if ((scroll_conservatively
15833 || emacs_scroll_step
15834 || temp_scroll_step
15835 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15836 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15837 && CHARPOS (startp) >= BEGV
15838 && CHARPOS (startp) <= ZV)
15839 {
15840 /* The function returns -1 if new fonts were loaded, 1 if
15841 successful, 0 if not successful. */
15842 int ss = try_scrolling (window, just_this_one_p,
15843 scroll_conservatively,
15844 emacs_scroll_step,
15845 temp_scroll_step, last_line_misfit);
15846 switch (ss)
15847 {
15848 case SCROLLING_SUCCESS:
15849 goto done;
15850
15851 case SCROLLING_NEED_LARGER_MATRICES:
15852 goto need_larger_matrices;
15853
15854 case SCROLLING_FAILED:
15855 break;
15856
15857 default:
15858 emacs_abort ();
15859 }
15860 }
15861
15862 /* Finally, just choose a place to start which positions point
15863 according to user preferences. */
15864
15865 recenter:
15866
15867 #ifdef GLYPH_DEBUG
15868 debug_method_add (w, "recenter");
15869 #endif
15870
15871 /* Forget any previously recorded base line for line number display. */
15872 if (!buffer_unchanged_p)
15873 w->base_line_number = 0;
15874
15875 /* Determine the window start relative to point. */
15876 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15877 it.current_y = it.last_visible_y;
15878 if (centering_position < 0)
15879 {
15880 int window_total_lines
15881 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15882 int margin =
15883 scroll_margin > 0
15884 ? min (scroll_margin, window_total_lines / 4)
15885 : 0;
15886 ptrdiff_t margin_pos = CHARPOS (startp);
15887 Lisp_Object aggressive;
15888 int scrolling_up;
15889
15890 /* If there is a scroll margin at the top of the window, find
15891 its character position. */
15892 if (margin
15893 /* Cannot call start_display if startp is not in the
15894 accessible region of the buffer. This can happen when we
15895 have just switched to a different buffer and/or changed
15896 its restriction. In that case, startp is initialized to
15897 the character position 1 (BEGV) because we did not yet
15898 have chance to display the buffer even once. */
15899 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15900 {
15901 struct it it1;
15902 void *it1data = NULL;
15903
15904 SAVE_IT (it1, it, it1data);
15905 start_display (&it1, w, startp);
15906 move_it_vertically (&it1, margin * frame_line_height);
15907 margin_pos = IT_CHARPOS (it1);
15908 RESTORE_IT (&it, &it, it1data);
15909 }
15910 scrolling_up = PT > margin_pos;
15911 aggressive =
15912 scrolling_up
15913 ? BVAR (current_buffer, scroll_up_aggressively)
15914 : BVAR (current_buffer, scroll_down_aggressively);
15915
15916 if (!MINI_WINDOW_P (w)
15917 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15918 {
15919 int pt_offset = 0;
15920
15921 /* Setting scroll-conservatively overrides
15922 scroll-*-aggressively. */
15923 if (!scroll_conservatively && NUMBERP (aggressive))
15924 {
15925 double float_amount = XFLOATINT (aggressive);
15926
15927 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15928 if (pt_offset == 0 && float_amount > 0)
15929 pt_offset = 1;
15930 if (pt_offset && margin > 0)
15931 margin -= 1;
15932 }
15933 /* Compute how much to move the window start backward from
15934 point so that point will be displayed where the user
15935 wants it. */
15936 if (scrolling_up)
15937 {
15938 centering_position = it.last_visible_y;
15939 if (pt_offset)
15940 centering_position -= pt_offset;
15941 centering_position -=
15942 frame_line_height * (1 + margin + (last_line_misfit != 0))
15943 + WINDOW_HEADER_LINE_HEIGHT (w);
15944 /* Don't let point enter the scroll margin near top of
15945 the window. */
15946 if (centering_position < margin * frame_line_height)
15947 centering_position = margin * frame_line_height;
15948 }
15949 else
15950 centering_position = margin * frame_line_height + pt_offset;
15951 }
15952 else
15953 /* Set the window start half the height of the window backward
15954 from point. */
15955 centering_position = window_box_height (w) / 2;
15956 }
15957 move_it_vertically_backward (&it, centering_position);
15958
15959 eassert (IT_CHARPOS (it) >= BEGV);
15960
15961 /* The function move_it_vertically_backward may move over more
15962 than the specified y-distance. If it->w is small, e.g. a
15963 mini-buffer window, we may end up in front of the window's
15964 display area. Start displaying at the start of the line
15965 containing PT in this case. */
15966 if (it.current_y <= 0)
15967 {
15968 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15969 move_it_vertically_backward (&it, 0);
15970 it.current_y = 0;
15971 }
15972
15973 it.current_x = it.hpos = 0;
15974
15975 /* Set the window start position here explicitly, to avoid an
15976 infinite loop in case the functions in window-scroll-functions
15977 get errors. */
15978 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15979
15980 /* Run scroll hooks. */
15981 startp = run_window_scroll_functions (window, it.current.pos);
15982
15983 /* Redisplay the window. */
15984 if (!current_matrix_up_to_date_p
15985 || windows_or_buffers_changed
15986 || cursor_type_changed
15987 /* Don't use try_window_reusing_current_matrix in this case
15988 because it can have changed the buffer. */
15989 || !NILP (Vwindow_scroll_functions)
15990 || !just_this_one_p
15991 || MINI_WINDOW_P (w)
15992 || !(used_current_matrix_p
15993 = try_window_reusing_current_matrix (w)))
15994 try_window (window, startp, 0);
15995
15996 /* If new fonts have been loaded (due to fontsets), give up. We
15997 have to start a new redisplay since we need to re-adjust glyph
15998 matrices. */
15999 if (fonts_changed_p)
16000 goto need_larger_matrices;
16001
16002 /* If cursor did not appear assume that the middle of the window is
16003 in the first line of the window. Do it again with the next line.
16004 (Imagine a window of height 100, displaying two lines of height
16005 60. Moving back 50 from it->last_visible_y will end in the first
16006 line.) */
16007 if (w->cursor.vpos < 0)
16008 {
16009 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16010 {
16011 clear_glyph_matrix (w->desired_matrix);
16012 move_it_by_lines (&it, 1);
16013 try_window (window, it.current.pos, 0);
16014 }
16015 else if (PT < IT_CHARPOS (it))
16016 {
16017 clear_glyph_matrix (w->desired_matrix);
16018 move_it_by_lines (&it, -1);
16019 try_window (window, it.current.pos, 0);
16020 }
16021 else
16022 {
16023 /* Not much we can do about it. */
16024 }
16025 }
16026
16027 /* Consider the following case: Window starts at BEGV, there is
16028 invisible, intangible text at BEGV, so that display starts at
16029 some point START > BEGV. It can happen that we are called with
16030 PT somewhere between BEGV and START. Try to handle that case. */
16031 if (w->cursor.vpos < 0)
16032 {
16033 struct glyph_row *row = w->current_matrix->rows;
16034 if (row->mode_line_p)
16035 ++row;
16036 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16037 }
16038
16039 if (!cursor_row_fully_visible_p (w, 0, 0))
16040 {
16041 /* If vscroll is enabled, disable it and try again. */
16042 if (w->vscroll)
16043 {
16044 w->vscroll = 0;
16045 clear_glyph_matrix (w->desired_matrix);
16046 goto recenter;
16047 }
16048
16049 /* Users who set scroll-conservatively to a large number want
16050 point just above/below the scroll margin. If we ended up
16051 with point's row partially visible, move the window start to
16052 make that row fully visible and out of the margin. */
16053 if (scroll_conservatively > SCROLL_LIMIT)
16054 {
16055 int window_total_lines
16056 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16057 int margin =
16058 scroll_margin > 0
16059 ? min (scroll_margin, window_total_lines / 4)
16060 : 0;
16061 int move_down = w->cursor.vpos >= window_total_lines / 2;
16062
16063 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16064 clear_glyph_matrix (w->desired_matrix);
16065 if (1 == try_window (window, it.current.pos,
16066 TRY_WINDOW_CHECK_MARGINS))
16067 goto done;
16068 }
16069
16070 /* If centering point failed to make the whole line visible,
16071 put point at the top instead. That has to make the whole line
16072 visible, if it can be done. */
16073 if (centering_position == 0)
16074 goto done;
16075
16076 clear_glyph_matrix (w->desired_matrix);
16077 centering_position = 0;
16078 goto recenter;
16079 }
16080
16081 done:
16082
16083 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16084 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16085 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16086
16087 /* Display the mode line, if we must. */
16088 if ((update_mode_line
16089 /* If window not full width, must redo its mode line
16090 if (a) the window to its side is being redone and
16091 (b) we do a frame-based redisplay. This is a consequence
16092 of how inverted lines are drawn in frame-based redisplay. */
16093 || (!just_this_one_p
16094 && !FRAME_WINDOW_P (f)
16095 && !WINDOW_FULL_WIDTH_P (w))
16096 /* Line number to display. */
16097 || w->base_line_pos > 0
16098 /* Column number is displayed and different from the one displayed. */
16099 || (w->column_number_displayed != -1
16100 && (w->column_number_displayed != current_column ())))
16101 /* This means that the window has a mode line. */
16102 && (WINDOW_WANTS_MODELINE_P (w)
16103 || WINDOW_WANTS_HEADER_LINE_P (w)))
16104 {
16105 display_mode_lines (w);
16106
16107 /* If mode line height has changed, arrange for a thorough
16108 immediate redisplay using the correct mode line height. */
16109 if (WINDOW_WANTS_MODELINE_P (w)
16110 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16111 {
16112 fonts_changed_p = 1;
16113 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16114 = DESIRED_MODE_LINE_HEIGHT (w);
16115 }
16116
16117 /* If header line height has changed, arrange for a thorough
16118 immediate redisplay using the correct header line height. */
16119 if (WINDOW_WANTS_HEADER_LINE_P (w)
16120 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16121 {
16122 fonts_changed_p = 1;
16123 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16124 = DESIRED_HEADER_LINE_HEIGHT (w);
16125 }
16126
16127 if (fonts_changed_p)
16128 goto need_larger_matrices;
16129 }
16130
16131 if (!line_number_displayed && w->base_line_pos != -1)
16132 {
16133 w->base_line_pos = 0;
16134 w->base_line_number = 0;
16135 }
16136
16137 finish_menu_bars:
16138
16139 /* When we reach a frame's selected window, redo the frame's menu bar. */
16140 if (update_mode_line
16141 && EQ (FRAME_SELECTED_WINDOW (f), window))
16142 {
16143 int redisplay_menu_p = 0;
16144
16145 if (FRAME_WINDOW_P (f))
16146 {
16147 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16148 || defined (HAVE_NS) || defined (USE_GTK)
16149 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16150 #else
16151 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16152 #endif
16153 }
16154 else
16155 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16156
16157 if (redisplay_menu_p)
16158 display_menu_bar (w);
16159
16160 #ifdef HAVE_WINDOW_SYSTEM
16161 if (FRAME_WINDOW_P (f))
16162 {
16163 #if defined (USE_GTK) || defined (HAVE_NS)
16164 if (FRAME_EXTERNAL_TOOL_BAR (f))
16165 redisplay_tool_bar (f);
16166 #else
16167 if (WINDOWP (f->tool_bar_window)
16168 && (FRAME_TOOL_BAR_LINES (f) > 0
16169 || !NILP (Vauto_resize_tool_bars))
16170 && redisplay_tool_bar (f))
16171 ignore_mouse_drag_p = 1;
16172 #endif
16173 }
16174 #endif
16175 }
16176
16177 #ifdef HAVE_WINDOW_SYSTEM
16178 if (FRAME_WINDOW_P (f)
16179 && update_window_fringes (w, (just_this_one_p
16180 || (!used_current_matrix_p && !overlay_arrow_seen)
16181 || w->pseudo_window_p)))
16182 {
16183 update_begin (f);
16184 block_input ();
16185 if (draw_window_fringes (w, 1))
16186 x_draw_vertical_border (w);
16187 unblock_input ();
16188 update_end (f);
16189 }
16190 #endif /* HAVE_WINDOW_SYSTEM */
16191
16192 /* We go to this label, with fonts_changed_p set,
16193 if it is necessary to try again using larger glyph matrices.
16194 We have to redeem the scroll bar even in this case,
16195 because the loop in redisplay_internal expects that. */
16196 need_larger_matrices:
16197 ;
16198 finish_scroll_bars:
16199
16200 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16201 {
16202 /* Set the thumb's position and size. */
16203 set_vertical_scroll_bar (w);
16204
16205 /* Note that we actually used the scroll bar attached to this
16206 window, so it shouldn't be deleted at the end of redisplay. */
16207 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16208 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16209 }
16210
16211 /* Restore current_buffer and value of point in it. The window
16212 update may have changed the buffer, so first make sure `opoint'
16213 is still valid (Bug#6177). */
16214 if (CHARPOS (opoint) < BEGV)
16215 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16216 else if (CHARPOS (opoint) > ZV)
16217 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16218 else
16219 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16220
16221 set_buffer_internal_1 (old);
16222 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16223 shorter. This can be caused by log truncation in *Messages*. */
16224 if (CHARPOS (lpoint) <= ZV)
16225 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16226
16227 unbind_to (count, Qnil);
16228 }
16229
16230
16231 /* Build the complete desired matrix of WINDOW with a window start
16232 buffer position POS.
16233
16234 Value is 1 if successful. It is zero if fonts were loaded during
16235 redisplay which makes re-adjusting glyph matrices necessary, and -1
16236 if point would appear in the scroll margins.
16237 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16238 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16239 set in FLAGS.) */
16240
16241 int
16242 try_window (Lisp_Object window, struct text_pos pos, int flags)
16243 {
16244 struct window *w = XWINDOW (window);
16245 struct it it;
16246 struct glyph_row *last_text_row = NULL;
16247 struct frame *f = XFRAME (w->frame);
16248 int frame_line_height = default_line_pixel_height (w);
16249
16250 /* Make POS the new window start. */
16251 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16252
16253 /* Mark cursor position as unknown. No overlay arrow seen. */
16254 w->cursor.vpos = -1;
16255 overlay_arrow_seen = 0;
16256
16257 /* Initialize iterator and info to start at POS. */
16258 start_display (&it, w, pos);
16259
16260 /* Display all lines of W. */
16261 while (it.current_y < it.last_visible_y)
16262 {
16263 if (display_line (&it))
16264 last_text_row = it.glyph_row - 1;
16265 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16266 return 0;
16267 }
16268
16269 /* Don't let the cursor end in the scroll margins. */
16270 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16271 && !MINI_WINDOW_P (w))
16272 {
16273 int this_scroll_margin;
16274 int window_total_lines
16275 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16276
16277 if (scroll_margin > 0)
16278 {
16279 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16280 this_scroll_margin *= frame_line_height;
16281 }
16282 else
16283 this_scroll_margin = 0;
16284
16285 if ((w->cursor.y >= 0 /* not vscrolled */
16286 && w->cursor.y < this_scroll_margin
16287 && CHARPOS (pos) > BEGV
16288 && IT_CHARPOS (it) < ZV)
16289 /* rms: considering make_cursor_line_fully_visible_p here
16290 seems to give wrong results. We don't want to recenter
16291 when the last line is partly visible, we want to allow
16292 that case to be handled in the usual way. */
16293 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16294 {
16295 w->cursor.vpos = -1;
16296 clear_glyph_matrix (w->desired_matrix);
16297 return -1;
16298 }
16299 }
16300
16301 /* If bottom moved off end of frame, change mode line percentage. */
16302 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
16303 w->update_mode_line = 1;
16304
16305 /* Set window_end_pos to the offset of the last character displayed
16306 on the window from the end of current_buffer. Set
16307 window_end_vpos to its row number. */
16308 if (last_text_row)
16309 {
16310 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16311 adjust_window_ends (w, last_text_row, 0);
16312 eassert
16313 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16314 w->window_end_vpos)));
16315 }
16316 else
16317 {
16318 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16319 w->window_end_pos = Z - ZV;
16320 w->window_end_vpos = 0;
16321 }
16322
16323 /* But that is not valid info until redisplay finishes. */
16324 w->window_end_valid = 0;
16325 return 1;
16326 }
16327
16328
16329 \f
16330 /************************************************************************
16331 Window redisplay reusing current matrix when buffer has not changed
16332 ************************************************************************/
16333
16334 /* Try redisplay of window W showing an unchanged buffer with a
16335 different window start than the last time it was displayed by
16336 reusing its current matrix. Value is non-zero if successful.
16337 W->start is the new window start. */
16338
16339 static int
16340 try_window_reusing_current_matrix (struct window *w)
16341 {
16342 struct frame *f = XFRAME (w->frame);
16343 struct glyph_row *bottom_row;
16344 struct it it;
16345 struct run run;
16346 struct text_pos start, new_start;
16347 int nrows_scrolled, i;
16348 struct glyph_row *last_text_row;
16349 struct glyph_row *last_reused_text_row;
16350 struct glyph_row *start_row;
16351 int start_vpos, min_y, max_y;
16352
16353 #ifdef GLYPH_DEBUG
16354 if (inhibit_try_window_reusing)
16355 return 0;
16356 #endif
16357
16358 if (/* This function doesn't handle terminal frames. */
16359 !FRAME_WINDOW_P (f)
16360 /* Don't try to reuse the display if windows have been split
16361 or such. */
16362 || windows_or_buffers_changed
16363 || cursor_type_changed)
16364 return 0;
16365
16366 /* Can't do this if region may have changed. */
16367 if (markpos_of_region () >= 0
16368 || w->region_showing
16369 || !NILP (Vshow_trailing_whitespace))
16370 return 0;
16371
16372 /* If top-line visibility has changed, give up. */
16373 if (WINDOW_WANTS_HEADER_LINE_P (w)
16374 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16375 return 0;
16376
16377 /* Give up if old or new display is scrolled vertically. We could
16378 make this function handle this, but right now it doesn't. */
16379 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16380 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16381 return 0;
16382
16383 /* The variable new_start now holds the new window start. The old
16384 start `start' can be determined from the current matrix. */
16385 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16386 start = start_row->minpos;
16387 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16388
16389 /* Clear the desired matrix for the display below. */
16390 clear_glyph_matrix (w->desired_matrix);
16391
16392 if (CHARPOS (new_start) <= CHARPOS (start))
16393 {
16394 /* Don't use this method if the display starts with an ellipsis
16395 displayed for invisible text. It's not easy to handle that case
16396 below, and it's certainly not worth the effort since this is
16397 not a frequent case. */
16398 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16399 return 0;
16400
16401 IF_DEBUG (debug_method_add (w, "twu1"));
16402
16403 /* Display up to a row that can be reused. The variable
16404 last_text_row is set to the last row displayed that displays
16405 text. Note that it.vpos == 0 if or if not there is a
16406 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16407 start_display (&it, w, new_start);
16408 w->cursor.vpos = -1;
16409 last_text_row = last_reused_text_row = NULL;
16410
16411 while (it.current_y < it.last_visible_y
16412 && !fonts_changed_p)
16413 {
16414 /* If we have reached into the characters in the START row,
16415 that means the line boundaries have changed. So we
16416 can't start copying with the row START. Maybe it will
16417 work to start copying with the following row. */
16418 while (IT_CHARPOS (it) > CHARPOS (start))
16419 {
16420 /* Advance to the next row as the "start". */
16421 start_row++;
16422 start = start_row->minpos;
16423 /* If there are no more rows to try, or just one, give up. */
16424 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16425 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16426 || CHARPOS (start) == ZV)
16427 {
16428 clear_glyph_matrix (w->desired_matrix);
16429 return 0;
16430 }
16431
16432 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16433 }
16434 /* If we have reached alignment, we can copy the rest of the
16435 rows. */
16436 if (IT_CHARPOS (it) == CHARPOS (start)
16437 /* Don't accept "alignment" inside a display vector,
16438 since start_row could have started in the middle of
16439 that same display vector (thus their character
16440 positions match), and we have no way of telling if
16441 that is the case. */
16442 && it.current.dpvec_index < 0)
16443 break;
16444
16445 if (display_line (&it))
16446 last_text_row = it.glyph_row - 1;
16447
16448 }
16449
16450 /* A value of current_y < last_visible_y means that we stopped
16451 at the previous window start, which in turn means that we
16452 have at least one reusable row. */
16453 if (it.current_y < it.last_visible_y)
16454 {
16455 struct glyph_row *row;
16456
16457 /* IT.vpos always starts from 0; it counts text lines. */
16458 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16459
16460 /* Find PT if not already found in the lines displayed. */
16461 if (w->cursor.vpos < 0)
16462 {
16463 int dy = it.current_y - start_row->y;
16464
16465 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16466 row = row_containing_pos (w, PT, row, NULL, dy);
16467 if (row)
16468 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16469 dy, nrows_scrolled);
16470 else
16471 {
16472 clear_glyph_matrix (w->desired_matrix);
16473 return 0;
16474 }
16475 }
16476
16477 /* Scroll the display. Do it before the current matrix is
16478 changed. The problem here is that update has not yet
16479 run, i.e. part of the current matrix is not up to date.
16480 scroll_run_hook will clear the cursor, and use the
16481 current matrix to get the height of the row the cursor is
16482 in. */
16483 run.current_y = start_row->y;
16484 run.desired_y = it.current_y;
16485 run.height = it.last_visible_y - it.current_y;
16486
16487 if (run.height > 0 && run.current_y != run.desired_y)
16488 {
16489 update_begin (f);
16490 FRAME_RIF (f)->update_window_begin_hook (w);
16491 FRAME_RIF (f)->clear_window_mouse_face (w);
16492 FRAME_RIF (f)->scroll_run_hook (w, &run);
16493 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16494 update_end (f);
16495 }
16496
16497 /* Shift current matrix down by nrows_scrolled lines. */
16498 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16499 rotate_matrix (w->current_matrix,
16500 start_vpos,
16501 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16502 nrows_scrolled);
16503
16504 /* Disable lines that must be updated. */
16505 for (i = 0; i < nrows_scrolled; ++i)
16506 (start_row + i)->enabled_p = 0;
16507
16508 /* Re-compute Y positions. */
16509 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16510 max_y = it.last_visible_y;
16511 for (row = start_row + nrows_scrolled;
16512 row < bottom_row;
16513 ++row)
16514 {
16515 row->y = it.current_y;
16516 row->visible_height = row->height;
16517
16518 if (row->y < min_y)
16519 row->visible_height -= min_y - row->y;
16520 if (row->y + row->height > max_y)
16521 row->visible_height -= row->y + row->height - max_y;
16522 if (row->fringe_bitmap_periodic_p)
16523 row->redraw_fringe_bitmaps_p = 1;
16524
16525 it.current_y += row->height;
16526
16527 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16528 last_reused_text_row = row;
16529 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16530 break;
16531 }
16532
16533 /* Disable lines in the current matrix which are now
16534 below the window. */
16535 for (++row; row < bottom_row; ++row)
16536 row->enabled_p = row->mode_line_p = 0;
16537 }
16538
16539 /* Update window_end_pos etc.; last_reused_text_row is the last
16540 reused row from the current matrix containing text, if any.
16541 The value of last_text_row is the last displayed line
16542 containing text. */
16543 if (last_reused_text_row)
16544 adjust_window_ends (w, last_reused_text_row, 1);
16545 else if (last_text_row)
16546 adjust_window_ends (w, last_text_row, 0);
16547 else
16548 {
16549 /* This window must be completely empty. */
16550 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16551 w->window_end_pos = Z - ZV;
16552 w->window_end_vpos = 0;
16553 }
16554 w->window_end_valid = 0;
16555
16556 /* Update hint: don't try scrolling again in update_window. */
16557 w->desired_matrix->no_scrolling_p = 1;
16558
16559 #ifdef GLYPH_DEBUG
16560 debug_method_add (w, "try_window_reusing_current_matrix 1");
16561 #endif
16562 return 1;
16563 }
16564 else if (CHARPOS (new_start) > CHARPOS (start))
16565 {
16566 struct glyph_row *pt_row, *row;
16567 struct glyph_row *first_reusable_row;
16568 struct glyph_row *first_row_to_display;
16569 int dy;
16570 int yb = window_text_bottom_y (w);
16571
16572 /* Find the row starting at new_start, if there is one. Don't
16573 reuse a partially visible line at the end. */
16574 first_reusable_row = start_row;
16575 while (first_reusable_row->enabled_p
16576 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16577 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16578 < CHARPOS (new_start)))
16579 ++first_reusable_row;
16580
16581 /* Give up if there is no row to reuse. */
16582 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16583 || !first_reusable_row->enabled_p
16584 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16585 != CHARPOS (new_start)))
16586 return 0;
16587
16588 /* We can reuse fully visible rows beginning with
16589 first_reusable_row to the end of the window. Set
16590 first_row_to_display to the first row that cannot be reused.
16591 Set pt_row to the row containing point, if there is any. */
16592 pt_row = NULL;
16593 for (first_row_to_display = first_reusable_row;
16594 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16595 ++first_row_to_display)
16596 {
16597 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16598 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16599 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16600 && first_row_to_display->ends_at_zv_p
16601 && pt_row == NULL)))
16602 pt_row = first_row_to_display;
16603 }
16604
16605 /* Start displaying at the start of first_row_to_display. */
16606 eassert (first_row_to_display->y < yb);
16607 init_to_row_start (&it, w, first_row_to_display);
16608
16609 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16610 - start_vpos);
16611 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16612 - nrows_scrolled);
16613 it.current_y = (first_row_to_display->y - first_reusable_row->y
16614 + WINDOW_HEADER_LINE_HEIGHT (w));
16615
16616 /* Display lines beginning with first_row_to_display in the
16617 desired matrix. Set last_text_row to the last row displayed
16618 that displays text. */
16619 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16620 if (pt_row == NULL)
16621 w->cursor.vpos = -1;
16622 last_text_row = NULL;
16623 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16624 if (display_line (&it))
16625 last_text_row = it.glyph_row - 1;
16626
16627 /* If point is in a reused row, adjust y and vpos of the cursor
16628 position. */
16629 if (pt_row)
16630 {
16631 w->cursor.vpos -= nrows_scrolled;
16632 w->cursor.y -= first_reusable_row->y - start_row->y;
16633 }
16634
16635 /* Give up if point isn't in a row displayed or reused. (This
16636 also handles the case where w->cursor.vpos < nrows_scrolled
16637 after the calls to display_line, which can happen with scroll
16638 margins. See bug#1295.) */
16639 if (w->cursor.vpos < 0)
16640 {
16641 clear_glyph_matrix (w->desired_matrix);
16642 return 0;
16643 }
16644
16645 /* Scroll the display. */
16646 run.current_y = first_reusable_row->y;
16647 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16648 run.height = it.last_visible_y - run.current_y;
16649 dy = run.current_y - run.desired_y;
16650
16651 if (run.height)
16652 {
16653 update_begin (f);
16654 FRAME_RIF (f)->update_window_begin_hook (w);
16655 FRAME_RIF (f)->clear_window_mouse_face (w);
16656 FRAME_RIF (f)->scroll_run_hook (w, &run);
16657 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16658 update_end (f);
16659 }
16660
16661 /* Adjust Y positions of reused rows. */
16662 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16663 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16664 max_y = it.last_visible_y;
16665 for (row = first_reusable_row; row < first_row_to_display; ++row)
16666 {
16667 row->y -= dy;
16668 row->visible_height = row->height;
16669 if (row->y < min_y)
16670 row->visible_height -= min_y - row->y;
16671 if (row->y + row->height > max_y)
16672 row->visible_height -= row->y + row->height - max_y;
16673 if (row->fringe_bitmap_periodic_p)
16674 row->redraw_fringe_bitmaps_p = 1;
16675 }
16676
16677 /* Scroll the current matrix. */
16678 eassert (nrows_scrolled > 0);
16679 rotate_matrix (w->current_matrix,
16680 start_vpos,
16681 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16682 -nrows_scrolled);
16683
16684 /* Disable rows not reused. */
16685 for (row -= nrows_scrolled; row < bottom_row; ++row)
16686 row->enabled_p = 0;
16687
16688 /* Point may have moved to a different line, so we cannot assume that
16689 the previous cursor position is valid; locate the correct row. */
16690 if (pt_row)
16691 {
16692 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16693 row < bottom_row
16694 && PT >= MATRIX_ROW_END_CHARPOS (row)
16695 && !row->ends_at_zv_p;
16696 row++)
16697 {
16698 w->cursor.vpos++;
16699 w->cursor.y = row->y;
16700 }
16701 if (row < bottom_row)
16702 {
16703 /* Can't simply scan the row for point with
16704 bidi-reordered glyph rows. Let set_cursor_from_row
16705 figure out where to put the cursor, and if it fails,
16706 give up. */
16707 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16708 {
16709 if (!set_cursor_from_row (w, row, w->current_matrix,
16710 0, 0, 0, 0))
16711 {
16712 clear_glyph_matrix (w->desired_matrix);
16713 return 0;
16714 }
16715 }
16716 else
16717 {
16718 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16719 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16720
16721 for (; glyph < end
16722 && (!BUFFERP (glyph->object)
16723 || glyph->charpos < PT);
16724 glyph++)
16725 {
16726 w->cursor.hpos++;
16727 w->cursor.x += glyph->pixel_width;
16728 }
16729 }
16730 }
16731 }
16732
16733 /* Adjust window end. A null value of last_text_row means that
16734 the window end is in reused rows which in turn means that
16735 only its vpos can have changed. */
16736 if (last_text_row)
16737 adjust_window_ends (w, last_text_row, 0);
16738 else
16739 w->window_end_vpos -= nrows_scrolled;
16740
16741 w->window_end_valid = 0;
16742 w->desired_matrix->no_scrolling_p = 1;
16743
16744 #ifdef GLYPH_DEBUG
16745 debug_method_add (w, "try_window_reusing_current_matrix 2");
16746 #endif
16747 return 1;
16748 }
16749
16750 return 0;
16751 }
16752
16753
16754 \f
16755 /************************************************************************
16756 Window redisplay reusing current matrix when buffer has changed
16757 ************************************************************************/
16758
16759 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16760 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16761 ptrdiff_t *, ptrdiff_t *);
16762 static struct glyph_row *
16763 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16764 struct glyph_row *);
16765
16766
16767 /* Return the last row in MATRIX displaying text. If row START is
16768 non-null, start searching with that row. IT gives the dimensions
16769 of the display. Value is null if matrix is empty; otherwise it is
16770 a pointer to the row found. */
16771
16772 static struct glyph_row *
16773 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16774 struct glyph_row *start)
16775 {
16776 struct glyph_row *row, *row_found;
16777
16778 /* Set row_found to the last row in IT->w's current matrix
16779 displaying text. The loop looks funny but think of partially
16780 visible lines. */
16781 row_found = NULL;
16782 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16783 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16784 {
16785 eassert (row->enabled_p);
16786 row_found = row;
16787 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16788 break;
16789 ++row;
16790 }
16791
16792 return row_found;
16793 }
16794
16795
16796 /* Return the last row in the current matrix of W that is not affected
16797 by changes at the start of current_buffer that occurred since W's
16798 current matrix was built. Value is null if no such row exists.
16799
16800 BEG_UNCHANGED us the number of characters unchanged at the start of
16801 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16802 first changed character in current_buffer. Characters at positions <
16803 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16804 when the current matrix was built. */
16805
16806 static struct glyph_row *
16807 find_last_unchanged_at_beg_row (struct window *w)
16808 {
16809 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16810 struct glyph_row *row;
16811 struct glyph_row *row_found = NULL;
16812 int yb = window_text_bottom_y (w);
16813
16814 /* Find the last row displaying unchanged text. */
16815 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16816 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16817 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16818 ++row)
16819 {
16820 if (/* If row ends before first_changed_pos, it is unchanged,
16821 except in some case. */
16822 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16823 /* When row ends in ZV and we write at ZV it is not
16824 unchanged. */
16825 && !row->ends_at_zv_p
16826 /* When first_changed_pos is the end of a continued line,
16827 row is not unchanged because it may be no longer
16828 continued. */
16829 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16830 && (row->continued_p
16831 || row->exact_window_width_line_p))
16832 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16833 needs to be recomputed, so don't consider this row as
16834 unchanged. This happens when the last line was
16835 bidi-reordered and was killed immediately before this
16836 redisplay cycle. In that case, ROW->end stores the
16837 buffer position of the first visual-order character of
16838 the killed text, which is now beyond ZV. */
16839 && CHARPOS (row->end.pos) <= ZV)
16840 row_found = row;
16841
16842 /* Stop if last visible row. */
16843 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16844 break;
16845 }
16846
16847 return row_found;
16848 }
16849
16850
16851 /* Find the first glyph row in the current matrix of W that is not
16852 affected by changes at the end of current_buffer since the
16853 time W's current matrix was built.
16854
16855 Return in *DELTA the number of chars by which buffer positions in
16856 unchanged text at the end of current_buffer must be adjusted.
16857
16858 Return in *DELTA_BYTES the corresponding number of bytes.
16859
16860 Value is null if no such row exists, i.e. all rows are affected by
16861 changes. */
16862
16863 static struct glyph_row *
16864 find_first_unchanged_at_end_row (struct window *w,
16865 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16866 {
16867 struct glyph_row *row;
16868 struct glyph_row *row_found = NULL;
16869
16870 *delta = *delta_bytes = 0;
16871
16872 /* Display must not have been paused, otherwise the current matrix
16873 is not up to date. */
16874 eassert (w->window_end_valid);
16875
16876 /* A value of window_end_pos >= END_UNCHANGED means that the window
16877 end is in the range of changed text. If so, there is no
16878 unchanged row at the end of W's current matrix. */
16879 if (w->window_end_pos >= END_UNCHANGED)
16880 return NULL;
16881
16882 /* Set row to the last row in W's current matrix displaying text. */
16883 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
16884
16885 /* If matrix is entirely empty, no unchanged row exists. */
16886 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16887 {
16888 /* The value of row is the last glyph row in the matrix having a
16889 meaningful buffer position in it. The end position of row
16890 corresponds to window_end_pos. This allows us to translate
16891 buffer positions in the current matrix to current buffer
16892 positions for characters not in changed text. */
16893 ptrdiff_t Z_old =
16894 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
16895 ptrdiff_t Z_BYTE_old =
16896 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16897 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16898 struct glyph_row *first_text_row
16899 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16900
16901 *delta = Z - Z_old;
16902 *delta_bytes = Z_BYTE - Z_BYTE_old;
16903
16904 /* Set last_unchanged_pos to the buffer position of the last
16905 character in the buffer that has not been changed. Z is the
16906 index + 1 of the last character in current_buffer, i.e. by
16907 subtracting END_UNCHANGED we get the index of the last
16908 unchanged character, and we have to add BEG to get its buffer
16909 position. */
16910 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16911 last_unchanged_pos_old = last_unchanged_pos - *delta;
16912
16913 /* Search backward from ROW for a row displaying a line that
16914 starts at a minimum position >= last_unchanged_pos_old. */
16915 for (; row > first_text_row; --row)
16916 {
16917 /* This used to abort, but it can happen.
16918 It is ok to just stop the search instead here. KFS. */
16919 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16920 break;
16921
16922 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16923 row_found = row;
16924 }
16925 }
16926
16927 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16928
16929 return row_found;
16930 }
16931
16932
16933 /* Make sure that glyph rows in the current matrix of window W
16934 reference the same glyph memory as corresponding rows in the
16935 frame's frame matrix. This function is called after scrolling W's
16936 current matrix on a terminal frame in try_window_id and
16937 try_window_reusing_current_matrix. */
16938
16939 static void
16940 sync_frame_with_window_matrix_rows (struct window *w)
16941 {
16942 struct frame *f = XFRAME (w->frame);
16943 struct glyph_row *window_row, *window_row_end, *frame_row;
16944
16945 /* Preconditions: W must be a leaf window and full-width. Its frame
16946 must have a frame matrix. */
16947 eassert (BUFFERP (w->contents));
16948 eassert (WINDOW_FULL_WIDTH_P (w));
16949 eassert (!FRAME_WINDOW_P (f));
16950
16951 /* If W is a full-width window, glyph pointers in W's current matrix
16952 have, by definition, to be the same as glyph pointers in the
16953 corresponding frame matrix. Note that frame matrices have no
16954 marginal areas (see build_frame_matrix). */
16955 window_row = w->current_matrix->rows;
16956 window_row_end = window_row + w->current_matrix->nrows;
16957 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16958 while (window_row < window_row_end)
16959 {
16960 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16961 struct glyph *end = window_row->glyphs[LAST_AREA];
16962
16963 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16964 frame_row->glyphs[TEXT_AREA] = start;
16965 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16966 frame_row->glyphs[LAST_AREA] = end;
16967
16968 /* Disable frame rows whose corresponding window rows have
16969 been disabled in try_window_id. */
16970 if (!window_row->enabled_p)
16971 frame_row->enabled_p = 0;
16972
16973 ++window_row, ++frame_row;
16974 }
16975 }
16976
16977
16978 /* Find the glyph row in window W containing CHARPOS. Consider all
16979 rows between START and END (not inclusive). END null means search
16980 all rows to the end of the display area of W. Value is the row
16981 containing CHARPOS or null. */
16982
16983 struct glyph_row *
16984 row_containing_pos (struct window *w, ptrdiff_t charpos,
16985 struct glyph_row *start, struct glyph_row *end, int dy)
16986 {
16987 struct glyph_row *row = start;
16988 struct glyph_row *best_row = NULL;
16989 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
16990 int last_y;
16991
16992 /* If we happen to start on a header-line, skip that. */
16993 if (row->mode_line_p)
16994 ++row;
16995
16996 if ((end && row >= end) || !row->enabled_p)
16997 return NULL;
16998
16999 last_y = window_text_bottom_y (w) - dy;
17000
17001 while (1)
17002 {
17003 /* Give up if we have gone too far. */
17004 if (end && row >= end)
17005 return NULL;
17006 /* This formerly returned if they were equal.
17007 I think that both quantities are of a "last plus one" type;
17008 if so, when they are equal, the row is within the screen. -- rms. */
17009 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17010 return NULL;
17011
17012 /* If it is in this row, return this row. */
17013 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17014 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17015 /* The end position of a row equals the start
17016 position of the next row. If CHARPOS is there, we
17017 would rather consider it displayed in the next
17018 line, except when this line ends in ZV. */
17019 && !row_for_charpos_p (row, charpos)))
17020 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17021 {
17022 struct glyph *g;
17023
17024 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17025 || (!best_row && !row->continued_p))
17026 return row;
17027 /* In bidi-reordered rows, there could be several rows whose
17028 edges surround CHARPOS, all of these rows belonging to
17029 the same continued line. We need to find the row which
17030 fits CHARPOS the best. */
17031 for (g = row->glyphs[TEXT_AREA];
17032 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17033 g++)
17034 {
17035 if (!STRINGP (g->object))
17036 {
17037 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17038 {
17039 mindif = eabs (g->charpos - charpos);
17040 best_row = row;
17041 /* Exact match always wins. */
17042 if (mindif == 0)
17043 return best_row;
17044 }
17045 }
17046 }
17047 }
17048 else if (best_row && !row->continued_p)
17049 return best_row;
17050 ++row;
17051 }
17052 }
17053
17054
17055 /* Try to redisplay window W by reusing its existing display. W's
17056 current matrix must be up to date when this function is called,
17057 i.e. window_end_valid must be nonzero.
17058
17059 Value is
17060
17061 1 if display has been updated
17062 0 if otherwise unsuccessful
17063 -1 if redisplay with same window start is known not to succeed
17064
17065 The following steps are performed:
17066
17067 1. Find the last row in the current matrix of W that is not
17068 affected by changes at the start of current_buffer. If no such row
17069 is found, give up.
17070
17071 2. Find the first row in W's current matrix that is not affected by
17072 changes at the end of current_buffer. Maybe there is no such row.
17073
17074 3. Display lines beginning with the row + 1 found in step 1 to the
17075 row found in step 2 or, if step 2 didn't find a row, to the end of
17076 the window.
17077
17078 4. If cursor is not known to appear on the window, give up.
17079
17080 5. If display stopped at the row found in step 2, scroll the
17081 display and current matrix as needed.
17082
17083 6. Maybe display some lines at the end of W, if we must. This can
17084 happen under various circumstances, like a partially visible line
17085 becoming fully visible, or because newly displayed lines are displayed
17086 in smaller font sizes.
17087
17088 7. Update W's window end information. */
17089
17090 static int
17091 try_window_id (struct window *w)
17092 {
17093 struct frame *f = XFRAME (w->frame);
17094 struct glyph_matrix *current_matrix = w->current_matrix;
17095 struct glyph_matrix *desired_matrix = w->desired_matrix;
17096 struct glyph_row *last_unchanged_at_beg_row;
17097 struct glyph_row *first_unchanged_at_end_row;
17098 struct glyph_row *row;
17099 struct glyph_row *bottom_row;
17100 int bottom_vpos;
17101 struct it it;
17102 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17103 int dvpos, dy;
17104 struct text_pos start_pos;
17105 struct run run;
17106 int first_unchanged_at_end_vpos = 0;
17107 struct glyph_row *last_text_row, *last_text_row_at_end;
17108 struct text_pos start;
17109 ptrdiff_t first_changed_charpos, last_changed_charpos;
17110
17111 #ifdef GLYPH_DEBUG
17112 if (inhibit_try_window_id)
17113 return 0;
17114 #endif
17115
17116 /* This is handy for debugging. */
17117 #if 0
17118 #define GIVE_UP(X) \
17119 do { \
17120 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17121 return 0; \
17122 } while (0)
17123 #else
17124 #define GIVE_UP(X) return 0
17125 #endif
17126
17127 SET_TEXT_POS_FROM_MARKER (start, w->start);
17128
17129 /* Don't use this for mini-windows because these can show
17130 messages and mini-buffers, and we don't handle that here. */
17131 if (MINI_WINDOW_P (w))
17132 GIVE_UP (1);
17133
17134 /* This flag is used to prevent redisplay optimizations. */
17135 if (windows_or_buffers_changed || cursor_type_changed)
17136 GIVE_UP (2);
17137
17138 /* Verify that narrowing has not changed.
17139 Also verify that we were not told to prevent redisplay optimizations.
17140 It would be nice to further
17141 reduce the number of cases where this prevents try_window_id. */
17142 if (current_buffer->clip_changed
17143 || current_buffer->prevent_redisplay_optimizations_p)
17144 GIVE_UP (3);
17145
17146 /* Window must either use window-based redisplay or be full width. */
17147 if (!FRAME_WINDOW_P (f)
17148 && (!FRAME_LINE_INS_DEL_OK (f)
17149 || !WINDOW_FULL_WIDTH_P (w)))
17150 GIVE_UP (4);
17151
17152 /* Give up if point is known NOT to appear in W. */
17153 if (PT < CHARPOS (start))
17154 GIVE_UP (5);
17155
17156 /* Another way to prevent redisplay optimizations. */
17157 if (w->last_modified == 0)
17158 GIVE_UP (6);
17159
17160 /* Verify that window is not hscrolled. */
17161 if (w->hscroll != 0)
17162 GIVE_UP (7);
17163
17164 /* Verify that display wasn't paused. */
17165 if (!w->window_end_valid)
17166 GIVE_UP (8);
17167
17168 /* Can't use this if highlighting a region because a cursor movement
17169 will do more than just set the cursor. */
17170 if (markpos_of_region () >= 0)
17171 GIVE_UP (9);
17172
17173 /* Likewise if highlighting trailing whitespace. */
17174 if (!NILP (Vshow_trailing_whitespace))
17175 GIVE_UP (11);
17176
17177 /* Likewise if showing a region. */
17178 if (w->region_showing)
17179 GIVE_UP (10);
17180
17181 /* Can't use this if overlay arrow position and/or string have
17182 changed. */
17183 if (overlay_arrows_changed_p ())
17184 GIVE_UP (12);
17185
17186 /* When word-wrap is on, adding a space to the first word of a
17187 wrapped line can change the wrap position, altering the line
17188 above it. It might be worthwhile to handle this more
17189 intelligently, but for now just redisplay from scratch. */
17190 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17191 GIVE_UP (21);
17192
17193 /* Under bidi reordering, adding or deleting a character in the
17194 beginning of a paragraph, before the first strong directional
17195 character, can change the base direction of the paragraph (unless
17196 the buffer specifies a fixed paragraph direction), which will
17197 require to redisplay the whole paragraph. It might be worthwhile
17198 to find the paragraph limits and widen the range of redisplayed
17199 lines to that, but for now just give up this optimization and
17200 redisplay from scratch. */
17201 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17202 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17203 GIVE_UP (22);
17204
17205 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17206 only if buffer has really changed. The reason is that the gap is
17207 initially at Z for freshly visited files. The code below would
17208 set end_unchanged to 0 in that case. */
17209 if (MODIFF > SAVE_MODIFF
17210 /* This seems to happen sometimes after saving a buffer. */
17211 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17212 {
17213 if (GPT - BEG < BEG_UNCHANGED)
17214 BEG_UNCHANGED = GPT - BEG;
17215 if (Z - GPT < END_UNCHANGED)
17216 END_UNCHANGED = Z - GPT;
17217 }
17218
17219 /* The position of the first and last character that has been changed. */
17220 first_changed_charpos = BEG + BEG_UNCHANGED;
17221 last_changed_charpos = Z - END_UNCHANGED;
17222
17223 /* If window starts after a line end, and the last change is in
17224 front of that newline, then changes don't affect the display.
17225 This case happens with stealth-fontification. Note that although
17226 the display is unchanged, glyph positions in the matrix have to
17227 be adjusted, of course. */
17228 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17229 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17230 && ((last_changed_charpos < CHARPOS (start)
17231 && CHARPOS (start) == BEGV)
17232 || (last_changed_charpos < CHARPOS (start) - 1
17233 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17234 {
17235 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17236 struct glyph_row *r0;
17237
17238 /* Compute how many chars/bytes have been added to or removed
17239 from the buffer. */
17240 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17241 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17242 Z_delta = Z - Z_old;
17243 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17244
17245 /* Give up if PT is not in the window. Note that it already has
17246 been checked at the start of try_window_id that PT is not in
17247 front of the window start. */
17248 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17249 GIVE_UP (13);
17250
17251 /* If window start is unchanged, we can reuse the whole matrix
17252 as is, after adjusting glyph positions. No need to compute
17253 the window end again, since its offset from Z hasn't changed. */
17254 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17255 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17256 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17257 /* PT must not be in a partially visible line. */
17258 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17259 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17260 {
17261 /* Adjust positions in the glyph matrix. */
17262 if (Z_delta || Z_delta_bytes)
17263 {
17264 struct glyph_row *r1
17265 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17266 increment_matrix_positions (w->current_matrix,
17267 MATRIX_ROW_VPOS (r0, current_matrix),
17268 MATRIX_ROW_VPOS (r1, current_matrix),
17269 Z_delta, Z_delta_bytes);
17270 }
17271
17272 /* Set the cursor. */
17273 row = row_containing_pos (w, PT, r0, NULL, 0);
17274 if (row)
17275 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17276 else
17277 emacs_abort ();
17278 return 1;
17279 }
17280 }
17281
17282 /* Handle the case that changes are all below what is displayed in
17283 the window, and that PT is in the window. This shortcut cannot
17284 be taken if ZV is visible in the window, and text has been added
17285 there that is visible in the window. */
17286 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17287 /* ZV is not visible in the window, or there are no
17288 changes at ZV, actually. */
17289 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17290 || first_changed_charpos == last_changed_charpos))
17291 {
17292 struct glyph_row *r0;
17293
17294 /* Give up if PT is not in the window. Note that it already has
17295 been checked at the start of try_window_id that PT is not in
17296 front of the window start. */
17297 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17298 GIVE_UP (14);
17299
17300 /* If window start is unchanged, we can reuse the whole matrix
17301 as is, without changing glyph positions since no text has
17302 been added/removed in front of the window end. */
17303 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17304 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17305 /* PT must not be in a partially visible line. */
17306 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17307 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17308 {
17309 /* We have to compute the window end anew since text
17310 could have been added/removed after it. */
17311 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17312 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17313
17314 /* Set the cursor. */
17315 row = row_containing_pos (w, PT, r0, NULL, 0);
17316 if (row)
17317 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17318 else
17319 emacs_abort ();
17320 return 2;
17321 }
17322 }
17323
17324 /* Give up if window start is in the changed area.
17325
17326 The condition used to read
17327
17328 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17329
17330 but why that was tested escapes me at the moment. */
17331 if (CHARPOS (start) >= first_changed_charpos
17332 && CHARPOS (start) <= last_changed_charpos)
17333 GIVE_UP (15);
17334
17335 /* Check that window start agrees with the start of the first glyph
17336 row in its current matrix. Check this after we know the window
17337 start is not in changed text, otherwise positions would not be
17338 comparable. */
17339 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17340 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17341 GIVE_UP (16);
17342
17343 /* Give up if the window ends in strings. Overlay strings
17344 at the end are difficult to handle, so don't try. */
17345 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
17346 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17347 GIVE_UP (20);
17348
17349 /* Compute the position at which we have to start displaying new
17350 lines. Some of the lines at the top of the window might be
17351 reusable because they are not displaying changed text. Find the
17352 last row in W's current matrix not affected by changes at the
17353 start of current_buffer. Value is null if changes start in the
17354 first line of window. */
17355 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17356 if (last_unchanged_at_beg_row)
17357 {
17358 /* Avoid starting to display in the middle of a character, a TAB
17359 for instance. This is easier than to set up the iterator
17360 exactly, and it's not a frequent case, so the additional
17361 effort wouldn't really pay off. */
17362 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17363 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17364 && last_unchanged_at_beg_row > w->current_matrix->rows)
17365 --last_unchanged_at_beg_row;
17366
17367 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17368 GIVE_UP (17);
17369
17370 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17371 GIVE_UP (18);
17372 start_pos = it.current.pos;
17373
17374 /* Start displaying new lines in the desired matrix at the same
17375 vpos we would use in the current matrix, i.e. below
17376 last_unchanged_at_beg_row. */
17377 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17378 current_matrix);
17379 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17380 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17381
17382 eassert (it.hpos == 0 && it.current_x == 0);
17383 }
17384 else
17385 {
17386 /* There are no reusable lines at the start of the window.
17387 Start displaying in the first text line. */
17388 start_display (&it, w, start);
17389 it.vpos = it.first_vpos;
17390 start_pos = it.current.pos;
17391 }
17392
17393 /* Find the first row that is not affected by changes at the end of
17394 the buffer. Value will be null if there is no unchanged row, in
17395 which case we must redisplay to the end of the window. delta
17396 will be set to the value by which buffer positions beginning with
17397 first_unchanged_at_end_row have to be adjusted due to text
17398 changes. */
17399 first_unchanged_at_end_row
17400 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17401 IF_DEBUG (debug_delta = delta);
17402 IF_DEBUG (debug_delta_bytes = delta_bytes);
17403
17404 /* Set stop_pos to the buffer position up to which we will have to
17405 display new lines. If first_unchanged_at_end_row != NULL, this
17406 is the buffer position of the start of the line displayed in that
17407 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17408 that we don't stop at a buffer position. */
17409 stop_pos = 0;
17410 if (first_unchanged_at_end_row)
17411 {
17412 eassert (last_unchanged_at_beg_row == NULL
17413 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17414
17415 /* If this is a continuation line, move forward to the next one
17416 that isn't. Changes in lines above affect this line.
17417 Caution: this may move first_unchanged_at_end_row to a row
17418 not displaying text. */
17419 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17420 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17421 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17422 < it.last_visible_y))
17423 ++first_unchanged_at_end_row;
17424
17425 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17426 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17427 >= it.last_visible_y))
17428 first_unchanged_at_end_row = NULL;
17429 else
17430 {
17431 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17432 + delta);
17433 first_unchanged_at_end_vpos
17434 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17435 eassert (stop_pos >= Z - END_UNCHANGED);
17436 }
17437 }
17438 else if (last_unchanged_at_beg_row == NULL)
17439 GIVE_UP (19);
17440
17441
17442 #ifdef GLYPH_DEBUG
17443
17444 /* Either there is no unchanged row at the end, or the one we have
17445 now displays text. This is a necessary condition for the window
17446 end pos calculation at the end of this function. */
17447 eassert (first_unchanged_at_end_row == NULL
17448 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17449
17450 debug_last_unchanged_at_beg_vpos
17451 = (last_unchanged_at_beg_row
17452 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17453 : -1);
17454 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17455
17456 #endif /* GLYPH_DEBUG */
17457
17458
17459 /* Display new lines. Set last_text_row to the last new line
17460 displayed which has text on it, i.e. might end up as being the
17461 line where the window_end_vpos is. */
17462 w->cursor.vpos = -1;
17463 last_text_row = NULL;
17464 overlay_arrow_seen = 0;
17465 while (it.current_y < it.last_visible_y
17466 && !fonts_changed_p
17467 && (first_unchanged_at_end_row == NULL
17468 || IT_CHARPOS (it) < stop_pos))
17469 {
17470 if (display_line (&it))
17471 last_text_row = it.glyph_row - 1;
17472 }
17473
17474 if (fonts_changed_p)
17475 return -1;
17476
17477
17478 /* Compute differences in buffer positions, y-positions etc. for
17479 lines reused at the bottom of the window. Compute what we can
17480 scroll. */
17481 if (first_unchanged_at_end_row
17482 /* No lines reused because we displayed everything up to the
17483 bottom of the window. */
17484 && it.current_y < it.last_visible_y)
17485 {
17486 dvpos = (it.vpos
17487 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17488 current_matrix));
17489 dy = it.current_y - first_unchanged_at_end_row->y;
17490 run.current_y = first_unchanged_at_end_row->y;
17491 run.desired_y = run.current_y + dy;
17492 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17493 }
17494 else
17495 {
17496 delta = delta_bytes = dvpos = dy
17497 = run.current_y = run.desired_y = run.height = 0;
17498 first_unchanged_at_end_row = NULL;
17499 }
17500 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17501
17502
17503 /* Find the cursor if not already found. We have to decide whether
17504 PT will appear on this window (it sometimes doesn't, but this is
17505 not a very frequent case.) This decision has to be made before
17506 the current matrix is altered. A value of cursor.vpos < 0 means
17507 that PT is either in one of the lines beginning at
17508 first_unchanged_at_end_row or below the window. Don't care for
17509 lines that might be displayed later at the window end; as
17510 mentioned, this is not a frequent case. */
17511 if (w->cursor.vpos < 0)
17512 {
17513 /* Cursor in unchanged rows at the top? */
17514 if (PT < CHARPOS (start_pos)
17515 && last_unchanged_at_beg_row)
17516 {
17517 row = row_containing_pos (w, PT,
17518 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17519 last_unchanged_at_beg_row + 1, 0);
17520 if (row)
17521 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17522 }
17523
17524 /* Start from first_unchanged_at_end_row looking for PT. */
17525 else if (first_unchanged_at_end_row)
17526 {
17527 row = row_containing_pos (w, PT - delta,
17528 first_unchanged_at_end_row, NULL, 0);
17529 if (row)
17530 set_cursor_from_row (w, row, w->current_matrix, delta,
17531 delta_bytes, dy, dvpos);
17532 }
17533
17534 /* Give up if cursor was not found. */
17535 if (w->cursor.vpos < 0)
17536 {
17537 clear_glyph_matrix (w->desired_matrix);
17538 return -1;
17539 }
17540 }
17541
17542 /* Don't let the cursor end in the scroll margins. */
17543 {
17544 int this_scroll_margin, cursor_height;
17545 int frame_line_height = default_line_pixel_height (w);
17546 int window_total_lines
17547 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
17548
17549 this_scroll_margin =
17550 max (0, min (scroll_margin, window_total_lines / 4));
17551 this_scroll_margin *= frame_line_height;
17552 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17553
17554 if ((w->cursor.y < this_scroll_margin
17555 && CHARPOS (start) > BEGV)
17556 /* Old redisplay didn't take scroll margin into account at the bottom,
17557 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17558 || (w->cursor.y + (make_cursor_line_fully_visible_p
17559 ? cursor_height + this_scroll_margin
17560 : 1)) > it.last_visible_y)
17561 {
17562 w->cursor.vpos = -1;
17563 clear_glyph_matrix (w->desired_matrix);
17564 return -1;
17565 }
17566 }
17567
17568 /* Scroll the display. Do it before changing the current matrix so
17569 that xterm.c doesn't get confused about where the cursor glyph is
17570 found. */
17571 if (dy && run.height)
17572 {
17573 update_begin (f);
17574
17575 if (FRAME_WINDOW_P (f))
17576 {
17577 FRAME_RIF (f)->update_window_begin_hook (w);
17578 FRAME_RIF (f)->clear_window_mouse_face (w);
17579 FRAME_RIF (f)->scroll_run_hook (w, &run);
17580 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17581 }
17582 else
17583 {
17584 /* Terminal frame. In this case, dvpos gives the number of
17585 lines to scroll by; dvpos < 0 means scroll up. */
17586 int from_vpos
17587 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17588 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17589 int end = (WINDOW_TOP_EDGE_LINE (w)
17590 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17591 + window_internal_height (w));
17592
17593 #if defined (HAVE_GPM) || defined (MSDOS)
17594 x_clear_window_mouse_face (w);
17595 #endif
17596 /* Perform the operation on the screen. */
17597 if (dvpos > 0)
17598 {
17599 /* Scroll last_unchanged_at_beg_row to the end of the
17600 window down dvpos lines. */
17601 set_terminal_window (f, end);
17602
17603 /* On dumb terminals delete dvpos lines at the end
17604 before inserting dvpos empty lines. */
17605 if (!FRAME_SCROLL_REGION_OK (f))
17606 ins_del_lines (f, end - dvpos, -dvpos);
17607
17608 /* Insert dvpos empty lines in front of
17609 last_unchanged_at_beg_row. */
17610 ins_del_lines (f, from, dvpos);
17611 }
17612 else if (dvpos < 0)
17613 {
17614 /* Scroll up last_unchanged_at_beg_vpos to the end of
17615 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17616 set_terminal_window (f, end);
17617
17618 /* Delete dvpos lines in front of
17619 last_unchanged_at_beg_vpos. ins_del_lines will set
17620 the cursor to the given vpos and emit |dvpos| delete
17621 line sequences. */
17622 ins_del_lines (f, from + dvpos, dvpos);
17623
17624 /* On a dumb terminal insert dvpos empty lines at the
17625 end. */
17626 if (!FRAME_SCROLL_REGION_OK (f))
17627 ins_del_lines (f, end + dvpos, -dvpos);
17628 }
17629
17630 set_terminal_window (f, 0);
17631 }
17632
17633 update_end (f);
17634 }
17635
17636 /* Shift reused rows of the current matrix to the right position.
17637 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17638 text. */
17639 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17640 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17641 if (dvpos < 0)
17642 {
17643 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17644 bottom_vpos, dvpos);
17645 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17646 bottom_vpos);
17647 }
17648 else if (dvpos > 0)
17649 {
17650 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17651 bottom_vpos, dvpos);
17652 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17653 first_unchanged_at_end_vpos + dvpos);
17654 }
17655
17656 /* For frame-based redisplay, make sure that current frame and window
17657 matrix are in sync with respect to glyph memory. */
17658 if (!FRAME_WINDOW_P (f))
17659 sync_frame_with_window_matrix_rows (w);
17660
17661 /* Adjust buffer positions in reused rows. */
17662 if (delta || delta_bytes)
17663 increment_matrix_positions (current_matrix,
17664 first_unchanged_at_end_vpos + dvpos,
17665 bottom_vpos, delta, delta_bytes);
17666
17667 /* Adjust Y positions. */
17668 if (dy)
17669 shift_glyph_matrix (w, current_matrix,
17670 first_unchanged_at_end_vpos + dvpos,
17671 bottom_vpos, dy);
17672
17673 if (first_unchanged_at_end_row)
17674 {
17675 first_unchanged_at_end_row += dvpos;
17676 if (first_unchanged_at_end_row->y >= it.last_visible_y
17677 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17678 first_unchanged_at_end_row = NULL;
17679 }
17680
17681 /* If scrolling up, there may be some lines to display at the end of
17682 the window. */
17683 last_text_row_at_end = NULL;
17684 if (dy < 0)
17685 {
17686 /* Scrolling up can leave for example a partially visible line
17687 at the end of the window to be redisplayed. */
17688 /* Set last_row to the glyph row in the current matrix where the
17689 window end line is found. It has been moved up or down in
17690 the matrix by dvpos. */
17691 int last_vpos = w->window_end_vpos + dvpos;
17692 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17693
17694 /* If last_row is the window end line, it should display text. */
17695 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
17696
17697 /* If window end line was partially visible before, begin
17698 displaying at that line. Otherwise begin displaying with the
17699 line following it. */
17700 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17701 {
17702 init_to_row_start (&it, w, last_row);
17703 it.vpos = last_vpos;
17704 it.current_y = last_row->y;
17705 }
17706 else
17707 {
17708 init_to_row_end (&it, w, last_row);
17709 it.vpos = 1 + last_vpos;
17710 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17711 ++last_row;
17712 }
17713
17714 /* We may start in a continuation line. If so, we have to
17715 get the right continuation_lines_width and current_x. */
17716 it.continuation_lines_width = last_row->continuation_lines_width;
17717 it.hpos = it.current_x = 0;
17718
17719 /* Display the rest of the lines at the window end. */
17720 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17721 while (it.current_y < it.last_visible_y
17722 && !fonts_changed_p)
17723 {
17724 /* Is it always sure that the display agrees with lines in
17725 the current matrix? I don't think so, so we mark rows
17726 displayed invalid in the current matrix by setting their
17727 enabled_p flag to zero. */
17728 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17729 if (display_line (&it))
17730 last_text_row_at_end = it.glyph_row - 1;
17731 }
17732 }
17733
17734 /* Update window_end_pos and window_end_vpos. */
17735 if (first_unchanged_at_end_row && !last_text_row_at_end)
17736 {
17737 /* Window end line if one of the preserved rows from the current
17738 matrix. Set row to the last row displaying text in current
17739 matrix starting at first_unchanged_at_end_row, after
17740 scrolling. */
17741 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17742 row = find_last_row_displaying_text (w->current_matrix, &it,
17743 first_unchanged_at_end_row);
17744 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17745 adjust_window_ends (w, row, 1);
17746 eassert (w->window_end_bytepos >= 0);
17747 IF_DEBUG (debug_method_add (w, "A"));
17748 }
17749 else if (last_text_row_at_end)
17750 {
17751 adjust_window_ends (w, last_text_row_at_end, 0);
17752 eassert (w->window_end_bytepos >= 0);
17753 IF_DEBUG (debug_method_add (w, "B"));
17754 }
17755 else if (last_text_row)
17756 {
17757 /* We have displayed either to the end of the window or at the
17758 end of the window, i.e. the last row with text is to be found
17759 in the desired matrix. */
17760 adjust_window_ends (w, last_text_row, 0);
17761 eassert (w->window_end_bytepos >= 0);
17762 }
17763 else if (first_unchanged_at_end_row == NULL
17764 && last_text_row == NULL
17765 && last_text_row_at_end == NULL)
17766 {
17767 /* Displayed to end of window, but no line containing text was
17768 displayed. Lines were deleted at the end of the window. */
17769 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17770 int vpos = w->window_end_vpos;
17771 struct glyph_row *current_row = current_matrix->rows + vpos;
17772 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17773
17774 for (row = NULL;
17775 row == NULL && vpos >= first_vpos;
17776 --vpos, --current_row, --desired_row)
17777 {
17778 if (desired_row->enabled_p)
17779 {
17780 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
17781 row = desired_row;
17782 }
17783 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
17784 row = current_row;
17785 }
17786
17787 eassert (row != NULL);
17788 w->window_end_vpos = vpos + 1;
17789 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17790 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17791 eassert (w->window_end_bytepos >= 0);
17792 IF_DEBUG (debug_method_add (w, "C"));
17793 }
17794 else
17795 emacs_abort ();
17796
17797 IF_DEBUG (debug_end_pos = w->window_end_pos;
17798 debug_end_vpos = w->window_end_vpos);
17799
17800 /* Record that display has not been completed. */
17801 w->window_end_valid = 0;
17802 w->desired_matrix->no_scrolling_p = 1;
17803 return 3;
17804
17805 #undef GIVE_UP
17806 }
17807
17808
17809 \f
17810 /***********************************************************************
17811 More debugging support
17812 ***********************************************************************/
17813
17814 #ifdef GLYPH_DEBUG
17815
17816 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17817 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17818 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17819
17820
17821 /* Dump the contents of glyph matrix MATRIX on stderr.
17822
17823 GLYPHS 0 means don't show glyph contents.
17824 GLYPHS 1 means show glyphs in short form
17825 GLYPHS > 1 means show glyphs in long form. */
17826
17827 void
17828 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17829 {
17830 int i;
17831 for (i = 0; i < matrix->nrows; ++i)
17832 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17833 }
17834
17835
17836 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17837 the glyph row and area where the glyph comes from. */
17838
17839 void
17840 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17841 {
17842 if (glyph->type == CHAR_GLYPH
17843 || glyph->type == GLYPHLESS_GLYPH)
17844 {
17845 fprintf (stderr,
17846 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17847 glyph - row->glyphs[TEXT_AREA],
17848 (glyph->type == CHAR_GLYPH
17849 ? 'C'
17850 : 'G'),
17851 glyph->charpos,
17852 (BUFFERP (glyph->object)
17853 ? 'B'
17854 : (STRINGP (glyph->object)
17855 ? 'S'
17856 : (INTEGERP (glyph->object)
17857 ? '0'
17858 : '-'))),
17859 glyph->pixel_width,
17860 glyph->u.ch,
17861 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17862 ? glyph->u.ch
17863 : '.'),
17864 glyph->face_id,
17865 glyph->left_box_line_p,
17866 glyph->right_box_line_p);
17867 }
17868 else if (glyph->type == STRETCH_GLYPH)
17869 {
17870 fprintf (stderr,
17871 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17872 glyph - row->glyphs[TEXT_AREA],
17873 'S',
17874 glyph->charpos,
17875 (BUFFERP (glyph->object)
17876 ? 'B'
17877 : (STRINGP (glyph->object)
17878 ? 'S'
17879 : (INTEGERP (glyph->object)
17880 ? '0'
17881 : '-'))),
17882 glyph->pixel_width,
17883 0,
17884 ' ',
17885 glyph->face_id,
17886 glyph->left_box_line_p,
17887 glyph->right_box_line_p);
17888 }
17889 else if (glyph->type == IMAGE_GLYPH)
17890 {
17891 fprintf (stderr,
17892 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17893 glyph - row->glyphs[TEXT_AREA],
17894 'I',
17895 glyph->charpos,
17896 (BUFFERP (glyph->object)
17897 ? 'B'
17898 : (STRINGP (glyph->object)
17899 ? 'S'
17900 : (INTEGERP (glyph->object)
17901 ? '0'
17902 : '-'))),
17903 glyph->pixel_width,
17904 glyph->u.img_id,
17905 '.',
17906 glyph->face_id,
17907 glyph->left_box_line_p,
17908 glyph->right_box_line_p);
17909 }
17910 else if (glyph->type == COMPOSITE_GLYPH)
17911 {
17912 fprintf (stderr,
17913 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
17914 glyph - row->glyphs[TEXT_AREA],
17915 '+',
17916 glyph->charpos,
17917 (BUFFERP (glyph->object)
17918 ? 'B'
17919 : (STRINGP (glyph->object)
17920 ? 'S'
17921 : (INTEGERP (glyph->object)
17922 ? '0'
17923 : '-'))),
17924 glyph->pixel_width,
17925 glyph->u.cmp.id);
17926 if (glyph->u.cmp.automatic)
17927 fprintf (stderr,
17928 "[%d-%d]",
17929 glyph->slice.cmp.from, glyph->slice.cmp.to);
17930 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17931 glyph->face_id,
17932 glyph->left_box_line_p,
17933 glyph->right_box_line_p);
17934 }
17935 }
17936
17937
17938 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17939 GLYPHS 0 means don't show glyph contents.
17940 GLYPHS 1 means show glyphs in short form
17941 GLYPHS > 1 means show glyphs in long form. */
17942
17943 void
17944 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17945 {
17946 if (glyphs != 1)
17947 {
17948 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17949 fprintf (stderr, "==============================================================================\n");
17950
17951 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17952 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17953 vpos,
17954 MATRIX_ROW_START_CHARPOS (row),
17955 MATRIX_ROW_END_CHARPOS (row),
17956 row->used[TEXT_AREA],
17957 row->contains_overlapping_glyphs_p,
17958 row->enabled_p,
17959 row->truncated_on_left_p,
17960 row->truncated_on_right_p,
17961 row->continued_p,
17962 MATRIX_ROW_CONTINUATION_LINE_P (row),
17963 MATRIX_ROW_DISPLAYS_TEXT_P (row),
17964 row->ends_at_zv_p,
17965 row->fill_line_p,
17966 row->ends_in_middle_of_char_p,
17967 row->starts_in_middle_of_char_p,
17968 row->mouse_face_p,
17969 row->x,
17970 row->y,
17971 row->pixel_width,
17972 row->height,
17973 row->visible_height,
17974 row->ascent,
17975 row->phys_ascent);
17976 /* The next 3 lines should align to "Start" in the header. */
17977 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
17978 row->end.overlay_string_index,
17979 row->continuation_lines_width);
17980 fprintf (stderr, " %9"pI"d %9"pI"d\n",
17981 CHARPOS (row->start.string_pos),
17982 CHARPOS (row->end.string_pos));
17983 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
17984 row->end.dpvec_index);
17985 }
17986
17987 if (glyphs > 1)
17988 {
17989 int area;
17990
17991 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17992 {
17993 struct glyph *glyph = row->glyphs[area];
17994 struct glyph *glyph_end = glyph + row->used[area];
17995
17996 /* Glyph for a line end in text. */
17997 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
17998 ++glyph_end;
17999
18000 if (glyph < glyph_end)
18001 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18002
18003 for (; glyph < glyph_end; ++glyph)
18004 dump_glyph (row, glyph, area);
18005 }
18006 }
18007 else if (glyphs == 1)
18008 {
18009 int area;
18010
18011 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18012 {
18013 char *s = alloca (row->used[area] + 4);
18014 int i;
18015
18016 for (i = 0; i < row->used[area]; ++i)
18017 {
18018 struct glyph *glyph = row->glyphs[area] + i;
18019 if (i == row->used[area] - 1
18020 && area == TEXT_AREA
18021 && INTEGERP (glyph->object)
18022 && glyph->type == CHAR_GLYPH
18023 && glyph->u.ch == ' ')
18024 {
18025 strcpy (&s[i], "[\\n]");
18026 i += 4;
18027 }
18028 else if (glyph->type == CHAR_GLYPH
18029 && glyph->u.ch < 0x80
18030 && glyph->u.ch >= ' ')
18031 s[i] = glyph->u.ch;
18032 else
18033 s[i] = '.';
18034 }
18035
18036 s[i] = '\0';
18037 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18038 }
18039 }
18040 }
18041
18042
18043 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18044 Sdump_glyph_matrix, 0, 1, "p",
18045 doc: /* Dump the current matrix of the selected window to stderr.
18046 Shows contents of glyph row structures. With non-nil
18047 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18048 glyphs in short form, otherwise show glyphs in long form. */)
18049 (Lisp_Object glyphs)
18050 {
18051 struct window *w = XWINDOW (selected_window);
18052 struct buffer *buffer = XBUFFER (w->contents);
18053
18054 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18055 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18056 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18057 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18058 fprintf (stderr, "=============================================\n");
18059 dump_glyph_matrix (w->current_matrix,
18060 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18061 return Qnil;
18062 }
18063
18064
18065 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18066 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18067 (void)
18068 {
18069 struct frame *f = XFRAME (selected_frame);
18070 dump_glyph_matrix (f->current_matrix, 1);
18071 return Qnil;
18072 }
18073
18074
18075 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18076 doc: /* Dump glyph row ROW to stderr.
18077 GLYPH 0 means don't dump glyphs.
18078 GLYPH 1 means dump glyphs in short form.
18079 GLYPH > 1 or omitted means dump glyphs in long form. */)
18080 (Lisp_Object row, Lisp_Object glyphs)
18081 {
18082 struct glyph_matrix *matrix;
18083 EMACS_INT vpos;
18084
18085 CHECK_NUMBER (row);
18086 matrix = XWINDOW (selected_window)->current_matrix;
18087 vpos = XINT (row);
18088 if (vpos >= 0 && vpos < matrix->nrows)
18089 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18090 vpos,
18091 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18092 return Qnil;
18093 }
18094
18095
18096 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18097 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18098 GLYPH 0 means don't dump glyphs.
18099 GLYPH 1 means dump glyphs in short form.
18100 GLYPH > 1 or omitted means dump glyphs in long form. */)
18101 (Lisp_Object row, Lisp_Object glyphs)
18102 {
18103 struct frame *sf = SELECTED_FRAME ();
18104 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18105 EMACS_INT vpos;
18106
18107 CHECK_NUMBER (row);
18108 vpos = XINT (row);
18109 if (vpos >= 0 && vpos < m->nrows)
18110 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18111 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18112 return Qnil;
18113 }
18114
18115
18116 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18117 doc: /* Toggle tracing of redisplay.
18118 With ARG, turn tracing on if and only if ARG is positive. */)
18119 (Lisp_Object arg)
18120 {
18121 if (NILP (arg))
18122 trace_redisplay_p = !trace_redisplay_p;
18123 else
18124 {
18125 arg = Fprefix_numeric_value (arg);
18126 trace_redisplay_p = XINT (arg) > 0;
18127 }
18128
18129 return Qnil;
18130 }
18131
18132
18133 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18134 doc: /* Like `format', but print result to stderr.
18135 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18136 (ptrdiff_t nargs, Lisp_Object *args)
18137 {
18138 Lisp_Object s = Fformat (nargs, args);
18139 fprintf (stderr, "%s", SDATA (s));
18140 return Qnil;
18141 }
18142
18143 #endif /* GLYPH_DEBUG */
18144
18145
18146 \f
18147 /***********************************************************************
18148 Building Desired Matrix Rows
18149 ***********************************************************************/
18150
18151 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18152 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18153
18154 static struct glyph_row *
18155 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18156 {
18157 struct frame *f = XFRAME (WINDOW_FRAME (w));
18158 struct buffer *buffer = XBUFFER (w->contents);
18159 struct buffer *old = current_buffer;
18160 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18161 int arrow_len = SCHARS (overlay_arrow_string);
18162 const unsigned char *arrow_end = arrow_string + arrow_len;
18163 const unsigned char *p;
18164 struct it it;
18165 bool multibyte_p;
18166 int n_glyphs_before;
18167
18168 set_buffer_temp (buffer);
18169 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18170 it.glyph_row->used[TEXT_AREA] = 0;
18171 SET_TEXT_POS (it.position, 0, 0);
18172
18173 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18174 p = arrow_string;
18175 while (p < arrow_end)
18176 {
18177 Lisp_Object face, ilisp;
18178
18179 /* Get the next character. */
18180 if (multibyte_p)
18181 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18182 else
18183 {
18184 it.c = it.char_to_display = *p, it.len = 1;
18185 if (! ASCII_CHAR_P (it.c))
18186 it.char_to_display = BYTE8_TO_CHAR (it.c);
18187 }
18188 p += it.len;
18189
18190 /* Get its face. */
18191 ilisp = make_number (p - arrow_string);
18192 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18193 it.face_id = compute_char_face (f, it.char_to_display, face);
18194
18195 /* Compute its width, get its glyphs. */
18196 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18197 SET_TEXT_POS (it.position, -1, -1);
18198 PRODUCE_GLYPHS (&it);
18199
18200 /* If this character doesn't fit any more in the line, we have
18201 to remove some glyphs. */
18202 if (it.current_x > it.last_visible_x)
18203 {
18204 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18205 break;
18206 }
18207 }
18208
18209 set_buffer_temp (old);
18210 return it.glyph_row;
18211 }
18212
18213
18214 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18215 glyphs to insert is determined by produce_special_glyphs. */
18216
18217 static void
18218 insert_left_trunc_glyphs (struct it *it)
18219 {
18220 struct it truncate_it;
18221 struct glyph *from, *end, *to, *toend;
18222
18223 eassert (!FRAME_WINDOW_P (it->f)
18224 || (!it->glyph_row->reversed_p
18225 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18226 || (it->glyph_row->reversed_p
18227 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18228
18229 /* Get the truncation glyphs. */
18230 truncate_it = *it;
18231 truncate_it.current_x = 0;
18232 truncate_it.face_id = DEFAULT_FACE_ID;
18233 truncate_it.glyph_row = &scratch_glyph_row;
18234 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18235 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18236 truncate_it.object = make_number (0);
18237 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18238
18239 /* Overwrite glyphs from IT with truncation glyphs. */
18240 if (!it->glyph_row->reversed_p)
18241 {
18242 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18243
18244 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18245 end = from + tused;
18246 to = it->glyph_row->glyphs[TEXT_AREA];
18247 toend = to + it->glyph_row->used[TEXT_AREA];
18248 if (FRAME_WINDOW_P (it->f))
18249 {
18250 /* On GUI frames, when variable-size fonts are displayed,
18251 the truncation glyphs may need more pixels than the row's
18252 glyphs they overwrite. We overwrite more glyphs to free
18253 enough screen real estate, and enlarge the stretch glyph
18254 on the right (see display_line), if there is one, to
18255 preserve the screen position of the truncation glyphs on
18256 the right. */
18257 int w = 0;
18258 struct glyph *g = to;
18259 short used;
18260
18261 /* The first glyph could be partially visible, in which case
18262 it->glyph_row->x will be negative. But we want the left
18263 truncation glyphs to be aligned at the left margin of the
18264 window, so we override the x coordinate at which the row
18265 will begin. */
18266 it->glyph_row->x = 0;
18267 while (g < toend && w < it->truncation_pixel_width)
18268 {
18269 w += g->pixel_width;
18270 ++g;
18271 }
18272 if (g - to - tused > 0)
18273 {
18274 memmove (to + tused, g, (toend - g) * sizeof(*g));
18275 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18276 }
18277 used = it->glyph_row->used[TEXT_AREA];
18278 if (it->glyph_row->truncated_on_right_p
18279 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18280 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18281 == STRETCH_GLYPH)
18282 {
18283 int extra = w - it->truncation_pixel_width;
18284
18285 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18286 }
18287 }
18288
18289 while (from < end)
18290 *to++ = *from++;
18291
18292 /* There may be padding glyphs left over. Overwrite them too. */
18293 if (!FRAME_WINDOW_P (it->f))
18294 {
18295 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18296 {
18297 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18298 while (from < end)
18299 *to++ = *from++;
18300 }
18301 }
18302
18303 if (to > toend)
18304 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18305 }
18306 else
18307 {
18308 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18309
18310 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18311 that back to front. */
18312 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18313 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18314 toend = it->glyph_row->glyphs[TEXT_AREA];
18315 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18316 if (FRAME_WINDOW_P (it->f))
18317 {
18318 int w = 0;
18319 struct glyph *g = to;
18320
18321 while (g >= toend && w < it->truncation_pixel_width)
18322 {
18323 w += g->pixel_width;
18324 --g;
18325 }
18326 if (to - g - tused > 0)
18327 to = g + tused;
18328 if (it->glyph_row->truncated_on_right_p
18329 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18330 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18331 {
18332 int extra = w - it->truncation_pixel_width;
18333
18334 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18335 }
18336 }
18337
18338 while (from >= end && to >= toend)
18339 *to-- = *from--;
18340 if (!FRAME_WINDOW_P (it->f))
18341 {
18342 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18343 {
18344 from =
18345 truncate_it.glyph_row->glyphs[TEXT_AREA]
18346 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18347 while (from >= end && to >= toend)
18348 *to-- = *from--;
18349 }
18350 }
18351 if (from >= end)
18352 {
18353 /* Need to free some room before prepending additional
18354 glyphs. */
18355 int move_by = from - end + 1;
18356 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18357 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18358
18359 for ( ; g >= g0; g--)
18360 g[move_by] = *g;
18361 while (from >= end)
18362 *to-- = *from--;
18363 it->glyph_row->used[TEXT_AREA] += move_by;
18364 }
18365 }
18366 }
18367
18368 /* Compute the hash code for ROW. */
18369 unsigned
18370 row_hash (struct glyph_row *row)
18371 {
18372 int area, k;
18373 unsigned hashval = 0;
18374
18375 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18376 for (k = 0; k < row->used[area]; ++k)
18377 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18378 + row->glyphs[area][k].u.val
18379 + row->glyphs[area][k].face_id
18380 + row->glyphs[area][k].padding_p
18381 + (row->glyphs[area][k].type << 2));
18382
18383 return hashval;
18384 }
18385
18386 /* Compute the pixel height and width of IT->glyph_row.
18387
18388 Most of the time, ascent and height of a display line will be equal
18389 to the max_ascent and max_height values of the display iterator
18390 structure. This is not the case if
18391
18392 1. We hit ZV without displaying anything. In this case, max_ascent
18393 and max_height will be zero.
18394
18395 2. We have some glyphs that don't contribute to the line height.
18396 (The glyph row flag contributes_to_line_height_p is for future
18397 pixmap extensions).
18398
18399 The first case is easily covered by using default values because in
18400 these cases, the line height does not really matter, except that it
18401 must not be zero. */
18402
18403 static void
18404 compute_line_metrics (struct it *it)
18405 {
18406 struct glyph_row *row = it->glyph_row;
18407
18408 if (FRAME_WINDOW_P (it->f))
18409 {
18410 int i, min_y, max_y;
18411
18412 /* The line may consist of one space only, that was added to
18413 place the cursor on it. If so, the row's height hasn't been
18414 computed yet. */
18415 if (row->height == 0)
18416 {
18417 if (it->max_ascent + it->max_descent == 0)
18418 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18419 row->ascent = it->max_ascent;
18420 row->height = it->max_ascent + it->max_descent;
18421 row->phys_ascent = it->max_phys_ascent;
18422 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18423 row->extra_line_spacing = it->max_extra_line_spacing;
18424 }
18425
18426 /* Compute the width of this line. */
18427 row->pixel_width = row->x;
18428 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18429 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18430
18431 eassert (row->pixel_width >= 0);
18432 eassert (row->ascent >= 0 && row->height > 0);
18433
18434 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18435 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18436
18437 /* If first line's physical ascent is larger than its logical
18438 ascent, use the physical ascent, and make the row taller.
18439 This makes accented characters fully visible. */
18440 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18441 && row->phys_ascent > row->ascent)
18442 {
18443 row->height += row->phys_ascent - row->ascent;
18444 row->ascent = row->phys_ascent;
18445 }
18446
18447 /* Compute how much of the line is visible. */
18448 row->visible_height = row->height;
18449
18450 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18451 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18452
18453 if (row->y < min_y)
18454 row->visible_height -= min_y - row->y;
18455 if (row->y + row->height > max_y)
18456 row->visible_height -= row->y + row->height - max_y;
18457 }
18458 else
18459 {
18460 row->pixel_width = row->used[TEXT_AREA];
18461 if (row->continued_p)
18462 row->pixel_width -= it->continuation_pixel_width;
18463 else if (row->truncated_on_right_p)
18464 row->pixel_width -= it->truncation_pixel_width;
18465 row->ascent = row->phys_ascent = 0;
18466 row->height = row->phys_height = row->visible_height = 1;
18467 row->extra_line_spacing = 0;
18468 }
18469
18470 /* Compute a hash code for this row. */
18471 row->hash = row_hash (row);
18472
18473 it->max_ascent = it->max_descent = 0;
18474 it->max_phys_ascent = it->max_phys_descent = 0;
18475 }
18476
18477
18478 /* Append one space to the glyph row of iterator IT if doing a
18479 window-based redisplay. The space has the same face as
18480 IT->face_id. Value is non-zero if a space was added.
18481
18482 This function is called to make sure that there is always one glyph
18483 at the end of a glyph row that the cursor can be set on under
18484 window-systems. (If there weren't such a glyph we would not know
18485 how wide and tall a box cursor should be displayed).
18486
18487 At the same time this space let's a nicely handle clearing to the
18488 end of the line if the row ends in italic text. */
18489
18490 static int
18491 append_space_for_newline (struct it *it, int default_face_p)
18492 {
18493 if (FRAME_WINDOW_P (it->f))
18494 {
18495 int n = it->glyph_row->used[TEXT_AREA];
18496
18497 if (it->glyph_row->glyphs[TEXT_AREA] + n
18498 < it->glyph_row->glyphs[1 + TEXT_AREA])
18499 {
18500 /* Save some values that must not be changed.
18501 Must save IT->c and IT->len because otherwise
18502 ITERATOR_AT_END_P wouldn't work anymore after
18503 append_space_for_newline has been called. */
18504 enum display_element_type saved_what = it->what;
18505 int saved_c = it->c, saved_len = it->len;
18506 int saved_char_to_display = it->char_to_display;
18507 int saved_x = it->current_x;
18508 int saved_face_id = it->face_id;
18509 int saved_box_end = it->end_of_box_run_p;
18510 struct text_pos saved_pos;
18511 Lisp_Object saved_object;
18512 struct face *face;
18513
18514 saved_object = it->object;
18515 saved_pos = it->position;
18516
18517 it->what = IT_CHARACTER;
18518 memset (&it->position, 0, sizeof it->position);
18519 it->object = make_number (0);
18520 it->c = it->char_to_display = ' ';
18521 it->len = 1;
18522
18523 /* If the default face was remapped, be sure to use the
18524 remapped face for the appended newline. */
18525 if (default_face_p)
18526 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18527 else if (it->face_before_selective_p)
18528 it->face_id = it->saved_face_id;
18529 face = FACE_FROM_ID (it->f, it->face_id);
18530 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18531 /* In R2L rows, we will prepend a stretch glyph that will
18532 have the end_of_box_run_p flag set for it, so there's no
18533 need for the appended newline glyph to have that flag
18534 set. */
18535 if (it->glyph_row->reversed_p
18536 /* But if the appended newline glyph goes all the way to
18537 the end of the row, there will be no stretch glyph,
18538 so leave the box flag set. */
18539 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18540 it->end_of_box_run_p = 0;
18541
18542 PRODUCE_GLYPHS (it);
18543
18544 it->override_ascent = -1;
18545 it->constrain_row_ascent_descent_p = 0;
18546 it->current_x = saved_x;
18547 it->object = saved_object;
18548 it->position = saved_pos;
18549 it->what = saved_what;
18550 it->face_id = saved_face_id;
18551 it->len = saved_len;
18552 it->c = saved_c;
18553 it->char_to_display = saved_char_to_display;
18554 it->end_of_box_run_p = saved_box_end;
18555 return 1;
18556 }
18557 }
18558
18559 return 0;
18560 }
18561
18562
18563 /* Extend the face of the last glyph in the text area of IT->glyph_row
18564 to the end of the display line. Called from display_line. If the
18565 glyph row is empty, add a space glyph to it so that we know the
18566 face to draw. Set the glyph row flag fill_line_p. If the glyph
18567 row is R2L, prepend a stretch glyph to cover the empty space to the
18568 left of the leftmost glyph. */
18569
18570 static void
18571 extend_face_to_end_of_line (struct it *it)
18572 {
18573 struct face *face, *default_face;
18574 struct frame *f = it->f;
18575
18576 /* If line is already filled, do nothing. Non window-system frames
18577 get a grace of one more ``pixel'' because their characters are
18578 1-``pixel'' wide, so they hit the equality too early. This grace
18579 is needed only for R2L rows that are not continued, to produce
18580 one extra blank where we could display the cursor. */
18581 if (it->current_x >= it->last_visible_x
18582 + (!FRAME_WINDOW_P (f)
18583 && it->glyph_row->reversed_p
18584 && !it->glyph_row->continued_p))
18585 return;
18586
18587 /* The default face, possibly remapped. */
18588 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18589
18590 /* Face extension extends the background and box of IT->face_id
18591 to the end of the line. If the background equals the background
18592 of the frame, we don't have to do anything. */
18593 if (it->face_before_selective_p)
18594 face = FACE_FROM_ID (f, it->saved_face_id);
18595 else
18596 face = FACE_FROM_ID (f, it->face_id);
18597
18598 if (FRAME_WINDOW_P (f)
18599 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
18600 && face->box == FACE_NO_BOX
18601 && face->background == FRAME_BACKGROUND_PIXEL (f)
18602 && !face->stipple
18603 && !it->glyph_row->reversed_p)
18604 return;
18605
18606 /* Set the glyph row flag indicating that the face of the last glyph
18607 in the text area has to be drawn to the end of the text area. */
18608 it->glyph_row->fill_line_p = 1;
18609
18610 /* If current character of IT is not ASCII, make sure we have the
18611 ASCII face. This will be automatically undone the next time
18612 get_next_display_element returns a multibyte character. Note
18613 that the character will always be single byte in unibyte
18614 text. */
18615 if (!ASCII_CHAR_P (it->c))
18616 {
18617 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18618 }
18619
18620 if (FRAME_WINDOW_P (f))
18621 {
18622 /* If the row is empty, add a space with the current face of IT,
18623 so that we know which face to draw. */
18624 if (it->glyph_row->used[TEXT_AREA] == 0)
18625 {
18626 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18627 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18628 it->glyph_row->used[TEXT_AREA] = 1;
18629 }
18630 #ifdef HAVE_WINDOW_SYSTEM
18631 if (it->glyph_row->reversed_p)
18632 {
18633 /* Prepend a stretch glyph to the row, such that the
18634 rightmost glyph will be drawn flushed all the way to the
18635 right margin of the window. The stretch glyph that will
18636 occupy the empty space, if any, to the left of the
18637 glyphs. */
18638 struct font *font = face->font ? face->font : FRAME_FONT (f);
18639 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18640 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18641 struct glyph *g;
18642 int row_width, stretch_ascent, stretch_width;
18643 struct text_pos saved_pos;
18644 int saved_face_id, saved_avoid_cursor, saved_box_start;
18645
18646 for (row_width = 0, g = row_start; g < row_end; g++)
18647 row_width += g->pixel_width;
18648 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18649 if (stretch_width > 0)
18650 {
18651 stretch_ascent =
18652 (((it->ascent + it->descent)
18653 * FONT_BASE (font)) / FONT_HEIGHT (font));
18654 saved_pos = it->position;
18655 memset (&it->position, 0, sizeof it->position);
18656 saved_avoid_cursor = it->avoid_cursor_p;
18657 it->avoid_cursor_p = 1;
18658 saved_face_id = it->face_id;
18659 saved_box_start = it->start_of_box_run_p;
18660 /* The last row's stretch glyph should get the default
18661 face, to avoid painting the rest of the window with
18662 the region face, if the region ends at ZV. */
18663 if (it->glyph_row->ends_at_zv_p)
18664 it->face_id = default_face->id;
18665 else
18666 it->face_id = face->id;
18667 it->start_of_box_run_p = 0;
18668 append_stretch_glyph (it, make_number (0), stretch_width,
18669 it->ascent + it->descent, stretch_ascent);
18670 it->position = saved_pos;
18671 it->avoid_cursor_p = saved_avoid_cursor;
18672 it->face_id = saved_face_id;
18673 it->start_of_box_run_p = saved_box_start;
18674 }
18675 }
18676 #endif /* HAVE_WINDOW_SYSTEM */
18677 }
18678 else
18679 {
18680 /* Save some values that must not be changed. */
18681 int saved_x = it->current_x;
18682 struct text_pos saved_pos;
18683 Lisp_Object saved_object;
18684 enum display_element_type saved_what = it->what;
18685 int saved_face_id = it->face_id;
18686
18687 saved_object = it->object;
18688 saved_pos = it->position;
18689
18690 it->what = IT_CHARACTER;
18691 memset (&it->position, 0, sizeof it->position);
18692 it->object = make_number (0);
18693 it->c = it->char_to_display = ' ';
18694 it->len = 1;
18695 /* The last row's blank glyphs should get the default face, to
18696 avoid painting the rest of the window with the region face,
18697 if the region ends at ZV. */
18698 if (it->glyph_row->ends_at_zv_p)
18699 it->face_id = default_face->id;
18700 else
18701 it->face_id = face->id;
18702
18703 PRODUCE_GLYPHS (it);
18704
18705 while (it->current_x <= it->last_visible_x)
18706 PRODUCE_GLYPHS (it);
18707
18708 /* Don't count these blanks really. It would let us insert a left
18709 truncation glyph below and make us set the cursor on them, maybe. */
18710 it->current_x = saved_x;
18711 it->object = saved_object;
18712 it->position = saved_pos;
18713 it->what = saved_what;
18714 it->face_id = saved_face_id;
18715 }
18716 }
18717
18718
18719 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18720 trailing whitespace. */
18721
18722 static int
18723 trailing_whitespace_p (ptrdiff_t charpos)
18724 {
18725 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18726 int c = 0;
18727
18728 while (bytepos < ZV_BYTE
18729 && (c = FETCH_CHAR (bytepos),
18730 c == ' ' || c == '\t'))
18731 ++bytepos;
18732
18733 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18734 {
18735 if (bytepos != PT_BYTE)
18736 return 1;
18737 }
18738 return 0;
18739 }
18740
18741
18742 /* Highlight trailing whitespace, if any, in ROW. */
18743
18744 static void
18745 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18746 {
18747 int used = row->used[TEXT_AREA];
18748
18749 if (used)
18750 {
18751 struct glyph *start = row->glyphs[TEXT_AREA];
18752 struct glyph *glyph = start + used - 1;
18753
18754 if (row->reversed_p)
18755 {
18756 /* Right-to-left rows need to be processed in the opposite
18757 direction, so swap the edge pointers. */
18758 glyph = start;
18759 start = row->glyphs[TEXT_AREA] + used - 1;
18760 }
18761
18762 /* Skip over glyphs inserted to display the cursor at the
18763 end of a line, for extending the face of the last glyph
18764 to the end of the line on terminals, and for truncation
18765 and continuation glyphs. */
18766 if (!row->reversed_p)
18767 {
18768 while (glyph >= start
18769 && glyph->type == CHAR_GLYPH
18770 && INTEGERP (glyph->object))
18771 --glyph;
18772 }
18773 else
18774 {
18775 while (glyph <= start
18776 && glyph->type == CHAR_GLYPH
18777 && INTEGERP (glyph->object))
18778 ++glyph;
18779 }
18780
18781 /* If last glyph is a space or stretch, and it's trailing
18782 whitespace, set the face of all trailing whitespace glyphs in
18783 IT->glyph_row to `trailing-whitespace'. */
18784 if ((row->reversed_p ? glyph <= start : glyph >= start)
18785 && BUFFERP (glyph->object)
18786 && (glyph->type == STRETCH_GLYPH
18787 || (glyph->type == CHAR_GLYPH
18788 && glyph->u.ch == ' '))
18789 && trailing_whitespace_p (glyph->charpos))
18790 {
18791 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18792 if (face_id < 0)
18793 return;
18794
18795 if (!row->reversed_p)
18796 {
18797 while (glyph >= start
18798 && BUFFERP (glyph->object)
18799 && (glyph->type == STRETCH_GLYPH
18800 || (glyph->type == CHAR_GLYPH
18801 && glyph->u.ch == ' ')))
18802 (glyph--)->face_id = face_id;
18803 }
18804 else
18805 {
18806 while (glyph <= start
18807 && BUFFERP (glyph->object)
18808 && (glyph->type == STRETCH_GLYPH
18809 || (glyph->type == CHAR_GLYPH
18810 && glyph->u.ch == ' ')))
18811 (glyph++)->face_id = face_id;
18812 }
18813 }
18814 }
18815 }
18816
18817
18818 /* Value is non-zero if glyph row ROW should be
18819 considered to hold the buffer position CHARPOS. */
18820
18821 static int
18822 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
18823 {
18824 int result = 1;
18825
18826 if (charpos == CHARPOS (row->end.pos)
18827 || charpos == MATRIX_ROW_END_CHARPOS (row))
18828 {
18829 /* Suppose the row ends on a string.
18830 Unless the row is continued, that means it ends on a newline
18831 in the string. If it's anything other than a display string
18832 (e.g., a before-string from an overlay), we don't want the
18833 cursor there. (This heuristic seems to give the optimal
18834 behavior for the various types of multi-line strings.)
18835 One exception: if the string has `cursor' property on one of
18836 its characters, we _do_ want the cursor there. */
18837 if (CHARPOS (row->end.string_pos) >= 0)
18838 {
18839 if (row->continued_p)
18840 result = 1;
18841 else
18842 {
18843 /* Check for `display' property. */
18844 struct glyph *beg = row->glyphs[TEXT_AREA];
18845 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18846 struct glyph *glyph;
18847
18848 result = 0;
18849 for (glyph = end; glyph >= beg; --glyph)
18850 if (STRINGP (glyph->object))
18851 {
18852 Lisp_Object prop
18853 = Fget_char_property (make_number (charpos),
18854 Qdisplay, Qnil);
18855 result =
18856 (!NILP (prop)
18857 && display_prop_string_p (prop, glyph->object));
18858 /* If there's a `cursor' property on one of the
18859 string's characters, this row is a cursor row,
18860 even though this is not a display string. */
18861 if (!result)
18862 {
18863 Lisp_Object s = glyph->object;
18864
18865 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18866 {
18867 ptrdiff_t gpos = glyph->charpos;
18868
18869 if (!NILP (Fget_char_property (make_number (gpos),
18870 Qcursor, s)))
18871 {
18872 result = 1;
18873 break;
18874 }
18875 }
18876 }
18877 break;
18878 }
18879 }
18880 }
18881 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18882 {
18883 /* If the row ends in middle of a real character,
18884 and the line is continued, we want the cursor here.
18885 That's because CHARPOS (ROW->end.pos) would equal
18886 PT if PT is before the character. */
18887 if (!row->ends_in_ellipsis_p)
18888 result = row->continued_p;
18889 else
18890 /* If the row ends in an ellipsis, then
18891 CHARPOS (ROW->end.pos) will equal point after the
18892 invisible text. We want that position to be displayed
18893 after the ellipsis. */
18894 result = 0;
18895 }
18896 /* If the row ends at ZV, display the cursor at the end of that
18897 row instead of at the start of the row below. */
18898 else if (row->ends_at_zv_p)
18899 result = 1;
18900 else
18901 result = 0;
18902 }
18903
18904 return result;
18905 }
18906
18907 /* Value is non-zero if glyph row ROW should be
18908 used to hold the cursor. */
18909
18910 static int
18911 cursor_row_p (struct glyph_row *row)
18912 {
18913 return row_for_charpos_p (row, PT);
18914 }
18915
18916 \f
18917
18918 /* Push the property PROP so that it will be rendered at the current
18919 position in IT. Return 1 if PROP was successfully pushed, 0
18920 otherwise. Called from handle_line_prefix to handle the
18921 `line-prefix' and `wrap-prefix' properties. */
18922
18923 static int
18924 push_prefix_prop (struct it *it, Lisp_Object prop)
18925 {
18926 struct text_pos pos =
18927 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18928
18929 eassert (it->method == GET_FROM_BUFFER
18930 || it->method == GET_FROM_DISPLAY_VECTOR
18931 || it->method == GET_FROM_STRING);
18932
18933 /* We need to save the current buffer/string position, so it will be
18934 restored by pop_it, because iterate_out_of_display_property
18935 depends on that being set correctly, but some situations leave
18936 it->position not yet set when this function is called. */
18937 push_it (it, &pos);
18938
18939 if (STRINGP (prop))
18940 {
18941 if (SCHARS (prop) == 0)
18942 {
18943 pop_it (it);
18944 return 0;
18945 }
18946
18947 it->string = prop;
18948 it->string_from_prefix_prop_p = 1;
18949 it->multibyte_p = STRING_MULTIBYTE (it->string);
18950 it->current.overlay_string_index = -1;
18951 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18952 it->end_charpos = it->string_nchars = SCHARS (it->string);
18953 it->method = GET_FROM_STRING;
18954 it->stop_charpos = 0;
18955 it->prev_stop = 0;
18956 it->base_level_stop = 0;
18957
18958 /* Force paragraph direction to be that of the parent
18959 buffer/string. */
18960 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18961 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18962 else
18963 it->paragraph_embedding = L2R;
18964
18965 /* Set up the bidi iterator for this display string. */
18966 if (it->bidi_p)
18967 {
18968 it->bidi_it.string.lstring = it->string;
18969 it->bidi_it.string.s = NULL;
18970 it->bidi_it.string.schars = it->end_charpos;
18971 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18972 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18973 it->bidi_it.string.unibyte = !it->multibyte_p;
18974 it->bidi_it.w = it->w;
18975 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18976 }
18977 }
18978 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18979 {
18980 it->method = GET_FROM_STRETCH;
18981 it->object = prop;
18982 }
18983 #ifdef HAVE_WINDOW_SYSTEM
18984 else if (IMAGEP (prop))
18985 {
18986 it->what = IT_IMAGE;
18987 it->image_id = lookup_image (it->f, prop);
18988 it->method = GET_FROM_IMAGE;
18989 }
18990 #endif /* HAVE_WINDOW_SYSTEM */
18991 else
18992 {
18993 pop_it (it); /* bogus display property, give up */
18994 return 0;
18995 }
18996
18997 return 1;
18998 }
18999
19000 /* Return the character-property PROP at the current position in IT. */
19001
19002 static Lisp_Object
19003 get_it_property (struct it *it, Lisp_Object prop)
19004 {
19005 Lisp_Object position, object = it->object;
19006
19007 if (STRINGP (object))
19008 position = make_number (IT_STRING_CHARPOS (*it));
19009 else if (BUFFERP (object))
19010 {
19011 position = make_number (IT_CHARPOS (*it));
19012 object = it->window;
19013 }
19014 else
19015 return Qnil;
19016
19017 return Fget_char_property (position, prop, object);
19018 }
19019
19020 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19021
19022 static void
19023 handle_line_prefix (struct it *it)
19024 {
19025 Lisp_Object prefix;
19026
19027 if (it->continuation_lines_width > 0)
19028 {
19029 prefix = get_it_property (it, Qwrap_prefix);
19030 if (NILP (prefix))
19031 prefix = Vwrap_prefix;
19032 }
19033 else
19034 {
19035 prefix = get_it_property (it, Qline_prefix);
19036 if (NILP (prefix))
19037 prefix = Vline_prefix;
19038 }
19039 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19040 {
19041 /* If the prefix is wider than the window, and we try to wrap
19042 it, it would acquire its own wrap prefix, and so on till the
19043 iterator stack overflows. So, don't wrap the prefix. */
19044 it->line_wrap = TRUNCATE;
19045 it->avoid_cursor_p = 1;
19046 }
19047 }
19048
19049 \f
19050
19051 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19052 only for R2L lines from display_line and display_string, when they
19053 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19054 the line/string needs to be continued on the next glyph row. */
19055 static void
19056 unproduce_glyphs (struct it *it, int n)
19057 {
19058 struct glyph *glyph, *end;
19059
19060 eassert (it->glyph_row);
19061 eassert (it->glyph_row->reversed_p);
19062 eassert (it->area == TEXT_AREA);
19063 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19064
19065 if (n > it->glyph_row->used[TEXT_AREA])
19066 n = it->glyph_row->used[TEXT_AREA];
19067 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19068 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19069 for ( ; glyph < end; glyph++)
19070 glyph[-n] = *glyph;
19071 }
19072
19073 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19074 and ROW->maxpos. */
19075 static void
19076 find_row_edges (struct it *it, struct glyph_row *row,
19077 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19078 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19079 {
19080 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19081 lines' rows is implemented for bidi-reordered rows. */
19082
19083 /* ROW->minpos is the value of min_pos, the minimal buffer position
19084 we have in ROW, or ROW->start.pos if that is smaller. */
19085 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19086 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19087 else
19088 /* We didn't find buffer positions smaller than ROW->start, or
19089 didn't find _any_ valid buffer positions in any of the glyphs,
19090 so we must trust the iterator's computed positions. */
19091 row->minpos = row->start.pos;
19092 if (max_pos <= 0)
19093 {
19094 max_pos = CHARPOS (it->current.pos);
19095 max_bpos = BYTEPOS (it->current.pos);
19096 }
19097
19098 /* Here are the various use-cases for ending the row, and the
19099 corresponding values for ROW->maxpos:
19100
19101 Line ends in a newline from buffer eol_pos + 1
19102 Line is continued from buffer max_pos + 1
19103 Line is truncated on right it->current.pos
19104 Line ends in a newline from string max_pos + 1(*)
19105 (*) + 1 only when line ends in a forward scan
19106 Line is continued from string max_pos
19107 Line is continued from display vector max_pos
19108 Line is entirely from a string min_pos == max_pos
19109 Line is entirely from a display vector min_pos == max_pos
19110 Line that ends at ZV ZV
19111
19112 If you discover other use-cases, please add them here as
19113 appropriate. */
19114 if (row->ends_at_zv_p)
19115 row->maxpos = it->current.pos;
19116 else if (row->used[TEXT_AREA])
19117 {
19118 int seen_this_string = 0;
19119 struct glyph_row *r1 = row - 1;
19120
19121 /* Did we see the same display string on the previous row? */
19122 if (STRINGP (it->object)
19123 /* this is not the first row */
19124 && row > it->w->desired_matrix->rows
19125 /* previous row is not the header line */
19126 && !r1->mode_line_p
19127 /* previous row also ends in a newline from a string */
19128 && r1->ends_in_newline_from_string_p)
19129 {
19130 struct glyph *start, *end;
19131
19132 /* Search for the last glyph of the previous row that came
19133 from buffer or string. Depending on whether the row is
19134 L2R or R2L, we need to process it front to back or the
19135 other way round. */
19136 if (!r1->reversed_p)
19137 {
19138 start = r1->glyphs[TEXT_AREA];
19139 end = start + r1->used[TEXT_AREA];
19140 /* Glyphs inserted by redisplay have an integer (zero)
19141 as their object. */
19142 while (end > start
19143 && INTEGERP ((end - 1)->object)
19144 && (end - 1)->charpos <= 0)
19145 --end;
19146 if (end > start)
19147 {
19148 if (EQ ((end - 1)->object, it->object))
19149 seen_this_string = 1;
19150 }
19151 else
19152 /* If all the glyphs of the previous row were inserted
19153 by redisplay, it means the previous row was
19154 produced from a single newline, which is only
19155 possible if that newline came from the same string
19156 as the one which produced this ROW. */
19157 seen_this_string = 1;
19158 }
19159 else
19160 {
19161 end = r1->glyphs[TEXT_AREA] - 1;
19162 start = end + r1->used[TEXT_AREA];
19163 while (end < start
19164 && INTEGERP ((end + 1)->object)
19165 && (end + 1)->charpos <= 0)
19166 ++end;
19167 if (end < start)
19168 {
19169 if (EQ ((end + 1)->object, it->object))
19170 seen_this_string = 1;
19171 }
19172 else
19173 seen_this_string = 1;
19174 }
19175 }
19176 /* Take note of each display string that covers a newline only
19177 once, the first time we see it. This is for when a display
19178 string includes more than one newline in it. */
19179 if (row->ends_in_newline_from_string_p && !seen_this_string)
19180 {
19181 /* If we were scanning the buffer forward when we displayed
19182 the string, we want to account for at least one buffer
19183 position that belongs to this row (position covered by
19184 the display string), so that cursor positioning will
19185 consider this row as a candidate when point is at the end
19186 of the visual line represented by this row. This is not
19187 required when scanning back, because max_pos will already
19188 have a much larger value. */
19189 if (CHARPOS (row->end.pos) > max_pos)
19190 INC_BOTH (max_pos, max_bpos);
19191 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19192 }
19193 else if (CHARPOS (it->eol_pos) > 0)
19194 SET_TEXT_POS (row->maxpos,
19195 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19196 else if (row->continued_p)
19197 {
19198 /* If max_pos is different from IT's current position, it
19199 means IT->method does not belong to the display element
19200 at max_pos. However, it also means that the display
19201 element at max_pos was displayed in its entirety on this
19202 line, which is equivalent to saying that the next line
19203 starts at the next buffer position. */
19204 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19205 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19206 else
19207 {
19208 INC_BOTH (max_pos, max_bpos);
19209 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19210 }
19211 }
19212 else if (row->truncated_on_right_p)
19213 /* display_line already called reseat_at_next_visible_line_start,
19214 which puts the iterator at the beginning of the next line, in
19215 the logical order. */
19216 row->maxpos = it->current.pos;
19217 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19218 /* A line that is entirely from a string/image/stretch... */
19219 row->maxpos = row->minpos;
19220 else
19221 emacs_abort ();
19222 }
19223 else
19224 row->maxpos = it->current.pos;
19225 }
19226
19227 /* Construct the glyph row IT->glyph_row in the desired matrix of
19228 IT->w from text at the current position of IT. See dispextern.h
19229 for an overview of struct it. Value is non-zero if
19230 IT->glyph_row displays text, as opposed to a line displaying ZV
19231 only. */
19232
19233 static int
19234 display_line (struct it *it)
19235 {
19236 struct glyph_row *row = it->glyph_row;
19237 Lisp_Object overlay_arrow_string;
19238 struct it wrap_it;
19239 void *wrap_data = NULL;
19240 int may_wrap = 0, wrap_x IF_LINT (= 0);
19241 int wrap_row_used = -1;
19242 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19243 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19244 int wrap_row_extra_line_spacing IF_LINT (= 0);
19245 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19246 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19247 int cvpos;
19248 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19249 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19250
19251 /* We always start displaying at hpos zero even if hscrolled. */
19252 eassert (it->hpos == 0 && it->current_x == 0);
19253
19254 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19255 >= it->w->desired_matrix->nrows)
19256 {
19257 it->w->nrows_scale_factor++;
19258 fonts_changed_p = 1;
19259 return 0;
19260 }
19261
19262 /* Is IT->w showing the region? */
19263 it->w->region_showing = it->region_beg_charpos > 0 ? it->region_beg_charpos : 0;
19264
19265 /* Clear the result glyph row and enable it. */
19266 prepare_desired_row (row);
19267
19268 row->y = it->current_y;
19269 row->start = it->start;
19270 row->continuation_lines_width = it->continuation_lines_width;
19271 row->displays_text_p = 1;
19272 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19273 it->starts_in_middle_of_char_p = 0;
19274
19275 /* Arrange the overlays nicely for our purposes. Usually, we call
19276 display_line on only one line at a time, in which case this
19277 can't really hurt too much, or we call it on lines which appear
19278 one after another in the buffer, in which case all calls to
19279 recenter_overlay_lists but the first will be pretty cheap. */
19280 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19281
19282 /* Move over display elements that are not visible because we are
19283 hscrolled. This may stop at an x-position < IT->first_visible_x
19284 if the first glyph is partially visible or if we hit a line end. */
19285 if (it->current_x < it->first_visible_x)
19286 {
19287 enum move_it_result move_result;
19288
19289 this_line_min_pos = row->start.pos;
19290 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19291 MOVE_TO_POS | MOVE_TO_X);
19292 /* If we are under a large hscroll, move_it_in_display_line_to
19293 could hit the end of the line without reaching
19294 it->first_visible_x. Pretend that we did reach it. This is
19295 especially important on a TTY, where we will call
19296 extend_face_to_end_of_line, which needs to know how many
19297 blank glyphs to produce. */
19298 if (it->current_x < it->first_visible_x
19299 && (move_result == MOVE_NEWLINE_OR_CR
19300 || move_result == MOVE_POS_MATCH_OR_ZV))
19301 it->current_x = it->first_visible_x;
19302
19303 /* Record the smallest positions seen while we moved over
19304 display elements that are not visible. This is needed by
19305 redisplay_internal for optimizing the case where the cursor
19306 stays inside the same line. The rest of this function only
19307 considers positions that are actually displayed, so
19308 RECORD_MAX_MIN_POS will not otherwise record positions that
19309 are hscrolled to the left of the left edge of the window. */
19310 min_pos = CHARPOS (this_line_min_pos);
19311 min_bpos = BYTEPOS (this_line_min_pos);
19312 }
19313 else
19314 {
19315 /* We only do this when not calling `move_it_in_display_line_to'
19316 above, because move_it_in_display_line_to calls
19317 handle_line_prefix itself. */
19318 handle_line_prefix (it);
19319 }
19320
19321 /* Get the initial row height. This is either the height of the
19322 text hscrolled, if there is any, or zero. */
19323 row->ascent = it->max_ascent;
19324 row->height = it->max_ascent + it->max_descent;
19325 row->phys_ascent = it->max_phys_ascent;
19326 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19327 row->extra_line_spacing = it->max_extra_line_spacing;
19328
19329 /* Utility macro to record max and min buffer positions seen until now. */
19330 #define RECORD_MAX_MIN_POS(IT) \
19331 do \
19332 { \
19333 int composition_p = !STRINGP ((IT)->string) \
19334 && ((IT)->what == IT_COMPOSITION); \
19335 ptrdiff_t current_pos = \
19336 composition_p ? (IT)->cmp_it.charpos \
19337 : IT_CHARPOS (*(IT)); \
19338 ptrdiff_t current_bpos = \
19339 composition_p ? CHAR_TO_BYTE (current_pos) \
19340 : IT_BYTEPOS (*(IT)); \
19341 if (current_pos < min_pos) \
19342 { \
19343 min_pos = current_pos; \
19344 min_bpos = current_bpos; \
19345 } \
19346 if (IT_CHARPOS (*it) > max_pos) \
19347 { \
19348 max_pos = IT_CHARPOS (*it); \
19349 max_bpos = IT_BYTEPOS (*it); \
19350 } \
19351 } \
19352 while (0)
19353
19354 /* Loop generating characters. The loop is left with IT on the next
19355 character to display. */
19356 while (1)
19357 {
19358 int n_glyphs_before, hpos_before, x_before;
19359 int x, nglyphs;
19360 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19361
19362 /* Retrieve the next thing to display. Value is zero if end of
19363 buffer reached. */
19364 if (!get_next_display_element (it))
19365 {
19366 /* Maybe add a space at the end of this line that is used to
19367 display the cursor there under X. Set the charpos of the
19368 first glyph of blank lines not corresponding to any text
19369 to -1. */
19370 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19371 row->exact_window_width_line_p = 1;
19372 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19373 || row->used[TEXT_AREA] == 0)
19374 {
19375 row->glyphs[TEXT_AREA]->charpos = -1;
19376 row->displays_text_p = 0;
19377
19378 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
19379 && (!MINI_WINDOW_P (it->w)
19380 || (minibuf_level && EQ (it->window, minibuf_window))))
19381 row->indicate_empty_line_p = 1;
19382 }
19383
19384 it->continuation_lines_width = 0;
19385 row->ends_at_zv_p = 1;
19386 /* A row that displays right-to-left text must always have
19387 its last face extended all the way to the end of line,
19388 even if this row ends in ZV, because we still write to
19389 the screen left to right. We also need to extend the
19390 last face if the default face is remapped to some
19391 different face, otherwise the functions that clear
19392 portions of the screen will clear with the default face's
19393 background color. */
19394 if (row->reversed_p
19395 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19396 extend_face_to_end_of_line (it);
19397 break;
19398 }
19399
19400 /* Now, get the metrics of what we want to display. This also
19401 generates glyphs in `row' (which is IT->glyph_row). */
19402 n_glyphs_before = row->used[TEXT_AREA];
19403 x = it->current_x;
19404
19405 /* Remember the line height so far in case the next element doesn't
19406 fit on the line. */
19407 if (it->line_wrap != TRUNCATE)
19408 {
19409 ascent = it->max_ascent;
19410 descent = it->max_descent;
19411 phys_ascent = it->max_phys_ascent;
19412 phys_descent = it->max_phys_descent;
19413
19414 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19415 {
19416 if (IT_DISPLAYING_WHITESPACE (it))
19417 may_wrap = 1;
19418 else if (may_wrap)
19419 {
19420 SAVE_IT (wrap_it, *it, wrap_data);
19421 wrap_x = x;
19422 wrap_row_used = row->used[TEXT_AREA];
19423 wrap_row_ascent = row->ascent;
19424 wrap_row_height = row->height;
19425 wrap_row_phys_ascent = row->phys_ascent;
19426 wrap_row_phys_height = row->phys_height;
19427 wrap_row_extra_line_spacing = row->extra_line_spacing;
19428 wrap_row_min_pos = min_pos;
19429 wrap_row_min_bpos = min_bpos;
19430 wrap_row_max_pos = max_pos;
19431 wrap_row_max_bpos = max_bpos;
19432 may_wrap = 0;
19433 }
19434 }
19435 }
19436
19437 PRODUCE_GLYPHS (it);
19438
19439 /* If this display element was in marginal areas, continue with
19440 the next one. */
19441 if (it->area != TEXT_AREA)
19442 {
19443 row->ascent = max (row->ascent, it->max_ascent);
19444 row->height = max (row->height, it->max_ascent + it->max_descent);
19445 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19446 row->phys_height = max (row->phys_height,
19447 it->max_phys_ascent + it->max_phys_descent);
19448 row->extra_line_spacing = max (row->extra_line_spacing,
19449 it->max_extra_line_spacing);
19450 set_iterator_to_next (it, 1);
19451 continue;
19452 }
19453
19454 /* Does the display element fit on the line? If we truncate
19455 lines, we should draw past the right edge of the window. If
19456 we don't truncate, we want to stop so that we can display the
19457 continuation glyph before the right margin. If lines are
19458 continued, there are two possible strategies for characters
19459 resulting in more than 1 glyph (e.g. tabs): Display as many
19460 glyphs as possible in this line and leave the rest for the
19461 continuation line, or display the whole element in the next
19462 line. Original redisplay did the former, so we do it also. */
19463 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19464 hpos_before = it->hpos;
19465 x_before = x;
19466
19467 if (/* Not a newline. */
19468 nglyphs > 0
19469 /* Glyphs produced fit entirely in the line. */
19470 && it->current_x < it->last_visible_x)
19471 {
19472 it->hpos += nglyphs;
19473 row->ascent = max (row->ascent, it->max_ascent);
19474 row->height = max (row->height, it->max_ascent + it->max_descent);
19475 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19476 row->phys_height = max (row->phys_height,
19477 it->max_phys_ascent + it->max_phys_descent);
19478 row->extra_line_spacing = max (row->extra_line_spacing,
19479 it->max_extra_line_spacing);
19480 if (it->current_x - it->pixel_width < it->first_visible_x)
19481 row->x = x - it->first_visible_x;
19482 /* Record the maximum and minimum buffer positions seen so
19483 far in glyphs that will be displayed by this row. */
19484 if (it->bidi_p)
19485 RECORD_MAX_MIN_POS (it);
19486 }
19487 else
19488 {
19489 int i, new_x;
19490 struct glyph *glyph;
19491
19492 for (i = 0; i < nglyphs; ++i, x = new_x)
19493 {
19494 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19495 new_x = x + glyph->pixel_width;
19496
19497 if (/* Lines are continued. */
19498 it->line_wrap != TRUNCATE
19499 && (/* Glyph doesn't fit on the line. */
19500 new_x > it->last_visible_x
19501 /* Or it fits exactly on a window system frame. */
19502 || (new_x == it->last_visible_x
19503 && FRAME_WINDOW_P (it->f)
19504 && (row->reversed_p
19505 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19506 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19507 {
19508 /* End of a continued line. */
19509
19510 if (it->hpos == 0
19511 || (new_x == it->last_visible_x
19512 && FRAME_WINDOW_P (it->f)
19513 && (row->reversed_p
19514 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19515 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19516 {
19517 /* Current glyph is the only one on the line or
19518 fits exactly on the line. We must continue
19519 the line because we can't draw the cursor
19520 after the glyph. */
19521 row->continued_p = 1;
19522 it->current_x = new_x;
19523 it->continuation_lines_width += new_x;
19524 ++it->hpos;
19525 if (i == nglyphs - 1)
19526 {
19527 /* If line-wrap is on, check if a previous
19528 wrap point was found. */
19529 if (wrap_row_used > 0
19530 /* Even if there is a previous wrap
19531 point, continue the line here as
19532 usual, if (i) the previous character
19533 was a space or tab AND (ii) the
19534 current character is not. */
19535 && (!may_wrap
19536 || IT_DISPLAYING_WHITESPACE (it)))
19537 goto back_to_wrap;
19538
19539 /* Record the maximum and minimum buffer
19540 positions seen so far in glyphs that will be
19541 displayed by this row. */
19542 if (it->bidi_p)
19543 RECORD_MAX_MIN_POS (it);
19544 set_iterator_to_next (it, 1);
19545 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19546 {
19547 if (!get_next_display_element (it))
19548 {
19549 row->exact_window_width_line_p = 1;
19550 it->continuation_lines_width = 0;
19551 row->continued_p = 0;
19552 row->ends_at_zv_p = 1;
19553 }
19554 else if (ITERATOR_AT_END_OF_LINE_P (it))
19555 {
19556 row->continued_p = 0;
19557 row->exact_window_width_line_p = 1;
19558 }
19559 }
19560 }
19561 else if (it->bidi_p)
19562 RECORD_MAX_MIN_POS (it);
19563 }
19564 else if (CHAR_GLYPH_PADDING_P (*glyph)
19565 && !FRAME_WINDOW_P (it->f))
19566 {
19567 /* A padding glyph that doesn't fit on this line.
19568 This means the whole character doesn't fit
19569 on the line. */
19570 if (row->reversed_p)
19571 unproduce_glyphs (it, row->used[TEXT_AREA]
19572 - n_glyphs_before);
19573 row->used[TEXT_AREA] = n_glyphs_before;
19574
19575 /* Fill the rest of the row with continuation
19576 glyphs like in 20.x. */
19577 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19578 < row->glyphs[1 + TEXT_AREA])
19579 produce_special_glyphs (it, IT_CONTINUATION);
19580
19581 row->continued_p = 1;
19582 it->current_x = x_before;
19583 it->continuation_lines_width += x_before;
19584
19585 /* Restore the height to what it was before the
19586 element not fitting on the line. */
19587 it->max_ascent = ascent;
19588 it->max_descent = descent;
19589 it->max_phys_ascent = phys_ascent;
19590 it->max_phys_descent = phys_descent;
19591 }
19592 else if (wrap_row_used > 0)
19593 {
19594 back_to_wrap:
19595 if (row->reversed_p)
19596 unproduce_glyphs (it,
19597 row->used[TEXT_AREA] - wrap_row_used);
19598 RESTORE_IT (it, &wrap_it, wrap_data);
19599 it->continuation_lines_width += wrap_x;
19600 row->used[TEXT_AREA] = wrap_row_used;
19601 row->ascent = wrap_row_ascent;
19602 row->height = wrap_row_height;
19603 row->phys_ascent = wrap_row_phys_ascent;
19604 row->phys_height = wrap_row_phys_height;
19605 row->extra_line_spacing = wrap_row_extra_line_spacing;
19606 min_pos = wrap_row_min_pos;
19607 min_bpos = wrap_row_min_bpos;
19608 max_pos = wrap_row_max_pos;
19609 max_bpos = wrap_row_max_bpos;
19610 row->continued_p = 1;
19611 row->ends_at_zv_p = 0;
19612 row->exact_window_width_line_p = 0;
19613 it->continuation_lines_width += x;
19614
19615 /* Make sure that a non-default face is extended
19616 up to the right margin of the window. */
19617 extend_face_to_end_of_line (it);
19618 }
19619 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19620 {
19621 /* A TAB that extends past the right edge of the
19622 window. This produces a single glyph on
19623 window system frames. We leave the glyph in
19624 this row and let it fill the row, but don't
19625 consume the TAB. */
19626 if ((row->reversed_p
19627 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19628 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19629 produce_special_glyphs (it, IT_CONTINUATION);
19630 it->continuation_lines_width += it->last_visible_x;
19631 row->ends_in_middle_of_char_p = 1;
19632 row->continued_p = 1;
19633 glyph->pixel_width = it->last_visible_x - x;
19634 it->starts_in_middle_of_char_p = 1;
19635 }
19636 else
19637 {
19638 /* Something other than a TAB that draws past
19639 the right edge of the window. Restore
19640 positions to values before the element. */
19641 if (row->reversed_p)
19642 unproduce_glyphs (it, row->used[TEXT_AREA]
19643 - (n_glyphs_before + i));
19644 row->used[TEXT_AREA] = n_glyphs_before + i;
19645
19646 /* Display continuation glyphs. */
19647 it->current_x = x_before;
19648 it->continuation_lines_width += x;
19649 if (!FRAME_WINDOW_P (it->f)
19650 || (row->reversed_p
19651 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19652 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19653 produce_special_glyphs (it, IT_CONTINUATION);
19654 row->continued_p = 1;
19655
19656 extend_face_to_end_of_line (it);
19657
19658 if (nglyphs > 1 && i > 0)
19659 {
19660 row->ends_in_middle_of_char_p = 1;
19661 it->starts_in_middle_of_char_p = 1;
19662 }
19663
19664 /* Restore the height to what it was before the
19665 element not fitting on the line. */
19666 it->max_ascent = ascent;
19667 it->max_descent = descent;
19668 it->max_phys_ascent = phys_ascent;
19669 it->max_phys_descent = phys_descent;
19670 }
19671
19672 break;
19673 }
19674 else if (new_x > it->first_visible_x)
19675 {
19676 /* Increment number of glyphs actually displayed. */
19677 ++it->hpos;
19678
19679 /* Record the maximum and minimum buffer positions
19680 seen so far in glyphs that will be displayed by
19681 this row. */
19682 if (it->bidi_p)
19683 RECORD_MAX_MIN_POS (it);
19684
19685 if (x < it->first_visible_x)
19686 /* Glyph is partially visible, i.e. row starts at
19687 negative X position. */
19688 row->x = x - it->first_visible_x;
19689 }
19690 else
19691 {
19692 /* Glyph is completely off the left margin of the
19693 window. This should not happen because of the
19694 move_it_in_display_line at the start of this
19695 function, unless the text display area of the
19696 window is empty. */
19697 eassert (it->first_visible_x <= it->last_visible_x);
19698 }
19699 }
19700 /* Even if this display element produced no glyphs at all,
19701 we want to record its position. */
19702 if (it->bidi_p && nglyphs == 0)
19703 RECORD_MAX_MIN_POS (it);
19704
19705 row->ascent = max (row->ascent, it->max_ascent);
19706 row->height = max (row->height, it->max_ascent + it->max_descent);
19707 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19708 row->phys_height = max (row->phys_height,
19709 it->max_phys_ascent + it->max_phys_descent);
19710 row->extra_line_spacing = max (row->extra_line_spacing,
19711 it->max_extra_line_spacing);
19712
19713 /* End of this display line if row is continued. */
19714 if (row->continued_p || row->ends_at_zv_p)
19715 break;
19716 }
19717
19718 at_end_of_line:
19719 /* Is this a line end? If yes, we're also done, after making
19720 sure that a non-default face is extended up to the right
19721 margin of the window. */
19722 if (ITERATOR_AT_END_OF_LINE_P (it))
19723 {
19724 int used_before = row->used[TEXT_AREA];
19725
19726 row->ends_in_newline_from_string_p = STRINGP (it->object);
19727
19728 /* Add a space at the end of the line that is used to
19729 display the cursor there. */
19730 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19731 append_space_for_newline (it, 0);
19732
19733 /* Extend the face to the end of the line. */
19734 extend_face_to_end_of_line (it);
19735
19736 /* Make sure we have the position. */
19737 if (used_before == 0)
19738 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19739
19740 /* Record the position of the newline, for use in
19741 find_row_edges. */
19742 it->eol_pos = it->current.pos;
19743
19744 /* Consume the line end. This skips over invisible lines. */
19745 set_iterator_to_next (it, 1);
19746 it->continuation_lines_width = 0;
19747 break;
19748 }
19749
19750 /* Proceed with next display element. Note that this skips
19751 over lines invisible because of selective display. */
19752 set_iterator_to_next (it, 1);
19753
19754 /* If we truncate lines, we are done when the last displayed
19755 glyphs reach past the right margin of the window. */
19756 if (it->line_wrap == TRUNCATE
19757 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19758 ? (it->current_x >= it->last_visible_x)
19759 : (it->current_x > it->last_visible_x)))
19760 {
19761 /* Maybe add truncation glyphs. */
19762 if (!FRAME_WINDOW_P (it->f)
19763 || (row->reversed_p
19764 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19765 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19766 {
19767 int i, n;
19768
19769 if (!row->reversed_p)
19770 {
19771 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19772 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19773 break;
19774 }
19775 else
19776 {
19777 for (i = 0; i < row->used[TEXT_AREA]; i++)
19778 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19779 break;
19780 /* Remove any padding glyphs at the front of ROW, to
19781 make room for the truncation glyphs we will be
19782 adding below. The loop below always inserts at
19783 least one truncation glyph, so also remove the
19784 last glyph added to ROW. */
19785 unproduce_glyphs (it, i + 1);
19786 /* Adjust i for the loop below. */
19787 i = row->used[TEXT_AREA] - (i + 1);
19788 }
19789
19790 it->current_x = x_before;
19791 if (!FRAME_WINDOW_P (it->f))
19792 {
19793 for (n = row->used[TEXT_AREA]; i < n; ++i)
19794 {
19795 row->used[TEXT_AREA] = i;
19796 produce_special_glyphs (it, IT_TRUNCATION);
19797 }
19798 }
19799 else
19800 {
19801 row->used[TEXT_AREA] = i;
19802 produce_special_glyphs (it, IT_TRUNCATION);
19803 }
19804 }
19805 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19806 {
19807 /* Don't truncate if we can overflow newline into fringe. */
19808 if (!get_next_display_element (it))
19809 {
19810 it->continuation_lines_width = 0;
19811 row->ends_at_zv_p = 1;
19812 row->exact_window_width_line_p = 1;
19813 break;
19814 }
19815 if (ITERATOR_AT_END_OF_LINE_P (it))
19816 {
19817 row->exact_window_width_line_p = 1;
19818 goto at_end_of_line;
19819 }
19820 it->current_x = x_before;
19821 }
19822
19823 row->truncated_on_right_p = 1;
19824 it->continuation_lines_width = 0;
19825 reseat_at_next_visible_line_start (it, 0);
19826 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19827 it->hpos = hpos_before;
19828 break;
19829 }
19830 }
19831
19832 if (wrap_data)
19833 bidi_unshelve_cache (wrap_data, 1);
19834
19835 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19836 at the left window margin. */
19837 if (it->first_visible_x
19838 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19839 {
19840 if (!FRAME_WINDOW_P (it->f)
19841 || (row->reversed_p
19842 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19843 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19844 insert_left_trunc_glyphs (it);
19845 row->truncated_on_left_p = 1;
19846 }
19847
19848 /* Remember the position at which this line ends.
19849
19850 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19851 cannot be before the call to find_row_edges below, since that is
19852 where these positions are determined. */
19853 row->end = it->current;
19854 if (!it->bidi_p)
19855 {
19856 row->minpos = row->start.pos;
19857 row->maxpos = row->end.pos;
19858 }
19859 else
19860 {
19861 /* ROW->minpos and ROW->maxpos must be the smallest and
19862 `1 + the largest' buffer positions in ROW. But if ROW was
19863 bidi-reordered, these two positions can be anywhere in the
19864 row, so we must determine them now. */
19865 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19866 }
19867
19868 /* If the start of this line is the overlay arrow-position, then
19869 mark this glyph row as the one containing the overlay arrow.
19870 This is clearly a mess with variable size fonts. It would be
19871 better to let it be displayed like cursors under X. */
19872 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
19873 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19874 !NILP (overlay_arrow_string)))
19875 {
19876 /* Overlay arrow in window redisplay is a fringe bitmap. */
19877 if (STRINGP (overlay_arrow_string))
19878 {
19879 struct glyph_row *arrow_row
19880 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19881 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19882 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19883 struct glyph *p = row->glyphs[TEXT_AREA];
19884 struct glyph *p2, *end;
19885
19886 /* Copy the arrow glyphs. */
19887 while (glyph < arrow_end)
19888 *p++ = *glyph++;
19889
19890 /* Throw away padding glyphs. */
19891 p2 = p;
19892 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19893 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19894 ++p2;
19895 if (p2 > p)
19896 {
19897 while (p2 < end)
19898 *p++ = *p2++;
19899 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19900 }
19901 }
19902 else
19903 {
19904 eassert (INTEGERP (overlay_arrow_string));
19905 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19906 }
19907 overlay_arrow_seen = 1;
19908 }
19909
19910 /* Highlight trailing whitespace. */
19911 if (!NILP (Vshow_trailing_whitespace))
19912 highlight_trailing_whitespace (it->f, it->glyph_row);
19913
19914 /* Compute pixel dimensions of this line. */
19915 compute_line_metrics (it);
19916
19917 /* Implementation note: No changes in the glyphs of ROW or in their
19918 faces can be done past this point, because compute_line_metrics
19919 computes ROW's hash value and stores it within the glyph_row
19920 structure. */
19921
19922 /* Record whether this row ends inside an ellipsis. */
19923 row->ends_in_ellipsis_p
19924 = (it->method == GET_FROM_DISPLAY_VECTOR
19925 && it->ellipsis_p);
19926
19927 /* Save fringe bitmaps in this row. */
19928 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19929 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19930 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19931 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19932
19933 it->left_user_fringe_bitmap = 0;
19934 it->left_user_fringe_face_id = 0;
19935 it->right_user_fringe_bitmap = 0;
19936 it->right_user_fringe_face_id = 0;
19937
19938 /* Maybe set the cursor. */
19939 cvpos = it->w->cursor.vpos;
19940 if ((cvpos < 0
19941 /* In bidi-reordered rows, keep checking for proper cursor
19942 position even if one has been found already, because buffer
19943 positions in such rows change non-linearly with ROW->VPOS,
19944 when a line is continued. One exception: when we are at ZV,
19945 display cursor on the first suitable glyph row, since all
19946 the empty rows after that also have their position set to ZV. */
19947 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19948 lines' rows is implemented for bidi-reordered rows. */
19949 || (it->bidi_p
19950 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19951 && PT >= MATRIX_ROW_START_CHARPOS (row)
19952 && PT <= MATRIX_ROW_END_CHARPOS (row)
19953 && cursor_row_p (row))
19954 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19955
19956 /* Prepare for the next line. This line starts horizontally at (X
19957 HPOS) = (0 0). Vertical positions are incremented. As a
19958 convenience for the caller, IT->glyph_row is set to the next
19959 row to be used. */
19960 it->current_x = it->hpos = 0;
19961 it->current_y += row->height;
19962 SET_TEXT_POS (it->eol_pos, 0, 0);
19963 ++it->vpos;
19964 ++it->glyph_row;
19965 /* The next row should by default use the same value of the
19966 reversed_p flag as this one. set_iterator_to_next decides when
19967 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19968 the flag accordingly. */
19969 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19970 it->glyph_row->reversed_p = row->reversed_p;
19971 it->start = row->end;
19972 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
19973
19974 #undef RECORD_MAX_MIN_POS
19975 }
19976
19977 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19978 Scurrent_bidi_paragraph_direction, 0, 1, 0,
19979 doc: /* Return paragraph direction at point in BUFFER.
19980 Value is either `left-to-right' or `right-to-left'.
19981 If BUFFER is omitted or nil, it defaults to the current buffer.
19982
19983 Paragraph direction determines how the text in the paragraph is displayed.
19984 In left-to-right paragraphs, text begins at the left margin of the window
19985 and the reading direction is generally left to right. In right-to-left
19986 paragraphs, text begins at the right margin and is read from right to left.
19987
19988 See also `bidi-paragraph-direction'. */)
19989 (Lisp_Object buffer)
19990 {
19991 struct buffer *buf = current_buffer;
19992 struct buffer *old = buf;
19993
19994 if (! NILP (buffer))
19995 {
19996 CHECK_BUFFER (buffer);
19997 buf = XBUFFER (buffer);
19998 }
19999
20000 if (NILP (BVAR (buf, bidi_display_reordering))
20001 || NILP (BVAR (buf, enable_multibyte_characters))
20002 /* When we are loading loadup.el, the character property tables
20003 needed for bidi iteration are not yet available. */
20004 || !NILP (Vpurify_flag))
20005 return Qleft_to_right;
20006 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20007 return BVAR (buf, bidi_paragraph_direction);
20008 else
20009 {
20010 /* Determine the direction from buffer text. We could try to
20011 use current_matrix if it is up to date, but this seems fast
20012 enough as it is. */
20013 struct bidi_it itb;
20014 ptrdiff_t pos = BUF_PT (buf);
20015 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20016 int c;
20017 void *itb_data = bidi_shelve_cache ();
20018
20019 set_buffer_temp (buf);
20020 /* bidi_paragraph_init finds the base direction of the paragraph
20021 by searching forward from paragraph start. We need the base
20022 direction of the current or _previous_ paragraph, so we need
20023 to make sure we are within that paragraph. To that end, find
20024 the previous non-empty line. */
20025 if (pos >= ZV && pos > BEGV)
20026 DEC_BOTH (pos, bytepos);
20027 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20028 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20029 {
20030 while ((c = FETCH_BYTE (bytepos)) == '\n'
20031 || c == ' ' || c == '\t' || c == '\f')
20032 {
20033 if (bytepos <= BEGV_BYTE)
20034 break;
20035 bytepos--;
20036 pos--;
20037 }
20038 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20039 bytepos--;
20040 }
20041 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20042 itb.paragraph_dir = NEUTRAL_DIR;
20043 itb.string.s = NULL;
20044 itb.string.lstring = Qnil;
20045 itb.string.bufpos = 0;
20046 itb.string.unibyte = 0;
20047 /* We have no window to use here for ignoring window-specific
20048 overlays. Using NULL for window pointer will cause
20049 compute_display_string_pos to use the current buffer. */
20050 itb.w = NULL;
20051 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20052 bidi_unshelve_cache (itb_data, 0);
20053 set_buffer_temp (old);
20054 switch (itb.paragraph_dir)
20055 {
20056 case L2R:
20057 return Qleft_to_right;
20058 break;
20059 case R2L:
20060 return Qright_to_left;
20061 break;
20062 default:
20063 emacs_abort ();
20064 }
20065 }
20066 }
20067
20068 DEFUN ("move-point-visually", Fmove_point_visually,
20069 Smove_point_visually, 1, 1, 0,
20070 doc: /* Move point in the visual order in the specified DIRECTION.
20071 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
20072 left.
20073
20074 Value is the new character position of point. */)
20075 (Lisp_Object direction)
20076 {
20077 struct window *w = XWINDOW (selected_window);
20078 struct buffer *b = XBUFFER (w->contents);
20079 struct glyph_row *row;
20080 int dir;
20081 Lisp_Object paragraph_dir;
20082
20083 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
20084 (!(ROW)->continued_p \
20085 && INTEGERP ((GLYPH)->object) \
20086 && (GLYPH)->type == CHAR_GLYPH \
20087 && (GLYPH)->u.ch == ' ' \
20088 && (GLYPH)->charpos >= 0 \
20089 && !(GLYPH)->avoid_cursor_p)
20090
20091 CHECK_NUMBER (direction);
20092 dir = XINT (direction);
20093 if (dir > 0)
20094 dir = 1;
20095 else
20096 dir = -1;
20097
20098 /* If current matrix is up-to-date, we can use the information
20099 recorded in the glyphs, at least as long as the goal is on the
20100 screen. */
20101 if (w->window_end_valid
20102 && !windows_or_buffers_changed
20103 && b
20104 && !b->clip_changed
20105 && !b->prevent_redisplay_optimizations_p
20106 && !window_outdated (w)
20107 && w->cursor.vpos >= 0
20108 && w->cursor.vpos < w->current_matrix->nrows
20109 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
20110 {
20111 struct glyph *g = row->glyphs[TEXT_AREA];
20112 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
20113 struct glyph *gpt = g + w->cursor.hpos;
20114
20115 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
20116 {
20117 if (BUFFERP (g->object) && g->charpos != PT)
20118 {
20119 SET_PT (g->charpos);
20120 w->cursor.vpos = -1;
20121 return make_number (PT);
20122 }
20123 else if (!INTEGERP (g->object) && !EQ (g->object, gpt->object))
20124 {
20125 ptrdiff_t new_pos;
20126
20127 if (BUFFERP (gpt->object))
20128 {
20129 new_pos = PT;
20130 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
20131 new_pos += (row->reversed_p ? -dir : dir);
20132 else
20133 new_pos -= (row->reversed_p ? -dir : dir);;
20134 }
20135 else if (BUFFERP (g->object))
20136 new_pos = g->charpos;
20137 else
20138 break;
20139 SET_PT (new_pos);
20140 w->cursor.vpos = -1;
20141 return make_number (PT);
20142 }
20143 else if (ROW_GLYPH_NEWLINE_P (row, g))
20144 {
20145 /* Glyphs inserted at the end of a non-empty line for
20146 positioning the cursor have zero charpos, so we must
20147 deduce the value of point by other means. */
20148 if (g->charpos > 0)
20149 SET_PT (g->charpos);
20150 else if (row->ends_at_zv_p && PT != ZV)
20151 SET_PT (ZV);
20152 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
20153 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20154 else
20155 break;
20156 w->cursor.vpos = -1;
20157 return make_number (PT);
20158 }
20159 }
20160 if (g == e || INTEGERP (g->object))
20161 {
20162 if (row->truncated_on_left_p || row->truncated_on_right_p)
20163 goto simulate_display;
20164 if (!row->reversed_p)
20165 row += dir;
20166 else
20167 row -= dir;
20168 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
20169 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
20170 goto simulate_display;
20171
20172 if (dir > 0)
20173 {
20174 if (row->reversed_p && !row->continued_p)
20175 {
20176 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20177 w->cursor.vpos = -1;
20178 return make_number (PT);
20179 }
20180 g = row->glyphs[TEXT_AREA];
20181 e = g + row->used[TEXT_AREA];
20182 for ( ; g < e; g++)
20183 {
20184 if (BUFFERP (g->object)
20185 /* Empty lines have only one glyph, which stands
20186 for the newline, and whose charpos is the
20187 buffer position of the newline. */
20188 || ROW_GLYPH_NEWLINE_P (row, g)
20189 /* When the buffer ends in a newline, the line at
20190 EOB also has one glyph, but its charpos is -1. */
20191 || (row->ends_at_zv_p
20192 && !row->reversed_p
20193 && INTEGERP (g->object)
20194 && g->type == CHAR_GLYPH
20195 && g->u.ch == ' '))
20196 {
20197 if (g->charpos > 0)
20198 SET_PT (g->charpos);
20199 else if (!row->reversed_p
20200 && row->ends_at_zv_p
20201 && PT != ZV)
20202 SET_PT (ZV);
20203 else
20204 continue;
20205 w->cursor.vpos = -1;
20206 return make_number (PT);
20207 }
20208 }
20209 }
20210 else
20211 {
20212 if (!row->reversed_p && !row->continued_p)
20213 {
20214 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20215 w->cursor.vpos = -1;
20216 return make_number (PT);
20217 }
20218 e = row->glyphs[TEXT_AREA];
20219 g = e + row->used[TEXT_AREA] - 1;
20220 for ( ; g >= e; g--)
20221 {
20222 if (BUFFERP (g->object)
20223 || (ROW_GLYPH_NEWLINE_P (row, g)
20224 && g->charpos > 0)
20225 /* Empty R2L lines on GUI frames have the buffer
20226 position of the newline stored in the stretch
20227 glyph. */
20228 || g->type == STRETCH_GLYPH
20229 || (row->ends_at_zv_p
20230 && row->reversed_p
20231 && INTEGERP (g->object)
20232 && g->type == CHAR_GLYPH
20233 && g->u.ch == ' '))
20234 {
20235 if (g->charpos > 0)
20236 SET_PT (g->charpos);
20237 else if (row->reversed_p
20238 && row->ends_at_zv_p
20239 && PT != ZV)
20240 SET_PT (ZV);
20241 else
20242 continue;
20243 w->cursor.vpos = -1;
20244 return make_number (PT);
20245 }
20246 }
20247 }
20248 }
20249 }
20250
20251 simulate_display:
20252
20253 /* If we wind up here, we failed to move by using the glyphs, so we
20254 need to simulate display instead. */
20255
20256 if (b)
20257 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
20258 else
20259 paragraph_dir = Qleft_to_right;
20260 if (EQ (paragraph_dir, Qright_to_left))
20261 dir = -dir;
20262 if (PT <= BEGV && dir < 0)
20263 xsignal0 (Qbeginning_of_buffer);
20264 else if (PT >= ZV && dir > 0)
20265 xsignal0 (Qend_of_buffer);
20266 else
20267 {
20268 struct text_pos pt;
20269 struct it it;
20270 int pt_x, target_x, pixel_width, pt_vpos;
20271 bool at_eol_p;
20272 bool overshoot_expected = false;
20273 bool target_is_eol_p = false;
20274
20275 /* Setup the arena. */
20276 SET_TEXT_POS (pt, PT, PT_BYTE);
20277 start_display (&it, w, pt);
20278
20279 if (it.cmp_it.id < 0
20280 && it.method == GET_FROM_STRING
20281 && it.area == TEXT_AREA
20282 && it.string_from_display_prop_p
20283 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
20284 overshoot_expected = true;
20285
20286 /* Find the X coordinate of point. We start from the beginning
20287 of this or previous line to make sure we are before point in
20288 the logical order (since the move_it_* functions can only
20289 move forward). */
20290 reseat_at_previous_visible_line_start (&it);
20291 it.current_x = it.hpos = it.current_y = it.vpos = 0;
20292 if (IT_CHARPOS (it) != PT)
20293 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
20294 -1, -1, -1, MOVE_TO_POS);
20295 pt_x = it.current_x;
20296 pt_vpos = it.vpos;
20297 if (dir > 0 || overshoot_expected)
20298 {
20299 struct glyph_row *row = it.glyph_row;
20300
20301 /* When point is at beginning of line, we don't have
20302 information about the glyph there loaded into struct
20303 it. Calling get_next_display_element fixes that. */
20304 if (pt_x == 0)
20305 get_next_display_element (&it);
20306 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20307 it.glyph_row = NULL;
20308 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
20309 it.glyph_row = row;
20310 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
20311 it, lest it will become out of sync with it's buffer
20312 position. */
20313 it.current_x = pt_x;
20314 }
20315 else
20316 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20317 pixel_width = it.pixel_width;
20318 if (overshoot_expected && at_eol_p)
20319 pixel_width = 0;
20320 else if (pixel_width <= 0)
20321 pixel_width = 1;
20322
20323 /* If there's a display string at point, we are actually at the
20324 glyph to the left of point, so we need to correct the X
20325 coordinate. */
20326 if (overshoot_expected)
20327 pt_x += pixel_width;
20328
20329 /* Compute target X coordinate, either to the left or to the
20330 right of point. On TTY frames, all characters have the same
20331 pixel width of 1, so we can use that. On GUI frames we don't
20332 have an easy way of getting at the pixel width of the
20333 character to the left of point, so we use a different method
20334 of getting to that place. */
20335 if (dir > 0)
20336 target_x = pt_x + pixel_width;
20337 else
20338 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
20339
20340 /* Target X coordinate could be one line above or below the line
20341 of point, in which case we need to adjust the target X
20342 coordinate. Also, if moving to the left, we need to begin at
20343 the left edge of the point's screen line. */
20344 if (dir < 0)
20345 {
20346 if (pt_x > 0)
20347 {
20348 start_display (&it, w, pt);
20349 reseat_at_previous_visible_line_start (&it);
20350 it.current_x = it.current_y = it.hpos = 0;
20351 if (pt_vpos != 0)
20352 move_it_by_lines (&it, pt_vpos);
20353 }
20354 else
20355 {
20356 move_it_by_lines (&it, -1);
20357 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
20358 target_is_eol_p = true;
20359 }
20360 }
20361 else
20362 {
20363 if (at_eol_p
20364 || (target_x >= it.last_visible_x
20365 && it.line_wrap != TRUNCATE))
20366 {
20367 if (pt_x > 0)
20368 move_it_by_lines (&it, 0);
20369 move_it_by_lines (&it, 1);
20370 target_x = 0;
20371 }
20372 }
20373
20374 /* Move to the target X coordinate. */
20375 #ifdef HAVE_WINDOW_SYSTEM
20376 /* On GUI frames, as we don't know the X coordinate of the
20377 character to the left of point, moving point to the left
20378 requires walking, one grapheme cluster at a time, until we
20379 find ourself at a place immediately to the left of the
20380 character at point. */
20381 if (FRAME_WINDOW_P (it.f) && dir < 0)
20382 {
20383 struct text_pos new_pos = it.current.pos;
20384 enum move_it_result rc = MOVE_X_REACHED;
20385
20386 while (it.current_x + it.pixel_width <= target_x
20387 && rc == MOVE_X_REACHED)
20388 {
20389 int new_x = it.current_x + it.pixel_width;
20390
20391 new_pos = it.current.pos;
20392 if (new_x == it.current_x)
20393 new_x++;
20394 rc = move_it_in_display_line_to (&it, ZV, new_x,
20395 MOVE_TO_POS | MOVE_TO_X);
20396 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
20397 break;
20398 }
20399 /* If we ended up on a composed character inside
20400 bidi-reordered text (e.g., Hebrew text with diacritics),
20401 the iterator gives us the buffer position of the last (in
20402 logical order) character of the composed grapheme cluster,
20403 which is not what we want. So we cheat: we compute the
20404 character position of the character that follows (in the
20405 logical order) the one where the above loop stopped. That
20406 character will appear on display to the left of point. */
20407 if (it.bidi_p
20408 && it.bidi_it.scan_dir == -1
20409 && new_pos.charpos - IT_CHARPOS (it) > 1)
20410 {
20411 new_pos.charpos = IT_CHARPOS (it) + 1;
20412 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
20413 }
20414 it.current.pos = new_pos;
20415 }
20416 else
20417 #endif
20418 if (it.current_x != target_x)
20419 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
20420
20421 /* When lines are truncated, the above loop will stop at the
20422 window edge. But we want to get to the end of line, even if
20423 it is beyond the window edge; automatic hscroll will then
20424 scroll the window to show point as appropriate. */
20425 if (target_is_eol_p && it.line_wrap == TRUNCATE
20426 && get_next_display_element (&it))
20427 {
20428 struct text_pos new_pos = it.current.pos;
20429
20430 while (!ITERATOR_AT_END_OF_LINE_P (&it))
20431 {
20432 set_iterator_to_next (&it, 0);
20433 if (it.method == GET_FROM_BUFFER)
20434 new_pos = it.current.pos;
20435 if (!get_next_display_element (&it))
20436 break;
20437 }
20438
20439 it.current.pos = new_pos;
20440 }
20441
20442 /* If we ended up in a display string that covers point, move to
20443 buffer position to the right in the visual order. */
20444 if (dir > 0)
20445 {
20446 while (IT_CHARPOS (it) == PT)
20447 {
20448 set_iterator_to_next (&it, 0);
20449 if (!get_next_display_element (&it))
20450 break;
20451 }
20452 }
20453
20454 /* Move point to that position. */
20455 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
20456 }
20457
20458 return make_number (PT);
20459
20460 #undef ROW_GLYPH_NEWLINE_P
20461 }
20462
20463 \f
20464 /***********************************************************************
20465 Menu Bar
20466 ***********************************************************************/
20467
20468 /* Redisplay the menu bar in the frame for window W.
20469
20470 The menu bar of X frames that don't have X toolkit support is
20471 displayed in a special window W->frame->menu_bar_window.
20472
20473 The menu bar of terminal frames is treated specially as far as
20474 glyph matrices are concerned. Menu bar lines are not part of
20475 windows, so the update is done directly on the frame matrix rows
20476 for the menu bar. */
20477
20478 static void
20479 display_menu_bar (struct window *w)
20480 {
20481 struct frame *f = XFRAME (WINDOW_FRAME (w));
20482 struct it it;
20483 Lisp_Object items;
20484 int i;
20485
20486 /* Don't do all this for graphical frames. */
20487 #ifdef HAVE_NTGUI
20488 if (FRAME_W32_P (f))
20489 return;
20490 #endif
20491 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20492 if (FRAME_X_P (f))
20493 return;
20494 #endif
20495
20496 #ifdef HAVE_NS
20497 if (FRAME_NS_P (f))
20498 return;
20499 #endif /* HAVE_NS */
20500
20501 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20502 eassert (!FRAME_WINDOW_P (f));
20503 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20504 it.first_visible_x = 0;
20505 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20506 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
20507 if (FRAME_WINDOW_P (f))
20508 {
20509 /* Menu bar lines are displayed in the desired matrix of the
20510 dummy window menu_bar_window. */
20511 struct window *menu_w;
20512 menu_w = XWINDOW (f->menu_bar_window);
20513 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20514 MENU_FACE_ID);
20515 it.first_visible_x = 0;
20516 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20517 }
20518 else
20519 #endif /* not USE_X_TOOLKIT and not USE_GTK */
20520 {
20521 /* This is a TTY frame, i.e. character hpos/vpos are used as
20522 pixel x/y. */
20523 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20524 MENU_FACE_ID);
20525 it.first_visible_x = 0;
20526 it.last_visible_x = FRAME_COLS (f);
20527 }
20528
20529 /* FIXME: This should be controlled by a user option. See the
20530 comments in redisplay_tool_bar and display_mode_line about
20531 this. */
20532 it.paragraph_embedding = L2R;
20533
20534 /* Clear all rows of the menu bar. */
20535 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20536 {
20537 struct glyph_row *row = it.glyph_row + i;
20538 clear_glyph_row (row);
20539 row->enabled_p = 1;
20540 row->full_width_p = 1;
20541 }
20542
20543 /* Display all items of the menu bar. */
20544 items = FRAME_MENU_BAR_ITEMS (it.f);
20545 for (i = 0; i < ASIZE (items); i += 4)
20546 {
20547 Lisp_Object string;
20548
20549 /* Stop at nil string. */
20550 string = AREF (items, i + 1);
20551 if (NILP (string))
20552 break;
20553
20554 /* Remember where item was displayed. */
20555 ASET (items, i + 3, make_number (it.hpos));
20556
20557 /* Display the item, pad with one space. */
20558 if (it.current_x < it.last_visible_x)
20559 display_string (NULL, string, Qnil, 0, 0, &it,
20560 SCHARS (string) + 1, 0, 0, -1);
20561 }
20562
20563 /* Fill out the line with spaces. */
20564 if (it.current_x < it.last_visible_x)
20565 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20566
20567 /* Compute the total height of the lines. */
20568 compute_line_metrics (&it);
20569 }
20570
20571
20572 \f
20573 /***********************************************************************
20574 Mode Line
20575 ***********************************************************************/
20576
20577 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20578 FORCE is non-zero, redisplay mode lines unconditionally.
20579 Otherwise, redisplay only mode lines that are garbaged. Value is
20580 the number of windows whose mode lines were redisplayed. */
20581
20582 static int
20583 redisplay_mode_lines (Lisp_Object window, int force)
20584 {
20585 int nwindows = 0;
20586
20587 while (!NILP (window))
20588 {
20589 struct window *w = XWINDOW (window);
20590
20591 if (WINDOWP (w->contents))
20592 nwindows += redisplay_mode_lines (w->contents, force);
20593 else if (force
20594 || FRAME_GARBAGED_P (XFRAME (w->frame))
20595 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20596 {
20597 struct text_pos lpoint;
20598 struct buffer *old = current_buffer;
20599
20600 /* Set the window's buffer for the mode line display. */
20601 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20602 set_buffer_internal_1 (XBUFFER (w->contents));
20603
20604 /* Point refers normally to the selected window. For any
20605 other window, set up appropriate value. */
20606 if (!EQ (window, selected_window))
20607 {
20608 struct text_pos pt;
20609
20610 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
20611 if (CHARPOS (pt) < BEGV)
20612 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
20613 else if (CHARPOS (pt) > (ZV - 1))
20614 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
20615 else
20616 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20617 }
20618
20619 /* Display mode lines. */
20620 clear_glyph_matrix (w->desired_matrix);
20621 if (display_mode_lines (w))
20622 {
20623 ++nwindows;
20624 w->must_be_updated_p = 1;
20625 }
20626
20627 /* Restore old settings. */
20628 set_buffer_internal_1 (old);
20629 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20630 }
20631
20632 window = w->next;
20633 }
20634
20635 return nwindows;
20636 }
20637
20638
20639 /* Display the mode and/or header line of window W. Value is the
20640 sum number of mode lines and header lines displayed. */
20641
20642 static int
20643 display_mode_lines (struct window *w)
20644 {
20645 Lisp_Object old_selected_window = selected_window;
20646 Lisp_Object old_selected_frame = selected_frame;
20647 Lisp_Object new_frame = w->frame;
20648 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
20649 int n = 0;
20650
20651 selected_frame = new_frame;
20652 /* FIXME: If we were to allow the mode-line's computation changing the buffer
20653 or window's point, then we'd need select_window_1 here as well. */
20654 XSETWINDOW (selected_window, w);
20655 XFRAME (new_frame)->selected_window = selected_window;
20656
20657 /* These will be set while the mode line specs are processed. */
20658 line_number_displayed = 0;
20659 w->column_number_displayed = -1;
20660
20661 if (WINDOW_WANTS_MODELINE_P (w))
20662 {
20663 struct window *sel_w = XWINDOW (old_selected_window);
20664
20665 /* Select mode line face based on the real selected window. */
20666 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20667 BVAR (current_buffer, mode_line_format));
20668 ++n;
20669 }
20670
20671 if (WINDOW_WANTS_HEADER_LINE_P (w))
20672 {
20673 display_mode_line (w, HEADER_LINE_FACE_ID,
20674 BVAR (current_buffer, header_line_format));
20675 ++n;
20676 }
20677
20678 XFRAME (new_frame)->selected_window = old_frame_selected_window;
20679 selected_frame = old_selected_frame;
20680 selected_window = old_selected_window;
20681 return n;
20682 }
20683
20684
20685 /* Display mode or header line of window W. FACE_ID specifies which
20686 line to display; it is either MODE_LINE_FACE_ID or
20687 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20688 display. Value is the pixel height of the mode/header line
20689 displayed. */
20690
20691 static int
20692 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20693 {
20694 struct it it;
20695 struct face *face;
20696 ptrdiff_t count = SPECPDL_INDEX ();
20697
20698 init_iterator (&it, w, -1, -1, NULL, face_id);
20699 /* Don't extend on a previously drawn mode-line.
20700 This may happen if called from pos_visible_p. */
20701 it.glyph_row->enabled_p = 0;
20702 prepare_desired_row (it.glyph_row);
20703
20704 it.glyph_row->mode_line_p = 1;
20705
20706 /* FIXME: This should be controlled by a user option. But
20707 supporting such an option is not trivial, since the mode line is
20708 made up of many separate strings. */
20709 it.paragraph_embedding = L2R;
20710
20711 record_unwind_protect (unwind_format_mode_line,
20712 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20713
20714 mode_line_target = MODE_LINE_DISPLAY;
20715
20716 /* Temporarily make frame's keyboard the current kboard so that
20717 kboard-local variables in the mode_line_format will get the right
20718 values. */
20719 push_kboard (FRAME_KBOARD (it.f));
20720 record_unwind_save_match_data ();
20721 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20722 pop_kboard ();
20723
20724 unbind_to (count, Qnil);
20725
20726 /* Fill up with spaces. */
20727 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20728
20729 compute_line_metrics (&it);
20730 it.glyph_row->full_width_p = 1;
20731 it.glyph_row->continued_p = 0;
20732 it.glyph_row->truncated_on_left_p = 0;
20733 it.glyph_row->truncated_on_right_p = 0;
20734
20735 /* Make a 3D mode-line have a shadow at its right end. */
20736 face = FACE_FROM_ID (it.f, face_id);
20737 extend_face_to_end_of_line (&it);
20738 if (face->box != FACE_NO_BOX)
20739 {
20740 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20741 + it.glyph_row->used[TEXT_AREA] - 1);
20742 last->right_box_line_p = 1;
20743 }
20744
20745 return it.glyph_row->height;
20746 }
20747
20748 /* Move element ELT in LIST to the front of LIST.
20749 Return the updated list. */
20750
20751 static Lisp_Object
20752 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20753 {
20754 register Lisp_Object tail, prev;
20755 register Lisp_Object tem;
20756
20757 tail = list;
20758 prev = Qnil;
20759 while (CONSP (tail))
20760 {
20761 tem = XCAR (tail);
20762
20763 if (EQ (elt, tem))
20764 {
20765 /* Splice out the link TAIL. */
20766 if (NILP (prev))
20767 list = XCDR (tail);
20768 else
20769 Fsetcdr (prev, XCDR (tail));
20770
20771 /* Now make it the first. */
20772 Fsetcdr (tail, list);
20773 return tail;
20774 }
20775 else
20776 prev = tail;
20777 tail = XCDR (tail);
20778 QUIT;
20779 }
20780
20781 /* Not found--return unchanged LIST. */
20782 return list;
20783 }
20784
20785 /* Contribute ELT to the mode line for window IT->w. How it
20786 translates into text depends on its data type.
20787
20788 IT describes the display environment in which we display, as usual.
20789
20790 DEPTH is the depth in recursion. It is used to prevent
20791 infinite recursion here.
20792
20793 FIELD_WIDTH is the number of characters the display of ELT should
20794 occupy in the mode line, and PRECISION is the maximum number of
20795 characters to display from ELT's representation. See
20796 display_string for details.
20797
20798 Returns the hpos of the end of the text generated by ELT.
20799
20800 PROPS is a property list to add to any string we encounter.
20801
20802 If RISKY is nonzero, remove (disregard) any properties in any string
20803 we encounter, and ignore :eval and :propertize.
20804
20805 The global variable `mode_line_target' determines whether the
20806 output is passed to `store_mode_line_noprop',
20807 `store_mode_line_string', or `display_string'. */
20808
20809 static int
20810 display_mode_element (struct it *it, int depth, int field_width, int precision,
20811 Lisp_Object elt, Lisp_Object props, int risky)
20812 {
20813 int n = 0, field, prec;
20814 int literal = 0;
20815
20816 tail_recurse:
20817 if (depth > 100)
20818 elt = build_string ("*too-deep*");
20819
20820 depth++;
20821
20822 switch (XTYPE (elt))
20823 {
20824 case Lisp_String:
20825 {
20826 /* A string: output it and check for %-constructs within it. */
20827 unsigned char c;
20828 ptrdiff_t offset = 0;
20829
20830 if (SCHARS (elt) > 0
20831 && (!NILP (props) || risky))
20832 {
20833 Lisp_Object oprops, aelt;
20834 oprops = Ftext_properties_at (make_number (0), elt);
20835
20836 /* If the starting string's properties are not what
20837 we want, translate the string. Also, if the string
20838 is risky, do that anyway. */
20839
20840 if (NILP (Fequal (props, oprops)) || risky)
20841 {
20842 /* If the starting string has properties,
20843 merge the specified ones onto the existing ones. */
20844 if (! NILP (oprops) && !risky)
20845 {
20846 Lisp_Object tem;
20847
20848 oprops = Fcopy_sequence (oprops);
20849 tem = props;
20850 while (CONSP (tem))
20851 {
20852 oprops = Fplist_put (oprops, XCAR (tem),
20853 XCAR (XCDR (tem)));
20854 tem = XCDR (XCDR (tem));
20855 }
20856 props = oprops;
20857 }
20858
20859 aelt = Fassoc (elt, mode_line_proptrans_alist);
20860 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20861 {
20862 /* AELT is what we want. Move it to the front
20863 without consing. */
20864 elt = XCAR (aelt);
20865 mode_line_proptrans_alist
20866 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20867 }
20868 else
20869 {
20870 Lisp_Object tem;
20871
20872 /* If AELT has the wrong props, it is useless.
20873 so get rid of it. */
20874 if (! NILP (aelt))
20875 mode_line_proptrans_alist
20876 = Fdelq (aelt, mode_line_proptrans_alist);
20877
20878 elt = Fcopy_sequence (elt);
20879 Fset_text_properties (make_number (0), Flength (elt),
20880 props, elt);
20881 /* Add this item to mode_line_proptrans_alist. */
20882 mode_line_proptrans_alist
20883 = Fcons (Fcons (elt, props),
20884 mode_line_proptrans_alist);
20885 /* Truncate mode_line_proptrans_alist
20886 to at most 50 elements. */
20887 tem = Fnthcdr (make_number (50),
20888 mode_line_proptrans_alist);
20889 if (! NILP (tem))
20890 XSETCDR (tem, Qnil);
20891 }
20892 }
20893 }
20894
20895 offset = 0;
20896
20897 if (literal)
20898 {
20899 prec = precision - n;
20900 switch (mode_line_target)
20901 {
20902 case MODE_LINE_NOPROP:
20903 case MODE_LINE_TITLE:
20904 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20905 break;
20906 case MODE_LINE_STRING:
20907 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20908 break;
20909 case MODE_LINE_DISPLAY:
20910 n += display_string (NULL, elt, Qnil, 0, 0, it,
20911 0, prec, 0, STRING_MULTIBYTE (elt));
20912 break;
20913 }
20914
20915 break;
20916 }
20917
20918 /* Handle the non-literal case. */
20919
20920 while ((precision <= 0 || n < precision)
20921 && SREF (elt, offset) != 0
20922 && (mode_line_target != MODE_LINE_DISPLAY
20923 || it->current_x < it->last_visible_x))
20924 {
20925 ptrdiff_t last_offset = offset;
20926
20927 /* Advance to end of string or next format specifier. */
20928 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20929 ;
20930
20931 if (offset - 1 != last_offset)
20932 {
20933 ptrdiff_t nchars, nbytes;
20934
20935 /* Output to end of string or up to '%'. Field width
20936 is length of string. Don't output more than
20937 PRECISION allows us. */
20938 offset--;
20939
20940 prec = c_string_width (SDATA (elt) + last_offset,
20941 offset - last_offset, precision - n,
20942 &nchars, &nbytes);
20943
20944 switch (mode_line_target)
20945 {
20946 case MODE_LINE_NOPROP:
20947 case MODE_LINE_TITLE:
20948 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20949 break;
20950 case MODE_LINE_STRING:
20951 {
20952 ptrdiff_t bytepos = last_offset;
20953 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20954 ptrdiff_t endpos = (precision <= 0
20955 ? string_byte_to_char (elt, offset)
20956 : charpos + nchars);
20957
20958 n += store_mode_line_string (NULL,
20959 Fsubstring (elt, make_number (charpos),
20960 make_number (endpos)),
20961 0, 0, 0, Qnil);
20962 }
20963 break;
20964 case MODE_LINE_DISPLAY:
20965 {
20966 ptrdiff_t bytepos = last_offset;
20967 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20968
20969 if (precision <= 0)
20970 nchars = string_byte_to_char (elt, offset) - charpos;
20971 n += display_string (NULL, elt, Qnil, 0, charpos,
20972 it, 0, nchars, 0,
20973 STRING_MULTIBYTE (elt));
20974 }
20975 break;
20976 }
20977 }
20978 else /* c == '%' */
20979 {
20980 ptrdiff_t percent_position = offset;
20981
20982 /* Get the specified minimum width. Zero means
20983 don't pad. */
20984 field = 0;
20985 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20986 field = field * 10 + c - '0';
20987
20988 /* Don't pad beyond the total padding allowed. */
20989 if (field_width - n > 0 && field > field_width - n)
20990 field = field_width - n;
20991
20992 /* Note that either PRECISION <= 0 or N < PRECISION. */
20993 prec = precision - n;
20994
20995 if (c == 'M')
20996 n += display_mode_element (it, depth, field, prec,
20997 Vglobal_mode_string, props,
20998 risky);
20999 else if (c != 0)
21000 {
21001 bool multibyte;
21002 ptrdiff_t bytepos, charpos;
21003 const char *spec;
21004 Lisp_Object string;
21005
21006 bytepos = percent_position;
21007 charpos = (STRING_MULTIBYTE (elt)
21008 ? string_byte_to_char (elt, bytepos)
21009 : bytepos);
21010 spec = decode_mode_spec (it->w, c, field, &string);
21011 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
21012
21013 switch (mode_line_target)
21014 {
21015 case MODE_LINE_NOPROP:
21016 case MODE_LINE_TITLE:
21017 n += store_mode_line_noprop (spec, field, prec);
21018 break;
21019 case MODE_LINE_STRING:
21020 {
21021 Lisp_Object tem = build_string (spec);
21022 props = Ftext_properties_at (make_number (charpos), elt);
21023 /* Should only keep face property in props */
21024 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
21025 }
21026 break;
21027 case MODE_LINE_DISPLAY:
21028 {
21029 int nglyphs_before, nwritten;
21030
21031 nglyphs_before = it->glyph_row->used[TEXT_AREA];
21032 nwritten = display_string (spec, string, elt,
21033 charpos, 0, it,
21034 field, prec, 0,
21035 multibyte);
21036
21037 /* Assign to the glyphs written above the
21038 string where the `%x' came from, position
21039 of the `%'. */
21040 if (nwritten > 0)
21041 {
21042 struct glyph *glyph
21043 = (it->glyph_row->glyphs[TEXT_AREA]
21044 + nglyphs_before);
21045 int i;
21046
21047 for (i = 0; i < nwritten; ++i)
21048 {
21049 glyph[i].object = elt;
21050 glyph[i].charpos = charpos;
21051 }
21052
21053 n += nwritten;
21054 }
21055 }
21056 break;
21057 }
21058 }
21059 else /* c == 0 */
21060 break;
21061 }
21062 }
21063 }
21064 break;
21065
21066 case Lisp_Symbol:
21067 /* A symbol: process the value of the symbol recursively
21068 as if it appeared here directly. Avoid error if symbol void.
21069 Special case: if value of symbol is a string, output the string
21070 literally. */
21071 {
21072 register Lisp_Object tem;
21073
21074 /* If the variable is not marked as risky to set
21075 then its contents are risky to use. */
21076 if (NILP (Fget (elt, Qrisky_local_variable)))
21077 risky = 1;
21078
21079 tem = Fboundp (elt);
21080 if (!NILP (tem))
21081 {
21082 tem = Fsymbol_value (elt);
21083 /* If value is a string, output that string literally:
21084 don't check for % within it. */
21085 if (STRINGP (tem))
21086 literal = 1;
21087
21088 if (!EQ (tem, elt))
21089 {
21090 /* Give up right away for nil or t. */
21091 elt = tem;
21092 goto tail_recurse;
21093 }
21094 }
21095 }
21096 break;
21097
21098 case Lisp_Cons:
21099 {
21100 register Lisp_Object car, tem;
21101
21102 /* A cons cell: five distinct cases.
21103 If first element is :eval or :propertize, do something special.
21104 If first element is a string or a cons, process all the elements
21105 and effectively concatenate them.
21106 If first element is a negative number, truncate displaying cdr to
21107 at most that many characters. If positive, pad (with spaces)
21108 to at least that many characters.
21109 If first element is a symbol, process the cadr or caddr recursively
21110 according to whether the symbol's value is non-nil or nil. */
21111 car = XCAR (elt);
21112 if (EQ (car, QCeval))
21113 {
21114 /* An element of the form (:eval FORM) means evaluate FORM
21115 and use the result as mode line elements. */
21116
21117 if (risky)
21118 break;
21119
21120 if (CONSP (XCDR (elt)))
21121 {
21122 Lisp_Object spec;
21123 spec = safe_eval (XCAR (XCDR (elt)));
21124 n += display_mode_element (it, depth, field_width - n,
21125 precision - n, spec, props,
21126 risky);
21127 }
21128 }
21129 else if (EQ (car, QCpropertize))
21130 {
21131 /* An element of the form (:propertize ELT PROPS...)
21132 means display ELT but applying properties PROPS. */
21133
21134 if (risky)
21135 break;
21136
21137 if (CONSP (XCDR (elt)))
21138 n += display_mode_element (it, depth, field_width - n,
21139 precision - n, XCAR (XCDR (elt)),
21140 XCDR (XCDR (elt)), risky);
21141 }
21142 else if (SYMBOLP (car))
21143 {
21144 tem = Fboundp (car);
21145 elt = XCDR (elt);
21146 if (!CONSP (elt))
21147 goto invalid;
21148 /* elt is now the cdr, and we know it is a cons cell.
21149 Use its car if CAR has a non-nil value. */
21150 if (!NILP (tem))
21151 {
21152 tem = Fsymbol_value (car);
21153 if (!NILP (tem))
21154 {
21155 elt = XCAR (elt);
21156 goto tail_recurse;
21157 }
21158 }
21159 /* Symbol's value is nil (or symbol is unbound)
21160 Get the cddr of the original list
21161 and if possible find the caddr and use that. */
21162 elt = XCDR (elt);
21163 if (NILP (elt))
21164 break;
21165 else if (!CONSP (elt))
21166 goto invalid;
21167 elt = XCAR (elt);
21168 goto tail_recurse;
21169 }
21170 else if (INTEGERP (car))
21171 {
21172 register int lim = XINT (car);
21173 elt = XCDR (elt);
21174 if (lim < 0)
21175 {
21176 /* Negative int means reduce maximum width. */
21177 if (precision <= 0)
21178 precision = -lim;
21179 else
21180 precision = min (precision, -lim);
21181 }
21182 else if (lim > 0)
21183 {
21184 /* Padding specified. Don't let it be more than
21185 current maximum. */
21186 if (precision > 0)
21187 lim = min (precision, lim);
21188
21189 /* If that's more padding than already wanted, queue it.
21190 But don't reduce padding already specified even if
21191 that is beyond the current truncation point. */
21192 field_width = max (lim, field_width);
21193 }
21194 goto tail_recurse;
21195 }
21196 else if (STRINGP (car) || CONSP (car))
21197 {
21198 Lisp_Object halftail = elt;
21199 int len = 0;
21200
21201 while (CONSP (elt)
21202 && (precision <= 0 || n < precision))
21203 {
21204 n += display_mode_element (it, depth,
21205 /* Do padding only after the last
21206 element in the list. */
21207 (! CONSP (XCDR (elt))
21208 ? field_width - n
21209 : 0),
21210 precision - n, XCAR (elt),
21211 props, risky);
21212 elt = XCDR (elt);
21213 len++;
21214 if ((len & 1) == 0)
21215 halftail = XCDR (halftail);
21216 /* Check for cycle. */
21217 if (EQ (halftail, elt))
21218 break;
21219 }
21220 }
21221 }
21222 break;
21223
21224 default:
21225 invalid:
21226 elt = build_string ("*invalid*");
21227 goto tail_recurse;
21228 }
21229
21230 /* Pad to FIELD_WIDTH. */
21231 if (field_width > 0 && n < field_width)
21232 {
21233 switch (mode_line_target)
21234 {
21235 case MODE_LINE_NOPROP:
21236 case MODE_LINE_TITLE:
21237 n += store_mode_line_noprop ("", field_width - n, 0);
21238 break;
21239 case MODE_LINE_STRING:
21240 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
21241 break;
21242 case MODE_LINE_DISPLAY:
21243 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
21244 0, 0, 0);
21245 break;
21246 }
21247 }
21248
21249 return n;
21250 }
21251
21252 /* Store a mode-line string element in mode_line_string_list.
21253
21254 If STRING is non-null, display that C string. Otherwise, the Lisp
21255 string LISP_STRING is displayed.
21256
21257 FIELD_WIDTH is the minimum number of output glyphs to produce.
21258 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21259 with spaces. FIELD_WIDTH <= 0 means don't pad.
21260
21261 PRECISION is the maximum number of characters to output from
21262 STRING. PRECISION <= 0 means don't truncate the string.
21263
21264 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
21265 properties to the string.
21266
21267 PROPS are the properties to add to the string.
21268 The mode_line_string_face face property is always added to the string.
21269 */
21270
21271 static int
21272 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
21273 int field_width, int precision, Lisp_Object props)
21274 {
21275 ptrdiff_t len;
21276 int n = 0;
21277
21278 if (string != NULL)
21279 {
21280 len = strlen (string);
21281 if (precision > 0 && len > precision)
21282 len = precision;
21283 lisp_string = make_string (string, len);
21284 if (NILP (props))
21285 props = mode_line_string_face_prop;
21286 else if (!NILP (mode_line_string_face))
21287 {
21288 Lisp_Object face = Fplist_get (props, Qface);
21289 props = Fcopy_sequence (props);
21290 if (NILP (face))
21291 face = mode_line_string_face;
21292 else
21293 face = list2 (face, mode_line_string_face);
21294 props = Fplist_put (props, Qface, face);
21295 }
21296 Fadd_text_properties (make_number (0), make_number (len),
21297 props, lisp_string);
21298 }
21299 else
21300 {
21301 len = XFASTINT (Flength (lisp_string));
21302 if (precision > 0 && len > precision)
21303 {
21304 len = precision;
21305 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
21306 precision = -1;
21307 }
21308 if (!NILP (mode_line_string_face))
21309 {
21310 Lisp_Object face;
21311 if (NILP (props))
21312 props = Ftext_properties_at (make_number (0), lisp_string);
21313 face = Fplist_get (props, Qface);
21314 if (NILP (face))
21315 face = mode_line_string_face;
21316 else
21317 face = list2 (face, mode_line_string_face);
21318 props = list2 (Qface, face);
21319 if (copy_string)
21320 lisp_string = Fcopy_sequence (lisp_string);
21321 }
21322 if (!NILP (props))
21323 Fadd_text_properties (make_number (0), make_number (len),
21324 props, lisp_string);
21325 }
21326
21327 if (len > 0)
21328 {
21329 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21330 n += len;
21331 }
21332
21333 if (field_width > len)
21334 {
21335 field_width -= len;
21336 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
21337 if (!NILP (props))
21338 Fadd_text_properties (make_number (0), make_number (field_width),
21339 props, lisp_string);
21340 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21341 n += field_width;
21342 }
21343
21344 return n;
21345 }
21346
21347
21348 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
21349 1, 4, 0,
21350 doc: /* Format a string out of a mode line format specification.
21351 First arg FORMAT specifies the mode line format (see `mode-line-format'
21352 for details) to use.
21353
21354 By default, the format is evaluated for the currently selected window.
21355
21356 Optional second arg FACE specifies the face property to put on all
21357 characters for which no face is specified. The value nil means the
21358 default face. The value t means whatever face the window's mode line
21359 currently uses (either `mode-line' or `mode-line-inactive',
21360 depending on whether the window is the selected window or not).
21361 An integer value means the value string has no text
21362 properties.
21363
21364 Optional third and fourth args WINDOW and BUFFER specify the window
21365 and buffer to use as the context for the formatting (defaults
21366 are the selected window and the WINDOW's buffer). */)
21367 (Lisp_Object format, Lisp_Object face,
21368 Lisp_Object window, Lisp_Object buffer)
21369 {
21370 struct it it;
21371 int len;
21372 struct window *w;
21373 struct buffer *old_buffer = NULL;
21374 int face_id;
21375 int no_props = INTEGERP (face);
21376 ptrdiff_t count = SPECPDL_INDEX ();
21377 Lisp_Object str;
21378 int string_start = 0;
21379
21380 w = decode_any_window (window);
21381 XSETWINDOW (window, w);
21382
21383 if (NILP (buffer))
21384 buffer = w->contents;
21385 CHECK_BUFFER (buffer);
21386
21387 /* Make formatting the modeline a non-op when noninteractive, otherwise
21388 there will be problems later caused by a partially initialized frame. */
21389 if (NILP (format) || noninteractive)
21390 return empty_unibyte_string;
21391
21392 if (no_props)
21393 face = Qnil;
21394
21395 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
21396 : EQ (face, Qt) ? (EQ (window, selected_window)
21397 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21398 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21399 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21400 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21401 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21402 : DEFAULT_FACE_ID;
21403
21404 old_buffer = current_buffer;
21405
21406 /* Save things including mode_line_proptrans_alist,
21407 and set that to nil so that we don't alter the outer value. */
21408 record_unwind_protect (unwind_format_mode_line,
21409 format_mode_line_unwind_data
21410 (XFRAME (WINDOW_FRAME (w)),
21411 old_buffer, selected_window, 1));
21412 mode_line_proptrans_alist = Qnil;
21413
21414 Fselect_window (window, Qt);
21415 set_buffer_internal_1 (XBUFFER (buffer));
21416
21417 init_iterator (&it, w, -1, -1, NULL, face_id);
21418
21419 if (no_props)
21420 {
21421 mode_line_target = MODE_LINE_NOPROP;
21422 mode_line_string_face_prop = Qnil;
21423 mode_line_string_list = Qnil;
21424 string_start = MODE_LINE_NOPROP_LEN (0);
21425 }
21426 else
21427 {
21428 mode_line_target = MODE_LINE_STRING;
21429 mode_line_string_list = Qnil;
21430 mode_line_string_face = face;
21431 mode_line_string_face_prop
21432 = NILP (face) ? Qnil : list2 (Qface, face);
21433 }
21434
21435 push_kboard (FRAME_KBOARD (it.f));
21436 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21437 pop_kboard ();
21438
21439 if (no_props)
21440 {
21441 len = MODE_LINE_NOPROP_LEN (string_start);
21442 str = make_string (mode_line_noprop_buf + string_start, len);
21443 }
21444 else
21445 {
21446 mode_line_string_list = Fnreverse (mode_line_string_list);
21447 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21448 empty_unibyte_string);
21449 }
21450
21451 unbind_to (count, Qnil);
21452 return str;
21453 }
21454
21455 /* Write a null-terminated, right justified decimal representation of
21456 the positive integer D to BUF using a minimal field width WIDTH. */
21457
21458 static void
21459 pint2str (register char *buf, register int width, register ptrdiff_t d)
21460 {
21461 register char *p = buf;
21462
21463 if (d <= 0)
21464 *p++ = '0';
21465 else
21466 {
21467 while (d > 0)
21468 {
21469 *p++ = d % 10 + '0';
21470 d /= 10;
21471 }
21472 }
21473
21474 for (width -= (int) (p - buf); width > 0; --width)
21475 *p++ = ' ';
21476 *p-- = '\0';
21477 while (p > buf)
21478 {
21479 d = *buf;
21480 *buf++ = *p;
21481 *p-- = d;
21482 }
21483 }
21484
21485 /* Write a null-terminated, right justified decimal and "human
21486 readable" representation of the nonnegative integer D to BUF using
21487 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21488
21489 static const char power_letter[] =
21490 {
21491 0, /* no letter */
21492 'k', /* kilo */
21493 'M', /* mega */
21494 'G', /* giga */
21495 'T', /* tera */
21496 'P', /* peta */
21497 'E', /* exa */
21498 'Z', /* zetta */
21499 'Y' /* yotta */
21500 };
21501
21502 static void
21503 pint2hrstr (char *buf, int width, ptrdiff_t d)
21504 {
21505 /* We aim to represent the nonnegative integer D as
21506 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21507 ptrdiff_t quotient = d;
21508 int remainder = 0;
21509 /* -1 means: do not use TENTHS. */
21510 int tenths = -1;
21511 int exponent = 0;
21512
21513 /* Length of QUOTIENT.TENTHS as a string. */
21514 int length;
21515
21516 char * psuffix;
21517 char * p;
21518
21519 if (quotient >= 1000)
21520 {
21521 /* Scale to the appropriate EXPONENT. */
21522 do
21523 {
21524 remainder = quotient % 1000;
21525 quotient /= 1000;
21526 exponent++;
21527 }
21528 while (quotient >= 1000);
21529
21530 /* Round to nearest and decide whether to use TENTHS or not. */
21531 if (quotient <= 9)
21532 {
21533 tenths = remainder / 100;
21534 if (remainder % 100 >= 50)
21535 {
21536 if (tenths < 9)
21537 tenths++;
21538 else
21539 {
21540 quotient++;
21541 if (quotient == 10)
21542 tenths = -1;
21543 else
21544 tenths = 0;
21545 }
21546 }
21547 }
21548 else
21549 if (remainder >= 500)
21550 {
21551 if (quotient < 999)
21552 quotient++;
21553 else
21554 {
21555 quotient = 1;
21556 exponent++;
21557 tenths = 0;
21558 }
21559 }
21560 }
21561
21562 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21563 if (tenths == -1 && quotient <= 99)
21564 if (quotient <= 9)
21565 length = 1;
21566 else
21567 length = 2;
21568 else
21569 length = 3;
21570 p = psuffix = buf + max (width, length);
21571
21572 /* Print EXPONENT. */
21573 *psuffix++ = power_letter[exponent];
21574 *psuffix = '\0';
21575
21576 /* Print TENTHS. */
21577 if (tenths >= 0)
21578 {
21579 *--p = '0' + tenths;
21580 *--p = '.';
21581 }
21582
21583 /* Print QUOTIENT. */
21584 do
21585 {
21586 int digit = quotient % 10;
21587 *--p = '0' + digit;
21588 }
21589 while ((quotient /= 10) != 0);
21590
21591 /* Print leading spaces. */
21592 while (buf < p)
21593 *--p = ' ';
21594 }
21595
21596 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21597 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21598 type of CODING_SYSTEM. Return updated pointer into BUF. */
21599
21600 static unsigned char invalid_eol_type[] = "(*invalid*)";
21601
21602 static char *
21603 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21604 {
21605 Lisp_Object val;
21606 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21607 const unsigned char *eol_str;
21608 int eol_str_len;
21609 /* The EOL conversion we are using. */
21610 Lisp_Object eoltype;
21611
21612 val = CODING_SYSTEM_SPEC (coding_system);
21613 eoltype = Qnil;
21614
21615 if (!VECTORP (val)) /* Not yet decided. */
21616 {
21617 *buf++ = multibyte ? '-' : ' ';
21618 if (eol_flag)
21619 eoltype = eol_mnemonic_undecided;
21620 /* Don't mention EOL conversion if it isn't decided. */
21621 }
21622 else
21623 {
21624 Lisp_Object attrs;
21625 Lisp_Object eolvalue;
21626
21627 attrs = AREF (val, 0);
21628 eolvalue = AREF (val, 2);
21629
21630 *buf++ = multibyte
21631 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21632 : ' ';
21633
21634 if (eol_flag)
21635 {
21636 /* The EOL conversion that is normal on this system. */
21637
21638 if (NILP (eolvalue)) /* Not yet decided. */
21639 eoltype = eol_mnemonic_undecided;
21640 else if (VECTORP (eolvalue)) /* Not yet decided. */
21641 eoltype = eol_mnemonic_undecided;
21642 else /* eolvalue is Qunix, Qdos, or Qmac. */
21643 eoltype = (EQ (eolvalue, Qunix)
21644 ? eol_mnemonic_unix
21645 : (EQ (eolvalue, Qdos) == 1
21646 ? eol_mnemonic_dos : eol_mnemonic_mac));
21647 }
21648 }
21649
21650 if (eol_flag)
21651 {
21652 /* Mention the EOL conversion if it is not the usual one. */
21653 if (STRINGP (eoltype))
21654 {
21655 eol_str = SDATA (eoltype);
21656 eol_str_len = SBYTES (eoltype);
21657 }
21658 else if (CHARACTERP (eoltype))
21659 {
21660 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21661 int c = XFASTINT (eoltype);
21662 eol_str_len = CHAR_STRING (c, tmp);
21663 eol_str = tmp;
21664 }
21665 else
21666 {
21667 eol_str = invalid_eol_type;
21668 eol_str_len = sizeof (invalid_eol_type) - 1;
21669 }
21670 memcpy (buf, eol_str, eol_str_len);
21671 buf += eol_str_len;
21672 }
21673
21674 return buf;
21675 }
21676
21677 /* Return a string for the output of a mode line %-spec for window W,
21678 generated by character C. FIELD_WIDTH > 0 means pad the string
21679 returned with spaces to that value. Return a Lisp string in
21680 *STRING if the resulting string is taken from that Lisp string.
21681
21682 Note we operate on the current buffer for most purposes. */
21683
21684 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21685
21686 static const char *
21687 decode_mode_spec (struct window *w, register int c, int field_width,
21688 Lisp_Object *string)
21689 {
21690 Lisp_Object obj;
21691 struct frame *f = XFRAME (WINDOW_FRAME (w));
21692 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21693 /* We are going to use f->decode_mode_spec_buffer as the buffer to
21694 produce strings from numerical values, so limit preposterously
21695 large values of FIELD_WIDTH to avoid overrunning the buffer's
21696 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
21697 bytes plus the terminating null. */
21698 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
21699 struct buffer *b = current_buffer;
21700
21701 obj = Qnil;
21702 *string = Qnil;
21703
21704 switch (c)
21705 {
21706 case '*':
21707 if (!NILP (BVAR (b, read_only)))
21708 return "%";
21709 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21710 return "*";
21711 return "-";
21712
21713 case '+':
21714 /* This differs from %* only for a modified read-only buffer. */
21715 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21716 return "*";
21717 if (!NILP (BVAR (b, read_only)))
21718 return "%";
21719 return "-";
21720
21721 case '&':
21722 /* This differs from %* in ignoring read-only-ness. */
21723 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21724 return "*";
21725 return "-";
21726
21727 case '%':
21728 return "%";
21729
21730 case '[':
21731 {
21732 int i;
21733 char *p;
21734
21735 if (command_loop_level > 5)
21736 return "[[[... ";
21737 p = decode_mode_spec_buf;
21738 for (i = 0; i < command_loop_level; i++)
21739 *p++ = '[';
21740 *p = 0;
21741 return decode_mode_spec_buf;
21742 }
21743
21744 case ']':
21745 {
21746 int i;
21747 char *p;
21748
21749 if (command_loop_level > 5)
21750 return " ...]]]";
21751 p = decode_mode_spec_buf;
21752 for (i = 0; i < command_loop_level; i++)
21753 *p++ = ']';
21754 *p = 0;
21755 return decode_mode_spec_buf;
21756 }
21757
21758 case '-':
21759 {
21760 register int i;
21761
21762 /* Let lots_of_dashes be a string of infinite length. */
21763 if (mode_line_target == MODE_LINE_NOPROP
21764 || mode_line_target == MODE_LINE_STRING)
21765 return "--";
21766 if (field_width <= 0
21767 || field_width > sizeof (lots_of_dashes))
21768 {
21769 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21770 decode_mode_spec_buf[i] = '-';
21771 decode_mode_spec_buf[i] = '\0';
21772 return decode_mode_spec_buf;
21773 }
21774 else
21775 return lots_of_dashes;
21776 }
21777
21778 case 'b':
21779 obj = BVAR (b, name);
21780 break;
21781
21782 case 'c':
21783 /* %c and %l are ignored in `frame-title-format'.
21784 (In redisplay_internal, the frame title is drawn _before_ the
21785 windows are updated, so the stuff which depends on actual
21786 window contents (such as %l) may fail to render properly, or
21787 even crash emacs.) */
21788 if (mode_line_target == MODE_LINE_TITLE)
21789 return "";
21790 else
21791 {
21792 ptrdiff_t col = current_column ();
21793 w->column_number_displayed = col;
21794 pint2str (decode_mode_spec_buf, width, col);
21795 return decode_mode_spec_buf;
21796 }
21797
21798 case 'e':
21799 #ifndef SYSTEM_MALLOC
21800 {
21801 if (NILP (Vmemory_full))
21802 return "";
21803 else
21804 return "!MEM FULL! ";
21805 }
21806 #else
21807 return "";
21808 #endif
21809
21810 case 'F':
21811 /* %F displays the frame name. */
21812 if (!NILP (f->title))
21813 return SSDATA (f->title);
21814 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21815 return SSDATA (f->name);
21816 return "Emacs";
21817
21818 case 'f':
21819 obj = BVAR (b, filename);
21820 break;
21821
21822 case 'i':
21823 {
21824 ptrdiff_t size = ZV - BEGV;
21825 pint2str (decode_mode_spec_buf, width, size);
21826 return decode_mode_spec_buf;
21827 }
21828
21829 case 'I':
21830 {
21831 ptrdiff_t size = ZV - BEGV;
21832 pint2hrstr (decode_mode_spec_buf, width, size);
21833 return decode_mode_spec_buf;
21834 }
21835
21836 case 'l':
21837 {
21838 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21839 ptrdiff_t topline, nlines, height;
21840 ptrdiff_t junk;
21841
21842 /* %c and %l are ignored in `frame-title-format'. */
21843 if (mode_line_target == MODE_LINE_TITLE)
21844 return "";
21845
21846 startpos = marker_position (w->start);
21847 startpos_byte = marker_byte_position (w->start);
21848 height = WINDOW_TOTAL_LINES (w);
21849
21850 /* If we decided that this buffer isn't suitable for line numbers,
21851 don't forget that too fast. */
21852 if (w->base_line_pos == -1)
21853 goto no_value;
21854
21855 /* If the buffer is very big, don't waste time. */
21856 if (INTEGERP (Vline_number_display_limit)
21857 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21858 {
21859 w->base_line_pos = 0;
21860 w->base_line_number = 0;
21861 goto no_value;
21862 }
21863
21864 if (w->base_line_number > 0
21865 && w->base_line_pos > 0
21866 && w->base_line_pos <= startpos)
21867 {
21868 line = w->base_line_number;
21869 linepos = w->base_line_pos;
21870 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21871 }
21872 else
21873 {
21874 line = 1;
21875 linepos = BUF_BEGV (b);
21876 linepos_byte = BUF_BEGV_BYTE (b);
21877 }
21878
21879 /* Count lines from base line to window start position. */
21880 nlines = display_count_lines (linepos_byte,
21881 startpos_byte,
21882 startpos, &junk);
21883
21884 topline = nlines + line;
21885
21886 /* Determine a new base line, if the old one is too close
21887 or too far away, or if we did not have one.
21888 "Too close" means it's plausible a scroll-down would
21889 go back past it. */
21890 if (startpos == BUF_BEGV (b))
21891 {
21892 w->base_line_number = topline;
21893 w->base_line_pos = BUF_BEGV (b);
21894 }
21895 else if (nlines < height + 25 || nlines > height * 3 + 50
21896 || linepos == BUF_BEGV (b))
21897 {
21898 ptrdiff_t limit = BUF_BEGV (b);
21899 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21900 ptrdiff_t position;
21901 ptrdiff_t distance =
21902 (height * 2 + 30) * line_number_display_limit_width;
21903
21904 if (startpos - distance > limit)
21905 {
21906 limit = startpos - distance;
21907 limit_byte = CHAR_TO_BYTE (limit);
21908 }
21909
21910 nlines = display_count_lines (startpos_byte,
21911 limit_byte,
21912 - (height * 2 + 30),
21913 &position);
21914 /* If we couldn't find the lines we wanted within
21915 line_number_display_limit_width chars per line,
21916 give up on line numbers for this window. */
21917 if (position == limit_byte && limit == startpos - distance)
21918 {
21919 w->base_line_pos = -1;
21920 w->base_line_number = 0;
21921 goto no_value;
21922 }
21923
21924 w->base_line_number = topline - nlines;
21925 w->base_line_pos = BYTE_TO_CHAR (position);
21926 }
21927
21928 /* Now count lines from the start pos to point. */
21929 nlines = display_count_lines (startpos_byte,
21930 PT_BYTE, PT, &junk);
21931
21932 /* Record that we did display the line number. */
21933 line_number_displayed = 1;
21934
21935 /* Make the string to show. */
21936 pint2str (decode_mode_spec_buf, width, topline + nlines);
21937 return decode_mode_spec_buf;
21938 no_value:
21939 {
21940 char* p = decode_mode_spec_buf;
21941 int pad = width - 2;
21942 while (pad-- > 0)
21943 *p++ = ' ';
21944 *p++ = '?';
21945 *p++ = '?';
21946 *p = '\0';
21947 return decode_mode_spec_buf;
21948 }
21949 }
21950 break;
21951
21952 case 'm':
21953 obj = BVAR (b, mode_name);
21954 break;
21955
21956 case 'n':
21957 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21958 return " Narrow";
21959 break;
21960
21961 case 'p':
21962 {
21963 ptrdiff_t pos = marker_position (w->start);
21964 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21965
21966 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
21967 {
21968 if (pos <= BUF_BEGV (b))
21969 return "All";
21970 else
21971 return "Bottom";
21972 }
21973 else if (pos <= BUF_BEGV (b))
21974 return "Top";
21975 else
21976 {
21977 if (total > 1000000)
21978 /* Do it differently for a large value, to avoid overflow. */
21979 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21980 else
21981 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21982 /* We can't normally display a 3-digit number,
21983 so get us a 2-digit number that is close. */
21984 if (total == 100)
21985 total = 99;
21986 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21987 return decode_mode_spec_buf;
21988 }
21989 }
21990
21991 /* Display percentage of size above the bottom of the screen. */
21992 case 'P':
21993 {
21994 ptrdiff_t toppos = marker_position (w->start);
21995 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
21996 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21997
21998 if (botpos >= BUF_ZV (b))
21999 {
22000 if (toppos <= BUF_BEGV (b))
22001 return "All";
22002 else
22003 return "Bottom";
22004 }
22005 else
22006 {
22007 if (total > 1000000)
22008 /* Do it differently for a large value, to avoid overflow. */
22009 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22010 else
22011 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
22012 /* We can't normally display a 3-digit number,
22013 so get us a 2-digit number that is close. */
22014 if (total == 100)
22015 total = 99;
22016 if (toppos <= BUF_BEGV (b))
22017 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
22018 else
22019 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22020 return decode_mode_spec_buf;
22021 }
22022 }
22023
22024 case 's':
22025 /* status of process */
22026 obj = Fget_buffer_process (Fcurrent_buffer ());
22027 if (NILP (obj))
22028 return "no process";
22029 #ifndef MSDOS
22030 obj = Fsymbol_name (Fprocess_status (obj));
22031 #endif
22032 break;
22033
22034 case '@':
22035 {
22036 ptrdiff_t count = inhibit_garbage_collection ();
22037 Lisp_Object val = call1 (intern ("file-remote-p"),
22038 BVAR (current_buffer, directory));
22039 unbind_to (count, Qnil);
22040
22041 if (NILP (val))
22042 return "-";
22043 else
22044 return "@";
22045 }
22046
22047 case 'z':
22048 /* coding-system (not including end-of-line format) */
22049 case 'Z':
22050 /* coding-system (including end-of-line type) */
22051 {
22052 int eol_flag = (c == 'Z');
22053 char *p = decode_mode_spec_buf;
22054
22055 if (! FRAME_WINDOW_P (f))
22056 {
22057 /* No need to mention EOL here--the terminal never needs
22058 to do EOL conversion. */
22059 p = decode_mode_spec_coding (CODING_ID_NAME
22060 (FRAME_KEYBOARD_CODING (f)->id),
22061 p, 0);
22062 p = decode_mode_spec_coding (CODING_ID_NAME
22063 (FRAME_TERMINAL_CODING (f)->id),
22064 p, 0);
22065 }
22066 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
22067 p, eol_flag);
22068
22069 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
22070 #ifdef subprocesses
22071 obj = Fget_buffer_process (Fcurrent_buffer ());
22072 if (PROCESSP (obj))
22073 {
22074 p = decode_mode_spec_coding
22075 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
22076 p = decode_mode_spec_coding
22077 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
22078 }
22079 #endif /* subprocesses */
22080 #endif /* 0 */
22081 *p = 0;
22082 return decode_mode_spec_buf;
22083 }
22084 }
22085
22086 if (STRINGP (obj))
22087 {
22088 *string = obj;
22089 return SSDATA (obj);
22090 }
22091 else
22092 return "";
22093 }
22094
22095
22096 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
22097 means count lines back from START_BYTE. But don't go beyond
22098 LIMIT_BYTE. Return the number of lines thus found (always
22099 nonnegative).
22100
22101 Set *BYTE_POS_PTR to the byte position where we stopped. This is
22102 either the position COUNT lines after/before START_BYTE, if we
22103 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
22104 COUNT lines. */
22105
22106 static ptrdiff_t
22107 display_count_lines (ptrdiff_t start_byte,
22108 ptrdiff_t limit_byte, ptrdiff_t count,
22109 ptrdiff_t *byte_pos_ptr)
22110 {
22111 register unsigned char *cursor;
22112 unsigned char *base;
22113
22114 register ptrdiff_t ceiling;
22115 register unsigned char *ceiling_addr;
22116 ptrdiff_t orig_count = count;
22117
22118 /* If we are not in selective display mode,
22119 check only for newlines. */
22120 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
22121 && !INTEGERP (BVAR (current_buffer, selective_display)));
22122
22123 if (count > 0)
22124 {
22125 while (start_byte < limit_byte)
22126 {
22127 ceiling = BUFFER_CEILING_OF (start_byte);
22128 ceiling = min (limit_byte - 1, ceiling);
22129 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
22130 base = (cursor = BYTE_POS_ADDR (start_byte));
22131
22132 do
22133 {
22134 if (selective_display)
22135 {
22136 while (*cursor != '\n' && *cursor != 015
22137 && ++cursor != ceiling_addr)
22138 continue;
22139 if (cursor == ceiling_addr)
22140 break;
22141 }
22142 else
22143 {
22144 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
22145 if (! cursor)
22146 break;
22147 }
22148
22149 cursor++;
22150
22151 if (--count == 0)
22152 {
22153 start_byte += cursor - base;
22154 *byte_pos_ptr = start_byte;
22155 return orig_count;
22156 }
22157 }
22158 while (cursor < ceiling_addr);
22159
22160 start_byte += ceiling_addr - base;
22161 }
22162 }
22163 else
22164 {
22165 while (start_byte > limit_byte)
22166 {
22167 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
22168 ceiling = max (limit_byte, ceiling);
22169 ceiling_addr = BYTE_POS_ADDR (ceiling);
22170 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
22171 while (1)
22172 {
22173 if (selective_display)
22174 {
22175 while (--cursor >= ceiling_addr
22176 && *cursor != '\n' && *cursor != 015)
22177 continue;
22178 if (cursor < ceiling_addr)
22179 break;
22180 }
22181 else
22182 {
22183 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
22184 if (! cursor)
22185 break;
22186 }
22187
22188 if (++count == 0)
22189 {
22190 start_byte += cursor - base + 1;
22191 *byte_pos_ptr = start_byte;
22192 /* When scanning backwards, we should
22193 not count the newline posterior to which we stop. */
22194 return - orig_count - 1;
22195 }
22196 }
22197 start_byte += ceiling_addr - base;
22198 }
22199 }
22200
22201 *byte_pos_ptr = limit_byte;
22202
22203 if (count < 0)
22204 return - orig_count + count;
22205 return orig_count - count;
22206
22207 }
22208
22209
22210 \f
22211 /***********************************************************************
22212 Displaying strings
22213 ***********************************************************************/
22214
22215 /* Display a NUL-terminated string, starting with index START.
22216
22217 If STRING is non-null, display that C string. Otherwise, the Lisp
22218 string LISP_STRING is displayed. There's a case that STRING is
22219 non-null and LISP_STRING is not nil. It means STRING is a string
22220 data of LISP_STRING. In that case, we display LISP_STRING while
22221 ignoring its text properties.
22222
22223 If FACE_STRING is not nil, FACE_STRING_POS is a position in
22224 FACE_STRING. Display STRING or LISP_STRING with the face at
22225 FACE_STRING_POS in FACE_STRING:
22226
22227 Display the string in the environment given by IT, but use the
22228 standard display table, temporarily.
22229
22230 FIELD_WIDTH is the minimum number of output glyphs to produce.
22231 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22232 with spaces. If STRING has more characters, more than FIELD_WIDTH
22233 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
22234
22235 PRECISION is the maximum number of characters to output from
22236 STRING. PRECISION < 0 means don't truncate the string.
22237
22238 This is roughly equivalent to printf format specifiers:
22239
22240 FIELD_WIDTH PRECISION PRINTF
22241 ----------------------------------------
22242 -1 -1 %s
22243 -1 10 %.10s
22244 10 -1 %10s
22245 20 10 %20.10s
22246
22247 MULTIBYTE zero means do not display multibyte chars, > 0 means do
22248 display them, and < 0 means obey the current buffer's value of
22249 enable_multibyte_characters.
22250
22251 Value is the number of columns displayed. */
22252
22253 static int
22254 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
22255 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
22256 int field_width, int precision, int max_x, int multibyte)
22257 {
22258 int hpos_at_start = it->hpos;
22259 int saved_face_id = it->face_id;
22260 struct glyph_row *row = it->glyph_row;
22261 ptrdiff_t it_charpos;
22262
22263 /* Initialize the iterator IT for iteration over STRING beginning
22264 with index START. */
22265 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
22266 precision, field_width, multibyte);
22267 if (string && STRINGP (lisp_string))
22268 /* LISP_STRING is the one returned by decode_mode_spec. We should
22269 ignore its text properties. */
22270 it->stop_charpos = it->end_charpos;
22271
22272 /* If displaying STRING, set up the face of the iterator from
22273 FACE_STRING, if that's given. */
22274 if (STRINGP (face_string))
22275 {
22276 ptrdiff_t endptr;
22277 struct face *face;
22278
22279 it->face_id
22280 = face_at_string_position (it->w, face_string, face_string_pos,
22281 0, it->region_beg_charpos,
22282 it->region_end_charpos,
22283 &endptr, it->base_face_id, 0);
22284 face = FACE_FROM_ID (it->f, it->face_id);
22285 it->face_box_p = face->box != FACE_NO_BOX;
22286 }
22287
22288 /* Set max_x to the maximum allowed X position. Don't let it go
22289 beyond the right edge of the window. */
22290 if (max_x <= 0)
22291 max_x = it->last_visible_x;
22292 else
22293 max_x = min (max_x, it->last_visible_x);
22294
22295 /* Skip over display elements that are not visible. because IT->w is
22296 hscrolled. */
22297 if (it->current_x < it->first_visible_x)
22298 move_it_in_display_line_to (it, 100000, it->first_visible_x,
22299 MOVE_TO_POS | MOVE_TO_X);
22300
22301 row->ascent = it->max_ascent;
22302 row->height = it->max_ascent + it->max_descent;
22303 row->phys_ascent = it->max_phys_ascent;
22304 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
22305 row->extra_line_spacing = it->max_extra_line_spacing;
22306
22307 if (STRINGP (it->string))
22308 it_charpos = IT_STRING_CHARPOS (*it);
22309 else
22310 it_charpos = IT_CHARPOS (*it);
22311
22312 /* This condition is for the case that we are called with current_x
22313 past last_visible_x. */
22314 while (it->current_x < max_x)
22315 {
22316 int x_before, x, n_glyphs_before, i, nglyphs;
22317
22318 /* Get the next display element. */
22319 if (!get_next_display_element (it))
22320 break;
22321
22322 /* Produce glyphs. */
22323 x_before = it->current_x;
22324 n_glyphs_before = row->used[TEXT_AREA];
22325 PRODUCE_GLYPHS (it);
22326
22327 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
22328 i = 0;
22329 x = x_before;
22330 while (i < nglyphs)
22331 {
22332 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
22333
22334 if (it->line_wrap != TRUNCATE
22335 && x + glyph->pixel_width > max_x)
22336 {
22337 /* End of continued line or max_x reached. */
22338 if (CHAR_GLYPH_PADDING_P (*glyph))
22339 {
22340 /* A wide character is unbreakable. */
22341 if (row->reversed_p)
22342 unproduce_glyphs (it, row->used[TEXT_AREA]
22343 - n_glyphs_before);
22344 row->used[TEXT_AREA] = n_glyphs_before;
22345 it->current_x = x_before;
22346 }
22347 else
22348 {
22349 if (row->reversed_p)
22350 unproduce_glyphs (it, row->used[TEXT_AREA]
22351 - (n_glyphs_before + i));
22352 row->used[TEXT_AREA] = n_glyphs_before + i;
22353 it->current_x = x;
22354 }
22355 break;
22356 }
22357 else if (x + glyph->pixel_width >= it->first_visible_x)
22358 {
22359 /* Glyph is at least partially visible. */
22360 ++it->hpos;
22361 if (x < it->first_visible_x)
22362 row->x = x - it->first_visible_x;
22363 }
22364 else
22365 {
22366 /* Glyph is off the left margin of the display area.
22367 Should not happen. */
22368 emacs_abort ();
22369 }
22370
22371 row->ascent = max (row->ascent, it->max_ascent);
22372 row->height = max (row->height, it->max_ascent + it->max_descent);
22373 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
22374 row->phys_height = max (row->phys_height,
22375 it->max_phys_ascent + it->max_phys_descent);
22376 row->extra_line_spacing = max (row->extra_line_spacing,
22377 it->max_extra_line_spacing);
22378 x += glyph->pixel_width;
22379 ++i;
22380 }
22381
22382 /* Stop if max_x reached. */
22383 if (i < nglyphs)
22384 break;
22385
22386 /* Stop at line ends. */
22387 if (ITERATOR_AT_END_OF_LINE_P (it))
22388 {
22389 it->continuation_lines_width = 0;
22390 break;
22391 }
22392
22393 set_iterator_to_next (it, 1);
22394 if (STRINGP (it->string))
22395 it_charpos = IT_STRING_CHARPOS (*it);
22396 else
22397 it_charpos = IT_CHARPOS (*it);
22398
22399 /* Stop if truncating at the right edge. */
22400 if (it->line_wrap == TRUNCATE
22401 && it->current_x >= it->last_visible_x)
22402 {
22403 /* Add truncation mark, but don't do it if the line is
22404 truncated at a padding space. */
22405 if (it_charpos < it->string_nchars)
22406 {
22407 if (!FRAME_WINDOW_P (it->f))
22408 {
22409 int ii, n;
22410
22411 if (it->current_x > it->last_visible_x)
22412 {
22413 if (!row->reversed_p)
22414 {
22415 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22416 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22417 break;
22418 }
22419 else
22420 {
22421 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22422 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22423 break;
22424 unproduce_glyphs (it, ii + 1);
22425 ii = row->used[TEXT_AREA] - (ii + 1);
22426 }
22427 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22428 {
22429 row->used[TEXT_AREA] = ii;
22430 produce_special_glyphs (it, IT_TRUNCATION);
22431 }
22432 }
22433 produce_special_glyphs (it, IT_TRUNCATION);
22434 }
22435 row->truncated_on_right_p = 1;
22436 }
22437 break;
22438 }
22439 }
22440
22441 /* Maybe insert a truncation at the left. */
22442 if (it->first_visible_x
22443 && it_charpos > 0)
22444 {
22445 if (!FRAME_WINDOW_P (it->f)
22446 || (row->reversed_p
22447 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22448 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22449 insert_left_trunc_glyphs (it);
22450 row->truncated_on_left_p = 1;
22451 }
22452
22453 it->face_id = saved_face_id;
22454
22455 /* Value is number of columns displayed. */
22456 return it->hpos - hpos_at_start;
22457 }
22458
22459
22460 \f
22461 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22462 appears as an element of LIST or as the car of an element of LIST.
22463 If PROPVAL is a list, compare each element against LIST in that
22464 way, and return 1/2 if any element of PROPVAL is found in LIST.
22465 Otherwise return 0. This function cannot quit.
22466 The return value is 2 if the text is invisible but with an ellipsis
22467 and 1 if it's invisible and without an ellipsis. */
22468
22469 int
22470 invisible_p (register Lisp_Object propval, Lisp_Object list)
22471 {
22472 register Lisp_Object tail, proptail;
22473
22474 for (tail = list; CONSP (tail); tail = XCDR (tail))
22475 {
22476 register Lisp_Object tem;
22477 tem = XCAR (tail);
22478 if (EQ (propval, tem))
22479 return 1;
22480 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22481 return NILP (XCDR (tem)) ? 1 : 2;
22482 }
22483
22484 if (CONSP (propval))
22485 {
22486 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22487 {
22488 Lisp_Object propelt;
22489 propelt = XCAR (proptail);
22490 for (tail = list; CONSP (tail); tail = XCDR (tail))
22491 {
22492 register Lisp_Object tem;
22493 tem = XCAR (tail);
22494 if (EQ (propelt, tem))
22495 return 1;
22496 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22497 return NILP (XCDR (tem)) ? 1 : 2;
22498 }
22499 }
22500 }
22501
22502 return 0;
22503 }
22504
22505 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22506 doc: /* Non-nil if the property makes the text invisible.
22507 POS-OR-PROP can be a marker or number, in which case it is taken to be
22508 a position in the current buffer and the value of the `invisible' property
22509 is checked; or it can be some other value, which is then presumed to be the
22510 value of the `invisible' property of the text of interest.
22511 The non-nil value returned can be t for truly invisible text or something
22512 else if the text is replaced by an ellipsis. */)
22513 (Lisp_Object pos_or_prop)
22514 {
22515 Lisp_Object prop
22516 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22517 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22518 : pos_or_prop);
22519 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22520 return (invis == 0 ? Qnil
22521 : invis == 1 ? Qt
22522 : make_number (invis));
22523 }
22524
22525 /* Calculate a width or height in pixels from a specification using
22526 the following elements:
22527
22528 SPEC ::=
22529 NUM - a (fractional) multiple of the default font width/height
22530 (NUM) - specifies exactly NUM pixels
22531 UNIT - a fixed number of pixels, see below.
22532 ELEMENT - size of a display element in pixels, see below.
22533 (NUM . SPEC) - equals NUM * SPEC
22534 (+ SPEC SPEC ...) - add pixel values
22535 (- SPEC SPEC ...) - subtract pixel values
22536 (- SPEC) - negate pixel value
22537
22538 NUM ::=
22539 INT or FLOAT - a number constant
22540 SYMBOL - use symbol's (buffer local) variable binding.
22541
22542 UNIT ::=
22543 in - pixels per inch *)
22544 mm - pixels per 1/1000 meter *)
22545 cm - pixels per 1/100 meter *)
22546 width - width of current font in pixels.
22547 height - height of current font in pixels.
22548
22549 *) using the ratio(s) defined in display-pixels-per-inch.
22550
22551 ELEMENT ::=
22552
22553 left-fringe - left fringe width in pixels
22554 right-fringe - right fringe width in pixels
22555
22556 left-margin - left margin width in pixels
22557 right-margin - right margin width in pixels
22558
22559 scroll-bar - scroll-bar area width in pixels
22560
22561 Examples:
22562
22563 Pixels corresponding to 5 inches:
22564 (5 . in)
22565
22566 Total width of non-text areas on left side of window (if scroll-bar is on left):
22567 '(space :width (+ left-fringe left-margin scroll-bar))
22568
22569 Align to first text column (in header line):
22570 '(space :align-to 0)
22571
22572 Align to middle of text area minus half the width of variable `my-image'
22573 containing a loaded image:
22574 '(space :align-to (0.5 . (- text my-image)))
22575
22576 Width of left margin minus width of 1 character in the default font:
22577 '(space :width (- left-margin 1))
22578
22579 Width of left margin minus width of 2 characters in the current font:
22580 '(space :width (- left-margin (2 . width)))
22581
22582 Center 1 character over left-margin (in header line):
22583 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22584
22585 Different ways to express width of left fringe plus left margin minus one pixel:
22586 '(space :width (- (+ left-fringe left-margin) (1)))
22587 '(space :width (+ left-fringe left-margin (- (1))))
22588 '(space :width (+ left-fringe left-margin (-1)))
22589
22590 */
22591
22592 static int
22593 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22594 struct font *font, int width_p, int *align_to)
22595 {
22596 double pixels;
22597
22598 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22599 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22600
22601 if (NILP (prop))
22602 return OK_PIXELS (0);
22603
22604 eassert (FRAME_LIVE_P (it->f));
22605
22606 if (SYMBOLP (prop))
22607 {
22608 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22609 {
22610 char *unit = SSDATA (SYMBOL_NAME (prop));
22611
22612 if (unit[0] == 'i' && unit[1] == 'n')
22613 pixels = 1.0;
22614 else if (unit[0] == 'm' && unit[1] == 'm')
22615 pixels = 25.4;
22616 else if (unit[0] == 'c' && unit[1] == 'm')
22617 pixels = 2.54;
22618 else
22619 pixels = 0;
22620 if (pixels > 0)
22621 {
22622 double ppi = (width_p ? FRAME_RES_X (it->f)
22623 : FRAME_RES_Y (it->f));
22624
22625 if (ppi > 0)
22626 return OK_PIXELS (ppi / pixels);
22627 return 0;
22628 }
22629 }
22630
22631 #ifdef HAVE_WINDOW_SYSTEM
22632 if (EQ (prop, Qheight))
22633 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22634 if (EQ (prop, Qwidth))
22635 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22636 #else
22637 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22638 return OK_PIXELS (1);
22639 #endif
22640
22641 if (EQ (prop, Qtext))
22642 return OK_PIXELS (width_p
22643 ? window_box_width (it->w, TEXT_AREA)
22644 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22645
22646 if (align_to && *align_to < 0)
22647 {
22648 *res = 0;
22649 if (EQ (prop, Qleft))
22650 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22651 if (EQ (prop, Qright))
22652 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22653 if (EQ (prop, Qcenter))
22654 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22655 + window_box_width (it->w, TEXT_AREA) / 2);
22656 if (EQ (prop, Qleft_fringe))
22657 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22658 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22659 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22660 if (EQ (prop, Qright_fringe))
22661 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22662 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22663 : window_box_right_offset (it->w, TEXT_AREA));
22664 if (EQ (prop, Qleft_margin))
22665 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22666 if (EQ (prop, Qright_margin))
22667 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22668 if (EQ (prop, Qscroll_bar))
22669 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22670 ? 0
22671 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22672 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22673 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22674 : 0)));
22675 }
22676 else
22677 {
22678 if (EQ (prop, Qleft_fringe))
22679 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22680 if (EQ (prop, Qright_fringe))
22681 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22682 if (EQ (prop, Qleft_margin))
22683 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22684 if (EQ (prop, Qright_margin))
22685 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22686 if (EQ (prop, Qscroll_bar))
22687 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22688 }
22689
22690 prop = buffer_local_value_1 (prop, it->w->contents);
22691 if (EQ (prop, Qunbound))
22692 prop = Qnil;
22693 }
22694
22695 if (INTEGERP (prop) || FLOATP (prop))
22696 {
22697 int base_unit = (width_p
22698 ? FRAME_COLUMN_WIDTH (it->f)
22699 : FRAME_LINE_HEIGHT (it->f));
22700 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22701 }
22702
22703 if (CONSP (prop))
22704 {
22705 Lisp_Object car = XCAR (prop);
22706 Lisp_Object cdr = XCDR (prop);
22707
22708 if (SYMBOLP (car))
22709 {
22710 #ifdef HAVE_WINDOW_SYSTEM
22711 if (FRAME_WINDOW_P (it->f)
22712 && valid_image_p (prop))
22713 {
22714 ptrdiff_t id = lookup_image (it->f, prop);
22715 struct image *img = IMAGE_FROM_ID (it->f, id);
22716
22717 return OK_PIXELS (width_p ? img->width : img->height);
22718 }
22719 #endif
22720 if (EQ (car, Qplus) || EQ (car, Qminus))
22721 {
22722 int first = 1;
22723 double px;
22724
22725 pixels = 0;
22726 while (CONSP (cdr))
22727 {
22728 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22729 font, width_p, align_to))
22730 return 0;
22731 if (first)
22732 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22733 else
22734 pixels += px;
22735 cdr = XCDR (cdr);
22736 }
22737 if (EQ (car, Qminus))
22738 pixels = -pixels;
22739 return OK_PIXELS (pixels);
22740 }
22741
22742 car = buffer_local_value_1 (car, it->w->contents);
22743 if (EQ (car, Qunbound))
22744 car = Qnil;
22745 }
22746
22747 if (INTEGERP (car) || FLOATP (car))
22748 {
22749 double fact;
22750 pixels = XFLOATINT (car);
22751 if (NILP (cdr))
22752 return OK_PIXELS (pixels);
22753 if (calc_pixel_width_or_height (&fact, it, cdr,
22754 font, width_p, align_to))
22755 return OK_PIXELS (pixels * fact);
22756 return 0;
22757 }
22758
22759 return 0;
22760 }
22761
22762 return 0;
22763 }
22764
22765 \f
22766 /***********************************************************************
22767 Glyph Display
22768 ***********************************************************************/
22769
22770 #ifdef HAVE_WINDOW_SYSTEM
22771
22772 #ifdef GLYPH_DEBUG
22773
22774 void
22775 dump_glyph_string (struct glyph_string *s)
22776 {
22777 fprintf (stderr, "glyph string\n");
22778 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22779 s->x, s->y, s->width, s->height);
22780 fprintf (stderr, " ybase = %d\n", s->ybase);
22781 fprintf (stderr, " hl = %d\n", s->hl);
22782 fprintf (stderr, " left overhang = %d, right = %d\n",
22783 s->left_overhang, s->right_overhang);
22784 fprintf (stderr, " nchars = %d\n", s->nchars);
22785 fprintf (stderr, " extends to end of line = %d\n",
22786 s->extends_to_end_of_line_p);
22787 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22788 fprintf (stderr, " bg width = %d\n", s->background_width);
22789 }
22790
22791 #endif /* GLYPH_DEBUG */
22792
22793 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22794 of XChar2b structures for S; it can't be allocated in
22795 init_glyph_string because it must be allocated via `alloca'. W
22796 is the window on which S is drawn. ROW and AREA are the glyph row
22797 and area within the row from which S is constructed. START is the
22798 index of the first glyph structure covered by S. HL is a
22799 face-override for drawing S. */
22800
22801 #ifdef HAVE_NTGUI
22802 #define OPTIONAL_HDC(hdc) HDC hdc,
22803 #define DECLARE_HDC(hdc) HDC hdc;
22804 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22805 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22806 #endif
22807
22808 #ifndef OPTIONAL_HDC
22809 #define OPTIONAL_HDC(hdc)
22810 #define DECLARE_HDC(hdc)
22811 #define ALLOCATE_HDC(hdc, f)
22812 #define RELEASE_HDC(hdc, f)
22813 #endif
22814
22815 static void
22816 init_glyph_string (struct glyph_string *s,
22817 OPTIONAL_HDC (hdc)
22818 XChar2b *char2b, struct window *w, struct glyph_row *row,
22819 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22820 {
22821 memset (s, 0, sizeof *s);
22822 s->w = w;
22823 s->f = XFRAME (w->frame);
22824 #ifdef HAVE_NTGUI
22825 s->hdc = hdc;
22826 #endif
22827 s->display = FRAME_X_DISPLAY (s->f);
22828 s->window = FRAME_X_WINDOW (s->f);
22829 s->char2b = char2b;
22830 s->hl = hl;
22831 s->row = row;
22832 s->area = area;
22833 s->first_glyph = row->glyphs[area] + start;
22834 s->height = row->height;
22835 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22836 s->ybase = s->y + row->ascent;
22837 }
22838
22839
22840 /* Append the list of glyph strings with head H and tail T to the list
22841 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22842
22843 static void
22844 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22845 struct glyph_string *h, struct glyph_string *t)
22846 {
22847 if (h)
22848 {
22849 if (*head)
22850 (*tail)->next = h;
22851 else
22852 *head = h;
22853 h->prev = *tail;
22854 *tail = t;
22855 }
22856 }
22857
22858
22859 /* Prepend the list of glyph strings with head H and tail T to the
22860 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22861 result. */
22862
22863 static void
22864 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22865 struct glyph_string *h, struct glyph_string *t)
22866 {
22867 if (h)
22868 {
22869 if (*head)
22870 (*head)->prev = t;
22871 else
22872 *tail = t;
22873 t->next = *head;
22874 *head = h;
22875 }
22876 }
22877
22878
22879 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22880 Set *HEAD and *TAIL to the resulting list. */
22881
22882 static void
22883 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22884 struct glyph_string *s)
22885 {
22886 s->next = s->prev = NULL;
22887 append_glyph_string_lists (head, tail, s, s);
22888 }
22889
22890
22891 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22892 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22893 make sure that X resources for the face returned are allocated.
22894 Value is a pointer to a realized face that is ready for display if
22895 DISPLAY_P is non-zero. */
22896
22897 static struct face *
22898 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22899 XChar2b *char2b, int display_p)
22900 {
22901 struct face *face = FACE_FROM_ID (f, face_id);
22902 unsigned code = 0;
22903
22904 if (face->font)
22905 {
22906 code = face->font->driver->encode_char (face->font, c);
22907
22908 if (code == FONT_INVALID_CODE)
22909 code = 0;
22910 }
22911 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22912
22913 /* Make sure X resources of the face are allocated. */
22914 #ifdef HAVE_X_WINDOWS
22915 if (display_p)
22916 #endif
22917 {
22918 eassert (face != NULL);
22919 PREPARE_FACE_FOR_DISPLAY (f, face);
22920 }
22921
22922 return face;
22923 }
22924
22925
22926 /* Get face and two-byte form of character glyph GLYPH on frame F.
22927 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22928 a pointer to a realized face that is ready for display. */
22929
22930 static struct face *
22931 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22932 XChar2b *char2b, int *two_byte_p)
22933 {
22934 struct face *face;
22935 unsigned code = 0;
22936
22937 eassert (glyph->type == CHAR_GLYPH);
22938 face = FACE_FROM_ID (f, glyph->face_id);
22939
22940 /* Make sure X resources of the face are allocated. */
22941 eassert (face != NULL);
22942 PREPARE_FACE_FOR_DISPLAY (f, face);
22943
22944 if (two_byte_p)
22945 *two_byte_p = 0;
22946
22947 if (face->font)
22948 {
22949 if (CHAR_BYTE8_P (glyph->u.ch))
22950 code = CHAR_TO_BYTE8 (glyph->u.ch);
22951 else
22952 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22953
22954 if (code == FONT_INVALID_CODE)
22955 code = 0;
22956 }
22957
22958 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22959 return face;
22960 }
22961
22962
22963 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22964 Return 1 if FONT has a glyph for C, otherwise return 0. */
22965
22966 static int
22967 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22968 {
22969 unsigned code;
22970
22971 if (CHAR_BYTE8_P (c))
22972 code = CHAR_TO_BYTE8 (c);
22973 else
22974 code = font->driver->encode_char (font, c);
22975
22976 if (code == FONT_INVALID_CODE)
22977 return 0;
22978 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22979 return 1;
22980 }
22981
22982
22983 /* Fill glyph string S with composition components specified by S->cmp.
22984
22985 BASE_FACE is the base face of the composition.
22986 S->cmp_from is the index of the first component for S.
22987
22988 OVERLAPS non-zero means S should draw the foreground only, and use
22989 its physical height for clipping. See also draw_glyphs.
22990
22991 Value is the index of a component not in S. */
22992
22993 static int
22994 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
22995 int overlaps)
22996 {
22997 int i;
22998 /* For all glyphs of this composition, starting at the offset
22999 S->cmp_from, until we reach the end of the definition or encounter a
23000 glyph that requires the different face, add it to S. */
23001 struct face *face;
23002
23003 eassert (s);
23004
23005 s->for_overlaps = overlaps;
23006 s->face = NULL;
23007 s->font = NULL;
23008 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
23009 {
23010 int c = COMPOSITION_GLYPH (s->cmp, i);
23011
23012 /* TAB in a composition means display glyphs with padding space
23013 on the left or right. */
23014 if (c != '\t')
23015 {
23016 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
23017 -1, Qnil);
23018
23019 face = get_char_face_and_encoding (s->f, c, face_id,
23020 s->char2b + i, 1);
23021 if (face)
23022 {
23023 if (! s->face)
23024 {
23025 s->face = face;
23026 s->font = s->face->font;
23027 }
23028 else if (s->face != face)
23029 break;
23030 }
23031 }
23032 ++s->nchars;
23033 }
23034 s->cmp_to = i;
23035
23036 if (s->face == NULL)
23037 {
23038 s->face = base_face->ascii_face;
23039 s->font = s->face->font;
23040 }
23041
23042 /* All glyph strings for the same composition has the same width,
23043 i.e. the width set for the first component of the composition. */
23044 s->width = s->first_glyph->pixel_width;
23045
23046 /* If the specified font could not be loaded, use the frame's
23047 default font, but record the fact that we couldn't load it in
23048 the glyph string so that we can draw rectangles for the
23049 characters of the glyph string. */
23050 if (s->font == NULL)
23051 {
23052 s->font_not_found_p = 1;
23053 s->font = FRAME_FONT (s->f);
23054 }
23055
23056 /* Adjust base line for subscript/superscript text. */
23057 s->ybase += s->first_glyph->voffset;
23058
23059 /* This glyph string must always be drawn with 16-bit functions. */
23060 s->two_byte_p = 1;
23061
23062 return s->cmp_to;
23063 }
23064
23065 static int
23066 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
23067 int start, int end, int overlaps)
23068 {
23069 struct glyph *glyph, *last;
23070 Lisp_Object lgstring;
23071 int i;
23072
23073 s->for_overlaps = overlaps;
23074 glyph = s->row->glyphs[s->area] + start;
23075 last = s->row->glyphs[s->area] + end;
23076 s->cmp_id = glyph->u.cmp.id;
23077 s->cmp_from = glyph->slice.cmp.from;
23078 s->cmp_to = glyph->slice.cmp.to + 1;
23079 s->face = FACE_FROM_ID (s->f, face_id);
23080 lgstring = composition_gstring_from_id (s->cmp_id);
23081 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
23082 glyph++;
23083 while (glyph < last
23084 && glyph->u.cmp.automatic
23085 && glyph->u.cmp.id == s->cmp_id
23086 && s->cmp_to == glyph->slice.cmp.from)
23087 s->cmp_to = (glyph++)->slice.cmp.to + 1;
23088
23089 for (i = s->cmp_from; i < s->cmp_to; i++)
23090 {
23091 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
23092 unsigned code = LGLYPH_CODE (lglyph);
23093
23094 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
23095 }
23096 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
23097 return glyph - s->row->glyphs[s->area];
23098 }
23099
23100
23101 /* Fill glyph string S from a sequence glyphs for glyphless characters.
23102 See the comment of fill_glyph_string for arguments.
23103 Value is the index of the first glyph not in S. */
23104
23105
23106 static int
23107 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
23108 int start, int end, int overlaps)
23109 {
23110 struct glyph *glyph, *last;
23111 int voffset;
23112
23113 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
23114 s->for_overlaps = overlaps;
23115 glyph = s->row->glyphs[s->area] + start;
23116 last = s->row->glyphs[s->area] + end;
23117 voffset = glyph->voffset;
23118 s->face = FACE_FROM_ID (s->f, face_id);
23119 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
23120 s->nchars = 1;
23121 s->width = glyph->pixel_width;
23122 glyph++;
23123 while (glyph < last
23124 && glyph->type == GLYPHLESS_GLYPH
23125 && glyph->voffset == voffset
23126 && glyph->face_id == face_id)
23127 {
23128 s->nchars++;
23129 s->width += glyph->pixel_width;
23130 glyph++;
23131 }
23132 s->ybase += voffset;
23133 return glyph - s->row->glyphs[s->area];
23134 }
23135
23136
23137 /* Fill glyph string S from a sequence of character glyphs.
23138
23139 FACE_ID is the face id of the string. START is the index of the
23140 first glyph to consider, END is the index of the last + 1.
23141 OVERLAPS non-zero means S should draw the foreground only, and use
23142 its physical height for clipping. See also draw_glyphs.
23143
23144 Value is the index of the first glyph not in S. */
23145
23146 static int
23147 fill_glyph_string (struct glyph_string *s, int face_id,
23148 int start, int end, int overlaps)
23149 {
23150 struct glyph *glyph, *last;
23151 int voffset;
23152 int glyph_not_available_p;
23153
23154 eassert (s->f == XFRAME (s->w->frame));
23155 eassert (s->nchars == 0);
23156 eassert (start >= 0 && end > start);
23157
23158 s->for_overlaps = overlaps;
23159 glyph = s->row->glyphs[s->area] + start;
23160 last = s->row->glyphs[s->area] + end;
23161 voffset = glyph->voffset;
23162 s->padding_p = glyph->padding_p;
23163 glyph_not_available_p = glyph->glyph_not_available_p;
23164
23165 while (glyph < last
23166 && glyph->type == CHAR_GLYPH
23167 && glyph->voffset == voffset
23168 /* Same face id implies same font, nowadays. */
23169 && glyph->face_id == face_id
23170 && glyph->glyph_not_available_p == glyph_not_available_p)
23171 {
23172 int two_byte_p;
23173
23174 s->face = get_glyph_face_and_encoding (s->f, glyph,
23175 s->char2b + s->nchars,
23176 &two_byte_p);
23177 s->two_byte_p = two_byte_p;
23178 ++s->nchars;
23179 eassert (s->nchars <= end - start);
23180 s->width += glyph->pixel_width;
23181 if (glyph++->padding_p != s->padding_p)
23182 break;
23183 }
23184
23185 s->font = s->face->font;
23186
23187 /* If the specified font could not be loaded, use the frame's font,
23188 but record the fact that we couldn't load it in
23189 S->font_not_found_p so that we can draw rectangles for the
23190 characters of the glyph string. */
23191 if (s->font == NULL || glyph_not_available_p)
23192 {
23193 s->font_not_found_p = 1;
23194 s->font = FRAME_FONT (s->f);
23195 }
23196
23197 /* Adjust base line for subscript/superscript text. */
23198 s->ybase += voffset;
23199
23200 eassert (s->face && s->face->gc);
23201 return glyph - s->row->glyphs[s->area];
23202 }
23203
23204
23205 /* Fill glyph string S from image glyph S->first_glyph. */
23206
23207 static void
23208 fill_image_glyph_string (struct glyph_string *s)
23209 {
23210 eassert (s->first_glyph->type == IMAGE_GLYPH);
23211 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
23212 eassert (s->img);
23213 s->slice = s->first_glyph->slice.img;
23214 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
23215 s->font = s->face->font;
23216 s->width = s->first_glyph->pixel_width;
23217
23218 /* Adjust base line for subscript/superscript text. */
23219 s->ybase += s->first_glyph->voffset;
23220 }
23221
23222
23223 /* Fill glyph string S from a sequence of stretch glyphs.
23224
23225 START is the index of the first glyph to consider,
23226 END is the index of the last + 1.
23227
23228 Value is the index of the first glyph not in S. */
23229
23230 static int
23231 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
23232 {
23233 struct glyph *glyph, *last;
23234 int voffset, face_id;
23235
23236 eassert (s->first_glyph->type == STRETCH_GLYPH);
23237
23238 glyph = s->row->glyphs[s->area] + start;
23239 last = s->row->glyphs[s->area] + end;
23240 face_id = glyph->face_id;
23241 s->face = FACE_FROM_ID (s->f, face_id);
23242 s->font = s->face->font;
23243 s->width = glyph->pixel_width;
23244 s->nchars = 1;
23245 voffset = glyph->voffset;
23246
23247 for (++glyph;
23248 (glyph < last
23249 && glyph->type == STRETCH_GLYPH
23250 && glyph->voffset == voffset
23251 && glyph->face_id == face_id);
23252 ++glyph)
23253 s->width += glyph->pixel_width;
23254
23255 /* Adjust base line for subscript/superscript text. */
23256 s->ybase += voffset;
23257
23258 /* The case that face->gc == 0 is handled when drawing the glyph
23259 string by calling PREPARE_FACE_FOR_DISPLAY. */
23260 eassert (s->face);
23261 return glyph - s->row->glyphs[s->area];
23262 }
23263
23264 static struct font_metrics *
23265 get_per_char_metric (struct font *font, XChar2b *char2b)
23266 {
23267 static struct font_metrics metrics;
23268 unsigned code;
23269
23270 if (! font)
23271 return NULL;
23272 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
23273 if (code == FONT_INVALID_CODE)
23274 return NULL;
23275 font->driver->text_extents (font, &code, 1, &metrics);
23276 return &metrics;
23277 }
23278
23279 /* EXPORT for RIF:
23280 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
23281 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
23282 assumed to be zero. */
23283
23284 void
23285 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
23286 {
23287 *left = *right = 0;
23288
23289 if (glyph->type == CHAR_GLYPH)
23290 {
23291 struct face *face;
23292 XChar2b char2b;
23293 struct font_metrics *pcm;
23294
23295 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
23296 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
23297 {
23298 if (pcm->rbearing > pcm->width)
23299 *right = pcm->rbearing - pcm->width;
23300 if (pcm->lbearing < 0)
23301 *left = -pcm->lbearing;
23302 }
23303 }
23304 else if (glyph->type == COMPOSITE_GLYPH)
23305 {
23306 if (! glyph->u.cmp.automatic)
23307 {
23308 struct composition *cmp = composition_table[glyph->u.cmp.id];
23309
23310 if (cmp->rbearing > cmp->pixel_width)
23311 *right = cmp->rbearing - cmp->pixel_width;
23312 if (cmp->lbearing < 0)
23313 *left = - cmp->lbearing;
23314 }
23315 else
23316 {
23317 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
23318 struct font_metrics metrics;
23319
23320 composition_gstring_width (gstring, glyph->slice.cmp.from,
23321 glyph->slice.cmp.to + 1, &metrics);
23322 if (metrics.rbearing > metrics.width)
23323 *right = metrics.rbearing - metrics.width;
23324 if (metrics.lbearing < 0)
23325 *left = - metrics.lbearing;
23326 }
23327 }
23328 }
23329
23330
23331 /* Return the index of the first glyph preceding glyph string S that
23332 is overwritten by S because of S's left overhang. Value is -1
23333 if no glyphs are overwritten. */
23334
23335 static int
23336 left_overwritten (struct glyph_string *s)
23337 {
23338 int k;
23339
23340 if (s->left_overhang)
23341 {
23342 int x = 0, i;
23343 struct glyph *glyphs = s->row->glyphs[s->area];
23344 int first = s->first_glyph - glyphs;
23345
23346 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
23347 x -= glyphs[i].pixel_width;
23348
23349 k = i + 1;
23350 }
23351 else
23352 k = -1;
23353
23354 return k;
23355 }
23356
23357
23358 /* Return the index of the first glyph preceding glyph string S that
23359 is overwriting S because of its right overhang. Value is -1 if no
23360 glyph in front of S overwrites S. */
23361
23362 static int
23363 left_overwriting (struct glyph_string *s)
23364 {
23365 int i, k, x;
23366 struct glyph *glyphs = s->row->glyphs[s->area];
23367 int first = s->first_glyph - glyphs;
23368
23369 k = -1;
23370 x = 0;
23371 for (i = first - 1; i >= 0; --i)
23372 {
23373 int left, right;
23374 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23375 if (x + right > 0)
23376 k = i;
23377 x -= glyphs[i].pixel_width;
23378 }
23379
23380 return k;
23381 }
23382
23383
23384 /* Return the index of the last glyph following glyph string S that is
23385 overwritten by S because of S's right overhang. Value is -1 if
23386 no such glyph is found. */
23387
23388 static int
23389 right_overwritten (struct glyph_string *s)
23390 {
23391 int k = -1;
23392
23393 if (s->right_overhang)
23394 {
23395 int x = 0, i;
23396 struct glyph *glyphs = s->row->glyphs[s->area];
23397 int first = (s->first_glyph - glyphs
23398 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23399 int end = s->row->used[s->area];
23400
23401 for (i = first; i < end && s->right_overhang > x; ++i)
23402 x += glyphs[i].pixel_width;
23403
23404 k = i;
23405 }
23406
23407 return k;
23408 }
23409
23410
23411 /* Return the index of the last glyph following glyph string S that
23412 overwrites S because of its left overhang. Value is negative
23413 if no such glyph is found. */
23414
23415 static int
23416 right_overwriting (struct glyph_string *s)
23417 {
23418 int i, k, x;
23419 int end = s->row->used[s->area];
23420 struct glyph *glyphs = s->row->glyphs[s->area];
23421 int first = (s->first_glyph - glyphs
23422 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23423
23424 k = -1;
23425 x = 0;
23426 for (i = first; i < end; ++i)
23427 {
23428 int left, right;
23429 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23430 if (x - left < 0)
23431 k = i;
23432 x += glyphs[i].pixel_width;
23433 }
23434
23435 return k;
23436 }
23437
23438
23439 /* Set background width of glyph string S. START is the index of the
23440 first glyph following S. LAST_X is the right-most x-position + 1
23441 in the drawing area. */
23442
23443 static void
23444 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23445 {
23446 /* If the face of this glyph string has to be drawn to the end of
23447 the drawing area, set S->extends_to_end_of_line_p. */
23448
23449 if (start == s->row->used[s->area]
23450 && s->area == TEXT_AREA
23451 && ((s->row->fill_line_p
23452 && (s->hl == DRAW_NORMAL_TEXT
23453 || s->hl == DRAW_IMAGE_RAISED
23454 || s->hl == DRAW_IMAGE_SUNKEN))
23455 || s->hl == DRAW_MOUSE_FACE))
23456 s->extends_to_end_of_line_p = 1;
23457
23458 /* If S extends its face to the end of the line, set its
23459 background_width to the distance to the right edge of the drawing
23460 area. */
23461 if (s->extends_to_end_of_line_p)
23462 s->background_width = last_x - s->x + 1;
23463 else
23464 s->background_width = s->width;
23465 }
23466
23467
23468 /* Compute overhangs and x-positions for glyph string S and its
23469 predecessors, or successors. X is the starting x-position for S.
23470 BACKWARD_P non-zero means process predecessors. */
23471
23472 static void
23473 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23474 {
23475 if (backward_p)
23476 {
23477 while (s)
23478 {
23479 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23480 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23481 x -= s->width;
23482 s->x = x;
23483 s = s->prev;
23484 }
23485 }
23486 else
23487 {
23488 while (s)
23489 {
23490 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23491 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23492 s->x = x;
23493 x += s->width;
23494 s = s->next;
23495 }
23496 }
23497 }
23498
23499
23500
23501 /* The following macros are only called from draw_glyphs below.
23502 They reference the following parameters of that function directly:
23503 `w', `row', `area', and `overlap_p'
23504 as well as the following local variables:
23505 `s', `f', and `hdc' (in W32) */
23506
23507 #ifdef HAVE_NTGUI
23508 /* On W32, silently add local `hdc' variable to argument list of
23509 init_glyph_string. */
23510 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23511 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23512 #else
23513 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23514 init_glyph_string (s, char2b, w, row, area, start, hl)
23515 #endif
23516
23517 /* Add a glyph string for a stretch glyph to the list of strings
23518 between HEAD and TAIL. START is the index of the stretch glyph in
23519 row area AREA of glyph row ROW. END is the index of the last glyph
23520 in that glyph row area. X is the current output position assigned
23521 to the new glyph string constructed. HL overrides that face of the
23522 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23523 is the right-most x-position of the drawing area. */
23524
23525 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23526 and below -- keep them on one line. */
23527 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23528 do \
23529 { \
23530 s = alloca (sizeof *s); \
23531 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23532 START = fill_stretch_glyph_string (s, START, END); \
23533 append_glyph_string (&HEAD, &TAIL, s); \
23534 s->x = (X); \
23535 } \
23536 while (0)
23537
23538
23539 /* Add a glyph string for an image glyph to the list of strings
23540 between HEAD and TAIL. START is the index of the image glyph in
23541 row area AREA of glyph row ROW. END is the index of the last glyph
23542 in that glyph row area. X is the current output position assigned
23543 to the new glyph string constructed. HL overrides that face of the
23544 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23545 is the right-most x-position of the drawing area. */
23546
23547 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23548 do \
23549 { \
23550 s = alloca (sizeof *s); \
23551 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23552 fill_image_glyph_string (s); \
23553 append_glyph_string (&HEAD, &TAIL, s); \
23554 ++START; \
23555 s->x = (X); \
23556 } \
23557 while (0)
23558
23559
23560 /* Add a glyph string for a sequence of character glyphs to the list
23561 of strings between HEAD and TAIL. START is the index of the first
23562 glyph in row area AREA of glyph row ROW that is part of the new
23563 glyph string. END is the index of the last glyph in that glyph row
23564 area. X is the current output position assigned to the new glyph
23565 string constructed. HL overrides that face of the glyph; e.g. it
23566 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23567 right-most x-position of the drawing area. */
23568
23569 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23570 do \
23571 { \
23572 int face_id; \
23573 XChar2b *char2b; \
23574 \
23575 face_id = (row)->glyphs[area][START].face_id; \
23576 \
23577 s = alloca (sizeof *s); \
23578 char2b = alloca ((END - START) * sizeof *char2b); \
23579 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23580 append_glyph_string (&HEAD, &TAIL, s); \
23581 s->x = (X); \
23582 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23583 } \
23584 while (0)
23585
23586
23587 /* Add a glyph string for a composite sequence to the list of strings
23588 between HEAD and TAIL. START is the index of the first glyph in
23589 row area AREA of glyph row ROW that is part of the new glyph
23590 string. END is the index of the last glyph in that glyph row area.
23591 X is the current output position assigned to the new glyph string
23592 constructed. HL overrides that face of the glyph; e.g. it is
23593 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23594 x-position of the drawing area. */
23595
23596 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23597 do { \
23598 int face_id = (row)->glyphs[area][START].face_id; \
23599 struct face *base_face = FACE_FROM_ID (f, face_id); \
23600 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23601 struct composition *cmp = composition_table[cmp_id]; \
23602 XChar2b *char2b; \
23603 struct glyph_string *first_s = NULL; \
23604 int n; \
23605 \
23606 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23607 \
23608 /* Make glyph_strings for each glyph sequence that is drawable by \
23609 the same face, and append them to HEAD/TAIL. */ \
23610 for (n = 0; n < cmp->glyph_len;) \
23611 { \
23612 s = alloca (sizeof *s); \
23613 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23614 append_glyph_string (&(HEAD), &(TAIL), s); \
23615 s->cmp = cmp; \
23616 s->cmp_from = n; \
23617 s->x = (X); \
23618 if (n == 0) \
23619 first_s = s; \
23620 n = fill_composite_glyph_string (s, base_face, overlaps); \
23621 } \
23622 \
23623 ++START; \
23624 s = first_s; \
23625 } while (0)
23626
23627
23628 /* Add a glyph string for a glyph-string sequence to the list of strings
23629 between HEAD and TAIL. */
23630
23631 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23632 do { \
23633 int face_id; \
23634 XChar2b *char2b; \
23635 Lisp_Object gstring; \
23636 \
23637 face_id = (row)->glyphs[area][START].face_id; \
23638 gstring = (composition_gstring_from_id \
23639 ((row)->glyphs[area][START].u.cmp.id)); \
23640 s = alloca (sizeof *s); \
23641 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23642 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23643 append_glyph_string (&(HEAD), &(TAIL), s); \
23644 s->x = (X); \
23645 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23646 } while (0)
23647
23648
23649 /* Add a glyph string for a sequence of glyphless character's glyphs
23650 to the list of strings between HEAD and TAIL. The meanings of
23651 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23652
23653 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23654 do \
23655 { \
23656 int face_id; \
23657 \
23658 face_id = (row)->glyphs[area][START].face_id; \
23659 \
23660 s = alloca (sizeof *s); \
23661 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23662 append_glyph_string (&HEAD, &TAIL, s); \
23663 s->x = (X); \
23664 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23665 overlaps); \
23666 } \
23667 while (0)
23668
23669
23670 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23671 of AREA of glyph row ROW on window W between indices START and END.
23672 HL overrides the face for drawing glyph strings, e.g. it is
23673 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23674 x-positions of the drawing area.
23675
23676 This is an ugly monster macro construct because we must use alloca
23677 to allocate glyph strings (because draw_glyphs can be called
23678 asynchronously). */
23679
23680 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23681 do \
23682 { \
23683 HEAD = TAIL = NULL; \
23684 while (START < END) \
23685 { \
23686 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23687 switch (first_glyph->type) \
23688 { \
23689 case CHAR_GLYPH: \
23690 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23691 HL, X, LAST_X); \
23692 break; \
23693 \
23694 case COMPOSITE_GLYPH: \
23695 if (first_glyph->u.cmp.automatic) \
23696 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23697 HL, X, LAST_X); \
23698 else \
23699 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23700 HL, X, LAST_X); \
23701 break; \
23702 \
23703 case STRETCH_GLYPH: \
23704 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23705 HL, X, LAST_X); \
23706 break; \
23707 \
23708 case IMAGE_GLYPH: \
23709 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23710 HL, X, LAST_X); \
23711 break; \
23712 \
23713 case GLYPHLESS_GLYPH: \
23714 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23715 HL, X, LAST_X); \
23716 break; \
23717 \
23718 default: \
23719 emacs_abort (); \
23720 } \
23721 \
23722 if (s) \
23723 { \
23724 set_glyph_string_background_width (s, START, LAST_X); \
23725 (X) += s->width; \
23726 } \
23727 } \
23728 } while (0)
23729
23730
23731 /* Draw glyphs between START and END in AREA of ROW on window W,
23732 starting at x-position X. X is relative to AREA in W. HL is a
23733 face-override with the following meaning:
23734
23735 DRAW_NORMAL_TEXT draw normally
23736 DRAW_CURSOR draw in cursor face
23737 DRAW_MOUSE_FACE draw in mouse face.
23738 DRAW_INVERSE_VIDEO draw in mode line face
23739 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23740 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23741
23742 If OVERLAPS is non-zero, draw only the foreground of characters and
23743 clip to the physical height of ROW. Non-zero value also defines
23744 the overlapping part to be drawn:
23745
23746 OVERLAPS_PRED overlap with preceding rows
23747 OVERLAPS_SUCC overlap with succeeding rows
23748 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23749 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23750
23751 Value is the x-position reached, relative to AREA of W. */
23752
23753 static int
23754 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23755 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23756 enum draw_glyphs_face hl, int overlaps)
23757 {
23758 struct glyph_string *head, *tail;
23759 struct glyph_string *s;
23760 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23761 int i, j, x_reached, last_x, area_left = 0;
23762 struct frame *f = XFRAME (WINDOW_FRAME (w));
23763 DECLARE_HDC (hdc);
23764
23765 ALLOCATE_HDC (hdc, f);
23766
23767 /* Let's rather be paranoid than getting a SEGV. */
23768 end = min (end, row->used[area]);
23769 start = clip_to_bounds (0, start, end);
23770
23771 /* Translate X to frame coordinates. Set last_x to the right
23772 end of the drawing area. */
23773 if (row->full_width_p)
23774 {
23775 /* X is relative to the left edge of W, without scroll bars
23776 or fringes. */
23777 area_left = WINDOW_LEFT_EDGE_X (w);
23778 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23779 }
23780 else
23781 {
23782 area_left = window_box_left (w, area);
23783 last_x = area_left + window_box_width (w, area);
23784 }
23785 x += area_left;
23786
23787 /* Build a doubly-linked list of glyph_string structures between
23788 head and tail from what we have to draw. Note that the macro
23789 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23790 the reason we use a separate variable `i'. */
23791 i = start;
23792 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23793 if (tail)
23794 x_reached = tail->x + tail->background_width;
23795 else
23796 x_reached = x;
23797
23798 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23799 the row, redraw some glyphs in front or following the glyph
23800 strings built above. */
23801 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23802 {
23803 struct glyph_string *h, *t;
23804 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23805 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23806 int check_mouse_face = 0;
23807 int dummy_x = 0;
23808
23809 /* If mouse highlighting is on, we may need to draw adjacent
23810 glyphs using mouse-face highlighting. */
23811 if (area == TEXT_AREA && row->mouse_face_p
23812 && hlinfo->mouse_face_beg_row >= 0
23813 && hlinfo->mouse_face_end_row >= 0)
23814 {
23815 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
23816
23817 if (row_vpos >= hlinfo->mouse_face_beg_row
23818 && row_vpos <= hlinfo->mouse_face_end_row)
23819 {
23820 check_mouse_face = 1;
23821 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
23822 ? hlinfo->mouse_face_beg_col : 0;
23823 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
23824 ? hlinfo->mouse_face_end_col
23825 : row->used[TEXT_AREA];
23826 }
23827 }
23828
23829 /* Compute overhangs for all glyph strings. */
23830 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23831 for (s = head; s; s = s->next)
23832 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23833
23834 /* Prepend glyph strings for glyphs in front of the first glyph
23835 string that are overwritten because of the first glyph
23836 string's left overhang. The background of all strings
23837 prepended must be drawn because the first glyph string
23838 draws over it. */
23839 i = left_overwritten (head);
23840 if (i >= 0)
23841 {
23842 enum draw_glyphs_face overlap_hl;
23843
23844 /* If this row contains mouse highlighting, attempt to draw
23845 the overlapped glyphs with the correct highlight. This
23846 code fails if the overlap encompasses more than one glyph
23847 and mouse-highlight spans only some of these glyphs.
23848 However, making it work perfectly involves a lot more
23849 code, and I don't know if the pathological case occurs in
23850 practice, so we'll stick to this for now. --- cyd */
23851 if (check_mouse_face
23852 && mouse_beg_col < start && mouse_end_col > i)
23853 overlap_hl = DRAW_MOUSE_FACE;
23854 else
23855 overlap_hl = DRAW_NORMAL_TEXT;
23856
23857 j = i;
23858 BUILD_GLYPH_STRINGS (j, start, h, t,
23859 overlap_hl, dummy_x, last_x);
23860 start = i;
23861 compute_overhangs_and_x (t, head->x, 1);
23862 prepend_glyph_string_lists (&head, &tail, h, t);
23863 clip_head = head;
23864 }
23865
23866 /* Prepend glyph strings for glyphs in front of the first glyph
23867 string that overwrite that glyph string because of their
23868 right overhang. For these strings, only the foreground must
23869 be drawn, because it draws over the glyph string at `head'.
23870 The background must not be drawn because this would overwrite
23871 right overhangs of preceding glyphs for which no glyph
23872 strings exist. */
23873 i = left_overwriting (head);
23874 if (i >= 0)
23875 {
23876 enum draw_glyphs_face overlap_hl;
23877
23878 if (check_mouse_face
23879 && mouse_beg_col < start && mouse_end_col > i)
23880 overlap_hl = DRAW_MOUSE_FACE;
23881 else
23882 overlap_hl = DRAW_NORMAL_TEXT;
23883
23884 clip_head = head;
23885 BUILD_GLYPH_STRINGS (i, start, h, t,
23886 overlap_hl, dummy_x, last_x);
23887 for (s = h; s; s = s->next)
23888 s->background_filled_p = 1;
23889 compute_overhangs_and_x (t, head->x, 1);
23890 prepend_glyph_string_lists (&head, &tail, h, t);
23891 }
23892
23893 /* Append glyphs strings for glyphs following the last glyph
23894 string tail that are overwritten by tail. The background of
23895 these strings has to be drawn because tail's foreground draws
23896 over it. */
23897 i = right_overwritten (tail);
23898 if (i >= 0)
23899 {
23900 enum draw_glyphs_face overlap_hl;
23901
23902 if (check_mouse_face
23903 && mouse_beg_col < i && mouse_end_col > end)
23904 overlap_hl = DRAW_MOUSE_FACE;
23905 else
23906 overlap_hl = DRAW_NORMAL_TEXT;
23907
23908 BUILD_GLYPH_STRINGS (end, i, h, t,
23909 overlap_hl, x, last_x);
23910 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23911 we don't have `end = i;' here. */
23912 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23913 append_glyph_string_lists (&head, &tail, h, t);
23914 clip_tail = tail;
23915 }
23916
23917 /* Append glyph strings for glyphs following the last glyph
23918 string tail that overwrite tail. The foreground of such
23919 glyphs has to be drawn because it writes into the background
23920 of tail. The background must not be drawn because it could
23921 paint over the foreground of following glyphs. */
23922 i = right_overwriting (tail);
23923 if (i >= 0)
23924 {
23925 enum draw_glyphs_face overlap_hl;
23926 if (check_mouse_face
23927 && mouse_beg_col < i && mouse_end_col > end)
23928 overlap_hl = DRAW_MOUSE_FACE;
23929 else
23930 overlap_hl = DRAW_NORMAL_TEXT;
23931
23932 clip_tail = tail;
23933 i++; /* We must include the Ith glyph. */
23934 BUILD_GLYPH_STRINGS (end, i, h, t,
23935 overlap_hl, x, last_x);
23936 for (s = h; s; s = s->next)
23937 s->background_filled_p = 1;
23938 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23939 append_glyph_string_lists (&head, &tail, h, t);
23940 }
23941 if (clip_head || clip_tail)
23942 for (s = head; s; s = s->next)
23943 {
23944 s->clip_head = clip_head;
23945 s->clip_tail = clip_tail;
23946 }
23947 }
23948
23949 /* Draw all strings. */
23950 for (s = head; s; s = s->next)
23951 FRAME_RIF (f)->draw_glyph_string (s);
23952
23953 #ifndef HAVE_NS
23954 /* When focus a sole frame and move horizontally, this sets on_p to 0
23955 causing a failure to erase prev cursor position. */
23956 if (area == TEXT_AREA
23957 && !row->full_width_p
23958 /* When drawing overlapping rows, only the glyph strings'
23959 foreground is drawn, which doesn't erase a cursor
23960 completely. */
23961 && !overlaps)
23962 {
23963 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23964 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23965 : (tail ? tail->x + tail->background_width : x));
23966 x0 -= area_left;
23967 x1 -= area_left;
23968
23969 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23970 row->y, MATRIX_ROW_BOTTOM_Y (row));
23971 }
23972 #endif
23973
23974 /* Value is the x-position up to which drawn, relative to AREA of W.
23975 This doesn't include parts drawn because of overhangs. */
23976 if (row->full_width_p)
23977 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23978 else
23979 x_reached -= area_left;
23980
23981 RELEASE_HDC (hdc, f);
23982
23983 return x_reached;
23984 }
23985
23986 /* Expand row matrix if too narrow. Don't expand if area
23987 is not present. */
23988
23989 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
23990 { \
23991 if (!fonts_changed_p \
23992 && (it->glyph_row->glyphs[area] \
23993 < it->glyph_row->glyphs[area + 1])) \
23994 { \
23995 it->w->ncols_scale_factor++; \
23996 fonts_changed_p = 1; \
23997 } \
23998 }
23999
24000 /* Store one glyph for IT->char_to_display in IT->glyph_row.
24001 Called from x_produce_glyphs when IT->glyph_row is non-null. */
24002
24003 static void
24004 append_glyph (struct it *it)
24005 {
24006 struct glyph *glyph;
24007 enum glyph_row_area area = it->area;
24008
24009 eassert (it->glyph_row);
24010 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
24011
24012 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24013 if (glyph < it->glyph_row->glyphs[area + 1])
24014 {
24015 /* If the glyph row is reversed, we need to prepend the glyph
24016 rather than append it. */
24017 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24018 {
24019 struct glyph *g;
24020
24021 /* Make room for the additional glyph. */
24022 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24023 g[1] = *g;
24024 glyph = it->glyph_row->glyphs[area];
24025 }
24026 glyph->charpos = CHARPOS (it->position);
24027 glyph->object = it->object;
24028 if (it->pixel_width > 0)
24029 {
24030 glyph->pixel_width = it->pixel_width;
24031 glyph->padding_p = 0;
24032 }
24033 else
24034 {
24035 /* Assure at least 1-pixel width. Otherwise, cursor can't
24036 be displayed correctly. */
24037 glyph->pixel_width = 1;
24038 glyph->padding_p = 1;
24039 }
24040 glyph->ascent = it->ascent;
24041 glyph->descent = it->descent;
24042 glyph->voffset = it->voffset;
24043 glyph->type = CHAR_GLYPH;
24044 glyph->avoid_cursor_p = it->avoid_cursor_p;
24045 glyph->multibyte_p = it->multibyte_p;
24046 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24047 {
24048 /* In R2L rows, the left and the right box edges need to be
24049 drawn in reverse direction. */
24050 glyph->right_box_line_p = it->start_of_box_run_p;
24051 glyph->left_box_line_p = it->end_of_box_run_p;
24052 }
24053 else
24054 {
24055 glyph->left_box_line_p = it->start_of_box_run_p;
24056 glyph->right_box_line_p = it->end_of_box_run_p;
24057 }
24058 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24059 || it->phys_descent > it->descent);
24060 glyph->glyph_not_available_p = it->glyph_not_available_p;
24061 glyph->face_id = it->face_id;
24062 glyph->u.ch = it->char_to_display;
24063 glyph->slice.img = null_glyph_slice;
24064 glyph->font_type = FONT_TYPE_UNKNOWN;
24065 if (it->bidi_p)
24066 {
24067 glyph->resolved_level = it->bidi_it.resolved_level;
24068 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24069 emacs_abort ();
24070 glyph->bidi_type = it->bidi_it.type;
24071 }
24072 else
24073 {
24074 glyph->resolved_level = 0;
24075 glyph->bidi_type = UNKNOWN_BT;
24076 }
24077 ++it->glyph_row->used[area];
24078 }
24079 else
24080 IT_EXPAND_MATRIX_WIDTH (it, area);
24081 }
24082
24083 /* Store one glyph for the composition IT->cmp_it.id in
24084 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
24085 non-null. */
24086
24087 static void
24088 append_composite_glyph (struct it *it)
24089 {
24090 struct glyph *glyph;
24091 enum glyph_row_area area = it->area;
24092
24093 eassert (it->glyph_row);
24094
24095 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24096 if (glyph < it->glyph_row->glyphs[area + 1])
24097 {
24098 /* If the glyph row is reversed, we need to prepend the glyph
24099 rather than append it. */
24100 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
24101 {
24102 struct glyph *g;
24103
24104 /* Make room for the new glyph. */
24105 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
24106 g[1] = *g;
24107 glyph = it->glyph_row->glyphs[it->area];
24108 }
24109 glyph->charpos = it->cmp_it.charpos;
24110 glyph->object = it->object;
24111 glyph->pixel_width = it->pixel_width;
24112 glyph->ascent = it->ascent;
24113 glyph->descent = it->descent;
24114 glyph->voffset = it->voffset;
24115 glyph->type = COMPOSITE_GLYPH;
24116 if (it->cmp_it.ch < 0)
24117 {
24118 glyph->u.cmp.automatic = 0;
24119 glyph->u.cmp.id = it->cmp_it.id;
24120 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
24121 }
24122 else
24123 {
24124 glyph->u.cmp.automatic = 1;
24125 glyph->u.cmp.id = it->cmp_it.id;
24126 glyph->slice.cmp.from = it->cmp_it.from;
24127 glyph->slice.cmp.to = it->cmp_it.to - 1;
24128 }
24129 glyph->avoid_cursor_p = it->avoid_cursor_p;
24130 glyph->multibyte_p = it->multibyte_p;
24131 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24132 {
24133 /* In R2L rows, the left and the right box edges need to be
24134 drawn in reverse direction. */
24135 glyph->right_box_line_p = it->start_of_box_run_p;
24136 glyph->left_box_line_p = it->end_of_box_run_p;
24137 }
24138 else
24139 {
24140 glyph->left_box_line_p = it->start_of_box_run_p;
24141 glyph->right_box_line_p = it->end_of_box_run_p;
24142 }
24143 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24144 || it->phys_descent > it->descent);
24145 glyph->padding_p = 0;
24146 glyph->glyph_not_available_p = 0;
24147 glyph->face_id = it->face_id;
24148 glyph->font_type = FONT_TYPE_UNKNOWN;
24149 if (it->bidi_p)
24150 {
24151 glyph->resolved_level = it->bidi_it.resolved_level;
24152 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24153 emacs_abort ();
24154 glyph->bidi_type = it->bidi_it.type;
24155 }
24156 ++it->glyph_row->used[area];
24157 }
24158 else
24159 IT_EXPAND_MATRIX_WIDTH (it, area);
24160 }
24161
24162
24163 /* Change IT->ascent and IT->height according to the setting of
24164 IT->voffset. */
24165
24166 static void
24167 take_vertical_position_into_account (struct it *it)
24168 {
24169 if (it->voffset)
24170 {
24171 if (it->voffset < 0)
24172 /* Increase the ascent so that we can display the text higher
24173 in the line. */
24174 it->ascent -= it->voffset;
24175 else
24176 /* Increase the descent so that we can display the text lower
24177 in the line. */
24178 it->descent += it->voffset;
24179 }
24180 }
24181
24182
24183 /* Produce glyphs/get display metrics for the image IT is loaded with.
24184 See the description of struct display_iterator in dispextern.h for
24185 an overview of struct display_iterator. */
24186
24187 static void
24188 produce_image_glyph (struct it *it)
24189 {
24190 struct image *img;
24191 struct face *face;
24192 int glyph_ascent, crop;
24193 struct glyph_slice slice;
24194
24195 eassert (it->what == IT_IMAGE);
24196
24197 face = FACE_FROM_ID (it->f, it->face_id);
24198 eassert (face);
24199 /* Make sure X resources of the face is loaded. */
24200 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24201
24202 if (it->image_id < 0)
24203 {
24204 /* Fringe bitmap. */
24205 it->ascent = it->phys_ascent = 0;
24206 it->descent = it->phys_descent = 0;
24207 it->pixel_width = 0;
24208 it->nglyphs = 0;
24209 return;
24210 }
24211
24212 img = IMAGE_FROM_ID (it->f, it->image_id);
24213 eassert (img);
24214 /* Make sure X resources of the image is loaded. */
24215 prepare_image_for_display (it->f, img);
24216
24217 slice.x = slice.y = 0;
24218 slice.width = img->width;
24219 slice.height = img->height;
24220
24221 if (INTEGERP (it->slice.x))
24222 slice.x = XINT (it->slice.x);
24223 else if (FLOATP (it->slice.x))
24224 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
24225
24226 if (INTEGERP (it->slice.y))
24227 slice.y = XINT (it->slice.y);
24228 else if (FLOATP (it->slice.y))
24229 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
24230
24231 if (INTEGERP (it->slice.width))
24232 slice.width = XINT (it->slice.width);
24233 else if (FLOATP (it->slice.width))
24234 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
24235
24236 if (INTEGERP (it->slice.height))
24237 slice.height = XINT (it->slice.height);
24238 else if (FLOATP (it->slice.height))
24239 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
24240
24241 if (slice.x >= img->width)
24242 slice.x = img->width;
24243 if (slice.y >= img->height)
24244 slice.y = img->height;
24245 if (slice.x + slice.width >= img->width)
24246 slice.width = img->width - slice.x;
24247 if (slice.y + slice.height > img->height)
24248 slice.height = img->height - slice.y;
24249
24250 if (slice.width == 0 || slice.height == 0)
24251 return;
24252
24253 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
24254
24255 it->descent = slice.height - glyph_ascent;
24256 if (slice.y == 0)
24257 it->descent += img->vmargin;
24258 if (slice.y + slice.height == img->height)
24259 it->descent += img->vmargin;
24260 it->phys_descent = it->descent;
24261
24262 it->pixel_width = slice.width;
24263 if (slice.x == 0)
24264 it->pixel_width += img->hmargin;
24265 if (slice.x + slice.width == img->width)
24266 it->pixel_width += img->hmargin;
24267
24268 /* It's quite possible for images to have an ascent greater than
24269 their height, so don't get confused in that case. */
24270 if (it->descent < 0)
24271 it->descent = 0;
24272
24273 it->nglyphs = 1;
24274
24275 if (face->box != FACE_NO_BOX)
24276 {
24277 if (face->box_line_width > 0)
24278 {
24279 if (slice.y == 0)
24280 it->ascent += face->box_line_width;
24281 if (slice.y + slice.height == img->height)
24282 it->descent += face->box_line_width;
24283 }
24284
24285 if (it->start_of_box_run_p && slice.x == 0)
24286 it->pixel_width += eabs (face->box_line_width);
24287 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
24288 it->pixel_width += eabs (face->box_line_width);
24289 }
24290
24291 take_vertical_position_into_account (it);
24292
24293 /* Automatically crop wide image glyphs at right edge so we can
24294 draw the cursor on same display row. */
24295 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
24296 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
24297 {
24298 it->pixel_width -= crop;
24299 slice.width -= crop;
24300 }
24301
24302 if (it->glyph_row)
24303 {
24304 struct glyph *glyph;
24305 enum glyph_row_area area = it->area;
24306
24307 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24308 if (glyph < it->glyph_row->glyphs[area + 1])
24309 {
24310 glyph->charpos = CHARPOS (it->position);
24311 glyph->object = it->object;
24312 glyph->pixel_width = it->pixel_width;
24313 glyph->ascent = glyph_ascent;
24314 glyph->descent = it->descent;
24315 glyph->voffset = it->voffset;
24316 glyph->type = IMAGE_GLYPH;
24317 glyph->avoid_cursor_p = it->avoid_cursor_p;
24318 glyph->multibyte_p = it->multibyte_p;
24319 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24320 {
24321 /* In R2L rows, the left and the right box edges need to be
24322 drawn in reverse direction. */
24323 glyph->right_box_line_p = it->start_of_box_run_p;
24324 glyph->left_box_line_p = it->end_of_box_run_p;
24325 }
24326 else
24327 {
24328 glyph->left_box_line_p = it->start_of_box_run_p;
24329 glyph->right_box_line_p = it->end_of_box_run_p;
24330 }
24331 glyph->overlaps_vertically_p = 0;
24332 glyph->padding_p = 0;
24333 glyph->glyph_not_available_p = 0;
24334 glyph->face_id = it->face_id;
24335 glyph->u.img_id = img->id;
24336 glyph->slice.img = slice;
24337 glyph->font_type = FONT_TYPE_UNKNOWN;
24338 if (it->bidi_p)
24339 {
24340 glyph->resolved_level = it->bidi_it.resolved_level;
24341 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24342 emacs_abort ();
24343 glyph->bidi_type = it->bidi_it.type;
24344 }
24345 ++it->glyph_row->used[area];
24346 }
24347 else
24348 IT_EXPAND_MATRIX_WIDTH (it, area);
24349 }
24350 }
24351
24352
24353 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
24354 of the glyph, WIDTH and HEIGHT are the width and height of the
24355 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
24356
24357 static void
24358 append_stretch_glyph (struct it *it, Lisp_Object object,
24359 int width, int height, int ascent)
24360 {
24361 struct glyph *glyph;
24362 enum glyph_row_area area = it->area;
24363
24364 eassert (ascent >= 0 && ascent <= height);
24365
24366 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24367 if (glyph < it->glyph_row->glyphs[area + 1])
24368 {
24369 /* If the glyph row is reversed, we need to prepend the glyph
24370 rather than append it. */
24371 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24372 {
24373 struct glyph *g;
24374
24375 /* Make room for the additional glyph. */
24376 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24377 g[1] = *g;
24378 glyph = it->glyph_row->glyphs[area];
24379 }
24380 glyph->charpos = CHARPOS (it->position);
24381 glyph->object = object;
24382 glyph->pixel_width = width;
24383 glyph->ascent = ascent;
24384 glyph->descent = height - ascent;
24385 glyph->voffset = it->voffset;
24386 glyph->type = STRETCH_GLYPH;
24387 glyph->avoid_cursor_p = it->avoid_cursor_p;
24388 glyph->multibyte_p = it->multibyte_p;
24389 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24390 {
24391 /* In R2L rows, the left and the right box edges need to be
24392 drawn in reverse direction. */
24393 glyph->right_box_line_p = it->start_of_box_run_p;
24394 glyph->left_box_line_p = it->end_of_box_run_p;
24395 }
24396 else
24397 {
24398 glyph->left_box_line_p = it->start_of_box_run_p;
24399 glyph->right_box_line_p = it->end_of_box_run_p;
24400 }
24401 glyph->overlaps_vertically_p = 0;
24402 glyph->padding_p = 0;
24403 glyph->glyph_not_available_p = 0;
24404 glyph->face_id = it->face_id;
24405 glyph->u.stretch.ascent = ascent;
24406 glyph->u.stretch.height = height;
24407 glyph->slice.img = null_glyph_slice;
24408 glyph->font_type = FONT_TYPE_UNKNOWN;
24409 if (it->bidi_p)
24410 {
24411 glyph->resolved_level = it->bidi_it.resolved_level;
24412 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24413 emacs_abort ();
24414 glyph->bidi_type = it->bidi_it.type;
24415 }
24416 else
24417 {
24418 glyph->resolved_level = 0;
24419 glyph->bidi_type = UNKNOWN_BT;
24420 }
24421 ++it->glyph_row->used[area];
24422 }
24423 else
24424 IT_EXPAND_MATRIX_WIDTH (it, area);
24425 }
24426
24427 #endif /* HAVE_WINDOW_SYSTEM */
24428
24429 /* Produce a stretch glyph for iterator IT. IT->object is the value
24430 of the glyph property displayed. The value must be a list
24431 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24432 being recognized:
24433
24434 1. `:width WIDTH' specifies that the space should be WIDTH *
24435 canonical char width wide. WIDTH may be an integer or floating
24436 point number.
24437
24438 2. `:relative-width FACTOR' specifies that the width of the stretch
24439 should be computed from the width of the first character having the
24440 `glyph' property, and should be FACTOR times that width.
24441
24442 3. `:align-to HPOS' specifies that the space should be wide enough
24443 to reach HPOS, a value in canonical character units.
24444
24445 Exactly one of the above pairs must be present.
24446
24447 4. `:height HEIGHT' specifies that the height of the stretch produced
24448 should be HEIGHT, measured in canonical character units.
24449
24450 5. `:relative-height FACTOR' specifies that the height of the
24451 stretch should be FACTOR times the height of the characters having
24452 the glyph property.
24453
24454 Either none or exactly one of 4 or 5 must be present.
24455
24456 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24457 of the stretch should be used for the ascent of the stretch.
24458 ASCENT must be in the range 0 <= ASCENT <= 100. */
24459
24460 void
24461 produce_stretch_glyph (struct it *it)
24462 {
24463 /* (space :width WIDTH :height HEIGHT ...) */
24464 Lisp_Object prop, plist;
24465 int width = 0, height = 0, align_to = -1;
24466 int zero_width_ok_p = 0;
24467 double tem;
24468 struct font *font = NULL;
24469
24470 #ifdef HAVE_WINDOW_SYSTEM
24471 int ascent = 0;
24472 int zero_height_ok_p = 0;
24473
24474 if (FRAME_WINDOW_P (it->f))
24475 {
24476 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24477 font = face->font ? face->font : FRAME_FONT (it->f);
24478 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24479 }
24480 #endif
24481
24482 /* List should start with `space'. */
24483 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24484 plist = XCDR (it->object);
24485
24486 /* Compute the width of the stretch. */
24487 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24488 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24489 {
24490 /* Absolute width `:width WIDTH' specified and valid. */
24491 zero_width_ok_p = 1;
24492 width = (int)tem;
24493 }
24494 #ifdef HAVE_WINDOW_SYSTEM
24495 else if (FRAME_WINDOW_P (it->f)
24496 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24497 {
24498 /* Relative width `:relative-width FACTOR' specified and valid.
24499 Compute the width of the characters having the `glyph'
24500 property. */
24501 struct it it2;
24502 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24503
24504 it2 = *it;
24505 if (it->multibyte_p)
24506 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24507 else
24508 {
24509 it2.c = it2.char_to_display = *p, it2.len = 1;
24510 if (! ASCII_CHAR_P (it2.c))
24511 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24512 }
24513
24514 it2.glyph_row = NULL;
24515 it2.what = IT_CHARACTER;
24516 x_produce_glyphs (&it2);
24517 width = NUMVAL (prop) * it2.pixel_width;
24518 }
24519 #endif /* HAVE_WINDOW_SYSTEM */
24520 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24521 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24522 {
24523 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24524 align_to = (align_to < 0
24525 ? 0
24526 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24527 else if (align_to < 0)
24528 align_to = window_box_left_offset (it->w, TEXT_AREA);
24529 width = max (0, (int)tem + align_to - it->current_x);
24530 zero_width_ok_p = 1;
24531 }
24532 else
24533 /* Nothing specified -> width defaults to canonical char width. */
24534 width = FRAME_COLUMN_WIDTH (it->f);
24535
24536 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24537 width = 1;
24538
24539 #ifdef HAVE_WINDOW_SYSTEM
24540 /* Compute height. */
24541 if (FRAME_WINDOW_P (it->f))
24542 {
24543 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24544 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24545 {
24546 height = (int)tem;
24547 zero_height_ok_p = 1;
24548 }
24549 else if (prop = Fplist_get (plist, QCrelative_height),
24550 NUMVAL (prop) > 0)
24551 height = FONT_HEIGHT (font) * NUMVAL (prop);
24552 else
24553 height = FONT_HEIGHT (font);
24554
24555 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24556 height = 1;
24557
24558 /* Compute percentage of height used for ascent. If
24559 `:ascent ASCENT' is present and valid, use that. Otherwise,
24560 derive the ascent from the font in use. */
24561 if (prop = Fplist_get (plist, QCascent),
24562 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24563 ascent = height * NUMVAL (prop) / 100.0;
24564 else if (!NILP (prop)
24565 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24566 ascent = min (max (0, (int)tem), height);
24567 else
24568 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24569 }
24570 else
24571 #endif /* HAVE_WINDOW_SYSTEM */
24572 height = 1;
24573
24574 if (width > 0 && it->line_wrap != TRUNCATE
24575 && it->current_x + width > it->last_visible_x)
24576 {
24577 width = it->last_visible_x - it->current_x;
24578 #ifdef HAVE_WINDOW_SYSTEM
24579 /* Subtract one more pixel from the stretch width, but only on
24580 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24581 width -= FRAME_WINDOW_P (it->f);
24582 #endif
24583 }
24584
24585 if (width > 0 && height > 0 && it->glyph_row)
24586 {
24587 Lisp_Object o_object = it->object;
24588 Lisp_Object object = it->stack[it->sp - 1].string;
24589 int n = width;
24590
24591 if (!STRINGP (object))
24592 object = it->w->contents;
24593 #ifdef HAVE_WINDOW_SYSTEM
24594 if (FRAME_WINDOW_P (it->f))
24595 append_stretch_glyph (it, object, width, height, ascent);
24596 else
24597 #endif
24598 {
24599 it->object = object;
24600 it->char_to_display = ' ';
24601 it->pixel_width = it->len = 1;
24602 while (n--)
24603 tty_append_glyph (it);
24604 it->object = o_object;
24605 }
24606 }
24607
24608 it->pixel_width = width;
24609 #ifdef HAVE_WINDOW_SYSTEM
24610 if (FRAME_WINDOW_P (it->f))
24611 {
24612 it->ascent = it->phys_ascent = ascent;
24613 it->descent = it->phys_descent = height - it->ascent;
24614 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24615 take_vertical_position_into_account (it);
24616 }
24617 else
24618 #endif
24619 it->nglyphs = width;
24620 }
24621
24622 /* Get information about special display element WHAT in an
24623 environment described by IT. WHAT is one of IT_TRUNCATION or
24624 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24625 non-null glyph_row member. This function ensures that fields like
24626 face_id, c, len of IT are left untouched. */
24627
24628 static void
24629 produce_special_glyphs (struct it *it, enum display_element_type what)
24630 {
24631 struct it temp_it;
24632 Lisp_Object gc;
24633 GLYPH glyph;
24634
24635 temp_it = *it;
24636 temp_it.object = make_number (0);
24637 memset (&temp_it.current, 0, sizeof temp_it.current);
24638
24639 if (what == IT_CONTINUATION)
24640 {
24641 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24642 if (it->bidi_it.paragraph_dir == R2L)
24643 SET_GLYPH_FROM_CHAR (glyph, '/');
24644 else
24645 SET_GLYPH_FROM_CHAR (glyph, '\\');
24646 if (it->dp
24647 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24648 {
24649 /* FIXME: Should we mirror GC for R2L lines? */
24650 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24651 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24652 }
24653 }
24654 else if (what == IT_TRUNCATION)
24655 {
24656 /* Truncation glyph. */
24657 SET_GLYPH_FROM_CHAR (glyph, '$');
24658 if (it->dp
24659 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24660 {
24661 /* FIXME: Should we mirror GC for R2L lines? */
24662 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24663 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24664 }
24665 }
24666 else
24667 emacs_abort ();
24668
24669 #ifdef HAVE_WINDOW_SYSTEM
24670 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24671 is turned off, we precede the truncation/continuation glyphs by a
24672 stretch glyph whose width is computed such that these special
24673 glyphs are aligned at the window margin, even when very different
24674 fonts are used in different glyph rows. */
24675 if (FRAME_WINDOW_P (temp_it.f)
24676 /* init_iterator calls this with it->glyph_row == NULL, and it
24677 wants only the pixel width of the truncation/continuation
24678 glyphs. */
24679 && temp_it.glyph_row
24680 /* insert_left_trunc_glyphs calls us at the beginning of the
24681 row, and it has its own calculation of the stretch glyph
24682 width. */
24683 && temp_it.glyph_row->used[TEXT_AREA] > 0
24684 && (temp_it.glyph_row->reversed_p
24685 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24686 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24687 {
24688 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24689
24690 if (stretch_width > 0)
24691 {
24692 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24693 struct font *font =
24694 face->font ? face->font : FRAME_FONT (temp_it.f);
24695 int stretch_ascent =
24696 (((temp_it.ascent + temp_it.descent)
24697 * FONT_BASE (font)) / FONT_HEIGHT (font));
24698
24699 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
24700 temp_it.ascent + temp_it.descent,
24701 stretch_ascent);
24702 }
24703 }
24704 #endif
24705
24706 temp_it.dp = NULL;
24707 temp_it.what = IT_CHARACTER;
24708 temp_it.len = 1;
24709 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24710 temp_it.face_id = GLYPH_FACE (glyph);
24711 temp_it.len = CHAR_BYTES (temp_it.c);
24712
24713 PRODUCE_GLYPHS (&temp_it);
24714 it->pixel_width = temp_it.pixel_width;
24715 it->nglyphs = temp_it.pixel_width;
24716 }
24717
24718 #ifdef HAVE_WINDOW_SYSTEM
24719
24720 /* Calculate line-height and line-spacing properties.
24721 An integer value specifies explicit pixel value.
24722 A float value specifies relative value to current face height.
24723 A cons (float . face-name) specifies relative value to
24724 height of specified face font.
24725
24726 Returns height in pixels, or nil. */
24727
24728
24729 static Lisp_Object
24730 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24731 int boff, int override)
24732 {
24733 Lisp_Object face_name = Qnil;
24734 int ascent, descent, height;
24735
24736 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24737 return val;
24738
24739 if (CONSP (val))
24740 {
24741 face_name = XCAR (val);
24742 val = XCDR (val);
24743 if (!NUMBERP (val))
24744 val = make_number (1);
24745 if (NILP (face_name))
24746 {
24747 height = it->ascent + it->descent;
24748 goto scale;
24749 }
24750 }
24751
24752 if (NILP (face_name))
24753 {
24754 font = FRAME_FONT (it->f);
24755 boff = FRAME_BASELINE_OFFSET (it->f);
24756 }
24757 else if (EQ (face_name, Qt))
24758 {
24759 override = 0;
24760 }
24761 else
24762 {
24763 int face_id;
24764 struct face *face;
24765
24766 face_id = lookup_named_face (it->f, face_name, 0);
24767 if (face_id < 0)
24768 return make_number (-1);
24769
24770 face = FACE_FROM_ID (it->f, face_id);
24771 font = face->font;
24772 if (font == NULL)
24773 return make_number (-1);
24774 boff = font->baseline_offset;
24775 if (font->vertical_centering)
24776 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24777 }
24778
24779 ascent = FONT_BASE (font) + boff;
24780 descent = FONT_DESCENT (font) - boff;
24781
24782 if (override)
24783 {
24784 it->override_ascent = ascent;
24785 it->override_descent = descent;
24786 it->override_boff = boff;
24787 }
24788
24789 height = ascent + descent;
24790
24791 scale:
24792 if (FLOATP (val))
24793 height = (int)(XFLOAT_DATA (val) * height);
24794 else if (INTEGERP (val))
24795 height *= XINT (val);
24796
24797 return make_number (height);
24798 }
24799
24800
24801 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24802 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24803 and only if this is for a character for which no font was found.
24804
24805 If the display method (it->glyphless_method) is
24806 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24807 length of the acronym or the hexadecimal string, UPPER_XOFF and
24808 UPPER_YOFF are pixel offsets for the upper part of the string,
24809 LOWER_XOFF and LOWER_YOFF are for the lower part.
24810
24811 For the other display methods, LEN through LOWER_YOFF are zero. */
24812
24813 static void
24814 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24815 short upper_xoff, short upper_yoff,
24816 short lower_xoff, short lower_yoff)
24817 {
24818 struct glyph *glyph;
24819 enum glyph_row_area area = it->area;
24820
24821 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24822 if (glyph < it->glyph_row->glyphs[area + 1])
24823 {
24824 /* If the glyph row is reversed, we need to prepend the glyph
24825 rather than append it. */
24826 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24827 {
24828 struct glyph *g;
24829
24830 /* Make room for the additional glyph. */
24831 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24832 g[1] = *g;
24833 glyph = it->glyph_row->glyphs[area];
24834 }
24835 glyph->charpos = CHARPOS (it->position);
24836 glyph->object = it->object;
24837 glyph->pixel_width = it->pixel_width;
24838 glyph->ascent = it->ascent;
24839 glyph->descent = it->descent;
24840 glyph->voffset = it->voffset;
24841 glyph->type = GLYPHLESS_GLYPH;
24842 glyph->u.glyphless.method = it->glyphless_method;
24843 glyph->u.glyphless.for_no_font = for_no_font;
24844 glyph->u.glyphless.len = len;
24845 glyph->u.glyphless.ch = it->c;
24846 glyph->slice.glyphless.upper_xoff = upper_xoff;
24847 glyph->slice.glyphless.upper_yoff = upper_yoff;
24848 glyph->slice.glyphless.lower_xoff = lower_xoff;
24849 glyph->slice.glyphless.lower_yoff = lower_yoff;
24850 glyph->avoid_cursor_p = it->avoid_cursor_p;
24851 glyph->multibyte_p = it->multibyte_p;
24852 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24853 {
24854 /* In R2L rows, the left and the right box edges need to be
24855 drawn in reverse direction. */
24856 glyph->right_box_line_p = it->start_of_box_run_p;
24857 glyph->left_box_line_p = it->end_of_box_run_p;
24858 }
24859 else
24860 {
24861 glyph->left_box_line_p = it->start_of_box_run_p;
24862 glyph->right_box_line_p = it->end_of_box_run_p;
24863 }
24864 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24865 || it->phys_descent > it->descent);
24866 glyph->padding_p = 0;
24867 glyph->glyph_not_available_p = 0;
24868 glyph->face_id = face_id;
24869 glyph->font_type = FONT_TYPE_UNKNOWN;
24870 if (it->bidi_p)
24871 {
24872 glyph->resolved_level = it->bidi_it.resolved_level;
24873 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24874 emacs_abort ();
24875 glyph->bidi_type = it->bidi_it.type;
24876 }
24877 ++it->glyph_row->used[area];
24878 }
24879 else
24880 IT_EXPAND_MATRIX_WIDTH (it, area);
24881 }
24882
24883
24884 /* Produce a glyph for a glyphless character for iterator IT.
24885 IT->glyphless_method specifies which method to use for displaying
24886 the character. See the description of enum
24887 glyphless_display_method in dispextern.h for the detail.
24888
24889 FOR_NO_FONT is nonzero if and only if this is for a character for
24890 which no font was found. ACRONYM, if non-nil, is an acronym string
24891 for the character. */
24892
24893 static void
24894 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24895 {
24896 int face_id;
24897 struct face *face;
24898 struct font *font;
24899 int base_width, base_height, width, height;
24900 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24901 int len;
24902
24903 /* Get the metrics of the base font. We always refer to the current
24904 ASCII face. */
24905 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24906 font = face->font ? face->font : FRAME_FONT (it->f);
24907 it->ascent = FONT_BASE (font) + font->baseline_offset;
24908 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24909 base_height = it->ascent + it->descent;
24910 base_width = font->average_width;
24911
24912 /* Get a face ID for the glyph by utilizing a cache (the same way as
24913 done for `escape-glyph' in get_next_display_element). */
24914 if (it->f == last_glyphless_glyph_frame
24915 && it->face_id == last_glyphless_glyph_face_id)
24916 {
24917 face_id = last_glyphless_glyph_merged_face_id;
24918 }
24919 else
24920 {
24921 /* Merge the `glyphless-char' face into the current face. */
24922 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
24923 last_glyphless_glyph_frame = it->f;
24924 last_glyphless_glyph_face_id = it->face_id;
24925 last_glyphless_glyph_merged_face_id = face_id;
24926 }
24927
24928 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24929 {
24930 it->pixel_width = THIN_SPACE_WIDTH;
24931 len = 0;
24932 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24933 }
24934 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24935 {
24936 width = CHAR_WIDTH (it->c);
24937 if (width == 0)
24938 width = 1;
24939 else if (width > 4)
24940 width = 4;
24941 it->pixel_width = base_width * width;
24942 len = 0;
24943 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24944 }
24945 else
24946 {
24947 char buf[7];
24948 const char *str;
24949 unsigned int code[6];
24950 int upper_len;
24951 int ascent, descent;
24952 struct font_metrics metrics_upper, metrics_lower;
24953
24954 face = FACE_FROM_ID (it->f, face_id);
24955 font = face->font ? face->font : FRAME_FONT (it->f);
24956 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24957
24958 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24959 {
24960 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
24961 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
24962 if (CONSP (acronym))
24963 acronym = XCAR (acronym);
24964 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24965 }
24966 else
24967 {
24968 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
24969 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
24970 str = buf;
24971 }
24972 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
24973 code[len] = font->driver->encode_char (font, str[len]);
24974 upper_len = (len + 1) / 2;
24975 font->driver->text_extents (font, code, upper_len,
24976 &metrics_upper);
24977 font->driver->text_extents (font, code + upper_len, len - upper_len,
24978 &metrics_lower);
24979
24980
24981
24982 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
24983 width = max (metrics_upper.width, metrics_lower.width) + 4;
24984 upper_xoff = upper_yoff = 2; /* the typical case */
24985 if (base_width >= width)
24986 {
24987 /* Align the upper to the left, the lower to the right. */
24988 it->pixel_width = base_width;
24989 lower_xoff = base_width - 2 - metrics_lower.width;
24990 }
24991 else
24992 {
24993 /* Center the shorter one. */
24994 it->pixel_width = width;
24995 if (metrics_upper.width >= metrics_lower.width)
24996 lower_xoff = (width - metrics_lower.width) / 2;
24997 else
24998 {
24999 /* FIXME: This code doesn't look right. It formerly was
25000 missing the "lower_xoff = 0;", which couldn't have
25001 been right since it left lower_xoff uninitialized. */
25002 lower_xoff = 0;
25003 upper_xoff = (width - metrics_upper.width) / 2;
25004 }
25005 }
25006
25007 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
25008 top, bottom, and between upper and lower strings. */
25009 height = (metrics_upper.ascent + metrics_upper.descent
25010 + metrics_lower.ascent + metrics_lower.descent) + 5;
25011 /* Center vertically.
25012 H:base_height, D:base_descent
25013 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
25014
25015 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
25016 descent = D - H/2 + h/2;
25017 lower_yoff = descent - 2 - ld;
25018 upper_yoff = lower_yoff - la - 1 - ud; */
25019 ascent = - (it->descent - (base_height + height + 1) / 2);
25020 descent = it->descent - (base_height - height) / 2;
25021 lower_yoff = descent - 2 - metrics_lower.descent;
25022 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
25023 - metrics_upper.descent);
25024 /* Don't make the height shorter than the base height. */
25025 if (height > base_height)
25026 {
25027 it->ascent = ascent;
25028 it->descent = descent;
25029 }
25030 }
25031
25032 it->phys_ascent = it->ascent;
25033 it->phys_descent = it->descent;
25034 if (it->glyph_row)
25035 append_glyphless_glyph (it, face_id, for_no_font, len,
25036 upper_xoff, upper_yoff,
25037 lower_xoff, lower_yoff);
25038 it->nglyphs = 1;
25039 take_vertical_position_into_account (it);
25040 }
25041
25042
25043 /* RIF:
25044 Produce glyphs/get display metrics for the display element IT is
25045 loaded with. See the description of struct it in dispextern.h
25046 for an overview of struct it. */
25047
25048 void
25049 x_produce_glyphs (struct it *it)
25050 {
25051 int extra_line_spacing = it->extra_line_spacing;
25052
25053 it->glyph_not_available_p = 0;
25054
25055 if (it->what == IT_CHARACTER)
25056 {
25057 XChar2b char2b;
25058 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25059 struct font *font = face->font;
25060 struct font_metrics *pcm = NULL;
25061 int boff; /* baseline offset */
25062
25063 if (font == NULL)
25064 {
25065 /* When no suitable font is found, display this character by
25066 the method specified in the first extra slot of
25067 Vglyphless_char_display. */
25068 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
25069
25070 eassert (it->what == IT_GLYPHLESS);
25071 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
25072 goto done;
25073 }
25074
25075 boff = font->baseline_offset;
25076 if (font->vertical_centering)
25077 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25078
25079 if (it->char_to_display != '\n' && it->char_to_display != '\t')
25080 {
25081 int stretched_p;
25082
25083 it->nglyphs = 1;
25084
25085 if (it->override_ascent >= 0)
25086 {
25087 it->ascent = it->override_ascent;
25088 it->descent = it->override_descent;
25089 boff = it->override_boff;
25090 }
25091 else
25092 {
25093 it->ascent = FONT_BASE (font) + boff;
25094 it->descent = FONT_DESCENT (font) - boff;
25095 }
25096
25097 if (get_char_glyph_code (it->char_to_display, font, &char2b))
25098 {
25099 pcm = get_per_char_metric (font, &char2b);
25100 if (pcm->width == 0
25101 && pcm->rbearing == 0 && pcm->lbearing == 0)
25102 pcm = NULL;
25103 }
25104
25105 if (pcm)
25106 {
25107 it->phys_ascent = pcm->ascent + boff;
25108 it->phys_descent = pcm->descent - boff;
25109 it->pixel_width = pcm->width;
25110 }
25111 else
25112 {
25113 it->glyph_not_available_p = 1;
25114 it->phys_ascent = it->ascent;
25115 it->phys_descent = it->descent;
25116 it->pixel_width = font->space_width;
25117 }
25118
25119 if (it->constrain_row_ascent_descent_p)
25120 {
25121 if (it->descent > it->max_descent)
25122 {
25123 it->ascent += it->descent - it->max_descent;
25124 it->descent = it->max_descent;
25125 }
25126 if (it->ascent > it->max_ascent)
25127 {
25128 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25129 it->ascent = it->max_ascent;
25130 }
25131 it->phys_ascent = min (it->phys_ascent, it->ascent);
25132 it->phys_descent = min (it->phys_descent, it->descent);
25133 extra_line_spacing = 0;
25134 }
25135
25136 /* If this is a space inside a region of text with
25137 `space-width' property, change its width. */
25138 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
25139 if (stretched_p)
25140 it->pixel_width *= XFLOATINT (it->space_width);
25141
25142 /* If face has a box, add the box thickness to the character
25143 height. If character has a box line to the left and/or
25144 right, add the box line width to the character's width. */
25145 if (face->box != FACE_NO_BOX)
25146 {
25147 int thick = face->box_line_width;
25148
25149 if (thick > 0)
25150 {
25151 it->ascent += thick;
25152 it->descent += thick;
25153 }
25154 else
25155 thick = -thick;
25156
25157 if (it->start_of_box_run_p)
25158 it->pixel_width += thick;
25159 if (it->end_of_box_run_p)
25160 it->pixel_width += thick;
25161 }
25162
25163 /* If face has an overline, add the height of the overline
25164 (1 pixel) and a 1 pixel margin to the character height. */
25165 if (face->overline_p)
25166 it->ascent += overline_margin;
25167
25168 if (it->constrain_row_ascent_descent_p)
25169 {
25170 if (it->ascent > it->max_ascent)
25171 it->ascent = it->max_ascent;
25172 if (it->descent > it->max_descent)
25173 it->descent = it->max_descent;
25174 }
25175
25176 take_vertical_position_into_account (it);
25177
25178 /* If we have to actually produce glyphs, do it. */
25179 if (it->glyph_row)
25180 {
25181 if (stretched_p)
25182 {
25183 /* Translate a space with a `space-width' property
25184 into a stretch glyph. */
25185 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
25186 / FONT_HEIGHT (font));
25187 append_stretch_glyph (it, it->object, it->pixel_width,
25188 it->ascent + it->descent, ascent);
25189 }
25190 else
25191 append_glyph (it);
25192
25193 /* If characters with lbearing or rbearing are displayed
25194 in this line, record that fact in a flag of the
25195 glyph row. This is used to optimize X output code. */
25196 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
25197 it->glyph_row->contains_overlapping_glyphs_p = 1;
25198 }
25199 if (! stretched_p && it->pixel_width == 0)
25200 /* We assure that all visible glyphs have at least 1-pixel
25201 width. */
25202 it->pixel_width = 1;
25203 }
25204 else if (it->char_to_display == '\n')
25205 {
25206 /* A newline has no width, but we need the height of the
25207 line. But if previous part of the line sets a height,
25208 don't increase that height */
25209
25210 Lisp_Object height;
25211 Lisp_Object total_height = Qnil;
25212
25213 it->override_ascent = -1;
25214 it->pixel_width = 0;
25215 it->nglyphs = 0;
25216
25217 height = get_it_property (it, Qline_height);
25218 /* Split (line-height total-height) list */
25219 if (CONSP (height)
25220 && CONSP (XCDR (height))
25221 && NILP (XCDR (XCDR (height))))
25222 {
25223 total_height = XCAR (XCDR (height));
25224 height = XCAR (height);
25225 }
25226 height = calc_line_height_property (it, height, font, boff, 1);
25227
25228 if (it->override_ascent >= 0)
25229 {
25230 it->ascent = it->override_ascent;
25231 it->descent = it->override_descent;
25232 boff = it->override_boff;
25233 }
25234 else
25235 {
25236 it->ascent = FONT_BASE (font) + boff;
25237 it->descent = FONT_DESCENT (font) - boff;
25238 }
25239
25240 if (EQ (height, Qt))
25241 {
25242 if (it->descent > it->max_descent)
25243 {
25244 it->ascent += it->descent - it->max_descent;
25245 it->descent = it->max_descent;
25246 }
25247 if (it->ascent > it->max_ascent)
25248 {
25249 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25250 it->ascent = it->max_ascent;
25251 }
25252 it->phys_ascent = min (it->phys_ascent, it->ascent);
25253 it->phys_descent = min (it->phys_descent, it->descent);
25254 it->constrain_row_ascent_descent_p = 1;
25255 extra_line_spacing = 0;
25256 }
25257 else
25258 {
25259 Lisp_Object spacing;
25260
25261 it->phys_ascent = it->ascent;
25262 it->phys_descent = it->descent;
25263
25264 if ((it->max_ascent > 0 || it->max_descent > 0)
25265 && face->box != FACE_NO_BOX
25266 && face->box_line_width > 0)
25267 {
25268 it->ascent += face->box_line_width;
25269 it->descent += face->box_line_width;
25270 }
25271 if (!NILP (height)
25272 && XINT (height) > it->ascent + it->descent)
25273 it->ascent = XINT (height) - it->descent;
25274
25275 if (!NILP (total_height))
25276 spacing = calc_line_height_property (it, total_height, font, boff, 0);
25277 else
25278 {
25279 spacing = get_it_property (it, Qline_spacing);
25280 spacing = calc_line_height_property (it, spacing, font, boff, 0);
25281 }
25282 if (INTEGERP (spacing))
25283 {
25284 extra_line_spacing = XINT (spacing);
25285 if (!NILP (total_height))
25286 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
25287 }
25288 }
25289 }
25290 else /* i.e. (it->char_to_display == '\t') */
25291 {
25292 if (font->space_width > 0)
25293 {
25294 int tab_width = it->tab_width * font->space_width;
25295 int x = it->current_x + it->continuation_lines_width;
25296 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
25297
25298 /* If the distance from the current position to the next tab
25299 stop is less than a space character width, use the
25300 tab stop after that. */
25301 if (next_tab_x - x < font->space_width)
25302 next_tab_x += tab_width;
25303
25304 it->pixel_width = next_tab_x - x;
25305 it->nglyphs = 1;
25306 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
25307 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
25308
25309 if (it->glyph_row)
25310 {
25311 append_stretch_glyph (it, it->object, it->pixel_width,
25312 it->ascent + it->descent, it->ascent);
25313 }
25314 }
25315 else
25316 {
25317 it->pixel_width = 0;
25318 it->nglyphs = 1;
25319 }
25320 }
25321 }
25322 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
25323 {
25324 /* A static composition.
25325
25326 Note: A composition is represented as one glyph in the
25327 glyph matrix. There are no padding glyphs.
25328
25329 Important note: pixel_width, ascent, and descent are the
25330 values of what is drawn by draw_glyphs (i.e. the values of
25331 the overall glyphs composed). */
25332 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25333 int boff; /* baseline offset */
25334 struct composition *cmp = composition_table[it->cmp_it.id];
25335 int glyph_len = cmp->glyph_len;
25336 struct font *font = face->font;
25337
25338 it->nglyphs = 1;
25339
25340 /* If we have not yet calculated pixel size data of glyphs of
25341 the composition for the current face font, calculate them
25342 now. Theoretically, we have to check all fonts for the
25343 glyphs, but that requires much time and memory space. So,
25344 here we check only the font of the first glyph. This may
25345 lead to incorrect display, but it's very rare, and C-l
25346 (recenter-top-bottom) can correct the display anyway. */
25347 if (! cmp->font || cmp->font != font)
25348 {
25349 /* Ascent and descent of the font of the first character
25350 of this composition (adjusted by baseline offset).
25351 Ascent and descent of overall glyphs should not be less
25352 than these, respectively. */
25353 int font_ascent, font_descent, font_height;
25354 /* Bounding box of the overall glyphs. */
25355 int leftmost, rightmost, lowest, highest;
25356 int lbearing, rbearing;
25357 int i, width, ascent, descent;
25358 int left_padded = 0, right_padded = 0;
25359 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
25360 XChar2b char2b;
25361 struct font_metrics *pcm;
25362 int font_not_found_p;
25363 ptrdiff_t pos;
25364
25365 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
25366 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
25367 break;
25368 if (glyph_len < cmp->glyph_len)
25369 right_padded = 1;
25370 for (i = 0; i < glyph_len; i++)
25371 {
25372 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
25373 break;
25374 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25375 }
25376 if (i > 0)
25377 left_padded = 1;
25378
25379 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
25380 : IT_CHARPOS (*it));
25381 /* If no suitable font is found, use the default font. */
25382 font_not_found_p = font == NULL;
25383 if (font_not_found_p)
25384 {
25385 face = face->ascii_face;
25386 font = face->font;
25387 }
25388 boff = font->baseline_offset;
25389 if (font->vertical_centering)
25390 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25391 font_ascent = FONT_BASE (font) + boff;
25392 font_descent = FONT_DESCENT (font) - boff;
25393 font_height = FONT_HEIGHT (font);
25394
25395 cmp->font = font;
25396
25397 pcm = NULL;
25398 if (! font_not_found_p)
25399 {
25400 get_char_face_and_encoding (it->f, c, it->face_id,
25401 &char2b, 0);
25402 pcm = get_per_char_metric (font, &char2b);
25403 }
25404
25405 /* Initialize the bounding box. */
25406 if (pcm)
25407 {
25408 width = cmp->glyph_len > 0 ? pcm->width : 0;
25409 ascent = pcm->ascent;
25410 descent = pcm->descent;
25411 lbearing = pcm->lbearing;
25412 rbearing = pcm->rbearing;
25413 }
25414 else
25415 {
25416 width = cmp->glyph_len > 0 ? font->space_width : 0;
25417 ascent = FONT_BASE (font);
25418 descent = FONT_DESCENT (font);
25419 lbearing = 0;
25420 rbearing = width;
25421 }
25422
25423 rightmost = width;
25424 leftmost = 0;
25425 lowest = - descent + boff;
25426 highest = ascent + boff;
25427
25428 if (! font_not_found_p
25429 && font->default_ascent
25430 && CHAR_TABLE_P (Vuse_default_ascent)
25431 && !NILP (Faref (Vuse_default_ascent,
25432 make_number (it->char_to_display))))
25433 highest = font->default_ascent + boff;
25434
25435 /* Draw the first glyph at the normal position. It may be
25436 shifted to right later if some other glyphs are drawn
25437 at the left. */
25438 cmp->offsets[i * 2] = 0;
25439 cmp->offsets[i * 2 + 1] = boff;
25440 cmp->lbearing = lbearing;
25441 cmp->rbearing = rbearing;
25442
25443 /* Set cmp->offsets for the remaining glyphs. */
25444 for (i++; i < glyph_len; i++)
25445 {
25446 int left, right, btm, top;
25447 int ch = COMPOSITION_GLYPH (cmp, i);
25448 int face_id;
25449 struct face *this_face;
25450
25451 if (ch == '\t')
25452 ch = ' ';
25453 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25454 this_face = FACE_FROM_ID (it->f, face_id);
25455 font = this_face->font;
25456
25457 if (font == NULL)
25458 pcm = NULL;
25459 else
25460 {
25461 get_char_face_and_encoding (it->f, ch, face_id,
25462 &char2b, 0);
25463 pcm = get_per_char_metric (font, &char2b);
25464 }
25465 if (! pcm)
25466 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25467 else
25468 {
25469 width = pcm->width;
25470 ascent = pcm->ascent;
25471 descent = pcm->descent;
25472 lbearing = pcm->lbearing;
25473 rbearing = pcm->rbearing;
25474 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25475 {
25476 /* Relative composition with or without
25477 alternate chars. */
25478 left = (leftmost + rightmost - width) / 2;
25479 btm = - descent + boff;
25480 if (font->relative_compose
25481 && (! CHAR_TABLE_P (Vignore_relative_composition)
25482 || NILP (Faref (Vignore_relative_composition,
25483 make_number (ch)))))
25484 {
25485
25486 if (- descent >= font->relative_compose)
25487 /* One extra pixel between two glyphs. */
25488 btm = highest + 1;
25489 else if (ascent <= 0)
25490 /* One extra pixel between two glyphs. */
25491 btm = lowest - 1 - ascent - descent;
25492 }
25493 }
25494 else
25495 {
25496 /* A composition rule is specified by an integer
25497 value that encodes global and new reference
25498 points (GREF and NREF). GREF and NREF are
25499 specified by numbers as below:
25500
25501 0---1---2 -- ascent
25502 | |
25503 | |
25504 | |
25505 9--10--11 -- center
25506 | |
25507 ---3---4---5--- baseline
25508 | |
25509 6---7---8 -- descent
25510 */
25511 int rule = COMPOSITION_RULE (cmp, i);
25512 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25513
25514 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25515 grefx = gref % 3, nrefx = nref % 3;
25516 grefy = gref / 3, nrefy = nref / 3;
25517 if (xoff)
25518 xoff = font_height * (xoff - 128) / 256;
25519 if (yoff)
25520 yoff = font_height * (yoff - 128) / 256;
25521
25522 left = (leftmost
25523 + grefx * (rightmost - leftmost) / 2
25524 - nrefx * width / 2
25525 + xoff);
25526
25527 btm = ((grefy == 0 ? highest
25528 : grefy == 1 ? 0
25529 : grefy == 2 ? lowest
25530 : (highest + lowest) / 2)
25531 - (nrefy == 0 ? ascent + descent
25532 : nrefy == 1 ? descent - boff
25533 : nrefy == 2 ? 0
25534 : (ascent + descent) / 2)
25535 + yoff);
25536 }
25537
25538 cmp->offsets[i * 2] = left;
25539 cmp->offsets[i * 2 + 1] = btm + descent;
25540
25541 /* Update the bounding box of the overall glyphs. */
25542 if (width > 0)
25543 {
25544 right = left + width;
25545 if (left < leftmost)
25546 leftmost = left;
25547 if (right > rightmost)
25548 rightmost = right;
25549 }
25550 top = btm + descent + ascent;
25551 if (top > highest)
25552 highest = top;
25553 if (btm < lowest)
25554 lowest = btm;
25555
25556 if (cmp->lbearing > left + lbearing)
25557 cmp->lbearing = left + lbearing;
25558 if (cmp->rbearing < left + rbearing)
25559 cmp->rbearing = left + rbearing;
25560 }
25561 }
25562
25563 /* If there are glyphs whose x-offsets are negative,
25564 shift all glyphs to the right and make all x-offsets
25565 non-negative. */
25566 if (leftmost < 0)
25567 {
25568 for (i = 0; i < cmp->glyph_len; i++)
25569 cmp->offsets[i * 2] -= leftmost;
25570 rightmost -= leftmost;
25571 cmp->lbearing -= leftmost;
25572 cmp->rbearing -= leftmost;
25573 }
25574
25575 if (left_padded && cmp->lbearing < 0)
25576 {
25577 for (i = 0; i < cmp->glyph_len; i++)
25578 cmp->offsets[i * 2] -= cmp->lbearing;
25579 rightmost -= cmp->lbearing;
25580 cmp->rbearing -= cmp->lbearing;
25581 cmp->lbearing = 0;
25582 }
25583 if (right_padded && rightmost < cmp->rbearing)
25584 {
25585 rightmost = cmp->rbearing;
25586 }
25587
25588 cmp->pixel_width = rightmost;
25589 cmp->ascent = highest;
25590 cmp->descent = - lowest;
25591 if (cmp->ascent < font_ascent)
25592 cmp->ascent = font_ascent;
25593 if (cmp->descent < font_descent)
25594 cmp->descent = font_descent;
25595 }
25596
25597 if (it->glyph_row
25598 && (cmp->lbearing < 0
25599 || cmp->rbearing > cmp->pixel_width))
25600 it->glyph_row->contains_overlapping_glyphs_p = 1;
25601
25602 it->pixel_width = cmp->pixel_width;
25603 it->ascent = it->phys_ascent = cmp->ascent;
25604 it->descent = it->phys_descent = cmp->descent;
25605 if (face->box != FACE_NO_BOX)
25606 {
25607 int thick = face->box_line_width;
25608
25609 if (thick > 0)
25610 {
25611 it->ascent += thick;
25612 it->descent += thick;
25613 }
25614 else
25615 thick = - thick;
25616
25617 if (it->start_of_box_run_p)
25618 it->pixel_width += thick;
25619 if (it->end_of_box_run_p)
25620 it->pixel_width += thick;
25621 }
25622
25623 /* If face has an overline, add the height of the overline
25624 (1 pixel) and a 1 pixel margin to the character height. */
25625 if (face->overline_p)
25626 it->ascent += overline_margin;
25627
25628 take_vertical_position_into_account (it);
25629 if (it->ascent < 0)
25630 it->ascent = 0;
25631 if (it->descent < 0)
25632 it->descent = 0;
25633
25634 if (it->glyph_row && cmp->glyph_len > 0)
25635 append_composite_glyph (it);
25636 }
25637 else if (it->what == IT_COMPOSITION)
25638 {
25639 /* A dynamic (automatic) composition. */
25640 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25641 Lisp_Object gstring;
25642 struct font_metrics metrics;
25643
25644 it->nglyphs = 1;
25645
25646 gstring = composition_gstring_from_id (it->cmp_it.id);
25647 it->pixel_width
25648 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25649 &metrics);
25650 if (it->glyph_row
25651 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25652 it->glyph_row->contains_overlapping_glyphs_p = 1;
25653 it->ascent = it->phys_ascent = metrics.ascent;
25654 it->descent = it->phys_descent = metrics.descent;
25655 if (face->box != FACE_NO_BOX)
25656 {
25657 int thick = face->box_line_width;
25658
25659 if (thick > 0)
25660 {
25661 it->ascent += thick;
25662 it->descent += thick;
25663 }
25664 else
25665 thick = - thick;
25666
25667 if (it->start_of_box_run_p)
25668 it->pixel_width += thick;
25669 if (it->end_of_box_run_p)
25670 it->pixel_width += thick;
25671 }
25672 /* If face has an overline, add the height of the overline
25673 (1 pixel) and a 1 pixel margin to the character height. */
25674 if (face->overline_p)
25675 it->ascent += overline_margin;
25676 take_vertical_position_into_account (it);
25677 if (it->ascent < 0)
25678 it->ascent = 0;
25679 if (it->descent < 0)
25680 it->descent = 0;
25681
25682 if (it->glyph_row)
25683 append_composite_glyph (it);
25684 }
25685 else if (it->what == IT_GLYPHLESS)
25686 produce_glyphless_glyph (it, 0, Qnil);
25687 else if (it->what == IT_IMAGE)
25688 produce_image_glyph (it);
25689 else if (it->what == IT_STRETCH)
25690 produce_stretch_glyph (it);
25691
25692 done:
25693 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25694 because this isn't true for images with `:ascent 100'. */
25695 eassert (it->ascent >= 0 && it->descent >= 0);
25696 if (it->area == TEXT_AREA)
25697 it->current_x += it->pixel_width;
25698
25699 if (extra_line_spacing > 0)
25700 {
25701 it->descent += extra_line_spacing;
25702 if (extra_line_spacing > it->max_extra_line_spacing)
25703 it->max_extra_line_spacing = extra_line_spacing;
25704 }
25705
25706 it->max_ascent = max (it->max_ascent, it->ascent);
25707 it->max_descent = max (it->max_descent, it->descent);
25708 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25709 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25710 }
25711
25712 /* EXPORT for RIF:
25713 Output LEN glyphs starting at START at the nominal cursor position.
25714 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
25715 being updated, and UPDATED_AREA is the area of that row being updated. */
25716
25717 void
25718 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
25719 struct glyph *start, enum glyph_row_area updated_area, int len)
25720 {
25721 int x, hpos, chpos = w->phys_cursor.hpos;
25722
25723 eassert (updated_row);
25724 /* When the window is hscrolled, cursor hpos can legitimately be out
25725 of bounds, but we draw the cursor at the corresponding window
25726 margin in that case. */
25727 if (!updated_row->reversed_p && chpos < 0)
25728 chpos = 0;
25729 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25730 chpos = updated_row->used[TEXT_AREA] - 1;
25731
25732 block_input ();
25733
25734 /* Write glyphs. */
25735
25736 hpos = start - updated_row->glyphs[updated_area];
25737 x = draw_glyphs (w, w->output_cursor.x,
25738 updated_row, updated_area,
25739 hpos, hpos + len,
25740 DRAW_NORMAL_TEXT, 0);
25741
25742 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25743 if (updated_area == TEXT_AREA
25744 && w->phys_cursor_on_p
25745 && w->phys_cursor.vpos == w->output_cursor.vpos
25746 && chpos >= hpos
25747 && chpos < hpos + len)
25748 w->phys_cursor_on_p = 0;
25749
25750 unblock_input ();
25751
25752 /* Advance the output cursor. */
25753 w->output_cursor.hpos += len;
25754 w->output_cursor.x = x;
25755 }
25756
25757
25758 /* EXPORT for RIF:
25759 Insert LEN glyphs from START at the nominal cursor position. */
25760
25761 void
25762 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
25763 struct glyph *start, enum glyph_row_area updated_area, int len)
25764 {
25765 struct frame *f;
25766 int line_height, shift_by_width, shifted_region_width;
25767 struct glyph_row *row;
25768 struct glyph *glyph;
25769 int frame_x, frame_y;
25770 ptrdiff_t hpos;
25771
25772 eassert (updated_row);
25773 block_input ();
25774 f = XFRAME (WINDOW_FRAME (w));
25775
25776 /* Get the height of the line we are in. */
25777 row = updated_row;
25778 line_height = row->height;
25779
25780 /* Get the width of the glyphs to insert. */
25781 shift_by_width = 0;
25782 for (glyph = start; glyph < start + len; ++glyph)
25783 shift_by_width += glyph->pixel_width;
25784
25785 /* Get the width of the region to shift right. */
25786 shifted_region_width = (window_box_width (w, updated_area)
25787 - w->output_cursor.x
25788 - shift_by_width);
25789
25790 /* Shift right. */
25791 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
25792 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
25793
25794 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25795 line_height, shift_by_width);
25796
25797 /* Write the glyphs. */
25798 hpos = start - row->glyphs[updated_area];
25799 draw_glyphs (w, w->output_cursor.x, row, updated_area,
25800 hpos, hpos + len,
25801 DRAW_NORMAL_TEXT, 0);
25802
25803 /* Advance the output cursor. */
25804 w->output_cursor.hpos += len;
25805 w->output_cursor.x += shift_by_width;
25806 unblock_input ();
25807 }
25808
25809
25810 /* EXPORT for RIF:
25811 Erase the current text line from the nominal cursor position
25812 (inclusive) to pixel column TO_X (exclusive). The idea is that
25813 everything from TO_X onward is already erased.
25814
25815 TO_X is a pixel position relative to UPDATED_AREA of currently
25816 updated window W. TO_X == -1 means clear to the end of this area. */
25817
25818 void
25819 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
25820 enum glyph_row_area updated_area, int to_x)
25821 {
25822 struct frame *f;
25823 int max_x, min_y, max_y;
25824 int from_x, from_y, to_y;
25825
25826 eassert (updated_row);
25827 f = XFRAME (w->frame);
25828
25829 if (updated_row->full_width_p)
25830 max_x = WINDOW_TOTAL_WIDTH (w);
25831 else
25832 max_x = window_box_width (w, updated_area);
25833 max_y = window_text_bottom_y (w);
25834
25835 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25836 of window. For TO_X > 0, truncate to end of drawing area. */
25837 if (to_x == 0)
25838 return;
25839 else if (to_x < 0)
25840 to_x = max_x;
25841 else
25842 to_x = min (to_x, max_x);
25843
25844 to_y = min (max_y, w->output_cursor.y + updated_row->height);
25845
25846 /* Notice if the cursor will be cleared by this operation. */
25847 if (!updated_row->full_width_p)
25848 notice_overwritten_cursor (w, updated_area,
25849 w->output_cursor.x, -1,
25850 updated_row->y,
25851 MATRIX_ROW_BOTTOM_Y (updated_row));
25852
25853 from_x = w->output_cursor.x;
25854
25855 /* Translate to frame coordinates. */
25856 if (updated_row->full_width_p)
25857 {
25858 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25859 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25860 }
25861 else
25862 {
25863 int area_left = window_box_left (w, updated_area);
25864 from_x += area_left;
25865 to_x += area_left;
25866 }
25867
25868 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25869 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
25870 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25871
25872 /* Prevent inadvertently clearing to end of the X window. */
25873 if (to_x > from_x && to_y > from_y)
25874 {
25875 block_input ();
25876 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25877 to_x - from_x, to_y - from_y);
25878 unblock_input ();
25879 }
25880 }
25881
25882 #endif /* HAVE_WINDOW_SYSTEM */
25883
25884
25885 \f
25886 /***********************************************************************
25887 Cursor types
25888 ***********************************************************************/
25889
25890 /* Value is the internal representation of the specified cursor type
25891 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25892 of the bar cursor. */
25893
25894 static enum text_cursor_kinds
25895 get_specified_cursor_type (Lisp_Object arg, int *width)
25896 {
25897 enum text_cursor_kinds type;
25898
25899 if (NILP (arg))
25900 return NO_CURSOR;
25901
25902 if (EQ (arg, Qbox))
25903 return FILLED_BOX_CURSOR;
25904
25905 if (EQ (arg, Qhollow))
25906 return HOLLOW_BOX_CURSOR;
25907
25908 if (EQ (arg, Qbar))
25909 {
25910 *width = 2;
25911 return BAR_CURSOR;
25912 }
25913
25914 if (CONSP (arg)
25915 && EQ (XCAR (arg), Qbar)
25916 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25917 {
25918 *width = XINT (XCDR (arg));
25919 return BAR_CURSOR;
25920 }
25921
25922 if (EQ (arg, Qhbar))
25923 {
25924 *width = 2;
25925 return HBAR_CURSOR;
25926 }
25927
25928 if (CONSP (arg)
25929 && EQ (XCAR (arg), Qhbar)
25930 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25931 {
25932 *width = XINT (XCDR (arg));
25933 return HBAR_CURSOR;
25934 }
25935
25936 /* Treat anything unknown as "hollow box cursor".
25937 It was bad to signal an error; people have trouble fixing
25938 .Xdefaults with Emacs, when it has something bad in it. */
25939 type = HOLLOW_BOX_CURSOR;
25940
25941 return type;
25942 }
25943
25944 /* Set the default cursor types for specified frame. */
25945 void
25946 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25947 {
25948 int width = 1;
25949 Lisp_Object tem;
25950
25951 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25952 FRAME_CURSOR_WIDTH (f) = width;
25953
25954 /* By default, set up the blink-off state depending on the on-state. */
25955
25956 tem = Fassoc (arg, Vblink_cursor_alist);
25957 if (!NILP (tem))
25958 {
25959 FRAME_BLINK_OFF_CURSOR (f)
25960 = get_specified_cursor_type (XCDR (tem), &width);
25961 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25962 }
25963 else
25964 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25965
25966 /* Make sure the cursor gets redrawn. */
25967 cursor_type_changed = 1;
25968 }
25969
25970
25971 #ifdef HAVE_WINDOW_SYSTEM
25972
25973 /* Return the cursor we want to be displayed in window W. Return
25974 width of bar/hbar cursor through WIDTH arg. Return with
25975 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
25976 (i.e. if the `system caret' should track this cursor).
25977
25978 In a mini-buffer window, we want the cursor only to appear if we
25979 are reading input from this window. For the selected window, we
25980 want the cursor type given by the frame parameter or buffer local
25981 setting of cursor-type. If explicitly marked off, draw no cursor.
25982 In all other cases, we want a hollow box cursor. */
25983
25984 static enum text_cursor_kinds
25985 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
25986 int *active_cursor)
25987 {
25988 struct frame *f = XFRAME (w->frame);
25989 struct buffer *b = XBUFFER (w->contents);
25990 int cursor_type = DEFAULT_CURSOR;
25991 Lisp_Object alt_cursor;
25992 int non_selected = 0;
25993
25994 *active_cursor = 1;
25995
25996 /* Echo area */
25997 if (cursor_in_echo_area
25998 && FRAME_HAS_MINIBUF_P (f)
25999 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
26000 {
26001 if (w == XWINDOW (echo_area_window))
26002 {
26003 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
26004 {
26005 *width = FRAME_CURSOR_WIDTH (f);
26006 return FRAME_DESIRED_CURSOR (f);
26007 }
26008 else
26009 return get_specified_cursor_type (BVAR (b, cursor_type), width);
26010 }
26011
26012 *active_cursor = 0;
26013 non_selected = 1;
26014 }
26015
26016 /* Detect a nonselected window or nonselected frame. */
26017 else if (w != XWINDOW (f->selected_window)
26018 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
26019 {
26020 *active_cursor = 0;
26021
26022 if (MINI_WINDOW_P (w) && minibuf_level == 0)
26023 return NO_CURSOR;
26024
26025 non_selected = 1;
26026 }
26027
26028 /* Never display a cursor in a window in which cursor-type is nil. */
26029 if (NILP (BVAR (b, cursor_type)))
26030 return NO_CURSOR;
26031
26032 /* Get the normal cursor type for this window. */
26033 if (EQ (BVAR (b, cursor_type), Qt))
26034 {
26035 cursor_type = FRAME_DESIRED_CURSOR (f);
26036 *width = FRAME_CURSOR_WIDTH (f);
26037 }
26038 else
26039 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
26040
26041 /* Use cursor-in-non-selected-windows instead
26042 for non-selected window or frame. */
26043 if (non_selected)
26044 {
26045 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
26046 if (!EQ (Qt, alt_cursor))
26047 return get_specified_cursor_type (alt_cursor, width);
26048 /* t means modify the normal cursor type. */
26049 if (cursor_type == FILLED_BOX_CURSOR)
26050 cursor_type = HOLLOW_BOX_CURSOR;
26051 else if (cursor_type == BAR_CURSOR && *width > 1)
26052 --*width;
26053 return cursor_type;
26054 }
26055
26056 /* Use normal cursor if not blinked off. */
26057 if (!w->cursor_off_p)
26058 {
26059 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26060 {
26061 if (cursor_type == FILLED_BOX_CURSOR)
26062 {
26063 /* Using a block cursor on large images can be very annoying.
26064 So use a hollow cursor for "large" images.
26065 If image is not transparent (no mask), also use hollow cursor. */
26066 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26067 if (img != NULL && IMAGEP (img->spec))
26068 {
26069 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
26070 where N = size of default frame font size.
26071 This should cover most of the "tiny" icons people may use. */
26072 if (!img->mask
26073 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
26074 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
26075 cursor_type = HOLLOW_BOX_CURSOR;
26076 }
26077 }
26078 else if (cursor_type != NO_CURSOR)
26079 {
26080 /* Display current only supports BOX and HOLLOW cursors for images.
26081 So for now, unconditionally use a HOLLOW cursor when cursor is
26082 not a solid box cursor. */
26083 cursor_type = HOLLOW_BOX_CURSOR;
26084 }
26085 }
26086 return cursor_type;
26087 }
26088
26089 /* Cursor is blinked off, so determine how to "toggle" it. */
26090
26091 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
26092 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
26093 return get_specified_cursor_type (XCDR (alt_cursor), width);
26094
26095 /* Then see if frame has specified a specific blink off cursor type. */
26096 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
26097 {
26098 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
26099 return FRAME_BLINK_OFF_CURSOR (f);
26100 }
26101
26102 #if 0
26103 /* Some people liked having a permanently visible blinking cursor,
26104 while others had very strong opinions against it. So it was
26105 decided to remove it. KFS 2003-09-03 */
26106
26107 /* Finally perform built-in cursor blinking:
26108 filled box <-> hollow box
26109 wide [h]bar <-> narrow [h]bar
26110 narrow [h]bar <-> no cursor
26111 other type <-> no cursor */
26112
26113 if (cursor_type == FILLED_BOX_CURSOR)
26114 return HOLLOW_BOX_CURSOR;
26115
26116 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
26117 {
26118 *width = 1;
26119 return cursor_type;
26120 }
26121 #endif
26122
26123 return NO_CURSOR;
26124 }
26125
26126
26127 /* Notice when the text cursor of window W has been completely
26128 overwritten by a drawing operation that outputs glyphs in AREA
26129 starting at X0 and ending at X1 in the line starting at Y0 and
26130 ending at Y1. X coordinates are area-relative. X1 < 0 means all
26131 the rest of the line after X0 has been written. Y coordinates
26132 are window-relative. */
26133
26134 static void
26135 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
26136 int x0, int x1, int y0, int y1)
26137 {
26138 int cx0, cx1, cy0, cy1;
26139 struct glyph_row *row;
26140
26141 if (!w->phys_cursor_on_p)
26142 return;
26143 if (area != TEXT_AREA)
26144 return;
26145
26146 if (w->phys_cursor.vpos < 0
26147 || w->phys_cursor.vpos >= w->current_matrix->nrows
26148 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
26149 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
26150 return;
26151
26152 if (row->cursor_in_fringe_p)
26153 {
26154 row->cursor_in_fringe_p = 0;
26155 draw_fringe_bitmap (w, row, row->reversed_p);
26156 w->phys_cursor_on_p = 0;
26157 return;
26158 }
26159
26160 cx0 = w->phys_cursor.x;
26161 cx1 = cx0 + w->phys_cursor_width;
26162 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
26163 return;
26164
26165 /* The cursor image will be completely removed from the
26166 screen if the output area intersects the cursor area in
26167 y-direction. When we draw in [y0 y1[, and some part of
26168 the cursor is at y < y0, that part must have been drawn
26169 before. When scrolling, the cursor is erased before
26170 actually scrolling, so we don't come here. When not
26171 scrolling, the rows above the old cursor row must have
26172 changed, and in this case these rows must have written
26173 over the cursor image.
26174
26175 Likewise if part of the cursor is below y1, with the
26176 exception of the cursor being in the first blank row at
26177 the buffer and window end because update_text_area
26178 doesn't draw that row. (Except when it does, but
26179 that's handled in update_text_area.) */
26180
26181 cy0 = w->phys_cursor.y;
26182 cy1 = cy0 + w->phys_cursor_height;
26183 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
26184 return;
26185
26186 w->phys_cursor_on_p = 0;
26187 }
26188
26189 #endif /* HAVE_WINDOW_SYSTEM */
26190
26191 \f
26192 /************************************************************************
26193 Mouse Face
26194 ************************************************************************/
26195
26196 #ifdef HAVE_WINDOW_SYSTEM
26197
26198 /* EXPORT for RIF:
26199 Fix the display of area AREA of overlapping row ROW in window W
26200 with respect to the overlapping part OVERLAPS. */
26201
26202 void
26203 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
26204 enum glyph_row_area area, int overlaps)
26205 {
26206 int i, x;
26207
26208 block_input ();
26209
26210 x = 0;
26211 for (i = 0; i < row->used[area];)
26212 {
26213 if (row->glyphs[area][i].overlaps_vertically_p)
26214 {
26215 int start = i, start_x = x;
26216
26217 do
26218 {
26219 x += row->glyphs[area][i].pixel_width;
26220 ++i;
26221 }
26222 while (i < row->used[area]
26223 && row->glyphs[area][i].overlaps_vertically_p);
26224
26225 draw_glyphs (w, start_x, row, area,
26226 start, i,
26227 DRAW_NORMAL_TEXT, overlaps);
26228 }
26229 else
26230 {
26231 x += row->glyphs[area][i].pixel_width;
26232 ++i;
26233 }
26234 }
26235
26236 unblock_input ();
26237 }
26238
26239
26240 /* EXPORT:
26241 Draw the cursor glyph of window W in glyph row ROW. See the
26242 comment of draw_glyphs for the meaning of HL. */
26243
26244 void
26245 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
26246 enum draw_glyphs_face hl)
26247 {
26248 /* If cursor hpos is out of bounds, don't draw garbage. This can
26249 happen in mini-buffer windows when switching between echo area
26250 glyphs and mini-buffer. */
26251 if ((row->reversed_p
26252 ? (w->phys_cursor.hpos >= 0)
26253 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
26254 {
26255 int on_p = w->phys_cursor_on_p;
26256 int x1;
26257 int hpos = w->phys_cursor.hpos;
26258
26259 /* When the window is hscrolled, cursor hpos can legitimately be
26260 out of bounds, but we draw the cursor at the corresponding
26261 window margin in that case. */
26262 if (!row->reversed_p && hpos < 0)
26263 hpos = 0;
26264 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26265 hpos = row->used[TEXT_AREA] - 1;
26266
26267 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
26268 hl, 0);
26269 w->phys_cursor_on_p = on_p;
26270
26271 if (hl == DRAW_CURSOR)
26272 w->phys_cursor_width = x1 - w->phys_cursor.x;
26273 /* When we erase the cursor, and ROW is overlapped by other
26274 rows, make sure that these overlapping parts of other rows
26275 are redrawn. */
26276 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
26277 {
26278 w->phys_cursor_width = x1 - w->phys_cursor.x;
26279
26280 if (row > w->current_matrix->rows
26281 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
26282 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
26283 OVERLAPS_ERASED_CURSOR);
26284
26285 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
26286 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
26287 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
26288 OVERLAPS_ERASED_CURSOR);
26289 }
26290 }
26291 }
26292
26293
26294 /* EXPORT:
26295 Erase the image of a cursor of window W from the screen. */
26296
26297 void
26298 erase_phys_cursor (struct window *w)
26299 {
26300 struct frame *f = XFRAME (w->frame);
26301 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26302 int hpos = w->phys_cursor.hpos;
26303 int vpos = w->phys_cursor.vpos;
26304 int mouse_face_here_p = 0;
26305 struct glyph_matrix *active_glyphs = w->current_matrix;
26306 struct glyph_row *cursor_row;
26307 struct glyph *cursor_glyph;
26308 enum draw_glyphs_face hl;
26309
26310 /* No cursor displayed or row invalidated => nothing to do on the
26311 screen. */
26312 if (w->phys_cursor_type == NO_CURSOR)
26313 goto mark_cursor_off;
26314
26315 /* VPOS >= active_glyphs->nrows means that window has been resized.
26316 Don't bother to erase the cursor. */
26317 if (vpos >= active_glyphs->nrows)
26318 goto mark_cursor_off;
26319
26320 /* If row containing cursor is marked invalid, there is nothing we
26321 can do. */
26322 cursor_row = MATRIX_ROW (active_glyphs, vpos);
26323 if (!cursor_row->enabled_p)
26324 goto mark_cursor_off;
26325
26326 /* If line spacing is > 0, old cursor may only be partially visible in
26327 window after split-window. So adjust visible height. */
26328 cursor_row->visible_height = min (cursor_row->visible_height,
26329 window_text_bottom_y (w) - cursor_row->y);
26330
26331 /* If row is completely invisible, don't attempt to delete a cursor which
26332 isn't there. This can happen if cursor is at top of a window, and
26333 we switch to a buffer with a header line in that window. */
26334 if (cursor_row->visible_height <= 0)
26335 goto mark_cursor_off;
26336
26337 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
26338 if (cursor_row->cursor_in_fringe_p)
26339 {
26340 cursor_row->cursor_in_fringe_p = 0;
26341 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
26342 goto mark_cursor_off;
26343 }
26344
26345 /* This can happen when the new row is shorter than the old one.
26346 In this case, either draw_glyphs or clear_end_of_line
26347 should have cleared the cursor. Note that we wouldn't be
26348 able to erase the cursor in this case because we don't have a
26349 cursor glyph at hand. */
26350 if ((cursor_row->reversed_p
26351 ? (w->phys_cursor.hpos < 0)
26352 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
26353 goto mark_cursor_off;
26354
26355 /* When the window is hscrolled, cursor hpos can legitimately be out
26356 of bounds, but we draw the cursor at the corresponding window
26357 margin in that case. */
26358 if (!cursor_row->reversed_p && hpos < 0)
26359 hpos = 0;
26360 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
26361 hpos = cursor_row->used[TEXT_AREA] - 1;
26362
26363 /* If the cursor is in the mouse face area, redisplay that when
26364 we clear the cursor. */
26365 if (! NILP (hlinfo->mouse_face_window)
26366 && coords_in_mouse_face_p (w, hpos, vpos)
26367 /* Don't redraw the cursor's spot in mouse face if it is at the
26368 end of a line (on a newline). The cursor appears there, but
26369 mouse highlighting does not. */
26370 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
26371 mouse_face_here_p = 1;
26372
26373 /* Maybe clear the display under the cursor. */
26374 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
26375 {
26376 int x, y, left_x;
26377 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
26378 int width;
26379
26380 cursor_glyph = get_phys_cursor_glyph (w);
26381 if (cursor_glyph == NULL)
26382 goto mark_cursor_off;
26383
26384 width = cursor_glyph->pixel_width;
26385 left_x = window_box_left_offset (w, TEXT_AREA);
26386 x = w->phys_cursor.x;
26387 if (x < left_x)
26388 width -= left_x - x;
26389 width = min (width, window_box_width (w, TEXT_AREA) - x);
26390 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
26391 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
26392
26393 if (width > 0)
26394 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
26395 }
26396
26397 /* Erase the cursor by redrawing the character underneath it. */
26398 if (mouse_face_here_p)
26399 hl = DRAW_MOUSE_FACE;
26400 else
26401 hl = DRAW_NORMAL_TEXT;
26402 draw_phys_cursor_glyph (w, cursor_row, hl);
26403
26404 mark_cursor_off:
26405 w->phys_cursor_on_p = 0;
26406 w->phys_cursor_type = NO_CURSOR;
26407 }
26408
26409
26410 /* EXPORT:
26411 Display or clear cursor of window W. If ON is zero, clear the
26412 cursor. If it is non-zero, display the cursor. If ON is nonzero,
26413 where to put the cursor is specified by HPOS, VPOS, X and Y. */
26414
26415 void
26416 display_and_set_cursor (struct window *w, bool on,
26417 int hpos, int vpos, int x, int y)
26418 {
26419 struct frame *f = XFRAME (w->frame);
26420 int new_cursor_type;
26421 int new_cursor_width;
26422 int active_cursor;
26423 struct glyph_row *glyph_row;
26424 struct glyph *glyph;
26425
26426 /* This is pointless on invisible frames, and dangerous on garbaged
26427 windows and frames; in the latter case, the frame or window may
26428 be in the midst of changing its size, and x and y may be off the
26429 window. */
26430 if (! FRAME_VISIBLE_P (f)
26431 || FRAME_GARBAGED_P (f)
26432 || vpos >= w->current_matrix->nrows
26433 || hpos >= w->current_matrix->matrix_w)
26434 return;
26435
26436 /* If cursor is off and we want it off, return quickly. */
26437 if (!on && !w->phys_cursor_on_p)
26438 return;
26439
26440 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26441 /* If cursor row is not enabled, we don't really know where to
26442 display the cursor. */
26443 if (!glyph_row->enabled_p)
26444 {
26445 w->phys_cursor_on_p = 0;
26446 return;
26447 }
26448
26449 glyph = NULL;
26450 if (!glyph_row->exact_window_width_line_p
26451 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26452 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26453
26454 eassert (input_blocked_p ());
26455
26456 /* Set new_cursor_type to the cursor we want to be displayed. */
26457 new_cursor_type = get_window_cursor_type (w, glyph,
26458 &new_cursor_width, &active_cursor);
26459
26460 /* If cursor is currently being shown and we don't want it to be or
26461 it is in the wrong place, or the cursor type is not what we want,
26462 erase it. */
26463 if (w->phys_cursor_on_p
26464 && (!on
26465 || w->phys_cursor.x != x
26466 || w->phys_cursor.y != y
26467 || new_cursor_type != w->phys_cursor_type
26468 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26469 && new_cursor_width != w->phys_cursor_width)))
26470 erase_phys_cursor (w);
26471
26472 /* Don't check phys_cursor_on_p here because that flag is only set
26473 to zero in some cases where we know that the cursor has been
26474 completely erased, to avoid the extra work of erasing the cursor
26475 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26476 still not be visible, or it has only been partly erased. */
26477 if (on)
26478 {
26479 w->phys_cursor_ascent = glyph_row->ascent;
26480 w->phys_cursor_height = glyph_row->height;
26481
26482 /* Set phys_cursor_.* before x_draw_.* is called because some
26483 of them may need the information. */
26484 w->phys_cursor.x = x;
26485 w->phys_cursor.y = glyph_row->y;
26486 w->phys_cursor.hpos = hpos;
26487 w->phys_cursor.vpos = vpos;
26488 }
26489
26490 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26491 new_cursor_type, new_cursor_width,
26492 on, active_cursor);
26493 }
26494
26495
26496 /* Switch the display of W's cursor on or off, according to the value
26497 of ON. */
26498
26499 static void
26500 update_window_cursor (struct window *w, bool on)
26501 {
26502 /* Don't update cursor in windows whose frame is in the process
26503 of being deleted. */
26504 if (w->current_matrix)
26505 {
26506 int hpos = w->phys_cursor.hpos;
26507 int vpos = w->phys_cursor.vpos;
26508 struct glyph_row *row;
26509
26510 if (vpos >= w->current_matrix->nrows
26511 || hpos >= w->current_matrix->matrix_w)
26512 return;
26513
26514 row = MATRIX_ROW (w->current_matrix, vpos);
26515
26516 /* When the window is hscrolled, cursor hpos can legitimately be
26517 out of bounds, but we draw the cursor at the corresponding
26518 window margin in that case. */
26519 if (!row->reversed_p && hpos < 0)
26520 hpos = 0;
26521 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26522 hpos = row->used[TEXT_AREA] - 1;
26523
26524 block_input ();
26525 display_and_set_cursor (w, on, hpos, vpos,
26526 w->phys_cursor.x, w->phys_cursor.y);
26527 unblock_input ();
26528 }
26529 }
26530
26531
26532 /* Call update_window_cursor with parameter ON_P on all leaf windows
26533 in the window tree rooted at W. */
26534
26535 static void
26536 update_cursor_in_window_tree (struct window *w, bool on_p)
26537 {
26538 while (w)
26539 {
26540 if (WINDOWP (w->contents))
26541 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
26542 else
26543 update_window_cursor (w, on_p);
26544
26545 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26546 }
26547 }
26548
26549
26550 /* EXPORT:
26551 Display the cursor on window W, or clear it, according to ON_P.
26552 Don't change the cursor's position. */
26553
26554 void
26555 x_update_cursor (struct frame *f, bool on_p)
26556 {
26557 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26558 }
26559
26560
26561 /* EXPORT:
26562 Clear the cursor of window W to background color, and mark the
26563 cursor as not shown. This is used when the text where the cursor
26564 is about to be rewritten. */
26565
26566 void
26567 x_clear_cursor (struct window *w)
26568 {
26569 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26570 update_window_cursor (w, 0);
26571 }
26572
26573 #endif /* HAVE_WINDOW_SYSTEM */
26574
26575 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26576 and MSDOS. */
26577 static void
26578 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26579 int start_hpos, int end_hpos,
26580 enum draw_glyphs_face draw)
26581 {
26582 #ifdef HAVE_WINDOW_SYSTEM
26583 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26584 {
26585 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26586 return;
26587 }
26588 #endif
26589 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26590 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26591 #endif
26592 }
26593
26594 /* Display the active region described by mouse_face_* according to DRAW. */
26595
26596 static void
26597 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26598 {
26599 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26600 struct frame *f = XFRAME (WINDOW_FRAME (w));
26601
26602 if (/* If window is in the process of being destroyed, don't bother
26603 to do anything. */
26604 w->current_matrix != NULL
26605 /* Don't update mouse highlight if hidden */
26606 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26607 /* Recognize when we are called to operate on rows that don't exist
26608 anymore. This can happen when a window is split. */
26609 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26610 {
26611 int phys_cursor_on_p = w->phys_cursor_on_p;
26612 struct glyph_row *row, *first, *last;
26613
26614 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26615 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26616
26617 for (row = first; row <= last && row->enabled_p; ++row)
26618 {
26619 int start_hpos, end_hpos, start_x;
26620
26621 /* For all but the first row, the highlight starts at column 0. */
26622 if (row == first)
26623 {
26624 /* R2L rows have BEG and END in reversed order, but the
26625 screen drawing geometry is always left to right. So
26626 we need to mirror the beginning and end of the
26627 highlighted area in R2L rows. */
26628 if (!row->reversed_p)
26629 {
26630 start_hpos = hlinfo->mouse_face_beg_col;
26631 start_x = hlinfo->mouse_face_beg_x;
26632 }
26633 else if (row == last)
26634 {
26635 start_hpos = hlinfo->mouse_face_end_col;
26636 start_x = hlinfo->mouse_face_end_x;
26637 }
26638 else
26639 {
26640 start_hpos = 0;
26641 start_x = 0;
26642 }
26643 }
26644 else if (row->reversed_p && row == last)
26645 {
26646 start_hpos = hlinfo->mouse_face_end_col;
26647 start_x = hlinfo->mouse_face_end_x;
26648 }
26649 else
26650 {
26651 start_hpos = 0;
26652 start_x = 0;
26653 }
26654
26655 if (row == last)
26656 {
26657 if (!row->reversed_p)
26658 end_hpos = hlinfo->mouse_face_end_col;
26659 else if (row == first)
26660 end_hpos = hlinfo->mouse_face_beg_col;
26661 else
26662 {
26663 end_hpos = row->used[TEXT_AREA];
26664 if (draw == DRAW_NORMAL_TEXT)
26665 row->fill_line_p = 1; /* Clear to end of line */
26666 }
26667 }
26668 else if (row->reversed_p && row == first)
26669 end_hpos = hlinfo->mouse_face_beg_col;
26670 else
26671 {
26672 end_hpos = row->used[TEXT_AREA];
26673 if (draw == DRAW_NORMAL_TEXT)
26674 row->fill_line_p = 1; /* Clear to end of line */
26675 }
26676
26677 if (end_hpos > start_hpos)
26678 {
26679 draw_row_with_mouse_face (w, start_x, row,
26680 start_hpos, end_hpos, draw);
26681
26682 row->mouse_face_p
26683 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26684 }
26685 }
26686
26687 #ifdef HAVE_WINDOW_SYSTEM
26688 /* When we've written over the cursor, arrange for it to
26689 be displayed again. */
26690 if (FRAME_WINDOW_P (f)
26691 && phys_cursor_on_p && !w->phys_cursor_on_p)
26692 {
26693 int hpos = w->phys_cursor.hpos;
26694
26695 /* When the window is hscrolled, cursor hpos can legitimately be
26696 out of bounds, but we draw the cursor at the corresponding
26697 window margin in that case. */
26698 if (!row->reversed_p && hpos < 0)
26699 hpos = 0;
26700 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26701 hpos = row->used[TEXT_AREA] - 1;
26702
26703 block_input ();
26704 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26705 w->phys_cursor.x, w->phys_cursor.y);
26706 unblock_input ();
26707 }
26708 #endif /* HAVE_WINDOW_SYSTEM */
26709 }
26710
26711 #ifdef HAVE_WINDOW_SYSTEM
26712 /* Change the mouse cursor. */
26713 if (FRAME_WINDOW_P (f))
26714 {
26715 if (draw == DRAW_NORMAL_TEXT
26716 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26717 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26718 else if (draw == DRAW_MOUSE_FACE)
26719 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26720 else
26721 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26722 }
26723 #endif /* HAVE_WINDOW_SYSTEM */
26724 }
26725
26726 /* EXPORT:
26727 Clear out the mouse-highlighted active region.
26728 Redraw it un-highlighted first. Value is non-zero if mouse
26729 face was actually drawn unhighlighted. */
26730
26731 int
26732 clear_mouse_face (Mouse_HLInfo *hlinfo)
26733 {
26734 int cleared = 0;
26735
26736 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26737 {
26738 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26739 cleared = 1;
26740 }
26741
26742 reset_mouse_highlight (hlinfo);
26743 return cleared;
26744 }
26745
26746 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26747 within the mouse face on that window. */
26748 static int
26749 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26750 {
26751 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26752
26753 /* Quickly resolve the easy cases. */
26754 if (!(WINDOWP (hlinfo->mouse_face_window)
26755 && XWINDOW (hlinfo->mouse_face_window) == w))
26756 return 0;
26757 if (vpos < hlinfo->mouse_face_beg_row
26758 || vpos > hlinfo->mouse_face_end_row)
26759 return 0;
26760 if (vpos > hlinfo->mouse_face_beg_row
26761 && vpos < hlinfo->mouse_face_end_row)
26762 return 1;
26763
26764 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26765 {
26766 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26767 {
26768 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26769 return 1;
26770 }
26771 else if ((vpos == hlinfo->mouse_face_beg_row
26772 && hpos >= hlinfo->mouse_face_beg_col)
26773 || (vpos == hlinfo->mouse_face_end_row
26774 && hpos < hlinfo->mouse_face_end_col))
26775 return 1;
26776 }
26777 else
26778 {
26779 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26780 {
26781 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26782 return 1;
26783 }
26784 else if ((vpos == hlinfo->mouse_face_beg_row
26785 && hpos <= hlinfo->mouse_face_beg_col)
26786 || (vpos == hlinfo->mouse_face_end_row
26787 && hpos > hlinfo->mouse_face_end_col))
26788 return 1;
26789 }
26790 return 0;
26791 }
26792
26793
26794 /* EXPORT:
26795 Non-zero if physical cursor of window W is within mouse face. */
26796
26797 int
26798 cursor_in_mouse_face_p (struct window *w)
26799 {
26800 int hpos = w->phys_cursor.hpos;
26801 int vpos = w->phys_cursor.vpos;
26802 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26803
26804 /* When the window is hscrolled, cursor hpos can legitimately be out
26805 of bounds, but we draw the cursor at the corresponding window
26806 margin in that case. */
26807 if (!row->reversed_p && hpos < 0)
26808 hpos = 0;
26809 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26810 hpos = row->used[TEXT_AREA] - 1;
26811
26812 return coords_in_mouse_face_p (w, hpos, vpos);
26813 }
26814
26815
26816 \f
26817 /* Find the glyph rows START_ROW and END_ROW of window W that display
26818 characters between buffer positions START_CHARPOS and END_CHARPOS
26819 (excluding END_CHARPOS). DISP_STRING is a display string that
26820 covers these buffer positions. This is similar to
26821 row_containing_pos, but is more accurate when bidi reordering makes
26822 buffer positions change non-linearly with glyph rows. */
26823 static void
26824 rows_from_pos_range (struct window *w,
26825 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26826 Lisp_Object disp_string,
26827 struct glyph_row **start, struct glyph_row **end)
26828 {
26829 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26830 int last_y = window_text_bottom_y (w);
26831 struct glyph_row *row;
26832
26833 *start = NULL;
26834 *end = NULL;
26835
26836 while (!first->enabled_p
26837 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26838 first++;
26839
26840 /* Find the START row. */
26841 for (row = first;
26842 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26843 row++)
26844 {
26845 /* A row can potentially be the START row if the range of the
26846 characters it displays intersects the range
26847 [START_CHARPOS..END_CHARPOS). */
26848 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26849 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26850 /* See the commentary in row_containing_pos, for the
26851 explanation of the complicated way to check whether
26852 some position is beyond the end of the characters
26853 displayed by a row. */
26854 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26855 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26856 && !row->ends_at_zv_p
26857 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26858 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26859 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26860 && !row->ends_at_zv_p
26861 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26862 {
26863 /* Found a candidate row. Now make sure at least one of the
26864 glyphs it displays has a charpos from the range
26865 [START_CHARPOS..END_CHARPOS).
26866
26867 This is not obvious because bidi reordering could make
26868 buffer positions of a row be 1,2,3,102,101,100, and if we
26869 want to highlight characters in [50..60), we don't want
26870 this row, even though [50..60) does intersect [1..103),
26871 the range of character positions given by the row's start
26872 and end positions. */
26873 struct glyph *g = row->glyphs[TEXT_AREA];
26874 struct glyph *e = g + row->used[TEXT_AREA];
26875
26876 while (g < e)
26877 {
26878 if (((BUFFERP (g->object) || INTEGERP (g->object))
26879 && start_charpos <= g->charpos && g->charpos < end_charpos)
26880 /* A glyph that comes from DISP_STRING is by
26881 definition to be highlighted. */
26882 || EQ (g->object, disp_string))
26883 *start = row;
26884 g++;
26885 }
26886 if (*start)
26887 break;
26888 }
26889 }
26890
26891 /* Find the END row. */
26892 if (!*start
26893 /* If the last row is partially visible, start looking for END
26894 from that row, instead of starting from FIRST. */
26895 && !(row->enabled_p
26896 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26897 row = first;
26898 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26899 {
26900 struct glyph_row *next = row + 1;
26901 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26902
26903 if (!next->enabled_p
26904 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26905 /* The first row >= START whose range of displayed characters
26906 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26907 is the row END + 1. */
26908 || (start_charpos < next_start
26909 && end_charpos < next_start)
26910 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26911 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26912 && !next->ends_at_zv_p
26913 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26914 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26915 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26916 && !next->ends_at_zv_p
26917 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26918 {
26919 *end = row;
26920 break;
26921 }
26922 else
26923 {
26924 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26925 but none of the characters it displays are in the range, it is
26926 also END + 1. */
26927 struct glyph *g = next->glyphs[TEXT_AREA];
26928 struct glyph *s = g;
26929 struct glyph *e = g + next->used[TEXT_AREA];
26930
26931 while (g < e)
26932 {
26933 if (((BUFFERP (g->object) || INTEGERP (g->object))
26934 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26935 /* If the buffer position of the first glyph in
26936 the row is equal to END_CHARPOS, it means
26937 the last character to be highlighted is the
26938 newline of ROW, and we must consider NEXT as
26939 END, not END+1. */
26940 || (((!next->reversed_p && g == s)
26941 || (next->reversed_p && g == e - 1))
26942 && (g->charpos == end_charpos
26943 /* Special case for when NEXT is an
26944 empty line at ZV. */
26945 || (g->charpos == -1
26946 && !row->ends_at_zv_p
26947 && next_start == end_charpos)))))
26948 /* A glyph that comes from DISP_STRING is by
26949 definition to be highlighted. */
26950 || EQ (g->object, disp_string))
26951 break;
26952 g++;
26953 }
26954 if (g == e)
26955 {
26956 *end = row;
26957 break;
26958 }
26959 /* The first row that ends at ZV must be the last to be
26960 highlighted. */
26961 else if (next->ends_at_zv_p)
26962 {
26963 *end = next;
26964 break;
26965 }
26966 }
26967 }
26968 }
26969
26970 /* This function sets the mouse_face_* elements of HLINFO, assuming
26971 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
26972 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
26973 for the overlay or run of text properties specifying the mouse
26974 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
26975 before-string and after-string that must also be highlighted.
26976 DISP_STRING, if non-nil, is a display string that may cover some
26977 or all of the highlighted text. */
26978
26979 static void
26980 mouse_face_from_buffer_pos (Lisp_Object window,
26981 Mouse_HLInfo *hlinfo,
26982 ptrdiff_t mouse_charpos,
26983 ptrdiff_t start_charpos,
26984 ptrdiff_t end_charpos,
26985 Lisp_Object before_string,
26986 Lisp_Object after_string,
26987 Lisp_Object disp_string)
26988 {
26989 struct window *w = XWINDOW (window);
26990 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26991 struct glyph_row *r1, *r2;
26992 struct glyph *glyph, *end;
26993 ptrdiff_t ignore, pos;
26994 int x;
26995
26996 eassert (NILP (disp_string) || STRINGP (disp_string));
26997 eassert (NILP (before_string) || STRINGP (before_string));
26998 eassert (NILP (after_string) || STRINGP (after_string));
26999
27000 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
27001 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
27002 if (r1 == NULL)
27003 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27004 /* If the before-string or display-string contains newlines,
27005 rows_from_pos_range skips to its last row. Move back. */
27006 if (!NILP (before_string) || !NILP (disp_string))
27007 {
27008 struct glyph_row *prev;
27009 while ((prev = r1 - 1, prev >= first)
27010 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
27011 && prev->used[TEXT_AREA] > 0)
27012 {
27013 struct glyph *beg = prev->glyphs[TEXT_AREA];
27014 glyph = beg + prev->used[TEXT_AREA];
27015 while (--glyph >= beg && INTEGERP (glyph->object));
27016 if (glyph < beg
27017 || !(EQ (glyph->object, before_string)
27018 || EQ (glyph->object, disp_string)))
27019 break;
27020 r1 = prev;
27021 }
27022 }
27023 if (r2 == NULL)
27024 {
27025 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27026 hlinfo->mouse_face_past_end = 1;
27027 }
27028 else if (!NILP (after_string))
27029 {
27030 /* If the after-string has newlines, advance to its last row. */
27031 struct glyph_row *next;
27032 struct glyph_row *last
27033 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27034
27035 for (next = r2 + 1;
27036 next <= last
27037 && next->used[TEXT_AREA] > 0
27038 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
27039 ++next)
27040 r2 = next;
27041 }
27042 /* The rest of the display engine assumes that mouse_face_beg_row is
27043 either above mouse_face_end_row or identical to it. But with
27044 bidi-reordered continued lines, the row for START_CHARPOS could
27045 be below the row for END_CHARPOS. If so, swap the rows and store
27046 them in correct order. */
27047 if (r1->y > r2->y)
27048 {
27049 struct glyph_row *tem = r2;
27050
27051 r2 = r1;
27052 r1 = tem;
27053 }
27054
27055 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
27056 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
27057
27058 /* For a bidi-reordered row, the positions of BEFORE_STRING,
27059 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
27060 could be anywhere in the row and in any order. The strategy
27061 below is to find the leftmost and the rightmost glyph that
27062 belongs to either of these 3 strings, or whose position is
27063 between START_CHARPOS and END_CHARPOS, and highlight all the
27064 glyphs between those two. This may cover more than just the text
27065 between START_CHARPOS and END_CHARPOS if the range of characters
27066 strides the bidi level boundary, e.g. if the beginning is in R2L
27067 text while the end is in L2R text or vice versa. */
27068 if (!r1->reversed_p)
27069 {
27070 /* This row is in a left to right paragraph. Scan it left to
27071 right. */
27072 glyph = r1->glyphs[TEXT_AREA];
27073 end = glyph + r1->used[TEXT_AREA];
27074 x = r1->x;
27075
27076 /* Skip truncation glyphs at the start of the glyph row. */
27077 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27078 for (; glyph < end
27079 && INTEGERP (glyph->object)
27080 && glyph->charpos < 0;
27081 ++glyph)
27082 x += glyph->pixel_width;
27083
27084 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27085 or DISP_STRING, and the first glyph from buffer whose
27086 position is between START_CHARPOS and END_CHARPOS. */
27087 for (; glyph < end
27088 && !INTEGERP (glyph->object)
27089 && !EQ (glyph->object, disp_string)
27090 && !(BUFFERP (glyph->object)
27091 && (glyph->charpos >= start_charpos
27092 && glyph->charpos < end_charpos));
27093 ++glyph)
27094 {
27095 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27096 are present at buffer positions between START_CHARPOS and
27097 END_CHARPOS, or if they come from an overlay. */
27098 if (EQ (glyph->object, before_string))
27099 {
27100 pos = string_buffer_position (before_string,
27101 start_charpos);
27102 /* If pos == 0, it means before_string came from an
27103 overlay, not from a buffer position. */
27104 if (!pos || (pos >= start_charpos && pos < end_charpos))
27105 break;
27106 }
27107 else if (EQ (glyph->object, after_string))
27108 {
27109 pos = string_buffer_position (after_string, end_charpos);
27110 if (!pos || (pos >= start_charpos && pos < end_charpos))
27111 break;
27112 }
27113 x += glyph->pixel_width;
27114 }
27115 hlinfo->mouse_face_beg_x = x;
27116 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27117 }
27118 else
27119 {
27120 /* This row is in a right to left paragraph. Scan it right to
27121 left. */
27122 struct glyph *g;
27123
27124 end = r1->glyphs[TEXT_AREA] - 1;
27125 glyph = end + r1->used[TEXT_AREA];
27126
27127 /* Skip truncation glyphs at the start of the glyph row. */
27128 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27129 for (; glyph > end
27130 && INTEGERP (glyph->object)
27131 && glyph->charpos < 0;
27132 --glyph)
27133 ;
27134
27135 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27136 or DISP_STRING, and the first glyph from buffer whose
27137 position is between START_CHARPOS and END_CHARPOS. */
27138 for (; glyph > end
27139 && !INTEGERP (glyph->object)
27140 && !EQ (glyph->object, disp_string)
27141 && !(BUFFERP (glyph->object)
27142 && (glyph->charpos >= start_charpos
27143 && glyph->charpos < end_charpos));
27144 --glyph)
27145 {
27146 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27147 are present at buffer positions between START_CHARPOS and
27148 END_CHARPOS, or if they come from an overlay. */
27149 if (EQ (glyph->object, before_string))
27150 {
27151 pos = string_buffer_position (before_string, start_charpos);
27152 /* If pos == 0, it means before_string came from an
27153 overlay, not from a buffer position. */
27154 if (!pos || (pos >= start_charpos && pos < end_charpos))
27155 break;
27156 }
27157 else if (EQ (glyph->object, after_string))
27158 {
27159 pos = string_buffer_position (after_string, end_charpos);
27160 if (!pos || (pos >= start_charpos && pos < end_charpos))
27161 break;
27162 }
27163 }
27164
27165 glyph++; /* first glyph to the right of the highlighted area */
27166 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
27167 x += g->pixel_width;
27168 hlinfo->mouse_face_beg_x = x;
27169 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27170 }
27171
27172 /* If the highlight ends in a different row, compute GLYPH and END
27173 for the end row. Otherwise, reuse the values computed above for
27174 the row where the highlight begins. */
27175 if (r2 != r1)
27176 {
27177 if (!r2->reversed_p)
27178 {
27179 glyph = r2->glyphs[TEXT_AREA];
27180 end = glyph + r2->used[TEXT_AREA];
27181 x = r2->x;
27182 }
27183 else
27184 {
27185 end = r2->glyphs[TEXT_AREA] - 1;
27186 glyph = end + r2->used[TEXT_AREA];
27187 }
27188 }
27189
27190 if (!r2->reversed_p)
27191 {
27192 /* Skip truncation and continuation glyphs near the end of the
27193 row, and also blanks and stretch glyphs inserted by
27194 extend_face_to_end_of_line. */
27195 while (end > glyph
27196 && INTEGERP ((end - 1)->object))
27197 --end;
27198 /* Scan the rest of the glyph row from the end, looking for the
27199 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27200 DISP_STRING, or whose position is between START_CHARPOS
27201 and END_CHARPOS */
27202 for (--end;
27203 end > glyph
27204 && !INTEGERP (end->object)
27205 && !EQ (end->object, disp_string)
27206 && !(BUFFERP (end->object)
27207 && (end->charpos >= start_charpos
27208 && end->charpos < end_charpos));
27209 --end)
27210 {
27211 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27212 are present at buffer positions between START_CHARPOS and
27213 END_CHARPOS, or if they come from an overlay. */
27214 if (EQ (end->object, before_string))
27215 {
27216 pos = string_buffer_position (before_string, start_charpos);
27217 if (!pos || (pos >= start_charpos && pos < end_charpos))
27218 break;
27219 }
27220 else if (EQ (end->object, after_string))
27221 {
27222 pos = string_buffer_position (after_string, end_charpos);
27223 if (!pos || (pos >= start_charpos && pos < end_charpos))
27224 break;
27225 }
27226 }
27227 /* Find the X coordinate of the last glyph to be highlighted. */
27228 for (; glyph <= end; ++glyph)
27229 x += glyph->pixel_width;
27230
27231 hlinfo->mouse_face_end_x = x;
27232 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
27233 }
27234 else
27235 {
27236 /* Skip truncation and continuation glyphs near the end of the
27237 row, and also blanks and stretch glyphs inserted by
27238 extend_face_to_end_of_line. */
27239 x = r2->x;
27240 end++;
27241 while (end < glyph
27242 && INTEGERP (end->object))
27243 {
27244 x += end->pixel_width;
27245 ++end;
27246 }
27247 /* Scan the rest of the glyph row from the end, looking for the
27248 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27249 DISP_STRING, or whose position is between START_CHARPOS
27250 and END_CHARPOS */
27251 for ( ;
27252 end < glyph
27253 && !INTEGERP (end->object)
27254 && !EQ (end->object, disp_string)
27255 && !(BUFFERP (end->object)
27256 && (end->charpos >= start_charpos
27257 && end->charpos < end_charpos));
27258 ++end)
27259 {
27260 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27261 are present at buffer positions between START_CHARPOS and
27262 END_CHARPOS, or if they come from an overlay. */
27263 if (EQ (end->object, before_string))
27264 {
27265 pos = string_buffer_position (before_string, start_charpos);
27266 if (!pos || (pos >= start_charpos && pos < end_charpos))
27267 break;
27268 }
27269 else if (EQ (end->object, after_string))
27270 {
27271 pos = string_buffer_position (after_string, end_charpos);
27272 if (!pos || (pos >= start_charpos && pos < end_charpos))
27273 break;
27274 }
27275 x += end->pixel_width;
27276 }
27277 /* If we exited the above loop because we arrived at the last
27278 glyph of the row, and its buffer position is still not in
27279 range, it means the last character in range is the preceding
27280 newline. Bump the end column and x values to get past the
27281 last glyph. */
27282 if (end == glyph
27283 && BUFFERP (end->object)
27284 && (end->charpos < start_charpos
27285 || end->charpos >= end_charpos))
27286 {
27287 x += end->pixel_width;
27288 ++end;
27289 }
27290 hlinfo->mouse_face_end_x = x;
27291 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
27292 }
27293
27294 hlinfo->mouse_face_window = window;
27295 hlinfo->mouse_face_face_id
27296 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
27297 mouse_charpos + 1,
27298 !hlinfo->mouse_face_hidden, -1);
27299 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27300 }
27301
27302 /* The following function is not used anymore (replaced with
27303 mouse_face_from_string_pos), but I leave it here for the time
27304 being, in case someone would. */
27305
27306 #if 0 /* not used */
27307
27308 /* Find the position of the glyph for position POS in OBJECT in
27309 window W's current matrix, and return in *X, *Y the pixel
27310 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
27311
27312 RIGHT_P non-zero means return the position of the right edge of the
27313 glyph, RIGHT_P zero means return the left edge position.
27314
27315 If no glyph for POS exists in the matrix, return the position of
27316 the glyph with the next smaller position that is in the matrix, if
27317 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
27318 exists in the matrix, return the position of the glyph with the
27319 next larger position in OBJECT.
27320
27321 Value is non-zero if a glyph was found. */
27322
27323 static int
27324 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
27325 int *hpos, int *vpos, int *x, int *y, int right_p)
27326 {
27327 int yb = window_text_bottom_y (w);
27328 struct glyph_row *r;
27329 struct glyph *best_glyph = NULL;
27330 struct glyph_row *best_row = NULL;
27331 int best_x = 0;
27332
27333 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27334 r->enabled_p && r->y < yb;
27335 ++r)
27336 {
27337 struct glyph *g = r->glyphs[TEXT_AREA];
27338 struct glyph *e = g + r->used[TEXT_AREA];
27339 int gx;
27340
27341 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27342 if (EQ (g->object, object))
27343 {
27344 if (g->charpos == pos)
27345 {
27346 best_glyph = g;
27347 best_x = gx;
27348 best_row = r;
27349 goto found;
27350 }
27351 else if (best_glyph == NULL
27352 || ((eabs (g->charpos - pos)
27353 < eabs (best_glyph->charpos - pos))
27354 && (right_p
27355 ? g->charpos < pos
27356 : g->charpos > pos)))
27357 {
27358 best_glyph = g;
27359 best_x = gx;
27360 best_row = r;
27361 }
27362 }
27363 }
27364
27365 found:
27366
27367 if (best_glyph)
27368 {
27369 *x = best_x;
27370 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
27371
27372 if (right_p)
27373 {
27374 *x += best_glyph->pixel_width;
27375 ++*hpos;
27376 }
27377
27378 *y = best_row->y;
27379 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
27380 }
27381
27382 return best_glyph != NULL;
27383 }
27384 #endif /* not used */
27385
27386 /* Find the positions of the first and the last glyphs in window W's
27387 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
27388 (assumed to be a string), and return in HLINFO's mouse_face_*
27389 members the pixel and column/row coordinates of those glyphs. */
27390
27391 static void
27392 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
27393 Lisp_Object object,
27394 ptrdiff_t startpos, ptrdiff_t endpos)
27395 {
27396 int yb = window_text_bottom_y (w);
27397 struct glyph_row *r;
27398 struct glyph *g, *e;
27399 int gx;
27400 int found = 0;
27401
27402 /* Find the glyph row with at least one position in the range
27403 [STARTPOS..ENDPOS], and the first glyph in that row whose
27404 position belongs to that range. */
27405 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27406 r->enabled_p && r->y < yb;
27407 ++r)
27408 {
27409 if (!r->reversed_p)
27410 {
27411 g = r->glyphs[TEXT_AREA];
27412 e = g + r->used[TEXT_AREA];
27413 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27414 if (EQ (g->object, object)
27415 && startpos <= g->charpos && g->charpos <= endpos)
27416 {
27417 hlinfo->mouse_face_beg_row
27418 = MATRIX_ROW_VPOS (r, w->current_matrix);
27419 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27420 hlinfo->mouse_face_beg_x = gx;
27421 found = 1;
27422 break;
27423 }
27424 }
27425 else
27426 {
27427 struct glyph *g1;
27428
27429 e = r->glyphs[TEXT_AREA];
27430 g = e + r->used[TEXT_AREA];
27431 for ( ; g > e; --g)
27432 if (EQ ((g-1)->object, object)
27433 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
27434 {
27435 hlinfo->mouse_face_beg_row
27436 = MATRIX_ROW_VPOS (r, w->current_matrix);
27437 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27438 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27439 gx += g1->pixel_width;
27440 hlinfo->mouse_face_beg_x = gx;
27441 found = 1;
27442 break;
27443 }
27444 }
27445 if (found)
27446 break;
27447 }
27448
27449 if (!found)
27450 return;
27451
27452 /* Starting with the next row, look for the first row which does NOT
27453 include any glyphs whose positions are in the range. */
27454 for (++r; r->enabled_p && r->y < yb; ++r)
27455 {
27456 g = r->glyphs[TEXT_AREA];
27457 e = g + r->used[TEXT_AREA];
27458 found = 0;
27459 for ( ; g < e; ++g)
27460 if (EQ (g->object, object)
27461 && startpos <= g->charpos && g->charpos <= endpos)
27462 {
27463 found = 1;
27464 break;
27465 }
27466 if (!found)
27467 break;
27468 }
27469
27470 /* The highlighted region ends on the previous row. */
27471 r--;
27472
27473 /* Set the end row. */
27474 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
27475
27476 /* Compute and set the end column and the end column's horizontal
27477 pixel coordinate. */
27478 if (!r->reversed_p)
27479 {
27480 g = r->glyphs[TEXT_AREA];
27481 e = g + r->used[TEXT_AREA];
27482 for ( ; e > g; --e)
27483 if (EQ ((e-1)->object, object)
27484 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
27485 break;
27486 hlinfo->mouse_face_end_col = e - g;
27487
27488 for (gx = r->x; g < e; ++g)
27489 gx += g->pixel_width;
27490 hlinfo->mouse_face_end_x = gx;
27491 }
27492 else
27493 {
27494 e = r->glyphs[TEXT_AREA];
27495 g = e + r->used[TEXT_AREA];
27496 for (gx = r->x ; e < g; ++e)
27497 {
27498 if (EQ (e->object, object)
27499 && startpos <= e->charpos && e->charpos <= endpos)
27500 break;
27501 gx += e->pixel_width;
27502 }
27503 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27504 hlinfo->mouse_face_end_x = gx;
27505 }
27506 }
27507
27508 #ifdef HAVE_WINDOW_SYSTEM
27509
27510 /* See if position X, Y is within a hot-spot of an image. */
27511
27512 static int
27513 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27514 {
27515 if (!CONSP (hot_spot))
27516 return 0;
27517
27518 if (EQ (XCAR (hot_spot), Qrect))
27519 {
27520 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27521 Lisp_Object rect = XCDR (hot_spot);
27522 Lisp_Object tem;
27523 if (!CONSP (rect))
27524 return 0;
27525 if (!CONSP (XCAR (rect)))
27526 return 0;
27527 if (!CONSP (XCDR (rect)))
27528 return 0;
27529 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27530 return 0;
27531 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27532 return 0;
27533 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27534 return 0;
27535 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27536 return 0;
27537 return 1;
27538 }
27539 else if (EQ (XCAR (hot_spot), Qcircle))
27540 {
27541 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27542 Lisp_Object circ = XCDR (hot_spot);
27543 Lisp_Object lr, lx0, ly0;
27544 if (CONSP (circ)
27545 && CONSP (XCAR (circ))
27546 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27547 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27548 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27549 {
27550 double r = XFLOATINT (lr);
27551 double dx = XINT (lx0) - x;
27552 double dy = XINT (ly0) - y;
27553 return (dx * dx + dy * dy <= r * r);
27554 }
27555 }
27556 else if (EQ (XCAR (hot_spot), Qpoly))
27557 {
27558 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27559 if (VECTORP (XCDR (hot_spot)))
27560 {
27561 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27562 Lisp_Object *poly = v->contents;
27563 ptrdiff_t n = v->header.size;
27564 ptrdiff_t i;
27565 int inside = 0;
27566 Lisp_Object lx, ly;
27567 int x0, y0;
27568
27569 /* Need an even number of coordinates, and at least 3 edges. */
27570 if (n < 6 || n & 1)
27571 return 0;
27572
27573 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27574 If count is odd, we are inside polygon. Pixels on edges
27575 may or may not be included depending on actual geometry of the
27576 polygon. */
27577 if ((lx = poly[n-2], !INTEGERP (lx))
27578 || (ly = poly[n-1], !INTEGERP (lx)))
27579 return 0;
27580 x0 = XINT (lx), y0 = XINT (ly);
27581 for (i = 0; i < n; i += 2)
27582 {
27583 int x1 = x0, y1 = y0;
27584 if ((lx = poly[i], !INTEGERP (lx))
27585 || (ly = poly[i+1], !INTEGERP (ly)))
27586 return 0;
27587 x0 = XINT (lx), y0 = XINT (ly);
27588
27589 /* Does this segment cross the X line? */
27590 if (x0 >= x)
27591 {
27592 if (x1 >= x)
27593 continue;
27594 }
27595 else if (x1 < x)
27596 continue;
27597 if (y > y0 && y > y1)
27598 continue;
27599 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27600 inside = !inside;
27601 }
27602 return inside;
27603 }
27604 }
27605 return 0;
27606 }
27607
27608 Lisp_Object
27609 find_hot_spot (Lisp_Object map, int x, int y)
27610 {
27611 while (CONSP (map))
27612 {
27613 if (CONSP (XCAR (map))
27614 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27615 return XCAR (map);
27616 map = XCDR (map);
27617 }
27618
27619 return Qnil;
27620 }
27621
27622 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27623 3, 3, 0,
27624 doc: /* Lookup in image map MAP coordinates X and Y.
27625 An image map is an alist where each element has the format (AREA ID PLIST).
27626 An AREA is specified as either a rectangle, a circle, or a polygon:
27627 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27628 pixel coordinates of the upper left and bottom right corners.
27629 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27630 and the radius of the circle; r may be a float or integer.
27631 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27632 vector describes one corner in the polygon.
27633 Returns the alist element for the first matching AREA in MAP. */)
27634 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27635 {
27636 if (NILP (map))
27637 return Qnil;
27638
27639 CHECK_NUMBER (x);
27640 CHECK_NUMBER (y);
27641
27642 return find_hot_spot (map,
27643 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27644 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27645 }
27646
27647
27648 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27649 static void
27650 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27651 {
27652 /* Do not change cursor shape while dragging mouse. */
27653 if (!NILP (do_mouse_tracking))
27654 return;
27655
27656 if (!NILP (pointer))
27657 {
27658 if (EQ (pointer, Qarrow))
27659 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27660 else if (EQ (pointer, Qhand))
27661 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27662 else if (EQ (pointer, Qtext))
27663 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27664 else if (EQ (pointer, intern ("hdrag")))
27665 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27666 #ifdef HAVE_X_WINDOWS
27667 else if (EQ (pointer, intern ("vdrag")))
27668 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27669 #endif
27670 else if (EQ (pointer, intern ("hourglass")))
27671 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27672 else if (EQ (pointer, Qmodeline))
27673 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27674 else
27675 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27676 }
27677
27678 if (cursor != No_Cursor)
27679 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27680 }
27681
27682 #endif /* HAVE_WINDOW_SYSTEM */
27683
27684 /* Take proper action when mouse has moved to the mode or header line
27685 or marginal area AREA of window W, x-position X and y-position Y.
27686 X is relative to the start of the text display area of W, so the
27687 width of bitmap areas and scroll bars must be subtracted to get a
27688 position relative to the start of the mode line. */
27689
27690 static void
27691 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27692 enum window_part area)
27693 {
27694 struct window *w = XWINDOW (window);
27695 struct frame *f = XFRAME (w->frame);
27696 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27697 #ifdef HAVE_WINDOW_SYSTEM
27698 Display_Info *dpyinfo;
27699 #endif
27700 Cursor cursor = No_Cursor;
27701 Lisp_Object pointer = Qnil;
27702 int dx, dy, width, height;
27703 ptrdiff_t charpos;
27704 Lisp_Object string, object = Qnil;
27705 Lisp_Object pos IF_LINT (= Qnil), help;
27706
27707 Lisp_Object mouse_face;
27708 int original_x_pixel = x;
27709 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27710 struct glyph_row *row IF_LINT (= 0);
27711
27712 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27713 {
27714 int x0;
27715 struct glyph *end;
27716
27717 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27718 returns them in row/column units! */
27719 string = mode_line_string (w, area, &x, &y, &charpos,
27720 &object, &dx, &dy, &width, &height);
27721
27722 row = (area == ON_MODE_LINE
27723 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27724 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27725
27726 /* Find the glyph under the mouse pointer. */
27727 if (row->mode_line_p && row->enabled_p)
27728 {
27729 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27730 end = glyph + row->used[TEXT_AREA];
27731
27732 for (x0 = original_x_pixel;
27733 glyph < end && x0 >= glyph->pixel_width;
27734 ++glyph)
27735 x0 -= glyph->pixel_width;
27736
27737 if (glyph >= end)
27738 glyph = NULL;
27739 }
27740 }
27741 else
27742 {
27743 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27744 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27745 returns them in row/column units! */
27746 string = marginal_area_string (w, area, &x, &y, &charpos,
27747 &object, &dx, &dy, &width, &height);
27748 }
27749
27750 help = Qnil;
27751
27752 #ifdef HAVE_WINDOW_SYSTEM
27753 if (IMAGEP (object))
27754 {
27755 Lisp_Object image_map, hotspot;
27756 if ((image_map = Fplist_get (XCDR (object), QCmap),
27757 !NILP (image_map))
27758 && (hotspot = find_hot_spot (image_map, dx, dy),
27759 CONSP (hotspot))
27760 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27761 {
27762 Lisp_Object plist;
27763
27764 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27765 If so, we could look for mouse-enter, mouse-leave
27766 properties in PLIST (and do something...). */
27767 hotspot = XCDR (hotspot);
27768 if (CONSP (hotspot)
27769 && (plist = XCAR (hotspot), CONSP (plist)))
27770 {
27771 pointer = Fplist_get (plist, Qpointer);
27772 if (NILP (pointer))
27773 pointer = Qhand;
27774 help = Fplist_get (plist, Qhelp_echo);
27775 if (!NILP (help))
27776 {
27777 help_echo_string = help;
27778 XSETWINDOW (help_echo_window, w);
27779 help_echo_object = w->contents;
27780 help_echo_pos = charpos;
27781 }
27782 }
27783 }
27784 if (NILP (pointer))
27785 pointer = Fplist_get (XCDR (object), QCpointer);
27786 }
27787 #endif /* HAVE_WINDOW_SYSTEM */
27788
27789 if (STRINGP (string))
27790 pos = make_number (charpos);
27791
27792 /* Set the help text and mouse pointer. If the mouse is on a part
27793 of the mode line without any text (e.g. past the right edge of
27794 the mode line text), use the default help text and pointer. */
27795 if (STRINGP (string) || area == ON_MODE_LINE)
27796 {
27797 /* Arrange to display the help by setting the global variables
27798 help_echo_string, help_echo_object, and help_echo_pos. */
27799 if (NILP (help))
27800 {
27801 if (STRINGP (string))
27802 help = Fget_text_property (pos, Qhelp_echo, string);
27803
27804 if (!NILP (help))
27805 {
27806 help_echo_string = help;
27807 XSETWINDOW (help_echo_window, w);
27808 help_echo_object = string;
27809 help_echo_pos = charpos;
27810 }
27811 else if (area == ON_MODE_LINE)
27812 {
27813 Lisp_Object default_help
27814 = buffer_local_value_1 (Qmode_line_default_help_echo,
27815 w->contents);
27816
27817 if (STRINGP (default_help))
27818 {
27819 help_echo_string = default_help;
27820 XSETWINDOW (help_echo_window, w);
27821 help_echo_object = Qnil;
27822 help_echo_pos = -1;
27823 }
27824 }
27825 }
27826
27827 #ifdef HAVE_WINDOW_SYSTEM
27828 /* Change the mouse pointer according to what is under it. */
27829 if (FRAME_WINDOW_P (f))
27830 {
27831 dpyinfo = FRAME_X_DISPLAY_INFO (f);
27832 if (STRINGP (string))
27833 {
27834 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27835
27836 if (NILP (pointer))
27837 pointer = Fget_text_property (pos, Qpointer, string);
27838
27839 /* Change the mouse pointer according to what is under X/Y. */
27840 if (NILP (pointer)
27841 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27842 {
27843 Lisp_Object map;
27844 map = Fget_text_property (pos, Qlocal_map, string);
27845 if (!KEYMAPP (map))
27846 map = Fget_text_property (pos, Qkeymap, string);
27847 if (!KEYMAPP (map))
27848 cursor = dpyinfo->vertical_scroll_bar_cursor;
27849 }
27850 }
27851 else
27852 /* Default mode-line pointer. */
27853 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27854 }
27855 #endif
27856 }
27857
27858 /* Change the mouse face according to what is under X/Y. */
27859 if (STRINGP (string))
27860 {
27861 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27862 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
27863 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27864 && glyph)
27865 {
27866 Lisp_Object b, e;
27867
27868 struct glyph * tmp_glyph;
27869
27870 int gpos;
27871 int gseq_length;
27872 int total_pixel_width;
27873 ptrdiff_t begpos, endpos, ignore;
27874
27875 int vpos, hpos;
27876
27877 b = Fprevious_single_property_change (make_number (charpos + 1),
27878 Qmouse_face, string, Qnil);
27879 if (NILP (b))
27880 begpos = 0;
27881 else
27882 begpos = XINT (b);
27883
27884 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27885 if (NILP (e))
27886 endpos = SCHARS (string);
27887 else
27888 endpos = XINT (e);
27889
27890 /* Calculate the glyph position GPOS of GLYPH in the
27891 displayed string, relative to the beginning of the
27892 highlighted part of the string.
27893
27894 Note: GPOS is different from CHARPOS. CHARPOS is the
27895 position of GLYPH in the internal string object. A mode
27896 line string format has structures which are converted to
27897 a flattened string by the Emacs Lisp interpreter. The
27898 internal string is an element of those structures. The
27899 displayed string is the flattened string. */
27900 tmp_glyph = row_start_glyph;
27901 while (tmp_glyph < glyph
27902 && (!(EQ (tmp_glyph->object, glyph->object)
27903 && begpos <= tmp_glyph->charpos
27904 && tmp_glyph->charpos < endpos)))
27905 tmp_glyph++;
27906 gpos = glyph - tmp_glyph;
27907
27908 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27909 the highlighted part of the displayed string to which
27910 GLYPH belongs. Note: GSEQ_LENGTH is different from
27911 SCHARS (STRING), because the latter returns the length of
27912 the internal string. */
27913 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27914 tmp_glyph > glyph
27915 && (!(EQ (tmp_glyph->object, glyph->object)
27916 && begpos <= tmp_glyph->charpos
27917 && tmp_glyph->charpos < endpos));
27918 tmp_glyph--)
27919 ;
27920 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27921
27922 /* Calculate the total pixel width of all the glyphs between
27923 the beginning of the highlighted area and GLYPH. */
27924 total_pixel_width = 0;
27925 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27926 total_pixel_width += tmp_glyph->pixel_width;
27927
27928 /* Pre calculation of re-rendering position. Note: X is in
27929 column units here, after the call to mode_line_string or
27930 marginal_area_string. */
27931 hpos = x - gpos;
27932 vpos = (area == ON_MODE_LINE
27933 ? (w->current_matrix)->nrows - 1
27934 : 0);
27935
27936 /* If GLYPH's position is included in the region that is
27937 already drawn in mouse face, we have nothing to do. */
27938 if ( EQ (window, hlinfo->mouse_face_window)
27939 && (!row->reversed_p
27940 ? (hlinfo->mouse_face_beg_col <= hpos
27941 && hpos < hlinfo->mouse_face_end_col)
27942 /* In R2L rows we swap BEG and END, see below. */
27943 : (hlinfo->mouse_face_end_col <= hpos
27944 && hpos < hlinfo->mouse_face_beg_col))
27945 && hlinfo->mouse_face_beg_row == vpos )
27946 return;
27947
27948 if (clear_mouse_face (hlinfo))
27949 cursor = No_Cursor;
27950
27951 if (!row->reversed_p)
27952 {
27953 hlinfo->mouse_face_beg_col = hpos;
27954 hlinfo->mouse_face_beg_x = original_x_pixel
27955 - (total_pixel_width + dx);
27956 hlinfo->mouse_face_end_col = hpos + gseq_length;
27957 hlinfo->mouse_face_end_x = 0;
27958 }
27959 else
27960 {
27961 /* In R2L rows, show_mouse_face expects BEG and END
27962 coordinates to be swapped. */
27963 hlinfo->mouse_face_end_col = hpos;
27964 hlinfo->mouse_face_end_x = original_x_pixel
27965 - (total_pixel_width + dx);
27966 hlinfo->mouse_face_beg_col = hpos + gseq_length;
27967 hlinfo->mouse_face_beg_x = 0;
27968 }
27969
27970 hlinfo->mouse_face_beg_row = vpos;
27971 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
27972 hlinfo->mouse_face_past_end = 0;
27973 hlinfo->mouse_face_window = window;
27974
27975 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
27976 charpos,
27977 0, 0, 0,
27978 &ignore,
27979 glyph->face_id,
27980 1);
27981 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27982
27983 if (NILP (pointer))
27984 pointer = Qhand;
27985 }
27986 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27987 clear_mouse_face (hlinfo);
27988 }
27989 #ifdef HAVE_WINDOW_SYSTEM
27990 if (FRAME_WINDOW_P (f))
27991 define_frame_cursor1 (f, cursor, pointer);
27992 #endif
27993 }
27994
27995
27996 /* EXPORT:
27997 Take proper action when the mouse has moved to position X, Y on
27998 frame F with regards to highlighting portions of display that have
27999 mouse-face properties. Also de-highlight portions of display where
28000 the mouse was before, set the mouse pointer shape as appropriate
28001 for the mouse coordinates, and activate help echo (tooltips).
28002 X and Y can be negative or out of range. */
28003
28004 void
28005 note_mouse_highlight (struct frame *f, int x, int y)
28006 {
28007 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28008 enum window_part part = ON_NOTHING;
28009 Lisp_Object window;
28010 struct window *w;
28011 Cursor cursor = No_Cursor;
28012 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
28013 struct buffer *b;
28014
28015 /* When a menu is active, don't highlight because this looks odd. */
28016 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
28017 if (popup_activated ())
28018 return;
28019 #endif
28020
28021 if (!f->glyphs_initialized_p
28022 || f->pointer_invisible)
28023 return;
28024
28025 hlinfo->mouse_face_mouse_x = x;
28026 hlinfo->mouse_face_mouse_y = y;
28027 hlinfo->mouse_face_mouse_frame = f;
28028
28029 if (hlinfo->mouse_face_defer)
28030 return;
28031
28032 /* Which window is that in? */
28033 window = window_from_coordinates (f, x, y, &part, 1);
28034
28035 /* If displaying active text in another window, clear that. */
28036 if (! EQ (window, hlinfo->mouse_face_window)
28037 /* Also clear if we move out of text area in same window. */
28038 || (!NILP (hlinfo->mouse_face_window)
28039 && !NILP (window)
28040 && part != ON_TEXT
28041 && part != ON_MODE_LINE
28042 && part != ON_HEADER_LINE))
28043 clear_mouse_face (hlinfo);
28044
28045 /* Not on a window -> return. */
28046 if (!WINDOWP (window))
28047 return;
28048
28049 /* Reset help_echo_string. It will get recomputed below. */
28050 help_echo_string = Qnil;
28051
28052 /* Convert to window-relative pixel coordinates. */
28053 w = XWINDOW (window);
28054 frame_to_window_pixel_xy (w, &x, &y);
28055
28056 #ifdef HAVE_WINDOW_SYSTEM
28057 /* Handle tool-bar window differently since it doesn't display a
28058 buffer. */
28059 if (EQ (window, f->tool_bar_window))
28060 {
28061 note_tool_bar_highlight (f, x, y);
28062 return;
28063 }
28064 #endif
28065
28066 /* Mouse is on the mode, header line or margin? */
28067 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
28068 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
28069 {
28070 note_mode_line_or_margin_highlight (window, x, y, part);
28071 return;
28072 }
28073
28074 #ifdef HAVE_WINDOW_SYSTEM
28075 if (part == ON_VERTICAL_BORDER)
28076 {
28077 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28078 help_echo_string = build_string ("drag-mouse-1: resize");
28079 }
28080 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
28081 || part == ON_SCROLL_BAR)
28082 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28083 else
28084 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28085 #endif
28086
28087 /* Are we in a window whose display is up to date?
28088 And verify the buffer's text has not changed. */
28089 b = XBUFFER (w->contents);
28090 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
28091 {
28092 int hpos, vpos, dx, dy, area = LAST_AREA;
28093 ptrdiff_t pos;
28094 struct glyph *glyph;
28095 Lisp_Object object;
28096 Lisp_Object mouse_face = Qnil, position;
28097 Lisp_Object *overlay_vec = NULL;
28098 ptrdiff_t i, noverlays;
28099 struct buffer *obuf;
28100 ptrdiff_t obegv, ozv;
28101 int same_region;
28102
28103 /* Find the glyph under X/Y. */
28104 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
28105
28106 #ifdef HAVE_WINDOW_SYSTEM
28107 /* Look for :pointer property on image. */
28108 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28109 {
28110 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
28111 if (img != NULL && IMAGEP (img->spec))
28112 {
28113 Lisp_Object image_map, hotspot;
28114 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
28115 !NILP (image_map))
28116 && (hotspot = find_hot_spot (image_map,
28117 glyph->slice.img.x + dx,
28118 glyph->slice.img.y + dy),
28119 CONSP (hotspot))
28120 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28121 {
28122 Lisp_Object plist;
28123
28124 /* Could check XCAR (hotspot) to see if we enter/leave
28125 this hot-spot.
28126 If so, we could look for mouse-enter, mouse-leave
28127 properties in PLIST (and do something...). */
28128 hotspot = XCDR (hotspot);
28129 if (CONSP (hotspot)
28130 && (plist = XCAR (hotspot), CONSP (plist)))
28131 {
28132 pointer = Fplist_get (plist, Qpointer);
28133 if (NILP (pointer))
28134 pointer = Qhand;
28135 help_echo_string = Fplist_get (plist, Qhelp_echo);
28136 if (!NILP (help_echo_string))
28137 {
28138 help_echo_window = window;
28139 help_echo_object = glyph->object;
28140 help_echo_pos = glyph->charpos;
28141 }
28142 }
28143 }
28144 if (NILP (pointer))
28145 pointer = Fplist_get (XCDR (img->spec), QCpointer);
28146 }
28147 }
28148 #endif /* HAVE_WINDOW_SYSTEM */
28149
28150 /* Clear mouse face if X/Y not over text. */
28151 if (glyph == NULL
28152 || area != TEXT_AREA
28153 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
28154 /* Glyph's OBJECT is an integer for glyphs inserted by the
28155 display engine for its internal purposes, like truncation
28156 and continuation glyphs and blanks beyond the end of
28157 line's text on text terminals. If we are over such a
28158 glyph, we are not over any text. */
28159 || INTEGERP (glyph->object)
28160 /* R2L rows have a stretch glyph at their front, which
28161 stands for no text, whereas L2R rows have no glyphs at
28162 all beyond the end of text. Treat such stretch glyphs
28163 like we do with NULL glyphs in L2R rows. */
28164 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
28165 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
28166 && glyph->type == STRETCH_GLYPH
28167 && glyph->avoid_cursor_p))
28168 {
28169 if (clear_mouse_face (hlinfo))
28170 cursor = No_Cursor;
28171 #ifdef HAVE_WINDOW_SYSTEM
28172 if (FRAME_WINDOW_P (f) && NILP (pointer))
28173 {
28174 if (area != TEXT_AREA)
28175 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28176 else
28177 pointer = Vvoid_text_area_pointer;
28178 }
28179 #endif
28180 goto set_cursor;
28181 }
28182
28183 pos = glyph->charpos;
28184 object = glyph->object;
28185 if (!STRINGP (object) && !BUFFERP (object))
28186 goto set_cursor;
28187
28188 /* If we get an out-of-range value, return now; avoid an error. */
28189 if (BUFFERP (object) && pos > BUF_Z (b))
28190 goto set_cursor;
28191
28192 /* Make the window's buffer temporarily current for
28193 overlays_at and compute_char_face. */
28194 obuf = current_buffer;
28195 current_buffer = b;
28196 obegv = BEGV;
28197 ozv = ZV;
28198 BEGV = BEG;
28199 ZV = Z;
28200
28201 /* Is this char mouse-active or does it have help-echo? */
28202 position = make_number (pos);
28203
28204 if (BUFFERP (object))
28205 {
28206 /* Put all the overlays we want in a vector in overlay_vec. */
28207 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
28208 /* Sort overlays into increasing priority order. */
28209 noverlays = sort_overlays (overlay_vec, noverlays, w);
28210 }
28211 else
28212 noverlays = 0;
28213
28214 if (NILP (Vmouse_highlight))
28215 {
28216 clear_mouse_face (hlinfo);
28217 goto check_help_echo;
28218 }
28219
28220 same_region = coords_in_mouse_face_p (w, hpos, vpos);
28221
28222 if (same_region)
28223 cursor = No_Cursor;
28224
28225 /* Check mouse-face highlighting. */
28226 if (! same_region
28227 /* If there exists an overlay with mouse-face overlapping
28228 the one we are currently highlighting, we have to
28229 check if we enter the overlapping overlay, and then
28230 highlight only that. */
28231 || (OVERLAYP (hlinfo->mouse_face_overlay)
28232 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
28233 {
28234 /* Find the highest priority overlay with a mouse-face. */
28235 Lisp_Object overlay = Qnil;
28236 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
28237 {
28238 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
28239 if (!NILP (mouse_face))
28240 overlay = overlay_vec[i];
28241 }
28242
28243 /* If we're highlighting the same overlay as before, there's
28244 no need to do that again. */
28245 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
28246 goto check_help_echo;
28247 hlinfo->mouse_face_overlay = overlay;
28248
28249 /* Clear the display of the old active region, if any. */
28250 if (clear_mouse_face (hlinfo))
28251 cursor = No_Cursor;
28252
28253 /* If no overlay applies, get a text property. */
28254 if (NILP (overlay))
28255 mouse_face = Fget_text_property (position, Qmouse_face, object);
28256
28257 /* Next, compute the bounds of the mouse highlighting and
28258 display it. */
28259 if (!NILP (mouse_face) && STRINGP (object))
28260 {
28261 /* The mouse-highlighting comes from a display string
28262 with a mouse-face. */
28263 Lisp_Object s, e;
28264 ptrdiff_t ignore;
28265
28266 s = Fprevious_single_property_change
28267 (make_number (pos + 1), Qmouse_face, object, Qnil);
28268 e = Fnext_single_property_change
28269 (position, Qmouse_face, object, Qnil);
28270 if (NILP (s))
28271 s = make_number (0);
28272 if (NILP (e))
28273 e = make_number (SCHARS (object) - 1);
28274 mouse_face_from_string_pos (w, hlinfo, object,
28275 XINT (s), XINT (e));
28276 hlinfo->mouse_face_past_end = 0;
28277 hlinfo->mouse_face_window = window;
28278 hlinfo->mouse_face_face_id
28279 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
28280 glyph->face_id, 1);
28281 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28282 cursor = No_Cursor;
28283 }
28284 else
28285 {
28286 /* The mouse-highlighting, if any, comes from an overlay
28287 or text property in the buffer. */
28288 Lisp_Object buffer IF_LINT (= Qnil);
28289 Lisp_Object disp_string IF_LINT (= Qnil);
28290
28291 if (STRINGP (object))
28292 {
28293 /* If we are on a display string with no mouse-face,
28294 check if the text under it has one. */
28295 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
28296 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28297 pos = string_buffer_position (object, start);
28298 if (pos > 0)
28299 {
28300 mouse_face = get_char_property_and_overlay
28301 (make_number (pos), Qmouse_face, w->contents, &overlay);
28302 buffer = w->contents;
28303 disp_string = object;
28304 }
28305 }
28306 else
28307 {
28308 buffer = object;
28309 disp_string = Qnil;
28310 }
28311
28312 if (!NILP (mouse_face))
28313 {
28314 Lisp_Object before, after;
28315 Lisp_Object before_string, after_string;
28316 /* To correctly find the limits of mouse highlight
28317 in a bidi-reordered buffer, we must not use the
28318 optimization of limiting the search in
28319 previous-single-property-change and
28320 next-single-property-change, because
28321 rows_from_pos_range needs the real start and end
28322 positions to DTRT in this case. That's because
28323 the first row visible in a window does not
28324 necessarily display the character whose position
28325 is the smallest. */
28326 Lisp_Object lim1 =
28327 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28328 ? Fmarker_position (w->start)
28329 : Qnil;
28330 Lisp_Object lim2 =
28331 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28332 ? make_number (BUF_Z (XBUFFER (buffer)) - w->window_end_pos)
28333 : Qnil;
28334
28335 if (NILP (overlay))
28336 {
28337 /* Handle the text property case. */
28338 before = Fprevious_single_property_change
28339 (make_number (pos + 1), Qmouse_face, buffer, lim1);
28340 after = Fnext_single_property_change
28341 (make_number (pos), Qmouse_face, buffer, lim2);
28342 before_string = after_string = Qnil;
28343 }
28344 else
28345 {
28346 /* Handle the overlay case. */
28347 before = Foverlay_start (overlay);
28348 after = Foverlay_end (overlay);
28349 before_string = Foverlay_get (overlay, Qbefore_string);
28350 after_string = Foverlay_get (overlay, Qafter_string);
28351
28352 if (!STRINGP (before_string)) before_string = Qnil;
28353 if (!STRINGP (after_string)) after_string = Qnil;
28354 }
28355
28356 mouse_face_from_buffer_pos (window, hlinfo, pos,
28357 NILP (before)
28358 ? 1
28359 : XFASTINT (before),
28360 NILP (after)
28361 ? BUF_Z (XBUFFER (buffer))
28362 : XFASTINT (after),
28363 before_string, after_string,
28364 disp_string);
28365 cursor = No_Cursor;
28366 }
28367 }
28368 }
28369
28370 check_help_echo:
28371
28372 /* Look for a `help-echo' property. */
28373 if (NILP (help_echo_string)) {
28374 Lisp_Object help, overlay;
28375
28376 /* Check overlays first. */
28377 help = overlay = Qnil;
28378 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
28379 {
28380 overlay = overlay_vec[i];
28381 help = Foverlay_get (overlay, Qhelp_echo);
28382 }
28383
28384 if (!NILP (help))
28385 {
28386 help_echo_string = help;
28387 help_echo_window = window;
28388 help_echo_object = overlay;
28389 help_echo_pos = pos;
28390 }
28391 else
28392 {
28393 Lisp_Object obj = glyph->object;
28394 ptrdiff_t charpos = glyph->charpos;
28395
28396 /* Try text properties. */
28397 if (STRINGP (obj)
28398 && charpos >= 0
28399 && charpos < SCHARS (obj))
28400 {
28401 help = Fget_text_property (make_number (charpos),
28402 Qhelp_echo, obj);
28403 if (NILP (help))
28404 {
28405 /* If the string itself doesn't specify a help-echo,
28406 see if the buffer text ``under'' it does. */
28407 struct glyph_row *r
28408 = MATRIX_ROW (w->current_matrix, vpos);
28409 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28410 ptrdiff_t p = string_buffer_position (obj, start);
28411 if (p > 0)
28412 {
28413 help = Fget_char_property (make_number (p),
28414 Qhelp_echo, w->contents);
28415 if (!NILP (help))
28416 {
28417 charpos = p;
28418 obj = w->contents;
28419 }
28420 }
28421 }
28422 }
28423 else if (BUFFERP (obj)
28424 && charpos >= BEGV
28425 && charpos < ZV)
28426 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28427 obj);
28428
28429 if (!NILP (help))
28430 {
28431 help_echo_string = help;
28432 help_echo_window = window;
28433 help_echo_object = obj;
28434 help_echo_pos = charpos;
28435 }
28436 }
28437 }
28438
28439 #ifdef HAVE_WINDOW_SYSTEM
28440 /* Look for a `pointer' property. */
28441 if (FRAME_WINDOW_P (f) && NILP (pointer))
28442 {
28443 /* Check overlays first. */
28444 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28445 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28446
28447 if (NILP (pointer))
28448 {
28449 Lisp_Object obj = glyph->object;
28450 ptrdiff_t charpos = glyph->charpos;
28451
28452 /* Try text properties. */
28453 if (STRINGP (obj)
28454 && charpos >= 0
28455 && charpos < SCHARS (obj))
28456 {
28457 pointer = Fget_text_property (make_number (charpos),
28458 Qpointer, obj);
28459 if (NILP (pointer))
28460 {
28461 /* If the string itself doesn't specify a pointer,
28462 see if the buffer text ``under'' it does. */
28463 struct glyph_row *r
28464 = MATRIX_ROW (w->current_matrix, vpos);
28465 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28466 ptrdiff_t p = string_buffer_position (obj, start);
28467 if (p > 0)
28468 pointer = Fget_char_property (make_number (p),
28469 Qpointer, w->contents);
28470 }
28471 }
28472 else if (BUFFERP (obj)
28473 && charpos >= BEGV
28474 && charpos < ZV)
28475 pointer = Fget_text_property (make_number (charpos),
28476 Qpointer, obj);
28477 }
28478 }
28479 #endif /* HAVE_WINDOW_SYSTEM */
28480
28481 BEGV = obegv;
28482 ZV = ozv;
28483 current_buffer = obuf;
28484 }
28485
28486 set_cursor:
28487
28488 #ifdef HAVE_WINDOW_SYSTEM
28489 if (FRAME_WINDOW_P (f))
28490 define_frame_cursor1 (f, cursor, pointer);
28491 #else
28492 /* This is here to prevent a compiler error, about "label at end of
28493 compound statement". */
28494 return;
28495 #endif
28496 }
28497
28498
28499 /* EXPORT for RIF:
28500 Clear any mouse-face on window W. This function is part of the
28501 redisplay interface, and is called from try_window_id and similar
28502 functions to ensure the mouse-highlight is off. */
28503
28504 void
28505 x_clear_window_mouse_face (struct window *w)
28506 {
28507 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28508 Lisp_Object window;
28509
28510 block_input ();
28511 XSETWINDOW (window, w);
28512 if (EQ (window, hlinfo->mouse_face_window))
28513 clear_mouse_face (hlinfo);
28514 unblock_input ();
28515 }
28516
28517
28518 /* EXPORT:
28519 Just discard the mouse face information for frame F, if any.
28520 This is used when the size of F is changed. */
28521
28522 void
28523 cancel_mouse_face (struct frame *f)
28524 {
28525 Lisp_Object window;
28526 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28527
28528 window = hlinfo->mouse_face_window;
28529 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28530 reset_mouse_highlight (hlinfo);
28531 }
28532
28533
28534 \f
28535 /***********************************************************************
28536 Exposure Events
28537 ***********************************************************************/
28538
28539 #ifdef HAVE_WINDOW_SYSTEM
28540
28541 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28542 which intersects rectangle R. R is in window-relative coordinates. */
28543
28544 static void
28545 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28546 enum glyph_row_area area)
28547 {
28548 struct glyph *first = row->glyphs[area];
28549 struct glyph *end = row->glyphs[area] + row->used[area];
28550 struct glyph *last;
28551 int first_x, start_x, x;
28552
28553 if (area == TEXT_AREA && row->fill_line_p)
28554 /* If row extends face to end of line write the whole line. */
28555 draw_glyphs (w, 0, row, area,
28556 0, row->used[area],
28557 DRAW_NORMAL_TEXT, 0);
28558 else
28559 {
28560 /* Set START_X to the window-relative start position for drawing glyphs of
28561 AREA. The first glyph of the text area can be partially visible.
28562 The first glyphs of other areas cannot. */
28563 start_x = window_box_left_offset (w, area);
28564 x = start_x;
28565 if (area == TEXT_AREA)
28566 x += row->x;
28567
28568 /* Find the first glyph that must be redrawn. */
28569 while (first < end
28570 && x + first->pixel_width < r->x)
28571 {
28572 x += first->pixel_width;
28573 ++first;
28574 }
28575
28576 /* Find the last one. */
28577 last = first;
28578 first_x = x;
28579 while (last < end
28580 && x < r->x + r->width)
28581 {
28582 x += last->pixel_width;
28583 ++last;
28584 }
28585
28586 /* Repaint. */
28587 if (last > first)
28588 draw_glyphs (w, first_x - start_x, row, area,
28589 first - row->glyphs[area], last - row->glyphs[area],
28590 DRAW_NORMAL_TEXT, 0);
28591 }
28592 }
28593
28594
28595 /* Redraw the parts of the glyph row ROW on window W intersecting
28596 rectangle R. R is in window-relative coordinates. Value is
28597 non-zero if mouse-face was overwritten. */
28598
28599 static int
28600 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28601 {
28602 eassert (row->enabled_p);
28603
28604 if (row->mode_line_p || w->pseudo_window_p)
28605 draw_glyphs (w, 0, row, TEXT_AREA,
28606 0, row->used[TEXT_AREA],
28607 DRAW_NORMAL_TEXT, 0);
28608 else
28609 {
28610 if (row->used[LEFT_MARGIN_AREA])
28611 expose_area (w, row, r, LEFT_MARGIN_AREA);
28612 if (row->used[TEXT_AREA])
28613 expose_area (w, row, r, TEXT_AREA);
28614 if (row->used[RIGHT_MARGIN_AREA])
28615 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28616 draw_row_fringe_bitmaps (w, row);
28617 }
28618
28619 return row->mouse_face_p;
28620 }
28621
28622
28623 /* Redraw those parts of glyphs rows during expose event handling that
28624 overlap other rows. Redrawing of an exposed line writes over parts
28625 of lines overlapping that exposed line; this function fixes that.
28626
28627 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28628 row in W's current matrix that is exposed and overlaps other rows.
28629 LAST_OVERLAPPING_ROW is the last such row. */
28630
28631 static void
28632 expose_overlaps (struct window *w,
28633 struct glyph_row *first_overlapping_row,
28634 struct glyph_row *last_overlapping_row,
28635 XRectangle *r)
28636 {
28637 struct glyph_row *row;
28638
28639 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28640 if (row->overlapping_p)
28641 {
28642 eassert (row->enabled_p && !row->mode_line_p);
28643
28644 row->clip = r;
28645 if (row->used[LEFT_MARGIN_AREA])
28646 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28647
28648 if (row->used[TEXT_AREA])
28649 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28650
28651 if (row->used[RIGHT_MARGIN_AREA])
28652 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28653 row->clip = NULL;
28654 }
28655 }
28656
28657
28658 /* Return non-zero if W's cursor intersects rectangle R. */
28659
28660 static int
28661 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28662 {
28663 XRectangle cr, result;
28664 struct glyph *cursor_glyph;
28665 struct glyph_row *row;
28666
28667 if (w->phys_cursor.vpos >= 0
28668 && w->phys_cursor.vpos < w->current_matrix->nrows
28669 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28670 row->enabled_p)
28671 && row->cursor_in_fringe_p)
28672 {
28673 /* Cursor is in the fringe. */
28674 cr.x = window_box_right_offset (w,
28675 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28676 ? RIGHT_MARGIN_AREA
28677 : TEXT_AREA));
28678 cr.y = row->y;
28679 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28680 cr.height = row->height;
28681 return x_intersect_rectangles (&cr, r, &result);
28682 }
28683
28684 cursor_glyph = get_phys_cursor_glyph (w);
28685 if (cursor_glyph)
28686 {
28687 /* r is relative to W's box, but w->phys_cursor.x is relative
28688 to left edge of W's TEXT area. Adjust it. */
28689 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28690 cr.y = w->phys_cursor.y;
28691 cr.width = cursor_glyph->pixel_width;
28692 cr.height = w->phys_cursor_height;
28693 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28694 I assume the effect is the same -- and this is portable. */
28695 return x_intersect_rectangles (&cr, r, &result);
28696 }
28697 /* If we don't understand the format, pretend we're not in the hot-spot. */
28698 return 0;
28699 }
28700
28701
28702 /* EXPORT:
28703 Draw a vertical window border to the right of window W if W doesn't
28704 have vertical scroll bars. */
28705
28706 void
28707 x_draw_vertical_border (struct window *w)
28708 {
28709 struct frame *f = XFRAME (WINDOW_FRAME (w));
28710
28711 /* We could do better, if we knew what type of scroll-bar the adjacent
28712 windows (on either side) have... But we don't :-(
28713 However, I think this works ok. ++KFS 2003-04-25 */
28714
28715 /* Redraw borders between horizontally adjacent windows. Don't
28716 do it for frames with vertical scroll bars because either the
28717 right scroll bar of a window, or the left scroll bar of its
28718 neighbor will suffice as a border. */
28719 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28720 return;
28721
28722 /* Note: It is necessary to redraw both the left and the right
28723 borders, for when only this single window W is being
28724 redisplayed. */
28725 if (!WINDOW_RIGHTMOST_P (w)
28726 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28727 {
28728 int x0, x1, y0, y1;
28729
28730 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28731 y1 -= 1;
28732
28733 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28734 x1 -= 1;
28735
28736 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28737 }
28738 if (!WINDOW_LEFTMOST_P (w)
28739 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28740 {
28741 int x0, x1, y0, y1;
28742
28743 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28744 y1 -= 1;
28745
28746 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28747 x0 -= 1;
28748
28749 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28750 }
28751 }
28752
28753
28754 /* Redraw the part of window W intersection rectangle FR. Pixel
28755 coordinates in FR are frame-relative. Call this function with
28756 input blocked. Value is non-zero if the exposure overwrites
28757 mouse-face. */
28758
28759 static int
28760 expose_window (struct window *w, XRectangle *fr)
28761 {
28762 struct frame *f = XFRAME (w->frame);
28763 XRectangle wr, r;
28764 int mouse_face_overwritten_p = 0;
28765
28766 /* If window is not yet fully initialized, do nothing. This can
28767 happen when toolkit scroll bars are used and a window is split.
28768 Reconfiguring the scroll bar will generate an expose for a newly
28769 created window. */
28770 if (w->current_matrix == NULL)
28771 return 0;
28772
28773 /* When we're currently updating the window, display and current
28774 matrix usually don't agree. Arrange for a thorough display
28775 later. */
28776 if (w->must_be_updated_p)
28777 {
28778 SET_FRAME_GARBAGED (f);
28779 return 0;
28780 }
28781
28782 /* Frame-relative pixel rectangle of W. */
28783 wr.x = WINDOW_LEFT_EDGE_X (w);
28784 wr.y = WINDOW_TOP_EDGE_Y (w);
28785 wr.width = WINDOW_TOTAL_WIDTH (w);
28786 wr.height = WINDOW_TOTAL_HEIGHT (w);
28787
28788 if (x_intersect_rectangles (fr, &wr, &r))
28789 {
28790 int yb = window_text_bottom_y (w);
28791 struct glyph_row *row;
28792 int cursor_cleared_p, phys_cursor_on_p;
28793 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28794
28795 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28796 r.x, r.y, r.width, r.height));
28797
28798 /* Convert to window coordinates. */
28799 r.x -= WINDOW_LEFT_EDGE_X (w);
28800 r.y -= WINDOW_TOP_EDGE_Y (w);
28801
28802 /* Turn off the cursor. */
28803 if (!w->pseudo_window_p
28804 && phys_cursor_in_rect_p (w, &r))
28805 {
28806 x_clear_cursor (w);
28807 cursor_cleared_p = 1;
28808 }
28809 else
28810 cursor_cleared_p = 0;
28811
28812 /* If the row containing the cursor extends face to end of line,
28813 then expose_area might overwrite the cursor outside the
28814 rectangle and thus notice_overwritten_cursor might clear
28815 w->phys_cursor_on_p. We remember the original value and
28816 check later if it is changed. */
28817 phys_cursor_on_p = w->phys_cursor_on_p;
28818
28819 /* Update lines intersecting rectangle R. */
28820 first_overlapping_row = last_overlapping_row = NULL;
28821 for (row = w->current_matrix->rows;
28822 row->enabled_p;
28823 ++row)
28824 {
28825 int y0 = row->y;
28826 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28827
28828 if ((y0 >= r.y && y0 < r.y + r.height)
28829 || (y1 > r.y && y1 < r.y + r.height)
28830 || (r.y >= y0 && r.y < y1)
28831 || (r.y + r.height > y0 && r.y + r.height < y1))
28832 {
28833 /* A header line may be overlapping, but there is no need
28834 to fix overlapping areas for them. KFS 2005-02-12 */
28835 if (row->overlapping_p && !row->mode_line_p)
28836 {
28837 if (first_overlapping_row == NULL)
28838 first_overlapping_row = row;
28839 last_overlapping_row = row;
28840 }
28841
28842 row->clip = fr;
28843 if (expose_line (w, row, &r))
28844 mouse_face_overwritten_p = 1;
28845 row->clip = NULL;
28846 }
28847 else if (row->overlapping_p)
28848 {
28849 /* We must redraw a row overlapping the exposed area. */
28850 if (y0 < r.y
28851 ? y0 + row->phys_height > r.y
28852 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28853 {
28854 if (first_overlapping_row == NULL)
28855 first_overlapping_row = row;
28856 last_overlapping_row = row;
28857 }
28858 }
28859
28860 if (y1 >= yb)
28861 break;
28862 }
28863
28864 /* Display the mode line if there is one. */
28865 if (WINDOW_WANTS_MODELINE_P (w)
28866 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28867 row->enabled_p)
28868 && row->y < r.y + r.height)
28869 {
28870 if (expose_line (w, row, &r))
28871 mouse_face_overwritten_p = 1;
28872 }
28873
28874 if (!w->pseudo_window_p)
28875 {
28876 /* Fix the display of overlapping rows. */
28877 if (first_overlapping_row)
28878 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28879 fr);
28880
28881 /* Draw border between windows. */
28882 x_draw_vertical_border (w);
28883
28884 /* Turn the cursor on again. */
28885 if (cursor_cleared_p
28886 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28887 update_window_cursor (w, 1);
28888 }
28889 }
28890
28891 return mouse_face_overwritten_p;
28892 }
28893
28894
28895
28896 /* Redraw (parts) of all windows in the window tree rooted at W that
28897 intersect R. R contains frame pixel coordinates. Value is
28898 non-zero if the exposure overwrites mouse-face. */
28899
28900 static int
28901 expose_window_tree (struct window *w, XRectangle *r)
28902 {
28903 struct frame *f = XFRAME (w->frame);
28904 int mouse_face_overwritten_p = 0;
28905
28906 while (w && !FRAME_GARBAGED_P (f))
28907 {
28908 if (WINDOWP (w->contents))
28909 mouse_face_overwritten_p
28910 |= expose_window_tree (XWINDOW (w->contents), r);
28911 else
28912 mouse_face_overwritten_p |= expose_window (w, r);
28913
28914 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28915 }
28916
28917 return mouse_face_overwritten_p;
28918 }
28919
28920
28921 /* EXPORT:
28922 Redisplay an exposed area of frame F. X and Y are the upper-left
28923 corner of the exposed rectangle. W and H are width and height of
28924 the exposed area. All are pixel values. W or H zero means redraw
28925 the entire frame. */
28926
28927 void
28928 expose_frame (struct frame *f, int x, int y, int w, int h)
28929 {
28930 XRectangle r;
28931 int mouse_face_overwritten_p = 0;
28932
28933 TRACE ((stderr, "expose_frame "));
28934
28935 /* No need to redraw if frame will be redrawn soon. */
28936 if (FRAME_GARBAGED_P (f))
28937 {
28938 TRACE ((stderr, " garbaged\n"));
28939 return;
28940 }
28941
28942 /* If basic faces haven't been realized yet, there is no point in
28943 trying to redraw anything. This can happen when we get an expose
28944 event while Emacs is starting, e.g. by moving another window. */
28945 if (FRAME_FACE_CACHE (f) == NULL
28946 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28947 {
28948 TRACE ((stderr, " no faces\n"));
28949 return;
28950 }
28951
28952 if (w == 0 || h == 0)
28953 {
28954 r.x = r.y = 0;
28955 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
28956 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
28957 }
28958 else
28959 {
28960 r.x = x;
28961 r.y = y;
28962 r.width = w;
28963 r.height = h;
28964 }
28965
28966 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
28967 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
28968
28969 if (WINDOWP (f->tool_bar_window))
28970 mouse_face_overwritten_p
28971 |= expose_window (XWINDOW (f->tool_bar_window), &r);
28972
28973 #ifdef HAVE_X_WINDOWS
28974 #ifndef MSDOS
28975 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
28976 if (WINDOWP (f->menu_bar_window))
28977 mouse_face_overwritten_p
28978 |= expose_window (XWINDOW (f->menu_bar_window), &r);
28979 #endif /* not USE_X_TOOLKIT and not USE_GTK */
28980 #endif
28981 #endif
28982
28983 /* Some window managers support a focus-follows-mouse style with
28984 delayed raising of frames. Imagine a partially obscured frame,
28985 and moving the mouse into partially obscured mouse-face on that
28986 frame. The visible part of the mouse-face will be highlighted,
28987 then the WM raises the obscured frame. With at least one WM, KDE
28988 2.1, Emacs is not getting any event for the raising of the frame
28989 (even tried with SubstructureRedirectMask), only Expose events.
28990 These expose events will draw text normally, i.e. not
28991 highlighted. Which means we must redo the highlight here.
28992 Subsume it under ``we love X''. --gerd 2001-08-15 */
28993 /* Included in Windows version because Windows most likely does not
28994 do the right thing if any third party tool offers
28995 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
28996 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
28997 {
28998 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28999 if (f == hlinfo->mouse_face_mouse_frame)
29000 {
29001 int mouse_x = hlinfo->mouse_face_mouse_x;
29002 int mouse_y = hlinfo->mouse_face_mouse_y;
29003 clear_mouse_face (hlinfo);
29004 note_mouse_highlight (f, mouse_x, mouse_y);
29005 }
29006 }
29007 }
29008
29009
29010 /* EXPORT:
29011 Determine the intersection of two rectangles R1 and R2. Return
29012 the intersection in *RESULT. Value is non-zero if RESULT is not
29013 empty. */
29014
29015 int
29016 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
29017 {
29018 XRectangle *left, *right;
29019 XRectangle *upper, *lower;
29020 int intersection_p = 0;
29021
29022 /* Rearrange so that R1 is the left-most rectangle. */
29023 if (r1->x < r2->x)
29024 left = r1, right = r2;
29025 else
29026 left = r2, right = r1;
29027
29028 /* X0 of the intersection is right.x0, if this is inside R1,
29029 otherwise there is no intersection. */
29030 if (right->x <= left->x + left->width)
29031 {
29032 result->x = right->x;
29033
29034 /* The right end of the intersection is the minimum of
29035 the right ends of left and right. */
29036 result->width = (min (left->x + left->width, right->x + right->width)
29037 - result->x);
29038
29039 /* Same game for Y. */
29040 if (r1->y < r2->y)
29041 upper = r1, lower = r2;
29042 else
29043 upper = r2, lower = r1;
29044
29045 /* The upper end of the intersection is lower.y0, if this is inside
29046 of upper. Otherwise, there is no intersection. */
29047 if (lower->y <= upper->y + upper->height)
29048 {
29049 result->y = lower->y;
29050
29051 /* The lower end of the intersection is the minimum of the lower
29052 ends of upper and lower. */
29053 result->height = (min (lower->y + lower->height,
29054 upper->y + upper->height)
29055 - result->y);
29056 intersection_p = 1;
29057 }
29058 }
29059
29060 return intersection_p;
29061 }
29062
29063 #endif /* HAVE_WINDOW_SYSTEM */
29064
29065 \f
29066 /***********************************************************************
29067 Initialization
29068 ***********************************************************************/
29069
29070 void
29071 syms_of_xdisp (void)
29072 {
29073 Vwith_echo_area_save_vector = Qnil;
29074 staticpro (&Vwith_echo_area_save_vector);
29075
29076 Vmessage_stack = Qnil;
29077 staticpro (&Vmessage_stack);
29078
29079 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
29080 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
29081
29082 message_dolog_marker1 = Fmake_marker ();
29083 staticpro (&message_dolog_marker1);
29084 message_dolog_marker2 = Fmake_marker ();
29085 staticpro (&message_dolog_marker2);
29086 message_dolog_marker3 = Fmake_marker ();
29087 staticpro (&message_dolog_marker3);
29088
29089 #ifdef GLYPH_DEBUG
29090 defsubr (&Sdump_frame_glyph_matrix);
29091 defsubr (&Sdump_glyph_matrix);
29092 defsubr (&Sdump_glyph_row);
29093 defsubr (&Sdump_tool_bar_row);
29094 defsubr (&Strace_redisplay);
29095 defsubr (&Strace_to_stderr);
29096 #endif
29097 #ifdef HAVE_WINDOW_SYSTEM
29098 defsubr (&Stool_bar_lines_needed);
29099 defsubr (&Slookup_image_map);
29100 #endif
29101 defsubr (&Sline_pixel_height);
29102 defsubr (&Sformat_mode_line);
29103 defsubr (&Sinvisible_p);
29104 defsubr (&Scurrent_bidi_paragraph_direction);
29105 defsubr (&Smove_point_visually);
29106
29107 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
29108 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
29109 DEFSYM (Qoverriding_local_map, "overriding-local-map");
29110 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
29111 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
29112 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
29113 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
29114 DEFSYM (Qeval, "eval");
29115 DEFSYM (QCdata, ":data");
29116 DEFSYM (Qdisplay, "display");
29117 DEFSYM (Qspace_width, "space-width");
29118 DEFSYM (Qraise, "raise");
29119 DEFSYM (Qslice, "slice");
29120 DEFSYM (Qspace, "space");
29121 DEFSYM (Qmargin, "margin");
29122 DEFSYM (Qpointer, "pointer");
29123 DEFSYM (Qleft_margin, "left-margin");
29124 DEFSYM (Qright_margin, "right-margin");
29125 DEFSYM (Qcenter, "center");
29126 DEFSYM (Qline_height, "line-height");
29127 DEFSYM (QCalign_to, ":align-to");
29128 DEFSYM (QCrelative_width, ":relative-width");
29129 DEFSYM (QCrelative_height, ":relative-height");
29130 DEFSYM (QCeval, ":eval");
29131 DEFSYM (QCpropertize, ":propertize");
29132 DEFSYM (QCfile, ":file");
29133 DEFSYM (Qfontified, "fontified");
29134 DEFSYM (Qfontification_functions, "fontification-functions");
29135 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
29136 DEFSYM (Qescape_glyph, "escape-glyph");
29137 DEFSYM (Qnobreak_space, "nobreak-space");
29138 DEFSYM (Qimage, "image");
29139 DEFSYM (Qtext, "text");
29140 DEFSYM (Qboth, "both");
29141 DEFSYM (Qboth_horiz, "both-horiz");
29142 DEFSYM (Qtext_image_horiz, "text-image-horiz");
29143 DEFSYM (QCmap, ":map");
29144 DEFSYM (QCpointer, ":pointer");
29145 DEFSYM (Qrect, "rect");
29146 DEFSYM (Qcircle, "circle");
29147 DEFSYM (Qpoly, "poly");
29148 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
29149 DEFSYM (Qgrow_only, "grow-only");
29150 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
29151 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
29152 DEFSYM (Qposition, "position");
29153 DEFSYM (Qbuffer_position, "buffer-position");
29154 DEFSYM (Qobject, "object");
29155 DEFSYM (Qbar, "bar");
29156 DEFSYM (Qhbar, "hbar");
29157 DEFSYM (Qbox, "box");
29158 DEFSYM (Qhollow, "hollow");
29159 DEFSYM (Qhand, "hand");
29160 DEFSYM (Qarrow, "arrow");
29161 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
29162
29163 list_of_error = list1 (list2 (intern_c_string ("error"),
29164 intern_c_string ("void-variable")));
29165 staticpro (&list_of_error);
29166
29167 DEFSYM (Qlast_arrow_position, "last-arrow-position");
29168 DEFSYM (Qlast_arrow_string, "last-arrow-string");
29169 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
29170 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
29171
29172 echo_buffer[0] = echo_buffer[1] = Qnil;
29173 staticpro (&echo_buffer[0]);
29174 staticpro (&echo_buffer[1]);
29175
29176 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
29177 staticpro (&echo_area_buffer[0]);
29178 staticpro (&echo_area_buffer[1]);
29179
29180 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
29181 staticpro (&Vmessages_buffer_name);
29182
29183 mode_line_proptrans_alist = Qnil;
29184 staticpro (&mode_line_proptrans_alist);
29185 mode_line_string_list = Qnil;
29186 staticpro (&mode_line_string_list);
29187 mode_line_string_face = Qnil;
29188 staticpro (&mode_line_string_face);
29189 mode_line_string_face_prop = Qnil;
29190 staticpro (&mode_line_string_face_prop);
29191 Vmode_line_unwind_vector = Qnil;
29192 staticpro (&Vmode_line_unwind_vector);
29193
29194 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
29195
29196 help_echo_string = Qnil;
29197 staticpro (&help_echo_string);
29198 help_echo_object = Qnil;
29199 staticpro (&help_echo_object);
29200 help_echo_window = Qnil;
29201 staticpro (&help_echo_window);
29202 previous_help_echo_string = Qnil;
29203 staticpro (&previous_help_echo_string);
29204 help_echo_pos = -1;
29205
29206 DEFSYM (Qright_to_left, "right-to-left");
29207 DEFSYM (Qleft_to_right, "left-to-right");
29208
29209 #ifdef HAVE_WINDOW_SYSTEM
29210 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
29211 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
29212 For example, if a block cursor is over a tab, it will be drawn as
29213 wide as that tab on the display. */);
29214 x_stretch_cursor_p = 0;
29215 #endif
29216
29217 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
29218 doc: /* Non-nil means highlight trailing whitespace.
29219 The face used for trailing whitespace is `trailing-whitespace'. */);
29220 Vshow_trailing_whitespace = Qnil;
29221
29222 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
29223 doc: /* Control highlighting of non-ASCII space and hyphen chars.
29224 If the value is t, Emacs highlights non-ASCII chars which have the
29225 same appearance as an ASCII space or hyphen, using the `nobreak-space'
29226 or `escape-glyph' face respectively.
29227
29228 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
29229 U+2011 (non-breaking hyphen) are affected.
29230
29231 Any other non-nil value means to display these characters as a escape
29232 glyph followed by an ordinary space or hyphen.
29233
29234 A value of nil means no special handling of these characters. */);
29235 Vnobreak_char_display = Qt;
29236
29237 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
29238 doc: /* The pointer shape to show in void text areas.
29239 A value of nil means to show the text pointer. Other options are `arrow',
29240 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
29241 Vvoid_text_area_pointer = Qarrow;
29242
29243 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
29244 doc: /* Non-nil means don't actually do any redisplay.
29245 This is used for internal purposes. */);
29246 Vinhibit_redisplay = Qnil;
29247
29248 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
29249 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
29250 Vglobal_mode_string = Qnil;
29251
29252 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
29253 doc: /* Marker for where to display an arrow on top of the buffer text.
29254 This must be the beginning of a line in order to work.
29255 See also `overlay-arrow-string'. */);
29256 Voverlay_arrow_position = Qnil;
29257
29258 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
29259 doc: /* String to display as an arrow in non-window frames.
29260 See also `overlay-arrow-position'. */);
29261 Voverlay_arrow_string = build_pure_c_string ("=>");
29262
29263 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
29264 doc: /* List of variables (symbols) which hold markers for overlay arrows.
29265 The symbols on this list are examined during redisplay to determine
29266 where to display overlay arrows. */);
29267 Voverlay_arrow_variable_list
29268 = list1 (intern_c_string ("overlay-arrow-position"));
29269
29270 DEFVAR_INT ("scroll-step", emacs_scroll_step,
29271 doc: /* The number of lines to try scrolling a window by when point moves out.
29272 If that fails to bring point back on frame, point is centered instead.
29273 If this is zero, point is always centered after it moves off frame.
29274 If you want scrolling to always be a line at a time, you should set
29275 `scroll-conservatively' to a large value rather than set this to 1. */);
29276
29277 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
29278 doc: /* Scroll up to this many lines, to bring point back on screen.
29279 If point moves off-screen, redisplay will scroll by up to
29280 `scroll-conservatively' lines in order to bring point just barely
29281 onto the screen again. If that cannot be done, then redisplay
29282 recenters point as usual.
29283
29284 If the value is greater than 100, redisplay will never recenter point,
29285 but will always scroll just enough text to bring point into view, even
29286 if you move far away.
29287
29288 A value of zero means always recenter point if it moves off screen. */);
29289 scroll_conservatively = 0;
29290
29291 DEFVAR_INT ("scroll-margin", scroll_margin,
29292 doc: /* Number of lines of margin at the top and bottom of a window.
29293 Recenter the window whenever point gets within this many lines
29294 of the top or bottom of the window. */);
29295 scroll_margin = 0;
29296
29297 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
29298 doc: /* Pixels per inch value for non-window system displays.
29299 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
29300 Vdisplay_pixels_per_inch = make_float (72.0);
29301
29302 #ifdef GLYPH_DEBUG
29303 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
29304 #endif
29305
29306 DEFVAR_LISP ("truncate-partial-width-windows",
29307 Vtruncate_partial_width_windows,
29308 doc: /* Non-nil means truncate lines in windows narrower than the frame.
29309 For an integer value, truncate lines in each window narrower than the
29310 full frame width, provided the window width is less than that integer;
29311 otherwise, respect the value of `truncate-lines'.
29312
29313 For any other non-nil value, truncate lines in all windows that do
29314 not span the full frame width.
29315
29316 A value of nil means to respect the value of `truncate-lines'.
29317
29318 If `word-wrap' is enabled, you might want to reduce this. */);
29319 Vtruncate_partial_width_windows = make_number (50);
29320
29321 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
29322 doc: /* Maximum buffer size for which line number should be displayed.
29323 If the buffer is bigger than this, the line number does not appear
29324 in the mode line. A value of nil means no limit. */);
29325 Vline_number_display_limit = Qnil;
29326
29327 DEFVAR_INT ("line-number-display-limit-width",
29328 line_number_display_limit_width,
29329 doc: /* Maximum line width (in characters) for line number display.
29330 If the average length of the lines near point is bigger than this, then the
29331 line number may be omitted from the mode line. */);
29332 line_number_display_limit_width = 200;
29333
29334 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
29335 doc: /* Non-nil means highlight region even in nonselected windows. */);
29336 highlight_nonselected_windows = 0;
29337
29338 DEFVAR_BOOL ("multiple-frames", multiple_frames,
29339 doc: /* Non-nil if more than one frame is visible on this display.
29340 Minibuffer-only frames don't count, but iconified frames do.
29341 This variable is not guaranteed to be accurate except while processing
29342 `frame-title-format' and `icon-title-format'. */);
29343
29344 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
29345 doc: /* Template for displaying the title bar of visible frames.
29346 \(Assuming the window manager supports this feature.)
29347
29348 This variable has the same structure as `mode-line-format', except that
29349 the %c and %l constructs are ignored. It is used only on frames for
29350 which no explicit name has been set \(see `modify-frame-parameters'). */);
29351
29352 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
29353 doc: /* Template for displaying the title bar of an iconified frame.
29354 \(Assuming the window manager supports this feature.)
29355 This variable has the same structure as `mode-line-format' (which see),
29356 and is used only on frames for which no explicit name has been set
29357 \(see `modify-frame-parameters'). */);
29358 Vicon_title_format
29359 = Vframe_title_format
29360 = listn (CONSTYPE_PURE, 3,
29361 intern_c_string ("multiple-frames"),
29362 build_pure_c_string ("%b"),
29363 listn (CONSTYPE_PURE, 4,
29364 empty_unibyte_string,
29365 intern_c_string ("invocation-name"),
29366 build_pure_c_string ("@"),
29367 intern_c_string ("system-name")));
29368
29369 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
29370 doc: /* Maximum number of lines to keep in the message log buffer.
29371 If nil, disable message logging. If t, log messages but don't truncate
29372 the buffer when it becomes large. */);
29373 Vmessage_log_max = make_number (1000);
29374
29375 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
29376 doc: /* Functions called before redisplay, if window sizes have changed.
29377 The value should be a list of functions that take one argument.
29378 Just before redisplay, for each frame, if any of its windows have changed
29379 size since the last redisplay, or have been split or deleted,
29380 all the functions in the list are called, with the frame as argument. */);
29381 Vwindow_size_change_functions = Qnil;
29382
29383 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
29384 doc: /* List of functions to call before redisplaying a window with scrolling.
29385 Each function is called with two arguments, the window and its new
29386 display-start position. Note that these functions are also called by
29387 `set-window-buffer'. Also note that the value of `window-end' is not
29388 valid when these functions are called.
29389
29390 Warning: Do not use this feature to alter the way the window
29391 is scrolled. It is not designed for that, and such use probably won't
29392 work. */);
29393 Vwindow_scroll_functions = Qnil;
29394
29395 DEFVAR_LISP ("window-text-change-functions",
29396 Vwindow_text_change_functions,
29397 doc: /* Functions to call in redisplay when text in the window might change. */);
29398 Vwindow_text_change_functions = Qnil;
29399
29400 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
29401 doc: /* Functions called when redisplay of a window reaches the end trigger.
29402 Each function is called with two arguments, the window and the end trigger value.
29403 See `set-window-redisplay-end-trigger'. */);
29404 Vredisplay_end_trigger_functions = Qnil;
29405
29406 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
29407 doc: /* Non-nil means autoselect window with mouse pointer.
29408 If nil, do not autoselect windows.
29409 A positive number means delay autoselection by that many seconds: a
29410 window is autoselected only after the mouse has remained in that
29411 window for the duration of the delay.
29412 A negative number has a similar effect, but causes windows to be
29413 autoselected only after the mouse has stopped moving. \(Because of
29414 the way Emacs compares mouse events, you will occasionally wait twice
29415 that time before the window gets selected.\)
29416 Any other value means to autoselect window instantaneously when the
29417 mouse pointer enters it.
29418
29419 Autoselection selects the minibuffer only if it is active, and never
29420 unselects the minibuffer if it is active.
29421
29422 When customizing this variable make sure that the actual value of
29423 `focus-follows-mouse' matches the behavior of your window manager. */);
29424 Vmouse_autoselect_window = Qnil;
29425
29426 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29427 doc: /* Non-nil means automatically resize tool-bars.
29428 This dynamically changes the tool-bar's height to the minimum height
29429 that is needed to make all tool-bar items visible.
29430 If value is `grow-only', the tool-bar's height is only increased
29431 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29432 Vauto_resize_tool_bars = Qt;
29433
29434 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29435 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29436 auto_raise_tool_bar_buttons_p = 1;
29437
29438 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29439 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29440 make_cursor_line_fully_visible_p = 1;
29441
29442 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29443 doc: /* Border below tool-bar in pixels.
29444 If an integer, use it as the height of the border.
29445 If it is one of `internal-border-width' or `border-width', use the
29446 value of the corresponding frame parameter.
29447 Otherwise, no border is added below the tool-bar. */);
29448 Vtool_bar_border = Qinternal_border_width;
29449
29450 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29451 doc: /* Margin around tool-bar buttons in pixels.
29452 If an integer, use that for both horizontal and vertical margins.
29453 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29454 HORZ specifying the horizontal margin, and VERT specifying the
29455 vertical margin. */);
29456 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29457
29458 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29459 doc: /* Relief thickness of tool-bar buttons. */);
29460 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29461
29462 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29463 doc: /* Tool bar style to use.
29464 It can be one of
29465 image - show images only
29466 text - show text only
29467 both - show both, text below image
29468 both-horiz - show text to the right of the image
29469 text-image-horiz - show text to the left of the image
29470 any other - use system default or image if no system default.
29471
29472 This variable only affects the GTK+ toolkit version of Emacs. */);
29473 Vtool_bar_style = Qnil;
29474
29475 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29476 doc: /* Maximum number of characters a label can have to be shown.
29477 The tool bar style must also show labels for this to have any effect, see
29478 `tool-bar-style'. */);
29479 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29480
29481 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29482 doc: /* List of functions to call to fontify regions of text.
29483 Each function is called with one argument POS. Functions must
29484 fontify a region starting at POS in the current buffer, and give
29485 fontified regions the property `fontified'. */);
29486 Vfontification_functions = Qnil;
29487 Fmake_variable_buffer_local (Qfontification_functions);
29488
29489 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29490 unibyte_display_via_language_environment,
29491 doc: /* Non-nil means display unibyte text according to language environment.
29492 Specifically, this means that raw bytes in the range 160-255 decimal
29493 are displayed by converting them to the equivalent multibyte characters
29494 according to the current language environment. As a result, they are
29495 displayed according to the current fontset.
29496
29497 Note that this variable affects only how these bytes are displayed,
29498 but does not change the fact they are interpreted as raw bytes. */);
29499 unibyte_display_via_language_environment = 0;
29500
29501 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29502 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29503 If a float, it specifies a fraction of the mini-window frame's height.
29504 If an integer, it specifies a number of lines. */);
29505 Vmax_mini_window_height = make_float (0.25);
29506
29507 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29508 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29509 A value of nil means don't automatically resize mini-windows.
29510 A value of t means resize them to fit the text displayed in them.
29511 A value of `grow-only', the default, means let mini-windows grow only;
29512 they return to their normal size when the minibuffer is closed, or the
29513 echo area becomes empty. */);
29514 Vresize_mini_windows = Qgrow_only;
29515
29516 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29517 doc: /* Alist specifying how to blink the cursor off.
29518 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29519 `cursor-type' frame-parameter or variable equals ON-STATE,
29520 comparing using `equal', Emacs uses OFF-STATE to specify
29521 how to blink it off. ON-STATE and OFF-STATE are values for
29522 the `cursor-type' frame parameter.
29523
29524 If a frame's ON-STATE has no entry in this list,
29525 the frame's other specifications determine how to blink the cursor off. */);
29526 Vblink_cursor_alist = Qnil;
29527
29528 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29529 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29530 If non-nil, windows are automatically scrolled horizontally to make
29531 point visible. */);
29532 automatic_hscrolling_p = 1;
29533 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29534
29535 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29536 doc: /* How many columns away from the window edge point is allowed to get
29537 before automatic hscrolling will horizontally scroll the window. */);
29538 hscroll_margin = 5;
29539
29540 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29541 doc: /* How many columns to scroll the window when point gets too close to the edge.
29542 When point is less than `hscroll-margin' columns from the window
29543 edge, automatic hscrolling will scroll the window by the amount of columns
29544 determined by this variable. If its value is a positive integer, scroll that
29545 many columns. If it's a positive floating-point number, it specifies the
29546 fraction of the window's width to scroll. If it's nil or zero, point will be
29547 centered horizontally after the scroll. Any other value, including negative
29548 numbers, are treated as if the value were zero.
29549
29550 Automatic hscrolling always moves point outside the scroll margin, so if
29551 point was more than scroll step columns inside the margin, the window will
29552 scroll more than the value given by the scroll step.
29553
29554 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29555 and `scroll-right' overrides this variable's effect. */);
29556 Vhscroll_step = make_number (0);
29557
29558 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29559 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29560 Bind this around calls to `message' to let it take effect. */);
29561 message_truncate_lines = 0;
29562
29563 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29564 doc: /* Normal hook run to update the menu bar definitions.
29565 Redisplay runs this hook before it redisplays the menu bar.
29566 This is used to update submenus such as Buffers,
29567 whose contents depend on various data. */);
29568 Vmenu_bar_update_hook = Qnil;
29569
29570 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29571 doc: /* Frame for which we are updating a menu.
29572 The enable predicate for a menu binding should check this variable. */);
29573 Vmenu_updating_frame = Qnil;
29574
29575 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29576 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29577 inhibit_menubar_update = 0;
29578
29579 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29580 doc: /* Prefix prepended to all continuation lines at display time.
29581 The value may be a string, an image, or a stretch-glyph; it is
29582 interpreted in the same way as the value of a `display' text property.
29583
29584 This variable is overridden by any `wrap-prefix' text or overlay
29585 property.
29586
29587 To add a prefix to non-continuation lines, use `line-prefix'. */);
29588 Vwrap_prefix = Qnil;
29589 DEFSYM (Qwrap_prefix, "wrap-prefix");
29590 Fmake_variable_buffer_local (Qwrap_prefix);
29591
29592 DEFVAR_LISP ("line-prefix", Vline_prefix,
29593 doc: /* Prefix prepended to all non-continuation lines at display time.
29594 The value may be a string, an image, or a stretch-glyph; it is
29595 interpreted in the same way as the value of a `display' text property.
29596
29597 This variable is overridden by any `line-prefix' text or overlay
29598 property.
29599
29600 To add a prefix to continuation lines, use `wrap-prefix'. */);
29601 Vline_prefix = Qnil;
29602 DEFSYM (Qline_prefix, "line-prefix");
29603 Fmake_variable_buffer_local (Qline_prefix);
29604
29605 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29606 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29607 inhibit_eval_during_redisplay = 0;
29608
29609 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29610 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29611 inhibit_free_realized_faces = 0;
29612
29613 #ifdef GLYPH_DEBUG
29614 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29615 doc: /* Inhibit try_window_id display optimization. */);
29616 inhibit_try_window_id = 0;
29617
29618 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29619 doc: /* Inhibit try_window_reusing display optimization. */);
29620 inhibit_try_window_reusing = 0;
29621
29622 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29623 doc: /* Inhibit try_cursor_movement display optimization. */);
29624 inhibit_try_cursor_movement = 0;
29625 #endif /* GLYPH_DEBUG */
29626
29627 DEFVAR_INT ("overline-margin", overline_margin,
29628 doc: /* Space between overline and text, in pixels.
29629 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29630 margin to the character height. */);
29631 overline_margin = 2;
29632
29633 DEFVAR_INT ("underline-minimum-offset",
29634 underline_minimum_offset,
29635 doc: /* Minimum distance between baseline and underline.
29636 This can improve legibility of underlined text at small font sizes,
29637 particularly when using variable `x-use-underline-position-properties'
29638 with fonts that specify an UNDERLINE_POSITION relatively close to the
29639 baseline. The default value is 1. */);
29640 underline_minimum_offset = 1;
29641
29642 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29643 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29644 This feature only works when on a window system that can change
29645 cursor shapes. */);
29646 display_hourglass_p = 1;
29647
29648 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29649 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29650 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29651
29652 hourglass_atimer = NULL;
29653 hourglass_shown_p = 0;
29654
29655 DEFSYM (Qglyphless_char, "glyphless-char");
29656 DEFSYM (Qhex_code, "hex-code");
29657 DEFSYM (Qempty_box, "empty-box");
29658 DEFSYM (Qthin_space, "thin-space");
29659 DEFSYM (Qzero_width, "zero-width");
29660
29661 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29662 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29663
29664 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29665 doc: /* Char-table defining glyphless characters.
29666 Each element, if non-nil, should be one of the following:
29667 an ASCII acronym string: display this string in a box
29668 `hex-code': display the hexadecimal code of a character in a box
29669 `empty-box': display as an empty box
29670 `thin-space': display as 1-pixel width space
29671 `zero-width': don't display
29672 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29673 display method for graphical terminals and text terminals respectively.
29674 GRAPHICAL and TEXT should each have one of the values listed above.
29675
29676 The char-table has one extra slot to control the display of a character for
29677 which no font is found. This slot only takes effect on graphical terminals.
29678 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29679 `thin-space'. The default is `empty-box'. */);
29680 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29681 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29682 Qempty_box);
29683
29684 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
29685 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
29686 Vdebug_on_message = Qnil;
29687 }
29688
29689
29690 /* Initialize this module when Emacs starts. */
29691
29692 void
29693 init_xdisp (void)
29694 {
29695 current_header_line_height = current_mode_line_height = -1;
29696
29697 CHARPOS (this_line_start_pos) = 0;
29698
29699 if (!noninteractive)
29700 {
29701 struct window *m = XWINDOW (minibuf_window);
29702 Lisp_Object frame = m->frame;
29703 struct frame *f = XFRAME (frame);
29704 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29705 struct window *r = XWINDOW (root);
29706 int i;
29707
29708 echo_area_window = minibuf_window;
29709
29710 r->top_line = FRAME_TOP_MARGIN (f);
29711 r->total_lines = FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
29712 r->total_cols = FRAME_COLS (f);
29713
29714 m->top_line = FRAME_LINES (f) - 1;
29715 m->total_lines = 1;
29716 m->total_cols = FRAME_COLS (f);
29717
29718 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29719 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29720 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29721
29722 /* The default ellipsis glyphs `...'. */
29723 for (i = 0; i < 3; ++i)
29724 default_invis_vector[i] = make_number ('.');
29725 }
29726
29727 {
29728 /* Allocate the buffer for frame titles.
29729 Also used for `format-mode-line'. */
29730 int size = 100;
29731 mode_line_noprop_buf = xmalloc (size);
29732 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29733 mode_line_noprop_ptr = mode_line_noprop_buf;
29734 mode_line_target = MODE_LINE_DISPLAY;
29735 }
29736
29737 help_echo_showing_p = 0;
29738 }
29739
29740 /* Platform-independent portion of hourglass implementation. */
29741
29742 /* Cancel a currently active hourglass timer, and start a new one. */
29743 void
29744 start_hourglass (void)
29745 {
29746 #if defined (HAVE_WINDOW_SYSTEM)
29747 struct timespec delay;
29748
29749 cancel_hourglass ();
29750
29751 if (INTEGERP (Vhourglass_delay)
29752 && XINT (Vhourglass_delay) > 0)
29753 delay = make_timespec (min (XINT (Vhourglass_delay),
29754 TYPE_MAXIMUM (time_t)),
29755 0);
29756 else if (FLOATP (Vhourglass_delay)
29757 && XFLOAT_DATA (Vhourglass_delay) > 0)
29758 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
29759 else
29760 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
29761
29762 #ifdef HAVE_NTGUI
29763 {
29764 extern void w32_note_current_window (void);
29765 w32_note_current_window ();
29766 }
29767 #endif /* HAVE_NTGUI */
29768
29769 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29770 show_hourglass, NULL);
29771 #endif
29772 }
29773
29774
29775 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29776 shown. */
29777 void
29778 cancel_hourglass (void)
29779 {
29780 #if defined (HAVE_WINDOW_SYSTEM)
29781 if (hourglass_atimer)
29782 {
29783 cancel_atimer (hourglass_atimer);
29784 hourglass_atimer = NULL;
29785 }
29786
29787 if (hourglass_shown_p)
29788 hide_hourglass ();
29789 #endif
29790 }