* window.h (struct window): Convert window_end_pos and
[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 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 \f
2465 /***********************************************************************
2466 Lisp form evaluation
2467 ***********************************************************************/
2468
2469 /* Error handler for safe_eval and safe_call. */
2470
2471 static Lisp_Object
2472 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2473 {
2474 add_to_log ("Error during redisplay: %S signaled %S",
2475 Flist (nargs, args), arg);
2476 return Qnil;
2477 }
2478
2479 /* Call function FUNC with the rest of NARGS - 1 arguments
2480 following. Return the result, or nil if something went
2481 wrong. Prevent redisplay during the evaluation. */
2482
2483 Lisp_Object
2484 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2485 {
2486 Lisp_Object val;
2487
2488 if (inhibit_eval_during_redisplay)
2489 val = Qnil;
2490 else
2491 {
2492 va_list ap;
2493 ptrdiff_t i;
2494 ptrdiff_t count = SPECPDL_INDEX ();
2495 struct gcpro gcpro1;
2496 Lisp_Object *args = alloca (nargs * word_size);
2497
2498 args[0] = func;
2499 va_start (ap, func);
2500 for (i = 1; i < nargs; i++)
2501 args[i] = va_arg (ap, Lisp_Object);
2502 va_end (ap);
2503
2504 GCPRO1 (args[0]);
2505 gcpro1.nvars = nargs;
2506 specbind (Qinhibit_redisplay, Qt);
2507 /* Use Qt to ensure debugger does not run,
2508 so there is no possibility of wanting to redisplay. */
2509 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2510 safe_eval_handler);
2511 UNGCPRO;
2512 val = unbind_to (count, val);
2513 }
2514
2515 return val;
2516 }
2517
2518
2519 /* Call function FN with one argument ARG.
2520 Return the result, or nil if something went wrong. */
2521
2522 Lisp_Object
2523 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2524 {
2525 return safe_call (2, fn, arg);
2526 }
2527
2528 static Lisp_Object Qeval;
2529
2530 Lisp_Object
2531 safe_eval (Lisp_Object sexpr)
2532 {
2533 return safe_call1 (Qeval, sexpr);
2534 }
2535
2536 /* Call function FN with two arguments ARG1 and ARG2.
2537 Return the result, or nil if something went wrong. */
2538
2539 Lisp_Object
2540 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2541 {
2542 return safe_call (3, fn, arg1, arg2);
2543 }
2544
2545
2546 \f
2547 /***********************************************************************
2548 Debugging
2549 ***********************************************************************/
2550
2551 #if 0
2552
2553 /* Define CHECK_IT to perform sanity checks on iterators.
2554 This is for debugging. It is too slow to do unconditionally. */
2555
2556 static void
2557 check_it (struct it *it)
2558 {
2559 if (it->method == GET_FROM_STRING)
2560 {
2561 eassert (STRINGP (it->string));
2562 eassert (IT_STRING_CHARPOS (*it) >= 0);
2563 }
2564 else
2565 {
2566 eassert (IT_STRING_CHARPOS (*it) < 0);
2567 if (it->method == GET_FROM_BUFFER)
2568 {
2569 /* Check that character and byte positions agree. */
2570 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2571 }
2572 }
2573
2574 if (it->dpvec)
2575 eassert (it->current.dpvec_index >= 0);
2576 else
2577 eassert (it->current.dpvec_index < 0);
2578 }
2579
2580 #define CHECK_IT(IT) check_it ((IT))
2581
2582 #else /* not 0 */
2583
2584 #define CHECK_IT(IT) (void) 0
2585
2586 #endif /* not 0 */
2587
2588
2589 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2590
2591 /* Check that the window end of window W is what we expect it
2592 to be---the last row in the current matrix displaying text. */
2593
2594 static void
2595 check_window_end (struct window *w)
2596 {
2597 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2598 {
2599 struct glyph_row *row;
2600 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2601 !row->enabled_p
2602 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2603 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2604 }
2605 }
2606
2607 #define CHECK_WINDOW_END(W) check_window_end ((W))
2608
2609 #else
2610
2611 #define CHECK_WINDOW_END(W) (void) 0
2612
2613 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2614
2615 /* Return mark position if current buffer has the region of non-zero length,
2616 or -1 otherwise. */
2617
2618 static ptrdiff_t
2619 markpos_of_region (void)
2620 {
2621 if (!NILP (Vtransient_mark_mode)
2622 && !NILP (BVAR (current_buffer, mark_active))
2623 && XMARKER (BVAR (current_buffer, mark))->buffer != NULL)
2624 {
2625 ptrdiff_t markpos = XMARKER (BVAR (current_buffer, mark))->charpos;
2626
2627 if (markpos != PT)
2628 return markpos;
2629 }
2630 return -1;
2631 }
2632
2633 /***********************************************************************
2634 Iterator initialization
2635 ***********************************************************************/
2636
2637 /* Initialize IT for displaying current_buffer in window W, starting
2638 at character position CHARPOS. CHARPOS < 0 means that no buffer
2639 position is specified which is useful when the iterator is assigned
2640 a position later. BYTEPOS is the byte position corresponding to
2641 CHARPOS.
2642
2643 If ROW is not null, calls to produce_glyphs with IT as parameter
2644 will produce glyphs in that row.
2645
2646 BASE_FACE_ID is the id of a base face to use. It must be one of
2647 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2648 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2649 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2650
2651 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2652 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2653 will be initialized to use the corresponding mode line glyph row of
2654 the desired matrix of W. */
2655
2656 void
2657 init_iterator (struct it *it, struct window *w,
2658 ptrdiff_t charpos, ptrdiff_t bytepos,
2659 struct glyph_row *row, enum face_id base_face_id)
2660 {
2661 ptrdiff_t markpos;
2662 enum face_id remapped_base_face_id = base_face_id;
2663
2664 /* Some precondition checks. */
2665 eassert (w != NULL && it != NULL);
2666 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2667 && charpos <= ZV));
2668
2669 /* If face attributes have been changed since the last redisplay,
2670 free realized faces now because they depend on face definitions
2671 that might have changed. Don't free faces while there might be
2672 desired matrices pending which reference these faces. */
2673 if (face_change_count && !inhibit_free_realized_faces)
2674 {
2675 face_change_count = 0;
2676 free_all_realized_faces (Qnil);
2677 }
2678
2679 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2680 if (! NILP (Vface_remapping_alist))
2681 remapped_base_face_id
2682 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2683
2684 /* Use one of the mode line rows of W's desired matrix if
2685 appropriate. */
2686 if (row == NULL)
2687 {
2688 if (base_face_id == MODE_LINE_FACE_ID
2689 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2690 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2691 else if (base_face_id == HEADER_LINE_FACE_ID)
2692 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2693 }
2694
2695 /* Clear IT. */
2696 memset (it, 0, sizeof *it);
2697 it->current.overlay_string_index = -1;
2698 it->current.dpvec_index = -1;
2699 it->base_face_id = remapped_base_face_id;
2700 it->string = Qnil;
2701 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2702 it->paragraph_embedding = L2R;
2703 it->bidi_it.string.lstring = Qnil;
2704 it->bidi_it.string.s = NULL;
2705 it->bidi_it.string.bufpos = 0;
2706 it->bidi_it.w = w;
2707
2708 /* The window in which we iterate over current_buffer: */
2709 XSETWINDOW (it->window, w);
2710 it->w = w;
2711 it->f = XFRAME (w->frame);
2712
2713 it->cmp_it.id = -1;
2714
2715 /* Extra space between lines (on window systems only). */
2716 if (base_face_id == DEFAULT_FACE_ID
2717 && FRAME_WINDOW_P (it->f))
2718 {
2719 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2720 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2721 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2722 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2723 * FRAME_LINE_HEIGHT (it->f));
2724 else if (it->f->extra_line_spacing > 0)
2725 it->extra_line_spacing = it->f->extra_line_spacing;
2726 it->max_extra_line_spacing = 0;
2727 }
2728
2729 /* If realized faces have been removed, e.g. because of face
2730 attribute changes of named faces, recompute them. When running
2731 in batch mode, the face cache of the initial frame is null. If
2732 we happen to get called, make a dummy face cache. */
2733 if (FRAME_FACE_CACHE (it->f) == NULL)
2734 init_frame_faces (it->f);
2735 if (FRAME_FACE_CACHE (it->f)->used == 0)
2736 recompute_basic_faces (it->f);
2737
2738 /* Current value of the `slice', `space-width', and 'height' properties. */
2739 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2740 it->space_width = Qnil;
2741 it->font_height = Qnil;
2742 it->override_ascent = -1;
2743
2744 /* Are control characters displayed as `^C'? */
2745 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2746
2747 /* -1 means everything between a CR and the following line end
2748 is invisible. >0 means lines indented more than this value are
2749 invisible. */
2750 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2751 ? (clip_to_bounds
2752 (-1, XINT (BVAR (current_buffer, selective_display)),
2753 PTRDIFF_MAX))
2754 : (!NILP (BVAR (current_buffer, selective_display))
2755 ? -1 : 0));
2756 it->selective_display_ellipsis_p
2757 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2758
2759 /* Display table to use. */
2760 it->dp = window_display_table (w);
2761
2762 /* Are multibyte characters enabled in current_buffer? */
2763 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2764
2765 /* If visible region is of non-zero length, set IT->region_beg_charpos
2766 and IT->region_end_charpos to the start and end of a visible region
2767 in window IT->w. Set both to -1 to indicate no region. */
2768 markpos = markpos_of_region ();
2769 if (markpos >= 0
2770 /* Maybe highlight only in selected window. */
2771 && (/* Either show region everywhere. */
2772 highlight_nonselected_windows
2773 /* Or show region in the selected window. */
2774 || w == XWINDOW (selected_window)
2775 /* Or show the region if we are in the mini-buffer and W is
2776 the window the mini-buffer refers to. */
2777 || (MINI_WINDOW_P (XWINDOW (selected_window))
2778 && WINDOWP (minibuf_selected_window)
2779 && w == XWINDOW (minibuf_selected_window))))
2780 {
2781 it->region_beg_charpos = min (PT, markpos);
2782 it->region_end_charpos = max (PT, markpos);
2783 }
2784 else
2785 it->region_beg_charpos = it->region_end_charpos = -1;
2786
2787 /* Get the position at which the redisplay_end_trigger hook should
2788 be run, if it is to be run at all. */
2789 if (MARKERP (w->redisplay_end_trigger)
2790 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2791 it->redisplay_end_trigger_charpos
2792 = marker_position (w->redisplay_end_trigger);
2793 else if (INTEGERP (w->redisplay_end_trigger))
2794 it->redisplay_end_trigger_charpos =
2795 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2796
2797 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2798
2799 /* Are lines in the display truncated? */
2800 if (base_face_id != DEFAULT_FACE_ID
2801 || it->w->hscroll
2802 || (! WINDOW_FULL_WIDTH_P (it->w)
2803 && ((!NILP (Vtruncate_partial_width_windows)
2804 && !INTEGERP (Vtruncate_partial_width_windows))
2805 || (INTEGERP (Vtruncate_partial_width_windows)
2806 && (WINDOW_TOTAL_COLS (it->w)
2807 < XINT (Vtruncate_partial_width_windows))))))
2808 it->line_wrap = TRUNCATE;
2809 else if (NILP (BVAR (current_buffer, truncate_lines)))
2810 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2811 ? WINDOW_WRAP : WORD_WRAP;
2812 else
2813 it->line_wrap = TRUNCATE;
2814
2815 /* Get dimensions of truncation and continuation glyphs. These are
2816 displayed as fringe bitmaps under X, but we need them for such
2817 frames when the fringes are turned off. But leave the dimensions
2818 zero for tooltip frames, as these glyphs look ugly there and also
2819 sabotage calculations of tooltip dimensions in x-show-tip. */
2820 #ifdef HAVE_WINDOW_SYSTEM
2821 if (!(FRAME_WINDOW_P (it->f)
2822 && FRAMEP (tip_frame)
2823 && it->f == XFRAME (tip_frame)))
2824 #endif
2825 {
2826 if (it->line_wrap == TRUNCATE)
2827 {
2828 /* We will need the truncation glyph. */
2829 eassert (it->glyph_row == NULL);
2830 produce_special_glyphs (it, IT_TRUNCATION);
2831 it->truncation_pixel_width = it->pixel_width;
2832 }
2833 else
2834 {
2835 /* We will need the continuation glyph. */
2836 eassert (it->glyph_row == NULL);
2837 produce_special_glyphs (it, IT_CONTINUATION);
2838 it->continuation_pixel_width = it->pixel_width;
2839 }
2840 }
2841
2842 /* Reset these values to zero because the produce_special_glyphs
2843 above has changed them. */
2844 it->pixel_width = it->ascent = it->descent = 0;
2845 it->phys_ascent = it->phys_descent = 0;
2846
2847 /* Set this after getting the dimensions of truncation and
2848 continuation glyphs, so that we don't produce glyphs when calling
2849 produce_special_glyphs, above. */
2850 it->glyph_row = row;
2851 it->area = TEXT_AREA;
2852
2853 /* Forget any previous info about this row being reversed. */
2854 if (it->glyph_row)
2855 it->glyph_row->reversed_p = 0;
2856
2857 /* Get the dimensions of the display area. The display area
2858 consists of the visible window area plus a horizontally scrolled
2859 part to the left of the window. All x-values are relative to the
2860 start of this total display area. */
2861 if (base_face_id != DEFAULT_FACE_ID)
2862 {
2863 /* Mode lines, menu bar in terminal frames. */
2864 it->first_visible_x = 0;
2865 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2866 }
2867 else
2868 {
2869 it->first_visible_x =
2870 window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2871 it->last_visible_x = (it->first_visible_x
2872 + window_box_width (w, TEXT_AREA));
2873
2874 /* If we truncate lines, leave room for the truncation glyph(s) at
2875 the right margin. Otherwise, leave room for the continuation
2876 glyph(s). Done only if the window has no fringes. Since we
2877 don't know at this point whether there will be any R2L lines in
2878 the window, we reserve space for truncation/continuation glyphs
2879 even if only one of the fringes is absent. */
2880 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2881 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2882 {
2883 if (it->line_wrap == TRUNCATE)
2884 it->last_visible_x -= it->truncation_pixel_width;
2885 else
2886 it->last_visible_x -= it->continuation_pixel_width;
2887 }
2888
2889 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2890 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2891 }
2892
2893 /* Leave room for a border glyph. */
2894 if (!FRAME_WINDOW_P (it->f)
2895 && !WINDOW_RIGHTMOST_P (it->w))
2896 it->last_visible_x -= 1;
2897
2898 it->last_visible_y = window_text_bottom_y (w);
2899
2900 /* For mode lines and alike, arrange for the first glyph having a
2901 left box line if the face specifies a box. */
2902 if (base_face_id != DEFAULT_FACE_ID)
2903 {
2904 struct face *face;
2905
2906 it->face_id = remapped_base_face_id;
2907
2908 /* If we have a boxed mode line, make the first character appear
2909 with a left box line. */
2910 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2911 if (face->box != FACE_NO_BOX)
2912 it->start_of_box_run_p = 1;
2913 }
2914
2915 /* If a buffer position was specified, set the iterator there,
2916 getting overlays and face properties from that position. */
2917 if (charpos >= BUF_BEG (current_buffer))
2918 {
2919 it->end_charpos = ZV;
2920 eassert (charpos == BYTE_TO_CHAR (bytepos));
2921 IT_CHARPOS (*it) = charpos;
2922 IT_BYTEPOS (*it) = bytepos;
2923
2924 /* We will rely on `reseat' to set this up properly, via
2925 handle_face_prop. */
2926 it->face_id = it->base_face_id;
2927
2928 it->start = it->current;
2929 /* Do we need to reorder bidirectional text? Not if this is a
2930 unibyte buffer: by definition, none of the single-byte
2931 characters are strong R2L, so no reordering is needed. And
2932 bidi.c doesn't support unibyte buffers anyway. Also, don't
2933 reorder while we are loading loadup.el, since the tables of
2934 character properties needed for reordering are not yet
2935 available. */
2936 it->bidi_p =
2937 NILP (Vpurify_flag)
2938 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2939 && it->multibyte_p;
2940
2941 /* If we are to reorder bidirectional text, init the bidi
2942 iterator. */
2943 if (it->bidi_p)
2944 {
2945 /* Note the paragraph direction that this buffer wants to
2946 use. */
2947 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2948 Qleft_to_right))
2949 it->paragraph_embedding = L2R;
2950 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2951 Qright_to_left))
2952 it->paragraph_embedding = R2L;
2953 else
2954 it->paragraph_embedding = NEUTRAL_DIR;
2955 bidi_unshelve_cache (NULL, 0);
2956 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2957 &it->bidi_it);
2958 }
2959
2960 /* Compute faces etc. */
2961 reseat (it, it->current.pos, 1);
2962 }
2963
2964 CHECK_IT (it);
2965 }
2966
2967
2968 /* Initialize IT for the display of window W with window start POS. */
2969
2970 void
2971 start_display (struct it *it, struct window *w, struct text_pos pos)
2972 {
2973 struct glyph_row *row;
2974 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2975
2976 row = w->desired_matrix->rows + first_vpos;
2977 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2978 it->first_vpos = first_vpos;
2979
2980 /* Don't reseat to previous visible line start if current start
2981 position is in a string or image. */
2982 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2983 {
2984 int start_at_line_beg_p;
2985 int first_y = it->current_y;
2986
2987 /* If window start is not at a line start, skip forward to POS to
2988 get the correct continuation lines width. */
2989 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2990 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2991 if (!start_at_line_beg_p)
2992 {
2993 int new_x;
2994
2995 reseat_at_previous_visible_line_start (it);
2996 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2997
2998 new_x = it->current_x + it->pixel_width;
2999
3000 /* If lines are continued, this line may end in the middle
3001 of a multi-glyph character (e.g. a control character
3002 displayed as \003, or in the middle of an overlay
3003 string). In this case move_it_to above will not have
3004 taken us to the start of the continuation line but to the
3005 end of the continued line. */
3006 if (it->current_x > 0
3007 && it->line_wrap != TRUNCATE /* Lines are continued. */
3008 && (/* And glyph doesn't fit on the line. */
3009 new_x > it->last_visible_x
3010 /* Or it fits exactly and we're on a window
3011 system frame. */
3012 || (new_x == it->last_visible_x
3013 && FRAME_WINDOW_P (it->f)
3014 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3015 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3016 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3017 {
3018 if ((it->current.dpvec_index >= 0
3019 || it->current.overlay_string_index >= 0)
3020 /* If we are on a newline from a display vector or
3021 overlay string, then we are already at the end of
3022 a screen line; no need to go to the next line in
3023 that case, as this line is not really continued.
3024 (If we do go to the next line, C-e will not DTRT.) */
3025 && it->c != '\n')
3026 {
3027 set_iterator_to_next (it, 1);
3028 move_it_in_display_line_to (it, -1, -1, 0);
3029 }
3030
3031 it->continuation_lines_width += it->current_x;
3032 }
3033 /* If the character at POS is displayed via a display
3034 vector, move_it_to above stops at the final glyph of
3035 IT->dpvec. To make the caller redisplay that character
3036 again (a.k.a. start at POS), we need to reset the
3037 dpvec_index to the beginning of IT->dpvec. */
3038 else if (it->current.dpvec_index >= 0)
3039 it->current.dpvec_index = 0;
3040
3041 /* We're starting a new display line, not affected by the
3042 height of the continued line, so clear the appropriate
3043 fields in the iterator structure. */
3044 it->max_ascent = it->max_descent = 0;
3045 it->max_phys_ascent = it->max_phys_descent = 0;
3046
3047 it->current_y = first_y;
3048 it->vpos = 0;
3049 it->current_x = it->hpos = 0;
3050 }
3051 }
3052 }
3053
3054
3055 /* Return 1 if POS is a position in ellipses displayed for invisible
3056 text. W is the window we display, for text property lookup. */
3057
3058 static int
3059 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3060 {
3061 Lisp_Object prop, window;
3062 int ellipses_p = 0;
3063 ptrdiff_t charpos = CHARPOS (pos->pos);
3064
3065 /* If POS specifies a position in a display vector, this might
3066 be for an ellipsis displayed for invisible text. We won't
3067 get the iterator set up for delivering that ellipsis unless
3068 we make sure that it gets aware of the invisible text. */
3069 if (pos->dpvec_index >= 0
3070 && pos->overlay_string_index < 0
3071 && CHARPOS (pos->string_pos) < 0
3072 && charpos > BEGV
3073 && (XSETWINDOW (window, w),
3074 prop = Fget_char_property (make_number (charpos),
3075 Qinvisible, window),
3076 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3077 {
3078 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3079 window);
3080 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3081 }
3082
3083 return ellipses_p;
3084 }
3085
3086
3087 /* Initialize IT for stepping through current_buffer in window W,
3088 starting at position POS that includes overlay string and display
3089 vector/ control character translation position information. Value
3090 is zero if there are overlay strings with newlines at POS. */
3091
3092 static int
3093 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3094 {
3095 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3096 int i, overlay_strings_with_newlines = 0;
3097
3098 /* If POS specifies a position in a display vector, this might
3099 be for an ellipsis displayed for invisible text. We won't
3100 get the iterator set up for delivering that ellipsis unless
3101 we make sure that it gets aware of the invisible text. */
3102 if (in_ellipses_for_invisible_text_p (pos, w))
3103 {
3104 --charpos;
3105 bytepos = 0;
3106 }
3107
3108 /* Keep in mind: the call to reseat in init_iterator skips invisible
3109 text, so we might end up at a position different from POS. This
3110 is only a problem when POS is a row start after a newline and an
3111 overlay starts there with an after-string, and the overlay has an
3112 invisible property. Since we don't skip invisible text in
3113 display_line and elsewhere immediately after consuming the
3114 newline before the row start, such a POS will not be in a string,
3115 but the call to init_iterator below will move us to the
3116 after-string. */
3117 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3118
3119 /* This only scans the current chunk -- it should scan all chunks.
3120 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3121 to 16 in 22.1 to make this a lesser problem. */
3122 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3123 {
3124 const char *s = SSDATA (it->overlay_strings[i]);
3125 const char *e = s + SBYTES (it->overlay_strings[i]);
3126
3127 while (s < e && *s != '\n')
3128 ++s;
3129
3130 if (s < e)
3131 {
3132 overlay_strings_with_newlines = 1;
3133 break;
3134 }
3135 }
3136
3137 /* If position is within an overlay string, set up IT to the right
3138 overlay string. */
3139 if (pos->overlay_string_index >= 0)
3140 {
3141 int relative_index;
3142
3143 /* If the first overlay string happens to have a `display'
3144 property for an image, the iterator will be set up for that
3145 image, and we have to undo that setup first before we can
3146 correct the overlay string index. */
3147 if (it->method == GET_FROM_IMAGE)
3148 pop_it (it);
3149
3150 /* We already have the first chunk of overlay strings in
3151 IT->overlay_strings. Load more until the one for
3152 pos->overlay_string_index is in IT->overlay_strings. */
3153 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3154 {
3155 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3156 it->current.overlay_string_index = 0;
3157 while (n--)
3158 {
3159 load_overlay_strings (it, 0);
3160 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3161 }
3162 }
3163
3164 it->current.overlay_string_index = pos->overlay_string_index;
3165 relative_index = (it->current.overlay_string_index
3166 % OVERLAY_STRING_CHUNK_SIZE);
3167 it->string = it->overlay_strings[relative_index];
3168 eassert (STRINGP (it->string));
3169 it->current.string_pos = pos->string_pos;
3170 it->method = GET_FROM_STRING;
3171 it->end_charpos = SCHARS (it->string);
3172 /* Set up the bidi iterator for this overlay string. */
3173 if (it->bidi_p)
3174 {
3175 it->bidi_it.string.lstring = it->string;
3176 it->bidi_it.string.s = NULL;
3177 it->bidi_it.string.schars = SCHARS (it->string);
3178 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3179 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3180 it->bidi_it.string.unibyte = !it->multibyte_p;
3181 it->bidi_it.w = it->w;
3182 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3183 FRAME_WINDOW_P (it->f), &it->bidi_it);
3184
3185 /* Synchronize the state of the bidi iterator with
3186 pos->string_pos. For any string position other than
3187 zero, this will be done automagically when we resume
3188 iteration over the string and get_visually_first_element
3189 is called. But if string_pos is zero, and the string is
3190 to be reordered for display, we need to resync manually,
3191 since it could be that the iteration state recorded in
3192 pos ended at string_pos of 0 moving backwards in string. */
3193 if (CHARPOS (pos->string_pos) == 0)
3194 {
3195 get_visually_first_element (it);
3196 if (IT_STRING_CHARPOS (*it) != 0)
3197 do {
3198 /* Paranoia. */
3199 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3200 bidi_move_to_visually_next (&it->bidi_it);
3201 } while (it->bidi_it.charpos != 0);
3202 }
3203 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3204 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3205 }
3206 }
3207
3208 if (CHARPOS (pos->string_pos) >= 0)
3209 {
3210 /* Recorded position is not in an overlay string, but in another
3211 string. This can only be a string from a `display' property.
3212 IT should already be filled with that string. */
3213 it->current.string_pos = pos->string_pos;
3214 eassert (STRINGP (it->string));
3215 if (it->bidi_p)
3216 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3217 FRAME_WINDOW_P (it->f), &it->bidi_it);
3218 }
3219
3220 /* Restore position in display vector translations, control
3221 character translations or ellipses. */
3222 if (pos->dpvec_index >= 0)
3223 {
3224 if (it->dpvec == NULL)
3225 get_next_display_element (it);
3226 eassert (it->dpvec && it->current.dpvec_index == 0);
3227 it->current.dpvec_index = pos->dpvec_index;
3228 }
3229
3230 CHECK_IT (it);
3231 return !overlay_strings_with_newlines;
3232 }
3233
3234
3235 /* Initialize IT for stepping through current_buffer in window W
3236 starting at ROW->start. */
3237
3238 static void
3239 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3240 {
3241 init_from_display_pos (it, w, &row->start);
3242 it->start = row->start;
3243 it->continuation_lines_width = row->continuation_lines_width;
3244 CHECK_IT (it);
3245 }
3246
3247
3248 /* Initialize IT for stepping through current_buffer in window W
3249 starting in the line following ROW, i.e. starting at ROW->end.
3250 Value is zero if there are overlay strings with newlines at ROW's
3251 end position. */
3252
3253 static int
3254 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3255 {
3256 int success = 0;
3257
3258 if (init_from_display_pos (it, w, &row->end))
3259 {
3260 if (row->continued_p)
3261 it->continuation_lines_width
3262 = row->continuation_lines_width + row->pixel_width;
3263 CHECK_IT (it);
3264 success = 1;
3265 }
3266
3267 return success;
3268 }
3269
3270
3271
3272 \f
3273 /***********************************************************************
3274 Text properties
3275 ***********************************************************************/
3276
3277 /* Called when IT reaches IT->stop_charpos. Handle text property and
3278 overlay changes. Set IT->stop_charpos to the next position where
3279 to stop. */
3280
3281 static void
3282 handle_stop (struct it *it)
3283 {
3284 enum prop_handled handled;
3285 int handle_overlay_change_p;
3286 struct props *p;
3287
3288 it->dpvec = NULL;
3289 it->current.dpvec_index = -1;
3290 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3291 it->ignore_overlay_strings_at_pos_p = 0;
3292 it->ellipsis_p = 0;
3293
3294 /* Use face of preceding text for ellipsis (if invisible) */
3295 if (it->selective_display_ellipsis_p)
3296 it->saved_face_id = it->face_id;
3297
3298 do
3299 {
3300 handled = HANDLED_NORMALLY;
3301
3302 /* Call text property handlers. */
3303 for (p = it_props; p->handler; ++p)
3304 {
3305 handled = p->handler (it);
3306
3307 if (handled == HANDLED_RECOMPUTE_PROPS)
3308 break;
3309 else if (handled == HANDLED_RETURN)
3310 {
3311 /* We still want to show before and after strings from
3312 overlays even if the actual buffer text is replaced. */
3313 if (!handle_overlay_change_p
3314 || it->sp > 1
3315 /* Don't call get_overlay_strings_1 if we already
3316 have overlay strings loaded, because doing so
3317 will load them again and push the iterator state
3318 onto the stack one more time, which is not
3319 expected by the rest of the code that processes
3320 overlay strings. */
3321 || (it->current.overlay_string_index < 0
3322 ? !get_overlay_strings_1 (it, 0, 0)
3323 : 0))
3324 {
3325 if (it->ellipsis_p)
3326 setup_for_ellipsis (it, 0);
3327 /* When handling a display spec, we might load an
3328 empty string. In that case, discard it here. We
3329 used to discard it in handle_single_display_spec,
3330 but that causes get_overlay_strings_1, above, to
3331 ignore overlay strings that we must check. */
3332 if (STRINGP (it->string) && !SCHARS (it->string))
3333 pop_it (it);
3334 return;
3335 }
3336 else if (STRINGP (it->string) && !SCHARS (it->string))
3337 pop_it (it);
3338 else
3339 {
3340 it->ignore_overlay_strings_at_pos_p = 1;
3341 it->string_from_display_prop_p = 0;
3342 it->from_disp_prop_p = 0;
3343 handle_overlay_change_p = 0;
3344 }
3345 handled = HANDLED_RECOMPUTE_PROPS;
3346 break;
3347 }
3348 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3349 handle_overlay_change_p = 0;
3350 }
3351
3352 if (handled != HANDLED_RECOMPUTE_PROPS)
3353 {
3354 /* Don't check for overlay strings below when set to deliver
3355 characters from a display vector. */
3356 if (it->method == GET_FROM_DISPLAY_VECTOR)
3357 handle_overlay_change_p = 0;
3358
3359 /* Handle overlay changes.
3360 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3361 if it finds overlays. */
3362 if (handle_overlay_change_p)
3363 handled = handle_overlay_change (it);
3364 }
3365
3366 if (it->ellipsis_p)
3367 {
3368 setup_for_ellipsis (it, 0);
3369 break;
3370 }
3371 }
3372 while (handled == HANDLED_RECOMPUTE_PROPS);
3373
3374 /* Determine where to stop next. */
3375 if (handled == HANDLED_NORMALLY)
3376 compute_stop_pos (it);
3377 }
3378
3379
3380 /* Compute IT->stop_charpos from text property and overlay change
3381 information for IT's current position. */
3382
3383 static void
3384 compute_stop_pos (struct it *it)
3385 {
3386 register INTERVAL iv, next_iv;
3387 Lisp_Object object, limit, position;
3388 ptrdiff_t charpos, bytepos;
3389
3390 if (STRINGP (it->string))
3391 {
3392 /* Strings are usually short, so don't limit the search for
3393 properties. */
3394 it->stop_charpos = it->end_charpos;
3395 object = it->string;
3396 limit = Qnil;
3397 charpos = IT_STRING_CHARPOS (*it);
3398 bytepos = IT_STRING_BYTEPOS (*it);
3399 }
3400 else
3401 {
3402 ptrdiff_t pos;
3403
3404 /* If end_charpos is out of range for some reason, such as a
3405 misbehaving display function, rationalize it (Bug#5984). */
3406 if (it->end_charpos > ZV)
3407 it->end_charpos = ZV;
3408 it->stop_charpos = it->end_charpos;
3409
3410 /* If next overlay change is in front of the current stop pos
3411 (which is IT->end_charpos), stop there. Note: value of
3412 next_overlay_change is point-max if no overlay change
3413 follows. */
3414 charpos = IT_CHARPOS (*it);
3415 bytepos = IT_BYTEPOS (*it);
3416 pos = next_overlay_change (charpos);
3417 if (pos < it->stop_charpos)
3418 it->stop_charpos = pos;
3419
3420 /* If showing the region, we have to stop at the region
3421 start or end because the face might change there. */
3422 if (it->region_beg_charpos > 0)
3423 {
3424 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3425 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3426 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3427 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3428 }
3429
3430 /* Set up variables for computing the stop position from text
3431 property changes. */
3432 XSETBUFFER (object, current_buffer);
3433 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3434 }
3435
3436 /* Get the interval containing IT's position. Value is a null
3437 interval if there isn't such an interval. */
3438 position = make_number (charpos);
3439 iv = validate_interval_range (object, &position, &position, 0);
3440 if (iv)
3441 {
3442 Lisp_Object values_here[LAST_PROP_IDX];
3443 struct props *p;
3444
3445 /* Get properties here. */
3446 for (p = it_props; p->handler; ++p)
3447 values_here[p->idx] = textget (iv->plist, *p->name);
3448
3449 /* Look for an interval following iv that has different
3450 properties. */
3451 for (next_iv = next_interval (iv);
3452 (next_iv
3453 && (NILP (limit)
3454 || XFASTINT (limit) > next_iv->position));
3455 next_iv = next_interval (next_iv))
3456 {
3457 for (p = it_props; p->handler; ++p)
3458 {
3459 Lisp_Object new_value;
3460
3461 new_value = textget (next_iv->plist, *p->name);
3462 if (!EQ (values_here[p->idx], new_value))
3463 break;
3464 }
3465
3466 if (p->handler)
3467 break;
3468 }
3469
3470 if (next_iv)
3471 {
3472 if (INTEGERP (limit)
3473 && next_iv->position >= XFASTINT (limit))
3474 /* No text property change up to limit. */
3475 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3476 else
3477 /* Text properties change in next_iv. */
3478 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3479 }
3480 }
3481
3482 if (it->cmp_it.id < 0)
3483 {
3484 ptrdiff_t stoppos = it->end_charpos;
3485
3486 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3487 stoppos = -1;
3488 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3489 stoppos, it->string);
3490 }
3491
3492 eassert (STRINGP (it->string)
3493 || (it->stop_charpos >= BEGV
3494 && it->stop_charpos >= IT_CHARPOS (*it)));
3495 }
3496
3497
3498 /* Return the position of the next overlay change after POS in
3499 current_buffer. Value is point-max if no overlay change
3500 follows. This is like `next-overlay-change' but doesn't use
3501 xmalloc. */
3502
3503 static ptrdiff_t
3504 next_overlay_change (ptrdiff_t pos)
3505 {
3506 ptrdiff_t i, noverlays;
3507 ptrdiff_t endpos;
3508 Lisp_Object *overlays;
3509
3510 /* Get all overlays at the given position. */
3511 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3512
3513 /* If any of these overlays ends before endpos,
3514 use its ending point instead. */
3515 for (i = 0; i < noverlays; ++i)
3516 {
3517 Lisp_Object oend;
3518 ptrdiff_t oendpos;
3519
3520 oend = OVERLAY_END (overlays[i]);
3521 oendpos = OVERLAY_POSITION (oend);
3522 endpos = min (endpos, oendpos);
3523 }
3524
3525 return endpos;
3526 }
3527
3528 /* How many characters forward to search for a display property or
3529 display string. Searching too far forward makes the bidi display
3530 sluggish, especially in small windows. */
3531 #define MAX_DISP_SCAN 250
3532
3533 /* Return the character position of a display string at or after
3534 position specified by POSITION. If no display string exists at or
3535 after POSITION, return ZV. A display string is either an overlay
3536 with `display' property whose value is a string, or a `display'
3537 text property whose value is a string. STRING is data about the
3538 string to iterate; if STRING->lstring is nil, we are iterating a
3539 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3540 on a GUI frame. DISP_PROP is set to zero if we searched
3541 MAX_DISP_SCAN characters forward without finding any display
3542 strings, non-zero otherwise. It is set to 2 if the display string
3543 uses any kind of `(space ...)' spec that will produce a stretch of
3544 white space in the text area. */
3545 ptrdiff_t
3546 compute_display_string_pos (struct text_pos *position,
3547 struct bidi_string_data *string,
3548 struct window *w,
3549 int frame_window_p, int *disp_prop)
3550 {
3551 /* OBJECT = nil means current buffer. */
3552 Lisp_Object object, object1;
3553 Lisp_Object pos, spec, limpos;
3554 int string_p = (string && (STRINGP (string->lstring) || string->s));
3555 ptrdiff_t eob = string_p ? string->schars : ZV;
3556 ptrdiff_t begb = string_p ? 0 : BEGV;
3557 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3558 ptrdiff_t lim =
3559 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3560 struct text_pos tpos;
3561 int rv = 0;
3562
3563 if (string && STRINGP (string->lstring))
3564 object1 = object = string->lstring;
3565 else if (w && !string_p)
3566 {
3567 XSETWINDOW (object, w);
3568 object1 = Qnil;
3569 }
3570 else
3571 object1 = object = Qnil;
3572
3573 *disp_prop = 1;
3574
3575 if (charpos >= eob
3576 /* We don't support display properties whose values are strings
3577 that have display string properties. */
3578 || string->from_disp_str
3579 /* C strings cannot have display properties. */
3580 || (string->s && !STRINGP (object)))
3581 {
3582 *disp_prop = 0;
3583 return eob;
3584 }
3585
3586 /* If the character at CHARPOS is where the display string begins,
3587 return CHARPOS. */
3588 pos = make_number (charpos);
3589 if (STRINGP (object))
3590 bufpos = string->bufpos;
3591 else
3592 bufpos = charpos;
3593 tpos = *position;
3594 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3595 && (charpos <= begb
3596 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3597 object),
3598 spec))
3599 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3600 frame_window_p)))
3601 {
3602 if (rv == 2)
3603 *disp_prop = 2;
3604 return charpos;
3605 }
3606
3607 /* Look forward for the first character with a `display' property
3608 that will replace the underlying text when displayed. */
3609 limpos = make_number (lim);
3610 do {
3611 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3612 CHARPOS (tpos) = XFASTINT (pos);
3613 if (CHARPOS (tpos) >= lim)
3614 {
3615 *disp_prop = 0;
3616 break;
3617 }
3618 if (STRINGP (object))
3619 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3620 else
3621 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3622 spec = Fget_char_property (pos, Qdisplay, object);
3623 if (!STRINGP (object))
3624 bufpos = CHARPOS (tpos);
3625 } while (NILP (spec)
3626 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3627 bufpos, frame_window_p)));
3628 if (rv == 2)
3629 *disp_prop = 2;
3630
3631 return CHARPOS (tpos);
3632 }
3633
3634 /* Return the character position of the end of the display string that
3635 started at CHARPOS. If there's no display string at CHARPOS,
3636 return -1. A display string is either an overlay with `display'
3637 property whose value is a string or a `display' text property whose
3638 value is a string. */
3639 ptrdiff_t
3640 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3641 {
3642 /* OBJECT = nil means current buffer. */
3643 Lisp_Object object =
3644 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3645 Lisp_Object pos = make_number (charpos);
3646 ptrdiff_t eob =
3647 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3648
3649 if (charpos >= eob || (string->s && !STRINGP (object)))
3650 return eob;
3651
3652 /* It could happen that the display property or overlay was removed
3653 since we found it in compute_display_string_pos above. One way
3654 this can happen is if JIT font-lock was called (through
3655 handle_fontified_prop), and jit-lock-functions remove text
3656 properties or overlays from the portion of buffer that includes
3657 CHARPOS. Muse mode is known to do that, for example. In this
3658 case, we return -1 to the caller, to signal that no display
3659 string is actually present at CHARPOS. See bidi_fetch_char for
3660 how this is handled.
3661
3662 An alternative would be to never look for display properties past
3663 it->stop_charpos. But neither compute_display_string_pos nor
3664 bidi_fetch_char that calls it know or care where the next
3665 stop_charpos is. */
3666 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3667 return -1;
3668
3669 /* Look forward for the first character where the `display' property
3670 changes. */
3671 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3672
3673 return XFASTINT (pos);
3674 }
3675
3676
3677 \f
3678 /***********************************************************************
3679 Fontification
3680 ***********************************************************************/
3681
3682 /* Handle changes in the `fontified' property of the current buffer by
3683 calling hook functions from Qfontification_functions to fontify
3684 regions of text. */
3685
3686 static enum prop_handled
3687 handle_fontified_prop (struct it *it)
3688 {
3689 Lisp_Object prop, pos;
3690 enum prop_handled handled = HANDLED_NORMALLY;
3691
3692 if (!NILP (Vmemory_full))
3693 return handled;
3694
3695 /* Get the value of the `fontified' property at IT's current buffer
3696 position. (The `fontified' property doesn't have a special
3697 meaning in strings.) If the value is nil, call functions from
3698 Qfontification_functions. */
3699 if (!STRINGP (it->string)
3700 && it->s == NULL
3701 && !NILP (Vfontification_functions)
3702 && !NILP (Vrun_hooks)
3703 && (pos = make_number (IT_CHARPOS (*it)),
3704 prop = Fget_char_property (pos, Qfontified, Qnil),
3705 /* Ignore the special cased nil value always present at EOB since
3706 no amount of fontifying will be able to change it. */
3707 NILP (prop) && IT_CHARPOS (*it) < Z))
3708 {
3709 ptrdiff_t count = SPECPDL_INDEX ();
3710 Lisp_Object val;
3711 struct buffer *obuf = current_buffer;
3712 int begv = BEGV, zv = ZV;
3713 int old_clip_changed = current_buffer->clip_changed;
3714
3715 val = Vfontification_functions;
3716 specbind (Qfontification_functions, Qnil);
3717
3718 eassert (it->end_charpos == ZV);
3719
3720 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3721 safe_call1 (val, pos);
3722 else
3723 {
3724 Lisp_Object fns, fn;
3725 struct gcpro gcpro1, gcpro2;
3726
3727 fns = Qnil;
3728 GCPRO2 (val, fns);
3729
3730 for (; CONSP (val); val = XCDR (val))
3731 {
3732 fn = XCAR (val);
3733
3734 if (EQ (fn, Qt))
3735 {
3736 /* A value of t indicates this hook has a local
3737 binding; it means to run the global binding too.
3738 In a global value, t should not occur. If it
3739 does, we must ignore it to avoid an endless
3740 loop. */
3741 for (fns = Fdefault_value (Qfontification_functions);
3742 CONSP (fns);
3743 fns = XCDR (fns))
3744 {
3745 fn = XCAR (fns);
3746 if (!EQ (fn, Qt))
3747 safe_call1 (fn, pos);
3748 }
3749 }
3750 else
3751 safe_call1 (fn, pos);
3752 }
3753
3754 UNGCPRO;
3755 }
3756
3757 unbind_to (count, Qnil);
3758
3759 /* Fontification functions routinely call `save-restriction'.
3760 Normally, this tags clip_changed, which can confuse redisplay
3761 (see discussion in Bug#6671). Since we don't perform any
3762 special handling of fontification changes in the case where
3763 `save-restriction' isn't called, there's no point doing so in
3764 this case either. So, if the buffer's restrictions are
3765 actually left unchanged, reset clip_changed. */
3766 if (obuf == current_buffer)
3767 {
3768 if (begv == BEGV && zv == ZV)
3769 current_buffer->clip_changed = old_clip_changed;
3770 }
3771 /* There isn't much we can reasonably do to protect against
3772 misbehaving fontification, but here's a fig leaf. */
3773 else if (BUFFER_LIVE_P (obuf))
3774 set_buffer_internal_1 (obuf);
3775
3776 /* The fontification code may have added/removed text.
3777 It could do even a lot worse, but let's at least protect against
3778 the most obvious case where only the text past `pos' gets changed',
3779 as is/was done in grep.el where some escapes sequences are turned
3780 into face properties (bug#7876). */
3781 it->end_charpos = ZV;
3782
3783 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3784 something. This avoids an endless loop if they failed to
3785 fontify the text for which reason ever. */
3786 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3787 handled = HANDLED_RECOMPUTE_PROPS;
3788 }
3789
3790 return handled;
3791 }
3792
3793
3794 \f
3795 /***********************************************************************
3796 Faces
3797 ***********************************************************************/
3798
3799 /* Set up iterator IT from face properties at its current position.
3800 Called from handle_stop. */
3801
3802 static enum prop_handled
3803 handle_face_prop (struct it *it)
3804 {
3805 int new_face_id;
3806 ptrdiff_t next_stop;
3807
3808 if (!STRINGP (it->string))
3809 {
3810 new_face_id
3811 = face_at_buffer_position (it->w,
3812 IT_CHARPOS (*it),
3813 it->region_beg_charpos,
3814 it->region_end_charpos,
3815 &next_stop,
3816 (IT_CHARPOS (*it)
3817 + TEXT_PROP_DISTANCE_LIMIT),
3818 0, it->base_face_id);
3819
3820 /* Is this a start of a run of characters with box face?
3821 Caveat: this can be called for a freshly initialized
3822 iterator; face_id is -1 in this case. We know that the new
3823 face will not change until limit, i.e. if the new face has a
3824 box, all characters up to limit will have one. But, as
3825 usual, we don't know whether limit is really the end. */
3826 if (new_face_id != it->face_id)
3827 {
3828 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3829 /* If it->face_id is -1, old_face below will be NULL, see
3830 the definition of FACE_FROM_ID. This will happen if this
3831 is the initial call that gets the face. */
3832 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3833
3834 /* If the value of face_id of the iterator is -1, we have to
3835 look in front of IT's position and see whether there is a
3836 face there that's different from new_face_id. */
3837 if (!old_face && IT_CHARPOS (*it) > BEG)
3838 {
3839 int prev_face_id = face_before_it_pos (it);
3840
3841 old_face = FACE_FROM_ID (it->f, prev_face_id);
3842 }
3843
3844 /* If the new face has a box, but the old face does not,
3845 this is the start of a run of characters with box face,
3846 i.e. this character has a shadow on the left side. */
3847 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3848 && (old_face == NULL || !old_face->box));
3849 it->face_box_p = new_face->box != FACE_NO_BOX;
3850 }
3851 }
3852 else
3853 {
3854 int base_face_id;
3855 ptrdiff_t bufpos;
3856 int i;
3857 Lisp_Object from_overlay
3858 = (it->current.overlay_string_index >= 0
3859 ? it->string_overlays[it->current.overlay_string_index
3860 % OVERLAY_STRING_CHUNK_SIZE]
3861 : Qnil);
3862
3863 /* See if we got to this string directly or indirectly from
3864 an overlay property. That includes the before-string or
3865 after-string of an overlay, strings in display properties
3866 provided by an overlay, their text properties, etc.
3867
3868 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3869 if (! NILP (from_overlay))
3870 for (i = it->sp - 1; i >= 0; i--)
3871 {
3872 if (it->stack[i].current.overlay_string_index >= 0)
3873 from_overlay
3874 = it->string_overlays[it->stack[i].current.overlay_string_index
3875 % OVERLAY_STRING_CHUNK_SIZE];
3876 else if (! NILP (it->stack[i].from_overlay))
3877 from_overlay = it->stack[i].from_overlay;
3878
3879 if (!NILP (from_overlay))
3880 break;
3881 }
3882
3883 if (! NILP (from_overlay))
3884 {
3885 bufpos = IT_CHARPOS (*it);
3886 /* For a string from an overlay, the base face depends
3887 only on text properties and ignores overlays. */
3888 base_face_id
3889 = face_for_overlay_string (it->w,
3890 IT_CHARPOS (*it),
3891 it->region_beg_charpos,
3892 it->region_end_charpos,
3893 &next_stop,
3894 (IT_CHARPOS (*it)
3895 + TEXT_PROP_DISTANCE_LIMIT),
3896 0,
3897 from_overlay);
3898 }
3899 else
3900 {
3901 bufpos = 0;
3902
3903 /* For strings from a `display' property, use the face at
3904 IT's current buffer position as the base face to merge
3905 with, so that overlay strings appear in the same face as
3906 surrounding text, unless they specify their own
3907 faces. */
3908 base_face_id = it->string_from_prefix_prop_p
3909 ? DEFAULT_FACE_ID
3910 : underlying_face_id (it);
3911 }
3912
3913 new_face_id = face_at_string_position (it->w,
3914 it->string,
3915 IT_STRING_CHARPOS (*it),
3916 bufpos,
3917 it->region_beg_charpos,
3918 it->region_end_charpos,
3919 &next_stop,
3920 base_face_id, 0);
3921
3922 /* Is this a start of a run of characters with box? Caveat:
3923 this can be called for a freshly allocated iterator; face_id
3924 is -1 is this case. We know that the new face will not
3925 change until the next check pos, i.e. if the new face has a
3926 box, all characters up to that position will have a
3927 box. But, as usual, we don't know whether that position
3928 is really the end. */
3929 if (new_face_id != it->face_id)
3930 {
3931 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3932 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3933
3934 /* If new face has a box but old face hasn't, this is the
3935 start of a run of characters with box, i.e. it has a
3936 shadow on the left side. */
3937 it->start_of_box_run_p
3938 = new_face->box && (old_face == NULL || !old_face->box);
3939 it->face_box_p = new_face->box != FACE_NO_BOX;
3940 }
3941 }
3942
3943 it->face_id = new_face_id;
3944 return HANDLED_NORMALLY;
3945 }
3946
3947
3948 /* Return the ID of the face ``underlying'' IT's current position,
3949 which is in a string. If the iterator is associated with a
3950 buffer, return the face at IT's current buffer position.
3951 Otherwise, use the iterator's base_face_id. */
3952
3953 static int
3954 underlying_face_id (struct it *it)
3955 {
3956 int face_id = it->base_face_id, i;
3957
3958 eassert (STRINGP (it->string));
3959
3960 for (i = it->sp - 1; i >= 0; --i)
3961 if (NILP (it->stack[i].string))
3962 face_id = it->stack[i].face_id;
3963
3964 return face_id;
3965 }
3966
3967
3968 /* Compute the face one character before or after the current position
3969 of IT, in the visual order. BEFORE_P non-zero means get the face
3970 in front (to the left in L2R paragraphs, to the right in R2L
3971 paragraphs) of IT's screen position. Value is the ID of the face. */
3972
3973 static int
3974 face_before_or_after_it_pos (struct it *it, int before_p)
3975 {
3976 int face_id, limit;
3977 ptrdiff_t next_check_charpos;
3978 struct it it_copy;
3979 void *it_copy_data = NULL;
3980
3981 eassert (it->s == NULL);
3982
3983 if (STRINGP (it->string))
3984 {
3985 ptrdiff_t bufpos, charpos;
3986 int base_face_id;
3987
3988 /* No face change past the end of the string (for the case
3989 we are padding with spaces). No face change before the
3990 string start. */
3991 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3992 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3993 return it->face_id;
3994
3995 if (!it->bidi_p)
3996 {
3997 /* Set charpos to the position before or after IT's current
3998 position, in the logical order, which in the non-bidi
3999 case is the same as the visual order. */
4000 if (before_p)
4001 charpos = IT_STRING_CHARPOS (*it) - 1;
4002 else if (it->what == IT_COMPOSITION)
4003 /* For composition, we must check the character after the
4004 composition. */
4005 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4006 else
4007 charpos = IT_STRING_CHARPOS (*it) + 1;
4008 }
4009 else
4010 {
4011 if (before_p)
4012 {
4013 /* With bidi iteration, the character before the current
4014 in the visual order cannot be found by simple
4015 iteration, because "reverse" reordering is not
4016 supported. Instead, we need to use the move_it_*
4017 family of functions. */
4018 /* Ignore face changes before the first visible
4019 character on this display line. */
4020 if (it->current_x <= it->first_visible_x)
4021 return it->face_id;
4022 SAVE_IT (it_copy, *it, it_copy_data);
4023 /* Implementation note: Since move_it_in_display_line
4024 works in the iterator geometry, and thinks the first
4025 character is always the leftmost, even in R2L lines,
4026 we don't need to distinguish between the R2L and L2R
4027 cases here. */
4028 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
4029 it_copy.current_x - 1, MOVE_TO_X);
4030 charpos = IT_STRING_CHARPOS (it_copy);
4031 RESTORE_IT (it, it, it_copy_data);
4032 }
4033 else
4034 {
4035 /* Set charpos to the string position of the character
4036 that comes after IT's current position in the visual
4037 order. */
4038 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4039
4040 it_copy = *it;
4041 while (n--)
4042 bidi_move_to_visually_next (&it_copy.bidi_it);
4043
4044 charpos = it_copy.bidi_it.charpos;
4045 }
4046 }
4047 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4048
4049 if (it->current.overlay_string_index >= 0)
4050 bufpos = IT_CHARPOS (*it);
4051 else
4052 bufpos = 0;
4053
4054 base_face_id = underlying_face_id (it);
4055
4056 /* Get the face for ASCII, or unibyte. */
4057 face_id = face_at_string_position (it->w,
4058 it->string,
4059 charpos,
4060 bufpos,
4061 it->region_beg_charpos,
4062 it->region_end_charpos,
4063 &next_check_charpos,
4064 base_face_id, 0);
4065
4066 /* Correct the face for charsets different from ASCII. Do it
4067 for the multibyte case only. The face returned above is
4068 suitable for unibyte text if IT->string is unibyte. */
4069 if (STRING_MULTIBYTE (it->string))
4070 {
4071 struct text_pos pos1 = string_pos (charpos, it->string);
4072 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4073 int c, len;
4074 struct face *face = FACE_FROM_ID (it->f, face_id);
4075
4076 c = string_char_and_length (p, &len);
4077 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4078 }
4079 }
4080 else
4081 {
4082 struct text_pos pos;
4083
4084 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4085 || (IT_CHARPOS (*it) <= BEGV && before_p))
4086 return it->face_id;
4087
4088 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4089 pos = it->current.pos;
4090
4091 if (!it->bidi_p)
4092 {
4093 if (before_p)
4094 DEC_TEXT_POS (pos, it->multibyte_p);
4095 else
4096 {
4097 if (it->what == IT_COMPOSITION)
4098 {
4099 /* For composition, we must check the position after
4100 the composition. */
4101 pos.charpos += it->cmp_it.nchars;
4102 pos.bytepos += it->len;
4103 }
4104 else
4105 INC_TEXT_POS (pos, it->multibyte_p);
4106 }
4107 }
4108 else
4109 {
4110 if (before_p)
4111 {
4112 /* With bidi iteration, the character before the current
4113 in the visual order cannot be found by simple
4114 iteration, because "reverse" reordering is not
4115 supported. Instead, we need to use the move_it_*
4116 family of functions. */
4117 /* Ignore face changes before the first visible
4118 character on this display line. */
4119 if (it->current_x <= it->first_visible_x)
4120 return it->face_id;
4121 SAVE_IT (it_copy, *it, it_copy_data);
4122 /* Implementation note: Since move_it_in_display_line
4123 works in the iterator geometry, and thinks the first
4124 character is always the leftmost, even in R2L lines,
4125 we don't need to distinguish between the R2L and L2R
4126 cases here. */
4127 move_it_in_display_line (&it_copy, ZV,
4128 it_copy.current_x - 1, MOVE_TO_X);
4129 pos = it_copy.current.pos;
4130 RESTORE_IT (it, it, it_copy_data);
4131 }
4132 else
4133 {
4134 /* Set charpos to the buffer position of the character
4135 that comes after IT's current position in the visual
4136 order. */
4137 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4138
4139 it_copy = *it;
4140 while (n--)
4141 bidi_move_to_visually_next (&it_copy.bidi_it);
4142
4143 SET_TEXT_POS (pos,
4144 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4145 }
4146 }
4147 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4148
4149 /* Determine face for CHARSET_ASCII, or unibyte. */
4150 face_id = face_at_buffer_position (it->w,
4151 CHARPOS (pos),
4152 it->region_beg_charpos,
4153 it->region_end_charpos,
4154 &next_check_charpos,
4155 limit, 0, -1);
4156
4157 /* Correct the face for charsets different from ASCII. Do it
4158 for the multibyte case only. The face returned above is
4159 suitable for unibyte text if current_buffer is unibyte. */
4160 if (it->multibyte_p)
4161 {
4162 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4163 struct face *face = FACE_FROM_ID (it->f, face_id);
4164 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4165 }
4166 }
4167
4168 return face_id;
4169 }
4170
4171
4172 \f
4173 /***********************************************************************
4174 Invisible text
4175 ***********************************************************************/
4176
4177 /* Set up iterator IT from invisible properties at its current
4178 position. Called from handle_stop. */
4179
4180 static enum prop_handled
4181 handle_invisible_prop (struct it *it)
4182 {
4183 enum prop_handled handled = HANDLED_NORMALLY;
4184 int invis_p;
4185 Lisp_Object prop;
4186
4187 if (STRINGP (it->string))
4188 {
4189 Lisp_Object end_charpos, limit, charpos;
4190
4191 /* Get the value of the invisible text property at the
4192 current position. Value will be nil if there is no such
4193 property. */
4194 charpos = make_number (IT_STRING_CHARPOS (*it));
4195 prop = Fget_text_property (charpos, Qinvisible, it->string);
4196 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4197
4198 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4199 {
4200 /* Record whether we have to display an ellipsis for the
4201 invisible text. */
4202 int display_ellipsis_p = (invis_p == 2);
4203 ptrdiff_t len, endpos;
4204
4205 handled = HANDLED_RECOMPUTE_PROPS;
4206
4207 /* Get the position at which the next visible text can be
4208 found in IT->string, if any. */
4209 endpos = len = SCHARS (it->string);
4210 XSETINT (limit, len);
4211 do
4212 {
4213 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4214 it->string, limit);
4215 if (INTEGERP (end_charpos))
4216 {
4217 endpos = XFASTINT (end_charpos);
4218 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4219 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4220 if (invis_p == 2)
4221 display_ellipsis_p = 1;
4222 }
4223 }
4224 while (invis_p && endpos < len);
4225
4226 if (display_ellipsis_p)
4227 it->ellipsis_p = 1;
4228
4229 if (endpos < len)
4230 {
4231 /* Text at END_CHARPOS is visible. Move IT there. */
4232 struct text_pos old;
4233 ptrdiff_t oldpos;
4234
4235 old = it->current.string_pos;
4236 oldpos = CHARPOS (old);
4237 if (it->bidi_p)
4238 {
4239 if (it->bidi_it.first_elt
4240 && it->bidi_it.charpos < SCHARS (it->string))
4241 bidi_paragraph_init (it->paragraph_embedding,
4242 &it->bidi_it, 1);
4243 /* Bidi-iterate out of the invisible text. */
4244 do
4245 {
4246 bidi_move_to_visually_next (&it->bidi_it);
4247 }
4248 while (oldpos <= it->bidi_it.charpos
4249 && it->bidi_it.charpos < endpos);
4250
4251 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4252 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4253 if (IT_CHARPOS (*it) >= endpos)
4254 it->prev_stop = endpos;
4255 }
4256 else
4257 {
4258 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4259 compute_string_pos (&it->current.string_pos, old, it->string);
4260 }
4261 }
4262 else
4263 {
4264 /* The rest of the string is invisible. If this is an
4265 overlay string, proceed with the next overlay string
4266 or whatever comes and return a character from there. */
4267 if (it->current.overlay_string_index >= 0
4268 && !display_ellipsis_p)
4269 {
4270 next_overlay_string (it);
4271 /* Don't check for overlay strings when we just
4272 finished processing them. */
4273 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4274 }
4275 else
4276 {
4277 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4278 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4279 }
4280 }
4281 }
4282 }
4283 else
4284 {
4285 ptrdiff_t newpos, next_stop, start_charpos, tem;
4286 Lisp_Object pos, overlay;
4287
4288 /* First of all, is there invisible text at this position? */
4289 tem = start_charpos = IT_CHARPOS (*it);
4290 pos = make_number (tem);
4291 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4292 &overlay);
4293 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4294
4295 /* If we are on invisible text, skip over it. */
4296 if (invis_p && start_charpos < it->end_charpos)
4297 {
4298 /* Record whether we have to display an ellipsis for the
4299 invisible text. */
4300 int display_ellipsis_p = invis_p == 2;
4301
4302 handled = HANDLED_RECOMPUTE_PROPS;
4303
4304 /* Loop skipping over invisible text. The loop is left at
4305 ZV or with IT on the first char being visible again. */
4306 do
4307 {
4308 /* Try to skip some invisible text. Return value is the
4309 position reached which can be equal to where we start
4310 if there is nothing invisible there. This skips both
4311 over invisible text properties and overlays with
4312 invisible property. */
4313 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4314
4315 /* If we skipped nothing at all we weren't at invisible
4316 text in the first place. If everything to the end of
4317 the buffer was skipped, end the loop. */
4318 if (newpos == tem || newpos >= ZV)
4319 invis_p = 0;
4320 else
4321 {
4322 /* We skipped some characters but not necessarily
4323 all there are. Check if we ended up on visible
4324 text. Fget_char_property returns the property of
4325 the char before the given position, i.e. if we
4326 get invis_p = 0, this means that the char at
4327 newpos is visible. */
4328 pos = make_number (newpos);
4329 prop = Fget_char_property (pos, Qinvisible, it->window);
4330 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4331 }
4332
4333 /* If we ended up on invisible text, proceed to
4334 skip starting with next_stop. */
4335 if (invis_p)
4336 tem = next_stop;
4337
4338 /* If there are adjacent invisible texts, don't lose the
4339 second one's ellipsis. */
4340 if (invis_p == 2)
4341 display_ellipsis_p = 1;
4342 }
4343 while (invis_p);
4344
4345 /* The position newpos is now either ZV or on visible text. */
4346 if (it->bidi_p)
4347 {
4348 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4349 int on_newline =
4350 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4351 int after_newline =
4352 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4353
4354 /* If the invisible text ends on a newline or on a
4355 character after a newline, we can avoid the costly,
4356 character by character, bidi iteration to NEWPOS, and
4357 instead simply reseat the iterator there. That's
4358 because all bidi reordering information is tossed at
4359 the newline. This is a big win for modes that hide
4360 complete lines, like Outline, Org, etc. */
4361 if (on_newline || after_newline)
4362 {
4363 struct text_pos tpos;
4364 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4365
4366 SET_TEXT_POS (tpos, newpos, bpos);
4367 reseat_1 (it, tpos, 0);
4368 /* If we reseat on a newline/ZV, we need to prep the
4369 bidi iterator for advancing to the next character
4370 after the newline/EOB, keeping the current paragraph
4371 direction (so that PRODUCE_GLYPHS does TRT wrt
4372 prepending/appending glyphs to a glyph row). */
4373 if (on_newline)
4374 {
4375 it->bidi_it.first_elt = 0;
4376 it->bidi_it.paragraph_dir = pdir;
4377 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4378 it->bidi_it.nchars = 1;
4379 it->bidi_it.ch_len = 1;
4380 }
4381 }
4382 else /* Must use the slow method. */
4383 {
4384 /* With bidi iteration, the region of invisible text
4385 could start and/or end in the middle of a
4386 non-base embedding level. Therefore, we need to
4387 skip invisible text using the bidi iterator,
4388 starting at IT's current position, until we find
4389 ourselves outside of the invisible text.
4390 Skipping invisible text _after_ bidi iteration
4391 avoids affecting the visual order of the
4392 displayed text when invisible properties are
4393 added or removed. */
4394 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4395 {
4396 /* If we were `reseat'ed to a new paragraph,
4397 determine the paragraph base direction. We
4398 need to do it now because
4399 next_element_from_buffer may not have a
4400 chance to do it, if we are going to skip any
4401 text at the beginning, which resets the
4402 FIRST_ELT flag. */
4403 bidi_paragraph_init (it->paragraph_embedding,
4404 &it->bidi_it, 1);
4405 }
4406 do
4407 {
4408 bidi_move_to_visually_next (&it->bidi_it);
4409 }
4410 while (it->stop_charpos <= it->bidi_it.charpos
4411 && it->bidi_it.charpos < newpos);
4412 IT_CHARPOS (*it) = it->bidi_it.charpos;
4413 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4414 /* If we overstepped NEWPOS, record its position in
4415 the iterator, so that we skip invisible text if
4416 later the bidi iteration lands us in the
4417 invisible region again. */
4418 if (IT_CHARPOS (*it) >= newpos)
4419 it->prev_stop = newpos;
4420 }
4421 }
4422 else
4423 {
4424 IT_CHARPOS (*it) = newpos;
4425 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4426 }
4427
4428 /* If there are before-strings at the start of invisible
4429 text, and the text is invisible because of a text
4430 property, arrange to show before-strings because 20.x did
4431 it that way. (If the text is invisible because of an
4432 overlay property instead of a text property, this is
4433 already handled in the overlay code.) */
4434 if (NILP (overlay)
4435 && get_overlay_strings (it, it->stop_charpos))
4436 {
4437 handled = HANDLED_RECOMPUTE_PROPS;
4438 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4439 }
4440 else if (display_ellipsis_p)
4441 {
4442 /* Make sure that the glyphs of the ellipsis will get
4443 correct `charpos' values. If we would not update
4444 it->position here, the glyphs would belong to the
4445 last visible character _before_ the invisible
4446 text, which confuses `set_cursor_from_row'.
4447
4448 We use the last invisible position instead of the
4449 first because this way the cursor is always drawn on
4450 the first "." of the ellipsis, whenever PT is inside
4451 the invisible text. Otherwise the cursor would be
4452 placed _after_ the ellipsis when the point is after the
4453 first invisible character. */
4454 if (!STRINGP (it->object))
4455 {
4456 it->position.charpos = newpos - 1;
4457 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4458 }
4459 it->ellipsis_p = 1;
4460 /* Let the ellipsis display before
4461 considering any properties of the following char.
4462 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4463 handled = HANDLED_RETURN;
4464 }
4465 }
4466 }
4467
4468 return handled;
4469 }
4470
4471
4472 /* Make iterator IT return `...' next.
4473 Replaces LEN characters from buffer. */
4474
4475 static void
4476 setup_for_ellipsis (struct it *it, int len)
4477 {
4478 /* Use the display table definition for `...'. Invalid glyphs
4479 will be handled by the method returning elements from dpvec. */
4480 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4481 {
4482 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4483 it->dpvec = v->contents;
4484 it->dpend = v->contents + v->header.size;
4485 }
4486 else
4487 {
4488 /* Default `...'. */
4489 it->dpvec = default_invis_vector;
4490 it->dpend = default_invis_vector + 3;
4491 }
4492
4493 it->dpvec_char_len = len;
4494 it->current.dpvec_index = 0;
4495 it->dpvec_face_id = -1;
4496
4497 /* Remember the current face id in case glyphs specify faces.
4498 IT's face is restored in set_iterator_to_next.
4499 saved_face_id was set to preceding char's face in handle_stop. */
4500 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4501 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4502
4503 it->method = GET_FROM_DISPLAY_VECTOR;
4504 it->ellipsis_p = 1;
4505 }
4506
4507
4508 \f
4509 /***********************************************************************
4510 'display' property
4511 ***********************************************************************/
4512
4513 /* Set up iterator IT from `display' property at its current position.
4514 Called from handle_stop.
4515 We return HANDLED_RETURN if some part of the display property
4516 overrides the display of the buffer text itself.
4517 Otherwise we return HANDLED_NORMALLY. */
4518
4519 static enum prop_handled
4520 handle_display_prop (struct it *it)
4521 {
4522 Lisp_Object propval, object, overlay;
4523 struct text_pos *position;
4524 ptrdiff_t bufpos;
4525 /* Nonzero if some property replaces the display of the text itself. */
4526 int display_replaced_p = 0;
4527
4528 if (STRINGP (it->string))
4529 {
4530 object = it->string;
4531 position = &it->current.string_pos;
4532 bufpos = CHARPOS (it->current.pos);
4533 }
4534 else
4535 {
4536 XSETWINDOW (object, it->w);
4537 position = &it->current.pos;
4538 bufpos = CHARPOS (*position);
4539 }
4540
4541 /* Reset those iterator values set from display property values. */
4542 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4543 it->space_width = Qnil;
4544 it->font_height = Qnil;
4545 it->voffset = 0;
4546
4547 /* We don't support recursive `display' properties, i.e. string
4548 values that have a string `display' property, that have a string
4549 `display' property etc. */
4550 if (!it->string_from_display_prop_p)
4551 it->area = TEXT_AREA;
4552
4553 propval = get_char_property_and_overlay (make_number (position->charpos),
4554 Qdisplay, object, &overlay);
4555 if (NILP (propval))
4556 return HANDLED_NORMALLY;
4557 /* Now OVERLAY is the overlay that gave us this property, or nil
4558 if it was a text property. */
4559
4560 if (!STRINGP (it->string))
4561 object = it->w->contents;
4562
4563 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4564 position, bufpos,
4565 FRAME_WINDOW_P (it->f));
4566
4567 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4568 }
4569
4570 /* Subroutine of handle_display_prop. Returns non-zero if the display
4571 specification in SPEC is a replacing specification, i.e. it would
4572 replace the text covered by `display' property with something else,
4573 such as an image or a display string. If SPEC includes any kind or
4574 `(space ...) specification, the value is 2; this is used by
4575 compute_display_string_pos, which see.
4576
4577 See handle_single_display_spec for documentation of arguments.
4578 frame_window_p is non-zero if the window being redisplayed is on a
4579 GUI frame; this argument is used only if IT is NULL, see below.
4580
4581 IT can be NULL, if this is called by the bidi reordering code
4582 through compute_display_string_pos, which see. In that case, this
4583 function only examines SPEC, but does not otherwise "handle" it, in
4584 the sense that it doesn't set up members of IT from the display
4585 spec. */
4586 static int
4587 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4588 Lisp_Object overlay, struct text_pos *position,
4589 ptrdiff_t bufpos, int frame_window_p)
4590 {
4591 int replacing_p = 0;
4592 int rv;
4593
4594 if (CONSP (spec)
4595 /* Simple specifications. */
4596 && !EQ (XCAR (spec), Qimage)
4597 && !EQ (XCAR (spec), Qspace)
4598 && !EQ (XCAR (spec), Qwhen)
4599 && !EQ (XCAR (spec), Qslice)
4600 && !EQ (XCAR (spec), Qspace_width)
4601 && !EQ (XCAR (spec), Qheight)
4602 && !EQ (XCAR (spec), Qraise)
4603 /* Marginal area specifications. */
4604 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4605 && !EQ (XCAR (spec), Qleft_fringe)
4606 && !EQ (XCAR (spec), Qright_fringe)
4607 && !NILP (XCAR (spec)))
4608 {
4609 for (; CONSP (spec); spec = XCDR (spec))
4610 {
4611 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4612 overlay, position, bufpos,
4613 replacing_p, frame_window_p)))
4614 {
4615 replacing_p = rv;
4616 /* If some text in a string is replaced, `position' no
4617 longer points to the position of `object'. */
4618 if (!it || STRINGP (object))
4619 break;
4620 }
4621 }
4622 }
4623 else if (VECTORP (spec))
4624 {
4625 ptrdiff_t i;
4626 for (i = 0; i < ASIZE (spec); ++i)
4627 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4628 overlay, position, bufpos,
4629 replacing_p, frame_window_p)))
4630 {
4631 replacing_p = rv;
4632 /* If some text in a string is replaced, `position' no
4633 longer points to the position of `object'. */
4634 if (!it || STRINGP (object))
4635 break;
4636 }
4637 }
4638 else
4639 {
4640 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4641 position, bufpos, 0,
4642 frame_window_p)))
4643 replacing_p = rv;
4644 }
4645
4646 return replacing_p;
4647 }
4648
4649 /* Value is the position of the end of the `display' property starting
4650 at START_POS in OBJECT. */
4651
4652 static struct text_pos
4653 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4654 {
4655 Lisp_Object end;
4656 struct text_pos end_pos;
4657
4658 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4659 Qdisplay, object, Qnil);
4660 CHARPOS (end_pos) = XFASTINT (end);
4661 if (STRINGP (object))
4662 compute_string_pos (&end_pos, start_pos, it->string);
4663 else
4664 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4665
4666 return end_pos;
4667 }
4668
4669
4670 /* Set up IT from a single `display' property specification SPEC. OBJECT
4671 is the object in which the `display' property was found. *POSITION
4672 is the position in OBJECT at which the `display' property was found.
4673 BUFPOS is the buffer position of OBJECT (different from POSITION if
4674 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4675 previously saw a display specification which already replaced text
4676 display with something else, for example an image; we ignore such
4677 properties after the first one has been processed.
4678
4679 OVERLAY is the overlay this `display' property came from,
4680 or nil if it was a text property.
4681
4682 If SPEC is a `space' or `image' specification, and in some other
4683 cases too, set *POSITION to the position where the `display'
4684 property ends.
4685
4686 If IT is NULL, only examine the property specification in SPEC, but
4687 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4688 is intended to be displayed in a window on a GUI frame.
4689
4690 Value is non-zero if something was found which replaces the display
4691 of buffer or string text. */
4692
4693 static int
4694 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4695 Lisp_Object overlay, struct text_pos *position,
4696 ptrdiff_t bufpos, int display_replaced_p,
4697 int frame_window_p)
4698 {
4699 Lisp_Object form;
4700 Lisp_Object location, value;
4701 struct text_pos start_pos = *position;
4702 int valid_p;
4703
4704 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4705 If the result is non-nil, use VALUE instead of SPEC. */
4706 form = Qt;
4707 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4708 {
4709 spec = XCDR (spec);
4710 if (!CONSP (spec))
4711 return 0;
4712 form = XCAR (spec);
4713 spec = XCDR (spec);
4714 }
4715
4716 if (!NILP (form) && !EQ (form, Qt))
4717 {
4718 ptrdiff_t count = SPECPDL_INDEX ();
4719 struct gcpro gcpro1;
4720
4721 /* Bind `object' to the object having the `display' property, a
4722 buffer or string. Bind `position' to the position in the
4723 object where the property was found, and `buffer-position'
4724 to the current position in the buffer. */
4725
4726 if (NILP (object))
4727 XSETBUFFER (object, current_buffer);
4728 specbind (Qobject, object);
4729 specbind (Qposition, make_number (CHARPOS (*position)));
4730 specbind (Qbuffer_position, make_number (bufpos));
4731 GCPRO1 (form);
4732 form = safe_eval (form);
4733 UNGCPRO;
4734 unbind_to (count, Qnil);
4735 }
4736
4737 if (NILP (form))
4738 return 0;
4739
4740 /* Handle `(height HEIGHT)' specifications. */
4741 if (CONSP (spec)
4742 && EQ (XCAR (spec), Qheight)
4743 && CONSP (XCDR (spec)))
4744 {
4745 if (it)
4746 {
4747 if (!FRAME_WINDOW_P (it->f))
4748 return 0;
4749
4750 it->font_height = XCAR (XCDR (spec));
4751 if (!NILP (it->font_height))
4752 {
4753 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4754 int new_height = -1;
4755
4756 if (CONSP (it->font_height)
4757 && (EQ (XCAR (it->font_height), Qplus)
4758 || EQ (XCAR (it->font_height), Qminus))
4759 && CONSP (XCDR (it->font_height))
4760 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4761 {
4762 /* `(+ N)' or `(- N)' where N is an integer. */
4763 int steps = XINT (XCAR (XCDR (it->font_height)));
4764 if (EQ (XCAR (it->font_height), Qplus))
4765 steps = - steps;
4766 it->face_id = smaller_face (it->f, it->face_id, steps);
4767 }
4768 else if (FUNCTIONP (it->font_height))
4769 {
4770 /* Call function with current height as argument.
4771 Value is the new height. */
4772 Lisp_Object height;
4773 height = safe_call1 (it->font_height,
4774 face->lface[LFACE_HEIGHT_INDEX]);
4775 if (NUMBERP (height))
4776 new_height = XFLOATINT (height);
4777 }
4778 else if (NUMBERP (it->font_height))
4779 {
4780 /* Value is a multiple of the canonical char height. */
4781 struct face *f;
4782
4783 f = FACE_FROM_ID (it->f,
4784 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4785 new_height = (XFLOATINT (it->font_height)
4786 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4787 }
4788 else
4789 {
4790 /* Evaluate IT->font_height with `height' bound to the
4791 current specified height to get the new height. */
4792 ptrdiff_t count = SPECPDL_INDEX ();
4793
4794 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4795 value = safe_eval (it->font_height);
4796 unbind_to (count, Qnil);
4797
4798 if (NUMBERP (value))
4799 new_height = XFLOATINT (value);
4800 }
4801
4802 if (new_height > 0)
4803 it->face_id = face_with_height (it->f, it->face_id, new_height);
4804 }
4805 }
4806
4807 return 0;
4808 }
4809
4810 /* Handle `(space-width WIDTH)'. */
4811 if (CONSP (spec)
4812 && EQ (XCAR (spec), Qspace_width)
4813 && CONSP (XCDR (spec)))
4814 {
4815 if (it)
4816 {
4817 if (!FRAME_WINDOW_P (it->f))
4818 return 0;
4819
4820 value = XCAR (XCDR (spec));
4821 if (NUMBERP (value) && XFLOATINT (value) > 0)
4822 it->space_width = value;
4823 }
4824
4825 return 0;
4826 }
4827
4828 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4829 if (CONSP (spec)
4830 && EQ (XCAR (spec), Qslice))
4831 {
4832 Lisp_Object tem;
4833
4834 if (it)
4835 {
4836 if (!FRAME_WINDOW_P (it->f))
4837 return 0;
4838
4839 if (tem = XCDR (spec), CONSP (tem))
4840 {
4841 it->slice.x = XCAR (tem);
4842 if (tem = XCDR (tem), CONSP (tem))
4843 {
4844 it->slice.y = XCAR (tem);
4845 if (tem = XCDR (tem), CONSP (tem))
4846 {
4847 it->slice.width = XCAR (tem);
4848 if (tem = XCDR (tem), CONSP (tem))
4849 it->slice.height = XCAR (tem);
4850 }
4851 }
4852 }
4853 }
4854
4855 return 0;
4856 }
4857
4858 /* Handle `(raise FACTOR)'. */
4859 if (CONSP (spec)
4860 && EQ (XCAR (spec), Qraise)
4861 && CONSP (XCDR (spec)))
4862 {
4863 if (it)
4864 {
4865 if (!FRAME_WINDOW_P (it->f))
4866 return 0;
4867
4868 #ifdef HAVE_WINDOW_SYSTEM
4869 value = XCAR (XCDR (spec));
4870 if (NUMBERP (value))
4871 {
4872 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4873 it->voffset = - (XFLOATINT (value)
4874 * (FONT_HEIGHT (face->font)));
4875 }
4876 #endif /* HAVE_WINDOW_SYSTEM */
4877 }
4878
4879 return 0;
4880 }
4881
4882 /* Don't handle the other kinds of display specifications
4883 inside a string that we got from a `display' property. */
4884 if (it && it->string_from_display_prop_p)
4885 return 0;
4886
4887 /* Characters having this form of property are not displayed, so
4888 we have to find the end of the property. */
4889 if (it)
4890 {
4891 start_pos = *position;
4892 *position = display_prop_end (it, object, start_pos);
4893 }
4894 value = Qnil;
4895
4896 /* Stop the scan at that end position--we assume that all
4897 text properties change there. */
4898 if (it)
4899 it->stop_charpos = position->charpos;
4900
4901 /* Handle `(left-fringe BITMAP [FACE])'
4902 and `(right-fringe BITMAP [FACE])'. */
4903 if (CONSP (spec)
4904 && (EQ (XCAR (spec), Qleft_fringe)
4905 || EQ (XCAR (spec), Qright_fringe))
4906 && CONSP (XCDR (spec)))
4907 {
4908 int fringe_bitmap;
4909
4910 if (it)
4911 {
4912 if (!FRAME_WINDOW_P (it->f))
4913 /* If we return here, POSITION has been advanced
4914 across the text with this property. */
4915 {
4916 /* Synchronize the bidi iterator with POSITION. This is
4917 needed because we are not going to push the iterator
4918 on behalf of this display property, so there will be
4919 no pop_it call to do this synchronization for us. */
4920 if (it->bidi_p)
4921 {
4922 it->position = *position;
4923 iterate_out_of_display_property (it);
4924 *position = it->position;
4925 }
4926 return 1;
4927 }
4928 }
4929 else if (!frame_window_p)
4930 return 1;
4931
4932 #ifdef HAVE_WINDOW_SYSTEM
4933 value = XCAR (XCDR (spec));
4934 if (!SYMBOLP (value)
4935 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4936 /* If we return here, POSITION has been advanced
4937 across the text with this property. */
4938 {
4939 if (it && it->bidi_p)
4940 {
4941 it->position = *position;
4942 iterate_out_of_display_property (it);
4943 *position = it->position;
4944 }
4945 return 1;
4946 }
4947
4948 if (it)
4949 {
4950 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4951
4952 if (CONSP (XCDR (XCDR (spec))))
4953 {
4954 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4955 int face_id2 = lookup_derived_face (it->f, face_name,
4956 FRINGE_FACE_ID, 0);
4957 if (face_id2 >= 0)
4958 face_id = face_id2;
4959 }
4960
4961 /* Save current settings of IT so that we can restore them
4962 when we are finished with the glyph property value. */
4963 push_it (it, position);
4964
4965 it->area = TEXT_AREA;
4966 it->what = IT_IMAGE;
4967 it->image_id = -1; /* no image */
4968 it->position = start_pos;
4969 it->object = NILP (object) ? it->w->contents : object;
4970 it->method = GET_FROM_IMAGE;
4971 it->from_overlay = Qnil;
4972 it->face_id = face_id;
4973 it->from_disp_prop_p = 1;
4974
4975 /* Say that we haven't consumed the characters with
4976 `display' property yet. The call to pop_it in
4977 set_iterator_to_next will clean this up. */
4978 *position = start_pos;
4979
4980 if (EQ (XCAR (spec), Qleft_fringe))
4981 {
4982 it->left_user_fringe_bitmap = fringe_bitmap;
4983 it->left_user_fringe_face_id = face_id;
4984 }
4985 else
4986 {
4987 it->right_user_fringe_bitmap = fringe_bitmap;
4988 it->right_user_fringe_face_id = face_id;
4989 }
4990 }
4991 #endif /* HAVE_WINDOW_SYSTEM */
4992 return 1;
4993 }
4994
4995 /* Prepare to handle `((margin left-margin) ...)',
4996 `((margin right-margin) ...)' and `((margin nil) ...)'
4997 prefixes for display specifications. */
4998 location = Qunbound;
4999 if (CONSP (spec) && CONSP (XCAR (spec)))
5000 {
5001 Lisp_Object tem;
5002
5003 value = XCDR (spec);
5004 if (CONSP (value))
5005 value = XCAR (value);
5006
5007 tem = XCAR (spec);
5008 if (EQ (XCAR (tem), Qmargin)
5009 && (tem = XCDR (tem),
5010 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5011 (NILP (tem)
5012 || EQ (tem, Qleft_margin)
5013 || EQ (tem, Qright_margin))))
5014 location = tem;
5015 }
5016
5017 if (EQ (location, Qunbound))
5018 {
5019 location = Qnil;
5020 value = spec;
5021 }
5022
5023 /* After this point, VALUE is the property after any
5024 margin prefix has been stripped. It must be a string,
5025 an image specification, or `(space ...)'.
5026
5027 LOCATION specifies where to display: `left-margin',
5028 `right-margin' or nil. */
5029
5030 valid_p = (STRINGP (value)
5031 #ifdef HAVE_WINDOW_SYSTEM
5032 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5033 && valid_image_p (value))
5034 #endif /* not HAVE_WINDOW_SYSTEM */
5035 || (CONSP (value) && EQ (XCAR (value), Qspace)));
5036
5037 if (valid_p && !display_replaced_p)
5038 {
5039 int retval = 1;
5040
5041 if (!it)
5042 {
5043 /* Callers need to know whether the display spec is any kind
5044 of `(space ...)' spec that is about to affect text-area
5045 display. */
5046 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5047 retval = 2;
5048 return retval;
5049 }
5050
5051 /* Save current settings of IT so that we can restore them
5052 when we are finished with the glyph property value. */
5053 push_it (it, position);
5054 it->from_overlay = overlay;
5055 it->from_disp_prop_p = 1;
5056
5057 if (NILP (location))
5058 it->area = TEXT_AREA;
5059 else if (EQ (location, Qleft_margin))
5060 it->area = LEFT_MARGIN_AREA;
5061 else
5062 it->area = RIGHT_MARGIN_AREA;
5063
5064 if (STRINGP (value))
5065 {
5066 it->string = value;
5067 it->multibyte_p = STRING_MULTIBYTE (it->string);
5068 it->current.overlay_string_index = -1;
5069 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5070 it->end_charpos = it->string_nchars = SCHARS (it->string);
5071 it->method = GET_FROM_STRING;
5072 it->stop_charpos = 0;
5073 it->prev_stop = 0;
5074 it->base_level_stop = 0;
5075 it->string_from_display_prop_p = 1;
5076 /* Say that we haven't consumed the characters with
5077 `display' property yet. The call to pop_it in
5078 set_iterator_to_next will clean this up. */
5079 if (BUFFERP (object))
5080 *position = start_pos;
5081
5082 /* Force paragraph direction to be that of the parent
5083 object. If the parent object's paragraph direction is
5084 not yet determined, default to L2R. */
5085 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5086 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5087 else
5088 it->paragraph_embedding = L2R;
5089
5090 /* Set up the bidi iterator for this display string. */
5091 if (it->bidi_p)
5092 {
5093 it->bidi_it.string.lstring = it->string;
5094 it->bidi_it.string.s = NULL;
5095 it->bidi_it.string.schars = it->end_charpos;
5096 it->bidi_it.string.bufpos = bufpos;
5097 it->bidi_it.string.from_disp_str = 1;
5098 it->bidi_it.string.unibyte = !it->multibyte_p;
5099 it->bidi_it.w = it->w;
5100 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5101 }
5102 }
5103 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5104 {
5105 it->method = GET_FROM_STRETCH;
5106 it->object = value;
5107 *position = it->position = start_pos;
5108 retval = 1 + (it->area == TEXT_AREA);
5109 }
5110 #ifdef HAVE_WINDOW_SYSTEM
5111 else
5112 {
5113 it->what = IT_IMAGE;
5114 it->image_id = lookup_image (it->f, value);
5115 it->position = start_pos;
5116 it->object = NILP (object) ? it->w->contents : object;
5117 it->method = GET_FROM_IMAGE;
5118
5119 /* Say that we haven't consumed the characters with
5120 `display' property yet. The call to pop_it in
5121 set_iterator_to_next will clean this up. */
5122 *position = start_pos;
5123 }
5124 #endif /* HAVE_WINDOW_SYSTEM */
5125
5126 return retval;
5127 }
5128
5129 /* Invalid property or property not supported. Restore
5130 POSITION to what it was before. */
5131 *position = start_pos;
5132 return 0;
5133 }
5134
5135 /* Check if PROP is a display property value whose text should be
5136 treated as intangible. OVERLAY is the overlay from which PROP
5137 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5138 specify the buffer position covered by PROP. */
5139
5140 int
5141 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5142 ptrdiff_t charpos, ptrdiff_t bytepos)
5143 {
5144 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5145 struct text_pos position;
5146
5147 SET_TEXT_POS (position, charpos, bytepos);
5148 return handle_display_spec (NULL, prop, Qnil, overlay,
5149 &position, charpos, frame_window_p);
5150 }
5151
5152
5153 /* Return 1 if PROP is a display sub-property value containing STRING.
5154
5155 Implementation note: this and the following function are really
5156 special cases of handle_display_spec and
5157 handle_single_display_spec, and should ideally use the same code.
5158 Until they do, these two pairs must be consistent and must be
5159 modified in sync. */
5160
5161 static int
5162 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5163 {
5164 if (EQ (string, prop))
5165 return 1;
5166
5167 /* Skip over `when FORM'. */
5168 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5169 {
5170 prop = XCDR (prop);
5171 if (!CONSP (prop))
5172 return 0;
5173 /* Actually, the condition following `when' should be eval'ed,
5174 like handle_single_display_spec does, and we should return
5175 zero if it evaluates to nil. However, this function is
5176 called only when the buffer was already displayed and some
5177 glyph in the glyph matrix was found to come from a display
5178 string. Therefore, the condition was already evaluated, and
5179 the result was non-nil, otherwise the display string wouldn't
5180 have been displayed and we would have never been called for
5181 this property. Thus, we can skip the evaluation and assume
5182 its result is non-nil. */
5183 prop = XCDR (prop);
5184 }
5185
5186 if (CONSP (prop))
5187 /* Skip over `margin LOCATION'. */
5188 if (EQ (XCAR (prop), Qmargin))
5189 {
5190 prop = XCDR (prop);
5191 if (!CONSP (prop))
5192 return 0;
5193
5194 prop = XCDR (prop);
5195 if (!CONSP (prop))
5196 return 0;
5197 }
5198
5199 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5200 }
5201
5202
5203 /* Return 1 if STRING appears in the `display' property PROP. */
5204
5205 static int
5206 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5207 {
5208 if (CONSP (prop)
5209 && !EQ (XCAR (prop), Qwhen)
5210 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5211 {
5212 /* A list of sub-properties. */
5213 while (CONSP (prop))
5214 {
5215 if (single_display_spec_string_p (XCAR (prop), string))
5216 return 1;
5217 prop = XCDR (prop);
5218 }
5219 }
5220 else if (VECTORP (prop))
5221 {
5222 /* A vector of sub-properties. */
5223 ptrdiff_t i;
5224 for (i = 0; i < ASIZE (prop); ++i)
5225 if (single_display_spec_string_p (AREF (prop, i), string))
5226 return 1;
5227 }
5228 else
5229 return single_display_spec_string_p (prop, string);
5230
5231 return 0;
5232 }
5233
5234 /* Look for STRING in overlays and text properties in the current
5235 buffer, between character positions FROM and TO (excluding TO).
5236 BACK_P non-zero means look back (in this case, TO is supposed to be
5237 less than FROM).
5238 Value is the first character position where STRING was found, or
5239 zero if it wasn't found before hitting TO.
5240
5241 This function may only use code that doesn't eval because it is
5242 called asynchronously from note_mouse_highlight. */
5243
5244 static ptrdiff_t
5245 string_buffer_position_lim (Lisp_Object string,
5246 ptrdiff_t from, ptrdiff_t to, int back_p)
5247 {
5248 Lisp_Object limit, prop, pos;
5249 int found = 0;
5250
5251 pos = make_number (max (from, BEGV));
5252
5253 if (!back_p) /* looking forward */
5254 {
5255 limit = make_number (min (to, ZV));
5256 while (!found && !EQ (pos, limit))
5257 {
5258 prop = Fget_char_property (pos, Qdisplay, Qnil);
5259 if (!NILP (prop) && display_prop_string_p (prop, string))
5260 found = 1;
5261 else
5262 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5263 limit);
5264 }
5265 }
5266 else /* looking back */
5267 {
5268 limit = make_number (max (to, BEGV));
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 = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5276 limit);
5277 }
5278 }
5279
5280 return found ? XINT (pos) : 0;
5281 }
5282
5283 /* Determine which buffer position in current buffer STRING comes from.
5284 AROUND_CHARPOS is an approximate position where it could come from.
5285 Value is the buffer position or 0 if it couldn't be determined.
5286
5287 This function is necessary because we don't record buffer positions
5288 in glyphs generated from strings (to keep struct glyph small).
5289 This function may only use code that doesn't eval because it is
5290 called asynchronously from note_mouse_highlight. */
5291
5292 static ptrdiff_t
5293 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5294 {
5295 const int MAX_DISTANCE = 1000;
5296 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5297 around_charpos + MAX_DISTANCE,
5298 0);
5299
5300 if (!found)
5301 found = string_buffer_position_lim (string, around_charpos,
5302 around_charpos - MAX_DISTANCE, 1);
5303 return found;
5304 }
5305
5306
5307 \f
5308 /***********************************************************************
5309 `composition' property
5310 ***********************************************************************/
5311
5312 /* Set up iterator IT from `composition' property at its current
5313 position. Called from handle_stop. */
5314
5315 static enum prop_handled
5316 handle_composition_prop (struct it *it)
5317 {
5318 Lisp_Object prop, string;
5319 ptrdiff_t pos, pos_byte, start, end;
5320
5321 if (STRINGP (it->string))
5322 {
5323 unsigned char *s;
5324
5325 pos = IT_STRING_CHARPOS (*it);
5326 pos_byte = IT_STRING_BYTEPOS (*it);
5327 string = it->string;
5328 s = SDATA (string) + pos_byte;
5329 it->c = STRING_CHAR (s);
5330 }
5331 else
5332 {
5333 pos = IT_CHARPOS (*it);
5334 pos_byte = IT_BYTEPOS (*it);
5335 string = Qnil;
5336 it->c = FETCH_CHAR (pos_byte);
5337 }
5338
5339 /* If there's a valid composition and point is not inside of the
5340 composition (in the case that the composition is from the current
5341 buffer), draw a glyph composed from the composition components. */
5342 if (find_composition (pos, -1, &start, &end, &prop, string)
5343 && composition_valid_p (start, end, prop)
5344 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5345 {
5346 if (start < pos)
5347 /* As we can't handle this situation (perhaps font-lock added
5348 a new composition), we just return here hoping that next
5349 redisplay will detect this composition much earlier. */
5350 return HANDLED_NORMALLY;
5351 if (start != pos)
5352 {
5353 if (STRINGP (it->string))
5354 pos_byte = string_char_to_byte (it->string, start);
5355 else
5356 pos_byte = CHAR_TO_BYTE (start);
5357 }
5358 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5359 prop, string);
5360
5361 if (it->cmp_it.id >= 0)
5362 {
5363 it->cmp_it.ch = -1;
5364 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5365 it->cmp_it.nglyphs = -1;
5366 }
5367 }
5368
5369 return HANDLED_NORMALLY;
5370 }
5371
5372
5373 \f
5374 /***********************************************************************
5375 Overlay strings
5376 ***********************************************************************/
5377
5378 /* The following structure is used to record overlay strings for
5379 later sorting in load_overlay_strings. */
5380
5381 struct overlay_entry
5382 {
5383 Lisp_Object overlay;
5384 Lisp_Object string;
5385 EMACS_INT priority;
5386 int after_string_p;
5387 };
5388
5389
5390 /* Set up iterator IT from overlay strings at its current position.
5391 Called from handle_stop. */
5392
5393 static enum prop_handled
5394 handle_overlay_change (struct it *it)
5395 {
5396 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5397 return HANDLED_RECOMPUTE_PROPS;
5398 else
5399 return HANDLED_NORMALLY;
5400 }
5401
5402
5403 /* Set up the next overlay string for delivery by IT, if there is an
5404 overlay string to deliver. Called by set_iterator_to_next when the
5405 end of the current overlay string is reached. If there are more
5406 overlay strings to display, IT->string and
5407 IT->current.overlay_string_index are set appropriately here.
5408 Otherwise IT->string is set to nil. */
5409
5410 static void
5411 next_overlay_string (struct it *it)
5412 {
5413 ++it->current.overlay_string_index;
5414 if (it->current.overlay_string_index == it->n_overlay_strings)
5415 {
5416 /* No more overlay strings. Restore IT's settings to what
5417 they were before overlay strings were processed, and
5418 continue to deliver from current_buffer. */
5419
5420 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5421 pop_it (it);
5422 eassert (it->sp > 0
5423 || (NILP (it->string)
5424 && it->method == GET_FROM_BUFFER
5425 && it->stop_charpos >= BEGV
5426 && it->stop_charpos <= it->end_charpos));
5427 it->current.overlay_string_index = -1;
5428 it->n_overlay_strings = 0;
5429 it->overlay_strings_charpos = -1;
5430 /* If there's an empty display string on the stack, pop the
5431 stack, to resync the bidi iterator with IT's position. Such
5432 empty strings are pushed onto the stack in
5433 get_overlay_strings_1. */
5434 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5435 pop_it (it);
5436
5437 /* If we're at the end of the buffer, record that we have
5438 processed the overlay strings there already, so that
5439 next_element_from_buffer doesn't try it again. */
5440 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5441 it->overlay_strings_at_end_processed_p = 1;
5442 }
5443 else
5444 {
5445 /* There are more overlay strings to process. If
5446 IT->current.overlay_string_index has advanced to a position
5447 where we must load IT->overlay_strings with more strings, do
5448 it. We must load at the IT->overlay_strings_charpos where
5449 IT->n_overlay_strings was originally computed; when invisible
5450 text is present, this might not be IT_CHARPOS (Bug#7016). */
5451 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5452
5453 if (it->current.overlay_string_index && i == 0)
5454 load_overlay_strings (it, it->overlay_strings_charpos);
5455
5456 /* Initialize IT to deliver display elements from the overlay
5457 string. */
5458 it->string = it->overlay_strings[i];
5459 it->multibyte_p = STRING_MULTIBYTE (it->string);
5460 SET_TEXT_POS (it->current.string_pos, 0, 0);
5461 it->method = GET_FROM_STRING;
5462 it->stop_charpos = 0;
5463 it->end_charpos = SCHARS (it->string);
5464 if (it->cmp_it.stop_pos >= 0)
5465 it->cmp_it.stop_pos = 0;
5466 it->prev_stop = 0;
5467 it->base_level_stop = 0;
5468
5469 /* Set up the bidi iterator for this overlay string. */
5470 if (it->bidi_p)
5471 {
5472 it->bidi_it.string.lstring = it->string;
5473 it->bidi_it.string.s = NULL;
5474 it->bidi_it.string.schars = SCHARS (it->string);
5475 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5476 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5477 it->bidi_it.string.unibyte = !it->multibyte_p;
5478 it->bidi_it.w = it->w;
5479 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5480 }
5481 }
5482
5483 CHECK_IT (it);
5484 }
5485
5486
5487 /* Compare two overlay_entry structures E1 and E2. Used as a
5488 comparison function for qsort in load_overlay_strings. Overlay
5489 strings for the same position are sorted so that
5490
5491 1. All after-strings come in front of before-strings, except
5492 when they come from the same overlay.
5493
5494 2. Within after-strings, strings are sorted so that overlay strings
5495 from overlays with higher priorities come first.
5496
5497 2. Within before-strings, strings are sorted so that overlay
5498 strings from overlays with higher priorities come last.
5499
5500 Value is analogous to strcmp. */
5501
5502
5503 static int
5504 compare_overlay_entries (const void *e1, const void *e2)
5505 {
5506 struct overlay_entry const *entry1 = e1;
5507 struct overlay_entry const *entry2 = e2;
5508 int result;
5509
5510 if (entry1->after_string_p != entry2->after_string_p)
5511 {
5512 /* Let after-strings appear in front of before-strings if
5513 they come from different overlays. */
5514 if (EQ (entry1->overlay, entry2->overlay))
5515 result = entry1->after_string_p ? 1 : -1;
5516 else
5517 result = entry1->after_string_p ? -1 : 1;
5518 }
5519 else if (entry1->priority != entry2->priority)
5520 {
5521 if (entry1->after_string_p)
5522 /* After-strings sorted in order of decreasing priority. */
5523 result = entry2->priority < entry1->priority ? -1 : 1;
5524 else
5525 /* Before-strings sorted in order of increasing priority. */
5526 result = entry1->priority < entry2->priority ? -1 : 1;
5527 }
5528 else
5529 result = 0;
5530
5531 return result;
5532 }
5533
5534
5535 /* Load the vector IT->overlay_strings with overlay strings from IT's
5536 current buffer position, or from CHARPOS if that is > 0. Set
5537 IT->n_overlays to the total number of overlay strings found.
5538
5539 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5540 a time. On entry into load_overlay_strings,
5541 IT->current.overlay_string_index gives the number of overlay
5542 strings that have already been loaded by previous calls to this
5543 function.
5544
5545 IT->add_overlay_start contains an additional overlay start
5546 position to consider for taking overlay strings from, if non-zero.
5547 This position comes into play when the overlay has an `invisible'
5548 property, and both before and after-strings. When we've skipped to
5549 the end of the overlay, because of its `invisible' property, we
5550 nevertheless want its before-string to appear.
5551 IT->add_overlay_start will contain the overlay start position
5552 in this case.
5553
5554 Overlay strings are sorted so that after-string strings come in
5555 front of before-string strings. Within before and after-strings,
5556 strings are sorted by overlay priority. See also function
5557 compare_overlay_entries. */
5558
5559 static void
5560 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5561 {
5562 Lisp_Object overlay, window, str, invisible;
5563 struct Lisp_Overlay *ov;
5564 ptrdiff_t start, end;
5565 ptrdiff_t size = 20;
5566 ptrdiff_t n = 0, i, j;
5567 int invis_p;
5568 struct overlay_entry *entries = alloca (size * sizeof *entries);
5569 USE_SAFE_ALLOCA;
5570
5571 if (charpos <= 0)
5572 charpos = IT_CHARPOS (*it);
5573
5574 /* Append the overlay string STRING of overlay OVERLAY to vector
5575 `entries' which has size `size' and currently contains `n'
5576 elements. AFTER_P non-zero means STRING is an after-string of
5577 OVERLAY. */
5578 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5579 do \
5580 { \
5581 Lisp_Object priority; \
5582 \
5583 if (n == size) \
5584 { \
5585 struct overlay_entry *old = entries; \
5586 SAFE_NALLOCA (entries, 2, size); \
5587 memcpy (entries, old, size * sizeof *entries); \
5588 size *= 2; \
5589 } \
5590 \
5591 entries[n].string = (STRING); \
5592 entries[n].overlay = (OVERLAY); \
5593 priority = Foverlay_get ((OVERLAY), Qpriority); \
5594 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5595 entries[n].after_string_p = (AFTER_P); \
5596 ++n; \
5597 } \
5598 while (0)
5599
5600 /* Process overlay before the overlay center. */
5601 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5602 {
5603 XSETMISC (overlay, ov);
5604 eassert (OVERLAYP (overlay));
5605 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5606 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5607
5608 if (end < charpos)
5609 break;
5610
5611 /* Skip this overlay if it doesn't start or end at IT's current
5612 position. */
5613 if (end != charpos && start != charpos)
5614 continue;
5615
5616 /* Skip this overlay if it doesn't apply to IT->w. */
5617 window = Foverlay_get (overlay, Qwindow);
5618 if (WINDOWP (window) && XWINDOW (window) != it->w)
5619 continue;
5620
5621 /* If the text ``under'' the overlay is invisible, both before-
5622 and after-strings from this overlay are visible; start and
5623 end position are indistinguishable. */
5624 invisible = Foverlay_get (overlay, Qinvisible);
5625 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5626
5627 /* If overlay has a non-empty before-string, record it. */
5628 if ((start == charpos || (end == charpos && invis_p))
5629 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5630 && SCHARS (str))
5631 RECORD_OVERLAY_STRING (overlay, str, 0);
5632
5633 /* If overlay has a non-empty after-string, record it. */
5634 if ((end == charpos || (start == charpos && invis_p))
5635 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5636 && SCHARS (str))
5637 RECORD_OVERLAY_STRING (overlay, str, 1);
5638 }
5639
5640 /* Process overlays after the overlay center. */
5641 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5642 {
5643 XSETMISC (overlay, ov);
5644 eassert (OVERLAYP (overlay));
5645 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5646 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5647
5648 if (start > charpos)
5649 break;
5650
5651 /* Skip this overlay if it doesn't start or end at IT's current
5652 position. */
5653 if (end != charpos && start != charpos)
5654 continue;
5655
5656 /* Skip this overlay if it doesn't apply to IT->w. */
5657 window = Foverlay_get (overlay, Qwindow);
5658 if (WINDOWP (window) && XWINDOW (window) != it->w)
5659 continue;
5660
5661 /* If the text ``under'' the overlay is invisible, it has a zero
5662 dimension, and both before- and after-strings apply. */
5663 invisible = Foverlay_get (overlay, Qinvisible);
5664 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5665
5666 /* If overlay has a non-empty before-string, record it. */
5667 if ((start == charpos || (end == charpos && invis_p))
5668 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5669 && SCHARS (str))
5670 RECORD_OVERLAY_STRING (overlay, str, 0);
5671
5672 /* If overlay has a non-empty after-string, record it. */
5673 if ((end == charpos || (start == charpos && invis_p))
5674 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5675 && SCHARS (str))
5676 RECORD_OVERLAY_STRING (overlay, str, 1);
5677 }
5678
5679 #undef RECORD_OVERLAY_STRING
5680
5681 /* Sort entries. */
5682 if (n > 1)
5683 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5684
5685 /* Record number of overlay strings, and where we computed it. */
5686 it->n_overlay_strings = n;
5687 it->overlay_strings_charpos = charpos;
5688
5689 /* IT->current.overlay_string_index is the number of overlay strings
5690 that have already been consumed by IT. Copy some of the
5691 remaining overlay strings to IT->overlay_strings. */
5692 i = 0;
5693 j = it->current.overlay_string_index;
5694 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5695 {
5696 it->overlay_strings[i] = entries[j].string;
5697 it->string_overlays[i++] = entries[j++].overlay;
5698 }
5699
5700 CHECK_IT (it);
5701 SAFE_FREE ();
5702 }
5703
5704
5705 /* Get the first chunk of overlay strings at IT's current buffer
5706 position, or at CHARPOS if that is > 0. Value is non-zero if at
5707 least one overlay string was found. */
5708
5709 static int
5710 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5711 {
5712 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5713 process. This fills IT->overlay_strings with strings, and sets
5714 IT->n_overlay_strings to the total number of strings to process.
5715 IT->pos.overlay_string_index has to be set temporarily to zero
5716 because load_overlay_strings needs this; it must be set to -1
5717 when no overlay strings are found because a zero value would
5718 indicate a position in the first overlay string. */
5719 it->current.overlay_string_index = 0;
5720 load_overlay_strings (it, charpos);
5721
5722 /* If we found overlay strings, set up IT to deliver display
5723 elements from the first one. Otherwise set up IT to deliver
5724 from current_buffer. */
5725 if (it->n_overlay_strings)
5726 {
5727 /* Make sure we know settings in current_buffer, so that we can
5728 restore meaningful values when we're done with the overlay
5729 strings. */
5730 if (compute_stop_p)
5731 compute_stop_pos (it);
5732 eassert (it->face_id >= 0);
5733
5734 /* Save IT's settings. They are restored after all overlay
5735 strings have been processed. */
5736 eassert (!compute_stop_p || it->sp == 0);
5737
5738 /* When called from handle_stop, there might be an empty display
5739 string loaded. In that case, don't bother saving it. But
5740 don't use this optimization with the bidi iterator, since we
5741 need the corresponding pop_it call to resync the bidi
5742 iterator's position with IT's position, after we are done
5743 with the overlay strings. (The corresponding call to pop_it
5744 in case of an empty display string is in
5745 next_overlay_string.) */
5746 if (!(!it->bidi_p
5747 && STRINGP (it->string) && !SCHARS (it->string)))
5748 push_it (it, NULL);
5749
5750 /* Set up IT to deliver display elements from the first overlay
5751 string. */
5752 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5753 it->string = it->overlay_strings[0];
5754 it->from_overlay = Qnil;
5755 it->stop_charpos = 0;
5756 eassert (STRINGP (it->string));
5757 it->end_charpos = SCHARS (it->string);
5758 it->prev_stop = 0;
5759 it->base_level_stop = 0;
5760 it->multibyte_p = STRING_MULTIBYTE (it->string);
5761 it->method = GET_FROM_STRING;
5762 it->from_disp_prop_p = 0;
5763
5764 /* Force paragraph direction to be that of the parent
5765 buffer. */
5766 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5767 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5768 else
5769 it->paragraph_embedding = L2R;
5770
5771 /* Set up the bidi iterator for this overlay string. */
5772 if (it->bidi_p)
5773 {
5774 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5775
5776 it->bidi_it.string.lstring = it->string;
5777 it->bidi_it.string.s = NULL;
5778 it->bidi_it.string.schars = SCHARS (it->string);
5779 it->bidi_it.string.bufpos = pos;
5780 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5781 it->bidi_it.string.unibyte = !it->multibyte_p;
5782 it->bidi_it.w = it->w;
5783 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5784 }
5785 return 1;
5786 }
5787
5788 it->current.overlay_string_index = -1;
5789 return 0;
5790 }
5791
5792 static int
5793 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5794 {
5795 it->string = Qnil;
5796 it->method = GET_FROM_BUFFER;
5797
5798 (void) get_overlay_strings_1 (it, charpos, 1);
5799
5800 CHECK_IT (it);
5801
5802 /* Value is non-zero if we found at least one overlay string. */
5803 return STRINGP (it->string);
5804 }
5805
5806
5807 \f
5808 /***********************************************************************
5809 Saving and restoring state
5810 ***********************************************************************/
5811
5812 /* Save current settings of IT on IT->stack. Called, for example,
5813 before setting up IT for an overlay string, to be able to restore
5814 IT's settings to what they were after the overlay string has been
5815 processed. If POSITION is non-NULL, it is the position to save on
5816 the stack instead of IT->position. */
5817
5818 static void
5819 push_it (struct it *it, struct text_pos *position)
5820 {
5821 struct iterator_stack_entry *p;
5822
5823 eassert (it->sp < IT_STACK_SIZE);
5824 p = it->stack + it->sp;
5825
5826 p->stop_charpos = it->stop_charpos;
5827 p->prev_stop = it->prev_stop;
5828 p->base_level_stop = it->base_level_stop;
5829 p->cmp_it = it->cmp_it;
5830 eassert (it->face_id >= 0);
5831 p->face_id = it->face_id;
5832 p->string = it->string;
5833 p->method = it->method;
5834 p->from_overlay = it->from_overlay;
5835 switch (p->method)
5836 {
5837 case GET_FROM_IMAGE:
5838 p->u.image.object = it->object;
5839 p->u.image.image_id = it->image_id;
5840 p->u.image.slice = it->slice;
5841 break;
5842 case GET_FROM_STRETCH:
5843 p->u.stretch.object = it->object;
5844 break;
5845 }
5846 p->position = position ? *position : it->position;
5847 p->current = it->current;
5848 p->end_charpos = it->end_charpos;
5849 p->string_nchars = it->string_nchars;
5850 p->area = it->area;
5851 p->multibyte_p = it->multibyte_p;
5852 p->avoid_cursor_p = it->avoid_cursor_p;
5853 p->space_width = it->space_width;
5854 p->font_height = it->font_height;
5855 p->voffset = it->voffset;
5856 p->string_from_display_prop_p = it->string_from_display_prop_p;
5857 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5858 p->display_ellipsis_p = 0;
5859 p->line_wrap = it->line_wrap;
5860 p->bidi_p = it->bidi_p;
5861 p->paragraph_embedding = it->paragraph_embedding;
5862 p->from_disp_prop_p = it->from_disp_prop_p;
5863 ++it->sp;
5864
5865 /* Save the state of the bidi iterator as well. */
5866 if (it->bidi_p)
5867 bidi_push_it (&it->bidi_it);
5868 }
5869
5870 static void
5871 iterate_out_of_display_property (struct it *it)
5872 {
5873 int buffer_p = !STRINGP (it->string);
5874 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5875 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5876
5877 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5878
5879 /* Maybe initialize paragraph direction. If we are at the beginning
5880 of a new paragraph, next_element_from_buffer may not have a
5881 chance to do that. */
5882 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5883 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5884 /* prev_stop can be zero, so check against BEGV as well. */
5885 while (it->bidi_it.charpos >= bob
5886 && it->prev_stop <= it->bidi_it.charpos
5887 && it->bidi_it.charpos < CHARPOS (it->position)
5888 && it->bidi_it.charpos < eob)
5889 bidi_move_to_visually_next (&it->bidi_it);
5890 /* Record the stop_pos we just crossed, for when we cross it
5891 back, maybe. */
5892 if (it->bidi_it.charpos > CHARPOS (it->position))
5893 it->prev_stop = CHARPOS (it->position);
5894 /* If we ended up not where pop_it put us, resync IT's
5895 positional members with the bidi iterator. */
5896 if (it->bidi_it.charpos != CHARPOS (it->position))
5897 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5898 if (buffer_p)
5899 it->current.pos = it->position;
5900 else
5901 it->current.string_pos = it->position;
5902 }
5903
5904 /* Restore IT's settings from IT->stack. Called, for example, when no
5905 more overlay strings must be processed, and we return to delivering
5906 display elements from a buffer, or when the end of a string from a
5907 `display' property is reached and we return to delivering display
5908 elements from an overlay string, or from a buffer. */
5909
5910 static void
5911 pop_it (struct it *it)
5912 {
5913 struct iterator_stack_entry *p;
5914 int from_display_prop = it->from_disp_prop_p;
5915
5916 eassert (it->sp > 0);
5917 --it->sp;
5918 p = it->stack + it->sp;
5919 it->stop_charpos = p->stop_charpos;
5920 it->prev_stop = p->prev_stop;
5921 it->base_level_stop = p->base_level_stop;
5922 it->cmp_it = p->cmp_it;
5923 it->face_id = p->face_id;
5924 it->current = p->current;
5925 it->position = p->position;
5926 it->string = p->string;
5927 it->from_overlay = p->from_overlay;
5928 if (NILP (it->string))
5929 SET_TEXT_POS (it->current.string_pos, -1, -1);
5930 it->method = p->method;
5931 switch (it->method)
5932 {
5933 case GET_FROM_IMAGE:
5934 it->image_id = p->u.image.image_id;
5935 it->object = p->u.image.object;
5936 it->slice = p->u.image.slice;
5937 break;
5938 case GET_FROM_STRETCH:
5939 it->object = p->u.stretch.object;
5940 break;
5941 case GET_FROM_BUFFER:
5942 it->object = it->w->contents;
5943 break;
5944 case GET_FROM_STRING:
5945 it->object = it->string;
5946 break;
5947 case GET_FROM_DISPLAY_VECTOR:
5948 if (it->s)
5949 it->method = GET_FROM_C_STRING;
5950 else if (STRINGP (it->string))
5951 it->method = GET_FROM_STRING;
5952 else
5953 {
5954 it->method = GET_FROM_BUFFER;
5955 it->object = it->w->contents;
5956 }
5957 }
5958 it->end_charpos = p->end_charpos;
5959 it->string_nchars = p->string_nchars;
5960 it->area = p->area;
5961 it->multibyte_p = p->multibyte_p;
5962 it->avoid_cursor_p = p->avoid_cursor_p;
5963 it->space_width = p->space_width;
5964 it->font_height = p->font_height;
5965 it->voffset = p->voffset;
5966 it->string_from_display_prop_p = p->string_from_display_prop_p;
5967 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5968 it->line_wrap = p->line_wrap;
5969 it->bidi_p = p->bidi_p;
5970 it->paragraph_embedding = p->paragraph_embedding;
5971 it->from_disp_prop_p = p->from_disp_prop_p;
5972 if (it->bidi_p)
5973 {
5974 bidi_pop_it (&it->bidi_it);
5975 /* Bidi-iterate until we get out of the portion of text, if any,
5976 covered by a `display' text property or by an overlay with
5977 `display' property. (We cannot just jump there, because the
5978 internal coherency of the bidi iterator state can not be
5979 preserved across such jumps.) We also must determine the
5980 paragraph base direction if the overlay we just processed is
5981 at the beginning of a new paragraph. */
5982 if (from_display_prop
5983 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5984 iterate_out_of_display_property (it);
5985
5986 eassert ((BUFFERP (it->object)
5987 && IT_CHARPOS (*it) == it->bidi_it.charpos
5988 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5989 || (STRINGP (it->object)
5990 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5991 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5992 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5993 }
5994 }
5995
5996
5997 \f
5998 /***********************************************************************
5999 Moving over lines
6000 ***********************************************************************/
6001
6002 /* Set IT's current position to the previous line start. */
6003
6004 static void
6005 back_to_previous_line_start (struct it *it)
6006 {
6007 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6008
6009 DEC_BOTH (cp, bp);
6010 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6011 }
6012
6013
6014 /* Move IT to the next line start.
6015
6016 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
6017 we skipped over part of the text (as opposed to moving the iterator
6018 continuously over the text). Otherwise, don't change the value
6019 of *SKIPPED_P.
6020
6021 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6022 iterator on the newline, if it was found.
6023
6024 Newlines may come from buffer text, overlay strings, or strings
6025 displayed via the `display' property. That's the reason we can't
6026 simply use find_newline_no_quit.
6027
6028 Note that this function may not skip over invisible text that is so
6029 because of text properties and immediately follows a newline. If
6030 it would, function reseat_at_next_visible_line_start, when called
6031 from set_iterator_to_next, would effectively make invisible
6032 characters following a newline part of the wrong glyph row, which
6033 leads to wrong cursor motion. */
6034
6035 static int
6036 forward_to_next_line_start (struct it *it, int *skipped_p,
6037 struct bidi_it *bidi_it_prev)
6038 {
6039 ptrdiff_t old_selective;
6040 int newline_found_p, n;
6041 const int MAX_NEWLINE_DISTANCE = 500;
6042
6043 /* If already on a newline, just consume it to avoid unintended
6044 skipping over invisible text below. */
6045 if (it->what == IT_CHARACTER
6046 && it->c == '\n'
6047 && CHARPOS (it->position) == IT_CHARPOS (*it))
6048 {
6049 if (it->bidi_p && bidi_it_prev)
6050 *bidi_it_prev = it->bidi_it;
6051 set_iterator_to_next (it, 0);
6052 it->c = 0;
6053 return 1;
6054 }
6055
6056 /* Don't handle selective display in the following. It's (a)
6057 unnecessary because it's done by the caller, and (b) leads to an
6058 infinite recursion because next_element_from_ellipsis indirectly
6059 calls this function. */
6060 old_selective = it->selective;
6061 it->selective = 0;
6062
6063 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6064 from buffer text. */
6065 for (n = newline_found_p = 0;
6066 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6067 n += STRINGP (it->string) ? 0 : 1)
6068 {
6069 if (!get_next_display_element (it))
6070 return 0;
6071 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6072 if (newline_found_p && it->bidi_p && bidi_it_prev)
6073 *bidi_it_prev = it->bidi_it;
6074 set_iterator_to_next (it, 0);
6075 }
6076
6077 /* If we didn't find a newline near enough, see if we can use a
6078 short-cut. */
6079 if (!newline_found_p)
6080 {
6081 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6082 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6083 1, &bytepos);
6084 Lisp_Object pos;
6085
6086 eassert (!STRINGP (it->string));
6087
6088 /* If there isn't any `display' property in sight, and no
6089 overlays, we can just use the position of the newline in
6090 buffer text. */
6091 if (it->stop_charpos >= limit
6092 || ((pos = Fnext_single_property_change (make_number (start),
6093 Qdisplay, Qnil,
6094 make_number (limit)),
6095 NILP (pos))
6096 && next_overlay_change (start) == ZV))
6097 {
6098 if (!it->bidi_p)
6099 {
6100 IT_CHARPOS (*it) = limit;
6101 IT_BYTEPOS (*it) = bytepos;
6102 }
6103 else
6104 {
6105 struct bidi_it bprev;
6106
6107 /* Help bidi.c avoid expensive searches for display
6108 properties and overlays, by telling it that there are
6109 none up to `limit'. */
6110 if (it->bidi_it.disp_pos < limit)
6111 {
6112 it->bidi_it.disp_pos = limit;
6113 it->bidi_it.disp_prop = 0;
6114 }
6115 do {
6116 bprev = it->bidi_it;
6117 bidi_move_to_visually_next (&it->bidi_it);
6118 } while (it->bidi_it.charpos != limit);
6119 IT_CHARPOS (*it) = limit;
6120 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6121 if (bidi_it_prev)
6122 *bidi_it_prev = bprev;
6123 }
6124 *skipped_p = newline_found_p = 1;
6125 }
6126 else
6127 {
6128 while (get_next_display_element (it)
6129 && !newline_found_p)
6130 {
6131 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6132 if (newline_found_p && it->bidi_p && bidi_it_prev)
6133 *bidi_it_prev = it->bidi_it;
6134 set_iterator_to_next (it, 0);
6135 }
6136 }
6137 }
6138
6139 it->selective = old_selective;
6140 return newline_found_p;
6141 }
6142
6143
6144 /* Set IT's current position to the previous visible line start. Skip
6145 invisible text that is so either due to text properties or due to
6146 selective display. Caution: this does not change IT->current_x and
6147 IT->hpos. */
6148
6149 static void
6150 back_to_previous_visible_line_start (struct it *it)
6151 {
6152 while (IT_CHARPOS (*it) > BEGV)
6153 {
6154 back_to_previous_line_start (it);
6155
6156 if (IT_CHARPOS (*it) <= BEGV)
6157 break;
6158
6159 /* If selective > 0, then lines indented more than its value are
6160 invisible. */
6161 if (it->selective > 0
6162 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6163 it->selective))
6164 continue;
6165
6166 /* Check the newline before point for invisibility. */
6167 {
6168 Lisp_Object prop;
6169 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6170 Qinvisible, it->window);
6171 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6172 continue;
6173 }
6174
6175 if (IT_CHARPOS (*it) <= BEGV)
6176 break;
6177
6178 {
6179 struct it it2;
6180 void *it2data = NULL;
6181 ptrdiff_t pos;
6182 ptrdiff_t beg, end;
6183 Lisp_Object val, overlay;
6184
6185 SAVE_IT (it2, *it, it2data);
6186
6187 /* If newline is part of a composition, continue from start of composition */
6188 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6189 && beg < IT_CHARPOS (*it))
6190 goto replaced;
6191
6192 /* If newline is replaced by a display property, find start of overlay
6193 or interval and continue search from that point. */
6194 pos = --IT_CHARPOS (it2);
6195 --IT_BYTEPOS (it2);
6196 it2.sp = 0;
6197 bidi_unshelve_cache (NULL, 0);
6198 it2.string_from_display_prop_p = 0;
6199 it2.from_disp_prop_p = 0;
6200 if (handle_display_prop (&it2) == HANDLED_RETURN
6201 && !NILP (val = get_char_property_and_overlay
6202 (make_number (pos), Qdisplay, Qnil, &overlay))
6203 && (OVERLAYP (overlay)
6204 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6205 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6206 {
6207 RESTORE_IT (it, it, it2data);
6208 goto replaced;
6209 }
6210
6211 /* Newline is not replaced by anything -- so we are done. */
6212 RESTORE_IT (it, it, it2data);
6213 break;
6214
6215 replaced:
6216 if (beg < BEGV)
6217 beg = BEGV;
6218 IT_CHARPOS (*it) = beg;
6219 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6220 }
6221 }
6222
6223 it->continuation_lines_width = 0;
6224
6225 eassert (IT_CHARPOS (*it) >= BEGV);
6226 eassert (IT_CHARPOS (*it) == BEGV
6227 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6228 CHECK_IT (it);
6229 }
6230
6231
6232 /* Reseat iterator IT at the previous visible line start. Skip
6233 invisible text that is so either due to text properties or due to
6234 selective display. At the end, update IT's overlay information,
6235 face information etc. */
6236
6237 void
6238 reseat_at_previous_visible_line_start (struct it *it)
6239 {
6240 back_to_previous_visible_line_start (it);
6241 reseat (it, it->current.pos, 1);
6242 CHECK_IT (it);
6243 }
6244
6245
6246 /* Reseat iterator IT on the next visible line start in the current
6247 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6248 preceding the line start. Skip over invisible text that is so
6249 because of selective display. Compute faces, overlays etc at the
6250 new position. Note that this function does not skip over text that
6251 is invisible because of text properties. */
6252
6253 static void
6254 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6255 {
6256 int newline_found_p, skipped_p = 0;
6257 struct bidi_it bidi_it_prev;
6258
6259 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6260
6261 /* Skip over lines that are invisible because they are indented
6262 more than the value of IT->selective. */
6263 if (it->selective > 0)
6264 while (IT_CHARPOS (*it) < ZV
6265 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6266 it->selective))
6267 {
6268 eassert (IT_BYTEPOS (*it) == BEGV
6269 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6270 newline_found_p =
6271 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6272 }
6273
6274 /* Position on the newline if that's what's requested. */
6275 if (on_newline_p && newline_found_p)
6276 {
6277 if (STRINGP (it->string))
6278 {
6279 if (IT_STRING_CHARPOS (*it) > 0)
6280 {
6281 if (!it->bidi_p)
6282 {
6283 --IT_STRING_CHARPOS (*it);
6284 --IT_STRING_BYTEPOS (*it);
6285 }
6286 else
6287 {
6288 /* We need to restore the bidi iterator to the state
6289 it had on the newline, and resync the IT's
6290 position with that. */
6291 it->bidi_it = bidi_it_prev;
6292 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6293 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6294 }
6295 }
6296 }
6297 else if (IT_CHARPOS (*it) > BEGV)
6298 {
6299 if (!it->bidi_p)
6300 {
6301 --IT_CHARPOS (*it);
6302 --IT_BYTEPOS (*it);
6303 }
6304 else
6305 {
6306 /* We need to restore the bidi iterator to the state it
6307 had on the newline and resync IT with that. */
6308 it->bidi_it = bidi_it_prev;
6309 IT_CHARPOS (*it) = it->bidi_it.charpos;
6310 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6311 }
6312 reseat (it, it->current.pos, 0);
6313 }
6314 }
6315 else if (skipped_p)
6316 reseat (it, it->current.pos, 0);
6317
6318 CHECK_IT (it);
6319 }
6320
6321
6322 \f
6323 /***********************************************************************
6324 Changing an iterator's position
6325 ***********************************************************************/
6326
6327 /* Change IT's current position to POS in current_buffer. If FORCE_P
6328 is non-zero, always check for text properties at the new position.
6329 Otherwise, text properties are only looked up if POS >=
6330 IT->check_charpos of a property. */
6331
6332 static void
6333 reseat (struct it *it, struct text_pos pos, int force_p)
6334 {
6335 ptrdiff_t original_pos = IT_CHARPOS (*it);
6336
6337 reseat_1 (it, pos, 0);
6338
6339 /* Determine where to check text properties. Avoid doing it
6340 where possible because text property lookup is very expensive. */
6341 if (force_p
6342 || CHARPOS (pos) > it->stop_charpos
6343 || CHARPOS (pos) < original_pos)
6344 {
6345 if (it->bidi_p)
6346 {
6347 /* For bidi iteration, we need to prime prev_stop and
6348 base_level_stop with our best estimations. */
6349 /* Implementation note: Of course, POS is not necessarily a
6350 stop position, so assigning prev_pos to it is a lie; we
6351 should have called compute_stop_backwards. However, if
6352 the current buffer does not include any R2L characters,
6353 that call would be a waste of cycles, because the
6354 iterator will never move back, and thus never cross this
6355 "fake" stop position. So we delay that backward search
6356 until the time we really need it, in next_element_from_buffer. */
6357 if (CHARPOS (pos) != it->prev_stop)
6358 it->prev_stop = CHARPOS (pos);
6359 if (CHARPOS (pos) < it->base_level_stop)
6360 it->base_level_stop = 0; /* meaning it's unknown */
6361 handle_stop (it);
6362 }
6363 else
6364 {
6365 handle_stop (it);
6366 it->prev_stop = it->base_level_stop = 0;
6367 }
6368
6369 }
6370
6371 CHECK_IT (it);
6372 }
6373
6374
6375 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6376 IT->stop_pos to POS, also. */
6377
6378 static void
6379 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6380 {
6381 /* Don't call this function when scanning a C string. */
6382 eassert (it->s == NULL);
6383
6384 /* POS must be a reasonable value. */
6385 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6386
6387 it->current.pos = it->position = pos;
6388 it->end_charpos = ZV;
6389 it->dpvec = NULL;
6390 it->current.dpvec_index = -1;
6391 it->current.overlay_string_index = -1;
6392 IT_STRING_CHARPOS (*it) = -1;
6393 IT_STRING_BYTEPOS (*it) = -1;
6394 it->string = Qnil;
6395 it->method = GET_FROM_BUFFER;
6396 it->object = it->w->contents;
6397 it->area = TEXT_AREA;
6398 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6399 it->sp = 0;
6400 it->string_from_display_prop_p = 0;
6401 it->string_from_prefix_prop_p = 0;
6402
6403 it->from_disp_prop_p = 0;
6404 it->face_before_selective_p = 0;
6405 if (it->bidi_p)
6406 {
6407 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6408 &it->bidi_it);
6409 bidi_unshelve_cache (NULL, 0);
6410 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6411 it->bidi_it.string.s = NULL;
6412 it->bidi_it.string.lstring = Qnil;
6413 it->bidi_it.string.bufpos = 0;
6414 it->bidi_it.string.unibyte = 0;
6415 it->bidi_it.w = it->w;
6416 }
6417
6418 if (set_stop_p)
6419 {
6420 it->stop_charpos = CHARPOS (pos);
6421 it->base_level_stop = CHARPOS (pos);
6422 }
6423 /* This make the information stored in it->cmp_it invalidate. */
6424 it->cmp_it.id = -1;
6425 }
6426
6427
6428 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6429 If S is non-null, it is a C string to iterate over. Otherwise,
6430 STRING gives a Lisp string to iterate over.
6431
6432 If PRECISION > 0, don't return more then PRECISION number of
6433 characters from the string.
6434
6435 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6436 characters have been returned. FIELD_WIDTH < 0 means an infinite
6437 field width.
6438
6439 MULTIBYTE = 0 means disable processing of multibyte characters,
6440 MULTIBYTE > 0 means enable it,
6441 MULTIBYTE < 0 means use IT->multibyte_p.
6442
6443 IT must be initialized via a prior call to init_iterator before
6444 calling this function. */
6445
6446 static void
6447 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6448 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6449 int multibyte)
6450 {
6451 /* No region in strings. */
6452 it->region_beg_charpos = it->region_end_charpos = -1;
6453
6454 /* No text property checks performed by default, but see below. */
6455 it->stop_charpos = -1;
6456
6457 /* Set iterator position and end position. */
6458 memset (&it->current, 0, sizeof it->current);
6459 it->current.overlay_string_index = -1;
6460 it->current.dpvec_index = -1;
6461 eassert (charpos >= 0);
6462
6463 /* If STRING is specified, use its multibyteness, otherwise use the
6464 setting of MULTIBYTE, if specified. */
6465 if (multibyte >= 0)
6466 it->multibyte_p = multibyte > 0;
6467
6468 /* Bidirectional reordering of strings is controlled by the default
6469 value of bidi-display-reordering. Don't try to reorder while
6470 loading loadup.el, as the necessary character property tables are
6471 not yet available. */
6472 it->bidi_p =
6473 NILP (Vpurify_flag)
6474 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6475
6476 if (s == NULL)
6477 {
6478 eassert (STRINGP (string));
6479 it->string = string;
6480 it->s = NULL;
6481 it->end_charpos = it->string_nchars = SCHARS (string);
6482 it->method = GET_FROM_STRING;
6483 it->current.string_pos = string_pos (charpos, string);
6484
6485 if (it->bidi_p)
6486 {
6487 it->bidi_it.string.lstring = string;
6488 it->bidi_it.string.s = NULL;
6489 it->bidi_it.string.schars = it->end_charpos;
6490 it->bidi_it.string.bufpos = 0;
6491 it->bidi_it.string.from_disp_str = 0;
6492 it->bidi_it.string.unibyte = !it->multibyte_p;
6493 it->bidi_it.w = it->w;
6494 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6495 FRAME_WINDOW_P (it->f), &it->bidi_it);
6496 }
6497 }
6498 else
6499 {
6500 it->s = (const unsigned char *) s;
6501 it->string = Qnil;
6502
6503 /* Note that we use IT->current.pos, not it->current.string_pos,
6504 for displaying C strings. */
6505 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6506 if (it->multibyte_p)
6507 {
6508 it->current.pos = c_string_pos (charpos, s, 1);
6509 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6510 }
6511 else
6512 {
6513 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6514 it->end_charpos = it->string_nchars = strlen (s);
6515 }
6516
6517 if (it->bidi_p)
6518 {
6519 it->bidi_it.string.lstring = Qnil;
6520 it->bidi_it.string.s = (const unsigned char *) s;
6521 it->bidi_it.string.schars = it->end_charpos;
6522 it->bidi_it.string.bufpos = 0;
6523 it->bidi_it.string.from_disp_str = 0;
6524 it->bidi_it.string.unibyte = !it->multibyte_p;
6525 it->bidi_it.w = it->w;
6526 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6527 &it->bidi_it);
6528 }
6529 it->method = GET_FROM_C_STRING;
6530 }
6531
6532 /* PRECISION > 0 means don't return more than PRECISION characters
6533 from the string. */
6534 if (precision > 0 && it->end_charpos - charpos > precision)
6535 {
6536 it->end_charpos = it->string_nchars = charpos + precision;
6537 if (it->bidi_p)
6538 it->bidi_it.string.schars = it->end_charpos;
6539 }
6540
6541 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6542 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6543 FIELD_WIDTH < 0 means infinite field width. This is useful for
6544 padding with `-' at the end of a mode line. */
6545 if (field_width < 0)
6546 field_width = INFINITY;
6547 /* Implementation note: We deliberately don't enlarge
6548 it->bidi_it.string.schars here to fit it->end_charpos, because
6549 the bidi iterator cannot produce characters out of thin air. */
6550 if (field_width > it->end_charpos - charpos)
6551 it->end_charpos = charpos + field_width;
6552
6553 /* Use the standard display table for displaying strings. */
6554 if (DISP_TABLE_P (Vstandard_display_table))
6555 it->dp = XCHAR_TABLE (Vstandard_display_table);
6556
6557 it->stop_charpos = charpos;
6558 it->prev_stop = charpos;
6559 it->base_level_stop = 0;
6560 if (it->bidi_p)
6561 {
6562 it->bidi_it.first_elt = 1;
6563 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6564 it->bidi_it.disp_pos = -1;
6565 }
6566 if (s == NULL && it->multibyte_p)
6567 {
6568 ptrdiff_t endpos = SCHARS (it->string);
6569 if (endpos > it->end_charpos)
6570 endpos = it->end_charpos;
6571 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6572 it->string);
6573 }
6574 CHECK_IT (it);
6575 }
6576
6577
6578 \f
6579 /***********************************************************************
6580 Iteration
6581 ***********************************************************************/
6582
6583 /* Map enum it_method value to corresponding next_element_from_* function. */
6584
6585 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6586 {
6587 next_element_from_buffer,
6588 next_element_from_display_vector,
6589 next_element_from_string,
6590 next_element_from_c_string,
6591 next_element_from_image,
6592 next_element_from_stretch
6593 };
6594
6595 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6596
6597
6598 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6599 (possibly with the following characters). */
6600
6601 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6602 ((IT)->cmp_it.id >= 0 \
6603 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6604 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6605 END_CHARPOS, (IT)->w, \
6606 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6607 (IT)->string)))
6608
6609
6610 /* Lookup the char-table Vglyphless_char_display for character C (-1
6611 if we want information for no-font case), and return the display
6612 method symbol. By side-effect, update it->what and
6613 it->glyphless_method. This function is called from
6614 get_next_display_element for each character element, and from
6615 x_produce_glyphs when no suitable font was found. */
6616
6617 Lisp_Object
6618 lookup_glyphless_char_display (int c, struct it *it)
6619 {
6620 Lisp_Object glyphless_method = Qnil;
6621
6622 if (CHAR_TABLE_P (Vglyphless_char_display)
6623 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6624 {
6625 if (c >= 0)
6626 {
6627 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6628 if (CONSP (glyphless_method))
6629 glyphless_method = FRAME_WINDOW_P (it->f)
6630 ? XCAR (glyphless_method)
6631 : XCDR (glyphless_method);
6632 }
6633 else
6634 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6635 }
6636
6637 retry:
6638 if (NILP (glyphless_method))
6639 {
6640 if (c >= 0)
6641 /* The default is to display the character by a proper font. */
6642 return Qnil;
6643 /* The default for the no-font case is to display an empty box. */
6644 glyphless_method = Qempty_box;
6645 }
6646 if (EQ (glyphless_method, Qzero_width))
6647 {
6648 if (c >= 0)
6649 return glyphless_method;
6650 /* This method can't be used for the no-font case. */
6651 glyphless_method = Qempty_box;
6652 }
6653 if (EQ (glyphless_method, Qthin_space))
6654 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6655 else if (EQ (glyphless_method, Qempty_box))
6656 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6657 else if (EQ (glyphless_method, Qhex_code))
6658 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6659 else if (STRINGP (glyphless_method))
6660 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6661 else
6662 {
6663 /* Invalid value. We use the default method. */
6664 glyphless_method = Qnil;
6665 goto retry;
6666 }
6667 it->what = IT_GLYPHLESS;
6668 return glyphless_method;
6669 }
6670
6671 /* Load IT's display element fields with information about the next
6672 display element from the current position of IT. Value is zero if
6673 end of buffer (or C string) is reached. */
6674
6675 static struct frame *last_escape_glyph_frame = NULL;
6676 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6677 static int last_escape_glyph_merged_face_id = 0;
6678
6679 struct frame *last_glyphless_glyph_frame = NULL;
6680 int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6681 int last_glyphless_glyph_merged_face_id = 0;
6682
6683 static int
6684 get_next_display_element (struct it *it)
6685 {
6686 /* Non-zero means that we found a display element. Zero means that
6687 we hit the end of what we iterate over. Performance note: the
6688 function pointer `method' used here turns out to be faster than
6689 using a sequence of if-statements. */
6690 int success_p;
6691
6692 get_next:
6693 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6694
6695 if (it->what == IT_CHARACTER)
6696 {
6697 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6698 and only if (a) the resolved directionality of that character
6699 is R..." */
6700 /* FIXME: Do we need an exception for characters from display
6701 tables? */
6702 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6703 it->c = bidi_mirror_char (it->c);
6704 /* Map via display table or translate control characters.
6705 IT->c, IT->len etc. have been set to the next character by
6706 the function call above. If we have a display table, and it
6707 contains an entry for IT->c, translate it. Don't do this if
6708 IT->c itself comes from a display table, otherwise we could
6709 end up in an infinite recursion. (An alternative could be to
6710 count the recursion depth of this function and signal an
6711 error when a certain maximum depth is reached.) Is it worth
6712 it? */
6713 if (success_p && it->dpvec == NULL)
6714 {
6715 Lisp_Object dv;
6716 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6717 int nonascii_space_p = 0;
6718 int nonascii_hyphen_p = 0;
6719 int c = it->c; /* This is the character to display. */
6720
6721 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6722 {
6723 eassert (SINGLE_BYTE_CHAR_P (c));
6724 if (unibyte_display_via_language_environment)
6725 {
6726 c = DECODE_CHAR (unibyte, c);
6727 if (c < 0)
6728 c = BYTE8_TO_CHAR (it->c);
6729 }
6730 else
6731 c = BYTE8_TO_CHAR (it->c);
6732 }
6733
6734 if (it->dp
6735 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6736 VECTORP (dv)))
6737 {
6738 struct Lisp_Vector *v = XVECTOR (dv);
6739
6740 /* Return the first character from the display table
6741 entry, if not empty. If empty, don't display the
6742 current character. */
6743 if (v->header.size)
6744 {
6745 it->dpvec_char_len = it->len;
6746 it->dpvec = v->contents;
6747 it->dpend = v->contents + v->header.size;
6748 it->current.dpvec_index = 0;
6749 it->dpvec_face_id = -1;
6750 it->saved_face_id = it->face_id;
6751 it->method = GET_FROM_DISPLAY_VECTOR;
6752 it->ellipsis_p = 0;
6753 }
6754 else
6755 {
6756 set_iterator_to_next (it, 0);
6757 }
6758 goto get_next;
6759 }
6760
6761 if (! NILP (lookup_glyphless_char_display (c, it)))
6762 {
6763 if (it->what == IT_GLYPHLESS)
6764 goto done;
6765 /* Don't display this character. */
6766 set_iterator_to_next (it, 0);
6767 goto get_next;
6768 }
6769
6770 /* If `nobreak-char-display' is non-nil, we display
6771 non-ASCII spaces and hyphens specially. */
6772 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6773 {
6774 if (c == 0xA0)
6775 nonascii_space_p = 1;
6776 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6777 nonascii_hyphen_p = 1;
6778 }
6779
6780 /* Translate control characters into `\003' or `^C' form.
6781 Control characters coming from a display table entry are
6782 currently not translated because we use IT->dpvec to hold
6783 the translation. This could easily be changed but I
6784 don't believe that it is worth doing.
6785
6786 The characters handled by `nobreak-char-display' must be
6787 translated too.
6788
6789 Non-printable characters and raw-byte characters are also
6790 translated to octal form. */
6791 if (((c < ' ' || c == 127) /* ASCII control chars */
6792 ? (it->area != TEXT_AREA
6793 /* In mode line, treat \n, \t like other crl chars. */
6794 || (c != '\t'
6795 && it->glyph_row
6796 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6797 || (c != '\n' && c != '\t'))
6798 : (nonascii_space_p
6799 || nonascii_hyphen_p
6800 || CHAR_BYTE8_P (c)
6801 || ! CHAR_PRINTABLE_P (c))))
6802 {
6803 /* C is a control character, non-ASCII space/hyphen,
6804 raw-byte, or a non-printable character which must be
6805 displayed either as '\003' or as `^C' where the '\\'
6806 and '^' can be defined in the display table. Fill
6807 IT->ctl_chars with glyphs for what we have to
6808 display. Then, set IT->dpvec to these glyphs. */
6809 Lisp_Object gc;
6810 int ctl_len;
6811 int face_id;
6812 int lface_id = 0;
6813 int escape_glyph;
6814
6815 /* Handle control characters with ^. */
6816
6817 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6818 {
6819 int g;
6820
6821 g = '^'; /* default glyph for Control */
6822 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6823 if (it->dp
6824 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6825 {
6826 g = GLYPH_CODE_CHAR (gc);
6827 lface_id = GLYPH_CODE_FACE (gc);
6828 }
6829 if (lface_id)
6830 {
6831 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6832 }
6833 else if (it->f == last_escape_glyph_frame
6834 && it->face_id == last_escape_glyph_face_id)
6835 {
6836 face_id = last_escape_glyph_merged_face_id;
6837 }
6838 else
6839 {
6840 /* Merge the escape-glyph face into the current face. */
6841 face_id = merge_faces (it->f, Qescape_glyph, 0,
6842 it->face_id);
6843 last_escape_glyph_frame = it->f;
6844 last_escape_glyph_face_id = it->face_id;
6845 last_escape_glyph_merged_face_id = face_id;
6846 }
6847
6848 XSETINT (it->ctl_chars[0], g);
6849 XSETINT (it->ctl_chars[1], c ^ 0100);
6850 ctl_len = 2;
6851 goto display_control;
6852 }
6853
6854 /* Handle non-ascii space in the mode where it only gets
6855 highlighting. */
6856
6857 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6858 {
6859 /* Merge `nobreak-space' into the current face. */
6860 face_id = merge_faces (it->f, Qnobreak_space, 0,
6861 it->face_id);
6862 XSETINT (it->ctl_chars[0], ' ');
6863 ctl_len = 1;
6864 goto display_control;
6865 }
6866
6867 /* Handle sequences that start with the "escape glyph". */
6868
6869 /* the default escape glyph is \. */
6870 escape_glyph = '\\';
6871
6872 if (it->dp
6873 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6874 {
6875 escape_glyph = GLYPH_CODE_CHAR (gc);
6876 lface_id = GLYPH_CODE_FACE (gc);
6877 }
6878 if (lface_id)
6879 {
6880 /* The display table specified a face.
6881 Merge it into face_id and also into escape_glyph. */
6882 face_id = merge_faces (it->f, Qt, lface_id,
6883 it->face_id);
6884 }
6885 else if (it->f == last_escape_glyph_frame
6886 && it->face_id == last_escape_glyph_face_id)
6887 {
6888 face_id = last_escape_glyph_merged_face_id;
6889 }
6890 else
6891 {
6892 /* Merge the escape-glyph face into the current face. */
6893 face_id = merge_faces (it->f, Qescape_glyph, 0,
6894 it->face_id);
6895 last_escape_glyph_frame = it->f;
6896 last_escape_glyph_face_id = it->face_id;
6897 last_escape_glyph_merged_face_id = face_id;
6898 }
6899
6900 /* Draw non-ASCII hyphen with just highlighting: */
6901
6902 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6903 {
6904 XSETINT (it->ctl_chars[0], '-');
6905 ctl_len = 1;
6906 goto display_control;
6907 }
6908
6909 /* Draw non-ASCII space/hyphen with escape glyph: */
6910
6911 if (nonascii_space_p || nonascii_hyphen_p)
6912 {
6913 XSETINT (it->ctl_chars[0], escape_glyph);
6914 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6915 ctl_len = 2;
6916 goto display_control;
6917 }
6918
6919 {
6920 char str[10];
6921 int len, i;
6922
6923 if (CHAR_BYTE8_P (c))
6924 /* Display \200 instead of \17777600. */
6925 c = CHAR_TO_BYTE8 (c);
6926 len = sprintf (str, "%03o", c);
6927
6928 XSETINT (it->ctl_chars[0], escape_glyph);
6929 for (i = 0; i < len; i++)
6930 XSETINT (it->ctl_chars[i + 1], str[i]);
6931 ctl_len = len + 1;
6932 }
6933
6934 display_control:
6935 /* Set up IT->dpvec and return first character from it. */
6936 it->dpvec_char_len = it->len;
6937 it->dpvec = it->ctl_chars;
6938 it->dpend = it->dpvec + ctl_len;
6939 it->current.dpvec_index = 0;
6940 it->dpvec_face_id = face_id;
6941 it->saved_face_id = it->face_id;
6942 it->method = GET_FROM_DISPLAY_VECTOR;
6943 it->ellipsis_p = 0;
6944 goto get_next;
6945 }
6946 it->char_to_display = c;
6947 }
6948 else if (success_p)
6949 {
6950 it->char_to_display = it->c;
6951 }
6952 }
6953
6954 /* Adjust face id for a multibyte character. There are no multibyte
6955 character in unibyte text. */
6956 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6957 && it->multibyte_p
6958 && success_p
6959 && FRAME_WINDOW_P (it->f))
6960 {
6961 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6962
6963 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6964 {
6965 /* Automatic composition with glyph-string. */
6966 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6967
6968 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6969 }
6970 else
6971 {
6972 ptrdiff_t pos = (it->s ? -1
6973 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6974 : IT_CHARPOS (*it));
6975 int c;
6976
6977 if (it->what == IT_CHARACTER)
6978 c = it->char_to_display;
6979 else
6980 {
6981 struct composition *cmp = composition_table[it->cmp_it.id];
6982 int i;
6983
6984 c = ' ';
6985 for (i = 0; i < cmp->glyph_len; i++)
6986 /* TAB in a composition means display glyphs with
6987 padding space on the left or right. */
6988 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6989 break;
6990 }
6991 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6992 }
6993 }
6994
6995 done:
6996 /* Is this character the last one of a run of characters with
6997 box? If yes, set IT->end_of_box_run_p to 1. */
6998 if (it->face_box_p
6999 && it->s == NULL)
7000 {
7001 if (it->method == GET_FROM_STRING && it->sp)
7002 {
7003 int face_id = underlying_face_id (it);
7004 struct face *face = FACE_FROM_ID (it->f, face_id);
7005
7006 if (face)
7007 {
7008 if (face->box == FACE_NO_BOX)
7009 {
7010 /* If the box comes from face properties in a
7011 display string, check faces in that string. */
7012 int string_face_id = face_after_it_pos (it);
7013 it->end_of_box_run_p
7014 = (FACE_FROM_ID (it->f, string_face_id)->box
7015 == FACE_NO_BOX);
7016 }
7017 /* Otherwise, the box comes from the underlying face.
7018 If this is the last string character displayed, check
7019 the next buffer location. */
7020 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7021 && (it->current.overlay_string_index
7022 == it->n_overlay_strings - 1))
7023 {
7024 ptrdiff_t ignore;
7025 int next_face_id;
7026 struct text_pos pos = it->current.pos;
7027 INC_TEXT_POS (pos, it->multibyte_p);
7028
7029 next_face_id = face_at_buffer_position
7030 (it->w, CHARPOS (pos), it->region_beg_charpos,
7031 it->region_end_charpos, &ignore,
7032 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
7033 -1);
7034 it->end_of_box_run_p
7035 = (FACE_FROM_ID (it->f, next_face_id)->box
7036 == FACE_NO_BOX);
7037 }
7038 }
7039 }
7040 else
7041 {
7042 int face_id = face_after_it_pos (it);
7043 it->end_of_box_run_p
7044 = (face_id != it->face_id
7045 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7046 }
7047 }
7048 /* If we reached the end of the object we've been iterating (e.g., a
7049 display string or an overlay string), and there's something on
7050 IT->stack, proceed with what's on the stack. It doesn't make
7051 sense to return zero if there's unprocessed stuff on the stack,
7052 because otherwise that stuff will never be displayed. */
7053 if (!success_p && it->sp > 0)
7054 {
7055 set_iterator_to_next (it, 0);
7056 success_p = get_next_display_element (it);
7057 }
7058
7059 /* Value is 0 if end of buffer or string reached. */
7060 return success_p;
7061 }
7062
7063
7064 /* Move IT to the next display element.
7065
7066 RESEAT_P non-zero means if called on a newline in buffer text,
7067 skip to the next visible line start.
7068
7069 Functions get_next_display_element and set_iterator_to_next are
7070 separate because I find this arrangement easier to handle than a
7071 get_next_display_element function that also increments IT's
7072 position. The way it is we can first look at an iterator's current
7073 display element, decide whether it fits on a line, and if it does,
7074 increment the iterator position. The other way around we probably
7075 would either need a flag indicating whether the iterator has to be
7076 incremented the next time, or we would have to implement a
7077 decrement position function which would not be easy to write. */
7078
7079 void
7080 set_iterator_to_next (struct it *it, int reseat_p)
7081 {
7082 /* Reset flags indicating start and end of a sequence of characters
7083 with box. Reset them at the start of this function because
7084 moving the iterator to a new position might set them. */
7085 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7086
7087 switch (it->method)
7088 {
7089 case GET_FROM_BUFFER:
7090 /* The current display element of IT is a character from
7091 current_buffer. Advance in the buffer, and maybe skip over
7092 invisible lines that are so because of selective display. */
7093 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7094 reseat_at_next_visible_line_start (it, 0);
7095 else if (it->cmp_it.id >= 0)
7096 {
7097 /* We are currently getting glyphs from a composition. */
7098 int i;
7099
7100 if (! it->bidi_p)
7101 {
7102 IT_CHARPOS (*it) += it->cmp_it.nchars;
7103 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7104 if (it->cmp_it.to < it->cmp_it.nglyphs)
7105 {
7106 it->cmp_it.from = it->cmp_it.to;
7107 }
7108 else
7109 {
7110 it->cmp_it.id = -1;
7111 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7112 IT_BYTEPOS (*it),
7113 it->end_charpos, Qnil);
7114 }
7115 }
7116 else if (! it->cmp_it.reversed_p)
7117 {
7118 /* Composition created while scanning forward. */
7119 /* Update IT's char/byte positions to point to the first
7120 character of the next grapheme cluster, or to the
7121 character visually after the current composition. */
7122 for (i = 0; i < it->cmp_it.nchars; i++)
7123 bidi_move_to_visually_next (&it->bidi_it);
7124 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7125 IT_CHARPOS (*it) = it->bidi_it.charpos;
7126
7127 if (it->cmp_it.to < it->cmp_it.nglyphs)
7128 {
7129 /* Proceed to the next grapheme cluster. */
7130 it->cmp_it.from = it->cmp_it.to;
7131 }
7132 else
7133 {
7134 /* No more grapheme clusters in this composition.
7135 Find the next stop position. */
7136 ptrdiff_t stop = it->end_charpos;
7137 if (it->bidi_it.scan_dir < 0)
7138 /* Now we are scanning backward and don't know
7139 where to stop. */
7140 stop = -1;
7141 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7142 IT_BYTEPOS (*it), stop, Qnil);
7143 }
7144 }
7145 else
7146 {
7147 /* Composition created while scanning backward. */
7148 /* Update IT's char/byte positions to point to the last
7149 character of the previous grapheme cluster, or the
7150 character visually after the current composition. */
7151 for (i = 0; i < it->cmp_it.nchars; i++)
7152 bidi_move_to_visually_next (&it->bidi_it);
7153 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7154 IT_CHARPOS (*it) = it->bidi_it.charpos;
7155 if (it->cmp_it.from > 0)
7156 {
7157 /* Proceed to the previous grapheme cluster. */
7158 it->cmp_it.to = it->cmp_it.from;
7159 }
7160 else
7161 {
7162 /* No more grapheme clusters in this composition.
7163 Find the next stop position. */
7164 ptrdiff_t stop = it->end_charpos;
7165 if (it->bidi_it.scan_dir < 0)
7166 /* Now we are scanning backward and don't know
7167 where to stop. */
7168 stop = -1;
7169 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7170 IT_BYTEPOS (*it), stop, Qnil);
7171 }
7172 }
7173 }
7174 else
7175 {
7176 eassert (it->len != 0);
7177
7178 if (!it->bidi_p)
7179 {
7180 IT_BYTEPOS (*it) += it->len;
7181 IT_CHARPOS (*it) += 1;
7182 }
7183 else
7184 {
7185 int prev_scan_dir = it->bidi_it.scan_dir;
7186 /* If this is a new paragraph, determine its base
7187 direction (a.k.a. its base embedding level). */
7188 if (it->bidi_it.new_paragraph)
7189 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7190 bidi_move_to_visually_next (&it->bidi_it);
7191 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7192 IT_CHARPOS (*it) = it->bidi_it.charpos;
7193 if (prev_scan_dir != it->bidi_it.scan_dir)
7194 {
7195 /* As the scan direction was changed, we must
7196 re-compute the stop position for composition. */
7197 ptrdiff_t stop = it->end_charpos;
7198 if (it->bidi_it.scan_dir < 0)
7199 stop = -1;
7200 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7201 IT_BYTEPOS (*it), stop, Qnil);
7202 }
7203 }
7204 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7205 }
7206 break;
7207
7208 case GET_FROM_C_STRING:
7209 /* Current display element of IT is from a C string. */
7210 if (!it->bidi_p
7211 /* If the string position is beyond string's end, it means
7212 next_element_from_c_string is padding the string with
7213 blanks, in which case we bypass the bidi iterator,
7214 because it cannot deal with such virtual characters. */
7215 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7216 {
7217 IT_BYTEPOS (*it) += it->len;
7218 IT_CHARPOS (*it) += 1;
7219 }
7220 else
7221 {
7222 bidi_move_to_visually_next (&it->bidi_it);
7223 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7224 IT_CHARPOS (*it) = it->bidi_it.charpos;
7225 }
7226 break;
7227
7228 case GET_FROM_DISPLAY_VECTOR:
7229 /* Current display element of IT is from a display table entry.
7230 Advance in the display table definition. Reset it to null if
7231 end reached, and continue with characters from buffers/
7232 strings. */
7233 ++it->current.dpvec_index;
7234
7235 /* Restore face of the iterator to what they were before the
7236 display vector entry (these entries may contain faces). */
7237 it->face_id = it->saved_face_id;
7238
7239 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7240 {
7241 int recheck_faces = it->ellipsis_p;
7242
7243 if (it->s)
7244 it->method = GET_FROM_C_STRING;
7245 else if (STRINGP (it->string))
7246 it->method = GET_FROM_STRING;
7247 else
7248 {
7249 it->method = GET_FROM_BUFFER;
7250 it->object = it->w->contents;
7251 }
7252
7253 it->dpvec = NULL;
7254 it->current.dpvec_index = -1;
7255
7256 /* Skip over characters which were displayed via IT->dpvec. */
7257 if (it->dpvec_char_len < 0)
7258 reseat_at_next_visible_line_start (it, 1);
7259 else if (it->dpvec_char_len > 0)
7260 {
7261 if (it->method == GET_FROM_STRING
7262 && it->current.overlay_string_index >= 0
7263 && it->n_overlay_strings > 0)
7264 it->ignore_overlay_strings_at_pos_p = 1;
7265 it->len = it->dpvec_char_len;
7266 set_iterator_to_next (it, reseat_p);
7267 }
7268
7269 /* Maybe recheck faces after display vector */
7270 if (recheck_faces)
7271 it->stop_charpos = IT_CHARPOS (*it);
7272 }
7273 break;
7274
7275 case GET_FROM_STRING:
7276 /* Current display element is a character from a Lisp string. */
7277 eassert (it->s == NULL && STRINGP (it->string));
7278 /* Don't advance past string end. These conditions are true
7279 when set_iterator_to_next is called at the end of
7280 get_next_display_element, in which case the Lisp string is
7281 already exhausted, and all we want is pop the iterator
7282 stack. */
7283 if (it->current.overlay_string_index >= 0)
7284 {
7285 /* This is an overlay string, so there's no padding with
7286 spaces, and the number of characters in the string is
7287 where the string ends. */
7288 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7289 goto consider_string_end;
7290 }
7291 else
7292 {
7293 /* Not an overlay string. There could be padding, so test
7294 against it->end_charpos . */
7295 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7296 goto consider_string_end;
7297 }
7298 if (it->cmp_it.id >= 0)
7299 {
7300 int i;
7301
7302 if (! it->bidi_p)
7303 {
7304 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7305 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7306 if (it->cmp_it.to < it->cmp_it.nglyphs)
7307 it->cmp_it.from = it->cmp_it.to;
7308 else
7309 {
7310 it->cmp_it.id = -1;
7311 composition_compute_stop_pos (&it->cmp_it,
7312 IT_STRING_CHARPOS (*it),
7313 IT_STRING_BYTEPOS (*it),
7314 it->end_charpos, it->string);
7315 }
7316 }
7317 else if (! it->cmp_it.reversed_p)
7318 {
7319 for (i = 0; i < it->cmp_it.nchars; i++)
7320 bidi_move_to_visually_next (&it->bidi_it);
7321 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7322 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7323
7324 if (it->cmp_it.to < it->cmp_it.nglyphs)
7325 it->cmp_it.from = it->cmp_it.to;
7326 else
7327 {
7328 ptrdiff_t stop = it->end_charpos;
7329 if (it->bidi_it.scan_dir < 0)
7330 stop = -1;
7331 composition_compute_stop_pos (&it->cmp_it,
7332 IT_STRING_CHARPOS (*it),
7333 IT_STRING_BYTEPOS (*it), stop,
7334 it->string);
7335 }
7336 }
7337 else
7338 {
7339 for (i = 0; i < it->cmp_it.nchars; i++)
7340 bidi_move_to_visually_next (&it->bidi_it);
7341 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7342 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7343 if (it->cmp_it.from > 0)
7344 it->cmp_it.to = it->cmp_it.from;
7345 else
7346 {
7347 ptrdiff_t stop = it->end_charpos;
7348 if (it->bidi_it.scan_dir < 0)
7349 stop = -1;
7350 composition_compute_stop_pos (&it->cmp_it,
7351 IT_STRING_CHARPOS (*it),
7352 IT_STRING_BYTEPOS (*it), stop,
7353 it->string);
7354 }
7355 }
7356 }
7357 else
7358 {
7359 if (!it->bidi_p
7360 /* If the string position is beyond string's end, it
7361 means next_element_from_string is padding the string
7362 with blanks, in which case we bypass the bidi
7363 iterator, because it cannot deal with such virtual
7364 characters. */
7365 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7366 {
7367 IT_STRING_BYTEPOS (*it) += it->len;
7368 IT_STRING_CHARPOS (*it) += 1;
7369 }
7370 else
7371 {
7372 int prev_scan_dir = it->bidi_it.scan_dir;
7373
7374 bidi_move_to_visually_next (&it->bidi_it);
7375 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7376 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7377 if (prev_scan_dir != it->bidi_it.scan_dir)
7378 {
7379 ptrdiff_t stop = it->end_charpos;
7380
7381 if (it->bidi_it.scan_dir < 0)
7382 stop = -1;
7383 composition_compute_stop_pos (&it->cmp_it,
7384 IT_STRING_CHARPOS (*it),
7385 IT_STRING_BYTEPOS (*it), stop,
7386 it->string);
7387 }
7388 }
7389 }
7390
7391 consider_string_end:
7392
7393 if (it->current.overlay_string_index >= 0)
7394 {
7395 /* IT->string is an overlay string. Advance to the
7396 next, if there is one. */
7397 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7398 {
7399 it->ellipsis_p = 0;
7400 next_overlay_string (it);
7401 if (it->ellipsis_p)
7402 setup_for_ellipsis (it, 0);
7403 }
7404 }
7405 else
7406 {
7407 /* IT->string is not an overlay string. If we reached
7408 its end, and there is something on IT->stack, proceed
7409 with what is on the stack. This can be either another
7410 string, this time an overlay string, or a buffer. */
7411 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7412 && it->sp > 0)
7413 {
7414 pop_it (it);
7415 if (it->method == GET_FROM_STRING)
7416 goto consider_string_end;
7417 }
7418 }
7419 break;
7420
7421 case GET_FROM_IMAGE:
7422 case GET_FROM_STRETCH:
7423 /* The position etc with which we have to proceed are on
7424 the stack. The position may be at the end of a string,
7425 if the `display' property takes up the whole string. */
7426 eassert (it->sp > 0);
7427 pop_it (it);
7428 if (it->method == GET_FROM_STRING)
7429 goto consider_string_end;
7430 break;
7431
7432 default:
7433 /* There are no other methods defined, so this should be a bug. */
7434 emacs_abort ();
7435 }
7436
7437 eassert (it->method != GET_FROM_STRING
7438 || (STRINGP (it->string)
7439 && IT_STRING_CHARPOS (*it) >= 0));
7440 }
7441
7442 /* Load IT's display element fields with information about the next
7443 display element which comes from a display table entry or from the
7444 result of translating a control character to one of the forms `^C'
7445 or `\003'.
7446
7447 IT->dpvec holds the glyphs to return as characters.
7448 IT->saved_face_id holds the face id before the display vector--it
7449 is restored into IT->face_id in set_iterator_to_next. */
7450
7451 static int
7452 next_element_from_display_vector (struct it *it)
7453 {
7454 Lisp_Object gc;
7455
7456 /* Precondition. */
7457 eassert (it->dpvec && it->current.dpvec_index >= 0);
7458
7459 it->face_id = it->saved_face_id;
7460
7461 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7462 That seemed totally bogus - so I changed it... */
7463 gc = it->dpvec[it->current.dpvec_index];
7464
7465 if (GLYPH_CODE_P (gc))
7466 {
7467 it->c = GLYPH_CODE_CHAR (gc);
7468 it->len = CHAR_BYTES (it->c);
7469
7470 /* The entry may contain a face id to use. Such a face id is
7471 the id of a Lisp face, not a realized face. A face id of
7472 zero means no face is specified. */
7473 if (it->dpvec_face_id >= 0)
7474 it->face_id = it->dpvec_face_id;
7475 else
7476 {
7477 int lface_id = GLYPH_CODE_FACE (gc);
7478 if (lface_id > 0)
7479 it->face_id = merge_faces (it->f, Qt, lface_id,
7480 it->saved_face_id);
7481 }
7482 }
7483 else
7484 /* Display table entry is invalid. Return a space. */
7485 it->c = ' ', it->len = 1;
7486
7487 /* Don't change position and object of the iterator here. They are
7488 still the values of the character that had this display table
7489 entry or was translated, and that's what we want. */
7490 it->what = IT_CHARACTER;
7491 return 1;
7492 }
7493
7494 /* Get the first element of string/buffer in the visual order, after
7495 being reseated to a new position in a string or a buffer. */
7496 static void
7497 get_visually_first_element (struct it *it)
7498 {
7499 int string_p = STRINGP (it->string) || it->s;
7500 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7501 ptrdiff_t bob = (string_p ? 0 : BEGV);
7502
7503 if (STRINGP (it->string))
7504 {
7505 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7506 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7507 }
7508 else
7509 {
7510 it->bidi_it.charpos = IT_CHARPOS (*it);
7511 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7512 }
7513
7514 if (it->bidi_it.charpos == eob)
7515 {
7516 /* Nothing to do, but reset the FIRST_ELT flag, like
7517 bidi_paragraph_init does, because we are not going to
7518 call it. */
7519 it->bidi_it.first_elt = 0;
7520 }
7521 else if (it->bidi_it.charpos == bob
7522 || (!string_p
7523 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7524 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7525 {
7526 /* If we are at the beginning of a line/string, we can produce
7527 the next element right away. */
7528 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7529 bidi_move_to_visually_next (&it->bidi_it);
7530 }
7531 else
7532 {
7533 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7534
7535 /* We need to prime the bidi iterator starting at the line's or
7536 string's beginning, before we will be able to produce the
7537 next element. */
7538 if (string_p)
7539 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7540 else
7541 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7542 IT_BYTEPOS (*it), -1,
7543 &it->bidi_it.bytepos);
7544 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7545 do
7546 {
7547 /* Now return to buffer/string position where we were asked
7548 to get the next display element, and produce that. */
7549 bidi_move_to_visually_next (&it->bidi_it);
7550 }
7551 while (it->bidi_it.bytepos != orig_bytepos
7552 && it->bidi_it.charpos < eob);
7553 }
7554
7555 /* Adjust IT's position information to where we ended up. */
7556 if (STRINGP (it->string))
7557 {
7558 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7559 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7560 }
7561 else
7562 {
7563 IT_CHARPOS (*it) = it->bidi_it.charpos;
7564 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7565 }
7566
7567 if (STRINGP (it->string) || !it->s)
7568 {
7569 ptrdiff_t stop, charpos, bytepos;
7570
7571 if (STRINGP (it->string))
7572 {
7573 eassert (!it->s);
7574 stop = SCHARS (it->string);
7575 if (stop > it->end_charpos)
7576 stop = it->end_charpos;
7577 charpos = IT_STRING_CHARPOS (*it);
7578 bytepos = IT_STRING_BYTEPOS (*it);
7579 }
7580 else
7581 {
7582 stop = it->end_charpos;
7583 charpos = IT_CHARPOS (*it);
7584 bytepos = IT_BYTEPOS (*it);
7585 }
7586 if (it->bidi_it.scan_dir < 0)
7587 stop = -1;
7588 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7589 it->string);
7590 }
7591 }
7592
7593 /* Load IT with the next display element from Lisp string IT->string.
7594 IT->current.string_pos is the current position within the string.
7595 If IT->current.overlay_string_index >= 0, the Lisp string is an
7596 overlay string. */
7597
7598 static int
7599 next_element_from_string (struct it *it)
7600 {
7601 struct text_pos position;
7602
7603 eassert (STRINGP (it->string));
7604 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7605 eassert (IT_STRING_CHARPOS (*it) >= 0);
7606 position = it->current.string_pos;
7607
7608 /* With bidi reordering, the character to display might not be the
7609 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7610 that we were reseat()ed to a new string, whose paragraph
7611 direction is not known. */
7612 if (it->bidi_p && it->bidi_it.first_elt)
7613 {
7614 get_visually_first_element (it);
7615 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7616 }
7617
7618 /* Time to check for invisible text? */
7619 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7620 {
7621 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7622 {
7623 if (!(!it->bidi_p
7624 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7625 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7626 {
7627 /* With bidi non-linear iteration, we could find
7628 ourselves far beyond the last computed stop_charpos,
7629 with several other stop positions in between that we
7630 missed. Scan them all now, in buffer's logical
7631 order, until we find and handle the last stop_charpos
7632 that precedes our current position. */
7633 handle_stop_backwards (it, it->stop_charpos);
7634 return GET_NEXT_DISPLAY_ELEMENT (it);
7635 }
7636 else
7637 {
7638 if (it->bidi_p)
7639 {
7640 /* Take note of the stop position we just moved
7641 across, for when we will move back across it. */
7642 it->prev_stop = it->stop_charpos;
7643 /* If we are at base paragraph embedding level, take
7644 note of the last stop position seen at this
7645 level. */
7646 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7647 it->base_level_stop = it->stop_charpos;
7648 }
7649 handle_stop (it);
7650
7651 /* Since a handler may have changed IT->method, we must
7652 recurse here. */
7653 return GET_NEXT_DISPLAY_ELEMENT (it);
7654 }
7655 }
7656 else if (it->bidi_p
7657 /* If we are before prev_stop, we may have overstepped
7658 on our way backwards a stop_pos, and if so, we need
7659 to handle that stop_pos. */
7660 && IT_STRING_CHARPOS (*it) < it->prev_stop
7661 /* We can sometimes back up for reasons that have nothing
7662 to do with bidi reordering. E.g., compositions. The
7663 code below is only needed when we are above the base
7664 embedding level, so test for that explicitly. */
7665 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7666 {
7667 /* If we lost track of base_level_stop, we have no better
7668 place for handle_stop_backwards to start from than string
7669 beginning. This happens, e.g., when we were reseated to
7670 the previous screenful of text by vertical-motion. */
7671 if (it->base_level_stop <= 0
7672 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7673 it->base_level_stop = 0;
7674 handle_stop_backwards (it, it->base_level_stop);
7675 return GET_NEXT_DISPLAY_ELEMENT (it);
7676 }
7677 }
7678
7679 if (it->current.overlay_string_index >= 0)
7680 {
7681 /* Get the next character from an overlay string. In overlay
7682 strings, there is no field width or padding with spaces to
7683 do. */
7684 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7685 {
7686 it->what = IT_EOB;
7687 return 0;
7688 }
7689 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7690 IT_STRING_BYTEPOS (*it),
7691 it->bidi_it.scan_dir < 0
7692 ? -1
7693 : SCHARS (it->string))
7694 && next_element_from_composition (it))
7695 {
7696 return 1;
7697 }
7698 else if (STRING_MULTIBYTE (it->string))
7699 {
7700 const unsigned char *s = (SDATA (it->string)
7701 + IT_STRING_BYTEPOS (*it));
7702 it->c = string_char_and_length (s, &it->len);
7703 }
7704 else
7705 {
7706 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7707 it->len = 1;
7708 }
7709 }
7710 else
7711 {
7712 /* Get the next character from a Lisp string that is not an
7713 overlay string. Such strings come from the mode line, for
7714 example. We may have to pad with spaces, or truncate the
7715 string. See also next_element_from_c_string. */
7716 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7717 {
7718 it->what = IT_EOB;
7719 return 0;
7720 }
7721 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7722 {
7723 /* Pad with spaces. */
7724 it->c = ' ', it->len = 1;
7725 CHARPOS (position) = BYTEPOS (position) = -1;
7726 }
7727 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7728 IT_STRING_BYTEPOS (*it),
7729 it->bidi_it.scan_dir < 0
7730 ? -1
7731 : it->string_nchars)
7732 && next_element_from_composition (it))
7733 {
7734 return 1;
7735 }
7736 else if (STRING_MULTIBYTE (it->string))
7737 {
7738 const unsigned char *s = (SDATA (it->string)
7739 + IT_STRING_BYTEPOS (*it));
7740 it->c = string_char_and_length (s, &it->len);
7741 }
7742 else
7743 {
7744 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7745 it->len = 1;
7746 }
7747 }
7748
7749 /* Record what we have and where it came from. */
7750 it->what = IT_CHARACTER;
7751 it->object = it->string;
7752 it->position = position;
7753 return 1;
7754 }
7755
7756
7757 /* Load IT with next display element from C string IT->s.
7758 IT->string_nchars is the maximum number of characters to return
7759 from the string. IT->end_charpos may be greater than
7760 IT->string_nchars when this function is called, in which case we
7761 may have to return padding spaces. Value is zero if end of string
7762 reached, including padding spaces. */
7763
7764 static int
7765 next_element_from_c_string (struct it *it)
7766 {
7767 int success_p = 1;
7768
7769 eassert (it->s);
7770 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7771 it->what = IT_CHARACTER;
7772 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7773 it->object = Qnil;
7774
7775 /* With bidi reordering, the character to display might not be the
7776 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7777 we were reseated to a new string, whose paragraph direction is
7778 not known. */
7779 if (it->bidi_p && it->bidi_it.first_elt)
7780 get_visually_first_element (it);
7781
7782 /* IT's position can be greater than IT->string_nchars in case a
7783 field width or precision has been specified when the iterator was
7784 initialized. */
7785 if (IT_CHARPOS (*it) >= it->end_charpos)
7786 {
7787 /* End of the game. */
7788 it->what = IT_EOB;
7789 success_p = 0;
7790 }
7791 else if (IT_CHARPOS (*it) >= it->string_nchars)
7792 {
7793 /* Pad with spaces. */
7794 it->c = ' ', it->len = 1;
7795 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7796 }
7797 else if (it->multibyte_p)
7798 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7799 else
7800 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7801
7802 return success_p;
7803 }
7804
7805
7806 /* Set up IT to return characters from an ellipsis, if appropriate.
7807 The definition of the ellipsis glyphs may come from a display table
7808 entry. This function fills IT with the first glyph from the
7809 ellipsis if an ellipsis is to be displayed. */
7810
7811 static int
7812 next_element_from_ellipsis (struct it *it)
7813 {
7814 if (it->selective_display_ellipsis_p)
7815 setup_for_ellipsis (it, it->len);
7816 else
7817 {
7818 /* The face at the current position may be different from the
7819 face we find after the invisible text. Remember what it
7820 was in IT->saved_face_id, and signal that it's there by
7821 setting face_before_selective_p. */
7822 it->saved_face_id = it->face_id;
7823 it->method = GET_FROM_BUFFER;
7824 it->object = it->w->contents;
7825 reseat_at_next_visible_line_start (it, 1);
7826 it->face_before_selective_p = 1;
7827 }
7828
7829 return GET_NEXT_DISPLAY_ELEMENT (it);
7830 }
7831
7832
7833 /* Deliver an image display element. The iterator IT is already
7834 filled with image information (done in handle_display_prop). Value
7835 is always 1. */
7836
7837
7838 static int
7839 next_element_from_image (struct it *it)
7840 {
7841 it->what = IT_IMAGE;
7842 it->ignore_overlay_strings_at_pos_p = 0;
7843 return 1;
7844 }
7845
7846
7847 /* Fill iterator IT with next display element from a stretch glyph
7848 property. IT->object is the value of the text property. Value is
7849 always 1. */
7850
7851 static int
7852 next_element_from_stretch (struct it *it)
7853 {
7854 it->what = IT_STRETCH;
7855 return 1;
7856 }
7857
7858 /* Scan backwards from IT's current position until we find a stop
7859 position, or until BEGV. This is called when we find ourself
7860 before both the last known prev_stop and base_level_stop while
7861 reordering bidirectional text. */
7862
7863 static void
7864 compute_stop_pos_backwards (struct it *it)
7865 {
7866 const int SCAN_BACK_LIMIT = 1000;
7867 struct text_pos pos;
7868 struct display_pos save_current = it->current;
7869 struct text_pos save_position = it->position;
7870 ptrdiff_t charpos = IT_CHARPOS (*it);
7871 ptrdiff_t where_we_are = charpos;
7872 ptrdiff_t save_stop_pos = it->stop_charpos;
7873 ptrdiff_t save_end_pos = it->end_charpos;
7874
7875 eassert (NILP (it->string) && !it->s);
7876 eassert (it->bidi_p);
7877 it->bidi_p = 0;
7878 do
7879 {
7880 it->end_charpos = min (charpos + 1, ZV);
7881 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7882 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7883 reseat_1 (it, pos, 0);
7884 compute_stop_pos (it);
7885 /* We must advance forward, right? */
7886 if (it->stop_charpos <= charpos)
7887 emacs_abort ();
7888 }
7889 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7890
7891 if (it->stop_charpos <= where_we_are)
7892 it->prev_stop = it->stop_charpos;
7893 else
7894 it->prev_stop = BEGV;
7895 it->bidi_p = 1;
7896 it->current = save_current;
7897 it->position = save_position;
7898 it->stop_charpos = save_stop_pos;
7899 it->end_charpos = save_end_pos;
7900 }
7901
7902 /* Scan forward from CHARPOS in the current buffer/string, until we
7903 find a stop position > current IT's position. Then handle the stop
7904 position before that. This is called when we bump into a stop
7905 position while reordering bidirectional text. CHARPOS should be
7906 the last previously processed stop_pos (or BEGV/0, if none were
7907 processed yet) whose position is less that IT's current
7908 position. */
7909
7910 static void
7911 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7912 {
7913 int bufp = !STRINGP (it->string);
7914 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7915 struct display_pos save_current = it->current;
7916 struct text_pos save_position = it->position;
7917 struct text_pos pos1;
7918 ptrdiff_t next_stop;
7919
7920 /* Scan in strict logical order. */
7921 eassert (it->bidi_p);
7922 it->bidi_p = 0;
7923 do
7924 {
7925 it->prev_stop = charpos;
7926 if (bufp)
7927 {
7928 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7929 reseat_1 (it, pos1, 0);
7930 }
7931 else
7932 it->current.string_pos = string_pos (charpos, it->string);
7933 compute_stop_pos (it);
7934 /* We must advance forward, right? */
7935 if (it->stop_charpos <= it->prev_stop)
7936 emacs_abort ();
7937 charpos = it->stop_charpos;
7938 }
7939 while (charpos <= where_we_are);
7940
7941 it->bidi_p = 1;
7942 it->current = save_current;
7943 it->position = save_position;
7944 next_stop = it->stop_charpos;
7945 it->stop_charpos = it->prev_stop;
7946 handle_stop (it);
7947 it->stop_charpos = next_stop;
7948 }
7949
7950 /* Load IT with the next display element from current_buffer. Value
7951 is zero if end of buffer reached. IT->stop_charpos is the next
7952 position at which to stop and check for text properties or buffer
7953 end. */
7954
7955 static int
7956 next_element_from_buffer (struct it *it)
7957 {
7958 int success_p = 1;
7959
7960 eassert (IT_CHARPOS (*it) >= BEGV);
7961 eassert (NILP (it->string) && !it->s);
7962 eassert (!it->bidi_p
7963 || (EQ (it->bidi_it.string.lstring, Qnil)
7964 && it->bidi_it.string.s == NULL));
7965
7966 /* With bidi reordering, the character to display might not be the
7967 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7968 we were reseat()ed to a new buffer position, which is potentially
7969 a different paragraph. */
7970 if (it->bidi_p && it->bidi_it.first_elt)
7971 {
7972 get_visually_first_element (it);
7973 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7974 }
7975
7976 if (IT_CHARPOS (*it) >= it->stop_charpos)
7977 {
7978 if (IT_CHARPOS (*it) >= it->end_charpos)
7979 {
7980 int overlay_strings_follow_p;
7981
7982 /* End of the game, except when overlay strings follow that
7983 haven't been returned yet. */
7984 if (it->overlay_strings_at_end_processed_p)
7985 overlay_strings_follow_p = 0;
7986 else
7987 {
7988 it->overlay_strings_at_end_processed_p = 1;
7989 overlay_strings_follow_p = get_overlay_strings (it, 0);
7990 }
7991
7992 if (overlay_strings_follow_p)
7993 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7994 else
7995 {
7996 it->what = IT_EOB;
7997 it->position = it->current.pos;
7998 success_p = 0;
7999 }
8000 }
8001 else if (!(!it->bidi_p
8002 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8003 || IT_CHARPOS (*it) == it->stop_charpos))
8004 {
8005 /* With bidi non-linear iteration, we could find ourselves
8006 far beyond the last computed stop_charpos, with several
8007 other stop positions in between that we missed. Scan
8008 them all now, in buffer's logical order, until we find
8009 and handle the last stop_charpos that precedes our
8010 current position. */
8011 handle_stop_backwards (it, it->stop_charpos);
8012 return GET_NEXT_DISPLAY_ELEMENT (it);
8013 }
8014 else
8015 {
8016 if (it->bidi_p)
8017 {
8018 /* Take note of the stop position we just moved across,
8019 for when we will move back across it. */
8020 it->prev_stop = it->stop_charpos;
8021 /* If we are at base paragraph embedding level, take
8022 note of the last stop position seen at this
8023 level. */
8024 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8025 it->base_level_stop = it->stop_charpos;
8026 }
8027 handle_stop (it);
8028 return GET_NEXT_DISPLAY_ELEMENT (it);
8029 }
8030 }
8031 else if (it->bidi_p
8032 /* If we are before prev_stop, we may have overstepped on
8033 our way backwards a stop_pos, and if so, we need to
8034 handle that stop_pos. */
8035 && IT_CHARPOS (*it) < it->prev_stop
8036 /* We can sometimes back up for reasons that have nothing
8037 to do with bidi reordering. E.g., compositions. The
8038 code below is only needed when we are above the base
8039 embedding level, so test for that explicitly. */
8040 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8041 {
8042 if (it->base_level_stop <= 0
8043 || IT_CHARPOS (*it) < it->base_level_stop)
8044 {
8045 /* If we lost track of base_level_stop, we need to find
8046 prev_stop by looking backwards. This happens, e.g., when
8047 we were reseated to the previous screenful of text by
8048 vertical-motion. */
8049 it->base_level_stop = BEGV;
8050 compute_stop_pos_backwards (it);
8051 handle_stop_backwards (it, it->prev_stop);
8052 }
8053 else
8054 handle_stop_backwards (it, it->base_level_stop);
8055 return GET_NEXT_DISPLAY_ELEMENT (it);
8056 }
8057 else
8058 {
8059 /* No face changes, overlays etc. in sight, so just return a
8060 character from current_buffer. */
8061 unsigned char *p;
8062 ptrdiff_t stop;
8063
8064 /* Maybe run the redisplay end trigger hook. Performance note:
8065 This doesn't seem to cost measurable time. */
8066 if (it->redisplay_end_trigger_charpos
8067 && it->glyph_row
8068 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8069 run_redisplay_end_trigger_hook (it);
8070
8071 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8072 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8073 stop)
8074 && next_element_from_composition (it))
8075 {
8076 return 1;
8077 }
8078
8079 /* Get the next character, maybe multibyte. */
8080 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8081 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8082 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8083 else
8084 it->c = *p, it->len = 1;
8085
8086 /* Record what we have and where it came from. */
8087 it->what = IT_CHARACTER;
8088 it->object = it->w->contents;
8089 it->position = it->current.pos;
8090
8091 /* Normally we return the character found above, except when we
8092 really want to return an ellipsis for selective display. */
8093 if (it->selective)
8094 {
8095 if (it->c == '\n')
8096 {
8097 /* A value of selective > 0 means hide lines indented more
8098 than that number of columns. */
8099 if (it->selective > 0
8100 && IT_CHARPOS (*it) + 1 < ZV
8101 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8102 IT_BYTEPOS (*it) + 1,
8103 it->selective))
8104 {
8105 success_p = next_element_from_ellipsis (it);
8106 it->dpvec_char_len = -1;
8107 }
8108 }
8109 else if (it->c == '\r' && it->selective == -1)
8110 {
8111 /* A value of selective == -1 means that everything from the
8112 CR to the end of the line is invisible, with maybe an
8113 ellipsis displayed for it. */
8114 success_p = next_element_from_ellipsis (it);
8115 it->dpvec_char_len = -1;
8116 }
8117 }
8118 }
8119
8120 /* Value is zero if end of buffer reached. */
8121 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8122 return success_p;
8123 }
8124
8125
8126 /* Run the redisplay end trigger hook for IT. */
8127
8128 static void
8129 run_redisplay_end_trigger_hook (struct it *it)
8130 {
8131 Lisp_Object args[3];
8132
8133 /* IT->glyph_row should be non-null, i.e. we should be actually
8134 displaying something, or otherwise we should not run the hook. */
8135 eassert (it->glyph_row);
8136
8137 /* Set up hook arguments. */
8138 args[0] = Qredisplay_end_trigger_functions;
8139 args[1] = it->window;
8140 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8141 it->redisplay_end_trigger_charpos = 0;
8142
8143 /* Since we are *trying* to run these functions, don't try to run
8144 them again, even if they get an error. */
8145 wset_redisplay_end_trigger (it->w, Qnil);
8146 Frun_hook_with_args (3, args);
8147
8148 /* Notice if it changed the face of the character we are on. */
8149 handle_face_prop (it);
8150 }
8151
8152
8153 /* Deliver a composition display element. Unlike the other
8154 next_element_from_XXX, this function is not registered in the array
8155 get_next_element[]. It is called from next_element_from_buffer and
8156 next_element_from_string when necessary. */
8157
8158 static int
8159 next_element_from_composition (struct it *it)
8160 {
8161 it->what = IT_COMPOSITION;
8162 it->len = it->cmp_it.nbytes;
8163 if (STRINGP (it->string))
8164 {
8165 if (it->c < 0)
8166 {
8167 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8168 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8169 return 0;
8170 }
8171 it->position = it->current.string_pos;
8172 it->object = it->string;
8173 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8174 IT_STRING_BYTEPOS (*it), it->string);
8175 }
8176 else
8177 {
8178 if (it->c < 0)
8179 {
8180 IT_CHARPOS (*it) += it->cmp_it.nchars;
8181 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8182 if (it->bidi_p)
8183 {
8184 if (it->bidi_it.new_paragraph)
8185 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8186 /* Resync the bidi iterator with IT's new position.
8187 FIXME: this doesn't support bidirectional text. */
8188 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8189 bidi_move_to_visually_next (&it->bidi_it);
8190 }
8191 return 0;
8192 }
8193 it->position = it->current.pos;
8194 it->object = it->w->contents;
8195 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8196 IT_BYTEPOS (*it), Qnil);
8197 }
8198 return 1;
8199 }
8200
8201
8202 \f
8203 /***********************************************************************
8204 Moving an iterator without producing glyphs
8205 ***********************************************************************/
8206
8207 /* Check if iterator is at a position corresponding to a valid buffer
8208 position after some move_it_ call. */
8209
8210 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8211 ((it)->method == GET_FROM_STRING \
8212 ? IT_STRING_CHARPOS (*it) == 0 \
8213 : 1)
8214
8215
8216 /* Move iterator IT to a specified buffer or X position within one
8217 line on the display without producing glyphs.
8218
8219 OP should be a bit mask including some or all of these bits:
8220 MOVE_TO_X: Stop upon reaching x-position TO_X.
8221 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8222 Regardless of OP's value, stop upon reaching the end of the display line.
8223
8224 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8225 This means, in particular, that TO_X includes window's horizontal
8226 scroll amount.
8227
8228 The return value has several possible values that
8229 say what condition caused the scan to stop:
8230
8231 MOVE_POS_MATCH_OR_ZV
8232 - when TO_POS or ZV was reached.
8233
8234 MOVE_X_REACHED
8235 -when TO_X was reached before TO_POS or ZV were reached.
8236
8237 MOVE_LINE_CONTINUED
8238 - when we reached the end of the display area and the line must
8239 be continued.
8240
8241 MOVE_LINE_TRUNCATED
8242 - when we reached the end of the display area and the line is
8243 truncated.
8244
8245 MOVE_NEWLINE_OR_CR
8246 - when we stopped at a line end, i.e. a newline or a CR and selective
8247 display is on. */
8248
8249 static enum move_it_result
8250 move_it_in_display_line_to (struct it *it,
8251 ptrdiff_t to_charpos, int to_x,
8252 enum move_operation_enum op)
8253 {
8254 enum move_it_result result = MOVE_UNDEFINED;
8255 struct glyph_row *saved_glyph_row;
8256 struct it wrap_it, atpos_it, atx_it, ppos_it;
8257 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8258 void *ppos_data = NULL;
8259 int may_wrap = 0;
8260 enum it_method prev_method = it->method;
8261 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8262 int saw_smaller_pos = prev_pos < to_charpos;
8263
8264 /* Don't produce glyphs in produce_glyphs. */
8265 saved_glyph_row = it->glyph_row;
8266 it->glyph_row = NULL;
8267
8268 /* Use wrap_it to save a copy of IT wherever a word wrap could
8269 occur. Use atpos_it to save a copy of IT at the desired buffer
8270 position, if found, so that we can scan ahead and check if the
8271 word later overshoots the window edge. Use atx_it similarly, for
8272 pixel positions. */
8273 wrap_it.sp = -1;
8274 atpos_it.sp = -1;
8275 atx_it.sp = -1;
8276
8277 /* Use ppos_it under bidi reordering to save a copy of IT for the
8278 position > CHARPOS that is the closest to CHARPOS. We restore
8279 that position in IT when we have scanned the entire display line
8280 without finding a match for CHARPOS and all the character
8281 positions are greater than CHARPOS. */
8282 if (it->bidi_p)
8283 {
8284 SAVE_IT (ppos_it, *it, ppos_data);
8285 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8286 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8287 SAVE_IT (ppos_it, *it, ppos_data);
8288 }
8289
8290 #define BUFFER_POS_REACHED_P() \
8291 ((op & MOVE_TO_POS) != 0 \
8292 && BUFFERP (it->object) \
8293 && (IT_CHARPOS (*it) == to_charpos \
8294 || ((!it->bidi_p \
8295 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8296 && IT_CHARPOS (*it) > to_charpos) \
8297 || (it->what == IT_COMPOSITION \
8298 && ((IT_CHARPOS (*it) > to_charpos \
8299 && to_charpos >= it->cmp_it.charpos) \
8300 || (IT_CHARPOS (*it) < to_charpos \
8301 && to_charpos <= it->cmp_it.charpos)))) \
8302 && (it->method == GET_FROM_BUFFER \
8303 || (it->method == GET_FROM_DISPLAY_VECTOR \
8304 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8305
8306 /* If there's a line-/wrap-prefix, handle it. */
8307 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8308 && it->current_y < it->last_visible_y)
8309 handle_line_prefix (it);
8310
8311 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8312 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8313
8314 while (1)
8315 {
8316 int x, i, ascent = 0, descent = 0;
8317
8318 /* Utility macro to reset an iterator with x, ascent, and descent. */
8319 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8320 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8321 (IT)->max_descent = descent)
8322
8323 /* Stop if we move beyond TO_CHARPOS (after an image or a
8324 display string or stretch glyph). */
8325 if ((op & MOVE_TO_POS) != 0
8326 && BUFFERP (it->object)
8327 && it->method == GET_FROM_BUFFER
8328 && (((!it->bidi_p
8329 /* When the iterator is at base embedding level, we
8330 are guaranteed that characters are delivered for
8331 display in strictly increasing order of their
8332 buffer positions. */
8333 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8334 && IT_CHARPOS (*it) > to_charpos)
8335 || (it->bidi_p
8336 && (prev_method == GET_FROM_IMAGE
8337 || prev_method == GET_FROM_STRETCH
8338 || prev_method == GET_FROM_STRING)
8339 /* Passed TO_CHARPOS from left to right. */
8340 && ((prev_pos < to_charpos
8341 && IT_CHARPOS (*it) > to_charpos)
8342 /* Passed TO_CHARPOS from right to left. */
8343 || (prev_pos > to_charpos
8344 && IT_CHARPOS (*it) < to_charpos)))))
8345 {
8346 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8347 {
8348 result = MOVE_POS_MATCH_OR_ZV;
8349 break;
8350 }
8351 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8352 /* If wrap_it is valid, the current position might be in a
8353 word that is wrapped. So, save the iterator in
8354 atpos_it and continue to see if wrapping happens. */
8355 SAVE_IT (atpos_it, *it, atpos_data);
8356 }
8357
8358 /* Stop when ZV reached.
8359 We used to stop here when TO_CHARPOS reached as well, but that is
8360 too soon if this glyph does not fit on this line. So we handle it
8361 explicitly below. */
8362 if (!get_next_display_element (it))
8363 {
8364 result = MOVE_POS_MATCH_OR_ZV;
8365 break;
8366 }
8367
8368 if (it->line_wrap == TRUNCATE)
8369 {
8370 if (BUFFER_POS_REACHED_P ())
8371 {
8372 result = MOVE_POS_MATCH_OR_ZV;
8373 break;
8374 }
8375 }
8376 else
8377 {
8378 if (it->line_wrap == WORD_WRAP)
8379 {
8380 if (IT_DISPLAYING_WHITESPACE (it))
8381 may_wrap = 1;
8382 else if (may_wrap)
8383 {
8384 /* We have reached a glyph that follows one or more
8385 whitespace characters. If the position is
8386 already found, we are done. */
8387 if (atpos_it.sp >= 0)
8388 {
8389 RESTORE_IT (it, &atpos_it, atpos_data);
8390 result = MOVE_POS_MATCH_OR_ZV;
8391 goto done;
8392 }
8393 if (atx_it.sp >= 0)
8394 {
8395 RESTORE_IT (it, &atx_it, atx_data);
8396 result = MOVE_X_REACHED;
8397 goto done;
8398 }
8399 /* Otherwise, we can wrap here. */
8400 SAVE_IT (wrap_it, *it, wrap_data);
8401 may_wrap = 0;
8402 }
8403 }
8404 }
8405
8406 /* Remember the line height for the current line, in case
8407 the next element doesn't fit on the line. */
8408 ascent = it->max_ascent;
8409 descent = it->max_descent;
8410
8411 /* The call to produce_glyphs will get the metrics of the
8412 display element IT is loaded with. Record the x-position
8413 before this display element, in case it doesn't fit on the
8414 line. */
8415 x = it->current_x;
8416
8417 PRODUCE_GLYPHS (it);
8418
8419 if (it->area != TEXT_AREA)
8420 {
8421 prev_method = it->method;
8422 if (it->method == GET_FROM_BUFFER)
8423 prev_pos = IT_CHARPOS (*it);
8424 set_iterator_to_next (it, 1);
8425 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8426 SET_TEXT_POS (this_line_min_pos,
8427 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8428 if (it->bidi_p
8429 && (op & MOVE_TO_POS)
8430 && IT_CHARPOS (*it) > to_charpos
8431 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8432 SAVE_IT (ppos_it, *it, ppos_data);
8433 continue;
8434 }
8435
8436 /* The number of glyphs we get back in IT->nglyphs will normally
8437 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8438 character on a terminal frame, or (iii) a line end. For the
8439 second case, IT->nglyphs - 1 padding glyphs will be present.
8440 (On X frames, there is only one glyph produced for a
8441 composite character.)
8442
8443 The behavior implemented below means, for continuation lines,
8444 that as many spaces of a TAB as fit on the current line are
8445 displayed there. For terminal frames, as many glyphs of a
8446 multi-glyph character are displayed in the current line, too.
8447 This is what the old redisplay code did, and we keep it that
8448 way. Under X, the whole shape of a complex character must
8449 fit on the line or it will be completely displayed in the
8450 next line.
8451
8452 Note that both for tabs and padding glyphs, all glyphs have
8453 the same width. */
8454 if (it->nglyphs)
8455 {
8456 /* More than one glyph or glyph doesn't fit on line. All
8457 glyphs have the same width. */
8458 int single_glyph_width = it->pixel_width / it->nglyphs;
8459 int new_x;
8460 int x_before_this_char = x;
8461 int hpos_before_this_char = it->hpos;
8462
8463 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8464 {
8465 new_x = x + single_glyph_width;
8466
8467 /* We want to leave anything reaching TO_X to the caller. */
8468 if ((op & MOVE_TO_X) && new_x > to_x)
8469 {
8470 if (BUFFER_POS_REACHED_P ())
8471 {
8472 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8473 goto buffer_pos_reached;
8474 if (atpos_it.sp < 0)
8475 {
8476 SAVE_IT (atpos_it, *it, atpos_data);
8477 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8478 }
8479 }
8480 else
8481 {
8482 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8483 {
8484 it->current_x = x;
8485 result = MOVE_X_REACHED;
8486 break;
8487 }
8488 if (atx_it.sp < 0)
8489 {
8490 SAVE_IT (atx_it, *it, atx_data);
8491 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8492 }
8493 }
8494 }
8495
8496 if (/* Lines are continued. */
8497 it->line_wrap != TRUNCATE
8498 && (/* And glyph doesn't fit on the line. */
8499 new_x > it->last_visible_x
8500 /* Or it fits exactly and we're on a window
8501 system frame. */
8502 || (new_x == it->last_visible_x
8503 && FRAME_WINDOW_P (it->f)
8504 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8505 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8506 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8507 {
8508 if (/* IT->hpos == 0 means the very first glyph
8509 doesn't fit on the line, e.g. a wide image. */
8510 it->hpos == 0
8511 || (new_x == it->last_visible_x
8512 && FRAME_WINDOW_P (it->f)))
8513 {
8514 ++it->hpos;
8515 it->current_x = new_x;
8516
8517 /* The character's last glyph just barely fits
8518 in this row. */
8519 if (i == it->nglyphs - 1)
8520 {
8521 /* If this is the destination position,
8522 return a position *before* it in this row,
8523 now that we know it fits in this row. */
8524 if (BUFFER_POS_REACHED_P ())
8525 {
8526 if (it->line_wrap != WORD_WRAP
8527 || wrap_it.sp < 0)
8528 {
8529 it->hpos = hpos_before_this_char;
8530 it->current_x = x_before_this_char;
8531 result = MOVE_POS_MATCH_OR_ZV;
8532 break;
8533 }
8534 if (it->line_wrap == WORD_WRAP
8535 && atpos_it.sp < 0)
8536 {
8537 SAVE_IT (atpos_it, *it, atpos_data);
8538 atpos_it.current_x = x_before_this_char;
8539 atpos_it.hpos = hpos_before_this_char;
8540 }
8541 }
8542
8543 prev_method = it->method;
8544 if (it->method == GET_FROM_BUFFER)
8545 prev_pos = IT_CHARPOS (*it);
8546 set_iterator_to_next (it, 1);
8547 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8548 SET_TEXT_POS (this_line_min_pos,
8549 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8550 /* On graphical terminals, newlines may
8551 "overflow" into the fringe if
8552 overflow-newline-into-fringe is non-nil.
8553 On text terminals, and on graphical
8554 terminals with no right margin, newlines
8555 may overflow into the last glyph on the
8556 display line.*/
8557 if (!FRAME_WINDOW_P (it->f)
8558 || ((it->bidi_p
8559 && it->bidi_it.paragraph_dir == R2L)
8560 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8561 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8562 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8563 {
8564 if (!get_next_display_element (it))
8565 {
8566 result = MOVE_POS_MATCH_OR_ZV;
8567 break;
8568 }
8569 if (BUFFER_POS_REACHED_P ())
8570 {
8571 if (ITERATOR_AT_END_OF_LINE_P (it))
8572 result = MOVE_POS_MATCH_OR_ZV;
8573 else
8574 result = MOVE_LINE_CONTINUED;
8575 break;
8576 }
8577 if (ITERATOR_AT_END_OF_LINE_P (it)
8578 && (it->line_wrap != WORD_WRAP
8579 || wrap_it.sp < 0))
8580 {
8581 result = MOVE_NEWLINE_OR_CR;
8582 break;
8583 }
8584 }
8585 }
8586 }
8587 else
8588 IT_RESET_X_ASCENT_DESCENT (it);
8589
8590 if (wrap_it.sp >= 0)
8591 {
8592 RESTORE_IT (it, &wrap_it, wrap_data);
8593 atpos_it.sp = -1;
8594 atx_it.sp = -1;
8595 }
8596
8597 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8598 IT_CHARPOS (*it)));
8599 result = MOVE_LINE_CONTINUED;
8600 break;
8601 }
8602
8603 if (BUFFER_POS_REACHED_P ())
8604 {
8605 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8606 goto buffer_pos_reached;
8607 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8608 {
8609 SAVE_IT (atpos_it, *it, atpos_data);
8610 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8611 }
8612 }
8613
8614 if (new_x > it->first_visible_x)
8615 {
8616 /* Glyph is visible. Increment number of glyphs that
8617 would be displayed. */
8618 ++it->hpos;
8619 }
8620 }
8621
8622 if (result != MOVE_UNDEFINED)
8623 break;
8624 }
8625 else if (BUFFER_POS_REACHED_P ())
8626 {
8627 buffer_pos_reached:
8628 IT_RESET_X_ASCENT_DESCENT (it);
8629 result = MOVE_POS_MATCH_OR_ZV;
8630 break;
8631 }
8632 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8633 {
8634 /* Stop when TO_X specified and reached. This check is
8635 necessary here because of lines consisting of a line end,
8636 only. The line end will not produce any glyphs and we
8637 would never get MOVE_X_REACHED. */
8638 eassert (it->nglyphs == 0);
8639 result = MOVE_X_REACHED;
8640 break;
8641 }
8642
8643 /* Is this a line end? If yes, we're done. */
8644 if (ITERATOR_AT_END_OF_LINE_P (it))
8645 {
8646 /* If we are past TO_CHARPOS, but never saw any character
8647 positions smaller than TO_CHARPOS, return
8648 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8649 did. */
8650 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8651 {
8652 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8653 {
8654 if (IT_CHARPOS (ppos_it) < ZV)
8655 {
8656 RESTORE_IT (it, &ppos_it, ppos_data);
8657 result = MOVE_POS_MATCH_OR_ZV;
8658 }
8659 else
8660 goto buffer_pos_reached;
8661 }
8662 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8663 && IT_CHARPOS (*it) > to_charpos)
8664 goto buffer_pos_reached;
8665 else
8666 result = MOVE_NEWLINE_OR_CR;
8667 }
8668 else
8669 result = MOVE_NEWLINE_OR_CR;
8670 break;
8671 }
8672
8673 prev_method = it->method;
8674 if (it->method == GET_FROM_BUFFER)
8675 prev_pos = IT_CHARPOS (*it);
8676 /* The current display element has been consumed. Advance
8677 to the next. */
8678 set_iterator_to_next (it, 1);
8679 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8680 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8681 if (IT_CHARPOS (*it) < to_charpos)
8682 saw_smaller_pos = 1;
8683 if (it->bidi_p
8684 && (op & MOVE_TO_POS)
8685 && IT_CHARPOS (*it) >= to_charpos
8686 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8687 SAVE_IT (ppos_it, *it, ppos_data);
8688
8689 /* Stop if lines are truncated and IT's current x-position is
8690 past the right edge of the window now. */
8691 if (it->line_wrap == TRUNCATE
8692 && it->current_x >= it->last_visible_x)
8693 {
8694 if (!FRAME_WINDOW_P (it->f)
8695 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8696 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8697 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8698 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8699 {
8700 int at_eob_p = 0;
8701
8702 if ((at_eob_p = !get_next_display_element (it))
8703 || BUFFER_POS_REACHED_P ()
8704 /* If we are past TO_CHARPOS, but never saw any
8705 character positions smaller than TO_CHARPOS,
8706 return MOVE_POS_MATCH_OR_ZV, like the
8707 unidirectional display did. */
8708 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8709 && !saw_smaller_pos
8710 && IT_CHARPOS (*it) > to_charpos))
8711 {
8712 if (it->bidi_p
8713 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8714 RESTORE_IT (it, &ppos_it, ppos_data);
8715 result = MOVE_POS_MATCH_OR_ZV;
8716 break;
8717 }
8718 if (ITERATOR_AT_END_OF_LINE_P (it))
8719 {
8720 result = MOVE_NEWLINE_OR_CR;
8721 break;
8722 }
8723 }
8724 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8725 && !saw_smaller_pos
8726 && IT_CHARPOS (*it) > to_charpos)
8727 {
8728 if (IT_CHARPOS (ppos_it) < ZV)
8729 RESTORE_IT (it, &ppos_it, ppos_data);
8730 result = MOVE_POS_MATCH_OR_ZV;
8731 break;
8732 }
8733 result = MOVE_LINE_TRUNCATED;
8734 break;
8735 }
8736 #undef IT_RESET_X_ASCENT_DESCENT
8737 }
8738
8739 #undef BUFFER_POS_REACHED_P
8740
8741 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8742 restore the saved iterator. */
8743 if (atpos_it.sp >= 0)
8744 RESTORE_IT (it, &atpos_it, atpos_data);
8745 else if (atx_it.sp >= 0)
8746 RESTORE_IT (it, &atx_it, atx_data);
8747
8748 done:
8749
8750 if (atpos_data)
8751 bidi_unshelve_cache (atpos_data, 1);
8752 if (atx_data)
8753 bidi_unshelve_cache (atx_data, 1);
8754 if (wrap_data)
8755 bidi_unshelve_cache (wrap_data, 1);
8756 if (ppos_data)
8757 bidi_unshelve_cache (ppos_data, 1);
8758
8759 /* Restore the iterator settings altered at the beginning of this
8760 function. */
8761 it->glyph_row = saved_glyph_row;
8762 return result;
8763 }
8764
8765 /* For external use. */
8766 void
8767 move_it_in_display_line (struct it *it,
8768 ptrdiff_t to_charpos, int to_x,
8769 enum move_operation_enum op)
8770 {
8771 if (it->line_wrap == WORD_WRAP
8772 && (op & MOVE_TO_X))
8773 {
8774 struct it save_it;
8775 void *save_data = NULL;
8776 int skip;
8777
8778 SAVE_IT (save_it, *it, save_data);
8779 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8780 /* When word-wrap is on, TO_X may lie past the end
8781 of a wrapped line. Then it->current is the
8782 character on the next line, so backtrack to the
8783 space before the wrap point. */
8784 if (skip == MOVE_LINE_CONTINUED)
8785 {
8786 int prev_x = max (it->current_x - 1, 0);
8787 RESTORE_IT (it, &save_it, save_data);
8788 move_it_in_display_line_to
8789 (it, -1, prev_x, MOVE_TO_X);
8790 }
8791 else
8792 bidi_unshelve_cache (save_data, 1);
8793 }
8794 else
8795 move_it_in_display_line_to (it, to_charpos, to_x, op);
8796 }
8797
8798
8799 /* Move IT forward until it satisfies one or more of the criteria in
8800 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8801
8802 OP is a bit-mask that specifies where to stop, and in particular,
8803 which of those four position arguments makes a difference. See the
8804 description of enum move_operation_enum.
8805
8806 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8807 screen line, this function will set IT to the next position that is
8808 displayed to the right of TO_CHARPOS on the screen. */
8809
8810 void
8811 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8812 {
8813 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8814 int line_height, line_start_x = 0, reached = 0;
8815 void *backup_data = NULL;
8816
8817 for (;;)
8818 {
8819 if (op & MOVE_TO_VPOS)
8820 {
8821 /* If no TO_CHARPOS and no TO_X specified, stop at the
8822 start of the line TO_VPOS. */
8823 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8824 {
8825 if (it->vpos == to_vpos)
8826 {
8827 reached = 1;
8828 break;
8829 }
8830 else
8831 skip = move_it_in_display_line_to (it, -1, -1, 0);
8832 }
8833 else
8834 {
8835 /* TO_VPOS >= 0 means stop at TO_X in the line at
8836 TO_VPOS, or at TO_POS, whichever comes first. */
8837 if (it->vpos == to_vpos)
8838 {
8839 reached = 2;
8840 break;
8841 }
8842
8843 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8844
8845 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8846 {
8847 reached = 3;
8848 break;
8849 }
8850 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8851 {
8852 /* We have reached TO_X but not in the line we want. */
8853 skip = move_it_in_display_line_to (it, to_charpos,
8854 -1, MOVE_TO_POS);
8855 if (skip == MOVE_POS_MATCH_OR_ZV)
8856 {
8857 reached = 4;
8858 break;
8859 }
8860 }
8861 }
8862 }
8863 else if (op & MOVE_TO_Y)
8864 {
8865 struct it it_backup;
8866
8867 if (it->line_wrap == WORD_WRAP)
8868 SAVE_IT (it_backup, *it, backup_data);
8869
8870 /* TO_Y specified means stop at TO_X in the line containing
8871 TO_Y---or at TO_CHARPOS if this is reached first. The
8872 problem is that we can't really tell whether the line
8873 contains TO_Y before we have completely scanned it, and
8874 this may skip past TO_X. What we do is to first scan to
8875 TO_X.
8876
8877 If TO_X is not specified, use a TO_X of zero. The reason
8878 is to make the outcome of this function more predictable.
8879 If we didn't use TO_X == 0, we would stop at the end of
8880 the line which is probably not what a caller would expect
8881 to happen. */
8882 skip = move_it_in_display_line_to
8883 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8884 (MOVE_TO_X | (op & MOVE_TO_POS)));
8885
8886 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8887 if (skip == MOVE_POS_MATCH_OR_ZV)
8888 reached = 5;
8889 else if (skip == MOVE_X_REACHED)
8890 {
8891 /* If TO_X was reached, we want to know whether TO_Y is
8892 in the line. We know this is the case if the already
8893 scanned glyphs make the line tall enough. Otherwise,
8894 we must check by scanning the rest of the line. */
8895 line_height = it->max_ascent + it->max_descent;
8896 if (to_y >= it->current_y
8897 && to_y < it->current_y + line_height)
8898 {
8899 reached = 6;
8900 break;
8901 }
8902 SAVE_IT (it_backup, *it, backup_data);
8903 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8904 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8905 op & MOVE_TO_POS);
8906 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8907 line_height = it->max_ascent + it->max_descent;
8908 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8909
8910 if (to_y >= it->current_y
8911 && to_y < it->current_y + line_height)
8912 {
8913 /* If TO_Y is in this line and TO_X was reached
8914 above, we scanned too far. We have to restore
8915 IT's settings to the ones before skipping. But
8916 keep the more accurate values of max_ascent and
8917 max_descent we've found while skipping the rest
8918 of the line, for the sake of callers, such as
8919 pos_visible_p, that need to know the line
8920 height. */
8921 int max_ascent = it->max_ascent;
8922 int max_descent = it->max_descent;
8923
8924 RESTORE_IT (it, &it_backup, backup_data);
8925 it->max_ascent = max_ascent;
8926 it->max_descent = max_descent;
8927 reached = 6;
8928 }
8929 else
8930 {
8931 skip = skip2;
8932 if (skip == MOVE_POS_MATCH_OR_ZV)
8933 reached = 7;
8934 }
8935 }
8936 else
8937 {
8938 /* Check whether TO_Y is in this line. */
8939 line_height = it->max_ascent + it->max_descent;
8940 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8941
8942 if (to_y >= it->current_y
8943 && to_y < it->current_y + line_height)
8944 {
8945 /* When word-wrap is on, TO_X may lie past the end
8946 of a wrapped line. Then it->current is the
8947 character on the next line, so backtrack to the
8948 space before the wrap point. */
8949 if (skip == MOVE_LINE_CONTINUED
8950 && it->line_wrap == WORD_WRAP)
8951 {
8952 int prev_x = max (it->current_x - 1, 0);
8953 RESTORE_IT (it, &it_backup, backup_data);
8954 skip = move_it_in_display_line_to
8955 (it, -1, prev_x, MOVE_TO_X);
8956 }
8957 reached = 6;
8958 }
8959 }
8960
8961 if (reached)
8962 break;
8963 }
8964 else if (BUFFERP (it->object)
8965 && (it->method == GET_FROM_BUFFER
8966 || it->method == GET_FROM_STRETCH)
8967 && IT_CHARPOS (*it) >= to_charpos
8968 /* Under bidi iteration, a call to set_iterator_to_next
8969 can scan far beyond to_charpos if the initial
8970 portion of the next line needs to be reordered. In
8971 that case, give move_it_in_display_line_to another
8972 chance below. */
8973 && !(it->bidi_p
8974 && it->bidi_it.scan_dir == -1))
8975 skip = MOVE_POS_MATCH_OR_ZV;
8976 else
8977 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8978
8979 switch (skip)
8980 {
8981 case MOVE_POS_MATCH_OR_ZV:
8982 reached = 8;
8983 goto out;
8984
8985 case MOVE_NEWLINE_OR_CR:
8986 set_iterator_to_next (it, 1);
8987 it->continuation_lines_width = 0;
8988 break;
8989
8990 case MOVE_LINE_TRUNCATED:
8991 it->continuation_lines_width = 0;
8992 reseat_at_next_visible_line_start (it, 0);
8993 if ((op & MOVE_TO_POS) != 0
8994 && IT_CHARPOS (*it) > to_charpos)
8995 {
8996 reached = 9;
8997 goto out;
8998 }
8999 break;
9000
9001 case MOVE_LINE_CONTINUED:
9002 /* For continued lines ending in a tab, some of the glyphs
9003 associated with the tab are displayed on the current
9004 line. Since it->current_x does not include these glyphs,
9005 we use it->last_visible_x instead. */
9006 if (it->c == '\t')
9007 {
9008 it->continuation_lines_width += it->last_visible_x;
9009 /* When moving by vpos, ensure that the iterator really
9010 advances to the next line (bug#847, bug#969). Fixme:
9011 do we need to do this in other circumstances? */
9012 if (it->current_x != it->last_visible_x
9013 && (op & MOVE_TO_VPOS)
9014 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9015 {
9016 line_start_x = it->current_x + it->pixel_width
9017 - it->last_visible_x;
9018 set_iterator_to_next (it, 0);
9019 }
9020 }
9021 else
9022 it->continuation_lines_width += it->current_x;
9023 break;
9024
9025 default:
9026 emacs_abort ();
9027 }
9028
9029 /* Reset/increment for the next run. */
9030 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9031 it->current_x = line_start_x;
9032 line_start_x = 0;
9033 it->hpos = 0;
9034 it->current_y += it->max_ascent + it->max_descent;
9035 ++it->vpos;
9036 last_height = it->max_ascent + it->max_descent;
9037 it->max_ascent = it->max_descent = 0;
9038 }
9039
9040 out:
9041
9042 /* On text terminals, we may stop at the end of a line in the middle
9043 of a multi-character glyph. If the glyph itself is continued,
9044 i.e. it is actually displayed on the next line, don't treat this
9045 stopping point as valid; move to the next line instead (unless
9046 that brings us offscreen). */
9047 if (!FRAME_WINDOW_P (it->f)
9048 && op & MOVE_TO_POS
9049 && IT_CHARPOS (*it) == to_charpos
9050 && it->what == IT_CHARACTER
9051 && it->nglyphs > 1
9052 && it->line_wrap == WINDOW_WRAP
9053 && it->current_x == it->last_visible_x - 1
9054 && it->c != '\n'
9055 && it->c != '\t'
9056 && it->vpos < it->w->window_end_vpos)
9057 {
9058 it->continuation_lines_width += it->current_x;
9059 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9060 it->current_y += it->max_ascent + it->max_descent;
9061 ++it->vpos;
9062 last_height = it->max_ascent + it->max_descent;
9063 }
9064
9065 if (backup_data)
9066 bidi_unshelve_cache (backup_data, 1);
9067
9068 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9069 }
9070
9071
9072 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9073
9074 If DY > 0, move IT backward at least that many pixels. DY = 0
9075 means move IT backward to the preceding line start or BEGV. This
9076 function may move over more than DY pixels if IT->current_y - DY
9077 ends up in the middle of a line; in this case IT->current_y will be
9078 set to the top of the line moved to. */
9079
9080 void
9081 move_it_vertically_backward (struct it *it, int dy)
9082 {
9083 int nlines, h;
9084 struct it it2, it3;
9085 void *it2data = NULL, *it3data = NULL;
9086 ptrdiff_t start_pos;
9087 int nchars_per_row
9088 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9089 ptrdiff_t pos_limit;
9090
9091 move_further_back:
9092 eassert (dy >= 0);
9093
9094 start_pos = IT_CHARPOS (*it);
9095
9096 /* Estimate how many newlines we must move back. */
9097 nlines = max (1, dy / default_line_pixel_height (it->w));
9098 if (it->line_wrap == TRUNCATE)
9099 pos_limit = BEGV;
9100 else
9101 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9102
9103 /* Set the iterator's position that many lines back. But don't go
9104 back more than NLINES full screen lines -- this wins a day with
9105 buffers which have very long lines. */
9106 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9107 back_to_previous_visible_line_start (it);
9108
9109 /* Reseat the iterator here. When moving backward, we don't want
9110 reseat to skip forward over invisible text, set up the iterator
9111 to deliver from overlay strings at the new position etc. So,
9112 use reseat_1 here. */
9113 reseat_1 (it, it->current.pos, 1);
9114
9115 /* We are now surely at a line start. */
9116 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9117 reordering is in effect. */
9118 it->continuation_lines_width = 0;
9119
9120 /* Move forward and see what y-distance we moved. First move to the
9121 start of the next line so that we get its height. We need this
9122 height to be able to tell whether we reached the specified
9123 y-distance. */
9124 SAVE_IT (it2, *it, it2data);
9125 it2.max_ascent = it2.max_descent = 0;
9126 do
9127 {
9128 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9129 MOVE_TO_POS | MOVE_TO_VPOS);
9130 }
9131 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9132 /* If we are in a display string which starts at START_POS,
9133 and that display string includes a newline, and we are
9134 right after that newline (i.e. at the beginning of a
9135 display line), exit the loop, because otherwise we will
9136 infloop, since move_it_to will see that it is already at
9137 START_POS and will not move. */
9138 || (it2.method == GET_FROM_STRING
9139 && IT_CHARPOS (it2) == start_pos
9140 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9141 eassert (IT_CHARPOS (*it) >= BEGV);
9142 SAVE_IT (it3, it2, it3data);
9143
9144 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9145 eassert (IT_CHARPOS (*it) >= BEGV);
9146 /* H is the actual vertical distance from the position in *IT
9147 and the starting position. */
9148 h = it2.current_y - it->current_y;
9149 /* NLINES is the distance in number of lines. */
9150 nlines = it2.vpos - it->vpos;
9151
9152 /* Correct IT's y and vpos position
9153 so that they are relative to the starting point. */
9154 it->vpos -= nlines;
9155 it->current_y -= h;
9156
9157 if (dy == 0)
9158 {
9159 /* DY == 0 means move to the start of the screen line. The
9160 value of nlines is > 0 if continuation lines were involved,
9161 or if the original IT position was at start of a line. */
9162 RESTORE_IT (it, it, it2data);
9163 if (nlines > 0)
9164 move_it_by_lines (it, nlines);
9165 /* The above code moves us to some position NLINES down,
9166 usually to its first glyph (leftmost in an L2R line), but
9167 that's not necessarily the start of the line, under bidi
9168 reordering. We want to get to the character position
9169 that is immediately after the newline of the previous
9170 line. */
9171 if (it->bidi_p
9172 && !it->continuation_lines_width
9173 && !STRINGP (it->string)
9174 && IT_CHARPOS (*it) > BEGV
9175 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9176 {
9177 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9178
9179 DEC_BOTH (cp, bp);
9180 cp = find_newline_no_quit (cp, bp, -1, NULL);
9181 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9182 }
9183 bidi_unshelve_cache (it3data, 1);
9184 }
9185 else
9186 {
9187 /* The y-position we try to reach, relative to *IT.
9188 Note that H has been subtracted in front of the if-statement. */
9189 int target_y = it->current_y + h - dy;
9190 int y0 = it3.current_y;
9191 int y1;
9192 int line_height;
9193
9194 RESTORE_IT (&it3, &it3, it3data);
9195 y1 = line_bottom_y (&it3);
9196 line_height = y1 - y0;
9197 RESTORE_IT (it, it, it2data);
9198 /* If we did not reach target_y, try to move further backward if
9199 we can. If we moved too far backward, try to move forward. */
9200 if (target_y < it->current_y
9201 /* This is heuristic. In a window that's 3 lines high, with
9202 a line height of 13 pixels each, recentering with point
9203 on the bottom line will try to move -39/2 = 19 pixels
9204 backward. Try to avoid moving into the first line. */
9205 && (it->current_y - target_y
9206 > min (window_box_height (it->w), line_height * 2 / 3))
9207 && IT_CHARPOS (*it) > BEGV)
9208 {
9209 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9210 target_y - it->current_y));
9211 dy = it->current_y - target_y;
9212 goto move_further_back;
9213 }
9214 else if (target_y >= it->current_y + line_height
9215 && IT_CHARPOS (*it) < ZV)
9216 {
9217 /* Should move forward by at least one line, maybe more.
9218
9219 Note: Calling move_it_by_lines can be expensive on
9220 terminal frames, where compute_motion is used (via
9221 vmotion) to do the job, when there are very long lines
9222 and truncate-lines is nil. That's the reason for
9223 treating terminal frames specially here. */
9224
9225 if (!FRAME_WINDOW_P (it->f))
9226 move_it_vertically (it, target_y - (it->current_y + line_height));
9227 else
9228 {
9229 do
9230 {
9231 move_it_by_lines (it, 1);
9232 }
9233 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9234 }
9235 }
9236 }
9237 }
9238
9239
9240 /* Move IT by a specified amount of pixel lines DY. DY negative means
9241 move backwards. DY = 0 means move to start of screen line. At the
9242 end, IT will be on the start of a screen line. */
9243
9244 void
9245 move_it_vertically (struct it *it, int dy)
9246 {
9247 if (dy <= 0)
9248 move_it_vertically_backward (it, -dy);
9249 else
9250 {
9251 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9252 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9253 MOVE_TO_POS | MOVE_TO_Y);
9254 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9255
9256 /* If buffer ends in ZV without a newline, move to the start of
9257 the line to satisfy the post-condition. */
9258 if (IT_CHARPOS (*it) == ZV
9259 && ZV > BEGV
9260 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9261 move_it_by_lines (it, 0);
9262 }
9263 }
9264
9265
9266 /* Move iterator IT past the end of the text line it is in. */
9267
9268 void
9269 move_it_past_eol (struct it *it)
9270 {
9271 enum move_it_result rc;
9272
9273 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9274 if (rc == MOVE_NEWLINE_OR_CR)
9275 set_iterator_to_next (it, 0);
9276 }
9277
9278
9279 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9280 negative means move up. DVPOS == 0 means move to the start of the
9281 screen line.
9282
9283 Optimization idea: If we would know that IT->f doesn't use
9284 a face with proportional font, we could be faster for
9285 truncate-lines nil. */
9286
9287 void
9288 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9289 {
9290
9291 /* The commented-out optimization uses vmotion on terminals. This
9292 gives bad results, because elements like it->what, on which
9293 callers such as pos_visible_p rely, aren't updated. */
9294 /* struct position pos;
9295 if (!FRAME_WINDOW_P (it->f))
9296 {
9297 struct text_pos textpos;
9298
9299 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9300 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9301 reseat (it, textpos, 1);
9302 it->vpos += pos.vpos;
9303 it->current_y += pos.vpos;
9304 }
9305 else */
9306
9307 if (dvpos == 0)
9308 {
9309 /* DVPOS == 0 means move to the start of the screen line. */
9310 move_it_vertically_backward (it, 0);
9311 /* Let next call to line_bottom_y calculate real line height */
9312 last_height = 0;
9313 }
9314 else if (dvpos > 0)
9315 {
9316 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9317 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9318 {
9319 /* Only move to the next buffer position if we ended up in a
9320 string from display property, not in an overlay string
9321 (before-string or after-string). That is because the
9322 latter don't conceal the underlying buffer position, so
9323 we can ask to move the iterator to the exact position we
9324 are interested in. Note that, even if we are already at
9325 IT_CHARPOS (*it), the call below is not a no-op, as it
9326 will detect that we are at the end of the string, pop the
9327 iterator, and compute it->current_x and it->hpos
9328 correctly. */
9329 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9330 -1, -1, -1, MOVE_TO_POS);
9331 }
9332 }
9333 else
9334 {
9335 struct it it2;
9336 void *it2data = NULL;
9337 ptrdiff_t start_charpos, i;
9338 int nchars_per_row
9339 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9340 ptrdiff_t pos_limit;
9341
9342 /* Start at the beginning of the screen line containing IT's
9343 position. This may actually move vertically backwards,
9344 in case of overlays, so adjust dvpos accordingly. */
9345 dvpos += it->vpos;
9346 move_it_vertically_backward (it, 0);
9347 dvpos -= it->vpos;
9348
9349 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9350 screen lines, and reseat the iterator there. */
9351 start_charpos = IT_CHARPOS (*it);
9352 if (it->line_wrap == TRUNCATE)
9353 pos_limit = BEGV;
9354 else
9355 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9356 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9357 back_to_previous_visible_line_start (it);
9358 reseat (it, it->current.pos, 1);
9359
9360 /* Move further back if we end up in a string or an image. */
9361 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9362 {
9363 /* First try to move to start of display line. */
9364 dvpos += it->vpos;
9365 move_it_vertically_backward (it, 0);
9366 dvpos -= it->vpos;
9367 if (IT_POS_VALID_AFTER_MOVE_P (it))
9368 break;
9369 /* If start of line is still in string or image,
9370 move further back. */
9371 back_to_previous_visible_line_start (it);
9372 reseat (it, it->current.pos, 1);
9373 dvpos--;
9374 }
9375
9376 it->current_x = it->hpos = 0;
9377
9378 /* Above call may have moved too far if continuation lines
9379 are involved. Scan forward and see if it did. */
9380 SAVE_IT (it2, *it, it2data);
9381 it2.vpos = it2.current_y = 0;
9382 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9383 it->vpos -= it2.vpos;
9384 it->current_y -= it2.current_y;
9385 it->current_x = it->hpos = 0;
9386
9387 /* If we moved too far back, move IT some lines forward. */
9388 if (it2.vpos > -dvpos)
9389 {
9390 int delta = it2.vpos + dvpos;
9391
9392 RESTORE_IT (&it2, &it2, it2data);
9393 SAVE_IT (it2, *it, it2data);
9394 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9395 /* Move back again if we got too far ahead. */
9396 if (IT_CHARPOS (*it) >= start_charpos)
9397 RESTORE_IT (it, &it2, it2data);
9398 else
9399 bidi_unshelve_cache (it2data, 1);
9400 }
9401 else
9402 RESTORE_IT (it, it, it2data);
9403 }
9404 }
9405
9406 /* Return 1 if IT points into the middle of a display vector. */
9407
9408 int
9409 in_display_vector_p (struct it *it)
9410 {
9411 return (it->method == GET_FROM_DISPLAY_VECTOR
9412 && it->current.dpvec_index > 0
9413 && it->dpvec + it->current.dpvec_index != it->dpend);
9414 }
9415
9416 \f
9417 /***********************************************************************
9418 Messages
9419 ***********************************************************************/
9420
9421
9422 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9423 to *Messages*. */
9424
9425 void
9426 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9427 {
9428 Lisp_Object args[3];
9429 Lisp_Object msg, fmt;
9430 char *buffer;
9431 ptrdiff_t len;
9432 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9433 USE_SAFE_ALLOCA;
9434
9435 fmt = msg = Qnil;
9436 GCPRO4 (fmt, msg, arg1, arg2);
9437
9438 args[0] = fmt = build_string (format);
9439 args[1] = arg1;
9440 args[2] = arg2;
9441 msg = Fformat (3, args);
9442
9443 len = SBYTES (msg) + 1;
9444 buffer = SAFE_ALLOCA (len);
9445 memcpy (buffer, SDATA (msg), len);
9446
9447 message_dolog (buffer, len - 1, 1, 0);
9448 SAFE_FREE ();
9449
9450 UNGCPRO;
9451 }
9452
9453
9454 /* Output a newline in the *Messages* buffer if "needs" one. */
9455
9456 void
9457 message_log_maybe_newline (void)
9458 {
9459 if (message_log_need_newline)
9460 message_dolog ("", 0, 1, 0);
9461 }
9462
9463
9464 /* Add a string M of length NBYTES to the message log, optionally
9465 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9466 true, means interpret the contents of M as multibyte. This
9467 function calls low-level routines in order to bypass text property
9468 hooks, etc. which might not be safe to run.
9469
9470 This may GC (insert may run before/after change hooks),
9471 so the buffer M must NOT point to a Lisp string. */
9472
9473 void
9474 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9475 {
9476 const unsigned char *msg = (const unsigned char *) m;
9477
9478 if (!NILP (Vmemory_full))
9479 return;
9480
9481 if (!NILP (Vmessage_log_max))
9482 {
9483 struct buffer *oldbuf;
9484 Lisp_Object oldpoint, oldbegv, oldzv;
9485 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9486 ptrdiff_t point_at_end = 0;
9487 ptrdiff_t zv_at_end = 0;
9488 Lisp_Object old_deactivate_mark;
9489 bool shown;
9490 struct gcpro gcpro1;
9491
9492 old_deactivate_mark = Vdeactivate_mark;
9493 oldbuf = current_buffer;
9494 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9495 bset_undo_list (current_buffer, Qt);
9496
9497 oldpoint = message_dolog_marker1;
9498 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9499 oldbegv = message_dolog_marker2;
9500 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9501 oldzv = message_dolog_marker3;
9502 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9503 GCPRO1 (old_deactivate_mark);
9504
9505 if (PT == Z)
9506 point_at_end = 1;
9507 if (ZV == Z)
9508 zv_at_end = 1;
9509
9510 BEGV = BEG;
9511 BEGV_BYTE = BEG_BYTE;
9512 ZV = Z;
9513 ZV_BYTE = Z_BYTE;
9514 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9515
9516 /* Insert the string--maybe converting multibyte to single byte
9517 or vice versa, so that all the text fits the buffer. */
9518 if (multibyte
9519 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9520 {
9521 ptrdiff_t i;
9522 int c, char_bytes;
9523 char work[1];
9524
9525 /* Convert a multibyte string to single-byte
9526 for the *Message* buffer. */
9527 for (i = 0; i < nbytes; i += char_bytes)
9528 {
9529 c = string_char_and_length (msg + i, &char_bytes);
9530 work[0] = (ASCII_CHAR_P (c)
9531 ? c
9532 : multibyte_char_to_unibyte (c));
9533 insert_1_both (work, 1, 1, 1, 0, 0);
9534 }
9535 }
9536 else if (! multibyte
9537 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9538 {
9539 ptrdiff_t i;
9540 int c, char_bytes;
9541 unsigned char str[MAX_MULTIBYTE_LENGTH];
9542 /* Convert a single-byte string to multibyte
9543 for the *Message* buffer. */
9544 for (i = 0; i < nbytes; i++)
9545 {
9546 c = msg[i];
9547 MAKE_CHAR_MULTIBYTE (c);
9548 char_bytes = CHAR_STRING (c, str);
9549 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9550 }
9551 }
9552 else if (nbytes)
9553 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9554
9555 if (nlflag)
9556 {
9557 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9558 printmax_t dups;
9559
9560 insert_1_both ("\n", 1, 1, 1, 0, 0);
9561
9562 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9563 this_bol = PT;
9564 this_bol_byte = PT_BYTE;
9565
9566 /* See if this line duplicates the previous one.
9567 If so, combine duplicates. */
9568 if (this_bol > BEG)
9569 {
9570 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9571 prev_bol = PT;
9572 prev_bol_byte = PT_BYTE;
9573
9574 dups = message_log_check_duplicate (prev_bol_byte,
9575 this_bol_byte);
9576 if (dups)
9577 {
9578 del_range_both (prev_bol, prev_bol_byte,
9579 this_bol, this_bol_byte, 0);
9580 if (dups > 1)
9581 {
9582 char dupstr[sizeof " [ times]"
9583 + INT_STRLEN_BOUND (printmax_t)];
9584
9585 /* If you change this format, don't forget to also
9586 change message_log_check_duplicate. */
9587 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9588 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9589 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9590 }
9591 }
9592 }
9593
9594 /* If we have more than the desired maximum number of lines
9595 in the *Messages* buffer now, delete the oldest ones.
9596 This is safe because we don't have undo in this buffer. */
9597
9598 if (NATNUMP (Vmessage_log_max))
9599 {
9600 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9601 -XFASTINT (Vmessage_log_max) - 1, 0);
9602 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9603 }
9604 }
9605 BEGV = marker_position (oldbegv);
9606 BEGV_BYTE = marker_byte_position (oldbegv);
9607
9608 if (zv_at_end)
9609 {
9610 ZV = Z;
9611 ZV_BYTE = Z_BYTE;
9612 }
9613 else
9614 {
9615 ZV = marker_position (oldzv);
9616 ZV_BYTE = marker_byte_position (oldzv);
9617 }
9618
9619 if (point_at_end)
9620 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9621 else
9622 /* We can't do Fgoto_char (oldpoint) because it will run some
9623 Lisp code. */
9624 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9625 marker_byte_position (oldpoint));
9626
9627 UNGCPRO;
9628 unchain_marker (XMARKER (oldpoint));
9629 unchain_marker (XMARKER (oldbegv));
9630 unchain_marker (XMARKER (oldzv));
9631
9632 shown = buffer_window_count (current_buffer) > 0;
9633 set_buffer_internal (oldbuf);
9634 /* We called insert_1_both above with its 5th argument (PREPARE)
9635 zero, which prevents insert_1_both from calling
9636 prepare_to_modify_buffer, which in turns prevents us from
9637 incrementing windows_or_buffers_changed even if *Messages* is
9638 shown in some window. So we must manually incrementing
9639 windows_or_buffers_changed here to make up for that. */
9640 if (shown)
9641 windows_or_buffers_changed++;
9642 else
9643 windows_or_buffers_changed = old_windows_or_buffers_changed;
9644 message_log_need_newline = !nlflag;
9645 Vdeactivate_mark = old_deactivate_mark;
9646 }
9647 }
9648
9649
9650 /* We are at the end of the buffer after just having inserted a newline.
9651 (Note: We depend on the fact we won't be crossing the gap.)
9652 Check to see if the most recent message looks a lot like the previous one.
9653 Return 0 if different, 1 if the new one should just replace it, or a
9654 value N > 1 if we should also append " [N times]". */
9655
9656 static intmax_t
9657 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9658 {
9659 ptrdiff_t i;
9660 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9661 int seen_dots = 0;
9662 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9663 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9664
9665 for (i = 0; i < len; i++)
9666 {
9667 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
9668 seen_dots = 1;
9669 if (p1[i] != p2[i])
9670 return seen_dots;
9671 }
9672 p1 += len;
9673 if (*p1 == '\n')
9674 return 2;
9675 if (*p1++ == ' ' && *p1++ == '[')
9676 {
9677 char *pend;
9678 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9679 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9680 return n + 1;
9681 }
9682 return 0;
9683 }
9684 \f
9685
9686 /* Display an echo area message M with a specified length of NBYTES
9687 bytes. The string may include null characters. If M is not a
9688 string, clear out any existing message, and let the mini-buffer
9689 text show through.
9690
9691 This function cancels echoing. */
9692
9693 void
9694 message3 (Lisp_Object m)
9695 {
9696 struct gcpro gcpro1;
9697
9698 GCPRO1 (m);
9699 clear_message (1,1);
9700 cancel_echoing ();
9701
9702 /* First flush out any partial line written with print. */
9703 message_log_maybe_newline ();
9704 if (STRINGP (m))
9705 {
9706 ptrdiff_t nbytes = SBYTES (m);
9707 bool multibyte = STRING_MULTIBYTE (m);
9708 USE_SAFE_ALLOCA;
9709 char *buffer = SAFE_ALLOCA (nbytes);
9710 memcpy (buffer, SDATA (m), nbytes);
9711 message_dolog (buffer, nbytes, 1, multibyte);
9712 SAFE_FREE ();
9713 }
9714 message3_nolog (m);
9715
9716 UNGCPRO;
9717 }
9718
9719
9720 /* The non-logging version of message3.
9721 This does not cancel echoing, because it is used for echoing.
9722 Perhaps we need to make a separate function for echoing
9723 and make this cancel echoing. */
9724
9725 void
9726 message3_nolog (Lisp_Object m)
9727 {
9728 struct frame *sf = SELECTED_FRAME ();
9729
9730 if (FRAME_INITIAL_P (sf))
9731 {
9732 if (noninteractive_need_newline)
9733 putc ('\n', stderr);
9734 noninteractive_need_newline = 0;
9735 if (STRINGP (m))
9736 fwrite (SDATA (m), SBYTES (m), 1, stderr);
9737 if (cursor_in_echo_area == 0)
9738 fprintf (stderr, "\n");
9739 fflush (stderr);
9740 }
9741 /* Error messages get reported properly by cmd_error, so this must be just an
9742 informative message; if the frame hasn't really been initialized yet, just
9743 toss it. */
9744 else if (INTERACTIVE && sf->glyphs_initialized_p)
9745 {
9746 /* Get the frame containing the mini-buffer
9747 that the selected frame is using. */
9748 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
9749 Lisp_Object frame = XWINDOW (mini_window)->frame;
9750 struct frame *f = XFRAME (frame);
9751
9752 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
9753 Fmake_frame_visible (frame);
9754
9755 if (STRINGP (m) && SCHARS (m) > 0)
9756 {
9757 set_message (m);
9758 if (minibuffer_auto_raise)
9759 Fraise_frame (frame);
9760 /* Assume we are not echoing.
9761 (If we are, echo_now will override this.) */
9762 echo_message_buffer = Qnil;
9763 }
9764 else
9765 clear_message (1, 1);
9766
9767 do_pending_window_change (0);
9768 echo_area_display (1);
9769 do_pending_window_change (0);
9770 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9771 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9772 }
9773 }
9774
9775
9776 /* Display a null-terminated echo area message M. If M is 0, clear
9777 out any existing message, and let the mini-buffer text show through.
9778
9779 The buffer M must continue to exist until after the echo area gets
9780 cleared or some other message gets displayed there. Do not pass
9781 text that is stored in a Lisp string. Do not pass text in a buffer
9782 that was alloca'd. */
9783
9784 void
9785 message1 (const char *m)
9786 {
9787 message3 (m ? build_unibyte_string (m) : Qnil);
9788 }
9789
9790
9791 /* The non-logging counterpart of message1. */
9792
9793 void
9794 message1_nolog (const char *m)
9795 {
9796 message3_nolog (m ? build_unibyte_string (m) : Qnil);
9797 }
9798
9799 /* Display a message M which contains a single %s
9800 which gets replaced with STRING. */
9801
9802 void
9803 message_with_string (const char *m, Lisp_Object string, int log)
9804 {
9805 CHECK_STRING (string);
9806
9807 if (noninteractive)
9808 {
9809 if (m)
9810 {
9811 if (noninteractive_need_newline)
9812 putc ('\n', stderr);
9813 noninteractive_need_newline = 0;
9814 fprintf (stderr, m, SDATA (string));
9815 if (!cursor_in_echo_area)
9816 fprintf (stderr, "\n");
9817 fflush (stderr);
9818 }
9819 }
9820 else if (INTERACTIVE)
9821 {
9822 /* The frame whose minibuffer we're going to display the message on.
9823 It may be larger than the selected frame, so we need
9824 to use its buffer, not the selected frame's buffer. */
9825 Lisp_Object mini_window;
9826 struct frame *f, *sf = SELECTED_FRAME ();
9827
9828 /* Get the frame containing the minibuffer
9829 that the selected frame is using. */
9830 mini_window = FRAME_MINIBUF_WINDOW (sf);
9831 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9832
9833 /* Error messages get reported properly by cmd_error, so this must be
9834 just an informative message; if the frame hasn't really been
9835 initialized yet, just toss it. */
9836 if (f->glyphs_initialized_p)
9837 {
9838 Lisp_Object args[2], msg;
9839 struct gcpro gcpro1, gcpro2;
9840
9841 args[0] = build_string (m);
9842 args[1] = msg = string;
9843 GCPRO2 (args[0], msg);
9844 gcpro1.nvars = 2;
9845
9846 msg = Fformat (2, args);
9847
9848 if (log)
9849 message3 (msg);
9850 else
9851 message3_nolog (msg);
9852
9853 UNGCPRO;
9854
9855 /* Print should start at the beginning of the message
9856 buffer next time. */
9857 message_buf_print = 0;
9858 }
9859 }
9860 }
9861
9862
9863 /* Dump an informative message to the minibuf. If M is 0, clear out
9864 any existing message, and let the mini-buffer text show through. */
9865
9866 static void
9867 vmessage (const char *m, va_list ap)
9868 {
9869 if (noninteractive)
9870 {
9871 if (m)
9872 {
9873 if (noninteractive_need_newline)
9874 putc ('\n', stderr);
9875 noninteractive_need_newline = 0;
9876 vfprintf (stderr, m, ap);
9877 if (cursor_in_echo_area == 0)
9878 fprintf (stderr, "\n");
9879 fflush (stderr);
9880 }
9881 }
9882 else if (INTERACTIVE)
9883 {
9884 /* The frame whose mini-buffer we're going to display the message
9885 on. It may be larger than the selected frame, so we need to
9886 use its buffer, not the selected frame's buffer. */
9887 Lisp_Object mini_window;
9888 struct frame *f, *sf = SELECTED_FRAME ();
9889
9890 /* Get the frame containing the mini-buffer
9891 that the selected frame is using. */
9892 mini_window = FRAME_MINIBUF_WINDOW (sf);
9893 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9894
9895 /* Error messages get reported properly by cmd_error, so this must be
9896 just an informative message; if the frame hasn't really been
9897 initialized yet, just toss it. */
9898 if (f->glyphs_initialized_p)
9899 {
9900 if (m)
9901 {
9902 ptrdiff_t len;
9903 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
9904 char *message_buf = alloca (maxsize + 1);
9905
9906 len = doprnt (message_buf, maxsize, m, 0, ap);
9907
9908 message3 (make_string (message_buf, len));
9909 }
9910 else
9911 message1 (0);
9912
9913 /* Print should start at the beginning of the message
9914 buffer next time. */
9915 message_buf_print = 0;
9916 }
9917 }
9918 }
9919
9920 void
9921 message (const char *m, ...)
9922 {
9923 va_list ap;
9924 va_start (ap, m);
9925 vmessage (m, ap);
9926 va_end (ap);
9927 }
9928
9929
9930 #if 0
9931 /* The non-logging version of message. */
9932
9933 void
9934 message_nolog (const char *m, ...)
9935 {
9936 Lisp_Object old_log_max;
9937 va_list ap;
9938 va_start (ap, m);
9939 old_log_max = Vmessage_log_max;
9940 Vmessage_log_max = Qnil;
9941 vmessage (m, ap);
9942 Vmessage_log_max = old_log_max;
9943 va_end (ap);
9944 }
9945 #endif
9946
9947
9948 /* Display the current message in the current mini-buffer. This is
9949 only called from error handlers in process.c, and is not time
9950 critical. */
9951
9952 void
9953 update_echo_area (void)
9954 {
9955 if (!NILP (echo_area_buffer[0]))
9956 {
9957 Lisp_Object string;
9958 string = Fcurrent_message ();
9959 message3 (string);
9960 }
9961 }
9962
9963
9964 /* Make sure echo area buffers in `echo_buffers' are live.
9965 If they aren't, make new ones. */
9966
9967 static void
9968 ensure_echo_area_buffers (void)
9969 {
9970 int i;
9971
9972 for (i = 0; i < 2; ++i)
9973 if (!BUFFERP (echo_buffer[i])
9974 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
9975 {
9976 char name[30];
9977 Lisp_Object old_buffer;
9978 int j;
9979
9980 old_buffer = echo_buffer[i];
9981 echo_buffer[i] = Fget_buffer_create
9982 (make_formatted_string (name, " *Echo Area %d*", i));
9983 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
9984 /* to force word wrap in echo area -
9985 it was decided to postpone this*/
9986 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9987
9988 for (j = 0; j < 2; ++j)
9989 if (EQ (old_buffer, echo_area_buffer[j]))
9990 echo_area_buffer[j] = echo_buffer[i];
9991 }
9992 }
9993
9994
9995 /* Call FN with args A1..A2 with either the current or last displayed
9996 echo_area_buffer as current buffer.
9997
9998 WHICH zero means use the current message buffer
9999 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10000 from echo_buffer[] and clear it.
10001
10002 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10003 suitable buffer from echo_buffer[] and clear it.
10004
10005 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10006 that the current message becomes the last displayed one, make
10007 choose a suitable buffer for echo_area_buffer[0], and clear it.
10008
10009 Value is what FN returns. */
10010
10011 static int
10012 with_echo_area_buffer (struct window *w, int which,
10013 int (*fn) (ptrdiff_t, Lisp_Object),
10014 ptrdiff_t a1, Lisp_Object a2)
10015 {
10016 Lisp_Object buffer;
10017 int this_one, the_other, clear_buffer_p, rc;
10018 ptrdiff_t count = SPECPDL_INDEX ();
10019
10020 /* If buffers aren't live, make new ones. */
10021 ensure_echo_area_buffers ();
10022
10023 clear_buffer_p = 0;
10024
10025 if (which == 0)
10026 this_one = 0, the_other = 1;
10027 else if (which > 0)
10028 this_one = 1, the_other = 0;
10029 else
10030 {
10031 this_one = 0, the_other = 1;
10032 clear_buffer_p = 1;
10033
10034 /* We need a fresh one in case the current echo buffer equals
10035 the one containing the last displayed echo area message. */
10036 if (!NILP (echo_area_buffer[this_one])
10037 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10038 echo_area_buffer[this_one] = Qnil;
10039 }
10040
10041 /* Choose a suitable buffer from echo_buffer[] is we don't
10042 have one. */
10043 if (NILP (echo_area_buffer[this_one]))
10044 {
10045 echo_area_buffer[this_one]
10046 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10047 ? echo_buffer[the_other]
10048 : echo_buffer[this_one]);
10049 clear_buffer_p = 1;
10050 }
10051
10052 buffer = echo_area_buffer[this_one];
10053
10054 /* Don't get confused by reusing the buffer used for echoing
10055 for a different purpose. */
10056 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10057 cancel_echoing ();
10058
10059 record_unwind_protect (unwind_with_echo_area_buffer,
10060 with_echo_area_buffer_unwind_data (w));
10061
10062 /* Make the echo area buffer current. Note that for display
10063 purposes, it is not necessary that the displayed window's buffer
10064 == current_buffer, except for text property lookup. So, let's
10065 only set that buffer temporarily here without doing a full
10066 Fset_window_buffer. We must also change w->pointm, though,
10067 because otherwise an assertions in unshow_buffer fails, and Emacs
10068 aborts. */
10069 set_buffer_internal_1 (XBUFFER (buffer));
10070 if (w)
10071 {
10072 wset_buffer (w, buffer);
10073 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10074 }
10075
10076 bset_undo_list (current_buffer, Qt);
10077 bset_read_only (current_buffer, Qnil);
10078 specbind (Qinhibit_read_only, Qt);
10079 specbind (Qinhibit_modification_hooks, Qt);
10080
10081 if (clear_buffer_p && Z > BEG)
10082 del_range (BEG, Z);
10083
10084 eassert (BEGV >= BEG);
10085 eassert (ZV <= Z && ZV >= BEGV);
10086
10087 rc = fn (a1, a2);
10088
10089 eassert (BEGV >= BEG);
10090 eassert (ZV <= Z && ZV >= BEGV);
10091
10092 unbind_to (count, Qnil);
10093 return rc;
10094 }
10095
10096
10097 /* Save state that should be preserved around the call to the function
10098 FN called in with_echo_area_buffer. */
10099
10100 static Lisp_Object
10101 with_echo_area_buffer_unwind_data (struct window *w)
10102 {
10103 int i = 0;
10104 Lisp_Object vector, tmp;
10105
10106 /* Reduce consing by keeping one vector in
10107 Vwith_echo_area_save_vector. */
10108 vector = Vwith_echo_area_save_vector;
10109 Vwith_echo_area_save_vector = Qnil;
10110
10111 if (NILP (vector))
10112 vector = Fmake_vector (make_number (9), Qnil);
10113
10114 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10115 ASET (vector, i, Vdeactivate_mark); ++i;
10116 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10117
10118 if (w)
10119 {
10120 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10121 ASET (vector, i, w->contents); ++i;
10122 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10123 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10124 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10125 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10126 }
10127 else
10128 {
10129 int end = i + 6;
10130 for (; i < end; ++i)
10131 ASET (vector, i, Qnil);
10132 }
10133
10134 eassert (i == ASIZE (vector));
10135 return vector;
10136 }
10137
10138
10139 /* Restore global state from VECTOR which was created by
10140 with_echo_area_buffer_unwind_data. */
10141
10142 static void
10143 unwind_with_echo_area_buffer (Lisp_Object vector)
10144 {
10145 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10146 Vdeactivate_mark = AREF (vector, 1);
10147 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10148
10149 if (WINDOWP (AREF (vector, 3)))
10150 {
10151 struct window *w;
10152 Lisp_Object buffer;
10153
10154 w = XWINDOW (AREF (vector, 3));
10155 buffer = AREF (vector, 4);
10156
10157 wset_buffer (w, buffer);
10158 set_marker_both (w->pointm, buffer,
10159 XFASTINT (AREF (vector, 5)),
10160 XFASTINT (AREF (vector, 6)));
10161 set_marker_both (w->start, buffer,
10162 XFASTINT (AREF (vector, 7)),
10163 XFASTINT (AREF (vector, 8)));
10164 }
10165
10166 Vwith_echo_area_save_vector = vector;
10167 }
10168
10169
10170 /* Set up the echo area for use by print functions. MULTIBYTE_P
10171 non-zero means we will print multibyte. */
10172
10173 void
10174 setup_echo_area_for_printing (int multibyte_p)
10175 {
10176 /* If we can't find an echo area any more, exit. */
10177 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10178 Fkill_emacs (Qnil);
10179
10180 ensure_echo_area_buffers ();
10181
10182 if (!message_buf_print)
10183 {
10184 /* A message has been output since the last time we printed.
10185 Choose a fresh echo area buffer. */
10186 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10187 echo_area_buffer[0] = echo_buffer[1];
10188 else
10189 echo_area_buffer[0] = echo_buffer[0];
10190
10191 /* Switch to that buffer and clear it. */
10192 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10193 bset_truncate_lines (current_buffer, Qnil);
10194
10195 if (Z > BEG)
10196 {
10197 ptrdiff_t count = SPECPDL_INDEX ();
10198 specbind (Qinhibit_read_only, Qt);
10199 /* Note that undo recording is always disabled. */
10200 del_range (BEG, Z);
10201 unbind_to (count, Qnil);
10202 }
10203 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10204
10205 /* Set up the buffer for the multibyteness we need. */
10206 if (multibyte_p
10207 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10208 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10209
10210 /* Raise the frame containing the echo area. */
10211 if (minibuffer_auto_raise)
10212 {
10213 struct frame *sf = SELECTED_FRAME ();
10214 Lisp_Object mini_window;
10215 mini_window = FRAME_MINIBUF_WINDOW (sf);
10216 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10217 }
10218
10219 message_log_maybe_newline ();
10220 message_buf_print = 1;
10221 }
10222 else
10223 {
10224 if (NILP (echo_area_buffer[0]))
10225 {
10226 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10227 echo_area_buffer[0] = echo_buffer[1];
10228 else
10229 echo_area_buffer[0] = echo_buffer[0];
10230 }
10231
10232 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10233 {
10234 /* Someone switched buffers between print requests. */
10235 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10236 bset_truncate_lines (current_buffer, Qnil);
10237 }
10238 }
10239 }
10240
10241
10242 /* Display an echo area message in window W. Value is non-zero if W's
10243 height is changed. If display_last_displayed_message_p is
10244 non-zero, display the message that was last displayed, otherwise
10245 display the current message. */
10246
10247 static int
10248 display_echo_area (struct window *w)
10249 {
10250 int i, no_message_p, window_height_changed_p;
10251
10252 /* Temporarily disable garbage collections while displaying the echo
10253 area. This is done because a GC can print a message itself.
10254 That message would modify the echo area buffer's contents while a
10255 redisplay of the buffer is going on, and seriously confuse
10256 redisplay. */
10257 ptrdiff_t count = inhibit_garbage_collection ();
10258
10259 /* If there is no message, we must call display_echo_area_1
10260 nevertheless because it resizes the window. But we will have to
10261 reset the echo_area_buffer in question to nil at the end because
10262 with_echo_area_buffer will sets it to an empty buffer. */
10263 i = display_last_displayed_message_p ? 1 : 0;
10264 no_message_p = NILP (echo_area_buffer[i]);
10265
10266 window_height_changed_p
10267 = with_echo_area_buffer (w, display_last_displayed_message_p,
10268 display_echo_area_1,
10269 (intptr_t) w, Qnil);
10270
10271 if (no_message_p)
10272 echo_area_buffer[i] = Qnil;
10273
10274 unbind_to (count, Qnil);
10275 return window_height_changed_p;
10276 }
10277
10278
10279 /* Helper for display_echo_area. Display the current buffer which
10280 contains the current echo area message in window W, a mini-window,
10281 a pointer to which is passed in A1. A2..A4 are currently not used.
10282 Change the height of W so that all of the message is displayed.
10283 Value is non-zero if height of W was changed. */
10284
10285 static int
10286 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10287 {
10288 intptr_t i1 = a1;
10289 struct window *w = (struct window *) i1;
10290 Lisp_Object window;
10291 struct text_pos start;
10292 int window_height_changed_p = 0;
10293
10294 /* Do this before displaying, so that we have a large enough glyph
10295 matrix for the display. If we can't get enough space for the
10296 whole text, display the last N lines. That works by setting w->start. */
10297 window_height_changed_p = resize_mini_window (w, 0);
10298
10299 /* Use the starting position chosen by resize_mini_window. */
10300 SET_TEXT_POS_FROM_MARKER (start, w->start);
10301
10302 /* Display. */
10303 clear_glyph_matrix (w->desired_matrix);
10304 XSETWINDOW (window, w);
10305 try_window (window, start, 0);
10306
10307 return window_height_changed_p;
10308 }
10309
10310
10311 /* Resize the echo area window to exactly the size needed for the
10312 currently displayed message, if there is one. If a mini-buffer
10313 is active, don't shrink it. */
10314
10315 void
10316 resize_echo_area_exactly (void)
10317 {
10318 if (BUFFERP (echo_area_buffer[0])
10319 && WINDOWP (echo_area_window))
10320 {
10321 struct window *w = XWINDOW (echo_area_window);
10322 int resized_p;
10323 Lisp_Object resize_exactly;
10324
10325 if (minibuf_level == 0)
10326 resize_exactly = Qt;
10327 else
10328 resize_exactly = Qnil;
10329
10330 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10331 (intptr_t) w, resize_exactly);
10332 if (resized_p)
10333 {
10334 ++windows_or_buffers_changed;
10335 ++update_mode_lines;
10336 redisplay_internal ();
10337 }
10338 }
10339 }
10340
10341
10342 /* Callback function for with_echo_area_buffer, when used from
10343 resize_echo_area_exactly. A1 contains a pointer to the window to
10344 resize, EXACTLY non-nil means resize the mini-window exactly to the
10345 size of the text displayed. A3 and A4 are not used. Value is what
10346 resize_mini_window returns. */
10347
10348 static int
10349 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10350 {
10351 intptr_t i1 = a1;
10352 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10353 }
10354
10355
10356 /* Resize mini-window W to fit the size of its contents. EXACT_P
10357 means size the window exactly to the size needed. Otherwise, it's
10358 only enlarged until W's buffer is empty.
10359
10360 Set W->start to the right place to begin display. If the whole
10361 contents fit, start at the beginning. Otherwise, start so as
10362 to make the end of the contents appear. This is particularly
10363 important for y-or-n-p, but seems desirable generally.
10364
10365 Value is non-zero if the window height has been changed. */
10366
10367 int
10368 resize_mini_window (struct window *w, int exact_p)
10369 {
10370 struct frame *f = XFRAME (w->frame);
10371 int window_height_changed_p = 0;
10372
10373 eassert (MINI_WINDOW_P (w));
10374
10375 /* By default, start display at the beginning. */
10376 set_marker_both (w->start, w->contents,
10377 BUF_BEGV (XBUFFER (w->contents)),
10378 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10379
10380 /* Don't resize windows while redisplaying a window; it would
10381 confuse redisplay functions when the size of the window they are
10382 displaying changes from under them. Such a resizing can happen,
10383 for instance, when which-func prints a long message while
10384 we are running fontification-functions. We're running these
10385 functions with safe_call which binds inhibit-redisplay to t. */
10386 if (!NILP (Vinhibit_redisplay))
10387 return 0;
10388
10389 /* Nil means don't try to resize. */
10390 if (NILP (Vresize_mini_windows)
10391 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10392 return 0;
10393
10394 if (!FRAME_MINIBUF_ONLY_P (f))
10395 {
10396 struct it it;
10397 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10398 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10399 int height;
10400 EMACS_INT max_height;
10401 int unit = FRAME_LINE_HEIGHT (f);
10402 struct text_pos start;
10403 struct buffer *old_current_buffer = NULL;
10404
10405 if (current_buffer != XBUFFER (w->contents))
10406 {
10407 old_current_buffer = current_buffer;
10408 set_buffer_internal (XBUFFER (w->contents));
10409 }
10410
10411 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10412
10413 /* Compute the max. number of lines specified by the user. */
10414 if (FLOATP (Vmax_mini_window_height))
10415 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10416 else if (INTEGERP (Vmax_mini_window_height))
10417 max_height = XINT (Vmax_mini_window_height);
10418 else
10419 max_height = total_height / 4;
10420
10421 /* Correct that max. height if it's bogus. */
10422 max_height = clip_to_bounds (1, max_height, total_height);
10423
10424 /* Find out the height of the text in the window. */
10425 if (it.line_wrap == TRUNCATE)
10426 height = 1;
10427 else
10428 {
10429 last_height = 0;
10430 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10431 if (it.max_ascent == 0 && it.max_descent == 0)
10432 height = it.current_y + last_height;
10433 else
10434 height = it.current_y + it.max_ascent + it.max_descent;
10435 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10436 height = (height + unit - 1) / unit;
10437 }
10438
10439 /* Compute a suitable window start. */
10440 if (height > max_height)
10441 {
10442 height = max_height;
10443 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10444 move_it_vertically_backward (&it, (height - 1) * unit);
10445 start = it.current.pos;
10446 }
10447 else
10448 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10449 SET_MARKER_FROM_TEXT_POS (w->start, start);
10450
10451 if (EQ (Vresize_mini_windows, Qgrow_only))
10452 {
10453 /* Let it grow only, until we display an empty message, in which
10454 case the window shrinks again. */
10455 if (height > WINDOW_TOTAL_LINES (w))
10456 {
10457 int old_height = WINDOW_TOTAL_LINES (w);
10458
10459 FRAME_WINDOWS_FROZEN (f) = 1;
10460 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10461 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10462 }
10463 else if (height < WINDOW_TOTAL_LINES (w)
10464 && (exact_p || BEGV == ZV))
10465 {
10466 int old_height = WINDOW_TOTAL_LINES (w);
10467
10468 FRAME_WINDOWS_FROZEN (f) = 0;
10469 shrink_mini_window (w);
10470 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10471 }
10472 }
10473 else
10474 {
10475 /* Always resize to exact size needed. */
10476 if (height > WINDOW_TOTAL_LINES (w))
10477 {
10478 int old_height = WINDOW_TOTAL_LINES (w);
10479
10480 FRAME_WINDOWS_FROZEN (f) = 1;
10481 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10482 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10483 }
10484 else if (height < WINDOW_TOTAL_LINES (w))
10485 {
10486 int old_height = WINDOW_TOTAL_LINES (w);
10487
10488 FRAME_WINDOWS_FROZEN (f) = 0;
10489 shrink_mini_window (w);
10490
10491 if (height)
10492 {
10493 FRAME_WINDOWS_FROZEN (f) = 1;
10494 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10495 }
10496
10497 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10498 }
10499 }
10500
10501 if (old_current_buffer)
10502 set_buffer_internal (old_current_buffer);
10503 }
10504
10505 return window_height_changed_p;
10506 }
10507
10508
10509 /* Value is the current message, a string, or nil if there is no
10510 current message. */
10511
10512 Lisp_Object
10513 current_message (void)
10514 {
10515 Lisp_Object msg;
10516
10517 if (!BUFFERP (echo_area_buffer[0]))
10518 msg = Qnil;
10519 else
10520 {
10521 with_echo_area_buffer (0, 0, current_message_1,
10522 (intptr_t) &msg, Qnil);
10523 if (NILP (msg))
10524 echo_area_buffer[0] = Qnil;
10525 }
10526
10527 return msg;
10528 }
10529
10530
10531 static int
10532 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10533 {
10534 intptr_t i1 = a1;
10535 Lisp_Object *msg = (Lisp_Object *) i1;
10536
10537 if (Z > BEG)
10538 *msg = make_buffer_string (BEG, Z, 1);
10539 else
10540 *msg = Qnil;
10541 return 0;
10542 }
10543
10544
10545 /* Push the current message on Vmessage_stack for later restoration
10546 by restore_message. Value is non-zero if the current message isn't
10547 empty. This is a relatively infrequent operation, so it's not
10548 worth optimizing. */
10549
10550 bool
10551 push_message (void)
10552 {
10553 Lisp_Object msg = current_message ();
10554 Vmessage_stack = Fcons (msg, Vmessage_stack);
10555 return STRINGP (msg);
10556 }
10557
10558
10559 /* Restore message display from the top of Vmessage_stack. */
10560
10561 void
10562 restore_message (void)
10563 {
10564 eassert (CONSP (Vmessage_stack));
10565 message3_nolog (XCAR (Vmessage_stack));
10566 }
10567
10568
10569 /* Handler for unwind-protect calling pop_message. */
10570
10571 void
10572 pop_message_unwind (void)
10573 {
10574 /* Pop the top-most entry off Vmessage_stack. */
10575 eassert (CONSP (Vmessage_stack));
10576 Vmessage_stack = XCDR (Vmessage_stack);
10577 }
10578
10579
10580 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10581 exits. If the stack is not empty, we have a missing pop_message
10582 somewhere. */
10583
10584 void
10585 check_message_stack (void)
10586 {
10587 if (!NILP (Vmessage_stack))
10588 emacs_abort ();
10589 }
10590
10591
10592 /* Truncate to NCHARS what will be displayed in the echo area the next
10593 time we display it---but don't redisplay it now. */
10594
10595 void
10596 truncate_echo_area (ptrdiff_t nchars)
10597 {
10598 if (nchars == 0)
10599 echo_area_buffer[0] = Qnil;
10600 else if (!noninteractive
10601 && INTERACTIVE
10602 && !NILP (echo_area_buffer[0]))
10603 {
10604 struct frame *sf = SELECTED_FRAME ();
10605 /* Error messages get reported properly by cmd_error, so this must be
10606 just an informative message; if the frame hasn't really been
10607 initialized yet, just toss it. */
10608 if (sf->glyphs_initialized_p)
10609 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10610 }
10611 }
10612
10613
10614 /* Helper function for truncate_echo_area. Truncate the current
10615 message to at most NCHARS characters. */
10616
10617 static int
10618 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10619 {
10620 if (BEG + nchars < Z)
10621 del_range (BEG + nchars, Z);
10622 if (Z == BEG)
10623 echo_area_buffer[0] = Qnil;
10624 return 0;
10625 }
10626
10627 /* Set the current message to STRING. */
10628
10629 static void
10630 set_message (Lisp_Object string)
10631 {
10632 eassert (STRINGP (string));
10633
10634 message_enable_multibyte = STRING_MULTIBYTE (string);
10635
10636 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10637 message_buf_print = 0;
10638 help_echo_showing_p = 0;
10639
10640 if (STRINGP (Vdebug_on_message)
10641 && STRINGP (string)
10642 && fast_string_match (Vdebug_on_message, string) >= 0)
10643 call_debugger (list2 (Qerror, string));
10644 }
10645
10646
10647 /* Helper function for set_message. First argument is ignored and second
10648 argument has the same meaning as for set_message.
10649 This function is called with the echo area buffer being current. */
10650
10651 static int
10652 set_message_1 (ptrdiff_t a1, Lisp_Object string)
10653 {
10654 eassert (STRINGP (string));
10655
10656 /* Change multibyteness of the echo buffer appropriately. */
10657 if (message_enable_multibyte
10658 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10659 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10660
10661 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10662 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10663 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10664
10665 /* Insert new message at BEG. */
10666 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10667
10668 /* This function takes care of single/multibyte conversion.
10669 We just have to ensure that the echo area buffer has the right
10670 setting of enable_multibyte_characters. */
10671 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
10672
10673 return 0;
10674 }
10675
10676
10677 /* Clear messages. CURRENT_P non-zero means clear the current
10678 message. LAST_DISPLAYED_P non-zero means clear the message
10679 last displayed. */
10680
10681 void
10682 clear_message (int current_p, int last_displayed_p)
10683 {
10684 if (current_p)
10685 {
10686 echo_area_buffer[0] = Qnil;
10687 message_cleared_p = 1;
10688 }
10689
10690 if (last_displayed_p)
10691 echo_area_buffer[1] = Qnil;
10692
10693 message_buf_print = 0;
10694 }
10695
10696 /* Clear garbaged frames.
10697
10698 This function is used where the old redisplay called
10699 redraw_garbaged_frames which in turn called redraw_frame which in
10700 turn called clear_frame. The call to clear_frame was a source of
10701 flickering. I believe a clear_frame is not necessary. It should
10702 suffice in the new redisplay to invalidate all current matrices,
10703 and ensure a complete redisplay of all windows. */
10704
10705 static void
10706 clear_garbaged_frames (void)
10707 {
10708 if (frame_garbaged)
10709 {
10710 Lisp_Object tail, frame;
10711 int changed_count = 0;
10712
10713 FOR_EACH_FRAME (tail, frame)
10714 {
10715 struct frame *f = XFRAME (frame);
10716
10717 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10718 {
10719 if (f->resized_p)
10720 {
10721 redraw_frame (f);
10722 f->force_flush_display_p = 1;
10723 }
10724 clear_current_matrices (f);
10725 changed_count++;
10726 f->garbaged = 0;
10727 f->resized_p = 0;
10728 }
10729 }
10730
10731 frame_garbaged = 0;
10732 if (changed_count)
10733 ++windows_or_buffers_changed;
10734 }
10735 }
10736
10737
10738 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10739 is non-zero update selected_frame. Value is non-zero if the
10740 mini-windows height has been changed. */
10741
10742 static int
10743 echo_area_display (int update_frame_p)
10744 {
10745 Lisp_Object mini_window;
10746 struct window *w;
10747 struct frame *f;
10748 int window_height_changed_p = 0;
10749 struct frame *sf = SELECTED_FRAME ();
10750
10751 mini_window = FRAME_MINIBUF_WINDOW (sf);
10752 w = XWINDOW (mini_window);
10753 f = XFRAME (WINDOW_FRAME (w));
10754
10755 /* Don't display if frame is invisible or not yet initialized. */
10756 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10757 return 0;
10758
10759 #ifdef HAVE_WINDOW_SYSTEM
10760 /* When Emacs starts, selected_frame may be the initial terminal
10761 frame. If we let this through, a message would be displayed on
10762 the terminal. */
10763 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10764 return 0;
10765 #endif /* HAVE_WINDOW_SYSTEM */
10766
10767 /* Redraw garbaged frames. */
10768 clear_garbaged_frames ();
10769
10770 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10771 {
10772 echo_area_window = mini_window;
10773 window_height_changed_p = display_echo_area (w);
10774 w->must_be_updated_p = 1;
10775
10776 /* Update the display, unless called from redisplay_internal.
10777 Also don't update the screen during redisplay itself. The
10778 update will happen at the end of redisplay, and an update
10779 here could cause confusion. */
10780 if (update_frame_p && !redisplaying_p)
10781 {
10782 int n = 0;
10783
10784 /* If the display update has been interrupted by pending
10785 input, update mode lines in the frame. Due to the
10786 pending input, it might have been that redisplay hasn't
10787 been called, so that mode lines above the echo area are
10788 garbaged. This looks odd, so we prevent it here. */
10789 if (!display_completed)
10790 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10791
10792 if (window_height_changed_p
10793 /* Don't do this if Emacs is shutting down. Redisplay
10794 needs to run hooks. */
10795 && !NILP (Vrun_hooks))
10796 {
10797 /* Must update other windows. Likewise as in other
10798 cases, don't let this update be interrupted by
10799 pending input. */
10800 ptrdiff_t count = SPECPDL_INDEX ();
10801 specbind (Qredisplay_dont_pause, Qt);
10802 windows_or_buffers_changed = 1;
10803 redisplay_internal ();
10804 unbind_to (count, Qnil);
10805 }
10806 else if (FRAME_WINDOW_P (f) && n == 0)
10807 {
10808 /* Window configuration is the same as before.
10809 Can do with a display update of the echo area,
10810 unless we displayed some mode lines. */
10811 update_single_window (w, 1);
10812 FRAME_RIF (f)->flush_display (f);
10813 }
10814 else
10815 update_frame (f, 1, 1);
10816
10817 /* If cursor is in the echo area, make sure that the next
10818 redisplay displays the minibuffer, so that the cursor will
10819 be replaced with what the minibuffer wants. */
10820 if (cursor_in_echo_area)
10821 ++windows_or_buffers_changed;
10822 }
10823 }
10824 else if (!EQ (mini_window, selected_window))
10825 windows_or_buffers_changed++;
10826
10827 /* Last displayed message is now the current message. */
10828 echo_area_buffer[1] = echo_area_buffer[0];
10829 /* Inform read_char that we're not echoing. */
10830 echo_message_buffer = Qnil;
10831
10832 /* Prevent redisplay optimization in redisplay_internal by resetting
10833 this_line_start_pos. This is done because the mini-buffer now
10834 displays the message instead of its buffer text. */
10835 if (EQ (mini_window, selected_window))
10836 CHARPOS (this_line_start_pos) = 0;
10837
10838 return window_height_changed_p;
10839 }
10840
10841 /* Nonzero if the current window's buffer is shown in more than one
10842 window and was modified since last redisplay. */
10843
10844 static int
10845 buffer_shared_and_changed (void)
10846 {
10847 return (buffer_window_count (current_buffer) > 1
10848 && UNCHANGED_MODIFIED < MODIFF);
10849 }
10850
10851 /* Nonzero if W's buffer was changed but not saved or Transient Mark mode
10852 is enabled and mark of W's buffer was changed since last W's update. */
10853
10854 static int
10855 window_buffer_changed (struct window *w)
10856 {
10857 struct buffer *b = XBUFFER (w->contents);
10858
10859 eassert (BUFFER_LIVE_P (b));
10860
10861 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star)
10862 || ((!NILP (Vtransient_mark_mode) && !NILP (BVAR (b, mark_active)))
10863 != (w->region_showing != 0)));
10864 }
10865
10866 /* Nonzero if W has %c in its mode line and mode line should be updated. */
10867
10868 static int
10869 mode_line_update_needed (struct window *w)
10870 {
10871 return (w->column_number_displayed != -1
10872 && !(PT == w->last_point && !window_outdated (w))
10873 && (w->column_number_displayed != current_column ()));
10874 }
10875
10876 /* Nonzero if window start of W is frozen and may not be changed during
10877 redisplay. */
10878
10879 static bool
10880 window_frozen_p (struct window *w)
10881 {
10882 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
10883 {
10884 Lisp_Object window;
10885
10886 XSETWINDOW (window, w);
10887 if (MINI_WINDOW_P (w))
10888 return 0;
10889 else if (EQ (window, selected_window))
10890 return 0;
10891 else if (MINI_WINDOW_P (XWINDOW (selected_window))
10892 && EQ (window, Vminibuf_scroll_window))
10893 /* This special window can't be frozen too. */
10894 return 0;
10895 else
10896 return 1;
10897 }
10898 return 0;
10899 }
10900
10901 /***********************************************************************
10902 Mode Lines and Frame Titles
10903 ***********************************************************************/
10904
10905 /* A buffer for constructing non-propertized mode-line strings and
10906 frame titles in it; allocated from the heap in init_xdisp and
10907 resized as needed in store_mode_line_noprop_char. */
10908
10909 static char *mode_line_noprop_buf;
10910
10911 /* The buffer's end, and a current output position in it. */
10912
10913 static char *mode_line_noprop_buf_end;
10914 static char *mode_line_noprop_ptr;
10915
10916 #define MODE_LINE_NOPROP_LEN(start) \
10917 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10918
10919 static enum {
10920 MODE_LINE_DISPLAY = 0,
10921 MODE_LINE_TITLE,
10922 MODE_LINE_NOPROP,
10923 MODE_LINE_STRING
10924 } mode_line_target;
10925
10926 /* Alist that caches the results of :propertize.
10927 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10928 static Lisp_Object mode_line_proptrans_alist;
10929
10930 /* List of strings making up the mode-line. */
10931 static Lisp_Object mode_line_string_list;
10932
10933 /* Base face property when building propertized mode line string. */
10934 static Lisp_Object mode_line_string_face;
10935 static Lisp_Object mode_line_string_face_prop;
10936
10937
10938 /* Unwind data for mode line strings */
10939
10940 static Lisp_Object Vmode_line_unwind_vector;
10941
10942 static Lisp_Object
10943 format_mode_line_unwind_data (struct frame *target_frame,
10944 struct buffer *obuf,
10945 Lisp_Object owin,
10946 int save_proptrans)
10947 {
10948 Lisp_Object vector, tmp;
10949
10950 /* Reduce consing by keeping one vector in
10951 Vwith_echo_area_save_vector. */
10952 vector = Vmode_line_unwind_vector;
10953 Vmode_line_unwind_vector = Qnil;
10954
10955 if (NILP (vector))
10956 vector = Fmake_vector (make_number (10), Qnil);
10957
10958 ASET (vector, 0, make_number (mode_line_target));
10959 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10960 ASET (vector, 2, mode_line_string_list);
10961 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10962 ASET (vector, 4, mode_line_string_face);
10963 ASET (vector, 5, mode_line_string_face_prop);
10964
10965 if (obuf)
10966 XSETBUFFER (tmp, obuf);
10967 else
10968 tmp = Qnil;
10969 ASET (vector, 6, tmp);
10970 ASET (vector, 7, owin);
10971 if (target_frame)
10972 {
10973 /* Similarly to `with-selected-window', if the operation selects
10974 a window on another frame, we must restore that frame's
10975 selected window, and (for a tty) the top-frame. */
10976 ASET (vector, 8, target_frame->selected_window);
10977 if (FRAME_TERMCAP_P (target_frame))
10978 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
10979 }
10980
10981 return vector;
10982 }
10983
10984 static void
10985 unwind_format_mode_line (Lisp_Object vector)
10986 {
10987 Lisp_Object old_window = AREF (vector, 7);
10988 Lisp_Object target_frame_window = AREF (vector, 8);
10989 Lisp_Object old_top_frame = AREF (vector, 9);
10990
10991 mode_line_target = XINT (AREF (vector, 0));
10992 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10993 mode_line_string_list = AREF (vector, 2);
10994 if (! EQ (AREF (vector, 3), Qt))
10995 mode_line_proptrans_alist = AREF (vector, 3);
10996 mode_line_string_face = AREF (vector, 4);
10997 mode_line_string_face_prop = AREF (vector, 5);
10998
10999 /* Select window before buffer, since it may change the buffer. */
11000 if (!NILP (old_window))
11001 {
11002 /* If the operation that we are unwinding had selected a window
11003 on a different frame, reset its frame-selected-window. For a
11004 text terminal, reset its top-frame if necessary. */
11005 if (!NILP (target_frame_window))
11006 {
11007 Lisp_Object frame
11008 = WINDOW_FRAME (XWINDOW (target_frame_window));
11009
11010 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11011 Fselect_window (target_frame_window, Qt);
11012
11013 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11014 Fselect_frame (old_top_frame, Qt);
11015 }
11016
11017 Fselect_window (old_window, Qt);
11018 }
11019
11020 if (!NILP (AREF (vector, 6)))
11021 {
11022 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11023 ASET (vector, 6, Qnil);
11024 }
11025
11026 Vmode_line_unwind_vector = vector;
11027 }
11028
11029
11030 /* Store a single character C for the frame title in mode_line_noprop_buf.
11031 Re-allocate mode_line_noprop_buf if necessary. */
11032
11033 static void
11034 store_mode_line_noprop_char (char c)
11035 {
11036 /* If output position has reached the end of the allocated buffer,
11037 increase the buffer's size. */
11038 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11039 {
11040 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11041 ptrdiff_t size = len;
11042 mode_line_noprop_buf =
11043 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11044 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11045 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11046 }
11047
11048 *mode_line_noprop_ptr++ = c;
11049 }
11050
11051
11052 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11053 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11054 characters that yield more columns than PRECISION; PRECISION <= 0
11055 means copy the whole string. Pad with spaces until FIELD_WIDTH
11056 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11057 pad. Called from display_mode_element when it is used to build a
11058 frame title. */
11059
11060 static int
11061 store_mode_line_noprop (const char *string, int field_width, int precision)
11062 {
11063 const unsigned char *str = (const unsigned char *) string;
11064 int n = 0;
11065 ptrdiff_t dummy, nbytes;
11066
11067 /* Copy at most PRECISION chars from STR. */
11068 nbytes = strlen (string);
11069 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11070 while (nbytes--)
11071 store_mode_line_noprop_char (*str++);
11072
11073 /* Fill up with spaces until FIELD_WIDTH reached. */
11074 while (field_width > 0
11075 && n < field_width)
11076 {
11077 store_mode_line_noprop_char (' ');
11078 ++n;
11079 }
11080
11081 return n;
11082 }
11083
11084 /***********************************************************************
11085 Frame Titles
11086 ***********************************************************************/
11087
11088 #ifdef HAVE_WINDOW_SYSTEM
11089
11090 /* Set the title of FRAME, if it has changed. The title format is
11091 Vicon_title_format if FRAME is iconified, otherwise it is
11092 frame_title_format. */
11093
11094 static void
11095 x_consider_frame_title (Lisp_Object frame)
11096 {
11097 struct frame *f = XFRAME (frame);
11098
11099 if (FRAME_WINDOW_P (f)
11100 || FRAME_MINIBUF_ONLY_P (f)
11101 || f->explicit_name)
11102 {
11103 /* Do we have more than one visible frame on this X display? */
11104 Lisp_Object tail, other_frame, fmt;
11105 ptrdiff_t title_start;
11106 char *title;
11107 ptrdiff_t len;
11108 struct it it;
11109 ptrdiff_t count = SPECPDL_INDEX ();
11110
11111 FOR_EACH_FRAME (tail, other_frame)
11112 {
11113 struct frame *tf = XFRAME (other_frame);
11114
11115 if (tf != f
11116 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11117 && !FRAME_MINIBUF_ONLY_P (tf)
11118 && !EQ (other_frame, tip_frame)
11119 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11120 break;
11121 }
11122
11123 /* Set global variable indicating that multiple frames exist. */
11124 multiple_frames = CONSP (tail);
11125
11126 /* Switch to the buffer of selected window of the frame. Set up
11127 mode_line_target so that display_mode_element will output into
11128 mode_line_noprop_buf; then display the title. */
11129 record_unwind_protect (unwind_format_mode_line,
11130 format_mode_line_unwind_data
11131 (f, current_buffer, selected_window, 0));
11132
11133 Fselect_window (f->selected_window, Qt);
11134 set_buffer_internal_1
11135 (XBUFFER (XWINDOW (f->selected_window)->contents));
11136 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11137
11138 mode_line_target = MODE_LINE_TITLE;
11139 title_start = MODE_LINE_NOPROP_LEN (0);
11140 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11141 NULL, DEFAULT_FACE_ID);
11142 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11143 len = MODE_LINE_NOPROP_LEN (title_start);
11144 title = mode_line_noprop_buf + title_start;
11145 unbind_to (count, Qnil);
11146
11147 /* Set the title only if it's changed. This avoids consing in
11148 the common case where it hasn't. (If it turns out that we've
11149 already wasted too much time by walking through the list with
11150 display_mode_element, then we might need to optimize at a
11151 higher level than this.) */
11152 if (! STRINGP (f->name)
11153 || SBYTES (f->name) != len
11154 || memcmp (title, SDATA (f->name), len) != 0)
11155 x_implicitly_set_name (f, make_string (title, len), Qnil);
11156 }
11157 }
11158
11159 #endif /* not HAVE_WINDOW_SYSTEM */
11160
11161 \f
11162 /***********************************************************************
11163 Menu Bars
11164 ***********************************************************************/
11165
11166
11167 /* Prepare for redisplay by updating menu-bar item lists when
11168 appropriate. This can call eval. */
11169
11170 void
11171 prepare_menu_bars (void)
11172 {
11173 int all_windows;
11174 struct gcpro gcpro1, gcpro2;
11175 struct frame *f;
11176 Lisp_Object tooltip_frame;
11177
11178 #ifdef HAVE_WINDOW_SYSTEM
11179 tooltip_frame = tip_frame;
11180 #else
11181 tooltip_frame = Qnil;
11182 #endif
11183
11184 /* Update all frame titles based on their buffer names, etc. We do
11185 this before the menu bars so that the buffer-menu will show the
11186 up-to-date frame titles. */
11187 #ifdef HAVE_WINDOW_SYSTEM
11188 if (windows_or_buffers_changed || update_mode_lines)
11189 {
11190 Lisp_Object tail, frame;
11191
11192 FOR_EACH_FRAME (tail, frame)
11193 {
11194 f = XFRAME (frame);
11195 if (!EQ (frame, tooltip_frame)
11196 && (FRAME_ICONIFIED_P (f)
11197 || FRAME_VISIBLE_P (f) == 1
11198 /* Exclude TTY frames that are obscured because they
11199 are not the top frame on their console. This is
11200 because x_consider_frame_title actually switches
11201 to the frame, which for TTY frames means it is
11202 marked as garbaged, and will be completely
11203 redrawn on the next redisplay cycle. This causes
11204 TTY frames to be completely redrawn, when there
11205 are more than one of them, even though nothing
11206 should be changed on display. */
11207 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11208 x_consider_frame_title (frame);
11209 }
11210 }
11211 #endif /* HAVE_WINDOW_SYSTEM */
11212
11213 /* Update the menu bar item lists, if appropriate. This has to be
11214 done before any actual redisplay or generation of display lines. */
11215 all_windows = (update_mode_lines
11216 || buffer_shared_and_changed ()
11217 || windows_or_buffers_changed);
11218 if (all_windows)
11219 {
11220 Lisp_Object tail, frame;
11221 ptrdiff_t count = SPECPDL_INDEX ();
11222 /* 1 means that update_menu_bar has run its hooks
11223 so any further calls to update_menu_bar shouldn't do so again. */
11224 int menu_bar_hooks_run = 0;
11225
11226 record_unwind_save_match_data ();
11227
11228 FOR_EACH_FRAME (tail, frame)
11229 {
11230 f = XFRAME (frame);
11231
11232 /* Ignore tooltip frame. */
11233 if (EQ (frame, tooltip_frame))
11234 continue;
11235
11236 /* If a window on this frame changed size, report that to
11237 the user and clear the size-change flag. */
11238 if (FRAME_WINDOW_SIZES_CHANGED (f))
11239 {
11240 Lisp_Object functions;
11241
11242 /* Clear flag first in case we get an error below. */
11243 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11244 functions = Vwindow_size_change_functions;
11245 GCPRO2 (tail, functions);
11246
11247 while (CONSP (functions))
11248 {
11249 if (!EQ (XCAR (functions), Qt))
11250 call1 (XCAR (functions), frame);
11251 functions = XCDR (functions);
11252 }
11253 UNGCPRO;
11254 }
11255
11256 GCPRO1 (tail);
11257 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11258 #ifdef HAVE_WINDOW_SYSTEM
11259 update_tool_bar (f, 0);
11260 #endif
11261 #ifdef HAVE_NS
11262 if (windows_or_buffers_changed
11263 && FRAME_NS_P (f))
11264 ns_set_doc_edited
11265 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->contents));
11266 #endif
11267 UNGCPRO;
11268 }
11269
11270 unbind_to (count, Qnil);
11271 }
11272 else
11273 {
11274 struct frame *sf = SELECTED_FRAME ();
11275 update_menu_bar (sf, 1, 0);
11276 #ifdef HAVE_WINDOW_SYSTEM
11277 update_tool_bar (sf, 1);
11278 #endif
11279 }
11280 }
11281
11282
11283 /* Update the menu bar item list for frame F. This has to be done
11284 before we start to fill in any display lines, because it can call
11285 eval.
11286
11287 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11288
11289 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11290 already ran the menu bar hooks for this redisplay, so there
11291 is no need to run them again. The return value is the
11292 updated value of this flag, to pass to the next call. */
11293
11294 static int
11295 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11296 {
11297 Lisp_Object window;
11298 register struct window *w;
11299
11300 /* If called recursively during a menu update, do nothing. This can
11301 happen when, for instance, an activate-menubar-hook causes a
11302 redisplay. */
11303 if (inhibit_menubar_update)
11304 return hooks_run;
11305
11306 window = FRAME_SELECTED_WINDOW (f);
11307 w = XWINDOW (window);
11308
11309 if (FRAME_WINDOW_P (f)
11310 ?
11311 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11312 || defined (HAVE_NS) || defined (USE_GTK)
11313 FRAME_EXTERNAL_MENU_BAR (f)
11314 #else
11315 FRAME_MENU_BAR_LINES (f) > 0
11316 #endif
11317 : FRAME_MENU_BAR_LINES (f) > 0)
11318 {
11319 /* If the user has switched buffers or windows, we need to
11320 recompute to reflect the new bindings. But we'll
11321 recompute when update_mode_lines is set too; that means
11322 that people can use force-mode-line-update to request
11323 that the menu bar be recomputed. The adverse effect on
11324 the rest of the redisplay algorithm is about the same as
11325 windows_or_buffers_changed anyway. */
11326 if (windows_or_buffers_changed
11327 /* This used to test w->update_mode_line, but we believe
11328 there is no need to recompute the menu in that case. */
11329 || update_mode_lines
11330 || window_buffer_changed (w))
11331 {
11332 struct buffer *prev = current_buffer;
11333 ptrdiff_t count = SPECPDL_INDEX ();
11334
11335 specbind (Qinhibit_menubar_update, Qt);
11336
11337 set_buffer_internal_1 (XBUFFER (w->contents));
11338 if (save_match_data)
11339 record_unwind_save_match_data ();
11340 if (NILP (Voverriding_local_map_menu_flag))
11341 {
11342 specbind (Qoverriding_terminal_local_map, Qnil);
11343 specbind (Qoverriding_local_map, Qnil);
11344 }
11345
11346 if (!hooks_run)
11347 {
11348 /* Run the Lucid hook. */
11349 safe_run_hooks (Qactivate_menubar_hook);
11350
11351 /* If it has changed current-menubar from previous value,
11352 really recompute the menu-bar from the value. */
11353 if (! NILP (Vlucid_menu_bar_dirty_flag))
11354 call0 (Qrecompute_lucid_menubar);
11355
11356 safe_run_hooks (Qmenu_bar_update_hook);
11357
11358 hooks_run = 1;
11359 }
11360
11361 XSETFRAME (Vmenu_updating_frame, f);
11362 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11363
11364 /* Redisplay the menu bar in case we changed it. */
11365 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11366 || defined (HAVE_NS) || defined (USE_GTK)
11367 if (FRAME_WINDOW_P (f))
11368 {
11369 #if defined (HAVE_NS)
11370 /* All frames on Mac OS share the same menubar. So only
11371 the selected frame should be allowed to set it. */
11372 if (f == SELECTED_FRAME ())
11373 #endif
11374 set_frame_menubar (f, 0, 0);
11375 }
11376 else
11377 /* On a terminal screen, the menu bar is an ordinary screen
11378 line, and this makes it get updated. */
11379 w->update_mode_line = 1;
11380 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11381 /* In the non-toolkit version, the menu bar is an ordinary screen
11382 line, and this makes it get updated. */
11383 w->update_mode_line = 1;
11384 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11385
11386 unbind_to (count, Qnil);
11387 set_buffer_internal_1 (prev);
11388 }
11389 }
11390
11391 return hooks_run;
11392 }
11393
11394
11395 \f
11396 /***********************************************************************
11397 Output Cursor
11398 ***********************************************************************/
11399
11400 #ifdef HAVE_WINDOW_SYSTEM
11401
11402 /* EXPORT:
11403 Nominal cursor position -- where to draw output.
11404 HPOS and VPOS are window relative glyph matrix coordinates.
11405 X and Y are window relative pixel coordinates. */
11406
11407 struct cursor_pos output_cursor;
11408
11409
11410 /* EXPORT:
11411 Set the global variable output_cursor to CURSOR. All cursor
11412 positions are relative to currently updated window. */
11413
11414 void
11415 set_output_cursor (struct cursor_pos *cursor)
11416 {
11417 output_cursor.hpos = cursor->hpos;
11418 output_cursor.vpos = cursor->vpos;
11419 output_cursor.x = cursor->x;
11420 output_cursor.y = cursor->y;
11421 }
11422
11423
11424 /* EXPORT for RIF:
11425 Set a nominal cursor position.
11426
11427 HPOS and VPOS are column/row positions in a window glyph matrix.
11428 X and Y are window text area relative pixel positions.
11429
11430 This is always done during window update, so the position is the
11431 future output cursor position for currently updated window W.
11432 NOTE: W is used only to check whether this function is called
11433 in a consistent manner via the redisplay interface. */
11434
11435 void
11436 x_cursor_to (struct window *w, int vpos, int hpos, int y, int x)
11437 {
11438 eassert (w);
11439
11440 /* Set the output cursor. */
11441 output_cursor.hpos = hpos;
11442 output_cursor.vpos = vpos;
11443 output_cursor.x = x;
11444 output_cursor.y = y;
11445 }
11446
11447 #endif /* HAVE_WINDOW_SYSTEM */
11448
11449 \f
11450 /***********************************************************************
11451 Tool-bars
11452 ***********************************************************************/
11453
11454 #ifdef HAVE_WINDOW_SYSTEM
11455
11456 /* Where the mouse was last time we reported a mouse event. */
11457
11458 struct frame *last_mouse_frame;
11459
11460 /* Tool-bar item index of the item on which a mouse button was pressed
11461 or -1. */
11462
11463 int last_tool_bar_item;
11464
11465 /* Select `frame' temporarily without running all the code in
11466 do_switch_frame.
11467 FIXME: Maybe do_switch_frame should be trimmed down similarly
11468 when `norecord' is set. */
11469 static void
11470 fast_set_selected_frame (Lisp_Object frame)
11471 {
11472 if (!EQ (selected_frame, frame))
11473 {
11474 selected_frame = frame;
11475 selected_window = XFRAME (frame)->selected_window;
11476 }
11477 }
11478
11479 /* Update the tool-bar item list for frame F. This has to be done
11480 before we start to fill in any display lines. Called from
11481 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11482 and restore it here. */
11483
11484 static void
11485 update_tool_bar (struct frame *f, int save_match_data)
11486 {
11487 #if defined (USE_GTK) || defined (HAVE_NS)
11488 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11489 #else
11490 int do_update = WINDOWP (f->tool_bar_window)
11491 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11492 #endif
11493
11494 if (do_update)
11495 {
11496 Lisp_Object window;
11497 struct window *w;
11498
11499 window = FRAME_SELECTED_WINDOW (f);
11500 w = XWINDOW (window);
11501
11502 /* If the user has switched buffers or windows, we need to
11503 recompute to reflect the new bindings. But we'll
11504 recompute when update_mode_lines is set too; that means
11505 that people can use force-mode-line-update to request
11506 that the menu bar be recomputed. The adverse effect on
11507 the rest of the redisplay algorithm is about the same as
11508 windows_or_buffers_changed anyway. */
11509 if (windows_or_buffers_changed
11510 || w->update_mode_line
11511 || update_mode_lines
11512 || window_buffer_changed (w))
11513 {
11514 struct buffer *prev = current_buffer;
11515 ptrdiff_t count = SPECPDL_INDEX ();
11516 Lisp_Object frame, new_tool_bar;
11517 int new_n_tool_bar;
11518 struct gcpro gcpro1;
11519
11520 /* Set current_buffer to the buffer of the selected
11521 window of the frame, so that we get the right local
11522 keymaps. */
11523 set_buffer_internal_1 (XBUFFER (w->contents));
11524
11525 /* Save match data, if we must. */
11526 if (save_match_data)
11527 record_unwind_save_match_data ();
11528
11529 /* Make sure that we don't accidentally use bogus keymaps. */
11530 if (NILP (Voverriding_local_map_menu_flag))
11531 {
11532 specbind (Qoverriding_terminal_local_map, Qnil);
11533 specbind (Qoverriding_local_map, Qnil);
11534 }
11535
11536 GCPRO1 (new_tool_bar);
11537
11538 /* We must temporarily set the selected frame to this frame
11539 before calling tool_bar_items, because the calculation of
11540 the tool-bar keymap uses the selected frame (see
11541 `tool-bar-make-keymap' in tool-bar.el). */
11542 eassert (EQ (selected_window,
11543 /* Since we only explicitly preserve selected_frame,
11544 check that selected_window would be redundant. */
11545 XFRAME (selected_frame)->selected_window));
11546 record_unwind_protect (fast_set_selected_frame, selected_frame);
11547 XSETFRAME (frame, f);
11548 fast_set_selected_frame (frame);
11549
11550 /* Build desired tool-bar items from keymaps. */
11551 new_tool_bar
11552 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11553 &new_n_tool_bar);
11554
11555 /* Redisplay the tool-bar if we changed it. */
11556 if (new_n_tool_bar != f->n_tool_bar_items
11557 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11558 {
11559 /* Redisplay that happens asynchronously due to an expose event
11560 may access f->tool_bar_items. Make sure we update both
11561 variables within BLOCK_INPUT so no such event interrupts. */
11562 block_input ();
11563 fset_tool_bar_items (f, new_tool_bar);
11564 f->n_tool_bar_items = new_n_tool_bar;
11565 w->update_mode_line = 1;
11566 unblock_input ();
11567 }
11568
11569 UNGCPRO;
11570
11571 unbind_to (count, Qnil);
11572 set_buffer_internal_1 (prev);
11573 }
11574 }
11575 }
11576
11577
11578 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11579 F's desired tool-bar contents. F->tool_bar_items must have
11580 been set up previously by calling prepare_menu_bars. */
11581
11582 static void
11583 build_desired_tool_bar_string (struct frame *f)
11584 {
11585 int i, size, size_needed;
11586 struct gcpro gcpro1, gcpro2, gcpro3;
11587 Lisp_Object image, plist, props;
11588
11589 image = plist = props = Qnil;
11590 GCPRO3 (image, plist, props);
11591
11592 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11593 Otherwise, make a new string. */
11594
11595 /* The size of the string we might be able to reuse. */
11596 size = (STRINGP (f->desired_tool_bar_string)
11597 ? SCHARS (f->desired_tool_bar_string)
11598 : 0);
11599
11600 /* We need one space in the string for each image. */
11601 size_needed = f->n_tool_bar_items;
11602
11603 /* Reuse f->desired_tool_bar_string, if possible. */
11604 if (size < size_needed || NILP (f->desired_tool_bar_string))
11605 fset_desired_tool_bar_string
11606 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11607 else
11608 {
11609 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11610 Fremove_text_properties (make_number (0), make_number (size),
11611 props, f->desired_tool_bar_string);
11612 }
11613
11614 /* Put a `display' property on the string for the images to display,
11615 put a `menu_item' property on tool-bar items with a value that
11616 is the index of the item in F's tool-bar item vector. */
11617 for (i = 0; i < f->n_tool_bar_items; ++i)
11618 {
11619 #define PROP(IDX) \
11620 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11621
11622 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11623 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11624 int hmargin, vmargin, relief, idx, end;
11625
11626 /* If image is a vector, choose the image according to the
11627 button state. */
11628 image = PROP (TOOL_BAR_ITEM_IMAGES);
11629 if (VECTORP (image))
11630 {
11631 if (enabled_p)
11632 idx = (selected_p
11633 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11634 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11635 else
11636 idx = (selected_p
11637 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11638 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11639
11640 eassert (ASIZE (image) >= idx);
11641 image = AREF (image, idx);
11642 }
11643 else
11644 idx = -1;
11645
11646 /* Ignore invalid image specifications. */
11647 if (!valid_image_p (image))
11648 continue;
11649
11650 /* Display the tool-bar button pressed, or depressed. */
11651 plist = Fcopy_sequence (XCDR (image));
11652
11653 /* Compute margin and relief to draw. */
11654 relief = (tool_bar_button_relief >= 0
11655 ? tool_bar_button_relief
11656 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11657 hmargin = vmargin = relief;
11658
11659 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11660 INT_MAX - max (hmargin, vmargin)))
11661 {
11662 hmargin += XFASTINT (Vtool_bar_button_margin);
11663 vmargin += XFASTINT (Vtool_bar_button_margin);
11664 }
11665 else if (CONSP (Vtool_bar_button_margin))
11666 {
11667 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11668 INT_MAX - hmargin))
11669 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11670
11671 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11672 INT_MAX - vmargin))
11673 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11674 }
11675
11676 if (auto_raise_tool_bar_buttons_p)
11677 {
11678 /* Add a `:relief' property to the image spec if the item is
11679 selected. */
11680 if (selected_p)
11681 {
11682 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11683 hmargin -= relief;
11684 vmargin -= relief;
11685 }
11686 }
11687 else
11688 {
11689 /* If image is selected, display it pressed, i.e. with a
11690 negative relief. If it's not selected, display it with a
11691 raised relief. */
11692 plist = Fplist_put (plist, QCrelief,
11693 (selected_p
11694 ? make_number (-relief)
11695 : make_number (relief)));
11696 hmargin -= relief;
11697 vmargin -= relief;
11698 }
11699
11700 /* Put a margin around the image. */
11701 if (hmargin || vmargin)
11702 {
11703 if (hmargin == vmargin)
11704 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11705 else
11706 plist = Fplist_put (plist, QCmargin,
11707 Fcons (make_number (hmargin),
11708 make_number (vmargin)));
11709 }
11710
11711 /* If button is not enabled, and we don't have special images
11712 for the disabled state, make the image appear disabled by
11713 applying an appropriate algorithm to it. */
11714 if (!enabled_p && idx < 0)
11715 plist = Fplist_put (plist, QCconversion, Qdisabled);
11716
11717 /* Put a `display' text property on the string for the image to
11718 display. Put a `menu-item' property on the string that gives
11719 the start of this item's properties in the tool-bar items
11720 vector. */
11721 image = Fcons (Qimage, plist);
11722 props = list4 (Qdisplay, image,
11723 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11724
11725 /* Let the last image hide all remaining spaces in the tool bar
11726 string. The string can be longer than needed when we reuse a
11727 previous string. */
11728 if (i + 1 == f->n_tool_bar_items)
11729 end = SCHARS (f->desired_tool_bar_string);
11730 else
11731 end = i + 1;
11732 Fadd_text_properties (make_number (i), make_number (end),
11733 props, f->desired_tool_bar_string);
11734 #undef PROP
11735 }
11736
11737 UNGCPRO;
11738 }
11739
11740
11741 /* Display one line of the tool-bar of frame IT->f.
11742
11743 HEIGHT specifies the desired height of the tool-bar line.
11744 If the actual height of the glyph row is less than HEIGHT, the
11745 row's height is increased to HEIGHT, and the icons are centered
11746 vertically in the new height.
11747
11748 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11749 count a final empty row in case the tool-bar width exactly matches
11750 the window width.
11751 */
11752
11753 static void
11754 display_tool_bar_line (struct it *it, int height)
11755 {
11756 struct glyph_row *row = it->glyph_row;
11757 int max_x = it->last_visible_x;
11758 struct glyph *last;
11759
11760 prepare_desired_row (row);
11761 row->y = it->current_y;
11762
11763 /* Note that this isn't made use of if the face hasn't a box,
11764 so there's no need to check the face here. */
11765 it->start_of_box_run_p = 1;
11766
11767 while (it->current_x < max_x)
11768 {
11769 int x, n_glyphs_before, i, nglyphs;
11770 struct it it_before;
11771
11772 /* Get the next display element. */
11773 if (!get_next_display_element (it))
11774 {
11775 /* Don't count empty row if we are counting needed tool-bar lines. */
11776 if (height < 0 && !it->hpos)
11777 return;
11778 break;
11779 }
11780
11781 /* Produce glyphs. */
11782 n_glyphs_before = row->used[TEXT_AREA];
11783 it_before = *it;
11784
11785 PRODUCE_GLYPHS (it);
11786
11787 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11788 i = 0;
11789 x = it_before.current_x;
11790 while (i < nglyphs)
11791 {
11792 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11793
11794 if (x + glyph->pixel_width > max_x)
11795 {
11796 /* Glyph doesn't fit on line. Backtrack. */
11797 row->used[TEXT_AREA] = n_glyphs_before;
11798 *it = it_before;
11799 /* If this is the only glyph on this line, it will never fit on the
11800 tool-bar, so skip it. But ensure there is at least one glyph,
11801 so we don't accidentally disable the tool-bar. */
11802 if (n_glyphs_before == 0
11803 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11804 break;
11805 goto out;
11806 }
11807
11808 ++it->hpos;
11809 x += glyph->pixel_width;
11810 ++i;
11811 }
11812
11813 /* Stop at line end. */
11814 if (ITERATOR_AT_END_OF_LINE_P (it))
11815 break;
11816
11817 set_iterator_to_next (it, 1);
11818 }
11819
11820 out:;
11821
11822 row->displays_text_p = row->used[TEXT_AREA] != 0;
11823
11824 /* Use default face for the border below the tool bar.
11825
11826 FIXME: When auto-resize-tool-bars is grow-only, there is
11827 no additional border below the possibly empty tool-bar lines.
11828 So to make the extra empty lines look "normal", we have to
11829 use the tool-bar face for the border too. */
11830 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
11831 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11832 it->face_id = DEFAULT_FACE_ID;
11833
11834 extend_face_to_end_of_line (it);
11835 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11836 last->right_box_line_p = 1;
11837 if (last == row->glyphs[TEXT_AREA])
11838 last->left_box_line_p = 1;
11839
11840 /* Make line the desired height and center it vertically. */
11841 if ((height -= it->max_ascent + it->max_descent) > 0)
11842 {
11843 /* Don't add more than one line height. */
11844 height %= FRAME_LINE_HEIGHT (it->f);
11845 it->max_ascent += height / 2;
11846 it->max_descent += (height + 1) / 2;
11847 }
11848
11849 compute_line_metrics (it);
11850
11851 /* If line is empty, make it occupy the rest of the tool-bar. */
11852 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
11853 {
11854 row->height = row->phys_height = it->last_visible_y - row->y;
11855 row->visible_height = row->height;
11856 row->ascent = row->phys_ascent = 0;
11857 row->extra_line_spacing = 0;
11858 }
11859
11860 row->full_width_p = 1;
11861 row->continued_p = 0;
11862 row->truncated_on_left_p = 0;
11863 row->truncated_on_right_p = 0;
11864
11865 it->current_x = it->hpos = 0;
11866 it->current_y += row->height;
11867 ++it->vpos;
11868 ++it->glyph_row;
11869 }
11870
11871
11872 /* Max tool-bar height. */
11873
11874 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11875 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11876
11877 /* Value is the number of screen lines needed to make all tool-bar
11878 items of frame F visible. The number of actual rows needed is
11879 returned in *N_ROWS if non-NULL. */
11880
11881 static int
11882 tool_bar_lines_needed (struct frame *f, int *n_rows)
11883 {
11884 struct window *w = XWINDOW (f->tool_bar_window);
11885 struct it it;
11886 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11887 the desired matrix, so use (unused) mode-line row as temporary row to
11888 avoid destroying the first tool-bar row. */
11889 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11890
11891 /* Initialize an iterator for iteration over
11892 F->desired_tool_bar_string in the tool-bar window of frame F. */
11893 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11894 it.first_visible_x = 0;
11895 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11896 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11897 it.paragraph_embedding = L2R;
11898
11899 while (!ITERATOR_AT_END_P (&it))
11900 {
11901 clear_glyph_row (temp_row);
11902 it.glyph_row = temp_row;
11903 display_tool_bar_line (&it, -1);
11904 }
11905 clear_glyph_row (temp_row);
11906
11907 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11908 if (n_rows)
11909 *n_rows = it.vpos > 0 ? it.vpos : -1;
11910
11911 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11912 }
11913
11914
11915 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11916 0, 1, 0,
11917 doc: /* Return the number of lines occupied by the tool bar of FRAME.
11918 If FRAME is nil or omitted, use the selected frame. */)
11919 (Lisp_Object frame)
11920 {
11921 struct frame *f = decode_any_frame (frame);
11922 struct window *w;
11923 int nlines = 0;
11924
11925 if (WINDOWP (f->tool_bar_window)
11926 && (w = XWINDOW (f->tool_bar_window),
11927 WINDOW_TOTAL_LINES (w) > 0))
11928 {
11929 update_tool_bar (f, 1);
11930 if (f->n_tool_bar_items)
11931 {
11932 build_desired_tool_bar_string (f);
11933 nlines = tool_bar_lines_needed (f, NULL);
11934 }
11935 }
11936
11937 return make_number (nlines);
11938 }
11939
11940
11941 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11942 height should be changed. */
11943
11944 static int
11945 redisplay_tool_bar (struct frame *f)
11946 {
11947 struct window *w;
11948 struct it it;
11949 struct glyph_row *row;
11950
11951 #if defined (USE_GTK) || defined (HAVE_NS)
11952 if (FRAME_EXTERNAL_TOOL_BAR (f))
11953 update_frame_tool_bar (f);
11954 return 0;
11955 #endif
11956
11957 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11958 do anything. This means you must start with tool-bar-lines
11959 non-zero to get the auto-sizing effect. Or in other words, you
11960 can turn off tool-bars by specifying tool-bar-lines zero. */
11961 if (!WINDOWP (f->tool_bar_window)
11962 || (w = XWINDOW (f->tool_bar_window),
11963 WINDOW_TOTAL_LINES (w) == 0))
11964 return 0;
11965
11966 /* Set up an iterator for the tool-bar window. */
11967 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11968 it.first_visible_x = 0;
11969 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11970 row = it.glyph_row;
11971
11972 /* Build a string that represents the contents of the tool-bar. */
11973 build_desired_tool_bar_string (f);
11974 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11975 /* FIXME: This should be controlled by a user option. But it
11976 doesn't make sense to have an R2L tool bar if the menu bar cannot
11977 be drawn also R2L, and making the menu bar R2L is tricky due
11978 toolkit-specific code that implements it. If an R2L tool bar is
11979 ever supported, display_tool_bar_line should also be augmented to
11980 call unproduce_glyphs like display_line and display_string
11981 do. */
11982 it.paragraph_embedding = L2R;
11983
11984 if (f->n_tool_bar_rows == 0)
11985 {
11986 int nlines;
11987
11988 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11989 nlines != WINDOW_TOTAL_LINES (w)))
11990 {
11991 Lisp_Object frame;
11992 int old_height = WINDOW_TOTAL_LINES (w);
11993
11994 XSETFRAME (frame, f);
11995 Fmodify_frame_parameters (frame,
11996 list1 (Fcons (Qtool_bar_lines,
11997 make_number (nlines))));
11998 if (WINDOW_TOTAL_LINES (w) != old_height)
11999 {
12000 clear_glyph_matrix (w->desired_matrix);
12001 fonts_changed_p = 1;
12002 return 1;
12003 }
12004 }
12005 }
12006
12007 /* Display as many lines as needed to display all tool-bar items. */
12008
12009 if (f->n_tool_bar_rows > 0)
12010 {
12011 int border, rows, height, extra;
12012
12013 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12014 border = XINT (Vtool_bar_border);
12015 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12016 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12017 else if (EQ (Vtool_bar_border, Qborder_width))
12018 border = f->border_width;
12019 else
12020 border = 0;
12021 if (border < 0)
12022 border = 0;
12023
12024 rows = f->n_tool_bar_rows;
12025 height = max (1, (it.last_visible_y - border) / rows);
12026 extra = it.last_visible_y - border - height * rows;
12027
12028 while (it.current_y < it.last_visible_y)
12029 {
12030 int h = 0;
12031 if (extra > 0 && rows-- > 0)
12032 {
12033 h = (extra + rows - 1) / rows;
12034 extra -= h;
12035 }
12036 display_tool_bar_line (&it, height + h);
12037 }
12038 }
12039 else
12040 {
12041 while (it.current_y < it.last_visible_y)
12042 display_tool_bar_line (&it, 0);
12043 }
12044
12045 /* It doesn't make much sense to try scrolling in the tool-bar
12046 window, so don't do it. */
12047 w->desired_matrix->no_scrolling_p = 1;
12048 w->must_be_updated_p = 1;
12049
12050 if (!NILP (Vauto_resize_tool_bars))
12051 {
12052 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12053 int change_height_p = 0;
12054
12055 /* If we couldn't display everything, change the tool-bar's
12056 height if there is room for more. */
12057 if (IT_STRING_CHARPOS (it) < it.end_charpos
12058 && it.current_y < max_tool_bar_height)
12059 change_height_p = 1;
12060
12061 row = it.glyph_row - 1;
12062
12063 /* If there are blank lines at the end, except for a partially
12064 visible blank line at the end that is smaller than
12065 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12066 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12067 && row->height >= FRAME_LINE_HEIGHT (f))
12068 change_height_p = 1;
12069
12070 /* If row displays tool-bar items, but is partially visible,
12071 change the tool-bar's height. */
12072 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12073 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12074 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12075 change_height_p = 1;
12076
12077 /* Resize windows as needed by changing the `tool-bar-lines'
12078 frame parameter. */
12079 if (change_height_p)
12080 {
12081 Lisp_Object frame;
12082 int old_height = WINDOW_TOTAL_LINES (w);
12083 int nrows;
12084 int nlines = tool_bar_lines_needed (f, &nrows);
12085
12086 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12087 && !f->minimize_tool_bar_window_p)
12088 ? (nlines > old_height)
12089 : (nlines != old_height));
12090 f->minimize_tool_bar_window_p = 0;
12091
12092 if (change_height_p)
12093 {
12094 XSETFRAME (frame, f);
12095 Fmodify_frame_parameters (frame,
12096 list1 (Fcons (Qtool_bar_lines,
12097 make_number (nlines))));
12098 if (WINDOW_TOTAL_LINES (w) != old_height)
12099 {
12100 clear_glyph_matrix (w->desired_matrix);
12101 f->n_tool_bar_rows = nrows;
12102 fonts_changed_p = 1;
12103 return 1;
12104 }
12105 }
12106 }
12107 }
12108
12109 f->minimize_tool_bar_window_p = 0;
12110 return 0;
12111 }
12112
12113
12114 /* Get information about the tool-bar item which is displayed in GLYPH
12115 on frame F. Return in *PROP_IDX the index where tool-bar item
12116 properties start in F->tool_bar_items. Value is zero if
12117 GLYPH doesn't display a tool-bar item. */
12118
12119 static int
12120 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12121 {
12122 Lisp_Object prop;
12123 int success_p;
12124 int charpos;
12125
12126 /* This function can be called asynchronously, which means we must
12127 exclude any possibility that Fget_text_property signals an
12128 error. */
12129 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12130 charpos = max (0, charpos);
12131
12132 /* Get the text property `menu-item' at pos. The value of that
12133 property is the start index of this item's properties in
12134 F->tool_bar_items. */
12135 prop = Fget_text_property (make_number (charpos),
12136 Qmenu_item, f->current_tool_bar_string);
12137 if (INTEGERP (prop))
12138 {
12139 *prop_idx = XINT (prop);
12140 success_p = 1;
12141 }
12142 else
12143 success_p = 0;
12144
12145 return success_p;
12146 }
12147
12148 \f
12149 /* Get information about the tool-bar item at position X/Y on frame F.
12150 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12151 the current matrix of the tool-bar window of F, or NULL if not
12152 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12153 item in F->tool_bar_items. Value is
12154
12155 -1 if X/Y is not on a tool-bar item
12156 0 if X/Y is on the same item that was highlighted before.
12157 1 otherwise. */
12158
12159 static int
12160 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12161 int *hpos, int *vpos, int *prop_idx)
12162 {
12163 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12164 struct window *w = XWINDOW (f->tool_bar_window);
12165 int area;
12166
12167 /* Find the glyph under X/Y. */
12168 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12169 if (*glyph == NULL)
12170 return -1;
12171
12172 /* Get the start of this tool-bar item's properties in
12173 f->tool_bar_items. */
12174 if (!tool_bar_item_info (f, *glyph, prop_idx))
12175 return -1;
12176
12177 /* Is mouse on the highlighted item? */
12178 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12179 && *vpos >= hlinfo->mouse_face_beg_row
12180 && *vpos <= hlinfo->mouse_face_end_row
12181 && (*vpos > hlinfo->mouse_face_beg_row
12182 || *hpos >= hlinfo->mouse_face_beg_col)
12183 && (*vpos < hlinfo->mouse_face_end_row
12184 || *hpos < hlinfo->mouse_face_end_col
12185 || hlinfo->mouse_face_past_end))
12186 return 0;
12187
12188 return 1;
12189 }
12190
12191
12192 /* EXPORT:
12193 Handle mouse button event on the tool-bar of frame F, at
12194 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12195 0 for button release. MODIFIERS is event modifiers for button
12196 release. */
12197
12198 void
12199 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12200 int modifiers)
12201 {
12202 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12203 struct window *w = XWINDOW (f->tool_bar_window);
12204 int hpos, vpos, prop_idx;
12205 struct glyph *glyph;
12206 Lisp_Object enabled_p;
12207 int ts;
12208
12209 /* If not on the highlighted tool-bar item, and mouse-highlight is
12210 non-nil, return. This is so we generate the tool-bar button
12211 click only when the mouse button is released on the same item as
12212 where it was pressed. However, when mouse-highlight is disabled,
12213 generate the click when the button is released regardless of the
12214 highlight, since tool-bar items are not highlighted in that
12215 case. */
12216 frame_to_window_pixel_xy (w, &x, &y);
12217 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12218 if (ts == -1
12219 || (ts != 0 && !NILP (Vmouse_highlight)))
12220 return;
12221
12222 /* When mouse-highlight is off, generate the click for the item
12223 where the button was pressed, disregarding where it was
12224 released. */
12225 if (NILP (Vmouse_highlight) && !down_p)
12226 prop_idx = last_tool_bar_item;
12227
12228 /* If item is disabled, do nothing. */
12229 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12230 if (NILP (enabled_p))
12231 return;
12232
12233 if (down_p)
12234 {
12235 /* Show item in pressed state. */
12236 if (!NILP (Vmouse_highlight))
12237 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12238 last_tool_bar_item = prop_idx;
12239 }
12240 else
12241 {
12242 Lisp_Object key, frame;
12243 struct input_event event;
12244 EVENT_INIT (event);
12245
12246 /* Show item in released state. */
12247 if (!NILP (Vmouse_highlight))
12248 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12249
12250 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12251
12252 XSETFRAME (frame, f);
12253 event.kind = TOOL_BAR_EVENT;
12254 event.frame_or_window = frame;
12255 event.arg = frame;
12256 kbd_buffer_store_event (&event);
12257
12258 event.kind = TOOL_BAR_EVENT;
12259 event.frame_or_window = frame;
12260 event.arg = key;
12261 event.modifiers = modifiers;
12262 kbd_buffer_store_event (&event);
12263 last_tool_bar_item = -1;
12264 }
12265 }
12266
12267
12268 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12269 tool-bar window-relative coordinates X/Y. Called from
12270 note_mouse_highlight. */
12271
12272 static void
12273 note_tool_bar_highlight (struct frame *f, int x, int y)
12274 {
12275 Lisp_Object window = f->tool_bar_window;
12276 struct window *w = XWINDOW (window);
12277 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12278 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12279 int hpos, vpos;
12280 struct glyph *glyph;
12281 struct glyph_row *row;
12282 int i;
12283 Lisp_Object enabled_p;
12284 int prop_idx;
12285 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12286 int mouse_down_p, rc;
12287
12288 /* Function note_mouse_highlight is called with negative X/Y
12289 values when mouse moves outside of the frame. */
12290 if (x <= 0 || y <= 0)
12291 {
12292 clear_mouse_face (hlinfo);
12293 return;
12294 }
12295
12296 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12297 if (rc < 0)
12298 {
12299 /* Not on tool-bar item. */
12300 clear_mouse_face (hlinfo);
12301 return;
12302 }
12303 else if (rc == 0)
12304 /* On same tool-bar item as before. */
12305 goto set_help_echo;
12306
12307 clear_mouse_face (hlinfo);
12308
12309 /* Mouse is down, but on different tool-bar item? */
12310 mouse_down_p = (dpyinfo->grabbed
12311 && f == last_mouse_frame
12312 && FRAME_LIVE_P (f));
12313 if (mouse_down_p
12314 && last_tool_bar_item != prop_idx)
12315 return;
12316
12317 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12318
12319 /* If tool-bar item is not enabled, don't highlight it. */
12320 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12321 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12322 {
12323 /* Compute the x-position of the glyph. In front and past the
12324 image is a space. We include this in the highlighted area. */
12325 row = MATRIX_ROW (w->current_matrix, vpos);
12326 for (i = x = 0; i < hpos; ++i)
12327 x += row->glyphs[TEXT_AREA][i].pixel_width;
12328
12329 /* Record this as the current active region. */
12330 hlinfo->mouse_face_beg_col = hpos;
12331 hlinfo->mouse_face_beg_row = vpos;
12332 hlinfo->mouse_face_beg_x = x;
12333 hlinfo->mouse_face_beg_y = row->y;
12334 hlinfo->mouse_face_past_end = 0;
12335
12336 hlinfo->mouse_face_end_col = hpos + 1;
12337 hlinfo->mouse_face_end_row = vpos;
12338 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12339 hlinfo->mouse_face_end_y = row->y;
12340 hlinfo->mouse_face_window = window;
12341 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12342
12343 /* Display it as active. */
12344 show_mouse_face (hlinfo, draw);
12345 }
12346
12347 set_help_echo:
12348
12349 /* Set help_echo_string to a help string to display for this tool-bar item.
12350 XTread_socket does the rest. */
12351 help_echo_object = help_echo_window = Qnil;
12352 help_echo_pos = -1;
12353 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12354 if (NILP (help_echo_string))
12355 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12356 }
12357
12358 #endif /* HAVE_WINDOW_SYSTEM */
12359
12360
12361 \f
12362 /************************************************************************
12363 Horizontal scrolling
12364 ************************************************************************/
12365
12366 static int hscroll_window_tree (Lisp_Object);
12367 static int hscroll_windows (Lisp_Object);
12368
12369 /* For all leaf windows in the window tree rooted at WINDOW, set their
12370 hscroll value so that PT is (i) visible in the window, and (ii) so
12371 that it is not within a certain margin at the window's left and
12372 right border. Value is non-zero if any window's hscroll has been
12373 changed. */
12374
12375 static int
12376 hscroll_window_tree (Lisp_Object window)
12377 {
12378 int hscrolled_p = 0;
12379 int hscroll_relative_p = FLOATP (Vhscroll_step);
12380 int hscroll_step_abs = 0;
12381 double hscroll_step_rel = 0;
12382
12383 if (hscroll_relative_p)
12384 {
12385 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12386 if (hscroll_step_rel < 0)
12387 {
12388 hscroll_relative_p = 0;
12389 hscroll_step_abs = 0;
12390 }
12391 }
12392 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12393 {
12394 hscroll_step_abs = XINT (Vhscroll_step);
12395 if (hscroll_step_abs < 0)
12396 hscroll_step_abs = 0;
12397 }
12398 else
12399 hscroll_step_abs = 0;
12400
12401 while (WINDOWP (window))
12402 {
12403 struct window *w = XWINDOW (window);
12404
12405 if (WINDOWP (w->contents))
12406 hscrolled_p |= hscroll_window_tree (w->contents);
12407 else if (w->cursor.vpos >= 0)
12408 {
12409 int h_margin;
12410 int text_area_width;
12411 struct glyph_row *current_cursor_row
12412 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12413 struct glyph_row *desired_cursor_row
12414 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12415 struct glyph_row *cursor_row
12416 = (desired_cursor_row->enabled_p
12417 ? desired_cursor_row
12418 : current_cursor_row);
12419 int row_r2l_p = cursor_row->reversed_p;
12420
12421 text_area_width = window_box_width (w, TEXT_AREA);
12422
12423 /* Scroll when cursor is inside this scroll margin. */
12424 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12425
12426 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12427 /* For left-to-right rows, hscroll when cursor is either
12428 (i) inside the right hscroll margin, or (ii) if it is
12429 inside the left margin and the window is already
12430 hscrolled. */
12431 && ((!row_r2l_p
12432 && ((w->hscroll
12433 && w->cursor.x <= h_margin)
12434 || (cursor_row->enabled_p
12435 && cursor_row->truncated_on_right_p
12436 && (w->cursor.x >= text_area_width - h_margin))))
12437 /* For right-to-left rows, the logic is similar,
12438 except that rules for scrolling to left and right
12439 are reversed. E.g., if cursor.x <= h_margin, we
12440 need to hscroll "to the right" unconditionally,
12441 and that will scroll the screen to the left so as
12442 to reveal the next portion of the row. */
12443 || (row_r2l_p
12444 && ((cursor_row->enabled_p
12445 /* FIXME: It is confusing to set the
12446 truncated_on_right_p flag when R2L rows
12447 are actually truncated on the left. */
12448 && cursor_row->truncated_on_right_p
12449 && w->cursor.x <= h_margin)
12450 || (w->hscroll
12451 && (w->cursor.x >= text_area_width - h_margin))))))
12452 {
12453 struct it it;
12454 ptrdiff_t hscroll;
12455 struct buffer *saved_current_buffer;
12456 ptrdiff_t pt;
12457 int wanted_x;
12458
12459 /* Find point in a display of infinite width. */
12460 saved_current_buffer = current_buffer;
12461 current_buffer = XBUFFER (w->contents);
12462
12463 if (w == XWINDOW (selected_window))
12464 pt = PT;
12465 else
12466 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12467
12468 /* Move iterator to pt starting at cursor_row->start in
12469 a line with infinite width. */
12470 init_to_row_start (&it, w, cursor_row);
12471 it.last_visible_x = INFINITY;
12472 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12473 current_buffer = saved_current_buffer;
12474
12475 /* Position cursor in window. */
12476 if (!hscroll_relative_p && hscroll_step_abs == 0)
12477 hscroll = max (0, (it.current_x
12478 - (ITERATOR_AT_END_OF_LINE_P (&it)
12479 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12480 : (text_area_width / 2))))
12481 / FRAME_COLUMN_WIDTH (it.f);
12482 else if ((!row_r2l_p
12483 && w->cursor.x >= text_area_width - h_margin)
12484 || (row_r2l_p && w->cursor.x <= h_margin))
12485 {
12486 if (hscroll_relative_p)
12487 wanted_x = text_area_width * (1 - hscroll_step_rel)
12488 - h_margin;
12489 else
12490 wanted_x = text_area_width
12491 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12492 - h_margin;
12493 hscroll
12494 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12495 }
12496 else
12497 {
12498 if (hscroll_relative_p)
12499 wanted_x = text_area_width * hscroll_step_rel
12500 + h_margin;
12501 else
12502 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12503 + h_margin;
12504 hscroll
12505 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12506 }
12507 hscroll = max (hscroll, w->min_hscroll);
12508
12509 /* Don't prevent redisplay optimizations if hscroll
12510 hasn't changed, as it will unnecessarily slow down
12511 redisplay. */
12512 if (w->hscroll != hscroll)
12513 {
12514 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12515 w->hscroll = hscroll;
12516 hscrolled_p = 1;
12517 }
12518 }
12519 }
12520
12521 window = w->next;
12522 }
12523
12524 /* Value is non-zero if hscroll of any leaf window has been changed. */
12525 return hscrolled_p;
12526 }
12527
12528
12529 /* Set hscroll so that cursor is visible and not inside horizontal
12530 scroll margins for all windows in the tree rooted at WINDOW. See
12531 also hscroll_window_tree above. Value is non-zero if any window's
12532 hscroll has been changed. If it has, desired matrices on the frame
12533 of WINDOW are cleared. */
12534
12535 static int
12536 hscroll_windows (Lisp_Object window)
12537 {
12538 int hscrolled_p = hscroll_window_tree (window);
12539 if (hscrolled_p)
12540 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12541 return hscrolled_p;
12542 }
12543
12544
12545 \f
12546 /************************************************************************
12547 Redisplay
12548 ************************************************************************/
12549
12550 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12551 to a non-zero value. This is sometimes handy to have in a debugger
12552 session. */
12553
12554 #ifdef GLYPH_DEBUG
12555
12556 /* First and last unchanged row for try_window_id. */
12557
12558 static int debug_first_unchanged_at_end_vpos;
12559 static int debug_last_unchanged_at_beg_vpos;
12560
12561 /* Delta vpos and y. */
12562
12563 static int debug_dvpos, debug_dy;
12564
12565 /* Delta in characters and bytes for try_window_id. */
12566
12567 static ptrdiff_t debug_delta, debug_delta_bytes;
12568
12569 /* Values of window_end_pos and window_end_vpos at the end of
12570 try_window_id. */
12571
12572 static ptrdiff_t debug_end_vpos;
12573
12574 /* Append a string to W->desired_matrix->method. FMT is a printf
12575 format string. If trace_redisplay_p is non-zero also printf the
12576 resulting string to stderr. */
12577
12578 static void debug_method_add (struct window *, char const *, ...)
12579 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12580
12581 static void
12582 debug_method_add (struct window *w, char const *fmt, ...)
12583 {
12584 void *ptr = w;
12585 char *method = w->desired_matrix->method;
12586 int len = strlen (method);
12587 int size = sizeof w->desired_matrix->method;
12588 int remaining = size - len - 1;
12589 va_list ap;
12590
12591 if (len && remaining)
12592 {
12593 method[len] = '|';
12594 --remaining, ++len;
12595 }
12596
12597 va_start (ap, fmt);
12598 vsnprintf (method + len, remaining + 1, fmt, ap);
12599 va_end (ap);
12600
12601 if (trace_redisplay_p)
12602 fprintf (stderr, "%p (%s): %s\n",
12603 ptr,
12604 ((BUFFERP (w->contents)
12605 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12606 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12607 : "no buffer"),
12608 method + len);
12609 }
12610
12611 #endif /* GLYPH_DEBUG */
12612
12613
12614 /* Value is non-zero if all changes in window W, which displays
12615 current_buffer, are in the text between START and END. START is a
12616 buffer position, END is given as a distance from Z. Used in
12617 redisplay_internal for display optimization. */
12618
12619 static int
12620 text_outside_line_unchanged_p (struct window *w,
12621 ptrdiff_t start, ptrdiff_t end)
12622 {
12623 int unchanged_p = 1;
12624
12625 /* If text or overlays have changed, see where. */
12626 if (window_outdated (w))
12627 {
12628 /* Gap in the line? */
12629 if (GPT < start || Z - GPT < end)
12630 unchanged_p = 0;
12631
12632 /* Changes start in front of the line, or end after it? */
12633 if (unchanged_p
12634 && (BEG_UNCHANGED < start - 1
12635 || END_UNCHANGED < end))
12636 unchanged_p = 0;
12637
12638 /* If selective display, can't optimize if changes start at the
12639 beginning of the line. */
12640 if (unchanged_p
12641 && INTEGERP (BVAR (current_buffer, selective_display))
12642 && XINT (BVAR (current_buffer, selective_display)) > 0
12643 && (BEG_UNCHANGED < start || GPT <= start))
12644 unchanged_p = 0;
12645
12646 /* If there are overlays at the start or end of the line, these
12647 may have overlay strings with newlines in them. A change at
12648 START, for instance, may actually concern the display of such
12649 overlay strings as well, and they are displayed on different
12650 lines. So, quickly rule out this case. (For the future, it
12651 might be desirable to implement something more telling than
12652 just BEG/END_UNCHANGED.) */
12653 if (unchanged_p)
12654 {
12655 if (BEG + BEG_UNCHANGED == start
12656 && overlay_touches_p (start))
12657 unchanged_p = 0;
12658 if (END_UNCHANGED == end
12659 && overlay_touches_p (Z - end))
12660 unchanged_p = 0;
12661 }
12662
12663 /* Under bidi reordering, adding or deleting a character in the
12664 beginning of a paragraph, before the first strong directional
12665 character, can change the base direction of the paragraph (unless
12666 the buffer specifies a fixed paragraph direction), which will
12667 require to redisplay the whole paragraph. It might be worthwhile
12668 to find the paragraph limits and widen the range of redisplayed
12669 lines to that, but for now just give up this optimization. */
12670 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
12671 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
12672 unchanged_p = 0;
12673 }
12674
12675 return unchanged_p;
12676 }
12677
12678
12679 /* Do a frame update, taking possible shortcuts into account. This is
12680 the main external entry point for redisplay.
12681
12682 If the last redisplay displayed an echo area message and that message
12683 is no longer requested, we clear the echo area or bring back the
12684 mini-buffer if that is in use. */
12685
12686 void
12687 redisplay (void)
12688 {
12689 redisplay_internal ();
12690 }
12691
12692
12693 static Lisp_Object
12694 overlay_arrow_string_or_property (Lisp_Object var)
12695 {
12696 Lisp_Object val;
12697
12698 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12699 return val;
12700
12701 return Voverlay_arrow_string;
12702 }
12703
12704 /* Return 1 if there are any overlay-arrows in current_buffer. */
12705 static int
12706 overlay_arrow_in_current_buffer_p (void)
12707 {
12708 Lisp_Object vlist;
12709
12710 for (vlist = Voverlay_arrow_variable_list;
12711 CONSP (vlist);
12712 vlist = XCDR (vlist))
12713 {
12714 Lisp_Object var = XCAR (vlist);
12715 Lisp_Object val;
12716
12717 if (!SYMBOLP (var))
12718 continue;
12719 val = find_symbol_value (var);
12720 if (MARKERP (val)
12721 && current_buffer == XMARKER (val)->buffer)
12722 return 1;
12723 }
12724 return 0;
12725 }
12726
12727
12728 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12729 has changed. */
12730
12731 static int
12732 overlay_arrows_changed_p (void)
12733 {
12734 Lisp_Object vlist;
12735
12736 for (vlist = Voverlay_arrow_variable_list;
12737 CONSP (vlist);
12738 vlist = XCDR (vlist))
12739 {
12740 Lisp_Object var = XCAR (vlist);
12741 Lisp_Object val, pstr;
12742
12743 if (!SYMBOLP (var))
12744 continue;
12745 val = find_symbol_value (var);
12746 if (!MARKERP (val))
12747 continue;
12748 if (! EQ (COERCE_MARKER (val),
12749 Fget (var, Qlast_arrow_position))
12750 || ! (pstr = overlay_arrow_string_or_property (var),
12751 EQ (pstr, Fget (var, Qlast_arrow_string))))
12752 return 1;
12753 }
12754 return 0;
12755 }
12756
12757 /* Mark overlay arrows to be updated on next redisplay. */
12758
12759 static void
12760 update_overlay_arrows (int up_to_date)
12761 {
12762 Lisp_Object vlist;
12763
12764 for (vlist = Voverlay_arrow_variable_list;
12765 CONSP (vlist);
12766 vlist = XCDR (vlist))
12767 {
12768 Lisp_Object var = XCAR (vlist);
12769
12770 if (!SYMBOLP (var))
12771 continue;
12772
12773 if (up_to_date > 0)
12774 {
12775 Lisp_Object val = find_symbol_value (var);
12776 Fput (var, Qlast_arrow_position,
12777 COERCE_MARKER (val));
12778 Fput (var, Qlast_arrow_string,
12779 overlay_arrow_string_or_property (var));
12780 }
12781 else if (up_to_date < 0
12782 || !NILP (Fget (var, Qlast_arrow_position)))
12783 {
12784 Fput (var, Qlast_arrow_position, Qt);
12785 Fput (var, Qlast_arrow_string, Qt);
12786 }
12787 }
12788 }
12789
12790
12791 /* Return overlay arrow string to display at row.
12792 Return integer (bitmap number) for arrow bitmap in left fringe.
12793 Return nil if no overlay arrow. */
12794
12795 static Lisp_Object
12796 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12797 {
12798 Lisp_Object vlist;
12799
12800 for (vlist = Voverlay_arrow_variable_list;
12801 CONSP (vlist);
12802 vlist = XCDR (vlist))
12803 {
12804 Lisp_Object var = XCAR (vlist);
12805 Lisp_Object val;
12806
12807 if (!SYMBOLP (var))
12808 continue;
12809
12810 val = find_symbol_value (var);
12811
12812 if (MARKERP (val)
12813 && current_buffer == XMARKER (val)->buffer
12814 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12815 {
12816 if (FRAME_WINDOW_P (it->f)
12817 /* FIXME: if ROW->reversed_p is set, this should test
12818 the right fringe, not the left one. */
12819 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12820 {
12821 #ifdef HAVE_WINDOW_SYSTEM
12822 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12823 {
12824 int fringe_bitmap;
12825 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12826 return make_number (fringe_bitmap);
12827 }
12828 #endif
12829 return make_number (-1); /* Use default arrow bitmap. */
12830 }
12831 return overlay_arrow_string_or_property (var);
12832 }
12833 }
12834
12835 return Qnil;
12836 }
12837
12838 /* Return 1 if point moved out of or into a composition. Otherwise
12839 return 0. PREV_BUF and PREV_PT are the last point buffer and
12840 position. BUF and PT are the current point buffer and position. */
12841
12842 static int
12843 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12844 struct buffer *buf, ptrdiff_t pt)
12845 {
12846 ptrdiff_t start, end;
12847 Lisp_Object prop;
12848 Lisp_Object buffer;
12849
12850 XSETBUFFER (buffer, buf);
12851 /* Check a composition at the last point if point moved within the
12852 same buffer. */
12853 if (prev_buf == buf)
12854 {
12855 if (prev_pt == pt)
12856 /* Point didn't move. */
12857 return 0;
12858
12859 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12860 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12861 && composition_valid_p (start, end, prop)
12862 && start < prev_pt && end > prev_pt)
12863 /* The last point was within the composition. Return 1 iff
12864 point moved out of the composition. */
12865 return (pt <= start || pt >= end);
12866 }
12867
12868 /* Check a composition at the current point. */
12869 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12870 && find_composition (pt, -1, &start, &end, &prop, buffer)
12871 && composition_valid_p (start, end, prop)
12872 && start < pt && end > pt);
12873 }
12874
12875 /* Reconsider the clip changes of buffer which is displayed in W. */
12876
12877 static void
12878 reconsider_clip_changes (struct window *w)
12879 {
12880 struct buffer *b = XBUFFER (w->contents);
12881
12882 if (b->clip_changed
12883 && w->window_end_valid
12884 && w->current_matrix->buffer == b
12885 && w->current_matrix->zv == BUF_ZV (b)
12886 && w->current_matrix->begv == BUF_BEGV (b))
12887 b->clip_changed = 0;
12888
12889 /* If display wasn't paused, and W is not a tool bar window, see if
12890 point has been moved into or out of a composition. In that case,
12891 we set b->clip_changed to 1 to force updating the screen. If
12892 b->clip_changed has already been set to 1, we can skip this
12893 check. */
12894 if (!b->clip_changed && w->window_end_valid)
12895 {
12896 ptrdiff_t pt = (w == XWINDOW (selected_window)
12897 ? PT : marker_position (w->pointm));
12898
12899 if ((w->current_matrix->buffer != b || pt != w->last_point)
12900 && check_point_in_composition (w->current_matrix->buffer,
12901 w->last_point, b, pt))
12902 b->clip_changed = 1;
12903 }
12904 }
12905
12906 #define STOP_POLLING \
12907 do { if (! polling_stopped_here) stop_polling (); \
12908 polling_stopped_here = 1; } while (0)
12909
12910 #define RESUME_POLLING \
12911 do { if (polling_stopped_here) start_polling (); \
12912 polling_stopped_here = 0; } while (0)
12913
12914
12915 /* Perhaps in the future avoid recentering windows if it
12916 is not necessary; currently that causes some problems. */
12917
12918 static void
12919 redisplay_internal (void)
12920 {
12921 struct window *w = XWINDOW (selected_window);
12922 struct window *sw;
12923 struct frame *fr;
12924 int pending;
12925 bool must_finish = 0, match_p;
12926 struct text_pos tlbufpos, tlendpos;
12927 int number_of_visible_frames;
12928 ptrdiff_t count;
12929 struct frame *sf;
12930 int polling_stopped_here = 0;
12931 Lisp_Object tail, frame;
12932
12933 /* Non-zero means redisplay has to consider all windows on all
12934 frames. Zero means, only selected_window is considered. */
12935 int consider_all_windows_p;
12936
12937 /* Non-zero means redisplay has to redisplay the miniwindow. */
12938 int update_miniwindow_p = 0;
12939
12940 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12941
12942 /* No redisplay if running in batch mode or frame is not yet fully
12943 initialized, or redisplay is explicitly turned off by setting
12944 Vinhibit_redisplay. */
12945 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12946 || !NILP (Vinhibit_redisplay))
12947 return;
12948
12949 /* Don't examine these until after testing Vinhibit_redisplay.
12950 When Emacs is shutting down, perhaps because its connection to
12951 X has dropped, we should not look at them at all. */
12952 fr = XFRAME (w->frame);
12953 sf = SELECTED_FRAME ();
12954
12955 if (!fr->glyphs_initialized_p)
12956 return;
12957
12958 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12959 if (popup_activated ())
12960 return;
12961 #endif
12962
12963 /* I don't think this happens but let's be paranoid. */
12964 if (redisplaying_p)
12965 return;
12966
12967 /* Record a function that clears redisplaying_p
12968 when we leave this function. */
12969 count = SPECPDL_INDEX ();
12970 record_unwind_protect_void (unwind_redisplay);
12971 redisplaying_p = 1;
12972 specbind (Qinhibit_free_realized_faces, Qnil);
12973
12974 /* Record this function, so it appears on the profiler's backtraces. */
12975 record_in_backtrace (Qredisplay_internal, &Qnil, 0);
12976
12977 FOR_EACH_FRAME (tail, frame)
12978 XFRAME (frame)->already_hscrolled_p = 0;
12979
12980 retry:
12981 /* Remember the currently selected window. */
12982 sw = w;
12983
12984 pending = 0;
12985 last_escape_glyph_frame = NULL;
12986 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12987 last_glyphless_glyph_frame = NULL;
12988 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12989
12990 /* If new fonts have been loaded that make a glyph matrix adjustment
12991 necessary, do it. */
12992 if (fonts_changed_p)
12993 {
12994 adjust_glyphs (NULL);
12995 ++windows_or_buffers_changed;
12996 fonts_changed_p = 0;
12997 }
12998
12999 /* If face_change_count is non-zero, init_iterator will free all
13000 realized faces, which includes the faces referenced from current
13001 matrices. So, we can't reuse current matrices in this case. */
13002 if (face_change_count)
13003 ++windows_or_buffers_changed;
13004
13005 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13006 && FRAME_TTY (sf)->previous_frame != sf)
13007 {
13008 /* Since frames on a single ASCII terminal share the same
13009 display area, displaying a different frame means redisplay
13010 the whole thing. */
13011 windows_or_buffers_changed++;
13012 SET_FRAME_GARBAGED (sf);
13013 #ifndef DOS_NT
13014 set_tty_color_mode (FRAME_TTY (sf), sf);
13015 #endif
13016 FRAME_TTY (sf)->previous_frame = sf;
13017 }
13018
13019 /* Set the visible flags for all frames. Do this before checking for
13020 resized or garbaged frames; they want to know if their frames are
13021 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13022 number_of_visible_frames = 0;
13023
13024 FOR_EACH_FRAME (tail, frame)
13025 {
13026 struct frame *f = XFRAME (frame);
13027
13028 if (FRAME_VISIBLE_P (f))
13029 ++number_of_visible_frames;
13030 clear_desired_matrices (f);
13031 }
13032
13033 /* Notice any pending interrupt request to change frame size. */
13034 do_pending_window_change (1);
13035
13036 /* do_pending_window_change could change the selected_window due to
13037 frame resizing which makes the selected window too small. */
13038 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13039 sw = w;
13040
13041 /* Clear frames marked as garbaged. */
13042 clear_garbaged_frames ();
13043
13044 /* Build menubar and tool-bar items. */
13045 if (NILP (Vmemory_full))
13046 prepare_menu_bars ();
13047
13048 if (windows_or_buffers_changed)
13049 update_mode_lines++;
13050
13051 reconsider_clip_changes (w);
13052
13053 /* In most cases selected window displays current buffer. */
13054 match_p = XBUFFER (w->contents) == current_buffer;
13055 if (match_p)
13056 {
13057 ptrdiff_t count1;
13058
13059 /* Detect case that we need to write or remove a star in the mode line. */
13060 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13061 {
13062 w->update_mode_line = 1;
13063 if (buffer_shared_and_changed ())
13064 update_mode_lines++;
13065 }
13066
13067 /* Avoid invocation of point motion hooks by `current_column' below. */
13068 count1 = SPECPDL_INDEX ();
13069 specbind (Qinhibit_point_motion_hooks, Qt);
13070
13071 if (mode_line_update_needed (w))
13072 w->update_mode_line = 1;
13073
13074 unbind_to (count1, Qnil);
13075 }
13076
13077 consider_all_windows_p = (update_mode_lines
13078 || buffer_shared_and_changed ()
13079 || cursor_type_changed);
13080
13081 /* If specs for an arrow have changed, do thorough redisplay
13082 to ensure we remove any arrow that should no longer exist. */
13083 if (overlay_arrows_changed_p ())
13084 consider_all_windows_p = windows_or_buffers_changed = 1;
13085
13086 /* Normally the message* functions will have already displayed and
13087 updated the echo area, but the frame may have been trashed, or
13088 the update may have been preempted, so display the echo area
13089 again here. Checking message_cleared_p captures the case that
13090 the echo area should be cleared. */
13091 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13092 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13093 || (message_cleared_p
13094 && minibuf_level == 0
13095 /* If the mini-window is currently selected, this means the
13096 echo-area doesn't show through. */
13097 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13098 {
13099 int window_height_changed_p = echo_area_display (0);
13100
13101 if (message_cleared_p)
13102 update_miniwindow_p = 1;
13103
13104 must_finish = 1;
13105
13106 /* If we don't display the current message, don't clear the
13107 message_cleared_p flag, because, if we did, we wouldn't clear
13108 the echo area in the next redisplay which doesn't preserve
13109 the echo area. */
13110 if (!display_last_displayed_message_p)
13111 message_cleared_p = 0;
13112
13113 if (fonts_changed_p)
13114 goto retry;
13115 else if (window_height_changed_p)
13116 {
13117 consider_all_windows_p = 1;
13118 ++update_mode_lines;
13119 ++windows_or_buffers_changed;
13120
13121 /* If window configuration was changed, frames may have been
13122 marked garbaged. Clear them or we will experience
13123 surprises wrt scrolling. */
13124 clear_garbaged_frames ();
13125 }
13126 }
13127 else if (EQ (selected_window, minibuf_window)
13128 && (current_buffer->clip_changed || window_outdated (w))
13129 && resize_mini_window (w, 0))
13130 {
13131 /* Resized active mini-window to fit the size of what it is
13132 showing if its contents might have changed. */
13133 must_finish = 1;
13134 /* FIXME: this causes all frames to be updated, which seems unnecessary
13135 since only the current frame needs to be considered. This function
13136 needs to be rewritten with two variables, consider_all_windows and
13137 consider_all_frames. */
13138 consider_all_windows_p = 1;
13139 ++windows_or_buffers_changed;
13140 ++update_mode_lines;
13141
13142 /* If window configuration was changed, frames may have been
13143 marked garbaged. Clear them or we will experience
13144 surprises wrt scrolling. */
13145 clear_garbaged_frames ();
13146 }
13147
13148 /* If showing the region, and mark has changed, we must redisplay
13149 the whole window. The assignment to this_line_start_pos prevents
13150 the optimization directly below this if-statement. */
13151 if (((!NILP (Vtransient_mark_mode)
13152 && !NILP (BVAR (XBUFFER (w->contents), mark_active)))
13153 != (w->region_showing > 0))
13154 || (w->region_showing
13155 && w->region_showing
13156 != XINT (Fmarker_position (BVAR (XBUFFER (w->contents), mark)))))
13157 CHARPOS (this_line_start_pos) = 0;
13158
13159 /* Optimize the case that only the line containing the cursor in the
13160 selected window has changed. Variables starting with this_ are
13161 set in display_line and record information about the line
13162 containing the cursor. */
13163 tlbufpos = this_line_start_pos;
13164 tlendpos = this_line_end_pos;
13165 if (!consider_all_windows_p
13166 && CHARPOS (tlbufpos) > 0
13167 && !w->update_mode_line
13168 && !current_buffer->clip_changed
13169 && !current_buffer->prevent_redisplay_optimizations_p
13170 && FRAME_VISIBLE_P (XFRAME (w->frame))
13171 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13172 /* Make sure recorded data applies to current buffer, etc. */
13173 && this_line_buffer == current_buffer
13174 && match_p
13175 && !w->force_start
13176 && !w->optional_new_start
13177 /* Point must be on the line that we have info recorded about. */
13178 && PT >= CHARPOS (tlbufpos)
13179 && PT <= Z - CHARPOS (tlendpos)
13180 /* All text outside that line, including its final newline,
13181 must be unchanged. */
13182 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13183 CHARPOS (tlendpos)))
13184 {
13185 if (CHARPOS (tlbufpos) > BEGV
13186 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13187 && (CHARPOS (tlbufpos) == ZV
13188 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13189 /* Former continuation line has disappeared by becoming empty. */
13190 goto cancel;
13191 else if (window_outdated (w) || MINI_WINDOW_P (w))
13192 {
13193 /* We have to handle the case of continuation around a
13194 wide-column character (see the comment in indent.c around
13195 line 1340).
13196
13197 For instance, in the following case:
13198
13199 -------- Insert --------
13200 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13201 J_I_ ==> J_I_ `^^' are cursors.
13202 ^^ ^^
13203 -------- --------
13204
13205 As we have to redraw the line above, we cannot use this
13206 optimization. */
13207
13208 struct it it;
13209 int line_height_before = this_line_pixel_height;
13210
13211 /* Note that start_display will handle the case that the
13212 line starting at tlbufpos is a continuation line. */
13213 start_display (&it, w, tlbufpos);
13214
13215 /* Implementation note: It this still necessary? */
13216 if (it.current_x != this_line_start_x)
13217 goto cancel;
13218
13219 TRACE ((stderr, "trying display optimization 1\n"));
13220 w->cursor.vpos = -1;
13221 overlay_arrow_seen = 0;
13222 it.vpos = this_line_vpos;
13223 it.current_y = this_line_y;
13224 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13225 display_line (&it);
13226
13227 /* If line contains point, is not continued,
13228 and ends at same distance from eob as before, we win. */
13229 if (w->cursor.vpos >= 0
13230 /* Line is not continued, otherwise this_line_start_pos
13231 would have been set to 0 in display_line. */
13232 && CHARPOS (this_line_start_pos)
13233 /* Line ends as before. */
13234 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13235 /* Line has same height as before. Otherwise other lines
13236 would have to be shifted up or down. */
13237 && this_line_pixel_height == line_height_before)
13238 {
13239 /* If this is not the window's last line, we must adjust
13240 the charstarts of the lines below. */
13241 if (it.current_y < it.last_visible_y)
13242 {
13243 struct glyph_row *row
13244 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13245 ptrdiff_t delta, delta_bytes;
13246
13247 /* We used to distinguish between two cases here,
13248 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13249 when the line ends in a newline or the end of the
13250 buffer's accessible portion. But both cases did
13251 the same, so they were collapsed. */
13252 delta = (Z
13253 - CHARPOS (tlendpos)
13254 - MATRIX_ROW_START_CHARPOS (row));
13255 delta_bytes = (Z_BYTE
13256 - BYTEPOS (tlendpos)
13257 - MATRIX_ROW_START_BYTEPOS (row));
13258
13259 increment_matrix_positions (w->current_matrix,
13260 this_line_vpos + 1,
13261 w->current_matrix->nrows,
13262 delta, delta_bytes);
13263 }
13264
13265 /* If this row displays text now but previously didn't,
13266 or vice versa, w->window_end_vpos may have to be
13267 adjusted. */
13268 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13269 {
13270 if (w->window_end_vpos < this_line_vpos)
13271 w->window_end_vpos = this_line_vpos;
13272 }
13273 else if (w->window_end_vpos == this_line_vpos
13274 && this_line_vpos > 0)
13275 w->window_end_vpos = this_line_vpos - 1;
13276 w->window_end_valid = 0;
13277
13278 /* Update hint: No need to try to scroll in update_window. */
13279 w->desired_matrix->no_scrolling_p = 1;
13280
13281 #ifdef GLYPH_DEBUG
13282 *w->desired_matrix->method = 0;
13283 debug_method_add (w, "optimization 1");
13284 #endif
13285 #ifdef HAVE_WINDOW_SYSTEM
13286 update_window_fringes (w, 0);
13287 #endif
13288 goto update;
13289 }
13290 else
13291 goto cancel;
13292 }
13293 else if (/* Cursor position hasn't changed. */
13294 PT == w->last_point
13295 /* Make sure the cursor was last displayed
13296 in this window. Otherwise we have to reposition it. */
13297 && 0 <= w->cursor.vpos
13298 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13299 {
13300 if (!must_finish)
13301 {
13302 do_pending_window_change (1);
13303 /* If selected_window changed, redisplay again. */
13304 if (WINDOWP (selected_window)
13305 && (w = XWINDOW (selected_window)) != sw)
13306 goto retry;
13307
13308 /* We used to always goto end_of_redisplay here, but this
13309 isn't enough if we have a blinking cursor. */
13310 if (w->cursor_off_p == w->last_cursor_off_p)
13311 goto end_of_redisplay;
13312 }
13313 goto update;
13314 }
13315 /* If highlighting the region, or if the cursor is in the echo area,
13316 then we can't just move the cursor. */
13317 else if (! (!NILP (Vtransient_mark_mode)
13318 && !NILP (BVAR (current_buffer, mark_active)))
13319 && (EQ (selected_window,
13320 BVAR (current_buffer, last_selected_window))
13321 || highlight_nonselected_windows)
13322 && !w->region_showing
13323 && NILP (Vshow_trailing_whitespace)
13324 && !cursor_in_echo_area)
13325 {
13326 struct it it;
13327 struct glyph_row *row;
13328
13329 /* Skip from tlbufpos to PT and see where it is. Note that
13330 PT may be in invisible text. If so, we will end at the
13331 next visible position. */
13332 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13333 NULL, DEFAULT_FACE_ID);
13334 it.current_x = this_line_start_x;
13335 it.current_y = this_line_y;
13336 it.vpos = this_line_vpos;
13337
13338 /* The call to move_it_to stops in front of PT, but
13339 moves over before-strings. */
13340 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13341
13342 if (it.vpos == this_line_vpos
13343 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13344 row->enabled_p))
13345 {
13346 eassert (this_line_vpos == it.vpos);
13347 eassert (this_line_y == it.current_y);
13348 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13349 #ifdef GLYPH_DEBUG
13350 *w->desired_matrix->method = 0;
13351 debug_method_add (w, "optimization 3");
13352 #endif
13353 goto update;
13354 }
13355 else
13356 goto cancel;
13357 }
13358
13359 cancel:
13360 /* Text changed drastically or point moved off of line. */
13361 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13362 }
13363
13364 CHARPOS (this_line_start_pos) = 0;
13365 consider_all_windows_p |= buffer_shared_and_changed ();
13366 ++clear_face_cache_count;
13367 #ifdef HAVE_WINDOW_SYSTEM
13368 ++clear_image_cache_count;
13369 #endif
13370
13371 /* Build desired matrices, and update the display. If
13372 consider_all_windows_p is non-zero, do it for all windows on all
13373 frames. Otherwise do it for selected_window, only. */
13374
13375 if (consider_all_windows_p)
13376 {
13377 FOR_EACH_FRAME (tail, frame)
13378 XFRAME (frame)->updated_p = 0;
13379
13380 FOR_EACH_FRAME (tail, frame)
13381 {
13382 struct frame *f = XFRAME (frame);
13383
13384 /* We don't have to do anything for unselected terminal
13385 frames. */
13386 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13387 && !EQ (FRAME_TTY (f)->top_frame, frame))
13388 continue;
13389
13390 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13391 {
13392 /* Mark all the scroll bars to be removed; we'll redeem
13393 the ones we want when we redisplay their windows. */
13394 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13395 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13396
13397 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13398 redisplay_windows (FRAME_ROOT_WINDOW (f));
13399
13400 /* The X error handler may have deleted that frame. */
13401 if (!FRAME_LIVE_P (f))
13402 continue;
13403
13404 /* Any scroll bars which redisplay_windows should have
13405 nuked should now go away. */
13406 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13407 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13408
13409 /* If fonts changed, display again. */
13410 /* ??? rms: I suspect it is a mistake to jump all the way
13411 back to retry here. It should just retry this frame. */
13412 if (fonts_changed_p)
13413 goto retry;
13414
13415 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13416 {
13417 /* See if we have to hscroll. */
13418 if (!f->already_hscrolled_p)
13419 {
13420 f->already_hscrolled_p = 1;
13421 if (hscroll_windows (f->root_window))
13422 goto retry;
13423 }
13424
13425 /* Prevent various kinds of signals during display
13426 update. stdio is not robust about handling
13427 signals, which can cause an apparent I/O
13428 error. */
13429 if (interrupt_input)
13430 unrequest_sigio ();
13431 STOP_POLLING;
13432
13433 /* Update the display. */
13434 set_window_update_flags (XWINDOW (f->root_window), 1);
13435 pending |= update_frame (f, 0, 0);
13436 f->updated_p = 1;
13437 }
13438 }
13439 }
13440
13441 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13442
13443 if (!pending)
13444 {
13445 /* Do the mark_window_display_accurate after all windows have
13446 been redisplayed because this call resets flags in buffers
13447 which are needed for proper redisplay. */
13448 FOR_EACH_FRAME (tail, frame)
13449 {
13450 struct frame *f = XFRAME (frame);
13451 if (f->updated_p)
13452 {
13453 mark_window_display_accurate (f->root_window, 1);
13454 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13455 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13456 }
13457 }
13458 }
13459 }
13460 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13461 {
13462 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13463 struct frame *mini_frame;
13464
13465 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13466 /* Use list_of_error, not Qerror, so that
13467 we catch only errors and don't run the debugger. */
13468 internal_condition_case_1 (redisplay_window_1, selected_window,
13469 list_of_error,
13470 redisplay_window_error);
13471 if (update_miniwindow_p)
13472 internal_condition_case_1 (redisplay_window_1, mini_window,
13473 list_of_error,
13474 redisplay_window_error);
13475
13476 /* Compare desired and current matrices, perform output. */
13477
13478 update:
13479 /* If fonts changed, display again. */
13480 if (fonts_changed_p)
13481 goto retry;
13482
13483 /* Prevent various kinds of signals during display update.
13484 stdio is not robust about handling signals,
13485 which can cause an apparent I/O error. */
13486 if (interrupt_input)
13487 unrequest_sigio ();
13488 STOP_POLLING;
13489
13490 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13491 {
13492 if (hscroll_windows (selected_window))
13493 goto retry;
13494
13495 XWINDOW (selected_window)->must_be_updated_p = 1;
13496 pending = update_frame (sf, 0, 0);
13497 }
13498
13499 /* We may have called echo_area_display at the top of this
13500 function. If the echo area is on another frame, that may
13501 have put text on a frame other than the selected one, so the
13502 above call to update_frame would not have caught it. Catch
13503 it here. */
13504 mini_window = FRAME_MINIBUF_WINDOW (sf);
13505 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13506
13507 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13508 {
13509 XWINDOW (mini_window)->must_be_updated_p = 1;
13510 pending |= update_frame (mini_frame, 0, 0);
13511 if (!pending && hscroll_windows (mini_window))
13512 goto retry;
13513 }
13514 }
13515
13516 /* If display was paused because of pending input, make sure we do a
13517 thorough update the next time. */
13518 if (pending)
13519 {
13520 /* Prevent the optimization at the beginning of
13521 redisplay_internal that tries a single-line update of the
13522 line containing the cursor in the selected window. */
13523 CHARPOS (this_line_start_pos) = 0;
13524
13525 /* Let the overlay arrow be updated the next time. */
13526 update_overlay_arrows (0);
13527
13528 /* If we pause after scrolling, some rows in the current
13529 matrices of some windows are not valid. */
13530 if (!WINDOW_FULL_WIDTH_P (w)
13531 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13532 update_mode_lines = 1;
13533 }
13534 else
13535 {
13536 if (!consider_all_windows_p)
13537 {
13538 /* This has already been done above if
13539 consider_all_windows_p is set. */
13540 mark_window_display_accurate_1 (w, 1);
13541
13542 /* Say overlay arrows are up to date. */
13543 update_overlay_arrows (1);
13544
13545 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13546 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13547 }
13548
13549 update_mode_lines = 0;
13550 windows_or_buffers_changed = 0;
13551 cursor_type_changed = 0;
13552 }
13553
13554 /* Start SIGIO interrupts coming again. Having them off during the
13555 code above makes it less likely one will discard output, but not
13556 impossible, since there might be stuff in the system buffer here.
13557 But it is much hairier to try to do anything about that. */
13558 if (interrupt_input)
13559 request_sigio ();
13560 RESUME_POLLING;
13561
13562 /* If a frame has become visible which was not before, redisplay
13563 again, so that we display it. Expose events for such a frame
13564 (which it gets when becoming visible) don't call the parts of
13565 redisplay constructing glyphs, so simply exposing a frame won't
13566 display anything in this case. So, we have to display these
13567 frames here explicitly. */
13568 if (!pending)
13569 {
13570 int new_count = 0;
13571
13572 FOR_EACH_FRAME (tail, frame)
13573 {
13574 int this_is_visible = 0;
13575
13576 if (XFRAME (frame)->visible)
13577 this_is_visible = 1;
13578
13579 if (this_is_visible)
13580 new_count++;
13581 }
13582
13583 if (new_count != number_of_visible_frames)
13584 windows_or_buffers_changed++;
13585 }
13586
13587 /* Change frame size now if a change is pending. */
13588 do_pending_window_change (1);
13589
13590 /* If we just did a pending size change, or have additional
13591 visible frames, or selected_window changed, redisplay again. */
13592 if ((windows_or_buffers_changed && !pending)
13593 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13594 goto retry;
13595
13596 /* Clear the face and image caches.
13597
13598 We used to do this only if consider_all_windows_p. But the cache
13599 needs to be cleared if a timer creates images in the current
13600 buffer (e.g. the test case in Bug#6230). */
13601
13602 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13603 {
13604 clear_face_cache (0);
13605 clear_face_cache_count = 0;
13606 }
13607
13608 #ifdef HAVE_WINDOW_SYSTEM
13609 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13610 {
13611 clear_image_caches (Qnil);
13612 clear_image_cache_count = 0;
13613 }
13614 #endif /* HAVE_WINDOW_SYSTEM */
13615
13616 end_of_redisplay:
13617 unbind_to (count, Qnil);
13618 RESUME_POLLING;
13619 }
13620
13621
13622 /* Redisplay, but leave alone any recent echo area message unless
13623 another message has been requested in its place.
13624
13625 This is useful in situations where you need to redisplay but no
13626 user action has occurred, making it inappropriate for the message
13627 area to be cleared. See tracking_off and
13628 wait_reading_process_output for examples of these situations.
13629
13630 FROM_WHERE is an integer saying from where this function was
13631 called. This is useful for debugging. */
13632
13633 void
13634 redisplay_preserve_echo_area (int from_where)
13635 {
13636 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13637
13638 if (!NILP (echo_area_buffer[1]))
13639 {
13640 /* We have a previously displayed message, but no current
13641 message. Redisplay the previous message. */
13642 display_last_displayed_message_p = 1;
13643 redisplay_internal ();
13644 display_last_displayed_message_p = 0;
13645 }
13646 else
13647 redisplay_internal ();
13648
13649 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13650 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13651 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13652 }
13653
13654
13655 /* Function registered with record_unwind_protect in redisplay_internal. */
13656
13657 static void
13658 unwind_redisplay (void)
13659 {
13660 redisplaying_p = 0;
13661 }
13662
13663
13664 /* Mark the display of leaf window W as accurate or inaccurate.
13665 If ACCURATE_P is non-zero mark display of W as accurate. If
13666 ACCURATE_P is zero, arrange for W to be redisplayed the next
13667 time redisplay_internal is called. */
13668
13669 static void
13670 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13671 {
13672 struct buffer *b = XBUFFER (w->contents);
13673
13674 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
13675 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
13676 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13677
13678 if (accurate_p)
13679 {
13680 b->clip_changed = 0;
13681 b->prevent_redisplay_optimizations_p = 0;
13682
13683 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13684 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13685 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13686 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13687
13688 w->current_matrix->buffer = b;
13689 w->current_matrix->begv = BUF_BEGV (b);
13690 w->current_matrix->zv = BUF_ZV (b);
13691
13692 w->last_cursor = w->cursor;
13693 w->last_cursor_off_p = w->cursor_off_p;
13694
13695 if (w == XWINDOW (selected_window))
13696 w->last_point = BUF_PT (b);
13697 else
13698 w->last_point = marker_position (w->pointm);
13699
13700 w->window_end_valid = 1;
13701 w->update_mode_line = 0;
13702 }
13703 }
13704
13705
13706 /* Mark the display of windows in the window tree rooted at WINDOW as
13707 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13708 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13709 be redisplayed the next time redisplay_internal is called. */
13710
13711 void
13712 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13713 {
13714 struct window *w;
13715
13716 for (; !NILP (window); window = w->next)
13717 {
13718 w = XWINDOW (window);
13719 if (WINDOWP (w->contents))
13720 mark_window_display_accurate (w->contents, accurate_p);
13721 else
13722 mark_window_display_accurate_1 (w, accurate_p);
13723 }
13724
13725 if (accurate_p)
13726 update_overlay_arrows (1);
13727 else
13728 /* Force a thorough redisplay the next time by setting
13729 last_arrow_position and last_arrow_string to t, which is
13730 unequal to any useful value of Voverlay_arrow_... */
13731 update_overlay_arrows (-1);
13732 }
13733
13734
13735 /* Return value in display table DP (Lisp_Char_Table *) for character
13736 C. Since a display table doesn't have any parent, we don't have to
13737 follow parent. Do not call this function directly but use the
13738 macro DISP_CHAR_VECTOR. */
13739
13740 Lisp_Object
13741 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13742 {
13743 Lisp_Object val;
13744
13745 if (ASCII_CHAR_P (c))
13746 {
13747 val = dp->ascii;
13748 if (SUB_CHAR_TABLE_P (val))
13749 val = XSUB_CHAR_TABLE (val)->contents[c];
13750 }
13751 else
13752 {
13753 Lisp_Object table;
13754
13755 XSETCHAR_TABLE (table, dp);
13756 val = char_table_ref (table, c);
13757 }
13758 if (NILP (val))
13759 val = dp->defalt;
13760 return val;
13761 }
13762
13763
13764 \f
13765 /***********************************************************************
13766 Window Redisplay
13767 ***********************************************************************/
13768
13769 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13770
13771 static void
13772 redisplay_windows (Lisp_Object window)
13773 {
13774 while (!NILP (window))
13775 {
13776 struct window *w = XWINDOW (window);
13777
13778 if (WINDOWP (w->contents))
13779 redisplay_windows (w->contents);
13780 else if (BUFFERP (w->contents))
13781 {
13782 displayed_buffer = XBUFFER (w->contents);
13783 /* Use list_of_error, not Qerror, so that
13784 we catch only errors and don't run the debugger. */
13785 internal_condition_case_1 (redisplay_window_0, window,
13786 list_of_error,
13787 redisplay_window_error);
13788 }
13789
13790 window = w->next;
13791 }
13792 }
13793
13794 static Lisp_Object
13795 redisplay_window_error (Lisp_Object ignore)
13796 {
13797 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13798 return Qnil;
13799 }
13800
13801 static Lisp_Object
13802 redisplay_window_0 (Lisp_Object window)
13803 {
13804 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13805 redisplay_window (window, 0);
13806 return Qnil;
13807 }
13808
13809 static Lisp_Object
13810 redisplay_window_1 (Lisp_Object window)
13811 {
13812 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13813 redisplay_window (window, 1);
13814 return Qnil;
13815 }
13816 \f
13817
13818 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13819 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13820 which positions recorded in ROW differ from current buffer
13821 positions.
13822
13823 Return 0 if cursor is not on this row, 1 otherwise. */
13824
13825 static int
13826 set_cursor_from_row (struct window *w, struct glyph_row *row,
13827 struct glyph_matrix *matrix,
13828 ptrdiff_t delta, ptrdiff_t delta_bytes,
13829 int dy, int dvpos)
13830 {
13831 struct glyph *glyph = row->glyphs[TEXT_AREA];
13832 struct glyph *end = glyph + row->used[TEXT_AREA];
13833 struct glyph *cursor = NULL;
13834 /* The last known character position in row. */
13835 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13836 int x = row->x;
13837 ptrdiff_t pt_old = PT - delta;
13838 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13839 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13840 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13841 /* A glyph beyond the edge of TEXT_AREA which we should never
13842 touch. */
13843 struct glyph *glyphs_end = end;
13844 /* Non-zero means we've found a match for cursor position, but that
13845 glyph has the avoid_cursor_p flag set. */
13846 int match_with_avoid_cursor = 0;
13847 /* Non-zero means we've seen at least one glyph that came from a
13848 display string. */
13849 int string_seen = 0;
13850 /* Largest and smallest buffer positions seen so far during scan of
13851 glyph row. */
13852 ptrdiff_t bpos_max = pos_before;
13853 ptrdiff_t bpos_min = pos_after;
13854 /* Last buffer position covered by an overlay string with an integer
13855 `cursor' property. */
13856 ptrdiff_t bpos_covered = 0;
13857 /* Non-zero means the display string on which to display the cursor
13858 comes from a text property, not from an overlay. */
13859 int string_from_text_prop = 0;
13860
13861 /* Don't even try doing anything if called for a mode-line or
13862 header-line row, since the rest of the code isn't prepared to
13863 deal with such calamities. */
13864 eassert (!row->mode_line_p);
13865 if (row->mode_line_p)
13866 return 0;
13867
13868 /* Skip over glyphs not having an object at the start and the end of
13869 the row. These are special glyphs like truncation marks on
13870 terminal frames. */
13871 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13872 {
13873 if (!row->reversed_p)
13874 {
13875 while (glyph < end
13876 && INTEGERP (glyph->object)
13877 && glyph->charpos < 0)
13878 {
13879 x += glyph->pixel_width;
13880 ++glyph;
13881 }
13882 while (end > glyph
13883 && INTEGERP ((end - 1)->object)
13884 /* CHARPOS is zero for blanks and stretch glyphs
13885 inserted by extend_face_to_end_of_line. */
13886 && (end - 1)->charpos <= 0)
13887 --end;
13888 glyph_before = glyph - 1;
13889 glyph_after = end;
13890 }
13891 else
13892 {
13893 struct glyph *g;
13894
13895 /* If the glyph row is reversed, we need to process it from back
13896 to front, so swap the edge pointers. */
13897 glyphs_end = end = glyph - 1;
13898 glyph += row->used[TEXT_AREA] - 1;
13899
13900 while (glyph > end + 1
13901 && INTEGERP (glyph->object)
13902 && glyph->charpos < 0)
13903 {
13904 --glyph;
13905 x -= glyph->pixel_width;
13906 }
13907 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13908 --glyph;
13909 /* By default, in reversed rows we put the cursor on the
13910 rightmost (first in the reading order) glyph. */
13911 for (g = end + 1; g < glyph; g++)
13912 x += g->pixel_width;
13913 while (end < glyph
13914 && INTEGERP ((end + 1)->object)
13915 && (end + 1)->charpos <= 0)
13916 ++end;
13917 glyph_before = glyph + 1;
13918 glyph_after = end;
13919 }
13920 }
13921 else if (row->reversed_p)
13922 {
13923 /* In R2L rows that don't display text, put the cursor on the
13924 rightmost glyph. Case in point: an empty last line that is
13925 part of an R2L paragraph. */
13926 cursor = end - 1;
13927 /* Avoid placing the cursor on the last glyph of the row, where
13928 on terminal frames we hold the vertical border between
13929 adjacent windows. */
13930 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13931 && !WINDOW_RIGHTMOST_P (w)
13932 && cursor == row->glyphs[LAST_AREA] - 1)
13933 cursor--;
13934 x = -1; /* will be computed below, at label compute_x */
13935 }
13936
13937 /* Step 1: Try to find the glyph whose character position
13938 corresponds to point. If that's not possible, find 2 glyphs
13939 whose character positions are the closest to point, one before
13940 point, the other after it. */
13941 if (!row->reversed_p)
13942 while (/* not marched to end of glyph row */
13943 glyph < end
13944 /* glyph was not inserted by redisplay for internal purposes */
13945 && !INTEGERP (glyph->object))
13946 {
13947 if (BUFFERP (glyph->object))
13948 {
13949 ptrdiff_t dpos = glyph->charpos - pt_old;
13950
13951 if (glyph->charpos > bpos_max)
13952 bpos_max = glyph->charpos;
13953 if (glyph->charpos < bpos_min)
13954 bpos_min = glyph->charpos;
13955 if (!glyph->avoid_cursor_p)
13956 {
13957 /* If we hit point, we've found the glyph on which to
13958 display the cursor. */
13959 if (dpos == 0)
13960 {
13961 match_with_avoid_cursor = 0;
13962 break;
13963 }
13964 /* See if we've found a better approximation to
13965 POS_BEFORE or to POS_AFTER. */
13966 if (0 > dpos && dpos > pos_before - pt_old)
13967 {
13968 pos_before = glyph->charpos;
13969 glyph_before = glyph;
13970 }
13971 else if (0 < dpos && dpos < pos_after - pt_old)
13972 {
13973 pos_after = glyph->charpos;
13974 glyph_after = glyph;
13975 }
13976 }
13977 else if (dpos == 0)
13978 match_with_avoid_cursor = 1;
13979 }
13980 else if (STRINGP (glyph->object))
13981 {
13982 Lisp_Object chprop;
13983 ptrdiff_t glyph_pos = glyph->charpos;
13984
13985 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13986 glyph->object);
13987 if (!NILP (chprop))
13988 {
13989 /* If the string came from a `display' text property,
13990 look up the buffer position of that property and
13991 use that position to update bpos_max, as if we
13992 actually saw such a position in one of the row's
13993 glyphs. This helps with supporting integer values
13994 of `cursor' property on the display string in
13995 situations where most or all of the row's buffer
13996 text is completely covered by display properties,
13997 so that no glyph with valid buffer positions is
13998 ever seen in the row. */
13999 ptrdiff_t prop_pos =
14000 string_buffer_position_lim (glyph->object, pos_before,
14001 pos_after, 0);
14002
14003 if (prop_pos >= pos_before)
14004 bpos_max = prop_pos - 1;
14005 }
14006 if (INTEGERP (chprop))
14007 {
14008 bpos_covered = bpos_max + XINT (chprop);
14009 /* If the `cursor' property covers buffer positions up
14010 to and including point, we should display cursor on
14011 this glyph. Note that, if a `cursor' property on one
14012 of the string's characters has an integer value, we
14013 will break out of the loop below _before_ we get to
14014 the position match above. IOW, integer values of
14015 the `cursor' property override the "exact match for
14016 point" strategy of positioning the cursor. */
14017 /* Implementation note: bpos_max == pt_old when, e.g.,
14018 we are in an empty line, where bpos_max is set to
14019 MATRIX_ROW_START_CHARPOS, see above. */
14020 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14021 {
14022 cursor = glyph;
14023 break;
14024 }
14025 }
14026
14027 string_seen = 1;
14028 }
14029 x += glyph->pixel_width;
14030 ++glyph;
14031 }
14032 else if (glyph > end) /* row is reversed */
14033 while (!INTEGERP (glyph->object))
14034 {
14035 if (BUFFERP (glyph->object))
14036 {
14037 ptrdiff_t dpos = glyph->charpos - pt_old;
14038
14039 if (glyph->charpos > bpos_max)
14040 bpos_max = glyph->charpos;
14041 if (glyph->charpos < bpos_min)
14042 bpos_min = glyph->charpos;
14043 if (!glyph->avoid_cursor_p)
14044 {
14045 if (dpos == 0)
14046 {
14047 match_with_avoid_cursor = 0;
14048 break;
14049 }
14050 if (0 > dpos && dpos > pos_before - pt_old)
14051 {
14052 pos_before = glyph->charpos;
14053 glyph_before = glyph;
14054 }
14055 else if (0 < dpos && dpos < pos_after - pt_old)
14056 {
14057 pos_after = glyph->charpos;
14058 glyph_after = glyph;
14059 }
14060 }
14061 else if (dpos == 0)
14062 match_with_avoid_cursor = 1;
14063 }
14064 else if (STRINGP (glyph->object))
14065 {
14066 Lisp_Object chprop;
14067 ptrdiff_t glyph_pos = glyph->charpos;
14068
14069 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14070 glyph->object);
14071 if (!NILP (chprop))
14072 {
14073 ptrdiff_t prop_pos =
14074 string_buffer_position_lim (glyph->object, pos_before,
14075 pos_after, 0);
14076
14077 if (prop_pos >= pos_before)
14078 bpos_max = prop_pos - 1;
14079 }
14080 if (INTEGERP (chprop))
14081 {
14082 bpos_covered = bpos_max + XINT (chprop);
14083 /* If the `cursor' property covers buffer positions up
14084 to and including point, we should display cursor on
14085 this glyph. */
14086 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14087 {
14088 cursor = glyph;
14089 break;
14090 }
14091 }
14092 string_seen = 1;
14093 }
14094 --glyph;
14095 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14096 {
14097 x--; /* can't use any pixel_width */
14098 break;
14099 }
14100 x -= glyph->pixel_width;
14101 }
14102
14103 /* Step 2: If we didn't find an exact match for point, we need to
14104 look for a proper place to put the cursor among glyphs between
14105 GLYPH_BEFORE and GLYPH_AFTER. */
14106 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14107 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14108 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14109 {
14110 /* An empty line has a single glyph whose OBJECT is zero and
14111 whose CHARPOS is the position of a newline on that line.
14112 Note that on a TTY, there are more glyphs after that, which
14113 were produced by extend_face_to_end_of_line, but their
14114 CHARPOS is zero or negative. */
14115 int empty_line_p =
14116 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14117 && INTEGERP (glyph->object) && glyph->charpos > 0
14118 /* On a TTY, continued and truncated rows also have a glyph at
14119 their end whose OBJECT is zero and whose CHARPOS is
14120 positive (the continuation and truncation glyphs), but such
14121 rows are obviously not "empty". */
14122 && !(row->continued_p || row->truncated_on_right_p);
14123
14124 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14125 {
14126 ptrdiff_t ellipsis_pos;
14127
14128 /* Scan back over the ellipsis glyphs. */
14129 if (!row->reversed_p)
14130 {
14131 ellipsis_pos = (glyph - 1)->charpos;
14132 while (glyph > row->glyphs[TEXT_AREA]
14133 && (glyph - 1)->charpos == ellipsis_pos)
14134 glyph--, x -= glyph->pixel_width;
14135 /* That loop always goes one position too far, including
14136 the glyph before the ellipsis. So scan forward over
14137 that one. */
14138 x += glyph->pixel_width;
14139 glyph++;
14140 }
14141 else /* row is reversed */
14142 {
14143 ellipsis_pos = (glyph + 1)->charpos;
14144 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14145 && (glyph + 1)->charpos == ellipsis_pos)
14146 glyph++, x += glyph->pixel_width;
14147 x -= glyph->pixel_width;
14148 glyph--;
14149 }
14150 }
14151 else if (match_with_avoid_cursor)
14152 {
14153 cursor = glyph_after;
14154 x = -1;
14155 }
14156 else if (string_seen)
14157 {
14158 int incr = row->reversed_p ? -1 : +1;
14159
14160 /* Need to find the glyph that came out of a string which is
14161 present at point. That glyph is somewhere between
14162 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14163 positioned between POS_BEFORE and POS_AFTER in the
14164 buffer. */
14165 struct glyph *start, *stop;
14166 ptrdiff_t pos = pos_before;
14167
14168 x = -1;
14169
14170 /* If the row ends in a newline from a display string,
14171 reordering could have moved the glyphs belonging to the
14172 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14173 in this case we extend the search to the last glyph in
14174 the row that was not inserted by redisplay. */
14175 if (row->ends_in_newline_from_string_p)
14176 {
14177 glyph_after = end;
14178 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14179 }
14180
14181 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14182 correspond to POS_BEFORE and POS_AFTER, respectively. We
14183 need START and STOP in the order that corresponds to the
14184 row's direction as given by its reversed_p flag. If the
14185 directionality of characters between POS_BEFORE and
14186 POS_AFTER is the opposite of the row's base direction,
14187 these characters will have been reordered for display,
14188 and we need to reverse START and STOP. */
14189 if (!row->reversed_p)
14190 {
14191 start = min (glyph_before, glyph_after);
14192 stop = max (glyph_before, glyph_after);
14193 }
14194 else
14195 {
14196 start = max (glyph_before, glyph_after);
14197 stop = min (glyph_before, glyph_after);
14198 }
14199 for (glyph = start + incr;
14200 row->reversed_p ? glyph > stop : glyph < stop; )
14201 {
14202
14203 /* Any glyphs that come from the buffer are here because
14204 of bidi reordering. Skip them, and only pay
14205 attention to glyphs that came from some string. */
14206 if (STRINGP (glyph->object))
14207 {
14208 Lisp_Object str;
14209 ptrdiff_t tem;
14210 /* If the display property covers the newline, we
14211 need to search for it one position farther. */
14212 ptrdiff_t lim = pos_after
14213 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14214
14215 string_from_text_prop = 0;
14216 str = glyph->object;
14217 tem = string_buffer_position_lim (str, pos, lim, 0);
14218 if (tem == 0 /* from overlay */
14219 || pos <= tem)
14220 {
14221 /* If the string from which this glyph came is
14222 found in the buffer at point, or at position
14223 that is closer to point than pos_after, then
14224 we've found the glyph we've been looking for.
14225 If it comes from an overlay (tem == 0), and
14226 it has the `cursor' property on one of its
14227 glyphs, record that glyph as a candidate for
14228 displaying the cursor. (As in the
14229 unidirectional version, we will display the
14230 cursor on the last candidate we find.) */
14231 if (tem == 0
14232 || tem == pt_old
14233 || (tem - pt_old > 0 && tem < pos_after))
14234 {
14235 /* The glyphs from this string could have
14236 been reordered. Find the one with the
14237 smallest string position. Or there could
14238 be a character in the string with the
14239 `cursor' property, which means display
14240 cursor on that character's glyph. */
14241 ptrdiff_t strpos = glyph->charpos;
14242
14243 if (tem)
14244 {
14245 cursor = glyph;
14246 string_from_text_prop = 1;
14247 }
14248 for ( ;
14249 (row->reversed_p ? glyph > stop : glyph < stop)
14250 && EQ (glyph->object, str);
14251 glyph += incr)
14252 {
14253 Lisp_Object cprop;
14254 ptrdiff_t gpos = glyph->charpos;
14255
14256 cprop = Fget_char_property (make_number (gpos),
14257 Qcursor,
14258 glyph->object);
14259 if (!NILP (cprop))
14260 {
14261 cursor = glyph;
14262 break;
14263 }
14264 if (tem && glyph->charpos < strpos)
14265 {
14266 strpos = glyph->charpos;
14267 cursor = glyph;
14268 }
14269 }
14270
14271 if (tem == pt_old
14272 || (tem - pt_old > 0 && tem < pos_after))
14273 goto compute_x;
14274 }
14275 if (tem)
14276 pos = tem + 1; /* don't find previous instances */
14277 }
14278 /* This string is not what we want; skip all of the
14279 glyphs that came from it. */
14280 while ((row->reversed_p ? glyph > stop : glyph < stop)
14281 && EQ (glyph->object, str))
14282 glyph += incr;
14283 }
14284 else
14285 glyph += incr;
14286 }
14287
14288 /* If we reached the end of the line, and END was from a string,
14289 the cursor is not on this line. */
14290 if (cursor == NULL
14291 && (row->reversed_p ? glyph <= end : glyph >= end)
14292 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14293 && STRINGP (end->object)
14294 && row->continued_p)
14295 return 0;
14296 }
14297 /* A truncated row may not include PT among its character positions.
14298 Setting the cursor inside the scroll margin will trigger
14299 recalculation of hscroll in hscroll_window_tree. But if a
14300 display string covers point, defer to the string-handling
14301 code below to figure this out. */
14302 else if (row->truncated_on_left_p && pt_old < bpos_min)
14303 {
14304 cursor = glyph_before;
14305 x = -1;
14306 }
14307 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14308 /* Zero-width characters produce no glyphs. */
14309 || (!empty_line_p
14310 && (row->reversed_p
14311 ? glyph_after > glyphs_end
14312 : glyph_after < glyphs_end)))
14313 {
14314 cursor = glyph_after;
14315 x = -1;
14316 }
14317 }
14318
14319 compute_x:
14320 if (cursor != NULL)
14321 glyph = cursor;
14322 else if (glyph == glyphs_end
14323 && pos_before == pos_after
14324 && STRINGP ((row->reversed_p
14325 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14326 : row->glyphs[TEXT_AREA])->object))
14327 {
14328 /* If all the glyphs of this row came from strings, put the
14329 cursor on the first glyph of the row. This avoids having the
14330 cursor outside of the text area in this very rare and hard
14331 use case. */
14332 glyph =
14333 row->reversed_p
14334 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14335 : row->glyphs[TEXT_AREA];
14336 }
14337 if (x < 0)
14338 {
14339 struct glyph *g;
14340
14341 /* Need to compute x that corresponds to GLYPH. */
14342 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14343 {
14344 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14345 emacs_abort ();
14346 x += g->pixel_width;
14347 }
14348 }
14349
14350 /* ROW could be part of a continued line, which, under bidi
14351 reordering, might have other rows whose start and end charpos
14352 occlude point. Only set w->cursor if we found a better
14353 approximation to the cursor position than we have from previously
14354 examined candidate rows belonging to the same continued line. */
14355 if (/* we already have a candidate row */
14356 w->cursor.vpos >= 0
14357 /* that candidate is not the row we are processing */
14358 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14359 /* Make sure cursor.vpos specifies a row whose start and end
14360 charpos occlude point, and it is valid candidate for being a
14361 cursor-row. This is because some callers of this function
14362 leave cursor.vpos at the row where the cursor was displayed
14363 during the last redisplay cycle. */
14364 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14365 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14366 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14367 {
14368 struct glyph *g1 =
14369 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14370
14371 /* Don't consider glyphs that are outside TEXT_AREA. */
14372 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14373 return 0;
14374 /* Keep the candidate whose buffer position is the closest to
14375 point or has the `cursor' property. */
14376 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14377 w->cursor.hpos >= 0
14378 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14379 && ((BUFFERP (g1->object)
14380 && (g1->charpos == pt_old /* an exact match always wins */
14381 || (BUFFERP (glyph->object)
14382 && eabs (g1->charpos - pt_old)
14383 < eabs (glyph->charpos - pt_old))))
14384 /* previous candidate is a glyph from a string that has
14385 a non-nil `cursor' property */
14386 || (STRINGP (g1->object)
14387 && (!NILP (Fget_char_property (make_number (g1->charpos),
14388 Qcursor, g1->object))
14389 /* previous candidate is from the same display
14390 string as this one, and the display string
14391 came from a text property */
14392 || (EQ (g1->object, glyph->object)
14393 && string_from_text_prop)
14394 /* this candidate is from newline and its
14395 position is not an exact match */
14396 || (INTEGERP (glyph->object)
14397 && glyph->charpos != pt_old)))))
14398 return 0;
14399 /* If this candidate gives an exact match, use that. */
14400 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14401 /* If this candidate is a glyph created for the
14402 terminating newline of a line, and point is on that
14403 newline, it wins because it's an exact match. */
14404 || (!row->continued_p
14405 && INTEGERP (glyph->object)
14406 && glyph->charpos == 0
14407 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14408 /* Otherwise, keep the candidate that comes from a row
14409 spanning less buffer positions. This may win when one or
14410 both candidate positions are on glyphs that came from
14411 display strings, for which we cannot compare buffer
14412 positions. */
14413 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14414 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14415 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14416 return 0;
14417 }
14418 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14419 w->cursor.x = x;
14420 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14421 w->cursor.y = row->y + dy;
14422
14423 if (w == XWINDOW (selected_window))
14424 {
14425 if (!row->continued_p
14426 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14427 && row->x == 0)
14428 {
14429 this_line_buffer = XBUFFER (w->contents);
14430
14431 CHARPOS (this_line_start_pos)
14432 = MATRIX_ROW_START_CHARPOS (row) + delta;
14433 BYTEPOS (this_line_start_pos)
14434 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14435
14436 CHARPOS (this_line_end_pos)
14437 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14438 BYTEPOS (this_line_end_pos)
14439 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14440
14441 this_line_y = w->cursor.y;
14442 this_line_pixel_height = row->height;
14443 this_line_vpos = w->cursor.vpos;
14444 this_line_start_x = row->x;
14445 }
14446 else
14447 CHARPOS (this_line_start_pos) = 0;
14448 }
14449
14450 return 1;
14451 }
14452
14453
14454 /* Run window scroll functions, if any, for WINDOW with new window
14455 start STARTP. Sets the window start of WINDOW to that position.
14456
14457 We assume that the window's buffer is really current. */
14458
14459 static struct text_pos
14460 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14461 {
14462 struct window *w = XWINDOW (window);
14463 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14464
14465 eassert (current_buffer == XBUFFER (w->contents));
14466
14467 if (!NILP (Vwindow_scroll_functions))
14468 {
14469 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14470 make_number (CHARPOS (startp)));
14471 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14472 /* In case the hook functions switch buffers. */
14473 set_buffer_internal (XBUFFER (w->contents));
14474 }
14475
14476 return startp;
14477 }
14478
14479
14480 /* Make sure the line containing the cursor is fully visible.
14481 A value of 1 means there is nothing to be done.
14482 (Either the line is fully visible, or it cannot be made so,
14483 or we cannot tell.)
14484
14485 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14486 is higher than window.
14487
14488 A value of 0 means the caller should do scrolling
14489 as if point had gone off the screen. */
14490
14491 static int
14492 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14493 {
14494 struct glyph_matrix *matrix;
14495 struct glyph_row *row;
14496 int window_height;
14497
14498 if (!make_cursor_line_fully_visible_p)
14499 return 1;
14500
14501 /* It's not always possible to find the cursor, e.g, when a window
14502 is full of overlay strings. Don't do anything in that case. */
14503 if (w->cursor.vpos < 0)
14504 return 1;
14505
14506 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14507 row = MATRIX_ROW (matrix, w->cursor.vpos);
14508
14509 /* If the cursor row is not partially visible, there's nothing to do. */
14510 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14511 return 1;
14512
14513 /* If the row the cursor is in is taller than the window's height,
14514 it's not clear what to do, so do nothing. */
14515 window_height = window_box_height (w);
14516 if (row->height >= window_height)
14517 {
14518 if (!force_p || MINI_WINDOW_P (w)
14519 || w->vscroll || w->cursor.vpos == 0)
14520 return 1;
14521 }
14522 return 0;
14523 }
14524
14525
14526 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14527 non-zero means only WINDOW is redisplayed in redisplay_internal.
14528 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14529 in redisplay_window to bring a partially visible line into view in
14530 the case that only the cursor has moved.
14531
14532 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14533 last screen line's vertical height extends past the end of the screen.
14534
14535 Value is
14536
14537 1 if scrolling succeeded
14538
14539 0 if scrolling didn't find point.
14540
14541 -1 if new fonts have been loaded so that we must interrupt
14542 redisplay, adjust glyph matrices, and try again. */
14543
14544 enum
14545 {
14546 SCROLLING_SUCCESS,
14547 SCROLLING_FAILED,
14548 SCROLLING_NEED_LARGER_MATRICES
14549 };
14550
14551 /* If scroll-conservatively is more than this, never recenter.
14552
14553 If you change this, don't forget to update the doc string of
14554 `scroll-conservatively' and the Emacs manual. */
14555 #define SCROLL_LIMIT 100
14556
14557 static int
14558 try_scrolling (Lisp_Object window, int just_this_one_p,
14559 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14560 int temp_scroll_step, int last_line_misfit)
14561 {
14562 struct window *w = XWINDOW (window);
14563 struct frame *f = XFRAME (w->frame);
14564 struct text_pos pos, startp;
14565 struct it it;
14566 int this_scroll_margin, scroll_max, rc, height;
14567 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14568 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14569 Lisp_Object aggressive;
14570 /* We will never try scrolling more than this number of lines. */
14571 int scroll_limit = SCROLL_LIMIT;
14572 int frame_line_height = default_line_pixel_height (w);
14573 int window_total_lines
14574 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14575
14576 #ifdef GLYPH_DEBUG
14577 debug_method_add (w, "try_scrolling");
14578 #endif
14579
14580 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14581
14582 /* Compute scroll margin height in pixels. We scroll when point is
14583 within this distance from the top or bottom of the window. */
14584 if (scroll_margin > 0)
14585 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
14586 * frame_line_height;
14587 else
14588 this_scroll_margin = 0;
14589
14590 /* Force arg_scroll_conservatively to have a reasonable value, to
14591 avoid scrolling too far away with slow move_it_* functions. Note
14592 that the user can supply scroll-conservatively equal to
14593 `most-positive-fixnum', which can be larger than INT_MAX. */
14594 if (arg_scroll_conservatively > scroll_limit)
14595 {
14596 arg_scroll_conservatively = scroll_limit + 1;
14597 scroll_max = scroll_limit * frame_line_height;
14598 }
14599 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14600 /* Compute how much we should try to scroll maximally to bring
14601 point into view. */
14602 scroll_max = (max (scroll_step,
14603 max (arg_scroll_conservatively, temp_scroll_step))
14604 * frame_line_height);
14605 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14606 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14607 /* We're trying to scroll because of aggressive scrolling but no
14608 scroll_step is set. Choose an arbitrary one. */
14609 scroll_max = 10 * frame_line_height;
14610 else
14611 scroll_max = 0;
14612
14613 too_near_end:
14614
14615 /* Decide whether to scroll down. */
14616 if (PT > CHARPOS (startp))
14617 {
14618 int scroll_margin_y;
14619
14620 /* Compute the pixel ypos of the scroll margin, then move IT to
14621 either that ypos or PT, whichever comes first. */
14622 start_display (&it, w, startp);
14623 scroll_margin_y = it.last_visible_y - this_scroll_margin
14624 - frame_line_height * extra_scroll_margin_lines;
14625 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14626 (MOVE_TO_POS | MOVE_TO_Y));
14627
14628 if (PT > CHARPOS (it.current.pos))
14629 {
14630 int y0 = line_bottom_y (&it);
14631 /* Compute how many pixels below window bottom to stop searching
14632 for PT. This avoids costly search for PT that is far away if
14633 the user limited scrolling by a small number of lines, but
14634 always finds PT if scroll_conservatively is set to a large
14635 number, such as most-positive-fixnum. */
14636 int slack = max (scroll_max, 10 * frame_line_height);
14637 int y_to_move = it.last_visible_y + slack;
14638
14639 /* Compute the distance from the scroll margin to PT or to
14640 the scroll limit, whichever comes first. This should
14641 include the height of the cursor line, to make that line
14642 fully visible. */
14643 move_it_to (&it, PT, -1, y_to_move,
14644 -1, MOVE_TO_POS | MOVE_TO_Y);
14645 dy = line_bottom_y (&it) - y0;
14646
14647 if (dy > scroll_max)
14648 return SCROLLING_FAILED;
14649
14650 if (dy > 0)
14651 scroll_down_p = 1;
14652 }
14653 }
14654
14655 if (scroll_down_p)
14656 {
14657 /* Point is in or below the bottom scroll margin, so move the
14658 window start down. If scrolling conservatively, move it just
14659 enough down to make point visible. If scroll_step is set,
14660 move it down by scroll_step. */
14661 if (arg_scroll_conservatively)
14662 amount_to_scroll
14663 = min (max (dy, frame_line_height),
14664 frame_line_height * arg_scroll_conservatively);
14665 else if (scroll_step || temp_scroll_step)
14666 amount_to_scroll = scroll_max;
14667 else
14668 {
14669 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14670 height = WINDOW_BOX_TEXT_HEIGHT (w);
14671 if (NUMBERP (aggressive))
14672 {
14673 double float_amount = XFLOATINT (aggressive) * height;
14674 int aggressive_scroll = float_amount;
14675 if (aggressive_scroll == 0 && float_amount > 0)
14676 aggressive_scroll = 1;
14677 /* Don't let point enter the scroll margin near top of
14678 the window. This could happen if the value of
14679 scroll_up_aggressively is too large and there are
14680 non-zero margins, because scroll_up_aggressively
14681 means put point that fraction of window height
14682 _from_the_bottom_margin_. */
14683 if (aggressive_scroll + 2*this_scroll_margin > height)
14684 aggressive_scroll = height - 2*this_scroll_margin;
14685 amount_to_scroll = dy + aggressive_scroll;
14686 }
14687 }
14688
14689 if (amount_to_scroll <= 0)
14690 return SCROLLING_FAILED;
14691
14692 start_display (&it, w, startp);
14693 if (arg_scroll_conservatively <= scroll_limit)
14694 move_it_vertically (&it, amount_to_scroll);
14695 else
14696 {
14697 /* Extra precision for users who set scroll-conservatively
14698 to a large number: make sure the amount we scroll
14699 the window start is never less than amount_to_scroll,
14700 which was computed as distance from window bottom to
14701 point. This matters when lines at window top and lines
14702 below window bottom have different height. */
14703 struct it it1;
14704 void *it1data = NULL;
14705 /* We use a temporary it1 because line_bottom_y can modify
14706 its argument, if it moves one line down; see there. */
14707 int start_y;
14708
14709 SAVE_IT (it1, it, it1data);
14710 start_y = line_bottom_y (&it1);
14711 do {
14712 RESTORE_IT (&it, &it, it1data);
14713 move_it_by_lines (&it, 1);
14714 SAVE_IT (it1, it, it1data);
14715 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14716 }
14717
14718 /* If STARTP is unchanged, move it down another screen line. */
14719 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14720 move_it_by_lines (&it, 1);
14721 startp = it.current.pos;
14722 }
14723 else
14724 {
14725 struct text_pos scroll_margin_pos = startp;
14726 int y_offset = 0;
14727
14728 /* See if point is inside the scroll margin at the top of the
14729 window. */
14730 if (this_scroll_margin)
14731 {
14732 int y_start;
14733
14734 start_display (&it, w, startp);
14735 y_start = it.current_y;
14736 move_it_vertically (&it, this_scroll_margin);
14737 scroll_margin_pos = it.current.pos;
14738 /* If we didn't move enough before hitting ZV, request
14739 additional amount of scroll, to move point out of the
14740 scroll margin. */
14741 if (IT_CHARPOS (it) == ZV
14742 && it.current_y - y_start < this_scroll_margin)
14743 y_offset = this_scroll_margin - (it.current_y - y_start);
14744 }
14745
14746 if (PT < CHARPOS (scroll_margin_pos))
14747 {
14748 /* Point is in the scroll margin at the top of the window or
14749 above what is displayed in the window. */
14750 int y0, y_to_move;
14751
14752 /* Compute the vertical distance from PT to the scroll
14753 margin position. Move as far as scroll_max allows, or
14754 one screenful, or 10 screen lines, whichever is largest.
14755 Give up if distance is greater than scroll_max or if we
14756 didn't reach the scroll margin position. */
14757 SET_TEXT_POS (pos, PT, PT_BYTE);
14758 start_display (&it, w, pos);
14759 y0 = it.current_y;
14760 y_to_move = max (it.last_visible_y,
14761 max (scroll_max, 10 * frame_line_height));
14762 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14763 y_to_move, -1,
14764 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14765 dy = it.current_y - y0;
14766 if (dy > scroll_max
14767 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
14768 return SCROLLING_FAILED;
14769
14770 /* Additional scroll for when ZV was too close to point. */
14771 dy += y_offset;
14772
14773 /* Compute new window start. */
14774 start_display (&it, w, startp);
14775
14776 if (arg_scroll_conservatively)
14777 amount_to_scroll = max (dy, frame_line_height *
14778 max (scroll_step, temp_scroll_step));
14779 else if (scroll_step || temp_scroll_step)
14780 amount_to_scroll = scroll_max;
14781 else
14782 {
14783 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14784 height = WINDOW_BOX_TEXT_HEIGHT (w);
14785 if (NUMBERP (aggressive))
14786 {
14787 double float_amount = XFLOATINT (aggressive) * height;
14788 int aggressive_scroll = float_amount;
14789 if (aggressive_scroll == 0 && float_amount > 0)
14790 aggressive_scroll = 1;
14791 /* Don't let point enter the scroll margin near
14792 bottom of the window, if the value of
14793 scroll_down_aggressively happens to be too
14794 large. */
14795 if (aggressive_scroll + 2*this_scroll_margin > height)
14796 aggressive_scroll = height - 2*this_scroll_margin;
14797 amount_to_scroll = dy + aggressive_scroll;
14798 }
14799 }
14800
14801 if (amount_to_scroll <= 0)
14802 return SCROLLING_FAILED;
14803
14804 move_it_vertically_backward (&it, amount_to_scroll);
14805 startp = it.current.pos;
14806 }
14807 }
14808
14809 /* Run window scroll functions. */
14810 startp = run_window_scroll_functions (window, startp);
14811
14812 /* Display the window. Give up if new fonts are loaded, or if point
14813 doesn't appear. */
14814 if (!try_window (window, startp, 0))
14815 rc = SCROLLING_NEED_LARGER_MATRICES;
14816 else if (w->cursor.vpos < 0)
14817 {
14818 clear_glyph_matrix (w->desired_matrix);
14819 rc = SCROLLING_FAILED;
14820 }
14821 else
14822 {
14823 /* Maybe forget recorded base line for line number display. */
14824 if (!just_this_one_p
14825 || current_buffer->clip_changed
14826 || BEG_UNCHANGED < CHARPOS (startp))
14827 w->base_line_number = 0;
14828
14829 /* If cursor ends up on a partially visible line,
14830 treat that as being off the bottom of the screen. */
14831 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14832 /* It's possible that the cursor is on the first line of the
14833 buffer, which is partially obscured due to a vscroll
14834 (Bug#7537). In that case, avoid looping forever . */
14835 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14836 {
14837 clear_glyph_matrix (w->desired_matrix);
14838 ++extra_scroll_margin_lines;
14839 goto too_near_end;
14840 }
14841 rc = SCROLLING_SUCCESS;
14842 }
14843
14844 return rc;
14845 }
14846
14847
14848 /* Compute a suitable window start for window W if display of W starts
14849 on a continuation line. Value is non-zero if a new window start
14850 was computed.
14851
14852 The new window start will be computed, based on W's width, starting
14853 from the start of the continued line. It is the start of the
14854 screen line with the minimum distance from the old start W->start. */
14855
14856 static int
14857 compute_window_start_on_continuation_line (struct window *w)
14858 {
14859 struct text_pos pos, start_pos;
14860 int window_start_changed_p = 0;
14861
14862 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14863
14864 /* If window start is on a continuation line... Window start may be
14865 < BEGV in case there's invisible text at the start of the
14866 buffer (M-x rmail, for example). */
14867 if (CHARPOS (start_pos) > BEGV
14868 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14869 {
14870 struct it it;
14871 struct glyph_row *row;
14872
14873 /* Handle the case that the window start is out of range. */
14874 if (CHARPOS (start_pos) < BEGV)
14875 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14876 else if (CHARPOS (start_pos) > ZV)
14877 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14878
14879 /* Find the start of the continued line. This should be fast
14880 because find_newline is fast (newline cache). */
14881 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14882 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14883 row, DEFAULT_FACE_ID);
14884 reseat_at_previous_visible_line_start (&it);
14885
14886 /* If the line start is "too far" away from the window start,
14887 say it takes too much time to compute a new window start. */
14888 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14889 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14890 {
14891 int min_distance, distance;
14892
14893 /* Move forward by display lines to find the new window
14894 start. If window width was enlarged, the new start can
14895 be expected to be > the old start. If window width was
14896 decreased, the new window start will be < the old start.
14897 So, we're looking for the display line start with the
14898 minimum distance from the old window start. */
14899 pos = it.current.pos;
14900 min_distance = INFINITY;
14901 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14902 distance < min_distance)
14903 {
14904 min_distance = distance;
14905 pos = it.current.pos;
14906 move_it_by_lines (&it, 1);
14907 }
14908
14909 /* Set the window start there. */
14910 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14911 window_start_changed_p = 1;
14912 }
14913 }
14914
14915 return window_start_changed_p;
14916 }
14917
14918
14919 /* Try cursor movement in case text has not changed in window WINDOW,
14920 with window start STARTP. Value is
14921
14922 CURSOR_MOVEMENT_SUCCESS if successful
14923
14924 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14925
14926 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14927 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14928 we want to scroll as if scroll-step were set to 1. See the code.
14929
14930 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14931 which case we have to abort this redisplay, and adjust matrices
14932 first. */
14933
14934 enum
14935 {
14936 CURSOR_MOVEMENT_SUCCESS,
14937 CURSOR_MOVEMENT_CANNOT_BE_USED,
14938 CURSOR_MOVEMENT_MUST_SCROLL,
14939 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14940 };
14941
14942 static int
14943 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14944 {
14945 struct window *w = XWINDOW (window);
14946 struct frame *f = XFRAME (w->frame);
14947 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14948
14949 #ifdef GLYPH_DEBUG
14950 if (inhibit_try_cursor_movement)
14951 return rc;
14952 #endif
14953
14954 /* Previously, there was a check for Lisp integer in the
14955 if-statement below. Now, this field is converted to
14956 ptrdiff_t, thus zero means invalid position in a buffer. */
14957 eassert (w->last_point > 0);
14958 /* Likewise there was a check whether window_end_vpos is nil or larger
14959 than the window. Now window_end_vpos is int and so never nil, but
14960 let's leave eassert to check whether it fits in the window. */
14961 eassert (w->window_end_vpos < w->current_matrix->nrows);
14962
14963 /* Handle case where text has not changed, only point, and it has
14964 not moved off the frame. */
14965 if (/* Point may be in this window. */
14966 PT >= CHARPOS (startp)
14967 /* Selective display hasn't changed. */
14968 && !current_buffer->clip_changed
14969 /* Function force-mode-line-update is used to force a thorough
14970 redisplay. It sets either windows_or_buffers_changed or
14971 update_mode_lines. So don't take a shortcut here for these
14972 cases. */
14973 && !update_mode_lines
14974 && !windows_or_buffers_changed
14975 && !cursor_type_changed
14976 /* Can't use this case if highlighting a region. When a
14977 region exists, cursor movement has to do more than just
14978 set the cursor. */
14979 && markpos_of_region () < 0
14980 && !w->region_showing
14981 && NILP (Vshow_trailing_whitespace)
14982 /* This code is not used for mini-buffer for the sake of the case
14983 of redisplaying to replace an echo area message; since in
14984 that case the mini-buffer contents per se are usually
14985 unchanged. This code is of no real use in the mini-buffer
14986 since the handling of this_line_start_pos, etc., in redisplay
14987 handles the same cases. */
14988 && !EQ (window, minibuf_window)
14989 && (FRAME_WINDOW_P (f)
14990 || !overlay_arrow_in_current_buffer_p ()))
14991 {
14992 int this_scroll_margin, top_scroll_margin;
14993 struct glyph_row *row = NULL;
14994 int frame_line_height = default_line_pixel_height (w);
14995 int window_total_lines
14996 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14997
14998 #ifdef GLYPH_DEBUG
14999 debug_method_add (w, "cursor movement");
15000 #endif
15001
15002 /* Scroll if point within this distance from the top or bottom
15003 of the window. This is a pixel value. */
15004 if (scroll_margin > 0)
15005 {
15006 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15007 this_scroll_margin *= frame_line_height;
15008 }
15009 else
15010 this_scroll_margin = 0;
15011
15012 top_scroll_margin = this_scroll_margin;
15013 if (WINDOW_WANTS_HEADER_LINE_P (w))
15014 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15015
15016 /* Start with the row the cursor was displayed during the last
15017 not paused redisplay. Give up if that row is not valid. */
15018 if (w->last_cursor.vpos < 0
15019 || w->last_cursor.vpos >= w->current_matrix->nrows)
15020 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15021 else
15022 {
15023 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
15024 if (row->mode_line_p)
15025 ++row;
15026 if (!row->enabled_p)
15027 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15028 }
15029
15030 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15031 {
15032 int scroll_p = 0, must_scroll = 0;
15033 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15034
15035 if (PT > w->last_point)
15036 {
15037 /* Point has moved forward. */
15038 while (MATRIX_ROW_END_CHARPOS (row) < PT
15039 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15040 {
15041 eassert (row->enabled_p);
15042 ++row;
15043 }
15044
15045 /* If the end position of a row equals the start
15046 position of the next row, and PT is at that position,
15047 we would rather display cursor in the next line. */
15048 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15049 && MATRIX_ROW_END_CHARPOS (row) == PT
15050 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15051 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15052 && !cursor_row_p (row))
15053 ++row;
15054
15055 /* If within the scroll margin, scroll. Note that
15056 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15057 the next line would be drawn, and that
15058 this_scroll_margin can be zero. */
15059 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15060 || PT > MATRIX_ROW_END_CHARPOS (row)
15061 /* Line is completely visible last line in window
15062 and PT is to be set in the next line. */
15063 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15064 && PT == MATRIX_ROW_END_CHARPOS (row)
15065 && !row->ends_at_zv_p
15066 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15067 scroll_p = 1;
15068 }
15069 else if (PT < w->last_point)
15070 {
15071 /* Cursor has to be moved backward. Note that PT >=
15072 CHARPOS (startp) because of the outer if-statement. */
15073 while (!row->mode_line_p
15074 && (MATRIX_ROW_START_CHARPOS (row) > PT
15075 || (MATRIX_ROW_START_CHARPOS (row) == PT
15076 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15077 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15078 row > w->current_matrix->rows
15079 && (row-1)->ends_in_newline_from_string_p))))
15080 && (row->y > top_scroll_margin
15081 || CHARPOS (startp) == BEGV))
15082 {
15083 eassert (row->enabled_p);
15084 --row;
15085 }
15086
15087 /* Consider the following case: Window starts at BEGV,
15088 there is invisible, intangible text at BEGV, so that
15089 display starts at some point START > BEGV. It can
15090 happen that we are called with PT somewhere between
15091 BEGV and START. Try to handle that case. */
15092 if (row < w->current_matrix->rows
15093 || row->mode_line_p)
15094 {
15095 row = w->current_matrix->rows;
15096 if (row->mode_line_p)
15097 ++row;
15098 }
15099
15100 /* Due to newlines in overlay strings, we may have to
15101 skip forward over overlay strings. */
15102 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15103 && MATRIX_ROW_END_CHARPOS (row) == PT
15104 && !cursor_row_p (row))
15105 ++row;
15106
15107 /* If within the scroll margin, scroll. */
15108 if (row->y < top_scroll_margin
15109 && CHARPOS (startp) != BEGV)
15110 scroll_p = 1;
15111 }
15112 else
15113 {
15114 /* Cursor did not move. So don't scroll even if cursor line
15115 is partially visible, as it was so before. */
15116 rc = CURSOR_MOVEMENT_SUCCESS;
15117 }
15118
15119 if (PT < MATRIX_ROW_START_CHARPOS (row)
15120 || PT > MATRIX_ROW_END_CHARPOS (row))
15121 {
15122 /* if PT is not in the glyph row, give up. */
15123 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15124 must_scroll = 1;
15125 }
15126 else if (rc != CURSOR_MOVEMENT_SUCCESS
15127 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15128 {
15129 struct glyph_row *row1;
15130
15131 /* If rows are bidi-reordered and point moved, back up
15132 until we find a row that does not belong to a
15133 continuation line. This is because we must consider
15134 all rows of a continued line as candidates for the
15135 new cursor positioning, since row start and end
15136 positions change non-linearly with vertical position
15137 in such rows. */
15138 /* FIXME: Revisit this when glyph ``spilling'' in
15139 continuation lines' rows is implemented for
15140 bidi-reordered rows. */
15141 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15142 MATRIX_ROW_CONTINUATION_LINE_P (row);
15143 --row)
15144 {
15145 /* If we hit the beginning of the displayed portion
15146 without finding the first row of a continued
15147 line, give up. */
15148 if (row <= row1)
15149 {
15150 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15151 break;
15152 }
15153 eassert (row->enabled_p);
15154 }
15155 }
15156 if (must_scroll)
15157 ;
15158 else if (rc != CURSOR_MOVEMENT_SUCCESS
15159 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15160 /* Make sure this isn't a header line by any chance, since
15161 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15162 && !row->mode_line_p
15163 && make_cursor_line_fully_visible_p)
15164 {
15165 if (PT == MATRIX_ROW_END_CHARPOS (row)
15166 && !row->ends_at_zv_p
15167 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15168 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15169 else if (row->height > window_box_height (w))
15170 {
15171 /* If we end up in a partially visible line, let's
15172 make it fully visible, except when it's taller
15173 than the window, in which case we can't do much
15174 about it. */
15175 *scroll_step = 1;
15176 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15177 }
15178 else
15179 {
15180 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15181 if (!cursor_row_fully_visible_p (w, 0, 1))
15182 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15183 else
15184 rc = CURSOR_MOVEMENT_SUCCESS;
15185 }
15186 }
15187 else if (scroll_p)
15188 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15189 else if (rc != CURSOR_MOVEMENT_SUCCESS
15190 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15191 {
15192 /* With bidi-reordered rows, there could be more than
15193 one candidate row whose start and end positions
15194 occlude point. We need to let set_cursor_from_row
15195 find the best candidate. */
15196 /* FIXME: Revisit this when glyph ``spilling'' in
15197 continuation lines' rows is implemented for
15198 bidi-reordered rows. */
15199 int rv = 0;
15200
15201 do
15202 {
15203 int at_zv_p = 0, exact_match_p = 0;
15204
15205 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15206 && PT <= MATRIX_ROW_END_CHARPOS (row)
15207 && cursor_row_p (row))
15208 rv |= set_cursor_from_row (w, row, w->current_matrix,
15209 0, 0, 0, 0);
15210 /* As soon as we've found the exact match for point,
15211 or the first suitable row whose ends_at_zv_p flag
15212 is set, we are done. */
15213 at_zv_p =
15214 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15215 if (rv && !at_zv_p
15216 && w->cursor.hpos >= 0
15217 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15218 w->cursor.vpos))
15219 {
15220 struct glyph_row *candidate =
15221 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15222 struct glyph *g =
15223 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15224 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15225
15226 exact_match_p =
15227 (BUFFERP (g->object) && g->charpos == PT)
15228 || (INTEGERP (g->object)
15229 && (g->charpos == PT
15230 || (g->charpos == 0 && endpos - 1 == PT)));
15231 }
15232 if (rv && (at_zv_p || exact_match_p))
15233 {
15234 rc = CURSOR_MOVEMENT_SUCCESS;
15235 break;
15236 }
15237 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15238 break;
15239 ++row;
15240 }
15241 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15242 || row->continued_p)
15243 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15244 || (MATRIX_ROW_START_CHARPOS (row) == PT
15245 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15246 /* If we didn't find any candidate rows, or exited the
15247 loop before all the candidates were examined, signal
15248 to the caller that this method failed. */
15249 if (rc != CURSOR_MOVEMENT_SUCCESS
15250 && !(rv
15251 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15252 && !row->continued_p))
15253 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15254 else if (rv)
15255 rc = CURSOR_MOVEMENT_SUCCESS;
15256 }
15257 else
15258 {
15259 do
15260 {
15261 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15262 {
15263 rc = CURSOR_MOVEMENT_SUCCESS;
15264 break;
15265 }
15266 ++row;
15267 }
15268 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15269 && MATRIX_ROW_START_CHARPOS (row) == PT
15270 && cursor_row_p (row));
15271 }
15272 }
15273 }
15274
15275 return rc;
15276 }
15277
15278 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15279 static
15280 #endif
15281 void
15282 set_vertical_scroll_bar (struct window *w)
15283 {
15284 ptrdiff_t start, end, whole;
15285
15286 /* Calculate the start and end positions for the current window.
15287 At some point, it would be nice to choose between scrollbars
15288 which reflect the whole buffer size, with special markers
15289 indicating narrowing, and scrollbars which reflect only the
15290 visible region.
15291
15292 Note that mini-buffers sometimes aren't displaying any text. */
15293 if (!MINI_WINDOW_P (w)
15294 || (w == XWINDOW (minibuf_window)
15295 && NILP (echo_area_buffer[0])))
15296 {
15297 struct buffer *buf = XBUFFER (w->contents);
15298 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15299 start = marker_position (w->start) - BUF_BEGV (buf);
15300 /* I don't think this is guaranteed to be right. For the
15301 moment, we'll pretend it is. */
15302 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15303
15304 if (end < start)
15305 end = start;
15306 if (whole < (end - start))
15307 whole = end - start;
15308 }
15309 else
15310 start = end = whole = 0;
15311
15312 /* Indicate what this scroll bar ought to be displaying now. */
15313 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15314 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15315 (w, end - start, whole, start);
15316 }
15317
15318
15319 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15320 selected_window is redisplayed.
15321
15322 We can return without actually redisplaying the window if
15323 fonts_changed_p. In that case, redisplay_internal will
15324 retry. */
15325
15326 static void
15327 redisplay_window (Lisp_Object window, int just_this_one_p)
15328 {
15329 struct window *w = XWINDOW (window);
15330 struct frame *f = XFRAME (w->frame);
15331 struct buffer *buffer = XBUFFER (w->contents);
15332 struct buffer *old = current_buffer;
15333 struct text_pos lpoint, opoint, startp;
15334 int update_mode_line;
15335 int tem;
15336 struct it it;
15337 /* Record it now because it's overwritten. */
15338 int current_matrix_up_to_date_p = 0;
15339 int used_current_matrix_p = 0;
15340 /* This is less strict than current_matrix_up_to_date_p.
15341 It indicates that the buffer contents and narrowing are unchanged. */
15342 int buffer_unchanged_p = 0;
15343 int temp_scroll_step = 0;
15344 ptrdiff_t count = SPECPDL_INDEX ();
15345 int rc;
15346 int centering_position = -1;
15347 int last_line_misfit = 0;
15348 ptrdiff_t beg_unchanged, end_unchanged;
15349 int frame_line_height;
15350
15351 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15352 opoint = lpoint;
15353
15354 #ifdef GLYPH_DEBUG
15355 *w->desired_matrix->method = 0;
15356 #endif
15357
15358 /* Make sure that both W's markers are valid. */
15359 eassert (XMARKER (w->start)->buffer == buffer);
15360 eassert (XMARKER (w->pointm)->buffer == buffer);
15361
15362 restart:
15363 reconsider_clip_changes (w);
15364 frame_line_height = default_line_pixel_height (w);
15365
15366 /* Has the mode line to be updated? */
15367 update_mode_line = (w->update_mode_line
15368 || update_mode_lines
15369 || buffer->clip_changed
15370 || buffer->prevent_redisplay_optimizations_p);
15371
15372 if (MINI_WINDOW_P (w))
15373 {
15374 if (w == XWINDOW (echo_area_window)
15375 && !NILP (echo_area_buffer[0]))
15376 {
15377 if (update_mode_line)
15378 /* We may have to update a tty frame's menu bar or a
15379 tool-bar. Example `M-x C-h C-h C-g'. */
15380 goto finish_menu_bars;
15381 else
15382 /* We've already displayed the echo area glyphs in this window. */
15383 goto finish_scroll_bars;
15384 }
15385 else if ((w != XWINDOW (minibuf_window)
15386 || minibuf_level == 0)
15387 /* When buffer is nonempty, redisplay window normally. */
15388 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15389 /* Quail displays non-mini buffers in minibuffer window.
15390 In that case, redisplay the window normally. */
15391 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15392 {
15393 /* W is a mini-buffer window, but it's not active, so clear
15394 it. */
15395 int yb = window_text_bottom_y (w);
15396 struct glyph_row *row;
15397 int y;
15398
15399 for (y = 0, row = w->desired_matrix->rows;
15400 y < yb;
15401 y += row->height, ++row)
15402 blank_row (w, row, y);
15403 goto finish_scroll_bars;
15404 }
15405
15406 clear_glyph_matrix (w->desired_matrix);
15407 }
15408
15409 /* Otherwise set up data on this window; select its buffer and point
15410 value. */
15411 /* Really select the buffer, for the sake of buffer-local
15412 variables. */
15413 set_buffer_internal_1 (XBUFFER (w->contents));
15414
15415 current_matrix_up_to_date_p
15416 = (w->window_end_valid
15417 && !current_buffer->clip_changed
15418 && !current_buffer->prevent_redisplay_optimizations_p
15419 && !window_outdated (w));
15420
15421 /* Run the window-bottom-change-functions
15422 if it is possible that the text on the screen has changed
15423 (either due to modification of the text, or any other reason). */
15424 if (!current_matrix_up_to_date_p
15425 && !NILP (Vwindow_text_change_functions))
15426 {
15427 safe_run_hooks (Qwindow_text_change_functions);
15428 goto restart;
15429 }
15430
15431 beg_unchanged = BEG_UNCHANGED;
15432 end_unchanged = END_UNCHANGED;
15433
15434 SET_TEXT_POS (opoint, PT, PT_BYTE);
15435
15436 specbind (Qinhibit_point_motion_hooks, Qt);
15437
15438 buffer_unchanged_p
15439 = (w->window_end_valid
15440 && !current_buffer->clip_changed
15441 && !window_outdated (w));
15442
15443 /* When windows_or_buffers_changed is non-zero, we can't rely on
15444 the window end being valid, so set it to nil there. */
15445 if (windows_or_buffers_changed)
15446 {
15447 /* If window starts on a continuation line, maybe adjust the
15448 window start in case the window's width changed. */
15449 if (XMARKER (w->start)->buffer == current_buffer)
15450 compute_window_start_on_continuation_line (w);
15451
15452 w->window_end_valid = 0;
15453 }
15454
15455 /* Some sanity checks. */
15456 CHECK_WINDOW_END (w);
15457 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15458 emacs_abort ();
15459 if (BYTEPOS (opoint) < CHARPOS (opoint))
15460 emacs_abort ();
15461
15462 if (mode_line_update_needed (w))
15463 update_mode_line = 1;
15464
15465 /* Point refers normally to the selected window. For any other
15466 window, set up appropriate value. */
15467 if (!EQ (window, selected_window))
15468 {
15469 ptrdiff_t new_pt = marker_position (w->pointm);
15470 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15471 if (new_pt < BEGV)
15472 {
15473 new_pt = BEGV;
15474 new_pt_byte = BEGV_BYTE;
15475 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15476 }
15477 else if (new_pt > (ZV - 1))
15478 {
15479 new_pt = ZV;
15480 new_pt_byte = ZV_BYTE;
15481 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15482 }
15483
15484 /* We don't use SET_PT so that the point-motion hooks don't run. */
15485 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15486 }
15487
15488 /* If any of the character widths specified in the display table
15489 have changed, invalidate the width run cache. It's true that
15490 this may be a bit late to catch such changes, but the rest of
15491 redisplay goes (non-fatally) haywire when the display table is
15492 changed, so why should we worry about doing any better? */
15493 if (current_buffer->width_run_cache)
15494 {
15495 struct Lisp_Char_Table *disptab = buffer_display_table ();
15496
15497 if (! disptab_matches_widthtab
15498 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15499 {
15500 invalidate_region_cache (current_buffer,
15501 current_buffer->width_run_cache,
15502 BEG, Z);
15503 recompute_width_table (current_buffer, disptab);
15504 }
15505 }
15506
15507 /* If window-start is screwed up, choose a new one. */
15508 if (XMARKER (w->start)->buffer != current_buffer)
15509 goto recenter;
15510
15511 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15512
15513 /* If someone specified a new starting point but did not insist,
15514 check whether it can be used. */
15515 if (w->optional_new_start
15516 && CHARPOS (startp) >= BEGV
15517 && CHARPOS (startp) <= ZV)
15518 {
15519 w->optional_new_start = 0;
15520 start_display (&it, w, startp);
15521 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15522 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15523 if (IT_CHARPOS (it) == PT)
15524 w->force_start = 1;
15525 /* IT may overshoot PT if text at PT is invisible. */
15526 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15527 w->force_start = 1;
15528 }
15529
15530 force_start:
15531
15532 /* Handle case where place to start displaying has been specified,
15533 unless the specified location is outside the accessible range. */
15534 if (w->force_start || window_frozen_p (w))
15535 {
15536 /* We set this later on if we have to adjust point. */
15537 int new_vpos = -1;
15538
15539 w->force_start = 0;
15540 w->vscroll = 0;
15541 w->window_end_valid = 0;
15542
15543 /* Forget any recorded base line for line number display. */
15544 if (!buffer_unchanged_p)
15545 w->base_line_number = 0;
15546
15547 /* Redisplay the mode line. Select the buffer properly for that.
15548 Also, run the hook window-scroll-functions
15549 because we have scrolled. */
15550 /* Note, we do this after clearing force_start because
15551 if there's an error, it is better to forget about force_start
15552 than to get into an infinite loop calling the hook functions
15553 and having them get more errors. */
15554 if (!update_mode_line
15555 || ! NILP (Vwindow_scroll_functions))
15556 {
15557 update_mode_line = 1;
15558 w->update_mode_line = 1;
15559 startp = run_window_scroll_functions (window, startp);
15560 }
15561
15562 if (CHARPOS (startp) < BEGV)
15563 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15564 else if (CHARPOS (startp) > ZV)
15565 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15566
15567 /* Redisplay, then check if cursor has been set during the
15568 redisplay. Give up if new fonts were loaded. */
15569 /* We used to issue a CHECK_MARGINS argument to try_window here,
15570 but this causes scrolling to fail when point begins inside
15571 the scroll margin (bug#148) -- cyd */
15572 if (!try_window (window, startp, 0))
15573 {
15574 w->force_start = 1;
15575 clear_glyph_matrix (w->desired_matrix);
15576 goto need_larger_matrices;
15577 }
15578
15579 if (w->cursor.vpos < 0 && !window_frozen_p (w))
15580 {
15581 /* If point does not appear, try to move point so it does
15582 appear. The desired matrix has been built above, so we
15583 can use it here. */
15584 new_vpos = window_box_height (w) / 2;
15585 }
15586
15587 if (!cursor_row_fully_visible_p (w, 0, 0))
15588 {
15589 /* Point does appear, but on a line partly visible at end of window.
15590 Move it back to a fully-visible line. */
15591 new_vpos = window_box_height (w);
15592 }
15593 else if (w->cursor.vpos >=0)
15594 {
15595 /* Some people insist on not letting point enter the scroll
15596 margin, even though this part handles windows that didn't
15597 scroll at all. */
15598 int window_total_lines
15599 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15600 int margin = min (scroll_margin, window_total_lines / 4);
15601 int pixel_margin = margin * frame_line_height;
15602 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15603
15604 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15605 below, which finds the row to move point to, advances by
15606 the Y coordinate of the _next_ row, see the definition of
15607 MATRIX_ROW_BOTTOM_Y. */
15608 if (w->cursor.vpos < margin + header_line)
15609 {
15610 w->cursor.vpos = -1;
15611 clear_glyph_matrix (w->desired_matrix);
15612 goto try_to_scroll;
15613 }
15614 else
15615 {
15616 int window_height = window_box_height (w);
15617
15618 if (header_line)
15619 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15620 if (w->cursor.y >= window_height - pixel_margin)
15621 {
15622 w->cursor.vpos = -1;
15623 clear_glyph_matrix (w->desired_matrix);
15624 goto try_to_scroll;
15625 }
15626 }
15627 }
15628
15629 /* If we need to move point for either of the above reasons,
15630 now actually do it. */
15631 if (new_vpos >= 0)
15632 {
15633 struct glyph_row *row;
15634
15635 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15636 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15637 ++row;
15638
15639 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15640 MATRIX_ROW_START_BYTEPOS (row));
15641
15642 if (w != XWINDOW (selected_window))
15643 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15644 else if (current_buffer == old)
15645 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15646
15647 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15648
15649 /* If we are highlighting the region, then we just changed
15650 the region, so redisplay to show it. */
15651 if (markpos_of_region () >= 0)
15652 {
15653 clear_glyph_matrix (w->desired_matrix);
15654 if (!try_window (window, startp, 0))
15655 goto need_larger_matrices;
15656 }
15657 }
15658
15659 #ifdef GLYPH_DEBUG
15660 debug_method_add (w, "forced window start");
15661 #endif
15662 goto done;
15663 }
15664
15665 /* Handle case where text has not changed, only point, and it has
15666 not moved off the frame, and we are not retrying after hscroll.
15667 (current_matrix_up_to_date_p is nonzero when retrying.) */
15668 if (current_matrix_up_to_date_p
15669 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15670 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15671 {
15672 switch (rc)
15673 {
15674 case CURSOR_MOVEMENT_SUCCESS:
15675 used_current_matrix_p = 1;
15676 goto done;
15677
15678 case CURSOR_MOVEMENT_MUST_SCROLL:
15679 goto try_to_scroll;
15680
15681 default:
15682 emacs_abort ();
15683 }
15684 }
15685 /* If current starting point was originally the beginning of a line
15686 but no longer is, find a new starting point. */
15687 else if (w->start_at_line_beg
15688 && !(CHARPOS (startp) <= BEGV
15689 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15690 {
15691 #ifdef GLYPH_DEBUG
15692 debug_method_add (w, "recenter 1");
15693 #endif
15694 goto recenter;
15695 }
15696
15697 /* Try scrolling with try_window_id. Value is > 0 if update has
15698 been done, it is -1 if we know that the same window start will
15699 not work. It is 0 if unsuccessful for some other reason. */
15700 else if ((tem = try_window_id (w)) != 0)
15701 {
15702 #ifdef GLYPH_DEBUG
15703 debug_method_add (w, "try_window_id %d", tem);
15704 #endif
15705
15706 if (fonts_changed_p)
15707 goto need_larger_matrices;
15708 if (tem > 0)
15709 goto done;
15710
15711 /* Otherwise try_window_id has returned -1 which means that we
15712 don't want the alternative below this comment to execute. */
15713 }
15714 else if (CHARPOS (startp) >= BEGV
15715 && CHARPOS (startp) <= ZV
15716 && PT >= CHARPOS (startp)
15717 && (CHARPOS (startp) < ZV
15718 /* Avoid starting at end of buffer. */
15719 || CHARPOS (startp) == BEGV
15720 || !window_outdated (w)))
15721 {
15722 int d1, d2, d3, d4, d5, d6;
15723
15724 /* If first window line is a continuation line, and window start
15725 is inside the modified region, but the first change is before
15726 current window start, we must select a new window start.
15727
15728 However, if this is the result of a down-mouse event (e.g. by
15729 extending the mouse-drag-overlay), we don't want to select a
15730 new window start, since that would change the position under
15731 the mouse, resulting in an unwanted mouse-movement rather
15732 than a simple mouse-click. */
15733 if (!w->start_at_line_beg
15734 && NILP (do_mouse_tracking)
15735 && CHARPOS (startp) > BEGV
15736 && CHARPOS (startp) > BEG + beg_unchanged
15737 && CHARPOS (startp) <= Z - end_unchanged
15738 /* Even if w->start_at_line_beg is nil, a new window may
15739 start at a line_beg, since that's how set_buffer_window
15740 sets it. So, we need to check the return value of
15741 compute_window_start_on_continuation_line. (See also
15742 bug#197). */
15743 && XMARKER (w->start)->buffer == current_buffer
15744 && compute_window_start_on_continuation_line (w)
15745 /* It doesn't make sense to force the window start like we
15746 do at label force_start if it is already known that point
15747 will not be visible in the resulting window, because
15748 doing so will move point from its correct position
15749 instead of scrolling the window to bring point into view.
15750 See bug#9324. */
15751 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15752 {
15753 w->force_start = 1;
15754 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15755 goto force_start;
15756 }
15757
15758 #ifdef GLYPH_DEBUG
15759 debug_method_add (w, "same window start");
15760 #endif
15761
15762 /* Try to redisplay starting at same place as before.
15763 If point has not moved off frame, accept the results. */
15764 if (!current_matrix_up_to_date_p
15765 /* Don't use try_window_reusing_current_matrix in this case
15766 because a window scroll function can have changed the
15767 buffer. */
15768 || !NILP (Vwindow_scroll_functions)
15769 || MINI_WINDOW_P (w)
15770 || !(used_current_matrix_p
15771 = try_window_reusing_current_matrix (w)))
15772 {
15773 IF_DEBUG (debug_method_add (w, "1"));
15774 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15775 /* -1 means we need to scroll.
15776 0 means we need new matrices, but fonts_changed_p
15777 is set in that case, so we will detect it below. */
15778 goto try_to_scroll;
15779 }
15780
15781 if (fonts_changed_p)
15782 goto need_larger_matrices;
15783
15784 if (w->cursor.vpos >= 0)
15785 {
15786 if (!just_this_one_p
15787 || current_buffer->clip_changed
15788 || BEG_UNCHANGED < CHARPOS (startp))
15789 /* Forget any recorded base line for line number display. */
15790 w->base_line_number = 0;
15791
15792 if (!cursor_row_fully_visible_p (w, 1, 0))
15793 {
15794 clear_glyph_matrix (w->desired_matrix);
15795 last_line_misfit = 1;
15796 }
15797 /* Drop through and scroll. */
15798 else
15799 goto done;
15800 }
15801 else
15802 clear_glyph_matrix (w->desired_matrix);
15803 }
15804
15805 try_to_scroll:
15806
15807 /* Redisplay the mode line. Select the buffer properly for that. */
15808 if (!update_mode_line)
15809 {
15810 update_mode_line = 1;
15811 w->update_mode_line = 1;
15812 }
15813
15814 /* Try to scroll by specified few lines. */
15815 if ((scroll_conservatively
15816 || emacs_scroll_step
15817 || temp_scroll_step
15818 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15819 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15820 && CHARPOS (startp) >= BEGV
15821 && CHARPOS (startp) <= ZV)
15822 {
15823 /* The function returns -1 if new fonts were loaded, 1 if
15824 successful, 0 if not successful. */
15825 int ss = try_scrolling (window, just_this_one_p,
15826 scroll_conservatively,
15827 emacs_scroll_step,
15828 temp_scroll_step, last_line_misfit);
15829 switch (ss)
15830 {
15831 case SCROLLING_SUCCESS:
15832 goto done;
15833
15834 case SCROLLING_NEED_LARGER_MATRICES:
15835 goto need_larger_matrices;
15836
15837 case SCROLLING_FAILED:
15838 break;
15839
15840 default:
15841 emacs_abort ();
15842 }
15843 }
15844
15845 /* Finally, just choose a place to start which positions point
15846 according to user preferences. */
15847
15848 recenter:
15849
15850 #ifdef GLYPH_DEBUG
15851 debug_method_add (w, "recenter");
15852 #endif
15853
15854 /* Forget any previously recorded base line for line number display. */
15855 if (!buffer_unchanged_p)
15856 w->base_line_number = 0;
15857
15858 /* Determine the window start relative to point. */
15859 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15860 it.current_y = it.last_visible_y;
15861 if (centering_position < 0)
15862 {
15863 int window_total_lines
15864 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15865 int margin =
15866 scroll_margin > 0
15867 ? min (scroll_margin, window_total_lines / 4)
15868 : 0;
15869 ptrdiff_t margin_pos = CHARPOS (startp);
15870 Lisp_Object aggressive;
15871 int scrolling_up;
15872
15873 /* If there is a scroll margin at the top of the window, find
15874 its character position. */
15875 if (margin
15876 /* Cannot call start_display if startp is not in the
15877 accessible region of the buffer. This can happen when we
15878 have just switched to a different buffer and/or changed
15879 its restriction. In that case, startp is initialized to
15880 the character position 1 (BEGV) because we did not yet
15881 have chance to display the buffer even once. */
15882 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15883 {
15884 struct it it1;
15885 void *it1data = NULL;
15886
15887 SAVE_IT (it1, it, it1data);
15888 start_display (&it1, w, startp);
15889 move_it_vertically (&it1, margin * frame_line_height);
15890 margin_pos = IT_CHARPOS (it1);
15891 RESTORE_IT (&it, &it, it1data);
15892 }
15893 scrolling_up = PT > margin_pos;
15894 aggressive =
15895 scrolling_up
15896 ? BVAR (current_buffer, scroll_up_aggressively)
15897 : BVAR (current_buffer, scroll_down_aggressively);
15898
15899 if (!MINI_WINDOW_P (w)
15900 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15901 {
15902 int pt_offset = 0;
15903
15904 /* Setting scroll-conservatively overrides
15905 scroll-*-aggressively. */
15906 if (!scroll_conservatively && NUMBERP (aggressive))
15907 {
15908 double float_amount = XFLOATINT (aggressive);
15909
15910 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15911 if (pt_offset == 0 && float_amount > 0)
15912 pt_offset = 1;
15913 if (pt_offset && margin > 0)
15914 margin -= 1;
15915 }
15916 /* Compute how much to move the window start backward from
15917 point so that point will be displayed where the user
15918 wants it. */
15919 if (scrolling_up)
15920 {
15921 centering_position = it.last_visible_y;
15922 if (pt_offset)
15923 centering_position -= pt_offset;
15924 centering_position -=
15925 frame_line_height * (1 + margin + (last_line_misfit != 0))
15926 + WINDOW_HEADER_LINE_HEIGHT (w);
15927 /* Don't let point enter the scroll margin near top of
15928 the window. */
15929 if (centering_position < margin * frame_line_height)
15930 centering_position = margin * frame_line_height;
15931 }
15932 else
15933 centering_position = margin * frame_line_height + pt_offset;
15934 }
15935 else
15936 /* Set the window start half the height of the window backward
15937 from point. */
15938 centering_position = window_box_height (w) / 2;
15939 }
15940 move_it_vertically_backward (&it, centering_position);
15941
15942 eassert (IT_CHARPOS (it) >= BEGV);
15943
15944 /* The function move_it_vertically_backward may move over more
15945 than the specified y-distance. If it->w is small, e.g. a
15946 mini-buffer window, we may end up in front of the window's
15947 display area. Start displaying at the start of the line
15948 containing PT in this case. */
15949 if (it.current_y <= 0)
15950 {
15951 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15952 move_it_vertically_backward (&it, 0);
15953 it.current_y = 0;
15954 }
15955
15956 it.current_x = it.hpos = 0;
15957
15958 /* Set the window start position here explicitly, to avoid an
15959 infinite loop in case the functions in window-scroll-functions
15960 get errors. */
15961 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15962
15963 /* Run scroll hooks. */
15964 startp = run_window_scroll_functions (window, it.current.pos);
15965
15966 /* Redisplay the window. */
15967 if (!current_matrix_up_to_date_p
15968 || windows_or_buffers_changed
15969 || cursor_type_changed
15970 /* Don't use try_window_reusing_current_matrix in this case
15971 because it can have changed the buffer. */
15972 || !NILP (Vwindow_scroll_functions)
15973 || !just_this_one_p
15974 || MINI_WINDOW_P (w)
15975 || !(used_current_matrix_p
15976 = try_window_reusing_current_matrix (w)))
15977 try_window (window, startp, 0);
15978
15979 /* If new fonts have been loaded (due to fontsets), give up. We
15980 have to start a new redisplay since we need to re-adjust glyph
15981 matrices. */
15982 if (fonts_changed_p)
15983 goto need_larger_matrices;
15984
15985 /* If cursor did not appear assume that the middle of the window is
15986 in the first line of the window. Do it again with the next line.
15987 (Imagine a window of height 100, displaying two lines of height
15988 60. Moving back 50 from it->last_visible_y will end in the first
15989 line.) */
15990 if (w->cursor.vpos < 0)
15991 {
15992 if (w->window_end_valid && PT >= Z - w->window_end_pos)
15993 {
15994 clear_glyph_matrix (w->desired_matrix);
15995 move_it_by_lines (&it, 1);
15996 try_window (window, it.current.pos, 0);
15997 }
15998 else if (PT < IT_CHARPOS (it))
15999 {
16000 clear_glyph_matrix (w->desired_matrix);
16001 move_it_by_lines (&it, -1);
16002 try_window (window, it.current.pos, 0);
16003 }
16004 else
16005 {
16006 /* Not much we can do about it. */
16007 }
16008 }
16009
16010 /* Consider the following case: Window starts at BEGV, there is
16011 invisible, intangible text at BEGV, so that display starts at
16012 some point START > BEGV. It can happen that we are called with
16013 PT somewhere between BEGV and START. Try to handle that case. */
16014 if (w->cursor.vpos < 0)
16015 {
16016 struct glyph_row *row = w->current_matrix->rows;
16017 if (row->mode_line_p)
16018 ++row;
16019 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16020 }
16021
16022 if (!cursor_row_fully_visible_p (w, 0, 0))
16023 {
16024 /* If vscroll is enabled, disable it and try again. */
16025 if (w->vscroll)
16026 {
16027 w->vscroll = 0;
16028 clear_glyph_matrix (w->desired_matrix);
16029 goto recenter;
16030 }
16031
16032 /* Users who set scroll-conservatively to a large number want
16033 point just above/below the scroll margin. If we ended up
16034 with point's row partially visible, move the window start to
16035 make that row fully visible and out of the margin. */
16036 if (scroll_conservatively > SCROLL_LIMIT)
16037 {
16038 int window_total_lines
16039 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16040 int margin =
16041 scroll_margin > 0
16042 ? min (scroll_margin, window_total_lines / 4)
16043 : 0;
16044 int move_down = w->cursor.vpos >= window_total_lines / 2;
16045
16046 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16047 clear_glyph_matrix (w->desired_matrix);
16048 if (1 == try_window (window, it.current.pos,
16049 TRY_WINDOW_CHECK_MARGINS))
16050 goto done;
16051 }
16052
16053 /* If centering point failed to make the whole line visible,
16054 put point at the top instead. That has to make the whole line
16055 visible, if it can be done. */
16056 if (centering_position == 0)
16057 goto done;
16058
16059 clear_glyph_matrix (w->desired_matrix);
16060 centering_position = 0;
16061 goto recenter;
16062 }
16063
16064 done:
16065
16066 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16067 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16068 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16069
16070 /* Display the mode line, if we must. */
16071 if ((update_mode_line
16072 /* If window not full width, must redo its mode line
16073 if (a) the window to its side is being redone and
16074 (b) we do a frame-based redisplay. This is a consequence
16075 of how inverted lines are drawn in frame-based redisplay. */
16076 || (!just_this_one_p
16077 && !FRAME_WINDOW_P (f)
16078 && !WINDOW_FULL_WIDTH_P (w))
16079 /* Line number to display. */
16080 || w->base_line_pos > 0
16081 /* Column number is displayed and different from the one displayed. */
16082 || (w->column_number_displayed != -1
16083 && (w->column_number_displayed != current_column ())))
16084 /* This means that the window has a mode line. */
16085 && (WINDOW_WANTS_MODELINE_P (w)
16086 || WINDOW_WANTS_HEADER_LINE_P (w)))
16087 {
16088 display_mode_lines (w);
16089
16090 /* If mode line height has changed, arrange for a thorough
16091 immediate redisplay using the correct mode line height. */
16092 if (WINDOW_WANTS_MODELINE_P (w)
16093 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16094 {
16095 fonts_changed_p = 1;
16096 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16097 = DESIRED_MODE_LINE_HEIGHT (w);
16098 }
16099
16100 /* If header line height has changed, arrange for a thorough
16101 immediate redisplay using the correct header line height. */
16102 if (WINDOW_WANTS_HEADER_LINE_P (w)
16103 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16104 {
16105 fonts_changed_p = 1;
16106 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16107 = DESIRED_HEADER_LINE_HEIGHT (w);
16108 }
16109
16110 if (fonts_changed_p)
16111 goto need_larger_matrices;
16112 }
16113
16114 if (!line_number_displayed && w->base_line_pos != -1)
16115 {
16116 w->base_line_pos = 0;
16117 w->base_line_number = 0;
16118 }
16119
16120 finish_menu_bars:
16121
16122 /* When we reach a frame's selected window, redo the frame's menu bar. */
16123 if (update_mode_line
16124 && EQ (FRAME_SELECTED_WINDOW (f), window))
16125 {
16126 int redisplay_menu_p = 0;
16127
16128 if (FRAME_WINDOW_P (f))
16129 {
16130 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16131 || defined (HAVE_NS) || defined (USE_GTK)
16132 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16133 #else
16134 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16135 #endif
16136 }
16137 else
16138 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16139
16140 if (redisplay_menu_p)
16141 display_menu_bar (w);
16142
16143 #ifdef HAVE_WINDOW_SYSTEM
16144 if (FRAME_WINDOW_P (f))
16145 {
16146 #if defined (USE_GTK) || defined (HAVE_NS)
16147 if (FRAME_EXTERNAL_TOOL_BAR (f))
16148 redisplay_tool_bar (f);
16149 #else
16150 if (WINDOWP (f->tool_bar_window)
16151 && (FRAME_TOOL_BAR_LINES (f) > 0
16152 || !NILP (Vauto_resize_tool_bars))
16153 && redisplay_tool_bar (f))
16154 ignore_mouse_drag_p = 1;
16155 #endif
16156 }
16157 #endif
16158 }
16159
16160 #ifdef HAVE_WINDOW_SYSTEM
16161 if (FRAME_WINDOW_P (f)
16162 && update_window_fringes (w, (just_this_one_p
16163 || (!used_current_matrix_p && !overlay_arrow_seen)
16164 || w->pseudo_window_p)))
16165 {
16166 update_begin (f);
16167 block_input ();
16168 if (draw_window_fringes (w, 1))
16169 x_draw_vertical_border (w);
16170 unblock_input ();
16171 update_end (f);
16172 }
16173 #endif /* HAVE_WINDOW_SYSTEM */
16174
16175 /* We go to this label, with fonts_changed_p set,
16176 if it is necessary to try again using larger glyph matrices.
16177 We have to redeem the scroll bar even in this case,
16178 because the loop in redisplay_internal expects that. */
16179 need_larger_matrices:
16180 ;
16181 finish_scroll_bars:
16182
16183 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16184 {
16185 /* Set the thumb's position and size. */
16186 set_vertical_scroll_bar (w);
16187
16188 /* Note that we actually used the scroll bar attached to this
16189 window, so it shouldn't be deleted at the end of redisplay. */
16190 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16191 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16192 }
16193
16194 /* Restore current_buffer and value of point in it. The window
16195 update may have changed the buffer, so first make sure `opoint'
16196 is still valid (Bug#6177). */
16197 if (CHARPOS (opoint) < BEGV)
16198 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16199 else if (CHARPOS (opoint) > ZV)
16200 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16201 else
16202 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16203
16204 set_buffer_internal_1 (old);
16205 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16206 shorter. This can be caused by log truncation in *Messages*. */
16207 if (CHARPOS (lpoint) <= ZV)
16208 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16209
16210 unbind_to (count, Qnil);
16211 }
16212
16213
16214 /* Build the complete desired matrix of WINDOW with a window start
16215 buffer position POS.
16216
16217 Value is 1 if successful. It is zero if fonts were loaded during
16218 redisplay which makes re-adjusting glyph matrices necessary, and -1
16219 if point would appear in the scroll margins.
16220 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16221 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16222 set in FLAGS.) */
16223
16224 int
16225 try_window (Lisp_Object window, struct text_pos pos, int flags)
16226 {
16227 struct window *w = XWINDOW (window);
16228 struct it it;
16229 struct glyph_row *last_text_row = NULL;
16230 struct frame *f = XFRAME (w->frame);
16231 int frame_line_height = default_line_pixel_height (w);
16232
16233 /* Make POS the new window start. */
16234 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16235
16236 /* Mark cursor position as unknown. No overlay arrow seen. */
16237 w->cursor.vpos = -1;
16238 overlay_arrow_seen = 0;
16239
16240 /* Initialize iterator and info to start at POS. */
16241 start_display (&it, w, pos);
16242
16243 /* Display all lines of W. */
16244 while (it.current_y < it.last_visible_y)
16245 {
16246 if (display_line (&it))
16247 last_text_row = it.glyph_row - 1;
16248 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16249 return 0;
16250 }
16251
16252 /* Don't let the cursor end in the scroll margins. */
16253 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16254 && !MINI_WINDOW_P (w))
16255 {
16256 int this_scroll_margin;
16257 int window_total_lines
16258 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16259
16260 if (scroll_margin > 0)
16261 {
16262 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16263 this_scroll_margin *= frame_line_height;
16264 }
16265 else
16266 this_scroll_margin = 0;
16267
16268 if ((w->cursor.y >= 0 /* not vscrolled */
16269 && w->cursor.y < this_scroll_margin
16270 && CHARPOS (pos) > BEGV
16271 && IT_CHARPOS (it) < ZV)
16272 /* rms: considering make_cursor_line_fully_visible_p here
16273 seems to give wrong results. We don't want to recenter
16274 when the last line is partly visible, we want to allow
16275 that case to be handled in the usual way. */
16276 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16277 {
16278 w->cursor.vpos = -1;
16279 clear_glyph_matrix (w->desired_matrix);
16280 return -1;
16281 }
16282 }
16283
16284 /* If bottom moved off end of frame, change mode line percentage. */
16285 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
16286 w->update_mode_line = 1;
16287
16288 /* Set window_end_pos to the offset of the last character displayed
16289 on the window from the end of current_buffer. Set
16290 window_end_vpos to its row number. */
16291 if (last_text_row)
16292 {
16293 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16294 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16295 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (last_text_row);
16296 w->window_end_vpos = MATRIX_ROW_VPOS (last_text_row, w->desired_matrix);
16297 eassert
16298 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16299 w->window_end_vpos)));
16300 }
16301 else
16302 {
16303 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16304 w->window_end_pos = Z - ZV;
16305 w->window_end_vpos = 0;
16306 }
16307
16308 /* But that is not valid info until redisplay finishes. */
16309 w->window_end_valid = 0;
16310 return 1;
16311 }
16312
16313
16314 \f
16315 /************************************************************************
16316 Window redisplay reusing current matrix when buffer has not changed
16317 ************************************************************************/
16318
16319 /* Try redisplay of window W showing an unchanged buffer with a
16320 different window start than the last time it was displayed by
16321 reusing its current matrix. Value is non-zero if successful.
16322 W->start is the new window start. */
16323
16324 static int
16325 try_window_reusing_current_matrix (struct window *w)
16326 {
16327 struct frame *f = XFRAME (w->frame);
16328 struct glyph_row *bottom_row;
16329 struct it it;
16330 struct run run;
16331 struct text_pos start, new_start;
16332 int nrows_scrolled, i;
16333 struct glyph_row *last_text_row;
16334 struct glyph_row *last_reused_text_row;
16335 struct glyph_row *start_row;
16336 int start_vpos, min_y, max_y;
16337
16338 #ifdef GLYPH_DEBUG
16339 if (inhibit_try_window_reusing)
16340 return 0;
16341 #endif
16342
16343 if (/* This function doesn't handle terminal frames. */
16344 !FRAME_WINDOW_P (f)
16345 /* Don't try to reuse the display if windows have been split
16346 or such. */
16347 || windows_or_buffers_changed
16348 || cursor_type_changed)
16349 return 0;
16350
16351 /* Can't do this if region may have changed. */
16352 if (markpos_of_region () >= 0
16353 || w->region_showing
16354 || !NILP (Vshow_trailing_whitespace))
16355 return 0;
16356
16357 /* If top-line visibility has changed, give up. */
16358 if (WINDOW_WANTS_HEADER_LINE_P (w)
16359 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16360 return 0;
16361
16362 /* Give up if old or new display is scrolled vertically. We could
16363 make this function handle this, but right now it doesn't. */
16364 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16365 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16366 return 0;
16367
16368 /* The variable new_start now holds the new window start. The old
16369 start `start' can be determined from the current matrix. */
16370 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16371 start = start_row->minpos;
16372 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16373
16374 /* Clear the desired matrix for the display below. */
16375 clear_glyph_matrix (w->desired_matrix);
16376
16377 if (CHARPOS (new_start) <= CHARPOS (start))
16378 {
16379 /* Don't use this method if the display starts with an ellipsis
16380 displayed for invisible text. It's not easy to handle that case
16381 below, and it's certainly not worth the effort since this is
16382 not a frequent case. */
16383 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16384 return 0;
16385
16386 IF_DEBUG (debug_method_add (w, "twu1"));
16387
16388 /* Display up to a row that can be reused. The variable
16389 last_text_row is set to the last row displayed that displays
16390 text. Note that it.vpos == 0 if or if not there is a
16391 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16392 start_display (&it, w, new_start);
16393 w->cursor.vpos = -1;
16394 last_text_row = last_reused_text_row = NULL;
16395
16396 while (it.current_y < it.last_visible_y
16397 && !fonts_changed_p)
16398 {
16399 /* If we have reached into the characters in the START row,
16400 that means the line boundaries have changed. So we
16401 can't start copying with the row START. Maybe it will
16402 work to start copying with the following row. */
16403 while (IT_CHARPOS (it) > CHARPOS (start))
16404 {
16405 /* Advance to the next row as the "start". */
16406 start_row++;
16407 start = start_row->minpos;
16408 /* If there are no more rows to try, or just one, give up. */
16409 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16410 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16411 || CHARPOS (start) == ZV)
16412 {
16413 clear_glyph_matrix (w->desired_matrix);
16414 return 0;
16415 }
16416
16417 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16418 }
16419 /* If we have reached alignment, we can copy the rest of the
16420 rows. */
16421 if (IT_CHARPOS (it) == CHARPOS (start)
16422 /* Don't accept "alignment" inside a display vector,
16423 since start_row could have started in the middle of
16424 that same display vector (thus their character
16425 positions match), and we have no way of telling if
16426 that is the case. */
16427 && it.current.dpvec_index < 0)
16428 break;
16429
16430 if (display_line (&it))
16431 last_text_row = it.glyph_row - 1;
16432
16433 }
16434
16435 /* A value of current_y < last_visible_y means that we stopped
16436 at the previous window start, which in turn means that we
16437 have at least one reusable row. */
16438 if (it.current_y < it.last_visible_y)
16439 {
16440 struct glyph_row *row;
16441
16442 /* IT.vpos always starts from 0; it counts text lines. */
16443 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16444
16445 /* Find PT if not already found in the lines displayed. */
16446 if (w->cursor.vpos < 0)
16447 {
16448 int dy = it.current_y - start_row->y;
16449
16450 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16451 row = row_containing_pos (w, PT, row, NULL, dy);
16452 if (row)
16453 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16454 dy, nrows_scrolled);
16455 else
16456 {
16457 clear_glyph_matrix (w->desired_matrix);
16458 return 0;
16459 }
16460 }
16461
16462 /* Scroll the display. Do it before the current matrix is
16463 changed. The problem here is that update has not yet
16464 run, i.e. part of the current matrix is not up to date.
16465 scroll_run_hook will clear the cursor, and use the
16466 current matrix to get the height of the row the cursor is
16467 in. */
16468 run.current_y = start_row->y;
16469 run.desired_y = it.current_y;
16470 run.height = it.last_visible_y - it.current_y;
16471
16472 if (run.height > 0 && run.current_y != run.desired_y)
16473 {
16474 update_begin (f);
16475 FRAME_RIF (f)->update_window_begin_hook (w);
16476 FRAME_RIF (f)->clear_window_mouse_face (w);
16477 FRAME_RIF (f)->scroll_run_hook (w, &run);
16478 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16479 update_end (f);
16480 }
16481
16482 /* Shift current matrix down by nrows_scrolled lines. */
16483 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16484 rotate_matrix (w->current_matrix,
16485 start_vpos,
16486 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16487 nrows_scrolled);
16488
16489 /* Disable lines that must be updated. */
16490 for (i = 0; i < nrows_scrolled; ++i)
16491 (start_row + i)->enabled_p = 0;
16492
16493 /* Re-compute Y positions. */
16494 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16495 max_y = it.last_visible_y;
16496 for (row = start_row + nrows_scrolled;
16497 row < bottom_row;
16498 ++row)
16499 {
16500 row->y = it.current_y;
16501 row->visible_height = row->height;
16502
16503 if (row->y < min_y)
16504 row->visible_height -= min_y - row->y;
16505 if (row->y + row->height > max_y)
16506 row->visible_height -= row->y + row->height - max_y;
16507 if (row->fringe_bitmap_periodic_p)
16508 row->redraw_fringe_bitmaps_p = 1;
16509
16510 it.current_y += row->height;
16511
16512 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16513 last_reused_text_row = row;
16514 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16515 break;
16516 }
16517
16518 /* Disable lines in the current matrix which are now
16519 below the window. */
16520 for (++row; row < bottom_row; ++row)
16521 row->enabled_p = row->mode_line_p = 0;
16522 }
16523
16524 /* Update window_end_pos etc.; last_reused_text_row is the last
16525 reused row from the current matrix containing text, if any.
16526 The value of last_text_row is the last displayed line
16527 containing text. */
16528 if (last_reused_text_row)
16529 {
16530 w->window_end_bytepos
16531 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
16532 w->window_end_pos
16533 = Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row);
16534 w->window_end_vpos
16535 = MATRIX_ROW_VPOS (last_reused_text_row, w->current_matrix);
16536 }
16537 else if (last_text_row)
16538 {
16539 w->window_end_bytepos
16540 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16541 w->window_end_pos
16542 = Z - MATRIX_ROW_END_CHARPOS (last_text_row);
16543 w->window_end_vpos
16544 = MATRIX_ROW_VPOS (last_text_row, w->desired_matrix);
16545 }
16546 else
16547 {
16548 /* This window must be completely empty. */
16549 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16550 w->window_end_pos = Z - ZV;
16551 w->window_end_vpos = 0;
16552 }
16553 w->window_end_valid = 0;
16554
16555 /* Update hint: don't try scrolling again in update_window. */
16556 w->desired_matrix->no_scrolling_p = 1;
16557
16558 #ifdef GLYPH_DEBUG
16559 debug_method_add (w, "try_window_reusing_current_matrix 1");
16560 #endif
16561 return 1;
16562 }
16563 else if (CHARPOS (new_start) > CHARPOS (start))
16564 {
16565 struct glyph_row *pt_row, *row;
16566 struct glyph_row *first_reusable_row;
16567 struct glyph_row *first_row_to_display;
16568 int dy;
16569 int yb = window_text_bottom_y (w);
16570
16571 /* Find the row starting at new_start, if there is one. Don't
16572 reuse a partially visible line at the end. */
16573 first_reusable_row = start_row;
16574 while (first_reusable_row->enabled_p
16575 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16576 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16577 < CHARPOS (new_start)))
16578 ++first_reusable_row;
16579
16580 /* Give up if there is no row to reuse. */
16581 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16582 || !first_reusable_row->enabled_p
16583 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16584 != CHARPOS (new_start)))
16585 return 0;
16586
16587 /* We can reuse fully visible rows beginning with
16588 first_reusable_row to the end of the window. Set
16589 first_row_to_display to the first row that cannot be reused.
16590 Set pt_row to the row containing point, if there is any. */
16591 pt_row = NULL;
16592 for (first_row_to_display = first_reusable_row;
16593 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16594 ++first_row_to_display)
16595 {
16596 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16597 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16598 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16599 && first_row_to_display->ends_at_zv_p
16600 && pt_row == NULL)))
16601 pt_row = first_row_to_display;
16602 }
16603
16604 /* Start displaying at the start of first_row_to_display. */
16605 eassert (first_row_to_display->y < yb);
16606 init_to_row_start (&it, w, first_row_to_display);
16607
16608 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16609 - start_vpos);
16610 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16611 - nrows_scrolled);
16612 it.current_y = (first_row_to_display->y - first_reusable_row->y
16613 + WINDOW_HEADER_LINE_HEIGHT (w));
16614
16615 /* Display lines beginning with first_row_to_display in the
16616 desired matrix. Set last_text_row to the last row displayed
16617 that displays text. */
16618 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16619 if (pt_row == NULL)
16620 w->cursor.vpos = -1;
16621 last_text_row = NULL;
16622 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16623 if (display_line (&it))
16624 last_text_row = it.glyph_row - 1;
16625
16626 /* If point is in a reused row, adjust y and vpos of the cursor
16627 position. */
16628 if (pt_row)
16629 {
16630 w->cursor.vpos -= nrows_scrolled;
16631 w->cursor.y -= first_reusable_row->y - start_row->y;
16632 }
16633
16634 /* Give up if point isn't in a row displayed or reused. (This
16635 also handles the case where w->cursor.vpos < nrows_scrolled
16636 after the calls to display_line, which can happen with scroll
16637 margins. See bug#1295.) */
16638 if (w->cursor.vpos < 0)
16639 {
16640 clear_glyph_matrix (w->desired_matrix);
16641 return 0;
16642 }
16643
16644 /* Scroll the display. */
16645 run.current_y = first_reusable_row->y;
16646 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16647 run.height = it.last_visible_y - run.current_y;
16648 dy = run.current_y - run.desired_y;
16649
16650 if (run.height)
16651 {
16652 update_begin (f);
16653 FRAME_RIF (f)->update_window_begin_hook (w);
16654 FRAME_RIF (f)->clear_window_mouse_face (w);
16655 FRAME_RIF (f)->scroll_run_hook (w, &run);
16656 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16657 update_end (f);
16658 }
16659
16660 /* Adjust Y positions of reused rows. */
16661 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16662 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16663 max_y = it.last_visible_y;
16664 for (row = first_reusable_row; row < first_row_to_display; ++row)
16665 {
16666 row->y -= dy;
16667 row->visible_height = row->height;
16668 if (row->y < min_y)
16669 row->visible_height -= min_y - row->y;
16670 if (row->y + row->height > max_y)
16671 row->visible_height -= row->y + row->height - max_y;
16672 if (row->fringe_bitmap_periodic_p)
16673 row->redraw_fringe_bitmaps_p = 1;
16674 }
16675
16676 /* Scroll the current matrix. */
16677 eassert (nrows_scrolled > 0);
16678 rotate_matrix (w->current_matrix,
16679 start_vpos,
16680 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16681 -nrows_scrolled);
16682
16683 /* Disable rows not reused. */
16684 for (row -= nrows_scrolled; row < bottom_row; ++row)
16685 row->enabled_p = 0;
16686
16687 /* Point may have moved to a different line, so we cannot assume that
16688 the previous cursor position is valid; locate the correct row. */
16689 if (pt_row)
16690 {
16691 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16692 row < bottom_row
16693 && PT >= MATRIX_ROW_END_CHARPOS (row)
16694 && !row->ends_at_zv_p;
16695 row++)
16696 {
16697 w->cursor.vpos++;
16698 w->cursor.y = row->y;
16699 }
16700 if (row < bottom_row)
16701 {
16702 /* Can't simply scan the row for point with
16703 bidi-reordered glyph rows. Let set_cursor_from_row
16704 figure out where to put the cursor, and if it fails,
16705 give up. */
16706 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16707 {
16708 if (!set_cursor_from_row (w, row, w->current_matrix,
16709 0, 0, 0, 0))
16710 {
16711 clear_glyph_matrix (w->desired_matrix);
16712 return 0;
16713 }
16714 }
16715 else
16716 {
16717 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16718 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16719
16720 for (; glyph < end
16721 && (!BUFFERP (glyph->object)
16722 || glyph->charpos < PT);
16723 glyph++)
16724 {
16725 w->cursor.hpos++;
16726 w->cursor.x += glyph->pixel_width;
16727 }
16728 }
16729 }
16730 }
16731
16732 /* Adjust window end. A null value of last_text_row means that
16733 the window end is in reused rows which in turn means that
16734 only its vpos can have changed. */
16735 if (last_text_row)
16736 {
16737 w->window_end_bytepos
16738 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16739 w->window_end_pos
16740 = Z - MATRIX_ROW_END_CHARPOS (last_text_row);
16741 w->window_end_vpos
16742 = MATRIX_ROW_VPOS (last_text_row, w->desired_matrix);
16743 }
16744 else
16745 w->window_end_vpos -= nrows_scrolled;
16746
16747 w->window_end_valid = 0;
16748 w->desired_matrix->no_scrolling_p = 1;
16749
16750 #ifdef GLYPH_DEBUG
16751 debug_method_add (w, "try_window_reusing_current_matrix 2");
16752 #endif
16753 return 1;
16754 }
16755
16756 return 0;
16757 }
16758
16759
16760 \f
16761 /************************************************************************
16762 Window redisplay reusing current matrix when buffer has changed
16763 ************************************************************************/
16764
16765 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16766 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16767 ptrdiff_t *, ptrdiff_t *);
16768 static struct glyph_row *
16769 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16770 struct glyph_row *);
16771
16772
16773 /* Return the last row in MATRIX displaying text. If row START is
16774 non-null, start searching with that row. IT gives the dimensions
16775 of the display. Value is null if matrix is empty; otherwise it is
16776 a pointer to the row found. */
16777
16778 static struct glyph_row *
16779 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16780 struct glyph_row *start)
16781 {
16782 struct glyph_row *row, *row_found;
16783
16784 /* Set row_found to the last row in IT->w's current matrix
16785 displaying text. The loop looks funny but think of partially
16786 visible lines. */
16787 row_found = NULL;
16788 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16789 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16790 {
16791 eassert (row->enabled_p);
16792 row_found = row;
16793 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16794 break;
16795 ++row;
16796 }
16797
16798 return row_found;
16799 }
16800
16801
16802 /* Return the last row in the current matrix of W that is not affected
16803 by changes at the start of current_buffer that occurred since W's
16804 current matrix was built. Value is null if no such row exists.
16805
16806 BEG_UNCHANGED us the number of characters unchanged at the start of
16807 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16808 first changed character in current_buffer. Characters at positions <
16809 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16810 when the current matrix was built. */
16811
16812 static struct glyph_row *
16813 find_last_unchanged_at_beg_row (struct window *w)
16814 {
16815 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16816 struct glyph_row *row;
16817 struct glyph_row *row_found = NULL;
16818 int yb = window_text_bottom_y (w);
16819
16820 /* Find the last row displaying unchanged text. */
16821 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16822 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16823 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16824 ++row)
16825 {
16826 if (/* If row ends before first_changed_pos, it is unchanged,
16827 except in some case. */
16828 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16829 /* When row ends in ZV and we write at ZV it is not
16830 unchanged. */
16831 && !row->ends_at_zv_p
16832 /* When first_changed_pos is the end of a continued line,
16833 row is not unchanged because it may be no longer
16834 continued. */
16835 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16836 && (row->continued_p
16837 || row->exact_window_width_line_p))
16838 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16839 needs to be recomputed, so don't consider this row as
16840 unchanged. This happens when the last line was
16841 bidi-reordered and was killed immediately before this
16842 redisplay cycle. In that case, ROW->end stores the
16843 buffer position of the first visual-order character of
16844 the killed text, which is now beyond ZV. */
16845 && CHARPOS (row->end.pos) <= ZV)
16846 row_found = row;
16847
16848 /* Stop if last visible row. */
16849 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16850 break;
16851 }
16852
16853 return row_found;
16854 }
16855
16856
16857 /* Find the first glyph row in the current matrix of W that is not
16858 affected by changes at the end of current_buffer since the
16859 time W's current matrix was built.
16860
16861 Return in *DELTA the number of chars by which buffer positions in
16862 unchanged text at the end of current_buffer must be adjusted.
16863
16864 Return in *DELTA_BYTES the corresponding number of bytes.
16865
16866 Value is null if no such row exists, i.e. all rows are affected by
16867 changes. */
16868
16869 static struct glyph_row *
16870 find_first_unchanged_at_end_row (struct window *w,
16871 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16872 {
16873 struct glyph_row *row;
16874 struct glyph_row *row_found = NULL;
16875
16876 *delta = *delta_bytes = 0;
16877
16878 /* Display must not have been paused, otherwise the current matrix
16879 is not up to date. */
16880 eassert (w->window_end_valid);
16881
16882 /* A value of window_end_pos >= END_UNCHANGED means that the window
16883 end is in the range of changed text. If so, there is no
16884 unchanged row at the end of W's current matrix. */
16885 if (w->window_end_pos >= END_UNCHANGED)
16886 return NULL;
16887
16888 /* Set row to the last row in W's current matrix displaying text. */
16889 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
16890
16891 /* If matrix is entirely empty, no unchanged row exists. */
16892 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16893 {
16894 /* The value of row is the last glyph row in the matrix having a
16895 meaningful buffer position in it. The end position of row
16896 corresponds to window_end_pos. This allows us to translate
16897 buffer positions in the current matrix to current buffer
16898 positions for characters not in changed text. */
16899 ptrdiff_t Z_old =
16900 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
16901 ptrdiff_t Z_BYTE_old =
16902 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16903 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16904 struct glyph_row *first_text_row
16905 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16906
16907 *delta = Z - Z_old;
16908 *delta_bytes = Z_BYTE - Z_BYTE_old;
16909
16910 /* Set last_unchanged_pos to the buffer position of the last
16911 character in the buffer that has not been changed. Z is the
16912 index + 1 of the last character in current_buffer, i.e. by
16913 subtracting END_UNCHANGED we get the index of the last
16914 unchanged character, and we have to add BEG to get its buffer
16915 position. */
16916 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16917 last_unchanged_pos_old = last_unchanged_pos - *delta;
16918
16919 /* Search backward from ROW for a row displaying a line that
16920 starts at a minimum position >= last_unchanged_pos_old. */
16921 for (; row > first_text_row; --row)
16922 {
16923 /* This used to abort, but it can happen.
16924 It is ok to just stop the search instead here. KFS. */
16925 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16926 break;
16927
16928 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16929 row_found = row;
16930 }
16931 }
16932
16933 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16934
16935 return row_found;
16936 }
16937
16938
16939 /* Make sure that glyph rows in the current matrix of window W
16940 reference the same glyph memory as corresponding rows in the
16941 frame's frame matrix. This function is called after scrolling W's
16942 current matrix on a terminal frame in try_window_id and
16943 try_window_reusing_current_matrix. */
16944
16945 static void
16946 sync_frame_with_window_matrix_rows (struct window *w)
16947 {
16948 struct frame *f = XFRAME (w->frame);
16949 struct glyph_row *window_row, *window_row_end, *frame_row;
16950
16951 /* Preconditions: W must be a leaf window and full-width. Its frame
16952 must have a frame matrix. */
16953 eassert (BUFFERP (w->contents));
16954 eassert (WINDOW_FULL_WIDTH_P (w));
16955 eassert (!FRAME_WINDOW_P (f));
16956
16957 /* If W is a full-width window, glyph pointers in W's current matrix
16958 have, by definition, to be the same as glyph pointers in the
16959 corresponding frame matrix. Note that frame matrices have no
16960 marginal areas (see build_frame_matrix). */
16961 window_row = w->current_matrix->rows;
16962 window_row_end = window_row + w->current_matrix->nrows;
16963 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16964 while (window_row < window_row_end)
16965 {
16966 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16967 struct glyph *end = window_row->glyphs[LAST_AREA];
16968
16969 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16970 frame_row->glyphs[TEXT_AREA] = start;
16971 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16972 frame_row->glyphs[LAST_AREA] = end;
16973
16974 /* Disable frame rows whose corresponding window rows have
16975 been disabled in try_window_id. */
16976 if (!window_row->enabled_p)
16977 frame_row->enabled_p = 0;
16978
16979 ++window_row, ++frame_row;
16980 }
16981 }
16982
16983
16984 /* Find the glyph row in window W containing CHARPOS. Consider all
16985 rows between START and END (not inclusive). END null means search
16986 all rows to the end of the display area of W. Value is the row
16987 containing CHARPOS or null. */
16988
16989 struct glyph_row *
16990 row_containing_pos (struct window *w, ptrdiff_t charpos,
16991 struct glyph_row *start, struct glyph_row *end, int dy)
16992 {
16993 struct glyph_row *row = start;
16994 struct glyph_row *best_row = NULL;
16995 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
16996 int last_y;
16997
16998 /* If we happen to start on a header-line, skip that. */
16999 if (row->mode_line_p)
17000 ++row;
17001
17002 if ((end && row >= end) || !row->enabled_p)
17003 return NULL;
17004
17005 last_y = window_text_bottom_y (w) - dy;
17006
17007 while (1)
17008 {
17009 /* Give up if we have gone too far. */
17010 if (end && row >= end)
17011 return NULL;
17012 /* This formerly returned if they were equal.
17013 I think that both quantities are of a "last plus one" type;
17014 if so, when they are equal, the row is within the screen. -- rms. */
17015 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17016 return NULL;
17017
17018 /* If it is in this row, return this row. */
17019 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17020 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17021 /* The end position of a row equals the start
17022 position of the next row. If CHARPOS is there, we
17023 would rather consider it displayed in the next
17024 line, except when this line ends in ZV. */
17025 && !row_for_charpos_p (row, charpos)))
17026 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17027 {
17028 struct glyph *g;
17029
17030 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17031 || (!best_row && !row->continued_p))
17032 return row;
17033 /* In bidi-reordered rows, there could be several rows whose
17034 edges surround CHARPOS, all of these rows belonging to
17035 the same continued line. We need to find the row which
17036 fits CHARPOS the best. */
17037 for (g = row->glyphs[TEXT_AREA];
17038 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17039 g++)
17040 {
17041 if (!STRINGP (g->object))
17042 {
17043 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17044 {
17045 mindif = eabs (g->charpos - charpos);
17046 best_row = row;
17047 /* Exact match always wins. */
17048 if (mindif == 0)
17049 return best_row;
17050 }
17051 }
17052 }
17053 }
17054 else if (best_row && !row->continued_p)
17055 return best_row;
17056 ++row;
17057 }
17058 }
17059
17060
17061 /* Try to redisplay window W by reusing its existing display. W's
17062 current matrix must be up to date when this function is called,
17063 i.e. window_end_valid must be nonzero.
17064
17065 Value is
17066
17067 1 if display has been updated
17068 0 if otherwise unsuccessful
17069 -1 if redisplay with same window start is known not to succeed
17070
17071 The following steps are performed:
17072
17073 1. Find the last row in the current matrix of W that is not
17074 affected by changes at the start of current_buffer. If no such row
17075 is found, give up.
17076
17077 2. Find the first row in W's current matrix that is not affected by
17078 changes at the end of current_buffer. Maybe there is no such row.
17079
17080 3. Display lines beginning with the row + 1 found in step 1 to the
17081 row found in step 2 or, if step 2 didn't find a row, to the end of
17082 the window.
17083
17084 4. If cursor is not known to appear on the window, give up.
17085
17086 5. If display stopped at the row found in step 2, scroll the
17087 display and current matrix as needed.
17088
17089 6. Maybe display some lines at the end of W, if we must. This can
17090 happen under various circumstances, like a partially visible line
17091 becoming fully visible, or because newly displayed lines are displayed
17092 in smaller font sizes.
17093
17094 7. Update W's window end information. */
17095
17096 static int
17097 try_window_id (struct window *w)
17098 {
17099 struct frame *f = XFRAME (w->frame);
17100 struct glyph_matrix *current_matrix = w->current_matrix;
17101 struct glyph_matrix *desired_matrix = w->desired_matrix;
17102 struct glyph_row *last_unchanged_at_beg_row;
17103 struct glyph_row *first_unchanged_at_end_row;
17104 struct glyph_row *row;
17105 struct glyph_row *bottom_row;
17106 int bottom_vpos;
17107 struct it it;
17108 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17109 int dvpos, dy;
17110 struct text_pos start_pos;
17111 struct run run;
17112 int first_unchanged_at_end_vpos = 0;
17113 struct glyph_row *last_text_row, *last_text_row_at_end;
17114 struct text_pos start;
17115 ptrdiff_t first_changed_charpos, last_changed_charpos;
17116
17117 #ifdef GLYPH_DEBUG
17118 if (inhibit_try_window_id)
17119 return 0;
17120 #endif
17121
17122 /* This is handy for debugging. */
17123 #if 0
17124 #define GIVE_UP(X) \
17125 do { \
17126 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17127 return 0; \
17128 } while (0)
17129 #else
17130 #define GIVE_UP(X) return 0
17131 #endif
17132
17133 SET_TEXT_POS_FROM_MARKER (start, w->start);
17134
17135 /* Don't use this for mini-windows because these can show
17136 messages and mini-buffers, and we don't handle that here. */
17137 if (MINI_WINDOW_P (w))
17138 GIVE_UP (1);
17139
17140 /* This flag is used to prevent redisplay optimizations. */
17141 if (windows_or_buffers_changed || cursor_type_changed)
17142 GIVE_UP (2);
17143
17144 /* Verify that narrowing has not changed.
17145 Also verify that we were not told to prevent redisplay optimizations.
17146 It would be nice to further
17147 reduce the number of cases where this prevents try_window_id. */
17148 if (current_buffer->clip_changed
17149 || current_buffer->prevent_redisplay_optimizations_p)
17150 GIVE_UP (3);
17151
17152 /* Window must either use window-based redisplay or be full width. */
17153 if (!FRAME_WINDOW_P (f)
17154 && (!FRAME_LINE_INS_DEL_OK (f)
17155 || !WINDOW_FULL_WIDTH_P (w)))
17156 GIVE_UP (4);
17157
17158 /* Give up if point is known NOT to appear in W. */
17159 if (PT < CHARPOS (start))
17160 GIVE_UP (5);
17161
17162 /* Another way to prevent redisplay optimizations. */
17163 if (w->last_modified == 0)
17164 GIVE_UP (6);
17165
17166 /* Verify that window is not hscrolled. */
17167 if (w->hscroll != 0)
17168 GIVE_UP (7);
17169
17170 /* Verify that display wasn't paused. */
17171 if (!w->window_end_valid)
17172 GIVE_UP (8);
17173
17174 /* Can't use this if highlighting a region because a cursor movement
17175 will do more than just set the cursor. */
17176 if (markpos_of_region () >= 0)
17177 GIVE_UP (9);
17178
17179 /* Likewise if highlighting trailing whitespace. */
17180 if (!NILP (Vshow_trailing_whitespace))
17181 GIVE_UP (11);
17182
17183 /* Likewise if showing a region. */
17184 if (w->region_showing)
17185 GIVE_UP (10);
17186
17187 /* Can't use this if overlay arrow position and/or string have
17188 changed. */
17189 if (overlay_arrows_changed_p ())
17190 GIVE_UP (12);
17191
17192 /* When word-wrap is on, adding a space to the first word of a
17193 wrapped line can change the wrap position, altering the line
17194 above it. It might be worthwhile to handle this more
17195 intelligently, but for now just redisplay from scratch. */
17196 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17197 GIVE_UP (21);
17198
17199 /* Under bidi reordering, adding or deleting a character in the
17200 beginning of a paragraph, before the first strong directional
17201 character, can change the base direction of the paragraph (unless
17202 the buffer specifies a fixed paragraph direction), which will
17203 require to redisplay the whole paragraph. It might be worthwhile
17204 to find the paragraph limits and widen the range of redisplayed
17205 lines to that, but for now just give up this optimization and
17206 redisplay from scratch. */
17207 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17208 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17209 GIVE_UP (22);
17210
17211 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17212 only if buffer has really changed. The reason is that the gap is
17213 initially at Z for freshly visited files. The code below would
17214 set end_unchanged to 0 in that case. */
17215 if (MODIFF > SAVE_MODIFF
17216 /* This seems to happen sometimes after saving a buffer. */
17217 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17218 {
17219 if (GPT - BEG < BEG_UNCHANGED)
17220 BEG_UNCHANGED = GPT - BEG;
17221 if (Z - GPT < END_UNCHANGED)
17222 END_UNCHANGED = Z - GPT;
17223 }
17224
17225 /* The position of the first and last character that has been changed. */
17226 first_changed_charpos = BEG + BEG_UNCHANGED;
17227 last_changed_charpos = Z - END_UNCHANGED;
17228
17229 /* If window starts after a line end, and the last change is in
17230 front of that newline, then changes don't affect the display.
17231 This case happens with stealth-fontification. Note that although
17232 the display is unchanged, glyph positions in the matrix have to
17233 be adjusted, of course. */
17234 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17235 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17236 && ((last_changed_charpos < CHARPOS (start)
17237 && CHARPOS (start) == BEGV)
17238 || (last_changed_charpos < CHARPOS (start) - 1
17239 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17240 {
17241 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17242 struct glyph_row *r0;
17243
17244 /* Compute how many chars/bytes have been added to or removed
17245 from the buffer. */
17246 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17247 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17248 Z_delta = Z - Z_old;
17249 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17250
17251 /* Give up if PT is not in the window. Note that it already has
17252 been checked at the start of try_window_id that PT is not in
17253 front of the window start. */
17254 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17255 GIVE_UP (13);
17256
17257 /* If window start is unchanged, we can reuse the whole matrix
17258 as is, after adjusting glyph positions. No need to compute
17259 the window end again, since its offset from Z hasn't changed. */
17260 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17261 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17262 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17263 /* PT must not be in a partially visible line. */
17264 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17265 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17266 {
17267 /* Adjust positions in the glyph matrix. */
17268 if (Z_delta || Z_delta_bytes)
17269 {
17270 struct glyph_row *r1
17271 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17272 increment_matrix_positions (w->current_matrix,
17273 MATRIX_ROW_VPOS (r0, current_matrix),
17274 MATRIX_ROW_VPOS (r1, current_matrix),
17275 Z_delta, Z_delta_bytes);
17276 }
17277
17278 /* Set the cursor. */
17279 row = row_containing_pos (w, PT, r0, NULL, 0);
17280 if (row)
17281 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17282 else
17283 emacs_abort ();
17284 return 1;
17285 }
17286 }
17287
17288 /* Handle the case that changes are all below what is displayed in
17289 the window, and that PT is in the window. This shortcut cannot
17290 be taken if ZV is visible in the window, and text has been added
17291 there that is visible in the window. */
17292 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17293 /* ZV is not visible in the window, or there are no
17294 changes at ZV, actually. */
17295 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17296 || first_changed_charpos == last_changed_charpos))
17297 {
17298 struct glyph_row *r0;
17299
17300 /* Give up if PT is not in the window. Note that it already has
17301 been checked at the start of try_window_id that PT is not in
17302 front of the window start. */
17303 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17304 GIVE_UP (14);
17305
17306 /* If window start is unchanged, we can reuse the whole matrix
17307 as is, without changing glyph positions since no text has
17308 been added/removed in front of the window end. */
17309 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17310 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17311 /* PT must not be in a partially visible line. */
17312 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17313 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17314 {
17315 /* We have to compute the window end anew since text
17316 could have been added/removed after it. */
17317 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17318 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17319
17320 /* Set the cursor. */
17321 row = row_containing_pos (w, PT, r0, NULL, 0);
17322 if (row)
17323 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17324 else
17325 emacs_abort ();
17326 return 2;
17327 }
17328 }
17329
17330 /* Give up if window start is in the changed area.
17331
17332 The condition used to read
17333
17334 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17335
17336 but why that was tested escapes me at the moment. */
17337 if (CHARPOS (start) >= first_changed_charpos
17338 && CHARPOS (start) <= last_changed_charpos)
17339 GIVE_UP (15);
17340
17341 /* Check that window start agrees with the start of the first glyph
17342 row in its current matrix. Check this after we know the window
17343 start is not in changed text, otherwise positions would not be
17344 comparable. */
17345 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17346 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17347 GIVE_UP (16);
17348
17349 /* Give up if the window ends in strings. Overlay strings
17350 at the end are difficult to handle, so don't try. */
17351 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
17352 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17353 GIVE_UP (20);
17354
17355 /* Compute the position at which we have to start displaying new
17356 lines. Some of the lines at the top of the window might be
17357 reusable because they are not displaying changed text. Find the
17358 last row in W's current matrix not affected by changes at the
17359 start of current_buffer. Value is null if changes start in the
17360 first line of window. */
17361 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17362 if (last_unchanged_at_beg_row)
17363 {
17364 /* Avoid starting to display in the middle of a character, a TAB
17365 for instance. This is easier than to set up the iterator
17366 exactly, and it's not a frequent case, so the additional
17367 effort wouldn't really pay off. */
17368 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17369 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17370 && last_unchanged_at_beg_row > w->current_matrix->rows)
17371 --last_unchanged_at_beg_row;
17372
17373 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17374 GIVE_UP (17);
17375
17376 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17377 GIVE_UP (18);
17378 start_pos = it.current.pos;
17379
17380 /* Start displaying new lines in the desired matrix at the same
17381 vpos we would use in the current matrix, i.e. below
17382 last_unchanged_at_beg_row. */
17383 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17384 current_matrix);
17385 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17386 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17387
17388 eassert (it.hpos == 0 && it.current_x == 0);
17389 }
17390 else
17391 {
17392 /* There are no reusable lines at the start of the window.
17393 Start displaying in the first text line. */
17394 start_display (&it, w, start);
17395 it.vpos = it.first_vpos;
17396 start_pos = it.current.pos;
17397 }
17398
17399 /* Find the first row that is not affected by changes at the end of
17400 the buffer. Value will be null if there is no unchanged row, in
17401 which case we must redisplay to the end of the window. delta
17402 will be set to the value by which buffer positions beginning with
17403 first_unchanged_at_end_row have to be adjusted due to text
17404 changes. */
17405 first_unchanged_at_end_row
17406 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17407 IF_DEBUG (debug_delta = delta);
17408 IF_DEBUG (debug_delta_bytes = delta_bytes);
17409
17410 /* Set stop_pos to the buffer position up to which we will have to
17411 display new lines. If first_unchanged_at_end_row != NULL, this
17412 is the buffer position of the start of the line displayed in that
17413 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17414 that we don't stop at a buffer position. */
17415 stop_pos = 0;
17416 if (first_unchanged_at_end_row)
17417 {
17418 eassert (last_unchanged_at_beg_row == NULL
17419 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17420
17421 /* If this is a continuation line, move forward to the next one
17422 that isn't. Changes in lines above affect this line.
17423 Caution: this may move first_unchanged_at_end_row to a row
17424 not displaying text. */
17425 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17426 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17427 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17428 < it.last_visible_y))
17429 ++first_unchanged_at_end_row;
17430
17431 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17432 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17433 >= it.last_visible_y))
17434 first_unchanged_at_end_row = NULL;
17435 else
17436 {
17437 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17438 + delta);
17439 first_unchanged_at_end_vpos
17440 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17441 eassert (stop_pos >= Z - END_UNCHANGED);
17442 }
17443 }
17444 else if (last_unchanged_at_beg_row == NULL)
17445 GIVE_UP (19);
17446
17447
17448 #ifdef GLYPH_DEBUG
17449
17450 /* Either there is no unchanged row at the end, or the one we have
17451 now displays text. This is a necessary condition for the window
17452 end pos calculation at the end of this function. */
17453 eassert (first_unchanged_at_end_row == NULL
17454 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17455
17456 debug_last_unchanged_at_beg_vpos
17457 = (last_unchanged_at_beg_row
17458 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17459 : -1);
17460 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17461
17462 #endif /* GLYPH_DEBUG */
17463
17464
17465 /* Display new lines. Set last_text_row to the last new line
17466 displayed which has text on it, i.e. might end up as being the
17467 line where the window_end_vpos is. */
17468 w->cursor.vpos = -1;
17469 last_text_row = NULL;
17470 overlay_arrow_seen = 0;
17471 while (it.current_y < it.last_visible_y
17472 && !fonts_changed_p
17473 && (first_unchanged_at_end_row == NULL
17474 || IT_CHARPOS (it) < stop_pos))
17475 {
17476 if (display_line (&it))
17477 last_text_row = it.glyph_row - 1;
17478 }
17479
17480 if (fonts_changed_p)
17481 return -1;
17482
17483
17484 /* Compute differences in buffer positions, y-positions etc. for
17485 lines reused at the bottom of the window. Compute what we can
17486 scroll. */
17487 if (first_unchanged_at_end_row
17488 /* No lines reused because we displayed everything up to the
17489 bottom of the window. */
17490 && it.current_y < it.last_visible_y)
17491 {
17492 dvpos = (it.vpos
17493 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17494 current_matrix));
17495 dy = it.current_y - first_unchanged_at_end_row->y;
17496 run.current_y = first_unchanged_at_end_row->y;
17497 run.desired_y = run.current_y + dy;
17498 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17499 }
17500 else
17501 {
17502 delta = delta_bytes = dvpos = dy
17503 = run.current_y = run.desired_y = run.height = 0;
17504 first_unchanged_at_end_row = NULL;
17505 }
17506 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17507
17508
17509 /* Find the cursor if not already found. We have to decide whether
17510 PT will appear on this window (it sometimes doesn't, but this is
17511 not a very frequent case.) This decision has to be made before
17512 the current matrix is altered. A value of cursor.vpos < 0 means
17513 that PT is either in one of the lines beginning at
17514 first_unchanged_at_end_row or below the window. Don't care for
17515 lines that might be displayed later at the window end; as
17516 mentioned, this is not a frequent case. */
17517 if (w->cursor.vpos < 0)
17518 {
17519 /* Cursor in unchanged rows at the top? */
17520 if (PT < CHARPOS (start_pos)
17521 && last_unchanged_at_beg_row)
17522 {
17523 row = row_containing_pos (w, PT,
17524 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17525 last_unchanged_at_beg_row + 1, 0);
17526 if (row)
17527 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17528 }
17529
17530 /* Start from first_unchanged_at_end_row looking for PT. */
17531 else if (first_unchanged_at_end_row)
17532 {
17533 row = row_containing_pos (w, PT - delta,
17534 first_unchanged_at_end_row, NULL, 0);
17535 if (row)
17536 set_cursor_from_row (w, row, w->current_matrix, delta,
17537 delta_bytes, dy, dvpos);
17538 }
17539
17540 /* Give up if cursor was not found. */
17541 if (w->cursor.vpos < 0)
17542 {
17543 clear_glyph_matrix (w->desired_matrix);
17544 return -1;
17545 }
17546 }
17547
17548 /* Don't let the cursor end in the scroll margins. */
17549 {
17550 int this_scroll_margin, cursor_height;
17551 int frame_line_height = default_line_pixel_height (w);
17552 int window_total_lines
17553 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
17554
17555 this_scroll_margin =
17556 max (0, min (scroll_margin, window_total_lines / 4));
17557 this_scroll_margin *= frame_line_height;
17558 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17559
17560 if ((w->cursor.y < this_scroll_margin
17561 && CHARPOS (start) > BEGV)
17562 /* Old redisplay didn't take scroll margin into account at the bottom,
17563 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17564 || (w->cursor.y + (make_cursor_line_fully_visible_p
17565 ? cursor_height + this_scroll_margin
17566 : 1)) > it.last_visible_y)
17567 {
17568 w->cursor.vpos = -1;
17569 clear_glyph_matrix (w->desired_matrix);
17570 return -1;
17571 }
17572 }
17573
17574 /* Scroll the display. Do it before changing the current matrix so
17575 that xterm.c doesn't get confused about where the cursor glyph is
17576 found. */
17577 if (dy && run.height)
17578 {
17579 update_begin (f);
17580
17581 if (FRAME_WINDOW_P (f))
17582 {
17583 FRAME_RIF (f)->update_window_begin_hook (w);
17584 FRAME_RIF (f)->clear_window_mouse_face (w);
17585 FRAME_RIF (f)->scroll_run_hook (w, &run);
17586 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17587 }
17588 else
17589 {
17590 /* Terminal frame. In this case, dvpos gives the number of
17591 lines to scroll by; dvpos < 0 means scroll up. */
17592 int from_vpos
17593 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17594 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17595 int end = (WINDOW_TOP_EDGE_LINE (w)
17596 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17597 + window_internal_height (w));
17598
17599 #if defined (HAVE_GPM) || defined (MSDOS)
17600 x_clear_window_mouse_face (w);
17601 #endif
17602 /* Perform the operation on the screen. */
17603 if (dvpos > 0)
17604 {
17605 /* Scroll last_unchanged_at_beg_row to the end of the
17606 window down dvpos lines. */
17607 set_terminal_window (f, end);
17608
17609 /* On dumb terminals delete dvpos lines at the end
17610 before inserting dvpos empty lines. */
17611 if (!FRAME_SCROLL_REGION_OK (f))
17612 ins_del_lines (f, end - dvpos, -dvpos);
17613
17614 /* Insert dvpos empty lines in front of
17615 last_unchanged_at_beg_row. */
17616 ins_del_lines (f, from, dvpos);
17617 }
17618 else if (dvpos < 0)
17619 {
17620 /* Scroll up last_unchanged_at_beg_vpos to the end of
17621 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17622 set_terminal_window (f, end);
17623
17624 /* Delete dvpos lines in front of
17625 last_unchanged_at_beg_vpos. ins_del_lines will set
17626 the cursor to the given vpos and emit |dvpos| delete
17627 line sequences. */
17628 ins_del_lines (f, from + dvpos, dvpos);
17629
17630 /* On a dumb terminal insert dvpos empty lines at the
17631 end. */
17632 if (!FRAME_SCROLL_REGION_OK (f))
17633 ins_del_lines (f, end + dvpos, -dvpos);
17634 }
17635
17636 set_terminal_window (f, 0);
17637 }
17638
17639 update_end (f);
17640 }
17641
17642 /* Shift reused rows of the current matrix to the right position.
17643 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17644 text. */
17645 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17646 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17647 if (dvpos < 0)
17648 {
17649 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17650 bottom_vpos, dvpos);
17651 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17652 bottom_vpos);
17653 }
17654 else if (dvpos > 0)
17655 {
17656 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17657 bottom_vpos, dvpos);
17658 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17659 first_unchanged_at_end_vpos + dvpos);
17660 }
17661
17662 /* For frame-based redisplay, make sure that current frame and window
17663 matrix are in sync with respect to glyph memory. */
17664 if (!FRAME_WINDOW_P (f))
17665 sync_frame_with_window_matrix_rows (w);
17666
17667 /* Adjust buffer positions in reused rows. */
17668 if (delta || delta_bytes)
17669 increment_matrix_positions (current_matrix,
17670 first_unchanged_at_end_vpos + dvpos,
17671 bottom_vpos, delta, delta_bytes);
17672
17673 /* Adjust Y positions. */
17674 if (dy)
17675 shift_glyph_matrix (w, current_matrix,
17676 first_unchanged_at_end_vpos + dvpos,
17677 bottom_vpos, dy);
17678
17679 if (first_unchanged_at_end_row)
17680 {
17681 first_unchanged_at_end_row += dvpos;
17682 if (first_unchanged_at_end_row->y >= it.last_visible_y
17683 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17684 first_unchanged_at_end_row = NULL;
17685 }
17686
17687 /* If scrolling up, there may be some lines to display at the end of
17688 the window. */
17689 last_text_row_at_end = NULL;
17690 if (dy < 0)
17691 {
17692 /* Scrolling up can leave for example a partially visible line
17693 at the end of the window to be redisplayed. */
17694 /* Set last_row to the glyph row in the current matrix where the
17695 window end line is found. It has been moved up or down in
17696 the matrix by dvpos. */
17697 int last_vpos = w->window_end_vpos + dvpos;
17698 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17699
17700 /* If last_row is the window end line, it should display text. */
17701 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
17702
17703 /* If window end line was partially visible before, begin
17704 displaying at that line. Otherwise begin displaying with the
17705 line following it. */
17706 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17707 {
17708 init_to_row_start (&it, w, last_row);
17709 it.vpos = last_vpos;
17710 it.current_y = last_row->y;
17711 }
17712 else
17713 {
17714 init_to_row_end (&it, w, last_row);
17715 it.vpos = 1 + last_vpos;
17716 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17717 ++last_row;
17718 }
17719
17720 /* We may start in a continuation line. If so, we have to
17721 get the right continuation_lines_width and current_x. */
17722 it.continuation_lines_width = last_row->continuation_lines_width;
17723 it.hpos = it.current_x = 0;
17724
17725 /* Display the rest of the lines at the window end. */
17726 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17727 while (it.current_y < it.last_visible_y
17728 && !fonts_changed_p)
17729 {
17730 /* Is it always sure that the display agrees with lines in
17731 the current matrix? I don't think so, so we mark rows
17732 displayed invalid in the current matrix by setting their
17733 enabled_p flag to zero. */
17734 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17735 if (display_line (&it))
17736 last_text_row_at_end = it.glyph_row - 1;
17737 }
17738 }
17739
17740 /* Update window_end_pos and window_end_vpos. */
17741 if (first_unchanged_at_end_row && !last_text_row_at_end)
17742 {
17743 /* Window end line if one of the preserved rows from the current
17744 matrix. Set row to the last row displaying text in current
17745 matrix starting at first_unchanged_at_end_row, after
17746 scrolling. */
17747 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17748 row = find_last_row_displaying_text (w->current_matrix, &it,
17749 first_unchanged_at_end_row);
17750 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17751
17752 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17753 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17754 w->window_end_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
17755 eassert (w->window_end_bytepos >= 0);
17756 IF_DEBUG (debug_method_add (w, "A"));
17757 }
17758 else if (last_text_row_at_end)
17759 {
17760 w->window_end_pos
17761 = Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end);
17762 w->window_end_bytepos
17763 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17764 w->window_end_vpos
17765 = MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix);
17766 eassert (w->window_end_bytepos >= 0);
17767 IF_DEBUG (debug_method_add (w, "B"));
17768 }
17769 else if (last_text_row)
17770 {
17771 /* We have displayed either to the end of the window or at the
17772 end of the window, i.e. the last row with text is to be found
17773 in the desired matrix. */
17774 w->window_end_pos
17775 = Z - MATRIX_ROW_END_CHARPOS (last_text_row);
17776 w->window_end_bytepos
17777 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17778 w->window_end_vpos = MATRIX_ROW_VPOS (last_text_row, desired_matrix);
17779 eassert (w->window_end_bytepos >= 0);
17780 }
17781 else if (first_unchanged_at_end_row == NULL
17782 && last_text_row == NULL
17783 && last_text_row_at_end == NULL)
17784 {
17785 /* Displayed to end of window, but no line containing text was
17786 displayed. Lines were deleted at the end of the window. */
17787 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17788 int vpos = w->window_end_vpos;
17789 struct glyph_row *current_row = current_matrix->rows + vpos;
17790 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17791
17792 for (row = NULL;
17793 row == NULL && vpos >= first_vpos;
17794 --vpos, --current_row, --desired_row)
17795 {
17796 if (desired_row->enabled_p)
17797 {
17798 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
17799 row = desired_row;
17800 }
17801 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
17802 row = current_row;
17803 }
17804
17805 eassert (row != NULL);
17806 w->window_end_vpos = vpos + 1;
17807 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17808 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17809 eassert (w->window_end_bytepos >= 0);
17810 IF_DEBUG (debug_method_add (w, "C"));
17811 }
17812 else
17813 emacs_abort ();
17814
17815 IF_DEBUG (debug_end_pos = w->window_end_pos;
17816 debug_end_vpos = w->window_end_vpos);
17817
17818 /* Record that display has not been completed. */
17819 w->window_end_valid = 0;
17820 w->desired_matrix->no_scrolling_p = 1;
17821 return 3;
17822
17823 #undef GIVE_UP
17824 }
17825
17826
17827 \f
17828 /***********************************************************************
17829 More debugging support
17830 ***********************************************************************/
17831
17832 #ifdef GLYPH_DEBUG
17833
17834 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17835 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17836 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17837
17838
17839 /* Dump the contents of glyph matrix MATRIX on stderr.
17840
17841 GLYPHS 0 means don't show glyph contents.
17842 GLYPHS 1 means show glyphs in short form
17843 GLYPHS > 1 means show glyphs in long form. */
17844
17845 void
17846 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17847 {
17848 int i;
17849 for (i = 0; i < matrix->nrows; ++i)
17850 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17851 }
17852
17853
17854 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17855 the glyph row and area where the glyph comes from. */
17856
17857 void
17858 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17859 {
17860 if (glyph->type == CHAR_GLYPH
17861 || glyph->type == GLYPHLESS_GLYPH)
17862 {
17863 fprintf (stderr,
17864 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17865 glyph - row->glyphs[TEXT_AREA],
17866 (glyph->type == CHAR_GLYPH
17867 ? 'C'
17868 : 'G'),
17869 glyph->charpos,
17870 (BUFFERP (glyph->object)
17871 ? 'B'
17872 : (STRINGP (glyph->object)
17873 ? 'S'
17874 : (INTEGERP (glyph->object)
17875 ? '0'
17876 : '-'))),
17877 glyph->pixel_width,
17878 glyph->u.ch,
17879 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17880 ? glyph->u.ch
17881 : '.'),
17882 glyph->face_id,
17883 glyph->left_box_line_p,
17884 glyph->right_box_line_p);
17885 }
17886 else if (glyph->type == STRETCH_GLYPH)
17887 {
17888 fprintf (stderr,
17889 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17890 glyph - row->glyphs[TEXT_AREA],
17891 'S',
17892 glyph->charpos,
17893 (BUFFERP (glyph->object)
17894 ? 'B'
17895 : (STRINGP (glyph->object)
17896 ? 'S'
17897 : (INTEGERP (glyph->object)
17898 ? '0'
17899 : '-'))),
17900 glyph->pixel_width,
17901 0,
17902 ' ',
17903 glyph->face_id,
17904 glyph->left_box_line_p,
17905 glyph->right_box_line_p);
17906 }
17907 else if (glyph->type == IMAGE_GLYPH)
17908 {
17909 fprintf (stderr,
17910 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17911 glyph - row->glyphs[TEXT_AREA],
17912 'I',
17913 glyph->charpos,
17914 (BUFFERP (glyph->object)
17915 ? 'B'
17916 : (STRINGP (glyph->object)
17917 ? 'S'
17918 : (INTEGERP (glyph->object)
17919 ? '0'
17920 : '-'))),
17921 glyph->pixel_width,
17922 glyph->u.img_id,
17923 '.',
17924 glyph->face_id,
17925 glyph->left_box_line_p,
17926 glyph->right_box_line_p);
17927 }
17928 else if (glyph->type == COMPOSITE_GLYPH)
17929 {
17930 fprintf (stderr,
17931 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
17932 glyph - row->glyphs[TEXT_AREA],
17933 '+',
17934 glyph->charpos,
17935 (BUFFERP (glyph->object)
17936 ? 'B'
17937 : (STRINGP (glyph->object)
17938 ? 'S'
17939 : (INTEGERP (glyph->object)
17940 ? '0'
17941 : '-'))),
17942 glyph->pixel_width,
17943 glyph->u.cmp.id);
17944 if (glyph->u.cmp.automatic)
17945 fprintf (stderr,
17946 "[%d-%d]",
17947 glyph->slice.cmp.from, glyph->slice.cmp.to);
17948 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17949 glyph->face_id,
17950 glyph->left_box_line_p,
17951 glyph->right_box_line_p);
17952 }
17953 }
17954
17955
17956 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17957 GLYPHS 0 means don't show glyph contents.
17958 GLYPHS 1 means show glyphs in short form
17959 GLYPHS > 1 means show glyphs in long form. */
17960
17961 void
17962 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17963 {
17964 if (glyphs != 1)
17965 {
17966 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17967 fprintf (stderr, "==============================================================================\n");
17968
17969 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17970 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17971 vpos,
17972 MATRIX_ROW_START_CHARPOS (row),
17973 MATRIX_ROW_END_CHARPOS (row),
17974 row->used[TEXT_AREA],
17975 row->contains_overlapping_glyphs_p,
17976 row->enabled_p,
17977 row->truncated_on_left_p,
17978 row->truncated_on_right_p,
17979 row->continued_p,
17980 MATRIX_ROW_CONTINUATION_LINE_P (row),
17981 MATRIX_ROW_DISPLAYS_TEXT_P (row),
17982 row->ends_at_zv_p,
17983 row->fill_line_p,
17984 row->ends_in_middle_of_char_p,
17985 row->starts_in_middle_of_char_p,
17986 row->mouse_face_p,
17987 row->x,
17988 row->y,
17989 row->pixel_width,
17990 row->height,
17991 row->visible_height,
17992 row->ascent,
17993 row->phys_ascent);
17994 /* The next 3 lines should align to "Start" in the header. */
17995 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
17996 row->end.overlay_string_index,
17997 row->continuation_lines_width);
17998 fprintf (stderr, " %9"pI"d %9"pI"d\n",
17999 CHARPOS (row->start.string_pos),
18000 CHARPOS (row->end.string_pos));
18001 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18002 row->end.dpvec_index);
18003 }
18004
18005 if (glyphs > 1)
18006 {
18007 int area;
18008
18009 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18010 {
18011 struct glyph *glyph = row->glyphs[area];
18012 struct glyph *glyph_end = glyph + row->used[area];
18013
18014 /* Glyph for a line end in text. */
18015 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18016 ++glyph_end;
18017
18018 if (glyph < glyph_end)
18019 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18020
18021 for (; glyph < glyph_end; ++glyph)
18022 dump_glyph (row, glyph, area);
18023 }
18024 }
18025 else if (glyphs == 1)
18026 {
18027 int area;
18028
18029 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18030 {
18031 char *s = alloca (row->used[area] + 4);
18032 int i;
18033
18034 for (i = 0; i < row->used[area]; ++i)
18035 {
18036 struct glyph *glyph = row->glyphs[area] + i;
18037 if (i == row->used[area] - 1
18038 && area == TEXT_AREA
18039 && INTEGERP (glyph->object)
18040 && glyph->type == CHAR_GLYPH
18041 && glyph->u.ch == ' ')
18042 {
18043 strcpy (&s[i], "[\\n]");
18044 i += 4;
18045 }
18046 else if (glyph->type == CHAR_GLYPH
18047 && glyph->u.ch < 0x80
18048 && glyph->u.ch >= ' ')
18049 s[i] = glyph->u.ch;
18050 else
18051 s[i] = '.';
18052 }
18053
18054 s[i] = '\0';
18055 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18056 }
18057 }
18058 }
18059
18060
18061 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18062 Sdump_glyph_matrix, 0, 1, "p",
18063 doc: /* Dump the current matrix of the selected window to stderr.
18064 Shows contents of glyph row structures. With non-nil
18065 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18066 glyphs in short form, otherwise show glyphs in long form. */)
18067 (Lisp_Object glyphs)
18068 {
18069 struct window *w = XWINDOW (selected_window);
18070 struct buffer *buffer = XBUFFER (w->contents);
18071
18072 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18073 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18074 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18075 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18076 fprintf (stderr, "=============================================\n");
18077 dump_glyph_matrix (w->current_matrix,
18078 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18079 return Qnil;
18080 }
18081
18082
18083 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18084 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18085 (void)
18086 {
18087 struct frame *f = XFRAME (selected_frame);
18088 dump_glyph_matrix (f->current_matrix, 1);
18089 return Qnil;
18090 }
18091
18092
18093 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18094 doc: /* Dump glyph row ROW to stderr.
18095 GLYPH 0 means don't dump glyphs.
18096 GLYPH 1 means dump glyphs in short form.
18097 GLYPH > 1 or omitted means dump glyphs in long form. */)
18098 (Lisp_Object row, Lisp_Object glyphs)
18099 {
18100 struct glyph_matrix *matrix;
18101 EMACS_INT vpos;
18102
18103 CHECK_NUMBER (row);
18104 matrix = XWINDOW (selected_window)->current_matrix;
18105 vpos = XINT (row);
18106 if (vpos >= 0 && vpos < matrix->nrows)
18107 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18108 vpos,
18109 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18110 return Qnil;
18111 }
18112
18113
18114 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18115 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18116 GLYPH 0 means don't dump glyphs.
18117 GLYPH 1 means dump glyphs in short form.
18118 GLYPH > 1 or omitted means dump glyphs in long form. */)
18119 (Lisp_Object row, Lisp_Object glyphs)
18120 {
18121 struct frame *sf = SELECTED_FRAME ();
18122 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18123 EMACS_INT vpos;
18124
18125 CHECK_NUMBER (row);
18126 vpos = XINT (row);
18127 if (vpos >= 0 && vpos < m->nrows)
18128 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18129 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18130 return Qnil;
18131 }
18132
18133
18134 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18135 doc: /* Toggle tracing of redisplay.
18136 With ARG, turn tracing on if and only if ARG is positive. */)
18137 (Lisp_Object arg)
18138 {
18139 if (NILP (arg))
18140 trace_redisplay_p = !trace_redisplay_p;
18141 else
18142 {
18143 arg = Fprefix_numeric_value (arg);
18144 trace_redisplay_p = XINT (arg) > 0;
18145 }
18146
18147 return Qnil;
18148 }
18149
18150
18151 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18152 doc: /* Like `format', but print result to stderr.
18153 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18154 (ptrdiff_t nargs, Lisp_Object *args)
18155 {
18156 Lisp_Object s = Fformat (nargs, args);
18157 fprintf (stderr, "%s", SDATA (s));
18158 return Qnil;
18159 }
18160
18161 #endif /* GLYPH_DEBUG */
18162
18163
18164 \f
18165 /***********************************************************************
18166 Building Desired Matrix Rows
18167 ***********************************************************************/
18168
18169 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18170 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18171
18172 static struct glyph_row *
18173 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18174 {
18175 struct frame *f = XFRAME (WINDOW_FRAME (w));
18176 struct buffer *buffer = XBUFFER (w->contents);
18177 struct buffer *old = current_buffer;
18178 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18179 int arrow_len = SCHARS (overlay_arrow_string);
18180 const unsigned char *arrow_end = arrow_string + arrow_len;
18181 const unsigned char *p;
18182 struct it it;
18183 bool multibyte_p;
18184 int n_glyphs_before;
18185
18186 set_buffer_temp (buffer);
18187 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18188 it.glyph_row->used[TEXT_AREA] = 0;
18189 SET_TEXT_POS (it.position, 0, 0);
18190
18191 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18192 p = arrow_string;
18193 while (p < arrow_end)
18194 {
18195 Lisp_Object face, ilisp;
18196
18197 /* Get the next character. */
18198 if (multibyte_p)
18199 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18200 else
18201 {
18202 it.c = it.char_to_display = *p, it.len = 1;
18203 if (! ASCII_CHAR_P (it.c))
18204 it.char_to_display = BYTE8_TO_CHAR (it.c);
18205 }
18206 p += it.len;
18207
18208 /* Get its face. */
18209 ilisp = make_number (p - arrow_string);
18210 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18211 it.face_id = compute_char_face (f, it.char_to_display, face);
18212
18213 /* Compute its width, get its glyphs. */
18214 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18215 SET_TEXT_POS (it.position, -1, -1);
18216 PRODUCE_GLYPHS (&it);
18217
18218 /* If this character doesn't fit any more in the line, we have
18219 to remove some glyphs. */
18220 if (it.current_x > it.last_visible_x)
18221 {
18222 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18223 break;
18224 }
18225 }
18226
18227 set_buffer_temp (old);
18228 return it.glyph_row;
18229 }
18230
18231
18232 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18233 glyphs to insert is determined by produce_special_glyphs. */
18234
18235 static void
18236 insert_left_trunc_glyphs (struct it *it)
18237 {
18238 struct it truncate_it;
18239 struct glyph *from, *end, *to, *toend;
18240
18241 eassert (!FRAME_WINDOW_P (it->f)
18242 || (!it->glyph_row->reversed_p
18243 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18244 || (it->glyph_row->reversed_p
18245 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18246
18247 /* Get the truncation glyphs. */
18248 truncate_it = *it;
18249 truncate_it.current_x = 0;
18250 truncate_it.face_id = DEFAULT_FACE_ID;
18251 truncate_it.glyph_row = &scratch_glyph_row;
18252 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18253 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18254 truncate_it.object = make_number (0);
18255 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18256
18257 /* Overwrite glyphs from IT with truncation glyphs. */
18258 if (!it->glyph_row->reversed_p)
18259 {
18260 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18261
18262 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18263 end = from + tused;
18264 to = it->glyph_row->glyphs[TEXT_AREA];
18265 toend = to + it->glyph_row->used[TEXT_AREA];
18266 if (FRAME_WINDOW_P (it->f))
18267 {
18268 /* On GUI frames, when variable-size fonts are displayed,
18269 the truncation glyphs may need more pixels than the row's
18270 glyphs they overwrite. We overwrite more glyphs to free
18271 enough screen real estate, and enlarge the stretch glyph
18272 on the right (see display_line), if there is one, to
18273 preserve the screen position of the truncation glyphs on
18274 the right. */
18275 int w = 0;
18276 struct glyph *g = to;
18277 short used;
18278
18279 /* The first glyph could be partially visible, in which case
18280 it->glyph_row->x will be negative. But we want the left
18281 truncation glyphs to be aligned at the left margin of the
18282 window, so we override the x coordinate at which the row
18283 will begin. */
18284 it->glyph_row->x = 0;
18285 while (g < toend && w < it->truncation_pixel_width)
18286 {
18287 w += g->pixel_width;
18288 ++g;
18289 }
18290 if (g - to - tused > 0)
18291 {
18292 memmove (to + tused, g, (toend - g) * sizeof(*g));
18293 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18294 }
18295 used = it->glyph_row->used[TEXT_AREA];
18296 if (it->glyph_row->truncated_on_right_p
18297 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18298 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18299 == STRETCH_GLYPH)
18300 {
18301 int extra = w - it->truncation_pixel_width;
18302
18303 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18304 }
18305 }
18306
18307 while (from < end)
18308 *to++ = *from++;
18309
18310 /* There may be padding glyphs left over. Overwrite them too. */
18311 if (!FRAME_WINDOW_P (it->f))
18312 {
18313 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18314 {
18315 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18316 while (from < end)
18317 *to++ = *from++;
18318 }
18319 }
18320
18321 if (to > toend)
18322 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18323 }
18324 else
18325 {
18326 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18327
18328 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18329 that back to front. */
18330 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18331 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18332 toend = it->glyph_row->glyphs[TEXT_AREA];
18333 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18334 if (FRAME_WINDOW_P (it->f))
18335 {
18336 int w = 0;
18337 struct glyph *g = to;
18338
18339 while (g >= toend && w < it->truncation_pixel_width)
18340 {
18341 w += g->pixel_width;
18342 --g;
18343 }
18344 if (to - g - tused > 0)
18345 to = g + tused;
18346 if (it->glyph_row->truncated_on_right_p
18347 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18348 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18349 {
18350 int extra = w - it->truncation_pixel_width;
18351
18352 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18353 }
18354 }
18355
18356 while (from >= end && to >= toend)
18357 *to-- = *from--;
18358 if (!FRAME_WINDOW_P (it->f))
18359 {
18360 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18361 {
18362 from =
18363 truncate_it.glyph_row->glyphs[TEXT_AREA]
18364 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18365 while (from >= end && to >= toend)
18366 *to-- = *from--;
18367 }
18368 }
18369 if (from >= end)
18370 {
18371 /* Need to free some room before prepending additional
18372 glyphs. */
18373 int move_by = from - end + 1;
18374 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18375 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18376
18377 for ( ; g >= g0; g--)
18378 g[move_by] = *g;
18379 while (from >= end)
18380 *to-- = *from--;
18381 it->glyph_row->used[TEXT_AREA] += move_by;
18382 }
18383 }
18384 }
18385
18386 /* Compute the hash code for ROW. */
18387 unsigned
18388 row_hash (struct glyph_row *row)
18389 {
18390 int area, k;
18391 unsigned hashval = 0;
18392
18393 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18394 for (k = 0; k < row->used[area]; ++k)
18395 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18396 + row->glyphs[area][k].u.val
18397 + row->glyphs[area][k].face_id
18398 + row->glyphs[area][k].padding_p
18399 + (row->glyphs[area][k].type << 2));
18400
18401 return hashval;
18402 }
18403
18404 /* Compute the pixel height and width of IT->glyph_row.
18405
18406 Most of the time, ascent and height of a display line will be equal
18407 to the max_ascent and max_height values of the display iterator
18408 structure. This is not the case if
18409
18410 1. We hit ZV without displaying anything. In this case, max_ascent
18411 and max_height will be zero.
18412
18413 2. We have some glyphs that don't contribute to the line height.
18414 (The glyph row flag contributes_to_line_height_p is for future
18415 pixmap extensions).
18416
18417 The first case is easily covered by using default values because in
18418 these cases, the line height does not really matter, except that it
18419 must not be zero. */
18420
18421 static void
18422 compute_line_metrics (struct it *it)
18423 {
18424 struct glyph_row *row = it->glyph_row;
18425
18426 if (FRAME_WINDOW_P (it->f))
18427 {
18428 int i, min_y, max_y;
18429
18430 /* The line may consist of one space only, that was added to
18431 place the cursor on it. If so, the row's height hasn't been
18432 computed yet. */
18433 if (row->height == 0)
18434 {
18435 if (it->max_ascent + it->max_descent == 0)
18436 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18437 row->ascent = it->max_ascent;
18438 row->height = it->max_ascent + it->max_descent;
18439 row->phys_ascent = it->max_phys_ascent;
18440 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18441 row->extra_line_spacing = it->max_extra_line_spacing;
18442 }
18443
18444 /* Compute the width of this line. */
18445 row->pixel_width = row->x;
18446 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18447 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18448
18449 eassert (row->pixel_width >= 0);
18450 eassert (row->ascent >= 0 && row->height > 0);
18451
18452 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18453 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18454
18455 /* If first line's physical ascent is larger than its logical
18456 ascent, use the physical ascent, and make the row taller.
18457 This makes accented characters fully visible. */
18458 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18459 && row->phys_ascent > row->ascent)
18460 {
18461 row->height += row->phys_ascent - row->ascent;
18462 row->ascent = row->phys_ascent;
18463 }
18464
18465 /* Compute how much of the line is visible. */
18466 row->visible_height = row->height;
18467
18468 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18469 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18470
18471 if (row->y < min_y)
18472 row->visible_height -= min_y - row->y;
18473 if (row->y + row->height > max_y)
18474 row->visible_height -= row->y + row->height - max_y;
18475 }
18476 else
18477 {
18478 row->pixel_width = row->used[TEXT_AREA];
18479 if (row->continued_p)
18480 row->pixel_width -= it->continuation_pixel_width;
18481 else if (row->truncated_on_right_p)
18482 row->pixel_width -= it->truncation_pixel_width;
18483 row->ascent = row->phys_ascent = 0;
18484 row->height = row->phys_height = row->visible_height = 1;
18485 row->extra_line_spacing = 0;
18486 }
18487
18488 /* Compute a hash code for this row. */
18489 row->hash = row_hash (row);
18490
18491 it->max_ascent = it->max_descent = 0;
18492 it->max_phys_ascent = it->max_phys_descent = 0;
18493 }
18494
18495
18496 /* Append one space to the glyph row of iterator IT if doing a
18497 window-based redisplay. The space has the same face as
18498 IT->face_id. Value is non-zero if a space was added.
18499
18500 This function is called to make sure that there is always one glyph
18501 at the end of a glyph row that the cursor can be set on under
18502 window-systems. (If there weren't such a glyph we would not know
18503 how wide and tall a box cursor should be displayed).
18504
18505 At the same time this space let's a nicely handle clearing to the
18506 end of the line if the row ends in italic text. */
18507
18508 static int
18509 append_space_for_newline (struct it *it, int default_face_p)
18510 {
18511 if (FRAME_WINDOW_P (it->f))
18512 {
18513 int n = it->glyph_row->used[TEXT_AREA];
18514
18515 if (it->glyph_row->glyphs[TEXT_AREA] + n
18516 < it->glyph_row->glyphs[1 + TEXT_AREA])
18517 {
18518 /* Save some values that must not be changed.
18519 Must save IT->c and IT->len because otherwise
18520 ITERATOR_AT_END_P wouldn't work anymore after
18521 append_space_for_newline has been called. */
18522 enum display_element_type saved_what = it->what;
18523 int saved_c = it->c, saved_len = it->len;
18524 int saved_char_to_display = it->char_to_display;
18525 int saved_x = it->current_x;
18526 int saved_face_id = it->face_id;
18527 int saved_box_end = it->end_of_box_run_p;
18528 struct text_pos saved_pos;
18529 Lisp_Object saved_object;
18530 struct face *face;
18531
18532 saved_object = it->object;
18533 saved_pos = it->position;
18534
18535 it->what = IT_CHARACTER;
18536 memset (&it->position, 0, sizeof it->position);
18537 it->object = make_number (0);
18538 it->c = it->char_to_display = ' ';
18539 it->len = 1;
18540
18541 /* If the default face was remapped, be sure to use the
18542 remapped face for the appended newline. */
18543 if (default_face_p)
18544 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18545 else if (it->face_before_selective_p)
18546 it->face_id = it->saved_face_id;
18547 face = FACE_FROM_ID (it->f, it->face_id);
18548 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18549 /* In R2L rows, we will prepend a stretch glyph that will
18550 have the end_of_box_run_p flag set for it, so there's no
18551 need for the appended newline glyph to have that flag
18552 set. */
18553 if (it->glyph_row->reversed_p
18554 /* But if the appended newline glyph goes all the way to
18555 the end of the row, there will be no stretch glyph,
18556 so leave the box flag set. */
18557 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18558 it->end_of_box_run_p = 0;
18559
18560 PRODUCE_GLYPHS (it);
18561
18562 it->override_ascent = -1;
18563 it->constrain_row_ascent_descent_p = 0;
18564 it->current_x = saved_x;
18565 it->object = saved_object;
18566 it->position = saved_pos;
18567 it->what = saved_what;
18568 it->face_id = saved_face_id;
18569 it->len = saved_len;
18570 it->c = saved_c;
18571 it->char_to_display = saved_char_to_display;
18572 it->end_of_box_run_p = saved_box_end;
18573 return 1;
18574 }
18575 }
18576
18577 return 0;
18578 }
18579
18580
18581 /* Extend the face of the last glyph in the text area of IT->glyph_row
18582 to the end of the display line. Called from display_line. If the
18583 glyph row is empty, add a space glyph to it so that we know the
18584 face to draw. Set the glyph row flag fill_line_p. If the glyph
18585 row is R2L, prepend a stretch glyph to cover the empty space to the
18586 left of the leftmost glyph. */
18587
18588 static void
18589 extend_face_to_end_of_line (struct it *it)
18590 {
18591 struct face *face, *default_face;
18592 struct frame *f = it->f;
18593
18594 /* If line is already filled, do nothing. Non window-system frames
18595 get a grace of one more ``pixel'' because their characters are
18596 1-``pixel'' wide, so they hit the equality too early. This grace
18597 is needed only for R2L rows that are not continued, to produce
18598 one extra blank where we could display the cursor. */
18599 if (it->current_x >= it->last_visible_x
18600 + (!FRAME_WINDOW_P (f)
18601 && it->glyph_row->reversed_p
18602 && !it->glyph_row->continued_p))
18603 return;
18604
18605 /* The default face, possibly remapped. */
18606 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18607
18608 /* Face extension extends the background and box of IT->face_id
18609 to the end of the line. If the background equals the background
18610 of the frame, we don't have to do anything. */
18611 if (it->face_before_selective_p)
18612 face = FACE_FROM_ID (f, it->saved_face_id);
18613 else
18614 face = FACE_FROM_ID (f, it->face_id);
18615
18616 if (FRAME_WINDOW_P (f)
18617 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
18618 && face->box == FACE_NO_BOX
18619 && face->background == FRAME_BACKGROUND_PIXEL (f)
18620 && !face->stipple
18621 && !it->glyph_row->reversed_p)
18622 return;
18623
18624 /* Set the glyph row flag indicating that the face of the last glyph
18625 in the text area has to be drawn to the end of the text area. */
18626 it->glyph_row->fill_line_p = 1;
18627
18628 /* If current character of IT is not ASCII, make sure we have the
18629 ASCII face. This will be automatically undone the next time
18630 get_next_display_element returns a multibyte character. Note
18631 that the character will always be single byte in unibyte
18632 text. */
18633 if (!ASCII_CHAR_P (it->c))
18634 {
18635 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18636 }
18637
18638 if (FRAME_WINDOW_P (f))
18639 {
18640 /* If the row is empty, add a space with the current face of IT,
18641 so that we know which face to draw. */
18642 if (it->glyph_row->used[TEXT_AREA] == 0)
18643 {
18644 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18645 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18646 it->glyph_row->used[TEXT_AREA] = 1;
18647 }
18648 #ifdef HAVE_WINDOW_SYSTEM
18649 if (it->glyph_row->reversed_p)
18650 {
18651 /* Prepend a stretch glyph to the row, such that the
18652 rightmost glyph will be drawn flushed all the way to the
18653 right margin of the window. The stretch glyph that will
18654 occupy the empty space, if any, to the left of the
18655 glyphs. */
18656 struct font *font = face->font ? face->font : FRAME_FONT (f);
18657 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18658 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18659 struct glyph *g;
18660 int row_width, stretch_ascent, stretch_width;
18661 struct text_pos saved_pos;
18662 int saved_face_id, saved_avoid_cursor, saved_box_start;
18663
18664 for (row_width = 0, g = row_start; g < row_end; g++)
18665 row_width += g->pixel_width;
18666 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18667 if (stretch_width > 0)
18668 {
18669 stretch_ascent =
18670 (((it->ascent + it->descent)
18671 * FONT_BASE (font)) / FONT_HEIGHT (font));
18672 saved_pos = it->position;
18673 memset (&it->position, 0, sizeof it->position);
18674 saved_avoid_cursor = it->avoid_cursor_p;
18675 it->avoid_cursor_p = 1;
18676 saved_face_id = it->face_id;
18677 saved_box_start = it->start_of_box_run_p;
18678 /* The last row's stretch glyph should get the default
18679 face, to avoid painting the rest of the window with
18680 the region face, if the region ends at ZV. */
18681 if (it->glyph_row->ends_at_zv_p)
18682 it->face_id = default_face->id;
18683 else
18684 it->face_id = face->id;
18685 it->start_of_box_run_p = 0;
18686 append_stretch_glyph (it, make_number (0), stretch_width,
18687 it->ascent + it->descent, stretch_ascent);
18688 it->position = saved_pos;
18689 it->avoid_cursor_p = saved_avoid_cursor;
18690 it->face_id = saved_face_id;
18691 it->start_of_box_run_p = saved_box_start;
18692 }
18693 }
18694 #endif /* HAVE_WINDOW_SYSTEM */
18695 }
18696 else
18697 {
18698 /* Save some values that must not be changed. */
18699 int saved_x = it->current_x;
18700 struct text_pos saved_pos;
18701 Lisp_Object saved_object;
18702 enum display_element_type saved_what = it->what;
18703 int saved_face_id = it->face_id;
18704
18705 saved_object = it->object;
18706 saved_pos = it->position;
18707
18708 it->what = IT_CHARACTER;
18709 memset (&it->position, 0, sizeof it->position);
18710 it->object = make_number (0);
18711 it->c = it->char_to_display = ' ';
18712 it->len = 1;
18713 /* The last row's blank glyphs should get the default face, to
18714 avoid painting the rest of the window with the region face,
18715 if the region ends at ZV. */
18716 if (it->glyph_row->ends_at_zv_p)
18717 it->face_id = default_face->id;
18718 else
18719 it->face_id = face->id;
18720
18721 PRODUCE_GLYPHS (it);
18722
18723 while (it->current_x <= it->last_visible_x)
18724 PRODUCE_GLYPHS (it);
18725
18726 /* Don't count these blanks really. It would let us insert a left
18727 truncation glyph below and make us set the cursor on them, maybe. */
18728 it->current_x = saved_x;
18729 it->object = saved_object;
18730 it->position = saved_pos;
18731 it->what = saved_what;
18732 it->face_id = saved_face_id;
18733 }
18734 }
18735
18736
18737 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18738 trailing whitespace. */
18739
18740 static int
18741 trailing_whitespace_p (ptrdiff_t charpos)
18742 {
18743 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18744 int c = 0;
18745
18746 while (bytepos < ZV_BYTE
18747 && (c = FETCH_CHAR (bytepos),
18748 c == ' ' || c == '\t'))
18749 ++bytepos;
18750
18751 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18752 {
18753 if (bytepos != PT_BYTE)
18754 return 1;
18755 }
18756 return 0;
18757 }
18758
18759
18760 /* Highlight trailing whitespace, if any, in ROW. */
18761
18762 static void
18763 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18764 {
18765 int used = row->used[TEXT_AREA];
18766
18767 if (used)
18768 {
18769 struct glyph *start = row->glyphs[TEXT_AREA];
18770 struct glyph *glyph = start + used - 1;
18771
18772 if (row->reversed_p)
18773 {
18774 /* Right-to-left rows need to be processed in the opposite
18775 direction, so swap the edge pointers. */
18776 glyph = start;
18777 start = row->glyphs[TEXT_AREA] + used - 1;
18778 }
18779
18780 /* Skip over glyphs inserted to display the cursor at the
18781 end of a line, for extending the face of the last glyph
18782 to the end of the line on terminals, and for truncation
18783 and continuation glyphs. */
18784 if (!row->reversed_p)
18785 {
18786 while (glyph >= start
18787 && glyph->type == CHAR_GLYPH
18788 && INTEGERP (glyph->object))
18789 --glyph;
18790 }
18791 else
18792 {
18793 while (glyph <= start
18794 && glyph->type == CHAR_GLYPH
18795 && INTEGERP (glyph->object))
18796 ++glyph;
18797 }
18798
18799 /* If last glyph is a space or stretch, and it's trailing
18800 whitespace, set the face of all trailing whitespace glyphs in
18801 IT->glyph_row to `trailing-whitespace'. */
18802 if ((row->reversed_p ? glyph <= start : glyph >= start)
18803 && BUFFERP (glyph->object)
18804 && (glyph->type == STRETCH_GLYPH
18805 || (glyph->type == CHAR_GLYPH
18806 && glyph->u.ch == ' '))
18807 && trailing_whitespace_p (glyph->charpos))
18808 {
18809 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18810 if (face_id < 0)
18811 return;
18812
18813 if (!row->reversed_p)
18814 {
18815 while (glyph >= start
18816 && BUFFERP (glyph->object)
18817 && (glyph->type == STRETCH_GLYPH
18818 || (glyph->type == CHAR_GLYPH
18819 && glyph->u.ch == ' ')))
18820 (glyph--)->face_id = face_id;
18821 }
18822 else
18823 {
18824 while (glyph <= start
18825 && BUFFERP (glyph->object)
18826 && (glyph->type == STRETCH_GLYPH
18827 || (glyph->type == CHAR_GLYPH
18828 && glyph->u.ch == ' ')))
18829 (glyph++)->face_id = face_id;
18830 }
18831 }
18832 }
18833 }
18834
18835
18836 /* Value is non-zero if glyph row ROW should be
18837 considered to hold the buffer position CHARPOS. */
18838
18839 static int
18840 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
18841 {
18842 int result = 1;
18843
18844 if (charpos == CHARPOS (row->end.pos)
18845 || charpos == MATRIX_ROW_END_CHARPOS (row))
18846 {
18847 /* Suppose the row ends on a string.
18848 Unless the row is continued, that means it ends on a newline
18849 in the string. If it's anything other than a display string
18850 (e.g., a before-string from an overlay), we don't want the
18851 cursor there. (This heuristic seems to give the optimal
18852 behavior for the various types of multi-line strings.)
18853 One exception: if the string has `cursor' property on one of
18854 its characters, we _do_ want the cursor there. */
18855 if (CHARPOS (row->end.string_pos) >= 0)
18856 {
18857 if (row->continued_p)
18858 result = 1;
18859 else
18860 {
18861 /* Check for `display' property. */
18862 struct glyph *beg = row->glyphs[TEXT_AREA];
18863 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18864 struct glyph *glyph;
18865
18866 result = 0;
18867 for (glyph = end; glyph >= beg; --glyph)
18868 if (STRINGP (glyph->object))
18869 {
18870 Lisp_Object prop
18871 = Fget_char_property (make_number (charpos),
18872 Qdisplay, Qnil);
18873 result =
18874 (!NILP (prop)
18875 && display_prop_string_p (prop, glyph->object));
18876 /* If there's a `cursor' property on one of the
18877 string's characters, this row is a cursor row,
18878 even though this is not a display string. */
18879 if (!result)
18880 {
18881 Lisp_Object s = glyph->object;
18882
18883 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18884 {
18885 ptrdiff_t gpos = glyph->charpos;
18886
18887 if (!NILP (Fget_char_property (make_number (gpos),
18888 Qcursor, s)))
18889 {
18890 result = 1;
18891 break;
18892 }
18893 }
18894 }
18895 break;
18896 }
18897 }
18898 }
18899 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18900 {
18901 /* If the row ends in middle of a real character,
18902 and the line is continued, we want the cursor here.
18903 That's because CHARPOS (ROW->end.pos) would equal
18904 PT if PT is before the character. */
18905 if (!row->ends_in_ellipsis_p)
18906 result = row->continued_p;
18907 else
18908 /* If the row ends in an ellipsis, then
18909 CHARPOS (ROW->end.pos) will equal point after the
18910 invisible text. We want that position to be displayed
18911 after the ellipsis. */
18912 result = 0;
18913 }
18914 /* If the row ends at ZV, display the cursor at the end of that
18915 row instead of at the start of the row below. */
18916 else if (row->ends_at_zv_p)
18917 result = 1;
18918 else
18919 result = 0;
18920 }
18921
18922 return result;
18923 }
18924
18925 /* Value is non-zero if glyph row ROW should be
18926 used to hold the cursor. */
18927
18928 static int
18929 cursor_row_p (struct glyph_row *row)
18930 {
18931 return row_for_charpos_p (row, PT);
18932 }
18933
18934 \f
18935
18936 /* Push the property PROP so that it will be rendered at the current
18937 position in IT. Return 1 if PROP was successfully pushed, 0
18938 otherwise. Called from handle_line_prefix to handle the
18939 `line-prefix' and `wrap-prefix' properties. */
18940
18941 static int
18942 push_prefix_prop (struct it *it, Lisp_Object prop)
18943 {
18944 struct text_pos pos =
18945 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18946
18947 eassert (it->method == GET_FROM_BUFFER
18948 || it->method == GET_FROM_DISPLAY_VECTOR
18949 || it->method == GET_FROM_STRING);
18950
18951 /* We need to save the current buffer/string position, so it will be
18952 restored by pop_it, because iterate_out_of_display_property
18953 depends on that being set correctly, but some situations leave
18954 it->position not yet set when this function is called. */
18955 push_it (it, &pos);
18956
18957 if (STRINGP (prop))
18958 {
18959 if (SCHARS (prop) == 0)
18960 {
18961 pop_it (it);
18962 return 0;
18963 }
18964
18965 it->string = prop;
18966 it->string_from_prefix_prop_p = 1;
18967 it->multibyte_p = STRING_MULTIBYTE (it->string);
18968 it->current.overlay_string_index = -1;
18969 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18970 it->end_charpos = it->string_nchars = SCHARS (it->string);
18971 it->method = GET_FROM_STRING;
18972 it->stop_charpos = 0;
18973 it->prev_stop = 0;
18974 it->base_level_stop = 0;
18975
18976 /* Force paragraph direction to be that of the parent
18977 buffer/string. */
18978 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18979 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18980 else
18981 it->paragraph_embedding = L2R;
18982
18983 /* Set up the bidi iterator for this display string. */
18984 if (it->bidi_p)
18985 {
18986 it->bidi_it.string.lstring = it->string;
18987 it->bidi_it.string.s = NULL;
18988 it->bidi_it.string.schars = it->end_charpos;
18989 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18990 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18991 it->bidi_it.string.unibyte = !it->multibyte_p;
18992 it->bidi_it.w = it->w;
18993 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18994 }
18995 }
18996 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18997 {
18998 it->method = GET_FROM_STRETCH;
18999 it->object = prop;
19000 }
19001 #ifdef HAVE_WINDOW_SYSTEM
19002 else if (IMAGEP (prop))
19003 {
19004 it->what = IT_IMAGE;
19005 it->image_id = lookup_image (it->f, prop);
19006 it->method = GET_FROM_IMAGE;
19007 }
19008 #endif /* HAVE_WINDOW_SYSTEM */
19009 else
19010 {
19011 pop_it (it); /* bogus display property, give up */
19012 return 0;
19013 }
19014
19015 return 1;
19016 }
19017
19018 /* Return the character-property PROP at the current position in IT. */
19019
19020 static Lisp_Object
19021 get_it_property (struct it *it, Lisp_Object prop)
19022 {
19023 Lisp_Object position, object = it->object;
19024
19025 if (STRINGP (object))
19026 position = make_number (IT_STRING_CHARPOS (*it));
19027 else if (BUFFERP (object))
19028 {
19029 position = make_number (IT_CHARPOS (*it));
19030 object = it->window;
19031 }
19032 else
19033 return Qnil;
19034
19035 return Fget_char_property (position, prop, object);
19036 }
19037
19038 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19039
19040 static void
19041 handle_line_prefix (struct it *it)
19042 {
19043 Lisp_Object prefix;
19044
19045 if (it->continuation_lines_width > 0)
19046 {
19047 prefix = get_it_property (it, Qwrap_prefix);
19048 if (NILP (prefix))
19049 prefix = Vwrap_prefix;
19050 }
19051 else
19052 {
19053 prefix = get_it_property (it, Qline_prefix);
19054 if (NILP (prefix))
19055 prefix = Vline_prefix;
19056 }
19057 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19058 {
19059 /* If the prefix is wider than the window, and we try to wrap
19060 it, it would acquire its own wrap prefix, and so on till the
19061 iterator stack overflows. So, don't wrap the prefix. */
19062 it->line_wrap = TRUNCATE;
19063 it->avoid_cursor_p = 1;
19064 }
19065 }
19066
19067 \f
19068
19069 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19070 only for R2L lines from display_line and display_string, when they
19071 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19072 the line/string needs to be continued on the next glyph row. */
19073 static void
19074 unproduce_glyphs (struct it *it, int n)
19075 {
19076 struct glyph *glyph, *end;
19077
19078 eassert (it->glyph_row);
19079 eassert (it->glyph_row->reversed_p);
19080 eassert (it->area == TEXT_AREA);
19081 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19082
19083 if (n > it->glyph_row->used[TEXT_AREA])
19084 n = it->glyph_row->used[TEXT_AREA];
19085 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19086 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19087 for ( ; glyph < end; glyph++)
19088 glyph[-n] = *glyph;
19089 }
19090
19091 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19092 and ROW->maxpos. */
19093 static void
19094 find_row_edges (struct it *it, struct glyph_row *row,
19095 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19096 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19097 {
19098 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19099 lines' rows is implemented for bidi-reordered rows. */
19100
19101 /* ROW->minpos is the value of min_pos, the minimal buffer position
19102 we have in ROW, or ROW->start.pos if that is smaller. */
19103 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19104 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19105 else
19106 /* We didn't find buffer positions smaller than ROW->start, or
19107 didn't find _any_ valid buffer positions in any of the glyphs,
19108 so we must trust the iterator's computed positions. */
19109 row->minpos = row->start.pos;
19110 if (max_pos <= 0)
19111 {
19112 max_pos = CHARPOS (it->current.pos);
19113 max_bpos = BYTEPOS (it->current.pos);
19114 }
19115
19116 /* Here are the various use-cases for ending the row, and the
19117 corresponding values for ROW->maxpos:
19118
19119 Line ends in a newline from buffer eol_pos + 1
19120 Line is continued from buffer max_pos + 1
19121 Line is truncated on right it->current.pos
19122 Line ends in a newline from string max_pos + 1(*)
19123 (*) + 1 only when line ends in a forward scan
19124 Line is continued from string max_pos
19125 Line is continued from display vector max_pos
19126 Line is entirely from a string min_pos == max_pos
19127 Line is entirely from a display vector min_pos == max_pos
19128 Line that ends at ZV ZV
19129
19130 If you discover other use-cases, please add them here as
19131 appropriate. */
19132 if (row->ends_at_zv_p)
19133 row->maxpos = it->current.pos;
19134 else if (row->used[TEXT_AREA])
19135 {
19136 int seen_this_string = 0;
19137 struct glyph_row *r1 = row - 1;
19138
19139 /* Did we see the same display string on the previous row? */
19140 if (STRINGP (it->object)
19141 /* this is not the first row */
19142 && row > it->w->desired_matrix->rows
19143 /* previous row is not the header line */
19144 && !r1->mode_line_p
19145 /* previous row also ends in a newline from a string */
19146 && r1->ends_in_newline_from_string_p)
19147 {
19148 struct glyph *start, *end;
19149
19150 /* Search for the last glyph of the previous row that came
19151 from buffer or string. Depending on whether the row is
19152 L2R or R2L, we need to process it front to back or the
19153 other way round. */
19154 if (!r1->reversed_p)
19155 {
19156 start = r1->glyphs[TEXT_AREA];
19157 end = start + r1->used[TEXT_AREA];
19158 /* Glyphs inserted by redisplay have an integer (zero)
19159 as their object. */
19160 while (end > start
19161 && INTEGERP ((end - 1)->object)
19162 && (end - 1)->charpos <= 0)
19163 --end;
19164 if (end > start)
19165 {
19166 if (EQ ((end - 1)->object, it->object))
19167 seen_this_string = 1;
19168 }
19169 else
19170 /* If all the glyphs of the previous row were inserted
19171 by redisplay, it means the previous row was
19172 produced from a single newline, which is only
19173 possible if that newline came from the same string
19174 as the one which produced this ROW. */
19175 seen_this_string = 1;
19176 }
19177 else
19178 {
19179 end = r1->glyphs[TEXT_AREA] - 1;
19180 start = end + r1->used[TEXT_AREA];
19181 while (end < start
19182 && INTEGERP ((end + 1)->object)
19183 && (end + 1)->charpos <= 0)
19184 ++end;
19185 if (end < start)
19186 {
19187 if (EQ ((end + 1)->object, it->object))
19188 seen_this_string = 1;
19189 }
19190 else
19191 seen_this_string = 1;
19192 }
19193 }
19194 /* Take note of each display string that covers a newline only
19195 once, the first time we see it. This is for when a display
19196 string includes more than one newline in it. */
19197 if (row->ends_in_newline_from_string_p && !seen_this_string)
19198 {
19199 /* If we were scanning the buffer forward when we displayed
19200 the string, we want to account for at least one buffer
19201 position that belongs to this row (position covered by
19202 the display string), so that cursor positioning will
19203 consider this row as a candidate when point is at the end
19204 of the visual line represented by this row. This is not
19205 required when scanning back, because max_pos will already
19206 have a much larger value. */
19207 if (CHARPOS (row->end.pos) > max_pos)
19208 INC_BOTH (max_pos, max_bpos);
19209 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19210 }
19211 else if (CHARPOS (it->eol_pos) > 0)
19212 SET_TEXT_POS (row->maxpos,
19213 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19214 else if (row->continued_p)
19215 {
19216 /* If max_pos is different from IT's current position, it
19217 means IT->method does not belong to the display element
19218 at max_pos. However, it also means that the display
19219 element at max_pos was displayed in its entirety on this
19220 line, which is equivalent to saying that the next line
19221 starts at the next buffer position. */
19222 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19223 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19224 else
19225 {
19226 INC_BOTH (max_pos, max_bpos);
19227 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19228 }
19229 }
19230 else if (row->truncated_on_right_p)
19231 /* display_line already called reseat_at_next_visible_line_start,
19232 which puts the iterator at the beginning of the next line, in
19233 the logical order. */
19234 row->maxpos = it->current.pos;
19235 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19236 /* A line that is entirely from a string/image/stretch... */
19237 row->maxpos = row->minpos;
19238 else
19239 emacs_abort ();
19240 }
19241 else
19242 row->maxpos = it->current.pos;
19243 }
19244
19245 /* Construct the glyph row IT->glyph_row in the desired matrix of
19246 IT->w from text at the current position of IT. See dispextern.h
19247 for an overview of struct it. Value is non-zero if
19248 IT->glyph_row displays text, as opposed to a line displaying ZV
19249 only. */
19250
19251 static int
19252 display_line (struct it *it)
19253 {
19254 struct glyph_row *row = it->glyph_row;
19255 Lisp_Object overlay_arrow_string;
19256 struct it wrap_it;
19257 void *wrap_data = NULL;
19258 int may_wrap = 0, wrap_x IF_LINT (= 0);
19259 int wrap_row_used = -1;
19260 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19261 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19262 int wrap_row_extra_line_spacing IF_LINT (= 0);
19263 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19264 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19265 int cvpos;
19266 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19267 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19268
19269 /* We always start displaying at hpos zero even if hscrolled. */
19270 eassert (it->hpos == 0 && it->current_x == 0);
19271
19272 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19273 >= it->w->desired_matrix->nrows)
19274 {
19275 it->w->nrows_scale_factor++;
19276 fonts_changed_p = 1;
19277 return 0;
19278 }
19279
19280 /* Is IT->w showing the region? */
19281 it->w->region_showing = it->region_beg_charpos > 0 ? it->region_beg_charpos : 0;
19282
19283 /* Clear the result glyph row and enable it. */
19284 prepare_desired_row (row);
19285
19286 row->y = it->current_y;
19287 row->start = it->start;
19288 row->continuation_lines_width = it->continuation_lines_width;
19289 row->displays_text_p = 1;
19290 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19291 it->starts_in_middle_of_char_p = 0;
19292
19293 /* Arrange the overlays nicely for our purposes. Usually, we call
19294 display_line on only one line at a time, in which case this
19295 can't really hurt too much, or we call it on lines which appear
19296 one after another in the buffer, in which case all calls to
19297 recenter_overlay_lists but the first will be pretty cheap. */
19298 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19299
19300 /* Move over display elements that are not visible because we are
19301 hscrolled. This may stop at an x-position < IT->first_visible_x
19302 if the first glyph is partially visible or if we hit a line end. */
19303 if (it->current_x < it->first_visible_x)
19304 {
19305 enum move_it_result move_result;
19306
19307 this_line_min_pos = row->start.pos;
19308 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19309 MOVE_TO_POS | MOVE_TO_X);
19310 /* If we are under a large hscroll, move_it_in_display_line_to
19311 could hit the end of the line without reaching
19312 it->first_visible_x. Pretend that we did reach it. This is
19313 especially important on a TTY, where we will call
19314 extend_face_to_end_of_line, which needs to know how many
19315 blank glyphs to produce. */
19316 if (it->current_x < it->first_visible_x
19317 && (move_result == MOVE_NEWLINE_OR_CR
19318 || move_result == MOVE_POS_MATCH_OR_ZV))
19319 it->current_x = it->first_visible_x;
19320
19321 /* Record the smallest positions seen while we moved over
19322 display elements that are not visible. This is needed by
19323 redisplay_internal for optimizing the case where the cursor
19324 stays inside the same line. The rest of this function only
19325 considers positions that are actually displayed, so
19326 RECORD_MAX_MIN_POS will not otherwise record positions that
19327 are hscrolled to the left of the left edge of the window. */
19328 min_pos = CHARPOS (this_line_min_pos);
19329 min_bpos = BYTEPOS (this_line_min_pos);
19330 }
19331 else
19332 {
19333 /* We only do this when not calling `move_it_in_display_line_to'
19334 above, because move_it_in_display_line_to calls
19335 handle_line_prefix itself. */
19336 handle_line_prefix (it);
19337 }
19338
19339 /* Get the initial row height. This is either the height of the
19340 text hscrolled, if there is any, or zero. */
19341 row->ascent = it->max_ascent;
19342 row->height = it->max_ascent + it->max_descent;
19343 row->phys_ascent = it->max_phys_ascent;
19344 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19345 row->extra_line_spacing = it->max_extra_line_spacing;
19346
19347 /* Utility macro to record max and min buffer positions seen until now. */
19348 #define RECORD_MAX_MIN_POS(IT) \
19349 do \
19350 { \
19351 int composition_p = !STRINGP ((IT)->string) \
19352 && ((IT)->what == IT_COMPOSITION); \
19353 ptrdiff_t current_pos = \
19354 composition_p ? (IT)->cmp_it.charpos \
19355 : IT_CHARPOS (*(IT)); \
19356 ptrdiff_t current_bpos = \
19357 composition_p ? CHAR_TO_BYTE (current_pos) \
19358 : IT_BYTEPOS (*(IT)); \
19359 if (current_pos < min_pos) \
19360 { \
19361 min_pos = current_pos; \
19362 min_bpos = current_bpos; \
19363 } \
19364 if (IT_CHARPOS (*it) > max_pos) \
19365 { \
19366 max_pos = IT_CHARPOS (*it); \
19367 max_bpos = IT_BYTEPOS (*it); \
19368 } \
19369 } \
19370 while (0)
19371
19372 /* Loop generating characters. The loop is left with IT on the next
19373 character to display. */
19374 while (1)
19375 {
19376 int n_glyphs_before, hpos_before, x_before;
19377 int x, nglyphs;
19378 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19379
19380 /* Retrieve the next thing to display. Value is zero if end of
19381 buffer reached. */
19382 if (!get_next_display_element (it))
19383 {
19384 /* Maybe add a space at the end of this line that is used to
19385 display the cursor there under X. Set the charpos of the
19386 first glyph of blank lines not corresponding to any text
19387 to -1. */
19388 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19389 row->exact_window_width_line_p = 1;
19390 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19391 || row->used[TEXT_AREA] == 0)
19392 {
19393 row->glyphs[TEXT_AREA]->charpos = -1;
19394 row->displays_text_p = 0;
19395
19396 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
19397 && (!MINI_WINDOW_P (it->w)
19398 || (minibuf_level && EQ (it->window, minibuf_window))))
19399 row->indicate_empty_line_p = 1;
19400 }
19401
19402 it->continuation_lines_width = 0;
19403 row->ends_at_zv_p = 1;
19404 /* A row that displays right-to-left text must always have
19405 its last face extended all the way to the end of line,
19406 even if this row ends in ZV, because we still write to
19407 the screen left to right. We also need to extend the
19408 last face if the default face is remapped to some
19409 different face, otherwise the functions that clear
19410 portions of the screen will clear with the default face's
19411 background color. */
19412 if (row->reversed_p
19413 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19414 extend_face_to_end_of_line (it);
19415 break;
19416 }
19417
19418 /* Now, get the metrics of what we want to display. This also
19419 generates glyphs in `row' (which is IT->glyph_row). */
19420 n_glyphs_before = row->used[TEXT_AREA];
19421 x = it->current_x;
19422
19423 /* Remember the line height so far in case the next element doesn't
19424 fit on the line. */
19425 if (it->line_wrap != TRUNCATE)
19426 {
19427 ascent = it->max_ascent;
19428 descent = it->max_descent;
19429 phys_ascent = it->max_phys_ascent;
19430 phys_descent = it->max_phys_descent;
19431
19432 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19433 {
19434 if (IT_DISPLAYING_WHITESPACE (it))
19435 may_wrap = 1;
19436 else if (may_wrap)
19437 {
19438 SAVE_IT (wrap_it, *it, wrap_data);
19439 wrap_x = x;
19440 wrap_row_used = row->used[TEXT_AREA];
19441 wrap_row_ascent = row->ascent;
19442 wrap_row_height = row->height;
19443 wrap_row_phys_ascent = row->phys_ascent;
19444 wrap_row_phys_height = row->phys_height;
19445 wrap_row_extra_line_spacing = row->extra_line_spacing;
19446 wrap_row_min_pos = min_pos;
19447 wrap_row_min_bpos = min_bpos;
19448 wrap_row_max_pos = max_pos;
19449 wrap_row_max_bpos = max_bpos;
19450 may_wrap = 0;
19451 }
19452 }
19453 }
19454
19455 PRODUCE_GLYPHS (it);
19456
19457 /* If this display element was in marginal areas, continue with
19458 the next one. */
19459 if (it->area != TEXT_AREA)
19460 {
19461 row->ascent = max (row->ascent, it->max_ascent);
19462 row->height = max (row->height, it->max_ascent + it->max_descent);
19463 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19464 row->phys_height = max (row->phys_height,
19465 it->max_phys_ascent + it->max_phys_descent);
19466 row->extra_line_spacing = max (row->extra_line_spacing,
19467 it->max_extra_line_spacing);
19468 set_iterator_to_next (it, 1);
19469 continue;
19470 }
19471
19472 /* Does the display element fit on the line? If we truncate
19473 lines, we should draw past the right edge of the window. If
19474 we don't truncate, we want to stop so that we can display the
19475 continuation glyph before the right margin. If lines are
19476 continued, there are two possible strategies for characters
19477 resulting in more than 1 glyph (e.g. tabs): Display as many
19478 glyphs as possible in this line and leave the rest for the
19479 continuation line, or display the whole element in the next
19480 line. Original redisplay did the former, so we do it also. */
19481 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19482 hpos_before = it->hpos;
19483 x_before = x;
19484
19485 if (/* Not a newline. */
19486 nglyphs > 0
19487 /* Glyphs produced fit entirely in the line. */
19488 && it->current_x < it->last_visible_x)
19489 {
19490 it->hpos += nglyphs;
19491 row->ascent = max (row->ascent, it->max_ascent);
19492 row->height = max (row->height, it->max_ascent + it->max_descent);
19493 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19494 row->phys_height = max (row->phys_height,
19495 it->max_phys_ascent + it->max_phys_descent);
19496 row->extra_line_spacing = max (row->extra_line_spacing,
19497 it->max_extra_line_spacing);
19498 if (it->current_x - it->pixel_width < it->first_visible_x)
19499 row->x = x - it->first_visible_x;
19500 /* Record the maximum and minimum buffer positions seen so
19501 far in glyphs that will be displayed by this row. */
19502 if (it->bidi_p)
19503 RECORD_MAX_MIN_POS (it);
19504 }
19505 else
19506 {
19507 int i, new_x;
19508 struct glyph *glyph;
19509
19510 for (i = 0; i < nglyphs; ++i, x = new_x)
19511 {
19512 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19513 new_x = x + glyph->pixel_width;
19514
19515 if (/* Lines are continued. */
19516 it->line_wrap != TRUNCATE
19517 && (/* Glyph doesn't fit on the line. */
19518 new_x > it->last_visible_x
19519 /* Or it fits exactly on a window system frame. */
19520 || (new_x == it->last_visible_x
19521 && FRAME_WINDOW_P (it->f)
19522 && (row->reversed_p
19523 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19524 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19525 {
19526 /* End of a continued line. */
19527
19528 if (it->hpos == 0
19529 || (new_x == it->last_visible_x
19530 && FRAME_WINDOW_P (it->f)
19531 && (row->reversed_p
19532 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19533 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19534 {
19535 /* Current glyph is the only one on the line or
19536 fits exactly on the line. We must continue
19537 the line because we can't draw the cursor
19538 after the glyph. */
19539 row->continued_p = 1;
19540 it->current_x = new_x;
19541 it->continuation_lines_width += new_x;
19542 ++it->hpos;
19543 if (i == nglyphs - 1)
19544 {
19545 /* If line-wrap is on, check if a previous
19546 wrap point was found. */
19547 if (wrap_row_used > 0
19548 /* Even if there is a previous wrap
19549 point, continue the line here as
19550 usual, if (i) the previous character
19551 was a space or tab AND (ii) the
19552 current character is not. */
19553 && (!may_wrap
19554 || IT_DISPLAYING_WHITESPACE (it)))
19555 goto back_to_wrap;
19556
19557 /* Record the maximum and minimum buffer
19558 positions seen so far in glyphs that will be
19559 displayed by this row. */
19560 if (it->bidi_p)
19561 RECORD_MAX_MIN_POS (it);
19562 set_iterator_to_next (it, 1);
19563 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19564 {
19565 if (!get_next_display_element (it))
19566 {
19567 row->exact_window_width_line_p = 1;
19568 it->continuation_lines_width = 0;
19569 row->continued_p = 0;
19570 row->ends_at_zv_p = 1;
19571 }
19572 else if (ITERATOR_AT_END_OF_LINE_P (it))
19573 {
19574 row->continued_p = 0;
19575 row->exact_window_width_line_p = 1;
19576 }
19577 }
19578 }
19579 else if (it->bidi_p)
19580 RECORD_MAX_MIN_POS (it);
19581 }
19582 else if (CHAR_GLYPH_PADDING_P (*glyph)
19583 && !FRAME_WINDOW_P (it->f))
19584 {
19585 /* A padding glyph that doesn't fit on this line.
19586 This means the whole character doesn't fit
19587 on the line. */
19588 if (row->reversed_p)
19589 unproduce_glyphs (it, row->used[TEXT_AREA]
19590 - n_glyphs_before);
19591 row->used[TEXT_AREA] = n_glyphs_before;
19592
19593 /* Fill the rest of the row with continuation
19594 glyphs like in 20.x. */
19595 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19596 < row->glyphs[1 + TEXT_AREA])
19597 produce_special_glyphs (it, IT_CONTINUATION);
19598
19599 row->continued_p = 1;
19600 it->current_x = x_before;
19601 it->continuation_lines_width += x_before;
19602
19603 /* Restore the height to what it was before the
19604 element not fitting on the line. */
19605 it->max_ascent = ascent;
19606 it->max_descent = descent;
19607 it->max_phys_ascent = phys_ascent;
19608 it->max_phys_descent = phys_descent;
19609 }
19610 else if (wrap_row_used > 0)
19611 {
19612 back_to_wrap:
19613 if (row->reversed_p)
19614 unproduce_glyphs (it,
19615 row->used[TEXT_AREA] - wrap_row_used);
19616 RESTORE_IT (it, &wrap_it, wrap_data);
19617 it->continuation_lines_width += wrap_x;
19618 row->used[TEXT_AREA] = wrap_row_used;
19619 row->ascent = wrap_row_ascent;
19620 row->height = wrap_row_height;
19621 row->phys_ascent = wrap_row_phys_ascent;
19622 row->phys_height = wrap_row_phys_height;
19623 row->extra_line_spacing = wrap_row_extra_line_spacing;
19624 min_pos = wrap_row_min_pos;
19625 min_bpos = wrap_row_min_bpos;
19626 max_pos = wrap_row_max_pos;
19627 max_bpos = wrap_row_max_bpos;
19628 row->continued_p = 1;
19629 row->ends_at_zv_p = 0;
19630 row->exact_window_width_line_p = 0;
19631 it->continuation_lines_width += x;
19632
19633 /* Make sure that a non-default face is extended
19634 up to the right margin of the window. */
19635 extend_face_to_end_of_line (it);
19636 }
19637 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19638 {
19639 /* A TAB that extends past the right edge of the
19640 window. This produces a single glyph on
19641 window system frames. We leave the glyph in
19642 this row and let it fill the row, but don't
19643 consume the TAB. */
19644 if ((row->reversed_p
19645 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19646 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19647 produce_special_glyphs (it, IT_CONTINUATION);
19648 it->continuation_lines_width += it->last_visible_x;
19649 row->ends_in_middle_of_char_p = 1;
19650 row->continued_p = 1;
19651 glyph->pixel_width = it->last_visible_x - x;
19652 it->starts_in_middle_of_char_p = 1;
19653 }
19654 else
19655 {
19656 /* Something other than a TAB that draws past
19657 the right edge of the window. Restore
19658 positions to values before the element. */
19659 if (row->reversed_p)
19660 unproduce_glyphs (it, row->used[TEXT_AREA]
19661 - (n_glyphs_before + i));
19662 row->used[TEXT_AREA] = n_glyphs_before + i;
19663
19664 /* Display continuation glyphs. */
19665 it->current_x = x_before;
19666 it->continuation_lines_width += x;
19667 if (!FRAME_WINDOW_P (it->f)
19668 || (row->reversed_p
19669 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19670 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19671 produce_special_glyphs (it, IT_CONTINUATION);
19672 row->continued_p = 1;
19673
19674 extend_face_to_end_of_line (it);
19675
19676 if (nglyphs > 1 && i > 0)
19677 {
19678 row->ends_in_middle_of_char_p = 1;
19679 it->starts_in_middle_of_char_p = 1;
19680 }
19681
19682 /* Restore the height to what it was before the
19683 element not fitting on the line. */
19684 it->max_ascent = ascent;
19685 it->max_descent = descent;
19686 it->max_phys_ascent = phys_ascent;
19687 it->max_phys_descent = phys_descent;
19688 }
19689
19690 break;
19691 }
19692 else if (new_x > it->first_visible_x)
19693 {
19694 /* Increment number of glyphs actually displayed. */
19695 ++it->hpos;
19696
19697 /* Record the maximum and minimum buffer positions
19698 seen so far in glyphs that will be displayed by
19699 this row. */
19700 if (it->bidi_p)
19701 RECORD_MAX_MIN_POS (it);
19702
19703 if (x < it->first_visible_x)
19704 /* Glyph is partially visible, i.e. row starts at
19705 negative X position. */
19706 row->x = x - it->first_visible_x;
19707 }
19708 else
19709 {
19710 /* Glyph is completely off the left margin of the
19711 window. This should not happen because of the
19712 move_it_in_display_line at the start of this
19713 function, unless the text display area of the
19714 window is empty. */
19715 eassert (it->first_visible_x <= it->last_visible_x);
19716 }
19717 }
19718 /* Even if this display element produced no glyphs at all,
19719 we want to record its position. */
19720 if (it->bidi_p && nglyphs == 0)
19721 RECORD_MAX_MIN_POS (it);
19722
19723 row->ascent = max (row->ascent, it->max_ascent);
19724 row->height = max (row->height, it->max_ascent + it->max_descent);
19725 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19726 row->phys_height = max (row->phys_height,
19727 it->max_phys_ascent + it->max_phys_descent);
19728 row->extra_line_spacing = max (row->extra_line_spacing,
19729 it->max_extra_line_spacing);
19730
19731 /* End of this display line if row is continued. */
19732 if (row->continued_p || row->ends_at_zv_p)
19733 break;
19734 }
19735
19736 at_end_of_line:
19737 /* Is this a line end? If yes, we're also done, after making
19738 sure that a non-default face is extended up to the right
19739 margin of the window. */
19740 if (ITERATOR_AT_END_OF_LINE_P (it))
19741 {
19742 int used_before = row->used[TEXT_AREA];
19743
19744 row->ends_in_newline_from_string_p = STRINGP (it->object);
19745
19746 /* Add a space at the end of the line that is used to
19747 display the cursor there. */
19748 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19749 append_space_for_newline (it, 0);
19750
19751 /* Extend the face to the end of the line. */
19752 extend_face_to_end_of_line (it);
19753
19754 /* Make sure we have the position. */
19755 if (used_before == 0)
19756 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19757
19758 /* Record the position of the newline, for use in
19759 find_row_edges. */
19760 it->eol_pos = it->current.pos;
19761
19762 /* Consume the line end. This skips over invisible lines. */
19763 set_iterator_to_next (it, 1);
19764 it->continuation_lines_width = 0;
19765 break;
19766 }
19767
19768 /* Proceed with next display element. Note that this skips
19769 over lines invisible because of selective display. */
19770 set_iterator_to_next (it, 1);
19771
19772 /* If we truncate lines, we are done when the last displayed
19773 glyphs reach past the right margin of the window. */
19774 if (it->line_wrap == TRUNCATE
19775 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19776 ? (it->current_x >= it->last_visible_x)
19777 : (it->current_x > it->last_visible_x)))
19778 {
19779 /* Maybe add truncation glyphs. */
19780 if (!FRAME_WINDOW_P (it->f)
19781 || (row->reversed_p
19782 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19783 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19784 {
19785 int i, n;
19786
19787 if (!row->reversed_p)
19788 {
19789 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19790 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19791 break;
19792 }
19793 else
19794 {
19795 for (i = 0; i < row->used[TEXT_AREA]; i++)
19796 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19797 break;
19798 /* Remove any padding glyphs at the front of ROW, to
19799 make room for the truncation glyphs we will be
19800 adding below. The loop below always inserts at
19801 least one truncation glyph, so also remove the
19802 last glyph added to ROW. */
19803 unproduce_glyphs (it, i + 1);
19804 /* Adjust i for the loop below. */
19805 i = row->used[TEXT_AREA] - (i + 1);
19806 }
19807
19808 it->current_x = x_before;
19809 if (!FRAME_WINDOW_P (it->f))
19810 {
19811 for (n = row->used[TEXT_AREA]; i < n; ++i)
19812 {
19813 row->used[TEXT_AREA] = i;
19814 produce_special_glyphs (it, IT_TRUNCATION);
19815 }
19816 }
19817 else
19818 {
19819 row->used[TEXT_AREA] = i;
19820 produce_special_glyphs (it, IT_TRUNCATION);
19821 }
19822 }
19823 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19824 {
19825 /* Don't truncate if we can overflow newline into fringe. */
19826 if (!get_next_display_element (it))
19827 {
19828 it->continuation_lines_width = 0;
19829 row->ends_at_zv_p = 1;
19830 row->exact_window_width_line_p = 1;
19831 break;
19832 }
19833 if (ITERATOR_AT_END_OF_LINE_P (it))
19834 {
19835 row->exact_window_width_line_p = 1;
19836 goto at_end_of_line;
19837 }
19838 it->current_x = x_before;
19839 }
19840
19841 row->truncated_on_right_p = 1;
19842 it->continuation_lines_width = 0;
19843 reseat_at_next_visible_line_start (it, 0);
19844 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19845 it->hpos = hpos_before;
19846 break;
19847 }
19848 }
19849
19850 if (wrap_data)
19851 bidi_unshelve_cache (wrap_data, 1);
19852
19853 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19854 at the left window margin. */
19855 if (it->first_visible_x
19856 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19857 {
19858 if (!FRAME_WINDOW_P (it->f)
19859 || (row->reversed_p
19860 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19861 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19862 insert_left_trunc_glyphs (it);
19863 row->truncated_on_left_p = 1;
19864 }
19865
19866 /* Remember the position at which this line ends.
19867
19868 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19869 cannot be before the call to find_row_edges below, since that is
19870 where these positions are determined. */
19871 row->end = it->current;
19872 if (!it->bidi_p)
19873 {
19874 row->minpos = row->start.pos;
19875 row->maxpos = row->end.pos;
19876 }
19877 else
19878 {
19879 /* ROW->minpos and ROW->maxpos must be the smallest and
19880 `1 + the largest' buffer positions in ROW. But if ROW was
19881 bidi-reordered, these two positions can be anywhere in the
19882 row, so we must determine them now. */
19883 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19884 }
19885
19886 /* If the start of this line is the overlay arrow-position, then
19887 mark this glyph row as the one containing the overlay arrow.
19888 This is clearly a mess with variable size fonts. It would be
19889 better to let it be displayed like cursors under X. */
19890 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
19891 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19892 !NILP (overlay_arrow_string)))
19893 {
19894 /* Overlay arrow in window redisplay is a fringe bitmap. */
19895 if (STRINGP (overlay_arrow_string))
19896 {
19897 struct glyph_row *arrow_row
19898 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19899 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19900 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19901 struct glyph *p = row->glyphs[TEXT_AREA];
19902 struct glyph *p2, *end;
19903
19904 /* Copy the arrow glyphs. */
19905 while (glyph < arrow_end)
19906 *p++ = *glyph++;
19907
19908 /* Throw away padding glyphs. */
19909 p2 = p;
19910 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19911 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19912 ++p2;
19913 if (p2 > p)
19914 {
19915 while (p2 < end)
19916 *p++ = *p2++;
19917 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19918 }
19919 }
19920 else
19921 {
19922 eassert (INTEGERP (overlay_arrow_string));
19923 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19924 }
19925 overlay_arrow_seen = 1;
19926 }
19927
19928 /* Highlight trailing whitespace. */
19929 if (!NILP (Vshow_trailing_whitespace))
19930 highlight_trailing_whitespace (it->f, it->glyph_row);
19931
19932 /* Compute pixel dimensions of this line. */
19933 compute_line_metrics (it);
19934
19935 /* Implementation note: No changes in the glyphs of ROW or in their
19936 faces can be done past this point, because compute_line_metrics
19937 computes ROW's hash value and stores it within the glyph_row
19938 structure. */
19939
19940 /* Record whether this row ends inside an ellipsis. */
19941 row->ends_in_ellipsis_p
19942 = (it->method == GET_FROM_DISPLAY_VECTOR
19943 && it->ellipsis_p);
19944
19945 /* Save fringe bitmaps in this row. */
19946 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19947 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19948 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19949 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19950
19951 it->left_user_fringe_bitmap = 0;
19952 it->left_user_fringe_face_id = 0;
19953 it->right_user_fringe_bitmap = 0;
19954 it->right_user_fringe_face_id = 0;
19955
19956 /* Maybe set the cursor. */
19957 cvpos = it->w->cursor.vpos;
19958 if ((cvpos < 0
19959 /* In bidi-reordered rows, keep checking for proper cursor
19960 position even if one has been found already, because buffer
19961 positions in such rows change non-linearly with ROW->VPOS,
19962 when a line is continued. One exception: when we are at ZV,
19963 display cursor on the first suitable glyph row, since all
19964 the empty rows after that also have their position set to ZV. */
19965 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19966 lines' rows is implemented for bidi-reordered rows. */
19967 || (it->bidi_p
19968 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19969 && PT >= MATRIX_ROW_START_CHARPOS (row)
19970 && PT <= MATRIX_ROW_END_CHARPOS (row)
19971 && cursor_row_p (row))
19972 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19973
19974 /* Prepare for the next line. This line starts horizontally at (X
19975 HPOS) = (0 0). Vertical positions are incremented. As a
19976 convenience for the caller, IT->glyph_row is set to the next
19977 row to be used. */
19978 it->current_x = it->hpos = 0;
19979 it->current_y += row->height;
19980 SET_TEXT_POS (it->eol_pos, 0, 0);
19981 ++it->vpos;
19982 ++it->glyph_row;
19983 /* The next row should by default use the same value of the
19984 reversed_p flag as this one. set_iterator_to_next decides when
19985 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19986 the flag accordingly. */
19987 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19988 it->glyph_row->reversed_p = row->reversed_p;
19989 it->start = row->end;
19990 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
19991
19992 #undef RECORD_MAX_MIN_POS
19993 }
19994
19995 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19996 Scurrent_bidi_paragraph_direction, 0, 1, 0,
19997 doc: /* Return paragraph direction at point in BUFFER.
19998 Value is either `left-to-right' or `right-to-left'.
19999 If BUFFER is omitted or nil, it defaults to the current buffer.
20000
20001 Paragraph direction determines how the text in the paragraph is displayed.
20002 In left-to-right paragraphs, text begins at the left margin of the window
20003 and the reading direction is generally left to right. In right-to-left
20004 paragraphs, text begins at the right margin and is read from right to left.
20005
20006 See also `bidi-paragraph-direction'. */)
20007 (Lisp_Object buffer)
20008 {
20009 struct buffer *buf = current_buffer;
20010 struct buffer *old = buf;
20011
20012 if (! NILP (buffer))
20013 {
20014 CHECK_BUFFER (buffer);
20015 buf = XBUFFER (buffer);
20016 }
20017
20018 if (NILP (BVAR (buf, bidi_display_reordering))
20019 || NILP (BVAR (buf, enable_multibyte_characters))
20020 /* When we are loading loadup.el, the character property tables
20021 needed for bidi iteration are not yet available. */
20022 || !NILP (Vpurify_flag))
20023 return Qleft_to_right;
20024 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20025 return BVAR (buf, bidi_paragraph_direction);
20026 else
20027 {
20028 /* Determine the direction from buffer text. We could try to
20029 use current_matrix if it is up to date, but this seems fast
20030 enough as it is. */
20031 struct bidi_it itb;
20032 ptrdiff_t pos = BUF_PT (buf);
20033 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20034 int c;
20035 void *itb_data = bidi_shelve_cache ();
20036
20037 set_buffer_temp (buf);
20038 /* bidi_paragraph_init finds the base direction of the paragraph
20039 by searching forward from paragraph start. We need the base
20040 direction of the current or _previous_ paragraph, so we need
20041 to make sure we are within that paragraph. To that end, find
20042 the previous non-empty line. */
20043 if (pos >= ZV && pos > BEGV)
20044 DEC_BOTH (pos, bytepos);
20045 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20046 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20047 {
20048 while ((c = FETCH_BYTE (bytepos)) == '\n'
20049 || c == ' ' || c == '\t' || c == '\f')
20050 {
20051 if (bytepos <= BEGV_BYTE)
20052 break;
20053 bytepos--;
20054 pos--;
20055 }
20056 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20057 bytepos--;
20058 }
20059 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20060 itb.paragraph_dir = NEUTRAL_DIR;
20061 itb.string.s = NULL;
20062 itb.string.lstring = Qnil;
20063 itb.string.bufpos = 0;
20064 itb.string.unibyte = 0;
20065 /* We have no window to use here for ignoring window-specific
20066 overlays. Using NULL for window pointer will cause
20067 compute_display_string_pos to use the current buffer. */
20068 itb.w = NULL;
20069 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20070 bidi_unshelve_cache (itb_data, 0);
20071 set_buffer_temp (old);
20072 switch (itb.paragraph_dir)
20073 {
20074 case L2R:
20075 return Qleft_to_right;
20076 break;
20077 case R2L:
20078 return Qright_to_left;
20079 break;
20080 default:
20081 emacs_abort ();
20082 }
20083 }
20084 }
20085
20086 DEFUN ("move-point-visually", Fmove_point_visually,
20087 Smove_point_visually, 1, 1, 0,
20088 doc: /* Move point in the visual order in the specified DIRECTION.
20089 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
20090 left.
20091
20092 Value is the new character position of point. */)
20093 (Lisp_Object direction)
20094 {
20095 struct window *w = XWINDOW (selected_window);
20096 struct buffer *b = XBUFFER (w->contents);
20097 struct glyph_row *row;
20098 int dir;
20099 Lisp_Object paragraph_dir;
20100
20101 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
20102 (!(ROW)->continued_p \
20103 && INTEGERP ((GLYPH)->object) \
20104 && (GLYPH)->type == CHAR_GLYPH \
20105 && (GLYPH)->u.ch == ' ' \
20106 && (GLYPH)->charpos >= 0 \
20107 && !(GLYPH)->avoid_cursor_p)
20108
20109 CHECK_NUMBER (direction);
20110 dir = XINT (direction);
20111 if (dir > 0)
20112 dir = 1;
20113 else
20114 dir = -1;
20115
20116 /* If current matrix is up-to-date, we can use the information
20117 recorded in the glyphs, at least as long as the goal is on the
20118 screen. */
20119 if (w->window_end_valid
20120 && !windows_or_buffers_changed
20121 && b
20122 && !b->clip_changed
20123 && !b->prevent_redisplay_optimizations_p
20124 && !window_outdated (w)
20125 && w->cursor.vpos >= 0
20126 && w->cursor.vpos < w->current_matrix->nrows
20127 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
20128 {
20129 struct glyph *g = row->glyphs[TEXT_AREA];
20130 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
20131 struct glyph *gpt = g + w->cursor.hpos;
20132
20133 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
20134 {
20135 if (BUFFERP (g->object) && g->charpos != PT)
20136 {
20137 SET_PT (g->charpos);
20138 w->cursor.vpos = -1;
20139 return make_number (PT);
20140 }
20141 else if (!INTEGERP (g->object) && !EQ (g->object, gpt->object))
20142 {
20143 ptrdiff_t new_pos;
20144
20145 if (BUFFERP (gpt->object))
20146 {
20147 new_pos = PT;
20148 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
20149 new_pos += (row->reversed_p ? -dir : dir);
20150 else
20151 new_pos -= (row->reversed_p ? -dir : dir);;
20152 }
20153 else if (BUFFERP (g->object))
20154 new_pos = g->charpos;
20155 else
20156 break;
20157 SET_PT (new_pos);
20158 w->cursor.vpos = -1;
20159 return make_number (PT);
20160 }
20161 else if (ROW_GLYPH_NEWLINE_P (row, g))
20162 {
20163 /* Glyphs inserted at the end of a non-empty line for
20164 positioning the cursor have zero charpos, so we must
20165 deduce the value of point by other means. */
20166 if (g->charpos > 0)
20167 SET_PT (g->charpos);
20168 else if (row->ends_at_zv_p && PT != ZV)
20169 SET_PT (ZV);
20170 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
20171 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20172 else
20173 break;
20174 w->cursor.vpos = -1;
20175 return make_number (PT);
20176 }
20177 }
20178 if (g == e || INTEGERP (g->object))
20179 {
20180 if (row->truncated_on_left_p || row->truncated_on_right_p)
20181 goto simulate_display;
20182 if (!row->reversed_p)
20183 row += dir;
20184 else
20185 row -= dir;
20186 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
20187 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
20188 goto simulate_display;
20189
20190 if (dir > 0)
20191 {
20192 if (row->reversed_p && !row->continued_p)
20193 {
20194 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20195 w->cursor.vpos = -1;
20196 return make_number (PT);
20197 }
20198 g = row->glyphs[TEXT_AREA];
20199 e = g + row->used[TEXT_AREA];
20200 for ( ; g < e; g++)
20201 {
20202 if (BUFFERP (g->object)
20203 /* Empty lines have only one glyph, which stands
20204 for the newline, and whose charpos is the
20205 buffer position of the newline. */
20206 || ROW_GLYPH_NEWLINE_P (row, g)
20207 /* When the buffer ends in a newline, the line at
20208 EOB also has one glyph, but its charpos is -1. */
20209 || (row->ends_at_zv_p
20210 && !row->reversed_p
20211 && INTEGERP (g->object)
20212 && g->type == CHAR_GLYPH
20213 && g->u.ch == ' '))
20214 {
20215 if (g->charpos > 0)
20216 SET_PT (g->charpos);
20217 else if (!row->reversed_p
20218 && row->ends_at_zv_p
20219 && PT != ZV)
20220 SET_PT (ZV);
20221 else
20222 continue;
20223 w->cursor.vpos = -1;
20224 return make_number (PT);
20225 }
20226 }
20227 }
20228 else
20229 {
20230 if (!row->reversed_p && !row->continued_p)
20231 {
20232 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20233 w->cursor.vpos = -1;
20234 return make_number (PT);
20235 }
20236 e = row->glyphs[TEXT_AREA];
20237 g = e + row->used[TEXT_AREA] - 1;
20238 for ( ; g >= e; g--)
20239 {
20240 if (BUFFERP (g->object)
20241 || (ROW_GLYPH_NEWLINE_P (row, g)
20242 && g->charpos > 0)
20243 /* Empty R2L lines on GUI frames have the buffer
20244 position of the newline stored in the stretch
20245 glyph. */
20246 || g->type == STRETCH_GLYPH
20247 || (row->ends_at_zv_p
20248 && row->reversed_p
20249 && INTEGERP (g->object)
20250 && g->type == CHAR_GLYPH
20251 && g->u.ch == ' '))
20252 {
20253 if (g->charpos > 0)
20254 SET_PT (g->charpos);
20255 else if (row->reversed_p
20256 && row->ends_at_zv_p
20257 && PT != ZV)
20258 SET_PT (ZV);
20259 else
20260 continue;
20261 w->cursor.vpos = -1;
20262 return make_number (PT);
20263 }
20264 }
20265 }
20266 }
20267 }
20268
20269 simulate_display:
20270
20271 /* If we wind up here, we failed to move by using the glyphs, so we
20272 need to simulate display instead. */
20273
20274 if (b)
20275 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
20276 else
20277 paragraph_dir = Qleft_to_right;
20278 if (EQ (paragraph_dir, Qright_to_left))
20279 dir = -dir;
20280 if (PT <= BEGV && dir < 0)
20281 xsignal0 (Qbeginning_of_buffer);
20282 else if (PT >= ZV && dir > 0)
20283 xsignal0 (Qend_of_buffer);
20284 else
20285 {
20286 struct text_pos pt;
20287 struct it it;
20288 int pt_x, target_x, pixel_width, pt_vpos;
20289 bool at_eol_p;
20290 bool overshoot_expected = false;
20291 bool target_is_eol_p = false;
20292
20293 /* Setup the arena. */
20294 SET_TEXT_POS (pt, PT, PT_BYTE);
20295 start_display (&it, w, pt);
20296
20297 if (it.cmp_it.id < 0
20298 && it.method == GET_FROM_STRING
20299 && it.area == TEXT_AREA
20300 && it.string_from_display_prop_p
20301 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
20302 overshoot_expected = true;
20303
20304 /* Find the X coordinate of point. We start from the beginning
20305 of this or previous line to make sure we are before point in
20306 the logical order (since the move_it_* functions can only
20307 move forward). */
20308 reseat_at_previous_visible_line_start (&it);
20309 it.current_x = it.hpos = it.current_y = it.vpos = 0;
20310 if (IT_CHARPOS (it) != PT)
20311 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
20312 -1, -1, -1, MOVE_TO_POS);
20313 pt_x = it.current_x;
20314 pt_vpos = it.vpos;
20315 if (dir > 0 || overshoot_expected)
20316 {
20317 struct glyph_row *row = it.glyph_row;
20318
20319 /* When point is at beginning of line, we don't have
20320 information about the glyph there loaded into struct
20321 it. Calling get_next_display_element fixes that. */
20322 if (pt_x == 0)
20323 get_next_display_element (&it);
20324 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20325 it.glyph_row = NULL;
20326 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
20327 it.glyph_row = row;
20328 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
20329 it, lest it will become out of sync with it's buffer
20330 position. */
20331 it.current_x = pt_x;
20332 }
20333 else
20334 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20335 pixel_width = it.pixel_width;
20336 if (overshoot_expected && at_eol_p)
20337 pixel_width = 0;
20338 else if (pixel_width <= 0)
20339 pixel_width = 1;
20340
20341 /* If there's a display string at point, we are actually at the
20342 glyph to the left of point, so we need to correct the X
20343 coordinate. */
20344 if (overshoot_expected)
20345 pt_x += pixel_width;
20346
20347 /* Compute target X coordinate, either to the left or to the
20348 right of point. On TTY frames, all characters have the same
20349 pixel width of 1, so we can use that. On GUI frames we don't
20350 have an easy way of getting at the pixel width of the
20351 character to the left of point, so we use a different method
20352 of getting to that place. */
20353 if (dir > 0)
20354 target_x = pt_x + pixel_width;
20355 else
20356 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
20357
20358 /* Target X coordinate could be one line above or below the line
20359 of point, in which case we need to adjust the target X
20360 coordinate. Also, if moving to the left, we need to begin at
20361 the left edge of the point's screen line. */
20362 if (dir < 0)
20363 {
20364 if (pt_x > 0)
20365 {
20366 start_display (&it, w, pt);
20367 reseat_at_previous_visible_line_start (&it);
20368 it.current_x = it.current_y = it.hpos = 0;
20369 if (pt_vpos != 0)
20370 move_it_by_lines (&it, pt_vpos);
20371 }
20372 else
20373 {
20374 move_it_by_lines (&it, -1);
20375 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
20376 target_is_eol_p = true;
20377 }
20378 }
20379 else
20380 {
20381 if (at_eol_p
20382 || (target_x >= it.last_visible_x
20383 && it.line_wrap != TRUNCATE))
20384 {
20385 if (pt_x > 0)
20386 move_it_by_lines (&it, 0);
20387 move_it_by_lines (&it, 1);
20388 target_x = 0;
20389 }
20390 }
20391
20392 /* Move to the target X coordinate. */
20393 #ifdef HAVE_WINDOW_SYSTEM
20394 /* On GUI frames, as we don't know the X coordinate of the
20395 character to the left of point, moving point to the left
20396 requires walking, one grapheme cluster at a time, until we
20397 find ourself at a place immediately to the left of the
20398 character at point. */
20399 if (FRAME_WINDOW_P (it.f) && dir < 0)
20400 {
20401 struct text_pos new_pos = it.current.pos;
20402 enum move_it_result rc = MOVE_X_REACHED;
20403
20404 while (it.current_x + it.pixel_width <= target_x
20405 && rc == MOVE_X_REACHED)
20406 {
20407 int new_x = it.current_x + it.pixel_width;
20408
20409 new_pos = it.current.pos;
20410 if (new_x == it.current_x)
20411 new_x++;
20412 rc = move_it_in_display_line_to (&it, ZV, new_x,
20413 MOVE_TO_POS | MOVE_TO_X);
20414 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
20415 break;
20416 }
20417 /* If we ended up on a composed character inside
20418 bidi-reordered text (e.g., Hebrew text with diacritics),
20419 the iterator gives us the buffer position of the last (in
20420 logical order) character of the composed grapheme cluster,
20421 which is not what we want. So we cheat: we compute the
20422 character position of the character that follows (in the
20423 logical order) the one where the above loop stopped. That
20424 character will appear on display to the left of point. */
20425 if (it.bidi_p
20426 && it.bidi_it.scan_dir == -1
20427 && new_pos.charpos - IT_CHARPOS (it) > 1)
20428 {
20429 new_pos.charpos = IT_CHARPOS (it) + 1;
20430 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
20431 }
20432 it.current.pos = new_pos;
20433 }
20434 else
20435 #endif
20436 if (it.current_x != target_x)
20437 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
20438
20439 /* When lines are truncated, the above loop will stop at the
20440 window edge. But we want to get to the end of line, even if
20441 it is beyond the window edge; automatic hscroll will then
20442 scroll the window to show point as appropriate. */
20443 if (target_is_eol_p && it.line_wrap == TRUNCATE
20444 && get_next_display_element (&it))
20445 {
20446 struct text_pos new_pos = it.current.pos;
20447
20448 while (!ITERATOR_AT_END_OF_LINE_P (&it))
20449 {
20450 set_iterator_to_next (&it, 0);
20451 if (it.method == GET_FROM_BUFFER)
20452 new_pos = it.current.pos;
20453 if (!get_next_display_element (&it))
20454 break;
20455 }
20456
20457 it.current.pos = new_pos;
20458 }
20459
20460 /* If we ended up in a display string that covers point, move to
20461 buffer position to the right in the visual order. */
20462 if (dir > 0)
20463 {
20464 while (IT_CHARPOS (it) == PT)
20465 {
20466 set_iterator_to_next (&it, 0);
20467 if (!get_next_display_element (&it))
20468 break;
20469 }
20470 }
20471
20472 /* Move point to that position. */
20473 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
20474 }
20475
20476 return make_number (PT);
20477
20478 #undef ROW_GLYPH_NEWLINE_P
20479 }
20480
20481 \f
20482 /***********************************************************************
20483 Menu Bar
20484 ***********************************************************************/
20485
20486 /* Redisplay the menu bar in the frame for window W.
20487
20488 The menu bar of X frames that don't have X toolkit support is
20489 displayed in a special window W->frame->menu_bar_window.
20490
20491 The menu bar of terminal frames is treated specially as far as
20492 glyph matrices are concerned. Menu bar lines are not part of
20493 windows, so the update is done directly on the frame matrix rows
20494 for the menu bar. */
20495
20496 static void
20497 display_menu_bar (struct window *w)
20498 {
20499 struct frame *f = XFRAME (WINDOW_FRAME (w));
20500 struct it it;
20501 Lisp_Object items;
20502 int i;
20503
20504 /* Don't do all this for graphical frames. */
20505 #ifdef HAVE_NTGUI
20506 if (FRAME_W32_P (f))
20507 return;
20508 #endif
20509 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20510 if (FRAME_X_P (f))
20511 return;
20512 #endif
20513
20514 #ifdef HAVE_NS
20515 if (FRAME_NS_P (f))
20516 return;
20517 #endif /* HAVE_NS */
20518
20519 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20520 eassert (!FRAME_WINDOW_P (f));
20521 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20522 it.first_visible_x = 0;
20523 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20524 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
20525 if (FRAME_WINDOW_P (f))
20526 {
20527 /* Menu bar lines are displayed in the desired matrix of the
20528 dummy window menu_bar_window. */
20529 struct window *menu_w;
20530 menu_w = XWINDOW (f->menu_bar_window);
20531 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20532 MENU_FACE_ID);
20533 it.first_visible_x = 0;
20534 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20535 }
20536 else
20537 #endif /* not USE_X_TOOLKIT and not USE_GTK */
20538 {
20539 /* This is a TTY frame, i.e. character hpos/vpos are used as
20540 pixel x/y. */
20541 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20542 MENU_FACE_ID);
20543 it.first_visible_x = 0;
20544 it.last_visible_x = FRAME_COLS (f);
20545 }
20546
20547 /* FIXME: This should be controlled by a user option. See the
20548 comments in redisplay_tool_bar and display_mode_line about
20549 this. */
20550 it.paragraph_embedding = L2R;
20551
20552 /* Clear all rows of the menu bar. */
20553 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20554 {
20555 struct glyph_row *row = it.glyph_row + i;
20556 clear_glyph_row (row);
20557 row->enabled_p = 1;
20558 row->full_width_p = 1;
20559 }
20560
20561 /* Display all items of the menu bar. */
20562 items = FRAME_MENU_BAR_ITEMS (it.f);
20563 for (i = 0; i < ASIZE (items); i += 4)
20564 {
20565 Lisp_Object string;
20566
20567 /* Stop at nil string. */
20568 string = AREF (items, i + 1);
20569 if (NILP (string))
20570 break;
20571
20572 /* Remember where item was displayed. */
20573 ASET (items, i + 3, make_number (it.hpos));
20574
20575 /* Display the item, pad with one space. */
20576 if (it.current_x < it.last_visible_x)
20577 display_string (NULL, string, Qnil, 0, 0, &it,
20578 SCHARS (string) + 1, 0, 0, -1);
20579 }
20580
20581 /* Fill out the line with spaces. */
20582 if (it.current_x < it.last_visible_x)
20583 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20584
20585 /* Compute the total height of the lines. */
20586 compute_line_metrics (&it);
20587 }
20588
20589
20590 \f
20591 /***********************************************************************
20592 Mode Line
20593 ***********************************************************************/
20594
20595 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20596 FORCE is non-zero, redisplay mode lines unconditionally.
20597 Otherwise, redisplay only mode lines that are garbaged. Value is
20598 the number of windows whose mode lines were redisplayed. */
20599
20600 static int
20601 redisplay_mode_lines (Lisp_Object window, int force)
20602 {
20603 int nwindows = 0;
20604
20605 while (!NILP (window))
20606 {
20607 struct window *w = XWINDOW (window);
20608
20609 if (WINDOWP (w->contents))
20610 nwindows += redisplay_mode_lines (w->contents, force);
20611 else if (force
20612 || FRAME_GARBAGED_P (XFRAME (w->frame))
20613 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20614 {
20615 struct text_pos lpoint;
20616 struct buffer *old = current_buffer;
20617
20618 /* Set the window's buffer for the mode line display. */
20619 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20620 set_buffer_internal_1 (XBUFFER (w->contents));
20621
20622 /* Point refers normally to the selected window. For any
20623 other window, set up appropriate value. */
20624 if (!EQ (window, selected_window))
20625 {
20626 struct text_pos pt;
20627
20628 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
20629 if (CHARPOS (pt) < BEGV)
20630 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
20631 else if (CHARPOS (pt) > (ZV - 1))
20632 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
20633 else
20634 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20635 }
20636
20637 /* Display mode lines. */
20638 clear_glyph_matrix (w->desired_matrix);
20639 if (display_mode_lines (w))
20640 {
20641 ++nwindows;
20642 w->must_be_updated_p = 1;
20643 }
20644
20645 /* Restore old settings. */
20646 set_buffer_internal_1 (old);
20647 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20648 }
20649
20650 window = w->next;
20651 }
20652
20653 return nwindows;
20654 }
20655
20656
20657 /* Display the mode and/or header line of window W. Value is the
20658 sum number of mode lines and header lines displayed. */
20659
20660 static int
20661 display_mode_lines (struct window *w)
20662 {
20663 Lisp_Object old_selected_window = selected_window;
20664 Lisp_Object old_selected_frame = selected_frame;
20665 Lisp_Object new_frame = w->frame;
20666 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
20667 int n = 0;
20668
20669 selected_frame = new_frame;
20670 /* FIXME: If we were to allow the mode-line's computation changing the buffer
20671 or window's point, then we'd need select_window_1 here as well. */
20672 XSETWINDOW (selected_window, w);
20673 XFRAME (new_frame)->selected_window = selected_window;
20674
20675 /* These will be set while the mode line specs are processed. */
20676 line_number_displayed = 0;
20677 w->column_number_displayed = -1;
20678
20679 if (WINDOW_WANTS_MODELINE_P (w))
20680 {
20681 struct window *sel_w = XWINDOW (old_selected_window);
20682
20683 /* Select mode line face based on the real selected window. */
20684 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20685 BVAR (current_buffer, mode_line_format));
20686 ++n;
20687 }
20688
20689 if (WINDOW_WANTS_HEADER_LINE_P (w))
20690 {
20691 display_mode_line (w, HEADER_LINE_FACE_ID,
20692 BVAR (current_buffer, header_line_format));
20693 ++n;
20694 }
20695
20696 XFRAME (new_frame)->selected_window = old_frame_selected_window;
20697 selected_frame = old_selected_frame;
20698 selected_window = old_selected_window;
20699 return n;
20700 }
20701
20702
20703 /* Display mode or header line of window W. FACE_ID specifies which
20704 line to display; it is either MODE_LINE_FACE_ID or
20705 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20706 display. Value is the pixel height of the mode/header line
20707 displayed. */
20708
20709 static int
20710 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20711 {
20712 struct it it;
20713 struct face *face;
20714 ptrdiff_t count = SPECPDL_INDEX ();
20715
20716 init_iterator (&it, w, -1, -1, NULL, face_id);
20717 /* Don't extend on a previously drawn mode-line.
20718 This may happen if called from pos_visible_p. */
20719 it.glyph_row->enabled_p = 0;
20720 prepare_desired_row (it.glyph_row);
20721
20722 it.glyph_row->mode_line_p = 1;
20723
20724 /* FIXME: This should be controlled by a user option. But
20725 supporting such an option is not trivial, since the mode line is
20726 made up of many separate strings. */
20727 it.paragraph_embedding = L2R;
20728
20729 record_unwind_protect (unwind_format_mode_line,
20730 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20731
20732 mode_line_target = MODE_LINE_DISPLAY;
20733
20734 /* Temporarily make frame's keyboard the current kboard so that
20735 kboard-local variables in the mode_line_format will get the right
20736 values. */
20737 push_kboard (FRAME_KBOARD (it.f));
20738 record_unwind_save_match_data ();
20739 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20740 pop_kboard ();
20741
20742 unbind_to (count, Qnil);
20743
20744 /* Fill up with spaces. */
20745 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20746
20747 compute_line_metrics (&it);
20748 it.glyph_row->full_width_p = 1;
20749 it.glyph_row->continued_p = 0;
20750 it.glyph_row->truncated_on_left_p = 0;
20751 it.glyph_row->truncated_on_right_p = 0;
20752
20753 /* Make a 3D mode-line have a shadow at its right end. */
20754 face = FACE_FROM_ID (it.f, face_id);
20755 extend_face_to_end_of_line (&it);
20756 if (face->box != FACE_NO_BOX)
20757 {
20758 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20759 + it.glyph_row->used[TEXT_AREA] - 1);
20760 last->right_box_line_p = 1;
20761 }
20762
20763 return it.glyph_row->height;
20764 }
20765
20766 /* Move element ELT in LIST to the front of LIST.
20767 Return the updated list. */
20768
20769 static Lisp_Object
20770 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20771 {
20772 register Lisp_Object tail, prev;
20773 register Lisp_Object tem;
20774
20775 tail = list;
20776 prev = Qnil;
20777 while (CONSP (tail))
20778 {
20779 tem = XCAR (tail);
20780
20781 if (EQ (elt, tem))
20782 {
20783 /* Splice out the link TAIL. */
20784 if (NILP (prev))
20785 list = XCDR (tail);
20786 else
20787 Fsetcdr (prev, XCDR (tail));
20788
20789 /* Now make it the first. */
20790 Fsetcdr (tail, list);
20791 return tail;
20792 }
20793 else
20794 prev = tail;
20795 tail = XCDR (tail);
20796 QUIT;
20797 }
20798
20799 /* Not found--return unchanged LIST. */
20800 return list;
20801 }
20802
20803 /* Contribute ELT to the mode line for window IT->w. How it
20804 translates into text depends on its data type.
20805
20806 IT describes the display environment in which we display, as usual.
20807
20808 DEPTH is the depth in recursion. It is used to prevent
20809 infinite recursion here.
20810
20811 FIELD_WIDTH is the number of characters the display of ELT should
20812 occupy in the mode line, and PRECISION is the maximum number of
20813 characters to display from ELT's representation. See
20814 display_string for details.
20815
20816 Returns the hpos of the end of the text generated by ELT.
20817
20818 PROPS is a property list to add to any string we encounter.
20819
20820 If RISKY is nonzero, remove (disregard) any properties in any string
20821 we encounter, and ignore :eval and :propertize.
20822
20823 The global variable `mode_line_target' determines whether the
20824 output is passed to `store_mode_line_noprop',
20825 `store_mode_line_string', or `display_string'. */
20826
20827 static int
20828 display_mode_element (struct it *it, int depth, int field_width, int precision,
20829 Lisp_Object elt, Lisp_Object props, int risky)
20830 {
20831 int n = 0, field, prec;
20832 int literal = 0;
20833
20834 tail_recurse:
20835 if (depth > 100)
20836 elt = build_string ("*too-deep*");
20837
20838 depth++;
20839
20840 switch (XTYPE (elt))
20841 {
20842 case Lisp_String:
20843 {
20844 /* A string: output it and check for %-constructs within it. */
20845 unsigned char c;
20846 ptrdiff_t offset = 0;
20847
20848 if (SCHARS (elt) > 0
20849 && (!NILP (props) || risky))
20850 {
20851 Lisp_Object oprops, aelt;
20852 oprops = Ftext_properties_at (make_number (0), elt);
20853
20854 /* If the starting string's properties are not what
20855 we want, translate the string. Also, if the string
20856 is risky, do that anyway. */
20857
20858 if (NILP (Fequal (props, oprops)) || risky)
20859 {
20860 /* If the starting string has properties,
20861 merge the specified ones onto the existing ones. */
20862 if (! NILP (oprops) && !risky)
20863 {
20864 Lisp_Object tem;
20865
20866 oprops = Fcopy_sequence (oprops);
20867 tem = props;
20868 while (CONSP (tem))
20869 {
20870 oprops = Fplist_put (oprops, XCAR (tem),
20871 XCAR (XCDR (tem)));
20872 tem = XCDR (XCDR (tem));
20873 }
20874 props = oprops;
20875 }
20876
20877 aelt = Fassoc (elt, mode_line_proptrans_alist);
20878 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20879 {
20880 /* AELT is what we want. Move it to the front
20881 without consing. */
20882 elt = XCAR (aelt);
20883 mode_line_proptrans_alist
20884 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20885 }
20886 else
20887 {
20888 Lisp_Object tem;
20889
20890 /* If AELT has the wrong props, it is useless.
20891 so get rid of it. */
20892 if (! NILP (aelt))
20893 mode_line_proptrans_alist
20894 = Fdelq (aelt, mode_line_proptrans_alist);
20895
20896 elt = Fcopy_sequence (elt);
20897 Fset_text_properties (make_number (0), Flength (elt),
20898 props, elt);
20899 /* Add this item to mode_line_proptrans_alist. */
20900 mode_line_proptrans_alist
20901 = Fcons (Fcons (elt, props),
20902 mode_line_proptrans_alist);
20903 /* Truncate mode_line_proptrans_alist
20904 to at most 50 elements. */
20905 tem = Fnthcdr (make_number (50),
20906 mode_line_proptrans_alist);
20907 if (! NILP (tem))
20908 XSETCDR (tem, Qnil);
20909 }
20910 }
20911 }
20912
20913 offset = 0;
20914
20915 if (literal)
20916 {
20917 prec = precision - n;
20918 switch (mode_line_target)
20919 {
20920 case MODE_LINE_NOPROP:
20921 case MODE_LINE_TITLE:
20922 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20923 break;
20924 case MODE_LINE_STRING:
20925 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20926 break;
20927 case MODE_LINE_DISPLAY:
20928 n += display_string (NULL, elt, Qnil, 0, 0, it,
20929 0, prec, 0, STRING_MULTIBYTE (elt));
20930 break;
20931 }
20932
20933 break;
20934 }
20935
20936 /* Handle the non-literal case. */
20937
20938 while ((precision <= 0 || n < precision)
20939 && SREF (elt, offset) != 0
20940 && (mode_line_target != MODE_LINE_DISPLAY
20941 || it->current_x < it->last_visible_x))
20942 {
20943 ptrdiff_t last_offset = offset;
20944
20945 /* Advance to end of string or next format specifier. */
20946 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20947 ;
20948
20949 if (offset - 1 != last_offset)
20950 {
20951 ptrdiff_t nchars, nbytes;
20952
20953 /* Output to end of string or up to '%'. Field width
20954 is length of string. Don't output more than
20955 PRECISION allows us. */
20956 offset--;
20957
20958 prec = c_string_width (SDATA (elt) + last_offset,
20959 offset - last_offset, precision - n,
20960 &nchars, &nbytes);
20961
20962 switch (mode_line_target)
20963 {
20964 case MODE_LINE_NOPROP:
20965 case MODE_LINE_TITLE:
20966 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20967 break;
20968 case MODE_LINE_STRING:
20969 {
20970 ptrdiff_t bytepos = last_offset;
20971 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20972 ptrdiff_t endpos = (precision <= 0
20973 ? string_byte_to_char (elt, offset)
20974 : charpos + nchars);
20975
20976 n += store_mode_line_string (NULL,
20977 Fsubstring (elt, make_number (charpos),
20978 make_number (endpos)),
20979 0, 0, 0, Qnil);
20980 }
20981 break;
20982 case MODE_LINE_DISPLAY:
20983 {
20984 ptrdiff_t bytepos = last_offset;
20985 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20986
20987 if (precision <= 0)
20988 nchars = string_byte_to_char (elt, offset) - charpos;
20989 n += display_string (NULL, elt, Qnil, 0, charpos,
20990 it, 0, nchars, 0,
20991 STRING_MULTIBYTE (elt));
20992 }
20993 break;
20994 }
20995 }
20996 else /* c == '%' */
20997 {
20998 ptrdiff_t percent_position = offset;
20999
21000 /* Get the specified minimum width. Zero means
21001 don't pad. */
21002 field = 0;
21003 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
21004 field = field * 10 + c - '0';
21005
21006 /* Don't pad beyond the total padding allowed. */
21007 if (field_width - n > 0 && field > field_width - n)
21008 field = field_width - n;
21009
21010 /* Note that either PRECISION <= 0 or N < PRECISION. */
21011 prec = precision - n;
21012
21013 if (c == 'M')
21014 n += display_mode_element (it, depth, field, prec,
21015 Vglobal_mode_string, props,
21016 risky);
21017 else if (c != 0)
21018 {
21019 bool multibyte;
21020 ptrdiff_t bytepos, charpos;
21021 const char *spec;
21022 Lisp_Object string;
21023
21024 bytepos = percent_position;
21025 charpos = (STRING_MULTIBYTE (elt)
21026 ? string_byte_to_char (elt, bytepos)
21027 : bytepos);
21028 spec = decode_mode_spec (it->w, c, field, &string);
21029 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
21030
21031 switch (mode_line_target)
21032 {
21033 case MODE_LINE_NOPROP:
21034 case MODE_LINE_TITLE:
21035 n += store_mode_line_noprop (spec, field, prec);
21036 break;
21037 case MODE_LINE_STRING:
21038 {
21039 Lisp_Object tem = build_string (spec);
21040 props = Ftext_properties_at (make_number (charpos), elt);
21041 /* Should only keep face property in props */
21042 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
21043 }
21044 break;
21045 case MODE_LINE_DISPLAY:
21046 {
21047 int nglyphs_before, nwritten;
21048
21049 nglyphs_before = it->glyph_row->used[TEXT_AREA];
21050 nwritten = display_string (spec, string, elt,
21051 charpos, 0, it,
21052 field, prec, 0,
21053 multibyte);
21054
21055 /* Assign to the glyphs written above the
21056 string where the `%x' came from, position
21057 of the `%'. */
21058 if (nwritten > 0)
21059 {
21060 struct glyph *glyph
21061 = (it->glyph_row->glyphs[TEXT_AREA]
21062 + nglyphs_before);
21063 int i;
21064
21065 for (i = 0; i < nwritten; ++i)
21066 {
21067 glyph[i].object = elt;
21068 glyph[i].charpos = charpos;
21069 }
21070
21071 n += nwritten;
21072 }
21073 }
21074 break;
21075 }
21076 }
21077 else /* c == 0 */
21078 break;
21079 }
21080 }
21081 }
21082 break;
21083
21084 case Lisp_Symbol:
21085 /* A symbol: process the value of the symbol recursively
21086 as if it appeared here directly. Avoid error if symbol void.
21087 Special case: if value of symbol is a string, output the string
21088 literally. */
21089 {
21090 register Lisp_Object tem;
21091
21092 /* If the variable is not marked as risky to set
21093 then its contents are risky to use. */
21094 if (NILP (Fget (elt, Qrisky_local_variable)))
21095 risky = 1;
21096
21097 tem = Fboundp (elt);
21098 if (!NILP (tem))
21099 {
21100 tem = Fsymbol_value (elt);
21101 /* If value is a string, output that string literally:
21102 don't check for % within it. */
21103 if (STRINGP (tem))
21104 literal = 1;
21105
21106 if (!EQ (tem, elt))
21107 {
21108 /* Give up right away for nil or t. */
21109 elt = tem;
21110 goto tail_recurse;
21111 }
21112 }
21113 }
21114 break;
21115
21116 case Lisp_Cons:
21117 {
21118 register Lisp_Object car, tem;
21119
21120 /* A cons cell: five distinct cases.
21121 If first element is :eval or :propertize, do something special.
21122 If first element is a string or a cons, process all the elements
21123 and effectively concatenate them.
21124 If first element is a negative number, truncate displaying cdr to
21125 at most that many characters. If positive, pad (with spaces)
21126 to at least that many characters.
21127 If first element is a symbol, process the cadr or caddr recursively
21128 according to whether the symbol's value is non-nil or nil. */
21129 car = XCAR (elt);
21130 if (EQ (car, QCeval))
21131 {
21132 /* An element of the form (:eval FORM) means evaluate FORM
21133 and use the result as mode line elements. */
21134
21135 if (risky)
21136 break;
21137
21138 if (CONSP (XCDR (elt)))
21139 {
21140 Lisp_Object spec;
21141 spec = safe_eval (XCAR (XCDR (elt)));
21142 n += display_mode_element (it, depth, field_width - n,
21143 precision - n, spec, props,
21144 risky);
21145 }
21146 }
21147 else if (EQ (car, QCpropertize))
21148 {
21149 /* An element of the form (:propertize ELT PROPS...)
21150 means display ELT but applying properties PROPS. */
21151
21152 if (risky)
21153 break;
21154
21155 if (CONSP (XCDR (elt)))
21156 n += display_mode_element (it, depth, field_width - n,
21157 precision - n, XCAR (XCDR (elt)),
21158 XCDR (XCDR (elt)), risky);
21159 }
21160 else if (SYMBOLP (car))
21161 {
21162 tem = Fboundp (car);
21163 elt = XCDR (elt);
21164 if (!CONSP (elt))
21165 goto invalid;
21166 /* elt is now the cdr, and we know it is a cons cell.
21167 Use its car if CAR has a non-nil value. */
21168 if (!NILP (tem))
21169 {
21170 tem = Fsymbol_value (car);
21171 if (!NILP (tem))
21172 {
21173 elt = XCAR (elt);
21174 goto tail_recurse;
21175 }
21176 }
21177 /* Symbol's value is nil (or symbol is unbound)
21178 Get the cddr of the original list
21179 and if possible find the caddr and use that. */
21180 elt = XCDR (elt);
21181 if (NILP (elt))
21182 break;
21183 else if (!CONSP (elt))
21184 goto invalid;
21185 elt = XCAR (elt);
21186 goto tail_recurse;
21187 }
21188 else if (INTEGERP (car))
21189 {
21190 register int lim = XINT (car);
21191 elt = XCDR (elt);
21192 if (lim < 0)
21193 {
21194 /* Negative int means reduce maximum width. */
21195 if (precision <= 0)
21196 precision = -lim;
21197 else
21198 precision = min (precision, -lim);
21199 }
21200 else if (lim > 0)
21201 {
21202 /* Padding specified. Don't let it be more than
21203 current maximum. */
21204 if (precision > 0)
21205 lim = min (precision, lim);
21206
21207 /* If that's more padding than already wanted, queue it.
21208 But don't reduce padding already specified even if
21209 that is beyond the current truncation point. */
21210 field_width = max (lim, field_width);
21211 }
21212 goto tail_recurse;
21213 }
21214 else if (STRINGP (car) || CONSP (car))
21215 {
21216 Lisp_Object halftail = elt;
21217 int len = 0;
21218
21219 while (CONSP (elt)
21220 && (precision <= 0 || n < precision))
21221 {
21222 n += display_mode_element (it, depth,
21223 /* Do padding only after the last
21224 element in the list. */
21225 (! CONSP (XCDR (elt))
21226 ? field_width - n
21227 : 0),
21228 precision - n, XCAR (elt),
21229 props, risky);
21230 elt = XCDR (elt);
21231 len++;
21232 if ((len & 1) == 0)
21233 halftail = XCDR (halftail);
21234 /* Check for cycle. */
21235 if (EQ (halftail, elt))
21236 break;
21237 }
21238 }
21239 }
21240 break;
21241
21242 default:
21243 invalid:
21244 elt = build_string ("*invalid*");
21245 goto tail_recurse;
21246 }
21247
21248 /* Pad to FIELD_WIDTH. */
21249 if (field_width > 0 && n < field_width)
21250 {
21251 switch (mode_line_target)
21252 {
21253 case MODE_LINE_NOPROP:
21254 case MODE_LINE_TITLE:
21255 n += store_mode_line_noprop ("", field_width - n, 0);
21256 break;
21257 case MODE_LINE_STRING:
21258 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
21259 break;
21260 case MODE_LINE_DISPLAY:
21261 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
21262 0, 0, 0);
21263 break;
21264 }
21265 }
21266
21267 return n;
21268 }
21269
21270 /* Store a mode-line string element in mode_line_string_list.
21271
21272 If STRING is non-null, display that C string. Otherwise, the Lisp
21273 string LISP_STRING is displayed.
21274
21275 FIELD_WIDTH is the minimum number of output glyphs to produce.
21276 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21277 with spaces. FIELD_WIDTH <= 0 means don't pad.
21278
21279 PRECISION is the maximum number of characters to output from
21280 STRING. PRECISION <= 0 means don't truncate the string.
21281
21282 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
21283 properties to the string.
21284
21285 PROPS are the properties to add to the string.
21286 The mode_line_string_face face property is always added to the string.
21287 */
21288
21289 static int
21290 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
21291 int field_width, int precision, Lisp_Object props)
21292 {
21293 ptrdiff_t len;
21294 int n = 0;
21295
21296 if (string != NULL)
21297 {
21298 len = strlen (string);
21299 if (precision > 0 && len > precision)
21300 len = precision;
21301 lisp_string = make_string (string, len);
21302 if (NILP (props))
21303 props = mode_line_string_face_prop;
21304 else if (!NILP (mode_line_string_face))
21305 {
21306 Lisp_Object face = Fplist_get (props, Qface);
21307 props = Fcopy_sequence (props);
21308 if (NILP (face))
21309 face = mode_line_string_face;
21310 else
21311 face = list2 (face, mode_line_string_face);
21312 props = Fplist_put (props, Qface, face);
21313 }
21314 Fadd_text_properties (make_number (0), make_number (len),
21315 props, lisp_string);
21316 }
21317 else
21318 {
21319 len = XFASTINT (Flength (lisp_string));
21320 if (precision > 0 && len > precision)
21321 {
21322 len = precision;
21323 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
21324 precision = -1;
21325 }
21326 if (!NILP (mode_line_string_face))
21327 {
21328 Lisp_Object face;
21329 if (NILP (props))
21330 props = Ftext_properties_at (make_number (0), lisp_string);
21331 face = Fplist_get (props, Qface);
21332 if (NILP (face))
21333 face = mode_line_string_face;
21334 else
21335 face = list2 (face, mode_line_string_face);
21336 props = list2 (Qface, face);
21337 if (copy_string)
21338 lisp_string = Fcopy_sequence (lisp_string);
21339 }
21340 if (!NILP (props))
21341 Fadd_text_properties (make_number (0), make_number (len),
21342 props, lisp_string);
21343 }
21344
21345 if (len > 0)
21346 {
21347 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21348 n += len;
21349 }
21350
21351 if (field_width > len)
21352 {
21353 field_width -= len;
21354 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
21355 if (!NILP (props))
21356 Fadd_text_properties (make_number (0), make_number (field_width),
21357 props, lisp_string);
21358 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21359 n += field_width;
21360 }
21361
21362 return n;
21363 }
21364
21365
21366 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
21367 1, 4, 0,
21368 doc: /* Format a string out of a mode line format specification.
21369 First arg FORMAT specifies the mode line format (see `mode-line-format'
21370 for details) to use.
21371
21372 By default, the format is evaluated for the currently selected window.
21373
21374 Optional second arg FACE specifies the face property to put on all
21375 characters for which no face is specified. The value nil means the
21376 default face. The value t means whatever face the window's mode line
21377 currently uses (either `mode-line' or `mode-line-inactive',
21378 depending on whether the window is the selected window or not).
21379 An integer value means the value string has no text
21380 properties.
21381
21382 Optional third and fourth args WINDOW and BUFFER specify the window
21383 and buffer to use as the context for the formatting (defaults
21384 are the selected window and the WINDOW's buffer). */)
21385 (Lisp_Object format, Lisp_Object face,
21386 Lisp_Object window, Lisp_Object buffer)
21387 {
21388 struct it it;
21389 int len;
21390 struct window *w;
21391 struct buffer *old_buffer = NULL;
21392 int face_id;
21393 int no_props = INTEGERP (face);
21394 ptrdiff_t count = SPECPDL_INDEX ();
21395 Lisp_Object str;
21396 int string_start = 0;
21397
21398 w = decode_any_window (window);
21399 XSETWINDOW (window, w);
21400
21401 if (NILP (buffer))
21402 buffer = w->contents;
21403 CHECK_BUFFER (buffer);
21404
21405 /* Make formatting the modeline a non-op when noninteractive, otherwise
21406 there will be problems later caused by a partially initialized frame. */
21407 if (NILP (format) || noninteractive)
21408 return empty_unibyte_string;
21409
21410 if (no_props)
21411 face = Qnil;
21412
21413 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
21414 : EQ (face, Qt) ? (EQ (window, selected_window)
21415 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21416 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21417 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21418 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21419 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21420 : DEFAULT_FACE_ID;
21421
21422 old_buffer = current_buffer;
21423
21424 /* Save things including mode_line_proptrans_alist,
21425 and set that to nil so that we don't alter the outer value. */
21426 record_unwind_protect (unwind_format_mode_line,
21427 format_mode_line_unwind_data
21428 (XFRAME (WINDOW_FRAME (w)),
21429 old_buffer, selected_window, 1));
21430 mode_line_proptrans_alist = Qnil;
21431
21432 Fselect_window (window, Qt);
21433 set_buffer_internal_1 (XBUFFER (buffer));
21434
21435 init_iterator (&it, w, -1, -1, NULL, face_id);
21436
21437 if (no_props)
21438 {
21439 mode_line_target = MODE_LINE_NOPROP;
21440 mode_line_string_face_prop = Qnil;
21441 mode_line_string_list = Qnil;
21442 string_start = MODE_LINE_NOPROP_LEN (0);
21443 }
21444 else
21445 {
21446 mode_line_target = MODE_LINE_STRING;
21447 mode_line_string_list = Qnil;
21448 mode_line_string_face = face;
21449 mode_line_string_face_prop
21450 = NILP (face) ? Qnil : list2 (Qface, face);
21451 }
21452
21453 push_kboard (FRAME_KBOARD (it.f));
21454 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21455 pop_kboard ();
21456
21457 if (no_props)
21458 {
21459 len = MODE_LINE_NOPROP_LEN (string_start);
21460 str = make_string (mode_line_noprop_buf + string_start, len);
21461 }
21462 else
21463 {
21464 mode_line_string_list = Fnreverse (mode_line_string_list);
21465 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21466 empty_unibyte_string);
21467 }
21468
21469 unbind_to (count, Qnil);
21470 return str;
21471 }
21472
21473 /* Write a null-terminated, right justified decimal representation of
21474 the positive integer D to BUF using a minimal field width WIDTH. */
21475
21476 static void
21477 pint2str (register char *buf, register int width, register ptrdiff_t d)
21478 {
21479 register char *p = buf;
21480
21481 if (d <= 0)
21482 *p++ = '0';
21483 else
21484 {
21485 while (d > 0)
21486 {
21487 *p++ = d % 10 + '0';
21488 d /= 10;
21489 }
21490 }
21491
21492 for (width -= (int) (p - buf); width > 0; --width)
21493 *p++ = ' ';
21494 *p-- = '\0';
21495 while (p > buf)
21496 {
21497 d = *buf;
21498 *buf++ = *p;
21499 *p-- = d;
21500 }
21501 }
21502
21503 /* Write a null-terminated, right justified decimal and "human
21504 readable" representation of the nonnegative integer D to BUF using
21505 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21506
21507 static const char power_letter[] =
21508 {
21509 0, /* no letter */
21510 'k', /* kilo */
21511 'M', /* mega */
21512 'G', /* giga */
21513 'T', /* tera */
21514 'P', /* peta */
21515 'E', /* exa */
21516 'Z', /* zetta */
21517 'Y' /* yotta */
21518 };
21519
21520 static void
21521 pint2hrstr (char *buf, int width, ptrdiff_t d)
21522 {
21523 /* We aim to represent the nonnegative integer D as
21524 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21525 ptrdiff_t quotient = d;
21526 int remainder = 0;
21527 /* -1 means: do not use TENTHS. */
21528 int tenths = -1;
21529 int exponent = 0;
21530
21531 /* Length of QUOTIENT.TENTHS as a string. */
21532 int length;
21533
21534 char * psuffix;
21535 char * p;
21536
21537 if (quotient >= 1000)
21538 {
21539 /* Scale to the appropriate EXPONENT. */
21540 do
21541 {
21542 remainder = quotient % 1000;
21543 quotient /= 1000;
21544 exponent++;
21545 }
21546 while (quotient >= 1000);
21547
21548 /* Round to nearest and decide whether to use TENTHS or not. */
21549 if (quotient <= 9)
21550 {
21551 tenths = remainder / 100;
21552 if (remainder % 100 >= 50)
21553 {
21554 if (tenths < 9)
21555 tenths++;
21556 else
21557 {
21558 quotient++;
21559 if (quotient == 10)
21560 tenths = -1;
21561 else
21562 tenths = 0;
21563 }
21564 }
21565 }
21566 else
21567 if (remainder >= 500)
21568 {
21569 if (quotient < 999)
21570 quotient++;
21571 else
21572 {
21573 quotient = 1;
21574 exponent++;
21575 tenths = 0;
21576 }
21577 }
21578 }
21579
21580 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21581 if (tenths == -1 && quotient <= 99)
21582 if (quotient <= 9)
21583 length = 1;
21584 else
21585 length = 2;
21586 else
21587 length = 3;
21588 p = psuffix = buf + max (width, length);
21589
21590 /* Print EXPONENT. */
21591 *psuffix++ = power_letter[exponent];
21592 *psuffix = '\0';
21593
21594 /* Print TENTHS. */
21595 if (tenths >= 0)
21596 {
21597 *--p = '0' + tenths;
21598 *--p = '.';
21599 }
21600
21601 /* Print QUOTIENT. */
21602 do
21603 {
21604 int digit = quotient % 10;
21605 *--p = '0' + digit;
21606 }
21607 while ((quotient /= 10) != 0);
21608
21609 /* Print leading spaces. */
21610 while (buf < p)
21611 *--p = ' ';
21612 }
21613
21614 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21615 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21616 type of CODING_SYSTEM. Return updated pointer into BUF. */
21617
21618 static unsigned char invalid_eol_type[] = "(*invalid*)";
21619
21620 static char *
21621 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21622 {
21623 Lisp_Object val;
21624 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21625 const unsigned char *eol_str;
21626 int eol_str_len;
21627 /* The EOL conversion we are using. */
21628 Lisp_Object eoltype;
21629
21630 val = CODING_SYSTEM_SPEC (coding_system);
21631 eoltype = Qnil;
21632
21633 if (!VECTORP (val)) /* Not yet decided. */
21634 {
21635 *buf++ = multibyte ? '-' : ' ';
21636 if (eol_flag)
21637 eoltype = eol_mnemonic_undecided;
21638 /* Don't mention EOL conversion if it isn't decided. */
21639 }
21640 else
21641 {
21642 Lisp_Object attrs;
21643 Lisp_Object eolvalue;
21644
21645 attrs = AREF (val, 0);
21646 eolvalue = AREF (val, 2);
21647
21648 *buf++ = multibyte
21649 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21650 : ' ';
21651
21652 if (eol_flag)
21653 {
21654 /* The EOL conversion that is normal on this system. */
21655
21656 if (NILP (eolvalue)) /* Not yet decided. */
21657 eoltype = eol_mnemonic_undecided;
21658 else if (VECTORP (eolvalue)) /* Not yet decided. */
21659 eoltype = eol_mnemonic_undecided;
21660 else /* eolvalue is Qunix, Qdos, or Qmac. */
21661 eoltype = (EQ (eolvalue, Qunix)
21662 ? eol_mnemonic_unix
21663 : (EQ (eolvalue, Qdos) == 1
21664 ? eol_mnemonic_dos : eol_mnemonic_mac));
21665 }
21666 }
21667
21668 if (eol_flag)
21669 {
21670 /* Mention the EOL conversion if it is not the usual one. */
21671 if (STRINGP (eoltype))
21672 {
21673 eol_str = SDATA (eoltype);
21674 eol_str_len = SBYTES (eoltype);
21675 }
21676 else if (CHARACTERP (eoltype))
21677 {
21678 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21679 int c = XFASTINT (eoltype);
21680 eol_str_len = CHAR_STRING (c, tmp);
21681 eol_str = tmp;
21682 }
21683 else
21684 {
21685 eol_str = invalid_eol_type;
21686 eol_str_len = sizeof (invalid_eol_type) - 1;
21687 }
21688 memcpy (buf, eol_str, eol_str_len);
21689 buf += eol_str_len;
21690 }
21691
21692 return buf;
21693 }
21694
21695 /* Return a string for the output of a mode line %-spec for window W,
21696 generated by character C. FIELD_WIDTH > 0 means pad the string
21697 returned with spaces to that value. Return a Lisp string in
21698 *STRING if the resulting string is taken from that Lisp string.
21699
21700 Note we operate on the current buffer for most purposes. */
21701
21702 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21703
21704 static const char *
21705 decode_mode_spec (struct window *w, register int c, int field_width,
21706 Lisp_Object *string)
21707 {
21708 Lisp_Object obj;
21709 struct frame *f = XFRAME (WINDOW_FRAME (w));
21710 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21711 /* We are going to use f->decode_mode_spec_buffer as the buffer to
21712 produce strings from numerical values, so limit preposterously
21713 large values of FIELD_WIDTH to avoid overrunning the buffer's
21714 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
21715 bytes plus the terminating null. */
21716 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
21717 struct buffer *b = current_buffer;
21718
21719 obj = Qnil;
21720 *string = Qnil;
21721
21722 switch (c)
21723 {
21724 case '*':
21725 if (!NILP (BVAR (b, read_only)))
21726 return "%";
21727 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21728 return "*";
21729 return "-";
21730
21731 case '+':
21732 /* This differs from %* only for a modified read-only buffer. */
21733 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21734 return "*";
21735 if (!NILP (BVAR (b, read_only)))
21736 return "%";
21737 return "-";
21738
21739 case '&':
21740 /* This differs from %* in ignoring read-only-ness. */
21741 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21742 return "*";
21743 return "-";
21744
21745 case '%':
21746 return "%";
21747
21748 case '[':
21749 {
21750 int i;
21751 char *p;
21752
21753 if (command_loop_level > 5)
21754 return "[[[... ";
21755 p = decode_mode_spec_buf;
21756 for (i = 0; i < command_loop_level; i++)
21757 *p++ = '[';
21758 *p = 0;
21759 return decode_mode_spec_buf;
21760 }
21761
21762 case ']':
21763 {
21764 int i;
21765 char *p;
21766
21767 if (command_loop_level > 5)
21768 return " ...]]]";
21769 p = decode_mode_spec_buf;
21770 for (i = 0; i < command_loop_level; i++)
21771 *p++ = ']';
21772 *p = 0;
21773 return decode_mode_spec_buf;
21774 }
21775
21776 case '-':
21777 {
21778 register int i;
21779
21780 /* Let lots_of_dashes be a string of infinite length. */
21781 if (mode_line_target == MODE_LINE_NOPROP
21782 || mode_line_target == MODE_LINE_STRING)
21783 return "--";
21784 if (field_width <= 0
21785 || field_width > sizeof (lots_of_dashes))
21786 {
21787 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21788 decode_mode_spec_buf[i] = '-';
21789 decode_mode_spec_buf[i] = '\0';
21790 return decode_mode_spec_buf;
21791 }
21792 else
21793 return lots_of_dashes;
21794 }
21795
21796 case 'b':
21797 obj = BVAR (b, name);
21798 break;
21799
21800 case 'c':
21801 /* %c and %l are ignored in `frame-title-format'.
21802 (In redisplay_internal, the frame title is drawn _before_ the
21803 windows are updated, so the stuff which depends on actual
21804 window contents (such as %l) may fail to render properly, or
21805 even crash emacs.) */
21806 if (mode_line_target == MODE_LINE_TITLE)
21807 return "";
21808 else
21809 {
21810 ptrdiff_t col = current_column ();
21811 w->column_number_displayed = col;
21812 pint2str (decode_mode_spec_buf, width, col);
21813 return decode_mode_spec_buf;
21814 }
21815
21816 case 'e':
21817 #ifndef SYSTEM_MALLOC
21818 {
21819 if (NILP (Vmemory_full))
21820 return "";
21821 else
21822 return "!MEM FULL! ";
21823 }
21824 #else
21825 return "";
21826 #endif
21827
21828 case 'F':
21829 /* %F displays the frame name. */
21830 if (!NILP (f->title))
21831 return SSDATA (f->title);
21832 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21833 return SSDATA (f->name);
21834 return "Emacs";
21835
21836 case 'f':
21837 obj = BVAR (b, filename);
21838 break;
21839
21840 case 'i':
21841 {
21842 ptrdiff_t size = ZV - BEGV;
21843 pint2str (decode_mode_spec_buf, width, size);
21844 return decode_mode_spec_buf;
21845 }
21846
21847 case 'I':
21848 {
21849 ptrdiff_t size = ZV - BEGV;
21850 pint2hrstr (decode_mode_spec_buf, width, size);
21851 return decode_mode_spec_buf;
21852 }
21853
21854 case 'l':
21855 {
21856 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21857 ptrdiff_t topline, nlines, height;
21858 ptrdiff_t junk;
21859
21860 /* %c and %l are ignored in `frame-title-format'. */
21861 if (mode_line_target == MODE_LINE_TITLE)
21862 return "";
21863
21864 startpos = marker_position (w->start);
21865 startpos_byte = marker_byte_position (w->start);
21866 height = WINDOW_TOTAL_LINES (w);
21867
21868 /* If we decided that this buffer isn't suitable for line numbers,
21869 don't forget that too fast. */
21870 if (w->base_line_pos == -1)
21871 goto no_value;
21872
21873 /* If the buffer is very big, don't waste time. */
21874 if (INTEGERP (Vline_number_display_limit)
21875 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21876 {
21877 w->base_line_pos = 0;
21878 w->base_line_number = 0;
21879 goto no_value;
21880 }
21881
21882 if (w->base_line_number > 0
21883 && w->base_line_pos > 0
21884 && w->base_line_pos <= startpos)
21885 {
21886 line = w->base_line_number;
21887 linepos = w->base_line_pos;
21888 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21889 }
21890 else
21891 {
21892 line = 1;
21893 linepos = BUF_BEGV (b);
21894 linepos_byte = BUF_BEGV_BYTE (b);
21895 }
21896
21897 /* Count lines from base line to window start position. */
21898 nlines = display_count_lines (linepos_byte,
21899 startpos_byte,
21900 startpos, &junk);
21901
21902 topline = nlines + line;
21903
21904 /* Determine a new base line, if the old one is too close
21905 or too far away, or if we did not have one.
21906 "Too close" means it's plausible a scroll-down would
21907 go back past it. */
21908 if (startpos == BUF_BEGV (b))
21909 {
21910 w->base_line_number = topline;
21911 w->base_line_pos = BUF_BEGV (b);
21912 }
21913 else if (nlines < height + 25 || nlines > height * 3 + 50
21914 || linepos == BUF_BEGV (b))
21915 {
21916 ptrdiff_t limit = BUF_BEGV (b);
21917 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21918 ptrdiff_t position;
21919 ptrdiff_t distance =
21920 (height * 2 + 30) * line_number_display_limit_width;
21921
21922 if (startpos - distance > limit)
21923 {
21924 limit = startpos - distance;
21925 limit_byte = CHAR_TO_BYTE (limit);
21926 }
21927
21928 nlines = display_count_lines (startpos_byte,
21929 limit_byte,
21930 - (height * 2 + 30),
21931 &position);
21932 /* If we couldn't find the lines we wanted within
21933 line_number_display_limit_width chars per line,
21934 give up on line numbers for this window. */
21935 if (position == limit_byte && limit == startpos - distance)
21936 {
21937 w->base_line_pos = -1;
21938 w->base_line_number = 0;
21939 goto no_value;
21940 }
21941
21942 w->base_line_number = topline - nlines;
21943 w->base_line_pos = BYTE_TO_CHAR (position);
21944 }
21945
21946 /* Now count lines from the start pos to point. */
21947 nlines = display_count_lines (startpos_byte,
21948 PT_BYTE, PT, &junk);
21949
21950 /* Record that we did display the line number. */
21951 line_number_displayed = 1;
21952
21953 /* Make the string to show. */
21954 pint2str (decode_mode_spec_buf, width, topline + nlines);
21955 return decode_mode_spec_buf;
21956 no_value:
21957 {
21958 char* p = decode_mode_spec_buf;
21959 int pad = width - 2;
21960 while (pad-- > 0)
21961 *p++ = ' ';
21962 *p++ = '?';
21963 *p++ = '?';
21964 *p = '\0';
21965 return decode_mode_spec_buf;
21966 }
21967 }
21968 break;
21969
21970 case 'm':
21971 obj = BVAR (b, mode_name);
21972 break;
21973
21974 case 'n':
21975 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21976 return " Narrow";
21977 break;
21978
21979 case 'p':
21980 {
21981 ptrdiff_t pos = marker_position (w->start);
21982 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21983
21984 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
21985 {
21986 if (pos <= BUF_BEGV (b))
21987 return "All";
21988 else
21989 return "Bottom";
21990 }
21991 else if (pos <= BUF_BEGV (b))
21992 return "Top";
21993 else
21994 {
21995 if (total > 1000000)
21996 /* Do it differently for a large value, to avoid overflow. */
21997 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21998 else
21999 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
22000 /* We can't normally display a 3-digit number,
22001 so get us a 2-digit number that is close. */
22002 if (total == 100)
22003 total = 99;
22004 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22005 return decode_mode_spec_buf;
22006 }
22007 }
22008
22009 /* Display percentage of size above the bottom of the screen. */
22010 case 'P':
22011 {
22012 ptrdiff_t toppos = marker_position (w->start);
22013 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
22014 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22015
22016 if (botpos >= BUF_ZV (b))
22017 {
22018 if (toppos <= BUF_BEGV (b))
22019 return "All";
22020 else
22021 return "Bottom";
22022 }
22023 else
22024 {
22025 if (total > 1000000)
22026 /* Do it differently for a large value, to avoid overflow. */
22027 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22028 else
22029 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
22030 /* We can't normally display a 3-digit number,
22031 so get us a 2-digit number that is close. */
22032 if (total == 100)
22033 total = 99;
22034 if (toppos <= BUF_BEGV (b))
22035 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
22036 else
22037 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22038 return decode_mode_spec_buf;
22039 }
22040 }
22041
22042 case 's':
22043 /* status of process */
22044 obj = Fget_buffer_process (Fcurrent_buffer ());
22045 if (NILP (obj))
22046 return "no process";
22047 #ifndef MSDOS
22048 obj = Fsymbol_name (Fprocess_status (obj));
22049 #endif
22050 break;
22051
22052 case '@':
22053 {
22054 ptrdiff_t count = inhibit_garbage_collection ();
22055 Lisp_Object val = call1 (intern ("file-remote-p"),
22056 BVAR (current_buffer, directory));
22057 unbind_to (count, Qnil);
22058
22059 if (NILP (val))
22060 return "-";
22061 else
22062 return "@";
22063 }
22064
22065 case 'z':
22066 /* coding-system (not including end-of-line format) */
22067 case 'Z':
22068 /* coding-system (including end-of-line type) */
22069 {
22070 int eol_flag = (c == 'Z');
22071 char *p = decode_mode_spec_buf;
22072
22073 if (! FRAME_WINDOW_P (f))
22074 {
22075 /* No need to mention EOL here--the terminal never needs
22076 to do EOL conversion. */
22077 p = decode_mode_spec_coding (CODING_ID_NAME
22078 (FRAME_KEYBOARD_CODING (f)->id),
22079 p, 0);
22080 p = decode_mode_spec_coding (CODING_ID_NAME
22081 (FRAME_TERMINAL_CODING (f)->id),
22082 p, 0);
22083 }
22084 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
22085 p, eol_flag);
22086
22087 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
22088 #ifdef subprocesses
22089 obj = Fget_buffer_process (Fcurrent_buffer ());
22090 if (PROCESSP (obj))
22091 {
22092 p = decode_mode_spec_coding
22093 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
22094 p = decode_mode_spec_coding
22095 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
22096 }
22097 #endif /* subprocesses */
22098 #endif /* 0 */
22099 *p = 0;
22100 return decode_mode_spec_buf;
22101 }
22102 }
22103
22104 if (STRINGP (obj))
22105 {
22106 *string = obj;
22107 return SSDATA (obj);
22108 }
22109 else
22110 return "";
22111 }
22112
22113
22114 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
22115 means count lines back from START_BYTE. But don't go beyond
22116 LIMIT_BYTE. Return the number of lines thus found (always
22117 nonnegative).
22118
22119 Set *BYTE_POS_PTR to the byte position where we stopped. This is
22120 either the position COUNT lines after/before START_BYTE, if we
22121 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
22122 COUNT lines. */
22123
22124 static ptrdiff_t
22125 display_count_lines (ptrdiff_t start_byte,
22126 ptrdiff_t limit_byte, ptrdiff_t count,
22127 ptrdiff_t *byte_pos_ptr)
22128 {
22129 register unsigned char *cursor;
22130 unsigned char *base;
22131
22132 register ptrdiff_t ceiling;
22133 register unsigned char *ceiling_addr;
22134 ptrdiff_t orig_count = count;
22135
22136 /* If we are not in selective display mode,
22137 check only for newlines. */
22138 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
22139 && !INTEGERP (BVAR (current_buffer, selective_display)));
22140
22141 if (count > 0)
22142 {
22143 while (start_byte < limit_byte)
22144 {
22145 ceiling = BUFFER_CEILING_OF (start_byte);
22146 ceiling = min (limit_byte - 1, ceiling);
22147 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
22148 base = (cursor = BYTE_POS_ADDR (start_byte));
22149
22150 do
22151 {
22152 if (selective_display)
22153 {
22154 while (*cursor != '\n' && *cursor != 015
22155 && ++cursor != ceiling_addr)
22156 continue;
22157 if (cursor == ceiling_addr)
22158 break;
22159 }
22160 else
22161 {
22162 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
22163 if (! cursor)
22164 break;
22165 }
22166
22167 cursor++;
22168
22169 if (--count == 0)
22170 {
22171 start_byte += cursor - base;
22172 *byte_pos_ptr = start_byte;
22173 return orig_count;
22174 }
22175 }
22176 while (cursor < ceiling_addr);
22177
22178 start_byte += ceiling_addr - base;
22179 }
22180 }
22181 else
22182 {
22183 while (start_byte > limit_byte)
22184 {
22185 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
22186 ceiling = max (limit_byte, ceiling);
22187 ceiling_addr = BYTE_POS_ADDR (ceiling);
22188 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
22189 while (1)
22190 {
22191 if (selective_display)
22192 {
22193 while (--cursor >= ceiling_addr
22194 && *cursor != '\n' && *cursor != 015)
22195 continue;
22196 if (cursor < ceiling_addr)
22197 break;
22198 }
22199 else
22200 {
22201 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
22202 if (! cursor)
22203 break;
22204 }
22205
22206 if (++count == 0)
22207 {
22208 start_byte += cursor - base + 1;
22209 *byte_pos_ptr = start_byte;
22210 /* When scanning backwards, we should
22211 not count the newline posterior to which we stop. */
22212 return - orig_count - 1;
22213 }
22214 }
22215 start_byte += ceiling_addr - base;
22216 }
22217 }
22218
22219 *byte_pos_ptr = limit_byte;
22220
22221 if (count < 0)
22222 return - orig_count + count;
22223 return orig_count - count;
22224
22225 }
22226
22227
22228 \f
22229 /***********************************************************************
22230 Displaying strings
22231 ***********************************************************************/
22232
22233 /* Display a NUL-terminated string, starting with index START.
22234
22235 If STRING is non-null, display that C string. Otherwise, the Lisp
22236 string LISP_STRING is displayed. There's a case that STRING is
22237 non-null and LISP_STRING is not nil. It means STRING is a string
22238 data of LISP_STRING. In that case, we display LISP_STRING while
22239 ignoring its text properties.
22240
22241 If FACE_STRING is not nil, FACE_STRING_POS is a position in
22242 FACE_STRING. Display STRING or LISP_STRING with the face at
22243 FACE_STRING_POS in FACE_STRING:
22244
22245 Display the string in the environment given by IT, but use the
22246 standard display table, temporarily.
22247
22248 FIELD_WIDTH is the minimum number of output glyphs to produce.
22249 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22250 with spaces. If STRING has more characters, more than FIELD_WIDTH
22251 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
22252
22253 PRECISION is the maximum number of characters to output from
22254 STRING. PRECISION < 0 means don't truncate the string.
22255
22256 This is roughly equivalent to printf format specifiers:
22257
22258 FIELD_WIDTH PRECISION PRINTF
22259 ----------------------------------------
22260 -1 -1 %s
22261 -1 10 %.10s
22262 10 -1 %10s
22263 20 10 %20.10s
22264
22265 MULTIBYTE zero means do not display multibyte chars, > 0 means do
22266 display them, and < 0 means obey the current buffer's value of
22267 enable_multibyte_characters.
22268
22269 Value is the number of columns displayed. */
22270
22271 static int
22272 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
22273 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
22274 int field_width, int precision, int max_x, int multibyte)
22275 {
22276 int hpos_at_start = it->hpos;
22277 int saved_face_id = it->face_id;
22278 struct glyph_row *row = it->glyph_row;
22279 ptrdiff_t it_charpos;
22280
22281 /* Initialize the iterator IT for iteration over STRING beginning
22282 with index START. */
22283 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
22284 precision, field_width, multibyte);
22285 if (string && STRINGP (lisp_string))
22286 /* LISP_STRING is the one returned by decode_mode_spec. We should
22287 ignore its text properties. */
22288 it->stop_charpos = it->end_charpos;
22289
22290 /* If displaying STRING, set up the face of the iterator from
22291 FACE_STRING, if that's given. */
22292 if (STRINGP (face_string))
22293 {
22294 ptrdiff_t endptr;
22295 struct face *face;
22296
22297 it->face_id
22298 = face_at_string_position (it->w, face_string, face_string_pos,
22299 0, it->region_beg_charpos,
22300 it->region_end_charpos,
22301 &endptr, it->base_face_id, 0);
22302 face = FACE_FROM_ID (it->f, it->face_id);
22303 it->face_box_p = face->box != FACE_NO_BOX;
22304 }
22305
22306 /* Set max_x to the maximum allowed X position. Don't let it go
22307 beyond the right edge of the window. */
22308 if (max_x <= 0)
22309 max_x = it->last_visible_x;
22310 else
22311 max_x = min (max_x, it->last_visible_x);
22312
22313 /* Skip over display elements that are not visible. because IT->w is
22314 hscrolled. */
22315 if (it->current_x < it->first_visible_x)
22316 move_it_in_display_line_to (it, 100000, it->first_visible_x,
22317 MOVE_TO_POS | MOVE_TO_X);
22318
22319 row->ascent = it->max_ascent;
22320 row->height = it->max_ascent + it->max_descent;
22321 row->phys_ascent = it->max_phys_ascent;
22322 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
22323 row->extra_line_spacing = it->max_extra_line_spacing;
22324
22325 if (STRINGP (it->string))
22326 it_charpos = IT_STRING_CHARPOS (*it);
22327 else
22328 it_charpos = IT_CHARPOS (*it);
22329
22330 /* This condition is for the case that we are called with current_x
22331 past last_visible_x. */
22332 while (it->current_x < max_x)
22333 {
22334 int x_before, x, n_glyphs_before, i, nglyphs;
22335
22336 /* Get the next display element. */
22337 if (!get_next_display_element (it))
22338 break;
22339
22340 /* Produce glyphs. */
22341 x_before = it->current_x;
22342 n_glyphs_before = row->used[TEXT_AREA];
22343 PRODUCE_GLYPHS (it);
22344
22345 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
22346 i = 0;
22347 x = x_before;
22348 while (i < nglyphs)
22349 {
22350 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
22351
22352 if (it->line_wrap != TRUNCATE
22353 && x + glyph->pixel_width > max_x)
22354 {
22355 /* End of continued line or max_x reached. */
22356 if (CHAR_GLYPH_PADDING_P (*glyph))
22357 {
22358 /* A wide character is unbreakable. */
22359 if (row->reversed_p)
22360 unproduce_glyphs (it, row->used[TEXT_AREA]
22361 - n_glyphs_before);
22362 row->used[TEXT_AREA] = n_glyphs_before;
22363 it->current_x = x_before;
22364 }
22365 else
22366 {
22367 if (row->reversed_p)
22368 unproduce_glyphs (it, row->used[TEXT_AREA]
22369 - (n_glyphs_before + i));
22370 row->used[TEXT_AREA] = n_glyphs_before + i;
22371 it->current_x = x;
22372 }
22373 break;
22374 }
22375 else if (x + glyph->pixel_width >= it->first_visible_x)
22376 {
22377 /* Glyph is at least partially visible. */
22378 ++it->hpos;
22379 if (x < it->first_visible_x)
22380 row->x = x - it->first_visible_x;
22381 }
22382 else
22383 {
22384 /* Glyph is off the left margin of the display area.
22385 Should not happen. */
22386 emacs_abort ();
22387 }
22388
22389 row->ascent = max (row->ascent, it->max_ascent);
22390 row->height = max (row->height, it->max_ascent + it->max_descent);
22391 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
22392 row->phys_height = max (row->phys_height,
22393 it->max_phys_ascent + it->max_phys_descent);
22394 row->extra_line_spacing = max (row->extra_line_spacing,
22395 it->max_extra_line_spacing);
22396 x += glyph->pixel_width;
22397 ++i;
22398 }
22399
22400 /* Stop if max_x reached. */
22401 if (i < nglyphs)
22402 break;
22403
22404 /* Stop at line ends. */
22405 if (ITERATOR_AT_END_OF_LINE_P (it))
22406 {
22407 it->continuation_lines_width = 0;
22408 break;
22409 }
22410
22411 set_iterator_to_next (it, 1);
22412 if (STRINGP (it->string))
22413 it_charpos = IT_STRING_CHARPOS (*it);
22414 else
22415 it_charpos = IT_CHARPOS (*it);
22416
22417 /* Stop if truncating at the right edge. */
22418 if (it->line_wrap == TRUNCATE
22419 && it->current_x >= it->last_visible_x)
22420 {
22421 /* Add truncation mark, but don't do it if the line is
22422 truncated at a padding space. */
22423 if (it_charpos < it->string_nchars)
22424 {
22425 if (!FRAME_WINDOW_P (it->f))
22426 {
22427 int ii, n;
22428
22429 if (it->current_x > it->last_visible_x)
22430 {
22431 if (!row->reversed_p)
22432 {
22433 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22434 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22435 break;
22436 }
22437 else
22438 {
22439 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22440 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22441 break;
22442 unproduce_glyphs (it, ii + 1);
22443 ii = row->used[TEXT_AREA] - (ii + 1);
22444 }
22445 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22446 {
22447 row->used[TEXT_AREA] = ii;
22448 produce_special_glyphs (it, IT_TRUNCATION);
22449 }
22450 }
22451 produce_special_glyphs (it, IT_TRUNCATION);
22452 }
22453 row->truncated_on_right_p = 1;
22454 }
22455 break;
22456 }
22457 }
22458
22459 /* Maybe insert a truncation at the left. */
22460 if (it->first_visible_x
22461 && it_charpos > 0)
22462 {
22463 if (!FRAME_WINDOW_P (it->f)
22464 || (row->reversed_p
22465 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22466 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22467 insert_left_trunc_glyphs (it);
22468 row->truncated_on_left_p = 1;
22469 }
22470
22471 it->face_id = saved_face_id;
22472
22473 /* Value is number of columns displayed. */
22474 return it->hpos - hpos_at_start;
22475 }
22476
22477
22478 \f
22479 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22480 appears as an element of LIST or as the car of an element of LIST.
22481 If PROPVAL is a list, compare each element against LIST in that
22482 way, and return 1/2 if any element of PROPVAL is found in LIST.
22483 Otherwise return 0. This function cannot quit.
22484 The return value is 2 if the text is invisible but with an ellipsis
22485 and 1 if it's invisible and without an ellipsis. */
22486
22487 int
22488 invisible_p (register Lisp_Object propval, Lisp_Object list)
22489 {
22490 register Lisp_Object tail, proptail;
22491
22492 for (tail = list; CONSP (tail); tail = XCDR (tail))
22493 {
22494 register Lisp_Object tem;
22495 tem = XCAR (tail);
22496 if (EQ (propval, tem))
22497 return 1;
22498 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22499 return NILP (XCDR (tem)) ? 1 : 2;
22500 }
22501
22502 if (CONSP (propval))
22503 {
22504 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22505 {
22506 Lisp_Object propelt;
22507 propelt = XCAR (proptail);
22508 for (tail = list; CONSP (tail); tail = XCDR (tail))
22509 {
22510 register Lisp_Object tem;
22511 tem = XCAR (tail);
22512 if (EQ (propelt, tem))
22513 return 1;
22514 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22515 return NILP (XCDR (tem)) ? 1 : 2;
22516 }
22517 }
22518 }
22519
22520 return 0;
22521 }
22522
22523 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22524 doc: /* Non-nil if the property makes the text invisible.
22525 POS-OR-PROP can be a marker or number, in which case it is taken to be
22526 a position in the current buffer and the value of the `invisible' property
22527 is checked; or it can be some other value, which is then presumed to be the
22528 value of the `invisible' property of the text of interest.
22529 The non-nil value returned can be t for truly invisible text or something
22530 else if the text is replaced by an ellipsis. */)
22531 (Lisp_Object pos_or_prop)
22532 {
22533 Lisp_Object prop
22534 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22535 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22536 : pos_or_prop);
22537 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22538 return (invis == 0 ? Qnil
22539 : invis == 1 ? Qt
22540 : make_number (invis));
22541 }
22542
22543 /* Calculate a width or height in pixels from a specification using
22544 the following elements:
22545
22546 SPEC ::=
22547 NUM - a (fractional) multiple of the default font width/height
22548 (NUM) - specifies exactly NUM pixels
22549 UNIT - a fixed number of pixels, see below.
22550 ELEMENT - size of a display element in pixels, see below.
22551 (NUM . SPEC) - equals NUM * SPEC
22552 (+ SPEC SPEC ...) - add pixel values
22553 (- SPEC SPEC ...) - subtract pixel values
22554 (- SPEC) - negate pixel value
22555
22556 NUM ::=
22557 INT or FLOAT - a number constant
22558 SYMBOL - use symbol's (buffer local) variable binding.
22559
22560 UNIT ::=
22561 in - pixels per inch *)
22562 mm - pixels per 1/1000 meter *)
22563 cm - pixels per 1/100 meter *)
22564 width - width of current font in pixels.
22565 height - height of current font in pixels.
22566
22567 *) using the ratio(s) defined in display-pixels-per-inch.
22568
22569 ELEMENT ::=
22570
22571 left-fringe - left fringe width in pixels
22572 right-fringe - right fringe width in pixels
22573
22574 left-margin - left margin width in pixels
22575 right-margin - right margin width in pixels
22576
22577 scroll-bar - scroll-bar area width in pixels
22578
22579 Examples:
22580
22581 Pixels corresponding to 5 inches:
22582 (5 . in)
22583
22584 Total width of non-text areas on left side of window (if scroll-bar is on left):
22585 '(space :width (+ left-fringe left-margin scroll-bar))
22586
22587 Align to first text column (in header line):
22588 '(space :align-to 0)
22589
22590 Align to middle of text area minus half the width of variable `my-image'
22591 containing a loaded image:
22592 '(space :align-to (0.5 . (- text my-image)))
22593
22594 Width of left margin minus width of 1 character in the default font:
22595 '(space :width (- left-margin 1))
22596
22597 Width of left margin minus width of 2 characters in the current font:
22598 '(space :width (- left-margin (2 . width)))
22599
22600 Center 1 character over left-margin (in header line):
22601 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22602
22603 Different ways to express width of left fringe plus left margin minus one pixel:
22604 '(space :width (- (+ left-fringe left-margin) (1)))
22605 '(space :width (+ left-fringe left-margin (- (1))))
22606 '(space :width (+ left-fringe left-margin (-1)))
22607
22608 */
22609
22610 static int
22611 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22612 struct font *font, int width_p, int *align_to)
22613 {
22614 double pixels;
22615
22616 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22617 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22618
22619 if (NILP (prop))
22620 return OK_PIXELS (0);
22621
22622 eassert (FRAME_LIVE_P (it->f));
22623
22624 if (SYMBOLP (prop))
22625 {
22626 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22627 {
22628 char *unit = SSDATA (SYMBOL_NAME (prop));
22629
22630 if (unit[0] == 'i' && unit[1] == 'n')
22631 pixels = 1.0;
22632 else if (unit[0] == 'm' && unit[1] == 'm')
22633 pixels = 25.4;
22634 else if (unit[0] == 'c' && unit[1] == 'm')
22635 pixels = 2.54;
22636 else
22637 pixels = 0;
22638 if (pixels > 0)
22639 {
22640 double ppi = (width_p ? FRAME_RES_X (it->f)
22641 : FRAME_RES_Y (it->f));
22642
22643 if (ppi > 0)
22644 return OK_PIXELS (ppi / pixels);
22645 return 0;
22646 }
22647 }
22648
22649 #ifdef HAVE_WINDOW_SYSTEM
22650 if (EQ (prop, Qheight))
22651 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22652 if (EQ (prop, Qwidth))
22653 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22654 #else
22655 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22656 return OK_PIXELS (1);
22657 #endif
22658
22659 if (EQ (prop, Qtext))
22660 return OK_PIXELS (width_p
22661 ? window_box_width (it->w, TEXT_AREA)
22662 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22663
22664 if (align_to && *align_to < 0)
22665 {
22666 *res = 0;
22667 if (EQ (prop, Qleft))
22668 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22669 if (EQ (prop, Qright))
22670 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22671 if (EQ (prop, Qcenter))
22672 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22673 + window_box_width (it->w, TEXT_AREA) / 2);
22674 if (EQ (prop, Qleft_fringe))
22675 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22676 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22677 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22678 if (EQ (prop, Qright_fringe))
22679 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22680 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22681 : window_box_right_offset (it->w, TEXT_AREA));
22682 if (EQ (prop, Qleft_margin))
22683 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22684 if (EQ (prop, Qright_margin))
22685 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22686 if (EQ (prop, Qscroll_bar))
22687 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22688 ? 0
22689 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22690 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22691 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22692 : 0)));
22693 }
22694 else
22695 {
22696 if (EQ (prop, Qleft_fringe))
22697 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22698 if (EQ (prop, Qright_fringe))
22699 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22700 if (EQ (prop, Qleft_margin))
22701 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22702 if (EQ (prop, Qright_margin))
22703 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22704 if (EQ (prop, Qscroll_bar))
22705 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22706 }
22707
22708 prop = buffer_local_value_1 (prop, it->w->contents);
22709 if (EQ (prop, Qunbound))
22710 prop = Qnil;
22711 }
22712
22713 if (INTEGERP (prop) || FLOATP (prop))
22714 {
22715 int base_unit = (width_p
22716 ? FRAME_COLUMN_WIDTH (it->f)
22717 : FRAME_LINE_HEIGHT (it->f));
22718 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22719 }
22720
22721 if (CONSP (prop))
22722 {
22723 Lisp_Object car = XCAR (prop);
22724 Lisp_Object cdr = XCDR (prop);
22725
22726 if (SYMBOLP (car))
22727 {
22728 #ifdef HAVE_WINDOW_SYSTEM
22729 if (FRAME_WINDOW_P (it->f)
22730 && valid_image_p (prop))
22731 {
22732 ptrdiff_t id = lookup_image (it->f, prop);
22733 struct image *img = IMAGE_FROM_ID (it->f, id);
22734
22735 return OK_PIXELS (width_p ? img->width : img->height);
22736 }
22737 #endif
22738 if (EQ (car, Qplus) || EQ (car, Qminus))
22739 {
22740 int first = 1;
22741 double px;
22742
22743 pixels = 0;
22744 while (CONSP (cdr))
22745 {
22746 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22747 font, width_p, align_to))
22748 return 0;
22749 if (first)
22750 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22751 else
22752 pixels += px;
22753 cdr = XCDR (cdr);
22754 }
22755 if (EQ (car, Qminus))
22756 pixels = -pixels;
22757 return OK_PIXELS (pixels);
22758 }
22759
22760 car = buffer_local_value_1 (car, it->w->contents);
22761 if (EQ (car, Qunbound))
22762 car = Qnil;
22763 }
22764
22765 if (INTEGERP (car) || FLOATP (car))
22766 {
22767 double fact;
22768 pixels = XFLOATINT (car);
22769 if (NILP (cdr))
22770 return OK_PIXELS (pixels);
22771 if (calc_pixel_width_or_height (&fact, it, cdr,
22772 font, width_p, align_to))
22773 return OK_PIXELS (pixels * fact);
22774 return 0;
22775 }
22776
22777 return 0;
22778 }
22779
22780 return 0;
22781 }
22782
22783 \f
22784 /***********************************************************************
22785 Glyph Display
22786 ***********************************************************************/
22787
22788 #ifdef HAVE_WINDOW_SYSTEM
22789
22790 #ifdef GLYPH_DEBUG
22791
22792 void
22793 dump_glyph_string (struct glyph_string *s)
22794 {
22795 fprintf (stderr, "glyph string\n");
22796 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22797 s->x, s->y, s->width, s->height);
22798 fprintf (stderr, " ybase = %d\n", s->ybase);
22799 fprintf (stderr, " hl = %d\n", s->hl);
22800 fprintf (stderr, " left overhang = %d, right = %d\n",
22801 s->left_overhang, s->right_overhang);
22802 fprintf (stderr, " nchars = %d\n", s->nchars);
22803 fprintf (stderr, " extends to end of line = %d\n",
22804 s->extends_to_end_of_line_p);
22805 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22806 fprintf (stderr, " bg width = %d\n", s->background_width);
22807 }
22808
22809 #endif /* GLYPH_DEBUG */
22810
22811 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22812 of XChar2b structures for S; it can't be allocated in
22813 init_glyph_string because it must be allocated via `alloca'. W
22814 is the window on which S is drawn. ROW and AREA are the glyph row
22815 and area within the row from which S is constructed. START is the
22816 index of the first glyph structure covered by S. HL is a
22817 face-override for drawing S. */
22818
22819 #ifdef HAVE_NTGUI
22820 #define OPTIONAL_HDC(hdc) HDC hdc,
22821 #define DECLARE_HDC(hdc) HDC hdc;
22822 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22823 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22824 #endif
22825
22826 #ifndef OPTIONAL_HDC
22827 #define OPTIONAL_HDC(hdc)
22828 #define DECLARE_HDC(hdc)
22829 #define ALLOCATE_HDC(hdc, f)
22830 #define RELEASE_HDC(hdc, f)
22831 #endif
22832
22833 static void
22834 init_glyph_string (struct glyph_string *s,
22835 OPTIONAL_HDC (hdc)
22836 XChar2b *char2b, struct window *w, struct glyph_row *row,
22837 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22838 {
22839 memset (s, 0, sizeof *s);
22840 s->w = w;
22841 s->f = XFRAME (w->frame);
22842 #ifdef HAVE_NTGUI
22843 s->hdc = hdc;
22844 #endif
22845 s->display = FRAME_X_DISPLAY (s->f);
22846 s->window = FRAME_X_WINDOW (s->f);
22847 s->char2b = char2b;
22848 s->hl = hl;
22849 s->row = row;
22850 s->area = area;
22851 s->first_glyph = row->glyphs[area] + start;
22852 s->height = row->height;
22853 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22854 s->ybase = s->y + row->ascent;
22855 }
22856
22857
22858 /* Append the list of glyph strings with head H and tail T to the list
22859 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22860
22861 static void
22862 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22863 struct glyph_string *h, struct glyph_string *t)
22864 {
22865 if (h)
22866 {
22867 if (*head)
22868 (*tail)->next = h;
22869 else
22870 *head = h;
22871 h->prev = *tail;
22872 *tail = t;
22873 }
22874 }
22875
22876
22877 /* Prepend the list of glyph strings with head H and tail T to the
22878 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22879 result. */
22880
22881 static void
22882 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22883 struct glyph_string *h, struct glyph_string *t)
22884 {
22885 if (h)
22886 {
22887 if (*head)
22888 (*head)->prev = t;
22889 else
22890 *tail = t;
22891 t->next = *head;
22892 *head = h;
22893 }
22894 }
22895
22896
22897 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22898 Set *HEAD and *TAIL to the resulting list. */
22899
22900 static void
22901 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22902 struct glyph_string *s)
22903 {
22904 s->next = s->prev = NULL;
22905 append_glyph_string_lists (head, tail, s, s);
22906 }
22907
22908
22909 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22910 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22911 make sure that X resources for the face returned are allocated.
22912 Value is a pointer to a realized face that is ready for display if
22913 DISPLAY_P is non-zero. */
22914
22915 static struct face *
22916 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22917 XChar2b *char2b, int display_p)
22918 {
22919 struct face *face = FACE_FROM_ID (f, face_id);
22920 unsigned code = 0;
22921
22922 if (face->font)
22923 {
22924 code = face->font->driver->encode_char (face->font, c);
22925
22926 if (code == FONT_INVALID_CODE)
22927 code = 0;
22928 }
22929 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22930
22931 /* Make sure X resources of the face are allocated. */
22932 #ifdef HAVE_X_WINDOWS
22933 if (display_p)
22934 #endif
22935 {
22936 eassert (face != NULL);
22937 PREPARE_FACE_FOR_DISPLAY (f, face);
22938 }
22939
22940 return face;
22941 }
22942
22943
22944 /* Get face and two-byte form of character glyph GLYPH on frame F.
22945 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22946 a pointer to a realized face that is ready for display. */
22947
22948 static struct face *
22949 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22950 XChar2b *char2b, int *two_byte_p)
22951 {
22952 struct face *face;
22953 unsigned code = 0;
22954
22955 eassert (glyph->type == CHAR_GLYPH);
22956 face = FACE_FROM_ID (f, glyph->face_id);
22957
22958 /* Make sure X resources of the face are allocated. */
22959 eassert (face != NULL);
22960 PREPARE_FACE_FOR_DISPLAY (f, face);
22961
22962 if (two_byte_p)
22963 *two_byte_p = 0;
22964
22965 if (face->font)
22966 {
22967 if (CHAR_BYTE8_P (glyph->u.ch))
22968 code = CHAR_TO_BYTE8 (glyph->u.ch);
22969 else
22970 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22971
22972 if (code == FONT_INVALID_CODE)
22973 code = 0;
22974 }
22975
22976 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22977 return face;
22978 }
22979
22980
22981 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22982 Return 1 if FONT has a glyph for C, otherwise return 0. */
22983
22984 static int
22985 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22986 {
22987 unsigned code;
22988
22989 if (CHAR_BYTE8_P (c))
22990 code = CHAR_TO_BYTE8 (c);
22991 else
22992 code = font->driver->encode_char (font, c);
22993
22994 if (code == FONT_INVALID_CODE)
22995 return 0;
22996 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22997 return 1;
22998 }
22999
23000
23001 /* Fill glyph string S with composition components specified by S->cmp.
23002
23003 BASE_FACE is the base face of the composition.
23004 S->cmp_from is the index of the first component for S.
23005
23006 OVERLAPS non-zero means S should draw the foreground only, and use
23007 its physical height for clipping. See also draw_glyphs.
23008
23009 Value is the index of a component not in S. */
23010
23011 static int
23012 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
23013 int overlaps)
23014 {
23015 int i;
23016 /* For all glyphs of this composition, starting at the offset
23017 S->cmp_from, until we reach the end of the definition or encounter a
23018 glyph that requires the different face, add it to S. */
23019 struct face *face;
23020
23021 eassert (s);
23022
23023 s->for_overlaps = overlaps;
23024 s->face = NULL;
23025 s->font = NULL;
23026 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
23027 {
23028 int c = COMPOSITION_GLYPH (s->cmp, i);
23029
23030 /* TAB in a composition means display glyphs with padding space
23031 on the left or right. */
23032 if (c != '\t')
23033 {
23034 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
23035 -1, Qnil);
23036
23037 face = get_char_face_and_encoding (s->f, c, face_id,
23038 s->char2b + i, 1);
23039 if (face)
23040 {
23041 if (! s->face)
23042 {
23043 s->face = face;
23044 s->font = s->face->font;
23045 }
23046 else if (s->face != face)
23047 break;
23048 }
23049 }
23050 ++s->nchars;
23051 }
23052 s->cmp_to = i;
23053
23054 if (s->face == NULL)
23055 {
23056 s->face = base_face->ascii_face;
23057 s->font = s->face->font;
23058 }
23059
23060 /* All glyph strings for the same composition has the same width,
23061 i.e. the width set for the first component of the composition. */
23062 s->width = s->first_glyph->pixel_width;
23063
23064 /* If the specified font could not be loaded, use the frame's
23065 default font, but record the fact that we couldn't load it in
23066 the glyph string so that we can draw rectangles for the
23067 characters of the glyph string. */
23068 if (s->font == NULL)
23069 {
23070 s->font_not_found_p = 1;
23071 s->font = FRAME_FONT (s->f);
23072 }
23073
23074 /* Adjust base line for subscript/superscript text. */
23075 s->ybase += s->first_glyph->voffset;
23076
23077 /* This glyph string must always be drawn with 16-bit functions. */
23078 s->two_byte_p = 1;
23079
23080 return s->cmp_to;
23081 }
23082
23083 static int
23084 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
23085 int start, int end, int overlaps)
23086 {
23087 struct glyph *glyph, *last;
23088 Lisp_Object lgstring;
23089 int i;
23090
23091 s->for_overlaps = overlaps;
23092 glyph = s->row->glyphs[s->area] + start;
23093 last = s->row->glyphs[s->area] + end;
23094 s->cmp_id = glyph->u.cmp.id;
23095 s->cmp_from = glyph->slice.cmp.from;
23096 s->cmp_to = glyph->slice.cmp.to + 1;
23097 s->face = FACE_FROM_ID (s->f, face_id);
23098 lgstring = composition_gstring_from_id (s->cmp_id);
23099 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
23100 glyph++;
23101 while (glyph < last
23102 && glyph->u.cmp.automatic
23103 && glyph->u.cmp.id == s->cmp_id
23104 && s->cmp_to == glyph->slice.cmp.from)
23105 s->cmp_to = (glyph++)->slice.cmp.to + 1;
23106
23107 for (i = s->cmp_from; i < s->cmp_to; i++)
23108 {
23109 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
23110 unsigned code = LGLYPH_CODE (lglyph);
23111
23112 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
23113 }
23114 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
23115 return glyph - s->row->glyphs[s->area];
23116 }
23117
23118
23119 /* Fill glyph string S from a sequence glyphs for glyphless characters.
23120 See the comment of fill_glyph_string for arguments.
23121 Value is the index of the first glyph not in S. */
23122
23123
23124 static int
23125 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
23126 int start, int end, int overlaps)
23127 {
23128 struct glyph *glyph, *last;
23129 int voffset;
23130
23131 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
23132 s->for_overlaps = overlaps;
23133 glyph = s->row->glyphs[s->area] + start;
23134 last = s->row->glyphs[s->area] + end;
23135 voffset = glyph->voffset;
23136 s->face = FACE_FROM_ID (s->f, face_id);
23137 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
23138 s->nchars = 1;
23139 s->width = glyph->pixel_width;
23140 glyph++;
23141 while (glyph < last
23142 && glyph->type == GLYPHLESS_GLYPH
23143 && glyph->voffset == voffset
23144 && glyph->face_id == face_id)
23145 {
23146 s->nchars++;
23147 s->width += glyph->pixel_width;
23148 glyph++;
23149 }
23150 s->ybase += voffset;
23151 return glyph - s->row->glyphs[s->area];
23152 }
23153
23154
23155 /* Fill glyph string S from a sequence of character glyphs.
23156
23157 FACE_ID is the face id of the string. START is the index of the
23158 first glyph to consider, END is the index of the last + 1.
23159 OVERLAPS non-zero means S should draw the foreground only, and use
23160 its physical height for clipping. See also draw_glyphs.
23161
23162 Value is the index of the first glyph not in S. */
23163
23164 static int
23165 fill_glyph_string (struct glyph_string *s, int face_id,
23166 int start, int end, int overlaps)
23167 {
23168 struct glyph *glyph, *last;
23169 int voffset;
23170 int glyph_not_available_p;
23171
23172 eassert (s->f == XFRAME (s->w->frame));
23173 eassert (s->nchars == 0);
23174 eassert (start >= 0 && end > start);
23175
23176 s->for_overlaps = overlaps;
23177 glyph = s->row->glyphs[s->area] + start;
23178 last = s->row->glyphs[s->area] + end;
23179 voffset = glyph->voffset;
23180 s->padding_p = glyph->padding_p;
23181 glyph_not_available_p = glyph->glyph_not_available_p;
23182
23183 while (glyph < last
23184 && glyph->type == CHAR_GLYPH
23185 && glyph->voffset == voffset
23186 /* Same face id implies same font, nowadays. */
23187 && glyph->face_id == face_id
23188 && glyph->glyph_not_available_p == glyph_not_available_p)
23189 {
23190 int two_byte_p;
23191
23192 s->face = get_glyph_face_and_encoding (s->f, glyph,
23193 s->char2b + s->nchars,
23194 &two_byte_p);
23195 s->two_byte_p = two_byte_p;
23196 ++s->nchars;
23197 eassert (s->nchars <= end - start);
23198 s->width += glyph->pixel_width;
23199 if (glyph++->padding_p != s->padding_p)
23200 break;
23201 }
23202
23203 s->font = s->face->font;
23204
23205 /* If the specified font could not be loaded, use the frame's font,
23206 but record the fact that we couldn't load it in
23207 S->font_not_found_p so that we can draw rectangles for the
23208 characters of the glyph string. */
23209 if (s->font == NULL || glyph_not_available_p)
23210 {
23211 s->font_not_found_p = 1;
23212 s->font = FRAME_FONT (s->f);
23213 }
23214
23215 /* Adjust base line for subscript/superscript text. */
23216 s->ybase += voffset;
23217
23218 eassert (s->face && s->face->gc);
23219 return glyph - s->row->glyphs[s->area];
23220 }
23221
23222
23223 /* Fill glyph string S from image glyph S->first_glyph. */
23224
23225 static void
23226 fill_image_glyph_string (struct glyph_string *s)
23227 {
23228 eassert (s->first_glyph->type == IMAGE_GLYPH);
23229 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
23230 eassert (s->img);
23231 s->slice = s->first_glyph->slice.img;
23232 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
23233 s->font = s->face->font;
23234 s->width = s->first_glyph->pixel_width;
23235
23236 /* Adjust base line for subscript/superscript text. */
23237 s->ybase += s->first_glyph->voffset;
23238 }
23239
23240
23241 /* Fill glyph string S from a sequence of stretch glyphs.
23242
23243 START is the index of the first glyph to consider,
23244 END is the index of the last + 1.
23245
23246 Value is the index of the first glyph not in S. */
23247
23248 static int
23249 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
23250 {
23251 struct glyph *glyph, *last;
23252 int voffset, face_id;
23253
23254 eassert (s->first_glyph->type == STRETCH_GLYPH);
23255
23256 glyph = s->row->glyphs[s->area] + start;
23257 last = s->row->glyphs[s->area] + end;
23258 face_id = glyph->face_id;
23259 s->face = FACE_FROM_ID (s->f, face_id);
23260 s->font = s->face->font;
23261 s->width = glyph->pixel_width;
23262 s->nchars = 1;
23263 voffset = glyph->voffset;
23264
23265 for (++glyph;
23266 (glyph < last
23267 && glyph->type == STRETCH_GLYPH
23268 && glyph->voffset == voffset
23269 && glyph->face_id == face_id);
23270 ++glyph)
23271 s->width += glyph->pixel_width;
23272
23273 /* Adjust base line for subscript/superscript text. */
23274 s->ybase += voffset;
23275
23276 /* The case that face->gc == 0 is handled when drawing the glyph
23277 string by calling PREPARE_FACE_FOR_DISPLAY. */
23278 eassert (s->face);
23279 return glyph - s->row->glyphs[s->area];
23280 }
23281
23282 static struct font_metrics *
23283 get_per_char_metric (struct font *font, XChar2b *char2b)
23284 {
23285 static struct font_metrics metrics;
23286 unsigned code;
23287
23288 if (! font)
23289 return NULL;
23290 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
23291 if (code == FONT_INVALID_CODE)
23292 return NULL;
23293 font->driver->text_extents (font, &code, 1, &metrics);
23294 return &metrics;
23295 }
23296
23297 /* EXPORT for RIF:
23298 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
23299 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
23300 assumed to be zero. */
23301
23302 void
23303 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
23304 {
23305 *left = *right = 0;
23306
23307 if (glyph->type == CHAR_GLYPH)
23308 {
23309 struct face *face;
23310 XChar2b char2b;
23311 struct font_metrics *pcm;
23312
23313 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
23314 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
23315 {
23316 if (pcm->rbearing > pcm->width)
23317 *right = pcm->rbearing - pcm->width;
23318 if (pcm->lbearing < 0)
23319 *left = -pcm->lbearing;
23320 }
23321 }
23322 else if (glyph->type == COMPOSITE_GLYPH)
23323 {
23324 if (! glyph->u.cmp.automatic)
23325 {
23326 struct composition *cmp = composition_table[glyph->u.cmp.id];
23327
23328 if (cmp->rbearing > cmp->pixel_width)
23329 *right = cmp->rbearing - cmp->pixel_width;
23330 if (cmp->lbearing < 0)
23331 *left = - cmp->lbearing;
23332 }
23333 else
23334 {
23335 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
23336 struct font_metrics metrics;
23337
23338 composition_gstring_width (gstring, glyph->slice.cmp.from,
23339 glyph->slice.cmp.to + 1, &metrics);
23340 if (metrics.rbearing > metrics.width)
23341 *right = metrics.rbearing - metrics.width;
23342 if (metrics.lbearing < 0)
23343 *left = - metrics.lbearing;
23344 }
23345 }
23346 }
23347
23348
23349 /* Return the index of the first glyph preceding glyph string S that
23350 is overwritten by S because of S's left overhang. Value is -1
23351 if no glyphs are overwritten. */
23352
23353 static int
23354 left_overwritten (struct glyph_string *s)
23355 {
23356 int k;
23357
23358 if (s->left_overhang)
23359 {
23360 int x = 0, i;
23361 struct glyph *glyphs = s->row->glyphs[s->area];
23362 int first = s->first_glyph - glyphs;
23363
23364 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
23365 x -= glyphs[i].pixel_width;
23366
23367 k = i + 1;
23368 }
23369 else
23370 k = -1;
23371
23372 return k;
23373 }
23374
23375
23376 /* Return the index of the first glyph preceding glyph string S that
23377 is overwriting S because of its right overhang. Value is -1 if no
23378 glyph in front of S overwrites S. */
23379
23380 static int
23381 left_overwriting (struct glyph_string *s)
23382 {
23383 int i, k, x;
23384 struct glyph *glyphs = s->row->glyphs[s->area];
23385 int first = s->first_glyph - glyphs;
23386
23387 k = -1;
23388 x = 0;
23389 for (i = first - 1; i >= 0; --i)
23390 {
23391 int left, right;
23392 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23393 if (x + right > 0)
23394 k = i;
23395 x -= glyphs[i].pixel_width;
23396 }
23397
23398 return k;
23399 }
23400
23401
23402 /* Return the index of the last glyph following glyph string S that is
23403 overwritten by S because of S's right overhang. Value is -1 if
23404 no such glyph is found. */
23405
23406 static int
23407 right_overwritten (struct glyph_string *s)
23408 {
23409 int k = -1;
23410
23411 if (s->right_overhang)
23412 {
23413 int x = 0, i;
23414 struct glyph *glyphs = s->row->glyphs[s->area];
23415 int first = (s->first_glyph - glyphs
23416 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23417 int end = s->row->used[s->area];
23418
23419 for (i = first; i < end && s->right_overhang > x; ++i)
23420 x += glyphs[i].pixel_width;
23421
23422 k = i;
23423 }
23424
23425 return k;
23426 }
23427
23428
23429 /* Return the index of the last glyph following glyph string S that
23430 overwrites S because of its left overhang. Value is negative
23431 if no such glyph is found. */
23432
23433 static int
23434 right_overwriting (struct glyph_string *s)
23435 {
23436 int i, k, x;
23437 int end = s->row->used[s->area];
23438 struct glyph *glyphs = s->row->glyphs[s->area];
23439 int first = (s->first_glyph - glyphs
23440 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23441
23442 k = -1;
23443 x = 0;
23444 for (i = first; i < end; ++i)
23445 {
23446 int left, right;
23447 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23448 if (x - left < 0)
23449 k = i;
23450 x += glyphs[i].pixel_width;
23451 }
23452
23453 return k;
23454 }
23455
23456
23457 /* Set background width of glyph string S. START is the index of the
23458 first glyph following S. LAST_X is the right-most x-position + 1
23459 in the drawing area. */
23460
23461 static void
23462 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23463 {
23464 /* If the face of this glyph string has to be drawn to the end of
23465 the drawing area, set S->extends_to_end_of_line_p. */
23466
23467 if (start == s->row->used[s->area]
23468 && s->area == TEXT_AREA
23469 && ((s->row->fill_line_p
23470 && (s->hl == DRAW_NORMAL_TEXT
23471 || s->hl == DRAW_IMAGE_RAISED
23472 || s->hl == DRAW_IMAGE_SUNKEN))
23473 || s->hl == DRAW_MOUSE_FACE))
23474 s->extends_to_end_of_line_p = 1;
23475
23476 /* If S extends its face to the end of the line, set its
23477 background_width to the distance to the right edge of the drawing
23478 area. */
23479 if (s->extends_to_end_of_line_p)
23480 s->background_width = last_x - s->x + 1;
23481 else
23482 s->background_width = s->width;
23483 }
23484
23485
23486 /* Compute overhangs and x-positions for glyph string S and its
23487 predecessors, or successors. X is the starting x-position for S.
23488 BACKWARD_P non-zero means process predecessors. */
23489
23490 static void
23491 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23492 {
23493 if (backward_p)
23494 {
23495 while (s)
23496 {
23497 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23498 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23499 x -= s->width;
23500 s->x = x;
23501 s = s->prev;
23502 }
23503 }
23504 else
23505 {
23506 while (s)
23507 {
23508 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23509 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23510 s->x = x;
23511 x += s->width;
23512 s = s->next;
23513 }
23514 }
23515 }
23516
23517
23518
23519 /* The following macros are only called from draw_glyphs below.
23520 They reference the following parameters of that function directly:
23521 `w', `row', `area', and `overlap_p'
23522 as well as the following local variables:
23523 `s', `f', and `hdc' (in W32) */
23524
23525 #ifdef HAVE_NTGUI
23526 /* On W32, silently add local `hdc' variable to argument list of
23527 init_glyph_string. */
23528 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23529 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23530 #else
23531 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23532 init_glyph_string (s, char2b, w, row, area, start, hl)
23533 #endif
23534
23535 /* Add a glyph string for a stretch glyph to the list of strings
23536 between HEAD and TAIL. START is the index of the stretch glyph in
23537 row area AREA of glyph row ROW. END is the index of the last glyph
23538 in that glyph row area. X is the current output position assigned
23539 to the new glyph string constructed. HL overrides that face of the
23540 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23541 is the right-most x-position of the drawing area. */
23542
23543 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23544 and below -- keep them on one line. */
23545 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23546 do \
23547 { \
23548 s = alloca (sizeof *s); \
23549 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23550 START = fill_stretch_glyph_string (s, START, END); \
23551 append_glyph_string (&HEAD, &TAIL, s); \
23552 s->x = (X); \
23553 } \
23554 while (0)
23555
23556
23557 /* Add a glyph string for an image glyph to the list of strings
23558 between HEAD and TAIL. START is the index of the image glyph in
23559 row area AREA of glyph row ROW. END is the index of the last glyph
23560 in that glyph row area. X is the current output position assigned
23561 to the new glyph string constructed. HL overrides that face of the
23562 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23563 is the right-most x-position of the drawing area. */
23564
23565 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23566 do \
23567 { \
23568 s = alloca (sizeof *s); \
23569 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23570 fill_image_glyph_string (s); \
23571 append_glyph_string (&HEAD, &TAIL, s); \
23572 ++START; \
23573 s->x = (X); \
23574 } \
23575 while (0)
23576
23577
23578 /* Add a glyph string for a sequence of character glyphs to the list
23579 of strings between HEAD and TAIL. START is the index of the first
23580 glyph in row area AREA of glyph row ROW that is part of the new
23581 glyph string. END is the index of the last glyph in that glyph row
23582 area. X is the current output position assigned to the new glyph
23583 string constructed. HL overrides that face of the glyph; e.g. it
23584 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23585 right-most x-position of the drawing area. */
23586
23587 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23588 do \
23589 { \
23590 int face_id; \
23591 XChar2b *char2b; \
23592 \
23593 face_id = (row)->glyphs[area][START].face_id; \
23594 \
23595 s = alloca (sizeof *s); \
23596 char2b = alloca ((END - START) * sizeof *char2b); \
23597 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23598 append_glyph_string (&HEAD, &TAIL, s); \
23599 s->x = (X); \
23600 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23601 } \
23602 while (0)
23603
23604
23605 /* Add a glyph string for a composite sequence to the list of strings
23606 between HEAD and TAIL. START is the index of the first glyph in
23607 row area AREA of glyph row ROW that is part of the new glyph
23608 string. END is the index of the last glyph in that glyph row area.
23609 X is the current output position assigned to the new glyph string
23610 constructed. HL overrides that face of the glyph; e.g. it is
23611 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23612 x-position of the drawing area. */
23613
23614 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23615 do { \
23616 int face_id = (row)->glyphs[area][START].face_id; \
23617 struct face *base_face = FACE_FROM_ID (f, face_id); \
23618 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23619 struct composition *cmp = composition_table[cmp_id]; \
23620 XChar2b *char2b; \
23621 struct glyph_string *first_s = NULL; \
23622 int n; \
23623 \
23624 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23625 \
23626 /* Make glyph_strings for each glyph sequence that is drawable by \
23627 the same face, and append them to HEAD/TAIL. */ \
23628 for (n = 0; n < cmp->glyph_len;) \
23629 { \
23630 s = alloca (sizeof *s); \
23631 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23632 append_glyph_string (&(HEAD), &(TAIL), s); \
23633 s->cmp = cmp; \
23634 s->cmp_from = n; \
23635 s->x = (X); \
23636 if (n == 0) \
23637 first_s = s; \
23638 n = fill_composite_glyph_string (s, base_face, overlaps); \
23639 } \
23640 \
23641 ++START; \
23642 s = first_s; \
23643 } while (0)
23644
23645
23646 /* Add a glyph string for a glyph-string sequence to the list of strings
23647 between HEAD and TAIL. */
23648
23649 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23650 do { \
23651 int face_id; \
23652 XChar2b *char2b; \
23653 Lisp_Object gstring; \
23654 \
23655 face_id = (row)->glyphs[area][START].face_id; \
23656 gstring = (composition_gstring_from_id \
23657 ((row)->glyphs[area][START].u.cmp.id)); \
23658 s = alloca (sizeof *s); \
23659 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23660 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23661 append_glyph_string (&(HEAD), &(TAIL), s); \
23662 s->x = (X); \
23663 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23664 } while (0)
23665
23666
23667 /* Add a glyph string for a sequence of glyphless character's glyphs
23668 to the list of strings between HEAD and TAIL. The meanings of
23669 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23670
23671 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23672 do \
23673 { \
23674 int face_id; \
23675 \
23676 face_id = (row)->glyphs[area][START].face_id; \
23677 \
23678 s = alloca (sizeof *s); \
23679 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23680 append_glyph_string (&HEAD, &TAIL, s); \
23681 s->x = (X); \
23682 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23683 overlaps); \
23684 } \
23685 while (0)
23686
23687
23688 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23689 of AREA of glyph row ROW on window W between indices START and END.
23690 HL overrides the face for drawing glyph strings, e.g. it is
23691 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23692 x-positions of the drawing area.
23693
23694 This is an ugly monster macro construct because we must use alloca
23695 to allocate glyph strings (because draw_glyphs can be called
23696 asynchronously). */
23697
23698 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23699 do \
23700 { \
23701 HEAD = TAIL = NULL; \
23702 while (START < END) \
23703 { \
23704 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23705 switch (first_glyph->type) \
23706 { \
23707 case CHAR_GLYPH: \
23708 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23709 HL, X, LAST_X); \
23710 break; \
23711 \
23712 case COMPOSITE_GLYPH: \
23713 if (first_glyph->u.cmp.automatic) \
23714 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23715 HL, X, LAST_X); \
23716 else \
23717 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23718 HL, X, LAST_X); \
23719 break; \
23720 \
23721 case STRETCH_GLYPH: \
23722 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23723 HL, X, LAST_X); \
23724 break; \
23725 \
23726 case IMAGE_GLYPH: \
23727 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23728 HL, X, LAST_X); \
23729 break; \
23730 \
23731 case GLYPHLESS_GLYPH: \
23732 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23733 HL, X, LAST_X); \
23734 break; \
23735 \
23736 default: \
23737 emacs_abort (); \
23738 } \
23739 \
23740 if (s) \
23741 { \
23742 set_glyph_string_background_width (s, START, LAST_X); \
23743 (X) += s->width; \
23744 } \
23745 } \
23746 } while (0)
23747
23748
23749 /* Draw glyphs between START and END in AREA of ROW on window W,
23750 starting at x-position X. X is relative to AREA in W. HL is a
23751 face-override with the following meaning:
23752
23753 DRAW_NORMAL_TEXT draw normally
23754 DRAW_CURSOR draw in cursor face
23755 DRAW_MOUSE_FACE draw in mouse face.
23756 DRAW_INVERSE_VIDEO draw in mode line face
23757 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23758 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23759
23760 If OVERLAPS is non-zero, draw only the foreground of characters and
23761 clip to the physical height of ROW. Non-zero value also defines
23762 the overlapping part to be drawn:
23763
23764 OVERLAPS_PRED overlap with preceding rows
23765 OVERLAPS_SUCC overlap with succeeding rows
23766 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23767 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23768
23769 Value is the x-position reached, relative to AREA of W. */
23770
23771 static int
23772 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23773 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23774 enum draw_glyphs_face hl, int overlaps)
23775 {
23776 struct glyph_string *head, *tail;
23777 struct glyph_string *s;
23778 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23779 int i, j, x_reached, last_x, area_left = 0;
23780 struct frame *f = XFRAME (WINDOW_FRAME (w));
23781 DECLARE_HDC (hdc);
23782
23783 ALLOCATE_HDC (hdc, f);
23784
23785 /* Let's rather be paranoid than getting a SEGV. */
23786 end = min (end, row->used[area]);
23787 start = clip_to_bounds (0, start, end);
23788
23789 /* Translate X to frame coordinates. Set last_x to the right
23790 end of the drawing area. */
23791 if (row->full_width_p)
23792 {
23793 /* X is relative to the left edge of W, without scroll bars
23794 or fringes. */
23795 area_left = WINDOW_LEFT_EDGE_X (w);
23796 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23797 }
23798 else
23799 {
23800 area_left = window_box_left (w, area);
23801 last_x = area_left + window_box_width (w, area);
23802 }
23803 x += area_left;
23804
23805 /* Build a doubly-linked list of glyph_string structures between
23806 head and tail from what we have to draw. Note that the macro
23807 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23808 the reason we use a separate variable `i'. */
23809 i = start;
23810 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23811 if (tail)
23812 x_reached = tail->x + tail->background_width;
23813 else
23814 x_reached = x;
23815
23816 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23817 the row, redraw some glyphs in front or following the glyph
23818 strings built above. */
23819 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23820 {
23821 struct glyph_string *h, *t;
23822 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23823 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23824 int check_mouse_face = 0;
23825 int dummy_x = 0;
23826
23827 /* If mouse highlighting is on, we may need to draw adjacent
23828 glyphs using mouse-face highlighting. */
23829 if (area == TEXT_AREA && row->mouse_face_p
23830 && hlinfo->mouse_face_beg_row >= 0
23831 && hlinfo->mouse_face_end_row >= 0)
23832 {
23833 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
23834
23835 if (row_vpos >= hlinfo->mouse_face_beg_row
23836 && row_vpos <= hlinfo->mouse_face_end_row)
23837 {
23838 check_mouse_face = 1;
23839 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
23840 ? hlinfo->mouse_face_beg_col : 0;
23841 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
23842 ? hlinfo->mouse_face_end_col
23843 : row->used[TEXT_AREA];
23844 }
23845 }
23846
23847 /* Compute overhangs for all glyph strings. */
23848 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23849 for (s = head; s; s = s->next)
23850 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23851
23852 /* Prepend glyph strings for glyphs in front of the first glyph
23853 string that are overwritten because of the first glyph
23854 string's left overhang. The background of all strings
23855 prepended must be drawn because the first glyph string
23856 draws over it. */
23857 i = left_overwritten (head);
23858 if (i >= 0)
23859 {
23860 enum draw_glyphs_face overlap_hl;
23861
23862 /* If this row contains mouse highlighting, attempt to draw
23863 the overlapped glyphs with the correct highlight. This
23864 code fails if the overlap encompasses more than one glyph
23865 and mouse-highlight spans only some of these glyphs.
23866 However, making it work perfectly involves a lot more
23867 code, and I don't know if the pathological case occurs in
23868 practice, so we'll stick to this for now. --- cyd */
23869 if (check_mouse_face
23870 && mouse_beg_col < start && mouse_end_col > i)
23871 overlap_hl = DRAW_MOUSE_FACE;
23872 else
23873 overlap_hl = DRAW_NORMAL_TEXT;
23874
23875 j = i;
23876 BUILD_GLYPH_STRINGS (j, start, h, t,
23877 overlap_hl, dummy_x, last_x);
23878 start = i;
23879 compute_overhangs_and_x (t, head->x, 1);
23880 prepend_glyph_string_lists (&head, &tail, h, t);
23881 clip_head = head;
23882 }
23883
23884 /* Prepend glyph strings for glyphs in front of the first glyph
23885 string that overwrite that glyph string because of their
23886 right overhang. For these strings, only the foreground must
23887 be drawn, because it draws over the glyph string at `head'.
23888 The background must not be drawn because this would overwrite
23889 right overhangs of preceding glyphs for which no glyph
23890 strings exist. */
23891 i = left_overwriting (head);
23892 if (i >= 0)
23893 {
23894 enum draw_glyphs_face overlap_hl;
23895
23896 if (check_mouse_face
23897 && mouse_beg_col < start && mouse_end_col > i)
23898 overlap_hl = DRAW_MOUSE_FACE;
23899 else
23900 overlap_hl = DRAW_NORMAL_TEXT;
23901
23902 clip_head = head;
23903 BUILD_GLYPH_STRINGS (i, start, h, t,
23904 overlap_hl, dummy_x, last_x);
23905 for (s = h; s; s = s->next)
23906 s->background_filled_p = 1;
23907 compute_overhangs_and_x (t, head->x, 1);
23908 prepend_glyph_string_lists (&head, &tail, h, t);
23909 }
23910
23911 /* Append glyphs strings for glyphs following the last glyph
23912 string tail that are overwritten by tail. The background of
23913 these strings has to be drawn because tail's foreground draws
23914 over it. */
23915 i = right_overwritten (tail);
23916 if (i >= 0)
23917 {
23918 enum draw_glyphs_face overlap_hl;
23919
23920 if (check_mouse_face
23921 && mouse_beg_col < i && mouse_end_col > end)
23922 overlap_hl = DRAW_MOUSE_FACE;
23923 else
23924 overlap_hl = DRAW_NORMAL_TEXT;
23925
23926 BUILD_GLYPH_STRINGS (end, i, h, t,
23927 overlap_hl, x, last_x);
23928 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23929 we don't have `end = i;' here. */
23930 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23931 append_glyph_string_lists (&head, &tail, h, t);
23932 clip_tail = tail;
23933 }
23934
23935 /* Append glyph strings for glyphs following the last glyph
23936 string tail that overwrite tail. The foreground of such
23937 glyphs has to be drawn because it writes into the background
23938 of tail. The background must not be drawn because it could
23939 paint over the foreground of following glyphs. */
23940 i = right_overwriting (tail);
23941 if (i >= 0)
23942 {
23943 enum draw_glyphs_face overlap_hl;
23944 if (check_mouse_face
23945 && mouse_beg_col < i && mouse_end_col > end)
23946 overlap_hl = DRAW_MOUSE_FACE;
23947 else
23948 overlap_hl = DRAW_NORMAL_TEXT;
23949
23950 clip_tail = tail;
23951 i++; /* We must include the Ith glyph. */
23952 BUILD_GLYPH_STRINGS (end, i, h, t,
23953 overlap_hl, x, last_x);
23954 for (s = h; s; s = s->next)
23955 s->background_filled_p = 1;
23956 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23957 append_glyph_string_lists (&head, &tail, h, t);
23958 }
23959 if (clip_head || clip_tail)
23960 for (s = head; s; s = s->next)
23961 {
23962 s->clip_head = clip_head;
23963 s->clip_tail = clip_tail;
23964 }
23965 }
23966
23967 /* Draw all strings. */
23968 for (s = head; s; s = s->next)
23969 FRAME_RIF (f)->draw_glyph_string (s);
23970
23971 #ifndef HAVE_NS
23972 /* When focus a sole frame and move horizontally, this sets on_p to 0
23973 causing a failure to erase prev cursor position. */
23974 if (area == TEXT_AREA
23975 && !row->full_width_p
23976 /* When drawing overlapping rows, only the glyph strings'
23977 foreground is drawn, which doesn't erase a cursor
23978 completely. */
23979 && !overlaps)
23980 {
23981 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23982 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23983 : (tail ? tail->x + tail->background_width : x));
23984 x0 -= area_left;
23985 x1 -= area_left;
23986
23987 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23988 row->y, MATRIX_ROW_BOTTOM_Y (row));
23989 }
23990 #endif
23991
23992 /* Value is the x-position up to which drawn, relative to AREA of W.
23993 This doesn't include parts drawn because of overhangs. */
23994 if (row->full_width_p)
23995 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23996 else
23997 x_reached -= area_left;
23998
23999 RELEASE_HDC (hdc, f);
24000
24001 return x_reached;
24002 }
24003
24004 /* Expand row matrix if too narrow. Don't expand if area
24005 is not present. */
24006
24007 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
24008 { \
24009 if (!fonts_changed_p \
24010 && (it->glyph_row->glyphs[area] \
24011 < it->glyph_row->glyphs[area + 1])) \
24012 { \
24013 it->w->ncols_scale_factor++; \
24014 fonts_changed_p = 1; \
24015 } \
24016 }
24017
24018 /* Store one glyph for IT->char_to_display in IT->glyph_row.
24019 Called from x_produce_glyphs when IT->glyph_row is non-null. */
24020
24021 static void
24022 append_glyph (struct it *it)
24023 {
24024 struct glyph *glyph;
24025 enum glyph_row_area area = it->area;
24026
24027 eassert (it->glyph_row);
24028 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
24029
24030 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24031 if (glyph < it->glyph_row->glyphs[area + 1])
24032 {
24033 /* If the glyph row is reversed, we need to prepend the glyph
24034 rather than append it. */
24035 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24036 {
24037 struct glyph *g;
24038
24039 /* Make room for the additional glyph. */
24040 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24041 g[1] = *g;
24042 glyph = it->glyph_row->glyphs[area];
24043 }
24044 glyph->charpos = CHARPOS (it->position);
24045 glyph->object = it->object;
24046 if (it->pixel_width > 0)
24047 {
24048 glyph->pixel_width = it->pixel_width;
24049 glyph->padding_p = 0;
24050 }
24051 else
24052 {
24053 /* Assure at least 1-pixel width. Otherwise, cursor can't
24054 be displayed correctly. */
24055 glyph->pixel_width = 1;
24056 glyph->padding_p = 1;
24057 }
24058 glyph->ascent = it->ascent;
24059 glyph->descent = it->descent;
24060 glyph->voffset = it->voffset;
24061 glyph->type = CHAR_GLYPH;
24062 glyph->avoid_cursor_p = it->avoid_cursor_p;
24063 glyph->multibyte_p = it->multibyte_p;
24064 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24065 {
24066 /* In R2L rows, the left and the right box edges need to be
24067 drawn in reverse direction. */
24068 glyph->right_box_line_p = it->start_of_box_run_p;
24069 glyph->left_box_line_p = it->end_of_box_run_p;
24070 }
24071 else
24072 {
24073 glyph->left_box_line_p = it->start_of_box_run_p;
24074 glyph->right_box_line_p = it->end_of_box_run_p;
24075 }
24076 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24077 || it->phys_descent > it->descent);
24078 glyph->glyph_not_available_p = it->glyph_not_available_p;
24079 glyph->face_id = it->face_id;
24080 glyph->u.ch = it->char_to_display;
24081 glyph->slice.img = null_glyph_slice;
24082 glyph->font_type = FONT_TYPE_UNKNOWN;
24083 if (it->bidi_p)
24084 {
24085 glyph->resolved_level = it->bidi_it.resolved_level;
24086 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24087 emacs_abort ();
24088 glyph->bidi_type = it->bidi_it.type;
24089 }
24090 else
24091 {
24092 glyph->resolved_level = 0;
24093 glyph->bidi_type = UNKNOWN_BT;
24094 }
24095 ++it->glyph_row->used[area];
24096 }
24097 else
24098 IT_EXPAND_MATRIX_WIDTH (it, area);
24099 }
24100
24101 /* Store one glyph for the composition IT->cmp_it.id in
24102 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
24103 non-null. */
24104
24105 static void
24106 append_composite_glyph (struct it *it)
24107 {
24108 struct glyph *glyph;
24109 enum glyph_row_area area = it->area;
24110
24111 eassert (it->glyph_row);
24112
24113 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24114 if (glyph < it->glyph_row->glyphs[area + 1])
24115 {
24116 /* If the glyph row is reversed, we need to prepend the glyph
24117 rather than append it. */
24118 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
24119 {
24120 struct glyph *g;
24121
24122 /* Make room for the new glyph. */
24123 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
24124 g[1] = *g;
24125 glyph = it->glyph_row->glyphs[it->area];
24126 }
24127 glyph->charpos = it->cmp_it.charpos;
24128 glyph->object = it->object;
24129 glyph->pixel_width = it->pixel_width;
24130 glyph->ascent = it->ascent;
24131 glyph->descent = it->descent;
24132 glyph->voffset = it->voffset;
24133 glyph->type = COMPOSITE_GLYPH;
24134 if (it->cmp_it.ch < 0)
24135 {
24136 glyph->u.cmp.automatic = 0;
24137 glyph->u.cmp.id = it->cmp_it.id;
24138 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
24139 }
24140 else
24141 {
24142 glyph->u.cmp.automatic = 1;
24143 glyph->u.cmp.id = it->cmp_it.id;
24144 glyph->slice.cmp.from = it->cmp_it.from;
24145 glyph->slice.cmp.to = it->cmp_it.to - 1;
24146 }
24147 glyph->avoid_cursor_p = it->avoid_cursor_p;
24148 glyph->multibyte_p = it->multibyte_p;
24149 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24150 {
24151 /* In R2L rows, the left and the right box edges need to be
24152 drawn in reverse direction. */
24153 glyph->right_box_line_p = it->start_of_box_run_p;
24154 glyph->left_box_line_p = it->end_of_box_run_p;
24155 }
24156 else
24157 {
24158 glyph->left_box_line_p = it->start_of_box_run_p;
24159 glyph->right_box_line_p = it->end_of_box_run_p;
24160 }
24161 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24162 || it->phys_descent > it->descent);
24163 glyph->padding_p = 0;
24164 glyph->glyph_not_available_p = 0;
24165 glyph->face_id = it->face_id;
24166 glyph->font_type = FONT_TYPE_UNKNOWN;
24167 if (it->bidi_p)
24168 {
24169 glyph->resolved_level = it->bidi_it.resolved_level;
24170 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24171 emacs_abort ();
24172 glyph->bidi_type = it->bidi_it.type;
24173 }
24174 ++it->glyph_row->used[area];
24175 }
24176 else
24177 IT_EXPAND_MATRIX_WIDTH (it, area);
24178 }
24179
24180
24181 /* Change IT->ascent and IT->height according to the setting of
24182 IT->voffset. */
24183
24184 static void
24185 take_vertical_position_into_account (struct it *it)
24186 {
24187 if (it->voffset)
24188 {
24189 if (it->voffset < 0)
24190 /* Increase the ascent so that we can display the text higher
24191 in the line. */
24192 it->ascent -= it->voffset;
24193 else
24194 /* Increase the descent so that we can display the text lower
24195 in the line. */
24196 it->descent += it->voffset;
24197 }
24198 }
24199
24200
24201 /* Produce glyphs/get display metrics for the image IT is loaded with.
24202 See the description of struct display_iterator in dispextern.h for
24203 an overview of struct display_iterator. */
24204
24205 static void
24206 produce_image_glyph (struct it *it)
24207 {
24208 struct image *img;
24209 struct face *face;
24210 int glyph_ascent, crop;
24211 struct glyph_slice slice;
24212
24213 eassert (it->what == IT_IMAGE);
24214
24215 face = FACE_FROM_ID (it->f, it->face_id);
24216 eassert (face);
24217 /* Make sure X resources of the face is loaded. */
24218 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24219
24220 if (it->image_id < 0)
24221 {
24222 /* Fringe bitmap. */
24223 it->ascent = it->phys_ascent = 0;
24224 it->descent = it->phys_descent = 0;
24225 it->pixel_width = 0;
24226 it->nglyphs = 0;
24227 return;
24228 }
24229
24230 img = IMAGE_FROM_ID (it->f, it->image_id);
24231 eassert (img);
24232 /* Make sure X resources of the image is loaded. */
24233 prepare_image_for_display (it->f, img);
24234
24235 slice.x = slice.y = 0;
24236 slice.width = img->width;
24237 slice.height = img->height;
24238
24239 if (INTEGERP (it->slice.x))
24240 slice.x = XINT (it->slice.x);
24241 else if (FLOATP (it->slice.x))
24242 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
24243
24244 if (INTEGERP (it->slice.y))
24245 slice.y = XINT (it->slice.y);
24246 else if (FLOATP (it->slice.y))
24247 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
24248
24249 if (INTEGERP (it->slice.width))
24250 slice.width = XINT (it->slice.width);
24251 else if (FLOATP (it->slice.width))
24252 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
24253
24254 if (INTEGERP (it->slice.height))
24255 slice.height = XINT (it->slice.height);
24256 else if (FLOATP (it->slice.height))
24257 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
24258
24259 if (slice.x >= img->width)
24260 slice.x = img->width;
24261 if (slice.y >= img->height)
24262 slice.y = img->height;
24263 if (slice.x + slice.width >= img->width)
24264 slice.width = img->width - slice.x;
24265 if (slice.y + slice.height > img->height)
24266 slice.height = img->height - slice.y;
24267
24268 if (slice.width == 0 || slice.height == 0)
24269 return;
24270
24271 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
24272
24273 it->descent = slice.height - glyph_ascent;
24274 if (slice.y == 0)
24275 it->descent += img->vmargin;
24276 if (slice.y + slice.height == img->height)
24277 it->descent += img->vmargin;
24278 it->phys_descent = it->descent;
24279
24280 it->pixel_width = slice.width;
24281 if (slice.x == 0)
24282 it->pixel_width += img->hmargin;
24283 if (slice.x + slice.width == img->width)
24284 it->pixel_width += img->hmargin;
24285
24286 /* It's quite possible for images to have an ascent greater than
24287 their height, so don't get confused in that case. */
24288 if (it->descent < 0)
24289 it->descent = 0;
24290
24291 it->nglyphs = 1;
24292
24293 if (face->box != FACE_NO_BOX)
24294 {
24295 if (face->box_line_width > 0)
24296 {
24297 if (slice.y == 0)
24298 it->ascent += face->box_line_width;
24299 if (slice.y + slice.height == img->height)
24300 it->descent += face->box_line_width;
24301 }
24302
24303 if (it->start_of_box_run_p && slice.x == 0)
24304 it->pixel_width += eabs (face->box_line_width);
24305 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
24306 it->pixel_width += eabs (face->box_line_width);
24307 }
24308
24309 take_vertical_position_into_account (it);
24310
24311 /* Automatically crop wide image glyphs at right edge so we can
24312 draw the cursor on same display row. */
24313 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
24314 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
24315 {
24316 it->pixel_width -= crop;
24317 slice.width -= crop;
24318 }
24319
24320 if (it->glyph_row)
24321 {
24322 struct glyph *glyph;
24323 enum glyph_row_area area = it->area;
24324
24325 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24326 if (glyph < it->glyph_row->glyphs[area + 1])
24327 {
24328 glyph->charpos = CHARPOS (it->position);
24329 glyph->object = it->object;
24330 glyph->pixel_width = it->pixel_width;
24331 glyph->ascent = glyph_ascent;
24332 glyph->descent = it->descent;
24333 glyph->voffset = it->voffset;
24334 glyph->type = IMAGE_GLYPH;
24335 glyph->avoid_cursor_p = it->avoid_cursor_p;
24336 glyph->multibyte_p = it->multibyte_p;
24337 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24338 {
24339 /* In R2L rows, the left and the right box edges need to be
24340 drawn in reverse direction. */
24341 glyph->right_box_line_p = it->start_of_box_run_p;
24342 glyph->left_box_line_p = it->end_of_box_run_p;
24343 }
24344 else
24345 {
24346 glyph->left_box_line_p = it->start_of_box_run_p;
24347 glyph->right_box_line_p = it->end_of_box_run_p;
24348 }
24349 glyph->overlaps_vertically_p = 0;
24350 glyph->padding_p = 0;
24351 glyph->glyph_not_available_p = 0;
24352 glyph->face_id = it->face_id;
24353 glyph->u.img_id = img->id;
24354 glyph->slice.img = slice;
24355 glyph->font_type = FONT_TYPE_UNKNOWN;
24356 if (it->bidi_p)
24357 {
24358 glyph->resolved_level = it->bidi_it.resolved_level;
24359 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24360 emacs_abort ();
24361 glyph->bidi_type = it->bidi_it.type;
24362 }
24363 ++it->glyph_row->used[area];
24364 }
24365 else
24366 IT_EXPAND_MATRIX_WIDTH (it, area);
24367 }
24368 }
24369
24370
24371 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
24372 of the glyph, WIDTH and HEIGHT are the width and height of the
24373 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
24374
24375 static void
24376 append_stretch_glyph (struct it *it, Lisp_Object object,
24377 int width, int height, int ascent)
24378 {
24379 struct glyph *glyph;
24380 enum glyph_row_area area = it->area;
24381
24382 eassert (ascent >= 0 && ascent <= height);
24383
24384 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24385 if (glyph < it->glyph_row->glyphs[area + 1])
24386 {
24387 /* If the glyph row is reversed, we need to prepend the glyph
24388 rather than append it. */
24389 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24390 {
24391 struct glyph *g;
24392
24393 /* Make room for the additional glyph. */
24394 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24395 g[1] = *g;
24396 glyph = it->glyph_row->glyphs[area];
24397 }
24398 glyph->charpos = CHARPOS (it->position);
24399 glyph->object = object;
24400 glyph->pixel_width = width;
24401 glyph->ascent = ascent;
24402 glyph->descent = height - ascent;
24403 glyph->voffset = it->voffset;
24404 glyph->type = STRETCH_GLYPH;
24405 glyph->avoid_cursor_p = it->avoid_cursor_p;
24406 glyph->multibyte_p = it->multibyte_p;
24407 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24408 {
24409 /* In R2L rows, the left and the right box edges need to be
24410 drawn in reverse direction. */
24411 glyph->right_box_line_p = it->start_of_box_run_p;
24412 glyph->left_box_line_p = it->end_of_box_run_p;
24413 }
24414 else
24415 {
24416 glyph->left_box_line_p = it->start_of_box_run_p;
24417 glyph->right_box_line_p = it->end_of_box_run_p;
24418 }
24419 glyph->overlaps_vertically_p = 0;
24420 glyph->padding_p = 0;
24421 glyph->glyph_not_available_p = 0;
24422 glyph->face_id = it->face_id;
24423 glyph->u.stretch.ascent = ascent;
24424 glyph->u.stretch.height = height;
24425 glyph->slice.img = null_glyph_slice;
24426 glyph->font_type = FONT_TYPE_UNKNOWN;
24427 if (it->bidi_p)
24428 {
24429 glyph->resolved_level = it->bidi_it.resolved_level;
24430 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24431 emacs_abort ();
24432 glyph->bidi_type = it->bidi_it.type;
24433 }
24434 else
24435 {
24436 glyph->resolved_level = 0;
24437 glyph->bidi_type = UNKNOWN_BT;
24438 }
24439 ++it->glyph_row->used[area];
24440 }
24441 else
24442 IT_EXPAND_MATRIX_WIDTH (it, area);
24443 }
24444
24445 #endif /* HAVE_WINDOW_SYSTEM */
24446
24447 /* Produce a stretch glyph for iterator IT. IT->object is the value
24448 of the glyph property displayed. The value must be a list
24449 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24450 being recognized:
24451
24452 1. `:width WIDTH' specifies that the space should be WIDTH *
24453 canonical char width wide. WIDTH may be an integer or floating
24454 point number.
24455
24456 2. `:relative-width FACTOR' specifies that the width of the stretch
24457 should be computed from the width of the first character having the
24458 `glyph' property, and should be FACTOR times that width.
24459
24460 3. `:align-to HPOS' specifies that the space should be wide enough
24461 to reach HPOS, a value in canonical character units.
24462
24463 Exactly one of the above pairs must be present.
24464
24465 4. `:height HEIGHT' specifies that the height of the stretch produced
24466 should be HEIGHT, measured in canonical character units.
24467
24468 5. `:relative-height FACTOR' specifies that the height of the
24469 stretch should be FACTOR times the height of the characters having
24470 the glyph property.
24471
24472 Either none or exactly one of 4 or 5 must be present.
24473
24474 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24475 of the stretch should be used for the ascent of the stretch.
24476 ASCENT must be in the range 0 <= ASCENT <= 100. */
24477
24478 void
24479 produce_stretch_glyph (struct it *it)
24480 {
24481 /* (space :width WIDTH :height HEIGHT ...) */
24482 Lisp_Object prop, plist;
24483 int width = 0, height = 0, align_to = -1;
24484 int zero_width_ok_p = 0;
24485 double tem;
24486 struct font *font = NULL;
24487
24488 #ifdef HAVE_WINDOW_SYSTEM
24489 int ascent = 0;
24490 int zero_height_ok_p = 0;
24491
24492 if (FRAME_WINDOW_P (it->f))
24493 {
24494 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24495 font = face->font ? face->font : FRAME_FONT (it->f);
24496 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24497 }
24498 #endif
24499
24500 /* List should start with `space'. */
24501 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24502 plist = XCDR (it->object);
24503
24504 /* Compute the width of the stretch. */
24505 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24506 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24507 {
24508 /* Absolute width `:width WIDTH' specified and valid. */
24509 zero_width_ok_p = 1;
24510 width = (int)tem;
24511 }
24512 #ifdef HAVE_WINDOW_SYSTEM
24513 else if (FRAME_WINDOW_P (it->f)
24514 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24515 {
24516 /* Relative width `:relative-width FACTOR' specified and valid.
24517 Compute the width of the characters having the `glyph'
24518 property. */
24519 struct it it2;
24520 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24521
24522 it2 = *it;
24523 if (it->multibyte_p)
24524 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24525 else
24526 {
24527 it2.c = it2.char_to_display = *p, it2.len = 1;
24528 if (! ASCII_CHAR_P (it2.c))
24529 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24530 }
24531
24532 it2.glyph_row = NULL;
24533 it2.what = IT_CHARACTER;
24534 x_produce_glyphs (&it2);
24535 width = NUMVAL (prop) * it2.pixel_width;
24536 }
24537 #endif /* HAVE_WINDOW_SYSTEM */
24538 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24539 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24540 {
24541 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24542 align_to = (align_to < 0
24543 ? 0
24544 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24545 else if (align_to < 0)
24546 align_to = window_box_left_offset (it->w, TEXT_AREA);
24547 width = max (0, (int)tem + align_to - it->current_x);
24548 zero_width_ok_p = 1;
24549 }
24550 else
24551 /* Nothing specified -> width defaults to canonical char width. */
24552 width = FRAME_COLUMN_WIDTH (it->f);
24553
24554 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24555 width = 1;
24556
24557 #ifdef HAVE_WINDOW_SYSTEM
24558 /* Compute height. */
24559 if (FRAME_WINDOW_P (it->f))
24560 {
24561 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24562 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24563 {
24564 height = (int)tem;
24565 zero_height_ok_p = 1;
24566 }
24567 else if (prop = Fplist_get (plist, QCrelative_height),
24568 NUMVAL (prop) > 0)
24569 height = FONT_HEIGHT (font) * NUMVAL (prop);
24570 else
24571 height = FONT_HEIGHT (font);
24572
24573 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24574 height = 1;
24575
24576 /* Compute percentage of height used for ascent. If
24577 `:ascent ASCENT' is present and valid, use that. Otherwise,
24578 derive the ascent from the font in use. */
24579 if (prop = Fplist_get (plist, QCascent),
24580 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24581 ascent = height * NUMVAL (prop) / 100.0;
24582 else if (!NILP (prop)
24583 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24584 ascent = min (max (0, (int)tem), height);
24585 else
24586 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24587 }
24588 else
24589 #endif /* HAVE_WINDOW_SYSTEM */
24590 height = 1;
24591
24592 if (width > 0 && it->line_wrap != TRUNCATE
24593 && it->current_x + width > it->last_visible_x)
24594 {
24595 width = it->last_visible_x - it->current_x;
24596 #ifdef HAVE_WINDOW_SYSTEM
24597 /* Subtract one more pixel from the stretch width, but only on
24598 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24599 width -= FRAME_WINDOW_P (it->f);
24600 #endif
24601 }
24602
24603 if (width > 0 && height > 0 && it->glyph_row)
24604 {
24605 Lisp_Object o_object = it->object;
24606 Lisp_Object object = it->stack[it->sp - 1].string;
24607 int n = width;
24608
24609 if (!STRINGP (object))
24610 object = it->w->contents;
24611 #ifdef HAVE_WINDOW_SYSTEM
24612 if (FRAME_WINDOW_P (it->f))
24613 append_stretch_glyph (it, object, width, height, ascent);
24614 else
24615 #endif
24616 {
24617 it->object = object;
24618 it->char_to_display = ' ';
24619 it->pixel_width = it->len = 1;
24620 while (n--)
24621 tty_append_glyph (it);
24622 it->object = o_object;
24623 }
24624 }
24625
24626 it->pixel_width = width;
24627 #ifdef HAVE_WINDOW_SYSTEM
24628 if (FRAME_WINDOW_P (it->f))
24629 {
24630 it->ascent = it->phys_ascent = ascent;
24631 it->descent = it->phys_descent = height - it->ascent;
24632 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24633 take_vertical_position_into_account (it);
24634 }
24635 else
24636 #endif
24637 it->nglyphs = width;
24638 }
24639
24640 /* Get information about special display element WHAT in an
24641 environment described by IT. WHAT is one of IT_TRUNCATION or
24642 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24643 non-null glyph_row member. This function ensures that fields like
24644 face_id, c, len of IT are left untouched. */
24645
24646 static void
24647 produce_special_glyphs (struct it *it, enum display_element_type what)
24648 {
24649 struct it temp_it;
24650 Lisp_Object gc;
24651 GLYPH glyph;
24652
24653 temp_it = *it;
24654 temp_it.object = make_number (0);
24655 memset (&temp_it.current, 0, sizeof temp_it.current);
24656
24657 if (what == IT_CONTINUATION)
24658 {
24659 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24660 if (it->bidi_it.paragraph_dir == R2L)
24661 SET_GLYPH_FROM_CHAR (glyph, '/');
24662 else
24663 SET_GLYPH_FROM_CHAR (glyph, '\\');
24664 if (it->dp
24665 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24666 {
24667 /* FIXME: Should we mirror GC for R2L lines? */
24668 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24669 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24670 }
24671 }
24672 else if (what == IT_TRUNCATION)
24673 {
24674 /* Truncation glyph. */
24675 SET_GLYPH_FROM_CHAR (glyph, '$');
24676 if (it->dp
24677 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24678 {
24679 /* FIXME: Should we mirror GC for R2L lines? */
24680 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24681 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24682 }
24683 }
24684 else
24685 emacs_abort ();
24686
24687 #ifdef HAVE_WINDOW_SYSTEM
24688 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24689 is turned off, we precede the truncation/continuation glyphs by a
24690 stretch glyph whose width is computed such that these special
24691 glyphs are aligned at the window margin, even when very different
24692 fonts are used in different glyph rows. */
24693 if (FRAME_WINDOW_P (temp_it.f)
24694 /* init_iterator calls this with it->glyph_row == NULL, and it
24695 wants only the pixel width of the truncation/continuation
24696 glyphs. */
24697 && temp_it.glyph_row
24698 /* insert_left_trunc_glyphs calls us at the beginning of the
24699 row, and it has its own calculation of the stretch glyph
24700 width. */
24701 && temp_it.glyph_row->used[TEXT_AREA] > 0
24702 && (temp_it.glyph_row->reversed_p
24703 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24704 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24705 {
24706 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24707
24708 if (stretch_width > 0)
24709 {
24710 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24711 struct font *font =
24712 face->font ? face->font : FRAME_FONT (temp_it.f);
24713 int stretch_ascent =
24714 (((temp_it.ascent + temp_it.descent)
24715 * FONT_BASE (font)) / FONT_HEIGHT (font));
24716
24717 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
24718 temp_it.ascent + temp_it.descent,
24719 stretch_ascent);
24720 }
24721 }
24722 #endif
24723
24724 temp_it.dp = NULL;
24725 temp_it.what = IT_CHARACTER;
24726 temp_it.len = 1;
24727 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24728 temp_it.face_id = GLYPH_FACE (glyph);
24729 temp_it.len = CHAR_BYTES (temp_it.c);
24730
24731 PRODUCE_GLYPHS (&temp_it);
24732 it->pixel_width = temp_it.pixel_width;
24733 it->nglyphs = temp_it.pixel_width;
24734 }
24735
24736 #ifdef HAVE_WINDOW_SYSTEM
24737
24738 /* Calculate line-height and line-spacing properties.
24739 An integer value specifies explicit pixel value.
24740 A float value specifies relative value to current face height.
24741 A cons (float . face-name) specifies relative value to
24742 height of specified face font.
24743
24744 Returns height in pixels, or nil. */
24745
24746
24747 static Lisp_Object
24748 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24749 int boff, int override)
24750 {
24751 Lisp_Object face_name = Qnil;
24752 int ascent, descent, height;
24753
24754 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24755 return val;
24756
24757 if (CONSP (val))
24758 {
24759 face_name = XCAR (val);
24760 val = XCDR (val);
24761 if (!NUMBERP (val))
24762 val = make_number (1);
24763 if (NILP (face_name))
24764 {
24765 height = it->ascent + it->descent;
24766 goto scale;
24767 }
24768 }
24769
24770 if (NILP (face_name))
24771 {
24772 font = FRAME_FONT (it->f);
24773 boff = FRAME_BASELINE_OFFSET (it->f);
24774 }
24775 else if (EQ (face_name, Qt))
24776 {
24777 override = 0;
24778 }
24779 else
24780 {
24781 int face_id;
24782 struct face *face;
24783
24784 face_id = lookup_named_face (it->f, face_name, 0);
24785 if (face_id < 0)
24786 return make_number (-1);
24787
24788 face = FACE_FROM_ID (it->f, face_id);
24789 font = face->font;
24790 if (font == NULL)
24791 return make_number (-1);
24792 boff = font->baseline_offset;
24793 if (font->vertical_centering)
24794 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24795 }
24796
24797 ascent = FONT_BASE (font) + boff;
24798 descent = FONT_DESCENT (font) - boff;
24799
24800 if (override)
24801 {
24802 it->override_ascent = ascent;
24803 it->override_descent = descent;
24804 it->override_boff = boff;
24805 }
24806
24807 height = ascent + descent;
24808
24809 scale:
24810 if (FLOATP (val))
24811 height = (int)(XFLOAT_DATA (val) * height);
24812 else if (INTEGERP (val))
24813 height *= XINT (val);
24814
24815 return make_number (height);
24816 }
24817
24818
24819 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24820 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24821 and only if this is for a character for which no font was found.
24822
24823 If the display method (it->glyphless_method) is
24824 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24825 length of the acronym or the hexadecimal string, UPPER_XOFF and
24826 UPPER_YOFF are pixel offsets for the upper part of the string,
24827 LOWER_XOFF and LOWER_YOFF are for the lower part.
24828
24829 For the other display methods, LEN through LOWER_YOFF are zero. */
24830
24831 static void
24832 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24833 short upper_xoff, short upper_yoff,
24834 short lower_xoff, short lower_yoff)
24835 {
24836 struct glyph *glyph;
24837 enum glyph_row_area area = it->area;
24838
24839 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24840 if (glyph < it->glyph_row->glyphs[area + 1])
24841 {
24842 /* If the glyph row is reversed, we need to prepend the glyph
24843 rather than append it. */
24844 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24845 {
24846 struct glyph *g;
24847
24848 /* Make room for the additional glyph. */
24849 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24850 g[1] = *g;
24851 glyph = it->glyph_row->glyphs[area];
24852 }
24853 glyph->charpos = CHARPOS (it->position);
24854 glyph->object = it->object;
24855 glyph->pixel_width = it->pixel_width;
24856 glyph->ascent = it->ascent;
24857 glyph->descent = it->descent;
24858 glyph->voffset = it->voffset;
24859 glyph->type = GLYPHLESS_GLYPH;
24860 glyph->u.glyphless.method = it->glyphless_method;
24861 glyph->u.glyphless.for_no_font = for_no_font;
24862 glyph->u.glyphless.len = len;
24863 glyph->u.glyphless.ch = it->c;
24864 glyph->slice.glyphless.upper_xoff = upper_xoff;
24865 glyph->slice.glyphless.upper_yoff = upper_yoff;
24866 glyph->slice.glyphless.lower_xoff = lower_xoff;
24867 glyph->slice.glyphless.lower_yoff = lower_yoff;
24868 glyph->avoid_cursor_p = it->avoid_cursor_p;
24869 glyph->multibyte_p = it->multibyte_p;
24870 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24871 {
24872 /* In R2L rows, the left and the right box edges need to be
24873 drawn in reverse direction. */
24874 glyph->right_box_line_p = it->start_of_box_run_p;
24875 glyph->left_box_line_p = it->end_of_box_run_p;
24876 }
24877 else
24878 {
24879 glyph->left_box_line_p = it->start_of_box_run_p;
24880 glyph->right_box_line_p = it->end_of_box_run_p;
24881 }
24882 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24883 || it->phys_descent > it->descent);
24884 glyph->padding_p = 0;
24885 glyph->glyph_not_available_p = 0;
24886 glyph->face_id = face_id;
24887 glyph->font_type = FONT_TYPE_UNKNOWN;
24888 if (it->bidi_p)
24889 {
24890 glyph->resolved_level = it->bidi_it.resolved_level;
24891 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24892 emacs_abort ();
24893 glyph->bidi_type = it->bidi_it.type;
24894 }
24895 ++it->glyph_row->used[area];
24896 }
24897 else
24898 IT_EXPAND_MATRIX_WIDTH (it, area);
24899 }
24900
24901
24902 /* Produce a glyph for a glyphless character for iterator IT.
24903 IT->glyphless_method specifies which method to use for displaying
24904 the character. See the description of enum
24905 glyphless_display_method in dispextern.h for the detail.
24906
24907 FOR_NO_FONT is nonzero if and only if this is for a character for
24908 which no font was found. ACRONYM, if non-nil, is an acronym string
24909 for the character. */
24910
24911 static void
24912 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24913 {
24914 int face_id;
24915 struct face *face;
24916 struct font *font;
24917 int base_width, base_height, width, height;
24918 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24919 int len;
24920
24921 /* Get the metrics of the base font. We always refer to the current
24922 ASCII face. */
24923 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24924 font = face->font ? face->font : FRAME_FONT (it->f);
24925 it->ascent = FONT_BASE (font) + font->baseline_offset;
24926 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24927 base_height = it->ascent + it->descent;
24928 base_width = font->average_width;
24929
24930 /* Get a face ID for the glyph by utilizing a cache (the same way as
24931 done for `escape-glyph' in get_next_display_element). */
24932 if (it->f == last_glyphless_glyph_frame
24933 && it->face_id == last_glyphless_glyph_face_id)
24934 {
24935 face_id = last_glyphless_glyph_merged_face_id;
24936 }
24937 else
24938 {
24939 /* Merge the `glyphless-char' face into the current face. */
24940 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
24941 last_glyphless_glyph_frame = it->f;
24942 last_glyphless_glyph_face_id = it->face_id;
24943 last_glyphless_glyph_merged_face_id = face_id;
24944 }
24945
24946 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24947 {
24948 it->pixel_width = THIN_SPACE_WIDTH;
24949 len = 0;
24950 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24951 }
24952 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24953 {
24954 width = CHAR_WIDTH (it->c);
24955 if (width == 0)
24956 width = 1;
24957 else if (width > 4)
24958 width = 4;
24959 it->pixel_width = base_width * width;
24960 len = 0;
24961 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24962 }
24963 else
24964 {
24965 char buf[7];
24966 const char *str;
24967 unsigned int code[6];
24968 int upper_len;
24969 int ascent, descent;
24970 struct font_metrics metrics_upper, metrics_lower;
24971
24972 face = FACE_FROM_ID (it->f, face_id);
24973 font = face->font ? face->font : FRAME_FONT (it->f);
24974 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24975
24976 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24977 {
24978 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
24979 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
24980 if (CONSP (acronym))
24981 acronym = XCAR (acronym);
24982 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24983 }
24984 else
24985 {
24986 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
24987 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
24988 str = buf;
24989 }
24990 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
24991 code[len] = font->driver->encode_char (font, str[len]);
24992 upper_len = (len + 1) / 2;
24993 font->driver->text_extents (font, code, upper_len,
24994 &metrics_upper);
24995 font->driver->text_extents (font, code + upper_len, len - upper_len,
24996 &metrics_lower);
24997
24998
24999
25000 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
25001 width = max (metrics_upper.width, metrics_lower.width) + 4;
25002 upper_xoff = upper_yoff = 2; /* the typical case */
25003 if (base_width >= width)
25004 {
25005 /* Align the upper to the left, the lower to the right. */
25006 it->pixel_width = base_width;
25007 lower_xoff = base_width - 2 - metrics_lower.width;
25008 }
25009 else
25010 {
25011 /* Center the shorter one. */
25012 it->pixel_width = width;
25013 if (metrics_upper.width >= metrics_lower.width)
25014 lower_xoff = (width - metrics_lower.width) / 2;
25015 else
25016 {
25017 /* FIXME: This code doesn't look right. It formerly was
25018 missing the "lower_xoff = 0;", which couldn't have
25019 been right since it left lower_xoff uninitialized. */
25020 lower_xoff = 0;
25021 upper_xoff = (width - metrics_upper.width) / 2;
25022 }
25023 }
25024
25025 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
25026 top, bottom, and between upper and lower strings. */
25027 height = (metrics_upper.ascent + metrics_upper.descent
25028 + metrics_lower.ascent + metrics_lower.descent) + 5;
25029 /* Center vertically.
25030 H:base_height, D:base_descent
25031 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
25032
25033 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
25034 descent = D - H/2 + h/2;
25035 lower_yoff = descent - 2 - ld;
25036 upper_yoff = lower_yoff - la - 1 - ud; */
25037 ascent = - (it->descent - (base_height + height + 1) / 2);
25038 descent = it->descent - (base_height - height) / 2;
25039 lower_yoff = descent - 2 - metrics_lower.descent;
25040 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
25041 - metrics_upper.descent);
25042 /* Don't make the height shorter than the base height. */
25043 if (height > base_height)
25044 {
25045 it->ascent = ascent;
25046 it->descent = descent;
25047 }
25048 }
25049
25050 it->phys_ascent = it->ascent;
25051 it->phys_descent = it->descent;
25052 if (it->glyph_row)
25053 append_glyphless_glyph (it, face_id, for_no_font, len,
25054 upper_xoff, upper_yoff,
25055 lower_xoff, lower_yoff);
25056 it->nglyphs = 1;
25057 take_vertical_position_into_account (it);
25058 }
25059
25060
25061 /* RIF:
25062 Produce glyphs/get display metrics for the display element IT is
25063 loaded with. See the description of struct it in dispextern.h
25064 for an overview of struct it. */
25065
25066 void
25067 x_produce_glyphs (struct it *it)
25068 {
25069 int extra_line_spacing = it->extra_line_spacing;
25070
25071 it->glyph_not_available_p = 0;
25072
25073 if (it->what == IT_CHARACTER)
25074 {
25075 XChar2b char2b;
25076 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25077 struct font *font = face->font;
25078 struct font_metrics *pcm = NULL;
25079 int boff; /* baseline offset */
25080
25081 if (font == NULL)
25082 {
25083 /* When no suitable font is found, display this character by
25084 the method specified in the first extra slot of
25085 Vglyphless_char_display. */
25086 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
25087
25088 eassert (it->what == IT_GLYPHLESS);
25089 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
25090 goto done;
25091 }
25092
25093 boff = font->baseline_offset;
25094 if (font->vertical_centering)
25095 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25096
25097 if (it->char_to_display != '\n' && it->char_to_display != '\t')
25098 {
25099 int stretched_p;
25100
25101 it->nglyphs = 1;
25102
25103 if (it->override_ascent >= 0)
25104 {
25105 it->ascent = it->override_ascent;
25106 it->descent = it->override_descent;
25107 boff = it->override_boff;
25108 }
25109 else
25110 {
25111 it->ascent = FONT_BASE (font) + boff;
25112 it->descent = FONT_DESCENT (font) - boff;
25113 }
25114
25115 if (get_char_glyph_code (it->char_to_display, font, &char2b))
25116 {
25117 pcm = get_per_char_metric (font, &char2b);
25118 if (pcm->width == 0
25119 && pcm->rbearing == 0 && pcm->lbearing == 0)
25120 pcm = NULL;
25121 }
25122
25123 if (pcm)
25124 {
25125 it->phys_ascent = pcm->ascent + boff;
25126 it->phys_descent = pcm->descent - boff;
25127 it->pixel_width = pcm->width;
25128 }
25129 else
25130 {
25131 it->glyph_not_available_p = 1;
25132 it->phys_ascent = it->ascent;
25133 it->phys_descent = it->descent;
25134 it->pixel_width = font->space_width;
25135 }
25136
25137 if (it->constrain_row_ascent_descent_p)
25138 {
25139 if (it->descent > it->max_descent)
25140 {
25141 it->ascent += it->descent - it->max_descent;
25142 it->descent = it->max_descent;
25143 }
25144 if (it->ascent > it->max_ascent)
25145 {
25146 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25147 it->ascent = it->max_ascent;
25148 }
25149 it->phys_ascent = min (it->phys_ascent, it->ascent);
25150 it->phys_descent = min (it->phys_descent, it->descent);
25151 extra_line_spacing = 0;
25152 }
25153
25154 /* If this is a space inside a region of text with
25155 `space-width' property, change its width. */
25156 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
25157 if (stretched_p)
25158 it->pixel_width *= XFLOATINT (it->space_width);
25159
25160 /* If face has a box, add the box thickness to the character
25161 height. If character has a box line to the left and/or
25162 right, add the box line width to the character's width. */
25163 if (face->box != FACE_NO_BOX)
25164 {
25165 int thick = face->box_line_width;
25166
25167 if (thick > 0)
25168 {
25169 it->ascent += thick;
25170 it->descent += thick;
25171 }
25172 else
25173 thick = -thick;
25174
25175 if (it->start_of_box_run_p)
25176 it->pixel_width += thick;
25177 if (it->end_of_box_run_p)
25178 it->pixel_width += thick;
25179 }
25180
25181 /* If face has an overline, add the height of the overline
25182 (1 pixel) and a 1 pixel margin to the character height. */
25183 if (face->overline_p)
25184 it->ascent += overline_margin;
25185
25186 if (it->constrain_row_ascent_descent_p)
25187 {
25188 if (it->ascent > it->max_ascent)
25189 it->ascent = it->max_ascent;
25190 if (it->descent > it->max_descent)
25191 it->descent = it->max_descent;
25192 }
25193
25194 take_vertical_position_into_account (it);
25195
25196 /* If we have to actually produce glyphs, do it. */
25197 if (it->glyph_row)
25198 {
25199 if (stretched_p)
25200 {
25201 /* Translate a space with a `space-width' property
25202 into a stretch glyph. */
25203 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
25204 / FONT_HEIGHT (font));
25205 append_stretch_glyph (it, it->object, it->pixel_width,
25206 it->ascent + it->descent, ascent);
25207 }
25208 else
25209 append_glyph (it);
25210
25211 /* If characters with lbearing or rbearing are displayed
25212 in this line, record that fact in a flag of the
25213 glyph row. This is used to optimize X output code. */
25214 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
25215 it->glyph_row->contains_overlapping_glyphs_p = 1;
25216 }
25217 if (! stretched_p && it->pixel_width == 0)
25218 /* We assure that all visible glyphs have at least 1-pixel
25219 width. */
25220 it->pixel_width = 1;
25221 }
25222 else if (it->char_to_display == '\n')
25223 {
25224 /* A newline has no width, but we need the height of the
25225 line. But if previous part of the line sets a height,
25226 don't increase that height */
25227
25228 Lisp_Object height;
25229 Lisp_Object total_height = Qnil;
25230
25231 it->override_ascent = -1;
25232 it->pixel_width = 0;
25233 it->nglyphs = 0;
25234
25235 height = get_it_property (it, Qline_height);
25236 /* Split (line-height total-height) list */
25237 if (CONSP (height)
25238 && CONSP (XCDR (height))
25239 && NILP (XCDR (XCDR (height))))
25240 {
25241 total_height = XCAR (XCDR (height));
25242 height = XCAR (height);
25243 }
25244 height = calc_line_height_property (it, height, font, boff, 1);
25245
25246 if (it->override_ascent >= 0)
25247 {
25248 it->ascent = it->override_ascent;
25249 it->descent = it->override_descent;
25250 boff = it->override_boff;
25251 }
25252 else
25253 {
25254 it->ascent = FONT_BASE (font) + boff;
25255 it->descent = FONT_DESCENT (font) - boff;
25256 }
25257
25258 if (EQ (height, Qt))
25259 {
25260 if (it->descent > it->max_descent)
25261 {
25262 it->ascent += it->descent - it->max_descent;
25263 it->descent = it->max_descent;
25264 }
25265 if (it->ascent > it->max_ascent)
25266 {
25267 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25268 it->ascent = it->max_ascent;
25269 }
25270 it->phys_ascent = min (it->phys_ascent, it->ascent);
25271 it->phys_descent = min (it->phys_descent, it->descent);
25272 it->constrain_row_ascent_descent_p = 1;
25273 extra_line_spacing = 0;
25274 }
25275 else
25276 {
25277 Lisp_Object spacing;
25278
25279 it->phys_ascent = it->ascent;
25280 it->phys_descent = it->descent;
25281
25282 if ((it->max_ascent > 0 || it->max_descent > 0)
25283 && face->box != FACE_NO_BOX
25284 && face->box_line_width > 0)
25285 {
25286 it->ascent += face->box_line_width;
25287 it->descent += face->box_line_width;
25288 }
25289 if (!NILP (height)
25290 && XINT (height) > it->ascent + it->descent)
25291 it->ascent = XINT (height) - it->descent;
25292
25293 if (!NILP (total_height))
25294 spacing = calc_line_height_property (it, total_height, font, boff, 0);
25295 else
25296 {
25297 spacing = get_it_property (it, Qline_spacing);
25298 spacing = calc_line_height_property (it, spacing, font, boff, 0);
25299 }
25300 if (INTEGERP (spacing))
25301 {
25302 extra_line_spacing = XINT (spacing);
25303 if (!NILP (total_height))
25304 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
25305 }
25306 }
25307 }
25308 else /* i.e. (it->char_to_display == '\t') */
25309 {
25310 if (font->space_width > 0)
25311 {
25312 int tab_width = it->tab_width * font->space_width;
25313 int x = it->current_x + it->continuation_lines_width;
25314 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
25315
25316 /* If the distance from the current position to the next tab
25317 stop is less than a space character width, use the
25318 tab stop after that. */
25319 if (next_tab_x - x < font->space_width)
25320 next_tab_x += tab_width;
25321
25322 it->pixel_width = next_tab_x - x;
25323 it->nglyphs = 1;
25324 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
25325 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
25326
25327 if (it->glyph_row)
25328 {
25329 append_stretch_glyph (it, it->object, it->pixel_width,
25330 it->ascent + it->descent, it->ascent);
25331 }
25332 }
25333 else
25334 {
25335 it->pixel_width = 0;
25336 it->nglyphs = 1;
25337 }
25338 }
25339 }
25340 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
25341 {
25342 /* A static composition.
25343
25344 Note: A composition is represented as one glyph in the
25345 glyph matrix. There are no padding glyphs.
25346
25347 Important note: pixel_width, ascent, and descent are the
25348 values of what is drawn by draw_glyphs (i.e. the values of
25349 the overall glyphs composed). */
25350 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25351 int boff; /* baseline offset */
25352 struct composition *cmp = composition_table[it->cmp_it.id];
25353 int glyph_len = cmp->glyph_len;
25354 struct font *font = face->font;
25355
25356 it->nglyphs = 1;
25357
25358 /* If we have not yet calculated pixel size data of glyphs of
25359 the composition for the current face font, calculate them
25360 now. Theoretically, we have to check all fonts for the
25361 glyphs, but that requires much time and memory space. So,
25362 here we check only the font of the first glyph. This may
25363 lead to incorrect display, but it's very rare, and C-l
25364 (recenter-top-bottom) can correct the display anyway. */
25365 if (! cmp->font || cmp->font != font)
25366 {
25367 /* Ascent and descent of the font of the first character
25368 of this composition (adjusted by baseline offset).
25369 Ascent and descent of overall glyphs should not be less
25370 than these, respectively. */
25371 int font_ascent, font_descent, font_height;
25372 /* Bounding box of the overall glyphs. */
25373 int leftmost, rightmost, lowest, highest;
25374 int lbearing, rbearing;
25375 int i, width, ascent, descent;
25376 int left_padded = 0, right_padded = 0;
25377 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
25378 XChar2b char2b;
25379 struct font_metrics *pcm;
25380 int font_not_found_p;
25381 ptrdiff_t pos;
25382
25383 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
25384 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
25385 break;
25386 if (glyph_len < cmp->glyph_len)
25387 right_padded = 1;
25388 for (i = 0; i < glyph_len; i++)
25389 {
25390 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
25391 break;
25392 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25393 }
25394 if (i > 0)
25395 left_padded = 1;
25396
25397 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
25398 : IT_CHARPOS (*it));
25399 /* If no suitable font is found, use the default font. */
25400 font_not_found_p = font == NULL;
25401 if (font_not_found_p)
25402 {
25403 face = face->ascii_face;
25404 font = face->font;
25405 }
25406 boff = font->baseline_offset;
25407 if (font->vertical_centering)
25408 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25409 font_ascent = FONT_BASE (font) + boff;
25410 font_descent = FONT_DESCENT (font) - boff;
25411 font_height = FONT_HEIGHT (font);
25412
25413 cmp->font = font;
25414
25415 pcm = NULL;
25416 if (! font_not_found_p)
25417 {
25418 get_char_face_and_encoding (it->f, c, it->face_id,
25419 &char2b, 0);
25420 pcm = get_per_char_metric (font, &char2b);
25421 }
25422
25423 /* Initialize the bounding box. */
25424 if (pcm)
25425 {
25426 width = cmp->glyph_len > 0 ? pcm->width : 0;
25427 ascent = pcm->ascent;
25428 descent = pcm->descent;
25429 lbearing = pcm->lbearing;
25430 rbearing = pcm->rbearing;
25431 }
25432 else
25433 {
25434 width = cmp->glyph_len > 0 ? font->space_width : 0;
25435 ascent = FONT_BASE (font);
25436 descent = FONT_DESCENT (font);
25437 lbearing = 0;
25438 rbearing = width;
25439 }
25440
25441 rightmost = width;
25442 leftmost = 0;
25443 lowest = - descent + boff;
25444 highest = ascent + boff;
25445
25446 if (! font_not_found_p
25447 && font->default_ascent
25448 && CHAR_TABLE_P (Vuse_default_ascent)
25449 && !NILP (Faref (Vuse_default_ascent,
25450 make_number (it->char_to_display))))
25451 highest = font->default_ascent + boff;
25452
25453 /* Draw the first glyph at the normal position. It may be
25454 shifted to right later if some other glyphs are drawn
25455 at the left. */
25456 cmp->offsets[i * 2] = 0;
25457 cmp->offsets[i * 2 + 1] = boff;
25458 cmp->lbearing = lbearing;
25459 cmp->rbearing = rbearing;
25460
25461 /* Set cmp->offsets for the remaining glyphs. */
25462 for (i++; i < glyph_len; i++)
25463 {
25464 int left, right, btm, top;
25465 int ch = COMPOSITION_GLYPH (cmp, i);
25466 int face_id;
25467 struct face *this_face;
25468
25469 if (ch == '\t')
25470 ch = ' ';
25471 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25472 this_face = FACE_FROM_ID (it->f, face_id);
25473 font = this_face->font;
25474
25475 if (font == NULL)
25476 pcm = NULL;
25477 else
25478 {
25479 get_char_face_and_encoding (it->f, ch, face_id,
25480 &char2b, 0);
25481 pcm = get_per_char_metric (font, &char2b);
25482 }
25483 if (! pcm)
25484 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25485 else
25486 {
25487 width = pcm->width;
25488 ascent = pcm->ascent;
25489 descent = pcm->descent;
25490 lbearing = pcm->lbearing;
25491 rbearing = pcm->rbearing;
25492 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25493 {
25494 /* Relative composition with or without
25495 alternate chars. */
25496 left = (leftmost + rightmost - width) / 2;
25497 btm = - descent + boff;
25498 if (font->relative_compose
25499 && (! CHAR_TABLE_P (Vignore_relative_composition)
25500 || NILP (Faref (Vignore_relative_composition,
25501 make_number (ch)))))
25502 {
25503
25504 if (- descent >= font->relative_compose)
25505 /* One extra pixel between two glyphs. */
25506 btm = highest + 1;
25507 else if (ascent <= 0)
25508 /* One extra pixel between two glyphs. */
25509 btm = lowest - 1 - ascent - descent;
25510 }
25511 }
25512 else
25513 {
25514 /* A composition rule is specified by an integer
25515 value that encodes global and new reference
25516 points (GREF and NREF). GREF and NREF are
25517 specified by numbers as below:
25518
25519 0---1---2 -- ascent
25520 | |
25521 | |
25522 | |
25523 9--10--11 -- center
25524 | |
25525 ---3---4---5--- baseline
25526 | |
25527 6---7---8 -- descent
25528 */
25529 int rule = COMPOSITION_RULE (cmp, i);
25530 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25531
25532 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25533 grefx = gref % 3, nrefx = nref % 3;
25534 grefy = gref / 3, nrefy = nref / 3;
25535 if (xoff)
25536 xoff = font_height * (xoff - 128) / 256;
25537 if (yoff)
25538 yoff = font_height * (yoff - 128) / 256;
25539
25540 left = (leftmost
25541 + grefx * (rightmost - leftmost) / 2
25542 - nrefx * width / 2
25543 + xoff);
25544
25545 btm = ((grefy == 0 ? highest
25546 : grefy == 1 ? 0
25547 : grefy == 2 ? lowest
25548 : (highest + lowest) / 2)
25549 - (nrefy == 0 ? ascent + descent
25550 : nrefy == 1 ? descent - boff
25551 : nrefy == 2 ? 0
25552 : (ascent + descent) / 2)
25553 + yoff);
25554 }
25555
25556 cmp->offsets[i * 2] = left;
25557 cmp->offsets[i * 2 + 1] = btm + descent;
25558
25559 /* Update the bounding box of the overall glyphs. */
25560 if (width > 0)
25561 {
25562 right = left + width;
25563 if (left < leftmost)
25564 leftmost = left;
25565 if (right > rightmost)
25566 rightmost = right;
25567 }
25568 top = btm + descent + ascent;
25569 if (top > highest)
25570 highest = top;
25571 if (btm < lowest)
25572 lowest = btm;
25573
25574 if (cmp->lbearing > left + lbearing)
25575 cmp->lbearing = left + lbearing;
25576 if (cmp->rbearing < left + rbearing)
25577 cmp->rbearing = left + rbearing;
25578 }
25579 }
25580
25581 /* If there are glyphs whose x-offsets are negative,
25582 shift all glyphs to the right and make all x-offsets
25583 non-negative. */
25584 if (leftmost < 0)
25585 {
25586 for (i = 0; i < cmp->glyph_len; i++)
25587 cmp->offsets[i * 2] -= leftmost;
25588 rightmost -= leftmost;
25589 cmp->lbearing -= leftmost;
25590 cmp->rbearing -= leftmost;
25591 }
25592
25593 if (left_padded && cmp->lbearing < 0)
25594 {
25595 for (i = 0; i < cmp->glyph_len; i++)
25596 cmp->offsets[i * 2] -= cmp->lbearing;
25597 rightmost -= cmp->lbearing;
25598 cmp->rbearing -= cmp->lbearing;
25599 cmp->lbearing = 0;
25600 }
25601 if (right_padded && rightmost < cmp->rbearing)
25602 {
25603 rightmost = cmp->rbearing;
25604 }
25605
25606 cmp->pixel_width = rightmost;
25607 cmp->ascent = highest;
25608 cmp->descent = - lowest;
25609 if (cmp->ascent < font_ascent)
25610 cmp->ascent = font_ascent;
25611 if (cmp->descent < font_descent)
25612 cmp->descent = font_descent;
25613 }
25614
25615 if (it->glyph_row
25616 && (cmp->lbearing < 0
25617 || cmp->rbearing > cmp->pixel_width))
25618 it->glyph_row->contains_overlapping_glyphs_p = 1;
25619
25620 it->pixel_width = cmp->pixel_width;
25621 it->ascent = it->phys_ascent = cmp->ascent;
25622 it->descent = it->phys_descent = cmp->descent;
25623 if (face->box != FACE_NO_BOX)
25624 {
25625 int thick = face->box_line_width;
25626
25627 if (thick > 0)
25628 {
25629 it->ascent += thick;
25630 it->descent += thick;
25631 }
25632 else
25633 thick = - thick;
25634
25635 if (it->start_of_box_run_p)
25636 it->pixel_width += thick;
25637 if (it->end_of_box_run_p)
25638 it->pixel_width += thick;
25639 }
25640
25641 /* If face has an overline, add the height of the overline
25642 (1 pixel) and a 1 pixel margin to the character height. */
25643 if (face->overline_p)
25644 it->ascent += overline_margin;
25645
25646 take_vertical_position_into_account (it);
25647 if (it->ascent < 0)
25648 it->ascent = 0;
25649 if (it->descent < 0)
25650 it->descent = 0;
25651
25652 if (it->glyph_row && cmp->glyph_len > 0)
25653 append_composite_glyph (it);
25654 }
25655 else if (it->what == IT_COMPOSITION)
25656 {
25657 /* A dynamic (automatic) composition. */
25658 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25659 Lisp_Object gstring;
25660 struct font_metrics metrics;
25661
25662 it->nglyphs = 1;
25663
25664 gstring = composition_gstring_from_id (it->cmp_it.id);
25665 it->pixel_width
25666 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25667 &metrics);
25668 if (it->glyph_row
25669 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25670 it->glyph_row->contains_overlapping_glyphs_p = 1;
25671 it->ascent = it->phys_ascent = metrics.ascent;
25672 it->descent = it->phys_descent = metrics.descent;
25673 if (face->box != FACE_NO_BOX)
25674 {
25675 int thick = face->box_line_width;
25676
25677 if (thick > 0)
25678 {
25679 it->ascent += thick;
25680 it->descent += thick;
25681 }
25682 else
25683 thick = - thick;
25684
25685 if (it->start_of_box_run_p)
25686 it->pixel_width += thick;
25687 if (it->end_of_box_run_p)
25688 it->pixel_width += thick;
25689 }
25690 /* If face has an overline, add the height of the overline
25691 (1 pixel) and a 1 pixel margin to the character height. */
25692 if (face->overline_p)
25693 it->ascent += overline_margin;
25694 take_vertical_position_into_account (it);
25695 if (it->ascent < 0)
25696 it->ascent = 0;
25697 if (it->descent < 0)
25698 it->descent = 0;
25699
25700 if (it->glyph_row)
25701 append_composite_glyph (it);
25702 }
25703 else if (it->what == IT_GLYPHLESS)
25704 produce_glyphless_glyph (it, 0, Qnil);
25705 else if (it->what == IT_IMAGE)
25706 produce_image_glyph (it);
25707 else if (it->what == IT_STRETCH)
25708 produce_stretch_glyph (it);
25709
25710 done:
25711 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25712 because this isn't true for images with `:ascent 100'. */
25713 eassert (it->ascent >= 0 && it->descent >= 0);
25714 if (it->area == TEXT_AREA)
25715 it->current_x += it->pixel_width;
25716
25717 if (extra_line_spacing > 0)
25718 {
25719 it->descent += extra_line_spacing;
25720 if (extra_line_spacing > it->max_extra_line_spacing)
25721 it->max_extra_line_spacing = extra_line_spacing;
25722 }
25723
25724 it->max_ascent = max (it->max_ascent, it->ascent);
25725 it->max_descent = max (it->max_descent, it->descent);
25726 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25727 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25728 }
25729
25730 /* EXPORT for RIF:
25731 Output LEN glyphs starting at START at the nominal cursor position.
25732 Advance the nominal cursor over the text. The global variable
25733 updated_row is the glyph row being updated, and updated_area is the
25734 area of that row being updated. */
25735
25736 void
25737 x_write_glyphs (struct window *w, struct glyph *start, int len)
25738 {
25739 int x, hpos, chpos = w->phys_cursor.hpos;
25740
25741 eassert (updated_row);
25742 /* When the window is hscrolled, cursor hpos can legitimately be out
25743 of bounds, but we draw the cursor at the corresponding window
25744 margin in that case. */
25745 if (!updated_row->reversed_p && chpos < 0)
25746 chpos = 0;
25747 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25748 chpos = updated_row->used[TEXT_AREA] - 1;
25749
25750 block_input ();
25751
25752 /* Write glyphs. */
25753
25754 hpos = start - updated_row->glyphs[updated_area];
25755 x = draw_glyphs (w, output_cursor.x,
25756 updated_row, updated_area,
25757 hpos, hpos + len,
25758 DRAW_NORMAL_TEXT, 0);
25759
25760 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25761 if (updated_area == TEXT_AREA
25762 && w->phys_cursor_on_p
25763 && w->phys_cursor.vpos == output_cursor.vpos
25764 && chpos >= hpos
25765 && chpos < hpos + len)
25766 w->phys_cursor_on_p = 0;
25767
25768 unblock_input ();
25769
25770 /* Advance the output cursor. */
25771 output_cursor.hpos += len;
25772 output_cursor.x = x;
25773 }
25774
25775
25776 /* EXPORT for RIF:
25777 Insert LEN glyphs from START at the nominal cursor position. */
25778
25779 void
25780 x_insert_glyphs (struct window *w, struct glyph *start, int len)
25781 {
25782 struct frame *f;
25783 int line_height, shift_by_width, shifted_region_width;
25784 struct glyph_row *row;
25785 struct glyph *glyph;
25786 int frame_x, frame_y;
25787 ptrdiff_t hpos;
25788
25789 eassert (updated_row);
25790 block_input ();
25791 f = XFRAME (WINDOW_FRAME (w));
25792
25793 /* Get the height of the line we are in. */
25794 row = updated_row;
25795 line_height = row->height;
25796
25797 /* Get the width of the glyphs to insert. */
25798 shift_by_width = 0;
25799 for (glyph = start; glyph < start + len; ++glyph)
25800 shift_by_width += glyph->pixel_width;
25801
25802 /* Get the width of the region to shift right. */
25803 shifted_region_width = (window_box_width (w, updated_area)
25804 - output_cursor.x
25805 - shift_by_width);
25806
25807 /* Shift right. */
25808 frame_x = window_box_left (w, updated_area) + output_cursor.x;
25809 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
25810
25811 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25812 line_height, shift_by_width);
25813
25814 /* Write the glyphs. */
25815 hpos = start - row->glyphs[updated_area];
25816 draw_glyphs (w, output_cursor.x, row, updated_area,
25817 hpos, hpos + len,
25818 DRAW_NORMAL_TEXT, 0);
25819
25820 /* Advance the output cursor. */
25821 output_cursor.hpos += len;
25822 output_cursor.x += shift_by_width;
25823 unblock_input ();
25824 }
25825
25826
25827 /* EXPORT for RIF:
25828 Erase the current text line from the nominal cursor position
25829 (inclusive) to pixel column TO_X (exclusive). The idea is that
25830 everything from TO_X onward is already erased.
25831
25832 TO_X is a pixel position relative to updated_area of currently
25833 updated window W. TO_X == -1 means clear to the end of this area. */
25834
25835 void
25836 x_clear_end_of_line (struct window *w, int to_x)
25837 {
25838 struct frame *f;
25839 int max_x, min_y, max_y;
25840 int from_x, from_y, to_y;
25841
25842 eassert (updated_row);
25843 f = XFRAME (w->frame);
25844
25845 if (updated_row->full_width_p)
25846 max_x = WINDOW_TOTAL_WIDTH (w);
25847 else
25848 max_x = window_box_width (w, updated_area);
25849 max_y = window_text_bottom_y (w);
25850
25851 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25852 of window. For TO_X > 0, truncate to end of drawing area. */
25853 if (to_x == 0)
25854 return;
25855 else if (to_x < 0)
25856 to_x = max_x;
25857 else
25858 to_x = min (to_x, max_x);
25859
25860 to_y = min (max_y, output_cursor.y + updated_row->height);
25861
25862 /* Notice if the cursor will be cleared by this operation. */
25863 if (!updated_row->full_width_p)
25864 notice_overwritten_cursor (w, updated_area,
25865 output_cursor.x, -1,
25866 updated_row->y,
25867 MATRIX_ROW_BOTTOM_Y (updated_row));
25868
25869 from_x = output_cursor.x;
25870
25871 /* Translate to frame coordinates. */
25872 if (updated_row->full_width_p)
25873 {
25874 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25875 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25876 }
25877 else
25878 {
25879 int area_left = window_box_left (w, updated_area);
25880 from_x += area_left;
25881 to_x += area_left;
25882 }
25883
25884 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25885 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
25886 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25887
25888 /* Prevent inadvertently clearing to end of the X window. */
25889 if (to_x > from_x && to_y > from_y)
25890 {
25891 block_input ();
25892 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25893 to_x - from_x, to_y - from_y);
25894 unblock_input ();
25895 }
25896 }
25897
25898 #endif /* HAVE_WINDOW_SYSTEM */
25899
25900
25901 \f
25902 /***********************************************************************
25903 Cursor types
25904 ***********************************************************************/
25905
25906 /* Value is the internal representation of the specified cursor type
25907 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25908 of the bar cursor. */
25909
25910 static enum text_cursor_kinds
25911 get_specified_cursor_type (Lisp_Object arg, int *width)
25912 {
25913 enum text_cursor_kinds type;
25914
25915 if (NILP (arg))
25916 return NO_CURSOR;
25917
25918 if (EQ (arg, Qbox))
25919 return FILLED_BOX_CURSOR;
25920
25921 if (EQ (arg, Qhollow))
25922 return HOLLOW_BOX_CURSOR;
25923
25924 if (EQ (arg, Qbar))
25925 {
25926 *width = 2;
25927 return BAR_CURSOR;
25928 }
25929
25930 if (CONSP (arg)
25931 && EQ (XCAR (arg), Qbar)
25932 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25933 {
25934 *width = XINT (XCDR (arg));
25935 return BAR_CURSOR;
25936 }
25937
25938 if (EQ (arg, Qhbar))
25939 {
25940 *width = 2;
25941 return HBAR_CURSOR;
25942 }
25943
25944 if (CONSP (arg)
25945 && EQ (XCAR (arg), Qhbar)
25946 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25947 {
25948 *width = XINT (XCDR (arg));
25949 return HBAR_CURSOR;
25950 }
25951
25952 /* Treat anything unknown as "hollow box cursor".
25953 It was bad to signal an error; people have trouble fixing
25954 .Xdefaults with Emacs, when it has something bad in it. */
25955 type = HOLLOW_BOX_CURSOR;
25956
25957 return type;
25958 }
25959
25960 /* Set the default cursor types for specified frame. */
25961 void
25962 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25963 {
25964 int width = 1;
25965 Lisp_Object tem;
25966
25967 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25968 FRAME_CURSOR_WIDTH (f) = width;
25969
25970 /* By default, set up the blink-off state depending on the on-state. */
25971
25972 tem = Fassoc (arg, Vblink_cursor_alist);
25973 if (!NILP (tem))
25974 {
25975 FRAME_BLINK_OFF_CURSOR (f)
25976 = get_specified_cursor_type (XCDR (tem), &width);
25977 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25978 }
25979 else
25980 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25981
25982 /* Make sure the cursor gets redrawn. */
25983 cursor_type_changed = 1;
25984 }
25985
25986
25987 #ifdef HAVE_WINDOW_SYSTEM
25988
25989 /* Return the cursor we want to be displayed in window W. Return
25990 width of bar/hbar cursor through WIDTH arg. Return with
25991 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
25992 (i.e. if the `system caret' should track this cursor).
25993
25994 In a mini-buffer window, we want the cursor only to appear if we
25995 are reading input from this window. For the selected window, we
25996 want the cursor type given by the frame parameter or buffer local
25997 setting of cursor-type. If explicitly marked off, draw no cursor.
25998 In all other cases, we want a hollow box cursor. */
25999
26000 static enum text_cursor_kinds
26001 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
26002 int *active_cursor)
26003 {
26004 struct frame *f = XFRAME (w->frame);
26005 struct buffer *b = XBUFFER (w->contents);
26006 int cursor_type = DEFAULT_CURSOR;
26007 Lisp_Object alt_cursor;
26008 int non_selected = 0;
26009
26010 *active_cursor = 1;
26011
26012 /* Echo area */
26013 if (cursor_in_echo_area
26014 && FRAME_HAS_MINIBUF_P (f)
26015 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
26016 {
26017 if (w == XWINDOW (echo_area_window))
26018 {
26019 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
26020 {
26021 *width = FRAME_CURSOR_WIDTH (f);
26022 return FRAME_DESIRED_CURSOR (f);
26023 }
26024 else
26025 return get_specified_cursor_type (BVAR (b, cursor_type), width);
26026 }
26027
26028 *active_cursor = 0;
26029 non_selected = 1;
26030 }
26031
26032 /* Detect a nonselected window or nonselected frame. */
26033 else if (w != XWINDOW (f->selected_window)
26034 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
26035 {
26036 *active_cursor = 0;
26037
26038 if (MINI_WINDOW_P (w) && minibuf_level == 0)
26039 return NO_CURSOR;
26040
26041 non_selected = 1;
26042 }
26043
26044 /* Never display a cursor in a window in which cursor-type is nil. */
26045 if (NILP (BVAR (b, cursor_type)))
26046 return NO_CURSOR;
26047
26048 /* Get the normal cursor type for this window. */
26049 if (EQ (BVAR (b, cursor_type), Qt))
26050 {
26051 cursor_type = FRAME_DESIRED_CURSOR (f);
26052 *width = FRAME_CURSOR_WIDTH (f);
26053 }
26054 else
26055 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
26056
26057 /* Use cursor-in-non-selected-windows instead
26058 for non-selected window or frame. */
26059 if (non_selected)
26060 {
26061 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
26062 if (!EQ (Qt, alt_cursor))
26063 return get_specified_cursor_type (alt_cursor, width);
26064 /* t means modify the normal cursor type. */
26065 if (cursor_type == FILLED_BOX_CURSOR)
26066 cursor_type = HOLLOW_BOX_CURSOR;
26067 else if (cursor_type == BAR_CURSOR && *width > 1)
26068 --*width;
26069 return cursor_type;
26070 }
26071
26072 /* Use normal cursor if not blinked off. */
26073 if (!w->cursor_off_p)
26074 {
26075 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26076 {
26077 if (cursor_type == FILLED_BOX_CURSOR)
26078 {
26079 /* Using a block cursor on large images can be very annoying.
26080 So use a hollow cursor for "large" images.
26081 If image is not transparent (no mask), also use hollow cursor. */
26082 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26083 if (img != NULL && IMAGEP (img->spec))
26084 {
26085 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
26086 where N = size of default frame font size.
26087 This should cover most of the "tiny" icons people may use. */
26088 if (!img->mask
26089 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
26090 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
26091 cursor_type = HOLLOW_BOX_CURSOR;
26092 }
26093 }
26094 else if (cursor_type != NO_CURSOR)
26095 {
26096 /* Display current only supports BOX and HOLLOW cursors for images.
26097 So for now, unconditionally use a HOLLOW cursor when cursor is
26098 not a solid box cursor. */
26099 cursor_type = HOLLOW_BOX_CURSOR;
26100 }
26101 }
26102 return cursor_type;
26103 }
26104
26105 /* Cursor is blinked off, so determine how to "toggle" it. */
26106
26107 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
26108 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
26109 return get_specified_cursor_type (XCDR (alt_cursor), width);
26110
26111 /* Then see if frame has specified a specific blink off cursor type. */
26112 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
26113 {
26114 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
26115 return FRAME_BLINK_OFF_CURSOR (f);
26116 }
26117
26118 #if 0
26119 /* Some people liked having a permanently visible blinking cursor,
26120 while others had very strong opinions against it. So it was
26121 decided to remove it. KFS 2003-09-03 */
26122
26123 /* Finally perform built-in cursor blinking:
26124 filled box <-> hollow box
26125 wide [h]bar <-> narrow [h]bar
26126 narrow [h]bar <-> no cursor
26127 other type <-> no cursor */
26128
26129 if (cursor_type == FILLED_BOX_CURSOR)
26130 return HOLLOW_BOX_CURSOR;
26131
26132 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
26133 {
26134 *width = 1;
26135 return cursor_type;
26136 }
26137 #endif
26138
26139 return NO_CURSOR;
26140 }
26141
26142
26143 /* Notice when the text cursor of window W has been completely
26144 overwritten by a drawing operation that outputs glyphs in AREA
26145 starting at X0 and ending at X1 in the line starting at Y0 and
26146 ending at Y1. X coordinates are area-relative. X1 < 0 means all
26147 the rest of the line after X0 has been written. Y coordinates
26148 are window-relative. */
26149
26150 static void
26151 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
26152 int x0, int x1, int y0, int y1)
26153 {
26154 int cx0, cx1, cy0, cy1;
26155 struct glyph_row *row;
26156
26157 if (!w->phys_cursor_on_p)
26158 return;
26159 if (area != TEXT_AREA)
26160 return;
26161
26162 if (w->phys_cursor.vpos < 0
26163 || w->phys_cursor.vpos >= w->current_matrix->nrows
26164 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
26165 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
26166 return;
26167
26168 if (row->cursor_in_fringe_p)
26169 {
26170 row->cursor_in_fringe_p = 0;
26171 draw_fringe_bitmap (w, row, row->reversed_p);
26172 w->phys_cursor_on_p = 0;
26173 return;
26174 }
26175
26176 cx0 = w->phys_cursor.x;
26177 cx1 = cx0 + w->phys_cursor_width;
26178 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
26179 return;
26180
26181 /* The cursor image will be completely removed from the
26182 screen if the output area intersects the cursor area in
26183 y-direction. When we draw in [y0 y1[, and some part of
26184 the cursor is at y < y0, that part must have been drawn
26185 before. When scrolling, the cursor is erased before
26186 actually scrolling, so we don't come here. When not
26187 scrolling, the rows above the old cursor row must have
26188 changed, and in this case these rows must have written
26189 over the cursor image.
26190
26191 Likewise if part of the cursor is below y1, with the
26192 exception of the cursor being in the first blank row at
26193 the buffer and window end because update_text_area
26194 doesn't draw that row. (Except when it does, but
26195 that's handled in update_text_area.) */
26196
26197 cy0 = w->phys_cursor.y;
26198 cy1 = cy0 + w->phys_cursor_height;
26199 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
26200 return;
26201
26202 w->phys_cursor_on_p = 0;
26203 }
26204
26205 #endif /* HAVE_WINDOW_SYSTEM */
26206
26207 \f
26208 /************************************************************************
26209 Mouse Face
26210 ************************************************************************/
26211
26212 #ifdef HAVE_WINDOW_SYSTEM
26213
26214 /* EXPORT for RIF:
26215 Fix the display of area AREA of overlapping row ROW in window W
26216 with respect to the overlapping part OVERLAPS. */
26217
26218 void
26219 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
26220 enum glyph_row_area area, int overlaps)
26221 {
26222 int i, x;
26223
26224 block_input ();
26225
26226 x = 0;
26227 for (i = 0; i < row->used[area];)
26228 {
26229 if (row->glyphs[area][i].overlaps_vertically_p)
26230 {
26231 int start = i, start_x = x;
26232
26233 do
26234 {
26235 x += row->glyphs[area][i].pixel_width;
26236 ++i;
26237 }
26238 while (i < row->used[area]
26239 && row->glyphs[area][i].overlaps_vertically_p);
26240
26241 draw_glyphs (w, start_x, row, area,
26242 start, i,
26243 DRAW_NORMAL_TEXT, overlaps);
26244 }
26245 else
26246 {
26247 x += row->glyphs[area][i].pixel_width;
26248 ++i;
26249 }
26250 }
26251
26252 unblock_input ();
26253 }
26254
26255
26256 /* EXPORT:
26257 Draw the cursor glyph of window W in glyph row ROW. See the
26258 comment of draw_glyphs for the meaning of HL. */
26259
26260 void
26261 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
26262 enum draw_glyphs_face hl)
26263 {
26264 /* If cursor hpos is out of bounds, don't draw garbage. This can
26265 happen in mini-buffer windows when switching between echo area
26266 glyphs and mini-buffer. */
26267 if ((row->reversed_p
26268 ? (w->phys_cursor.hpos >= 0)
26269 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
26270 {
26271 int on_p = w->phys_cursor_on_p;
26272 int x1;
26273 int hpos = w->phys_cursor.hpos;
26274
26275 /* When the window is hscrolled, cursor hpos can legitimately be
26276 out of bounds, but we draw the cursor at the corresponding
26277 window margin in that case. */
26278 if (!row->reversed_p && hpos < 0)
26279 hpos = 0;
26280 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26281 hpos = row->used[TEXT_AREA] - 1;
26282
26283 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
26284 hl, 0);
26285 w->phys_cursor_on_p = on_p;
26286
26287 if (hl == DRAW_CURSOR)
26288 w->phys_cursor_width = x1 - w->phys_cursor.x;
26289 /* When we erase the cursor, and ROW is overlapped by other
26290 rows, make sure that these overlapping parts of other rows
26291 are redrawn. */
26292 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
26293 {
26294 w->phys_cursor_width = x1 - w->phys_cursor.x;
26295
26296 if (row > w->current_matrix->rows
26297 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
26298 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
26299 OVERLAPS_ERASED_CURSOR);
26300
26301 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
26302 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
26303 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
26304 OVERLAPS_ERASED_CURSOR);
26305 }
26306 }
26307 }
26308
26309
26310 /* EXPORT:
26311 Erase the image of a cursor of window W from the screen. */
26312
26313 void
26314 erase_phys_cursor (struct window *w)
26315 {
26316 struct frame *f = XFRAME (w->frame);
26317 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26318 int hpos = w->phys_cursor.hpos;
26319 int vpos = w->phys_cursor.vpos;
26320 int mouse_face_here_p = 0;
26321 struct glyph_matrix *active_glyphs = w->current_matrix;
26322 struct glyph_row *cursor_row;
26323 struct glyph *cursor_glyph;
26324 enum draw_glyphs_face hl;
26325
26326 /* No cursor displayed or row invalidated => nothing to do on the
26327 screen. */
26328 if (w->phys_cursor_type == NO_CURSOR)
26329 goto mark_cursor_off;
26330
26331 /* VPOS >= active_glyphs->nrows means that window has been resized.
26332 Don't bother to erase the cursor. */
26333 if (vpos >= active_glyphs->nrows)
26334 goto mark_cursor_off;
26335
26336 /* If row containing cursor is marked invalid, there is nothing we
26337 can do. */
26338 cursor_row = MATRIX_ROW (active_glyphs, vpos);
26339 if (!cursor_row->enabled_p)
26340 goto mark_cursor_off;
26341
26342 /* If line spacing is > 0, old cursor may only be partially visible in
26343 window after split-window. So adjust visible height. */
26344 cursor_row->visible_height = min (cursor_row->visible_height,
26345 window_text_bottom_y (w) - cursor_row->y);
26346
26347 /* If row is completely invisible, don't attempt to delete a cursor which
26348 isn't there. This can happen if cursor is at top of a window, and
26349 we switch to a buffer with a header line in that window. */
26350 if (cursor_row->visible_height <= 0)
26351 goto mark_cursor_off;
26352
26353 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
26354 if (cursor_row->cursor_in_fringe_p)
26355 {
26356 cursor_row->cursor_in_fringe_p = 0;
26357 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
26358 goto mark_cursor_off;
26359 }
26360
26361 /* This can happen when the new row is shorter than the old one.
26362 In this case, either draw_glyphs or clear_end_of_line
26363 should have cleared the cursor. Note that we wouldn't be
26364 able to erase the cursor in this case because we don't have a
26365 cursor glyph at hand. */
26366 if ((cursor_row->reversed_p
26367 ? (w->phys_cursor.hpos < 0)
26368 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
26369 goto mark_cursor_off;
26370
26371 /* When the window is hscrolled, cursor hpos can legitimately be out
26372 of bounds, but we draw the cursor at the corresponding window
26373 margin in that case. */
26374 if (!cursor_row->reversed_p && hpos < 0)
26375 hpos = 0;
26376 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
26377 hpos = cursor_row->used[TEXT_AREA] - 1;
26378
26379 /* If the cursor is in the mouse face area, redisplay that when
26380 we clear the cursor. */
26381 if (! NILP (hlinfo->mouse_face_window)
26382 && coords_in_mouse_face_p (w, hpos, vpos)
26383 /* Don't redraw the cursor's spot in mouse face if it is at the
26384 end of a line (on a newline). The cursor appears there, but
26385 mouse highlighting does not. */
26386 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
26387 mouse_face_here_p = 1;
26388
26389 /* Maybe clear the display under the cursor. */
26390 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
26391 {
26392 int x, y, left_x;
26393 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
26394 int width;
26395
26396 cursor_glyph = get_phys_cursor_glyph (w);
26397 if (cursor_glyph == NULL)
26398 goto mark_cursor_off;
26399
26400 width = cursor_glyph->pixel_width;
26401 left_x = window_box_left_offset (w, TEXT_AREA);
26402 x = w->phys_cursor.x;
26403 if (x < left_x)
26404 width -= left_x - x;
26405 width = min (width, window_box_width (w, TEXT_AREA) - x);
26406 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
26407 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
26408
26409 if (width > 0)
26410 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
26411 }
26412
26413 /* Erase the cursor by redrawing the character underneath it. */
26414 if (mouse_face_here_p)
26415 hl = DRAW_MOUSE_FACE;
26416 else
26417 hl = DRAW_NORMAL_TEXT;
26418 draw_phys_cursor_glyph (w, cursor_row, hl);
26419
26420 mark_cursor_off:
26421 w->phys_cursor_on_p = 0;
26422 w->phys_cursor_type = NO_CURSOR;
26423 }
26424
26425
26426 /* EXPORT:
26427 Display or clear cursor of window W. If ON is zero, clear the
26428 cursor. If it is non-zero, display the cursor. If ON is nonzero,
26429 where to put the cursor is specified by HPOS, VPOS, X and Y. */
26430
26431 void
26432 display_and_set_cursor (struct window *w, int on,
26433 int hpos, int vpos, int x, int y)
26434 {
26435 struct frame *f = XFRAME (w->frame);
26436 int new_cursor_type;
26437 int new_cursor_width;
26438 int active_cursor;
26439 struct glyph_row *glyph_row;
26440 struct glyph *glyph;
26441
26442 /* This is pointless on invisible frames, and dangerous on garbaged
26443 windows and frames; in the latter case, the frame or window may
26444 be in the midst of changing its size, and x and y may be off the
26445 window. */
26446 if (! FRAME_VISIBLE_P (f)
26447 || FRAME_GARBAGED_P (f)
26448 || vpos >= w->current_matrix->nrows
26449 || hpos >= w->current_matrix->matrix_w)
26450 return;
26451
26452 /* If cursor is off and we want it off, return quickly. */
26453 if (!on && !w->phys_cursor_on_p)
26454 return;
26455
26456 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26457 /* If cursor row is not enabled, we don't really know where to
26458 display the cursor. */
26459 if (!glyph_row->enabled_p)
26460 {
26461 w->phys_cursor_on_p = 0;
26462 return;
26463 }
26464
26465 glyph = NULL;
26466 if (!glyph_row->exact_window_width_line_p
26467 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26468 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26469
26470 eassert (input_blocked_p ());
26471
26472 /* Set new_cursor_type to the cursor we want to be displayed. */
26473 new_cursor_type = get_window_cursor_type (w, glyph,
26474 &new_cursor_width, &active_cursor);
26475
26476 /* If cursor is currently being shown and we don't want it to be or
26477 it is in the wrong place, or the cursor type is not what we want,
26478 erase it. */
26479 if (w->phys_cursor_on_p
26480 && (!on
26481 || w->phys_cursor.x != x
26482 || w->phys_cursor.y != y
26483 || new_cursor_type != w->phys_cursor_type
26484 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26485 && new_cursor_width != w->phys_cursor_width)))
26486 erase_phys_cursor (w);
26487
26488 /* Don't check phys_cursor_on_p here because that flag is only set
26489 to zero in some cases where we know that the cursor has been
26490 completely erased, to avoid the extra work of erasing the cursor
26491 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26492 still not be visible, or it has only been partly erased. */
26493 if (on)
26494 {
26495 w->phys_cursor_ascent = glyph_row->ascent;
26496 w->phys_cursor_height = glyph_row->height;
26497
26498 /* Set phys_cursor_.* before x_draw_.* is called because some
26499 of them may need the information. */
26500 w->phys_cursor.x = x;
26501 w->phys_cursor.y = glyph_row->y;
26502 w->phys_cursor.hpos = hpos;
26503 w->phys_cursor.vpos = vpos;
26504 }
26505
26506 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26507 new_cursor_type, new_cursor_width,
26508 on, active_cursor);
26509 }
26510
26511
26512 /* Switch the display of W's cursor on or off, according to the value
26513 of ON. */
26514
26515 static void
26516 update_window_cursor (struct window *w, int on)
26517 {
26518 /* Don't update cursor in windows whose frame is in the process
26519 of being deleted. */
26520 if (w->current_matrix)
26521 {
26522 int hpos = w->phys_cursor.hpos;
26523 int vpos = w->phys_cursor.vpos;
26524 struct glyph_row *row;
26525
26526 if (vpos >= w->current_matrix->nrows
26527 || hpos >= w->current_matrix->matrix_w)
26528 return;
26529
26530 row = MATRIX_ROW (w->current_matrix, vpos);
26531
26532 /* When the window is hscrolled, cursor hpos can legitimately be
26533 out of bounds, but we draw the cursor at the corresponding
26534 window margin in that case. */
26535 if (!row->reversed_p && hpos < 0)
26536 hpos = 0;
26537 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26538 hpos = row->used[TEXT_AREA] - 1;
26539
26540 block_input ();
26541 display_and_set_cursor (w, on, hpos, vpos,
26542 w->phys_cursor.x, w->phys_cursor.y);
26543 unblock_input ();
26544 }
26545 }
26546
26547
26548 /* Call update_window_cursor with parameter ON_P on all leaf windows
26549 in the window tree rooted at W. */
26550
26551 static void
26552 update_cursor_in_window_tree (struct window *w, int on_p)
26553 {
26554 while (w)
26555 {
26556 if (WINDOWP (w->contents))
26557 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
26558 else
26559 update_window_cursor (w, on_p);
26560
26561 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26562 }
26563 }
26564
26565
26566 /* EXPORT:
26567 Display the cursor on window W, or clear it, according to ON_P.
26568 Don't change the cursor's position. */
26569
26570 void
26571 x_update_cursor (struct frame *f, int on_p)
26572 {
26573 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26574 }
26575
26576
26577 /* EXPORT:
26578 Clear the cursor of window W to background color, and mark the
26579 cursor as not shown. This is used when the text where the cursor
26580 is about to be rewritten. */
26581
26582 void
26583 x_clear_cursor (struct window *w)
26584 {
26585 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26586 update_window_cursor (w, 0);
26587 }
26588
26589 #endif /* HAVE_WINDOW_SYSTEM */
26590
26591 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26592 and MSDOS. */
26593 static void
26594 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26595 int start_hpos, int end_hpos,
26596 enum draw_glyphs_face draw)
26597 {
26598 #ifdef HAVE_WINDOW_SYSTEM
26599 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26600 {
26601 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26602 return;
26603 }
26604 #endif
26605 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26606 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26607 #endif
26608 }
26609
26610 /* Display the active region described by mouse_face_* according to DRAW. */
26611
26612 static void
26613 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26614 {
26615 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26616 struct frame *f = XFRAME (WINDOW_FRAME (w));
26617
26618 if (/* If window is in the process of being destroyed, don't bother
26619 to do anything. */
26620 w->current_matrix != NULL
26621 /* Don't update mouse highlight if hidden */
26622 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26623 /* Recognize when we are called to operate on rows that don't exist
26624 anymore. This can happen when a window is split. */
26625 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26626 {
26627 int phys_cursor_on_p = w->phys_cursor_on_p;
26628 struct glyph_row *row, *first, *last;
26629
26630 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26631 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26632
26633 for (row = first; row <= last && row->enabled_p; ++row)
26634 {
26635 int start_hpos, end_hpos, start_x;
26636
26637 /* For all but the first row, the highlight starts at column 0. */
26638 if (row == first)
26639 {
26640 /* R2L rows have BEG and END in reversed order, but the
26641 screen drawing geometry is always left to right. So
26642 we need to mirror the beginning and end of the
26643 highlighted area in R2L rows. */
26644 if (!row->reversed_p)
26645 {
26646 start_hpos = hlinfo->mouse_face_beg_col;
26647 start_x = hlinfo->mouse_face_beg_x;
26648 }
26649 else if (row == last)
26650 {
26651 start_hpos = hlinfo->mouse_face_end_col;
26652 start_x = hlinfo->mouse_face_end_x;
26653 }
26654 else
26655 {
26656 start_hpos = 0;
26657 start_x = 0;
26658 }
26659 }
26660 else if (row->reversed_p && row == last)
26661 {
26662 start_hpos = hlinfo->mouse_face_end_col;
26663 start_x = hlinfo->mouse_face_end_x;
26664 }
26665 else
26666 {
26667 start_hpos = 0;
26668 start_x = 0;
26669 }
26670
26671 if (row == last)
26672 {
26673 if (!row->reversed_p)
26674 end_hpos = hlinfo->mouse_face_end_col;
26675 else if (row == first)
26676 end_hpos = hlinfo->mouse_face_beg_col;
26677 else
26678 {
26679 end_hpos = row->used[TEXT_AREA];
26680 if (draw == DRAW_NORMAL_TEXT)
26681 row->fill_line_p = 1; /* Clear to end of line */
26682 }
26683 }
26684 else if (row->reversed_p && row == first)
26685 end_hpos = hlinfo->mouse_face_beg_col;
26686 else
26687 {
26688 end_hpos = row->used[TEXT_AREA];
26689 if (draw == DRAW_NORMAL_TEXT)
26690 row->fill_line_p = 1; /* Clear to end of line */
26691 }
26692
26693 if (end_hpos > start_hpos)
26694 {
26695 draw_row_with_mouse_face (w, start_x, row,
26696 start_hpos, end_hpos, draw);
26697
26698 row->mouse_face_p
26699 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26700 }
26701 }
26702
26703 #ifdef HAVE_WINDOW_SYSTEM
26704 /* When we've written over the cursor, arrange for it to
26705 be displayed again. */
26706 if (FRAME_WINDOW_P (f)
26707 && phys_cursor_on_p && !w->phys_cursor_on_p)
26708 {
26709 int hpos = w->phys_cursor.hpos;
26710
26711 /* When the window is hscrolled, cursor hpos can legitimately be
26712 out of bounds, but we draw the cursor at the corresponding
26713 window margin in that case. */
26714 if (!row->reversed_p && hpos < 0)
26715 hpos = 0;
26716 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26717 hpos = row->used[TEXT_AREA] - 1;
26718
26719 block_input ();
26720 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26721 w->phys_cursor.x, w->phys_cursor.y);
26722 unblock_input ();
26723 }
26724 #endif /* HAVE_WINDOW_SYSTEM */
26725 }
26726
26727 #ifdef HAVE_WINDOW_SYSTEM
26728 /* Change the mouse cursor. */
26729 if (FRAME_WINDOW_P (f))
26730 {
26731 if (draw == DRAW_NORMAL_TEXT
26732 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26733 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26734 else if (draw == DRAW_MOUSE_FACE)
26735 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26736 else
26737 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26738 }
26739 #endif /* HAVE_WINDOW_SYSTEM */
26740 }
26741
26742 /* EXPORT:
26743 Clear out the mouse-highlighted active region.
26744 Redraw it un-highlighted first. Value is non-zero if mouse
26745 face was actually drawn unhighlighted. */
26746
26747 int
26748 clear_mouse_face (Mouse_HLInfo *hlinfo)
26749 {
26750 int cleared = 0;
26751
26752 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26753 {
26754 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26755 cleared = 1;
26756 }
26757
26758 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26759 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26760 hlinfo->mouse_face_window = Qnil;
26761 hlinfo->mouse_face_overlay = Qnil;
26762 return cleared;
26763 }
26764
26765 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26766 within the mouse face on that window. */
26767 static int
26768 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26769 {
26770 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26771
26772 /* Quickly resolve the easy cases. */
26773 if (!(WINDOWP (hlinfo->mouse_face_window)
26774 && XWINDOW (hlinfo->mouse_face_window) == w))
26775 return 0;
26776 if (vpos < hlinfo->mouse_face_beg_row
26777 || vpos > hlinfo->mouse_face_end_row)
26778 return 0;
26779 if (vpos > hlinfo->mouse_face_beg_row
26780 && vpos < hlinfo->mouse_face_end_row)
26781 return 1;
26782
26783 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26784 {
26785 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26786 {
26787 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26788 return 1;
26789 }
26790 else if ((vpos == hlinfo->mouse_face_beg_row
26791 && hpos >= hlinfo->mouse_face_beg_col)
26792 || (vpos == hlinfo->mouse_face_end_row
26793 && hpos < hlinfo->mouse_face_end_col))
26794 return 1;
26795 }
26796 else
26797 {
26798 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26799 {
26800 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26801 return 1;
26802 }
26803 else if ((vpos == hlinfo->mouse_face_beg_row
26804 && hpos <= hlinfo->mouse_face_beg_col)
26805 || (vpos == hlinfo->mouse_face_end_row
26806 && hpos > hlinfo->mouse_face_end_col))
26807 return 1;
26808 }
26809 return 0;
26810 }
26811
26812
26813 /* EXPORT:
26814 Non-zero if physical cursor of window W is within mouse face. */
26815
26816 int
26817 cursor_in_mouse_face_p (struct window *w)
26818 {
26819 int hpos = w->phys_cursor.hpos;
26820 int vpos = w->phys_cursor.vpos;
26821 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26822
26823 /* When the window is hscrolled, cursor hpos can legitimately be out
26824 of bounds, but we draw the cursor at the corresponding window
26825 margin in that case. */
26826 if (!row->reversed_p && hpos < 0)
26827 hpos = 0;
26828 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26829 hpos = row->used[TEXT_AREA] - 1;
26830
26831 return coords_in_mouse_face_p (w, hpos, vpos);
26832 }
26833
26834
26835 \f
26836 /* Find the glyph rows START_ROW and END_ROW of window W that display
26837 characters between buffer positions START_CHARPOS and END_CHARPOS
26838 (excluding END_CHARPOS). DISP_STRING is a display string that
26839 covers these buffer positions. This is similar to
26840 row_containing_pos, but is more accurate when bidi reordering makes
26841 buffer positions change non-linearly with glyph rows. */
26842 static void
26843 rows_from_pos_range (struct window *w,
26844 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26845 Lisp_Object disp_string,
26846 struct glyph_row **start, struct glyph_row **end)
26847 {
26848 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26849 int last_y = window_text_bottom_y (w);
26850 struct glyph_row *row;
26851
26852 *start = NULL;
26853 *end = NULL;
26854
26855 while (!first->enabled_p
26856 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26857 first++;
26858
26859 /* Find the START row. */
26860 for (row = first;
26861 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26862 row++)
26863 {
26864 /* A row can potentially be the START row if the range of the
26865 characters it displays intersects the range
26866 [START_CHARPOS..END_CHARPOS). */
26867 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26868 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26869 /* See the commentary in row_containing_pos, for the
26870 explanation of the complicated way to check whether
26871 some position is beyond the end of the characters
26872 displayed by a row. */
26873 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26874 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26875 && !row->ends_at_zv_p
26876 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26877 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26878 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26879 && !row->ends_at_zv_p
26880 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26881 {
26882 /* Found a candidate row. Now make sure at least one of the
26883 glyphs it displays has a charpos from the range
26884 [START_CHARPOS..END_CHARPOS).
26885
26886 This is not obvious because bidi reordering could make
26887 buffer positions of a row be 1,2,3,102,101,100, and if we
26888 want to highlight characters in [50..60), we don't want
26889 this row, even though [50..60) does intersect [1..103),
26890 the range of character positions given by the row's start
26891 and end positions. */
26892 struct glyph *g = row->glyphs[TEXT_AREA];
26893 struct glyph *e = g + row->used[TEXT_AREA];
26894
26895 while (g < e)
26896 {
26897 if (((BUFFERP (g->object) || INTEGERP (g->object))
26898 && start_charpos <= g->charpos && g->charpos < end_charpos)
26899 /* A glyph that comes from DISP_STRING is by
26900 definition to be highlighted. */
26901 || EQ (g->object, disp_string))
26902 *start = row;
26903 g++;
26904 }
26905 if (*start)
26906 break;
26907 }
26908 }
26909
26910 /* Find the END row. */
26911 if (!*start
26912 /* If the last row is partially visible, start looking for END
26913 from that row, instead of starting from FIRST. */
26914 && !(row->enabled_p
26915 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26916 row = first;
26917 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26918 {
26919 struct glyph_row *next = row + 1;
26920 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26921
26922 if (!next->enabled_p
26923 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26924 /* The first row >= START whose range of displayed characters
26925 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26926 is the row END + 1. */
26927 || (start_charpos < next_start
26928 && end_charpos < next_start)
26929 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26930 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26931 && !next->ends_at_zv_p
26932 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26933 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26934 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26935 && !next->ends_at_zv_p
26936 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26937 {
26938 *end = row;
26939 break;
26940 }
26941 else
26942 {
26943 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26944 but none of the characters it displays are in the range, it is
26945 also END + 1. */
26946 struct glyph *g = next->glyphs[TEXT_AREA];
26947 struct glyph *s = g;
26948 struct glyph *e = g + next->used[TEXT_AREA];
26949
26950 while (g < e)
26951 {
26952 if (((BUFFERP (g->object) || INTEGERP (g->object))
26953 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26954 /* If the buffer position of the first glyph in
26955 the row is equal to END_CHARPOS, it means
26956 the last character to be highlighted is the
26957 newline of ROW, and we must consider NEXT as
26958 END, not END+1. */
26959 || (((!next->reversed_p && g == s)
26960 || (next->reversed_p && g == e - 1))
26961 && (g->charpos == end_charpos
26962 /* Special case for when NEXT is an
26963 empty line at ZV. */
26964 || (g->charpos == -1
26965 && !row->ends_at_zv_p
26966 && next_start == end_charpos)))))
26967 /* A glyph that comes from DISP_STRING is by
26968 definition to be highlighted. */
26969 || EQ (g->object, disp_string))
26970 break;
26971 g++;
26972 }
26973 if (g == e)
26974 {
26975 *end = row;
26976 break;
26977 }
26978 /* The first row that ends at ZV must be the last to be
26979 highlighted. */
26980 else if (next->ends_at_zv_p)
26981 {
26982 *end = next;
26983 break;
26984 }
26985 }
26986 }
26987 }
26988
26989 /* This function sets the mouse_face_* elements of HLINFO, assuming
26990 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
26991 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
26992 for the overlay or run of text properties specifying the mouse
26993 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
26994 before-string and after-string that must also be highlighted.
26995 DISP_STRING, if non-nil, is a display string that may cover some
26996 or all of the highlighted text. */
26997
26998 static void
26999 mouse_face_from_buffer_pos (Lisp_Object window,
27000 Mouse_HLInfo *hlinfo,
27001 ptrdiff_t mouse_charpos,
27002 ptrdiff_t start_charpos,
27003 ptrdiff_t end_charpos,
27004 Lisp_Object before_string,
27005 Lisp_Object after_string,
27006 Lisp_Object disp_string)
27007 {
27008 struct window *w = XWINDOW (window);
27009 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27010 struct glyph_row *r1, *r2;
27011 struct glyph *glyph, *end;
27012 ptrdiff_t ignore, pos;
27013 int x;
27014
27015 eassert (NILP (disp_string) || STRINGP (disp_string));
27016 eassert (NILP (before_string) || STRINGP (before_string));
27017 eassert (NILP (after_string) || STRINGP (after_string));
27018
27019 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
27020 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
27021 if (r1 == NULL)
27022 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27023 /* If the before-string or display-string contains newlines,
27024 rows_from_pos_range skips to its last row. Move back. */
27025 if (!NILP (before_string) || !NILP (disp_string))
27026 {
27027 struct glyph_row *prev;
27028 while ((prev = r1 - 1, prev >= first)
27029 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
27030 && prev->used[TEXT_AREA] > 0)
27031 {
27032 struct glyph *beg = prev->glyphs[TEXT_AREA];
27033 glyph = beg + prev->used[TEXT_AREA];
27034 while (--glyph >= beg && INTEGERP (glyph->object));
27035 if (glyph < beg
27036 || !(EQ (glyph->object, before_string)
27037 || EQ (glyph->object, disp_string)))
27038 break;
27039 r1 = prev;
27040 }
27041 }
27042 if (r2 == NULL)
27043 {
27044 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27045 hlinfo->mouse_face_past_end = 1;
27046 }
27047 else if (!NILP (after_string))
27048 {
27049 /* If the after-string has newlines, advance to its last row. */
27050 struct glyph_row *next;
27051 struct glyph_row *last
27052 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27053
27054 for (next = r2 + 1;
27055 next <= last
27056 && next->used[TEXT_AREA] > 0
27057 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
27058 ++next)
27059 r2 = next;
27060 }
27061 /* The rest of the display engine assumes that mouse_face_beg_row is
27062 either above mouse_face_end_row or identical to it. But with
27063 bidi-reordered continued lines, the row for START_CHARPOS could
27064 be below the row for END_CHARPOS. If so, swap the rows and store
27065 them in correct order. */
27066 if (r1->y > r2->y)
27067 {
27068 struct glyph_row *tem = r2;
27069
27070 r2 = r1;
27071 r1 = tem;
27072 }
27073
27074 hlinfo->mouse_face_beg_y = r1->y;
27075 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
27076 hlinfo->mouse_face_end_y = r2->y;
27077 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
27078
27079 /* For a bidi-reordered row, the positions of BEFORE_STRING,
27080 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
27081 could be anywhere in the row and in any order. The strategy
27082 below is to find the leftmost and the rightmost glyph that
27083 belongs to either of these 3 strings, or whose position is
27084 between START_CHARPOS and END_CHARPOS, and highlight all the
27085 glyphs between those two. This may cover more than just the text
27086 between START_CHARPOS and END_CHARPOS if the range of characters
27087 strides the bidi level boundary, e.g. if the beginning is in R2L
27088 text while the end is in L2R text or vice versa. */
27089 if (!r1->reversed_p)
27090 {
27091 /* This row is in a left to right paragraph. Scan it left to
27092 right. */
27093 glyph = r1->glyphs[TEXT_AREA];
27094 end = glyph + r1->used[TEXT_AREA];
27095 x = r1->x;
27096
27097 /* Skip truncation glyphs at the start of the glyph row. */
27098 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27099 for (; glyph < end
27100 && INTEGERP (glyph->object)
27101 && glyph->charpos < 0;
27102 ++glyph)
27103 x += glyph->pixel_width;
27104
27105 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27106 or DISP_STRING, and the first glyph from buffer whose
27107 position is between START_CHARPOS and END_CHARPOS. */
27108 for (; glyph < end
27109 && !INTEGERP (glyph->object)
27110 && !EQ (glyph->object, disp_string)
27111 && !(BUFFERP (glyph->object)
27112 && (glyph->charpos >= start_charpos
27113 && glyph->charpos < end_charpos));
27114 ++glyph)
27115 {
27116 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27117 are present at buffer positions between START_CHARPOS and
27118 END_CHARPOS, or if they come from an overlay. */
27119 if (EQ (glyph->object, before_string))
27120 {
27121 pos = string_buffer_position (before_string,
27122 start_charpos);
27123 /* If pos == 0, it means before_string came from an
27124 overlay, not from a buffer position. */
27125 if (!pos || (pos >= start_charpos && pos < end_charpos))
27126 break;
27127 }
27128 else if (EQ (glyph->object, after_string))
27129 {
27130 pos = string_buffer_position (after_string, end_charpos);
27131 if (!pos || (pos >= start_charpos && pos < end_charpos))
27132 break;
27133 }
27134 x += glyph->pixel_width;
27135 }
27136 hlinfo->mouse_face_beg_x = x;
27137 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27138 }
27139 else
27140 {
27141 /* This row is in a right to left paragraph. Scan it right to
27142 left. */
27143 struct glyph *g;
27144
27145 end = r1->glyphs[TEXT_AREA] - 1;
27146 glyph = end + r1->used[TEXT_AREA];
27147
27148 /* Skip truncation glyphs at the start of the glyph row. */
27149 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27150 for (; glyph > end
27151 && INTEGERP (glyph->object)
27152 && glyph->charpos < 0;
27153 --glyph)
27154 ;
27155
27156 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27157 or DISP_STRING, and the first glyph from buffer whose
27158 position is between START_CHARPOS and END_CHARPOS. */
27159 for (; glyph > end
27160 && !INTEGERP (glyph->object)
27161 && !EQ (glyph->object, disp_string)
27162 && !(BUFFERP (glyph->object)
27163 && (glyph->charpos >= start_charpos
27164 && glyph->charpos < end_charpos));
27165 --glyph)
27166 {
27167 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27168 are present at buffer positions between START_CHARPOS and
27169 END_CHARPOS, or if they come from an overlay. */
27170 if (EQ (glyph->object, before_string))
27171 {
27172 pos = string_buffer_position (before_string, start_charpos);
27173 /* If pos == 0, it means before_string came from an
27174 overlay, not from a buffer position. */
27175 if (!pos || (pos >= start_charpos && pos < end_charpos))
27176 break;
27177 }
27178 else if (EQ (glyph->object, after_string))
27179 {
27180 pos = string_buffer_position (after_string, end_charpos);
27181 if (!pos || (pos >= start_charpos && pos < end_charpos))
27182 break;
27183 }
27184 }
27185
27186 glyph++; /* first glyph to the right of the highlighted area */
27187 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
27188 x += g->pixel_width;
27189 hlinfo->mouse_face_beg_x = x;
27190 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27191 }
27192
27193 /* If the highlight ends in a different row, compute GLYPH and END
27194 for the end row. Otherwise, reuse the values computed above for
27195 the row where the highlight begins. */
27196 if (r2 != r1)
27197 {
27198 if (!r2->reversed_p)
27199 {
27200 glyph = r2->glyphs[TEXT_AREA];
27201 end = glyph + r2->used[TEXT_AREA];
27202 x = r2->x;
27203 }
27204 else
27205 {
27206 end = r2->glyphs[TEXT_AREA] - 1;
27207 glyph = end + r2->used[TEXT_AREA];
27208 }
27209 }
27210
27211 if (!r2->reversed_p)
27212 {
27213 /* Skip truncation and continuation glyphs near the end of the
27214 row, and also blanks and stretch glyphs inserted by
27215 extend_face_to_end_of_line. */
27216 while (end > glyph
27217 && INTEGERP ((end - 1)->object))
27218 --end;
27219 /* Scan the rest of the glyph row from the end, looking for the
27220 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27221 DISP_STRING, or whose position is between START_CHARPOS
27222 and END_CHARPOS */
27223 for (--end;
27224 end > glyph
27225 && !INTEGERP (end->object)
27226 && !EQ (end->object, disp_string)
27227 && !(BUFFERP (end->object)
27228 && (end->charpos >= start_charpos
27229 && end->charpos < end_charpos));
27230 --end)
27231 {
27232 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27233 are present at buffer positions between START_CHARPOS and
27234 END_CHARPOS, or if they come from an overlay. */
27235 if (EQ (end->object, before_string))
27236 {
27237 pos = string_buffer_position (before_string, start_charpos);
27238 if (!pos || (pos >= start_charpos && pos < end_charpos))
27239 break;
27240 }
27241 else if (EQ (end->object, after_string))
27242 {
27243 pos = string_buffer_position (after_string, end_charpos);
27244 if (!pos || (pos >= start_charpos && pos < end_charpos))
27245 break;
27246 }
27247 }
27248 /* Find the X coordinate of the last glyph to be highlighted. */
27249 for (; glyph <= end; ++glyph)
27250 x += glyph->pixel_width;
27251
27252 hlinfo->mouse_face_end_x = x;
27253 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
27254 }
27255 else
27256 {
27257 /* Skip truncation and continuation glyphs near the end of the
27258 row, and also blanks and stretch glyphs inserted by
27259 extend_face_to_end_of_line. */
27260 x = r2->x;
27261 end++;
27262 while (end < glyph
27263 && INTEGERP (end->object))
27264 {
27265 x += end->pixel_width;
27266 ++end;
27267 }
27268 /* Scan the rest of the glyph row from the end, looking for the
27269 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27270 DISP_STRING, or whose position is between START_CHARPOS
27271 and END_CHARPOS */
27272 for ( ;
27273 end < glyph
27274 && !INTEGERP (end->object)
27275 && !EQ (end->object, disp_string)
27276 && !(BUFFERP (end->object)
27277 && (end->charpos >= start_charpos
27278 && end->charpos < end_charpos));
27279 ++end)
27280 {
27281 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27282 are present at buffer positions between START_CHARPOS and
27283 END_CHARPOS, or if they come from an overlay. */
27284 if (EQ (end->object, before_string))
27285 {
27286 pos = string_buffer_position (before_string, start_charpos);
27287 if (!pos || (pos >= start_charpos && pos < end_charpos))
27288 break;
27289 }
27290 else if (EQ (end->object, after_string))
27291 {
27292 pos = string_buffer_position (after_string, end_charpos);
27293 if (!pos || (pos >= start_charpos && pos < end_charpos))
27294 break;
27295 }
27296 x += end->pixel_width;
27297 }
27298 /* If we exited the above loop because we arrived at the last
27299 glyph of the row, and its buffer position is still not in
27300 range, it means the last character in range is the preceding
27301 newline. Bump the end column and x values to get past the
27302 last glyph. */
27303 if (end == glyph
27304 && BUFFERP (end->object)
27305 && (end->charpos < start_charpos
27306 || end->charpos >= end_charpos))
27307 {
27308 x += end->pixel_width;
27309 ++end;
27310 }
27311 hlinfo->mouse_face_end_x = x;
27312 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
27313 }
27314
27315 hlinfo->mouse_face_window = window;
27316 hlinfo->mouse_face_face_id
27317 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
27318 mouse_charpos + 1,
27319 !hlinfo->mouse_face_hidden, -1);
27320 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27321 }
27322
27323 /* The following function is not used anymore (replaced with
27324 mouse_face_from_string_pos), but I leave it here for the time
27325 being, in case someone would. */
27326
27327 #if 0 /* not used */
27328
27329 /* Find the position of the glyph for position POS in OBJECT in
27330 window W's current matrix, and return in *X, *Y the pixel
27331 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
27332
27333 RIGHT_P non-zero means return the position of the right edge of the
27334 glyph, RIGHT_P zero means return the left edge position.
27335
27336 If no glyph for POS exists in the matrix, return the position of
27337 the glyph with the next smaller position that is in the matrix, if
27338 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
27339 exists in the matrix, return the position of the glyph with the
27340 next larger position in OBJECT.
27341
27342 Value is non-zero if a glyph was found. */
27343
27344 static int
27345 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
27346 int *hpos, int *vpos, int *x, int *y, int right_p)
27347 {
27348 int yb = window_text_bottom_y (w);
27349 struct glyph_row *r;
27350 struct glyph *best_glyph = NULL;
27351 struct glyph_row *best_row = NULL;
27352 int best_x = 0;
27353
27354 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27355 r->enabled_p && r->y < yb;
27356 ++r)
27357 {
27358 struct glyph *g = r->glyphs[TEXT_AREA];
27359 struct glyph *e = g + r->used[TEXT_AREA];
27360 int gx;
27361
27362 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27363 if (EQ (g->object, object))
27364 {
27365 if (g->charpos == pos)
27366 {
27367 best_glyph = g;
27368 best_x = gx;
27369 best_row = r;
27370 goto found;
27371 }
27372 else if (best_glyph == NULL
27373 || ((eabs (g->charpos - pos)
27374 < eabs (best_glyph->charpos - pos))
27375 && (right_p
27376 ? g->charpos < pos
27377 : g->charpos > pos)))
27378 {
27379 best_glyph = g;
27380 best_x = gx;
27381 best_row = r;
27382 }
27383 }
27384 }
27385
27386 found:
27387
27388 if (best_glyph)
27389 {
27390 *x = best_x;
27391 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
27392
27393 if (right_p)
27394 {
27395 *x += best_glyph->pixel_width;
27396 ++*hpos;
27397 }
27398
27399 *y = best_row->y;
27400 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
27401 }
27402
27403 return best_glyph != NULL;
27404 }
27405 #endif /* not used */
27406
27407 /* Find the positions of the first and the last glyphs in window W's
27408 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
27409 (assumed to be a string), and return in HLINFO's mouse_face_*
27410 members the pixel and column/row coordinates of those glyphs. */
27411
27412 static void
27413 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
27414 Lisp_Object object,
27415 ptrdiff_t startpos, ptrdiff_t endpos)
27416 {
27417 int yb = window_text_bottom_y (w);
27418 struct glyph_row *r;
27419 struct glyph *g, *e;
27420 int gx;
27421 int found = 0;
27422
27423 /* Find the glyph row with at least one position in the range
27424 [STARTPOS..ENDPOS], and the first glyph in that row whose
27425 position belongs to that range. */
27426 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27427 r->enabled_p && r->y < yb;
27428 ++r)
27429 {
27430 if (!r->reversed_p)
27431 {
27432 g = r->glyphs[TEXT_AREA];
27433 e = g + r->used[TEXT_AREA];
27434 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27435 if (EQ (g->object, object)
27436 && startpos <= g->charpos && g->charpos <= endpos)
27437 {
27438 hlinfo->mouse_face_beg_row
27439 = MATRIX_ROW_VPOS (r, w->current_matrix);
27440 hlinfo->mouse_face_beg_y = r->y;
27441 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27442 hlinfo->mouse_face_beg_x = gx;
27443 found = 1;
27444 break;
27445 }
27446 }
27447 else
27448 {
27449 struct glyph *g1;
27450
27451 e = r->glyphs[TEXT_AREA];
27452 g = e + r->used[TEXT_AREA];
27453 for ( ; g > e; --g)
27454 if (EQ ((g-1)->object, object)
27455 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
27456 {
27457 hlinfo->mouse_face_beg_row
27458 = MATRIX_ROW_VPOS (r, w->current_matrix);
27459 hlinfo->mouse_face_beg_y = r->y;
27460 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27461 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27462 gx += g1->pixel_width;
27463 hlinfo->mouse_face_beg_x = gx;
27464 found = 1;
27465 break;
27466 }
27467 }
27468 if (found)
27469 break;
27470 }
27471
27472 if (!found)
27473 return;
27474
27475 /* Starting with the next row, look for the first row which does NOT
27476 include any glyphs whose positions are in the range. */
27477 for (++r; r->enabled_p && r->y < yb; ++r)
27478 {
27479 g = r->glyphs[TEXT_AREA];
27480 e = g + r->used[TEXT_AREA];
27481 found = 0;
27482 for ( ; g < e; ++g)
27483 if (EQ (g->object, object)
27484 && startpos <= g->charpos && g->charpos <= endpos)
27485 {
27486 found = 1;
27487 break;
27488 }
27489 if (!found)
27490 break;
27491 }
27492
27493 /* The highlighted region ends on the previous row. */
27494 r--;
27495
27496 /* Set the end row and its vertical pixel coordinate. */
27497 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
27498 hlinfo->mouse_face_end_y = r->y;
27499
27500 /* Compute and set the end column and the end column's horizontal
27501 pixel coordinate. */
27502 if (!r->reversed_p)
27503 {
27504 g = r->glyphs[TEXT_AREA];
27505 e = g + r->used[TEXT_AREA];
27506 for ( ; e > g; --e)
27507 if (EQ ((e-1)->object, object)
27508 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
27509 break;
27510 hlinfo->mouse_face_end_col = e - g;
27511
27512 for (gx = r->x; g < e; ++g)
27513 gx += g->pixel_width;
27514 hlinfo->mouse_face_end_x = gx;
27515 }
27516 else
27517 {
27518 e = r->glyphs[TEXT_AREA];
27519 g = e + r->used[TEXT_AREA];
27520 for (gx = r->x ; e < g; ++e)
27521 {
27522 if (EQ (e->object, object)
27523 && startpos <= e->charpos && e->charpos <= endpos)
27524 break;
27525 gx += e->pixel_width;
27526 }
27527 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27528 hlinfo->mouse_face_end_x = gx;
27529 }
27530 }
27531
27532 #ifdef HAVE_WINDOW_SYSTEM
27533
27534 /* See if position X, Y is within a hot-spot of an image. */
27535
27536 static int
27537 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27538 {
27539 if (!CONSP (hot_spot))
27540 return 0;
27541
27542 if (EQ (XCAR (hot_spot), Qrect))
27543 {
27544 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27545 Lisp_Object rect = XCDR (hot_spot);
27546 Lisp_Object tem;
27547 if (!CONSP (rect))
27548 return 0;
27549 if (!CONSP (XCAR (rect)))
27550 return 0;
27551 if (!CONSP (XCDR (rect)))
27552 return 0;
27553 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27554 return 0;
27555 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27556 return 0;
27557 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27558 return 0;
27559 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27560 return 0;
27561 return 1;
27562 }
27563 else if (EQ (XCAR (hot_spot), Qcircle))
27564 {
27565 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27566 Lisp_Object circ = XCDR (hot_spot);
27567 Lisp_Object lr, lx0, ly0;
27568 if (CONSP (circ)
27569 && CONSP (XCAR (circ))
27570 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27571 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27572 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27573 {
27574 double r = XFLOATINT (lr);
27575 double dx = XINT (lx0) - x;
27576 double dy = XINT (ly0) - y;
27577 return (dx * dx + dy * dy <= r * r);
27578 }
27579 }
27580 else if (EQ (XCAR (hot_spot), Qpoly))
27581 {
27582 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27583 if (VECTORP (XCDR (hot_spot)))
27584 {
27585 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27586 Lisp_Object *poly = v->contents;
27587 ptrdiff_t n = v->header.size;
27588 ptrdiff_t i;
27589 int inside = 0;
27590 Lisp_Object lx, ly;
27591 int x0, y0;
27592
27593 /* Need an even number of coordinates, and at least 3 edges. */
27594 if (n < 6 || n & 1)
27595 return 0;
27596
27597 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27598 If count is odd, we are inside polygon. Pixels on edges
27599 may or may not be included depending on actual geometry of the
27600 polygon. */
27601 if ((lx = poly[n-2], !INTEGERP (lx))
27602 || (ly = poly[n-1], !INTEGERP (lx)))
27603 return 0;
27604 x0 = XINT (lx), y0 = XINT (ly);
27605 for (i = 0; i < n; i += 2)
27606 {
27607 int x1 = x0, y1 = y0;
27608 if ((lx = poly[i], !INTEGERP (lx))
27609 || (ly = poly[i+1], !INTEGERP (ly)))
27610 return 0;
27611 x0 = XINT (lx), y0 = XINT (ly);
27612
27613 /* Does this segment cross the X line? */
27614 if (x0 >= x)
27615 {
27616 if (x1 >= x)
27617 continue;
27618 }
27619 else if (x1 < x)
27620 continue;
27621 if (y > y0 && y > y1)
27622 continue;
27623 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27624 inside = !inside;
27625 }
27626 return inside;
27627 }
27628 }
27629 return 0;
27630 }
27631
27632 Lisp_Object
27633 find_hot_spot (Lisp_Object map, int x, int y)
27634 {
27635 while (CONSP (map))
27636 {
27637 if (CONSP (XCAR (map))
27638 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27639 return XCAR (map);
27640 map = XCDR (map);
27641 }
27642
27643 return Qnil;
27644 }
27645
27646 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27647 3, 3, 0,
27648 doc: /* Lookup in image map MAP coordinates X and Y.
27649 An image map is an alist where each element has the format (AREA ID PLIST).
27650 An AREA is specified as either a rectangle, a circle, or a polygon:
27651 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27652 pixel coordinates of the upper left and bottom right corners.
27653 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27654 and the radius of the circle; r may be a float or integer.
27655 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27656 vector describes one corner in the polygon.
27657 Returns the alist element for the first matching AREA in MAP. */)
27658 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27659 {
27660 if (NILP (map))
27661 return Qnil;
27662
27663 CHECK_NUMBER (x);
27664 CHECK_NUMBER (y);
27665
27666 return find_hot_spot (map,
27667 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27668 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27669 }
27670
27671
27672 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27673 static void
27674 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27675 {
27676 /* Do not change cursor shape while dragging mouse. */
27677 if (!NILP (do_mouse_tracking))
27678 return;
27679
27680 if (!NILP (pointer))
27681 {
27682 if (EQ (pointer, Qarrow))
27683 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27684 else if (EQ (pointer, Qhand))
27685 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27686 else if (EQ (pointer, Qtext))
27687 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27688 else if (EQ (pointer, intern ("hdrag")))
27689 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27690 #ifdef HAVE_X_WINDOWS
27691 else if (EQ (pointer, intern ("vdrag")))
27692 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27693 #endif
27694 else if (EQ (pointer, intern ("hourglass")))
27695 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27696 else if (EQ (pointer, Qmodeline))
27697 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27698 else
27699 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27700 }
27701
27702 if (cursor != No_Cursor)
27703 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27704 }
27705
27706 #endif /* HAVE_WINDOW_SYSTEM */
27707
27708 /* Take proper action when mouse has moved to the mode or header line
27709 or marginal area AREA of window W, x-position X and y-position Y.
27710 X is relative to the start of the text display area of W, so the
27711 width of bitmap areas and scroll bars must be subtracted to get a
27712 position relative to the start of the mode line. */
27713
27714 static void
27715 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27716 enum window_part area)
27717 {
27718 struct window *w = XWINDOW (window);
27719 struct frame *f = XFRAME (w->frame);
27720 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27721 #ifdef HAVE_WINDOW_SYSTEM
27722 Display_Info *dpyinfo;
27723 #endif
27724 Cursor cursor = No_Cursor;
27725 Lisp_Object pointer = Qnil;
27726 int dx, dy, width, height;
27727 ptrdiff_t charpos;
27728 Lisp_Object string, object = Qnil;
27729 Lisp_Object pos IF_LINT (= Qnil), help;
27730
27731 Lisp_Object mouse_face;
27732 int original_x_pixel = x;
27733 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27734 struct glyph_row *row IF_LINT (= 0);
27735
27736 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27737 {
27738 int x0;
27739 struct glyph *end;
27740
27741 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27742 returns them in row/column units! */
27743 string = mode_line_string (w, area, &x, &y, &charpos,
27744 &object, &dx, &dy, &width, &height);
27745
27746 row = (area == ON_MODE_LINE
27747 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27748 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27749
27750 /* Find the glyph under the mouse pointer. */
27751 if (row->mode_line_p && row->enabled_p)
27752 {
27753 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27754 end = glyph + row->used[TEXT_AREA];
27755
27756 for (x0 = original_x_pixel;
27757 glyph < end && x0 >= glyph->pixel_width;
27758 ++glyph)
27759 x0 -= glyph->pixel_width;
27760
27761 if (glyph >= end)
27762 glyph = NULL;
27763 }
27764 }
27765 else
27766 {
27767 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27768 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27769 returns them in row/column units! */
27770 string = marginal_area_string (w, area, &x, &y, &charpos,
27771 &object, &dx, &dy, &width, &height);
27772 }
27773
27774 help = Qnil;
27775
27776 #ifdef HAVE_WINDOW_SYSTEM
27777 if (IMAGEP (object))
27778 {
27779 Lisp_Object image_map, hotspot;
27780 if ((image_map = Fplist_get (XCDR (object), QCmap),
27781 !NILP (image_map))
27782 && (hotspot = find_hot_spot (image_map, dx, dy),
27783 CONSP (hotspot))
27784 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27785 {
27786 Lisp_Object plist;
27787
27788 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27789 If so, we could look for mouse-enter, mouse-leave
27790 properties in PLIST (and do something...). */
27791 hotspot = XCDR (hotspot);
27792 if (CONSP (hotspot)
27793 && (plist = XCAR (hotspot), CONSP (plist)))
27794 {
27795 pointer = Fplist_get (plist, Qpointer);
27796 if (NILP (pointer))
27797 pointer = Qhand;
27798 help = Fplist_get (plist, Qhelp_echo);
27799 if (!NILP (help))
27800 {
27801 help_echo_string = help;
27802 XSETWINDOW (help_echo_window, w);
27803 help_echo_object = w->contents;
27804 help_echo_pos = charpos;
27805 }
27806 }
27807 }
27808 if (NILP (pointer))
27809 pointer = Fplist_get (XCDR (object), QCpointer);
27810 }
27811 #endif /* HAVE_WINDOW_SYSTEM */
27812
27813 if (STRINGP (string))
27814 pos = make_number (charpos);
27815
27816 /* Set the help text and mouse pointer. If the mouse is on a part
27817 of the mode line without any text (e.g. past the right edge of
27818 the mode line text), use the default help text and pointer. */
27819 if (STRINGP (string) || area == ON_MODE_LINE)
27820 {
27821 /* Arrange to display the help by setting the global variables
27822 help_echo_string, help_echo_object, and help_echo_pos. */
27823 if (NILP (help))
27824 {
27825 if (STRINGP (string))
27826 help = Fget_text_property (pos, Qhelp_echo, string);
27827
27828 if (!NILP (help))
27829 {
27830 help_echo_string = help;
27831 XSETWINDOW (help_echo_window, w);
27832 help_echo_object = string;
27833 help_echo_pos = charpos;
27834 }
27835 else if (area == ON_MODE_LINE)
27836 {
27837 Lisp_Object default_help
27838 = buffer_local_value_1 (Qmode_line_default_help_echo,
27839 w->contents);
27840
27841 if (STRINGP (default_help))
27842 {
27843 help_echo_string = default_help;
27844 XSETWINDOW (help_echo_window, w);
27845 help_echo_object = Qnil;
27846 help_echo_pos = -1;
27847 }
27848 }
27849 }
27850
27851 #ifdef HAVE_WINDOW_SYSTEM
27852 /* Change the mouse pointer according to what is under it. */
27853 if (FRAME_WINDOW_P (f))
27854 {
27855 dpyinfo = FRAME_X_DISPLAY_INFO (f);
27856 if (STRINGP (string))
27857 {
27858 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27859
27860 if (NILP (pointer))
27861 pointer = Fget_text_property (pos, Qpointer, string);
27862
27863 /* Change the mouse pointer according to what is under X/Y. */
27864 if (NILP (pointer)
27865 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27866 {
27867 Lisp_Object map;
27868 map = Fget_text_property (pos, Qlocal_map, string);
27869 if (!KEYMAPP (map))
27870 map = Fget_text_property (pos, Qkeymap, string);
27871 if (!KEYMAPP (map))
27872 cursor = dpyinfo->vertical_scroll_bar_cursor;
27873 }
27874 }
27875 else
27876 /* Default mode-line pointer. */
27877 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27878 }
27879 #endif
27880 }
27881
27882 /* Change the mouse face according to what is under X/Y. */
27883 if (STRINGP (string))
27884 {
27885 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27886 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
27887 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27888 && glyph)
27889 {
27890 Lisp_Object b, e;
27891
27892 struct glyph * tmp_glyph;
27893
27894 int gpos;
27895 int gseq_length;
27896 int total_pixel_width;
27897 ptrdiff_t begpos, endpos, ignore;
27898
27899 int vpos, hpos;
27900
27901 b = Fprevious_single_property_change (make_number (charpos + 1),
27902 Qmouse_face, string, Qnil);
27903 if (NILP (b))
27904 begpos = 0;
27905 else
27906 begpos = XINT (b);
27907
27908 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27909 if (NILP (e))
27910 endpos = SCHARS (string);
27911 else
27912 endpos = XINT (e);
27913
27914 /* Calculate the glyph position GPOS of GLYPH in the
27915 displayed string, relative to the beginning of the
27916 highlighted part of the string.
27917
27918 Note: GPOS is different from CHARPOS. CHARPOS is the
27919 position of GLYPH in the internal string object. A mode
27920 line string format has structures which are converted to
27921 a flattened string by the Emacs Lisp interpreter. The
27922 internal string is an element of those structures. The
27923 displayed string is the flattened string. */
27924 tmp_glyph = row_start_glyph;
27925 while (tmp_glyph < glyph
27926 && (!(EQ (tmp_glyph->object, glyph->object)
27927 && begpos <= tmp_glyph->charpos
27928 && tmp_glyph->charpos < endpos)))
27929 tmp_glyph++;
27930 gpos = glyph - tmp_glyph;
27931
27932 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27933 the highlighted part of the displayed string to which
27934 GLYPH belongs. Note: GSEQ_LENGTH is different from
27935 SCHARS (STRING), because the latter returns the length of
27936 the internal string. */
27937 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27938 tmp_glyph > glyph
27939 && (!(EQ (tmp_glyph->object, glyph->object)
27940 && begpos <= tmp_glyph->charpos
27941 && tmp_glyph->charpos < endpos));
27942 tmp_glyph--)
27943 ;
27944 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27945
27946 /* Calculate the total pixel width of all the glyphs between
27947 the beginning of the highlighted area and GLYPH. */
27948 total_pixel_width = 0;
27949 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27950 total_pixel_width += tmp_glyph->pixel_width;
27951
27952 /* Pre calculation of re-rendering position. Note: X is in
27953 column units here, after the call to mode_line_string or
27954 marginal_area_string. */
27955 hpos = x - gpos;
27956 vpos = (area == ON_MODE_LINE
27957 ? (w->current_matrix)->nrows - 1
27958 : 0);
27959
27960 /* If GLYPH's position is included in the region that is
27961 already drawn in mouse face, we have nothing to do. */
27962 if ( EQ (window, hlinfo->mouse_face_window)
27963 && (!row->reversed_p
27964 ? (hlinfo->mouse_face_beg_col <= hpos
27965 && hpos < hlinfo->mouse_face_end_col)
27966 /* In R2L rows we swap BEG and END, see below. */
27967 : (hlinfo->mouse_face_end_col <= hpos
27968 && hpos < hlinfo->mouse_face_beg_col))
27969 && hlinfo->mouse_face_beg_row == vpos )
27970 return;
27971
27972 if (clear_mouse_face (hlinfo))
27973 cursor = No_Cursor;
27974
27975 if (!row->reversed_p)
27976 {
27977 hlinfo->mouse_face_beg_col = hpos;
27978 hlinfo->mouse_face_beg_x = original_x_pixel
27979 - (total_pixel_width + dx);
27980 hlinfo->mouse_face_end_col = hpos + gseq_length;
27981 hlinfo->mouse_face_end_x = 0;
27982 }
27983 else
27984 {
27985 /* In R2L rows, show_mouse_face expects BEG and END
27986 coordinates to be swapped. */
27987 hlinfo->mouse_face_end_col = hpos;
27988 hlinfo->mouse_face_end_x = original_x_pixel
27989 - (total_pixel_width + dx);
27990 hlinfo->mouse_face_beg_col = hpos + gseq_length;
27991 hlinfo->mouse_face_beg_x = 0;
27992 }
27993
27994 hlinfo->mouse_face_beg_row = vpos;
27995 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
27996 hlinfo->mouse_face_beg_y = 0;
27997 hlinfo->mouse_face_end_y = 0;
27998 hlinfo->mouse_face_past_end = 0;
27999 hlinfo->mouse_face_window = window;
28000
28001 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
28002 charpos,
28003 0, 0, 0,
28004 &ignore,
28005 glyph->face_id,
28006 1);
28007 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28008
28009 if (NILP (pointer))
28010 pointer = Qhand;
28011 }
28012 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28013 clear_mouse_face (hlinfo);
28014 }
28015 #ifdef HAVE_WINDOW_SYSTEM
28016 if (FRAME_WINDOW_P (f))
28017 define_frame_cursor1 (f, cursor, pointer);
28018 #endif
28019 }
28020
28021
28022 /* EXPORT:
28023 Take proper action when the mouse has moved to position X, Y on
28024 frame F with regards to highlighting portions of display that have
28025 mouse-face properties. Also de-highlight portions of display where
28026 the mouse was before, set the mouse pointer shape as appropriate
28027 for the mouse coordinates, and activate help echo (tooltips).
28028 X and Y can be negative or out of range. */
28029
28030 void
28031 note_mouse_highlight (struct frame *f, int x, int y)
28032 {
28033 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28034 enum window_part part = ON_NOTHING;
28035 Lisp_Object window;
28036 struct window *w;
28037 Cursor cursor = No_Cursor;
28038 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
28039 struct buffer *b;
28040
28041 /* When a menu is active, don't highlight because this looks odd. */
28042 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
28043 if (popup_activated ())
28044 return;
28045 #endif
28046
28047 if (!f->glyphs_initialized_p
28048 || f->pointer_invisible)
28049 return;
28050
28051 hlinfo->mouse_face_mouse_x = x;
28052 hlinfo->mouse_face_mouse_y = y;
28053 hlinfo->mouse_face_mouse_frame = f;
28054
28055 if (hlinfo->mouse_face_defer)
28056 return;
28057
28058 /* Which window is that in? */
28059 window = window_from_coordinates (f, x, y, &part, 1);
28060
28061 /* If displaying active text in another window, clear that. */
28062 if (! EQ (window, hlinfo->mouse_face_window)
28063 /* Also clear if we move out of text area in same window. */
28064 || (!NILP (hlinfo->mouse_face_window)
28065 && !NILP (window)
28066 && part != ON_TEXT
28067 && part != ON_MODE_LINE
28068 && part != ON_HEADER_LINE))
28069 clear_mouse_face (hlinfo);
28070
28071 /* Not on a window -> return. */
28072 if (!WINDOWP (window))
28073 return;
28074
28075 /* Reset help_echo_string. It will get recomputed below. */
28076 help_echo_string = Qnil;
28077
28078 /* Convert to window-relative pixel coordinates. */
28079 w = XWINDOW (window);
28080 frame_to_window_pixel_xy (w, &x, &y);
28081
28082 #ifdef HAVE_WINDOW_SYSTEM
28083 /* Handle tool-bar window differently since it doesn't display a
28084 buffer. */
28085 if (EQ (window, f->tool_bar_window))
28086 {
28087 note_tool_bar_highlight (f, x, y);
28088 return;
28089 }
28090 #endif
28091
28092 /* Mouse is on the mode, header line or margin? */
28093 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
28094 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
28095 {
28096 note_mode_line_or_margin_highlight (window, x, y, part);
28097 return;
28098 }
28099
28100 #ifdef HAVE_WINDOW_SYSTEM
28101 if (part == ON_VERTICAL_BORDER)
28102 {
28103 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28104 help_echo_string = build_string ("drag-mouse-1: resize");
28105 }
28106 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
28107 || part == ON_SCROLL_BAR)
28108 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28109 else
28110 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28111 #endif
28112
28113 /* Are we in a window whose display is up to date?
28114 And verify the buffer's text has not changed. */
28115 b = XBUFFER (w->contents);
28116 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
28117 {
28118 int hpos, vpos, dx, dy, area = LAST_AREA;
28119 ptrdiff_t pos;
28120 struct glyph *glyph;
28121 Lisp_Object object;
28122 Lisp_Object mouse_face = Qnil, position;
28123 Lisp_Object *overlay_vec = NULL;
28124 ptrdiff_t i, noverlays;
28125 struct buffer *obuf;
28126 ptrdiff_t obegv, ozv;
28127 int same_region;
28128
28129 /* Find the glyph under X/Y. */
28130 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
28131
28132 #ifdef HAVE_WINDOW_SYSTEM
28133 /* Look for :pointer property on image. */
28134 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28135 {
28136 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
28137 if (img != NULL && IMAGEP (img->spec))
28138 {
28139 Lisp_Object image_map, hotspot;
28140 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
28141 !NILP (image_map))
28142 && (hotspot = find_hot_spot (image_map,
28143 glyph->slice.img.x + dx,
28144 glyph->slice.img.y + dy),
28145 CONSP (hotspot))
28146 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28147 {
28148 Lisp_Object plist;
28149
28150 /* Could check XCAR (hotspot) to see if we enter/leave
28151 this hot-spot.
28152 If so, we could look for mouse-enter, mouse-leave
28153 properties in PLIST (and do something...). */
28154 hotspot = XCDR (hotspot);
28155 if (CONSP (hotspot)
28156 && (plist = XCAR (hotspot), CONSP (plist)))
28157 {
28158 pointer = Fplist_get (plist, Qpointer);
28159 if (NILP (pointer))
28160 pointer = Qhand;
28161 help_echo_string = Fplist_get (plist, Qhelp_echo);
28162 if (!NILP (help_echo_string))
28163 {
28164 help_echo_window = window;
28165 help_echo_object = glyph->object;
28166 help_echo_pos = glyph->charpos;
28167 }
28168 }
28169 }
28170 if (NILP (pointer))
28171 pointer = Fplist_get (XCDR (img->spec), QCpointer);
28172 }
28173 }
28174 #endif /* HAVE_WINDOW_SYSTEM */
28175
28176 /* Clear mouse face if X/Y not over text. */
28177 if (glyph == NULL
28178 || area != TEXT_AREA
28179 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
28180 /* Glyph's OBJECT is an integer for glyphs inserted by the
28181 display engine for its internal purposes, like truncation
28182 and continuation glyphs and blanks beyond the end of
28183 line's text on text terminals. If we are over such a
28184 glyph, we are not over any text. */
28185 || INTEGERP (glyph->object)
28186 /* R2L rows have a stretch glyph at their front, which
28187 stands for no text, whereas L2R rows have no glyphs at
28188 all beyond the end of text. Treat such stretch glyphs
28189 like we do with NULL glyphs in L2R rows. */
28190 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
28191 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
28192 && glyph->type == STRETCH_GLYPH
28193 && glyph->avoid_cursor_p))
28194 {
28195 if (clear_mouse_face (hlinfo))
28196 cursor = No_Cursor;
28197 #ifdef HAVE_WINDOW_SYSTEM
28198 if (FRAME_WINDOW_P (f) && NILP (pointer))
28199 {
28200 if (area != TEXT_AREA)
28201 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28202 else
28203 pointer = Vvoid_text_area_pointer;
28204 }
28205 #endif
28206 goto set_cursor;
28207 }
28208
28209 pos = glyph->charpos;
28210 object = glyph->object;
28211 if (!STRINGP (object) && !BUFFERP (object))
28212 goto set_cursor;
28213
28214 /* If we get an out-of-range value, return now; avoid an error. */
28215 if (BUFFERP (object) && pos > BUF_Z (b))
28216 goto set_cursor;
28217
28218 /* Make the window's buffer temporarily current for
28219 overlays_at and compute_char_face. */
28220 obuf = current_buffer;
28221 current_buffer = b;
28222 obegv = BEGV;
28223 ozv = ZV;
28224 BEGV = BEG;
28225 ZV = Z;
28226
28227 /* Is this char mouse-active or does it have help-echo? */
28228 position = make_number (pos);
28229
28230 if (BUFFERP (object))
28231 {
28232 /* Put all the overlays we want in a vector in overlay_vec. */
28233 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
28234 /* Sort overlays into increasing priority order. */
28235 noverlays = sort_overlays (overlay_vec, noverlays, w);
28236 }
28237 else
28238 noverlays = 0;
28239
28240 if (NILP (Vmouse_highlight))
28241 {
28242 clear_mouse_face (hlinfo);
28243 goto check_help_echo;
28244 }
28245
28246 same_region = coords_in_mouse_face_p (w, hpos, vpos);
28247
28248 if (same_region)
28249 cursor = No_Cursor;
28250
28251 /* Check mouse-face highlighting. */
28252 if (! same_region
28253 /* If there exists an overlay with mouse-face overlapping
28254 the one we are currently highlighting, we have to
28255 check if we enter the overlapping overlay, and then
28256 highlight only that. */
28257 || (OVERLAYP (hlinfo->mouse_face_overlay)
28258 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
28259 {
28260 /* Find the highest priority overlay with a mouse-face. */
28261 Lisp_Object overlay = Qnil;
28262 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
28263 {
28264 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
28265 if (!NILP (mouse_face))
28266 overlay = overlay_vec[i];
28267 }
28268
28269 /* If we're highlighting the same overlay as before, there's
28270 no need to do that again. */
28271 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
28272 goto check_help_echo;
28273 hlinfo->mouse_face_overlay = overlay;
28274
28275 /* Clear the display of the old active region, if any. */
28276 if (clear_mouse_face (hlinfo))
28277 cursor = No_Cursor;
28278
28279 /* If no overlay applies, get a text property. */
28280 if (NILP (overlay))
28281 mouse_face = Fget_text_property (position, Qmouse_face, object);
28282
28283 /* Next, compute the bounds of the mouse highlighting and
28284 display it. */
28285 if (!NILP (mouse_face) && STRINGP (object))
28286 {
28287 /* The mouse-highlighting comes from a display string
28288 with a mouse-face. */
28289 Lisp_Object s, e;
28290 ptrdiff_t ignore;
28291
28292 s = Fprevious_single_property_change
28293 (make_number (pos + 1), Qmouse_face, object, Qnil);
28294 e = Fnext_single_property_change
28295 (position, Qmouse_face, object, Qnil);
28296 if (NILP (s))
28297 s = make_number (0);
28298 if (NILP (e))
28299 e = make_number (SCHARS (object) - 1);
28300 mouse_face_from_string_pos (w, hlinfo, object,
28301 XINT (s), XINT (e));
28302 hlinfo->mouse_face_past_end = 0;
28303 hlinfo->mouse_face_window = window;
28304 hlinfo->mouse_face_face_id
28305 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
28306 glyph->face_id, 1);
28307 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28308 cursor = No_Cursor;
28309 }
28310 else
28311 {
28312 /* The mouse-highlighting, if any, comes from an overlay
28313 or text property in the buffer. */
28314 Lisp_Object buffer IF_LINT (= Qnil);
28315 Lisp_Object disp_string IF_LINT (= Qnil);
28316
28317 if (STRINGP (object))
28318 {
28319 /* If we are on a display string with no mouse-face,
28320 check if the text under it has one. */
28321 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
28322 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28323 pos = string_buffer_position (object, start);
28324 if (pos > 0)
28325 {
28326 mouse_face = get_char_property_and_overlay
28327 (make_number (pos), Qmouse_face, w->contents, &overlay);
28328 buffer = w->contents;
28329 disp_string = object;
28330 }
28331 }
28332 else
28333 {
28334 buffer = object;
28335 disp_string = Qnil;
28336 }
28337
28338 if (!NILP (mouse_face))
28339 {
28340 Lisp_Object before, after;
28341 Lisp_Object before_string, after_string;
28342 /* To correctly find the limits of mouse highlight
28343 in a bidi-reordered buffer, we must not use the
28344 optimization of limiting the search in
28345 previous-single-property-change and
28346 next-single-property-change, because
28347 rows_from_pos_range needs the real start and end
28348 positions to DTRT in this case. That's because
28349 the first row visible in a window does not
28350 necessarily display the character whose position
28351 is the smallest. */
28352 Lisp_Object lim1 =
28353 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28354 ? Fmarker_position (w->start)
28355 : Qnil;
28356 Lisp_Object lim2 =
28357 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28358 ? make_number (BUF_Z (XBUFFER (buffer)) - w->window_end_pos)
28359 : Qnil;
28360
28361 if (NILP (overlay))
28362 {
28363 /* Handle the text property case. */
28364 before = Fprevious_single_property_change
28365 (make_number (pos + 1), Qmouse_face, buffer, lim1);
28366 after = Fnext_single_property_change
28367 (make_number (pos), Qmouse_face, buffer, lim2);
28368 before_string = after_string = Qnil;
28369 }
28370 else
28371 {
28372 /* Handle the overlay case. */
28373 before = Foverlay_start (overlay);
28374 after = Foverlay_end (overlay);
28375 before_string = Foverlay_get (overlay, Qbefore_string);
28376 after_string = Foverlay_get (overlay, Qafter_string);
28377
28378 if (!STRINGP (before_string)) before_string = Qnil;
28379 if (!STRINGP (after_string)) after_string = Qnil;
28380 }
28381
28382 mouse_face_from_buffer_pos (window, hlinfo, pos,
28383 NILP (before)
28384 ? 1
28385 : XFASTINT (before),
28386 NILP (after)
28387 ? BUF_Z (XBUFFER (buffer))
28388 : XFASTINT (after),
28389 before_string, after_string,
28390 disp_string);
28391 cursor = No_Cursor;
28392 }
28393 }
28394 }
28395
28396 check_help_echo:
28397
28398 /* Look for a `help-echo' property. */
28399 if (NILP (help_echo_string)) {
28400 Lisp_Object help, overlay;
28401
28402 /* Check overlays first. */
28403 help = overlay = Qnil;
28404 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
28405 {
28406 overlay = overlay_vec[i];
28407 help = Foverlay_get (overlay, Qhelp_echo);
28408 }
28409
28410 if (!NILP (help))
28411 {
28412 help_echo_string = help;
28413 help_echo_window = window;
28414 help_echo_object = overlay;
28415 help_echo_pos = pos;
28416 }
28417 else
28418 {
28419 Lisp_Object obj = glyph->object;
28420 ptrdiff_t charpos = glyph->charpos;
28421
28422 /* Try text properties. */
28423 if (STRINGP (obj)
28424 && charpos >= 0
28425 && charpos < SCHARS (obj))
28426 {
28427 help = Fget_text_property (make_number (charpos),
28428 Qhelp_echo, obj);
28429 if (NILP (help))
28430 {
28431 /* If the string itself doesn't specify a help-echo,
28432 see if the buffer text ``under'' it does. */
28433 struct glyph_row *r
28434 = MATRIX_ROW (w->current_matrix, vpos);
28435 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28436 ptrdiff_t p = string_buffer_position (obj, start);
28437 if (p > 0)
28438 {
28439 help = Fget_char_property (make_number (p),
28440 Qhelp_echo, w->contents);
28441 if (!NILP (help))
28442 {
28443 charpos = p;
28444 obj = w->contents;
28445 }
28446 }
28447 }
28448 }
28449 else if (BUFFERP (obj)
28450 && charpos >= BEGV
28451 && charpos < ZV)
28452 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28453 obj);
28454
28455 if (!NILP (help))
28456 {
28457 help_echo_string = help;
28458 help_echo_window = window;
28459 help_echo_object = obj;
28460 help_echo_pos = charpos;
28461 }
28462 }
28463 }
28464
28465 #ifdef HAVE_WINDOW_SYSTEM
28466 /* Look for a `pointer' property. */
28467 if (FRAME_WINDOW_P (f) && NILP (pointer))
28468 {
28469 /* Check overlays first. */
28470 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28471 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28472
28473 if (NILP (pointer))
28474 {
28475 Lisp_Object obj = glyph->object;
28476 ptrdiff_t charpos = glyph->charpos;
28477
28478 /* Try text properties. */
28479 if (STRINGP (obj)
28480 && charpos >= 0
28481 && charpos < SCHARS (obj))
28482 {
28483 pointer = Fget_text_property (make_number (charpos),
28484 Qpointer, obj);
28485 if (NILP (pointer))
28486 {
28487 /* If the string itself doesn't specify a pointer,
28488 see if the buffer text ``under'' it does. */
28489 struct glyph_row *r
28490 = MATRIX_ROW (w->current_matrix, vpos);
28491 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28492 ptrdiff_t p = string_buffer_position (obj, start);
28493 if (p > 0)
28494 pointer = Fget_char_property (make_number (p),
28495 Qpointer, w->contents);
28496 }
28497 }
28498 else if (BUFFERP (obj)
28499 && charpos >= BEGV
28500 && charpos < ZV)
28501 pointer = Fget_text_property (make_number (charpos),
28502 Qpointer, obj);
28503 }
28504 }
28505 #endif /* HAVE_WINDOW_SYSTEM */
28506
28507 BEGV = obegv;
28508 ZV = ozv;
28509 current_buffer = obuf;
28510 }
28511
28512 set_cursor:
28513
28514 #ifdef HAVE_WINDOW_SYSTEM
28515 if (FRAME_WINDOW_P (f))
28516 define_frame_cursor1 (f, cursor, pointer);
28517 #else
28518 /* This is here to prevent a compiler error, about "label at end of
28519 compound statement". */
28520 return;
28521 #endif
28522 }
28523
28524
28525 /* EXPORT for RIF:
28526 Clear any mouse-face on window W. This function is part of the
28527 redisplay interface, and is called from try_window_id and similar
28528 functions to ensure the mouse-highlight is off. */
28529
28530 void
28531 x_clear_window_mouse_face (struct window *w)
28532 {
28533 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28534 Lisp_Object window;
28535
28536 block_input ();
28537 XSETWINDOW (window, w);
28538 if (EQ (window, hlinfo->mouse_face_window))
28539 clear_mouse_face (hlinfo);
28540 unblock_input ();
28541 }
28542
28543
28544 /* EXPORT:
28545 Just discard the mouse face information for frame F, if any.
28546 This is used when the size of F is changed. */
28547
28548 void
28549 cancel_mouse_face (struct frame *f)
28550 {
28551 Lisp_Object window;
28552 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28553
28554 window = hlinfo->mouse_face_window;
28555 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28556 {
28557 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28558 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28559 hlinfo->mouse_face_window = Qnil;
28560 }
28561 }
28562
28563
28564 \f
28565 /***********************************************************************
28566 Exposure Events
28567 ***********************************************************************/
28568
28569 #ifdef HAVE_WINDOW_SYSTEM
28570
28571 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28572 which intersects rectangle R. R is in window-relative coordinates. */
28573
28574 static void
28575 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28576 enum glyph_row_area area)
28577 {
28578 struct glyph *first = row->glyphs[area];
28579 struct glyph *end = row->glyphs[area] + row->used[area];
28580 struct glyph *last;
28581 int first_x, start_x, x;
28582
28583 if (area == TEXT_AREA && row->fill_line_p)
28584 /* If row extends face to end of line write the whole line. */
28585 draw_glyphs (w, 0, row, area,
28586 0, row->used[area],
28587 DRAW_NORMAL_TEXT, 0);
28588 else
28589 {
28590 /* Set START_X to the window-relative start position for drawing glyphs of
28591 AREA. The first glyph of the text area can be partially visible.
28592 The first glyphs of other areas cannot. */
28593 start_x = window_box_left_offset (w, area);
28594 x = start_x;
28595 if (area == TEXT_AREA)
28596 x += row->x;
28597
28598 /* Find the first glyph that must be redrawn. */
28599 while (first < end
28600 && x + first->pixel_width < r->x)
28601 {
28602 x += first->pixel_width;
28603 ++first;
28604 }
28605
28606 /* Find the last one. */
28607 last = first;
28608 first_x = x;
28609 while (last < end
28610 && x < r->x + r->width)
28611 {
28612 x += last->pixel_width;
28613 ++last;
28614 }
28615
28616 /* Repaint. */
28617 if (last > first)
28618 draw_glyphs (w, first_x - start_x, row, area,
28619 first - row->glyphs[area], last - row->glyphs[area],
28620 DRAW_NORMAL_TEXT, 0);
28621 }
28622 }
28623
28624
28625 /* Redraw the parts of the glyph row ROW on window W intersecting
28626 rectangle R. R is in window-relative coordinates. Value is
28627 non-zero if mouse-face was overwritten. */
28628
28629 static int
28630 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28631 {
28632 eassert (row->enabled_p);
28633
28634 if (row->mode_line_p || w->pseudo_window_p)
28635 draw_glyphs (w, 0, row, TEXT_AREA,
28636 0, row->used[TEXT_AREA],
28637 DRAW_NORMAL_TEXT, 0);
28638 else
28639 {
28640 if (row->used[LEFT_MARGIN_AREA])
28641 expose_area (w, row, r, LEFT_MARGIN_AREA);
28642 if (row->used[TEXT_AREA])
28643 expose_area (w, row, r, TEXT_AREA);
28644 if (row->used[RIGHT_MARGIN_AREA])
28645 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28646 draw_row_fringe_bitmaps (w, row);
28647 }
28648
28649 return row->mouse_face_p;
28650 }
28651
28652
28653 /* Redraw those parts of glyphs rows during expose event handling that
28654 overlap other rows. Redrawing of an exposed line writes over parts
28655 of lines overlapping that exposed line; this function fixes that.
28656
28657 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28658 row in W's current matrix that is exposed and overlaps other rows.
28659 LAST_OVERLAPPING_ROW is the last such row. */
28660
28661 static void
28662 expose_overlaps (struct window *w,
28663 struct glyph_row *first_overlapping_row,
28664 struct glyph_row *last_overlapping_row,
28665 XRectangle *r)
28666 {
28667 struct glyph_row *row;
28668
28669 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28670 if (row->overlapping_p)
28671 {
28672 eassert (row->enabled_p && !row->mode_line_p);
28673
28674 row->clip = r;
28675 if (row->used[LEFT_MARGIN_AREA])
28676 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28677
28678 if (row->used[TEXT_AREA])
28679 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28680
28681 if (row->used[RIGHT_MARGIN_AREA])
28682 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28683 row->clip = NULL;
28684 }
28685 }
28686
28687
28688 /* Return non-zero if W's cursor intersects rectangle R. */
28689
28690 static int
28691 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28692 {
28693 XRectangle cr, result;
28694 struct glyph *cursor_glyph;
28695 struct glyph_row *row;
28696
28697 if (w->phys_cursor.vpos >= 0
28698 && w->phys_cursor.vpos < w->current_matrix->nrows
28699 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28700 row->enabled_p)
28701 && row->cursor_in_fringe_p)
28702 {
28703 /* Cursor is in the fringe. */
28704 cr.x = window_box_right_offset (w,
28705 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28706 ? RIGHT_MARGIN_AREA
28707 : TEXT_AREA));
28708 cr.y = row->y;
28709 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28710 cr.height = row->height;
28711 return x_intersect_rectangles (&cr, r, &result);
28712 }
28713
28714 cursor_glyph = get_phys_cursor_glyph (w);
28715 if (cursor_glyph)
28716 {
28717 /* r is relative to W's box, but w->phys_cursor.x is relative
28718 to left edge of W's TEXT area. Adjust it. */
28719 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28720 cr.y = w->phys_cursor.y;
28721 cr.width = cursor_glyph->pixel_width;
28722 cr.height = w->phys_cursor_height;
28723 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28724 I assume the effect is the same -- and this is portable. */
28725 return x_intersect_rectangles (&cr, r, &result);
28726 }
28727 /* If we don't understand the format, pretend we're not in the hot-spot. */
28728 return 0;
28729 }
28730
28731
28732 /* EXPORT:
28733 Draw a vertical window border to the right of window W if W doesn't
28734 have vertical scroll bars. */
28735
28736 void
28737 x_draw_vertical_border (struct window *w)
28738 {
28739 struct frame *f = XFRAME (WINDOW_FRAME (w));
28740
28741 /* We could do better, if we knew what type of scroll-bar the adjacent
28742 windows (on either side) have... But we don't :-(
28743 However, I think this works ok. ++KFS 2003-04-25 */
28744
28745 /* Redraw borders between horizontally adjacent windows. Don't
28746 do it for frames with vertical scroll bars because either the
28747 right scroll bar of a window, or the left scroll bar of its
28748 neighbor will suffice as a border. */
28749 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28750 return;
28751
28752 /* Note: It is necessary to redraw both the left and the right
28753 borders, for when only this single window W is being
28754 redisplayed. */
28755 if (!WINDOW_RIGHTMOST_P (w)
28756 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28757 {
28758 int x0, x1, y0, y1;
28759
28760 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28761 y1 -= 1;
28762
28763 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28764 x1 -= 1;
28765
28766 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28767 }
28768 if (!WINDOW_LEFTMOST_P (w)
28769 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28770 {
28771 int x0, x1, y0, y1;
28772
28773 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28774 y1 -= 1;
28775
28776 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28777 x0 -= 1;
28778
28779 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28780 }
28781 }
28782
28783
28784 /* Redraw the part of window W intersection rectangle FR. Pixel
28785 coordinates in FR are frame-relative. Call this function with
28786 input blocked. Value is non-zero if the exposure overwrites
28787 mouse-face. */
28788
28789 static int
28790 expose_window (struct window *w, XRectangle *fr)
28791 {
28792 struct frame *f = XFRAME (w->frame);
28793 XRectangle wr, r;
28794 int mouse_face_overwritten_p = 0;
28795
28796 /* If window is not yet fully initialized, do nothing. This can
28797 happen when toolkit scroll bars are used and a window is split.
28798 Reconfiguring the scroll bar will generate an expose for a newly
28799 created window. */
28800 if (w->current_matrix == NULL)
28801 return 0;
28802
28803 /* When we're currently updating the window, display and current
28804 matrix usually don't agree. Arrange for a thorough display
28805 later. */
28806 if (w->must_be_updated_p)
28807 {
28808 SET_FRAME_GARBAGED (f);
28809 return 0;
28810 }
28811
28812 /* Frame-relative pixel rectangle of W. */
28813 wr.x = WINDOW_LEFT_EDGE_X (w);
28814 wr.y = WINDOW_TOP_EDGE_Y (w);
28815 wr.width = WINDOW_TOTAL_WIDTH (w);
28816 wr.height = WINDOW_TOTAL_HEIGHT (w);
28817
28818 if (x_intersect_rectangles (fr, &wr, &r))
28819 {
28820 int yb = window_text_bottom_y (w);
28821 struct glyph_row *row;
28822 int cursor_cleared_p, phys_cursor_on_p;
28823 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28824
28825 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28826 r.x, r.y, r.width, r.height));
28827
28828 /* Convert to window coordinates. */
28829 r.x -= WINDOW_LEFT_EDGE_X (w);
28830 r.y -= WINDOW_TOP_EDGE_Y (w);
28831
28832 /* Turn off the cursor. */
28833 if (!w->pseudo_window_p
28834 && phys_cursor_in_rect_p (w, &r))
28835 {
28836 x_clear_cursor (w);
28837 cursor_cleared_p = 1;
28838 }
28839 else
28840 cursor_cleared_p = 0;
28841
28842 /* If the row containing the cursor extends face to end of line,
28843 then expose_area might overwrite the cursor outside the
28844 rectangle and thus notice_overwritten_cursor might clear
28845 w->phys_cursor_on_p. We remember the original value and
28846 check later if it is changed. */
28847 phys_cursor_on_p = w->phys_cursor_on_p;
28848
28849 /* Update lines intersecting rectangle R. */
28850 first_overlapping_row = last_overlapping_row = NULL;
28851 for (row = w->current_matrix->rows;
28852 row->enabled_p;
28853 ++row)
28854 {
28855 int y0 = row->y;
28856 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28857
28858 if ((y0 >= r.y && y0 < r.y + r.height)
28859 || (y1 > r.y && y1 < r.y + r.height)
28860 || (r.y >= y0 && r.y < y1)
28861 || (r.y + r.height > y0 && r.y + r.height < y1))
28862 {
28863 /* A header line may be overlapping, but there is no need
28864 to fix overlapping areas for them. KFS 2005-02-12 */
28865 if (row->overlapping_p && !row->mode_line_p)
28866 {
28867 if (first_overlapping_row == NULL)
28868 first_overlapping_row = row;
28869 last_overlapping_row = row;
28870 }
28871
28872 row->clip = fr;
28873 if (expose_line (w, row, &r))
28874 mouse_face_overwritten_p = 1;
28875 row->clip = NULL;
28876 }
28877 else if (row->overlapping_p)
28878 {
28879 /* We must redraw a row overlapping the exposed area. */
28880 if (y0 < r.y
28881 ? y0 + row->phys_height > r.y
28882 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28883 {
28884 if (first_overlapping_row == NULL)
28885 first_overlapping_row = row;
28886 last_overlapping_row = row;
28887 }
28888 }
28889
28890 if (y1 >= yb)
28891 break;
28892 }
28893
28894 /* Display the mode line if there is one. */
28895 if (WINDOW_WANTS_MODELINE_P (w)
28896 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28897 row->enabled_p)
28898 && row->y < r.y + r.height)
28899 {
28900 if (expose_line (w, row, &r))
28901 mouse_face_overwritten_p = 1;
28902 }
28903
28904 if (!w->pseudo_window_p)
28905 {
28906 /* Fix the display of overlapping rows. */
28907 if (first_overlapping_row)
28908 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28909 fr);
28910
28911 /* Draw border between windows. */
28912 x_draw_vertical_border (w);
28913
28914 /* Turn the cursor on again. */
28915 if (cursor_cleared_p
28916 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28917 update_window_cursor (w, 1);
28918 }
28919 }
28920
28921 return mouse_face_overwritten_p;
28922 }
28923
28924
28925
28926 /* Redraw (parts) of all windows in the window tree rooted at W that
28927 intersect R. R contains frame pixel coordinates. Value is
28928 non-zero if the exposure overwrites mouse-face. */
28929
28930 static int
28931 expose_window_tree (struct window *w, XRectangle *r)
28932 {
28933 struct frame *f = XFRAME (w->frame);
28934 int mouse_face_overwritten_p = 0;
28935
28936 while (w && !FRAME_GARBAGED_P (f))
28937 {
28938 if (WINDOWP (w->contents))
28939 mouse_face_overwritten_p
28940 |= expose_window_tree (XWINDOW (w->contents), r);
28941 else
28942 mouse_face_overwritten_p |= expose_window (w, r);
28943
28944 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28945 }
28946
28947 return mouse_face_overwritten_p;
28948 }
28949
28950
28951 /* EXPORT:
28952 Redisplay an exposed area of frame F. X and Y are the upper-left
28953 corner of the exposed rectangle. W and H are width and height of
28954 the exposed area. All are pixel values. W or H zero means redraw
28955 the entire frame. */
28956
28957 void
28958 expose_frame (struct frame *f, int x, int y, int w, int h)
28959 {
28960 XRectangle r;
28961 int mouse_face_overwritten_p = 0;
28962
28963 TRACE ((stderr, "expose_frame "));
28964
28965 /* No need to redraw if frame will be redrawn soon. */
28966 if (FRAME_GARBAGED_P (f))
28967 {
28968 TRACE ((stderr, " garbaged\n"));
28969 return;
28970 }
28971
28972 /* If basic faces haven't been realized yet, there is no point in
28973 trying to redraw anything. This can happen when we get an expose
28974 event while Emacs is starting, e.g. by moving another window. */
28975 if (FRAME_FACE_CACHE (f) == NULL
28976 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28977 {
28978 TRACE ((stderr, " no faces\n"));
28979 return;
28980 }
28981
28982 if (w == 0 || h == 0)
28983 {
28984 r.x = r.y = 0;
28985 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
28986 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
28987 }
28988 else
28989 {
28990 r.x = x;
28991 r.y = y;
28992 r.width = w;
28993 r.height = h;
28994 }
28995
28996 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
28997 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
28998
28999 if (WINDOWP (f->tool_bar_window))
29000 mouse_face_overwritten_p
29001 |= expose_window (XWINDOW (f->tool_bar_window), &r);
29002
29003 #ifdef HAVE_X_WINDOWS
29004 #ifndef MSDOS
29005 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
29006 if (WINDOWP (f->menu_bar_window))
29007 mouse_face_overwritten_p
29008 |= expose_window (XWINDOW (f->menu_bar_window), &r);
29009 #endif /* not USE_X_TOOLKIT and not USE_GTK */
29010 #endif
29011 #endif
29012
29013 /* Some window managers support a focus-follows-mouse style with
29014 delayed raising of frames. Imagine a partially obscured frame,
29015 and moving the mouse into partially obscured mouse-face on that
29016 frame. The visible part of the mouse-face will be highlighted,
29017 then the WM raises the obscured frame. With at least one WM, KDE
29018 2.1, Emacs is not getting any event for the raising of the frame
29019 (even tried with SubstructureRedirectMask), only Expose events.
29020 These expose events will draw text normally, i.e. not
29021 highlighted. Which means we must redo the highlight here.
29022 Subsume it under ``we love X''. --gerd 2001-08-15 */
29023 /* Included in Windows version because Windows most likely does not
29024 do the right thing if any third party tool offers
29025 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
29026 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
29027 {
29028 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29029 if (f == hlinfo->mouse_face_mouse_frame)
29030 {
29031 int mouse_x = hlinfo->mouse_face_mouse_x;
29032 int mouse_y = hlinfo->mouse_face_mouse_y;
29033 clear_mouse_face (hlinfo);
29034 note_mouse_highlight (f, mouse_x, mouse_y);
29035 }
29036 }
29037 }
29038
29039
29040 /* EXPORT:
29041 Determine the intersection of two rectangles R1 and R2. Return
29042 the intersection in *RESULT. Value is non-zero if RESULT is not
29043 empty. */
29044
29045 int
29046 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
29047 {
29048 XRectangle *left, *right;
29049 XRectangle *upper, *lower;
29050 int intersection_p = 0;
29051
29052 /* Rearrange so that R1 is the left-most rectangle. */
29053 if (r1->x < r2->x)
29054 left = r1, right = r2;
29055 else
29056 left = r2, right = r1;
29057
29058 /* X0 of the intersection is right.x0, if this is inside R1,
29059 otherwise there is no intersection. */
29060 if (right->x <= left->x + left->width)
29061 {
29062 result->x = right->x;
29063
29064 /* The right end of the intersection is the minimum of
29065 the right ends of left and right. */
29066 result->width = (min (left->x + left->width, right->x + right->width)
29067 - result->x);
29068
29069 /* Same game for Y. */
29070 if (r1->y < r2->y)
29071 upper = r1, lower = r2;
29072 else
29073 upper = r2, lower = r1;
29074
29075 /* The upper end of the intersection is lower.y0, if this is inside
29076 of upper. Otherwise, there is no intersection. */
29077 if (lower->y <= upper->y + upper->height)
29078 {
29079 result->y = lower->y;
29080
29081 /* The lower end of the intersection is the minimum of the lower
29082 ends of upper and lower. */
29083 result->height = (min (lower->y + lower->height,
29084 upper->y + upper->height)
29085 - result->y);
29086 intersection_p = 1;
29087 }
29088 }
29089
29090 return intersection_p;
29091 }
29092
29093 #endif /* HAVE_WINDOW_SYSTEM */
29094
29095 \f
29096 /***********************************************************************
29097 Initialization
29098 ***********************************************************************/
29099
29100 void
29101 syms_of_xdisp (void)
29102 {
29103 Vwith_echo_area_save_vector = Qnil;
29104 staticpro (&Vwith_echo_area_save_vector);
29105
29106 Vmessage_stack = Qnil;
29107 staticpro (&Vmessage_stack);
29108
29109 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
29110 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
29111
29112 message_dolog_marker1 = Fmake_marker ();
29113 staticpro (&message_dolog_marker1);
29114 message_dolog_marker2 = Fmake_marker ();
29115 staticpro (&message_dolog_marker2);
29116 message_dolog_marker3 = Fmake_marker ();
29117 staticpro (&message_dolog_marker3);
29118
29119 #ifdef GLYPH_DEBUG
29120 defsubr (&Sdump_frame_glyph_matrix);
29121 defsubr (&Sdump_glyph_matrix);
29122 defsubr (&Sdump_glyph_row);
29123 defsubr (&Sdump_tool_bar_row);
29124 defsubr (&Strace_redisplay);
29125 defsubr (&Strace_to_stderr);
29126 #endif
29127 #ifdef HAVE_WINDOW_SYSTEM
29128 defsubr (&Stool_bar_lines_needed);
29129 defsubr (&Slookup_image_map);
29130 #endif
29131 defsubr (&Sline_pixel_height);
29132 defsubr (&Sformat_mode_line);
29133 defsubr (&Sinvisible_p);
29134 defsubr (&Scurrent_bidi_paragraph_direction);
29135 defsubr (&Smove_point_visually);
29136
29137 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
29138 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
29139 DEFSYM (Qoverriding_local_map, "overriding-local-map");
29140 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
29141 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
29142 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
29143 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
29144 DEFSYM (Qeval, "eval");
29145 DEFSYM (QCdata, ":data");
29146 DEFSYM (Qdisplay, "display");
29147 DEFSYM (Qspace_width, "space-width");
29148 DEFSYM (Qraise, "raise");
29149 DEFSYM (Qslice, "slice");
29150 DEFSYM (Qspace, "space");
29151 DEFSYM (Qmargin, "margin");
29152 DEFSYM (Qpointer, "pointer");
29153 DEFSYM (Qleft_margin, "left-margin");
29154 DEFSYM (Qright_margin, "right-margin");
29155 DEFSYM (Qcenter, "center");
29156 DEFSYM (Qline_height, "line-height");
29157 DEFSYM (QCalign_to, ":align-to");
29158 DEFSYM (QCrelative_width, ":relative-width");
29159 DEFSYM (QCrelative_height, ":relative-height");
29160 DEFSYM (QCeval, ":eval");
29161 DEFSYM (QCpropertize, ":propertize");
29162 DEFSYM (QCfile, ":file");
29163 DEFSYM (Qfontified, "fontified");
29164 DEFSYM (Qfontification_functions, "fontification-functions");
29165 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
29166 DEFSYM (Qescape_glyph, "escape-glyph");
29167 DEFSYM (Qnobreak_space, "nobreak-space");
29168 DEFSYM (Qimage, "image");
29169 DEFSYM (Qtext, "text");
29170 DEFSYM (Qboth, "both");
29171 DEFSYM (Qboth_horiz, "both-horiz");
29172 DEFSYM (Qtext_image_horiz, "text-image-horiz");
29173 DEFSYM (QCmap, ":map");
29174 DEFSYM (QCpointer, ":pointer");
29175 DEFSYM (Qrect, "rect");
29176 DEFSYM (Qcircle, "circle");
29177 DEFSYM (Qpoly, "poly");
29178 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
29179 DEFSYM (Qgrow_only, "grow-only");
29180 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
29181 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
29182 DEFSYM (Qposition, "position");
29183 DEFSYM (Qbuffer_position, "buffer-position");
29184 DEFSYM (Qobject, "object");
29185 DEFSYM (Qbar, "bar");
29186 DEFSYM (Qhbar, "hbar");
29187 DEFSYM (Qbox, "box");
29188 DEFSYM (Qhollow, "hollow");
29189 DEFSYM (Qhand, "hand");
29190 DEFSYM (Qarrow, "arrow");
29191 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
29192
29193 list_of_error = list1 (list2 (intern_c_string ("error"),
29194 intern_c_string ("void-variable")));
29195 staticpro (&list_of_error);
29196
29197 DEFSYM (Qlast_arrow_position, "last-arrow-position");
29198 DEFSYM (Qlast_arrow_string, "last-arrow-string");
29199 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
29200 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
29201
29202 echo_buffer[0] = echo_buffer[1] = Qnil;
29203 staticpro (&echo_buffer[0]);
29204 staticpro (&echo_buffer[1]);
29205
29206 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
29207 staticpro (&echo_area_buffer[0]);
29208 staticpro (&echo_area_buffer[1]);
29209
29210 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
29211 staticpro (&Vmessages_buffer_name);
29212
29213 mode_line_proptrans_alist = Qnil;
29214 staticpro (&mode_line_proptrans_alist);
29215 mode_line_string_list = Qnil;
29216 staticpro (&mode_line_string_list);
29217 mode_line_string_face = Qnil;
29218 staticpro (&mode_line_string_face);
29219 mode_line_string_face_prop = Qnil;
29220 staticpro (&mode_line_string_face_prop);
29221 Vmode_line_unwind_vector = Qnil;
29222 staticpro (&Vmode_line_unwind_vector);
29223
29224 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
29225
29226 help_echo_string = Qnil;
29227 staticpro (&help_echo_string);
29228 help_echo_object = Qnil;
29229 staticpro (&help_echo_object);
29230 help_echo_window = Qnil;
29231 staticpro (&help_echo_window);
29232 previous_help_echo_string = Qnil;
29233 staticpro (&previous_help_echo_string);
29234 help_echo_pos = -1;
29235
29236 DEFSYM (Qright_to_left, "right-to-left");
29237 DEFSYM (Qleft_to_right, "left-to-right");
29238
29239 #ifdef HAVE_WINDOW_SYSTEM
29240 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
29241 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
29242 For example, if a block cursor is over a tab, it will be drawn as
29243 wide as that tab on the display. */);
29244 x_stretch_cursor_p = 0;
29245 #endif
29246
29247 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
29248 doc: /* Non-nil means highlight trailing whitespace.
29249 The face used for trailing whitespace is `trailing-whitespace'. */);
29250 Vshow_trailing_whitespace = Qnil;
29251
29252 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
29253 doc: /* Control highlighting of non-ASCII space and hyphen chars.
29254 If the value is t, Emacs highlights non-ASCII chars which have the
29255 same appearance as an ASCII space or hyphen, using the `nobreak-space'
29256 or `escape-glyph' face respectively.
29257
29258 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
29259 U+2011 (non-breaking hyphen) are affected.
29260
29261 Any other non-nil value means to display these characters as a escape
29262 glyph followed by an ordinary space or hyphen.
29263
29264 A value of nil means no special handling of these characters. */);
29265 Vnobreak_char_display = Qt;
29266
29267 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
29268 doc: /* The pointer shape to show in void text areas.
29269 A value of nil means to show the text pointer. Other options are `arrow',
29270 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
29271 Vvoid_text_area_pointer = Qarrow;
29272
29273 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
29274 doc: /* Non-nil means don't actually do any redisplay.
29275 This is used for internal purposes. */);
29276 Vinhibit_redisplay = Qnil;
29277
29278 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
29279 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
29280 Vglobal_mode_string = Qnil;
29281
29282 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
29283 doc: /* Marker for where to display an arrow on top of the buffer text.
29284 This must be the beginning of a line in order to work.
29285 See also `overlay-arrow-string'. */);
29286 Voverlay_arrow_position = Qnil;
29287
29288 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
29289 doc: /* String to display as an arrow in non-window frames.
29290 See also `overlay-arrow-position'. */);
29291 Voverlay_arrow_string = build_pure_c_string ("=>");
29292
29293 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
29294 doc: /* List of variables (symbols) which hold markers for overlay arrows.
29295 The symbols on this list are examined during redisplay to determine
29296 where to display overlay arrows. */);
29297 Voverlay_arrow_variable_list
29298 = list1 (intern_c_string ("overlay-arrow-position"));
29299
29300 DEFVAR_INT ("scroll-step", emacs_scroll_step,
29301 doc: /* The number of lines to try scrolling a window by when point moves out.
29302 If that fails to bring point back on frame, point is centered instead.
29303 If this is zero, point is always centered after it moves off frame.
29304 If you want scrolling to always be a line at a time, you should set
29305 `scroll-conservatively' to a large value rather than set this to 1. */);
29306
29307 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
29308 doc: /* Scroll up to this many lines, to bring point back on screen.
29309 If point moves off-screen, redisplay will scroll by up to
29310 `scroll-conservatively' lines in order to bring point just barely
29311 onto the screen again. If that cannot be done, then redisplay
29312 recenters point as usual.
29313
29314 If the value is greater than 100, redisplay will never recenter point,
29315 but will always scroll just enough text to bring point into view, even
29316 if you move far away.
29317
29318 A value of zero means always recenter point if it moves off screen. */);
29319 scroll_conservatively = 0;
29320
29321 DEFVAR_INT ("scroll-margin", scroll_margin,
29322 doc: /* Number of lines of margin at the top and bottom of a window.
29323 Recenter the window whenever point gets within this many lines
29324 of the top or bottom of the window. */);
29325 scroll_margin = 0;
29326
29327 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
29328 doc: /* Pixels per inch value for non-window system displays.
29329 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
29330 Vdisplay_pixels_per_inch = make_float (72.0);
29331
29332 #ifdef GLYPH_DEBUG
29333 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
29334 #endif
29335
29336 DEFVAR_LISP ("truncate-partial-width-windows",
29337 Vtruncate_partial_width_windows,
29338 doc: /* Non-nil means truncate lines in windows narrower than the frame.
29339 For an integer value, truncate lines in each window narrower than the
29340 full frame width, provided the window width is less than that integer;
29341 otherwise, respect the value of `truncate-lines'.
29342
29343 For any other non-nil value, truncate lines in all windows that do
29344 not span the full frame width.
29345
29346 A value of nil means to respect the value of `truncate-lines'.
29347
29348 If `word-wrap' is enabled, you might want to reduce this. */);
29349 Vtruncate_partial_width_windows = make_number (50);
29350
29351 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
29352 doc: /* Maximum buffer size for which line number should be displayed.
29353 If the buffer is bigger than this, the line number does not appear
29354 in the mode line. A value of nil means no limit. */);
29355 Vline_number_display_limit = Qnil;
29356
29357 DEFVAR_INT ("line-number-display-limit-width",
29358 line_number_display_limit_width,
29359 doc: /* Maximum line width (in characters) for line number display.
29360 If the average length of the lines near point is bigger than this, then the
29361 line number may be omitted from the mode line. */);
29362 line_number_display_limit_width = 200;
29363
29364 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
29365 doc: /* Non-nil means highlight region even in nonselected windows. */);
29366 highlight_nonselected_windows = 0;
29367
29368 DEFVAR_BOOL ("multiple-frames", multiple_frames,
29369 doc: /* Non-nil if more than one frame is visible on this display.
29370 Minibuffer-only frames don't count, but iconified frames do.
29371 This variable is not guaranteed to be accurate except while processing
29372 `frame-title-format' and `icon-title-format'. */);
29373
29374 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
29375 doc: /* Template for displaying the title bar of visible frames.
29376 \(Assuming the window manager supports this feature.)
29377
29378 This variable has the same structure as `mode-line-format', except that
29379 the %c and %l constructs are ignored. It is used only on frames for
29380 which no explicit name has been set \(see `modify-frame-parameters'). */);
29381
29382 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
29383 doc: /* Template for displaying the title bar of an iconified frame.
29384 \(Assuming the window manager supports this feature.)
29385 This variable has the same structure as `mode-line-format' (which see),
29386 and is used only on frames for which no explicit name has been set
29387 \(see `modify-frame-parameters'). */);
29388 Vicon_title_format
29389 = Vframe_title_format
29390 = listn (CONSTYPE_PURE, 3,
29391 intern_c_string ("multiple-frames"),
29392 build_pure_c_string ("%b"),
29393 listn (CONSTYPE_PURE, 4,
29394 empty_unibyte_string,
29395 intern_c_string ("invocation-name"),
29396 build_pure_c_string ("@"),
29397 intern_c_string ("system-name")));
29398
29399 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
29400 doc: /* Maximum number of lines to keep in the message log buffer.
29401 If nil, disable message logging. If t, log messages but don't truncate
29402 the buffer when it becomes large. */);
29403 Vmessage_log_max = make_number (1000);
29404
29405 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
29406 doc: /* Functions called before redisplay, if window sizes have changed.
29407 The value should be a list of functions that take one argument.
29408 Just before redisplay, for each frame, if any of its windows have changed
29409 size since the last redisplay, or have been split or deleted,
29410 all the functions in the list are called, with the frame as argument. */);
29411 Vwindow_size_change_functions = Qnil;
29412
29413 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
29414 doc: /* List of functions to call before redisplaying a window with scrolling.
29415 Each function is called with two arguments, the window and its new
29416 display-start position. Note that these functions are also called by
29417 `set-window-buffer'. Also note that the value of `window-end' is not
29418 valid when these functions are called.
29419
29420 Warning: Do not use this feature to alter the way the window
29421 is scrolled. It is not designed for that, and such use probably won't
29422 work. */);
29423 Vwindow_scroll_functions = Qnil;
29424
29425 DEFVAR_LISP ("window-text-change-functions",
29426 Vwindow_text_change_functions,
29427 doc: /* Functions to call in redisplay when text in the window might change. */);
29428 Vwindow_text_change_functions = Qnil;
29429
29430 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
29431 doc: /* Functions called when redisplay of a window reaches the end trigger.
29432 Each function is called with two arguments, the window and the end trigger value.
29433 See `set-window-redisplay-end-trigger'. */);
29434 Vredisplay_end_trigger_functions = Qnil;
29435
29436 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
29437 doc: /* Non-nil means autoselect window with mouse pointer.
29438 If nil, do not autoselect windows.
29439 A positive number means delay autoselection by that many seconds: a
29440 window is autoselected only after the mouse has remained in that
29441 window for the duration of the delay.
29442 A negative number has a similar effect, but causes windows to be
29443 autoselected only after the mouse has stopped moving. \(Because of
29444 the way Emacs compares mouse events, you will occasionally wait twice
29445 that time before the window gets selected.\)
29446 Any other value means to autoselect window instantaneously when the
29447 mouse pointer enters it.
29448
29449 Autoselection selects the minibuffer only if it is active, and never
29450 unselects the minibuffer if it is active.
29451
29452 When customizing this variable make sure that the actual value of
29453 `focus-follows-mouse' matches the behavior of your window manager. */);
29454 Vmouse_autoselect_window = Qnil;
29455
29456 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29457 doc: /* Non-nil means automatically resize tool-bars.
29458 This dynamically changes the tool-bar's height to the minimum height
29459 that is needed to make all tool-bar items visible.
29460 If value is `grow-only', the tool-bar's height is only increased
29461 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29462 Vauto_resize_tool_bars = Qt;
29463
29464 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29465 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29466 auto_raise_tool_bar_buttons_p = 1;
29467
29468 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29469 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29470 make_cursor_line_fully_visible_p = 1;
29471
29472 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29473 doc: /* Border below tool-bar in pixels.
29474 If an integer, use it as the height of the border.
29475 If it is one of `internal-border-width' or `border-width', use the
29476 value of the corresponding frame parameter.
29477 Otherwise, no border is added below the tool-bar. */);
29478 Vtool_bar_border = Qinternal_border_width;
29479
29480 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29481 doc: /* Margin around tool-bar buttons in pixels.
29482 If an integer, use that for both horizontal and vertical margins.
29483 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29484 HORZ specifying the horizontal margin, and VERT specifying the
29485 vertical margin. */);
29486 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29487
29488 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29489 doc: /* Relief thickness of tool-bar buttons. */);
29490 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29491
29492 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29493 doc: /* Tool bar style to use.
29494 It can be one of
29495 image - show images only
29496 text - show text only
29497 both - show both, text below image
29498 both-horiz - show text to the right of the image
29499 text-image-horiz - show text to the left of the image
29500 any other - use system default or image if no system default.
29501
29502 This variable only affects the GTK+ toolkit version of Emacs. */);
29503 Vtool_bar_style = Qnil;
29504
29505 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29506 doc: /* Maximum number of characters a label can have to be shown.
29507 The tool bar style must also show labels for this to have any effect, see
29508 `tool-bar-style'. */);
29509 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29510
29511 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29512 doc: /* List of functions to call to fontify regions of text.
29513 Each function is called with one argument POS. Functions must
29514 fontify a region starting at POS in the current buffer, and give
29515 fontified regions the property `fontified'. */);
29516 Vfontification_functions = Qnil;
29517 Fmake_variable_buffer_local (Qfontification_functions);
29518
29519 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29520 unibyte_display_via_language_environment,
29521 doc: /* Non-nil means display unibyte text according to language environment.
29522 Specifically, this means that raw bytes in the range 160-255 decimal
29523 are displayed by converting them to the equivalent multibyte characters
29524 according to the current language environment. As a result, they are
29525 displayed according to the current fontset.
29526
29527 Note that this variable affects only how these bytes are displayed,
29528 but does not change the fact they are interpreted as raw bytes. */);
29529 unibyte_display_via_language_environment = 0;
29530
29531 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29532 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29533 If a float, it specifies a fraction of the mini-window frame's height.
29534 If an integer, it specifies a number of lines. */);
29535 Vmax_mini_window_height = make_float (0.25);
29536
29537 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29538 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29539 A value of nil means don't automatically resize mini-windows.
29540 A value of t means resize them to fit the text displayed in them.
29541 A value of `grow-only', the default, means let mini-windows grow only;
29542 they return to their normal size when the minibuffer is closed, or the
29543 echo area becomes empty. */);
29544 Vresize_mini_windows = Qgrow_only;
29545
29546 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29547 doc: /* Alist specifying how to blink the cursor off.
29548 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29549 `cursor-type' frame-parameter or variable equals ON-STATE,
29550 comparing using `equal', Emacs uses OFF-STATE to specify
29551 how to blink it off. ON-STATE and OFF-STATE are values for
29552 the `cursor-type' frame parameter.
29553
29554 If a frame's ON-STATE has no entry in this list,
29555 the frame's other specifications determine how to blink the cursor off. */);
29556 Vblink_cursor_alist = Qnil;
29557
29558 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29559 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29560 If non-nil, windows are automatically scrolled horizontally to make
29561 point visible. */);
29562 automatic_hscrolling_p = 1;
29563 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29564
29565 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29566 doc: /* How many columns away from the window edge point is allowed to get
29567 before automatic hscrolling will horizontally scroll the window. */);
29568 hscroll_margin = 5;
29569
29570 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29571 doc: /* How many columns to scroll the window when point gets too close to the edge.
29572 When point is less than `hscroll-margin' columns from the window
29573 edge, automatic hscrolling will scroll the window by the amount of columns
29574 determined by this variable. If its value is a positive integer, scroll that
29575 many columns. If it's a positive floating-point number, it specifies the
29576 fraction of the window's width to scroll. If it's nil or zero, point will be
29577 centered horizontally after the scroll. Any other value, including negative
29578 numbers, are treated as if the value were zero.
29579
29580 Automatic hscrolling always moves point outside the scroll margin, so if
29581 point was more than scroll step columns inside the margin, the window will
29582 scroll more than the value given by the scroll step.
29583
29584 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29585 and `scroll-right' overrides this variable's effect. */);
29586 Vhscroll_step = make_number (0);
29587
29588 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29589 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29590 Bind this around calls to `message' to let it take effect. */);
29591 message_truncate_lines = 0;
29592
29593 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29594 doc: /* Normal hook run to update the menu bar definitions.
29595 Redisplay runs this hook before it redisplays the menu bar.
29596 This is used to update submenus such as Buffers,
29597 whose contents depend on various data. */);
29598 Vmenu_bar_update_hook = Qnil;
29599
29600 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29601 doc: /* Frame for which we are updating a menu.
29602 The enable predicate for a menu binding should check this variable. */);
29603 Vmenu_updating_frame = Qnil;
29604
29605 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29606 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29607 inhibit_menubar_update = 0;
29608
29609 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29610 doc: /* Prefix prepended to all continuation lines at display time.
29611 The value may be a string, an image, or a stretch-glyph; it is
29612 interpreted in the same way as the value of a `display' text property.
29613
29614 This variable is overridden by any `wrap-prefix' text or overlay
29615 property.
29616
29617 To add a prefix to non-continuation lines, use `line-prefix'. */);
29618 Vwrap_prefix = Qnil;
29619 DEFSYM (Qwrap_prefix, "wrap-prefix");
29620 Fmake_variable_buffer_local (Qwrap_prefix);
29621
29622 DEFVAR_LISP ("line-prefix", Vline_prefix,
29623 doc: /* Prefix prepended to all non-continuation lines at display time.
29624 The value may be a string, an image, or a stretch-glyph; it is
29625 interpreted in the same way as the value of a `display' text property.
29626
29627 This variable is overridden by any `line-prefix' text or overlay
29628 property.
29629
29630 To add a prefix to continuation lines, use `wrap-prefix'. */);
29631 Vline_prefix = Qnil;
29632 DEFSYM (Qline_prefix, "line-prefix");
29633 Fmake_variable_buffer_local (Qline_prefix);
29634
29635 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29636 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29637 inhibit_eval_during_redisplay = 0;
29638
29639 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29640 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29641 inhibit_free_realized_faces = 0;
29642
29643 #ifdef GLYPH_DEBUG
29644 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29645 doc: /* Inhibit try_window_id display optimization. */);
29646 inhibit_try_window_id = 0;
29647
29648 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29649 doc: /* Inhibit try_window_reusing display optimization. */);
29650 inhibit_try_window_reusing = 0;
29651
29652 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29653 doc: /* Inhibit try_cursor_movement display optimization. */);
29654 inhibit_try_cursor_movement = 0;
29655 #endif /* GLYPH_DEBUG */
29656
29657 DEFVAR_INT ("overline-margin", overline_margin,
29658 doc: /* Space between overline and text, in pixels.
29659 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29660 margin to the character height. */);
29661 overline_margin = 2;
29662
29663 DEFVAR_INT ("underline-minimum-offset",
29664 underline_minimum_offset,
29665 doc: /* Minimum distance between baseline and underline.
29666 This can improve legibility of underlined text at small font sizes,
29667 particularly when using variable `x-use-underline-position-properties'
29668 with fonts that specify an UNDERLINE_POSITION relatively close to the
29669 baseline. The default value is 1. */);
29670 underline_minimum_offset = 1;
29671
29672 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29673 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29674 This feature only works when on a window system that can change
29675 cursor shapes. */);
29676 display_hourglass_p = 1;
29677
29678 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29679 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29680 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29681
29682 hourglass_atimer = NULL;
29683 hourglass_shown_p = 0;
29684
29685 DEFSYM (Qglyphless_char, "glyphless-char");
29686 DEFSYM (Qhex_code, "hex-code");
29687 DEFSYM (Qempty_box, "empty-box");
29688 DEFSYM (Qthin_space, "thin-space");
29689 DEFSYM (Qzero_width, "zero-width");
29690
29691 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29692 /* Intern this now in case it isn't already done.
29693 Setting this variable twice is harmless.
29694 But don't staticpro it here--that is done in alloc.c. */
29695 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
29696 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29697
29698 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29699 doc: /* Char-table defining glyphless characters.
29700 Each element, if non-nil, should be one of the following:
29701 an ASCII acronym string: display this string in a box
29702 `hex-code': display the hexadecimal code of a character in a box
29703 `empty-box': display as an empty box
29704 `thin-space': display as 1-pixel width space
29705 `zero-width': don't display
29706 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29707 display method for graphical terminals and text terminals respectively.
29708 GRAPHICAL and TEXT should each have one of the values listed above.
29709
29710 The char-table has one extra slot to control the display of a character for
29711 which no font is found. This slot only takes effect on graphical terminals.
29712 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29713 `thin-space'. The default is `empty-box'. */);
29714 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29715 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29716 Qempty_box);
29717
29718 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
29719 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
29720 Vdebug_on_message = Qnil;
29721 }
29722
29723
29724 /* Initialize this module when Emacs starts. */
29725
29726 void
29727 init_xdisp (void)
29728 {
29729 current_header_line_height = current_mode_line_height = -1;
29730
29731 CHARPOS (this_line_start_pos) = 0;
29732
29733 if (!noninteractive)
29734 {
29735 struct window *m = XWINDOW (minibuf_window);
29736 Lisp_Object frame = m->frame;
29737 struct frame *f = XFRAME (frame);
29738 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29739 struct window *r = XWINDOW (root);
29740 int i;
29741
29742 echo_area_window = minibuf_window;
29743
29744 r->top_line = FRAME_TOP_MARGIN (f);
29745 r->total_lines = FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
29746 r->total_cols = FRAME_COLS (f);
29747
29748 m->top_line = FRAME_LINES (f) - 1;
29749 m->total_lines = 1;
29750 m->total_cols = FRAME_COLS (f);
29751
29752 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29753 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29754 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29755
29756 /* The default ellipsis glyphs `...'. */
29757 for (i = 0; i < 3; ++i)
29758 default_invis_vector[i] = make_number ('.');
29759 }
29760
29761 {
29762 /* Allocate the buffer for frame titles.
29763 Also used for `format-mode-line'. */
29764 int size = 100;
29765 mode_line_noprop_buf = xmalloc (size);
29766 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29767 mode_line_noprop_ptr = mode_line_noprop_buf;
29768 mode_line_target = MODE_LINE_DISPLAY;
29769 }
29770
29771 help_echo_showing_p = 0;
29772 }
29773
29774 /* Platform-independent portion of hourglass implementation. */
29775
29776 /* Cancel a currently active hourglass timer, and start a new one. */
29777 void
29778 start_hourglass (void)
29779 {
29780 #if defined (HAVE_WINDOW_SYSTEM)
29781 EMACS_TIME delay;
29782
29783 cancel_hourglass ();
29784
29785 if (INTEGERP (Vhourglass_delay)
29786 && XINT (Vhourglass_delay) > 0)
29787 delay = make_emacs_time (min (XINT (Vhourglass_delay),
29788 TYPE_MAXIMUM (time_t)),
29789 0);
29790 else if (FLOATP (Vhourglass_delay)
29791 && XFLOAT_DATA (Vhourglass_delay) > 0)
29792 delay = EMACS_TIME_FROM_DOUBLE (XFLOAT_DATA (Vhourglass_delay));
29793 else
29794 delay = make_emacs_time (DEFAULT_HOURGLASS_DELAY, 0);
29795
29796 #ifdef HAVE_NTGUI
29797 {
29798 extern void w32_note_current_window (void);
29799 w32_note_current_window ();
29800 }
29801 #endif /* HAVE_NTGUI */
29802
29803 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29804 show_hourglass, NULL);
29805 #endif
29806 }
29807
29808
29809 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29810 shown. */
29811 void
29812 cancel_hourglass (void)
29813 {
29814 #if defined (HAVE_WINDOW_SYSTEM)
29815 if (hourglass_atimer)
29816 {
29817 cancel_atimer (hourglass_atimer);
29818 hourglass_atimer = NULL;
29819 }
29820
29821 if (hourglass_shown_p)
29822 hide_hourglass ();
29823 #endif
29824 }