* xdisp.c (adjust_window_ends): Move duplicated code to new function.
[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 static void
2465 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2466 {
2467 eassert (w);
2468 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2469 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2470 w->window_end_vpos
2471 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2472 }
2473
2474 /***********************************************************************
2475 Lisp form evaluation
2476 ***********************************************************************/
2477
2478 /* Error handler for safe_eval and safe_call. */
2479
2480 static Lisp_Object
2481 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2482 {
2483 add_to_log ("Error during redisplay: %S signaled %S",
2484 Flist (nargs, args), arg);
2485 return Qnil;
2486 }
2487
2488 /* Call function FUNC with the rest of NARGS - 1 arguments
2489 following. Return the result, or nil if something went
2490 wrong. Prevent redisplay during the evaluation. */
2491
2492 Lisp_Object
2493 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2494 {
2495 Lisp_Object val;
2496
2497 if (inhibit_eval_during_redisplay)
2498 val = Qnil;
2499 else
2500 {
2501 va_list ap;
2502 ptrdiff_t i;
2503 ptrdiff_t count = SPECPDL_INDEX ();
2504 struct gcpro gcpro1;
2505 Lisp_Object *args = alloca (nargs * word_size);
2506
2507 args[0] = func;
2508 va_start (ap, func);
2509 for (i = 1; i < nargs; i++)
2510 args[i] = va_arg (ap, Lisp_Object);
2511 va_end (ap);
2512
2513 GCPRO1 (args[0]);
2514 gcpro1.nvars = nargs;
2515 specbind (Qinhibit_redisplay, Qt);
2516 /* Use Qt to ensure debugger does not run,
2517 so there is no possibility of wanting to redisplay. */
2518 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2519 safe_eval_handler);
2520 UNGCPRO;
2521 val = unbind_to (count, val);
2522 }
2523
2524 return val;
2525 }
2526
2527
2528 /* Call function FN with one argument ARG.
2529 Return the result, or nil if something went wrong. */
2530
2531 Lisp_Object
2532 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2533 {
2534 return safe_call (2, fn, arg);
2535 }
2536
2537 static Lisp_Object Qeval;
2538
2539 Lisp_Object
2540 safe_eval (Lisp_Object sexpr)
2541 {
2542 return safe_call1 (Qeval, sexpr);
2543 }
2544
2545 /* Call function FN with two arguments ARG1 and ARG2.
2546 Return the result, or nil if something went wrong. */
2547
2548 Lisp_Object
2549 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2550 {
2551 return safe_call (3, fn, arg1, arg2);
2552 }
2553
2554
2555 \f
2556 /***********************************************************************
2557 Debugging
2558 ***********************************************************************/
2559
2560 #if 0
2561
2562 /* Define CHECK_IT to perform sanity checks on iterators.
2563 This is for debugging. It is too slow to do unconditionally. */
2564
2565 static void
2566 check_it (struct it *it)
2567 {
2568 if (it->method == GET_FROM_STRING)
2569 {
2570 eassert (STRINGP (it->string));
2571 eassert (IT_STRING_CHARPOS (*it) >= 0);
2572 }
2573 else
2574 {
2575 eassert (IT_STRING_CHARPOS (*it) < 0);
2576 if (it->method == GET_FROM_BUFFER)
2577 {
2578 /* Check that character and byte positions agree. */
2579 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2580 }
2581 }
2582
2583 if (it->dpvec)
2584 eassert (it->current.dpvec_index >= 0);
2585 else
2586 eassert (it->current.dpvec_index < 0);
2587 }
2588
2589 #define CHECK_IT(IT) check_it ((IT))
2590
2591 #else /* not 0 */
2592
2593 #define CHECK_IT(IT) (void) 0
2594
2595 #endif /* not 0 */
2596
2597
2598 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2599
2600 /* Check that the window end of window W is what we expect it
2601 to be---the last row in the current matrix displaying text. */
2602
2603 static void
2604 check_window_end (struct window *w)
2605 {
2606 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2607 {
2608 struct glyph_row *row;
2609 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2610 !row->enabled_p
2611 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2612 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2613 }
2614 }
2615
2616 #define CHECK_WINDOW_END(W) check_window_end ((W))
2617
2618 #else
2619
2620 #define CHECK_WINDOW_END(W) (void) 0
2621
2622 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2623
2624 /* Return mark position if current buffer has the region of non-zero length,
2625 or -1 otherwise. */
2626
2627 static ptrdiff_t
2628 markpos_of_region (void)
2629 {
2630 if (!NILP (Vtransient_mark_mode)
2631 && !NILP (BVAR (current_buffer, mark_active))
2632 && XMARKER (BVAR (current_buffer, mark))->buffer != NULL)
2633 {
2634 ptrdiff_t markpos = XMARKER (BVAR (current_buffer, mark))->charpos;
2635
2636 if (markpos != PT)
2637 return markpos;
2638 }
2639 return -1;
2640 }
2641
2642 /***********************************************************************
2643 Iterator initialization
2644 ***********************************************************************/
2645
2646 /* Initialize IT for displaying current_buffer in window W, starting
2647 at character position CHARPOS. CHARPOS < 0 means that no buffer
2648 position is specified which is useful when the iterator is assigned
2649 a position later. BYTEPOS is the byte position corresponding to
2650 CHARPOS.
2651
2652 If ROW is not null, calls to produce_glyphs with IT as parameter
2653 will produce glyphs in that row.
2654
2655 BASE_FACE_ID is the id of a base face to use. It must be one of
2656 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2657 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2658 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2659
2660 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2661 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2662 will be initialized to use the corresponding mode line glyph row of
2663 the desired matrix of W. */
2664
2665 void
2666 init_iterator (struct it *it, struct window *w,
2667 ptrdiff_t charpos, ptrdiff_t bytepos,
2668 struct glyph_row *row, enum face_id base_face_id)
2669 {
2670 ptrdiff_t markpos;
2671 enum face_id remapped_base_face_id = base_face_id;
2672
2673 /* Some precondition checks. */
2674 eassert (w != NULL && it != NULL);
2675 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2676 && charpos <= ZV));
2677
2678 /* If face attributes have been changed since the last redisplay,
2679 free realized faces now because they depend on face definitions
2680 that might have changed. Don't free faces while there might be
2681 desired matrices pending which reference these faces. */
2682 if (face_change_count && !inhibit_free_realized_faces)
2683 {
2684 face_change_count = 0;
2685 free_all_realized_faces (Qnil);
2686 }
2687
2688 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2689 if (! NILP (Vface_remapping_alist))
2690 remapped_base_face_id
2691 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2692
2693 /* Use one of the mode line rows of W's desired matrix if
2694 appropriate. */
2695 if (row == NULL)
2696 {
2697 if (base_face_id == MODE_LINE_FACE_ID
2698 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2699 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2700 else if (base_face_id == HEADER_LINE_FACE_ID)
2701 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2702 }
2703
2704 /* Clear IT. */
2705 memset (it, 0, sizeof *it);
2706 it->current.overlay_string_index = -1;
2707 it->current.dpvec_index = -1;
2708 it->base_face_id = remapped_base_face_id;
2709 it->string = Qnil;
2710 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2711 it->paragraph_embedding = L2R;
2712 it->bidi_it.string.lstring = Qnil;
2713 it->bidi_it.string.s = NULL;
2714 it->bidi_it.string.bufpos = 0;
2715 it->bidi_it.w = w;
2716
2717 /* The window in which we iterate over current_buffer: */
2718 XSETWINDOW (it->window, w);
2719 it->w = w;
2720 it->f = XFRAME (w->frame);
2721
2722 it->cmp_it.id = -1;
2723
2724 /* Extra space between lines (on window systems only). */
2725 if (base_face_id == DEFAULT_FACE_ID
2726 && FRAME_WINDOW_P (it->f))
2727 {
2728 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2729 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2730 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2731 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2732 * FRAME_LINE_HEIGHT (it->f));
2733 else if (it->f->extra_line_spacing > 0)
2734 it->extra_line_spacing = it->f->extra_line_spacing;
2735 it->max_extra_line_spacing = 0;
2736 }
2737
2738 /* If realized faces have been removed, e.g. because of face
2739 attribute changes of named faces, recompute them. When running
2740 in batch mode, the face cache of the initial frame is null. If
2741 we happen to get called, make a dummy face cache. */
2742 if (FRAME_FACE_CACHE (it->f) == NULL)
2743 init_frame_faces (it->f);
2744 if (FRAME_FACE_CACHE (it->f)->used == 0)
2745 recompute_basic_faces (it->f);
2746
2747 /* Current value of the `slice', `space-width', and 'height' properties. */
2748 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2749 it->space_width = Qnil;
2750 it->font_height = Qnil;
2751 it->override_ascent = -1;
2752
2753 /* Are control characters displayed as `^C'? */
2754 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2755
2756 /* -1 means everything between a CR and the following line end
2757 is invisible. >0 means lines indented more than this value are
2758 invisible. */
2759 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2760 ? (clip_to_bounds
2761 (-1, XINT (BVAR (current_buffer, selective_display)),
2762 PTRDIFF_MAX))
2763 : (!NILP (BVAR (current_buffer, selective_display))
2764 ? -1 : 0));
2765 it->selective_display_ellipsis_p
2766 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2767
2768 /* Display table to use. */
2769 it->dp = window_display_table (w);
2770
2771 /* Are multibyte characters enabled in current_buffer? */
2772 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2773
2774 /* If visible region is of non-zero length, set IT->region_beg_charpos
2775 and IT->region_end_charpos to the start and end of a visible region
2776 in window IT->w. Set both to -1 to indicate no region. */
2777 markpos = markpos_of_region ();
2778 if (markpos >= 0
2779 /* Maybe highlight only in selected window. */
2780 && (/* Either show region everywhere. */
2781 highlight_nonselected_windows
2782 /* Or show region in the selected window. */
2783 || w == XWINDOW (selected_window)
2784 /* Or show the region if we are in the mini-buffer and W is
2785 the window the mini-buffer refers to. */
2786 || (MINI_WINDOW_P (XWINDOW (selected_window))
2787 && WINDOWP (minibuf_selected_window)
2788 && w == XWINDOW (minibuf_selected_window))))
2789 {
2790 it->region_beg_charpos = min (PT, markpos);
2791 it->region_end_charpos = max (PT, markpos);
2792 }
2793 else
2794 it->region_beg_charpos = it->region_end_charpos = -1;
2795
2796 /* Get the position at which the redisplay_end_trigger hook should
2797 be run, if it is to be run at all. */
2798 if (MARKERP (w->redisplay_end_trigger)
2799 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2800 it->redisplay_end_trigger_charpos
2801 = marker_position (w->redisplay_end_trigger);
2802 else if (INTEGERP (w->redisplay_end_trigger))
2803 it->redisplay_end_trigger_charpos =
2804 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2805
2806 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2807
2808 /* Are lines in the display truncated? */
2809 if (base_face_id != DEFAULT_FACE_ID
2810 || it->w->hscroll
2811 || (! WINDOW_FULL_WIDTH_P (it->w)
2812 && ((!NILP (Vtruncate_partial_width_windows)
2813 && !INTEGERP (Vtruncate_partial_width_windows))
2814 || (INTEGERP (Vtruncate_partial_width_windows)
2815 && (WINDOW_TOTAL_COLS (it->w)
2816 < XINT (Vtruncate_partial_width_windows))))))
2817 it->line_wrap = TRUNCATE;
2818 else if (NILP (BVAR (current_buffer, truncate_lines)))
2819 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2820 ? WINDOW_WRAP : WORD_WRAP;
2821 else
2822 it->line_wrap = TRUNCATE;
2823
2824 /* Get dimensions of truncation and continuation glyphs. These are
2825 displayed as fringe bitmaps under X, but we need them for such
2826 frames when the fringes are turned off. But leave the dimensions
2827 zero for tooltip frames, as these glyphs look ugly there and also
2828 sabotage calculations of tooltip dimensions in x-show-tip. */
2829 #ifdef HAVE_WINDOW_SYSTEM
2830 if (!(FRAME_WINDOW_P (it->f)
2831 && FRAMEP (tip_frame)
2832 && it->f == XFRAME (tip_frame)))
2833 #endif
2834 {
2835 if (it->line_wrap == TRUNCATE)
2836 {
2837 /* We will need the truncation glyph. */
2838 eassert (it->glyph_row == NULL);
2839 produce_special_glyphs (it, IT_TRUNCATION);
2840 it->truncation_pixel_width = it->pixel_width;
2841 }
2842 else
2843 {
2844 /* We will need the continuation glyph. */
2845 eassert (it->glyph_row == NULL);
2846 produce_special_glyphs (it, IT_CONTINUATION);
2847 it->continuation_pixel_width = it->pixel_width;
2848 }
2849 }
2850
2851 /* Reset these values to zero because the produce_special_glyphs
2852 above has changed them. */
2853 it->pixel_width = it->ascent = it->descent = 0;
2854 it->phys_ascent = it->phys_descent = 0;
2855
2856 /* Set this after getting the dimensions of truncation and
2857 continuation glyphs, so that we don't produce glyphs when calling
2858 produce_special_glyphs, above. */
2859 it->glyph_row = row;
2860 it->area = TEXT_AREA;
2861
2862 /* Forget any previous info about this row being reversed. */
2863 if (it->glyph_row)
2864 it->glyph_row->reversed_p = 0;
2865
2866 /* Get the dimensions of the display area. The display area
2867 consists of the visible window area plus a horizontally scrolled
2868 part to the left of the window. All x-values are relative to the
2869 start of this total display area. */
2870 if (base_face_id != DEFAULT_FACE_ID)
2871 {
2872 /* Mode lines, menu bar in terminal frames. */
2873 it->first_visible_x = 0;
2874 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2875 }
2876 else
2877 {
2878 it->first_visible_x =
2879 window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2880 it->last_visible_x = (it->first_visible_x
2881 + window_box_width (w, TEXT_AREA));
2882
2883 /* If we truncate lines, leave room for the truncation glyph(s) at
2884 the right margin. Otherwise, leave room for the continuation
2885 glyph(s). Done only if the window has no fringes. Since we
2886 don't know at this point whether there will be any R2L lines in
2887 the window, we reserve space for truncation/continuation glyphs
2888 even if only one of the fringes is absent. */
2889 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2890 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2891 {
2892 if (it->line_wrap == TRUNCATE)
2893 it->last_visible_x -= it->truncation_pixel_width;
2894 else
2895 it->last_visible_x -= it->continuation_pixel_width;
2896 }
2897
2898 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2899 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2900 }
2901
2902 /* Leave room for a border glyph. */
2903 if (!FRAME_WINDOW_P (it->f)
2904 && !WINDOW_RIGHTMOST_P (it->w))
2905 it->last_visible_x -= 1;
2906
2907 it->last_visible_y = window_text_bottom_y (w);
2908
2909 /* For mode lines and alike, arrange for the first glyph having a
2910 left box line if the face specifies a box. */
2911 if (base_face_id != DEFAULT_FACE_ID)
2912 {
2913 struct face *face;
2914
2915 it->face_id = remapped_base_face_id;
2916
2917 /* If we have a boxed mode line, make the first character appear
2918 with a left box line. */
2919 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2920 if (face->box != FACE_NO_BOX)
2921 it->start_of_box_run_p = 1;
2922 }
2923
2924 /* If a buffer position was specified, set the iterator there,
2925 getting overlays and face properties from that position. */
2926 if (charpos >= BUF_BEG (current_buffer))
2927 {
2928 it->end_charpos = ZV;
2929 eassert (charpos == BYTE_TO_CHAR (bytepos));
2930 IT_CHARPOS (*it) = charpos;
2931 IT_BYTEPOS (*it) = bytepos;
2932
2933 /* We will rely on `reseat' to set this up properly, via
2934 handle_face_prop. */
2935 it->face_id = it->base_face_id;
2936
2937 it->start = it->current;
2938 /* Do we need to reorder bidirectional text? Not if this is a
2939 unibyte buffer: by definition, none of the single-byte
2940 characters are strong R2L, so no reordering is needed. And
2941 bidi.c doesn't support unibyte buffers anyway. Also, don't
2942 reorder while we are loading loadup.el, since the tables of
2943 character properties needed for reordering are not yet
2944 available. */
2945 it->bidi_p =
2946 NILP (Vpurify_flag)
2947 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2948 && it->multibyte_p;
2949
2950 /* If we are to reorder bidirectional text, init the bidi
2951 iterator. */
2952 if (it->bidi_p)
2953 {
2954 /* Note the paragraph direction that this buffer wants to
2955 use. */
2956 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2957 Qleft_to_right))
2958 it->paragraph_embedding = L2R;
2959 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2960 Qright_to_left))
2961 it->paragraph_embedding = R2L;
2962 else
2963 it->paragraph_embedding = NEUTRAL_DIR;
2964 bidi_unshelve_cache (NULL, 0);
2965 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2966 &it->bidi_it);
2967 }
2968
2969 /* Compute faces etc. */
2970 reseat (it, it->current.pos, 1);
2971 }
2972
2973 CHECK_IT (it);
2974 }
2975
2976
2977 /* Initialize IT for the display of window W with window start POS. */
2978
2979 void
2980 start_display (struct it *it, struct window *w, struct text_pos pos)
2981 {
2982 struct glyph_row *row;
2983 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2984
2985 row = w->desired_matrix->rows + first_vpos;
2986 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2987 it->first_vpos = first_vpos;
2988
2989 /* Don't reseat to previous visible line start if current start
2990 position is in a string or image. */
2991 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2992 {
2993 int start_at_line_beg_p;
2994 int first_y = it->current_y;
2995
2996 /* If window start is not at a line start, skip forward to POS to
2997 get the correct continuation lines width. */
2998 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2999 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
3000 if (!start_at_line_beg_p)
3001 {
3002 int new_x;
3003
3004 reseat_at_previous_visible_line_start (it);
3005 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3006
3007 new_x = it->current_x + it->pixel_width;
3008
3009 /* If lines are continued, this line may end in the middle
3010 of a multi-glyph character (e.g. a control character
3011 displayed as \003, or in the middle of an overlay
3012 string). In this case move_it_to above will not have
3013 taken us to the start of the continuation line but to the
3014 end of the continued line. */
3015 if (it->current_x > 0
3016 && it->line_wrap != TRUNCATE /* Lines are continued. */
3017 && (/* And glyph doesn't fit on the line. */
3018 new_x > it->last_visible_x
3019 /* Or it fits exactly and we're on a window
3020 system frame. */
3021 || (new_x == it->last_visible_x
3022 && FRAME_WINDOW_P (it->f)
3023 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3024 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3025 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3026 {
3027 if ((it->current.dpvec_index >= 0
3028 || it->current.overlay_string_index >= 0)
3029 /* If we are on a newline from a display vector or
3030 overlay string, then we are already at the end of
3031 a screen line; no need to go to the next line in
3032 that case, as this line is not really continued.
3033 (If we do go to the next line, C-e will not DTRT.) */
3034 && it->c != '\n')
3035 {
3036 set_iterator_to_next (it, 1);
3037 move_it_in_display_line_to (it, -1, -1, 0);
3038 }
3039
3040 it->continuation_lines_width += it->current_x;
3041 }
3042 /* If the character at POS is displayed via a display
3043 vector, move_it_to above stops at the final glyph of
3044 IT->dpvec. To make the caller redisplay that character
3045 again (a.k.a. start at POS), we need to reset the
3046 dpvec_index to the beginning of IT->dpvec. */
3047 else if (it->current.dpvec_index >= 0)
3048 it->current.dpvec_index = 0;
3049
3050 /* We're starting a new display line, not affected by the
3051 height of the continued line, so clear the appropriate
3052 fields in the iterator structure. */
3053 it->max_ascent = it->max_descent = 0;
3054 it->max_phys_ascent = it->max_phys_descent = 0;
3055
3056 it->current_y = first_y;
3057 it->vpos = 0;
3058 it->current_x = it->hpos = 0;
3059 }
3060 }
3061 }
3062
3063
3064 /* Return 1 if POS is a position in ellipses displayed for invisible
3065 text. W is the window we display, for text property lookup. */
3066
3067 static int
3068 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3069 {
3070 Lisp_Object prop, window;
3071 int ellipses_p = 0;
3072 ptrdiff_t charpos = CHARPOS (pos->pos);
3073
3074 /* If POS specifies a position in a display vector, this might
3075 be for an ellipsis displayed for invisible text. We won't
3076 get the iterator set up for delivering that ellipsis unless
3077 we make sure that it gets aware of the invisible text. */
3078 if (pos->dpvec_index >= 0
3079 && pos->overlay_string_index < 0
3080 && CHARPOS (pos->string_pos) < 0
3081 && charpos > BEGV
3082 && (XSETWINDOW (window, w),
3083 prop = Fget_char_property (make_number (charpos),
3084 Qinvisible, window),
3085 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3086 {
3087 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3088 window);
3089 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3090 }
3091
3092 return ellipses_p;
3093 }
3094
3095
3096 /* Initialize IT for stepping through current_buffer in window W,
3097 starting at position POS that includes overlay string and display
3098 vector/ control character translation position information. Value
3099 is zero if there are overlay strings with newlines at POS. */
3100
3101 static int
3102 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3103 {
3104 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3105 int i, overlay_strings_with_newlines = 0;
3106
3107 /* If POS specifies a position in a display vector, this might
3108 be for an ellipsis displayed for invisible text. We won't
3109 get the iterator set up for delivering that ellipsis unless
3110 we make sure that it gets aware of the invisible text. */
3111 if (in_ellipses_for_invisible_text_p (pos, w))
3112 {
3113 --charpos;
3114 bytepos = 0;
3115 }
3116
3117 /* Keep in mind: the call to reseat in init_iterator skips invisible
3118 text, so we might end up at a position different from POS. This
3119 is only a problem when POS is a row start after a newline and an
3120 overlay starts there with an after-string, and the overlay has an
3121 invisible property. Since we don't skip invisible text in
3122 display_line and elsewhere immediately after consuming the
3123 newline before the row start, such a POS will not be in a string,
3124 but the call to init_iterator below will move us to the
3125 after-string. */
3126 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3127
3128 /* This only scans the current chunk -- it should scan all chunks.
3129 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3130 to 16 in 22.1 to make this a lesser problem. */
3131 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3132 {
3133 const char *s = SSDATA (it->overlay_strings[i]);
3134 const char *e = s + SBYTES (it->overlay_strings[i]);
3135
3136 while (s < e && *s != '\n')
3137 ++s;
3138
3139 if (s < e)
3140 {
3141 overlay_strings_with_newlines = 1;
3142 break;
3143 }
3144 }
3145
3146 /* If position is within an overlay string, set up IT to the right
3147 overlay string. */
3148 if (pos->overlay_string_index >= 0)
3149 {
3150 int relative_index;
3151
3152 /* If the first overlay string happens to have a `display'
3153 property for an image, the iterator will be set up for that
3154 image, and we have to undo that setup first before we can
3155 correct the overlay string index. */
3156 if (it->method == GET_FROM_IMAGE)
3157 pop_it (it);
3158
3159 /* We already have the first chunk of overlay strings in
3160 IT->overlay_strings. Load more until the one for
3161 pos->overlay_string_index is in IT->overlay_strings. */
3162 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3163 {
3164 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3165 it->current.overlay_string_index = 0;
3166 while (n--)
3167 {
3168 load_overlay_strings (it, 0);
3169 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3170 }
3171 }
3172
3173 it->current.overlay_string_index = pos->overlay_string_index;
3174 relative_index = (it->current.overlay_string_index
3175 % OVERLAY_STRING_CHUNK_SIZE);
3176 it->string = it->overlay_strings[relative_index];
3177 eassert (STRINGP (it->string));
3178 it->current.string_pos = pos->string_pos;
3179 it->method = GET_FROM_STRING;
3180 it->end_charpos = SCHARS (it->string);
3181 /* Set up the bidi iterator for this overlay string. */
3182 if (it->bidi_p)
3183 {
3184 it->bidi_it.string.lstring = it->string;
3185 it->bidi_it.string.s = NULL;
3186 it->bidi_it.string.schars = SCHARS (it->string);
3187 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3188 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3189 it->bidi_it.string.unibyte = !it->multibyte_p;
3190 it->bidi_it.w = it->w;
3191 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3192 FRAME_WINDOW_P (it->f), &it->bidi_it);
3193
3194 /* Synchronize the state of the bidi iterator with
3195 pos->string_pos. For any string position other than
3196 zero, this will be done automagically when we resume
3197 iteration over the string and get_visually_first_element
3198 is called. But if string_pos is zero, and the string is
3199 to be reordered for display, we need to resync manually,
3200 since it could be that the iteration state recorded in
3201 pos ended at string_pos of 0 moving backwards in string. */
3202 if (CHARPOS (pos->string_pos) == 0)
3203 {
3204 get_visually_first_element (it);
3205 if (IT_STRING_CHARPOS (*it) != 0)
3206 do {
3207 /* Paranoia. */
3208 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3209 bidi_move_to_visually_next (&it->bidi_it);
3210 } while (it->bidi_it.charpos != 0);
3211 }
3212 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3213 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3214 }
3215 }
3216
3217 if (CHARPOS (pos->string_pos) >= 0)
3218 {
3219 /* Recorded position is not in an overlay string, but in another
3220 string. This can only be a string from a `display' property.
3221 IT should already be filled with that string. */
3222 it->current.string_pos = pos->string_pos;
3223 eassert (STRINGP (it->string));
3224 if (it->bidi_p)
3225 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3226 FRAME_WINDOW_P (it->f), &it->bidi_it);
3227 }
3228
3229 /* Restore position in display vector translations, control
3230 character translations or ellipses. */
3231 if (pos->dpvec_index >= 0)
3232 {
3233 if (it->dpvec == NULL)
3234 get_next_display_element (it);
3235 eassert (it->dpvec && it->current.dpvec_index == 0);
3236 it->current.dpvec_index = pos->dpvec_index;
3237 }
3238
3239 CHECK_IT (it);
3240 return !overlay_strings_with_newlines;
3241 }
3242
3243
3244 /* Initialize IT for stepping through current_buffer in window W
3245 starting at ROW->start. */
3246
3247 static void
3248 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3249 {
3250 init_from_display_pos (it, w, &row->start);
3251 it->start = row->start;
3252 it->continuation_lines_width = row->continuation_lines_width;
3253 CHECK_IT (it);
3254 }
3255
3256
3257 /* Initialize IT for stepping through current_buffer in window W
3258 starting in the line following ROW, i.e. starting at ROW->end.
3259 Value is zero if there are overlay strings with newlines at ROW's
3260 end position. */
3261
3262 static int
3263 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3264 {
3265 int success = 0;
3266
3267 if (init_from_display_pos (it, w, &row->end))
3268 {
3269 if (row->continued_p)
3270 it->continuation_lines_width
3271 = row->continuation_lines_width + row->pixel_width;
3272 CHECK_IT (it);
3273 success = 1;
3274 }
3275
3276 return success;
3277 }
3278
3279
3280
3281 \f
3282 /***********************************************************************
3283 Text properties
3284 ***********************************************************************/
3285
3286 /* Called when IT reaches IT->stop_charpos. Handle text property and
3287 overlay changes. Set IT->stop_charpos to the next position where
3288 to stop. */
3289
3290 static void
3291 handle_stop (struct it *it)
3292 {
3293 enum prop_handled handled;
3294 int handle_overlay_change_p;
3295 struct props *p;
3296
3297 it->dpvec = NULL;
3298 it->current.dpvec_index = -1;
3299 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3300 it->ignore_overlay_strings_at_pos_p = 0;
3301 it->ellipsis_p = 0;
3302
3303 /* Use face of preceding text for ellipsis (if invisible) */
3304 if (it->selective_display_ellipsis_p)
3305 it->saved_face_id = it->face_id;
3306
3307 do
3308 {
3309 handled = HANDLED_NORMALLY;
3310
3311 /* Call text property handlers. */
3312 for (p = it_props; p->handler; ++p)
3313 {
3314 handled = p->handler (it);
3315
3316 if (handled == HANDLED_RECOMPUTE_PROPS)
3317 break;
3318 else if (handled == HANDLED_RETURN)
3319 {
3320 /* We still want to show before and after strings from
3321 overlays even if the actual buffer text is replaced. */
3322 if (!handle_overlay_change_p
3323 || it->sp > 1
3324 /* Don't call get_overlay_strings_1 if we already
3325 have overlay strings loaded, because doing so
3326 will load them again and push the iterator state
3327 onto the stack one more time, which is not
3328 expected by the rest of the code that processes
3329 overlay strings. */
3330 || (it->current.overlay_string_index < 0
3331 ? !get_overlay_strings_1 (it, 0, 0)
3332 : 0))
3333 {
3334 if (it->ellipsis_p)
3335 setup_for_ellipsis (it, 0);
3336 /* When handling a display spec, we might load an
3337 empty string. In that case, discard it here. We
3338 used to discard it in handle_single_display_spec,
3339 but that causes get_overlay_strings_1, above, to
3340 ignore overlay strings that we must check. */
3341 if (STRINGP (it->string) && !SCHARS (it->string))
3342 pop_it (it);
3343 return;
3344 }
3345 else if (STRINGP (it->string) && !SCHARS (it->string))
3346 pop_it (it);
3347 else
3348 {
3349 it->ignore_overlay_strings_at_pos_p = 1;
3350 it->string_from_display_prop_p = 0;
3351 it->from_disp_prop_p = 0;
3352 handle_overlay_change_p = 0;
3353 }
3354 handled = HANDLED_RECOMPUTE_PROPS;
3355 break;
3356 }
3357 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3358 handle_overlay_change_p = 0;
3359 }
3360
3361 if (handled != HANDLED_RECOMPUTE_PROPS)
3362 {
3363 /* Don't check for overlay strings below when set to deliver
3364 characters from a display vector. */
3365 if (it->method == GET_FROM_DISPLAY_VECTOR)
3366 handle_overlay_change_p = 0;
3367
3368 /* Handle overlay changes.
3369 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3370 if it finds overlays. */
3371 if (handle_overlay_change_p)
3372 handled = handle_overlay_change (it);
3373 }
3374
3375 if (it->ellipsis_p)
3376 {
3377 setup_for_ellipsis (it, 0);
3378 break;
3379 }
3380 }
3381 while (handled == HANDLED_RECOMPUTE_PROPS);
3382
3383 /* Determine where to stop next. */
3384 if (handled == HANDLED_NORMALLY)
3385 compute_stop_pos (it);
3386 }
3387
3388
3389 /* Compute IT->stop_charpos from text property and overlay change
3390 information for IT's current position. */
3391
3392 static void
3393 compute_stop_pos (struct it *it)
3394 {
3395 register INTERVAL iv, next_iv;
3396 Lisp_Object object, limit, position;
3397 ptrdiff_t charpos, bytepos;
3398
3399 if (STRINGP (it->string))
3400 {
3401 /* Strings are usually short, so don't limit the search for
3402 properties. */
3403 it->stop_charpos = it->end_charpos;
3404 object = it->string;
3405 limit = Qnil;
3406 charpos = IT_STRING_CHARPOS (*it);
3407 bytepos = IT_STRING_BYTEPOS (*it);
3408 }
3409 else
3410 {
3411 ptrdiff_t pos;
3412
3413 /* If end_charpos is out of range for some reason, such as a
3414 misbehaving display function, rationalize it (Bug#5984). */
3415 if (it->end_charpos > ZV)
3416 it->end_charpos = ZV;
3417 it->stop_charpos = it->end_charpos;
3418
3419 /* If next overlay change is in front of the current stop pos
3420 (which is IT->end_charpos), stop there. Note: value of
3421 next_overlay_change is point-max if no overlay change
3422 follows. */
3423 charpos = IT_CHARPOS (*it);
3424 bytepos = IT_BYTEPOS (*it);
3425 pos = next_overlay_change (charpos);
3426 if (pos < it->stop_charpos)
3427 it->stop_charpos = pos;
3428
3429 /* If showing the region, we have to stop at the region
3430 start or end because the face might change there. */
3431 if (it->region_beg_charpos > 0)
3432 {
3433 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3434 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3435 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3436 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3437 }
3438
3439 /* Set up variables for computing the stop position from text
3440 property changes. */
3441 XSETBUFFER (object, current_buffer);
3442 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3443 }
3444
3445 /* Get the interval containing IT's position. Value is a null
3446 interval if there isn't such an interval. */
3447 position = make_number (charpos);
3448 iv = validate_interval_range (object, &position, &position, 0);
3449 if (iv)
3450 {
3451 Lisp_Object values_here[LAST_PROP_IDX];
3452 struct props *p;
3453
3454 /* Get properties here. */
3455 for (p = it_props; p->handler; ++p)
3456 values_here[p->idx] = textget (iv->plist, *p->name);
3457
3458 /* Look for an interval following iv that has different
3459 properties. */
3460 for (next_iv = next_interval (iv);
3461 (next_iv
3462 && (NILP (limit)
3463 || XFASTINT (limit) > next_iv->position));
3464 next_iv = next_interval (next_iv))
3465 {
3466 for (p = it_props; p->handler; ++p)
3467 {
3468 Lisp_Object new_value;
3469
3470 new_value = textget (next_iv->plist, *p->name);
3471 if (!EQ (values_here[p->idx], new_value))
3472 break;
3473 }
3474
3475 if (p->handler)
3476 break;
3477 }
3478
3479 if (next_iv)
3480 {
3481 if (INTEGERP (limit)
3482 && next_iv->position >= XFASTINT (limit))
3483 /* No text property change up to limit. */
3484 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3485 else
3486 /* Text properties change in next_iv. */
3487 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3488 }
3489 }
3490
3491 if (it->cmp_it.id < 0)
3492 {
3493 ptrdiff_t stoppos = it->end_charpos;
3494
3495 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3496 stoppos = -1;
3497 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3498 stoppos, it->string);
3499 }
3500
3501 eassert (STRINGP (it->string)
3502 || (it->stop_charpos >= BEGV
3503 && it->stop_charpos >= IT_CHARPOS (*it)));
3504 }
3505
3506
3507 /* Return the position of the next overlay change after POS in
3508 current_buffer. Value is point-max if no overlay change
3509 follows. This is like `next-overlay-change' but doesn't use
3510 xmalloc. */
3511
3512 static ptrdiff_t
3513 next_overlay_change (ptrdiff_t pos)
3514 {
3515 ptrdiff_t i, noverlays;
3516 ptrdiff_t endpos;
3517 Lisp_Object *overlays;
3518
3519 /* Get all overlays at the given position. */
3520 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3521
3522 /* If any of these overlays ends before endpos,
3523 use its ending point instead. */
3524 for (i = 0; i < noverlays; ++i)
3525 {
3526 Lisp_Object oend;
3527 ptrdiff_t oendpos;
3528
3529 oend = OVERLAY_END (overlays[i]);
3530 oendpos = OVERLAY_POSITION (oend);
3531 endpos = min (endpos, oendpos);
3532 }
3533
3534 return endpos;
3535 }
3536
3537 /* How many characters forward to search for a display property or
3538 display string. Searching too far forward makes the bidi display
3539 sluggish, especially in small windows. */
3540 #define MAX_DISP_SCAN 250
3541
3542 /* Return the character position of a display string at or after
3543 position specified by POSITION. If no display string exists at or
3544 after POSITION, return ZV. A display string is either an overlay
3545 with `display' property whose value is a string, or a `display'
3546 text property whose value is a string. STRING is data about the
3547 string to iterate; if STRING->lstring is nil, we are iterating a
3548 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3549 on a GUI frame. DISP_PROP is set to zero if we searched
3550 MAX_DISP_SCAN characters forward without finding any display
3551 strings, non-zero otherwise. It is set to 2 if the display string
3552 uses any kind of `(space ...)' spec that will produce a stretch of
3553 white space in the text area. */
3554 ptrdiff_t
3555 compute_display_string_pos (struct text_pos *position,
3556 struct bidi_string_data *string,
3557 struct window *w,
3558 int frame_window_p, int *disp_prop)
3559 {
3560 /* OBJECT = nil means current buffer. */
3561 Lisp_Object object, object1;
3562 Lisp_Object pos, spec, limpos;
3563 int string_p = (string && (STRINGP (string->lstring) || string->s));
3564 ptrdiff_t eob = string_p ? string->schars : ZV;
3565 ptrdiff_t begb = string_p ? 0 : BEGV;
3566 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3567 ptrdiff_t lim =
3568 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3569 struct text_pos tpos;
3570 int rv = 0;
3571
3572 if (string && STRINGP (string->lstring))
3573 object1 = object = string->lstring;
3574 else if (w && !string_p)
3575 {
3576 XSETWINDOW (object, w);
3577 object1 = Qnil;
3578 }
3579 else
3580 object1 = object = Qnil;
3581
3582 *disp_prop = 1;
3583
3584 if (charpos >= eob
3585 /* We don't support display properties whose values are strings
3586 that have display string properties. */
3587 || string->from_disp_str
3588 /* C strings cannot have display properties. */
3589 || (string->s && !STRINGP (object)))
3590 {
3591 *disp_prop = 0;
3592 return eob;
3593 }
3594
3595 /* If the character at CHARPOS is where the display string begins,
3596 return CHARPOS. */
3597 pos = make_number (charpos);
3598 if (STRINGP (object))
3599 bufpos = string->bufpos;
3600 else
3601 bufpos = charpos;
3602 tpos = *position;
3603 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3604 && (charpos <= begb
3605 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3606 object),
3607 spec))
3608 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3609 frame_window_p)))
3610 {
3611 if (rv == 2)
3612 *disp_prop = 2;
3613 return charpos;
3614 }
3615
3616 /* Look forward for the first character with a `display' property
3617 that will replace the underlying text when displayed. */
3618 limpos = make_number (lim);
3619 do {
3620 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3621 CHARPOS (tpos) = XFASTINT (pos);
3622 if (CHARPOS (tpos) >= lim)
3623 {
3624 *disp_prop = 0;
3625 break;
3626 }
3627 if (STRINGP (object))
3628 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3629 else
3630 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3631 spec = Fget_char_property (pos, Qdisplay, object);
3632 if (!STRINGP (object))
3633 bufpos = CHARPOS (tpos);
3634 } while (NILP (spec)
3635 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3636 bufpos, frame_window_p)));
3637 if (rv == 2)
3638 *disp_prop = 2;
3639
3640 return CHARPOS (tpos);
3641 }
3642
3643 /* Return the character position of the end of the display string that
3644 started at CHARPOS. If there's no display string at CHARPOS,
3645 return -1. A display string is either an overlay with `display'
3646 property whose value is a string or a `display' text property whose
3647 value is a string. */
3648 ptrdiff_t
3649 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3650 {
3651 /* OBJECT = nil means current buffer. */
3652 Lisp_Object object =
3653 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3654 Lisp_Object pos = make_number (charpos);
3655 ptrdiff_t eob =
3656 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3657
3658 if (charpos >= eob || (string->s && !STRINGP (object)))
3659 return eob;
3660
3661 /* It could happen that the display property or overlay was removed
3662 since we found it in compute_display_string_pos above. One way
3663 this can happen is if JIT font-lock was called (through
3664 handle_fontified_prop), and jit-lock-functions remove text
3665 properties or overlays from the portion of buffer that includes
3666 CHARPOS. Muse mode is known to do that, for example. In this
3667 case, we return -1 to the caller, to signal that no display
3668 string is actually present at CHARPOS. See bidi_fetch_char for
3669 how this is handled.
3670
3671 An alternative would be to never look for display properties past
3672 it->stop_charpos. But neither compute_display_string_pos nor
3673 bidi_fetch_char that calls it know or care where the next
3674 stop_charpos is. */
3675 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3676 return -1;
3677
3678 /* Look forward for the first character where the `display' property
3679 changes. */
3680 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3681
3682 return XFASTINT (pos);
3683 }
3684
3685
3686 \f
3687 /***********************************************************************
3688 Fontification
3689 ***********************************************************************/
3690
3691 /* Handle changes in the `fontified' property of the current buffer by
3692 calling hook functions from Qfontification_functions to fontify
3693 regions of text. */
3694
3695 static enum prop_handled
3696 handle_fontified_prop (struct it *it)
3697 {
3698 Lisp_Object prop, pos;
3699 enum prop_handled handled = HANDLED_NORMALLY;
3700
3701 if (!NILP (Vmemory_full))
3702 return handled;
3703
3704 /* Get the value of the `fontified' property at IT's current buffer
3705 position. (The `fontified' property doesn't have a special
3706 meaning in strings.) If the value is nil, call functions from
3707 Qfontification_functions. */
3708 if (!STRINGP (it->string)
3709 && it->s == NULL
3710 && !NILP (Vfontification_functions)
3711 && !NILP (Vrun_hooks)
3712 && (pos = make_number (IT_CHARPOS (*it)),
3713 prop = Fget_char_property (pos, Qfontified, Qnil),
3714 /* Ignore the special cased nil value always present at EOB since
3715 no amount of fontifying will be able to change it. */
3716 NILP (prop) && IT_CHARPOS (*it) < Z))
3717 {
3718 ptrdiff_t count = SPECPDL_INDEX ();
3719 Lisp_Object val;
3720 struct buffer *obuf = current_buffer;
3721 int begv = BEGV, zv = ZV;
3722 int old_clip_changed = current_buffer->clip_changed;
3723
3724 val = Vfontification_functions;
3725 specbind (Qfontification_functions, Qnil);
3726
3727 eassert (it->end_charpos == ZV);
3728
3729 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3730 safe_call1 (val, pos);
3731 else
3732 {
3733 Lisp_Object fns, fn;
3734 struct gcpro gcpro1, gcpro2;
3735
3736 fns = Qnil;
3737 GCPRO2 (val, fns);
3738
3739 for (; CONSP (val); val = XCDR (val))
3740 {
3741 fn = XCAR (val);
3742
3743 if (EQ (fn, Qt))
3744 {
3745 /* A value of t indicates this hook has a local
3746 binding; it means to run the global binding too.
3747 In a global value, t should not occur. If it
3748 does, we must ignore it to avoid an endless
3749 loop. */
3750 for (fns = Fdefault_value (Qfontification_functions);
3751 CONSP (fns);
3752 fns = XCDR (fns))
3753 {
3754 fn = XCAR (fns);
3755 if (!EQ (fn, Qt))
3756 safe_call1 (fn, pos);
3757 }
3758 }
3759 else
3760 safe_call1 (fn, pos);
3761 }
3762
3763 UNGCPRO;
3764 }
3765
3766 unbind_to (count, Qnil);
3767
3768 /* Fontification functions routinely call `save-restriction'.
3769 Normally, this tags clip_changed, which can confuse redisplay
3770 (see discussion in Bug#6671). Since we don't perform any
3771 special handling of fontification changes in the case where
3772 `save-restriction' isn't called, there's no point doing so in
3773 this case either. So, if the buffer's restrictions are
3774 actually left unchanged, reset clip_changed. */
3775 if (obuf == current_buffer)
3776 {
3777 if (begv == BEGV && zv == ZV)
3778 current_buffer->clip_changed = old_clip_changed;
3779 }
3780 /* There isn't much we can reasonably do to protect against
3781 misbehaving fontification, but here's a fig leaf. */
3782 else if (BUFFER_LIVE_P (obuf))
3783 set_buffer_internal_1 (obuf);
3784
3785 /* The fontification code may have added/removed text.
3786 It could do even a lot worse, but let's at least protect against
3787 the most obvious case where only the text past `pos' gets changed',
3788 as is/was done in grep.el where some escapes sequences are turned
3789 into face properties (bug#7876). */
3790 it->end_charpos = ZV;
3791
3792 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3793 something. This avoids an endless loop if they failed to
3794 fontify the text for which reason ever. */
3795 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3796 handled = HANDLED_RECOMPUTE_PROPS;
3797 }
3798
3799 return handled;
3800 }
3801
3802
3803 \f
3804 /***********************************************************************
3805 Faces
3806 ***********************************************************************/
3807
3808 /* Set up iterator IT from face properties at its current position.
3809 Called from handle_stop. */
3810
3811 static enum prop_handled
3812 handle_face_prop (struct it *it)
3813 {
3814 int new_face_id;
3815 ptrdiff_t next_stop;
3816
3817 if (!STRINGP (it->string))
3818 {
3819 new_face_id
3820 = face_at_buffer_position (it->w,
3821 IT_CHARPOS (*it),
3822 it->region_beg_charpos,
3823 it->region_end_charpos,
3824 &next_stop,
3825 (IT_CHARPOS (*it)
3826 + TEXT_PROP_DISTANCE_LIMIT),
3827 0, it->base_face_id);
3828
3829 /* Is this a start of a run of characters with box face?
3830 Caveat: this can be called for a freshly initialized
3831 iterator; face_id is -1 in this case. We know that the new
3832 face will not change until limit, i.e. if the new face has a
3833 box, all characters up to limit will have one. But, as
3834 usual, we don't know whether limit is really the end. */
3835 if (new_face_id != it->face_id)
3836 {
3837 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3838 /* If it->face_id is -1, old_face below will be NULL, see
3839 the definition of FACE_FROM_ID. This will happen if this
3840 is the initial call that gets the face. */
3841 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3842
3843 /* If the value of face_id of the iterator is -1, we have to
3844 look in front of IT's position and see whether there is a
3845 face there that's different from new_face_id. */
3846 if (!old_face && IT_CHARPOS (*it) > BEG)
3847 {
3848 int prev_face_id = face_before_it_pos (it);
3849
3850 old_face = FACE_FROM_ID (it->f, prev_face_id);
3851 }
3852
3853 /* If the new face has a box, but the old face does not,
3854 this is the start of a run of characters with box face,
3855 i.e. this character has a shadow on the left side. */
3856 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3857 && (old_face == NULL || !old_face->box));
3858 it->face_box_p = new_face->box != FACE_NO_BOX;
3859 }
3860 }
3861 else
3862 {
3863 int base_face_id;
3864 ptrdiff_t bufpos;
3865 int i;
3866 Lisp_Object from_overlay
3867 = (it->current.overlay_string_index >= 0
3868 ? it->string_overlays[it->current.overlay_string_index
3869 % OVERLAY_STRING_CHUNK_SIZE]
3870 : Qnil);
3871
3872 /* See if we got to this string directly or indirectly from
3873 an overlay property. That includes the before-string or
3874 after-string of an overlay, strings in display properties
3875 provided by an overlay, their text properties, etc.
3876
3877 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3878 if (! NILP (from_overlay))
3879 for (i = it->sp - 1; i >= 0; i--)
3880 {
3881 if (it->stack[i].current.overlay_string_index >= 0)
3882 from_overlay
3883 = it->string_overlays[it->stack[i].current.overlay_string_index
3884 % OVERLAY_STRING_CHUNK_SIZE];
3885 else if (! NILP (it->stack[i].from_overlay))
3886 from_overlay = it->stack[i].from_overlay;
3887
3888 if (!NILP (from_overlay))
3889 break;
3890 }
3891
3892 if (! NILP (from_overlay))
3893 {
3894 bufpos = IT_CHARPOS (*it);
3895 /* For a string from an overlay, the base face depends
3896 only on text properties and ignores overlays. */
3897 base_face_id
3898 = face_for_overlay_string (it->w,
3899 IT_CHARPOS (*it),
3900 it->region_beg_charpos,
3901 it->region_end_charpos,
3902 &next_stop,
3903 (IT_CHARPOS (*it)
3904 + TEXT_PROP_DISTANCE_LIMIT),
3905 0,
3906 from_overlay);
3907 }
3908 else
3909 {
3910 bufpos = 0;
3911
3912 /* For strings from a `display' property, use the face at
3913 IT's current buffer position as the base face to merge
3914 with, so that overlay strings appear in the same face as
3915 surrounding text, unless they specify their own
3916 faces. */
3917 base_face_id = it->string_from_prefix_prop_p
3918 ? DEFAULT_FACE_ID
3919 : underlying_face_id (it);
3920 }
3921
3922 new_face_id = face_at_string_position (it->w,
3923 it->string,
3924 IT_STRING_CHARPOS (*it),
3925 bufpos,
3926 it->region_beg_charpos,
3927 it->region_end_charpos,
3928 &next_stop,
3929 base_face_id, 0);
3930
3931 /* Is this a start of a run of characters with box? Caveat:
3932 this can be called for a freshly allocated iterator; face_id
3933 is -1 is this case. We know that the new face will not
3934 change until the next check pos, i.e. if the new face has a
3935 box, all characters up to that position will have a
3936 box. But, as usual, we don't know whether that position
3937 is really the end. */
3938 if (new_face_id != it->face_id)
3939 {
3940 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3941 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3942
3943 /* If new face has a box but old face hasn't, this is the
3944 start of a run of characters with box, i.e. it has a
3945 shadow on the left side. */
3946 it->start_of_box_run_p
3947 = new_face->box && (old_face == NULL || !old_face->box);
3948 it->face_box_p = new_face->box != FACE_NO_BOX;
3949 }
3950 }
3951
3952 it->face_id = new_face_id;
3953 return HANDLED_NORMALLY;
3954 }
3955
3956
3957 /* Return the ID of the face ``underlying'' IT's current position,
3958 which is in a string. If the iterator is associated with a
3959 buffer, return the face at IT's current buffer position.
3960 Otherwise, use the iterator's base_face_id. */
3961
3962 static int
3963 underlying_face_id (struct it *it)
3964 {
3965 int face_id = it->base_face_id, i;
3966
3967 eassert (STRINGP (it->string));
3968
3969 for (i = it->sp - 1; i >= 0; --i)
3970 if (NILP (it->stack[i].string))
3971 face_id = it->stack[i].face_id;
3972
3973 return face_id;
3974 }
3975
3976
3977 /* Compute the face one character before or after the current position
3978 of IT, in the visual order. BEFORE_P non-zero means get the face
3979 in front (to the left in L2R paragraphs, to the right in R2L
3980 paragraphs) of IT's screen position. Value is the ID of the face. */
3981
3982 static int
3983 face_before_or_after_it_pos (struct it *it, int before_p)
3984 {
3985 int face_id, limit;
3986 ptrdiff_t next_check_charpos;
3987 struct it it_copy;
3988 void *it_copy_data = NULL;
3989
3990 eassert (it->s == NULL);
3991
3992 if (STRINGP (it->string))
3993 {
3994 ptrdiff_t bufpos, charpos;
3995 int base_face_id;
3996
3997 /* No face change past the end of the string (for the case
3998 we are padding with spaces). No face change before the
3999 string start. */
4000 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
4001 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
4002 return it->face_id;
4003
4004 if (!it->bidi_p)
4005 {
4006 /* Set charpos to the position before or after IT's current
4007 position, in the logical order, which in the non-bidi
4008 case is the same as the visual order. */
4009 if (before_p)
4010 charpos = IT_STRING_CHARPOS (*it) - 1;
4011 else if (it->what == IT_COMPOSITION)
4012 /* For composition, we must check the character after the
4013 composition. */
4014 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4015 else
4016 charpos = IT_STRING_CHARPOS (*it) + 1;
4017 }
4018 else
4019 {
4020 if (before_p)
4021 {
4022 /* With bidi iteration, the character before the current
4023 in the visual order cannot be found by simple
4024 iteration, because "reverse" reordering is not
4025 supported. Instead, we need to use the move_it_*
4026 family of functions. */
4027 /* Ignore face changes before the first visible
4028 character on this display line. */
4029 if (it->current_x <= it->first_visible_x)
4030 return it->face_id;
4031 SAVE_IT (it_copy, *it, it_copy_data);
4032 /* Implementation note: Since move_it_in_display_line
4033 works in the iterator geometry, and thinks the first
4034 character is always the leftmost, even in R2L lines,
4035 we don't need to distinguish between the R2L and L2R
4036 cases here. */
4037 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
4038 it_copy.current_x - 1, MOVE_TO_X);
4039 charpos = IT_STRING_CHARPOS (it_copy);
4040 RESTORE_IT (it, it, it_copy_data);
4041 }
4042 else
4043 {
4044 /* Set charpos to the string position of the character
4045 that comes after IT's current position in the visual
4046 order. */
4047 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4048
4049 it_copy = *it;
4050 while (n--)
4051 bidi_move_to_visually_next (&it_copy.bidi_it);
4052
4053 charpos = it_copy.bidi_it.charpos;
4054 }
4055 }
4056 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4057
4058 if (it->current.overlay_string_index >= 0)
4059 bufpos = IT_CHARPOS (*it);
4060 else
4061 bufpos = 0;
4062
4063 base_face_id = underlying_face_id (it);
4064
4065 /* Get the face for ASCII, or unibyte. */
4066 face_id = face_at_string_position (it->w,
4067 it->string,
4068 charpos,
4069 bufpos,
4070 it->region_beg_charpos,
4071 it->region_end_charpos,
4072 &next_check_charpos,
4073 base_face_id, 0);
4074
4075 /* Correct the face for charsets different from ASCII. Do it
4076 for the multibyte case only. The face returned above is
4077 suitable for unibyte text if IT->string is unibyte. */
4078 if (STRING_MULTIBYTE (it->string))
4079 {
4080 struct text_pos pos1 = string_pos (charpos, it->string);
4081 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4082 int c, len;
4083 struct face *face = FACE_FROM_ID (it->f, face_id);
4084
4085 c = string_char_and_length (p, &len);
4086 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4087 }
4088 }
4089 else
4090 {
4091 struct text_pos pos;
4092
4093 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4094 || (IT_CHARPOS (*it) <= BEGV && before_p))
4095 return it->face_id;
4096
4097 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4098 pos = it->current.pos;
4099
4100 if (!it->bidi_p)
4101 {
4102 if (before_p)
4103 DEC_TEXT_POS (pos, it->multibyte_p);
4104 else
4105 {
4106 if (it->what == IT_COMPOSITION)
4107 {
4108 /* For composition, we must check the position after
4109 the composition. */
4110 pos.charpos += it->cmp_it.nchars;
4111 pos.bytepos += it->len;
4112 }
4113 else
4114 INC_TEXT_POS (pos, it->multibyte_p);
4115 }
4116 }
4117 else
4118 {
4119 if (before_p)
4120 {
4121 /* With bidi iteration, the character before the current
4122 in the visual order cannot be found by simple
4123 iteration, because "reverse" reordering is not
4124 supported. Instead, we need to use the move_it_*
4125 family of functions. */
4126 /* Ignore face changes before the first visible
4127 character on this display line. */
4128 if (it->current_x <= it->first_visible_x)
4129 return it->face_id;
4130 SAVE_IT (it_copy, *it, it_copy_data);
4131 /* Implementation note: Since move_it_in_display_line
4132 works in the iterator geometry, and thinks the first
4133 character is always the leftmost, even in R2L lines,
4134 we don't need to distinguish between the R2L and L2R
4135 cases here. */
4136 move_it_in_display_line (&it_copy, ZV,
4137 it_copy.current_x - 1, MOVE_TO_X);
4138 pos = it_copy.current.pos;
4139 RESTORE_IT (it, it, it_copy_data);
4140 }
4141 else
4142 {
4143 /* Set charpos to the buffer position of the character
4144 that comes after IT's current position in the visual
4145 order. */
4146 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4147
4148 it_copy = *it;
4149 while (n--)
4150 bidi_move_to_visually_next (&it_copy.bidi_it);
4151
4152 SET_TEXT_POS (pos,
4153 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4154 }
4155 }
4156 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4157
4158 /* Determine face for CHARSET_ASCII, or unibyte. */
4159 face_id = face_at_buffer_position (it->w,
4160 CHARPOS (pos),
4161 it->region_beg_charpos,
4162 it->region_end_charpos,
4163 &next_check_charpos,
4164 limit, 0, -1);
4165
4166 /* Correct the face for charsets different from ASCII. Do it
4167 for the multibyte case only. The face returned above is
4168 suitable for unibyte text if current_buffer is unibyte. */
4169 if (it->multibyte_p)
4170 {
4171 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4172 struct face *face = FACE_FROM_ID (it->f, face_id);
4173 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4174 }
4175 }
4176
4177 return face_id;
4178 }
4179
4180
4181 \f
4182 /***********************************************************************
4183 Invisible text
4184 ***********************************************************************/
4185
4186 /* Set up iterator IT from invisible properties at its current
4187 position. Called from handle_stop. */
4188
4189 static enum prop_handled
4190 handle_invisible_prop (struct it *it)
4191 {
4192 enum prop_handled handled = HANDLED_NORMALLY;
4193 int invis_p;
4194 Lisp_Object prop;
4195
4196 if (STRINGP (it->string))
4197 {
4198 Lisp_Object end_charpos, limit, charpos;
4199
4200 /* Get the value of the invisible text property at the
4201 current position. Value will be nil if there is no such
4202 property. */
4203 charpos = make_number (IT_STRING_CHARPOS (*it));
4204 prop = Fget_text_property (charpos, Qinvisible, it->string);
4205 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4206
4207 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4208 {
4209 /* Record whether we have to display an ellipsis for the
4210 invisible text. */
4211 int display_ellipsis_p = (invis_p == 2);
4212 ptrdiff_t len, endpos;
4213
4214 handled = HANDLED_RECOMPUTE_PROPS;
4215
4216 /* Get the position at which the next visible text can be
4217 found in IT->string, if any. */
4218 endpos = len = SCHARS (it->string);
4219 XSETINT (limit, len);
4220 do
4221 {
4222 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4223 it->string, limit);
4224 if (INTEGERP (end_charpos))
4225 {
4226 endpos = XFASTINT (end_charpos);
4227 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4228 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4229 if (invis_p == 2)
4230 display_ellipsis_p = 1;
4231 }
4232 }
4233 while (invis_p && endpos < len);
4234
4235 if (display_ellipsis_p)
4236 it->ellipsis_p = 1;
4237
4238 if (endpos < len)
4239 {
4240 /* Text at END_CHARPOS is visible. Move IT there. */
4241 struct text_pos old;
4242 ptrdiff_t oldpos;
4243
4244 old = it->current.string_pos;
4245 oldpos = CHARPOS (old);
4246 if (it->bidi_p)
4247 {
4248 if (it->bidi_it.first_elt
4249 && it->bidi_it.charpos < SCHARS (it->string))
4250 bidi_paragraph_init (it->paragraph_embedding,
4251 &it->bidi_it, 1);
4252 /* Bidi-iterate out of the invisible text. */
4253 do
4254 {
4255 bidi_move_to_visually_next (&it->bidi_it);
4256 }
4257 while (oldpos <= it->bidi_it.charpos
4258 && it->bidi_it.charpos < endpos);
4259
4260 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4261 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4262 if (IT_CHARPOS (*it) >= endpos)
4263 it->prev_stop = endpos;
4264 }
4265 else
4266 {
4267 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4268 compute_string_pos (&it->current.string_pos, old, it->string);
4269 }
4270 }
4271 else
4272 {
4273 /* The rest of the string is invisible. If this is an
4274 overlay string, proceed with the next overlay string
4275 or whatever comes and return a character from there. */
4276 if (it->current.overlay_string_index >= 0
4277 && !display_ellipsis_p)
4278 {
4279 next_overlay_string (it);
4280 /* Don't check for overlay strings when we just
4281 finished processing them. */
4282 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4283 }
4284 else
4285 {
4286 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4287 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4288 }
4289 }
4290 }
4291 }
4292 else
4293 {
4294 ptrdiff_t newpos, next_stop, start_charpos, tem;
4295 Lisp_Object pos, overlay;
4296
4297 /* First of all, is there invisible text at this position? */
4298 tem = start_charpos = IT_CHARPOS (*it);
4299 pos = make_number (tem);
4300 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4301 &overlay);
4302 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4303
4304 /* If we are on invisible text, skip over it. */
4305 if (invis_p && start_charpos < it->end_charpos)
4306 {
4307 /* Record whether we have to display an ellipsis for the
4308 invisible text. */
4309 int display_ellipsis_p = invis_p == 2;
4310
4311 handled = HANDLED_RECOMPUTE_PROPS;
4312
4313 /* Loop skipping over invisible text. The loop is left at
4314 ZV or with IT on the first char being visible again. */
4315 do
4316 {
4317 /* Try to skip some invisible text. Return value is the
4318 position reached which can be equal to where we start
4319 if there is nothing invisible there. This skips both
4320 over invisible text properties and overlays with
4321 invisible property. */
4322 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4323
4324 /* If we skipped nothing at all we weren't at invisible
4325 text in the first place. If everything to the end of
4326 the buffer was skipped, end the loop. */
4327 if (newpos == tem || newpos >= ZV)
4328 invis_p = 0;
4329 else
4330 {
4331 /* We skipped some characters but not necessarily
4332 all there are. Check if we ended up on visible
4333 text. Fget_char_property returns the property of
4334 the char before the given position, i.e. if we
4335 get invis_p = 0, this means that the char at
4336 newpos is visible. */
4337 pos = make_number (newpos);
4338 prop = Fget_char_property (pos, Qinvisible, it->window);
4339 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4340 }
4341
4342 /* If we ended up on invisible text, proceed to
4343 skip starting with next_stop. */
4344 if (invis_p)
4345 tem = next_stop;
4346
4347 /* If there are adjacent invisible texts, don't lose the
4348 second one's ellipsis. */
4349 if (invis_p == 2)
4350 display_ellipsis_p = 1;
4351 }
4352 while (invis_p);
4353
4354 /* The position newpos is now either ZV or on visible text. */
4355 if (it->bidi_p)
4356 {
4357 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4358 int on_newline =
4359 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4360 int after_newline =
4361 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4362
4363 /* If the invisible text ends on a newline or on a
4364 character after a newline, we can avoid the costly,
4365 character by character, bidi iteration to NEWPOS, and
4366 instead simply reseat the iterator there. That's
4367 because all bidi reordering information is tossed at
4368 the newline. This is a big win for modes that hide
4369 complete lines, like Outline, Org, etc. */
4370 if (on_newline || after_newline)
4371 {
4372 struct text_pos tpos;
4373 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4374
4375 SET_TEXT_POS (tpos, newpos, bpos);
4376 reseat_1 (it, tpos, 0);
4377 /* If we reseat on a newline/ZV, we need to prep the
4378 bidi iterator for advancing to the next character
4379 after the newline/EOB, keeping the current paragraph
4380 direction (so that PRODUCE_GLYPHS does TRT wrt
4381 prepending/appending glyphs to a glyph row). */
4382 if (on_newline)
4383 {
4384 it->bidi_it.first_elt = 0;
4385 it->bidi_it.paragraph_dir = pdir;
4386 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4387 it->bidi_it.nchars = 1;
4388 it->bidi_it.ch_len = 1;
4389 }
4390 }
4391 else /* Must use the slow method. */
4392 {
4393 /* With bidi iteration, the region of invisible text
4394 could start and/or end in the middle of a
4395 non-base embedding level. Therefore, we need to
4396 skip invisible text using the bidi iterator,
4397 starting at IT's current position, until we find
4398 ourselves outside of the invisible text.
4399 Skipping invisible text _after_ bidi iteration
4400 avoids affecting the visual order of the
4401 displayed text when invisible properties are
4402 added or removed. */
4403 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4404 {
4405 /* If we were `reseat'ed to a new paragraph,
4406 determine the paragraph base direction. We
4407 need to do it now because
4408 next_element_from_buffer may not have a
4409 chance to do it, if we are going to skip any
4410 text at the beginning, which resets the
4411 FIRST_ELT flag. */
4412 bidi_paragraph_init (it->paragraph_embedding,
4413 &it->bidi_it, 1);
4414 }
4415 do
4416 {
4417 bidi_move_to_visually_next (&it->bidi_it);
4418 }
4419 while (it->stop_charpos <= it->bidi_it.charpos
4420 && it->bidi_it.charpos < newpos);
4421 IT_CHARPOS (*it) = it->bidi_it.charpos;
4422 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4423 /* If we overstepped NEWPOS, record its position in
4424 the iterator, so that we skip invisible text if
4425 later the bidi iteration lands us in the
4426 invisible region again. */
4427 if (IT_CHARPOS (*it) >= newpos)
4428 it->prev_stop = newpos;
4429 }
4430 }
4431 else
4432 {
4433 IT_CHARPOS (*it) = newpos;
4434 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4435 }
4436
4437 /* If there are before-strings at the start of invisible
4438 text, and the text is invisible because of a text
4439 property, arrange to show before-strings because 20.x did
4440 it that way. (If the text is invisible because of an
4441 overlay property instead of a text property, this is
4442 already handled in the overlay code.) */
4443 if (NILP (overlay)
4444 && get_overlay_strings (it, it->stop_charpos))
4445 {
4446 handled = HANDLED_RECOMPUTE_PROPS;
4447 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4448 }
4449 else if (display_ellipsis_p)
4450 {
4451 /* Make sure that the glyphs of the ellipsis will get
4452 correct `charpos' values. If we would not update
4453 it->position here, the glyphs would belong to the
4454 last visible character _before_ the invisible
4455 text, which confuses `set_cursor_from_row'.
4456
4457 We use the last invisible position instead of the
4458 first because this way the cursor is always drawn on
4459 the first "." of the ellipsis, whenever PT is inside
4460 the invisible text. Otherwise the cursor would be
4461 placed _after_ the ellipsis when the point is after the
4462 first invisible character. */
4463 if (!STRINGP (it->object))
4464 {
4465 it->position.charpos = newpos - 1;
4466 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4467 }
4468 it->ellipsis_p = 1;
4469 /* Let the ellipsis display before
4470 considering any properties of the following char.
4471 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4472 handled = HANDLED_RETURN;
4473 }
4474 }
4475 }
4476
4477 return handled;
4478 }
4479
4480
4481 /* Make iterator IT return `...' next.
4482 Replaces LEN characters from buffer. */
4483
4484 static void
4485 setup_for_ellipsis (struct it *it, int len)
4486 {
4487 /* Use the display table definition for `...'. Invalid glyphs
4488 will be handled by the method returning elements from dpvec. */
4489 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4490 {
4491 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4492 it->dpvec = v->contents;
4493 it->dpend = v->contents + v->header.size;
4494 }
4495 else
4496 {
4497 /* Default `...'. */
4498 it->dpvec = default_invis_vector;
4499 it->dpend = default_invis_vector + 3;
4500 }
4501
4502 it->dpvec_char_len = len;
4503 it->current.dpvec_index = 0;
4504 it->dpvec_face_id = -1;
4505
4506 /* Remember the current face id in case glyphs specify faces.
4507 IT's face is restored in set_iterator_to_next.
4508 saved_face_id was set to preceding char's face in handle_stop. */
4509 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4510 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4511
4512 it->method = GET_FROM_DISPLAY_VECTOR;
4513 it->ellipsis_p = 1;
4514 }
4515
4516
4517 \f
4518 /***********************************************************************
4519 'display' property
4520 ***********************************************************************/
4521
4522 /* Set up iterator IT from `display' property at its current position.
4523 Called from handle_stop.
4524 We return HANDLED_RETURN if some part of the display property
4525 overrides the display of the buffer text itself.
4526 Otherwise we return HANDLED_NORMALLY. */
4527
4528 static enum prop_handled
4529 handle_display_prop (struct it *it)
4530 {
4531 Lisp_Object propval, object, overlay;
4532 struct text_pos *position;
4533 ptrdiff_t bufpos;
4534 /* Nonzero if some property replaces the display of the text itself. */
4535 int display_replaced_p = 0;
4536
4537 if (STRINGP (it->string))
4538 {
4539 object = it->string;
4540 position = &it->current.string_pos;
4541 bufpos = CHARPOS (it->current.pos);
4542 }
4543 else
4544 {
4545 XSETWINDOW (object, it->w);
4546 position = &it->current.pos;
4547 bufpos = CHARPOS (*position);
4548 }
4549
4550 /* Reset those iterator values set from display property values. */
4551 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4552 it->space_width = Qnil;
4553 it->font_height = Qnil;
4554 it->voffset = 0;
4555
4556 /* We don't support recursive `display' properties, i.e. string
4557 values that have a string `display' property, that have a string
4558 `display' property etc. */
4559 if (!it->string_from_display_prop_p)
4560 it->area = TEXT_AREA;
4561
4562 propval = get_char_property_and_overlay (make_number (position->charpos),
4563 Qdisplay, object, &overlay);
4564 if (NILP (propval))
4565 return HANDLED_NORMALLY;
4566 /* Now OVERLAY is the overlay that gave us this property, or nil
4567 if it was a text property. */
4568
4569 if (!STRINGP (it->string))
4570 object = it->w->contents;
4571
4572 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4573 position, bufpos,
4574 FRAME_WINDOW_P (it->f));
4575
4576 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4577 }
4578
4579 /* Subroutine of handle_display_prop. Returns non-zero if the display
4580 specification in SPEC is a replacing specification, i.e. it would
4581 replace the text covered by `display' property with something else,
4582 such as an image or a display string. If SPEC includes any kind or
4583 `(space ...) specification, the value is 2; this is used by
4584 compute_display_string_pos, which see.
4585
4586 See handle_single_display_spec for documentation of arguments.
4587 frame_window_p is non-zero if the window being redisplayed is on a
4588 GUI frame; this argument is used only if IT is NULL, see below.
4589
4590 IT can be NULL, if this is called by the bidi reordering code
4591 through compute_display_string_pos, which see. In that case, this
4592 function only examines SPEC, but does not otherwise "handle" it, in
4593 the sense that it doesn't set up members of IT from the display
4594 spec. */
4595 static int
4596 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4597 Lisp_Object overlay, struct text_pos *position,
4598 ptrdiff_t bufpos, int frame_window_p)
4599 {
4600 int replacing_p = 0;
4601 int rv;
4602
4603 if (CONSP (spec)
4604 /* Simple specifications. */
4605 && !EQ (XCAR (spec), Qimage)
4606 && !EQ (XCAR (spec), Qspace)
4607 && !EQ (XCAR (spec), Qwhen)
4608 && !EQ (XCAR (spec), Qslice)
4609 && !EQ (XCAR (spec), Qspace_width)
4610 && !EQ (XCAR (spec), Qheight)
4611 && !EQ (XCAR (spec), Qraise)
4612 /* Marginal area specifications. */
4613 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4614 && !EQ (XCAR (spec), Qleft_fringe)
4615 && !EQ (XCAR (spec), Qright_fringe)
4616 && !NILP (XCAR (spec)))
4617 {
4618 for (; CONSP (spec); spec = XCDR (spec))
4619 {
4620 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4621 overlay, position, bufpos,
4622 replacing_p, frame_window_p)))
4623 {
4624 replacing_p = rv;
4625 /* If some text in a string is replaced, `position' no
4626 longer points to the position of `object'. */
4627 if (!it || STRINGP (object))
4628 break;
4629 }
4630 }
4631 }
4632 else if (VECTORP (spec))
4633 {
4634 ptrdiff_t i;
4635 for (i = 0; i < ASIZE (spec); ++i)
4636 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4637 overlay, position, bufpos,
4638 replacing_p, frame_window_p)))
4639 {
4640 replacing_p = rv;
4641 /* If some text in a string is replaced, `position' no
4642 longer points to the position of `object'. */
4643 if (!it || STRINGP (object))
4644 break;
4645 }
4646 }
4647 else
4648 {
4649 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4650 position, bufpos, 0,
4651 frame_window_p)))
4652 replacing_p = rv;
4653 }
4654
4655 return replacing_p;
4656 }
4657
4658 /* Value is the position of the end of the `display' property starting
4659 at START_POS in OBJECT. */
4660
4661 static struct text_pos
4662 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4663 {
4664 Lisp_Object end;
4665 struct text_pos end_pos;
4666
4667 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4668 Qdisplay, object, Qnil);
4669 CHARPOS (end_pos) = XFASTINT (end);
4670 if (STRINGP (object))
4671 compute_string_pos (&end_pos, start_pos, it->string);
4672 else
4673 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4674
4675 return end_pos;
4676 }
4677
4678
4679 /* Set up IT from a single `display' property specification SPEC. OBJECT
4680 is the object in which the `display' property was found. *POSITION
4681 is the position in OBJECT at which the `display' property was found.
4682 BUFPOS is the buffer position of OBJECT (different from POSITION if
4683 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4684 previously saw a display specification which already replaced text
4685 display with something else, for example an image; we ignore such
4686 properties after the first one has been processed.
4687
4688 OVERLAY is the overlay this `display' property came from,
4689 or nil if it was a text property.
4690
4691 If SPEC is a `space' or `image' specification, and in some other
4692 cases too, set *POSITION to the position where the `display'
4693 property ends.
4694
4695 If IT is NULL, only examine the property specification in SPEC, but
4696 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4697 is intended to be displayed in a window on a GUI frame.
4698
4699 Value is non-zero if something was found which replaces the display
4700 of buffer or string text. */
4701
4702 static int
4703 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4704 Lisp_Object overlay, struct text_pos *position,
4705 ptrdiff_t bufpos, int display_replaced_p,
4706 int frame_window_p)
4707 {
4708 Lisp_Object form;
4709 Lisp_Object location, value;
4710 struct text_pos start_pos = *position;
4711 int valid_p;
4712
4713 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4714 If the result is non-nil, use VALUE instead of SPEC. */
4715 form = Qt;
4716 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4717 {
4718 spec = XCDR (spec);
4719 if (!CONSP (spec))
4720 return 0;
4721 form = XCAR (spec);
4722 spec = XCDR (spec);
4723 }
4724
4725 if (!NILP (form) && !EQ (form, Qt))
4726 {
4727 ptrdiff_t count = SPECPDL_INDEX ();
4728 struct gcpro gcpro1;
4729
4730 /* Bind `object' to the object having the `display' property, a
4731 buffer or string. Bind `position' to the position in the
4732 object where the property was found, and `buffer-position'
4733 to the current position in the buffer. */
4734
4735 if (NILP (object))
4736 XSETBUFFER (object, current_buffer);
4737 specbind (Qobject, object);
4738 specbind (Qposition, make_number (CHARPOS (*position)));
4739 specbind (Qbuffer_position, make_number (bufpos));
4740 GCPRO1 (form);
4741 form = safe_eval (form);
4742 UNGCPRO;
4743 unbind_to (count, Qnil);
4744 }
4745
4746 if (NILP (form))
4747 return 0;
4748
4749 /* Handle `(height HEIGHT)' specifications. */
4750 if (CONSP (spec)
4751 && EQ (XCAR (spec), Qheight)
4752 && CONSP (XCDR (spec)))
4753 {
4754 if (it)
4755 {
4756 if (!FRAME_WINDOW_P (it->f))
4757 return 0;
4758
4759 it->font_height = XCAR (XCDR (spec));
4760 if (!NILP (it->font_height))
4761 {
4762 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4763 int new_height = -1;
4764
4765 if (CONSP (it->font_height)
4766 && (EQ (XCAR (it->font_height), Qplus)
4767 || EQ (XCAR (it->font_height), Qminus))
4768 && CONSP (XCDR (it->font_height))
4769 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4770 {
4771 /* `(+ N)' or `(- N)' where N is an integer. */
4772 int steps = XINT (XCAR (XCDR (it->font_height)));
4773 if (EQ (XCAR (it->font_height), Qplus))
4774 steps = - steps;
4775 it->face_id = smaller_face (it->f, it->face_id, steps);
4776 }
4777 else if (FUNCTIONP (it->font_height))
4778 {
4779 /* Call function with current height as argument.
4780 Value is the new height. */
4781 Lisp_Object height;
4782 height = safe_call1 (it->font_height,
4783 face->lface[LFACE_HEIGHT_INDEX]);
4784 if (NUMBERP (height))
4785 new_height = XFLOATINT (height);
4786 }
4787 else if (NUMBERP (it->font_height))
4788 {
4789 /* Value is a multiple of the canonical char height. */
4790 struct face *f;
4791
4792 f = FACE_FROM_ID (it->f,
4793 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4794 new_height = (XFLOATINT (it->font_height)
4795 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4796 }
4797 else
4798 {
4799 /* Evaluate IT->font_height with `height' bound to the
4800 current specified height to get the new height. */
4801 ptrdiff_t count = SPECPDL_INDEX ();
4802
4803 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4804 value = safe_eval (it->font_height);
4805 unbind_to (count, Qnil);
4806
4807 if (NUMBERP (value))
4808 new_height = XFLOATINT (value);
4809 }
4810
4811 if (new_height > 0)
4812 it->face_id = face_with_height (it->f, it->face_id, new_height);
4813 }
4814 }
4815
4816 return 0;
4817 }
4818
4819 /* Handle `(space-width WIDTH)'. */
4820 if (CONSP (spec)
4821 && EQ (XCAR (spec), Qspace_width)
4822 && CONSP (XCDR (spec)))
4823 {
4824 if (it)
4825 {
4826 if (!FRAME_WINDOW_P (it->f))
4827 return 0;
4828
4829 value = XCAR (XCDR (spec));
4830 if (NUMBERP (value) && XFLOATINT (value) > 0)
4831 it->space_width = value;
4832 }
4833
4834 return 0;
4835 }
4836
4837 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4838 if (CONSP (spec)
4839 && EQ (XCAR (spec), Qslice))
4840 {
4841 Lisp_Object tem;
4842
4843 if (it)
4844 {
4845 if (!FRAME_WINDOW_P (it->f))
4846 return 0;
4847
4848 if (tem = XCDR (spec), CONSP (tem))
4849 {
4850 it->slice.x = XCAR (tem);
4851 if (tem = XCDR (tem), CONSP (tem))
4852 {
4853 it->slice.y = XCAR (tem);
4854 if (tem = XCDR (tem), CONSP (tem))
4855 {
4856 it->slice.width = XCAR (tem);
4857 if (tem = XCDR (tem), CONSP (tem))
4858 it->slice.height = XCAR (tem);
4859 }
4860 }
4861 }
4862 }
4863
4864 return 0;
4865 }
4866
4867 /* Handle `(raise FACTOR)'. */
4868 if (CONSP (spec)
4869 && EQ (XCAR (spec), Qraise)
4870 && CONSP (XCDR (spec)))
4871 {
4872 if (it)
4873 {
4874 if (!FRAME_WINDOW_P (it->f))
4875 return 0;
4876
4877 #ifdef HAVE_WINDOW_SYSTEM
4878 value = XCAR (XCDR (spec));
4879 if (NUMBERP (value))
4880 {
4881 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4882 it->voffset = - (XFLOATINT (value)
4883 * (FONT_HEIGHT (face->font)));
4884 }
4885 #endif /* HAVE_WINDOW_SYSTEM */
4886 }
4887
4888 return 0;
4889 }
4890
4891 /* Don't handle the other kinds of display specifications
4892 inside a string that we got from a `display' property. */
4893 if (it && it->string_from_display_prop_p)
4894 return 0;
4895
4896 /* Characters having this form of property are not displayed, so
4897 we have to find the end of the property. */
4898 if (it)
4899 {
4900 start_pos = *position;
4901 *position = display_prop_end (it, object, start_pos);
4902 }
4903 value = Qnil;
4904
4905 /* Stop the scan at that end position--we assume that all
4906 text properties change there. */
4907 if (it)
4908 it->stop_charpos = position->charpos;
4909
4910 /* Handle `(left-fringe BITMAP [FACE])'
4911 and `(right-fringe BITMAP [FACE])'. */
4912 if (CONSP (spec)
4913 && (EQ (XCAR (spec), Qleft_fringe)
4914 || EQ (XCAR (spec), Qright_fringe))
4915 && CONSP (XCDR (spec)))
4916 {
4917 int fringe_bitmap;
4918
4919 if (it)
4920 {
4921 if (!FRAME_WINDOW_P (it->f))
4922 /* If we return here, POSITION has been advanced
4923 across the text with this property. */
4924 {
4925 /* Synchronize the bidi iterator with POSITION. This is
4926 needed because we are not going to push the iterator
4927 on behalf of this display property, so there will be
4928 no pop_it call to do this synchronization for us. */
4929 if (it->bidi_p)
4930 {
4931 it->position = *position;
4932 iterate_out_of_display_property (it);
4933 *position = it->position;
4934 }
4935 return 1;
4936 }
4937 }
4938 else if (!frame_window_p)
4939 return 1;
4940
4941 #ifdef HAVE_WINDOW_SYSTEM
4942 value = XCAR (XCDR (spec));
4943 if (!SYMBOLP (value)
4944 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4945 /* If we return here, POSITION has been advanced
4946 across the text with this property. */
4947 {
4948 if (it && it->bidi_p)
4949 {
4950 it->position = *position;
4951 iterate_out_of_display_property (it);
4952 *position = it->position;
4953 }
4954 return 1;
4955 }
4956
4957 if (it)
4958 {
4959 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4960
4961 if (CONSP (XCDR (XCDR (spec))))
4962 {
4963 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4964 int face_id2 = lookup_derived_face (it->f, face_name,
4965 FRINGE_FACE_ID, 0);
4966 if (face_id2 >= 0)
4967 face_id = face_id2;
4968 }
4969
4970 /* Save current settings of IT so that we can restore them
4971 when we are finished with the glyph property value. */
4972 push_it (it, position);
4973
4974 it->area = TEXT_AREA;
4975 it->what = IT_IMAGE;
4976 it->image_id = -1; /* no image */
4977 it->position = start_pos;
4978 it->object = NILP (object) ? it->w->contents : object;
4979 it->method = GET_FROM_IMAGE;
4980 it->from_overlay = Qnil;
4981 it->face_id = face_id;
4982 it->from_disp_prop_p = 1;
4983
4984 /* Say that we haven't consumed the characters with
4985 `display' property yet. The call to pop_it in
4986 set_iterator_to_next will clean this up. */
4987 *position = start_pos;
4988
4989 if (EQ (XCAR (spec), Qleft_fringe))
4990 {
4991 it->left_user_fringe_bitmap = fringe_bitmap;
4992 it->left_user_fringe_face_id = face_id;
4993 }
4994 else
4995 {
4996 it->right_user_fringe_bitmap = fringe_bitmap;
4997 it->right_user_fringe_face_id = face_id;
4998 }
4999 }
5000 #endif /* HAVE_WINDOW_SYSTEM */
5001 return 1;
5002 }
5003
5004 /* Prepare to handle `((margin left-margin) ...)',
5005 `((margin right-margin) ...)' and `((margin nil) ...)'
5006 prefixes for display specifications. */
5007 location = Qunbound;
5008 if (CONSP (spec) && CONSP (XCAR (spec)))
5009 {
5010 Lisp_Object tem;
5011
5012 value = XCDR (spec);
5013 if (CONSP (value))
5014 value = XCAR (value);
5015
5016 tem = XCAR (spec);
5017 if (EQ (XCAR (tem), Qmargin)
5018 && (tem = XCDR (tem),
5019 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5020 (NILP (tem)
5021 || EQ (tem, Qleft_margin)
5022 || EQ (tem, Qright_margin))))
5023 location = tem;
5024 }
5025
5026 if (EQ (location, Qunbound))
5027 {
5028 location = Qnil;
5029 value = spec;
5030 }
5031
5032 /* After this point, VALUE is the property after any
5033 margin prefix has been stripped. It must be a string,
5034 an image specification, or `(space ...)'.
5035
5036 LOCATION specifies where to display: `left-margin',
5037 `right-margin' or nil. */
5038
5039 valid_p = (STRINGP (value)
5040 #ifdef HAVE_WINDOW_SYSTEM
5041 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5042 && valid_image_p (value))
5043 #endif /* not HAVE_WINDOW_SYSTEM */
5044 || (CONSP (value) && EQ (XCAR (value), Qspace)));
5045
5046 if (valid_p && !display_replaced_p)
5047 {
5048 int retval = 1;
5049
5050 if (!it)
5051 {
5052 /* Callers need to know whether the display spec is any kind
5053 of `(space ...)' spec that is about to affect text-area
5054 display. */
5055 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5056 retval = 2;
5057 return retval;
5058 }
5059
5060 /* Save current settings of IT so that we can restore them
5061 when we are finished with the glyph property value. */
5062 push_it (it, position);
5063 it->from_overlay = overlay;
5064 it->from_disp_prop_p = 1;
5065
5066 if (NILP (location))
5067 it->area = TEXT_AREA;
5068 else if (EQ (location, Qleft_margin))
5069 it->area = LEFT_MARGIN_AREA;
5070 else
5071 it->area = RIGHT_MARGIN_AREA;
5072
5073 if (STRINGP (value))
5074 {
5075 it->string = value;
5076 it->multibyte_p = STRING_MULTIBYTE (it->string);
5077 it->current.overlay_string_index = -1;
5078 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5079 it->end_charpos = it->string_nchars = SCHARS (it->string);
5080 it->method = GET_FROM_STRING;
5081 it->stop_charpos = 0;
5082 it->prev_stop = 0;
5083 it->base_level_stop = 0;
5084 it->string_from_display_prop_p = 1;
5085 /* Say that we haven't consumed the characters with
5086 `display' property yet. The call to pop_it in
5087 set_iterator_to_next will clean this up. */
5088 if (BUFFERP (object))
5089 *position = start_pos;
5090
5091 /* Force paragraph direction to be that of the parent
5092 object. If the parent object's paragraph direction is
5093 not yet determined, default to L2R. */
5094 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5095 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5096 else
5097 it->paragraph_embedding = L2R;
5098
5099 /* Set up the bidi iterator for this display string. */
5100 if (it->bidi_p)
5101 {
5102 it->bidi_it.string.lstring = it->string;
5103 it->bidi_it.string.s = NULL;
5104 it->bidi_it.string.schars = it->end_charpos;
5105 it->bidi_it.string.bufpos = bufpos;
5106 it->bidi_it.string.from_disp_str = 1;
5107 it->bidi_it.string.unibyte = !it->multibyte_p;
5108 it->bidi_it.w = it->w;
5109 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5110 }
5111 }
5112 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5113 {
5114 it->method = GET_FROM_STRETCH;
5115 it->object = value;
5116 *position = it->position = start_pos;
5117 retval = 1 + (it->area == TEXT_AREA);
5118 }
5119 #ifdef HAVE_WINDOW_SYSTEM
5120 else
5121 {
5122 it->what = IT_IMAGE;
5123 it->image_id = lookup_image (it->f, value);
5124 it->position = start_pos;
5125 it->object = NILP (object) ? it->w->contents : object;
5126 it->method = GET_FROM_IMAGE;
5127
5128 /* Say that we haven't consumed the characters with
5129 `display' property yet. The call to pop_it in
5130 set_iterator_to_next will clean this up. */
5131 *position = start_pos;
5132 }
5133 #endif /* HAVE_WINDOW_SYSTEM */
5134
5135 return retval;
5136 }
5137
5138 /* Invalid property or property not supported. Restore
5139 POSITION to what it was before. */
5140 *position = start_pos;
5141 return 0;
5142 }
5143
5144 /* Check if PROP is a display property value whose text should be
5145 treated as intangible. OVERLAY is the overlay from which PROP
5146 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5147 specify the buffer position covered by PROP. */
5148
5149 int
5150 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5151 ptrdiff_t charpos, ptrdiff_t bytepos)
5152 {
5153 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5154 struct text_pos position;
5155
5156 SET_TEXT_POS (position, charpos, bytepos);
5157 return handle_display_spec (NULL, prop, Qnil, overlay,
5158 &position, charpos, frame_window_p);
5159 }
5160
5161
5162 /* Return 1 if PROP is a display sub-property value containing STRING.
5163
5164 Implementation note: this and the following function are really
5165 special cases of handle_display_spec and
5166 handle_single_display_spec, and should ideally use the same code.
5167 Until they do, these two pairs must be consistent and must be
5168 modified in sync. */
5169
5170 static int
5171 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5172 {
5173 if (EQ (string, prop))
5174 return 1;
5175
5176 /* Skip over `when FORM'. */
5177 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5178 {
5179 prop = XCDR (prop);
5180 if (!CONSP (prop))
5181 return 0;
5182 /* Actually, the condition following `when' should be eval'ed,
5183 like handle_single_display_spec does, and we should return
5184 zero if it evaluates to nil. However, this function is
5185 called only when the buffer was already displayed and some
5186 glyph in the glyph matrix was found to come from a display
5187 string. Therefore, the condition was already evaluated, and
5188 the result was non-nil, otherwise the display string wouldn't
5189 have been displayed and we would have never been called for
5190 this property. Thus, we can skip the evaluation and assume
5191 its result is non-nil. */
5192 prop = XCDR (prop);
5193 }
5194
5195 if (CONSP (prop))
5196 /* Skip over `margin LOCATION'. */
5197 if (EQ (XCAR (prop), Qmargin))
5198 {
5199 prop = XCDR (prop);
5200 if (!CONSP (prop))
5201 return 0;
5202
5203 prop = XCDR (prop);
5204 if (!CONSP (prop))
5205 return 0;
5206 }
5207
5208 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5209 }
5210
5211
5212 /* Return 1 if STRING appears in the `display' property PROP. */
5213
5214 static int
5215 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5216 {
5217 if (CONSP (prop)
5218 && !EQ (XCAR (prop), Qwhen)
5219 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5220 {
5221 /* A list of sub-properties. */
5222 while (CONSP (prop))
5223 {
5224 if (single_display_spec_string_p (XCAR (prop), string))
5225 return 1;
5226 prop = XCDR (prop);
5227 }
5228 }
5229 else if (VECTORP (prop))
5230 {
5231 /* A vector of sub-properties. */
5232 ptrdiff_t i;
5233 for (i = 0; i < ASIZE (prop); ++i)
5234 if (single_display_spec_string_p (AREF (prop, i), string))
5235 return 1;
5236 }
5237 else
5238 return single_display_spec_string_p (prop, string);
5239
5240 return 0;
5241 }
5242
5243 /* Look for STRING in overlays and text properties in the current
5244 buffer, between character positions FROM and TO (excluding TO).
5245 BACK_P non-zero means look back (in this case, TO is supposed to be
5246 less than FROM).
5247 Value is the first character position where STRING was found, or
5248 zero if it wasn't found before hitting TO.
5249
5250 This function may only use code that doesn't eval because it is
5251 called asynchronously from note_mouse_highlight. */
5252
5253 static ptrdiff_t
5254 string_buffer_position_lim (Lisp_Object string,
5255 ptrdiff_t from, ptrdiff_t to, int back_p)
5256 {
5257 Lisp_Object limit, prop, pos;
5258 int found = 0;
5259
5260 pos = make_number (max (from, BEGV));
5261
5262 if (!back_p) /* looking forward */
5263 {
5264 limit = make_number (min (to, ZV));
5265 while (!found && !EQ (pos, limit))
5266 {
5267 prop = Fget_char_property (pos, Qdisplay, Qnil);
5268 if (!NILP (prop) && display_prop_string_p (prop, string))
5269 found = 1;
5270 else
5271 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5272 limit);
5273 }
5274 }
5275 else /* looking back */
5276 {
5277 limit = make_number (max (to, BEGV));
5278 while (!found && !EQ (pos, limit))
5279 {
5280 prop = Fget_char_property (pos, Qdisplay, Qnil);
5281 if (!NILP (prop) && display_prop_string_p (prop, string))
5282 found = 1;
5283 else
5284 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5285 limit);
5286 }
5287 }
5288
5289 return found ? XINT (pos) : 0;
5290 }
5291
5292 /* Determine which buffer position in current buffer STRING comes from.
5293 AROUND_CHARPOS is an approximate position where it could come from.
5294 Value is the buffer position or 0 if it couldn't be determined.
5295
5296 This function is necessary because we don't record buffer positions
5297 in glyphs generated from strings (to keep struct glyph small).
5298 This function may only use code that doesn't eval because it is
5299 called asynchronously from note_mouse_highlight. */
5300
5301 static ptrdiff_t
5302 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5303 {
5304 const int MAX_DISTANCE = 1000;
5305 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5306 around_charpos + MAX_DISTANCE,
5307 0);
5308
5309 if (!found)
5310 found = string_buffer_position_lim (string, around_charpos,
5311 around_charpos - MAX_DISTANCE, 1);
5312 return found;
5313 }
5314
5315
5316 \f
5317 /***********************************************************************
5318 `composition' property
5319 ***********************************************************************/
5320
5321 /* Set up iterator IT from `composition' property at its current
5322 position. Called from handle_stop. */
5323
5324 static enum prop_handled
5325 handle_composition_prop (struct it *it)
5326 {
5327 Lisp_Object prop, string;
5328 ptrdiff_t pos, pos_byte, start, end;
5329
5330 if (STRINGP (it->string))
5331 {
5332 unsigned char *s;
5333
5334 pos = IT_STRING_CHARPOS (*it);
5335 pos_byte = IT_STRING_BYTEPOS (*it);
5336 string = it->string;
5337 s = SDATA (string) + pos_byte;
5338 it->c = STRING_CHAR (s);
5339 }
5340 else
5341 {
5342 pos = IT_CHARPOS (*it);
5343 pos_byte = IT_BYTEPOS (*it);
5344 string = Qnil;
5345 it->c = FETCH_CHAR (pos_byte);
5346 }
5347
5348 /* If there's a valid composition and point is not inside of the
5349 composition (in the case that the composition is from the current
5350 buffer), draw a glyph composed from the composition components. */
5351 if (find_composition (pos, -1, &start, &end, &prop, string)
5352 && composition_valid_p (start, end, prop)
5353 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5354 {
5355 if (start < pos)
5356 /* As we can't handle this situation (perhaps font-lock added
5357 a new composition), we just return here hoping that next
5358 redisplay will detect this composition much earlier. */
5359 return HANDLED_NORMALLY;
5360 if (start != pos)
5361 {
5362 if (STRINGP (it->string))
5363 pos_byte = string_char_to_byte (it->string, start);
5364 else
5365 pos_byte = CHAR_TO_BYTE (start);
5366 }
5367 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5368 prop, string);
5369
5370 if (it->cmp_it.id >= 0)
5371 {
5372 it->cmp_it.ch = -1;
5373 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5374 it->cmp_it.nglyphs = -1;
5375 }
5376 }
5377
5378 return HANDLED_NORMALLY;
5379 }
5380
5381
5382 \f
5383 /***********************************************************************
5384 Overlay strings
5385 ***********************************************************************/
5386
5387 /* The following structure is used to record overlay strings for
5388 later sorting in load_overlay_strings. */
5389
5390 struct overlay_entry
5391 {
5392 Lisp_Object overlay;
5393 Lisp_Object string;
5394 EMACS_INT priority;
5395 int after_string_p;
5396 };
5397
5398
5399 /* Set up iterator IT from overlay strings at its current position.
5400 Called from handle_stop. */
5401
5402 static enum prop_handled
5403 handle_overlay_change (struct it *it)
5404 {
5405 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5406 return HANDLED_RECOMPUTE_PROPS;
5407 else
5408 return HANDLED_NORMALLY;
5409 }
5410
5411
5412 /* Set up the next overlay string for delivery by IT, if there is an
5413 overlay string to deliver. Called by set_iterator_to_next when the
5414 end of the current overlay string is reached. If there are more
5415 overlay strings to display, IT->string and
5416 IT->current.overlay_string_index are set appropriately here.
5417 Otherwise IT->string is set to nil. */
5418
5419 static void
5420 next_overlay_string (struct it *it)
5421 {
5422 ++it->current.overlay_string_index;
5423 if (it->current.overlay_string_index == it->n_overlay_strings)
5424 {
5425 /* No more overlay strings. Restore IT's settings to what
5426 they were before overlay strings were processed, and
5427 continue to deliver from current_buffer. */
5428
5429 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5430 pop_it (it);
5431 eassert (it->sp > 0
5432 || (NILP (it->string)
5433 && it->method == GET_FROM_BUFFER
5434 && it->stop_charpos >= BEGV
5435 && it->stop_charpos <= it->end_charpos));
5436 it->current.overlay_string_index = -1;
5437 it->n_overlay_strings = 0;
5438 it->overlay_strings_charpos = -1;
5439 /* If there's an empty display string on the stack, pop the
5440 stack, to resync the bidi iterator with IT's position. Such
5441 empty strings are pushed onto the stack in
5442 get_overlay_strings_1. */
5443 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5444 pop_it (it);
5445
5446 /* If we're at the end of the buffer, record that we have
5447 processed the overlay strings there already, so that
5448 next_element_from_buffer doesn't try it again. */
5449 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5450 it->overlay_strings_at_end_processed_p = 1;
5451 }
5452 else
5453 {
5454 /* There are more overlay strings to process. If
5455 IT->current.overlay_string_index has advanced to a position
5456 where we must load IT->overlay_strings with more strings, do
5457 it. We must load at the IT->overlay_strings_charpos where
5458 IT->n_overlay_strings was originally computed; when invisible
5459 text is present, this might not be IT_CHARPOS (Bug#7016). */
5460 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5461
5462 if (it->current.overlay_string_index && i == 0)
5463 load_overlay_strings (it, it->overlay_strings_charpos);
5464
5465 /* Initialize IT to deliver display elements from the overlay
5466 string. */
5467 it->string = it->overlay_strings[i];
5468 it->multibyte_p = STRING_MULTIBYTE (it->string);
5469 SET_TEXT_POS (it->current.string_pos, 0, 0);
5470 it->method = GET_FROM_STRING;
5471 it->stop_charpos = 0;
5472 it->end_charpos = SCHARS (it->string);
5473 if (it->cmp_it.stop_pos >= 0)
5474 it->cmp_it.stop_pos = 0;
5475 it->prev_stop = 0;
5476 it->base_level_stop = 0;
5477
5478 /* Set up the bidi iterator for this overlay string. */
5479 if (it->bidi_p)
5480 {
5481 it->bidi_it.string.lstring = it->string;
5482 it->bidi_it.string.s = NULL;
5483 it->bidi_it.string.schars = SCHARS (it->string);
5484 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5485 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5486 it->bidi_it.string.unibyte = !it->multibyte_p;
5487 it->bidi_it.w = it->w;
5488 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5489 }
5490 }
5491
5492 CHECK_IT (it);
5493 }
5494
5495
5496 /* Compare two overlay_entry structures E1 and E2. Used as a
5497 comparison function for qsort in load_overlay_strings. Overlay
5498 strings for the same position are sorted so that
5499
5500 1. All after-strings come in front of before-strings, except
5501 when they come from the same overlay.
5502
5503 2. Within after-strings, strings are sorted so that overlay strings
5504 from overlays with higher priorities come first.
5505
5506 2. Within before-strings, strings are sorted so that overlay
5507 strings from overlays with higher priorities come last.
5508
5509 Value is analogous to strcmp. */
5510
5511
5512 static int
5513 compare_overlay_entries (const void *e1, const void *e2)
5514 {
5515 struct overlay_entry const *entry1 = e1;
5516 struct overlay_entry const *entry2 = e2;
5517 int result;
5518
5519 if (entry1->after_string_p != entry2->after_string_p)
5520 {
5521 /* Let after-strings appear in front of before-strings if
5522 they come from different overlays. */
5523 if (EQ (entry1->overlay, entry2->overlay))
5524 result = entry1->after_string_p ? 1 : -1;
5525 else
5526 result = entry1->after_string_p ? -1 : 1;
5527 }
5528 else if (entry1->priority != entry2->priority)
5529 {
5530 if (entry1->after_string_p)
5531 /* After-strings sorted in order of decreasing priority. */
5532 result = entry2->priority < entry1->priority ? -1 : 1;
5533 else
5534 /* Before-strings sorted in order of increasing priority. */
5535 result = entry1->priority < entry2->priority ? -1 : 1;
5536 }
5537 else
5538 result = 0;
5539
5540 return result;
5541 }
5542
5543
5544 /* Load the vector IT->overlay_strings with overlay strings from IT's
5545 current buffer position, or from CHARPOS if that is > 0. Set
5546 IT->n_overlays to the total number of overlay strings found.
5547
5548 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5549 a time. On entry into load_overlay_strings,
5550 IT->current.overlay_string_index gives the number of overlay
5551 strings that have already been loaded by previous calls to this
5552 function.
5553
5554 IT->add_overlay_start contains an additional overlay start
5555 position to consider for taking overlay strings from, if non-zero.
5556 This position comes into play when the overlay has an `invisible'
5557 property, and both before and after-strings. When we've skipped to
5558 the end of the overlay, because of its `invisible' property, we
5559 nevertheless want its before-string to appear.
5560 IT->add_overlay_start will contain the overlay start position
5561 in this case.
5562
5563 Overlay strings are sorted so that after-string strings come in
5564 front of before-string strings. Within before and after-strings,
5565 strings are sorted by overlay priority. See also function
5566 compare_overlay_entries. */
5567
5568 static void
5569 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5570 {
5571 Lisp_Object overlay, window, str, invisible;
5572 struct Lisp_Overlay *ov;
5573 ptrdiff_t start, end;
5574 ptrdiff_t size = 20;
5575 ptrdiff_t n = 0, i, j;
5576 int invis_p;
5577 struct overlay_entry *entries = alloca (size * sizeof *entries);
5578 USE_SAFE_ALLOCA;
5579
5580 if (charpos <= 0)
5581 charpos = IT_CHARPOS (*it);
5582
5583 /* Append the overlay string STRING of overlay OVERLAY to vector
5584 `entries' which has size `size' and currently contains `n'
5585 elements. AFTER_P non-zero means STRING is an after-string of
5586 OVERLAY. */
5587 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5588 do \
5589 { \
5590 Lisp_Object priority; \
5591 \
5592 if (n == size) \
5593 { \
5594 struct overlay_entry *old = entries; \
5595 SAFE_NALLOCA (entries, 2, size); \
5596 memcpy (entries, old, size * sizeof *entries); \
5597 size *= 2; \
5598 } \
5599 \
5600 entries[n].string = (STRING); \
5601 entries[n].overlay = (OVERLAY); \
5602 priority = Foverlay_get ((OVERLAY), Qpriority); \
5603 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5604 entries[n].after_string_p = (AFTER_P); \
5605 ++n; \
5606 } \
5607 while (0)
5608
5609 /* Process overlay before the overlay center. */
5610 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5611 {
5612 XSETMISC (overlay, ov);
5613 eassert (OVERLAYP (overlay));
5614 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5615 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5616
5617 if (end < charpos)
5618 break;
5619
5620 /* Skip this overlay if it doesn't start or end at IT's current
5621 position. */
5622 if (end != charpos && start != charpos)
5623 continue;
5624
5625 /* Skip this overlay if it doesn't apply to IT->w. */
5626 window = Foverlay_get (overlay, Qwindow);
5627 if (WINDOWP (window) && XWINDOW (window) != it->w)
5628 continue;
5629
5630 /* If the text ``under'' the overlay is invisible, both before-
5631 and after-strings from this overlay are visible; start and
5632 end position are indistinguishable. */
5633 invisible = Foverlay_get (overlay, Qinvisible);
5634 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5635
5636 /* If overlay has a non-empty before-string, record it. */
5637 if ((start == charpos || (end == charpos && invis_p))
5638 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5639 && SCHARS (str))
5640 RECORD_OVERLAY_STRING (overlay, str, 0);
5641
5642 /* If overlay has a non-empty after-string, record it. */
5643 if ((end == charpos || (start == charpos && invis_p))
5644 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5645 && SCHARS (str))
5646 RECORD_OVERLAY_STRING (overlay, str, 1);
5647 }
5648
5649 /* Process overlays after the overlay center. */
5650 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5651 {
5652 XSETMISC (overlay, ov);
5653 eassert (OVERLAYP (overlay));
5654 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5655 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5656
5657 if (start > charpos)
5658 break;
5659
5660 /* Skip this overlay if it doesn't start or end at IT's current
5661 position. */
5662 if (end != charpos && start != charpos)
5663 continue;
5664
5665 /* Skip this overlay if it doesn't apply to IT->w. */
5666 window = Foverlay_get (overlay, Qwindow);
5667 if (WINDOWP (window) && XWINDOW (window) != it->w)
5668 continue;
5669
5670 /* If the text ``under'' the overlay is invisible, it has a zero
5671 dimension, and both before- and after-strings apply. */
5672 invisible = Foverlay_get (overlay, Qinvisible);
5673 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5674
5675 /* If overlay has a non-empty before-string, record it. */
5676 if ((start == charpos || (end == charpos && invis_p))
5677 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5678 && SCHARS (str))
5679 RECORD_OVERLAY_STRING (overlay, str, 0);
5680
5681 /* If overlay has a non-empty after-string, record it. */
5682 if ((end == charpos || (start == charpos && invis_p))
5683 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5684 && SCHARS (str))
5685 RECORD_OVERLAY_STRING (overlay, str, 1);
5686 }
5687
5688 #undef RECORD_OVERLAY_STRING
5689
5690 /* Sort entries. */
5691 if (n > 1)
5692 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5693
5694 /* Record number of overlay strings, and where we computed it. */
5695 it->n_overlay_strings = n;
5696 it->overlay_strings_charpos = charpos;
5697
5698 /* IT->current.overlay_string_index is the number of overlay strings
5699 that have already been consumed by IT. Copy some of the
5700 remaining overlay strings to IT->overlay_strings. */
5701 i = 0;
5702 j = it->current.overlay_string_index;
5703 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5704 {
5705 it->overlay_strings[i] = entries[j].string;
5706 it->string_overlays[i++] = entries[j++].overlay;
5707 }
5708
5709 CHECK_IT (it);
5710 SAFE_FREE ();
5711 }
5712
5713
5714 /* Get the first chunk of overlay strings at IT's current buffer
5715 position, or at CHARPOS if that is > 0. Value is non-zero if at
5716 least one overlay string was found. */
5717
5718 static int
5719 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5720 {
5721 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5722 process. This fills IT->overlay_strings with strings, and sets
5723 IT->n_overlay_strings to the total number of strings to process.
5724 IT->pos.overlay_string_index has to be set temporarily to zero
5725 because load_overlay_strings needs this; it must be set to -1
5726 when no overlay strings are found because a zero value would
5727 indicate a position in the first overlay string. */
5728 it->current.overlay_string_index = 0;
5729 load_overlay_strings (it, charpos);
5730
5731 /* If we found overlay strings, set up IT to deliver display
5732 elements from the first one. Otherwise set up IT to deliver
5733 from current_buffer. */
5734 if (it->n_overlay_strings)
5735 {
5736 /* Make sure we know settings in current_buffer, so that we can
5737 restore meaningful values when we're done with the overlay
5738 strings. */
5739 if (compute_stop_p)
5740 compute_stop_pos (it);
5741 eassert (it->face_id >= 0);
5742
5743 /* Save IT's settings. They are restored after all overlay
5744 strings have been processed. */
5745 eassert (!compute_stop_p || it->sp == 0);
5746
5747 /* When called from handle_stop, there might be an empty display
5748 string loaded. In that case, don't bother saving it. But
5749 don't use this optimization with the bidi iterator, since we
5750 need the corresponding pop_it call to resync the bidi
5751 iterator's position with IT's position, after we are done
5752 with the overlay strings. (The corresponding call to pop_it
5753 in case of an empty display string is in
5754 next_overlay_string.) */
5755 if (!(!it->bidi_p
5756 && STRINGP (it->string) && !SCHARS (it->string)))
5757 push_it (it, NULL);
5758
5759 /* Set up IT to deliver display elements from the first overlay
5760 string. */
5761 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5762 it->string = it->overlay_strings[0];
5763 it->from_overlay = Qnil;
5764 it->stop_charpos = 0;
5765 eassert (STRINGP (it->string));
5766 it->end_charpos = SCHARS (it->string);
5767 it->prev_stop = 0;
5768 it->base_level_stop = 0;
5769 it->multibyte_p = STRING_MULTIBYTE (it->string);
5770 it->method = GET_FROM_STRING;
5771 it->from_disp_prop_p = 0;
5772
5773 /* Force paragraph direction to be that of the parent
5774 buffer. */
5775 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5776 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5777 else
5778 it->paragraph_embedding = L2R;
5779
5780 /* Set up the bidi iterator for this overlay string. */
5781 if (it->bidi_p)
5782 {
5783 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5784
5785 it->bidi_it.string.lstring = it->string;
5786 it->bidi_it.string.s = NULL;
5787 it->bidi_it.string.schars = SCHARS (it->string);
5788 it->bidi_it.string.bufpos = pos;
5789 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5790 it->bidi_it.string.unibyte = !it->multibyte_p;
5791 it->bidi_it.w = it->w;
5792 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5793 }
5794 return 1;
5795 }
5796
5797 it->current.overlay_string_index = -1;
5798 return 0;
5799 }
5800
5801 static int
5802 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5803 {
5804 it->string = Qnil;
5805 it->method = GET_FROM_BUFFER;
5806
5807 (void) get_overlay_strings_1 (it, charpos, 1);
5808
5809 CHECK_IT (it);
5810
5811 /* Value is non-zero if we found at least one overlay string. */
5812 return STRINGP (it->string);
5813 }
5814
5815
5816 \f
5817 /***********************************************************************
5818 Saving and restoring state
5819 ***********************************************************************/
5820
5821 /* Save current settings of IT on IT->stack. Called, for example,
5822 before setting up IT for an overlay string, to be able to restore
5823 IT's settings to what they were after the overlay string has been
5824 processed. If POSITION is non-NULL, it is the position to save on
5825 the stack instead of IT->position. */
5826
5827 static void
5828 push_it (struct it *it, struct text_pos *position)
5829 {
5830 struct iterator_stack_entry *p;
5831
5832 eassert (it->sp < IT_STACK_SIZE);
5833 p = it->stack + it->sp;
5834
5835 p->stop_charpos = it->stop_charpos;
5836 p->prev_stop = it->prev_stop;
5837 p->base_level_stop = it->base_level_stop;
5838 p->cmp_it = it->cmp_it;
5839 eassert (it->face_id >= 0);
5840 p->face_id = it->face_id;
5841 p->string = it->string;
5842 p->method = it->method;
5843 p->from_overlay = it->from_overlay;
5844 switch (p->method)
5845 {
5846 case GET_FROM_IMAGE:
5847 p->u.image.object = it->object;
5848 p->u.image.image_id = it->image_id;
5849 p->u.image.slice = it->slice;
5850 break;
5851 case GET_FROM_STRETCH:
5852 p->u.stretch.object = it->object;
5853 break;
5854 }
5855 p->position = position ? *position : it->position;
5856 p->current = it->current;
5857 p->end_charpos = it->end_charpos;
5858 p->string_nchars = it->string_nchars;
5859 p->area = it->area;
5860 p->multibyte_p = it->multibyte_p;
5861 p->avoid_cursor_p = it->avoid_cursor_p;
5862 p->space_width = it->space_width;
5863 p->font_height = it->font_height;
5864 p->voffset = it->voffset;
5865 p->string_from_display_prop_p = it->string_from_display_prop_p;
5866 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5867 p->display_ellipsis_p = 0;
5868 p->line_wrap = it->line_wrap;
5869 p->bidi_p = it->bidi_p;
5870 p->paragraph_embedding = it->paragraph_embedding;
5871 p->from_disp_prop_p = it->from_disp_prop_p;
5872 ++it->sp;
5873
5874 /* Save the state of the bidi iterator as well. */
5875 if (it->bidi_p)
5876 bidi_push_it (&it->bidi_it);
5877 }
5878
5879 static void
5880 iterate_out_of_display_property (struct it *it)
5881 {
5882 int buffer_p = !STRINGP (it->string);
5883 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5884 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5885
5886 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5887
5888 /* Maybe initialize paragraph direction. If we are at the beginning
5889 of a new paragraph, next_element_from_buffer may not have a
5890 chance to do that. */
5891 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5892 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5893 /* prev_stop can be zero, so check against BEGV as well. */
5894 while (it->bidi_it.charpos >= bob
5895 && it->prev_stop <= it->bidi_it.charpos
5896 && it->bidi_it.charpos < CHARPOS (it->position)
5897 && it->bidi_it.charpos < eob)
5898 bidi_move_to_visually_next (&it->bidi_it);
5899 /* Record the stop_pos we just crossed, for when we cross it
5900 back, maybe. */
5901 if (it->bidi_it.charpos > CHARPOS (it->position))
5902 it->prev_stop = CHARPOS (it->position);
5903 /* If we ended up not where pop_it put us, resync IT's
5904 positional members with the bidi iterator. */
5905 if (it->bidi_it.charpos != CHARPOS (it->position))
5906 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5907 if (buffer_p)
5908 it->current.pos = it->position;
5909 else
5910 it->current.string_pos = it->position;
5911 }
5912
5913 /* Restore IT's settings from IT->stack. Called, for example, when no
5914 more overlay strings must be processed, and we return to delivering
5915 display elements from a buffer, or when the end of a string from a
5916 `display' property is reached and we return to delivering display
5917 elements from an overlay string, or from a buffer. */
5918
5919 static void
5920 pop_it (struct it *it)
5921 {
5922 struct iterator_stack_entry *p;
5923 int from_display_prop = it->from_disp_prop_p;
5924
5925 eassert (it->sp > 0);
5926 --it->sp;
5927 p = it->stack + it->sp;
5928 it->stop_charpos = p->stop_charpos;
5929 it->prev_stop = p->prev_stop;
5930 it->base_level_stop = p->base_level_stop;
5931 it->cmp_it = p->cmp_it;
5932 it->face_id = p->face_id;
5933 it->current = p->current;
5934 it->position = p->position;
5935 it->string = p->string;
5936 it->from_overlay = p->from_overlay;
5937 if (NILP (it->string))
5938 SET_TEXT_POS (it->current.string_pos, -1, -1);
5939 it->method = p->method;
5940 switch (it->method)
5941 {
5942 case GET_FROM_IMAGE:
5943 it->image_id = p->u.image.image_id;
5944 it->object = p->u.image.object;
5945 it->slice = p->u.image.slice;
5946 break;
5947 case GET_FROM_STRETCH:
5948 it->object = p->u.stretch.object;
5949 break;
5950 case GET_FROM_BUFFER:
5951 it->object = it->w->contents;
5952 break;
5953 case GET_FROM_STRING:
5954 it->object = it->string;
5955 break;
5956 case GET_FROM_DISPLAY_VECTOR:
5957 if (it->s)
5958 it->method = GET_FROM_C_STRING;
5959 else if (STRINGP (it->string))
5960 it->method = GET_FROM_STRING;
5961 else
5962 {
5963 it->method = GET_FROM_BUFFER;
5964 it->object = it->w->contents;
5965 }
5966 }
5967 it->end_charpos = p->end_charpos;
5968 it->string_nchars = p->string_nchars;
5969 it->area = p->area;
5970 it->multibyte_p = p->multibyte_p;
5971 it->avoid_cursor_p = p->avoid_cursor_p;
5972 it->space_width = p->space_width;
5973 it->font_height = p->font_height;
5974 it->voffset = p->voffset;
5975 it->string_from_display_prop_p = p->string_from_display_prop_p;
5976 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5977 it->line_wrap = p->line_wrap;
5978 it->bidi_p = p->bidi_p;
5979 it->paragraph_embedding = p->paragraph_embedding;
5980 it->from_disp_prop_p = p->from_disp_prop_p;
5981 if (it->bidi_p)
5982 {
5983 bidi_pop_it (&it->bidi_it);
5984 /* Bidi-iterate until we get out of the portion of text, if any,
5985 covered by a `display' text property or by an overlay with
5986 `display' property. (We cannot just jump there, because the
5987 internal coherency of the bidi iterator state can not be
5988 preserved across such jumps.) We also must determine the
5989 paragraph base direction if the overlay we just processed is
5990 at the beginning of a new paragraph. */
5991 if (from_display_prop
5992 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5993 iterate_out_of_display_property (it);
5994
5995 eassert ((BUFFERP (it->object)
5996 && IT_CHARPOS (*it) == it->bidi_it.charpos
5997 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5998 || (STRINGP (it->object)
5999 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
6000 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
6001 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
6002 }
6003 }
6004
6005
6006 \f
6007 /***********************************************************************
6008 Moving over lines
6009 ***********************************************************************/
6010
6011 /* Set IT's current position to the previous line start. */
6012
6013 static void
6014 back_to_previous_line_start (struct it *it)
6015 {
6016 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6017
6018 DEC_BOTH (cp, bp);
6019 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6020 }
6021
6022
6023 /* Move IT to the next line start.
6024
6025 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
6026 we skipped over part of the text (as opposed to moving the iterator
6027 continuously over the text). Otherwise, don't change the value
6028 of *SKIPPED_P.
6029
6030 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6031 iterator on the newline, if it was found.
6032
6033 Newlines may come from buffer text, overlay strings, or strings
6034 displayed via the `display' property. That's the reason we can't
6035 simply use find_newline_no_quit.
6036
6037 Note that this function may not skip over invisible text that is so
6038 because of text properties and immediately follows a newline. If
6039 it would, function reseat_at_next_visible_line_start, when called
6040 from set_iterator_to_next, would effectively make invisible
6041 characters following a newline part of the wrong glyph row, which
6042 leads to wrong cursor motion. */
6043
6044 static int
6045 forward_to_next_line_start (struct it *it, int *skipped_p,
6046 struct bidi_it *bidi_it_prev)
6047 {
6048 ptrdiff_t old_selective;
6049 int newline_found_p, n;
6050 const int MAX_NEWLINE_DISTANCE = 500;
6051
6052 /* If already on a newline, just consume it to avoid unintended
6053 skipping over invisible text below. */
6054 if (it->what == IT_CHARACTER
6055 && it->c == '\n'
6056 && CHARPOS (it->position) == IT_CHARPOS (*it))
6057 {
6058 if (it->bidi_p && bidi_it_prev)
6059 *bidi_it_prev = it->bidi_it;
6060 set_iterator_to_next (it, 0);
6061 it->c = 0;
6062 return 1;
6063 }
6064
6065 /* Don't handle selective display in the following. It's (a)
6066 unnecessary because it's done by the caller, and (b) leads to an
6067 infinite recursion because next_element_from_ellipsis indirectly
6068 calls this function. */
6069 old_selective = it->selective;
6070 it->selective = 0;
6071
6072 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6073 from buffer text. */
6074 for (n = newline_found_p = 0;
6075 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6076 n += STRINGP (it->string) ? 0 : 1)
6077 {
6078 if (!get_next_display_element (it))
6079 return 0;
6080 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6081 if (newline_found_p && it->bidi_p && bidi_it_prev)
6082 *bidi_it_prev = it->bidi_it;
6083 set_iterator_to_next (it, 0);
6084 }
6085
6086 /* If we didn't find a newline near enough, see if we can use a
6087 short-cut. */
6088 if (!newline_found_p)
6089 {
6090 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6091 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6092 1, &bytepos);
6093 Lisp_Object pos;
6094
6095 eassert (!STRINGP (it->string));
6096
6097 /* If there isn't any `display' property in sight, and no
6098 overlays, we can just use the position of the newline in
6099 buffer text. */
6100 if (it->stop_charpos >= limit
6101 || ((pos = Fnext_single_property_change (make_number (start),
6102 Qdisplay, Qnil,
6103 make_number (limit)),
6104 NILP (pos))
6105 && next_overlay_change (start) == ZV))
6106 {
6107 if (!it->bidi_p)
6108 {
6109 IT_CHARPOS (*it) = limit;
6110 IT_BYTEPOS (*it) = bytepos;
6111 }
6112 else
6113 {
6114 struct bidi_it bprev;
6115
6116 /* Help bidi.c avoid expensive searches for display
6117 properties and overlays, by telling it that there are
6118 none up to `limit'. */
6119 if (it->bidi_it.disp_pos < limit)
6120 {
6121 it->bidi_it.disp_pos = limit;
6122 it->bidi_it.disp_prop = 0;
6123 }
6124 do {
6125 bprev = it->bidi_it;
6126 bidi_move_to_visually_next (&it->bidi_it);
6127 } while (it->bidi_it.charpos != limit);
6128 IT_CHARPOS (*it) = limit;
6129 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6130 if (bidi_it_prev)
6131 *bidi_it_prev = bprev;
6132 }
6133 *skipped_p = newline_found_p = 1;
6134 }
6135 else
6136 {
6137 while (get_next_display_element (it)
6138 && !newline_found_p)
6139 {
6140 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6141 if (newline_found_p && it->bidi_p && bidi_it_prev)
6142 *bidi_it_prev = it->bidi_it;
6143 set_iterator_to_next (it, 0);
6144 }
6145 }
6146 }
6147
6148 it->selective = old_selective;
6149 return newline_found_p;
6150 }
6151
6152
6153 /* Set IT's current position to the previous visible line start. Skip
6154 invisible text that is so either due to text properties or due to
6155 selective display. Caution: this does not change IT->current_x and
6156 IT->hpos. */
6157
6158 static void
6159 back_to_previous_visible_line_start (struct it *it)
6160 {
6161 while (IT_CHARPOS (*it) > BEGV)
6162 {
6163 back_to_previous_line_start (it);
6164
6165 if (IT_CHARPOS (*it) <= BEGV)
6166 break;
6167
6168 /* If selective > 0, then lines indented more than its value are
6169 invisible. */
6170 if (it->selective > 0
6171 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6172 it->selective))
6173 continue;
6174
6175 /* Check the newline before point for invisibility. */
6176 {
6177 Lisp_Object prop;
6178 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6179 Qinvisible, it->window);
6180 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6181 continue;
6182 }
6183
6184 if (IT_CHARPOS (*it) <= BEGV)
6185 break;
6186
6187 {
6188 struct it it2;
6189 void *it2data = NULL;
6190 ptrdiff_t pos;
6191 ptrdiff_t beg, end;
6192 Lisp_Object val, overlay;
6193
6194 SAVE_IT (it2, *it, it2data);
6195
6196 /* If newline is part of a composition, continue from start of composition */
6197 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6198 && beg < IT_CHARPOS (*it))
6199 goto replaced;
6200
6201 /* If newline is replaced by a display property, find start of overlay
6202 or interval and continue search from that point. */
6203 pos = --IT_CHARPOS (it2);
6204 --IT_BYTEPOS (it2);
6205 it2.sp = 0;
6206 bidi_unshelve_cache (NULL, 0);
6207 it2.string_from_display_prop_p = 0;
6208 it2.from_disp_prop_p = 0;
6209 if (handle_display_prop (&it2) == HANDLED_RETURN
6210 && !NILP (val = get_char_property_and_overlay
6211 (make_number (pos), Qdisplay, Qnil, &overlay))
6212 && (OVERLAYP (overlay)
6213 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6214 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6215 {
6216 RESTORE_IT (it, it, it2data);
6217 goto replaced;
6218 }
6219
6220 /* Newline is not replaced by anything -- so we are done. */
6221 RESTORE_IT (it, it, it2data);
6222 break;
6223
6224 replaced:
6225 if (beg < BEGV)
6226 beg = BEGV;
6227 IT_CHARPOS (*it) = beg;
6228 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6229 }
6230 }
6231
6232 it->continuation_lines_width = 0;
6233
6234 eassert (IT_CHARPOS (*it) >= BEGV);
6235 eassert (IT_CHARPOS (*it) == BEGV
6236 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6237 CHECK_IT (it);
6238 }
6239
6240
6241 /* Reseat iterator IT at the previous visible line start. Skip
6242 invisible text that is so either due to text properties or due to
6243 selective display. At the end, update IT's overlay information,
6244 face information etc. */
6245
6246 void
6247 reseat_at_previous_visible_line_start (struct it *it)
6248 {
6249 back_to_previous_visible_line_start (it);
6250 reseat (it, it->current.pos, 1);
6251 CHECK_IT (it);
6252 }
6253
6254
6255 /* Reseat iterator IT on the next visible line start in the current
6256 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6257 preceding the line start. Skip over invisible text that is so
6258 because of selective display. Compute faces, overlays etc at the
6259 new position. Note that this function does not skip over text that
6260 is invisible because of text properties. */
6261
6262 static void
6263 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6264 {
6265 int newline_found_p, skipped_p = 0;
6266 struct bidi_it bidi_it_prev;
6267
6268 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6269
6270 /* Skip over lines that are invisible because they are indented
6271 more than the value of IT->selective. */
6272 if (it->selective > 0)
6273 while (IT_CHARPOS (*it) < ZV
6274 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6275 it->selective))
6276 {
6277 eassert (IT_BYTEPOS (*it) == BEGV
6278 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6279 newline_found_p =
6280 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6281 }
6282
6283 /* Position on the newline if that's what's requested. */
6284 if (on_newline_p && newline_found_p)
6285 {
6286 if (STRINGP (it->string))
6287 {
6288 if (IT_STRING_CHARPOS (*it) > 0)
6289 {
6290 if (!it->bidi_p)
6291 {
6292 --IT_STRING_CHARPOS (*it);
6293 --IT_STRING_BYTEPOS (*it);
6294 }
6295 else
6296 {
6297 /* We need to restore the bidi iterator to the state
6298 it had on the newline, and resync the IT's
6299 position with that. */
6300 it->bidi_it = bidi_it_prev;
6301 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6302 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6303 }
6304 }
6305 }
6306 else if (IT_CHARPOS (*it) > BEGV)
6307 {
6308 if (!it->bidi_p)
6309 {
6310 --IT_CHARPOS (*it);
6311 --IT_BYTEPOS (*it);
6312 }
6313 else
6314 {
6315 /* We need to restore the bidi iterator to the state it
6316 had on the newline and resync IT with that. */
6317 it->bidi_it = bidi_it_prev;
6318 IT_CHARPOS (*it) = it->bidi_it.charpos;
6319 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6320 }
6321 reseat (it, it->current.pos, 0);
6322 }
6323 }
6324 else if (skipped_p)
6325 reseat (it, it->current.pos, 0);
6326
6327 CHECK_IT (it);
6328 }
6329
6330
6331 \f
6332 /***********************************************************************
6333 Changing an iterator's position
6334 ***********************************************************************/
6335
6336 /* Change IT's current position to POS in current_buffer. If FORCE_P
6337 is non-zero, always check for text properties at the new position.
6338 Otherwise, text properties are only looked up if POS >=
6339 IT->check_charpos of a property. */
6340
6341 static void
6342 reseat (struct it *it, struct text_pos pos, int force_p)
6343 {
6344 ptrdiff_t original_pos = IT_CHARPOS (*it);
6345
6346 reseat_1 (it, pos, 0);
6347
6348 /* Determine where to check text properties. Avoid doing it
6349 where possible because text property lookup is very expensive. */
6350 if (force_p
6351 || CHARPOS (pos) > it->stop_charpos
6352 || CHARPOS (pos) < original_pos)
6353 {
6354 if (it->bidi_p)
6355 {
6356 /* For bidi iteration, we need to prime prev_stop and
6357 base_level_stop with our best estimations. */
6358 /* Implementation note: Of course, POS is not necessarily a
6359 stop position, so assigning prev_pos to it is a lie; we
6360 should have called compute_stop_backwards. However, if
6361 the current buffer does not include any R2L characters,
6362 that call would be a waste of cycles, because the
6363 iterator will never move back, and thus never cross this
6364 "fake" stop position. So we delay that backward search
6365 until the time we really need it, in next_element_from_buffer. */
6366 if (CHARPOS (pos) != it->prev_stop)
6367 it->prev_stop = CHARPOS (pos);
6368 if (CHARPOS (pos) < it->base_level_stop)
6369 it->base_level_stop = 0; /* meaning it's unknown */
6370 handle_stop (it);
6371 }
6372 else
6373 {
6374 handle_stop (it);
6375 it->prev_stop = it->base_level_stop = 0;
6376 }
6377
6378 }
6379
6380 CHECK_IT (it);
6381 }
6382
6383
6384 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6385 IT->stop_pos to POS, also. */
6386
6387 static void
6388 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6389 {
6390 /* Don't call this function when scanning a C string. */
6391 eassert (it->s == NULL);
6392
6393 /* POS must be a reasonable value. */
6394 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6395
6396 it->current.pos = it->position = pos;
6397 it->end_charpos = ZV;
6398 it->dpvec = NULL;
6399 it->current.dpvec_index = -1;
6400 it->current.overlay_string_index = -1;
6401 IT_STRING_CHARPOS (*it) = -1;
6402 IT_STRING_BYTEPOS (*it) = -1;
6403 it->string = Qnil;
6404 it->method = GET_FROM_BUFFER;
6405 it->object = it->w->contents;
6406 it->area = TEXT_AREA;
6407 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6408 it->sp = 0;
6409 it->string_from_display_prop_p = 0;
6410 it->string_from_prefix_prop_p = 0;
6411
6412 it->from_disp_prop_p = 0;
6413 it->face_before_selective_p = 0;
6414 if (it->bidi_p)
6415 {
6416 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6417 &it->bidi_it);
6418 bidi_unshelve_cache (NULL, 0);
6419 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6420 it->bidi_it.string.s = NULL;
6421 it->bidi_it.string.lstring = Qnil;
6422 it->bidi_it.string.bufpos = 0;
6423 it->bidi_it.string.unibyte = 0;
6424 it->bidi_it.w = it->w;
6425 }
6426
6427 if (set_stop_p)
6428 {
6429 it->stop_charpos = CHARPOS (pos);
6430 it->base_level_stop = CHARPOS (pos);
6431 }
6432 /* This make the information stored in it->cmp_it invalidate. */
6433 it->cmp_it.id = -1;
6434 }
6435
6436
6437 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6438 If S is non-null, it is a C string to iterate over. Otherwise,
6439 STRING gives a Lisp string to iterate over.
6440
6441 If PRECISION > 0, don't return more then PRECISION number of
6442 characters from the string.
6443
6444 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6445 characters have been returned. FIELD_WIDTH < 0 means an infinite
6446 field width.
6447
6448 MULTIBYTE = 0 means disable processing of multibyte characters,
6449 MULTIBYTE > 0 means enable it,
6450 MULTIBYTE < 0 means use IT->multibyte_p.
6451
6452 IT must be initialized via a prior call to init_iterator before
6453 calling this function. */
6454
6455 static void
6456 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6457 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6458 int multibyte)
6459 {
6460 /* No region in strings. */
6461 it->region_beg_charpos = it->region_end_charpos = -1;
6462
6463 /* No text property checks performed by default, but see below. */
6464 it->stop_charpos = -1;
6465
6466 /* Set iterator position and end position. */
6467 memset (&it->current, 0, sizeof it->current);
6468 it->current.overlay_string_index = -1;
6469 it->current.dpvec_index = -1;
6470 eassert (charpos >= 0);
6471
6472 /* If STRING is specified, use its multibyteness, otherwise use the
6473 setting of MULTIBYTE, if specified. */
6474 if (multibyte >= 0)
6475 it->multibyte_p = multibyte > 0;
6476
6477 /* Bidirectional reordering of strings is controlled by the default
6478 value of bidi-display-reordering. Don't try to reorder while
6479 loading loadup.el, as the necessary character property tables are
6480 not yet available. */
6481 it->bidi_p =
6482 NILP (Vpurify_flag)
6483 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6484
6485 if (s == NULL)
6486 {
6487 eassert (STRINGP (string));
6488 it->string = string;
6489 it->s = NULL;
6490 it->end_charpos = it->string_nchars = SCHARS (string);
6491 it->method = GET_FROM_STRING;
6492 it->current.string_pos = string_pos (charpos, string);
6493
6494 if (it->bidi_p)
6495 {
6496 it->bidi_it.string.lstring = string;
6497 it->bidi_it.string.s = NULL;
6498 it->bidi_it.string.schars = it->end_charpos;
6499 it->bidi_it.string.bufpos = 0;
6500 it->bidi_it.string.from_disp_str = 0;
6501 it->bidi_it.string.unibyte = !it->multibyte_p;
6502 it->bidi_it.w = it->w;
6503 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6504 FRAME_WINDOW_P (it->f), &it->bidi_it);
6505 }
6506 }
6507 else
6508 {
6509 it->s = (const unsigned char *) s;
6510 it->string = Qnil;
6511
6512 /* Note that we use IT->current.pos, not it->current.string_pos,
6513 for displaying C strings. */
6514 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6515 if (it->multibyte_p)
6516 {
6517 it->current.pos = c_string_pos (charpos, s, 1);
6518 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6519 }
6520 else
6521 {
6522 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6523 it->end_charpos = it->string_nchars = strlen (s);
6524 }
6525
6526 if (it->bidi_p)
6527 {
6528 it->bidi_it.string.lstring = Qnil;
6529 it->bidi_it.string.s = (const unsigned char *) s;
6530 it->bidi_it.string.schars = it->end_charpos;
6531 it->bidi_it.string.bufpos = 0;
6532 it->bidi_it.string.from_disp_str = 0;
6533 it->bidi_it.string.unibyte = !it->multibyte_p;
6534 it->bidi_it.w = it->w;
6535 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6536 &it->bidi_it);
6537 }
6538 it->method = GET_FROM_C_STRING;
6539 }
6540
6541 /* PRECISION > 0 means don't return more than PRECISION characters
6542 from the string. */
6543 if (precision > 0 && it->end_charpos - charpos > precision)
6544 {
6545 it->end_charpos = it->string_nchars = charpos + precision;
6546 if (it->bidi_p)
6547 it->bidi_it.string.schars = it->end_charpos;
6548 }
6549
6550 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6551 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6552 FIELD_WIDTH < 0 means infinite field width. This is useful for
6553 padding with `-' at the end of a mode line. */
6554 if (field_width < 0)
6555 field_width = INFINITY;
6556 /* Implementation note: We deliberately don't enlarge
6557 it->bidi_it.string.schars here to fit it->end_charpos, because
6558 the bidi iterator cannot produce characters out of thin air. */
6559 if (field_width > it->end_charpos - charpos)
6560 it->end_charpos = charpos + field_width;
6561
6562 /* Use the standard display table for displaying strings. */
6563 if (DISP_TABLE_P (Vstandard_display_table))
6564 it->dp = XCHAR_TABLE (Vstandard_display_table);
6565
6566 it->stop_charpos = charpos;
6567 it->prev_stop = charpos;
6568 it->base_level_stop = 0;
6569 if (it->bidi_p)
6570 {
6571 it->bidi_it.first_elt = 1;
6572 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6573 it->bidi_it.disp_pos = -1;
6574 }
6575 if (s == NULL && it->multibyte_p)
6576 {
6577 ptrdiff_t endpos = SCHARS (it->string);
6578 if (endpos > it->end_charpos)
6579 endpos = it->end_charpos;
6580 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6581 it->string);
6582 }
6583 CHECK_IT (it);
6584 }
6585
6586
6587 \f
6588 /***********************************************************************
6589 Iteration
6590 ***********************************************************************/
6591
6592 /* Map enum it_method value to corresponding next_element_from_* function. */
6593
6594 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6595 {
6596 next_element_from_buffer,
6597 next_element_from_display_vector,
6598 next_element_from_string,
6599 next_element_from_c_string,
6600 next_element_from_image,
6601 next_element_from_stretch
6602 };
6603
6604 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6605
6606
6607 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6608 (possibly with the following characters). */
6609
6610 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6611 ((IT)->cmp_it.id >= 0 \
6612 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6613 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6614 END_CHARPOS, (IT)->w, \
6615 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6616 (IT)->string)))
6617
6618
6619 /* Lookup the char-table Vglyphless_char_display for character C (-1
6620 if we want information for no-font case), and return the display
6621 method symbol. By side-effect, update it->what and
6622 it->glyphless_method. This function is called from
6623 get_next_display_element for each character element, and from
6624 x_produce_glyphs when no suitable font was found. */
6625
6626 Lisp_Object
6627 lookup_glyphless_char_display (int c, struct it *it)
6628 {
6629 Lisp_Object glyphless_method = Qnil;
6630
6631 if (CHAR_TABLE_P (Vglyphless_char_display)
6632 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6633 {
6634 if (c >= 0)
6635 {
6636 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6637 if (CONSP (glyphless_method))
6638 glyphless_method = FRAME_WINDOW_P (it->f)
6639 ? XCAR (glyphless_method)
6640 : XCDR (glyphless_method);
6641 }
6642 else
6643 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6644 }
6645
6646 retry:
6647 if (NILP (glyphless_method))
6648 {
6649 if (c >= 0)
6650 /* The default is to display the character by a proper font. */
6651 return Qnil;
6652 /* The default for the no-font case is to display an empty box. */
6653 glyphless_method = Qempty_box;
6654 }
6655 if (EQ (glyphless_method, Qzero_width))
6656 {
6657 if (c >= 0)
6658 return glyphless_method;
6659 /* This method can't be used for the no-font case. */
6660 glyphless_method = Qempty_box;
6661 }
6662 if (EQ (glyphless_method, Qthin_space))
6663 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6664 else if (EQ (glyphless_method, Qempty_box))
6665 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6666 else if (EQ (glyphless_method, Qhex_code))
6667 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6668 else if (STRINGP (glyphless_method))
6669 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6670 else
6671 {
6672 /* Invalid value. We use the default method. */
6673 glyphless_method = Qnil;
6674 goto retry;
6675 }
6676 it->what = IT_GLYPHLESS;
6677 return glyphless_method;
6678 }
6679
6680 /* Load IT's display element fields with information about the next
6681 display element from the current position of IT. Value is zero if
6682 end of buffer (or C string) is reached. */
6683
6684 static struct frame *last_escape_glyph_frame = NULL;
6685 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6686 static int last_escape_glyph_merged_face_id = 0;
6687
6688 struct frame *last_glyphless_glyph_frame = NULL;
6689 int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6690 int last_glyphless_glyph_merged_face_id = 0;
6691
6692 static int
6693 get_next_display_element (struct it *it)
6694 {
6695 /* Non-zero means that we found a display element. Zero means that
6696 we hit the end of what we iterate over. Performance note: the
6697 function pointer `method' used here turns out to be faster than
6698 using a sequence of if-statements. */
6699 int success_p;
6700
6701 get_next:
6702 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6703
6704 if (it->what == IT_CHARACTER)
6705 {
6706 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6707 and only if (a) the resolved directionality of that character
6708 is R..." */
6709 /* FIXME: Do we need an exception for characters from display
6710 tables? */
6711 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6712 it->c = bidi_mirror_char (it->c);
6713 /* Map via display table or translate control characters.
6714 IT->c, IT->len etc. have been set to the next character by
6715 the function call above. If we have a display table, and it
6716 contains an entry for IT->c, translate it. Don't do this if
6717 IT->c itself comes from a display table, otherwise we could
6718 end up in an infinite recursion. (An alternative could be to
6719 count the recursion depth of this function and signal an
6720 error when a certain maximum depth is reached.) Is it worth
6721 it? */
6722 if (success_p && it->dpvec == NULL)
6723 {
6724 Lisp_Object dv;
6725 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6726 int nonascii_space_p = 0;
6727 int nonascii_hyphen_p = 0;
6728 int c = it->c; /* This is the character to display. */
6729
6730 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6731 {
6732 eassert (SINGLE_BYTE_CHAR_P (c));
6733 if (unibyte_display_via_language_environment)
6734 {
6735 c = DECODE_CHAR (unibyte, c);
6736 if (c < 0)
6737 c = BYTE8_TO_CHAR (it->c);
6738 }
6739 else
6740 c = BYTE8_TO_CHAR (it->c);
6741 }
6742
6743 if (it->dp
6744 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6745 VECTORP (dv)))
6746 {
6747 struct Lisp_Vector *v = XVECTOR (dv);
6748
6749 /* Return the first character from the display table
6750 entry, if not empty. If empty, don't display the
6751 current character. */
6752 if (v->header.size)
6753 {
6754 it->dpvec_char_len = it->len;
6755 it->dpvec = v->contents;
6756 it->dpend = v->contents + v->header.size;
6757 it->current.dpvec_index = 0;
6758 it->dpvec_face_id = -1;
6759 it->saved_face_id = it->face_id;
6760 it->method = GET_FROM_DISPLAY_VECTOR;
6761 it->ellipsis_p = 0;
6762 }
6763 else
6764 {
6765 set_iterator_to_next (it, 0);
6766 }
6767 goto get_next;
6768 }
6769
6770 if (! NILP (lookup_glyphless_char_display (c, it)))
6771 {
6772 if (it->what == IT_GLYPHLESS)
6773 goto done;
6774 /* Don't display this character. */
6775 set_iterator_to_next (it, 0);
6776 goto get_next;
6777 }
6778
6779 /* If `nobreak-char-display' is non-nil, we display
6780 non-ASCII spaces and hyphens specially. */
6781 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6782 {
6783 if (c == 0xA0)
6784 nonascii_space_p = 1;
6785 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6786 nonascii_hyphen_p = 1;
6787 }
6788
6789 /* Translate control characters into `\003' or `^C' form.
6790 Control characters coming from a display table entry are
6791 currently not translated because we use IT->dpvec to hold
6792 the translation. This could easily be changed but I
6793 don't believe that it is worth doing.
6794
6795 The characters handled by `nobreak-char-display' must be
6796 translated too.
6797
6798 Non-printable characters and raw-byte characters are also
6799 translated to octal form. */
6800 if (((c < ' ' || c == 127) /* ASCII control chars */
6801 ? (it->area != TEXT_AREA
6802 /* In mode line, treat \n, \t like other crl chars. */
6803 || (c != '\t'
6804 && it->glyph_row
6805 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6806 || (c != '\n' && c != '\t'))
6807 : (nonascii_space_p
6808 || nonascii_hyphen_p
6809 || CHAR_BYTE8_P (c)
6810 || ! CHAR_PRINTABLE_P (c))))
6811 {
6812 /* C is a control character, non-ASCII space/hyphen,
6813 raw-byte, or a non-printable character which must be
6814 displayed either as '\003' or as `^C' where the '\\'
6815 and '^' can be defined in the display table. Fill
6816 IT->ctl_chars with glyphs for what we have to
6817 display. Then, set IT->dpvec to these glyphs. */
6818 Lisp_Object gc;
6819 int ctl_len;
6820 int face_id;
6821 int lface_id = 0;
6822 int escape_glyph;
6823
6824 /* Handle control characters with ^. */
6825
6826 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6827 {
6828 int g;
6829
6830 g = '^'; /* default glyph for Control */
6831 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6832 if (it->dp
6833 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6834 {
6835 g = GLYPH_CODE_CHAR (gc);
6836 lface_id = GLYPH_CODE_FACE (gc);
6837 }
6838 if (lface_id)
6839 {
6840 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6841 }
6842 else if (it->f == last_escape_glyph_frame
6843 && it->face_id == last_escape_glyph_face_id)
6844 {
6845 face_id = last_escape_glyph_merged_face_id;
6846 }
6847 else
6848 {
6849 /* Merge the escape-glyph face into the current face. */
6850 face_id = merge_faces (it->f, Qescape_glyph, 0,
6851 it->face_id);
6852 last_escape_glyph_frame = it->f;
6853 last_escape_glyph_face_id = it->face_id;
6854 last_escape_glyph_merged_face_id = face_id;
6855 }
6856
6857 XSETINT (it->ctl_chars[0], g);
6858 XSETINT (it->ctl_chars[1], c ^ 0100);
6859 ctl_len = 2;
6860 goto display_control;
6861 }
6862
6863 /* Handle non-ascii space in the mode where it only gets
6864 highlighting. */
6865
6866 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6867 {
6868 /* Merge `nobreak-space' into the current face. */
6869 face_id = merge_faces (it->f, Qnobreak_space, 0,
6870 it->face_id);
6871 XSETINT (it->ctl_chars[0], ' ');
6872 ctl_len = 1;
6873 goto display_control;
6874 }
6875
6876 /* Handle sequences that start with the "escape glyph". */
6877
6878 /* the default escape glyph is \. */
6879 escape_glyph = '\\';
6880
6881 if (it->dp
6882 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6883 {
6884 escape_glyph = GLYPH_CODE_CHAR (gc);
6885 lface_id = GLYPH_CODE_FACE (gc);
6886 }
6887 if (lface_id)
6888 {
6889 /* The display table specified a face.
6890 Merge it into face_id and also into escape_glyph. */
6891 face_id = merge_faces (it->f, Qt, lface_id,
6892 it->face_id);
6893 }
6894 else if (it->f == last_escape_glyph_frame
6895 && it->face_id == last_escape_glyph_face_id)
6896 {
6897 face_id = last_escape_glyph_merged_face_id;
6898 }
6899 else
6900 {
6901 /* Merge the escape-glyph face into the current face. */
6902 face_id = merge_faces (it->f, Qescape_glyph, 0,
6903 it->face_id);
6904 last_escape_glyph_frame = it->f;
6905 last_escape_glyph_face_id = it->face_id;
6906 last_escape_glyph_merged_face_id = face_id;
6907 }
6908
6909 /* Draw non-ASCII hyphen with just highlighting: */
6910
6911 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6912 {
6913 XSETINT (it->ctl_chars[0], '-');
6914 ctl_len = 1;
6915 goto display_control;
6916 }
6917
6918 /* Draw non-ASCII space/hyphen with escape glyph: */
6919
6920 if (nonascii_space_p || nonascii_hyphen_p)
6921 {
6922 XSETINT (it->ctl_chars[0], escape_glyph);
6923 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6924 ctl_len = 2;
6925 goto display_control;
6926 }
6927
6928 {
6929 char str[10];
6930 int len, i;
6931
6932 if (CHAR_BYTE8_P (c))
6933 /* Display \200 instead of \17777600. */
6934 c = CHAR_TO_BYTE8 (c);
6935 len = sprintf (str, "%03o", c);
6936
6937 XSETINT (it->ctl_chars[0], escape_glyph);
6938 for (i = 0; i < len; i++)
6939 XSETINT (it->ctl_chars[i + 1], str[i]);
6940 ctl_len = len + 1;
6941 }
6942
6943 display_control:
6944 /* Set up IT->dpvec and return first character from it. */
6945 it->dpvec_char_len = it->len;
6946 it->dpvec = it->ctl_chars;
6947 it->dpend = it->dpvec + ctl_len;
6948 it->current.dpvec_index = 0;
6949 it->dpvec_face_id = face_id;
6950 it->saved_face_id = it->face_id;
6951 it->method = GET_FROM_DISPLAY_VECTOR;
6952 it->ellipsis_p = 0;
6953 goto get_next;
6954 }
6955 it->char_to_display = c;
6956 }
6957 else if (success_p)
6958 {
6959 it->char_to_display = it->c;
6960 }
6961 }
6962
6963 /* Adjust face id for a multibyte character. There are no multibyte
6964 character in unibyte text. */
6965 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6966 && it->multibyte_p
6967 && success_p
6968 && FRAME_WINDOW_P (it->f))
6969 {
6970 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6971
6972 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6973 {
6974 /* Automatic composition with glyph-string. */
6975 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6976
6977 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6978 }
6979 else
6980 {
6981 ptrdiff_t pos = (it->s ? -1
6982 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6983 : IT_CHARPOS (*it));
6984 int c;
6985
6986 if (it->what == IT_CHARACTER)
6987 c = it->char_to_display;
6988 else
6989 {
6990 struct composition *cmp = composition_table[it->cmp_it.id];
6991 int i;
6992
6993 c = ' ';
6994 for (i = 0; i < cmp->glyph_len; i++)
6995 /* TAB in a composition means display glyphs with
6996 padding space on the left or right. */
6997 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6998 break;
6999 }
7000 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7001 }
7002 }
7003
7004 done:
7005 /* Is this character the last one of a run of characters with
7006 box? If yes, set IT->end_of_box_run_p to 1. */
7007 if (it->face_box_p
7008 && it->s == NULL)
7009 {
7010 if (it->method == GET_FROM_STRING && it->sp)
7011 {
7012 int face_id = underlying_face_id (it);
7013 struct face *face = FACE_FROM_ID (it->f, face_id);
7014
7015 if (face)
7016 {
7017 if (face->box == FACE_NO_BOX)
7018 {
7019 /* If the box comes from face properties in a
7020 display string, check faces in that string. */
7021 int string_face_id = face_after_it_pos (it);
7022 it->end_of_box_run_p
7023 = (FACE_FROM_ID (it->f, string_face_id)->box
7024 == FACE_NO_BOX);
7025 }
7026 /* Otherwise, the box comes from the underlying face.
7027 If this is the last string character displayed, check
7028 the next buffer location. */
7029 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7030 && (it->current.overlay_string_index
7031 == it->n_overlay_strings - 1))
7032 {
7033 ptrdiff_t ignore;
7034 int next_face_id;
7035 struct text_pos pos = it->current.pos;
7036 INC_TEXT_POS (pos, it->multibyte_p);
7037
7038 next_face_id = face_at_buffer_position
7039 (it->w, CHARPOS (pos), it->region_beg_charpos,
7040 it->region_end_charpos, &ignore,
7041 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
7042 -1);
7043 it->end_of_box_run_p
7044 = (FACE_FROM_ID (it->f, next_face_id)->box
7045 == FACE_NO_BOX);
7046 }
7047 }
7048 }
7049 else
7050 {
7051 int face_id = face_after_it_pos (it);
7052 it->end_of_box_run_p
7053 = (face_id != it->face_id
7054 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7055 }
7056 }
7057 /* If we reached the end of the object we've been iterating (e.g., a
7058 display string or an overlay string), and there's something on
7059 IT->stack, proceed with what's on the stack. It doesn't make
7060 sense to return zero if there's unprocessed stuff on the stack,
7061 because otherwise that stuff will never be displayed. */
7062 if (!success_p && it->sp > 0)
7063 {
7064 set_iterator_to_next (it, 0);
7065 success_p = get_next_display_element (it);
7066 }
7067
7068 /* Value is 0 if end of buffer or string reached. */
7069 return success_p;
7070 }
7071
7072
7073 /* Move IT to the next display element.
7074
7075 RESEAT_P non-zero means if called on a newline in buffer text,
7076 skip to the next visible line start.
7077
7078 Functions get_next_display_element and set_iterator_to_next are
7079 separate because I find this arrangement easier to handle than a
7080 get_next_display_element function that also increments IT's
7081 position. The way it is we can first look at an iterator's current
7082 display element, decide whether it fits on a line, and if it does,
7083 increment the iterator position. The other way around we probably
7084 would either need a flag indicating whether the iterator has to be
7085 incremented the next time, or we would have to implement a
7086 decrement position function which would not be easy to write. */
7087
7088 void
7089 set_iterator_to_next (struct it *it, int reseat_p)
7090 {
7091 /* Reset flags indicating start and end of a sequence of characters
7092 with box. Reset them at the start of this function because
7093 moving the iterator to a new position might set them. */
7094 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7095
7096 switch (it->method)
7097 {
7098 case GET_FROM_BUFFER:
7099 /* The current display element of IT is a character from
7100 current_buffer. Advance in the buffer, and maybe skip over
7101 invisible lines that are so because of selective display. */
7102 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7103 reseat_at_next_visible_line_start (it, 0);
7104 else if (it->cmp_it.id >= 0)
7105 {
7106 /* We are currently getting glyphs from a composition. */
7107 int i;
7108
7109 if (! it->bidi_p)
7110 {
7111 IT_CHARPOS (*it) += it->cmp_it.nchars;
7112 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7113 if (it->cmp_it.to < it->cmp_it.nglyphs)
7114 {
7115 it->cmp_it.from = it->cmp_it.to;
7116 }
7117 else
7118 {
7119 it->cmp_it.id = -1;
7120 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7121 IT_BYTEPOS (*it),
7122 it->end_charpos, Qnil);
7123 }
7124 }
7125 else if (! it->cmp_it.reversed_p)
7126 {
7127 /* Composition created while scanning forward. */
7128 /* Update IT's char/byte positions to point to the first
7129 character of the next grapheme cluster, or to the
7130 character visually after the current composition. */
7131 for (i = 0; i < it->cmp_it.nchars; i++)
7132 bidi_move_to_visually_next (&it->bidi_it);
7133 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7134 IT_CHARPOS (*it) = it->bidi_it.charpos;
7135
7136 if (it->cmp_it.to < it->cmp_it.nglyphs)
7137 {
7138 /* Proceed to the next grapheme cluster. */
7139 it->cmp_it.from = it->cmp_it.to;
7140 }
7141 else
7142 {
7143 /* No more grapheme clusters in this composition.
7144 Find the next stop position. */
7145 ptrdiff_t stop = it->end_charpos;
7146 if (it->bidi_it.scan_dir < 0)
7147 /* Now we are scanning backward and don't know
7148 where to stop. */
7149 stop = -1;
7150 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7151 IT_BYTEPOS (*it), stop, Qnil);
7152 }
7153 }
7154 else
7155 {
7156 /* Composition created while scanning backward. */
7157 /* Update IT's char/byte positions to point to the last
7158 character of the previous grapheme cluster, or the
7159 character visually after the current composition. */
7160 for (i = 0; i < it->cmp_it.nchars; i++)
7161 bidi_move_to_visually_next (&it->bidi_it);
7162 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7163 IT_CHARPOS (*it) = it->bidi_it.charpos;
7164 if (it->cmp_it.from > 0)
7165 {
7166 /* Proceed to the previous grapheme cluster. */
7167 it->cmp_it.to = it->cmp_it.from;
7168 }
7169 else
7170 {
7171 /* No more grapheme clusters in this composition.
7172 Find the next stop position. */
7173 ptrdiff_t stop = it->end_charpos;
7174 if (it->bidi_it.scan_dir < 0)
7175 /* Now we are scanning backward and don't know
7176 where to stop. */
7177 stop = -1;
7178 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7179 IT_BYTEPOS (*it), stop, Qnil);
7180 }
7181 }
7182 }
7183 else
7184 {
7185 eassert (it->len != 0);
7186
7187 if (!it->bidi_p)
7188 {
7189 IT_BYTEPOS (*it) += it->len;
7190 IT_CHARPOS (*it) += 1;
7191 }
7192 else
7193 {
7194 int prev_scan_dir = it->bidi_it.scan_dir;
7195 /* If this is a new paragraph, determine its base
7196 direction (a.k.a. its base embedding level). */
7197 if (it->bidi_it.new_paragraph)
7198 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7199 bidi_move_to_visually_next (&it->bidi_it);
7200 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7201 IT_CHARPOS (*it) = it->bidi_it.charpos;
7202 if (prev_scan_dir != it->bidi_it.scan_dir)
7203 {
7204 /* As the scan direction was changed, we must
7205 re-compute the stop position for composition. */
7206 ptrdiff_t stop = it->end_charpos;
7207 if (it->bidi_it.scan_dir < 0)
7208 stop = -1;
7209 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7210 IT_BYTEPOS (*it), stop, Qnil);
7211 }
7212 }
7213 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7214 }
7215 break;
7216
7217 case GET_FROM_C_STRING:
7218 /* Current display element of IT is from a C string. */
7219 if (!it->bidi_p
7220 /* If the string position is beyond string's end, it means
7221 next_element_from_c_string is padding the string with
7222 blanks, in which case we bypass the bidi iterator,
7223 because it cannot deal with such virtual characters. */
7224 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7225 {
7226 IT_BYTEPOS (*it) += it->len;
7227 IT_CHARPOS (*it) += 1;
7228 }
7229 else
7230 {
7231 bidi_move_to_visually_next (&it->bidi_it);
7232 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7233 IT_CHARPOS (*it) = it->bidi_it.charpos;
7234 }
7235 break;
7236
7237 case GET_FROM_DISPLAY_VECTOR:
7238 /* Current display element of IT is from a display table entry.
7239 Advance in the display table definition. Reset it to null if
7240 end reached, and continue with characters from buffers/
7241 strings. */
7242 ++it->current.dpvec_index;
7243
7244 /* Restore face of the iterator to what they were before the
7245 display vector entry (these entries may contain faces). */
7246 it->face_id = it->saved_face_id;
7247
7248 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7249 {
7250 int recheck_faces = it->ellipsis_p;
7251
7252 if (it->s)
7253 it->method = GET_FROM_C_STRING;
7254 else if (STRINGP (it->string))
7255 it->method = GET_FROM_STRING;
7256 else
7257 {
7258 it->method = GET_FROM_BUFFER;
7259 it->object = it->w->contents;
7260 }
7261
7262 it->dpvec = NULL;
7263 it->current.dpvec_index = -1;
7264
7265 /* Skip over characters which were displayed via IT->dpvec. */
7266 if (it->dpvec_char_len < 0)
7267 reseat_at_next_visible_line_start (it, 1);
7268 else if (it->dpvec_char_len > 0)
7269 {
7270 if (it->method == GET_FROM_STRING
7271 && it->current.overlay_string_index >= 0
7272 && it->n_overlay_strings > 0)
7273 it->ignore_overlay_strings_at_pos_p = 1;
7274 it->len = it->dpvec_char_len;
7275 set_iterator_to_next (it, reseat_p);
7276 }
7277
7278 /* Maybe recheck faces after display vector */
7279 if (recheck_faces)
7280 it->stop_charpos = IT_CHARPOS (*it);
7281 }
7282 break;
7283
7284 case GET_FROM_STRING:
7285 /* Current display element is a character from a Lisp string. */
7286 eassert (it->s == NULL && STRINGP (it->string));
7287 /* Don't advance past string end. These conditions are true
7288 when set_iterator_to_next is called at the end of
7289 get_next_display_element, in which case the Lisp string is
7290 already exhausted, and all we want is pop the iterator
7291 stack. */
7292 if (it->current.overlay_string_index >= 0)
7293 {
7294 /* This is an overlay string, so there's no padding with
7295 spaces, and the number of characters in the string is
7296 where the string ends. */
7297 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7298 goto consider_string_end;
7299 }
7300 else
7301 {
7302 /* Not an overlay string. There could be padding, so test
7303 against it->end_charpos . */
7304 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7305 goto consider_string_end;
7306 }
7307 if (it->cmp_it.id >= 0)
7308 {
7309 int i;
7310
7311 if (! it->bidi_p)
7312 {
7313 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7314 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7315 if (it->cmp_it.to < it->cmp_it.nglyphs)
7316 it->cmp_it.from = it->cmp_it.to;
7317 else
7318 {
7319 it->cmp_it.id = -1;
7320 composition_compute_stop_pos (&it->cmp_it,
7321 IT_STRING_CHARPOS (*it),
7322 IT_STRING_BYTEPOS (*it),
7323 it->end_charpos, it->string);
7324 }
7325 }
7326 else if (! it->cmp_it.reversed_p)
7327 {
7328 for (i = 0; i < it->cmp_it.nchars; i++)
7329 bidi_move_to_visually_next (&it->bidi_it);
7330 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7331 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7332
7333 if (it->cmp_it.to < it->cmp_it.nglyphs)
7334 it->cmp_it.from = it->cmp_it.to;
7335 else
7336 {
7337 ptrdiff_t stop = it->end_charpos;
7338 if (it->bidi_it.scan_dir < 0)
7339 stop = -1;
7340 composition_compute_stop_pos (&it->cmp_it,
7341 IT_STRING_CHARPOS (*it),
7342 IT_STRING_BYTEPOS (*it), stop,
7343 it->string);
7344 }
7345 }
7346 else
7347 {
7348 for (i = 0; i < it->cmp_it.nchars; i++)
7349 bidi_move_to_visually_next (&it->bidi_it);
7350 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7351 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7352 if (it->cmp_it.from > 0)
7353 it->cmp_it.to = it->cmp_it.from;
7354 else
7355 {
7356 ptrdiff_t stop = it->end_charpos;
7357 if (it->bidi_it.scan_dir < 0)
7358 stop = -1;
7359 composition_compute_stop_pos (&it->cmp_it,
7360 IT_STRING_CHARPOS (*it),
7361 IT_STRING_BYTEPOS (*it), stop,
7362 it->string);
7363 }
7364 }
7365 }
7366 else
7367 {
7368 if (!it->bidi_p
7369 /* If the string position is beyond string's end, it
7370 means next_element_from_string is padding the string
7371 with blanks, in which case we bypass the bidi
7372 iterator, because it cannot deal with such virtual
7373 characters. */
7374 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7375 {
7376 IT_STRING_BYTEPOS (*it) += it->len;
7377 IT_STRING_CHARPOS (*it) += 1;
7378 }
7379 else
7380 {
7381 int prev_scan_dir = it->bidi_it.scan_dir;
7382
7383 bidi_move_to_visually_next (&it->bidi_it);
7384 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7385 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7386 if (prev_scan_dir != it->bidi_it.scan_dir)
7387 {
7388 ptrdiff_t stop = it->end_charpos;
7389
7390 if (it->bidi_it.scan_dir < 0)
7391 stop = -1;
7392 composition_compute_stop_pos (&it->cmp_it,
7393 IT_STRING_CHARPOS (*it),
7394 IT_STRING_BYTEPOS (*it), stop,
7395 it->string);
7396 }
7397 }
7398 }
7399
7400 consider_string_end:
7401
7402 if (it->current.overlay_string_index >= 0)
7403 {
7404 /* IT->string is an overlay string. Advance to the
7405 next, if there is one. */
7406 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7407 {
7408 it->ellipsis_p = 0;
7409 next_overlay_string (it);
7410 if (it->ellipsis_p)
7411 setup_for_ellipsis (it, 0);
7412 }
7413 }
7414 else
7415 {
7416 /* IT->string is not an overlay string. If we reached
7417 its end, and there is something on IT->stack, proceed
7418 with what is on the stack. This can be either another
7419 string, this time an overlay string, or a buffer. */
7420 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7421 && it->sp > 0)
7422 {
7423 pop_it (it);
7424 if (it->method == GET_FROM_STRING)
7425 goto consider_string_end;
7426 }
7427 }
7428 break;
7429
7430 case GET_FROM_IMAGE:
7431 case GET_FROM_STRETCH:
7432 /* The position etc with which we have to proceed are on
7433 the stack. The position may be at the end of a string,
7434 if the `display' property takes up the whole string. */
7435 eassert (it->sp > 0);
7436 pop_it (it);
7437 if (it->method == GET_FROM_STRING)
7438 goto consider_string_end;
7439 break;
7440
7441 default:
7442 /* There are no other methods defined, so this should be a bug. */
7443 emacs_abort ();
7444 }
7445
7446 eassert (it->method != GET_FROM_STRING
7447 || (STRINGP (it->string)
7448 && IT_STRING_CHARPOS (*it) >= 0));
7449 }
7450
7451 /* Load IT's display element fields with information about the next
7452 display element which comes from a display table entry or from the
7453 result of translating a control character to one of the forms `^C'
7454 or `\003'.
7455
7456 IT->dpvec holds the glyphs to return as characters.
7457 IT->saved_face_id holds the face id before the display vector--it
7458 is restored into IT->face_id in set_iterator_to_next. */
7459
7460 static int
7461 next_element_from_display_vector (struct it *it)
7462 {
7463 Lisp_Object gc;
7464
7465 /* Precondition. */
7466 eassert (it->dpvec && it->current.dpvec_index >= 0);
7467
7468 it->face_id = it->saved_face_id;
7469
7470 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7471 That seemed totally bogus - so I changed it... */
7472 gc = it->dpvec[it->current.dpvec_index];
7473
7474 if (GLYPH_CODE_P (gc))
7475 {
7476 it->c = GLYPH_CODE_CHAR (gc);
7477 it->len = CHAR_BYTES (it->c);
7478
7479 /* The entry may contain a face id to use. Such a face id is
7480 the id of a Lisp face, not a realized face. A face id of
7481 zero means no face is specified. */
7482 if (it->dpvec_face_id >= 0)
7483 it->face_id = it->dpvec_face_id;
7484 else
7485 {
7486 int lface_id = GLYPH_CODE_FACE (gc);
7487 if (lface_id > 0)
7488 it->face_id = merge_faces (it->f, Qt, lface_id,
7489 it->saved_face_id);
7490 }
7491 }
7492 else
7493 /* Display table entry is invalid. Return a space. */
7494 it->c = ' ', it->len = 1;
7495
7496 /* Don't change position and object of the iterator here. They are
7497 still the values of the character that had this display table
7498 entry or was translated, and that's what we want. */
7499 it->what = IT_CHARACTER;
7500 return 1;
7501 }
7502
7503 /* Get the first element of string/buffer in the visual order, after
7504 being reseated to a new position in a string or a buffer. */
7505 static void
7506 get_visually_first_element (struct it *it)
7507 {
7508 int string_p = STRINGP (it->string) || it->s;
7509 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7510 ptrdiff_t bob = (string_p ? 0 : BEGV);
7511
7512 if (STRINGP (it->string))
7513 {
7514 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7515 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7516 }
7517 else
7518 {
7519 it->bidi_it.charpos = IT_CHARPOS (*it);
7520 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7521 }
7522
7523 if (it->bidi_it.charpos == eob)
7524 {
7525 /* Nothing to do, but reset the FIRST_ELT flag, like
7526 bidi_paragraph_init does, because we are not going to
7527 call it. */
7528 it->bidi_it.first_elt = 0;
7529 }
7530 else if (it->bidi_it.charpos == bob
7531 || (!string_p
7532 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7533 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7534 {
7535 /* If we are at the beginning of a line/string, we can produce
7536 the next element right away. */
7537 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7538 bidi_move_to_visually_next (&it->bidi_it);
7539 }
7540 else
7541 {
7542 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7543
7544 /* We need to prime the bidi iterator starting at the line's or
7545 string's beginning, before we will be able to produce the
7546 next element. */
7547 if (string_p)
7548 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7549 else
7550 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7551 IT_BYTEPOS (*it), -1,
7552 &it->bidi_it.bytepos);
7553 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7554 do
7555 {
7556 /* Now return to buffer/string position where we were asked
7557 to get the next display element, and produce that. */
7558 bidi_move_to_visually_next (&it->bidi_it);
7559 }
7560 while (it->bidi_it.bytepos != orig_bytepos
7561 && it->bidi_it.charpos < eob);
7562 }
7563
7564 /* Adjust IT's position information to where we ended up. */
7565 if (STRINGP (it->string))
7566 {
7567 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7568 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7569 }
7570 else
7571 {
7572 IT_CHARPOS (*it) = it->bidi_it.charpos;
7573 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7574 }
7575
7576 if (STRINGP (it->string) || !it->s)
7577 {
7578 ptrdiff_t stop, charpos, bytepos;
7579
7580 if (STRINGP (it->string))
7581 {
7582 eassert (!it->s);
7583 stop = SCHARS (it->string);
7584 if (stop > it->end_charpos)
7585 stop = it->end_charpos;
7586 charpos = IT_STRING_CHARPOS (*it);
7587 bytepos = IT_STRING_BYTEPOS (*it);
7588 }
7589 else
7590 {
7591 stop = it->end_charpos;
7592 charpos = IT_CHARPOS (*it);
7593 bytepos = IT_BYTEPOS (*it);
7594 }
7595 if (it->bidi_it.scan_dir < 0)
7596 stop = -1;
7597 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7598 it->string);
7599 }
7600 }
7601
7602 /* Load IT with the next display element from Lisp string IT->string.
7603 IT->current.string_pos is the current position within the string.
7604 If IT->current.overlay_string_index >= 0, the Lisp string is an
7605 overlay string. */
7606
7607 static int
7608 next_element_from_string (struct it *it)
7609 {
7610 struct text_pos position;
7611
7612 eassert (STRINGP (it->string));
7613 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7614 eassert (IT_STRING_CHARPOS (*it) >= 0);
7615 position = it->current.string_pos;
7616
7617 /* With bidi reordering, the character to display might not be the
7618 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7619 that we were reseat()ed to a new string, whose paragraph
7620 direction is not known. */
7621 if (it->bidi_p && it->bidi_it.first_elt)
7622 {
7623 get_visually_first_element (it);
7624 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7625 }
7626
7627 /* Time to check for invisible text? */
7628 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7629 {
7630 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7631 {
7632 if (!(!it->bidi_p
7633 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7634 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7635 {
7636 /* With bidi non-linear iteration, we could find
7637 ourselves far beyond the last computed stop_charpos,
7638 with several other stop positions in between that we
7639 missed. Scan them all now, in buffer's logical
7640 order, until we find and handle the last stop_charpos
7641 that precedes our current position. */
7642 handle_stop_backwards (it, it->stop_charpos);
7643 return GET_NEXT_DISPLAY_ELEMENT (it);
7644 }
7645 else
7646 {
7647 if (it->bidi_p)
7648 {
7649 /* Take note of the stop position we just moved
7650 across, for when we will move back across it. */
7651 it->prev_stop = it->stop_charpos;
7652 /* If we are at base paragraph embedding level, take
7653 note of the last stop position seen at this
7654 level. */
7655 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7656 it->base_level_stop = it->stop_charpos;
7657 }
7658 handle_stop (it);
7659
7660 /* Since a handler may have changed IT->method, we must
7661 recurse here. */
7662 return GET_NEXT_DISPLAY_ELEMENT (it);
7663 }
7664 }
7665 else if (it->bidi_p
7666 /* If we are before prev_stop, we may have overstepped
7667 on our way backwards a stop_pos, and if so, we need
7668 to handle that stop_pos. */
7669 && IT_STRING_CHARPOS (*it) < it->prev_stop
7670 /* We can sometimes back up for reasons that have nothing
7671 to do with bidi reordering. E.g., compositions. The
7672 code below is only needed when we are above the base
7673 embedding level, so test for that explicitly. */
7674 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7675 {
7676 /* If we lost track of base_level_stop, we have no better
7677 place for handle_stop_backwards to start from than string
7678 beginning. This happens, e.g., when we were reseated to
7679 the previous screenful of text by vertical-motion. */
7680 if (it->base_level_stop <= 0
7681 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7682 it->base_level_stop = 0;
7683 handle_stop_backwards (it, it->base_level_stop);
7684 return GET_NEXT_DISPLAY_ELEMENT (it);
7685 }
7686 }
7687
7688 if (it->current.overlay_string_index >= 0)
7689 {
7690 /* Get the next character from an overlay string. In overlay
7691 strings, there is no field width or padding with spaces to
7692 do. */
7693 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7694 {
7695 it->what = IT_EOB;
7696 return 0;
7697 }
7698 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7699 IT_STRING_BYTEPOS (*it),
7700 it->bidi_it.scan_dir < 0
7701 ? -1
7702 : SCHARS (it->string))
7703 && next_element_from_composition (it))
7704 {
7705 return 1;
7706 }
7707 else if (STRING_MULTIBYTE (it->string))
7708 {
7709 const unsigned char *s = (SDATA (it->string)
7710 + IT_STRING_BYTEPOS (*it));
7711 it->c = string_char_and_length (s, &it->len);
7712 }
7713 else
7714 {
7715 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7716 it->len = 1;
7717 }
7718 }
7719 else
7720 {
7721 /* Get the next character from a Lisp string that is not an
7722 overlay string. Such strings come from the mode line, for
7723 example. We may have to pad with spaces, or truncate the
7724 string. See also next_element_from_c_string. */
7725 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7726 {
7727 it->what = IT_EOB;
7728 return 0;
7729 }
7730 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7731 {
7732 /* Pad with spaces. */
7733 it->c = ' ', it->len = 1;
7734 CHARPOS (position) = BYTEPOS (position) = -1;
7735 }
7736 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7737 IT_STRING_BYTEPOS (*it),
7738 it->bidi_it.scan_dir < 0
7739 ? -1
7740 : it->string_nchars)
7741 && next_element_from_composition (it))
7742 {
7743 return 1;
7744 }
7745 else if (STRING_MULTIBYTE (it->string))
7746 {
7747 const unsigned char *s = (SDATA (it->string)
7748 + IT_STRING_BYTEPOS (*it));
7749 it->c = string_char_and_length (s, &it->len);
7750 }
7751 else
7752 {
7753 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7754 it->len = 1;
7755 }
7756 }
7757
7758 /* Record what we have and where it came from. */
7759 it->what = IT_CHARACTER;
7760 it->object = it->string;
7761 it->position = position;
7762 return 1;
7763 }
7764
7765
7766 /* Load IT with next display element from C string IT->s.
7767 IT->string_nchars is the maximum number of characters to return
7768 from the string. IT->end_charpos may be greater than
7769 IT->string_nchars when this function is called, in which case we
7770 may have to return padding spaces. Value is zero if end of string
7771 reached, including padding spaces. */
7772
7773 static int
7774 next_element_from_c_string (struct it *it)
7775 {
7776 int success_p = 1;
7777
7778 eassert (it->s);
7779 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7780 it->what = IT_CHARACTER;
7781 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7782 it->object = Qnil;
7783
7784 /* With bidi reordering, the character to display might not be the
7785 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7786 we were reseated to a new string, whose paragraph direction is
7787 not known. */
7788 if (it->bidi_p && it->bidi_it.first_elt)
7789 get_visually_first_element (it);
7790
7791 /* IT's position can be greater than IT->string_nchars in case a
7792 field width or precision has been specified when the iterator was
7793 initialized. */
7794 if (IT_CHARPOS (*it) >= it->end_charpos)
7795 {
7796 /* End of the game. */
7797 it->what = IT_EOB;
7798 success_p = 0;
7799 }
7800 else if (IT_CHARPOS (*it) >= it->string_nchars)
7801 {
7802 /* Pad with spaces. */
7803 it->c = ' ', it->len = 1;
7804 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7805 }
7806 else if (it->multibyte_p)
7807 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7808 else
7809 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7810
7811 return success_p;
7812 }
7813
7814
7815 /* Set up IT to return characters from an ellipsis, if appropriate.
7816 The definition of the ellipsis glyphs may come from a display table
7817 entry. This function fills IT with the first glyph from the
7818 ellipsis if an ellipsis is to be displayed. */
7819
7820 static int
7821 next_element_from_ellipsis (struct it *it)
7822 {
7823 if (it->selective_display_ellipsis_p)
7824 setup_for_ellipsis (it, it->len);
7825 else
7826 {
7827 /* The face at the current position may be different from the
7828 face we find after the invisible text. Remember what it
7829 was in IT->saved_face_id, and signal that it's there by
7830 setting face_before_selective_p. */
7831 it->saved_face_id = it->face_id;
7832 it->method = GET_FROM_BUFFER;
7833 it->object = it->w->contents;
7834 reseat_at_next_visible_line_start (it, 1);
7835 it->face_before_selective_p = 1;
7836 }
7837
7838 return GET_NEXT_DISPLAY_ELEMENT (it);
7839 }
7840
7841
7842 /* Deliver an image display element. The iterator IT is already
7843 filled with image information (done in handle_display_prop). Value
7844 is always 1. */
7845
7846
7847 static int
7848 next_element_from_image (struct it *it)
7849 {
7850 it->what = IT_IMAGE;
7851 it->ignore_overlay_strings_at_pos_p = 0;
7852 return 1;
7853 }
7854
7855
7856 /* Fill iterator IT with next display element from a stretch glyph
7857 property. IT->object is the value of the text property. Value is
7858 always 1. */
7859
7860 static int
7861 next_element_from_stretch (struct it *it)
7862 {
7863 it->what = IT_STRETCH;
7864 return 1;
7865 }
7866
7867 /* Scan backwards from IT's current position until we find a stop
7868 position, or until BEGV. This is called when we find ourself
7869 before both the last known prev_stop and base_level_stop while
7870 reordering bidirectional text. */
7871
7872 static void
7873 compute_stop_pos_backwards (struct it *it)
7874 {
7875 const int SCAN_BACK_LIMIT = 1000;
7876 struct text_pos pos;
7877 struct display_pos save_current = it->current;
7878 struct text_pos save_position = it->position;
7879 ptrdiff_t charpos = IT_CHARPOS (*it);
7880 ptrdiff_t where_we_are = charpos;
7881 ptrdiff_t save_stop_pos = it->stop_charpos;
7882 ptrdiff_t save_end_pos = it->end_charpos;
7883
7884 eassert (NILP (it->string) && !it->s);
7885 eassert (it->bidi_p);
7886 it->bidi_p = 0;
7887 do
7888 {
7889 it->end_charpos = min (charpos + 1, ZV);
7890 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7891 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7892 reseat_1 (it, pos, 0);
7893 compute_stop_pos (it);
7894 /* We must advance forward, right? */
7895 if (it->stop_charpos <= charpos)
7896 emacs_abort ();
7897 }
7898 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7899
7900 if (it->stop_charpos <= where_we_are)
7901 it->prev_stop = it->stop_charpos;
7902 else
7903 it->prev_stop = BEGV;
7904 it->bidi_p = 1;
7905 it->current = save_current;
7906 it->position = save_position;
7907 it->stop_charpos = save_stop_pos;
7908 it->end_charpos = save_end_pos;
7909 }
7910
7911 /* Scan forward from CHARPOS in the current buffer/string, until we
7912 find a stop position > current IT's position. Then handle the stop
7913 position before that. This is called when we bump into a stop
7914 position while reordering bidirectional text. CHARPOS should be
7915 the last previously processed stop_pos (or BEGV/0, if none were
7916 processed yet) whose position is less that IT's current
7917 position. */
7918
7919 static void
7920 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7921 {
7922 int bufp = !STRINGP (it->string);
7923 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7924 struct display_pos save_current = it->current;
7925 struct text_pos save_position = it->position;
7926 struct text_pos pos1;
7927 ptrdiff_t next_stop;
7928
7929 /* Scan in strict logical order. */
7930 eassert (it->bidi_p);
7931 it->bidi_p = 0;
7932 do
7933 {
7934 it->prev_stop = charpos;
7935 if (bufp)
7936 {
7937 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7938 reseat_1 (it, pos1, 0);
7939 }
7940 else
7941 it->current.string_pos = string_pos (charpos, it->string);
7942 compute_stop_pos (it);
7943 /* We must advance forward, right? */
7944 if (it->stop_charpos <= it->prev_stop)
7945 emacs_abort ();
7946 charpos = it->stop_charpos;
7947 }
7948 while (charpos <= where_we_are);
7949
7950 it->bidi_p = 1;
7951 it->current = save_current;
7952 it->position = save_position;
7953 next_stop = it->stop_charpos;
7954 it->stop_charpos = it->prev_stop;
7955 handle_stop (it);
7956 it->stop_charpos = next_stop;
7957 }
7958
7959 /* Load IT with the next display element from current_buffer. Value
7960 is zero if end of buffer reached. IT->stop_charpos is the next
7961 position at which to stop and check for text properties or buffer
7962 end. */
7963
7964 static int
7965 next_element_from_buffer (struct it *it)
7966 {
7967 int success_p = 1;
7968
7969 eassert (IT_CHARPOS (*it) >= BEGV);
7970 eassert (NILP (it->string) && !it->s);
7971 eassert (!it->bidi_p
7972 || (EQ (it->bidi_it.string.lstring, Qnil)
7973 && it->bidi_it.string.s == NULL));
7974
7975 /* With bidi reordering, the character to display might not be the
7976 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7977 we were reseat()ed to a new buffer position, which is potentially
7978 a different paragraph. */
7979 if (it->bidi_p && it->bidi_it.first_elt)
7980 {
7981 get_visually_first_element (it);
7982 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7983 }
7984
7985 if (IT_CHARPOS (*it) >= it->stop_charpos)
7986 {
7987 if (IT_CHARPOS (*it) >= it->end_charpos)
7988 {
7989 int overlay_strings_follow_p;
7990
7991 /* End of the game, except when overlay strings follow that
7992 haven't been returned yet. */
7993 if (it->overlay_strings_at_end_processed_p)
7994 overlay_strings_follow_p = 0;
7995 else
7996 {
7997 it->overlay_strings_at_end_processed_p = 1;
7998 overlay_strings_follow_p = get_overlay_strings (it, 0);
7999 }
8000
8001 if (overlay_strings_follow_p)
8002 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8003 else
8004 {
8005 it->what = IT_EOB;
8006 it->position = it->current.pos;
8007 success_p = 0;
8008 }
8009 }
8010 else if (!(!it->bidi_p
8011 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8012 || IT_CHARPOS (*it) == it->stop_charpos))
8013 {
8014 /* With bidi non-linear iteration, we could find ourselves
8015 far beyond the last computed stop_charpos, with several
8016 other stop positions in between that we missed. Scan
8017 them all now, in buffer's logical order, until we find
8018 and handle the last stop_charpos that precedes our
8019 current position. */
8020 handle_stop_backwards (it, it->stop_charpos);
8021 return GET_NEXT_DISPLAY_ELEMENT (it);
8022 }
8023 else
8024 {
8025 if (it->bidi_p)
8026 {
8027 /* Take note of the stop position we just moved across,
8028 for when we will move back across it. */
8029 it->prev_stop = it->stop_charpos;
8030 /* If we are at base paragraph embedding level, take
8031 note of the last stop position seen at this
8032 level. */
8033 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8034 it->base_level_stop = it->stop_charpos;
8035 }
8036 handle_stop (it);
8037 return GET_NEXT_DISPLAY_ELEMENT (it);
8038 }
8039 }
8040 else if (it->bidi_p
8041 /* If we are before prev_stop, we may have overstepped on
8042 our way backwards a stop_pos, and if so, we need to
8043 handle that stop_pos. */
8044 && IT_CHARPOS (*it) < it->prev_stop
8045 /* We can sometimes back up for reasons that have nothing
8046 to do with bidi reordering. E.g., compositions. The
8047 code below is only needed when we are above the base
8048 embedding level, so test for that explicitly. */
8049 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8050 {
8051 if (it->base_level_stop <= 0
8052 || IT_CHARPOS (*it) < it->base_level_stop)
8053 {
8054 /* If we lost track of base_level_stop, we need to find
8055 prev_stop by looking backwards. This happens, e.g., when
8056 we were reseated to the previous screenful of text by
8057 vertical-motion. */
8058 it->base_level_stop = BEGV;
8059 compute_stop_pos_backwards (it);
8060 handle_stop_backwards (it, it->prev_stop);
8061 }
8062 else
8063 handle_stop_backwards (it, it->base_level_stop);
8064 return GET_NEXT_DISPLAY_ELEMENT (it);
8065 }
8066 else
8067 {
8068 /* No face changes, overlays etc. in sight, so just return a
8069 character from current_buffer. */
8070 unsigned char *p;
8071 ptrdiff_t stop;
8072
8073 /* Maybe run the redisplay end trigger hook. Performance note:
8074 This doesn't seem to cost measurable time. */
8075 if (it->redisplay_end_trigger_charpos
8076 && it->glyph_row
8077 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8078 run_redisplay_end_trigger_hook (it);
8079
8080 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8081 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8082 stop)
8083 && next_element_from_composition (it))
8084 {
8085 return 1;
8086 }
8087
8088 /* Get the next character, maybe multibyte. */
8089 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8090 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8091 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8092 else
8093 it->c = *p, it->len = 1;
8094
8095 /* Record what we have and where it came from. */
8096 it->what = IT_CHARACTER;
8097 it->object = it->w->contents;
8098 it->position = it->current.pos;
8099
8100 /* Normally we return the character found above, except when we
8101 really want to return an ellipsis for selective display. */
8102 if (it->selective)
8103 {
8104 if (it->c == '\n')
8105 {
8106 /* A value of selective > 0 means hide lines indented more
8107 than that number of columns. */
8108 if (it->selective > 0
8109 && IT_CHARPOS (*it) + 1 < ZV
8110 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8111 IT_BYTEPOS (*it) + 1,
8112 it->selective))
8113 {
8114 success_p = next_element_from_ellipsis (it);
8115 it->dpvec_char_len = -1;
8116 }
8117 }
8118 else if (it->c == '\r' && it->selective == -1)
8119 {
8120 /* A value of selective == -1 means that everything from the
8121 CR to the end of the line is invisible, with maybe an
8122 ellipsis displayed for it. */
8123 success_p = next_element_from_ellipsis (it);
8124 it->dpvec_char_len = -1;
8125 }
8126 }
8127 }
8128
8129 /* Value is zero if end of buffer reached. */
8130 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8131 return success_p;
8132 }
8133
8134
8135 /* Run the redisplay end trigger hook for IT. */
8136
8137 static void
8138 run_redisplay_end_trigger_hook (struct it *it)
8139 {
8140 Lisp_Object args[3];
8141
8142 /* IT->glyph_row should be non-null, i.e. we should be actually
8143 displaying something, or otherwise we should not run the hook. */
8144 eassert (it->glyph_row);
8145
8146 /* Set up hook arguments. */
8147 args[0] = Qredisplay_end_trigger_functions;
8148 args[1] = it->window;
8149 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8150 it->redisplay_end_trigger_charpos = 0;
8151
8152 /* Since we are *trying* to run these functions, don't try to run
8153 them again, even if they get an error. */
8154 wset_redisplay_end_trigger (it->w, Qnil);
8155 Frun_hook_with_args (3, args);
8156
8157 /* Notice if it changed the face of the character we are on. */
8158 handle_face_prop (it);
8159 }
8160
8161
8162 /* Deliver a composition display element. Unlike the other
8163 next_element_from_XXX, this function is not registered in the array
8164 get_next_element[]. It is called from next_element_from_buffer and
8165 next_element_from_string when necessary. */
8166
8167 static int
8168 next_element_from_composition (struct it *it)
8169 {
8170 it->what = IT_COMPOSITION;
8171 it->len = it->cmp_it.nbytes;
8172 if (STRINGP (it->string))
8173 {
8174 if (it->c < 0)
8175 {
8176 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8177 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8178 return 0;
8179 }
8180 it->position = it->current.string_pos;
8181 it->object = it->string;
8182 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8183 IT_STRING_BYTEPOS (*it), it->string);
8184 }
8185 else
8186 {
8187 if (it->c < 0)
8188 {
8189 IT_CHARPOS (*it) += it->cmp_it.nchars;
8190 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8191 if (it->bidi_p)
8192 {
8193 if (it->bidi_it.new_paragraph)
8194 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8195 /* Resync the bidi iterator with IT's new position.
8196 FIXME: this doesn't support bidirectional text. */
8197 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8198 bidi_move_to_visually_next (&it->bidi_it);
8199 }
8200 return 0;
8201 }
8202 it->position = it->current.pos;
8203 it->object = it->w->contents;
8204 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8205 IT_BYTEPOS (*it), Qnil);
8206 }
8207 return 1;
8208 }
8209
8210
8211 \f
8212 /***********************************************************************
8213 Moving an iterator without producing glyphs
8214 ***********************************************************************/
8215
8216 /* Check if iterator is at a position corresponding to a valid buffer
8217 position after some move_it_ call. */
8218
8219 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8220 ((it)->method == GET_FROM_STRING \
8221 ? IT_STRING_CHARPOS (*it) == 0 \
8222 : 1)
8223
8224
8225 /* Move iterator IT to a specified buffer or X position within one
8226 line on the display without producing glyphs.
8227
8228 OP should be a bit mask including some or all of these bits:
8229 MOVE_TO_X: Stop upon reaching x-position TO_X.
8230 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8231 Regardless of OP's value, stop upon reaching the end of the display line.
8232
8233 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8234 This means, in particular, that TO_X includes window's horizontal
8235 scroll amount.
8236
8237 The return value has several possible values that
8238 say what condition caused the scan to stop:
8239
8240 MOVE_POS_MATCH_OR_ZV
8241 - when TO_POS or ZV was reached.
8242
8243 MOVE_X_REACHED
8244 -when TO_X was reached before TO_POS or ZV were reached.
8245
8246 MOVE_LINE_CONTINUED
8247 - when we reached the end of the display area and the line must
8248 be continued.
8249
8250 MOVE_LINE_TRUNCATED
8251 - when we reached the end of the display area and the line is
8252 truncated.
8253
8254 MOVE_NEWLINE_OR_CR
8255 - when we stopped at a line end, i.e. a newline or a CR and selective
8256 display is on. */
8257
8258 static enum move_it_result
8259 move_it_in_display_line_to (struct it *it,
8260 ptrdiff_t to_charpos, int to_x,
8261 enum move_operation_enum op)
8262 {
8263 enum move_it_result result = MOVE_UNDEFINED;
8264 struct glyph_row *saved_glyph_row;
8265 struct it wrap_it, atpos_it, atx_it, ppos_it;
8266 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8267 void *ppos_data = NULL;
8268 int may_wrap = 0;
8269 enum it_method prev_method = it->method;
8270 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8271 int saw_smaller_pos = prev_pos < to_charpos;
8272
8273 /* Don't produce glyphs in produce_glyphs. */
8274 saved_glyph_row = it->glyph_row;
8275 it->glyph_row = NULL;
8276
8277 /* Use wrap_it to save a copy of IT wherever a word wrap could
8278 occur. Use atpos_it to save a copy of IT at the desired buffer
8279 position, if found, so that we can scan ahead and check if the
8280 word later overshoots the window edge. Use atx_it similarly, for
8281 pixel positions. */
8282 wrap_it.sp = -1;
8283 atpos_it.sp = -1;
8284 atx_it.sp = -1;
8285
8286 /* Use ppos_it under bidi reordering to save a copy of IT for the
8287 position > CHARPOS that is the closest to CHARPOS. We restore
8288 that position in IT when we have scanned the entire display line
8289 without finding a match for CHARPOS and all the character
8290 positions are greater than CHARPOS. */
8291 if (it->bidi_p)
8292 {
8293 SAVE_IT (ppos_it, *it, ppos_data);
8294 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8295 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8296 SAVE_IT (ppos_it, *it, ppos_data);
8297 }
8298
8299 #define BUFFER_POS_REACHED_P() \
8300 ((op & MOVE_TO_POS) != 0 \
8301 && BUFFERP (it->object) \
8302 && (IT_CHARPOS (*it) == to_charpos \
8303 || ((!it->bidi_p \
8304 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8305 && IT_CHARPOS (*it) > to_charpos) \
8306 || (it->what == IT_COMPOSITION \
8307 && ((IT_CHARPOS (*it) > to_charpos \
8308 && to_charpos >= it->cmp_it.charpos) \
8309 || (IT_CHARPOS (*it) < to_charpos \
8310 && to_charpos <= it->cmp_it.charpos)))) \
8311 && (it->method == GET_FROM_BUFFER \
8312 || (it->method == GET_FROM_DISPLAY_VECTOR \
8313 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8314
8315 /* If there's a line-/wrap-prefix, handle it. */
8316 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8317 && it->current_y < it->last_visible_y)
8318 handle_line_prefix (it);
8319
8320 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8321 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8322
8323 while (1)
8324 {
8325 int x, i, ascent = 0, descent = 0;
8326
8327 /* Utility macro to reset an iterator with x, ascent, and descent. */
8328 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8329 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8330 (IT)->max_descent = descent)
8331
8332 /* Stop if we move beyond TO_CHARPOS (after an image or a
8333 display string or stretch glyph). */
8334 if ((op & MOVE_TO_POS) != 0
8335 && BUFFERP (it->object)
8336 && it->method == GET_FROM_BUFFER
8337 && (((!it->bidi_p
8338 /* When the iterator is at base embedding level, we
8339 are guaranteed that characters are delivered for
8340 display in strictly increasing order of their
8341 buffer positions. */
8342 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8343 && IT_CHARPOS (*it) > to_charpos)
8344 || (it->bidi_p
8345 && (prev_method == GET_FROM_IMAGE
8346 || prev_method == GET_FROM_STRETCH
8347 || prev_method == GET_FROM_STRING)
8348 /* Passed TO_CHARPOS from left to right. */
8349 && ((prev_pos < to_charpos
8350 && IT_CHARPOS (*it) > to_charpos)
8351 /* Passed TO_CHARPOS from right to left. */
8352 || (prev_pos > to_charpos
8353 && IT_CHARPOS (*it) < to_charpos)))))
8354 {
8355 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8356 {
8357 result = MOVE_POS_MATCH_OR_ZV;
8358 break;
8359 }
8360 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8361 /* If wrap_it is valid, the current position might be in a
8362 word that is wrapped. So, save the iterator in
8363 atpos_it and continue to see if wrapping happens. */
8364 SAVE_IT (atpos_it, *it, atpos_data);
8365 }
8366
8367 /* Stop when ZV reached.
8368 We used to stop here when TO_CHARPOS reached as well, but that is
8369 too soon if this glyph does not fit on this line. So we handle it
8370 explicitly below. */
8371 if (!get_next_display_element (it))
8372 {
8373 result = MOVE_POS_MATCH_OR_ZV;
8374 break;
8375 }
8376
8377 if (it->line_wrap == TRUNCATE)
8378 {
8379 if (BUFFER_POS_REACHED_P ())
8380 {
8381 result = MOVE_POS_MATCH_OR_ZV;
8382 break;
8383 }
8384 }
8385 else
8386 {
8387 if (it->line_wrap == WORD_WRAP)
8388 {
8389 if (IT_DISPLAYING_WHITESPACE (it))
8390 may_wrap = 1;
8391 else if (may_wrap)
8392 {
8393 /* We have reached a glyph that follows one or more
8394 whitespace characters. If the position is
8395 already found, we are done. */
8396 if (atpos_it.sp >= 0)
8397 {
8398 RESTORE_IT (it, &atpos_it, atpos_data);
8399 result = MOVE_POS_MATCH_OR_ZV;
8400 goto done;
8401 }
8402 if (atx_it.sp >= 0)
8403 {
8404 RESTORE_IT (it, &atx_it, atx_data);
8405 result = MOVE_X_REACHED;
8406 goto done;
8407 }
8408 /* Otherwise, we can wrap here. */
8409 SAVE_IT (wrap_it, *it, wrap_data);
8410 may_wrap = 0;
8411 }
8412 }
8413 }
8414
8415 /* Remember the line height for the current line, in case
8416 the next element doesn't fit on the line. */
8417 ascent = it->max_ascent;
8418 descent = it->max_descent;
8419
8420 /* The call to produce_glyphs will get the metrics of the
8421 display element IT is loaded with. Record the x-position
8422 before this display element, in case it doesn't fit on the
8423 line. */
8424 x = it->current_x;
8425
8426 PRODUCE_GLYPHS (it);
8427
8428 if (it->area != TEXT_AREA)
8429 {
8430 prev_method = it->method;
8431 if (it->method == GET_FROM_BUFFER)
8432 prev_pos = IT_CHARPOS (*it);
8433 set_iterator_to_next (it, 1);
8434 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8435 SET_TEXT_POS (this_line_min_pos,
8436 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8437 if (it->bidi_p
8438 && (op & MOVE_TO_POS)
8439 && IT_CHARPOS (*it) > to_charpos
8440 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8441 SAVE_IT (ppos_it, *it, ppos_data);
8442 continue;
8443 }
8444
8445 /* The number of glyphs we get back in IT->nglyphs will normally
8446 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8447 character on a terminal frame, or (iii) a line end. For the
8448 second case, IT->nglyphs - 1 padding glyphs will be present.
8449 (On X frames, there is only one glyph produced for a
8450 composite character.)
8451
8452 The behavior implemented below means, for continuation lines,
8453 that as many spaces of a TAB as fit on the current line are
8454 displayed there. For terminal frames, as many glyphs of a
8455 multi-glyph character are displayed in the current line, too.
8456 This is what the old redisplay code did, and we keep it that
8457 way. Under X, the whole shape of a complex character must
8458 fit on the line or it will be completely displayed in the
8459 next line.
8460
8461 Note that both for tabs and padding glyphs, all glyphs have
8462 the same width. */
8463 if (it->nglyphs)
8464 {
8465 /* More than one glyph or glyph doesn't fit on line. All
8466 glyphs have the same width. */
8467 int single_glyph_width = it->pixel_width / it->nglyphs;
8468 int new_x;
8469 int x_before_this_char = x;
8470 int hpos_before_this_char = it->hpos;
8471
8472 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8473 {
8474 new_x = x + single_glyph_width;
8475
8476 /* We want to leave anything reaching TO_X to the caller. */
8477 if ((op & MOVE_TO_X) && new_x > to_x)
8478 {
8479 if (BUFFER_POS_REACHED_P ())
8480 {
8481 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8482 goto buffer_pos_reached;
8483 if (atpos_it.sp < 0)
8484 {
8485 SAVE_IT (atpos_it, *it, atpos_data);
8486 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8487 }
8488 }
8489 else
8490 {
8491 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8492 {
8493 it->current_x = x;
8494 result = MOVE_X_REACHED;
8495 break;
8496 }
8497 if (atx_it.sp < 0)
8498 {
8499 SAVE_IT (atx_it, *it, atx_data);
8500 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8501 }
8502 }
8503 }
8504
8505 if (/* Lines are continued. */
8506 it->line_wrap != TRUNCATE
8507 && (/* And glyph doesn't fit on the line. */
8508 new_x > it->last_visible_x
8509 /* Or it fits exactly and we're on a window
8510 system frame. */
8511 || (new_x == it->last_visible_x
8512 && FRAME_WINDOW_P (it->f)
8513 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8514 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8515 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8516 {
8517 if (/* IT->hpos == 0 means the very first glyph
8518 doesn't fit on the line, e.g. a wide image. */
8519 it->hpos == 0
8520 || (new_x == it->last_visible_x
8521 && FRAME_WINDOW_P (it->f)))
8522 {
8523 ++it->hpos;
8524 it->current_x = new_x;
8525
8526 /* The character's last glyph just barely fits
8527 in this row. */
8528 if (i == it->nglyphs - 1)
8529 {
8530 /* If this is the destination position,
8531 return a position *before* it in this row,
8532 now that we know it fits in this row. */
8533 if (BUFFER_POS_REACHED_P ())
8534 {
8535 if (it->line_wrap != WORD_WRAP
8536 || wrap_it.sp < 0)
8537 {
8538 it->hpos = hpos_before_this_char;
8539 it->current_x = x_before_this_char;
8540 result = MOVE_POS_MATCH_OR_ZV;
8541 break;
8542 }
8543 if (it->line_wrap == WORD_WRAP
8544 && atpos_it.sp < 0)
8545 {
8546 SAVE_IT (atpos_it, *it, atpos_data);
8547 atpos_it.current_x = x_before_this_char;
8548 atpos_it.hpos = hpos_before_this_char;
8549 }
8550 }
8551
8552 prev_method = it->method;
8553 if (it->method == GET_FROM_BUFFER)
8554 prev_pos = IT_CHARPOS (*it);
8555 set_iterator_to_next (it, 1);
8556 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8557 SET_TEXT_POS (this_line_min_pos,
8558 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8559 /* On graphical terminals, newlines may
8560 "overflow" into the fringe if
8561 overflow-newline-into-fringe is non-nil.
8562 On text terminals, and on graphical
8563 terminals with no right margin, newlines
8564 may overflow into the last glyph on the
8565 display line.*/
8566 if (!FRAME_WINDOW_P (it->f)
8567 || ((it->bidi_p
8568 && it->bidi_it.paragraph_dir == R2L)
8569 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8570 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8571 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8572 {
8573 if (!get_next_display_element (it))
8574 {
8575 result = MOVE_POS_MATCH_OR_ZV;
8576 break;
8577 }
8578 if (BUFFER_POS_REACHED_P ())
8579 {
8580 if (ITERATOR_AT_END_OF_LINE_P (it))
8581 result = MOVE_POS_MATCH_OR_ZV;
8582 else
8583 result = MOVE_LINE_CONTINUED;
8584 break;
8585 }
8586 if (ITERATOR_AT_END_OF_LINE_P (it)
8587 && (it->line_wrap != WORD_WRAP
8588 || wrap_it.sp < 0))
8589 {
8590 result = MOVE_NEWLINE_OR_CR;
8591 break;
8592 }
8593 }
8594 }
8595 }
8596 else
8597 IT_RESET_X_ASCENT_DESCENT (it);
8598
8599 if (wrap_it.sp >= 0)
8600 {
8601 RESTORE_IT (it, &wrap_it, wrap_data);
8602 atpos_it.sp = -1;
8603 atx_it.sp = -1;
8604 }
8605
8606 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8607 IT_CHARPOS (*it)));
8608 result = MOVE_LINE_CONTINUED;
8609 break;
8610 }
8611
8612 if (BUFFER_POS_REACHED_P ())
8613 {
8614 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8615 goto buffer_pos_reached;
8616 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8617 {
8618 SAVE_IT (atpos_it, *it, atpos_data);
8619 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8620 }
8621 }
8622
8623 if (new_x > it->first_visible_x)
8624 {
8625 /* Glyph is visible. Increment number of glyphs that
8626 would be displayed. */
8627 ++it->hpos;
8628 }
8629 }
8630
8631 if (result != MOVE_UNDEFINED)
8632 break;
8633 }
8634 else if (BUFFER_POS_REACHED_P ())
8635 {
8636 buffer_pos_reached:
8637 IT_RESET_X_ASCENT_DESCENT (it);
8638 result = MOVE_POS_MATCH_OR_ZV;
8639 break;
8640 }
8641 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8642 {
8643 /* Stop when TO_X specified and reached. This check is
8644 necessary here because of lines consisting of a line end,
8645 only. The line end will not produce any glyphs and we
8646 would never get MOVE_X_REACHED. */
8647 eassert (it->nglyphs == 0);
8648 result = MOVE_X_REACHED;
8649 break;
8650 }
8651
8652 /* Is this a line end? If yes, we're done. */
8653 if (ITERATOR_AT_END_OF_LINE_P (it))
8654 {
8655 /* If we are past TO_CHARPOS, but never saw any character
8656 positions smaller than TO_CHARPOS, return
8657 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8658 did. */
8659 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8660 {
8661 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8662 {
8663 if (IT_CHARPOS (ppos_it) < ZV)
8664 {
8665 RESTORE_IT (it, &ppos_it, ppos_data);
8666 result = MOVE_POS_MATCH_OR_ZV;
8667 }
8668 else
8669 goto buffer_pos_reached;
8670 }
8671 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8672 && IT_CHARPOS (*it) > to_charpos)
8673 goto buffer_pos_reached;
8674 else
8675 result = MOVE_NEWLINE_OR_CR;
8676 }
8677 else
8678 result = MOVE_NEWLINE_OR_CR;
8679 break;
8680 }
8681
8682 prev_method = it->method;
8683 if (it->method == GET_FROM_BUFFER)
8684 prev_pos = IT_CHARPOS (*it);
8685 /* The current display element has been consumed. Advance
8686 to the next. */
8687 set_iterator_to_next (it, 1);
8688 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8689 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8690 if (IT_CHARPOS (*it) < to_charpos)
8691 saw_smaller_pos = 1;
8692 if (it->bidi_p
8693 && (op & MOVE_TO_POS)
8694 && IT_CHARPOS (*it) >= to_charpos
8695 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8696 SAVE_IT (ppos_it, *it, ppos_data);
8697
8698 /* Stop if lines are truncated and IT's current x-position is
8699 past the right edge of the window now. */
8700 if (it->line_wrap == TRUNCATE
8701 && it->current_x >= it->last_visible_x)
8702 {
8703 if (!FRAME_WINDOW_P (it->f)
8704 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8705 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8706 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8707 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8708 {
8709 int at_eob_p = 0;
8710
8711 if ((at_eob_p = !get_next_display_element (it))
8712 || BUFFER_POS_REACHED_P ()
8713 /* If we are past TO_CHARPOS, but never saw any
8714 character positions smaller than TO_CHARPOS,
8715 return MOVE_POS_MATCH_OR_ZV, like the
8716 unidirectional display did. */
8717 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8718 && !saw_smaller_pos
8719 && IT_CHARPOS (*it) > to_charpos))
8720 {
8721 if (it->bidi_p
8722 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8723 RESTORE_IT (it, &ppos_it, ppos_data);
8724 result = MOVE_POS_MATCH_OR_ZV;
8725 break;
8726 }
8727 if (ITERATOR_AT_END_OF_LINE_P (it))
8728 {
8729 result = MOVE_NEWLINE_OR_CR;
8730 break;
8731 }
8732 }
8733 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8734 && !saw_smaller_pos
8735 && IT_CHARPOS (*it) > to_charpos)
8736 {
8737 if (IT_CHARPOS (ppos_it) < ZV)
8738 RESTORE_IT (it, &ppos_it, ppos_data);
8739 result = MOVE_POS_MATCH_OR_ZV;
8740 break;
8741 }
8742 result = MOVE_LINE_TRUNCATED;
8743 break;
8744 }
8745 #undef IT_RESET_X_ASCENT_DESCENT
8746 }
8747
8748 #undef BUFFER_POS_REACHED_P
8749
8750 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8751 restore the saved iterator. */
8752 if (atpos_it.sp >= 0)
8753 RESTORE_IT (it, &atpos_it, atpos_data);
8754 else if (atx_it.sp >= 0)
8755 RESTORE_IT (it, &atx_it, atx_data);
8756
8757 done:
8758
8759 if (atpos_data)
8760 bidi_unshelve_cache (atpos_data, 1);
8761 if (atx_data)
8762 bidi_unshelve_cache (atx_data, 1);
8763 if (wrap_data)
8764 bidi_unshelve_cache (wrap_data, 1);
8765 if (ppos_data)
8766 bidi_unshelve_cache (ppos_data, 1);
8767
8768 /* Restore the iterator settings altered at the beginning of this
8769 function. */
8770 it->glyph_row = saved_glyph_row;
8771 return result;
8772 }
8773
8774 /* For external use. */
8775 void
8776 move_it_in_display_line (struct it *it,
8777 ptrdiff_t to_charpos, int to_x,
8778 enum move_operation_enum op)
8779 {
8780 if (it->line_wrap == WORD_WRAP
8781 && (op & MOVE_TO_X))
8782 {
8783 struct it save_it;
8784 void *save_data = NULL;
8785 int skip;
8786
8787 SAVE_IT (save_it, *it, save_data);
8788 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8789 /* When word-wrap is on, TO_X may lie past the end
8790 of a wrapped line. Then it->current is the
8791 character on the next line, so backtrack to the
8792 space before the wrap point. */
8793 if (skip == MOVE_LINE_CONTINUED)
8794 {
8795 int prev_x = max (it->current_x - 1, 0);
8796 RESTORE_IT (it, &save_it, save_data);
8797 move_it_in_display_line_to
8798 (it, -1, prev_x, MOVE_TO_X);
8799 }
8800 else
8801 bidi_unshelve_cache (save_data, 1);
8802 }
8803 else
8804 move_it_in_display_line_to (it, to_charpos, to_x, op);
8805 }
8806
8807
8808 /* Move IT forward until it satisfies one or more of the criteria in
8809 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8810
8811 OP is a bit-mask that specifies where to stop, and in particular,
8812 which of those four position arguments makes a difference. See the
8813 description of enum move_operation_enum.
8814
8815 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8816 screen line, this function will set IT to the next position that is
8817 displayed to the right of TO_CHARPOS on the screen. */
8818
8819 void
8820 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8821 {
8822 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8823 int line_height, line_start_x = 0, reached = 0;
8824 void *backup_data = NULL;
8825
8826 for (;;)
8827 {
8828 if (op & MOVE_TO_VPOS)
8829 {
8830 /* If no TO_CHARPOS and no TO_X specified, stop at the
8831 start of the line TO_VPOS. */
8832 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8833 {
8834 if (it->vpos == to_vpos)
8835 {
8836 reached = 1;
8837 break;
8838 }
8839 else
8840 skip = move_it_in_display_line_to (it, -1, -1, 0);
8841 }
8842 else
8843 {
8844 /* TO_VPOS >= 0 means stop at TO_X in the line at
8845 TO_VPOS, or at TO_POS, whichever comes first. */
8846 if (it->vpos == to_vpos)
8847 {
8848 reached = 2;
8849 break;
8850 }
8851
8852 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8853
8854 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8855 {
8856 reached = 3;
8857 break;
8858 }
8859 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8860 {
8861 /* We have reached TO_X but not in the line we want. */
8862 skip = move_it_in_display_line_to (it, to_charpos,
8863 -1, MOVE_TO_POS);
8864 if (skip == MOVE_POS_MATCH_OR_ZV)
8865 {
8866 reached = 4;
8867 break;
8868 }
8869 }
8870 }
8871 }
8872 else if (op & MOVE_TO_Y)
8873 {
8874 struct it it_backup;
8875
8876 if (it->line_wrap == WORD_WRAP)
8877 SAVE_IT (it_backup, *it, backup_data);
8878
8879 /* TO_Y specified means stop at TO_X in the line containing
8880 TO_Y---or at TO_CHARPOS if this is reached first. The
8881 problem is that we can't really tell whether the line
8882 contains TO_Y before we have completely scanned it, and
8883 this may skip past TO_X. What we do is to first scan to
8884 TO_X.
8885
8886 If TO_X is not specified, use a TO_X of zero. The reason
8887 is to make the outcome of this function more predictable.
8888 If we didn't use TO_X == 0, we would stop at the end of
8889 the line which is probably not what a caller would expect
8890 to happen. */
8891 skip = move_it_in_display_line_to
8892 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8893 (MOVE_TO_X | (op & MOVE_TO_POS)));
8894
8895 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8896 if (skip == MOVE_POS_MATCH_OR_ZV)
8897 reached = 5;
8898 else if (skip == MOVE_X_REACHED)
8899 {
8900 /* If TO_X was reached, we want to know whether TO_Y is
8901 in the line. We know this is the case if the already
8902 scanned glyphs make the line tall enough. Otherwise,
8903 we must check by scanning the rest of the line. */
8904 line_height = it->max_ascent + it->max_descent;
8905 if (to_y >= it->current_y
8906 && to_y < it->current_y + line_height)
8907 {
8908 reached = 6;
8909 break;
8910 }
8911 SAVE_IT (it_backup, *it, backup_data);
8912 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8913 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8914 op & MOVE_TO_POS);
8915 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8916 line_height = it->max_ascent + it->max_descent;
8917 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8918
8919 if (to_y >= it->current_y
8920 && to_y < it->current_y + line_height)
8921 {
8922 /* If TO_Y is in this line and TO_X was reached
8923 above, we scanned too far. We have to restore
8924 IT's settings to the ones before skipping. But
8925 keep the more accurate values of max_ascent and
8926 max_descent we've found while skipping the rest
8927 of the line, for the sake of callers, such as
8928 pos_visible_p, that need to know the line
8929 height. */
8930 int max_ascent = it->max_ascent;
8931 int max_descent = it->max_descent;
8932
8933 RESTORE_IT (it, &it_backup, backup_data);
8934 it->max_ascent = max_ascent;
8935 it->max_descent = max_descent;
8936 reached = 6;
8937 }
8938 else
8939 {
8940 skip = skip2;
8941 if (skip == MOVE_POS_MATCH_OR_ZV)
8942 reached = 7;
8943 }
8944 }
8945 else
8946 {
8947 /* Check whether TO_Y is in this line. */
8948 line_height = it->max_ascent + it->max_descent;
8949 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8950
8951 if (to_y >= it->current_y
8952 && to_y < it->current_y + line_height)
8953 {
8954 /* When word-wrap is on, TO_X may lie past the end
8955 of a wrapped line. Then it->current is the
8956 character on the next line, so backtrack to the
8957 space before the wrap point. */
8958 if (skip == MOVE_LINE_CONTINUED
8959 && it->line_wrap == WORD_WRAP)
8960 {
8961 int prev_x = max (it->current_x - 1, 0);
8962 RESTORE_IT (it, &it_backup, backup_data);
8963 skip = move_it_in_display_line_to
8964 (it, -1, prev_x, MOVE_TO_X);
8965 }
8966 reached = 6;
8967 }
8968 }
8969
8970 if (reached)
8971 break;
8972 }
8973 else if (BUFFERP (it->object)
8974 && (it->method == GET_FROM_BUFFER
8975 || it->method == GET_FROM_STRETCH)
8976 && IT_CHARPOS (*it) >= to_charpos
8977 /* Under bidi iteration, a call to set_iterator_to_next
8978 can scan far beyond to_charpos if the initial
8979 portion of the next line needs to be reordered. In
8980 that case, give move_it_in_display_line_to another
8981 chance below. */
8982 && !(it->bidi_p
8983 && it->bidi_it.scan_dir == -1))
8984 skip = MOVE_POS_MATCH_OR_ZV;
8985 else
8986 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8987
8988 switch (skip)
8989 {
8990 case MOVE_POS_MATCH_OR_ZV:
8991 reached = 8;
8992 goto out;
8993
8994 case MOVE_NEWLINE_OR_CR:
8995 set_iterator_to_next (it, 1);
8996 it->continuation_lines_width = 0;
8997 break;
8998
8999 case MOVE_LINE_TRUNCATED:
9000 it->continuation_lines_width = 0;
9001 reseat_at_next_visible_line_start (it, 0);
9002 if ((op & MOVE_TO_POS) != 0
9003 && IT_CHARPOS (*it) > to_charpos)
9004 {
9005 reached = 9;
9006 goto out;
9007 }
9008 break;
9009
9010 case MOVE_LINE_CONTINUED:
9011 /* For continued lines ending in a tab, some of the glyphs
9012 associated with the tab are displayed on the current
9013 line. Since it->current_x does not include these glyphs,
9014 we use it->last_visible_x instead. */
9015 if (it->c == '\t')
9016 {
9017 it->continuation_lines_width += it->last_visible_x;
9018 /* When moving by vpos, ensure that the iterator really
9019 advances to the next line (bug#847, bug#969). Fixme:
9020 do we need to do this in other circumstances? */
9021 if (it->current_x != it->last_visible_x
9022 && (op & MOVE_TO_VPOS)
9023 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9024 {
9025 line_start_x = it->current_x + it->pixel_width
9026 - it->last_visible_x;
9027 set_iterator_to_next (it, 0);
9028 }
9029 }
9030 else
9031 it->continuation_lines_width += it->current_x;
9032 break;
9033
9034 default:
9035 emacs_abort ();
9036 }
9037
9038 /* Reset/increment for the next run. */
9039 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9040 it->current_x = line_start_x;
9041 line_start_x = 0;
9042 it->hpos = 0;
9043 it->current_y += it->max_ascent + it->max_descent;
9044 ++it->vpos;
9045 last_height = it->max_ascent + it->max_descent;
9046 it->max_ascent = it->max_descent = 0;
9047 }
9048
9049 out:
9050
9051 /* On text terminals, we may stop at the end of a line in the middle
9052 of a multi-character glyph. If the glyph itself is continued,
9053 i.e. it is actually displayed on the next line, don't treat this
9054 stopping point as valid; move to the next line instead (unless
9055 that brings us offscreen). */
9056 if (!FRAME_WINDOW_P (it->f)
9057 && op & MOVE_TO_POS
9058 && IT_CHARPOS (*it) == to_charpos
9059 && it->what == IT_CHARACTER
9060 && it->nglyphs > 1
9061 && it->line_wrap == WINDOW_WRAP
9062 && it->current_x == it->last_visible_x - 1
9063 && it->c != '\n'
9064 && it->c != '\t'
9065 && it->vpos < it->w->window_end_vpos)
9066 {
9067 it->continuation_lines_width += it->current_x;
9068 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9069 it->current_y += it->max_ascent + it->max_descent;
9070 ++it->vpos;
9071 last_height = it->max_ascent + it->max_descent;
9072 }
9073
9074 if (backup_data)
9075 bidi_unshelve_cache (backup_data, 1);
9076
9077 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9078 }
9079
9080
9081 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9082
9083 If DY > 0, move IT backward at least that many pixels. DY = 0
9084 means move IT backward to the preceding line start or BEGV. This
9085 function may move over more than DY pixels if IT->current_y - DY
9086 ends up in the middle of a line; in this case IT->current_y will be
9087 set to the top of the line moved to. */
9088
9089 void
9090 move_it_vertically_backward (struct it *it, int dy)
9091 {
9092 int nlines, h;
9093 struct it it2, it3;
9094 void *it2data = NULL, *it3data = NULL;
9095 ptrdiff_t start_pos;
9096 int nchars_per_row
9097 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9098 ptrdiff_t pos_limit;
9099
9100 move_further_back:
9101 eassert (dy >= 0);
9102
9103 start_pos = IT_CHARPOS (*it);
9104
9105 /* Estimate how many newlines we must move back. */
9106 nlines = max (1, dy / default_line_pixel_height (it->w));
9107 if (it->line_wrap == TRUNCATE)
9108 pos_limit = BEGV;
9109 else
9110 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9111
9112 /* Set the iterator's position that many lines back. But don't go
9113 back more than NLINES full screen lines -- this wins a day with
9114 buffers which have very long lines. */
9115 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9116 back_to_previous_visible_line_start (it);
9117
9118 /* Reseat the iterator here. When moving backward, we don't want
9119 reseat to skip forward over invisible text, set up the iterator
9120 to deliver from overlay strings at the new position etc. So,
9121 use reseat_1 here. */
9122 reseat_1 (it, it->current.pos, 1);
9123
9124 /* We are now surely at a line start. */
9125 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9126 reordering is in effect. */
9127 it->continuation_lines_width = 0;
9128
9129 /* Move forward and see what y-distance we moved. First move to the
9130 start of the next line so that we get its height. We need this
9131 height to be able to tell whether we reached the specified
9132 y-distance. */
9133 SAVE_IT (it2, *it, it2data);
9134 it2.max_ascent = it2.max_descent = 0;
9135 do
9136 {
9137 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9138 MOVE_TO_POS | MOVE_TO_VPOS);
9139 }
9140 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9141 /* If we are in a display string which starts at START_POS,
9142 and that display string includes a newline, and we are
9143 right after that newline (i.e. at the beginning of a
9144 display line), exit the loop, because otherwise we will
9145 infloop, since move_it_to will see that it is already at
9146 START_POS and will not move. */
9147 || (it2.method == GET_FROM_STRING
9148 && IT_CHARPOS (it2) == start_pos
9149 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9150 eassert (IT_CHARPOS (*it) >= BEGV);
9151 SAVE_IT (it3, it2, it3data);
9152
9153 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9154 eassert (IT_CHARPOS (*it) >= BEGV);
9155 /* H is the actual vertical distance from the position in *IT
9156 and the starting position. */
9157 h = it2.current_y - it->current_y;
9158 /* NLINES is the distance in number of lines. */
9159 nlines = it2.vpos - it->vpos;
9160
9161 /* Correct IT's y and vpos position
9162 so that they are relative to the starting point. */
9163 it->vpos -= nlines;
9164 it->current_y -= h;
9165
9166 if (dy == 0)
9167 {
9168 /* DY == 0 means move to the start of the screen line. The
9169 value of nlines is > 0 if continuation lines were involved,
9170 or if the original IT position was at start of a line. */
9171 RESTORE_IT (it, it, it2data);
9172 if (nlines > 0)
9173 move_it_by_lines (it, nlines);
9174 /* The above code moves us to some position NLINES down,
9175 usually to its first glyph (leftmost in an L2R line), but
9176 that's not necessarily the start of the line, under bidi
9177 reordering. We want to get to the character position
9178 that is immediately after the newline of the previous
9179 line. */
9180 if (it->bidi_p
9181 && !it->continuation_lines_width
9182 && !STRINGP (it->string)
9183 && IT_CHARPOS (*it) > BEGV
9184 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9185 {
9186 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9187
9188 DEC_BOTH (cp, bp);
9189 cp = find_newline_no_quit (cp, bp, -1, NULL);
9190 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9191 }
9192 bidi_unshelve_cache (it3data, 1);
9193 }
9194 else
9195 {
9196 /* The y-position we try to reach, relative to *IT.
9197 Note that H has been subtracted in front of the if-statement. */
9198 int target_y = it->current_y + h - dy;
9199 int y0 = it3.current_y;
9200 int y1;
9201 int line_height;
9202
9203 RESTORE_IT (&it3, &it3, it3data);
9204 y1 = line_bottom_y (&it3);
9205 line_height = y1 - y0;
9206 RESTORE_IT (it, it, it2data);
9207 /* If we did not reach target_y, try to move further backward if
9208 we can. If we moved too far backward, try to move forward. */
9209 if (target_y < it->current_y
9210 /* This is heuristic. In a window that's 3 lines high, with
9211 a line height of 13 pixels each, recentering with point
9212 on the bottom line will try to move -39/2 = 19 pixels
9213 backward. Try to avoid moving into the first line. */
9214 && (it->current_y - target_y
9215 > min (window_box_height (it->w), line_height * 2 / 3))
9216 && IT_CHARPOS (*it) > BEGV)
9217 {
9218 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9219 target_y - it->current_y));
9220 dy = it->current_y - target_y;
9221 goto move_further_back;
9222 }
9223 else if (target_y >= it->current_y + line_height
9224 && IT_CHARPOS (*it) < ZV)
9225 {
9226 /* Should move forward by at least one line, maybe more.
9227
9228 Note: Calling move_it_by_lines can be expensive on
9229 terminal frames, where compute_motion is used (via
9230 vmotion) to do the job, when there are very long lines
9231 and truncate-lines is nil. That's the reason for
9232 treating terminal frames specially here. */
9233
9234 if (!FRAME_WINDOW_P (it->f))
9235 move_it_vertically (it, target_y - (it->current_y + line_height));
9236 else
9237 {
9238 do
9239 {
9240 move_it_by_lines (it, 1);
9241 }
9242 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9243 }
9244 }
9245 }
9246 }
9247
9248
9249 /* Move IT by a specified amount of pixel lines DY. DY negative means
9250 move backwards. DY = 0 means move to start of screen line. At the
9251 end, IT will be on the start of a screen line. */
9252
9253 void
9254 move_it_vertically (struct it *it, int dy)
9255 {
9256 if (dy <= 0)
9257 move_it_vertically_backward (it, -dy);
9258 else
9259 {
9260 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9261 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9262 MOVE_TO_POS | MOVE_TO_Y);
9263 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9264
9265 /* If buffer ends in ZV without a newline, move to the start of
9266 the line to satisfy the post-condition. */
9267 if (IT_CHARPOS (*it) == ZV
9268 && ZV > BEGV
9269 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9270 move_it_by_lines (it, 0);
9271 }
9272 }
9273
9274
9275 /* Move iterator IT past the end of the text line it is in. */
9276
9277 void
9278 move_it_past_eol (struct it *it)
9279 {
9280 enum move_it_result rc;
9281
9282 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9283 if (rc == MOVE_NEWLINE_OR_CR)
9284 set_iterator_to_next (it, 0);
9285 }
9286
9287
9288 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9289 negative means move up. DVPOS == 0 means move to the start of the
9290 screen line.
9291
9292 Optimization idea: If we would know that IT->f doesn't use
9293 a face with proportional font, we could be faster for
9294 truncate-lines nil. */
9295
9296 void
9297 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9298 {
9299
9300 /* The commented-out optimization uses vmotion on terminals. This
9301 gives bad results, because elements like it->what, on which
9302 callers such as pos_visible_p rely, aren't updated. */
9303 /* struct position pos;
9304 if (!FRAME_WINDOW_P (it->f))
9305 {
9306 struct text_pos textpos;
9307
9308 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9309 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9310 reseat (it, textpos, 1);
9311 it->vpos += pos.vpos;
9312 it->current_y += pos.vpos;
9313 }
9314 else */
9315
9316 if (dvpos == 0)
9317 {
9318 /* DVPOS == 0 means move to the start of the screen line. */
9319 move_it_vertically_backward (it, 0);
9320 /* Let next call to line_bottom_y calculate real line height */
9321 last_height = 0;
9322 }
9323 else if (dvpos > 0)
9324 {
9325 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9326 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9327 {
9328 /* Only move to the next buffer position if we ended up in a
9329 string from display property, not in an overlay string
9330 (before-string or after-string). That is because the
9331 latter don't conceal the underlying buffer position, so
9332 we can ask to move the iterator to the exact position we
9333 are interested in. Note that, even if we are already at
9334 IT_CHARPOS (*it), the call below is not a no-op, as it
9335 will detect that we are at the end of the string, pop the
9336 iterator, and compute it->current_x and it->hpos
9337 correctly. */
9338 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9339 -1, -1, -1, MOVE_TO_POS);
9340 }
9341 }
9342 else
9343 {
9344 struct it it2;
9345 void *it2data = NULL;
9346 ptrdiff_t start_charpos, i;
9347 int nchars_per_row
9348 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9349 ptrdiff_t pos_limit;
9350
9351 /* Start at the beginning of the screen line containing IT's
9352 position. This may actually move vertically backwards,
9353 in case of overlays, so adjust dvpos accordingly. */
9354 dvpos += it->vpos;
9355 move_it_vertically_backward (it, 0);
9356 dvpos -= it->vpos;
9357
9358 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9359 screen lines, and reseat the iterator there. */
9360 start_charpos = IT_CHARPOS (*it);
9361 if (it->line_wrap == TRUNCATE)
9362 pos_limit = BEGV;
9363 else
9364 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9365 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9366 back_to_previous_visible_line_start (it);
9367 reseat (it, it->current.pos, 1);
9368
9369 /* Move further back if we end up in a string or an image. */
9370 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9371 {
9372 /* First try to move to start of display line. */
9373 dvpos += it->vpos;
9374 move_it_vertically_backward (it, 0);
9375 dvpos -= it->vpos;
9376 if (IT_POS_VALID_AFTER_MOVE_P (it))
9377 break;
9378 /* If start of line is still in string or image,
9379 move further back. */
9380 back_to_previous_visible_line_start (it);
9381 reseat (it, it->current.pos, 1);
9382 dvpos--;
9383 }
9384
9385 it->current_x = it->hpos = 0;
9386
9387 /* Above call may have moved too far if continuation lines
9388 are involved. Scan forward and see if it did. */
9389 SAVE_IT (it2, *it, it2data);
9390 it2.vpos = it2.current_y = 0;
9391 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9392 it->vpos -= it2.vpos;
9393 it->current_y -= it2.current_y;
9394 it->current_x = it->hpos = 0;
9395
9396 /* If we moved too far back, move IT some lines forward. */
9397 if (it2.vpos > -dvpos)
9398 {
9399 int delta = it2.vpos + dvpos;
9400
9401 RESTORE_IT (&it2, &it2, it2data);
9402 SAVE_IT (it2, *it, it2data);
9403 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9404 /* Move back again if we got too far ahead. */
9405 if (IT_CHARPOS (*it) >= start_charpos)
9406 RESTORE_IT (it, &it2, it2data);
9407 else
9408 bidi_unshelve_cache (it2data, 1);
9409 }
9410 else
9411 RESTORE_IT (it, it, it2data);
9412 }
9413 }
9414
9415 /* Return 1 if IT points into the middle of a display vector. */
9416
9417 int
9418 in_display_vector_p (struct it *it)
9419 {
9420 return (it->method == GET_FROM_DISPLAY_VECTOR
9421 && it->current.dpvec_index > 0
9422 && it->dpvec + it->current.dpvec_index != it->dpend);
9423 }
9424
9425 \f
9426 /***********************************************************************
9427 Messages
9428 ***********************************************************************/
9429
9430
9431 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9432 to *Messages*. */
9433
9434 void
9435 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9436 {
9437 Lisp_Object args[3];
9438 Lisp_Object msg, fmt;
9439 char *buffer;
9440 ptrdiff_t len;
9441 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9442 USE_SAFE_ALLOCA;
9443
9444 fmt = msg = Qnil;
9445 GCPRO4 (fmt, msg, arg1, arg2);
9446
9447 args[0] = fmt = build_string (format);
9448 args[1] = arg1;
9449 args[2] = arg2;
9450 msg = Fformat (3, args);
9451
9452 len = SBYTES (msg) + 1;
9453 buffer = SAFE_ALLOCA (len);
9454 memcpy (buffer, SDATA (msg), len);
9455
9456 message_dolog (buffer, len - 1, 1, 0);
9457 SAFE_FREE ();
9458
9459 UNGCPRO;
9460 }
9461
9462
9463 /* Output a newline in the *Messages* buffer if "needs" one. */
9464
9465 void
9466 message_log_maybe_newline (void)
9467 {
9468 if (message_log_need_newline)
9469 message_dolog ("", 0, 1, 0);
9470 }
9471
9472
9473 /* Add a string M of length NBYTES to the message log, optionally
9474 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9475 true, means interpret the contents of M as multibyte. This
9476 function calls low-level routines in order to bypass text property
9477 hooks, etc. which might not be safe to run.
9478
9479 This may GC (insert may run before/after change hooks),
9480 so the buffer M must NOT point to a Lisp string. */
9481
9482 void
9483 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9484 {
9485 const unsigned char *msg = (const unsigned char *) m;
9486
9487 if (!NILP (Vmemory_full))
9488 return;
9489
9490 if (!NILP (Vmessage_log_max))
9491 {
9492 struct buffer *oldbuf;
9493 Lisp_Object oldpoint, oldbegv, oldzv;
9494 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9495 ptrdiff_t point_at_end = 0;
9496 ptrdiff_t zv_at_end = 0;
9497 Lisp_Object old_deactivate_mark;
9498 bool shown;
9499 struct gcpro gcpro1;
9500
9501 old_deactivate_mark = Vdeactivate_mark;
9502 oldbuf = current_buffer;
9503 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9504 bset_undo_list (current_buffer, Qt);
9505
9506 oldpoint = message_dolog_marker1;
9507 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9508 oldbegv = message_dolog_marker2;
9509 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9510 oldzv = message_dolog_marker3;
9511 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9512 GCPRO1 (old_deactivate_mark);
9513
9514 if (PT == Z)
9515 point_at_end = 1;
9516 if (ZV == Z)
9517 zv_at_end = 1;
9518
9519 BEGV = BEG;
9520 BEGV_BYTE = BEG_BYTE;
9521 ZV = Z;
9522 ZV_BYTE = Z_BYTE;
9523 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9524
9525 /* Insert the string--maybe converting multibyte to single byte
9526 or vice versa, so that all the text fits the buffer. */
9527 if (multibyte
9528 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9529 {
9530 ptrdiff_t i;
9531 int c, char_bytes;
9532 char work[1];
9533
9534 /* Convert a multibyte string to single-byte
9535 for the *Message* buffer. */
9536 for (i = 0; i < nbytes; i += char_bytes)
9537 {
9538 c = string_char_and_length (msg + i, &char_bytes);
9539 work[0] = (ASCII_CHAR_P (c)
9540 ? c
9541 : multibyte_char_to_unibyte (c));
9542 insert_1_both (work, 1, 1, 1, 0, 0);
9543 }
9544 }
9545 else if (! multibyte
9546 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9547 {
9548 ptrdiff_t i;
9549 int c, char_bytes;
9550 unsigned char str[MAX_MULTIBYTE_LENGTH];
9551 /* Convert a single-byte string to multibyte
9552 for the *Message* buffer. */
9553 for (i = 0; i < nbytes; i++)
9554 {
9555 c = msg[i];
9556 MAKE_CHAR_MULTIBYTE (c);
9557 char_bytes = CHAR_STRING (c, str);
9558 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9559 }
9560 }
9561 else if (nbytes)
9562 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9563
9564 if (nlflag)
9565 {
9566 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9567 printmax_t dups;
9568
9569 insert_1_both ("\n", 1, 1, 1, 0, 0);
9570
9571 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9572 this_bol = PT;
9573 this_bol_byte = PT_BYTE;
9574
9575 /* See if this line duplicates the previous one.
9576 If so, combine duplicates. */
9577 if (this_bol > BEG)
9578 {
9579 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9580 prev_bol = PT;
9581 prev_bol_byte = PT_BYTE;
9582
9583 dups = message_log_check_duplicate (prev_bol_byte,
9584 this_bol_byte);
9585 if (dups)
9586 {
9587 del_range_both (prev_bol, prev_bol_byte,
9588 this_bol, this_bol_byte, 0);
9589 if (dups > 1)
9590 {
9591 char dupstr[sizeof " [ times]"
9592 + INT_STRLEN_BOUND (printmax_t)];
9593
9594 /* If you change this format, don't forget to also
9595 change message_log_check_duplicate. */
9596 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9597 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9598 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9599 }
9600 }
9601 }
9602
9603 /* If we have more than the desired maximum number of lines
9604 in the *Messages* buffer now, delete the oldest ones.
9605 This is safe because we don't have undo in this buffer. */
9606
9607 if (NATNUMP (Vmessage_log_max))
9608 {
9609 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9610 -XFASTINT (Vmessage_log_max) - 1, 0);
9611 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9612 }
9613 }
9614 BEGV = marker_position (oldbegv);
9615 BEGV_BYTE = marker_byte_position (oldbegv);
9616
9617 if (zv_at_end)
9618 {
9619 ZV = Z;
9620 ZV_BYTE = Z_BYTE;
9621 }
9622 else
9623 {
9624 ZV = marker_position (oldzv);
9625 ZV_BYTE = marker_byte_position (oldzv);
9626 }
9627
9628 if (point_at_end)
9629 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9630 else
9631 /* We can't do Fgoto_char (oldpoint) because it will run some
9632 Lisp code. */
9633 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9634 marker_byte_position (oldpoint));
9635
9636 UNGCPRO;
9637 unchain_marker (XMARKER (oldpoint));
9638 unchain_marker (XMARKER (oldbegv));
9639 unchain_marker (XMARKER (oldzv));
9640
9641 shown = buffer_window_count (current_buffer) > 0;
9642 set_buffer_internal (oldbuf);
9643 /* We called insert_1_both above with its 5th argument (PREPARE)
9644 zero, which prevents insert_1_both from calling
9645 prepare_to_modify_buffer, which in turns prevents us from
9646 incrementing windows_or_buffers_changed even if *Messages* is
9647 shown in some window. So we must manually incrementing
9648 windows_or_buffers_changed here to make up for that. */
9649 if (shown)
9650 windows_or_buffers_changed++;
9651 else
9652 windows_or_buffers_changed = old_windows_or_buffers_changed;
9653 message_log_need_newline = !nlflag;
9654 Vdeactivate_mark = old_deactivate_mark;
9655 }
9656 }
9657
9658
9659 /* We are at the end of the buffer after just having inserted a newline.
9660 (Note: We depend on the fact we won't be crossing the gap.)
9661 Check to see if the most recent message looks a lot like the previous one.
9662 Return 0 if different, 1 if the new one should just replace it, or a
9663 value N > 1 if we should also append " [N times]". */
9664
9665 static intmax_t
9666 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9667 {
9668 ptrdiff_t i;
9669 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9670 int seen_dots = 0;
9671 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9672 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9673
9674 for (i = 0; i < len; i++)
9675 {
9676 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
9677 seen_dots = 1;
9678 if (p1[i] != p2[i])
9679 return seen_dots;
9680 }
9681 p1 += len;
9682 if (*p1 == '\n')
9683 return 2;
9684 if (*p1++ == ' ' && *p1++ == '[')
9685 {
9686 char *pend;
9687 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9688 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9689 return n + 1;
9690 }
9691 return 0;
9692 }
9693 \f
9694
9695 /* Display an echo area message M with a specified length of NBYTES
9696 bytes. The string may include null characters. If M is not a
9697 string, clear out any existing message, and let the mini-buffer
9698 text show through.
9699
9700 This function cancels echoing. */
9701
9702 void
9703 message3 (Lisp_Object m)
9704 {
9705 struct gcpro gcpro1;
9706
9707 GCPRO1 (m);
9708 clear_message (1,1);
9709 cancel_echoing ();
9710
9711 /* First flush out any partial line written with print. */
9712 message_log_maybe_newline ();
9713 if (STRINGP (m))
9714 {
9715 ptrdiff_t nbytes = SBYTES (m);
9716 bool multibyte = STRING_MULTIBYTE (m);
9717 USE_SAFE_ALLOCA;
9718 char *buffer = SAFE_ALLOCA (nbytes);
9719 memcpy (buffer, SDATA (m), nbytes);
9720 message_dolog (buffer, nbytes, 1, multibyte);
9721 SAFE_FREE ();
9722 }
9723 message3_nolog (m);
9724
9725 UNGCPRO;
9726 }
9727
9728
9729 /* The non-logging version of message3.
9730 This does not cancel echoing, because it is used for echoing.
9731 Perhaps we need to make a separate function for echoing
9732 and make this cancel echoing. */
9733
9734 void
9735 message3_nolog (Lisp_Object m)
9736 {
9737 struct frame *sf = SELECTED_FRAME ();
9738
9739 if (FRAME_INITIAL_P (sf))
9740 {
9741 if (noninteractive_need_newline)
9742 putc ('\n', stderr);
9743 noninteractive_need_newline = 0;
9744 if (STRINGP (m))
9745 fwrite (SDATA (m), SBYTES (m), 1, stderr);
9746 if (cursor_in_echo_area == 0)
9747 fprintf (stderr, "\n");
9748 fflush (stderr);
9749 }
9750 /* Error messages get reported properly by cmd_error, so this must be just an
9751 informative message; if the frame hasn't really been initialized yet, just
9752 toss it. */
9753 else if (INTERACTIVE && sf->glyphs_initialized_p)
9754 {
9755 /* Get the frame containing the mini-buffer
9756 that the selected frame is using. */
9757 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
9758 Lisp_Object frame = XWINDOW (mini_window)->frame;
9759 struct frame *f = XFRAME (frame);
9760
9761 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
9762 Fmake_frame_visible (frame);
9763
9764 if (STRINGP (m) && SCHARS (m) > 0)
9765 {
9766 set_message (m);
9767 if (minibuffer_auto_raise)
9768 Fraise_frame (frame);
9769 /* Assume we are not echoing.
9770 (If we are, echo_now will override this.) */
9771 echo_message_buffer = Qnil;
9772 }
9773 else
9774 clear_message (1, 1);
9775
9776 do_pending_window_change (0);
9777 echo_area_display (1);
9778 do_pending_window_change (0);
9779 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9780 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9781 }
9782 }
9783
9784
9785 /* Display a null-terminated echo area message M. If M is 0, clear
9786 out any existing message, and let the mini-buffer text show through.
9787
9788 The buffer M must continue to exist until after the echo area gets
9789 cleared or some other message gets displayed there. Do not pass
9790 text that is stored in a Lisp string. Do not pass text in a buffer
9791 that was alloca'd. */
9792
9793 void
9794 message1 (const char *m)
9795 {
9796 message3 (m ? build_unibyte_string (m) : Qnil);
9797 }
9798
9799
9800 /* The non-logging counterpart of message1. */
9801
9802 void
9803 message1_nolog (const char *m)
9804 {
9805 message3_nolog (m ? build_unibyte_string (m) : Qnil);
9806 }
9807
9808 /* Display a message M which contains a single %s
9809 which gets replaced with STRING. */
9810
9811 void
9812 message_with_string (const char *m, Lisp_Object string, int log)
9813 {
9814 CHECK_STRING (string);
9815
9816 if (noninteractive)
9817 {
9818 if (m)
9819 {
9820 if (noninteractive_need_newline)
9821 putc ('\n', stderr);
9822 noninteractive_need_newline = 0;
9823 fprintf (stderr, m, SDATA (string));
9824 if (!cursor_in_echo_area)
9825 fprintf (stderr, "\n");
9826 fflush (stderr);
9827 }
9828 }
9829 else if (INTERACTIVE)
9830 {
9831 /* The frame whose minibuffer we're going to display the message on.
9832 It may be larger than the selected frame, so we need
9833 to use its buffer, not the selected frame's buffer. */
9834 Lisp_Object mini_window;
9835 struct frame *f, *sf = SELECTED_FRAME ();
9836
9837 /* Get the frame containing the minibuffer
9838 that the selected frame is using. */
9839 mini_window = FRAME_MINIBUF_WINDOW (sf);
9840 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9841
9842 /* Error messages get reported properly by cmd_error, so this must be
9843 just an informative message; if the frame hasn't really been
9844 initialized yet, just toss it. */
9845 if (f->glyphs_initialized_p)
9846 {
9847 Lisp_Object args[2], msg;
9848 struct gcpro gcpro1, gcpro2;
9849
9850 args[0] = build_string (m);
9851 args[1] = msg = string;
9852 GCPRO2 (args[0], msg);
9853 gcpro1.nvars = 2;
9854
9855 msg = Fformat (2, args);
9856
9857 if (log)
9858 message3 (msg);
9859 else
9860 message3_nolog (msg);
9861
9862 UNGCPRO;
9863
9864 /* Print should start at the beginning of the message
9865 buffer next time. */
9866 message_buf_print = 0;
9867 }
9868 }
9869 }
9870
9871
9872 /* Dump an informative message to the minibuf. If M is 0, clear out
9873 any existing message, and let the mini-buffer text show through. */
9874
9875 static void
9876 vmessage (const char *m, va_list ap)
9877 {
9878 if (noninteractive)
9879 {
9880 if (m)
9881 {
9882 if (noninteractive_need_newline)
9883 putc ('\n', stderr);
9884 noninteractive_need_newline = 0;
9885 vfprintf (stderr, m, ap);
9886 if (cursor_in_echo_area == 0)
9887 fprintf (stderr, "\n");
9888 fflush (stderr);
9889 }
9890 }
9891 else if (INTERACTIVE)
9892 {
9893 /* The frame whose mini-buffer we're going to display the message
9894 on. It may be larger than the selected frame, so we need to
9895 use its buffer, not the selected frame's buffer. */
9896 Lisp_Object mini_window;
9897 struct frame *f, *sf = SELECTED_FRAME ();
9898
9899 /* Get the frame containing the mini-buffer
9900 that the selected frame is using. */
9901 mini_window = FRAME_MINIBUF_WINDOW (sf);
9902 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9903
9904 /* Error messages get reported properly by cmd_error, so this must be
9905 just an informative message; if the frame hasn't really been
9906 initialized yet, just toss it. */
9907 if (f->glyphs_initialized_p)
9908 {
9909 if (m)
9910 {
9911 ptrdiff_t len;
9912 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
9913 char *message_buf = alloca (maxsize + 1);
9914
9915 len = doprnt (message_buf, maxsize, m, 0, ap);
9916
9917 message3 (make_string (message_buf, len));
9918 }
9919 else
9920 message1 (0);
9921
9922 /* Print should start at the beginning of the message
9923 buffer next time. */
9924 message_buf_print = 0;
9925 }
9926 }
9927 }
9928
9929 void
9930 message (const char *m, ...)
9931 {
9932 va_list ap;
9933 va_start (ap, m);
9934 vmessage (m, ap);
9935 va_end (ap);
9936 }
9937
9938
9939 #if 0
9940 /* The non-logging version of message. */
9941
9942 void
9943 message_nolog (const char *m, ...)
9944 {
9945 Lisp_Object old_log_max;
9946 va_list ap;
9947 va_start (ap, m);
9948 old_log_max = Vmessage_log_max;
9949 Vmessage_log_max = Qnil;
9950 vmessage (m, ap);
9951 Vmessage_log_max = old_log_max;
9952 va_end (ap);
9953 }
9954 #endif
9955
9956
9957 /* Display the current message in the current mini-buffer. This is
9958 only called from error handlers in process.c, and is not time
9959 critical. */
9960
9961 void
9962 update_echo_area (void)
9963 {
9964 if (!NILP (echo_area_buffer[0]))
9965 {
9966 Lisp_Object string;
9967 string = Fcurrent_message ();
9968 message3 (string);
9969 }
9970 }
9971
9972
9973 /* Make sure echo area buffers in `echo_buffers' are live.
9974 If they aren't, make new ones. */
9975
9976 static void
9977 ensure_echo_area_buffers (void)
9978 {
9979 int i;
9980
9981 for (i = 0; i < 2; ++i)
9982 if (!BUFFERP (echo_buffer[i])
9983 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
9984 {
9985 char name[30];
9986 Lisp_Object old_buffer;
9987 int j;
9988
9989 old_buffer = echo_buffer[i];
9990 echo_buffer[i] = Fget_buffer_create
9991 (make_formatted_string (name, " *Echo Area %d*", i));
9992 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
9993 /* to force word wrap in echo area -
9994 it was decided to postpone this*/
9995 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9996
9997 for (j = 0; j < 2; ++j)
9998 if (EQ (old_buffer, echo_area_buffer[j]))
9999 echo_area_buffer[j] = echo_buffer[i];
10000 }
10001 }
10002
10003
10004 /* Call FN with args A1..A2 with either the current or last displayed
10005 echo_area_buffer as current buffer.
10006
10007 WHICH zero means use the current message buffer
10008 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10009 from echo_buffer[] and clear it.
10010
10011 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10012 suitable buffer from echo_buffer[] and clear it.
10013
10014 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10015 that the current message becomes the last displayed one, make
10016 choose a suitable buffer for echo_area_buffer[0], and clear it.
10017
10018 Value is what FN returns. */
10019
10020 static int
10021 with_echo_area_buffer (struct window *w, int which,
10022 int (*fn) (ptrdiff_t, Lisp_Object),
10023 ptrdiff_t a1, Lisp_Object a2)
10024 {
10025 Lisp_Object buffer;
10026 int this_one, the_other, clear_buffer_p, rc;
10027 ptrdiff_t count = SPECPDL_INDEX ();
10028
10029 /* If buffers aren't live, make new ones. */
10030 ensure_echo_area_buffers ();
10031
10032 clear_buffer_p = 0;
10033
10034 if (which == 0)
10035 this_one = 0, the_other = 1;
10036 else if (which > 0)
10037 this_one = 1, the_other = 0;
10038 else
10039 {
10040 this_one = 0, the_other = 1;
10041 clear_buffer_p = 1;
10042
10043 /* We need a fresh one in case the current echo buffer equals
10044 the one containing the last displayed echo area message. */
10045 if (!NILP (echo_area_buffer[this_one])
10046 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10047 echo_area_buffer[this_one] = Qnil;
10048 }
10049
10050 /* Choose a suitable buffer from echo_buffer[] is we don't
10051 have one. */
10052 if (NILP (echo_area_buffer[this_one]))
10053 {
10054 echo_area_buffer[this_one]
10055 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10056 ? echo_buffer[the_other]
10057 : echo_buffer[this_one]);
10058 clear_buffer_p = 1;
10059 }
10060
10061 buffer = echo_area_buffer[this_one];
10062
10063 /* Don't get confused by reusing the buffer used for echoing
10064 for a different purpose. */
10065 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10066 cancel_echoing ();
10067
10068 record_unwind_protect (unwind_with_echo_area_buffer,
10069 with_echo_area_buffer_unwind_data (w));
10070
10071 /* Make the echo area buffer current. Note that for display
10072 purposes, it is not necessary that the displayed window's buffer
10073 == current_buffer, except for text property lookup. So, let's
10074 only set that buffer temporarily here without doing a full
10075 Fset_window_buffer. We must also change w->pointm, though,
10076 because otherwise an assertions in unshow_buffer fails, and Emacs
10077 aborts. */
10078 set_buffer_internal_1 (XBUFFER (buffer));
10079 if (w)
10080 {
10081 wset_buffer (w, buffer);
10082 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10083 }
10084
10085 bset_undo_list (current_buffer, Qt);
10086 bset_read_only (current_buffer, Qnil);
10087 specbind (Qinhibit_read_only, Qt);
10088 specbind (Qinhibit_modification_hooks, Qt);
10089
10090 if (clear_buffer_p && Z > BEG)
10091 del_range (BEG, Z);
10092
10093 eassert (BEGV >= BEG);
10094 eassert (ZV <= Z && ZV >= BEGV);
10095
10096 rc = fn (a1, a2);
10097
10098 eassert (BEGV >= BEG);
10099 eassert (ZV <= Z && ZV >= BEGV);
10100
10101 unbind_to (count, Qnil);
10102 return rc;
10103 }
10104
10105
10106 /* Save state that should be preserved around the call to the function
10107 FN called in with_echo_area_buffer. */
10108
10109 static Lisp_Object
10110 with_echo_area_buffer_unwind_data (struct window *w)
10111 {
10112 int i = 0;
10113 Lisp_Object vector, tmp;
10114
10115 /* Reduce consing by keeping one vector in
10116 Vwith_echo_area_save_vector. */
10117 vector = Vwith_echo_area_save_vector;
10118 Vwith_echo_area_save_vector = Qnil;
10119
10120 if (NILP (vector))
10121 vector = Fmake_vector (make_number (9), Qnil);
10122
10123 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10124 ASET (vector, i, Vdeactivate_mark); ++i;
10125 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10126
10127 if (w)
10128 {
10129 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10130 ASET (vector, i, w->contents); ++i;
10131 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10132 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10133 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10134 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10135 }
10136 else
10137 {
10138 int end = i + 6;
10139 for (; i < end; ++i)
10140 ASET (vector, i, Qnil);
10141 }
10142
10143 eassert (i == ASIZE (vector));
10144 return vector;
10145 }
10146
10147
10148 /* Restore global state from VECTOR which was created by
10149 with_echo_area_buffer_unwind_data. */
10150
10151 static void
10152 unwind_with_echo_area_buffer (Lisp_Object vector)
10153 {
10154 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10155 Vdeactivate_mark = AREF (vector, 1);
10156 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10157
10158 if (WINDOWP (AREF (vector, 3)))
10159 {
10160 struct window *w;
10161 Lisp_Object buffer;
10162
10163 w = XWINDOW (AREF (vector, 3));
10164 buffer = AREF (vector, 4);
10165
10166 wset_buffer (w, buffer);
10167 set_marker_both (w->pointm, buffer,
10168 XFASTINT (AREF (vector, 5)),
10169 XFASTINT (AREF (vector, 6)));
10170 set_marker_both (w->start, buffer,
10171 XFASTINT (AREF (vector, 7)),
10172 XFASTINT (AREF (vector, 8)));
10173 }
10174
10175 Vwith_echo_area_save_vector = vector;
10176 }
10177
10178
10179 /* Set up the echo area for use by print functions. MULTIBYTE_P
10180 non-zero means we will print multibyte. */
10181
10182 void
10183 setup_echo_area_for_printing (int multibyte_p)
10184 {
10185 /* If we can't find an echo area any more, exit. */
10186 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10187 Fkill_emacs (Qnil);
10188
10189 ensure_echo_area_buffers ();
10190
10191 if (!message_buf_print)
10192 {
10193 /* A message has been output since the last time we printed.
10194 Choose a fresh echo area buffer. */
10195 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10196 echo_area_buffer[0] = echo_buffer[1];
10197 else
10198 echo_area_buffer[0] = echo_buffer[0];
10199
10200 /* Switch to that buffer and clear it. */
10201 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10202 bset_truncate_lines (current_buffer, Qnil);
10203
10204 if (Z > BEG)
10205 {
10206 ptrdiff_t count = SPECPDL_INDEX ();
10207 specbind (Qinhibit_read_only, Qt);
10208 /* Note that undo recording is always disabled. */
10209 del_range (BEG, Z);
10210 unbind_to (count, Qnil);
10211 }
10212 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10213
10214 /* Set up the buffer for the multibyteness we need. */
10215 if (multibyte_p
10216 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10217 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10218
10219 /* Raise the frame containing the echo area. */
10220 if (minibuffer_auto_raise)
10221 {
10222 struct frame *sf = SELECTED_FRAME ();
10223 Lisp_Object mini_window;
10224 mini_window = FRAME_MINIBUF_WINDOW (sf);
10225 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10226 }
10227
10228 message_log_maybe_newline ();
10229 message_buf_print = 1;
10230 }
10231 else
10232 {
10233 if (NILP (echo_area_buffer[0]))
10234 {
10235 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10236 echo_area_buffer[0] = echo_buffer[1];
10237 else
10238 echo_area_buffer[0] = echo_buffer[0];
10239 }
10240
10241 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10242 {
10243 /* Someone switched buffers between print requests. */
10244 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10245 bset_truncate_lines (current_buffer, Qnil);
10246 }
10247 }
10248 }
10249
10250
10251 /* Display an echo area message in window W. Value is non-zero if W's
10252 height is changed. If display_last_displayed_message_p is
10253 non-zero, display the message that was last displayed, otherwise
10254 display the current message. */
10255
10256 static int
10257 display_echo_area (struct window *w)
10258 {
10259 int i, no_message_p, window_height_changed_p;
10260
10261 /* Temporarily disable garbage collections while displaying the echo
10262 area. This is done because a GC can print a message itself.
10263 That message would modify the echo area buffer's contents while a
10264 redisplay of the buffer is going on, and seriously confuse
10265 redisplay. */
10266 ptrdiff_t count = inhibit_garbage_collection ();
10267
10268 /* If there is no message, we must call display_echo_area_1
10269 nevertheless because it resizes the window. But we will have to
10270 reset the echo_area_buffer in question to nil at the end because
10271 with_echo_area_buffer will sets it to an empty buffer. */
10272 i = display_last_displayed_message_p ? 1 : 0;
10273 no_message_p = NILP (echo_area_buffer[i]);
10274
10275 window_height_changed_p
10276 = with_echo_area_buffer (w, display_last_displayed_message_p,
10277 display_echo_area_1,
10278 (intptr_t) w, Qnil);
10279
10280 if (no_message_p)
10281 echo_area_buffer[i] = Qnil;
10282
10283 unbind_to (count, Qnil);
10284 return window_height_changed_p;
10285 }
10286
10287
10288 /* Helper for display_echo_area. Display the current buffer which
10289 contains the current echo area message in window W, a mini-window,
10290 a pointer to which is passed in A1. A2..A4 are currently not used.
10291 Change the height of W so that all of the message is displayed.
10292 Value is non-zero if height of W was changed. */
10293
10294 static int
10295 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10296 {
10297 intptr_t i1 = a1;
10298 struct window *w = (struct window *) i1;
10299 Lisp_Object window;
10300 struct text_pos start;
10301 int window_height_changed_p = 0;
10302
10303 /* Do this before displaying, so that we have a large enough glyph
10304 matrix for the display. If we can't get enough space for the
10305 whole text, display the last N lines. That works by setting w->start. */
10306 window_height_changed_p = resize_mini_window (w, 0);
10307
10308 /* Use the starting position chosen by resize_mini_window. */
10309 SET_TEXT_POS_FROM_MARKER (start, w->start);
10310
10311 /* Display. */
10312 clear_glyph_matrix (w->desired_matrix);
10313 XSETWINDOW (window, w);
10314 try_window (window, start, 0);
10315
10316 return window_height_changed_p;
10317 }
10318
10319
10320 /* Resize the echo area window to exactly the size needed for the
10321 currently displayed message, if there is one. If a mini-buffer
10322 is active, don't shrink it. */
10323
10324 void
10325 resize_echo_area_exactly (void)
10326 {
10327 if (BUFFERP (echo_area_buffer[0])
10328 && WINDOWP (echo_area_window))
10329 {
10330 struct window *w = XWINDOW (echo_area_window);
10331 int resized_p;
10332 Lisp_Object resize_exactly;
10333
10334 if (minibuf_level == 0)
10335 resize_exactly = Qt;
10336 else
10337 resize_exactly = Qnil;
10338
10339 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10340 (intptr_t) w, resize_exactly);
10341 if (resized_p)
10342 {
10343 ++windows_or_buffers_changed;
10344 ++update_mode_lines;
10345 redisplay_internal ();
10346 }
10347 }
10348 }
10349
10350
10351 /* Callback function for with_echo_area_buffer, when used from
10352 resize_echo_area_exactly. A1 contains a pointer to the window to
10353 resize, EXACTLY non-nil means resize the mini-window exactly to the
10354 size of the text displayed. A3 and A4 are not used. Value is what
10355 resize_mini_window returns. */
10356
10357 static int
10358 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10359 {
10360 intptr_t i1 = a1;
10361 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10362 }
10363
10364
10365 /* Resize mini-window W to fit the size of its contents. EXACT_P
10366 means size the window exactly to the size needed. Otherwise, it's
10367 only enlarged until W's buffer is empty.
10368
10369 Set W->start to the right place to begin display. If the whole
10370 contents fit, start at the beginning. Otherwise, start so as
10371 to make the end of the contents appear. This is particularly
10372 important for y-or-n-p, but seems desirable generally.
10373
10374 Value is non-zero if the window height has been changed. */
10375
10376 int
10377 resize_mini_window (struct window *w, int exact_p)
10378 {
10379 struct frame *f = XFRAME (w->frame);
10380 int window_height_changed_p = 0;
10381
10382 eassert (MINI_WINDOW_P (w));
10383
10384 /* By default, start display at the beginning. */
10385 set_marker_both (w->start, w->contents,
10386 BUF_BEGV (XBUFFER (w->contents)),
10387 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10388
10389 /* Don't resize windows while redisplaying a window; it would
10390 confuse redisplay functions when the size of the window they are
10391 displaying changes from under them. Such a resizing can happen,
10392 for instance, when which-func prints a long message while
10393 we are running fontification-functions. We're running these
10394 functions with safe_call which binds inhibit-redisplay to t. */
10395 if (!NILP (Vinhibit_redisplay))
10396 return 0;
10397
10398 /* Nil means don't try to resize. */
10399 if (NILP (Vresize_mini_windows)
10400 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10401 return 0;
10402
10403 if (!FRAME_MINIBUF_ONLY_P (f))
10404 {
10405 struct it it;
10406 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10407 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10408 int height;
10409 EMACS_INT max_height;
10410 int unit = FRAME_LINE_HEIGHT (f);
10411 struct text_pos start;
10412 struct buffer *old_current_buffer = NULL;
10413
10414 if (current_buffer != XBUFFER (w->contents))
10415 {
10416 old_current_buffer = current_buffer;
10417 set_buffer_internal (XBUFFER (w->contents));
10418 }
10419
10420 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10421
10422 /* Compute the max. number of lines specified by the user. */
10423 if (FLOATP (Vmax_mini_window_height))
10424 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10425 else if (INTEGERP (Vmax_mini_window_height))
10426 max_height = XINT (Vmax_mini_window_height);
10427 else
10428 max_height = total_height / 4;
10429
10430 /* Correct that max. height if it's bogus. */
10431 max_height = clip_to_bounds (1, max_height, total_height);
10432
10433 /* Find out the height of the text in the window. */
10434 if (it.line_wrap == TRUNCATE)
10435 height = 1;
10436 else
10437 {
10438 last_height = 0;
10439 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10440 if (it.max_ascent == 0 && it.max_descent == 0)
10441 height = it.current_y + last_height;
10442 else
10443 height = it.current_y + it.max_ascent + it.max_descent;
10444 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10445 height = (height + unit - 1) / unit;
10446 }
10447
10448 /* Compute a suitable window start. */
10449 if (height > max_height)
10450 {
10451 height = max_height;
10452 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10453 move_it_vertically_backward (&it, (height - 1) * unit);
10454 start = it.current.pos;
10455 }
10456 else
10457 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10458 SET_MARKER_FROM_TEXT_POS (w->start, start);
10459
10460 if (EQ (Vresize_mini_windows, Qgrow_only))
10461 {
10462 /* Let it grow only, until we display an empty message, in which
10463 case the window shrinks again. */
10464 if (height > WINDOW_TOTAL_LINES (w))
10465 {
10466 int old_height = WINDOW_TOTAL_LINES (w);
10467
10468 FRAME_WINDOWS_FROZEN (f) = 1;
10469 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10470 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10471 }
10472 else if (height < WINDOW_TOTAL_LINES (w)
10473 && (exact_p || BEGV == ZV))
10474 {
10475 int old_height = WINDOW_TOTAL_LINES (w);
10476
10477 FRAME_WINDOWS_FROZEN (f) = 0;
10478 shrink_mini_window (w);
10479 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10480 }
10481 }
10482 else
10483 {
10484 /* Always resize to exact size needed. */
10485 if (height > WINDOW_TOTAL_LINES (w))
10486 {
10487 int old_height = WINDOW_TOTAL_LINES (w);
10488
10489 FRAME_WINDOWS_FROZEN (f) = 1;
10490 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10491 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10492 }
10493 else if (height < WINDOW_TOTAL_LINES (w))
10494 {
10495 int old_height = WINDOW_TOTAL_LINES (w);
10496
10497 FRAME_WINDOWS_FROZEN (f) = 0;
10498 shrink_mini_window (w);
10499
10500 if (height)
10501 {
10502 FRAME_WINDOWS_FROZEN (f) = 1;
10503 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10504 }
10505
10506 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10507 }
10508 }
10509
10510 if (old_current_buffer)
10511 set_buffer_internal (old_current_buffer);
10512 }
10513
10514 return window_height_changed_p;
10515 }
10516
10517
10518 /* Value is the current message, a string, or nil if there is no
10519 current message. */
10520
10521 Lisp_Object
10522 current_message (void)
10523 {
10524 Lisp_Object msg;
10525
10526 if (!BUFFERP (echo_area_buffer[0]))
10527 msg = Qnil;
10528 else
10529 {
10530 with_echo_area_buffer (0, 0, current_message_1,
10531 (intptr_t) &msg, Qnil);
10532 if (NILP (msg))
10533 echo_area_buffer[0] = Qnil;
10534 }
10535
10536 return msg;
10537 }
10538
10539
10540 static int
10541 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10542 {
10543 intptr_t i1 = a1;
10544 Lisp_Object *msg = (Lisp_Object *) i1;
10545
10546 if (Z > BEG)
10547 *msg = make_buffer_string (BEG, Z, 1);
10548 else
10549 *msg = Qnil;
10550 return 0;
10551 }
10552
10553
10554 /* Push the current message on Vmessage_stack for later restoration
10555 by restore_message. Value is non-zero if the current message isn't
10556 empty. This is a relatively infrequent operation, so it's not
10557 worth optimizing. */
10558
10559 bool
10560 push_message (void)
10561 {
10562 Lisp_Object msg = current_message ();
10563 Vmessage_stack = Fcons (msg, Vmessage_stack);
10564 return STRINGP (msg);
10565 }
10566
10567
10568 /* Restore message display from the top of Vmessage_stack. */
10569
10570 void
10571 restore_message (void)
10572 {
10573 eassert (CONSP (Vmessage_stack));
10574 message3_nolog (XCAR (Vmessage_stack));
10575 }
10576
10577
10578 /* Handler for unwind-protect calling pop_message. */
10579
10580 void
10581 pop_message_unwind (void)
10582 {
10583 /* Pop the top-most entry off Vmessage_stack. */
10584 eassert (CONSP (Vmessage_stack));
10585 Vmessage_stack = XCDR (Vmessage_stack);
10586 }
10587
10588
10589 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10590 exits. If the stack is not empty, we have a missing pop_message
10591 somewhere. */
10592
10593 void
10594 check_message_stack (void)
10595 {
10596 if (!NILP (Vmessage_stack))
10597 emacs_abort ();
10598 }
10599
10600
10601 /* Truncate to NCHARS what will be displayed in the echo area the next
10602 time we display it---but don't redisplay it now. */
10603
10604 void
10605 truncate_echo_area (ptrdiff_t nchars)
10606 {
10607 if (nchars == 0)
10608 echo_area_buffer[0] = Qnil;
10609 else if (!noninteractive
10610 && INTERACTIVE
10611 && !NILP (echo_area_buffer[0]))
10612 {
10613 struct frame *sf = SELECTED_FRAME ();
10614 /* Error messages get reported properly by cmd_error, so this must be
10615 just an informative message; if the frame hasn't really been
10616 initialized yet, just toss it. */
10617 if (sf->glyphs_initialized_p)
10618 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10619 }
10620 }
10621
10622
10623 /* Helper function for truncate_echo_area. Truncate the current
10624 message to at most NCHARS characters. */
10625
10626 static int
10627 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10628 {
10629 if (BEG + nchars < Z)
10630 del_range (BEG + nchars, Z);
10631 if (Z == BEG)
10632 echo_area_buffer[0] = Qnil;
10633 return 0;
10634 }
10635
10636 /* Set the current message to STRING. */
10637
10638 static void
10639 set_message (Lisp_Object string)
10640 {
10641 eassert (STRINGP (string));
10642
10643 message_enable_multibyte = STRING_MULTIBYTE (string);
10644
10645 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10646 message_buf_print = 0;
10647 help_echo_showing_p = 0;
10648
10649 if (STRINGP (Vdebug_on_message)
10650 && STRINGP (string)
10651 && fast_string_match (Vdebug_on_message, string) >= 0)
10652 call_debugger (list2 (Qerror, string));
10653 }
10654
10655
10656 /* Helper function for set_message. First argument is ignored and second
10657 argument has the same meaning as for set_message.
10658 This function is called with the echo area buffer being current. */
10659
10660 static int
10661 set_message_1 (ptrdiff_t a1, Lisp_Object string)
10662 {
10663 eassert (STRINGP (string));
10664
10665 /* Change multibyteness of the echo buffer appropriately. */
10666 if (message_enable_multibyte
10667 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10668 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10669
10670 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10671 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10672 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10673
10674 /* Insert new message at BEG. */
10675 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10676
10677 /* This function takes care of single/multibyte conversion.
10678 We just have to ensure that the echo area buffer has the right
10679 setting of enable_multibyte_characters. */
10680 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
10681
10682 return 0;
10683 }
10684
10685
10686 /* Clear messages. CURRENT_P non-zero means clear the current
10687 message. LAST_DISPLAYED_P non-zero means clear the message
10688 last displayed. */
10689
10690 void
10691 clear_message (int current_p, int last_displayed_p)
10692 {
10693 if (current_p)
10694 {
10695 echo_area_buffer[0] = Qnil;
10696 message_cleared_p = 1;
10697 }
10698
10699 if (last_displayed_p)
10700 echo_area_buffer[1] = Qnil;
10701
10702 message_buf_print = 0;
10703 }
10704
10705 /* Clear garbaged frames.
10706
10707 This function is used where the old redisplay called
10708 redraw_garbaged_frames which in turn called redraw_frame which in
10709 turn called clear_frame. The call to clear_frame was a source of
10710 flickering. I believe a clear_frame is not necessary. It should
10711 suffice in the new redisplay to invalidate all current matrices,
10712 and ensure a complete redisplay of all windows. */
10713
10714 static void
10715 clear_garbaged_frames (void)
10716 {
10717 if (frame_garbaged)
10718 {
10719 Lisp_Object tail, frame;
10720 int changed_count = 0;
10721
10722 FOR_EACH_FRAME (tail, frame)
10723 {
10724 struct frame *f = XFRAME (frame);
10725
10726 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10727 {
10728 if (f->resized_p)
10729 {
10730 redraw_frame (f);
10731 f->force_flush_display_p = 1;
10732 }
10733 clear_current_matrices (f);
10734 changed_count++;
10735 f->garbaged = 0;
10736 f->resized_p = 0;
10737 }
10738 }
10739
10740 frame_garbaged = 0;
10741 if (changed_count)
10742 ++windows_or_buffers_changed;
10743 }
10744 }
10745
10746
10747 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10748 is non-zero update selected_frame. Value is non-zero if the
10749 mini-windows height has been changed. */
10750
10751 static int
10752 echo_area_display (int update_frame_p)
10753 {
10754 Lisp_Object mini_window;
10755 struct window *w;
10756 struct frame *f;
10757 int window_height_changed_p = 0;
10758 struct frame *sf = SELECTED_FRAME ();
10759
10760 mini_window = FRAME_MINIBUF_WINDOW (sf);
10761 w = XWINDOW (mini_window);
10762 f = XFRAME (WINDOW_FRAME (w));
10763
10764 /* Don't display if frame is invisible or not yet initialized. */
10765 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10766 return 0;
10767
10768 #ifdef HAVE_WINDOW_SYSTEM
10769 /* When Emacs starts, selected_frame may be the initial terminal
10770 frame. If we let this through, a message would be displayed on
10771 the terminal. */
10772 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10773 return 0;
10774 #endif /* HAVE_WINDOW_SYSTEM */
10775
10776 /* Redraw garbaged frames. */
10777 clear_garbaged_frames ();
10778
10779 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10780 {
10781 echo_area_window = mini_window;
10782 window_height_changed_p = display_echo_area (w);
10783 w->must_be_updated_p = 1;
10784
10785 /* Update the display, unless called from redisplay_internal.
10786 Also don't update the screen during redisplay itself. The
10787 update will happen at the end of redisplay, and an update
10788 here could cause confusion. */
10789 if (update_frame_p && !redisplaying_p)
10790 {
10791 int n = 0;
10792
10793 /* If the display update has been interrupted by pending
10794 input, update mode lines in the frame. Due to the
10795 pending input, it might have been that redisplay hasn't
10796 been called, so that mode lines above the echo area are
10797 garbaged. This looks odd, so we prevent it here. */
10798 if (!display_completed)
10799 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10800
10801 if (window_height_changed_p
10802 /* Don't do this if Emacs is shutting down. Redisplay
10803 needs to run hooks. */
10804 && !NILP (Vrun_hooks))
10805 {
10806 /* Must update other windows. Likewise as in other
10807 cases, don't let this update be interrupted by
10808 pending input. */
10809 ptrdiff_t count = SPECPDL_INDEX ();
10810 specbind (Qredisplay_dont_pause, Qt);
10811 windows_or_buffers_changed = 1;
10812 redisplay_internal ();
10813 unbind_to (count, Qnil);
10814 }
10815 else if (FRAME_WINDOW_P (f) && n == 0)
10816 {
10817 /* Window configuration is the same as before.
10818 Can do with a display update of the echo area,
10819 unless we displayed some mode lines. */
10820 update_single_window (w, 1);
10821 FRAME_RIF (f)->flush_display (f);
10822 }
10823 else
10824 update_frame (f, 1, 1);
10825
10826 /* If cursor is in the echo area, make sure that the next
10827 redisplay displays the minibuffer, so that the cursor will
10828 be replaced with what the minibuffer wants. */
10829 if (cursor_in_echo_area)
10830 ++windows_or_buffers_changed;
10831 }
10832 }
10833 else if (!EQ (mini_window, selected_window))
10834 windows_or_buffers_changed++;
10835
10836 /* Last displayed message is now the current message. */
10837 echo_area_buffer[1] = echo_area_buffer[0];
10838 /* Inform read_char that we're not echoing. */
10839 echo_message_buffer = Qnil;
10840
10841 /* Prevent redisplay optimization in redisplay_internal by resetting
10842 this_line_start_pos. This is done because the mini-buffer now
10843 displays the message instead of its buffer text. */
10844 if (EQ (mini_window, selected_window))
10845 CHARPOS (this_line_start_pos) = 0;
10846
10847 return window_height_changed_p;
10848 }
10849
10850 /* Nonzero if the current window's buffer is shown in more than one
10851 window and was modified since last redisplay. */
10852
10853 static int
10854 buffer_shared_and_changed (void)
10855 {
10856 return (buffer_window_count (current_buffer) > 1
10857 && UNCHANGED_MODIFIED < MODIFF);
10858 }
10859
10860 /* Nonzero if W's buffer was changed but not saved or Transient Mark mode
10861 is enabled and mark of W's buffer was changed since last W's update. */
10862
10863 static int
10864 window_buffer_changed (struct window *w)
10865 {
10866 struct buffer *b = XBUFFER (w->contents);
10867
10868 eassert (BUFFER_LIVE_P (b));
10869
10870 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star)
10871 || ((!NILP (Vtransient_mark_mode) && !NILP (BVAR (b, mark_active)))
10872 != (w->region_showing != 0)));
10873 }
10874
10875 /* Nonzero if W has %c in its mode line and mode line should be updated. */
10876
10877 static int
10878 mode_line_update_needed (struct window *w)
10879 {
10880 return (w->column_number_displayed != -1
10881 && !(PT == w->last_point && !window_outdated (w))
10882 && (w->column_number_displayed != current_column ()));
10883 }
10884
10885 /* Nonzero if window start of W is frozen and may not be changed during
10886 redisplay. */
10887
10888 static bool
10889 window_frozen_p (struct window *w)
10890 {
10891 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
10892 {
10893 Lisp_Object window;
10894
10895 XSETWINDOW (window, w);
10896 if (MINI_WINDOW_P (w))
10897 return 0;
10898 else if (EQ (window, selected_window))
10899 return 0;
10900 else if (MINI_WINDOW_P (XWINDOW (selected_window))
10901 && EQ (window, Vminibuf_scroll_window))
10902 /* This special window can't be frozen too. */
10903 return 0;
10904 else
10905 return 1;
10906 }
10907 return 0;
10908 }
10909
10910 /***********************************************************************
10911 Mode Lines and Frame Titles
10912 ***********************************************************************/
10913
10914 /* A buffer for constructing non-propertized mode-line strings and
10915 frame titles in it; allocated from the heap in init_xdisp and
10916 resized as needed in store_mode_line_noprop_char. */
10917
10918 static char *mode_line_noprop_buf;
10919
10920 /* The buffer's end, and a current output position in it. */
10921
10922 static char *mode_line_noprop_buf_end;
10923 static char *mode_line_noprop_ptr;
10924
10925 #define MODE_LINE_NOPROP_LEN(start) \
10926 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10927
10928 static enum {
10929 MODE_LINE_DISPLAY = 0,
10930 MODE_LINE_TITLE,
10931 MODE_LINE_NOPROP,
10932 MODE_LINE_STRING
10933 } mode_line_target;
10934
10935 /* Alist that caches the results of :propertize.
10936 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10937 static Lisp_Object mode_line_proptrans_alist;
10938
10939 /* List of strings making up the mode-line. */
10940 static Lisp_Object mode_line_string_list;
10941
10942 /* Base face property when building propertized mode line string. */
10943 static Lisp_Object mode_line_string_face;
10944 static Lisp_Object mode_line_string_face_prop;
10945
10946
10947 /* Unwind data for mode line strings */
10948
10949 static Lisp_Object Vmode_line_unwind_vector;
10950
10951 static Lisp_Object
10952 format_mode_line_unwind_data (struct frame *target_frame,
10953 struct buffer *obuf,
10954 Lisp_Object owin,
10955 int save_proptrans)
10956 {
10957 Lisp_Object vector, tmp;
10958
10959 /* Reduce consing by keeping one vector in
10960 Vwith_echo_area_save_vector. */
10961 vector = Vmode_line_unwind_vector;
10962 Vmode_line_unwind_vector = Qnil;
10963
10964 if (NILP (vector))
10965 vector = Fmake_vector (make_number (10), Qnil);
10966
10967 ASET (vector, 0, make_number (mode_line_target));
10968 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10969 ASET (vector, 2, mode_line_string_list);
10970 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10971 ASET (vector, 4, mode_line_string_face);
10972 ASET (vector, 5, mode_line_string_face_prop);
10973
10974 if (obuf)
10975 XSETBUFFER (tmp, obuf);
10976 else
10977 tmp = Qnil;
10978 ASET (vector, 6, tmp);
10979 ASET (vector, 7, owin);
10980 if (target_frame)
10981 {
10982 /* Similarly to `with-selected-window', if the operation selects
10983 a window on another frame, we must restore that frame's
10984 selected window, and (for a tty) the top-frame. */
10985 ASET (vector, 8, target_frame->selected_window);
10986 if (FRAME_TERMCAP_P (target_frame))
10987 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
10988 }
10989
10990 return vector;
10991 }
10992
10993 static void
10994 unwind_format_mode_line (Lisp_Object vector)
10995 {
10996 Lisp_Object old_window = AREF (vector, 7);
10997 Lisp_Object target_frame_window = AREF (vector, 8);
10998 Lisp_Object old_top_frame = AREF (vector, 9);
10999
11000 mode_line_target = XINT (AREF (vector, 0));
11001 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11002 mode_line_string_list = AREF (vector, 2);
11003 if (! EQ (AREF (vector, 3), Qt))
11004 mode_line_proptrans_alist = AREF (vector, 3);
11005 mode_line_string_face = AREF (vector, 4);
11006 mode_line_string_face_prop = AREF (vector, 5);
11007
11008 /* Select window before buffer, since it may change the buffer. */
11009 if (!NILP (old_window))
11010 {
11011 /* If the operation that we are unwinding had selected a window
11012 on a different frame, reset its frame-selected-window. For a
11013 text terminal, reset its top-frame if necessary. */
11014 if (!NILP (target_frame_window))
11015 {
11016 Lisp_Object frame
11017 = WINDOW_FRAME (XWINDOW (target_frame_window));
11018
11019 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11020 Fselect_window (target_frame_window, Qt);
11021
11022 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11023 Fselect_frame (old_top_frame, Qt);
11024 }
11025
11026 Fselect_window (old_window, Qt);
11027 }
11028
11029 if (!NILP (AREF (vector, 6)))
11030 {
11031 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11032 ASET (vector, 6, Qnil);
11033 }
11034
11035 Vmode_line_unwind_vector = vector;
11036 }
11037
11038
11039 /* Store a single character C for the frame title in mode_line_noprop_buf.
11040 Re-allocate mode_line_noprop_buf if necessary. */
11041
11042 static void
11043 store_mode_line_noprop_char (char c)
11044 {
11045 /* If output position has reached the end of the allocated buffer,
11046 increase the buffer's size. */
11047 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11048 {
11049 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11050 ptrdiff_t size = len;
11051 mode_line_noprop_buf =
11052 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11053 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11054 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11055 }
11056
11057 *mode_line_noprop_ptr++ = c;
11058 }
11059
11060
11061 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11062 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11063 characters that yield more columns than PRECISION; PRECISION <= 0
11064 means copy the whole string. Pad with spaces until FIELD_WIDTH
11065 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11066 pad. Called from display_mode_element when it is used to build a
11067 frame title. */
11068
11069 static int
11070 store_mode_line_noprop (const char *string, int field_width, int precision)
11071 {
11072 const unsigned char *str = (const unsigned char *) string;
11073 int n = 0;
11074 ptrdiff_t dummy, nbytes;
11075
11076 /* Copy at most PRECISION chars from STR. */
11077 nbytes = strlen (string);
11078 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11079 while (nbytes--)
11080 store_mode_line_noprop_char (*str++);
11081
11082 /* Fill up with spaces until FIELD_WIDTH reached. */
11083 while (field_width > 0
11084 && n < field_width)
11085 {
11086 store_mode_line_noprop_char (' ');
11087 ++n;
11088 }
11089
11090 return n;
11091 }
11092
11093 /***********************************************************************
11094 Frame Titles
11095 ***********************************************************************/
11096
11097 #ifdef HAVE_WINDOW_SYSTEM
11098
11099 /* Set the title of FRAME, if it has changed. The title format is
11100 Vicon_title_format if FRAME is iconified, otherwise it is
11101 frame_title_format. */
11102
11103 static void
11104 x_consider_frame_title (Lisp_Object frame)
11105 {
11106 struct frame *f = XFRAME (frame);
11107
11108 if (FRAME_WINDOW_P (f)
11109 || FRAME_MINIBUF_ONLY_P (f)
11110 || f->explicit_name)
11111 {
11112 /* Do we have more than one visible frame on this X display? */
11113 Lisp_Object tail, other_frame, fmt;
11114 ptrdiff_t title_start;
11115 char *title;
11116 ptrdiff_t len;
11117 struct it it;
11118 ptrdiff_t count = SPECPDL_INDEX ();
11119
11120 FOR_EACH_FRAME (tail, other_frame)
11121 {
11122 struct frame *tf = XFRAME (other_frame);
11123
11124 if (tf != f
11125 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11126 && !FRAME_MINIBUF_ONLY_P (tf)
11127 && !EQ (other_frame, tip_frame)
11128 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11129 break;
11130 }
11131
11132 /* Set global variable indicating that multiple frames exist. */
11133 multiple_frames = CONSP (tail);
11134
11135 /* Switch to the buffer of selected window of the frame. Set up
11136 mode_line_target so that display_mode_element will output into
11137 mode_line_noprop_buf; then display the title. */
11138 record_unwind_protect (unwind_format_mode_line,
11139 format_mode_line_unwind_data
11140 (f, current_buffer, selected_window, 0));
11141
11142 Fselect_window (f->selected_window, Qt);
11143 set_buffer_internal_1
11144 (XBUFFER (XWINDOW (f->selected_window)->contents));
11145 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11146
11147 mode_line_target = MODE_LINE_TITLE;
11148 title_start = MODE_LINE_NOPROP_LEN (0);
11149 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11150 NULL, DEFAULT_FACE_ID);
11151 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11152 len = MODE_LINE_NOPROP_LEN (title_start);
11153 title = mode_line_noprop_buf + title_start;
11154 unbind_to (count, Qnil);
11155
11156 /* Set the title only if it's changed. This avoids consing in
11157 the common case where it hasn't. (If it turns out that we've
11158 already wasted too much time by walking through the list with
11159 display_mode_element, then we might need to optimize at a
11160 higher level than this.) */
11161 if (! STRINGP (f->name)
11162 || SBYTES (f->name) != len
11163 || memcmp (title, SDATA (f->name), len) != 0)
11164 x_implicitly_set_name (f, make_string (title, len), Qnil);
11165 }
11166 }
11167
11168 #endif /* not HAVE_WINDOW_SYSTEM */
11169
11170 \f
11171 /***********************************************************************
11172 Menu Bars
11173 ***********************************************************************/
11174
11175
11176 /* Prepare for redisplay by updating menu-bar item lists when
11177 appropriate. This can call eval. */
11178
11179 void
11180 prepare_menu_bars (void)
11181 {
11182 int all_windows;
11183 struct gcpro gcpro1, gcpro2;
11184 struct frame *f;
11185 Lisp_Object tooltip_frame;
11186
11187 #ifdef HAVE_WINDOW_SYSTEM
11188 tooltip_frame = tip_frame;
11189 #else
11190 tooltip_frame = Qnil;
11191 #endif
11192
11193 /* Update all frame titles based on their buffer names, etc. We do
11194 this before the menu bars so that the buffer-menu will show the
11195 up-to-date frame titles. */
11196 #ifdef HAVE_WINDOW_SYSTEM
11197 if (windows_or_buffers_changed || update_mode_lines)
11198 {
11199 Lisp_Object tail, frame;
11200
11201 FOR_EACH_FRAME (tail, frame)
11202 {
11203 f = XFRAME (frame);
11204 if (!EQ (frame, tooltip_frame)
11205 && (FRAME_ICONIFIED_P (f)
11206 || FRAME_VISIBLE_P (f) == 1
11207 /* Exclude TTY frames that are obscured because they
11208 are not the top frame on their console. This is
11209 because x_consider_frame_title actually switches
11210 to the frame, which for TTY frames means it is
11211 marked as garbaged, and will be completely
11212 redrawn on the next redisplay cycle. This causes
11213 TTY frames to be completely redrawn, when there
11214 are more than one of them, even though nothing
11215 should be changed on display. */
11216 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11217 x_consider_frame_title (frame);
11218 }
11219 }
11220 #endif /* HAVE_WINDOW_SYSTEM */
11221
11222 /* Update the menu bar item lists, if appropriate. This has to be
11223 done before any actual redisplay or generation of display lines. */
11224 all_windows = (update_mode_lines
11225 || buffer_shared_and_changed ()
11226 || windows_or_buffers_changed);
11227 if (all_windows)
11228 {
11229 Lisp_Object tail, frame;
11230 ptrdiff_t count = SPECPDL_INDEX ();
11231 /* 1 means that update_menu_bar has run its hooks
11232 so any further calls to update_menu_bar shouldn't do so again. */
11233 int menu_bar_hooks_run = 0;
11234
11235 record_unwind_save_match_data ();
11236
11237 FOR_EACH_FRAME (tail, frame)
11238 {
11239 f = XFRAME (frame);
11240
11241 /* Ignore tooltip frame. */
11242 if (EQ (frame, tooltip_frame))
11243 continue;
11244
11245 /* If a window on this frame changed size, report that to
11246 the user and clear the size-change flag. */
11247 if (FRAME_WINDOW_SIZES_CHANGED (f))
11248 {
11249 Lisp_Object functions;
11250
11251 /* Clear flag first in case we get an error below. */
11252 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11253 functions = Vwindow_size_change_functions;
11254 GCPRO2 (tail, functions);
11255
11256 while (CONSP (functions))
11257 {
11258 if (!EQ (XCAR (functions), Qt))
11259 call1 (XCAR (functions), frame);
11260 functions = XCDR (functions);
11261 }
11262 UNGCPRO;
11263 }
11264
11265 GCPRO1 (tail);
11266 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11267 #ifdef HAVE_WINDOW_SYSTEM
11268 update_tool_bar (f, 0);
11269 #endif
11270 #ifdef HAVE_NS
11271 if (windows_or_buffers_changed
11272 && FRAME_NS_P (f))
11273 ns_set_doc_edited
11274 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->contents));
11275 #endif
11276 UNGCPRO;
11277 }
11278
11279 unbind_to (count, Qnil);
11280 }
11281 else
11282 {
11283 struct frame *sf = SELECTED_FRAME ();
11284 update_menu_bar (sf, 1, 0);
11285 #ifdef HAVE_WINDOW_SYSTEM
11286 update_tool_bar (sf, 1);
11287 #endif
11288 }
11289 }
11290
11291
11292 /* Update the menu bar item list for frame F. This has to be done
11293 before we start to fill in any display lines, because it can call
11294 eval.
11295
11296 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11297
11298 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11299 already ran the menu bar hooks for this redisplay, so there
11300 is no need to run them again. The return value is the
11301 updated value of this flag, to pass to the next call. */
11302
11303 static int
11304 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11305 {
11306 Lisp_Object window;
11307 register struct window *w;
11308
11309 /* If called recursively during a menu update, do nothing. This can
11310 happen when, for instance, an activate-menubar-hook causes a
11311 redisplay. */
11312 if (inhibit_menubar_update)
11313 return hooks_run;
11314
11315 window = FRAME_SELECTED_WINDOW (f);
11316 w = XWINDOW (window);
11317
11318 if (FRAME_WINDOW_P (f)
11319 ?
11320 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11321 || defined (HAVE_NS) || defined (USE_GTK)
11322 FRAME_EXTERNAL_MENU_BAR (f)
11323 #else
11324 FRAME_MENU_BAR_LINES (f) > 0
11325 #endif
11326 : FRAME_MENU_BAR_LINES (f) > 0)
11327 {
11328 /* If the user has switched buffers or windows, we need to
11329 recompute to reflect the new bindings. But we'll
11330 recompute when update_mode_lines is set too; that means
11331 that people can use force-mode-line-update to request
11332 that the menu bar be recomputed. The adverse effect on
11333 the rest of the redisplay algorithm is about the same as
11334 windows_or_buffers_changed anyway. */
11335 if (windows_or_buffers_changed
11336 /* This used to test w->update_mode_line, but we believe
11337 there is no need to recompute the menu in that case. */
11338 || update_mode_lines
11339 || window_buffer_changed (w))
11340 {
11341 struct buffer *prev = current_buffer;
11342 ptrdiff_t count = SPECPDL_INDEX ();
11343
11344 specbind (Qinhibit_menubar_update, Qt);
11345
11346 set_buffer_internal_1 (XBUFFER (w->contents));
11347 if (save_match_data)
11348 record_unwind_save_match_data ();
11349 if (NILP (Voverriding_local_map_menu_flag))
11350 {
11351 specbind (Qoverriding_terminal_local_map, Qnil);
11352 specbind (Qoverriding_local_map, Qnil);
11353 }
11354
11355 if (!hooks_run)
11356 {
11357 /* Run the Lucid hook. */
11358 safe_run_hooks (Qactivate_menubar_hook);
11359
11360 /* If it has changed current-menubar from previous value,
11361 really recompute the menu-bar from the value. */
11362 if (! NILP (Vlucid_menu_bar_dirty_flag))
11363 call0 (Qrecompute_lucid_menubar);
11364
11365 safe_run_hooks (Qmenu_bar_update_hook);
11366
11367 hooks_run = 1;
11368 }
11369
11370 XSETFRAME (Vmenu_updating_frame, f);
11371 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11372
11373 /* Redisplay the menu bar in case we changed it. */
11374 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11375 || defined (HAVE_NS) || defined (USE_GTK)
11376 if (FRAME_WINDOW_P (f))
11377 {
11378 #if defined (HAVE_NS)
11379 /* All frames on Mac OS share the same menubar. So only
11380 the selected frame should be allowed to set it. */
11381 if (f == SELECTED_FRAME ())
11382 #endif
11383 set_frame_menubar (f, 0, 0);
11384 }
11385 else
11386 /* On a terminal screen, the menu bar is an ordinary screen
11387 line, and this makes it get updated. */
11388 w->update_mode_line = 1;
11389 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11390 /* In the non-toolkit version, the menu bar is an ordinary screen
11391 line, and this makes it get updated. */
11392 w->update_mode_line = 1;
11393 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11394
11395 unbind_to (count, Qnil);
11396 set_buffer_internal_1 (prev);
11397 }
11398 }
11399
11400 return hooks_run;
11401 }
11402
11403
11404 \f
11405 /***********************************************************************
11406 Output Cursor
11407 ***********************************************************************/
11408
11409 #ifdef HAVE_WINDOW_SYSTEM
11410
11411 /* EXPORT:
11412 Nominal cursor position -- where to draw output.
11413 HPOS and VPOS are window relative glyph matrix coordinates.
11414 X and Y are window relative pixel coordinates. */
11415
11416 struct cursor_pos output_cursor;
11417
11418
11419 /* EXPORT:
11420 Set the global variable output_cursor to CURSOR. All cursor
11421 positions are relative to currently updated window. */
11422
11423 void
11424 set_output_cursor (struct cursor_pos *cursor)
11425 {
11426 output_cursor.hpos = cursor->hpos;
11427 output_cursor.vpos = cursor->vpos;
11428 output_cursor.x = cursor->x;
11429 output_cursor.y = cursor->y;
11430 }
11431
11432
11433 /* EXPORT for RIF:
11434 Set a nominal cursor position.
11435
11436 HPOS and VPOS are column/row positions in a window glyph matrix.
11437 X and Y are window text area relative pixel positions.
11438
11439 This is always done during window update, so the position is the
11440 future output cursor position for currently updated window W.
11441 NOTE: W is used only to check whether this function is called
11442 in a consistent manner via the redisplay interface. */
11443
11444 void
11445 x_cursor_to (struct window *w, int vpos, int hpos, int y, int x)
11446 {
11447 eassert (w);
11448
11449 /* Set the output cursor. */
11450 output_cursor.hpos = hpos;
11451 output_cursor.vpos = vpos;
11452 output_cursor.x = x;
11453 output_cursor.y = y;
11454 }
11455
11456 #endif /* HAVE_WINDOW_SYSTEM */
11457
11458 \f
11459 /***********************************************************************
11460 Tool-bars
11461 ***********************************************************************/
11462
11463 #ifdef HAVE_WINDOW_SYSTEM
11464
11465 /* Where the mouse was last time we reported a mouse event. */
11466
11467 struct frame *last_mouse_frame;
11468
11469 /* Tool-bar item index of the item on which a mouse button was pressed
11470 or -1. */
11471
11472 int last_tool_bar_item;
11473
11474 /* Select `frame' temporarily without running all the code in
11475 do_switch_frame.
11476 FIXME: Maybe do_switch_frame should be trimmed down similarly
11477 when `norecord' is set. */
11478 static void
11479 fast_set_selected_frame (Lisp_Object frame)
11480 {
11481 if (!EQ (selected_frame, frame))
11482 {
11483 selected_frame = frame;
11484 selected_window = XFRAME (frame)->selected_window;
11485 }
11486 }
11487
11488 /* Update the tool-bar item list for frame F. This has to be done
11489 before we start to fill in any display lines. Called from
11490 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11491 and restore it here. */
11492
11493 static void
11494 update_tool_bar (struct frame *f, int save_match_data)
11495 {
11496 #if defined (USE_GTK) || defined (HAVE_NS)
11497 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11498 #else
11499 int do_update = WINDOWP (f->tool_bar_window)
11500 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11501 #endif
11502
11503 if (do_update)
11504 {
11505 Lisp_Object window;
11506 struct window *w;
11507
11508 window = FRAME_SELECTED_WINDOW (f);
11509 w = XWINDOW (window);
11510
11511 /* If the user has switched buffers or windows, we need to
11512 recompute to reflect the new bindings. But we'll
11513 recompute when update_mode_lines is set too; that means
11514 that people can use force-mode-line-update to request
11515 that the menu bar be recomputed. The adverse effect on
11516 the rest of the redisplay algorithm is about the same as
11517 windows_or_buffers_changed anyway. */
11518 if (windows_or_buffers_changed
11519 || w->update_mode_line
11520 || update_mode_lines
11521 || window_buffer_changed (w))
11522 {
11523 struct buffer *prev = current_buffer;
11524 ptrdiff_t count = SPECPDL_INDEX ();
11525 Lisp_Object frame, new_tool_bar;
11526 int new_n_tool_bar;
11527 struct gcpro gcpro1;
11528
11529 /* Set current_buffer to the buffer of the selected
11530 window of the frame, so that we get the right local
11531 keymaps. */
11532 set_buffer_internal_1 (XBUFFER (w->contents));
11533
11534 /* Save match data, if we must. */
11535 if (save_match_data)
11536 record_unwind_save_match_data ();
11537
11538 /* Make sure that we don't accidentally use bogus keymaps. */
11539 if (NILP (Voverriding_local_map_menu_flag))
11540 {
11541 specbind (Qoverriding_terminal_local_map, Qnil);
11542 specbind (Qoverriding_local_map, Qnil);
11543 }
11544
11545 GCPRO1 (new_tool_bar);
11546
11547 /* We must temporarily set the selected frame to this frame
11548 before calling tool_bar_items, because the calculation of
11549 the tool-bar keymap uses the selected frame (see
11550 `tool-bar-make-keymap' in tool-bar.el). */
11551 eassert (EQ (selected_window,
11552 /* Since we only explicitly preserve selected_frame,
11553 check that selected_window would be redundant. */
11554 XFRAME (selected_frame)->selected_window));
11555 record_unwind_protect (fast_set_selected_frame, selected_frame);
11556 XSETFRAME (frame, f);
11557 fast_set_selected_frame (frame);
11558
11559 /* Build desired tool-bar items from keymaps. */
11560 new_tool_bar
11561 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11562 &new_n_tool_bar);
11563
11564 /* Redisplay the tool-bar if we changed it. */
11565 if (new_n_tool_bar != f->n_tool_bar_items
11566 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11567 {
11568 /* Redisplay that happens asynchronously due to an expose event
11569 may access f->tool_bar_items. Make sure we update both
11570 variables within BLOCK_INPUT so no such event interrupts. */
11571 block_input ();
11572 fset_tool_bar_items (f, new_tool_bar);
11573 f->n_tool_bar_items = new_n_tool_bar;
11574 w->update_mode_line = 1;
11575 unblock_input ();
11576 }
11577
11578 UNGCPRO;
11579
11580 unbind_to (count, Qnil);
11581 set_buffer_internal_1 (prev);
11582 }
11583 }
11584 }
11585
11586
11587 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11588 F's desired tool-bar contents. F->tool_bar_items must have
11589 been set up previously by calling prepare_menu_bars. */
11590
11591 static void
11592 build_desired_tool_bar_string (struct frame *f)
11593 {
11594 int i, size, size_needed;
11595 struct gcpro gcpro1, gcpro2, gcpro3;
11596 Lisp_Object image, plist, props;
11597
11598 image = plist = props = Qnil;
11599 GCPRO3 (image, plist, props);
11600
11601 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11602 Otherwise, make a new string. */
11603
11604 /* The size of the string we might be able to reuse. */
11605 size = (STRINGP (f->desired_tool_bar_string)
11606 ? SCHARS (f->desired_tool_bar_string)
11607 : 0);
11608
11609 /* We need one space in the string for each image. */
11610 size_needed = f->n_tool_bar_items;
11611
11612 /* Reuse f->desired_tool_bar_string, if possible. */
11613 if (size < size_needed || NILP (f->desired_tool_bar_string))
11614 fset_desired_tool_bar_string
11615 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11616 else
11617 {
11618 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11619 Fremove_text_properties (make_number (0), make_number (size),
11620 props, f->desired_tool_bar_string);
11621 }
11622
11623 /* Put a `display' property on the string for the images to display,
11624 put a `menu_item' property on tool-bar items with a value that
11625 is the index of the item in F's tool-bar item vector. */
11626 for (i = 0; i < f->n_tool_bar_items; ++i)
11627 {
11628 #define PROP(IDX) \
11629 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11630
11631 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11632 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11633 int hmargin, vmargin, relief, idx, end;
11634
11635 /* If image is a vector, choose the image according to the
11636 button state. */
11637 image = PROP (TOOL_BAR_ITEM_IMAGES);
11638 if (VECTORP (image))
11639 {
11640 if (enabled_p)
11641 idx = (selected_p
11642 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11643 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11644 else
11645 idx = (selected_p
11646 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11647 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11648
11649 eassert (ASIZE (image) >= idx);
11650 image = AREF (image, idx);
11651 }
11652 else
11653 idx = -1;
11654
11655 /* Ignore invalid image specifications. */
11656 if (!valid_image_p (image))
11657 continue;
11658
11659 /* Display the tool-bar button pressed, or depressed. */
11660 plist = Fcopy_sequence (XCDR (image));
11661
11662 /* Compute margin and relief to draw. */
11663 relief = (tool_bar_button_relief >= 0
11664 ? tool_bar_button_relief
11665 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11666 hmargin = vmargin = relief;
11667
11668 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11669 INT_MAX - max (hmargin, vmargin)))
11670 {
11671 hmargin += XFASTINT (Vtool_bar_button_margin);
11672 vmargin += XFASTINT (Vtool_bar_button_margin);
11673 }
11674 else if (CONSP (Vtool_bar_button_margin))
11675 {
11676 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11677 INT_MAX - hmargin))
11678 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11679
11680 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11681 INT_MAX - vmargin))
11682 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11683 }
11684
11685 if (auto_raise_tool_bar_buttons_p)
11686 {
11687 /* Add a `:relief' property to the image spec if the item is
11688 selected. */
11689 if (selected_p)
11690 {
11691 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11692 hmargin -= relief;
11693 vmargin -= relief;
11694 }
11695 }
11696 else
11697 {
11698 /* If image is selected, display it pressed, i.e. with a
11699 negative relief. If it's not selected, display it with a
11700 raised relief. */
11701 plist = Fplist_put (plist, QCrelief,
11702 (selected_p
11703 ? make_number (-relief)
11704 : make_number (relief)));
11705 hmargin -= relief;
11706 vmargin -= relief;
11707 }
11708
11709 /* Put a margin around the image. */
11710 if (hmargin || vmargin)
11711 {
11712 if (hmargin == vmargin)
11713 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11714 else
11715 plist = Fplist_put (plist, QCmargin,
11716 Fcons (make_number (hmargin),
11717 make_number (vmargin)));
11718 }
11719
11720 /* If button is not enabled, and we don't have special images
11721 for the disabled state, make the image appear disabled by
11722 applying an appropriate algorithm to it. */
11723 if (!enabled_p && idx < 0)
11724 plist = Fplist_put (plist, QCconversion, Qdisabled);
11725
11726 /* Put a `display' text property on the string for the image to
11727 display. Put a `menu-item' property on the string that gives
11728 the start of this item's properties in the tool-bar items
11729 vector. */
11730 image = Fcons (Qimage, plist);
11731 props = list4 (Qdisplay, image,
11732 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11733
11734 /* Let the last image hide all remaining spaces in the tool bar
11735 string. The string can be longer than needed when we reuse a
11736 previous string. */
11737 if (i + 1 == f->n_tool_bar_items)
11738 end = SCHARS (f->desired_tool_bar_string);
11739 else
11740 end = i + 1;
11741 Fadd_text_properties (make_number (i), make_number (end),
11742 props, f->desired_tool_bar_string);
11743 #undef PROP
11744 }
11745
11746 UNGCPRO;
11747 }
11748
11749
11750 /* Display one line of the tool-bar of frame IT->f.
11751
11752 HEIGHT specifies the desired height of the tool-bar line.
11753 If the actual height of the glyph row is less than HEIGHT, the
11754 row's height is increased to HEIGHT, and the icons are centered
11755 vertically in the new height.
11756
11757 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11758 count a final empty row in case the tool-bar width exactly matches
11759 the window width.
11760 */
11761
11762 static void
11763 display_tool_bar_line (struct it *it, int height)
11764 {
11765 struct glyph_row *row = it->glyph_row;
11766 int max_x = it->last_visible_x;
11767 struct glyph *last;
11768
11769 prepare_desired_row (row);
11770 row->y = it->current_y;
11771
11772 /* Note that this isn't made use of if the face hasn't a box,
11773 so there's no need to check the face here. */
11774 it->start_of_box_run_p = 1;
11775
11776 while (it->current_x < max_x)
11777 {
11778 int x, n_glyphs_before, i, nglyphs;
11779 struct it it_before;
11780
11781 /* Get the next display element. */
11782 if (!get_next_display_element (it))
11783 {
11784 /* Don't count empty row if we are counting needed tool-bar lines. */
11785 if (height < 0 && !it->hpos)
11786 return;
11787 break;
11788 }
11789
11790 /* Produce glyphs. */
11791 n_glyphs_before = row->used[TEXT_AREA];
11792 it_before = *it;
11793
11794 PRODUCE_GLYPHS (it);
11795
11796 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11797 i = 0;
11798 x = it_before.current_x;
11799 while (i < nglyphs)
11800 {
11801 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11802
11803 if (x + glyph->pixel_width > max_x)
11804 {
11805 /* Glyph doesn't fit on line. Backtrack. */
11806 row->used[TEXT_AREA] = n_glyphs_before;
11807 *it = it_before;
11808 /* If this is the only glyph on this line, it will never fit on the
11809 tool-bar, so skip it. But ensure there is at least one glyph,
11810 so we don't accidentally disable the tool-bar. */
11811 if (n_glyphs_before == 0
11812 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11813 break;
11814 goto out;
11815 }
11816
11817 ++it->hpos;
11818 x += glyph->pixel_width;
11819 ++i;
11820 }
11821
11822 /* Stop at line end. */
11823 if (ITERATOR_AT_END_OF_LINE_P (it))
11824 break;
11825
11826 set_iterator_to_next (it, 1);
11827 }
11828
11829 out:;
11830
11831 row->displays_text_p = row->used[TEXT_AREA] != 0;
11832
11833 /* Use default face for the border below the tool bar.
11834
11835 FIXME: When auto-resize-tool-bars is grow-only, there is
11836 no additional border below the possibly empty tool-bar lines.
11837 So to make the extra empty lines look "normal", we have to
11838 use the tool-bar face for the border too. */
11839 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
11840 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11841 it->face_id = DEFAULT_FACE_ID;
11842
11843 extend_face_to_end_of_line (it);
11844 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11845 last->right_box_line_p = 1;
11846 if (last == row->glyphs[TEXT_AREA])
11847 last->left_box_line_p = 1;
11848
11849 /* Make line the desired height and center it vertically. */
11850 if ((height -= it->max_ascent + it->max_descent) > 0)
11851 {
11852 /* Don't add more than one line height. */
11853 height %= FRAME_LINE_HEIGHT (it->f);
11854 it->max_ascent += height / 2;
11855 it->max_descent += (height + 1) / 2;
11856 }
11857
11858 compute_line_metrics (it);
11859
11860 /* If line is empty, make it occupy the rest of the tool-bar. */
11861 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
11862 {
11863 row->height = row->phys_height = it->last_visible_y - row->y;
11864 row->visible_height = row->height;
11865 row->ascent = row->phys_ascent = 0;
11866 row->extra_line_spacing = 0;
11867 }
11868
11869 row->full_width_p = 1;
11870 row->continued_p = 0;
11871 row->truncated_on_left_p = 0;
11872 row->truncated_on_right_p = 0;
11873
11874 it->current_x = it->hpos = 0;
11875 it->current_y += row->height;
11876 ++it->vpos;
11877 ++it->glyph_row;
11878 }
11879
11880
11881 /* Max tool-bar height. */
11882
11883 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11884 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11885
11886 /* Value is the number of screen lines needed to make all tool-bar
11887 items of frame F visible. The number of actual rows needed is
11888 returned in *N_ROWS if non-NULL. */
11889
11890 static int
11891 tool_bar_lines_needed (struct frame *f, int *n_rows)
11892 {
11893 struct window *w = XWINDOW (f->tool_bar_window);
11894 struct it it;
11895 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11896 the desired matrix, so use (unused) mode-line row as temporary row to
11897 avoid destroying the first tool-bar row. */
11898 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11899
11900 /* Initialize an iterator for iteration over
11901 F->desired_tool_bar_string in the tool-bar window of frame F. */
11902 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11903 it.first_visible_x = 0;
11904 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11905 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11906 it.paragraph_embedding = L2R;
11907
11908 while (!ITERATOR_AT_END_P (&it))
11909 {
11910 clear_glyph_row (temp_row);
11911 it.glyph_row = temp_row;
11912 display_tool_bar_line (&it, -1);
11913 }
11914 clear_glyph_row (temp_row);
11915
11916 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11917 if (n_rows)
11918 *n_rows = it.vpos > 0 ? it.vpos : -1;
11919
11920 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11921 }
11922
11923
11924 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11925 0, 1, 0,
11926 doc: /* Return the number of lines occupied by the tool bar of FRAME.
11927 If FRAME is nil or omitted, use the selected frame. */)
11928 (Lisp_Object frame)
11929 {
11930 struct frame *f = decode_any_frame (frame);
11931 struct window *w;
11932 int nlines = 0;
11933
11934 if (WINDOWP (f->tool_bar_window)
11935 && (w = XWINDOW (f->tool_bar_window),
11936 WINDOW_TOTAL_LINES (w) > 0))
11937 {
11938 update_tool_bar (f, 1);
11939 if (f->n_tool_bar_items)
11940 {
11941 build_desired_tool_bar_string (f);
11942 nlines = tool_bar_lines_needed (f, NULL);
11943 }
11944 }
11945
11946 return make_number (nlines);
11947 }
11948
11949
11950 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11951 height should be changed. */
11952
11953 static int
11954 redisplay_tool_bar (struct frame *f)
11955 {
11956 struct window *w;
11957 struct it it;
11958 struct glyph_row *row;
11959
11960 #if defined (USE_GTK) || defined (HAVE_NS)
11961 if (FRAME_EXTERNAL_TOOL_BAR (f))
11962 update_frame_tool_bar (f);
11963 return 0;
11964 #endif
11965
11966 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11967 do anything. This means you must start with tool-bar-lines
11968 non-zero to get the auto-sizing effect. Or in other words, you
11969 can turn off tool-bars by specifying tool-bar-lines zero. */
11970 if (!WINDOWP (f->tool_bar_window)
11971 || (w = XWINDOW (f->tool_bar_window),
11972 WINDOW_TOTAL_LINES (w) == 0))
11973 return 0;
11974
11975 /* Set up an iterator for the tool-bar window. */
11976 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11977 it.first_visible_x = 0;
11978 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11979 row = it.glyph_row;
11980
11981 /* Build a string that represents the contents of the tool-bar. */
11982 build_desired_tool_bar_string (f);
11983 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11984 /* FIXME: This should be controlled by a user option. But it
11985 doesn't make sense to have an R2L tool bar if the menu bar cannot
11986 be drawn also R2L, and making the menu bar R2L is tricky due
11987 toolkit-specific code that implements it. If an R2L tool bar is
11988 ever supported, display_tool_bar_line should also be augmented to
11989 call unproduce_glyphs like display_line and display_string
11990 do. */
11991 it.paragraph_embedding = L2R;
11992
11993 if (f->n_tool_bar_rows == 0)
11994 {
11995 int nlines;
11996
11997 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11998 nlines != WINDOW_TOTAL_LINES (w)))
11999 {
12000 Lisp_Object frame;
12001 int old_height = WINDOW_TOTAL_LINES (w);
12002
12003 XSETFRAME (frame, f);
12004 Fmodify_frame_parameters (frame,
12005 list1 (Fcons (Qtool_bar_lines,
12006 make_number (nlines))));
12007 if (WINDOW_TOTAL_LINES (w) != old_height)
12008 {
12009 clear_glyph_matrix (w->desired_matrix);
12010 fonts_changed_p = 1;
12011 return 1;
12012 }
12013 }
12014 }
12015
12016 /* Display as many lines as needed to display all tool-bar items. */
12017
12018 if (f->n_tool_bar_rows > 0)
12019 {
12020 int border, rows, height, extra;
12021
12022 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12023 border = XINT (Vtool_bar_border);
12024 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12025 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12026 else if (EQ (Vtool_bar_border, Qborder_width))
12027 border = f->border_width;
12028 else
12029 border = 0;
12030 if (border < 0)
12031 border = 0;
12032
12033 rows = f->n_tool_bar_rows;
12034 height = max (1, (it.last_visible_y - border) / rows);
12035 extra = it.last_visible_y - border - height * rows;
12036
12037 while (it.current_y < it.last_visible_y)
12038 {
12039 int h = 0;
12040 if (extra > 0 && rows-- > 0)
12041 {
12042 h = (extra + rows - 1) / rows;
12043 extra -= h;
12044 }
12045 display_tool_bar_line (&it, height + h);
12046 }
12047 }
12048 else
12049 {
12050 while (it.current_y < it.last_visible_y)
12051 display_tool_bar_line (&it, 0);
12052 }
12053
12054 /* It doesn't make much sense to try scrolling in the tool-bar
12055 window, so don't do it. */
12056 w->desired_matrix->no_scrolling_p = 1;
12057 w->must_be_updated_p = 1;
12058
12059 if (!NILP (Vauto_resize_tool_bars))
12060 {
12061 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12062 int change_height_p = 0;
12063
12064 /* If we couldn't display everything, change the tool-bar's
12065 height if there is room for more. */
12066 if (IT_STRING_CHARPOS (it) < it.end_charpos
12067 && it.current_y < max_tool_bar_height)
12068 change_height_p = 1;
12069
12070 row = it.glyph_row - 1;
12071
12072 /* If there are blank lines at the end, except for a partially
12073 visible blank line at the end that is smaller than
12074 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12075 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12076 && row->height >= FRAME_LINE_HEIGHT (f))
12077 change_height_p = 1;
12078
12079 /* If row displays tool-bar items, but is partially visible,
12080 change the tool-bar's height. */
12081 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12082 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12083 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12084 change_height_p = 1;
12085
12086 /* Resize windows as needed by changing the `tool-bar-lines'
12087 frame parameter. */
12088 if (change_height_p)
12089 {
12090 Lisp_Object frame;
12091 int old_height = WINDOW_TOTAL_LINES (w);
12092 int nrows;
12093 int nlines = tool_bar_lines_needed (f, &nrows);
12094
12095 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12096 && !f->minimize_tool_bar_window_p)
12097 ? (nlines > old_height)
12098 : (nlines != old_height));
12099 f->minimize_tool_bar_window_p = 0;
12100
12101 if (change_height_p)
12102 {
12103 XSETFRAME (frame, f);
12104 Fmodify_frame_parameters (frame,
12105 list1 (Fcons (Qtool_bar_lines,
12106 make_number (nlines))));
12107 if (WINDOW_TOTAL_LINES (w) != old_height)
12108 {
12109 clear_glyph_matrix (w->desired_matrix);
12110 f->n_tool_bar_rows = nrows;
12111 fonts_changed_p = 1;
12112 return 1;
12113 }
12114 }
12115 }
12116 }
12117
12118 f->minimize_tool_bar_window_p = 0;
12119 return 0;
12120 }
12121
12122
12123 /* Get information about the tool-bar item which is displayed in GLYPH
12124 on frame F. Return in *PROP_IDX the index where tool-bar item
12125 properties start in F->tool_bar_items. Value is zero if
12126 GLYPH doesn't display a tool-bar item. */
12127
12128 static int
12129 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12130 {
12131 Lisp_Object prop;
12132 int success_p;
12133 int charpos;
12134
12135 /* This function can be called asynchronously, which means we must
12136 exclude any possibility that Fget_text_property signals an
12137 error. */
12138 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12139 charpos = max (0, charpos);
12140
12141 /* Get the text property `menu-item' at pos. The value of that
12142 property is the start index of this item's properties in
12143 F->tool_bar_items. */
12144 prop = Fget_text_property (make_number (charpos),
12145 Qmenu_item, f->current_tool_bar_string);
12146 if (INTEGERP (prop))
12147 {
12148 *prop_idx = XINT (prop);
12149 success_p = 1;
12150 }
12151 else
12152 success_p = 0;
12153
12154 return success_p;
12155 }
12156
12157 \f
12158 /* Get information about the tool-bar item at position X/Y on frame F.
12159 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12160 the current matrix of the tool-bar window of F, or NULL if not
12161 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12162 item in F->tool_bar_items. Value is
12163
12164 -1 if X/Y is not on a tool-bar item
12165 0 if X/Y is on the same item that was highlighted before.
12166 1 otherwise. */
12167
12168 static int
12169 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12170 int *hpos, int *vpos, int *prop_idx)
12171 {
12172 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12173 struct window *w = XWINDOW (f->tool_bar_window);
12174 int area;
12175
12176 /* Find the glyph under X/Y. */
12177 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12178 if (*glyph == NULL)
12179 return -1;
12180
12181 /* Get the start of this tool-bar item's properties in
12182 f->tool_bar_items. */
12183 if (!tool_bar_item_info (f, *glyph, prop_idx))
12184 return -1;
12185
12186 /* Is mouse on the highlighted item? */
12187 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12188 && *vpos >= hlinfo->mouse_face_beg_row
12189 && *vpos <= hlinfo->mouse_face_end_row
12190 && (*vpos > hlinfo->mouse_face_beg_row
12191 || *hpos >= hlinfo->mouse_face_beg_col)
12192 && (*vpos < hlinfo->mouse_face_end_row
12193 || *hpos < hlinfo->mouse_face_end_col
12194 || hlinfo->mouse_face_past_end))
12195 return 0;
12196
12197 return 1;
12198 }
12199
12200
12201 /* EXPORT:
12202 Handle mouse button event on the tool-bar of frame F, at
12203 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12204 0 for button release. MODIFIERS is event modifiers for button
12205 release. */
12206
12207 void
12208 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12209 int modifiers)
12210 {
12211 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12212 struct window *w = XWINDOW (f->tool_bar_window);
12213 int hpos, vpos, prop_idx;
12214 struct glyph *glyph;
12215 Lisp_Object enabled_p;
12216 int ts;
12217
12218 /* If not on the highlighted tool-bar item, and mouse-highlight is
12219 non-nil, return. This is so we generate the tool-bar button
12220 click only when the mouse button is released on the same item as
12221 where it was pressed. However, when mouse-highlight is disabled,
12222 generate the click when the button is released regardless of the
12223 highlight, since tool-bar items are not highlighted in that
12224 case. */
12225 frame_to_window_pixel_xy (w, &x, &y);
12226 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12227 if (ts == -1
12228 || (ts != 0 && !NILP (Vmouse_highlight)))
12229 return;
12230
12231 /* When mouse-highlight is off, generate the click for the item
12232 where the button was pressed, disregarding where it was
12233 released. */
12234 if (NILP (Vmouse_highlight) && !down_p)
12235 prop_idx = last_tool_bar_item;
12236
12237 /* If item is disabled, do nothing. */
12238 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12239 if (NILP (enabled_p))
12240 return;
12241
12242 if (down_p)
12243 {
12244 /* Show item in pressed state. */
12245 if (!NILP (Vmouse_highlight))
12246 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12247 last_tool_bar_item = prop_idx;
12248 }
12249 else
12250 {
12251 Lisp_Object key, frame;
12252 struct input_event event;
12253 EVENT_INIT (event);
12254
12255 /* Show item in released state. */
12256 if (!NILP (Vmouse_highlight))
12257 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12258
12259 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12260
12261 XSETFRAME (frame, f);
12262 event.kind = TOOL_BAR_EVENT;
12263 event.frame_or_window = frame;
12264 event.arg = frame;
12265 kbd_buffer_store_event (&event);
12266
12267 event.kind = TOOL_BAR_EVENT;
12268 event.frame_or_window = frame;
12269 event.arg = key;
12270 event.modifiers = modifiers;
12271 kbd_buffer_store_event (&event);
12272 last_tool_bar_item = -1;
12273 }
12274 }
12275
12276
12277 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12278 tool-bar window-relative coordinates X/Y. Called from
12279 note_mouse_highlight. */
12280
12281 static void
12282 note_tool_bar_highlight (struct frame *f, int x, int y)
12283 {
12284 Lisp_Object window = f->tool_bar_window;
12285 struct window *w = XWINDOW (window);
12286 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12287 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12288 int hpos, vpos;
12289 struct glyph *glyph;
12290 struct glyph_row *row;
12291 int i;
12292 Lisp_Object enabled_p;
12293 int prop_idx;
12294 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12295 int mouse_down_p, rc;
12296
12297 /* Function note_mouse_highlight is called with negative X/Y
12298 values when mouse moves outside of the frame. */
12299 if (x <= 0 || y <= 0)
12300 {
12301 clear_mouse_face (hlinfo);
12302 return;
12303 }
12304
12305 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12306 if (rc < 0)
12307 {
12308 /* Not on tool-bar item. */
12309 clear_mouse_face (hlinfo);
12310 return;
12311 }
12312 else if (rc == 0)
12313 /* On same tool-bar item as before. */
12314 goto set_help_echo;
12315
12316 clear_mouse_face (hlinfo);
12317
12318 /* Mouse is down, but on different tool-bar item? */
12319 mouse_down_p = (dpyinfo->grabbed
12320 && f == last_mouse_frame
12321 && FRAME_LIVE_P (f));
12322 if (mouse_down_p
12323 && last_tool_bar_item != prop_idx)
12324 return;
12325
12326 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12327
12328 /* If tool-bar item is not enabled, don't highlight it. */
12329 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12330 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12331 {
12332 /* Compute the x-position of the glyph. In front and past the
12333 image is a space. We include this in the highlighted area. */
12334 row = MATRIX_ROW (w->current_matrix, vpos);
12335 for (i = x = 0; i < hpos; ++i)
12336 x += row->glyphs[TEXT_AREA][i].pixel_width;
12337
12338 /* Record this as the current active region. */
12339 hlinfo->mouse_face_beg_col = hpos;
12340 hlinfo->mouse_face_beg_row = vpos;
12341 hlinfo->mouse_face_beg_x = x;
12342 hlinfo->mouse_face_beg_y = row->y;
12343 hlinfo->mouse_face_past_end = 0;
12344
12345 hlinfo->mouse_face_end_col = hpos + 1;
12346 hlinfo->mouse_face_end_row = vpos;
12347 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12348 hlinfo->mouse_face_end_y = row->y;
12349 hlinfo->mouse_face_window = window;
12350 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12351
12352 /* Display it as active. */
12353 show_mouse_face (hlinfo, draw);
12354 }
12355
12356 set_help_echo:
12357
12358 /* Set help_echo_string to a help string to display for this tool-bar item.
12359 XTread_socket does the rest. */
12360 help_echo_object = help_echo_window = Qnil;
12361 help_echo_pos = -1;
12362 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12363 if (NILP (help_echo_string))
12364 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12365 }
12366
12367 #endif /* HAVE_WINDOW_SYSTEM */
12368
12369
12370 \f
12371 /************************************************************************
12372 Horizontal scrolling
12373 ************************************************************************/
12374
12375 static int hscroll_window_tree (Lisp_Object);
12376 static int hscroll_windows (Lisp_Object);
12377
12378 /* For all leaf windows in the window tree rooted at WINDOW, set their
12379 hscroll value so that PT is (i) visible in the window, and (ii) so
12380 that it is not within a certain margin at the window's left and
12381 right border. Value is non-zero if any window's hscroll has been
12382 changed. */
12383
12384 static int
12385 hscroll_window_tree (Lisp_Object window)
12386 {
12387 int hscrolled_p = 0;
12388 int hscroll_relative_p = FLOATP (Vhscroll_step);
12389 int hscroll_step_abs = 0;
12390 double hscroll_step_rel = 0;
12391
12392 if (hscroll_relative_p)
12393 {
12394 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12395 if (hscroll_step_rel < 0)
12396 {
12397 hscroll_relative_p = 0;
12398 hscroll_step_abs = 0;
12399 }
12400 }
12401 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12402 {
12403 hscroll_step_abs = XINT (Vhscroll_step);
12404 if (hscroll_step_abs < 0)
12405 hscroll_step_abs = 0;
12406 }
12407 else
12408 hscroll_step_abs = 0;
12409
12410 while (WINDOWP (window))
12411 {
12412 struct window *w = XWINDOW (window);
12413
12414 if (WINDOWP (w->contents))
12415 hscrolled_p |= hscroll_window_tree (w->contents);
12416 else if (w->cursor.vpos >= 0)
12417 {
12418 int h_margin;
12419 int text_area_width;
12420 struct glyph_row *current_cursor_row
12421 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12422 struct glyph_row *desired_cursor_row
12423 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12424 struct glyph_row *cursor_row
12425 = (desired_cursor_row->enabled_p
12426 ? desired_cursor_row
12427 : current_cursor_row);
12428 int row_r2l_p = cursor_row->reversed_p;
12429
12430 text_area_width = window_box_width (w, TEXT_AREA);
12431
12432 /* Scroll when cursor is inside this scroll margin. */
12433 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12434
12435 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12436 /* For left-to-right rows, hscroll when cursor is either
12437 (i) inside the right hscroll margin, or (ii) if it is
12438 inside the left margin and the window is already
12439 hscrolled. */
12440 && ((!row_r2l_p
12441 && ((w->hscroll
12442 && w->cursor.x <= h_margin)
12443 || (cursor_row->enabled_p
12444 && cursor_row->truncated_on_right_p
12445 && (w->cursor.x >= text_area_width - h_margin))))
12446 /* For right-to-left rows, the logic is similar,
12447 except that rules for scrolling to left and right
12448 are reversed. E.g., if cursor.x <= h_margin, we
12449 need to hscroll "to the right" unconditionally,
12450 and that will scroll the screen to the left so as
12451 to reveal the next portion of the row. */
12452 || (row_r2l_p
12453 && ((cursor_row->enabled_p
12454 /* FIXME: It is confusing to set the
12455 truncated_on_right_p flag when R2L rows
12456 are actually truncated on the left. */
12457 && cursor_row->truncated_on_right_p
12458 && w->cursor.x <= h_margin)
12459 || (w->hscroll
12460 && (w->cursor.x >= text_area_width - h_margin))))))
12461 {
12462 struct it it;
12463 ptrdiff_t hscroll;
12464 struct buffer *saved_current_buffer;
12465 ptrdiff_t pt;
12466 int wanted_x;
12467
12468 /* Find point in a display of infinite width. */
12469 saved_current_buffer = current_buffer;
12470 current_buffer = XBUFFER (w->contents);
12471
12472 if (w == XWINDOW (selected_window))
12473 pt = PT;
12474 else
12475 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12476
12477 /* Move iterator to pt starting at cursor_row->start in
12478 a line with infinite width. */
12479 init_to_row_start (&it, w, cursor_row);
12480 it.last_visible_x = INFINITY;
12481 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12482 current_buffer = saved_current_buffer;
12483
12484 /* Position cursor in window. */
12485 if (!hscroll_relative_p && hscroll_step_abs == 0)
12486 hscroll = max (0, (it.current_x
12487 - (ITERATOR_AT_END_OF_LINE_P (&it)
12488 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12489 : (text_area_width / 2))))
12490 / FRAME_COLUMN_WIDTH (it.f);
12491 else if ((!row_r2l_p
12492 && w->cursor.x >= text_area_width - h_margin)
12493 || (row_r2l_p && w->cursor.x <= h_margin))
12494 {
12495 if (hscroll_relative_p)
12496 wanted_x = text_area_width * (1 - hscroll_step_rel)
12497 - h_margin;
12498 else
12499 wanted_x = text_area_width
12500 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12501 - h_margin;
12502 hscroll
12503 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12504 }
12505 else
12506 {
12507 if (hscroll_relative_p)
12508 wanted_x = text_area_width * hscroll_step_rel
12509 + h_margin;
12510 else
12511 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12512 + h_margin;
12513 hscroll
12514 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12515 }
12516 hscroll = max (hscroll, w->min_hscroll);
12517
12518 /* Don't prevent redisplay optimizations if hscroll
12519 hasn't changed, as it will unnecessarily slow down
12520 redisplay. */
12521 if (w->hscroll != hscroll)
12522 {
12523 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12524 w->hscroll = hscroll;
12525 hscrolled_p = 1;
12526 }
12527 }
12528 }
12529
12530 window = w->next;
12531 }
12532
12533 /* Value is non-zero if hscroll of any leaf window has been changed. */
12534 return hscrolled_p;
12535 }
12536
12537
12538 /* Set hscroll so that cursor is visible and not inside horizontal
12539 scroll margins for all windows in the tree rooted at WINDOW. See
12540 also hscroll_window_tree above. Value is non-zero if any window's
12541 hscroll has been changed. If it has, desired matrices on the frame
12542 of WINDOW are cleared. */
12543
12544 static int
12545 hscroll_windows (Lisp_Object window)
12546 {
12547 int hscrolled_p = hscroll_window_tree (window);
12548 if (hscrolled_p)
12549 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12550 return hscrolled_p;
12551 }
12552
12553
12554 \f
12555 /************************************************************************
12556 Redisplay
12557 ************************************************************************/
12558
12559 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12560 to a non-zero value. This is sometimes handy to have in a debugger
12561 session. */
12562
12563 #ifdef GLYPH_DEBUG
12564
12565 /* First and last unchanged row for try_window_id. */
12566
12567 static int debug_first_unchanged_at_end_vpos;
12568 static int debug_last_unchanged_at_beg_vpos;
12569
12570 /* Delta vpos and y. */
12571
12572 static int debug_dvpos, debug_dy;
12573
12574 /* Delta in characters and bytes for try_window_id. */
12575
12576 static ptrdiff_t debug_delta, debug_delta_bytes;
12577
12578 /* Values of window_end_pos and window_end_vpos at the end of
12579 try_window_id. */
12580
12581 static ptrdiff_t debug_end_vpos;
12582
12583 /* Append a string to W->desired_matrix->method. FMT is a printf
12584 format string. If trace_redisplay_p is non-zero also printf the
12585 resulting string to stderr. */
12586
12587 static void debug_method_add (struct window *, char const *, ...)
12588 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12589
12590 static void
12591 debug_method_add (struct window *w, char const *fmt, ...)
12592 {
12593 void *ptr = w;
12594 char *method = w->desired_matrix->method;
12595 int len = strlen (method);
12596 int size = sizeof w->desired_matrix->method;
12597 int remaining = size - len - 1;
12598 va_list ap;
12599
12600 if (len && remaining)
12601 {
12602 method[len] = '|';
12603 --remaining, ++len;
12604 }
12605
12606 va_start (ap, fmt);
12607 vsnprintf (method + len, remaining + 1, fmt, ap);
12608 va_end (ap);
12609
12610 if (trace_redisplay_p)
12611 fprintf (stderr, "%p (%s): %s\n",
12612 ptr,
12613 ((BUFFERP (w->contents)
12614 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12615 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12616 : "no buffer"),
12617 method + len);
12618 }
12619
12620 #endif /* GLYPH_DEBUG */
12621
12622
12623 /* Value is non-zero if all changes in window W, which displays
12624 current_buffer, are in the text between START and END. START is a
12625 buffer position, END is given as a distance from Z. Used in
12626 redisplay_internal for display optimization. */
12627
12628 static int
12629 text_outside_line_unchanged_p (struct window *w,
12630 ptrdiff_t start, ptrdiff_t end)
12631 {
12632 int unchanged_p = 1;
12633
12634 /* If text or overlays have changed, see where. */
12635 if (window_outdated (w))
12636 {
12637 /* Gap in the line? */
12638 if (GPT < start || Z - GPT < end)
12639 unchanged_p = 0;
12640
12641 /* Changes start in front of the line, or end after it? */
12642 if (unchanged_p
12643 && (BEG_UNCHANGED < start - 1
12644 || END_UNCHANGED < end))
12645 unchanged_p = 0;
12646
12647 /* If selective display, can't optimize if changes start at the
12648 beginning of the line. */
12649 if (unchanged_p
12650 && INTEGERP (BVAR (current_buffer, selective_display))
12651 && XINT (BVAR (current_buffer, selective_display)) > 0
12652 && (BEG_UNCHANGED < start || GPT <= start))
12653 unchanged_p = 0;
12654
12655 /* If there are overlays at the start or end of the line, these
12656 may have overlay strings with newlines in them. A change at
12657 START, for instance, may actually concern the display of such
12658 overlay strings as well, and they are displayed on different
12659 lines. So, quickly rule out this case. (For the future, it
12660 might be desirable to implement something more telling than
12661 just BEG/END_UNCHANGED.) */
12662 if (unchanged_p)
12663 {
12664 if (BEG + BEG_UNCHANGED == start
12665 && overlay_touches_p (start))
12666 unchanged_p = 0;
12667 if (END_UNCHANGED == end
12668 && overlay_touches_p (Z - end))
12669 unchanged_p = 0;
12670 }
12671
12672 /* Under bidi reordering, adding or deleting a character in the
12673 beginning of a paragraph, before the first strong directional
12674 character, can change the base direction of the paragraph (unless
12675 the buffer specifies a fixed paragraph direction), which will
12676 require to redisplay the whole paragraph. It might be worthwhile
12677 to find the paragraph limits and widen the range of redisplayed
12678 lines to that, but for now just give up this optimization. */
12679 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
12680 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
12681 unchanged_p = 0;
12682 }
12683
12684 return unchanged_p;
12685 }
12686
12687
12688 /* Do a frame update, taking possible shortcuts into account. This is
12689 the main external entry point for redisplay.
12690
12691 If the last redisplay displayed an echo area message and that message
12692 is no longer requested, we clear the echo area or bring back the
12693 mini-buffer if that is in use. */
12694
12695 void
12696 redisplay (void)
12697 {
12698 redisplay_internal ();
12699 }
12700
12701
12702 static Lisp_Object
12703 overlay_arrow_string_or_property (Lisp_Object var)
12704 {
12705 Lisp_Object val;
12706
12707 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12708 return val;
12709
12710 return Voverlay_arrow_string;
12711 }
12712
12713 /* Return 1 if there are any overlay-arrows in current_buffer. */
12714 static int
12715 overlay_arrow_in_current_buffer_p (void)
12716 {
12717 Lisp_Object vlist;
12718
12719 for (vlist = Voverlay_arrow_variable_list;
12720 CONSP (vlist);
12721 vlist = XCDR (vlist))
12722 {
12723 Lisp_Object var = XCAR (vlist);
12724 Lisp_Object val;
12725
12726 if (!SYMBOLP (var))
12727 continue;
12728 val = find_symbol_value (var);
12729 if (MARKERP (val)
12730 && current_buffer == XMARKER (val)->buffer)
12731 return 1;
12732 }
12733 return 0;
12734 }
12735
12736
12737 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12738 has changed. */
12739
12740 static int
12741 overlay_arrows_changed_p (void)
12742 {
12743 Lisp_Object vlist;
12744
12745 for (vlist = Voverlay_arrow_variable_list;
12746 CONSP (vlist);
12747 vlist = XCDR (vlist))
12748 {
12749 Lisp_Object var = XCAR (vlist);
12750 Lisp_Object val, pstr;
12751
12752 if (!SYMBOLP (var))
12753 continue;
12754 val = find_symbol_value (var);
12755 if (!MARKERP (val))
12756 continue;
12757 if (! EQ (COERCE_MARKER (val),
12758 Fget (var, Qlast_arrow_position))
12759 || ! (pstr = overlay_arrow_string_or_property (var),
12760 EQ (pstr, Fget (var, Qlast_arrow_string))))
12761 return 1;
12762 }
12763 return 0;
12764 }
12765
12766 /* Mark overlay arrows to be updated on next redisplay. */
12767
12768 static void
12769 update_overlay_arrows (int up_to_date)
12770 {
12771 Lisp_Object vlist;
12772
12773 for (vlist = Voverlay_arrow_variable_list;
12774 CONSP (vlist);
12775 vlist = XCDR (vlist))
12776 {
12777 Lisp_Object var = XCAR (vlist);
12778
12779 if (!SYMBOLP (var))
12780 continue;
12781
12782 if (up_to_date > 0)
12783 {
12784 Lisp_Object val = find_symbol_value (var);
12785 Fput (var, Qlast_arrow_position,
12786 COERCE_MARKER (val));
12787 Fput (var, Qlast_arrow_string,
12788 overlay_arrow_string_or_property (var));
12789 }
12790 else if (up_to_date < 0
12791 || !NILP (Fget (var, Qlast_arrow_position)))
12792 {
12793 Fput (var, Qlast_arrow_position, Qt);
12794 Fput (var, Qlast_arrow_string, Qt);
12795 }
12796 }
12797 }
12798
12799
12800 /* Return overlay arrow string to display at row.
12801 Return integer (bitmap number) for arrow bitmap in left fringe.
12802 Return nil if no overlay arrow. */
12803
12804 static Lisp_Object
12805 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12806 {
12807 Lisp_Object vlist;
12808
12809 for (vlist = Voverlay_arrow_variable_list;
12810 CONSP (vlist);
12811 vlist = XCDR (vlist))
12812 {
12813 Lisp_Object var = XCAR (vlist);
12814 Lisp_Object val;
12815
12816 if (!SYMBOLP (var))
12817 continue;
12818
12819 val = find_symbol_value (var);
12820
12821 if (MARKERP (val)
12822 && current_buffer == XMARKER (val)->buffer
12823 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12824 {
12825 if (FRAME_WINDOW_P (it->f)
12826 /* FIXME: if ROW->reversed_p is set, this should test
12827 the right fringe, not the left one. */
12828 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12829 {
12830 #ifdef HAVE_WINDOW_SYSTEM
12831 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12832 {
12833 int fringe_bitmap;
12834 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12835 return make_number (fringe_bitmap);
12836 }
12837 #endif
12838 return make_number (-1); /* Use default arrow bitmap. */
12839 }
12840 return overlay_arrow_string_or_property (var);
12841 }
12842 }
12843
12844 return Qnil;
12845 }
12846
12847 /* Return 1 if point moved out of or into a composition. Otherwise
12848 return 0. PREV_BUF and PREV_PT are the last point buffer and
12849 position. BUF and PT are the current point buffer and position. */
12850
12851 static int
12852 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12853 struct buffer *buf, ptrdiff_t pt)
12854 {
12855 ptrdiff_t start, end;
12856 Lisp_Object prop;
12857 Lisp_Object buffer;
12858
12859 XSETBUFFER (buffer, buf);
12860 /* Check a composition at the last point if point moved within the
12861 same buffer. */
12862 if (prev_buf == buf)
12863 {
12864 if (prev_pt == pt)
12865 /* Point didn't move. */
12866 return 0;
12867
12868 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12869 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12870 && composition_valid_p (start, end, prop)
12871 && start < prev_pt && end > prev_pt)
12872 /* The last point was within the composition. Return 1 iff
12873 point moved out of the composition. */
12874 return (pt <= start || pt >= end);
12875 }
12876
12877 /* Check a composition at the current point. */
12878 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12879 && find_composition (pt, -1, &start, &end, &prop, buffer)
12880 && composition_valid_p (start, end, prop)
12881 && start < pt && end > pt);
12882 }
12883
12884 /* Reconsider the clip changes of buffer which is displayed in W. */
12885
12886 static void
12887 reconsider_clip_changes (struct window *w)
12888 {
12889 struct buffer *b = XBUFFER (w->contents);
12890
12891 if (b->clip_changed
12892 && w->window_end_valid
12893 && w->current_matrix->buffer == b
12894 && w->current_matrix->zv == BUF_ZV (b)
12895 && w->current_matrix->begv == BUF_BEGV (b))
12896 b->clip_changed = 0;
12897
12898 /* If display wasn't paused, and W is not a tool bar window, see if
12899 point has been moved into or out of a composition. In that case,
12900 we set b->clip_changed to 1 to force updating the screen. If
12901 b->clip_changed has already been set to 1, we can skip this
12902 check. */
12903 if (!b->clip_changed && w->window_end_valid)
12904 {
12905 ptrdiff_t pt = (w == XWINDOW (selected_window)
12906 ? PT : marker_position (w->pointm));
12907
12908 if ((w->current_matrix->buffer != b || pt != w->last_point)
12909 && check_point_in_composition (w->current_matrix->buffer,
12910 w->last_point, b, pt))
12911 b->clip_changed = 1;
12912 }
12913 }
12914
12915 #define STOP_POLLING \
12916 do { if (! polling_stopped_here) stop_polling (); \
12917 polling_stopped_here = 1; } while (0)
12918
12919 #define RESUME_POLLING \
12920 do { if (polling_stopped_here) start_polling (); \
12921 polling_stopped_here = 0; } while (0)
12922
12923
12924 /* Perhaps in the future avoid recentering windows if it
12925 is not necessary; currently that causes some problems. */
12926
12927 static void
12928 redisplay_internal (void)
12929 {
12930 struct window *w = XWINDOW (selected_window);
12931 struct window *sw;
12932 struct frame *fr;
12933 int pending;
12934 bool must_finish = 0, match_p;
12935 struct text_pos tlbufpos, tlendpos;
12936 int number_of_visible_frames;
12937 ptrdiff_t count;
12938 struct frame *sf;
12939 int polling_stopped_here = 0;
12940 Lisp_Object tail, frame;
12941
12942 /* Non-zero means redisplay has to consider all windows on all
12943 frames. Zero means, only selected_window is considered. */
12944 int consider_all_windows_p;
12945
12946 /* Non-zero means redisplay has to redisplay the miniwindow. */
12947 int update_miniwindow_p = 0;
12948
12949 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12950
12951 /* No redisplay if running in batch mode or frame is not yet fully
12952 initialized, or redisplay is explicitly turned off by setting
12953 Vinhibit_redisplay. */
12954 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12955 || !NILP (Vinhibit_redisplay))
12956 return;
12957
12958 /* Don't examine these until after testing Vinhibit_redisplay.
12959 When Emacs is shutting down, perhaps because its connection to
12960 X has dropped, we should not look at them at all. */
12961 fr = XFRAME (w->frame);
12962 sf = SELECTED_FRAME ();
12963
12964 if (!fr->glyphs_initialized_p)
12965 return;
12966
12967 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12968 if (popup_activated ())
12969 return;
12970 #endif
12971
12972 /* I don't think this happens but let's be paranoid. */
12973 if (redisplaying_p)
12974 return;
12975
12976 /* Record a function that clears redisplaying_p
12977 when we leave this function. */
12978 count = SPECPDL_INDEX ();
12979 record_unwind_protect_void (unwind_redisplay);
12980 redisplaying_p = 1;
12981 specbind (Qinhibit_free_realized_faces, Qnil);
12982
12983 /* Record this function, so it appears on the profiler's backtraces. */
12984 record_in_backtrace (Qredisplay_internal, &Qnil, 0);
12985
12986 FOR_EACH_FRAME (tail, frame)
12987 XFRAME (frame)->already_hscrolled_p = 0;
12988
12989 retry:
12990 /* Remember the currently selected window. */
12991 sw = w;
12992
12993 pending = 0;
12994 last_escape_glyph_frame = NULL;
12995 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12996 last_glyphless_glyph_frame = NULL;
12997 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12998
12999 /* If new fonts have been loaded that make a glyph matrix adjustment
13000 necessary, do it. */
13001 if (fonts_changed_p)
13002 {
13003 adjust_glyphs (NULL);
13004 ++windows_or_buffers_changed;
13005 fonts_changed_p = 0;
13006 }
13007
13008 /* If face_change_count is non-zero, init_iterator will free all
13009 realized faces, which includes the faces referenced from current
13010 matrices. So, we can't reuse current matrices in this case. */
13011 if (face_change_count)
13012 ++windows_or_buffers_changed;
13013
13014 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13015 && FRAME_TTY (sf)->previous_frame != sf)
13016 {
13017 /* Since frames on a single ASCII terminal share the same
13018 display area, displaying a different frame means redisplay
13019 the whole thing. */
13020 windows_or_buffers_changed++;
13021 SET_FRAME_GARBAGED (sf);
13022 #ifndef DOS_NT
13023 set_tty_color_mode (FRAME_TTY (sf), sf);
13024 #endif
13025 FRAME_TTY (sf)->previous_frame = sf;
13026 }
13027
13028 /* Set the visible flags for all frames. Do this before checking for
13029 resized or garbaged frames; they want to know if their frames are
13030 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13031 number_of_visible_frames = 0;
13032
13033 FOR_EACH_FRAME (tail, frame)
13034 {
13035 struct frame *f = XFRAME (frame);
13036
13037 if (FRAME_VISIBLE_P (f))
13038 ++number_of_visible_frames;
13039 clear_desired_matrices (f);
13040 }
13041
13042 /* Notice any pending interrupt request to change frame size. */
13043 do_pending_window_change (1);
13044
13045 /* do_pending_window_change could change the selected_window due to
13046 frame resizing which makes the selected window too small. */
13047 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13048 sw = w;
13049
13050 /* Clear frames marked as garbaged. */
13051 clear_garbaged_frames ();
13052
13053 /* Build menubar and tool-bar items. */
13054 if (NILP (Vmemory_full))
13055 prepare_menu_bars ();
13056
13057 if (windows_or_buffers_changed)
13058 update_mode_lines++;
13059
13060 reconsider_clip_changes (w);
13061
13062 /* In most cases selected window displays current buffer. */
13063 match_p = XBUFFER (w->contents) == current_buffer;
13064 if (match_p)
13065 {
13066 ptrdiff_t count1;
13067
13068 /* Detect case that we need to write or remove a star in the mode line. */
13069 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13070 {
13071 w->update_mode_line = 1;
13072 if (buffer_shared_and_changed ())
13073 update_mode_lines++;
13074 }
13075
13076 /* Avoid invocation of point motion hooks by `current_column' below. */
13077 count1 = SPECPDL_INDEX ();
13078 specbind (Qinhibit_point_motion_hooks, Qt);
13079
13080 if (mode_line_update_needed (w))
13081 w->update_mode_line = 1;
13082
13083 unbind_to (count1, Qnil);
13084 }
13085
13086 consider_all_windows_p = (update_mode_lines
13087 || buffer_shared_and_changed ()
13088 || cursor_type_changed);
13089
13090 /* If specs for an arrow have changed, do thorough redisplay
13091 to ensure we remove any arrow that should no longer exist. */
13092 if (overlay_arrows_changed_p ())
13093 consider_all_windows_p = windows_or_buffers_changed = 1;
13094
13095 /* Normally the message* functions will have already displayed and
13096 updated the echo area, but the frame may have been trashed, or
13097 the update may have been preempted, so display the echo area
13098 again here. Checking message_cleared_p captures the case that
13099 the echo area should be cleared. */
13100 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13101 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13102 || (message_cleared_p
13103 && minibuf_level == 0
13104 /* If the mini-window is currently selected, this means the
13105 echo-area doesn't show through. */
13106 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13107 {
13108 int window_height_changed_p = echo_area_display (0);
13109
13110 if (message_cleared_p)
13111 update_miniwindow_p = 1;
13112
13113 must_finish = 1;
13114
13115 /* If we don't display the current message, don't clear the
13116 message_cleared_p flag, because, if we did, we wouldn't clear
13117 the echo area in the next redisplay which doesn't preserve
13118 the echo area. */
13119 if (!display_last_displayed_message_p)
13120 message_cleared_p = 0;
13121
13122 if (fonts_changed_p)
13123 goto retry;
13124 else if (window_height_changed_p)
13125 {
13126 consider_all_windows_p = 1;
13127 ++update_mode_lines;
13128 ++windows_or_buffers_changed;
13129
13130 /* If window configuration was changed, frames may have been
13131 marked garbaged. Clear them or we will experience
13132 surprises wrt scrolling. */
13133 clear_garbaged_frames ();
13134 }
13135 }
13136 else if (EQ (selected_window, minibuf_window)
13137 && (current_buffer->clip_changed || window_outdated (w))
13138 && resize_mini_window (w, 0))
13139 {
13140 /* Resized active mini-window to fit the size of what it is
13141 showing if its contents might have changed. */
13142 must_finish = 1;
13143 /* FIXME: this causes all frames to be updated, which seems unnecessary
13144 since only the current frame needs to be considered. This function
13145 needs to be rewritten with two variables, consider_all_windows and
13146 consider_all_frames. */
13147 consider_all_windows_p = 1;
13148 ++windows_or_buffers_changed;
13149 ++update_mode_lines;
13150
13151 /* If window configuration was changed, frames may have been
13152 marked garbaged. Clear them or we will experience
13153 surprises wrt scrolling. */
13154 clear_garbaged_frames ();
13155 }
13156
13157 /* If showing the region, and mark has changed, we must redisplay
13158 the whole window. The assignment to this_line_start_pos prevents
13159 the optimization directly below this if-statement. */
13160 if (((!NILP (Vtransient_mark_mode)
13161 && !NILP (BVAR (XBUFFER (w->contents), mark_active)))
13162 != (w->region_showing > 0))
13163 || (w->region_showing
13164 && w->region_showing
13165 != XINT (Fmarker_position (BVAR (XBUFFER (w->contents), mark)))))
13166 CHARPOS (this_line_start_pos) = 0;
13167
13168 /* Optimize the case that only the line containing the cursor in the
13169 selected window has changed. Variables starting with this_ are
13170 set in display_line and record information about the line
13171 containing the cursor. */
13172 tlbufpos = this_line_start_pos;
13173 tlendpos = this_line_end_pos;
13174 if (!consider_all_windows_p
13175 && CHARPOS (tlbufpos) > 0
13176 && !w->update_mode_line
13177 && !current_buffer->clip_changed
13178 && !current_buffer->prevent_redisplay_optimizations_p
13179 && FRAME_VISIBLE_P (XFRAME (w->frame))
13180 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13181 /* Make sure recorded data applies to current buffer, etc. */
13182 && this_line_buffer == current_buffer
13183 && match_p
13184 && !w->force_start
13185 && !w->optional_new_start
13186 /* Point must be on the line that we have info recorded about. */
13187 && PT >= CHARPOS (tlbufpos)
13188 && PT <= Z - CHARPOS (tlendpos)
13189 /* All text outside that line, including its final newline,
13190 must be unchanged. */
13191 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13192 CHARPOS (tlendpos)))
13193 {
13194 if (CHARPOS (tlbufpos) > BEGV
13195 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13196 && (CHARPOS (tlbufpos) == ZV
13197 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13198 /* Former continuation line has disappeared by becoming empty. */
13199 goto cancel;
13200 else if (window_outdated (w) || MINI_WINDOW_P (w))
13201 {
13202 /* We have to handle the case of continuation around a
13203 wide-column character (see the comment in indent.c around
13204 line 1340).
13205
13206 For instance, in the following case:
13207
13208 -------- Insert --------
13209 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13210 J_I_ ==> J_I_ `^^' are cursors.
13211 ^^ ^^
13212 -------- --------
13213
13214 As we have to redraw the line above, we cannot use this
13215 optimization. */
13216
13217 struct it it;
13218 int line_height_before = this_line_pixel_height;
13219
13220 /* Note that start_display will handle the case that the
13221 line starting at tlbufpos is a continuation line. */
13222 start_display (&it, w, tlbufpos);
13223
13224 /* Implementation note: It this still necessary? */
13225 if (it.current_x != this_line_start_x)
13226 goto cancel;
13227
13228 TRACE ((stderr, "trying display optimization 1\n"));
13229 w->cursor.vpos = -1;
13230 overlay_arrow_seen = 0;
13231 it.vpos = this_line_vpos;
13232 it.current_y = this_line_y;
13233 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13234 display_line (&it);
13235
13236 /* If line contains point, is not continued,
13237 and ends at same distance from eob as before, we win. */
13238 if (w->cursor.vpos >= 0
13239 /* Line is not continued, otherwise this_line_start_pos
13240 would have been set to 0 in display_line. */
13241 && CHARPOS (this_line_start_pos)
13242 /* Line ends as before. */
13243 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13244 /* Line has same height as before. Otherwise other lines
13245 would have to be shifted up or down. */
13246 && this_line_pixel_height == line_height_before)
13247 {
13248 /* If this is not the window's last line, we must adjust
13249 the charstarts of the lines below. */
13250 if (it.current_y < it.last_visible_y)
13251 {
13252 struct glyph_row *row
13253 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13254 ptrdiff_t delta, delta_bytes;
13255
13256 /* We used to distinguish between two cases here,
13257 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13258 when the line ends in a newline or the end of the
13259 buffer's accessible portion. But both cases did
13260 the same, so they were collapsed. */
13261 delta = (Z
13262 - CHARPOS (tlendpos)
13263 - MATRIX_ROW_START_CHARPOS (row));
13264 delta_bytes = (Z_BYTE
13265 - BYTEPOS (tlendpos)
13266 - MATRIX_ROW_START_BYTEPOS (row));
13267
13268 increment_matrix_positions (w->current_matrix,
13269 this_line_vpos + 1,
13270 w->current_matrix->nrows,
13271 delta, delta_bytes);
13272 }
13273
13274 /* If this row displays text now but previously didn't,
13275 or vice versa, w->window_end_vpos may have to be
13276 adjusted. */
13277 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13278 {
13279 if (w->window_end_vpos < this_line_vpos)
13280 w->window_end_vpos = this_line_vpos;
13281 }
13282 else if (w->window_end_vpos == this_line_vpos
13283 && this_line_vpos > 0)
13284 w->window_end_vpos = this_line_vpos - 1;
13285 w->window_end_valid = 0;
13286
13287 /* Update hint: No need to try to scroll in update_window. */
13288 w->desired_matrix->no_scrolling_p = 1;
13289
13290 #ifdef GLYPH_DEBUG
13291 *w->desired_matrix->method = 0;
13292 debug_method_add (w, "optimization 1");
13293 #endif
13294 #ifdef HAVE_WINDOW_SYSTEM
13295 update_window_fringes (w, 0);
13296 #endif
13297 goto update;
13298 }
13299 else
13300 goto cancel;
13301 }
13302 else if (/* Cursor position hasn't changed. */
13303 PT == w->last_point
13304 /* Make sure the cursor was last displayed
13305 in this window. Otherwise we have to reposition it. */
13306 && 0 <= w->cursor.vpos
13307 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13308 {
13309 if (!must_finish)
13310 {
13311 do_pending_window_change (1);
13312 /* If selected_window changed, redisplay again. */
13313 if (WINDOWP (selected_window)
13314 && (w = XWINDOW (selected_window)) != sw)
13315 goto retry;
13316
13317 /* We used to always goto end_of_redisplay here, but this
13318 isn't enough if we have a blinking cursor. */
13319 if (w->cursor_off_p == w->last_cursor_off_p)
13320 goto end_of_redisplay;
13321 }
13322 goto update;
13323 }
13324 /* If highlighting the region, or if the cursor is in the echo area,
13325 then we can't just move the cursor. */
13326 else if (! (!NILP (Vtransient_mark_mode)
13327 && !NILP (BVAR (current_buffer, mark_active)))
13328 && (EQ (selected_window,
13329 BVAR (current_buffer, last_selected_window))
13330 || highlight_nonselected_windows)
13331 && !w->region_showing
13332 && NILP (Vshow_trailing_whitespace)
13333 && !cursor_in_echo_area)
13334 {
13335 struct it it;
13336 struct glyph_row *row;
13337
13338 /* Skip from tlbufpos to PT and see where it is. Note that
13339 PT may be in invisible text. If so, we will end at the
13340 next visible position. */
13341 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13342 NULL, DEFAULT_FACE_ID);
13343 it.current_x = this_line_start_x;
13344 it.current_y = this_line_y;
13345 it.vpos = this_line_vpos;
13346
13347 /* The call to move_it_to stops in front of PT, but
13348 moves over before-strings. */
13349 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13350
13351 if (it.vpos == this_line_vpos
13352 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13353 row->enabled_p))
13354 {
13355 eassert (this_line_vpos == it.vpos);
13356 eassert (this_line_y == it.current_y);
13357 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13358 #ifdef GLYPH_DEBUG
13359 *w->desired_matrix->method = 0;
13360 debug_method_add (w, "optimization 3");
13361 #endif
13362 goto update;
13363 }
13364 else
13365 goto cancel;
13366 }
13367
13368 cancel:
13369 /* Text changed drastically or point moved off of line. */
13370 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13371 }
13372
13373 CHARPOS (this_line_start_pos) = 0;
13374 consider_all_windows_p |= buffer_shared_and_changed ();
13375 ++clear_face_cache_count;
13376 #ifdef HAVE_WINDOW_SYSTEM
13377 ++clear_image_cache_count;
13378 #endif
13379
13380 /* Build desired matrices, and update the display. If
13381 consider_all_windows_p is non-zero, do it for all windows on all
13382 frames. Otherwise do it for selected_window, only. */
13383
13384 if (consider_all_windows_p)
13385 {
13386 FOR_EACH_FRAME (tail, frame)
13387 XFRAME (frame)->updated_p = 0;
13388
13389 FOR_EACH_FRAME (tail, frame)
13390 {
13391 struct frame *f = XFRAME (frame);
13392
13393 /* We don't have to do anything for unselected terminal
13394 frames. */
13395 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13396 && !EQ (FRAME_TTY (f)->top_frame, frame))
13397 continue;
13398
13399 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13400 {
13401 /* Mark all the scroll bars to be removed; we'll redeem
13402 the ones we want when we redisplay their windows. */
13403 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13404 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13405
13406 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13407 redisplay_windows (FRAME_ROOT_WINDOW (f));
13408
13409 /* The X error handler may have deleted that frame. */
13410 if (!FRAME_LIVE_P (f))
13411 continue;
13412
13413 /* Any scroll bars which redisplay_windows should have
13414 nuked should now go away. */
13415 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13416 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13417
13418 /* If fonts changed, display again. */
13419 /* ??? rms: I suspect it is a mistake to jump all the way
13420 back to retry here. It should just retry this frame. */
13421 if (fonts_changed_p)
13422 goto retry;
13423
13424 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13425 {
13426 /* See if we have to hscroll. */
13427 if (!f->already_hscrolled_p)
13428 {
13429 f->already_hscrolled_p = 1;
13430 if (hscroll_windows (f->root_window))
13431 goto retry;
13432 }
13433
13434 /* Prevent various kinds of signals during display
13435 update. stdio is not robust about handling
13436 signals, which can cause an apparent I/O
13437 error. */
13438 if (interrupt_input)
13439 unrequest_sigio ();
13440 STOP_POLLING;
13441
13442 /* Update the display. */
13443 set_window_update_flags (XWINDOW (f->root_window), 1);
13444 pending |= update_frame (f, 0, 0);
13445 f->updated_p = 1;
13446 }
13447 }
13448 }
13449
13450 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13451
13452 if (!pending)
13453 {
13454 /* Do the mark_window_display_accurate after all windows have
13455 been redisplayed because this call resets flags in buffers
13456 which are needed for proper redisplay. */
13457 FOR_EACH_FRAME (tail, frame)
13458 {
13459 struct frame *f = XFRAME (frame);
13460 if (f->updated_p)
13461 {
13462 mark_window_display_accurate (f->root_window, 1);
13463 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13464 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13465 }
13466 }
13467 }
13468 }
13469 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13470 {
13471 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13472 struct frame *mini_frame;
13473
13474 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13475 /* Use list_of_error, not Qerror, so that
13476 we catch only errors and don't run the debugger. */
13477 internal_condition_case_1 (redisplay_window_1, selected_window,
13478 list_of_error,
13479 redisplay_window_error);
13480 if (update_miniwindow_p)
13481 internal_condition_case_1 (redisplay_window_1, mini_window,
13482 list_of_error,
13483 redisplay_window_error);
13484
13485 /* Compare desired and current matrices, perform output. */
13486
13487 update:
13488 /* If fonts changed, display again. */
13489 if (fonts_changed_p)
13490 goto retry;
13491
13492 /* Prevent various kinds of signals during display update.
13493 stdio is not robust about handling signals,
13494 which can cause an apparent I/O error. */
13495 if (interrupt_input)
13496 unrequest_sigio ();
13497 STOP_POLLING;
13498
13499 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13500 {
13501 if (hscroll_windows (selected_window))
13502 goto retry;
13503
13504 XWINDOW (selected_window)->must_be_updated_p = 1;
13505 pending = update_frame (sf, 0, 0);
13506 }
13507
13508 /* We may have called echo_area_display at the top of this
13509 function. If the echo area is on another frame, that may
13510 have put text on a frame other than the selected one, so the
13511 above call to update_frame would not have caught it. Catch
13512 it here. */
13513 mini_window = FRAME_MINIBUF_WINDOW (sf);
13514 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13515
13516 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13517 {
13518 XWINDOW (mini_window)->must_be_updated_p = 1;
13519 pending |= update_frame (mini_frame, 0, 0);
13520 if (!pending && hscroll_windows (mini_window))
13521 goto retry;
13522 }
13523 }
13524
13525 /* If display was paused because of pending input, make sure we do a
13526 thorough update the next time. */
13527 if (pending)
13528 {
13529 /* Prevent the optimization at the beginning of
13530 redisplay_internal that tries a single-line update of the
13531 line containing the cursor in the selected window. */
13532 CHARPOS (this_line_start_pos) = 0;
13533
13534 /* Let the overlay arrow be updated the next time. */
13535 update_overlay_arrows (0);
13536
13537 /* If we pause after scrolling, some rows in the current
13538 matrices of some windows are not valid. */
13539 if (!WINDOW_FULL_WIDTH_P (w)
13540 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13541 update_mode_lines = 1;
13542 }
13543 else
13544 {
13545 if (!consider_all_windows_p)
13546 {
13547 /* This has already been done above if
13548 consider_all_windows_p is set. */
13549 mark_window_display_accurate_1 (w, 1);
13550
13551 /* Say overlay arrows are up to date. */
13552 update_overlay_arrows (1);
13553
13554 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13555 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13556 }
13557
13558 update_mode_lines = 0;
13559 windows_or_buffers_changed = 0;
13560 cursor_type_changed = 0;
13561 }
13562
13563 /* Start SIGIO interrupts coming again. Having them off during the
13564 code above makes it less likely one will discard output, but not
13565 impossible, since there might be stuff in the system buffer here.
13566 But it is much hairier to try to do anything about that. */
13567 if (interrupt_input)
13568 request_sigio ();
13569 RESUME_POLLING;
13570
13571 /* If a frame has become visible which was not before, redisplay
13572 again, so that we display it. Expose events for such a frame
13573 (which it gets when becoming visible) don't call the parts of
13574 redisplay constructing glyphs, so simply exposing a frame won't
13575 display anything in this case. So, we have to display these
13576 frames here explicitly. */
13577 if (!pending)
13578 {
13579 int new_count = 0;
13580
13581 FOR_EACH_FRAME (tail, frame)
13582 {
13583 int this_is_visible = 0;
13584
13585 if (XFRAME (frame)->visible)
13586 this_is_visible = 1;
13587
13588 if (this_is_visible)
13589 new_count++;
13590 }
13591
13592 if (new_count != number_of_visible_frames)
13593 windows_or_buffers_changed++;
13594 }
13595
13596 /* Change frame size now if a change is pending. */
13597 do_pending_window_change (1);
13598
13599 /* If we just did a pending size change, or have additional
13600 visible frames, or selected_window changed, redisplay again. */
13601 if ((windows_or_buffers_changed && !pending)
13602 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13603 goto retry;
13604
13605 /* Clear the face and image caches.
13606
13607 We used to do this only if consider_all_windows_p. But the cache
13608 needs to be cleared if a timer creates images in the current
13609 buffer (e.g. the test case in Bug#6230). */
13610
13611 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13612 {
13613 clear_face_cache (0);
13614 clear_face_cache_count = 0;
13615 }
13616
13617 #ifdef HAVE_WINDOW_SYSTEM
13618 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13619 {
13620 clear_image_caches (Qnil);
13621 clear_image_cache_count = 0;
13622 }
13623 #endif /* HAVE_WINDOW_SYSTEM */
13624
13625 end_of_redisplay:
13626 unbind_to (count, Qnil);
13627 RESUME_POLLING;
13628 }
13629
13630
13631 /* Redisplay, but leave alone any recent echo area message unless
13632 another message has been requested in its place.
13633
13634 This is useful in situations where you need to redisplay but no
13635 user action has occurred, making it inappropriate for the message
13636 area to be cleared. See tracking_off and
13637 wait_reading_process_output for examples of these situations.
13638
13639 FROM_WHERE is an integer saying from where this function was
13640 called. This is useful for debugging. */
13641
13642 void
13643 redisplay_preserve_echo_area (int from_where)
13644 {
13645 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13646
13647 if (!NILP (echo_area_buffer[1]))
13648 {
13649 /* We have a previously displayed message, but no current
13650 message. Redisplay the previous message. */
13651 display_last_displayed_message_p = 1;
13652 redisplay_internal ();
13653 display_last_displayed_message_p = 0;
13654 }
13655 else
13656 redisplay_internal ();
13657
13658 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13659 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13660 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13661 }
13662
13663
13664 /* Function registered with record_unwind_protect in redisplay_internal. */
13665
13666 static void
13667 unwind_redisplay (void)
13668 {
13669 redisplaying_p = 0;
13670 }
13671
13672
13673 /* Mark the display of leaf window W as accurate or inaccurate.
13674 If ACCURATE_P is non-zero mark display of W as accurate. If
13675 ACCURATE_P is zero, arrange for W to be redisplayed the next
13676 time redisplay_internal is called. */
13677
13678 static void
13679 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13680 {
13681 struct buffer *b = XBUFFER (w->contents);
13682
13683 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
13684 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
13685 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13686
13687 if (accurate_p)
13688 {
13689 b->clip_changed = 0;
13690 b->prevent_redisplay_optimizations_p = 0;
13691
13692 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13693 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13694 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13695 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13696
13697 w->current_matrix->buffer = b;
13698 w->current_matrix->begv = BUF_BEGV (b);
13699 w->current_matrix->zv = BUF_ZV (b);
13700
13701 w->last_cursor = w->cursor;
13702 w->last_cursor_off_p = w->cursor_off_p;
13703
13704 if (w == XWINDOW (selected_window))
13705 w->last_point = BUF_PT (b);
13706 else
13707 w->last_point = marker_position (w->pointm);
13708
13709 w->window_end_valid = 1;
13710 w->update_mode_line = 0;
13711 }
13712 }
13713
13714
13715 /* Mark the display of windows in the window tree rooted at WINDOW as
13716 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13717 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13718 be redisplayed the next time redisplay_internal is called. */
13719
13720 void
13721 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13722 {
13723 struct window *w;
13724
13725 for (; !NILP (window); window = w->next)
13726 {
13727 w = XWINDOW (window);
13728 if (WINDOWP (w->contents))
13729 mark_window_display_accurate (w->contents, accurate_p);
13730 else
13731 mark_window_display_accurate_1 (w, accurate_p);
13732 }
13733
13734 if (accurate_p)
13735 update_overlay_arrows (1);
13736 else
13737 /* Force a thorough redisplay the next time by setting
13738 last_arrow_position and last_arrow_string to t, which is
13739 unequal to any useful value of Voverlay_arrow_... */
13740 update_overlay_arrows (-1);
13741 }
13742
13743
13744 /* Return value in display table DP (Lisp_Char_Table *) for character
13745 C. Since a display table doesn't have any parent, we don't have to
13746 follow parent. Do not call this function directly but use the
13747 macro DISP_CHAR_VECTOR. */
13748
13749 Lisp_Object
13750 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13751 {
13752 Lisp_Object val;
13753
13754 if (ASCII_CHAR_P (c))
13755 {
13756 val = dp->ascii;
13757 if (SUB_CHAR_TABLE_P (val))
13758 val = XSUB_CHAR_TABLE (val)->contents[c];
13759 }
13760 else
13761 {
13762 Lisp_Object table;
13763
13764 XSETCHAR_TABLE (table, dp);
13765 val = char_table_ref (table, c);
13766 }
13767 if (NILP (val))
13768 val = dp->defalt;
13769 return val;
13770 }
13771
13772
13773 \f
13774 /***********************************************************************
13775 Window Redisplay
13776 ***********************************************************************/
13777
13778 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13779
13780 static void
13781 redisplay_windows (Lisp_Object window)
13782 {
13783 while (!NILP (window))
13784 {
13785 struct window *w = XWINDOW (window);
13786
13787 if (WINDOWP (w->contents))
13788 redisplay_windows (w->contents);
13789 else if (BUFFERP (w->contents))
13790 {
13791 displayed_buffer = XBUFFER (w->contents);
13792 /* Use list_of_error, not Qerror, so that
13793 we catch only errors and don't run the debugger. */
13794 internal_condition_case_1 (redisplay_window_0, window,
13795 list_of_error,
13796 redisplay_window_error);
13797 }
13798
13799 window = w->next;
13800 }
13801 }
13802
13803 static Lisp_Object
13804 redisplay_window_error (Lisp_Object ignore)
13805 {
13806 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13807 return Qnil;
13808 }
13809
13810 static Lisp_Object
13811 redisplay_window_0 (Lisp_Object window)
13812 {
13813 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13814 redisplay_window (window, 0);
13815 return Qnil;
13816 }
13817
13818 static Lisp_Object
13819 redisplay_window_1 (Lisp_Object window)
13820 {
13821 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13822 redisplay_window (window, 1);
13823 return Qnil;
13824 }
13825 \f
13826
13827 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13828 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13829 which positions recorded in ROW differ from current buffer
13830 positions.
13831
13832 Return 0 if cursor is not on this row, 1 otherwise. */
13833
13834 static int
13835 set_cursor_from_row (struct window *w, struct glyph_row *row,
13836 struct glyph_matrix *matrix,
13837 ptrdiff_t delta, ptrdiff_t delta_bytes,
13838 int dy, int dvpos)
13839 {
13840 struct glyph *glyph = row->glyphs[TEXT_AREA];
13841 struct glyph *end = glyph + row->used[TEXT_AREA];
13842 struct glyph *cursor = NULL;
13843 /* The last known character position in row. */
13844 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13845 int x = row->x;
13846 ptrdiff_t pt_old = PT - delta;
13847 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13848 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13849 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13850 /* A glyph beyond the edge of TEXT_AREA which we should never
13851 touch. */
13852 struct glyph *glyphs_end = end;
13853 /* Non-zero means we've found a match for cursor position, but that
13854 glyph has the avoid_cursor_p flag set. */
13855 int match_with_avoid_cursor = 0;
13856 /* Non-zero means we've seen at least one glyph that came from a
13857 display string. */
13858 int string_seen = 0;
13859 /* Largest and smallest buffer positions seen so far during scan of
13860 glyph row. */
13861 ptrdiff_t bpos_max = pos_before;
13862 ptrdiff_t bpos_min = pos_after;
13863 /* Last buffer position covered by an overlay string with an integer
13864 `cursor' property. */
13865 ptrdiff_t bpos_covered = 0;
13866 /* Non-zero means the display string on which to display the cursor
13867 comes from a text property, not from an overlay. */
13868 int string_from_text_prop = 0;
13869
13870 /* Don't even try doing anything if called for a mode-line or
13871 header-line row, since the rest of the code isn't prepared to
13872 deal with such calamities. */
13873 eassert (!row->mode_line_p);
13874 if (row->mode_line_p)
13875 return 0;
13876
13877 /* Skip over glyphs not having an object at the start and the end of
13878 the row. These are special glyphs like truncation marks on
13879 terminal frames. */
13880 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13881 {
13882 if (!row->reversed_p)
13883 {
13884 while (glyph < end
13885 && INTEGERP (glyph->object)
13886 && glyph->charpos < 0)
13887 {
13888 x += glyph->pixel_width;
13889 ++glyph;
13890 }
13891 while (end > glyph
13892 && INTEGERP ((end - 1)->object)
13893 /* CHARPOS is zero for blanks and stretch glyphs
13894 inserted by extend_face_to_end_of_line. */
13895 && (end - 1)->charpos <= 0)
13896 --end;
13897 glyph_before = glyph - 1;
13898 glyph_after = end;
13899 }
13900 else
13901 {
13902 struct glyph *g;
13903
13904 /* If the glyph row is reversed, we need to process it from back
13905 to front, so swap the edge pointers. */
13906 glyphs_end = end = glyph - 1;
13907 glyph += row->used[TEXT_AREA] - 1;
13908
13909 while (glyph > end + 1
13910 && INTEGERP (glyph->object)
13911 && glyph->charpos < 0)
13912 {
13913 --glyph;
13914 x -= glyph->pixel_width;
13915 }
13916 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13917 --glyph;
13918 /* By default, in reversed rows we put the cursor on the
13919 rightmost (first in the reading order) glyph. */
13920 for (g = end + 1; g < glyph; g++)
13921 x += g->pixel_width;
13922 while (end < glyph
13923 && INTEGERP ((end + 1)->object)
13924 && (end + 1)->charpos <= 0)
13925 ++end;
13926 glyph_before = glyph + 1;
13927 glyph_after = end;
13928 }
13929 }
13930 else if (row->reversed_p)
13931 {
13932 /* In R2L rows that don't display text, put the cursor on the
13933 rightmost glyph. Case in point: an empty last line that is
13934 part of an R2L paragraph. */
13935 cursor = end - 1;
13936 /* Avoid placing the cursor on the last glyph of the row, where
13937 on terminal frames we hold the vertical border between
13938 adjacent windows. */
13939 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13940 && !WINDOW_RIGHTMOST_P (w)
13941 && cursor == row->glyphs[LAST_AREA] - 1)
13942 cursor--;
13943 x = -1; /* will be computed below, at label compute_x */
13944 }
13945
13946 /* Step 1: Try to find the glyph whose character position
13947 corresponds to point. If that's not possible, find 2 glyphs
13948 whose character positions are the closest to point, one before
13949 point, the other after it. */
13950 if (!row->reversed_p)
13951 while (/* not marched to end of glyph row */
13952 glyph < end
13953 /* glyph was not inserted by redisplay for internal purposes */
13954 && !INTEGERP (glyph->object))
13955 {
13956 if (BUFFERP (glyph->object))
13957 {
13958 ptrdiff_t dpos = glyph->charpos - pt_old;
13959
13960 if (glyph->charpos > bpos_max)
13961 bpos_max = glyph->charpos;
13962 if (glyph->charpos < bpos_min)
13963 bpos_min = glyph->charpos;
13964 if (!glyph->avoid_cursor_p)
13965 {
13966 /* If we hit point, we've found the glyph on which to
13967 display the cursor. */
13968 if (dpos == 0)
13969 {
13970 match_with_avoid_cursor = 0;
13971 break;
13972 }
13973 /* See if we've found a better approximation to
13974 POS_BEFORE or to POS_AFTER. */
13975 if (0 > dpos && dpos > pos_before - pt_old)
13976 {
13977 pos_before = glyph->charpos;
13978 glyph_before = glyph;
13979 }
13980 else if (0 < dpos && dpos < pos_after - pt_old)
13981 {
13982 pos_after = glyph->charpos;
13983 glyph_after = glyph;
13984 }
13985 }
13986 else if (dpos == 0)
13987 match_with_avoid_cursor = 1;
13988 }
13989 else if (STRINGP (glyph->object))
13990 {
13991 Lisp_Object chprop;
13992 ptrdiff_t glyph_pos = glyph->charpos;
13993
13994 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13995 glyph->object);
13996 if (!NILP (chprop))
13997 {
13998 /* If the string came from a `display' text property,
13999 look up the buffer position of that property and
14000 use that position to update bpos_max, as if we
14001 actually saw such a position in one of the row's
14002 glyphs. This helps with supporting integer values
14003 of `cursor' property on the display string in
14004 situations where most or all of the row's buffer
14005 text is completely covered by display properties,
14006 so that no glyph with valid buffer positions is
14007 ever seen in the row. */
14008 ptrdiff_t prop_pos =
14009 string_buffer_position_lim (glyph->object, pos_before,
14010 pos_after, 0);
14011
14012 if (prop_pos >= pos_before)
14013 bpos_max = prop_pos - 1;
14014 }
14015 if (INTEGERP (chprop))
14016 {
14017 bpos_covered = bpos_max + XINT (chprop);
14018 /* If the `cursor' property covers buffer positions up
14019 to and including point, we should display cursor on
14020 this glyph. Note that, if a `cursor' property on one
14021 of the string's characters has an integer value, we
14022 will break out of the loop below _before_ we get to
14023 the position match above. IOW, integer values of
14024 the `cursor' property override the "exact match for
14025 point" strategy of positioning the cursor. */
14026 /* Implementation note: bpos_max == pt_old when, e.g.,
14027 we are in an empty line, where bpos_max is set to
14028 MATRIX_ROW_START_CHARPOS, see above. */
14029 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14030 {
14031 cursor = glyph;
14032 break;
14033 }
14034 }
14035
14036 string_seen = 1;
14037 }
14038 x += glyph->pixel_width;
14039 ++glyph;
14040 }
14041 else if (glyph > end) /* row is reversed */
14042 while (!INTEGERP (glyph->object))
14043 {
14044 if (BUFFERP (glyph->object))
14045 {
14046 ptrdiff_t dpos = glyph->charpos - pt_old;
14047
14048 if (glyph->charpos > bpos_max)
14049 bpos_max = glyph->charpos;
14050 if (glyph->charpos < bpos_min)
14051 bpos_min = glyph->charpos;
14052 if (!glyph->avoid_cursor_p)
14053 {
14054 if (dpos == 0)
14055 {
14056 match_with_avoid_cursor = 0;
14057 break;
14058 }
14059 if (0 > dpos && dpos > pos_before - pt_old)
14060 {
14061 pos_before = glyph->charpos;
14062 glyph_before = glyph;
14063 }
14064 else if (0 < dpos && dpos < pos_after - pt_old)
14065 {
14066 pos_after = glyph->charpos;
14067 glyph_after = glyph;
14068 }
14069 }
14070 else if (dpos == 0)
14071 match_with_avoid_cursor = 1;
14072 }
14073 else if (STRINGP (glyph->object))
14074 {
14075 Lisp_Object chprop;
14076 ptrdiff_t glyph_pos = glyph->charpos;
14077
14078 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14079 glyph->object);
14080 if (!NILP (chprop))
14081 {
14082 ptrdiff_t prop_pos =
14083 string_buffer_position_lim (glyph->object, pos_before,
14084 pos_after, 0);
14085
14086 if (prop_pos >= pos_before)
14087 bpos_max = prop_pos - 1;
14088 }
14089 if (INTEGERP (chprop))
14090 {
14091 bpos_covered = bpos_max + XINT (chprop);
14092 /* If the `cursor' property covers buffer positions up
14093 to and including point, we should display cursor on
14094 this glyph. */
14095 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14096 {
14097 cursor = glyph;
14098 break;
14099 }
14100 }
14101 string_seen = 1;
14102 }
14103 --glyph;
14104 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14105 {
14106 x--; /* can't use any pixel_width */
14107 break;
14108 }
14109 x -= glyph->pixel_width;
14110 }
14111
14112 /* Step 2: If we didn't find an exact match for point, we need to
14113 look for a proper place to put the cursor among glyphs between
14114 GLYPH_BEFORE and GLYPH_AFTER. */
14115 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14116 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14117 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14118 {
14119 /* An empty line has a single glyph whose OBJECT is zero and
14120 whose CHARPOS is the position of a newline on that line.
14121 Note that on a TTY, there are more glyphs after that, which
14122 were produced by extend_face_to_end_of_line, but their
14123 CHARPOS is zero or negative. */
14124 int empty_line_p =
14125 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14126 && INTEGERP (glyph->object) && glyph->charpos > 0
14127 /* On a TTY, continued and truncated rows also have a glyph at
14128 their end whose OBJECT is zero and whose CHARPOS is
14129 positive (the continuation and truncation glyphs), but such
14130 rows are obviously not "empty". */
14131 && !(row->continued_p || row->truncated_on_right_p);
14132
14133 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14134 {
14135 ptrdiff_t ellipsis_pos;
14136
14137 /* Scan back over the ellipsis glyphs. */
14138 if (!row->reversed_p)
14139 {
14140 ellipsis_pos = (glyph - 1)->charpos;
14141 while (glyph > row->glyphs[TEXT_AREA]
14142 && (glyph - 1)->charpos == ellipsis_pos)
14143 glyph--, x -= glyph->pixel_width;
14144 /* That loop always goes one position too far, including
14145 the glyph before the ellipsis. So scan forward over
14146 that one. */
14147 x += glyph->pixel_width;
14148 glyph++;
14149 }
14150 else /* row is reversed */
14151 {
14152 ellipsis_pos = (glyph + 1)->charpos;
14153 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14154 && (glyph + 1)->charpos == ellipsis_pos)
14155 glyph++, x += glyph->pixel_width;
14156 x -= glyph->pixel_width;
14157 glyph--;
14158 }
14159 }
14160 else if (match_with_avoid_cursor)
14161 {
14162 cursor = glyph_after;
14163 x = -1;
14164 }
14165 else if (string_seen)
14166 {
14167 int incr = row->reversed_p ? -1 : +1;
14168
14169 /* Need to find the glyph that came out of a string which is
14170 present at point. That glyph is somewhere between
14171 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14172 positioned between POS_BEFORE and POS_AFTER in the
14173 buffer. */
14174 struct glyph *start, *stop;
14175 ptrdiff_t pos = pos_before;
14176
14177 x = -1;
14178
14179 /* If the row ends in a newline from a display string,
14180 reordering could have moved the glyphs belonging to the
14181 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14182 in this case we extend the search to the last glyph in
14183 the row that was not inserted by redisplay. */
14184 if (row->ends_in_newline_from_string_p)
14185 {
14186 glyph_after = end;
14187 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14188 }
14189
14190 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14191 correspond to POS_BEFORE and POS_AFTER, respectively. We
14192 need START and STOP in the order that corresponds to the
14193 row's direction as given by its reversed_p flag. If the
14194 directionality of characters between POS_BEFORE and
14195 POS_AFTER is the opposite of the row's base direction,
14196 these characters will have been reordered for display,
14197 and we need to reverse START and STOP. */
14198 if (!row->reversed_p)
14199 {
14200 start = min (glyph_before, glyph_after);
14201 stop = max (glyph_before, glyph_after);
14202 }
14203 else
14204 {
14205 start = max (glyph_before, glyph_after);
14206 stop = min (glyph_before, glyph_after);
14207 }
14208 for (glyph = start + incr;
14209 row->reversed_p ? glyph > stop : glyph < stop; )
14210 {
14211
14212 /* Any glyphs that come from the buffer are here because
14213 of bidi reordering. Skip them, and only pay
14214 attention to glyphs that came from some string. */
14215 if (STRINGP (glyph->object))
14216 {
14217 Lisp_Object str;
14218 ptrdiff_t tem;
14219 /* If the display property covers the newline, we
14220 need to search for it one position farther. */
14221 ptrdiff_t lim = pos_after
14222 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14223
14224 string_from_text_prop = 0;
14225 str = glyph->object;
14226 tem = string_buffer_position_lim (str, pos, lim, 0);
14227 if (tem == 0 /* from overlay */
14228 || pos <= tem)
14229 {
14230 /* If the string from which this glyph came is
14231 found in the buffer at point, or at position
14232 that is closer to point than pos_after, then
14233 we've found the glyph we've been looking for.
14234 If it comes from an overlay (tem == 0), and
14235 it has the `cursor' property on one of its
14236 glyphs, record that glyph as a candidate for
14237 displaying the cursor. (As in the
14238 unidirectional version, we will display the
14239 cursor on the last candidate we find.) */
14240 if (tem == 0
14241 || tem == pt_old
14242 || (tem - pt_old > 0 && tem < pos_after))
14243 {
14244 /* The glyphs from this string could have
14245 been reordered. Find the one with the
14246 smallest string position. Or there could
14247 be a character in the string with the
14248 `cursor' property, which means display
14249 cursor on that character's glyph. */
14250 ptrdiff_t strpos = glyph->charpos;
14251
14252 if (tem)
14253 {
14254 cursor = glyph;
14255 string_from_text_prop = 1;
14256 }
14257 for ( ;
14258 (row->reversed_p ? glyph > stop : glyph < stop)
14259 && EQ (glyph->object, str);
14260 glyph += incr)
14261 {
14262 Lisp_Object cprop;
14263 ptrdiff_t gpos = glyph->charpos;
14264
14265 cprop = Fget_char_property (make_number (gpos),
14266 Qcursor,
14267 glyph->object);
14268 if (!NILP (cprop))
14269 {
14270 cursor = glyph;
14271 break;
14272 }
14273 if (tem && glyph->charpos < strpos)
14274 {
14275 strpos = glyph->charpos;
14276 cursor = glyph;
14277 }
14278 }
14279
14280 if (tem == pt_old
14281 || (tem - pt_old > 0 && tem < pos_after))
14282 goto compute_x;
14283 }
14284 if (tem)
14285 pos = tem + 1; /* don't find previous instances */
14286 }
14287 /* This string is not what we want; skip all of the
14288 glyphs that came from it. */
14289 while ((row->reversed_p ? glyph > stop : glyph < stop)
14290 && EQ (glyph->object, str))
14291 glyph += incr;
14292 }
14293 else
14294 glyph += incr;
14295 }
14296
14297 /* If we reached the end of the line, and END was from a string,
14298 the cursor is not on this line. */
14299 if (cursor == NULL
14300 && (row->reversed_p ? glyph <= end : glyph >= end)
14301 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14302 && STRINGP (end->object)
14303 && row->continued_p)
14304 return 0;
14305 }
14306 /* A truncated row may not include PT among its character positions.
14307 Setting the cursor inside the scroll margin will trigger
14308 recalculation of hscroll in hscroll_window_tree. But if a
14309 display string covers point, defer to the string-handling
14310 code below to figure this out. */
14311 else if (row->truncated_on_left_p && pt_old < bpos_min)
14312 {
14313 cursor = glyph_before;
14314 x = -1;
14315 }
14316 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14317 /* Zero-width characters produce no glyphs. */
14318 || (!empty_line_p
14319 && (row->reversed_p
14320 ? glyph_after > glyphs_end
14321 : glyph_after < glyphs_end)))
14322 {
14323 cursor = glyph_after;
14324 x = -1;
14325 }
14326 }
14327
14328 compute_x:
14329 if (cursor != NULL)
14330 glyph = cursor;
14331 else if (glyph == glyphs_end
14332 && pos_before == pos_after
14333 && STRINGP ((row->reversed_p
14334 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14335 : row->glyphs[TEXT_AREA])->object))
14336 {
14337 /* If all the glyphs of this row came from strings, put the
14338 cursor on the first glyph of the row. This avoids having the
14339 cursor outside of the text area in this very rare and hard
14340 use case. */
14341 glyph =
14342 row->reversed_p
14343 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14344 : row->glyphs[TEXT_AREA];
14345 }
14346 if (x < 0)
14347 {
14348 struct glyph *g;
14349
14350 /* Need to compute x that corresponds to GLYPH. */
14351 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14352 {
14353 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14354 emacs_abort ();
14355 x += g->pixel_width;
14356 }
14357 }
14358
14359 /* ROW could be part of a continued line, which, under bidi
14360 reordering, might have other rows whose start and end charpos
14361 occlude point. Only set w->cursor if we found a better
14362 approximation to the cursor position than we have from previously
14363 examined candidate rows belonging to the same continued line. */
14364 if (/* we already have a candidate row */
14365 w->cursor.vpos >= 0
14366 /* that candidate is not the row we are processing */
14367 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14368 /* Make sure cursor.vpos specifies a row whose start and end
14369 charpos occlude point, and it is valid candidate for being a
14370 cursor-row. This is because some callers of this function
14371 leave cursor.vpos at the row where the cursor was displayed
14372 during the last redisplay cycle. */
14373 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14374 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14375 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14376 {
14377 struct glyph *g1 =
14378 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14379
14380 /* Don't consider glyphs that are outside TEXT_AREA. */
14381 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14382 return 0;
14383 /* Keep the candidate whose buffer position is the closest to
14384 point or has the `cursor' property. */
14385 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14386 w->cursor.hpos >= 0
14387 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14388 && ((BUFFERP (g1->object)
14389 && (g1->charpos == pt_old /* an exact match always wins */
14390 || (BUFFERP (glyph->object)
14391 && eabs (g1->charpos - pt_old)
14392 < eabs (glyph->charpos - pt_old))))
14393 /* previous candidate is a glyph from a string that has
14394 a non-nil `cursor' property */
14395 || (STRINGP (g1->object)
14396 && (!NILP (Fget_char_property (make_number (g1->charpos),
14397 Qcursor, g1->object))
14398 /* previous candidate is from the same display
14399 string as this one, and the display string
14400 came from a text property */
14401 || (EQ (g1->object, glyph->object)
14402 && string_from_text_prop)
14403 /* this candidate is from newline and its
14404 position is not an exact match */
14405 || (INTEGERP (glyph->object)
14406 && glyph->charpos != pt_old)))))
14407 return 0;
14408 /* If this candidate gives an exact match, use that. */
14409 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14410 /* If this candidate is a glyph created for the
14411 terminating newline of a line, and point is on that
14412 newline, it wins because it's an exact match. */
14413 || (!row->continued_p
14414 && INTEGERP (glyph->object)
14415 && glyph->charpos == 0
14416 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14417 /* Otherwise, keep the candidate that comes from a row
14418 spanning less buffer positions. This may win when one or
14419 both candidate positions are on glyphs that came from
14420 display strings, for which we cannot compare buffer
14421 positions. */
14422 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14423 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14424 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14425 return 0;
14426 }
14427 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14428 w->cursor.x = x;
14429 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14430 w->cursor.y = row->y + dy;
14431
14432 if (w == XWINDOW (selected_window))
14433 {
14434 if (!row->continued_p
14435 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14436 && row->x == 0)
14437 {
14438 this_line_buffer = XBUFFER (w->contents);
14439
14440 CHARPOS (this_line_start_pos)
14441 = MATRIX_ROW_START_CHARPOS (row) + delta;
14442 BYTEPOS (this_line_start_pos)
14443 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14444
14445 CHARPOS (this_line_end_pos)
14446 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14447 BYTEPOS (this_line_end_pos)
14448 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14449
14450 this_line_y = w->cursor.y;
14451 this_line_pixel_height = row->height;
14452 this_line_vpos = w->cursor.vpos;
14453 this_line_start_x = row->x;
14454 }
14455 else
14456 CHARPOS (this_line_start_pos) = 0;
14457 }
14458
14459 return 1;
14460 }
14461
14462
14463 /* Run window scroll functions, if any, for WINDOW with new window
14464 start STARTP. Sets the window start of WINDOW to that position.
14465
14466 We assume that the window's buffer is really current. */
14467
14468 static struct text_pos
14469 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14470 {
14471 struct window *w = XWINDOW (window);
14472 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14473
14474 eassert (current_buffer == XBUFFER (w->contents));
14475
14476 if (!NILP (Vwindow_scroll_functions))
14477 {
14478 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14479 make_number (CHARPOS (startp)));
14480 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14481 /* In case the hook functions switch buffers. */
14482 set_buffer_internal (XBUFFER (w->contents));
14483 }
14484
14485 return startp;
14486 }
14487
14488
14489 /* Make sure the line containing the cursor is fully visible.
14490 A value of 1 means there is nothing to be done.
14491 (Either the line is fully visible, or it cannot be made so,
14492 or we cannot tell.)
14493
14494 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14495 is higher than window.
14496
14497 A value of 0 means the caller should do scrolling
14498 as if point had gone off the screen. */
14499
14500 static int
14501 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14502 {
14503 struct glyph_matrix *matrix;
14504 struct glyph_row *row;
14505 int window_height;
14506
14507 if (!make_cursor_line_fully_visible_p)
14508 return 1;
14509
14510 /* It's not always possible to find the cursor, e.g, when a window
14511 is full of overlay strings. Don't do anything in that case. */
14512 if (w->cursor.vpos < 0)
14513 return 1;
14514
14515 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14516 row = MATRIX_ROW (matrix, w->cursor.vpos);
14517
14518 /* If the cursor row is not partially visible, there's nothing to do. */
14519 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14520 return 1;
14521
14522 /* If the row the cursor is in is taller than the window's height,
14523 it's not clear what to do, so do nothing. */
14524 window_height = window_box_height (w);
14525 if (row->height >= window_height)
14526 {
14527 if (!force_p || MINI_WINDOW_P (w)
14528 || w->vscroll || w->cursor.vpos == 0)
14529 return 1;
14530 }
14531 return 0;
14532 }
14533
14534
14535 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14536 non-zero means only WINDOW is redisplayed in redisplay_internal.
14537 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14538 in redisplay_window to bring a partially visible line into view in
14539 the case that only the cursor has moved.
14540
14541 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14542 last screen line's vertical height extends past the end of the screen.
14543
14544 Value is
14545
14546 1 if scrolling succeeded
14547
14548 0 if scrolling didn't find point.
14549
14550 -1 if new fonts have been loaded so that we must interrupt
14551 redisplay, adjust glyph matrices, and try again. */
14552
14553 enum
14554 {
14555 SCROLLING_SUCCESS,
14556 SCROLLING_FAILED,
14557 SCROLLING_NEED_LARGER_MATRICES
14558 };
14559
14560 /* If scroll-conservatively is more than this, never recenter.
14561
14562 If you change this, don't forget to update the doc string of
14563 `scroll-conservatively' and the Emacs manual. */
14564 #define SCROLL_LIMIT 100
14565
14566 static int
14567 try_scrolling (Lisp_Object window, int just_this_one_p,
14568 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14569 int temp_scroll_step, int last_line_misfit)
14570 {
14571 struct window *w = XWINDOW (window);
14572 struct frame *f = XFRAME (w->frame);
14573 struct text_pos pos, startp;
14574 struct it it;
14575 int this_scroll_margin, scroll_max, rc, height;
14576 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14577 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14578 Lisp_Object aggressive;
14579 /* We will never try scrolling more than this number of lines. */
14580 int scroll_limit = SCROLL_LIMIT;
14581 int frame_line_height = default_line_pixel_height (w);
14582 int window_total_lines
14583 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14584
14585 #ifdef GLYPH_DEBUG
14586 debug_method_add (w, "try_scrolling");
14587 #endif
14588
14589 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14590
14591 /* Compute scroll margin height in pixels. We scroll when point is
14592 within this distance from the top or bottom of the window. */
14593 if (scroll_margin > 0)
14594 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
14595 * frame_line_height;
14596 else
14597 this_scroll_margin = 0;
14598
14599 /* Force arg_scroll_conservatively to have a reasonable value, to
14600 avoid scrolling too far away with slow move_it_* functions. Note
14601 that the user can supply scroll-conservatively equal to
14602 `most-positive-fixnum', which can be larger than INT_MAX. */
14603 if (arg_scroll_conservatively > scroll_limit)
14604 {
14605 arg_scroll_conservatively = scroll_limit + 1;
14606 scroll_max = scroll_limit * frame_line_height;
14607 }
14608 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14609 /* Compute how much we should try to scroll maximally to bring
14610 point into view. */
14611 scroll_max = (max (scroll_step,
14612 max (arg_scroll_conservatively, temp_scroll_step))
14613 * frame_line_height);
14614 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14615 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14616 /* We're trying to scroll because of aggressive scrolling but no
14617 scroll_step is set. Choose an arbitrary one. */
14618 scroll_max = 10 * frame_line_height;
14619 else
14620 scroll_max = 0;
14621
14622 too_near_end:
14623
14624 /* Decide whether to scroll down. */
14625 if (PT > CHARPOS (startp))
14626 {
14627 int scroll_margin_y;
14628
14629 /* Compute the pixel ypos of the scroll margin, then move IT to
14630 either that ypos or PT, whichever comes first. */
14631 start_display (&it, w, startp);
14632 scroll_margin_y = it.last_visible_y - this_scroll_margin
14633 - frame_line_height * extra_scroll_margin_lines;
14634 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14635 (MOVE_TO_POS | MOVE_TO_Y));
14636
14637 if (PT > CHARPOS (it.current.pos))
14638 {
14639 int y0 = line_bottom_y (&it);
14640 /* Compute how many pixels below window bottom to stop searching
14641 for PT. This avoids costly search for PT that is far away if
14642 the user limited scrolling by a small number of lines, but
14643 always finds PT if scroll_conservatively is set to a large
14644 number, such as most-positive-fixnum. */
14645 int slack = max (scroll_max, 10 * frame_line_height);
14646 int y_to_move = it.last_visible_y + slack;
14647
14648 /* Compute the distance from the scroll margin to PT or to
14649 the scroll limit, whichever comes first. This should
14650 include the height of the cursor line, to make that line
14651 fully visible. */
14652 move_it_to (&it, PT, -1, y_to_move,
14653 -1, MOVE_TO_POS | MOVE_TO_Y);
14654 dy = line_bottom_y (&it) - y0;
14655
14656 if (dy > scroll_max)
14657 return SCROLLING_FAILED;
14658
14659 if (dy > 0)
14660 scroll_down_p = 1;
14661 }
14662 }
14663
14664 if (scroll_down_p)
14665 {
14666 /* Point is in or below the bottom scroll margin, so move the
14667 window start down. If scrolling conservatively, move it just
14668 enough down to make point visible. If scroll_step is set,
14669 move it down by scroll_step. */
14670 if (arg_scroll_conservatively)
14671 amount_to_scroll
14672 = min (max (dy, frame_line_height),
14673 frame_line_height * arg_scroll_conservatively);
14674 else if (scroll_step || temp_scroll_step)
14675 amount_to_scroll = scroll_max;
14676 else
14677 {
14678 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14679 height = WINDOW_BOX_TEXT_HEIGHT (w);
14680 if (NUMBERP (aggressive))
14681 {
14682 double float_amount = XFLOATINT (aggressive) * height;
14683 int aggressive_scroll = float_amount;
14684 if (aggressive_scroll == 0 && float_amount > 0)
14685 aggressive_scroll = 1;
14686 /* Don't let point enter the scroll margin near top of
14687 the window. This could happen if the value of
14688 scroll_up_aggressively is too large and there are
14689 non-zero margins, because scroll_up_aggressively
14690 means put point that fraction of window height
14691 _from_the_bottom_margin_. */
14692 if (aggressive_scroll + 2*this_scroll_margin > height)
14693 aggressive_scroll = height - 2*this_scroll_margin;
14694 amount_to_scroll = dy + aggressive_scroll;
14695 }
14696 }
14697
14698 if (amount_to_scroll <= 0)
14699 return SCROLLING_FAILED;
14700
14701 start_display (&it, w, startp);
14702 if (arg_scroll_conservatively <= scroll_limit)
14703 move_it_vertically (&it, amount_to_scroll);
14704 else
14705 {
14706 /* Extra precision for users who set scroll-conservatively
14707 to a large number: make sure the amount we scroll
14708 the window start is never less than amount_to_scroll,
14709 which was computed as distance from window bottom to
14710 point. This matters when lines at window top and lines
14711 below window bottom have different height. */
14712 struct it it1;
14713 void *it1data = NULL;
14714 /* We use a temporary it1 because line_bottom_y can modify
14715 its argument, if it moves one line down; see there. */
14716 int start_y;
14717
14718 SAVE_IT (it1, it, it1data);
14719 start_y = line_bottom_y (&it1);
14720 do {
14721 RESTORE_IT (&it, &it, it1data);
14722 move_it_by_lines (&it, 1);
14723 SAVE_IT (it1, it, it1data);
14724 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14725 }
14726
14727 /* If STARTP is unchanged, move it down another screen line. */
14728 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14729 move_it_by_lines (&it, 1);
14730 startp = it.current.pos;
14731 }
14732 else
14733 {
14734 struct text_pos scroll_margin_pos = startp;
14735 int y_offset = 0;
14736
14737 /* See if point is inside the scroll margin at the top of the
14738 window. */
14739 if (this_scroll_margin)
14740 {
14741 int y_start;
14742
14743 start_display (&it, w, startp);
14744 y_start = it.current_y;
14745 move_it_vertically (&it, this_scroll_margin);
14746 scroll_margin_pos = it.current.pos;
14747 /* If we didn't move enough before hitting ZV, request
14748 additional amount of scroll, to move point out of the
14749 scroll margin. */
14750 if (IT_CHARPOS (it) == ZV
14751 && it.current_y - y_start < this_scroll_margin)
14752 y_offset = this_scroll_margin - (it.current_y - y_start);
14753 }
14754
14755 if (PT < CHARPOS (scroll_margin_pos))
14756 {
14757 /* Point is in the scroll margin at the top of the window or
14758 above what is displayed in the window. */
14759 int y0, y_to_move;
14760
14761 /* Compute the vertical distance from PT to the scroll
14762 margin position. Move as far as scroll_max allows, or
14763 one screenful, or 10 screen lines, whichever is largest.
14764 Give up if distance is greater than scroll_max or if we
14765 didn't reach the scroll margin position. */
14766 SET_TEXT_POS (pos, PT, PT_BYTE);
14767 start_display (&it, w, pos);
14768 y0 = it.current_y;
14769 y_to_move = max (it.last_visible_y,
14770 max (scroll_max, 10 * frame_line_height));
14771 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14772 y_to_move, -1,
14773 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14774 dy = it.current_y - y0;
14775 if (dy > scroll_max
14776 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
14777 return SCROLLING_FAILED;
14778
14779 /* Additional scroll for when ZV was too close to point. */
14780 dy += y_offset;
14781
14782 /* Compute new window start. */
14783 start_display (&it, w, startp);
14784
14785 if (arg_scroll_conservatively)
14786 amount_to_scroll = max (dy, frame_line_height *
14787 max (scroll_step, temp_scroll_step));
14788 else if (scroll_step || temp_scroll_step)
14789 amount_to_scroll = scroll_max;
14790 else
14791 {
14792 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14793 height = WINDOW_BOX_TEXT_HEIGHT (w);
14794 if (NUMBERP (aggressive))
14795 {
14796 double float_amount = XFLOATINT (aggressive) * height;
14797 int aggressive_scroll = float_amount;
14798 if (aggressive_scroll == 0 && float_amount > 0)
14799 aggressive_scroll = 1;
14800 /* Don't let point enter the scroll margin near
14801 bottom of the window, if the value of
14802 scroll_down_aggressively happens to be too
14803 large. */
14804 if (aggressive_scroll + 2*this_scroll_margin > height)
14805 aggressive_scroll = height - 2*this_scroll_margin;
14806 amount_to_scroll = dy + aggressive_scroll;
14807 }
14808 }
14809
14810 if (amount_to_scroll <= 0)
14811 return SCROLLING_FAILED;
14812
14813 move_it_vertically_backward (&it, amount_to_scroll);
14814 startp = it.current.pos;
14815 }
14816 }
14817
14818 /* Run window scroll functions. */
14819 startp = run_window_scroll_functions (window, startp);
14820
14821 /* Display the window. Give up if new fonts are loaded, or if point
14822 doesn't appear. */
14823 if (!try_window (window, startp, 0))
14824 rc = SCROLLING_NEED_LARGER_MATRICES;
14825 else if (w->cursor.vpos < 0)
14826 {
14827 clear_glyph_matrix (w->desired_matrix);
14828 rc = SCROLLING_FAILED;
14829 }
14830 else
14831 {
14832 /* Maybe forget recorded base line for line number display. */
14833 if (!just_this_one_p
14834 || current_buffer->clip_changed
14835 || BEG_UNCHANGED < CHARPOS (startp))
14836 w->base_line_number = 0;
14837
14838 /* If cursor ends up on a partially visible line,
14839 treat that as being off the bottom of the screen. */
14840 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14841 /* It's possible that the cursor is on the first line of the
14842 buffer, which is partially obscured due to a vscroll
14843 (Bug#7537). In that case, avoid looping forever . */
14844 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14845 {
14846 clear_glyph_matrix (w->desired_matrix);
14847 ++extra_scroll_margin_lines;
14848 goto too_near_end;
14849 }
14850 rc = SCROLLING_SUCCESS;
14851 }
14852
14853 return rc;
14854 }
14855
14856
14857 /* Compute a suitable window start for window W if display of W starts
14858 on a continuation line. Value is non-zero if a new window start
14859 was computed.
14860
14861 The new window start will be computed, based on W's width, starting
14862 from the start of the continued line. It is the start of the
14863 screen line with the minimum distance from the old start W->start. */
14864
14865 static int
14866 compute_window_start_on_continuation_line (struct window *w)
14867 {
14868 struct text_pos pos, start_pos;
14869 int window_start_changed_p = 0;
14870
14871 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14872
14873 /* If window start is on a continuation line... Window start may be
14874 < BEGV in case there's invisible text at the start of the
14875 buffer (M-x rmail, for example). */
14876 if (CHARPOS (start_pos) > BEGV
14877 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14878 {
14879 struct it it;
14880 struct glyph_row *row;
14881
14882 /* Handle the case that the window start is out of range. */
14883 if (CHARPOS (start_pos) < BEGV)
14884 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14885 else if (CHARPOS (start_pos) > ZV)
14886 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14887
14888 /* Find the start of the continued line. This should be fast
14889 because find_newline is fast (newline cache). */
14890 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14891 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14892 row, DEFAULT_FACE_ID);
14893 reseat_at_previous_visible_line_start (&it);
14894
14895 /* If the line start is "too far" away from the window start,
14896 say it takes too much time to compute a new window start. */
14897 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14898 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14899 {
14900 int min_distance, distance;
14901
14902 /* Move forward by display lines to find the new window
14903 start. If window width was enlarged, the new start can
14904 be expected to be > the old start. If window width was
14905 decreased, the new window start will be < the old start.
14906 So, we're looking for the display line start with the
14907 minimum distance from the old window start. */
14908 pos = it.current.pos;
14909 min_distance = INFINITY;
14910 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14911 distance < min_distance)
14912 {
14913 min_distance = distance;
14914 pos = it.current.pos;
14915 move_it_by_lines (&it, 1);
14916 }
14917
14918 /* Set the window start there. */
14919 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14920 window_start_changed_p = 1;
14921 }
14922 }
14923
14924 return window_start_changed_p;
14925 }
14926
14927
14928 /* Try cursor movement in case text has not changed in window WINDOW,
14929 with window start STARTP. Value is
14930
14931 CURSOR_MOVEMENT_SUCCESS if successful
14932
14933 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14934
14935 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14936 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14937 we want to scroll as if scroll-step were set to 1. See the code.
14938
14939 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14940 which case we have to abort this redisplay, and adjust matrices
14941 first. */
14942
14943 enum
14944 {
14945 CURSOR_MOVEMENT_SUCCESS,
14946 CURSOR_MOVEMENT_CANNOT_BE_USED,
14947 CURSOR_MOVEMENT_MUST_SCROLL,
14948 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14949 };
14950
14951 static int
14952 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14953 {
14954 struct window *w = XWINDOW (window);
14955 struct frame *f = XFRAME (w->frame);
14956 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14957
14958 #ifdef GLYPH_DEBUG
14959 if (inhibit_try_cursor_movement)
14960 return rc;
14961 #endif
14962
14963 /* Previously, there was a check for Lisp integer in the
14964 if-statement below. Now, this field is converted to
14965 ptrdiff_t, thus zero means invalid position in a buffer. */
14966 eassert (w->last_point > 0);
14967 /* Likewise there was a check whether window_end_vpos is nil or larger
14968 than the window. Now window_end_vpos is int and so never nil, but
14969 let's leave eassert to check whether it fits in the window. */
14970 eassert (w->window_end_vpos < w->current_matrix->nrows);
14971
14972 /* Handle case where text has not changed, only point, and it has
14973 not moved off the frame. */
14974 if (/* Point may be in this window. */
14975 PT >= CHARPOS (startp)
14976 /* Selective display hasn't changed. */
14977 && !current_buffer->clip_changed
14978 /* Function force-mode-line-update is used to force a thorough
14979 redisplay. It sets either windows_or_buffers_changed or
14980 update_mode_lines. So don't take a shortcut here for these
14981 cases. */
14982 && !update_mode_lines
14983 && !windows_or_buffers_changed
14984 && !cursor_type_changed
14985 /* Can't use this case if highlighting a region. When a
14986 region exists, cursor movement has to do more than just
14987 set the cursor. */
14988 && markpos_of_region () < 0
14989 && !w->region_showing
14990 && NILP (Vshow_trailing_whitespace)
14991 /* This code is not used for mini-buffer for the sake of the case
14992 of redisplaying to replace an echo area message; since in
14993 that case the mini-buffer contents per se are usually
14994 unchanged. This code is of no real use in the mini-buffer
14995 since the handling of this_line_start_pos, etc., in redisplay
14996 handles the same cases. */
14997 && !EQ (window, minibuf_window)
14998 && (FRAME_WINDOW_P (f)
14999 || !overlay_arrow_in_current_buffer_p ()))
15000 {
15001 int this_scroll_margin, top_scroll_margin;
15002 struct glyph_row *row = NULL;
15003 int frame_line_height = default_line_pixel_height (w);
15004 int window_total_lines
15005 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15006
15007 #ifdef GLYPH_DEBUG
15008 debug_method_add (w, "cursor movement");
15009 #endif
15010
15011 /* Scroll if point within this distance from the top or bottom
15012 of the window. This is a pixel value. */
15013 if (scroll_margin > 0)
15014 {
15015 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15016 this_scroll_margin *= frame_line_height;
15017 }
15018 else
15019 this_scroll_margin = 0;
15020
15021 top_scroll_margin = this_scroll_margin;
15022 if (WINDOW_WANTS_HEADER_LINE_P (w))
15023 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15024
15025 /* Start with the row the cursor was displayed during the last
15026 not paused redisplay. Give up if that row is not valid. */
15027 if (w->last_cursor.vpos < 0
15028 || w->last_cursor.vpos >= w->current_matrix->nrows)
15029 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15030 else
15031 {
15032 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
15033 if (row->mode_line_p)
15034 ++row;
15035 if (!row->enabled_p)
15036 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15037 }
15038
15039 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15040 {
15041 int scroll_p = 0, must_scroll = 0;
15042 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15043
15044 if (PT > w->last_point)
15045 {
15046 /* Point has moved forward. */
15047 while (MATRIX_ROW_END_CHARPOS (row) < PT
15048 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15049 {
15050 eassert (row->enabled_p);
15051 ++row;
15052 }
15053
15054 /* If the end position of a row equals the start
15055 position of the next row, and PT is at that position,
15056 we would rather display cursor in the next line. */
15057 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15058 && MATRIX_ROW_END_CHARPOS (row) == PT
15059 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15060 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15061 && !cursor_row_p (row))
15062 ++row;
15063
15064 /* If within the scroll margin, scroll. Note that
15065 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15066 the next line would be drawn, and that
15067 this_scroll_margin can be zero. */
15068 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15069 || PT > MATRIX_ROW_END_CHARPOS (row)
15070 /* Line is completely visible last line in window
15071 and PT is to be set in the next line. */
15072 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15073 && PT == MATRIX_ROW_END_CHARPOS (row)
15074 && !row->ends_at_zv_p
15075 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15076 scroll_p = 1;
15077 }
15078 else if (PT < w->last_point)
15079 {
15080 /* Cursor has to be moved backward. Note that PT >=
15081 CHARPOS (startp) because of the outer if-statement. */
15082 while (!row->mode_line_p
15083 && (MATRIX_ROW_START_CHARPOS (row) > PT
15084 || (MATRIX_ROW_START_CHARPOS (row) == PT
15085 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15086 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15087 row > w->current_matrix->rows
15088 && (row-1)->ends_in_newline_from_string_p))))
15089 && (row->y > top_scroll_margin
15090 || CHARPOS (startp) == BEGV))
15091 {
15092 eassert (row->enabled_p);
15093 --row;
15094 }
15095
15096 /* Consider the following case: Window starts at BEGV,
15097 there is invisible, intangible text at BEGV, so that
15098 display starts at some point START > BEGV. It can
15099 happen that we are called with PT somewhere between
15100 BEGV and START. Try to handle that case. */
15101 if (row < w->current_matrix->rows
15102 || row->mode_line_p)
15103 {
15104 row = w->current_matrix->rows;
15105 if (row->mode_line_p)
15106 ++row;
15107 }
15108
15109 /* Due to newlines in overlay strings, we may have to
15110 skip forward over overlay strings. */
15111 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15112 && MATRIX_ROW_END_CHARPOS (row) == PT
15113 && !cursor_row_p (row))
15114 ++row;
15115
15116 /* If within the scroll margin, scroll. */
15117 if (row->y < top_scroll_margin
15118 && CHARPOS (startp) != BEGV)
15119 scroll_p = 1;
15120 }
15121 else
15122 {
15123 /* Cursor did not move. So don't scroll even if cursor line
15124 is partially visible, as it was so before. */
15125 rc = CURSOR_MOVEMENT_SUCCESS;
15126 }
15127
15128 if (PT < MATRIX_ROW_START_CHARPOS (row)
15129 || PT > MATRIX_ROW_END_CHARPOS (row))
15130 {
15131 /* if PT is not in the glyph row, give up. */
15132 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15133 must_scroll = 1;
15134 }
15135 else if (rc != CURSOR_MOVEMENT_SUCCESS
15136 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15137 {
15138 struct glyph_row *row1;
15139
15140 /* If rows are bidi-reordered and point moved, back up
15141 until we find a row that does not belong to a
15142 continuation line. This is because we must consider
15143 all rows of a continued line as candidates for the
15144 new cursor positioning, since row start and end
15145 positions change non-linearly with vertical position
15146 in such rows. */
15147 /* FIXME: Revisit this when glyph ``spilling'' in
15148 continuation lines' rows is implemented for
15149 bidi-reordered rows. */
15150 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15151 MATRIX_ROW_CONTINUATION_LINE_P (row);
15152 --row)
15153 {
15154 /* If we hit the beginning of the displayed portion
15155 without finding the first row of a continued
15156 line, give up. */
15157 if (row <= row1)
15158 {
15159 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15160 break;
15161 }
15162 eassert (row->enabled_p);
15163 }
15164 }
15165 if (must_scroll)
15166 ;
15167 else if (rc != CURSOR_MOVEMENT_SUCCESS
15168 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15169 /* Make sure this isn't a header line by any chance, since
15170 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15171 && !row->mode_line_p
15172 && make_cursor_line_fully_visible_p)
15173 {
15174 if (PT == MATRIX_ROW_END_CHARPOS (row)
15175 && !row->ends_at_zv_p
15176 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15177 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15178 else if (row->height > window_box_height (w))
15179 {
15180 /* If we end up in a partially visible line, let's
15181 make it fully visible, except when it's taller
15182 than the window, in which case we can't do much
15183 about it. */
15184 *scroll_step = 1;
15185 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15186 }
15187 else
15188 {
15189 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15190 if (!cursor_row_fully_visible_p (w, 0, 1))
15191 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15192 else
15193 rc = CURSOR_MOVEMENT_SUCCESS;
15194 }
15195 }
15196 else if (scroll_p)
15197 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15198 else if (rc != CURSOR_MOVEMENT_SUCCESS
15199 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15200 {
15201 /* With bidi-reordered rows, there could be more than
15202 one candidate row whose start and end positions
15203 occlude point. We need to let set_cursor_from_row
15204 find the best candidate. */
15205 /* FIXME: Revisit this when glyph ``spilling'' in
15206 continuation lines' rows is implemented for
15207 bidi-reordered rows. */
15208 int rv = 0;
15209
15210 do
15211 {
15212 int at_zv_p = 0, exact_match_p = 0;
15213
15214 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15215 && PT <= MATRIX_ROW_END_CHARPOS (row)
15216 && cursor_row_p (row))
15217 rv |= set_cursor_from_row (w, row, w->current_matrix,
15218 0, 0, 0, 0);
15219 /* As soon as we've found the exact match for point,
15220 or the first suitable row whose ends_at_zv_p flag
15221 is set, we are done. */
15222 at_zv_p =
15223 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15224 if (rv && !at_zv_p
15225 && w->cursor.hpos >= 0
15226 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15227 w->cursor.vpos))
15228 {
15229 struct glyph_row *candidate =
15230 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15231 struct glyph *g =
15232 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15233 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15234
15235 exact_match_p =
15236 (BUFFERP (g->object) && g->charpos == PT)
15237 || (INTEGERP (g->object)
15238 && (g->charpos == PT
15239 || (g->charpos == 0 && endpos - 1 == PT)));
15240 }
15241 if (rv && (at_zv_p || exact_match_p))
15242 {
15243 rc = CURSOR_MOVEMENT_SUCCESS;
15244 break;
15245 }
15246 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15247 break;
15248 ++row;
15249 }
15250 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15251 || row->continued_p)
15252 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15253 || (MATRIX_ROW_START_CHARPOS (row) == PT
15254 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15255 /* If we didn't find any candidate rows, or exited the
15256 loop before all the candidates were examined, signal
15257 to the caller that this method failed. */
15258 if (rc != CURSOR_MOVEMENT_SUCCESS
15259 && !(rv
15260 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15261 && !row->continued_p))
15262 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15263 else if (rv)
15264 rc = CURSOR_MOVEMENT_SUCCESS;
15265 }
15266 else
15267 {
15268 do
15269 {
15270 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15271 {
15272 rc = CURSOR_MOVEMENT_SUCCESS;
15273 break;
15274 }
15275 ++row;
15276 }
15277 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15278 && MATRIX_ROW_START_CHARPOS (row) == PT
15279 && cursor_row_p (row));
15280 }
15281 }
15282 }
15283
15284 return rc;
15285 }
15286
15287 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15288 static
15289 #endif
15290 void
15291 set_vertical_scroll_bar (struct window *w)
15292 {
15293 ptrdiff_t start, end, whole;
15294
15295 /* Calculate the start and end positions for the current window.
15296 At some point, it would be nice to choose between scrollbars
15297 which reflect the whole buffer size, with special markers
15298 indicating narrowing, and scrollbars which reflect only the
15299 visible region.
15300
15301 Note that mini-buffers sometimes aren't displaying any text. */
15302 if (!MINI_WINDOW_P (w)
15303 || (w == XWINDOW (minibuf_window)
15304 && NILP (echo_area_buffer[0])))
15305 {
15306 struct buffer *buf = XBUFFER (w->contents);
15307 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15308 start = marker_position (w->start) - BUF_BEGV (buf);
15309 /* I don't think this is guaranteed to be right. For the
15310 moment, we'll pretend it is. */
15311 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15312
15313 if (end < start)
15314 end = start;
15315 if (whole < (end - start))
15316 whole = end - start;
15317 }
15318 else
15319 start = end = whole = 0;
15320
15321 /* Indicate what this scroll bar ought to be displaying now. */
15322 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15323 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15324 (w, end - start, whole, start);
15325 }
15326
15327
15328 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15329 selected_window is redisplayed.
15330
15331 We can return without actually redisplaying the window if
15332 fonts_changed_p. In that case, redisplay_internal will
15333 retry. */
15334
15335 static void
15336 redisplay_window (Lisp_Object window, int just_this_one_p)
15337 {
15338 struct window *w = XWINDOW (window);
15339 struct frame *f = XFRAME (w->frame);
15340 struct buffer *buffer = XBUFFER (w->contents);
15341 struct buffer *old = current_buffer;
15342 struct text_pos lpoint, opoint, startp;
15343 int update_mode_line;
15344 int tem;
15345 struct it it;
15346 /* Record it now because it's overwritten. */
15347 int current_matrix_up_to_date_p = 0;
15348 int used_current_matrix_p = 0;
15349 /* This is less strict than current_matrix_up_to_date_p.
15350 It indicates that the buffer contents and narrowing are unchanged. */
15351 int buffer_unchanged_p = 0;
15352 int temp_scroll_step = 0;
15353 ptrdiff_t count = SPECPDL_INDEX ();
15354 int rc;
15355 int centering_position = -1;
15356 int last_line_misfit = 0;
15357 ptrdiff_t beg_unchanged, end_unchanged;
15358 int frame_line_height;
15359
15360 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15361 opoint = lpoint;
15362
15363 #ifdef GLYPH_DEBUG
15364 *w->desired_matrix->method = 0;
15365 #endif
15366
15367 /* Make sure that both W's markers are valid. */
15368 eassert (XMARKER (w->start)->buffer == buffer);
15369 eassert (XMARKER (w->pointm)->buffer == buffer);
15370
15371 restart:
15372 reconsider_clip_changes (w);
15373 frame_line_height = default_line_pixel_height (w);
15374
15375 /* Has the mode line to be updated? */
15376 update_mode_line = (w->update_mode_line
15377 || update_mode_lines
15378 || buffer->clip_changed
15379 || buffer->prevent_redisplay_optimizations_p);
15380
15381 if (MINI_WINDOW_P (w))
15382 {
15383 if (w == XWINDOW (echo_area_window)
15384 && !NILP (echo_area_buffer[0]))
15385 {
15386 if (update_mode_line)
15387 /* We may have to update a tty frame's menu bar or a
15388 tool-bar. Example `M-x C-h C-h C-g'. */
15389 goto finish_menu_bars;
15390 else
15391 /* We've already displayed the echo area glyphs in this window. */
15392 goto finish_scroll_bars;
15393 }
15394 else if ((w != XWINDOW (minibuf_window)
15395 || minibuf_level == 0)
15396 /* When buffer is nonempty, redisplay window normally. */
15397 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15398 /* Quail displays non-mini buffers in minibuffer window.
15399 In that case, redisplay the window normally. */
15400 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15401 {
15402 /* W is a mini-buffer window, but it's not active, so clear
15403 it. */
15404 int yb = window_text_bottom_y (w);
15405 struct glyph_row *row;
15406 int y;
15407
15408 for (y = 0, row = w->desired_matrix->rows;
15409 y < yb;
15410 y += row->height, ++row)
15411 blank_row (w, row, y);
15412 goto finish_scroll_bars;
15413 }
15414
15415 clear_glyph_matrix (w->desired_matrix);
15416 }
15417
15418 /* Otherwise set up data on this window; select its buffer and point
15419 value. */
15420 /* Really select the buffer, for the sake of buffer-local
15421 variables. */
15422 set_buffer_internal_1 (XBUFFER (w->contents));
15423
15424 current_matrix_up_to_date_p
15425 = (w->window_end_valid
15426 && !current_buffer->clip_changed
15427 && !current_buffer->prevent_redisplay_optimizations_p
15428 && !window_outdated (w));
15429
15430 /* Run the window-bottom-change-functions
15431 if it is possible that the text on the screen has changed
15432 (either due to modification of the text, or any other reason). */
15433 if (!current_matrix_up_to_date_p
15434 && !NILP (Vwindow_text_change_functions))
15435 {
15436 safe_run_hooks (Qwindow_text_change_functions);
15437 goto restart;
15438 }
15439
15440 beg_unchanged = BEG_UNCHANGED;
15441 end_unchanged = END_UNCHANGED;
15442
15443 SET_TEXT_POS (opoint, PT, PT_BYTE);
15444
15445 specbind (Qinhibit_point_motion_hooks, Qt);
15446
15447 buffer_unchanged_p
15448 = (w->window_end_valid
15449 && !current_buffer->clip_changed
15450 && !window_outdated (w));
15451
15452 /* When windows_or_buffers_changed is non-zero, we can't rely on
15453 the window end being valid, so set it to nil there. */
15454 if (windows_or_buffers_changed)
15455 {
15456 /* If window starts on a continuation line, maybe adjust the
15457 window start in case the window's width changed. */
15458 if (XMARKER (w->start)->buffer == current_buffer)
15459 compute_window_start_on_continuation_line (w);
15460
15461 w->window_end_valid = 0;
15462 }
15463
15464 /* Some sanity checks. */
15465 CHECK_WINDOW_END (w);
15466 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15467 emacs_abort ();
15468 if (BYTEPOS (opoint) < CHARPOS (opoint))
15469 emacs_abort ();
15470
15471 if (mode_line_update_needed (w))
15472 update_mode_line = 1;
15473
15474 /* Point refers normally to the selected window. For any other
15475 window, set up appropriate value. */
15476 if (!EQ (window, selected_window))
15477 {
15478 ptrdiff_t new_pt = marker_position (w->pointm);
15479 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15480 if (new_pt < BEGV)
15481 {
15482 new_pt = BEGV;
15483 new_pt_byte = BEGV_BYTE;
15484 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15485 }
15486 else if (new_pt > (ZV - 1))
15487 {
15488 new_pt = ZV;
15489 new_pt_byte = ZV_BYTE;
15490 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15491 }
15492
15493 /* We don't use SET_PT so that the point-motion hooks don't run. */
15494 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15495 }
15496
15497 /* If any of the character widths specified in the display table
15498 have changed, invalidate the width run cache. It's true that
15499 this may be a bit late to catch such changes, but the rest of
15500 redisplay goes (non-fatally) haywire when the display table is
15501 changed, so why should we worry about doing any better? */
15502 if (current_buffer->width_run_cache)
15503 {
15504 struct Lisp_Char_Table *disptab = buffer_display_table ();
15505
15506 if (! disptab_matches_widthtab
15507 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15508 {
15509 invalidate_region_cache (current_buffer,
15510 current_buffer->width_run_cache,
15511 BEG, Z);
15512 recompute_width_table (current_buffer, disptab);
15513 }
15514 }
15515
15516 /* If window-start is screwed up, choose a new one. */
15517 if (XMARKER (w->start)->buffer != current_buffer)
15518 goto recenter;
15519
15520 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15521
15522 /* If someone specified a new starting point but did not insist,
15523 check whether it can be used. */
15524 if (w->optional_new_start
15525 && CHARPOS (startp) >= BEGV
15526 && CHARPOS (startp) <= ZV)
15527 {
15528 w->optional_new_start = 0;
15529 start_display (&it, w, startp);
15530 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15531 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15532 if (IT_CHARPOS (it) == PT)
15533 w->force_start = 1;
15534 /* IT may overshoot PT if text at PT is invisible. */
15535 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15536 w->force_start = 1;
15537 }
15538
15539 force_start:
15540
15541 /* Handle case where place to start displaying has been specified,
15542 unless the specified location is outside the accessible range. */
15543 if (w->force_start || window_frozen_p (w))
15544 {
15545 /* We set this later on if we have to adjust point. */
15546 int new_vpos = -1;
15547
15548 w->force_start = 0;
15549 w->vscroll = 0;
15550 w->window_end_valid = 0;
15551
15552 /* Forget any recorded base line for line number display. */
15553 if (!buffer_unchanged_p)
15554 w->base_line_number = 0;
15555
15556 /* Redisplay the mode line. Select the buffer properly for that.
15557 Also, run the hook window-scroll-functions
15558 because we have scrolled. */
15559 /* Note, we do this after clearing force_start because
15560 if there's an error, it is better to forget about force_start
15561 than to get into an infinite loop calling the hook functions
15562 and having them get more errors. */
15563 if (!update_mode_line
15564 || ! NILP (Vwindow_scroll_functions))
15565 {
15566 update_mode_line = 1;
15567 w->update_mode_line = 1;
15568 startp = run_window_scroll_functions (window, startp);
15569 }
15570
15571 if (CHARPOS (startp) < BEGV)
15572 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15573 else if (CHARPOS (startp) > ZV)
15574 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15575
15576 /* Redisplay, then check if cursor has been set during the
15577 redisplay. Give up if new fonts were loaded. */
15578 /* We used to issue a CHECK_MARGINS argument to try_window here,
15579 but this causes scrolling to fail when point begins inside
15580 the scroll margin (bug#148) -- cyd */
15581 if (!try_window (window, startp, 0))
15582 {
15583 w->force_start = 1;
15584 clear_glyph_matrix (w->desired_matrix);
15585 goto need_larger_matrices;
15586 }
15587
15588 if (w->cursor.vpos < 0 && !window_frozen_p (w))
15589 {
15590 /* If point does not appear, try to move point so it does
15591 appear. The desired matrix has been built above, so we
15592 can use it here. */
15593 new_vpos = window_box_height (w) / 2;
15594 }
15595
15596 if (!cursor_row_fully_visible_p (w, 0, 0))
15597 {
15598 /* Point does appear, but on a line partly visible at end of window.
15599 Move it back to a fully-visible line. */
15600 new_vpos = window_box_height (w);
15601 }
15602 else if (w->cursor.vpos >=0)
15603 {
15604 /* Some people insist on not letting point enter the scroll
15605 margin, even though this part handles windows that didn't
15606 scroll at all. */
15607 int window_total_lines
15608 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15609 int margin = min (scroll_margin, window_total_lines / 4);
15610 int pixel_margin = margin * frame_line_height;
15611 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15612
15613 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15614 below, which finds the row to move point to, advances by
15615 the Y coordinate of the _next_ row, see the definition of
15616 MATRIX_ROW_BOTTOM_Y. */
15617 if (w->cursor.vpos < margin + header_line)
15618 {
15619 w->cursor.vpos = -1;
15620 clear_glyph_matrix (w->desired_matrix);
15621 goto try_to_scroll;
15622 }
15623 else
15624 {
15625 int window_height = window_box_height (w);
15626
15627 if (header_line)
15628 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15629 if (w->cursor.y >= window_height - pixel_margin)
15630 {
15631 w->cursor.vpos = -1;
15632 clear_glyph_matrix (w->desired_matrix);
15633 goto try_to_scroll;
15634 }
15635 }
15636 }
15637
15638 /* If we need to move point for either of the above reasons,
15639 now actually do it. */
15640 if (new_vpos >= 0)
15641 {
15642 struct glyph_row *row;
15643
15644 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15645 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15646 ++row;
15647
15648 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15649 MATRIX_ROW_START_BYTEPOS (row));
15650
15651 if (w != XWINDOW (selected_window))
15652 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15653 else if (current_buffer == old)
15654 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15655
15656 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15657
15658 /* If we are highlighting the region, then we just changed
15659 the region, so redisplay to show it. */
15660 if (markpos_of_region () >= 0)
15661 {
15662 clear_glyph_matrix (w->desired_matrix);
15663 if (!try_window (window, startp, 0))
15664 goto need_larger_matrices;
15665 }
15666 }
15667
15668 #ifdef GLYPH_DEBUG
15669 debug_method_add (w, "forced window start");
15670 #endif
15671 goto done;
15672 }
15673
15674 /* Handle case where text has not changed, only point, and it has
15675 not moved off the frame, and we are not retrying after hscroll.
15676 (current_matrix_up_to_date_p is nonzero when retrying.) */
15677 if (current_matrix_up_to_date_p
15678 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15679 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15680 {
15681 switch (rc)
15682 {
15683 case CURSOR_MOVEMENT_SUCCESS:
15684 used_current_matrix_p = 1;
15685 goto done;
15686
15687 case CURSOR_MOVEMENT_MUST_SCROLL:
15688 goto try_to_scroll;
15689
15690 default:
15691 emacs_abort ();
15692 }
15693 }
15694 /* If current starting point was originally the beginning of a line
15695 but no longer is, find a new starting point. */
15696 else if (w->start_at_line_beg
15697 && !(CHARPOS (startp) <= BEGV
15698 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15699 {
15700 #ifdef GLYPH_DEBUG
15701 debug_method_add (w, "recenter 1");
15702 #endif
15703 goto recenter;
15704 }
15705
15706 /* Try scrolling with try_window_id. Value is > 0 if update has
15707 been done, it is -1 if we know that the same window start will
15708 not work. It is 0 if unsuccessful for some other reason. */
15709 else if ((tem = try_window_id (w)) != 0)
15710 {
15711 #ifdef GLYPH_DEBUG
15712 debug_method_add (w, "try_window_id %d", tem);
15713 #endif
15714
15715 if (fonts_changed_p)
15716 goto need_larger_matrices;
15717 if (tem > 0)
15718 goto done;
15719
15720 /* Otherwise try_window_id has returned -1 which means that we
15721 don't want the alternative below this comment to execute. */
15722 }
15723 else if (CHARPOS (startp) >= BEGV
15724 && CHARPOS (startp) <= ZV
15725 && PT >= CHARPOS (startp)
15726 && (CHARPOS (startp) < ZV
15727 /* Avoid starting at end of buffer. */
15728 || CHARPOS (startp) == BEGV
15729 || !window_outdated (w)))
15730 {
15731 int d1, d2, d3, d4, d5, d6;
15732
15733 /* If first window line is a continuation line, and window start
15734 is inside the modified region, but the first change is before
15735 current window start, we must select a new window start.
15736
15737 However, if this is the result of a down-mouse event (e.g. by
15738 extending the mouse-drag-overlay), we don't want to select a
15739 new window start, since that would change the position under
15740 the mouse, resulting in an unwanted mouse-movement rather
15741 than a simple mouse-click. */
15742 if (!w->start_at_line_beg
15743 && NILP (do_mouse_tracking)
15744 && CHARPOS (startp) > BEGV
15745 && CHARPOS (startp) > BEG + beg_unchanged
15746 && CHARPOS (startp) <= Z - end_unchanged
15747 /* Even if w->start_at_line_beg is nil, a new window may
15748 start at a line_beg, since that's how set_buffer_window
15749 sets it. So, we need to check the return value of
15750 compute_window_start_on_continuation_line. (See also
15751 bug#197). */
15752 && XMARKER (w->start)->buffer == current_buffer
15753 && compute_window_start_on_continuation_line (w)
15754 /* It doesn't make sense to force the window start like we
15755 do at label force_start if it is already known that point
15756 will not be visible in the resulting window, because
15757 doing so will move point from its correct position
15758 instead of scrolling the window to bring point into view.
15759 See bug#9324. */
15760 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15761 {
15762 w->force_start = 1;
15763 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15764 goto force_start;
15765 }
15766
15767 #ifdef GLYPH_DEBUG
15768 debug_method_add (w, "same window start");
15769 #endif
15770
15771 /* Try to redisplay starting at same place as before.
15772 If point has not moved off frame, accept the results. */
15773 if (!current_matrix_up_to_date_p
15774 /* Don't use try_window_reusing_current_matrix in this case
15775 because a window scroll function can have changed the
15776 buffer. */
15777 || !NILP (Vwindow_scroll_functions)
15778 || MINI_WINDOW_P (w)
15779 || !(used_current_matrix_p
15780 = try_window_reusing_current_matrix (w)))
15781 {
15782 IF_DEBUG (debug_method_add (w, "1"));
15783 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15784 /* -1 means we need to scroll.
15785 0 means we need new matrices, but fonts_changed_p
15786 is set in that case, so we will detect it below. */
15787 goto try_to_scroll;
15788 }
15789
15790 if (fonts_changed_p)
15791 goto need_larger_matrices;
15792
15793 if (w->cursor.vpos >= 0)
15794 {
15795 if (!just_this_one_p
15796 || current_buffer->clip_changed
15797 || BEG_UNCHANGED < CHARPOS (startp))
15798 /* Forget any recorded base line for line number display. */
15799 w->base_line_number = 0;
15800
15801 if (!cursor_row_fully_visible_p (w, 1, 0))
15802 {
15803 clear_glyph_matrix (w->desired_matrix);
15804 last_line_misfit = 1;
15805 }
15806 /* Drop through and scroll. */
15807 else
15808 goto done;
15809 }
15810 else
15811 clear_glyph_matrix (w->desired_matrix);
15812 }
15813
15814 try_to_scroll:
15815
15816 /* Redisplay the mode line. Select the buffer properly for that. */
15817 if (!update_mode_line)
15818 {
15819 update_mode_line = 1;
15820 w->update_mode_line = 1;
15821 }
15822
15823 /* Try to scroll by specified few lines. */
15824 if ((scroll_conservatively
15825 || emacs_scroll_step
15826 || temp_scroll_step
15827 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15828 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15829 && CHARPOS (startp) >= BEGV
15830 && CHARPOS (startp) <= ZV)
15831 {
15832 /* The function returns -1 if new fonts were loaded, 1 if
15833 successful, 0 if not successful. */
15834 int ss = try_scrolling (window, just_this_one_p,
15835 scroll_conservatively,
15836 emacs_scroll_step,
15837 temp_scroll_step, last_line_misfit);
15838 switch (ss)
15839 {
15840 case SCROLLING_SUCCESS:
15841 goto done;
15842
15843 case SCROLLING_NEED_LARGER_MATRICES:
15844 goto need_larger_matrices;
15845
15846 case SCROLLING_FAILED:
15847 break;
15848
15849 default:
15850 emacs_abort ();
15851 }
15852 }
15853
15854 /* Finally, just choose a place to start which positions point
15855 according to user preferences. */
15856
15857 recenter:
15858
15859 #ifdef GLYPH_DEBUG
15860 debug_method_add (w, "recenter");
15861 #endif
15862
15863 /* Forget any previously recorded base line for line number display. */
15864 if (!buffer_unchanged_p)
15865 w->base_line_number = 0;
15866
15867 /* Determine the window start relative to point. */
15868 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15869 it.current_y = it.last_visible_y;
15870 if (centering_position < 0)
15871 {
15872 int window_total_lines
15873 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15874 int margin =
15875 scroll_margin > 0
15876 ? min (scroll_margin, window_total_lines / 4)
15877 : 0;
15878 ptrdiff_t margin_pos = CHARPOS (startp);
15879 Lisp_Object aggressive;
15880 int scrolling_up;
15881
15882 /* If there is a scroll margin at the top of the window, find
15883 its character position. */
15884 if (margin
15885 /* Cannot call start_display if startp is not in the
15886 accessible region of the buffer. This can happen when we
15887 have just switched to a different buffer and/or changed
15888 its restriction. In that case, startp is initialized to
15889 the character position 1 (BEGV) because we did not yet
15890 have chance to display the buffer even once. */
15891 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15892 {
15893 struct it it1;
15894 void *it1data = NULL;
15895
15896 SAVE_IT (it1, it, it1data);
15897 start_display (&it1, w, startp);
15898 move_it_vertically (&it1, margin * frame_line_height);
15899 margin_pos = IT_CHARPOS (it1);
15900 RESTORE_IT (&it, &it, it1data);
15901 }
15902 scrolling_up = PT > margin_pos;
15903 aggressive =
15904 scrolling_up
15905 ? BVAR (current_buffer, scroll_up_aggressively)
15906 : BVAR (current_buffer, scroll_down_aggressively);
15907
15908 if (!MINI_WINDOW_P (w)
15909 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15910 {
15911 int pt_offset = 0;
15912
15913 /* Setting scroll-conservatively overrides
15914 scroll-*-aggressively. */
15915 if (!scroll_conservatively && NUMBERP (aggressive))
15916 {
15917 double float_amount = XFLOATINT (aggressive);
15918
15919 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15920 if (pt_offset == 0 && float_amount > 0)
15921 pt_offset = 1;
15922 if (pt_offset && margin > 0)
15923 margin -= 1;
15924 }
15925 /* Compute how much to move the window start backward from
15926 point so that point will be displayed where the user
15927 wants it. */
15928 if (scrolling_up)
15929 {
15930 centering_position = it.last_visible_y;
15931 if (pt_offset)
15932 centering_position -= pt_offset;
15933 centering_position -=
15934 frame_line_height * (1 + margin + (last_line_misfit != 0))
15935 + WINDOW_HEADER_LINE_HEIGHT (w);
15936 /* Don't let point enter the scroll margin near top of
15937 the window. */
15938 if (centering_position < margin * frame_line_height)
15939 centering_position = margin * frame_line_height;
15940 }
15941 else
15942 centering_position = margin * frame_line_height + pt_offset;
15943 }
15944 else
15945 /* Set the window start half the height of the window backward
15946 from point. */
15947 centering_position = window_box_height (w) / 2;
15948 }
15949 move_it_vertically_backward (&it, centering_position);
15950
15951 eassert (IT_CHARPOS (it) >= BEGV);
15952
15953 /* The function move_it_vertically_backward may move over more
15954 than the specified y-distance. If it->w is small, e.g. a
15955 mini-buffer window, we may end up in front of the window's
15956 display area. Start displaying at the start of the line
15957 containing PT in this case. */
15958 if (it.current_y <= 0)
15959 {
15960 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15961 move_it_vertically_backward (&it, 0);
15962 it.current_y = 0;
15963 }
15964
15965 it.current_x = it.hpos = 0;
15966
15967 /* Set the window start position here explicitly, to avoid an
15968 infinite loop in case the functions in window-scroll-functions
15969 get errors. */
15970 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15971
15972 /* Run scroll hooks. */
15973 startp = run_window_scroll_functions (window, it.current.pos);
15974
15975 /* Redisplay the window. */
15976 if (!current_matrix_up_to_date_p
15977 || windows_or_buffers_changed
15978 || cursor_type_changed
15979 /* Don't use try_window_reusing_current_matrix in this case
15980 because it can have changed the buffer. */
15981 || !NILP (Vwindow_scroll_functions)
15982 || !just_this_one_p
15983 || MINI_WINDOW_P (w)
15984 || !(used_current_matrix_p
15985 = try_window_reusing_current_matrix (w)))
15986 try_window (window, startp, 0);
15987
15988 /* If new fonts have been loaded (due to fontsets), give up. We
15989 have to start a new redisplay since we need to re-adjust glyph
15990 matrices. */
15991 if (fonts_changed_p)
15992 goto need_larger_matrices;
15993
15994 /* If cursor did not appear assume that the middle of the window is
15995 in the first line of the window. Do it again with the next line.
15996 (Imagine a window of height 100, displaying two lines of height
15997 60. Moving back 50 from it->last_visible_y will end in the first
15998 line.) */
15999 if (w->cursor.vpos < 0)
16000 {
16001 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16002 {
16003 clear_glyph_matrix (w->desired_matrix);
16004 move_it_by_lines (&it, 1);
16005 try_window (window, it.current.pos, 0);
16006 }
16007 else if (PT < IT_CHARPOS (it))
16008 {
16009 clear_glyph_matrix (w->desired_matrix);
16010 move_it_by_lines (&it, -1);
16011 try_window (window, it.current.pos, 0);
16012 }
16013 else
16014 {
16015 /* Not much we can do about it. */
16016 }
16017 }
16018
16019 /* Consider the following case: Window starts at BEGV, there is
16020 invisible, intangible text at BEGV, so that display starts at
16021 some point START > BEGV. It can happen that we are called with
16022 PT somewhere between BEGV and START. Try to handle that case. */
16023 if (w->cursor.vpos < 0)
16024 {
16025 struct glyph_row *row = w->current_matrix->rows;
16026 if (row->mode_line_p)
16027 ++row;
16028 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16029 }
16030
16031 if (!cursor_row_fully_visible_p (w, 0, 0))
16032 {
16033 /* If vscroll is enabled, disable it and try again. */
16034 if (w->vscroll)
16035 {
16036 w->vscroll = 0;
16037 clear_glyph_matrix (w->desired_matrix);
16038 goto recenter;
16039 }
16040
16041 /* Users who set scroll-conservatively to a large number want
16042 point just above/below the scroll margin. If we ended up
16043 with point's row partially visible, move the window start to
16044 make that row fully visible and out of the margin. */
16045 if (scroll_conservatively > SCROLL_LIMIT)
16046 {
16047 int window_total_lines
16048 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16049 int margin =
16050 scroll_margin > 0
16051 ? min (scroll_margin, window_total_lines / 4)
16052 : 0;
16053 int move_down = w->cursor.vpos >= window_total_lines / 2;
16054
16055 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16056 clear_glyph_matrix (w->desired_matrix);
16057 if (1 == try_window (window, it.current.pos,
16058 TRY_WINDOW_CHECK_MARGINS))
16059 goto done;
16060 }
16061
16062 /* If centering point failed to make the whole line visible,
16063 put point at the top instead. That has to make the whole line
16064 visible, if it can be done. */
16065 if (centering_position == 0)
16066 goto done;
16067
16068 clear_glyph_matrix (w->desired_matrix);
16069 centering_position = 0;
16070 goto recenter;
16071 }
16072
16073 done:
16074
16075 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16076 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16077 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16078
16079 /* Display the mode line, if we must. */
16080 if ((update_mode_line
16081 /* If window not full width, must redo its mode line
16082 if (a) the window to its side is being redone and
16083 (b) we do a frame-based redisplay. This is a consequence
16084 of how inverted lines are drawn in frame-based redisplay. */
16085 || (!just_this_one_p
16086 && !FRAME_WINDOW_P (f)
16087 && !WINDOW_FULL_WIDTH_P (w))
16088 /* Line number to display. */
16089 || w->base_line_pos > 0
16090 /* Column number is displayed and different from the one displayed. */
16091 || (w->column_number_displayed != -1
16092 && (w->column_number_displayed != current_column ())))
16093 /* This means that the window has a mode line. */
16094 && (WINDOW_WANTS_MODELINE_P (w)
16095 || WINDOW_WANTS_HEADER_LINE_P (w)))
16096 {
16097 display_mode_lines (w);
16098
16099 /* If mode line height has changed, arrange for a thorough
16100 immediate redisplay using the correct mode line height. */
16101 if (WINDOW_WANTS_MODELINE_P (w)
16102 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16103 {
16104 fonts_changed_p = 1;
16105 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16106 = DESIRED_MODE_LINE_HEIGHT (w);
16107 }
16108
16109 /* If header line height has changed, arrange for a thorough
16110 immediate redisplay using the correct header line height. */
16111 if (WINDOW_WANTS_HEADER_LINE_P (w)
16112 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16113 {
16114 fonts_changed_p = 1;
16115 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16116 = DESIRED_HEADER_LINE_HEIGHT (w);
16117 }
16118
16119 if (fonts_changed_p)
16120 goto need_larger_matrices;
16121 }
16122
16123 if (!line_number_displayed && w->base_line_pos != -1)
16124 {
16125 w->base_line_pos = 0;
16126 w->base_line_number = 0;
16127 }
16128
16129 finish_menu_bars:
16130
16131 /* When we reach a frame's selected window, redo the frame's menu bar. */
16132 if (update_mode_line
16133 && EQ (FRAME_SELECTED_WINDOW (f), window))
16134 {
16135 int redisplay_menu_p = 0;
16136
16137 if (FRAME_WINDOW_P (f))
16138 {
16139 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16140 || defined (HAVE_NS) || defined (USE_GTK)
16141 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16142 #else
16143 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16144 #endif
16145 }
16146 else
16147 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16148
16149 if (redisplay_menu_p)
16150 display_menu_bar (w);
16151
16152 #ifdef HAVE_WINDOW_SYSTEM
16153 if (FRAME_WINDOW_P (f))
16154 {
16155 #if defined (USE_GTK) || defined (HAVE_NS)
16156 if (FRAME_EXTERNAL_TOOL_BAR (f))
16157 redisplay_tool_bar (f);
16158 #else
16159 if (WINDOWP (f->tool_bar_window)
16160 && (FRAME_TOOL_BAR_LINES (f) > 0
16161 || !NILP (Vauto_resize_tool_bars))
16162 && redisplay_tool_bar (f))
16163 ignore_mouse_drag_p = 1;
16164 #endif
16165 }
16166 #endif
16167 }
16168
16169 #ifdef HAVE_WINDOW_SYSTEM
16170 if (FRAME_WINDOW_P (f)
16171 && update_window_fringes (w, (just_this_one_p
16172 || (!used_current_matrix_p && !overlay_arrow_seen)
16173 || w->pseudo_window_p)))
16174 {
16175 update_begin (f);
16176 block_input ();
16177 if (draw_window_fringes (w, 1))
16178 x_draw_vertical_border (w);
16179 unblock_input ();
16180 update_end (f);
16181 }
16182 #endif /* HAVE_WINDOW_SYSTEM */
16183
16184 /* We go to this label, with fonts_changed_p set,
16185 if it is necessary to try again using larger glyph matrices.
16186 We have to redeem the scroll bar even in this case,
16187 because the loop in redisplay_internal expects that. */
16188 need_larger_matrices:
16189 ;
16190 finish_scroll_bars:
16191
16192 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16193 {
16194 /* Set the thumb's position and size. */
16195 set_vertical_scroll_bar (w);
16196
16197 /* Note that we actually used the scroll bar attached to this
16198 window, so it shouldn't be deleted at the end of redisplay. */
16199 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16200 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16201 }
16202
16203 /* Restore current_buffer and value of point in it. The window
16204 update may have changed the buffer, so first make sure `opoint'
16205 is still valid (Bug#6177). */
16206 if (CHARPOS (opoint) < BEGV)
16207 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16208 else if (CHARPOS (opoint) > ZV)
16209 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16210 else
16211 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16212
16213 set_buffer_internal_1 (old);
16214 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16215 shorter. This can be caused by log truncation in *Messages*. */
16216 if (CHARPOS (lpoint) <= ZV)
16217 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16218
16219 unbind_to (count, Qnil);
16220 }
16221
16222
16223 /* Build the complete desired matrix of WINDOW with a window start
16224 buffer position POS.
16225
16226 Value is 1 if successful. It is zero if fonts were loaded during
16227 redisplay which makes re-adjusting glyph matrices necessary, and -1
16228 if point would appear in the scroll margins.
16229 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16230 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16231 set in FLAGS.) */
16232
16233 int
16234 try_window (Lisp_Object window, struct text_pos pos, int flags)
16235 {
16236 struct window *w = XWINDOW (window);
16237 struct it it;
16238 struct glyph_row *last_text_row = NULL;
16239 struct frame *f = XFRAME (w->frame);
16240 int frame_line_height = default_line_pixel_height (w);
16241
16242 /* Make POS the new window start. */
16243 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16244
16245 /* Mark cursor position as unknown. No overlay arrow seen. */
16246 w->cursor.vpos = -1;
16247 overlay_arrow_seen = 0;
16248
16249 /* Initialize iterator and info to start at POS. */
16250 start_display (&it, w, pos);
16251
16252 /* Display all lines of W. */
16253 while (it.current_y < it.last_visible_y)
16254 {
16255 if (display_line (&it))
16256 last_text_row = it.glyph_row - 1;
16257 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16258 return 0;
16259 }
16260
16261 /* Don't let the cursor end in the scroll margins. */
16262 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16263 && !MINI_WINDOW_P (w))
16264 {
16265 int this_scroll_margin;
16266 int window_total_lines
16267 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16268
16269 if (scroll_margin > 0)
16270 {
16271 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16272 this_scroll_margin *= frame_line_height;
16273 }
16274 else
16275 this_scroll_margin = 0;
16276
16277 if ((w->cursor.y >= 0 /* not vscrolled */
16278 && w->cursor.y < this_scroll_margin
16279 && CHARPOS (pos) > BEGV
16280 && IT_CHARPOS (it) < ZV)
16281 /* rms: considering make_cursor_line_fully_visible_p here
16282 seems to give wrong results. We don't want to recenter
16283 when the last line is partly visible, we want to allow
16284 that case to be handled in the usual way. */
16285 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16286 {
16287 w->cursor.vpos = -1;
16288 clear_glyph_matrix (w->desired_matrix);
16289 return -1;
16290 }
16291 }
16292
16293 /* If bottom moved off end of frame, change mode line percentage. */
16294 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
16295 w->update_mode_line = 1;
16296
16297 /* Set window_end_pos to the offset of the last character displayed
16298 on the window from the end of current_buffer. Set
16299 window_end_vpos to its row number. */
16300 if (last_text_row)
16301 {
16302 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16303 adjust_window_ends (w, last_text_row, 0);
16304 eassert
16305 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16306 w->window_end_vpos)));
16307 }
16308 else
16309 {
16310 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16311 w->window_end_pos = Z - ZV;
16312 w->window_end_vpos = 0;
16313 }
16314
16315 /* But that is not valid info until redisplay finishes. */
16316 w->window_end_valid = 0;
16317 return 1;
16318 }
16319
16320
16321 \f
16322 /************************************************************************
16323 Window redisplay reusing current matrix when buffer has not changed
16324 ************************************************************************/
16325
16326 /* Try redisplay of window W showing an unchanged buffer with a
16327 different window start than the last time it was displayed by
16328 reusing its current matrix. Value is non-zero if successful.
16329 W->start is the new window start. */
16330
16331 static int
16332 try_window_reusing_current_matrix (struct window *w)
16333 {
16334 struct frame *f = XFRAME (w->frame);
16335 struct glyph_row *bottom_row;
16336 struct it it;
16337 struct run run;
16338 struct text_pos start, new_start;
16339 int nrows_scrolled, i;
16340 struct glyph_row *last_text_row;
16341 struct glyph_row *last_reused_text_row;
16342 struct glyph_row *start_row;
16343 int start_vpos, min_y, max_y;
16344
16345 #ifdef GLYPH_DEBUG
16346 if (inhibit_try_window_reusing)
16347 return 0;
16348 #endif
16349
16350 if (/* This function doesn't handle terminal frames. */
16351 !FRAME_WINDOW_P (f)
16352 /* Don't try to reuse the display if windows have been split
16353 or such. */
16354 || windows_or_buffers_changed
16355 || cursor_type_changed)
16356 return 0;
16357
16358 /* Can't do this if region may have changed. */
16359 if (markpos_of_region () >= 0
16360 || w->region_showing
16361 || !NILP (Vshow_trailing_whitespace))
16362 return 0;
16363
16364 /* If top-line visibility has changed, give up. */
16365 if (WINDOW_WANTS_HEADER_LINE_P (w)
16366 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16367 return 0;
16368
16369 /* Give up if old or new display is scrolled vertically. We could
16370 make this function handle this, but right now it doesn't. */
16371 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16372 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16373 return 0;
16374
16375 /* The variable new_start now holds the new window start. The old
16376 start `start' can be determined from the current matrix. */
16377 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16378 start = start_row->minpos;
16379 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16380
16381 /* Clear the desired matrix for the display below. */
16382 clear_glyph_matrix (w->desired_matrix);
16383
16384 if (CHARPOS (new_start) <= CHARPOS (start))
16385 {
16386 /* Don't use this method if the display starts with an ellipsis
16387 displayed for invisible text. It's not easy to handle that case
16388 below, and it's certainly not worth the effort since this is
16389 not a frequent case. */
16390 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16391 return 0;
16392
16393 IF_DEBUG (debug_method_add (w, "twu1"));
16394
16395 /* Display up to a row that can be reused. The variable
16396 last_text_row is set to the last row displayed that displays
16397 text. Note that it.vpos == 0 if or if not there is a
16398 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16399 start_display (&it, w, new_start);
16400 w->cursor.vpos = -1;
16401 last_text_row = last_reused_text_row = NULL;
16402
16403 while (it.current_y < it.last_visible_y
16404 && !fonts_changed_p)
16405 {
16406 /* If we have reached into the characters in the START row,
16407 that means the line boundaries have changed. So we
16408 can't start copying with the row START. Maybe it will
16409 work to start copying with the following row. */
16410 while (IT_CHARPOS (it) > CHARPOS (start))
16411 {
16412 /* Advance to the next row as the "start". */
16413 start_row++;
16414 start = start_row->minpos;
16415 /* If there are no more rows to try, or just one, give up. */
16416 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16417 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16418 || CHARPOS (start) == ZV)
16419 {
16420 clear_glyph_matrix (w->desired_matrix);
16421 return 0;
16422 }
16423
16424 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16425 }
16426 /* If we have reached alignment, we can copy the rest of the
16427 rows. */
16428 if (IT_CHARPOS (it) == CHARPOS (start)
16429 /* Don't accept "alignment" inside a display vector,
16430 since start_row could have started in the middle of
16431 that same display vector (thus their character
16432 positions match), and we have no way of telling if
16433 that is the case. */
16434 && it.current.dpvec_index < 0)
16435 break;
16436
16437 if (display_line (&it))
16438 last_text_row = it.glyph_row - 1;
16439
16440 }
16441
16442 /* A value of current_y < last_visible_y means that we stopped
16443 at the previous window start, which in turn means that we
16444 have at least one reusable row. */
16445 if (it.current_y < it.last_visible_y)
16446 {
16447 struct glyph_row *row;
16448
16449 /* IT.vpos always starts from 0; it counts text lines. */
16450 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16451
16452 /* Find PT if not already found in the lines displayed. */
16453 if (w->cursor.vpos < 0)
16454 {
16455 int dy = it.current_y - start_row->y;
16456
16457 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16458 row = row_containing_pos (w, PT, row, NULL, dy);
16459 if (row)
16460 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16461 dy, nrows_scrolled);
16462 else
16463 {
16464 clear_glyph_matrix (w->desired_matrix);
16465 return 0;
16466 }
16467 }
16468
16469 /* Scroll the display. Do it before the current matrix is
16470 changed. The problem here is that update has not yet
16471 run, i.e. part of the current matrix is not up to date.
16472 scroll_run_hook will clear the cursor, and use the
16473 current matrix to get the height of the row the cursor is
16474 in. */
16475 run.current_y = start_row->y;
16476 run.desired_y = it.current_y;
16477 run.height = it.last_visible_y - it.current_y;
16478
16479 if (run.height > 0 && run.current_y != run.desired_y)
16480 {
16481 update_begin (f);
16482 FRAME_RIF (f)->update_window_begin_hook (w);
16483 FRAME_RIF (f)->clear_window_mouse_face (w);
16484 FRAME_RIF (f)->scroll_run_hook (w, &run);
16485 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16486 update_end (f);
16487 }
16488
16489 /* Shift current matrix down by nrows_scrolled lines. */
16490 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16491 rotate_matrix (w->current_matrix,
16492 start_vpos,
16493 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16494 nrows_scrolled);
16495
16496 /* Disable lines that must be updated. */
16497 for (i = 0; i < nrows_scrolled; ++i)
16498 (start_row + i)->enabled_p = 0;
16499
16500 /* Re-compute Y positions. */
16501 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16502 max_y = it.last_visible_y;
16503 for (row = start_row + nrows_scrolled;
16504 row < bottom_row;
16505 ++row)
16506 {
16507 row->y = it.current_y;
16508 row->visible_height = row->height;
16509
16510 if (row->y < min_y)
16511 row->visible_height -= min_y - row->y;
16512 if (row->y + row->height > max_y)
16513 row->visible_height -= row->y + row->height - max_y;
16514 if (row->fringe_bitmap_periodic_p)
16515 row->redraw_fringe_bitmaps_p = 1;
16516
16517 it.current_y += row->height;
16518
16519 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16520 last_reused_text_row = row;
16521 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16522 break;
16523 }
16524
16525 /* Disable lines in the current matrix which are now
16526 below the window. */
16527 for (++row; row < bottom_row; ++row)
16528 row->enabled_p = row->mode_line_p = 0;
16529 }
16530
16531 /* Update window_end_pos etc.; last_reused_text_row is the last
16532 reused row from the current matrix containing text, if any.
16533 The value of last_text_row is the last displayed line
16534 containing text. */
16535 if (last_reused_text_row)
16536 adjust_window_ends (w, last_reused_text_row, 1);
16537 else if (last_text_row)
16538 adjust_window_ends (w, last_text_row, 0);
16539 else
16540 {
16541 /* This window must be completely empty. */
16542 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16543 w->window_end_pos = Z - ZV;
16544 w->window_end_vpos = 0;
16545 }
16546 w->window_end_valid = 0;
16547
16548 /* Update hint: don't try scrolling again in update_window. */
16549 w->desired_matrix->no_scrolling_p = 1;
16550
16551 #ifdef GLYPH_DEBUG
16552 debug_method_add (w, "try_window_reusing_current_matrix 1");
16553 #endif
16554 return 1;
16555 }
16556 else if (CHARPOS (new_start) > CHARPOS (start))
16557 {
16558 struct glyph_row *pt_row, *row;
16559 struct glyph_row *first_reusable_row;
16560 struct glyph_row *first_row_to_display;
16561 int dy;
16562 int yb = window_text_bottom_y (w);
16563
16564 /* Find the row starting at new_start, if there is one. Don't
16565 reuse a partially visible line at the end. */
16566 first_reusable_row = start_row;
16567 while (first_reusable_row->enabled_p
16568 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16569 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16570 < CHARPOS (new_start)))
16571 ++first_reusable_row;
16572
16573 /* Give up if there is no row to reuse. */
16574 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16575 || !first_reusable_row->enabled_p
16576 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16577 != CHARPOS (new_start)))
16578 return 0;
16579
16580 /* We can reuse fully visible rows beginning with
16581 first_reusable_row to the end of the window. Set
16582 first_row_to_display to the first row that cannot be reused.
16583 Set pt_row to the row containing point, if there is any. */
16584 pt_row = NULL;
16585 for (first_row_to_display = first_reusable_row;
16586 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16587 ++first_row_to_display)
16588 {
16589 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16590 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16591 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16592 && first_row_to_display->ends_at_zv_p
16593 && pt_row == NULL)))
16594 pt_row = first_row_to_display;
16595 }
16596
16597 /* Start displaying at the start of first_row_to_display. */
16598 eassert (first_row_to_display->y < yb);
16599 init_to_row_start (&it, w, first_row_to_display);
16600
16601 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16602 - start_vpos);
16603 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16604 - nrows_scrolled);
16605 it.current_y = (first_row_to_display->y - first_reusable_row->y
16606 + WINDOW_HEADER_LINE_HEIGHT (w));
16607
16608 /* Display lines beginning with first_row_to_display in the
16609 desired matrix. Set last_text_row to the last row displayed
16610 that displays text. */
16611 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16612 if (pt_row == NULL)
16613 w->cursor.vpos = -1;
16614 last_text_row = NULL;
16615 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16616 if (display_line (&it))
16617 last_text_row = it.glyph_row - 1;
16618
16619 /* If point is in a reused row, adjust y and vpos of the cursor
16620 position. */
16621 if (pt_row)
16622 {
16623 w->cursor.vpos -= nrows_scrolled;
16624 w->cursor.y -= first_reusable_row->y - start_row->y;
16625 }
16626
16627 /* Give up if point isn't in a row displayed or reused. (This
16628 also handles the case where w->cursor.vpos < nrows_scrolled
16629 after the calls to display_line, which can happen with scroll
16630 margins. See bug#1295.) */
16631 if (w->cursor.vpos < 0)
16632 {
16633 clear_glyph_matrix (w->desired_matrix);
16634 return 0;
16635 }
16636
16637 /* Scroll the display. */
16638 run.current_y = first_reusable_row->y;
16639 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16640 run.height = it.last_visible_y - run.current_y;
16641 dy = run.current_y - run.desired_y;
16642
16643 if (run.height)
16644 {
16645 update_begin (f);
16646 FRAME_RIF (f)->update_window_begin_hook (w);
16647 FRAME_RIF (f)->clear_window_mouse_face (w);
16648 FRAME_RIF (f)->scroll_run_hook (w, &run);
16649 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16650 update_end (f);
16651 }
16652
16653 /* Adjust Y positions of reused rows. */
16654 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16655 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16656 max_y = it.last_visible_y;
16657 for (row = first_reusable_row; row < first_row_to_display; ++row)
16658 {
16659 row->y -= dy;
16660 row->visible_height = row->height;
16661 if (row->y < min_y)
16662 row->visible_height -= min_y - row->y;
16663 if (row->y + row->height > max_y)
16664 row->visible_height -= row->y + row->height - max_y;
16665 if (row->fringe_bitmap_periodic_p)
16666 row->redraw_fringe_bitmaps_p = 1;
16667 }
16668
16669 /* Scroll the current matrix. */
16670 eassert (nrows_scrolled > 0);
16671 rotate_matrix (w->current_matrix,
16672 start_vpos,
16673 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16674 -nrows_scrolled);
16675
16676 /* Disable rows not reused. */
16677 for (row -= nrows_scrolled; row < bottom_row; ++row)
16678 row->enabled_p = 0;
16679
16680 /* Point may have moved to a different line, so we cannot assume that
16681 the previous cursor position is valid; locate the correct row. */
16682 if (pt_row)
16683 {
16684 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16685 row < bottom_row
16686 && PT >= MATRIX_ROW_END_CHARPOS (row)
16687 && !row->ends_at_zv_p;
16688 row++)
16689 {
16690 w->cursor.vpos++;
16691 w->cursor.y = row->y;
16692 }
16693 if (row < bottom_row)
16694 {
16695 /* Can't simply scan the row for point with
16696 bidi-reordered glyph rows. Let set_cursor_from_row
16697 figure out where to put the cursor, and if it fails,
16698 give up. */
16699 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16700 {
16701 if (!set_cursor_from_row (w, row, w->current_matrix,
16702 0, 0, 0, 0))
16703 {
16704 clear_glyph_matrix (w->desired_matrix);
16705 return 0;
16706 }
16707 }
16708 else
16709 {
16710 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16711 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16712
16713 for (; glyph < end
16714 && (!BUFFERP (glyph->object)
16715 || glyph->charpos < PT);
16716 glyph++)
16717 {
16718 w->cursor.hpos++;
16719 w->cursor.x += glyph->pixel_width;
16720 }
16721 }
16722 }
16723 }
16724
16725 /* Adjust window end. A null value of last_text_row means that
16726 the window end is in reused rows which in turn means that
16727 only its vpos can have changed. */
16728 if (last_text_row)
16729 adjust_window_ends (w, last_text_row, 0);
16730 else
16731 w->window_end_vpos -= nrows_scrolled;
16732
16733 w->window_end_valid = 0;
16734 w->desired_matrix->no_scrolling_p = 1;
16735
16736 #ifdef GLYPH_DEBUG
16737 debug_method_add (w, "try_window_reusing_current_matrix 2");
16738 #endif
16739 return 1;
16740 }
16741
16742 return 0;
16743 }
16744
16745
16746 \f
16747 /************************************************************************
16748 Window redisplay reusing current matrix when buffer has changed
16749 ************************************************************************/
16750
16751 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16752 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16753 ptrdiff_t *, ptrdiff_t *);
16754 static struct glyph_row *
16755 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16756 struct glyph_row *);
16757
16758
16759 /* Return the last row in MATRIX displaying text. If row START is
16760 non-null, start searching with that row. IT gives the dimensions
16761 of the display. Value is null if matrix is empty; otherwise it is
16762 a pointer to the row found. */
16763
16764 static struct glyph_row *
16765 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16766 struct glyph_row *start)
16767 {
16768 struct glyph_row *row, *row_found;
16769
16770 /* Set row_found to the last row in IT->w's current matrix
16771 displaying text. The loop looks funny but think of partially
16772 visible lines. */
16773 row_found = NULL;
16774 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16775 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16776 {
16777 eassert (row->enabled_p);
16778 row_found = row;
16779 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16780 break;
16781 ++row;
16782 }
16783
16784 return row_found;
16785 }
16786
16787
16788 /* Return the last row in the current matrix of W that is not affected
16789 by changes at the start of current_buffer that occurred since W's
16790 current matrix was built. Value is null if no such row exists.
16791
16792 BEG_UNCHANGED us the number of characters unchanged at the start of
16793 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16794 first changed character in current_buffer. Characters at positions <
16795 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16796 when the current matrix was built. */
16797
16798 static struct glyph_row *
16799 find_last_unchanged_at_beg_row (struct window *w)
16800 {
16801 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16802 struct glyph_row *row;
16803 struct glyph_row *row_found = NULL;
16804 int yb = window_text_bottom_y (w);
16805
16806 /* Find the last row displaying unchanged text. */
16807 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16808 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16809 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16810 ++row)
16811 {
16812 if (/* If row ends before first_changed_pos, it is unchanged,
16813 except in some case. */
16814 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16815 /* When row ends in ZV and we write at ZV it is not
16816 unchanged. */
16817 && !row->ends_at_zv_p
16818 /* When first_changed_pos is the end of a continued line,
16819 row is not unchanged because it may be no longer
16820 continued. */
16821 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16822 && (row->continued_p
16823 || row->exact_window_width_line_p))
16824 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16825 needs to be recomputed, so don't consider this row as
16826 unchanged. This happens when the last line was
16827 bidi-reordered and was killed immediately before this
16828 redisplay cycle. In that case, ROW->end stores the
16829 buffer position of the first visual-order character of
16830 the killed text, which is now beyond ZV. */
16831 && CHARPOS (row->end.pos) <= ZV)
16832 row_found = row;
16833
16834 /* Stop if last visible row. */
16835 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16836 break;
16837 }
16838
16839 return row_found;
16840 }
16841
16842
16843 /* Find the first glyph row in the current matrix of W that is not
16844 affected by changes at the end of current_buffer since the
16845 time W's current matrix was built.
16846
16847 Return in *DELTA the number of chars by which buffer positions in
16848 unchanged text at the end of current_buffer must be adjusted.
16849
16850 Return in *DELTA_BYTES the corresponding number of bytes.
16851
16852 Value is null if no such row exists, i.e. all rows are affected by
16853 changes. */
16854
16855 static struct glyph_row *
16856 find_first_unchanged_at_end_row (struct window *w,
16857 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16858 {
16859 struct glyph_row *row;
16860 struct glyph_row *row_found = NULL;
16861
16862 *delta = *delta_bytes = 0;
16863
16864 /* Display must not have been paused, otherwise the current matrix
16865 is not up to date. */
16866 eassert (w->window_end_valid);
16867
16868 /* A value of window_end_pos >= END_UNCHANGED means that the window
16869 end is in the range of changed text. If so, there is no
16870 unchanged row at the end of W's current matrix. */
16871 if (w->window_end_pos >= END_UNCHANGED)
16872 return NULL;
16873
16874 /* Set row to the last row in W's current matrix displaying text. */
16875 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
16876
16877 /* If matrix is entirely empty, no unchanged row exists. */
16878 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16879 {
16880 /* The value of row is the last glyph row in the matrix having a
16881 meaningful buffer position in it. The end position of row
16882 corresponds to window_end_pos. This allows us to translate
16883 buffer positions in the current matrix to current buffer
16884 positions for characters not in changed text. */
16885 ptrdiff_t Z_old =
16886 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
16887 ptrdiff_t Z_BYTE_old =
16888 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16889 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16890 struct glyph_row *first_text_row
16891 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16892
16893 *delta = Z - Z_old;
16894 *delta_bytes = Z_BYTE - Z_BYTE_old;
16895
16896 /* Set last_unchanged_pos to the buffer position of the last
16897 character in the buffer that has not been changed. Z is the
16898 index + 1 of the last character in current_buffer, i.e. by
16899 subtracting END_UNCHANGED we get the index of the last
16900 unchanged character, and we have to add BEG to get its buffer
16901 position. */
16902 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16903 last_unchanged_pos_old = last_unchanged_pos - *delta;
16904
16905 /* Search backward from ROW for a row displaying a line that
16906 starts at a minimum position >= last_unchanged_pos_old. */
16907 for (; row > first_text_row; --row)
16908 {
16909 /* This used to abort, but it can happen.
16910 It is ok to just stop the search instead here. KFS. */
16911 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16912 break;
16913
16914 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16915 row_found = row;
16916 }
16917 }
16918
16919 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16920
16921 return row_found;
16922 }
16923
16924
16925 /* Make sure that glyph rows in the current matrix of window W
16926 reference the same glyph memory as corresponding rows in the
16927 frame's frame matrix. This function is called after scrolling W's
16928 current matrix on a terminal frame in try_window_id and
16929 try_window_reusing_current_matrix. */
16930
16931 static void
16932 sync_frame_with_window_matrix_rows (struct window *w)
16933 {
16934 struct frame *f = XFRAME (w->frame);
16935 struct glyph_row *window_row, *window_row_end, *frame_row;
16936
16937 /* Preconditions: W must be a leaf window and full-width. Its frame
16938 must have a frame matrix. */
16939 eassert (BUFFERP (w->contents));
16940 eassert (WINDOW_FULL_WIDTH_P (w));
16941 eassert (!FRAME_WINDOW_P (f));
16942
16943 /* If W is a full-width window, glyph pointers in W's current matrix
16944 have, by definition, to be the same as glyph pointers in the
16945 corresponding frame matrix. Note that frame matrices have no
16946 marginal areas (see build_frame_matrix). */
16947 window_row = w->current_matrix->rows;
16948 window_row_end = window_row + w->current_matrix->nrows;
16949 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16950 while (window_row < window_row_end)
16951 {
16952 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16953 struct glyph *end = window_row->glyphs[LAST_AREA];
16954
16955 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16956 frame_row->glyphs[TEXT_AREA] = start;
16957 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16958 frame_row->glyphs[LAST_AREA] = end;
16959
16960 /* Disable frame rows whose corresponding window rows have
16961 been disabled in try_window_id. */
16962 if (!window_row->enabled_p)
16963 frame_row->enabled_p = 0;
16964
16965 ++window_row, ++frame_row;
16966 }
16967 }
16968
16969
16970 /* Find the glyph row in window W containing CHARPOS. Consider all
16971 rows between START and END (not inclusive). END null means search
16972 all rows to the end of the display area of W. Value is the row
16973 containing CHARPOS or null. */
16974
16975 struct glyph_row *
16976 row_containing_pos (struct window *w, ptrdiff_t charpos,
16977 struct glyph_row *start, struct glyph_row *end, int dy)
16978 {
16979 struct glyph_row *row = start;
16980 struct glyph_row *best_row = NULL;
16981 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
16982 int last_y;
16983
16984 /* If we happen to start on a header-line, skip that. */
16985 if (row->mode_line_p)
16986 ++row;
16987
16988 if ((end && row >= end) || !row->enabled_p)
16989 return NULL;
16990
16991 last_y = window_text_bottom_y (w) - dy;
16992
16993 while (1)
16994 {
16995 /* Give up if we have gone too far. */
16996 if (end && row >= end)
16997 return NULL;
16998 /* This formerly returned if they were equal.
16999 I think that both quantities are of a "last plus one" type;
17000 if so, when they are equal, the row is within the screen. -- rms. */
17001 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17002 return NULL;
17003
17004 /* If it is in this row, return this row. */
17005 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17006 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17007 /* The end position of a row equals the start
17008 position of the next row. If CHARPOS is there, we
17009 would rather consider it displayed in the next
17010 line, except when this line ends in ZV. */
17011 && !row_for_charpos_p (row, charpos)))
17012 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17013 {
17014 struct glyph *g;
17015
17016 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17017 || (!best_row && !row->continued_p))
17018 return row;
17019 /* In bidi-reordered rows, there could be several rows whose
17020 edges surround CHARPOS, all of these rows belonging to
17021 the same continued line. We need to find the row which
17022 fits CHARPOS the best. */
17023 for (g = row->glyphs[TEXT_AREA];
17024 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17025 g++)
17026 {
17027 if (!STRINGP (g->object))
17028 {
17029 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17030 {
17031 mindif = eabs (g->charpos - charpos);
17032 best_row = row;
17033 /* Exact match always wins. */
17034 if (mindif == 0)
17035 return best_row;
17036 }
17037 }
17038 }
17039 }
17040 else if (best_row && !row->continued_p)
17041 return best_row;
17042 ++row;
17043 }
17044 }
17045
17046
17047 /* Try to redisplay window W by reusing its existing display. W's
17048 current matrix must be up to date when this function is called,
17049 i.e. window_end_valid must be nonzero.
17050
17051 Value is
17052
17053 1 if display has been updated
17054 0 if otherwise unsuccessful
17055 -1 if redisplay with same window start is known not to succeed
17056
17057 The following steps are performed:
17058
17059 1. Find the last row in the current matrix of W that is not
17060 affected by changes at the start of current_buffer. If no such row
17061 is found, give up.
17062
17063 2. Find the first row in W's current matrix that is not affected by
17064 changes at the end of current_buffer. Maybe there is no such row.
17065
17066 3. Display lines beginning with the row + 1 found in step 1 to the
17067 row found in step 2 or, if step 2 didn't find a row, to the end of
17068 the window.
17069
17070 4. If cursor is not known to appear on the window, give up.
17071
17072 5. If display stopped at the row found in step 2, scroll the
17073 display and current matrix as needed.
17074
17075 6. Maybe display some lines at the end of W, if we must. This can
17076 happen under various circumstances, like a partially visible line
17077 becoming fully visible, or because newly displayed lines are displayed
17078 in smaller font sizes.
17079
17080 7. Update W's window end information. */
17081
17082 static int
17083 try_window_id (struct window *w)
17084 {
17085 struct frame *f = XFRAME (w->frame);
17086 struct glyph_matrix *current_matrix = w->current_matrix;
17087 struct glyph_matrix *desired_matrix = w->desired_matrix;
17088 struct glyph_row *last_unchanged_at_beg_row;
17089 struct glyph_row *first_unchanged_at_end_row;
17090 struct glyph_row *row;
17091 struct glyph_row *bottom_row;
17092 int bottom_vpos;
17093 struct it it;
17094 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17095 int dvpos, dy;
17096 struct text_pos start_pos;
17097 struct run run;
17098 int first_unchanged_at_end_vpos = 0;
17099 struct glyph_row *last_text_row, *last_text_row_at_end;
17100 struct text_pos start;
17101 ptrdiff_t first_changed_charpos, last_changed_charpos;
17102
17103 #ifdef GLYPH_DEBUG
17104 if (inhibit_try_window_id)
17105 return 0;
17106 #endif
17107
17108 /* This is handy for debugging. */
17109 #if 0
17110 #define GIVE_UP(X) \
17111 do { \
17112 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17113 return 0; \
17114 } while (0)
17115 #else
17116 #define GIVE_UP(X) return 0
17117 #endif
17118
17119 SET_TEXT_POS_FROM_MARKER (start, w->start);
17120
17121 /* Don't use this for mini-windows because these can show
17122 messages and mini-buffers, and we don't handle that here. */
17123 if (MINI_WINDOW_P (w))
17124 GIVE_UP (1);
17125
17126 /* This flag is used to prevent redisplay optimizations. */
17127 if (windows_or_buffers_changed || cursor_type_changed)
17128 GIVE_UP (2);
17129
17130 /* Verify that narrowing has not changed.
17131 Also verify that we were not told to prevent redisplay optimizations.
17132 It would be nice to further
17133 reduce the number of cases where this prevents try_window_id. */
17134 if (current_buffer->clip_changed
17135 || current_buffer->prevent_redisplay_optimizations_p)
17136 GIVE_UP (3);
17137
17138 /* Window must either use window-based redisplay or be full width. */
17139 if (!FRAME_WINDOW_P (f)
17140 && (!FRAME_LINE_INS_DEL_OK (f)
17141 || !WINDOW_FULL_WIDTH_P (w)))
17142 GIVE_UP (4);
17143
17144 /* Give up if point is known NOT to appear in W. */
17145 if (PT < CHARPOS (start))
17146 GIVE_UP (5);
17147
17148 /* Another way to prevent redisplay optimizations. */
17149 if (w->last_modified == 0)
17150 GIVE_UP (6);
17151
17152 /* Verify that window is not hscrolled. */
17153 if (w->hscroll != 0)
17154 GIVE_UP (7);
17155
17156 /* Verify that display wasn't paused. */
17157 if (!w->window_end_valid)
17158 GIVE_UP (8);
17159
17160 /* Can't use this if highlighting a region because a cursor movement
17161 will do more than just set the cursor. */
17162 if (markpos_of_region () >= 0)
17163 GIVE_UP (9);
17164
17165 /* Likewise if highlighting trailing whitespace. */
17166 if (!NILP (Vshow_trailing_whitespace))
17167 GIVE_UP (11);
17168
17169 /* Likewise if showing a region. */
17170 if (w->region_showing)
17171 GIVE_UP (10);
17172
17173 /* Can't use this if overlay arrow position and/or string have
17174 changed. */
17175 if (overlay_arrows_changed_p ())
17176 GIVE_UP (12);
17177
17178 /* When word-wrap is on, adding a space to the first word of a
17179 wrapped line can change the wrap position, altering the line
17180 above it. It might be worthwhile to handle this more
17181 intelligently, but for now just redisplay from scratch. */
17182 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17183 GIVE_UP (21);
17184
17185 /* Under bidi reordering, adding or deleting a character in the
17186 beginning of a paragraph, before the first strong directional
17187 character, can change the base direction of the paragraph (unless
17188 the buffer specifies a fixed paragraph direction), which will
17189 require to redisplay the whole paragraph. It might be worthwhile
17190 to find the paragraph limits and widen the range of redisplayed
17191 lines to that, but for now just give up this optimization and
17192 redisplay from scratch. */
17193 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17194 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17195 GIVE_UP (22);
17196
17197 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17198 only if buffer has really changed. The reason is that the gap is
17199 initially at Z for freshly visited files. The code below would
17200 set end_unchanged to 0 in that case. */
17201 if (MODIFF > SAVE_MODIFF
17202 /* This seems to happen sometimes after saving a buffer. */
17203 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17204 {
17205 if (GPT - BEG < BEG_UNCHANGED)
17206 BEG_UNCHANGED = GPT - BEG;
17207 if (Z - GPT < END_UNCHANGED)
17208 END_UNCHANGED = Z - GPT;
17209 }
17210
17211 /* The position of the first and last character that has been changed. */
17212 first_changed_charpos = BEG + BEG_UNCHANGED;
17213 last_changed_charpos = Z - END_UNCHANGED;
17214
17215 /* If window starts after a line end, and the last change is in
17216 front of that newline, then changes don't affect the display.
17217 This case happens with stealth-fontification. Note that although
17218 the display is unchanged, glyph positions in the matrix have to
17219 be adjusted, of course. */
17220 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17221 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17222 && ((last_changed_charpos < CHARPOS (start)
17223 && CHARPOS (start) == BEGV)
17224 || (last_changed_charpos < CHARPOS (start) - 1
17225 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17226 {
17227 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17228 struct glyph_row *r0;
17229
17230 /* Compute how many chars/bytes have been added to or removed
17231 from the buffer. */
17232 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17233 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17234 Z_delta = Z - Z_old;
17235 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17236
17237 /* Give up if PT is not in the window. Note that it already has
17238 been checked at the start of try_window_id that PT is not in
17239 front of the window start. */
17240 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17241 GIVE_UP (13);
17242
17243 /* If window start is unchanged, we can reuse the whole matrix
17244 as is, after adjusting glyph positions. No need to compute
17245 the window end again, since its offset from Z hasn't changed. */
17246 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17247 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17248 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17249 /* PT must not be in a partially visible line. */
17250 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17251 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17252 {
17253 /* Adjust positions in the glyph matrix. */
17254 if (Z_delta || Z_delta_bytes)
17255 {
17256 struct glyph_row *r1
17257 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17258 increment_matrix_positions (w->current_matrix,
17259 MATRIX_ROW_VPOS (r0, current_matrix),
17260 MATRIX_ROW_VPOS (r1, current_matrix),
17261 Z_delta, Z_delta_bytes);
17262 }
17263
17264 /* Set the cursor. */
17265 row = row_containing_pos (w, PT, r0, NULL, 0);
17266 if (row)
17267 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17268 else
17269 emacs_abort ();
17270 return 1;
17271 }
17272 }
17273
17274 /* Handle the case that changes are all below what is displayed in
17275 the window, and that PT is in the window. This shortcut cannot
17276 be taken if ZV is visible in the window, and text has been added
17277 there that is visible in the window. */
17278 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17279 /* ZV is not visible in the window, or there are no
17280 changes at ZV, actually. */
17281 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17282 || first_changed_charpos == last_changed_charpos))
17283 {
17284 struct glyph_row *r0;
17285
17286 /* Give up if PT is not in the window. Note that it already has
17287 been checked at the start of try_window_id that PT is not in
17288 front of the window start. */
17289 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17290 GIVE_UP (14);
17291
17292 /* If window start is unchanged, we can reuse the whole matrix
17293 as is, without changing glyph positions since no text has
17294 been added/removed in front of the window end. */
17295 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17296 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17297 /* PT must not be in a partially visible line. */
17298 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17299 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17300 {
17301 /* We have to compute the window end anew since text
17302 could have been added/removed after it. */
17303 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17304 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17305
17306 /* Set the cursor. */
17307 row = row_containing_pos (w, PT, r0, NULL, 0);
17308 if (row)
17309 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17310 else
17311 emacs_abort ();
17312 return 2;
17313 }
17314 }
17315
17316 /* Give up if window start is in the changed area.
17317
17318 The condition used to read
17319
17320 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17321
17322 but why that was tested escapes me at the moment. */
17323 if (CHARPOS (start) >= first_changed_charpos
17324 && CHARPOS (start) <= last_changed_charpos)
17325 GIVE_UP (15);
17326
17327 /* Check that window start agrees with the start of the first glyph
17328 row in its current matrix. Check this after we know the window
17329 start is not in changed text, otherwise positions would not be
17330 comparable. */
17331 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17332 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17333 GIVE_UP (16);
17334
17335 /* Give up if the window ends in strings. Overlay strings
17336 at the end are difficult to handle, so don't try. */
17337 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
17338 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17339 GIVE_UP (20);
17340
17341 /* Compute the position at which we have to start displaying new
17342 lines. Some of the lines at the top of the window might be
17343 reusable because they are not displaying changed text. Find the
17344 last row in W's current matrix not affected by changes at the
17345 start of current_buffer. Value is null if changes start in the
17346 first line of window. */
17347 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17348 if (last_unchanged_at_beg_row)
17349 {
17350 /* Avoid starting to display in the middle of a character, a TAB
17351 for instance. This is easier than to set up the iterator
17352 exactly, and it's not a frequent case, so the additional
17353 effort wouldn't really pay off. */
17354 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17355 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17356 && last_unchanged_at_beg_row > w->current_matrix->rows)
17357 --last_unchanged_at_beg_row;
17358
17359 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17360 GIVE_UP (17);
17361
17362 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17363 GIVE_UP (18);
17364 start_pos = it.current.pos;
17365
17366 /* Start displaying new lines in the desired matrix at the same
17367 vpos we would use in the current matrix, i.e. below
17368 last_unchanged_at_beg_row. */
17369 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17370 current_matrix);
17371 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17372 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17373
17374 eassert (it.hpos == 0 && it.current_x == 0);
17375 }
17376 else
17377 {
17378 /* There are no reusable lines at the start of the window.
17379 Start displaying in the first text line. */
17380 start_display (&it, w, start);
17381 it.vpos = it.first_vpos;
17382 start_pos = it.current.pos;
17383 }
17384
17385 /* Find the first row that is not affected by changes at the end of
17386 the buffer. Value will be null if there is no unchanged row, in
17387 which case we must redisplay to the end of the window. delta
17388 will be set to the value by which buffer positions beginning with
17389 first_unchanged_at_end_row have to be adjusted due to text
17390 changes. */
17391 first_unchanged_at_end_row
17392 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17393 IF_DEBUG (debug_delta = delta);
17394 IF_DEBUG (debug_delta_bytes = delta_bytes);
17395
17396 /* Set stop_pos to the buffer position up to which we will have to
17397 display new lines. If first_unchanged_at_end_row != NULL, this
17398 is the buffer position of the start of the line displayed in that
17399 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17400 that we don't stop at a buffer position. */
17401 stop_pos = 0;
17402 if (first_unchanged_at_end_row)
17403 {
17404 eassert (last_unchanged_at_beg_row == NULL
17405 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17406
17407 /* If this is a continuation line, move forward to the next one
17408 that isn't. Changes in lines above affect this line.
17409 Caution: this may move first_unchanged_at_end_row to a row
17410 not displaying text. */
17411 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17412 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17413 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17414 < it.last_visible_y))
17415 ++first_unchanged_at_end_row;
17416
17417 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17418 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17419 >= it.last_visible_y))
17420 first_unchanged_at_end_row = NULL;
17421 else
17422 {
17423 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17424 + delta);
17425 first_unchanged_at_end_vpos
17426 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17427 eassert (stop_pos >= Z - END_UNCHANGED);
17428 }
17429 }
17430 else if (last_unchanged_at_beg_row == NULL)
17431 GIVE_UP (19);
17432
17433
17434 #ifdef GLYPH_DEBUG
17435
17436 /* Either there is no unchanged row at the end, or the one we have
17437 now displays text. This is a necessary condition for the window
17438 end pos calculation at the end of this function. */
17439 eassert (first_unchanged_at_end_row == NULL
17440 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17441
17442 debug_last_unchanged_at_beg_vpos
17443 = (last_unchanged_at_beg_row
17444 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17445 : -1);
17446 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17447
17448 #endif /* GLYPH_DEBUG */
17449
17450
17451 /* Display new lines. Set last_text_row to the last new line
17452 displayed which has text on it, i.e. might end up as being the
17453 line where the window_end_vpos is. */
17454 w->cursor.vpos = -1;
17455 last_text_row = NULL;
17456 overlay_arrow_seen = 0;
17457 while (it.current_y < it.last_visible_y
17458 && !fonts_changed_p
17459 && (first_unchanged_at_end_row == NULL
17460 || IT_CHARPOS (it) < stop_pos))
17461 {
17462 if (display_line (&it))
17463 last_text_row = it.glyph_row - 1;
17464 }
17465
17466 if (fonts_changed_p)
17467 return -1;
17468
17469
17470 /* Compute differences in buffer positions, y-positions etc. for
17471 lines reused at the bottom of the window. Compute what we can
17472 scroll. */
17473 if (first_unchanged_at_end_row
17474 /* No lines reused because we displayed everything up to the
17475 bottom of the window. */
17476 && it.current_y < it.last_visible_y)
17477 {
17478 dvpos = (it.vpos
17479 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17480 current_matrix));
17481 dy = it.current_y - first_unchanged_at_end_row->y;
17482 run.current_y = first_unchanged_at_end_row->y;
17483 run.desired_y = run.current_y + dy;
17484 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17485 }
17486 else
17487 {
17488 delta = delta_bytes = dvpos = dy
17489 = run.current_y = run.desired_y = run.height = 0;
17490 first_unchanged_at_end_row = NULL;
17491 }
17492 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17493
17494
17495 /* Find the cursor if not already found. We have to decide whether
17496 PT will appear on this window (it sometimes doesn't, but this is
17497 not a very frequent case.) This decision has to be made before
17498 the current matrix is altered. A value of cursor.vpos < 0 means
17499 that PT is either in one of the lines beginning at
17500 first_unchanged_at_end_row or below the window. Don't care for
17501 lines that might be displayed later at the window end; as
17502 mentioned, this is not a frequent case. */
17503 if (w->cursor.vpos < 0)
17504 {
17505 /* Cursor in unchanged rows at the top? */
17506 if (PT < CHARPOS (start_pos)
17507 && last_unchanged_at_beg_row)
17508 {
17509 row = row_containing_pos (w, PT,
17510 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17511 last_unchanged_at_beg_row + 1, 0);
17512 if (row)
17513 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17514 }
17515
17516 /* Start from first_unchanged_at_end_row looking for PT. */
17517 else if (first_unchanged_at_end_row)
17518 {
17519 row = row_containing_pos (w, PT - delta,
17520 first_unchanged_at_end_row, NULL, 0);
17521 if (row)
17522 set_cursor_from_row (w, row, w->current_matrix, delta,
17523 delta_bytes, dy, dvpos);
17524 }
17525
17526 /* Give up if cursor was not found. */
17527 if (w->cursor.vpos < 0)
17528 {
17529 clear_glyph_matrix (w->desired_matrix);
17530 return -1;
17531 }
17532 }
17533
17534 /* Don't let the cursor end in the scroll margins. */
17535 {
17536 int this_scroll_margin, cursor_height;
17537 int frame_line_height = default_line_pixel_height (w);
17538 int window_total_lines
17539 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
17540
17541 this_scroll_margin =
17542 max (0, min (scroll_margin, window_total_lines / 4));
17543 this_scroll_margin *= frame_line_height;
17544 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17545
17546 if ((w->cursor.y < this_scroll_margin
17547 && CHARPOS (start) > BEGV)
17548 /* Old redisplay didn't take scroll margin into account at the bottom,
17549 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17550 || (w->cursor.y + (make_cursor_line_fully_visible_p
17551 ? cursor_height + this_scroll_margin
17552 : 1)) > it.last_visible_y)
17553 {
17554 w->cursor.vpos = -1;
17555 clear_glyph_matrix (w->desired_matrix);
17556 return -1;
17557 }
17558 }
17559
17560 /* Scroll the display. Do it before changing the current matrix so
17561 that xterm.c doesn't get confused about where the cursor glyph is
17562 found. */
17563 if (dy && run.height)
17564 {
17565 update_begin (f);
17566
17567 if (FRAME_WINDOW_P (f))
17568 {
17569 FRAME_RIF (f)->update_window_begin_hook (w);
17570 FRAME_RIF (f)->clear_window_mouse_face (w);
17571 FRAME_RIF (f)->scroll_run_hook (w, &run);
17572 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17573 }
17574 else
17575 {
17576 /* Terminal frame. In this case, dvpos gives the number of
17577 lines to scroll by; dvpos < 0 means scroll up. */
17578 int from_vpos
17579 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17580 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17581 int end = (WINDOW_TOP_EDGE_LINE (w)
17582 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17583 + window_internal_height (w));
17584
17585 #if defined (HAVE_GPM) || defined (MSDOS)
17586 x_clear_window_mouse_face (w);
17587 #endif
17588 /* Perform the operation on the screen. */
17589 if (dvpos > 0)
17590 {
17591 /* Scroll last_unchanged_at_beg_row to the end of the
17592 window down dvpos lines. */
17593 set_terminal_window (f, end);
17594
17595 /* On dumb terminals delete dvpos lines at the end
17596 before inserting dvpos empty lines. */
17597 if (!FRAME_SCROLL_REGION_OK (f))
17598 ins_del_lines (f, end - dvpos, -dvpos);
17599
17600 /* Insert dvpos empty lines in front of
17601 last_unchanged_at_beg_row. */
17602 ins_del_lines (f, from, dvpos);
17603 }
17604 else if (dvpos < 0)
17605 {
17606 /* Scroll up last_unchanged_at_beg_vpos to the end of
17607 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17608 set_terminal_window (f, end);
17609
17610 /* Delete dvpos lines in front of
17611 last_unchanged_at_beg_vpos. ins_del_lines will set
17612 the cursor to the given vpos and emit |dvpos| delete
17613 line sequences. */
17614 ins_del_lines (f, from + dvpos, dvpos);
17615
17616 /* On a dumb terminal insert dvpos empty lines at the
17617 end. */
17618 if (!FRAME_SCROLL_REGION_OK (f))
17619 ins_del_lines (f, end + dvpos, -dvpos);
17620 }
17621
17622 set_terminal_window (f, 0);
17623 }
17624
17625 update_end (f);
17626 }
17627
17628 /* Shift reused rows of the current matrix to the right position.
17629 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17630 text. */
17631 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17632 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17633 if (dvpos < 0)
17634 {
17635 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17636 bottom_vpos, dvpos);
17637 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17638 bottom_vpos);
17639 }
17640 else if (dvpos > 0)
17641 {
17642 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17643 bottom_vpos, dvpos);
17644 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17645 first_unchanged_at_end_vpos + dvpos);
17646 }
17647
17648 /* For frame-based redisplay, make sure that current frame and window
17649 matrix are in sync with respect to glyph memory. */
17650 if (!FRAME_WINDOW_P (f))
17651 sync_frame_with_window_matrix_rows (w);
17652
17653 /* Adjust buffer positions in reused rows. */
17654 if (delta || delta_bytes)
17655 increment_matrix_positions (current_matrix,
17656 first_unchanged_at_end_vpos + dvpos,
17657 bottom_vpos, delta, delta_bytes);
17658
17659 /* Adjust Y positions. */
17660 if (dy)
17661 shift_glyph_matrix (w, current_matrix,
17662 first_unchanged_at_end_vpos + dvpos,
17663 bottom_vpos, dy);
17664
17665 if (first_unchanged_at_end_row)
17666 {
17667 first_unchanged_at_end_row += dvpos;
17668 if (first_unchanged_at_end_row->y >= it.last_visible_y
17669 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17670 first_unchanged_at_end_row = NULL;
17671 }
17672
17673 /* If scrolling up, there may be some lines to display at the end of
17674 the window. */
17675 last_text_row_at_end = NULL;
17676 if (dy < 0)
17677 {
17678 /* Scrolling up can leave for example a partially visible line
17679 at the end of the window to be redisplayed. */
17680 /* Set last_row to the glyph row in the current matrix where the
17681 window end line is found. It has been moved up or down in
17682 the matrix by dvpos. */
17683 int last_vpos = w->window_end_vpos + dvpos;
17684 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17685
17686 /* If last_row is the window end line, it should display text. */
17687 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
17688
17689 /* If window end line was partially visible before, begin
17690 displaying at that line. Otherwise begin displaying with the
17691 line following it. */
17692 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17693 {
17694 init_to_row_start (&it, w, last_row);
17695 it.vpos = last_vpos;
17696 it.current_y = last_row->y;
17697 }
17698 else
17699 {
17700 init_to_row_end (&it, w, last_row);
17701 it.vpos = 1 + last_vpos;
17702 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17703 ++last_row;
17704 }
17705
17706 /* We may start in a continuation line. If so, we have to
17707 get the right continuation_lines_width and current_x. */
17708 it.continuation_lines_width = last_row->continuation_lines_width;
17709 it.hpos = it.current_x = 0;
17710
17711 /* Display the rest of the lines at the window end. */
17712 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17713 while (it.current_y < it.last_visible_y
17714 && !fonts_changed_p)
17715 {
17716 /* Is it always sure that the display agrees with lines in
17717 the current matrix? I don't think so, so we mark rows
17718 displayed invalid in the current matrix by setting their
17719 enabled_p flag to zero. */
17720 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17721 if (display_line (&it))
17722 last_text_row_at_end = it.glyph_row - 1;
17723 }
17724 }
17725
17726 /* Update window_end_pos and window_end_vpos. */
17727 if (first_unchanged_at_end_row && !last_text_row_at_end)
17728 {
17729 /* Window end line if one of the preserved rows from the current
17730 matrix. Set row to the last row displaying text in current
17731 matrix starting at first_unchanged_at_end_row, after
17732 scrolling. */
17733 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17734 row = find_last_row_displaying_text (w->current_matrix, &it,
17735 first_unchanged_at_end_row);
17736 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17737 adjust_window_ends (w, row, 1);
17738 eassert (w->window_end_bytepos >= 0);
17739 IF_DEBUG (debug_method_add (w, "A"));
17740 }
17741 else if (last_text_row_at_end)
17742 {
17743 adjust_window_ends (w, last_text_row_at_end, 0);
17744 eassert (w->window_end_bytepos >= 0);
17745 IF_DEBUG (debug_method_add (w, "B"));
17746 }
17747 else if (last_text_row)
17748 {
17749 /* We have displayed either to the end of the window or at the
17750 end of the window, i.e. the last row with text is to be found
17751 in the desired matrix. */
17752 adjust_window_ends (w, last_text_row, 0);
17753 eassert (w->window_end_bytepos >= 0);
17754 }
17755 else if (first_unchanged_at_end_row == NULL
17756 && last_text_row == NULL
17757 && last_text_row_at_end == NULL)
17758 {
17759 /* Displayed to end of window, but no line containing text was
17760 displayed. Lines were deleted at the end of the window. */
17761 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17762 int vpos = w->window_end_vpos;
17763 struct glyph_row *current_row = current_matrix->rows + vpos;
17764 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17765
17766 for (row = NULL;
17767 row == NULL && vpos >= first_vpos;
17768 --vpos, --current_row, --desired_row)
17769 {
17770 if (desired_row->enabled_p)
17771 {
17772 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
17773 row = desired_row;
17774 }
17775 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
17776 row = current_row;
17777 }
17778
17779 eassert (row != NULL);
17780 w->window_end_vpos = vpos + 1;
17781 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17782 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17783 eassert (w->window_end_bytepos >= 0);
17784 IF_DEBUG (debug_method_add (w, "C"));
17785 }
17786 else
17787 emacs_abort ();
17788
17789 IF_DEBUG (debug_end_pos = w->window_end_pos;
17790 debug_end_vpos = w->window_end_vpos);
17791
17792 /* Record that display has not been completed. */
17793 w->window_end_valid = 0;
17794 w->desired_matrix->no_scrolling_p = 1;
17795 return 3;
17796
17797 #undef GIVE_UP
17798 }
17799
17800
17801 \f
17802 /***********************************************************************
17803 More debugging support
17804 ***********************************************************************/
17805
17806 #ifdef GLYPH_DEBUG
17807
17808 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17809 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17810 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17811
17812
17813 /* Dump the contents of glyph matrix MATRIX on stderr.
17814
17815 GLYPHS 0 means don't show glyph contents.
17816 GLYPHS 1 means show glyphs in short form
17817 GLYPHS > 1 means show glyphs in long form. */
17818
17819 void
17820 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17821 {
17822 int i;
17823 for (i = 0; i < matrix->nrows; ++i)
17824 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17825 }
17826
17827
17828 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17829 the glyph row and area where the glyph comes from. */
17830
17831 void
17832 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17833 {
17834 if (glyph->type == CHAR_GLYPH
17835 || glyph->type == GLYPHLESS_GLYPH)
17836 {
17837 fprintf (stderr,
17838 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17839 glyph - row->glyphs[TEXT_AREA],
17840 (glyph->type == CHAR_GLYPH
17841 ? 'C'
17842 : 'G'),
17843 glyph->charpos,
17844 (BUFFERP (glyph->object)
17845 ? 'B'
17846 : (STRINGP (glyph->object)
17847 ? 'S'
17848 : (INTEGERP (glyph->object)
17849 ? '0'
17850 : '-'))),
17851 glyph->pixel_width,
17852 glyph->u.ch,
17853 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17854 ? glyph->u.ch
17855 : '.'),
17856 glyph->face_id,
17857 glyph->left_box_line_p,
17858 glyph->right_box_line_p);
17859 }
17860 else if (glyph->type == STRETCH_GLYPH)
17861 {
17862 fprintf (stderr,
17863 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17864 glyph - row->glyphs[TEXT_AREA],
17865 'S',
17866 glyph->charpos,
17867 (BUFFERP (glyph->object)
17868 ? 'B'
17869 : (STRINGP (glyph->object)
17870 ? 'S'
17871 : (INTEGERP (glyph->object)
17872 ? '0'
17873 : '-'))),
17874 glyph->pixel_width,
17875 0,
17876 ' ',
17877 glyph->face_id,
17878 glyph->left_box_line_p,
17879 glyph->right_box_line_p);
17880 }
17881 else if (glyph->type == IMAGE_GLYPH)
17882 {
17883 fprintf (stderr,
17884 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17885 glyph - row->glyphs[TEXT_AREA],
17886 'I',
17887 glyph->charpos,
17888 (BUFFERP (glyph->object)
17889 ? 'B'
17890 : (STRINGP (glyph->object)
17891 ? 'S'
17892 : (INTEGERP (glyph->object)
17893 ? '0'
17894 : '-'))),
17895 glyph->pixel_width,
17896 glyph->u.img_id,
17897 '.',
17898 glyph->face_id,
17899 glyph->left_box_line_p,
17900 glyph->right_box_line_p);
17901 }
17902 else if (glyph->type == COMPOSITE_GLYPH)
17903 {
17904 fprintf (stderr,
17905 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
17906 glyph - row->glyphs[TEXT_AREA],
17907 '+',
17908 glyph->charpos,
17909 (BUFFERP (glyph->object)
17910 ? 'B'
17911 : (STRINGP (glyph->object)
17912 ? 'S'
17913 : (INTEGERP (glyph->object)
17914 ? '0'
17915 : '-'))),
17916 glyph->pixel_width,
17917 glyph->u.cmp.id);
17918 if (glyph->u.cmp.automatic)
17919 fprintf (stderr,
17920 "[%d-%d]",
17921 glyph->slice.cmp.from, glyph->slice.cmp.to);
17922 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17923 glyph->face_id,
17924 glyph->left_box_line_p,
17925 glyph->right_box_line_p);
17926 }
17927 }
17928
17929
17930 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17931 GLYPHS 0 means don't show glyph contents.
17932 GLYPHS 1 means show glyphs in short form
17933 GLYPHS > 1 means show glyphs in long form. */
17934
17935 void
17936 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17937 {
17938 if (glyphs != 1)
17939 {
17940 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17941 fprintf (stderr, "==============================================================================\n");
17942
17943 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17944 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17945 vpos,
17946 MATRIX_ROW_START_CHARPOS (row),
17947 MATRIX_ROW_END_CHARPOS (row),
17948 row->used[TEXT_AREA],
17949 row->contains_overlapping_glyphs_p,
17950 row->enabled_p,
17951 row->truncated_on_left_p,
17952 row->truncated_on_right_p,
17953 row->continued_p,
17954 MATRIX_ROW_CONTINUATION_LINE_P (row),
17955 MATRIX_ROW_DISPLAYS_TEXT_P (row),
17956 row->ends_at_zv_p,
17957 row->fill_line_p,
17958 row->ends_in_middle_of_char_p,
17959 row->starts_in_middle_of_char_p,
17960 row->mouse_face_p,
17961 row->x,
17962 row->y,
17963 row->pixel_width,
17964 row->height,
17965 row->visible_height,
17966 row->ascent,
17967 row->phys_ascent);
17968 /* The next 3 lines should align to "Start" in the header. */
17969 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
17970 row->end.overlay_string_index,
17971 row->continuation_lines_width);
17972 fprintf (stderr, " %9"pI"d %9"pI"d\n",
17973 CHARPOS (row->start.string_pos),
17974 CHARPOS (row->end.string_pos));
17975 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
17976 row->end.dpvec_index);
17977 }
17978
17979 if (glyphs > 1)
17980 {
17981 int area;
17982
17983 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17984 {
17985 struct glyph *glyph = row->glyphs[area];
17986 struct glyph *glyph_end = glyph + row->used[area];
17987
17988 /* Glyph for a line end in text. */
17989 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
17990 ++glyph_end;
17991
17992 if (glyph < glyph_end)
17993 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
17994
17995 for (; glyph < glyph_end; ++glyph)
17996 dump_glyph (row, glyph, area);
17997 }
17998 }
17999 else if (glyphs == 1)
18000 {
18001 int area;
18002
18003 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18004 {
18005 char *s = alloca (row->used[area] + 4);
18006 int i;
18007
18008 for (i = 0; i < row->used[area]; ++i)
18009 {
18010 struct glyph *glyph = row->glyphs[area] + i;
18011 if (i == row->used[area] - 1
18012 && area == TEXT_AREA
18013 && INTEGERP (glyph->object)
18014 && glyph->type == CHAR_GLYPH
18015 && glyph->u.ch == ' ')
18016 {
18017 strcpy (&s[i], "[\\n]");
18018 i += 4;
18019 }
18020 else if (glyph->type == CHAR_GLYPH
18021 && glyph->u.ch < 0x80
18022 && glyph->u.ch >= ' ')
18023 s[i] = glyph->u.ch;
18024 else
18025 s[i] = '.';
18026 }
18027
18028 s[i] = '\0';
18029 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18030 }
18031 }
18032 }
18033
18034
18035 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18036 Sdump_glyph_matrix, 0, 1, "p",
18037 doc: /* Dump the current matrix of the selected window to stderr.
18038 Shows contents of glyph row structures. With non-nil
18039 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18040 glyphs in short form, otherwise show glyphs in long form. */)
18041 (Lisp_Object glyphs)
18042 {
18043 struct window *w = XWINDOW (selected_window);
18044 struct buffer *buffer = XBUFFER (w->contents);
18045
18046 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18047 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18048 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18049 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18050 fprintf (stderr, "=============================================\n");
18051 dump_glyph_matrix (w->current_matrix,
18052 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18053 return Qnil;
18054 }
18055
18056
18057 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18058 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18059 (void)
18060 {
18061 struct frame *f = XFRAME (selected_frame);
18062 dump_glyph_matrix (f->current_matrix, 1);
18063 return Qnil;
18064 }
18065
18066
18067 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18068 doc: /* Dump glyph row ROW to stderr.
18069 GLYPH 0 means don't dump glyphs.
18070 GLYPH 1 means dump glyphs in short form.
18071 GLYPH > 1 or omitted means dump glyphs in long form. */)
18072 (Lisp_Object row, Lisp_Object glyphs)
18073 {
18074 struct glyph_matrix *matrix;
18075 EMACS_INT vpos;
18076
18077 CHECK_NUMBER (row);
18078 matrix = XWINDOW (selected_window)->current_matrix;
18079 vpos = XINT (row);
18080 if (vpos >= 0 && vpos < matrix->nrows)
18081 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18082 vpos,
18083 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18084 return Qnil;
18085 }
18086
18087
18088 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18089 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18090 GLYPH 0 means don't dump glyphs.
18091 GLYPH 1 means dump glyphs in short form.
18092 GLYPH > 1 or omitted means dump glyphs in long form. */)
18093 (Lisp_Object row, Lisp_Object glyphs)
18094 {
18095 struct frame *sf = SELECTED_FRAME ();
18096 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18097 EMACS_INT vpos;
18098
18099 CHECK_NUMBER (row);
18100 vpos = XINT (row);
18101 if (vpos >= 0 && vpos < m->nrows)
18102 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18103 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18104 return Qnil;
18105 }
18106
18107
18108 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18109 doc: /* Toggle tracing of redisplay.
18110 With ARG, turn tracing on if and only if ARG is positive. */)
18111 (Lisp_Object arg)
18112 {
18113 if (NILP (arg))
18114 trace_redisplay_p = !trace_redisplay_p;
18115 else
18116 {
18117 arg = Fprefix_numeric_value (arg);
18118 trace_redisplay_p = XINT (arg) > 0;
18119 }
18120
18121 return Qnil;
18122 }
18123
18124
18125 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18126 doc: /* Like `format', but print result to stderr.
18127 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18128 (ptrdiff_t nargs, Lisp_Object *args)
18129 {
18130 Lisp_Object s = Fformat (nargs, args);
18131 fprintf (stderr, "%s", SDATA (s));
18132 return Qnil;
18133 }
18134
18135 #endif /* GLYPH_DEBUG */
18136
18137
18138 \f
18139 /***********************************************************************
18140 Building Desired Matrix Rows
18141 ***********************************************************************/
18142
18143 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18144 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18145
18146 static struct glyph_row *
18147 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18148 {
18149 struct frame *f = XFRAME (WINDOW_FRAME (w));
18150 struct buffer *buffer = XBUFFER (w->contents);
18151 struct buffer *old = current_buffer;
18152 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18153 int arrow_len = SCHARS (overlay_arrow_string);
18154 const unsigned char *arrow_end = arrow_string + arrow_len;
18155 const unsigned char *p;
18156 struct it it;
18157 bool multibyte_p;
18158 int n_glyphs_before;
18159
18160 set_buffer_temp (buffer);
18161 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18162 it.glyph_row->used[TEXT_AREA] = 0;
18163 SET_TEXT_POS (it.position, 0, 0);
18164
18165 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18166 p = arrow_string;
18167 while (p < arrow_end)
18168 {
18169 Lisp_Object face, ilisp;
18170
18171 /* Get the next character. */
18172 if (multibyte_p)
18173 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18174 else
18175 {
18176 it.c = it.char_to_display = *p, it.len = 1;
18177 if (! ASCII_CHAR_P (it.c))
18178 it.char_to_display = BYTE8_TO_CHAR (it.c);
18179 }
18180 p += it.len;
18181
18182 /* Get its face. */
18183 ilisp = make_number (p - arrow_string);
18184 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18185 it.face_id = compute_char_face (f, it.char_to_display, face);
18186
18187 /* Compute its width, get its glyphs. */
18188 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18189 SET_TEXT_POS (it.position, -1, -1);
18190 PRODUCE_GLYPHS (&it);
18191
18192 /* If this character doesn't fit any more in the line, we have
18193 to remove some glyphs. */
18194 if (it.current_x > it.last_visible_x)
18195 {
18196 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18197 break;
18198 }
18199 }
18200
18201 set_buffer_temp (old);
18202 return it.glyph_row;
18203 }
18204
18205
18206 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18207 glyphs to insert is determined by produce_special_glyphs. */
18208
18209 static void
18210 insert_left_trunc_glyphs (struct it *it)
18211 {
18212 struct it truncate_it;
18213 struct glyph *from, *end, *to, *toend;
18214
18215 eassert (!FRAME_WINDOW_P (it->f)
18216 || (!it->glyph_row->reversed_p
18217 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18218 || (it->glyph_row->reversed_p
18219 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18220
18221 /* Get the truncation glyphs. */
18222 truncate_it = *it;
18223 truncate_it.current_x = 0;
18224 truncate_it.face_id = DEFAULT_FACE_ID;
18225 truncate_it.glyph_row = &scratch_glyph_row;
18226 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18227 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18228 truncate_it.object = make_number (0);
18229 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18230
18231 /* Overwrite glyphs from IT with truncation glyphs. */
18232 if (!it->glyph_row->reversed_p)
18233 {
18234 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18235
18236 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18237 end = from + tused;
18238 to = it->glyph_row->glyphs[TEXT_AREA];
18239 toend = to + it->glyph_row->used[TEXT_AREA];
18240 if (FRAME_WINDOW_P (it->f))
18241 {
18242 /* On GUI frames, when variable-size fonts are displayed,
18243 the truncation glyphs may need more pixels than the row's
18244 glyphs they overwrite. We overwrite more glyphs to free
18245 enough screen real estate, and enlarge the stretch glyph
18246 on the right (see display_line), if there is one, to
18247 preserve the screen position of the truncation glyphs on
18248 the right. */
18249 int w = 0;
18250 struct glyph *g = to;
18251 short used;
18252
18253 /* The first glyph could be partially visible, in which case
18254 it->glyph_row->x will be negative. But we want the left
18255 truncation glyphs to be aligned at the left margin of the
18256 window, so we override the x coordinate at which the row
18257 will begin. */
18258 it->glyph_row->x = 0;
18259 while (g < toend && w < it->truncation_pixel_width)
18260 {
18261 w += g->pixel_width;
18262 ++g;
18263 }
18264 if (g - to - tused > 0)
18265 {
18266 memmove (to + tused, g, (toend - g) * sizeof(*g));
18267 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18268 }
18269 used = it->glyph_row->used[TEXT_AREA];
18270 if (it->glyph_row->truncated_on_right_p
18271 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18272 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18273 == STRETCH_GLYPH)
18274 {
18275 int extra = w - it->truncation_pixel_width;
18276
18277 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18278 }
18279 }
18280
18281 while (from < end)
18282 *to++ = *from++;
18283
18284 /* There may be padding glyphs left over. Overwrite them too. */
18285 if (!FRAME_WINDOW_P (it->f))
18286 {
18287 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18288 {
18289 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18290 while (from < end)
18291 *to++ = *from++;
18292 }
18293 }
18294
18295 if (to > toend)
18296 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18297 }
18298 else
18299 {
18300 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18301
18302 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18303 that back to front. */
18304 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18305 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18306 toend = it->glyph_row->glyphs[TEXT_AREA];
18307 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18308 if (FRAME_WINDOW_P (it->f))
18309 {
18310 int w = 0;
18311 struct glyph *g = to;
18312
18313 while (g >= toend && w < it->truncation_pixel_width)
18314 {
18315 w += g->pixel_width;
18316 --g;
18317 }
18318 if (to - g - tused > 0)
18319 to = g + tused;
18320 if (it->glyph_row->truncated_on_right_p
18321 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18322 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18323 {
18324 int extra = w - it->truncation_pixel_width;
18325
18326 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18327 }
18328 }
18329
18330 while (from >= end && to >= toend)
18331 *to-- = *from--;
18332 if (!FRAME_WINDOW_P (it->f))
18333 {
18334 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18335 {
18336 from =
18337 truncate_it.glyph_row->glyphs[TEXT_AREA]
18338 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18339 while (from >= end && to >= toend)
18340 *to-- = *from--;
18341 }
18342 }
18343 if (from >= end)
18344 {
18345 /* Need to free some room before prepending additional
18346 glyphs. */
18347 int move_by = from - end + 1;
18348 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18349 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18350
18351 for ( ; g >= g0; g--)
18352 g[move_by] = *g;
18353 while (from >= end)
18354 *to-- = *from--;
18355 it->glyph_row->used[TEXT_AREA] += move_by;
18356 }
18357 }
18358 }
18359
18360 /* Compute the hash code for ROW. */
18361 unsigned
18362 row_hash (struct glyph_row *row)
18363 {
18364 int area, k;
18365 unsigned hashval = 0;
18366
18367 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18368 for (k = 0; k < row->used[area]; ++k)
18369 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18370 + row->glyphs[area][k].u.val
18371 + row->glyphs[area][k].face_id
18372 + row->glyphs[area][k].padding_p
18373 + (row->glyphs[area][k].type << 2));
18374
18375 return hashval;
18376 }
18377
18378 /* Compute the pixel height and width of IT->glyph_row.
18379
18380 Most of the time, ascent and height of a display line will be equal
18381 to the max_ascent and max_height values of the display iterator
18382 structure. This is not the case if
18383
18384 1. We hit ZV without displaying anything. In this case, max_ascent
18385 and max_height will be zero.
18386
18387 2. We have some glyphs that don't contribute to the line height.
18388 (The glyph row flag contributes_to_line_height_p is for future
18389 pixmap extensions).
18390
18391 The first case is easily covered by using default values because in
18392 these cases, the line height does not really matter, except that it
18393 must not be zero. */
18394
18395 static void
18396 compute_line_metrics (struct it *it)
18397 {
18398 struct glyph_row *row = it->glyph_row;
18399
18400 if (FRAME_WINDOW_P (it->f))
18401 {
18402 int i, min_y, max_y;
18403
18404 /* The line may consist of one space only, that was added to
18405 place the cursor on it. If so, the row's height hasn't been
18406 computed yet. */
18407 if (row->height == 0)
18408 {
18409 if (it->max_ascent + it->max_descent == 0)
18410 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18411 row->ascent = it->max_ascent;
18412 row->height = it->max_ascent + it->max_descent;
18413 row->phys_ascent = it->max_phys_ascent;
18414 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18415 row->extra_line_spacing = it->max_extra_line_spacing;
18416 }
18417
18418 /* Compute the width of this line. */
18419 row->pixel_width = row->x;
18420 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18421 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18422
18423 eassert (row->pixel_width >= 0);
18424 eassert (row->ascent >= 0 && row->height > 0);
18425
18426 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18427 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18428
18429 /* If first line's physical ascent is larger than its logical
18430 ascent, use the physical ascent, and make the row taller.
18431 This makes accented characters fully visible. */
18432 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18433 && row->phys_ascent > row->ascent)
18434 {
18435 row->height += row->phys_ascent - row->ascent;
18436 row->ascent = row->phys_ascent;
18437 }
18438
18439 /* Compute how much of the line is visible. */
18440 row->visible_height = row->height;
18441
18442 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18443 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18444
18445 if (row->y < min_y)
18446 row->visible_height -= min_y - row->y;
18447 if (row->y + row->height > max_y)
18448 row->visible_height -= row->y + row->height - max_y;
18449 }
18450 else
18451 {
18452 row->pixel_width = row->used[TEXT_AREA];
18453 if (row->continued_p)
18454 row->pixel_width -= it->continuation_pixel_width;
18455 else if (row->truncated_on_right_p)
18456 row->pixel_width -= it->truncation_pixel_width;
18457 row->ascent = row->phys_ascent = 0;
18458 row->height = row->phys_height = row->visible_height = 1;
18459 row->extra_line_spacing = 0;
18460 }
18461
18462 /* Compute a hash code for this row. */
18463 row->hash = row_hash (row);
18464
18465 it->max_ascent = it->max_descent = 0;
18466 it->max_phys_ascent = it->max_phys_descent = 0;
18467 }
18468
18469
18470 /* Append one space to the glyph row of iterator IT if doing a
18471 window-based redisplay. The space has the same face as
18472 IT->face_id. Value is non-zero if a space was added.
18473
18474 This function is called to make sure that there is always one glyph
18475 at the end of a glyph row that the cursor can be set on under
18476 window-systems. (If there weren't such a glyph we would not know
18477 how wide and tall a box cursor should be displayed).
18478
18479 At the same time this space let's a nicely handle clearing to the
18480 end of the line if the row ends in italic text. */
18481
18482 static int
18483 append_space_for_newline (struct it *it, int default_face_p)
18484 {
18485 if (FRAME_WINDOW_P (it->f))
18486 {
18487 int n = it->glyph_row->used[TEXT_AREA];
18488
18489 if (it->glyph_row->glyphs[TEXT_AREA] + n
18490 < it->glyph_row->glyphs[1 + TEXT_AREA])
18491 {
18492 /* Save some values that must not be changed.
18493 Must save IT->c and IT->len because otherwise
18494 ITERATOR_AT_END_P wouldn't work anymore after
18495 append_space_for_newline has been called. */
18496 enum display_element_type saved_what = it->what;
18497 int saved_c = it->c, saved_len = it->len;
18498 int saved_char_to_display = it->char_to_display;
18499 int saved_x = it->current_x;
18500 int saved_face_id = it->face_id;
18501 int saved_box_end = it->end_of_box_run_p;
18502 struct text_pos saved_pos;
18503 Lisp_Object saved_object;
18504 struct face *face;
18505
18506 saved_object = it->object;
18507 saved_pos = it->position;
18508
18509 it->what = IT_CHARACTER;
18510 memset (&it->position, 0, sizeof it->position);
18511 it->object = make_number (0);
18512 it->c = it->char_to_display = ' ';
18513 it->len = 1;
18514
18515 /* If the default face was remapped, be sure to use the
18516 remapped face for the appended newline. */
18517 if (default_face_p)
18518 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18519 else if (it->face_before_selective_p)
18520 it->face_id = it->saved_face_id;
18521 face = FACE_FROM_ID (it->f, it->face_id);
18522 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18523 /* In R2L rows, we will prepend a stretch glyph that will
18524 have the end_of_box_run_p flag set for it, so there's no
18525 need for the appended newline glyph to have that flag
18526 set. */
18527 if (it->glyph_row->reversed_p
18528 /* But if the appended newline glyph goes all the way to
18529 the end of the row, there will be no stretch glyph,
18530 so leave the box flag set. */
18531 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18532 it->end_of_box_run_p = 0;
18533
18534 PRODUCE_GLYPHS (it);
18535
18536 it->override_ascent = -1;
18537 it->constrain_row_ascent_descent_p = 0;
18538 it->current_x = saved_x;
18539 it->object = saved_object;
18540 it->position = saved_pos;
18541 it->what = saved_what;
18542 it->face_id = saved_face_id;
18543 it->len = saved_len;
18544 it->c = saved_c;
18545 it->char_to_display = saved_char_to_display;
18546 it->end_of_box_run_p = saved_box_end;
18547 return 1;
18548 }
18549 }
18550
18551 return 0;
18552 }
18553
18554
18555 /* Extend the face of the last glyph in the text area of IT->glyph_row
18556 to the end of the display line. Called from display_line. If the
18557 glyph row is empty, add a space glyph to it so that we know the
18558 face to draw. Set the glyph row flag fill_line_p. If the glyph
18559 row is R2L, prepend a stretch glyph to cover the empty space to the
18560 left of the leftmost glyph. */
18561
18562 static void
18563 extend_face_to_end_of_line (struct it *it)
18564 {
18565 struct face *face, *default_face;
18566 struct frame *f = it->f;
18567
18568 /* If line is already filled, do nothing. Non window-system frames
18569 get a grace of one more ``pixel'' because their characters are
18570 1-``pixel'' wide, so they hit the equality too early. This grace
18571 is needed only for R2L rows that are not continued, to produce
18572 one extra blank where we could display the cursor. */
18573 if (it->current_x >= it->last_visible_x
18574 + (!FRAME_WINDOW_P (f)
18575 && it->glyph_row->reversed_p
18576 && !it->glyph_row->continued_p))
18577 return;
18578
18579 /* The default face, possibly remapped. */
18580 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18581
18582 /* Face extension extends the background and box of IT->face_id
18583 to the end of the line. If the background equals the background
18584 of the frame, we don't have to do anything. */
18585 if (it->face_before_selective_p)
18586 face = FACE_FROM_ID (f, it->saved_face_id);
18587 else
18588 face = FACE_FROM_ID (f, it->face_id);
18589
18590 if (FRAME_WINDOW_P (f)
18591 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
18592 && face->box == FACE_NO_BOX
18593 && face->background == FRAME_BACKGROUND_PIXEL (f)
18594 && !face->stipple
18595 && !it->glyph_row->reversed_p)
18596 return;
18597
18598 /* Set the glyph row flag indicating that the face of the last glyph
18599 in the text area has to be drawn to the end of the text area. */
18600 it->glyph_row->fill_line_p = 1;
18601
18602 /* If current character of IT is not ASCII, make sure we have the
18603 ASCII face. This will be automatically undone the next time
18604 get_next_display_element returns a multibyte character. Note
18605 that the character will always be single byte in unibyte
18606 text. */
18607 if (!ASCII_CHAR_P (it->c))
18608 {
18609 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18610 }
18611
18612 if (FRAME_WINDOW_P (f))
18613 {
18614 /* If the row is empty, add a space with the current face of IT,
18615 so that we know which face to draw. */
18616 if (it->glyph_row->used[TEXT_AREA] == 0)
18617 {
18618 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18619 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18620 it->glyph_row->used[TEXT_AREA] = 1;
18621 }
18622 #ifdef HAVE_WINDOW_SYSTEM
18623 if (it->glyph_row->reversed_p)
18624 {
18625 /* Prepend a stretch glyph to the row, such that the
18626 rightmost glyph will be drawn flushed all the way to the
18627 right margin of the window. The stretch glyph that will
18628 occupy the empty space, if any, to the left of the
18629 glyphs. */
18630 struct font *font = face->font ? face->font : FRAME_FONT (f);
18631 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18632 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18633 struct glyph *g;
18634 int row_width, stretch_ascent, stretch_width;
18635 struct text_pos saved_pos;
18636 int saved_face_id, saved_avoid_cursor, saved_box_start;
18637
18638 for (row_width = 0, g = row_start; g < row_end; g++)
18639 row_width += g->pixel_width;
18640 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18641 if (stretch_width > 0)
18642 {
18643 stretch_ascent =
18644 (((it->ascent + it->descent)
18645 * FONT_BASE (font)) / FONT_HEIGHT (font));
18646 saved_pos = it->position;
18647 memset (&it->position, 0, sizeof it->position);
18648 saved_avoid_cursor = it->avoid_cursor_p;
18649 it->avoid_cursor_p = 1;
18650 saved_face_id = it->face_id;
18651 saved_box_start = it->start_of_box_run_p;
18652 /* The last row's stretch glyph should get the default
18653 face, to avoid painting the rest of the window with
18654 the region face, if the region ends at ZV. */
18655 if (it->glyph_row->ends_at_zv_p)
18656 it->face_id = default_face->id;
18657 else
18658 it->face_id = face->id;
18659 it->start_of_box_run_p = 0;
18660 append_stretch_glyph (it, make_number (0), stretch_width,
18661 it->ascent + it->descent, stretch_ascent);
18662 it->position = saved_pos;
18663 it->avoid_cursor_p = saved_avoid_cursor;
18664 it->face_id = saved_face_id;
18665 it->start_of_box_run_p = saved_box_start;
18666 }
18667 }
18668 #endif /* HAVE_WINDOW_SYSTEM */
18669 }
18670 else
18671 {
18672 /* Save some values that must not be changed. */
18673 int saved_x = it->current_x;
18674 struct text_pos saved_pos;
18675 Lisp_Object saved_object;
18676 enum display_element_type saved_what = it->what;
18677 int saved_face_id = it->face_id;
18678
18679 saved_object = it->object;
18680 saved_pos = it->position;
18681
18682 it->what = IT_CHARACTER;
18683 memset (&it->position, 0, sizeof it->position);
18684 it->object = make_number (0);
18685 it->c = it->char_to_display = ' ';
18686 it->len = 1;
18687 /* The last row's blank glyphs should get the default face, to
18688 avoid painting the rest of the window with the region face,
18689 if the region ends at ZV. */
18690 if (it->glyph_row->ends_at_zv_p)
18691 it->face_id = default_face->id;
18692 else
18693 it->face_id = face->id;
18694
18695 PRODUCE_GLYPHS (it);
18696
18697 while (it->current_x <= it->last_visible_x)
18698 PRODUCE_GLYPHS (it);
18699
18700 /* Don't count these blanks really. It would let us insert a left
18701 truncation glyph below and make us set the cursor on them, maybe. */
18702 it->current_x = saved_x;
18703 it->object = saved_object;
18704 it->position = saved_pos;
18705 it->what = saved_what;
18706 it->face_id = saved_face_id;
18707 }
18708 }
18709
18710
18711 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18712 trailing whitespace. */
18713
18714 static int
18715 trailing_whitespace_p (ptrdiff_t charpos)
18716 {
18717 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18718 int c = 0;
18719
18720 while (bytepos < ZV_BYTE
18721 && (c = FETCH_CHAR (bytepos),
18722 c == ' ' || c == '\t'))
18723 ++bytepos;
18724
18725 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18726 {
18727 if (bytepos != PT_BYTE)
18728 return 1;
18729 }
18730 return 0;
18731 }
18732
18733
18734 /* Highlight trailing whitespace, if any, in ROW. */
18735
18736 static void
18737 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18738 {
18739 int used = row->used[TEXT_AREA];
18740
18741 if (used)
18742 {
18743 struct glyph *start = row->glyphs[TEXT_AREA];
18744 struct glyph *glyph = start + used - 1;
18745
18746 if (row->reversed_p)
18747 {
18748 /* Right-to-left rows need to be processed in the opposite
18749 direction, so swap the edge pointers. */
18750 glyph = start;
18751 start = row->glyphs[TEXT_AREA] + used - 1;
18752 }
18753
18754 /* Skip over glyphs inserted to display the cursor at the
18755 end of a line, for extending the face of the last glyph
18756 to the end of the line on terminals, and for truncation
18757 and continuation glyphs. */
18758 if (!row->reversed_p)
18759 {
18760 while (glyph >= start
18761 && glyph->type == CHAR_GLYPH
18762 && INTEGERP (glyph->object))
18763 --glyph;
18764 }
18765 else
18766 {
18767 while (glyph <= start
18768 && glyph->type == CHAR_GLYPH
18769 && INTEGERP (glyph->object))
18770 ++glyph;
18771 }
18772
18773 /* If last glyph is a space or stretch, and it's trailing
18774 whitespace, set the face of all trailing whitespace glyphs in
18775 IT->glyph_row to `trailing-whitespace'. */
18776 if ((row->reversed_p ? glyph <= start : glyph >= start)
18777 && BUFFERP (glyph->object)
18778 && (glyph->type == STRETCH_GLYPH
18779 || (glyph->type == CHAR_GLYPH
18780 && glyph->u.ch == ' '))
18781 && trailing_whitespace_p (glyph->charpos))
18782 {
18783 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18784 if (face_id < 0)
18785 return;
18786
18787 if (!row->reversed_p)
18788 {
18789 while (glyph >= start
18790 && BUFFERP (glyph->object)
18791 && (glyph->type == STRETCH_GLYPH
18792 || (glyph->type == CHAR_GLYPH
18793 && glyph->u.ch == ' ')))
18794 (glyph--)->face_id = face_id;
18795 }
18796 else
18797 {
18798 while (glyph <= start
18799 && BUFFERP (glyph->object)
18800 && (glyph->type == STRETCH_GLYPH
18801 || (glyph->type == CHAR_GLYPH
18802 && glyph->u.ch == ' ')))
18803 (glyph++)->face_id = face_id;
18804 }
18805 }
18806 }
18807 }
18808
18809
18810 /* Value is non-zero if glyph row ROW should be
18811 considered to hold the buffer position CHARPOS. */
18812
18813 static int
18814 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
18815 {
18816 int result = 1;
18817
18818 if (charpos == CHARPOS (row->end.pos)
18819 || charpos == MATRIX_ROW_END_CHARPOS (row))
18820 {
18821 /* Suppose the row ends on a string.
18822 Unless the row is continued, that means it ends on a newline
18823 in the string. If it's anything other than a display string
18824 (e.g., a before-string from an overlay), we don't want the
18825 cursor there. (This heuristic seems to give the optimal
18826 behavior for the various types of multi-line strings.)
18827 One exception: if the string has `cursor' property on one of
18828 its characters, we _do_ want the cursor there. */
18829 if (CHARPOS (row->end.string_pos) >= 0)
18830 {
18831 if (row->continued_p)
18832 result = 1;
18833 else
18834 {
18835 /* Check for `display' property. */
18836 struct glyph *beg = row->glyphs[TEXT_AREA];
18837 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18838 struct glyph *glyph;
18839
18840 result = 0;
18841 for (glyph = end; glyph >= beg; --glyph)
18842 if (STRINGP (glyph->object))
18843 {
18844 Lisp_Object prop
18845 = Fget_char_property (make_number (charpos),
18846 Qdisplay, Qnil);
18847 result =
18848 (!NILP (prop)
18849 && display_prop_string_p (prop, glyph->object));
18850 /* If there's a `cursor' property on one of the
18851 string's characters, this row is a cursor row,
18852 even though this is not a display string. */
18853 if (!result)
18854 {
18855 Lisp_Object s = glyph->object;
18856
18857 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18858 {
18859 ptrdiff_t gpos = glyph->charpos;
18860
18861 if (!NILP (Fget_char_property (make_number (gpos),
18862 Qcursor, s)))
18863 {
18864 result = 1;
18865 break;
18866 }
18867 }
18868 }
18869 break;
18870 }
18871 }
18872 }
18873 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18874 {
18875 /* If the row ends in middle of a real character,
18876 and the line is continued, we want the cursor here.
18877 That's because CHARPOS (ROW->end.pos) would equal
18878 PT if PT is before the character. */
18879 if (!row->ends_in_ellipsis_p)
18880 result = row->continued_p;
18881 else
18882 /* If the row ends in an ellipsis, then
18883 CHARPOS (ROW->end.pos) will equal point after the
18884 invisible text. We want that position to be displayed
18885 after the ellipsis. */
18886 result = 0;
18887 }
18888 /* If the row ends at ZV, display the cursor at the end of that
18889 row instead of at the start of the row below. */
18890 else if (row->ends_at_zv_p)
18891 result = 1;
18892 else
18893 result = 0;
18894 }
18895
18896 return result;
18897 }
18898
18899 /* Value is non-zero if glyph row ROW should be
18900 used to hold the cursor. */
18901
18902 static int
18903 cursor_row_p (struct glyph_row *row)
18904 {
18905 return row_for_charpos_p (row, PT);
18906 }
18907
18908 \f
18909
18910 /* Push the property PROP so that it will be rendered at the current
18911 position in IT. Return 1 if PROP was successfully pushed, 0
18912 otherwise. Called from handle_line_prefix to handle the
18913 `line-prefix' and `wrap-prefix' properties. */
18914
18915 static int
18916 push_prefix_prop (struct it *it, Lisp_Object prop)
18917 {
18918 struct text_pos pos =
18919 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18920
18921 eassert (it->method == GET_FROM_BUFFER
18922 || it->method == GET_FROM_DISPLAY_VECTOR
18923 || it->method == GET_FROM_STRING);
18924
18925 /* We need to save the current buffer/string position, so it will be
18926 restored by pop_it, because iterate_out_of_display_property
18927 depends on that being set correctly, but some situations leave
18928 it->position not yet set when this function is called. */
18929 push_it (it, &pos);
18930
18931 if (STRINGP (prop))
18932 {
18933 if (SCHARS (prop) == 0)
18934 {
18935 pop_it (it);
18936 return 0;
18937 }
18938
18939 it->string = prop;
18940 it->string_from_prefix_prop_p = 1;
18941 it->multibyte_p = STRING_MULTIBYTE (it->string);
18942 it->current.overlay_string_index = -1;
18943 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18944 it->end_charpos = it->string_nchars = SCHARS (it->string);
18945 it->method = GET_FROM_STRING;
18946 it->stop_charpos = 0;
18947 it->prev_stop = 0;
18948 it->base_level_stop = 0;
18949
18950 /* Force paragraph direction to be that of the parent
18951 buffer/string. */
18952 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18953 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18954 else
18955 it->paragraph_embedding = L2R;
18956
18957 /* Set up the bidi iterator for this display string. */
18958 if (it->bidi_p)
18959 {
18960 it->bidi_it.string.lstring = it->string;
18961 it->bidi_it.string.s = NULL;
18962 it->bidi_it.string.schars = it->end_charpos;
18963 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18964 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18965 it->bidi_it.string.unibyte = !it->multibyte_p;
18966 it->bidi_it.w = it->w;
18967 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18968 }
18969 }
18970 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18971 {
18972 it->method = GET_FROM_STRETCH;
18973 it->object = prop;
18974 }
18975 #ifdef HAVE_WINDOW_SYSTEM
18976 else if (IMAGEP (prop))
18977 {
18978 it->what = IT_IMAGE;
18979 it->image_id = lookup_image (it->f, prop);
18980 it->method = GET_FROM_IMAGE;
18981 }
18982 #endif /* HAVE_WINDOW_SYSTEM */
18983 else
18984 {
18985 pop_it (it); /* bogus display property, give up */
18986 return 0;
18987 }
18988
18989 return 1;
18990 }
18991
18992 /* Return the character-property PROP at the current position in IT. */
18993
18994 static Lisp_Object
18995 get_it_property (struct it *it, Lisp_Object prop)
18996 {
18997 Lisp_Object position, object = it->object;
18998
18999 if (STRINGP (object))
19000 position = make_number (IT_STRING_CHARPOS (*it));
19001 else if (BUFFERP (object))
19002 {
19003 position = make_number (IT_CHARPOS (*it));
19004 object = it->window;
19005 }
19006 else
19007 return Qnil;
19008
19009 return Fget_char_property (position, prop, object);
19010 }
19011
19012 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19013
19014 static void
19015 handle_line_prefix (struct it *it)
19016 {
19017 Lisp_Object prefix;
19018
19019 if (it->continuation_lines_width > 0)
19020 {
19021 prefix = get_it_property (it, Qwrap_prefix);
19022 if (NILP (prefix))
19023 prefix = Vwrap_prefix;
19024 }
19025 else
19026 {
19027 prefix = get_it_property (it, Qline_prefix);
19028 if (NILP (prefix))
19029 prefix = Vline_prefix;
19030 }
19031 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19032 {
19033 /* If the prefix is wider than the window, and we try to wrap
19034 it, it would acquire its own wrap prefix, and so on till the
19035 iterator stack overflows. So, don't wrap the prefix. */
19036 it->line_wrap = TRUNCATE;
19037 it->avoid_cursor_p = 1;
19038 }
19039 }
19040
19041 \f
19042
19043 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19044 only for R2L lines from display_line and display_string, when they
19045 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19046 the line/string needs to be continued on the next glyph row. */
19047 static void
19048 unproduce_glyphs (struct it *it, int n)
19049 {
19050 struct glyph *glyph, *end;
19051
19052 eassert (it->glyph_row);
19053 eassert (it->glyph_row->reversed_p);
19054 eassert (it->area == TEXT_AREA);
19055 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19056
19057 if (n > it->glyph_row->used[TEXT_AREA])
19058 n = it->glyph_row->used[TEXT_AREA];
19059 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19060 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19061 for ( ; glyph < end; glyph++)
19062 glyph[-n] = *glyph;
19063 }
19064
19065 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19066 and ROW->maxpos. */
19067 static void
19068 find_row_edges (struct it *it, struct glyph_row *row,
19069 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19070 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19071 {
19072 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19073 lines' rows is implemented for bidi-reordered rows. */
19074
19075 /* ROW->minpos is the value of min_pos, the minimal buffer position
19076 we have in ROW, or ROW->start.pos if that is smaller. */
19077 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19078 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19079 else
19080 /* We didn't find buffer positions smaller than ROW->start, or
19081 didn't find _any_ valid buffer positions in any of the glyphs,
19082 so we must trust the iterator's computed positions. */
19083 row->minpos = row->start.pos;
19084 if (max_pos <= 0)
19085 {
19086 max_pos = CHARPOS (it->current.pos);
19087 max_bpos = BYTEPOS (it->current.pos);
19088 }
19089
19090 /* Here are the various use-cases for ending the row, and the
19091 corresponding values for ROW->maxpos:
19092
19093 Line ends in a newline from buffer eol_pos + 1
19094 Line is continued from buffer max_pos + 1
19095 Line is truncated on right it->current.pos
19096 Line ends in a newline from string max_pos + 1(*)
19097 (*) + 1 only when line ends in a forward scan
19098 Line is continued from string max_pos
19099 Line is continued from display vector max_pos
19100 Line is entirely from a string min_pos == max_pos
19101 Line is entirely from a display vector min_pos == max_pos
19102 Line that ends at ZV ZV
19103
19104 If you discover other use-cases, please add them here as
19105 appropriate. */
19106 if (row->ends_at_zv_p)
19107 row->maxpos = it->current.pos;
19108 else if (row->used[TEXT_AREA])
19109 {
19110 int seen_this_string = 0;
19111 struct glyph_row *r1 = row - 1;
19112
19113 /* Did we see the same display string on the previous row? */
19114 if (STRINGP (it->object)
19115 /* this is not the first row */
19116 && row > it->w->desired_matrix->rows
19117 /* previous row is not the header line */
19118 && !r1->mode_line_p
19119 /* previous row also ends in a newline from a string */
19120 && r1->ends_in_newline_from_string_p)
19121 {
19122 struct glyph *start, *end;
19123
19124 /* Search for the last glyph of the previous row that came
19125 from buffer or string. Depending on whether the row is
19126 L2R or R2L, we need to process it front to back or the
19127 other way round. */
19128 if (!r1->reversed_p)
19129 {
19130 start = r1->glyphs[TEXT_AREA];
19131 end = start + r1->used[TEXT_AREA];
19132 /* Glyphs inserted by redisplay have an integer (zero)
19133 as their object. */
19134 while (end > start
19135 && INTEGERP ((end - 1)->object)
19136 && (end - 1)->charpos <= 0)
19137 --end;
19138 if (end > start)
19139 {
19140 if (EQ ((end - 1)->object, it->object))
19141 seen_this_string = 1;
19142 }
19143 else
19144 /* If all the glyphs of the previous row were inserted
19145 by redisplay, it means the previous row was
19146 produced from a single newline, which is only
19147 possible if that newline came from the same string
19148 as the one which produced this ROW. */
19149 seen_this_string = 1;
19150 }
19151 else
19152 {
19153 end = r1->glyphs[TEXT_AREA] - 1;
19154 start = end + r1->used[TEXT_AREA];
19155 while (end < start
19156 && INTEGERP ((end + 1)->object)
19157 && (end + 1)->charpos <= 0)
19158 ++end;
19159 if (end < start)
19160 {
19161 if (EQ ((end + 1)->object, it->object))
19162 seen_this_string = 1;
19163 }
19164 else
19165 seen_this_string = 1;
19166 }
19167 }
19168 /* Take note of each display string that covers a newline only
19169 once, the first time we see it. This is for when a display
19170 string includes more than one newline in it. */
19171 if (row->ends_in_newline_from_string_p && !seen_this_string)
19172 {
19173 /* If we were scanning the buffer forward when we displayed
19174 the string, we want to account for at least one buffer
19175 position that belongs to this row (position covered by
19176 the display string), so that cursor positioning will
19177 consider this row as a candidate when point is at the end
19178 of the visual line represented by this row. This is not
19179 required when scanning back, because max_pos will already
19180 have a much larger value. */
19181 if (CHARPOS (row->end.pos) > max_pos)
19182 INC_BOTH (max_pos, max_bpos);
19183 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19184 }
19185 else if (CHARPOS (it->eol_pos) > 0)
19186 SET_TEXT_POS (row->maxpos,
19187 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19188 else if (row->continued_p)
19189 {
19190 /* If max_pos is different from IT's current position, it
19191 means IT->method does not belong to the display element
19192 at max_pos. However, it also means that the display
19193 element at max_pos was displayed in its entirety on this
19194 line, which is equivalent to saying that the next line
19195 starts at the next buffer position. */
19196 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19197 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19198 else
19199 {
19200 INC_BOTH (max_pos, max_bpos);
19201 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19202 }
19203 }
19204 else if (row->truncated_on_right_p)
19205 /* display_line already called reseat_at_next_visible_line_start,
19206 which puts the iterator at the beginning of the next line, in
19207 the logical order. */
19208 row->maxpos = it->current.pos;
19209 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19210 /* A line that is entirely from a string/image/stretch... */
19211 row->maxpos = row->minpos;
19212 else
19213 emacs_abort ();
19214 }
19215 else
19216 row->maxpos = it->current.pos;
19217 }
19218
19219 /* Construct the glyph row IT->glyph_row in the desired matrix of
19220 IT->w from text at the current position of IT. See dispextern.h
19221 for an overview of struct it. Value is non-zero if
19222 IT->glyph_row displays text, as opposed to a line displaying ZV
19223 only. */
19224
19225 static int
19226 display_line (struct it *it)
19227 {
19228 struct glyph_row *row = it->glyph_row;
19229 Lisp_Object overlay_arrow_string;
19230 struct it wrap_it;
19231 void *wrap_data = NULL;
19232 int may_wrap = 0, wrap_x IF_LINT (= 0);
19233 int wrap_row_used = -1;
19234 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19235 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19236 int wrap_row_extra_line_spacing IF_LINT (= 0);
19237 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19238 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19239 int cvpos;
19240 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19241 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19242
19243 /* We always start displaying at hpos zero even if hscrolled. */
19244 eassert (it->hpos == 0 && it->current_x == 0);
19245
19246 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19247 >= it->w->desired_matrix->nrows)
19248 {
19249 it->w->nrows_scale_factor++;
19250 fonts_changed_p = 1;
19251 return 0;
19252 }
19253
19254 /* Is IT->w showing the region? */
19255 it->w->region_showing = it->region_beg_charpos > 0 ? it->region_beg_charpos : 0;
19256
19257 /* Clear the result glyph row and enable it. */
19258 prepare_desired_row (row);
19259
19260 row->y = it->current_y;
19261 row->start = it->start;
19262 row->continuation_lines_width = it->continuation_lines_width;
19263 row->displays_text_p = 1;
19264 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19265 it->starts_in_middle_of_char_p = 0;
19266
19267 /* Arrange the overlays nicely for our purposes. Usually, we call
19268 display_line on only one line at a time, in which case this
19269 can't really hurt too much, or we call it on lines which appear
19270 one after another in the buffer, in which case all calls to
19271 recenter_overlay_lists but the first will be pretty cheap. */
19272 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19273
19274 /* Move over display elements that are not visible because we are
19275 hscrolled. This may stop at an x-position < IT->first_visible_x
19276 if the first glyph is partially visible or if we hit a line end. */
19277 if (it->current_x < it->first_visible_x)
19278 {
19279 enum move_it_result move_result;
19280
19281 this_line_min_pos = row->start.pos;
19282 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19283 MOVE_TO_POS | MOVE_TO_X);
19284 /* If we are under a large hscroll, move_it_in_display_line_to
19285 could hit the end of the line without reaching
19286 it->first_visible_x. Pretend that we did reach it. This is
19287 especially important on a TTY, where we will call
19288 extend_face_to_end_of_line, which needs to know how many
19289 blank glyphs to produce. */
19290 if (it->current_x < it->first_visible_x
19291 && (move_result == MOVE_NEWLINE_OR_CR
19292 || move_result == MOVE_POS_MATCH_OR_ZV))
19293 it->current_x = it->first_visible_x;
19294
19295 /* Record the smallest positions seen while we moved over
19296 display elements that are not visible. This is needed by
19297 redisplay_internal for optimizing the case where the cursor
19298 stays inside the same line. The rest of this function only
19299 considers positions that are actually displayed, so
19300 RECORD_MAX_MIN_POS will not otherwise record positions that
19301 are hscrolled to the left of the left edge of the window. */
19302 min_pos = CHARPOS (this_line_min_pos);
19303 min_bpos = BYTEPOS (this_line_min_pos);
19304 }
19305 else
19306 {
19307 /* We only do this when not calling `move_it_in_display_line_to'
19308 above, because move_it_in_display_line_to calls
19309 handle_line_prefix itself. */
19310 handle_line_prefix (it);
19311 }
19312
19313 /* Get the initial row height. This is either the height of the
19314 text hscrolled, if there is any, or zero. */
19315 row->ascent = it->max_ascent;
19316 row->height = it->max_ascent + it->max_descent;
19317 row->phys_ascent = it->max_phys_ascent;
19318 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19319 row->extra_line_spacing = it->max_extra_line_spacing;
19320
19321 /* Utility macro to record max and min buffer positions seen until now. */
19322 #define RECORD_MAX_MIN_POS(IT) \
19323 do \
19324 { \
19325 int composition_p = !STRINGP ((IT)->string) \
19326 && ((IT)->what == IT_COMPOSITION); \
19327 ptrdiff_t current_pos = \
19328 composition_p ? (IT)->cmp_it.charpos \
19329 : IT_CHARPOS (*(IT)); \
19330 ptrdiff_t current_bpos = \
19331 composition_p ? CHAR_TO_BYTE (current_pos) \
19332 : IT_BYTEPOS (*(IT)); \
19333 if (current_pos < min_pos) \
19334 { \
19335 min_pos = current_pos; \
19336 min_bpos = current_bpos; \
19337 } \
19338 if (IT_CHARPOS (*it) > max_pos) \
19339 { \
19340 max_pos = IT_CHARPOS (*it); \
19341 max_bpos = IT_BYTEPOS (*it); \
19342 } \
19343 } \
19344 while (0)
19345
19346 /* Loop generating characters. The loop is left with IT on the next
19347 character to display. */
19348 while (1)
19349 {
19350 int n_glyphs_before, hpos_before, x_before;
19351 int x, nglyphs;
19352 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19353
19354 /* Retrieve the next thing to display. Value is zero if end of
19355 buffer reached. */
19356 if (!get_next_display_element (it))
19357 {
19358 /* Maybe add a space at the end of this line that is used to
19359 display the cursor there under X. Set the charpos of the
19360 first glyph of blank lines not corresponding to any text
19361 to -1. */
19362 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19363 row->exact_window_width_line_p = 1;
19364 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19365 || row->used[TEXT_AREA] == 0)
19366 {
19367 row->glyphs[TEXT_AREA]->charpos = -1;
19368 row->displays_text_p = 0;
19369
19370 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
19371 && (!MINI_WINDOW_P (it->w)
19372 || (minibuf_level && EQ (it->window, minibuf_window))))
19373 row->indicate_empty_line_p = 1;
19374 }
19375
19376 it->continuation_lines_width = 0;
19377 row->ends_at_zv_p = 1;
19378 /* A row that displays right-to-left text must always have
19379 its last face extended all the way to the end of line,
19380 even if this row ends in ZV, because we still write to
19381 the screen left to right. We also need to extend the
19382 last face if the default face is remapped to some
19383 different face, otherwise the functions that clear
19384 portions of the screen will clear with the default face's
19385 background color. */
19386 if (row->reversed_p
19387 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19388 extend_face_to_end_of_line (it);
19389 break;
19390 }
19391
19392 /* Now, get the metrics of what we want to display. This also
19393 generates glyphs in `row' (which is IT->glyph_row). */
19394 n_glyphs_before = row->used[TEXT_AREA];
19395 x = it->current_x;
19396
19397 /* Remember the line height so far in case the next element doesn't
19398 fit on the line. */
19399 if (it->line_wrap != TRUNCATE)
19400 {
19401 ascent = it->max_ascent;
19402 descent = it->max_descent;
19403 phys_ascent = it->max_phys_ascent;
19404 phys_descent = it->max_phys_descent;
19405
19406 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19407 {
19408 if (IT_DISPLAYING_WHITESPACE (it))
19409 may_wrap = 1;
19410 else if (may_wrap)
19411 {
19412 SAVE_IT (wrap_it, *it, wrap_data);
19413 wrap_x = x;
19414 wrap_row_used = row->used[TEXT_AREA];
19415 wrap_row_ascent = row->ascent;
19416 wrap_row_height = row->height;
19417 wrap_row_phys_ascent = row->phys_ascent;
19418 wrap_row_phys_height = row->phys_height;
19419 wrap_row_extra_line_spacing = row->extra_line_spacing;
19420 wrap_row_min_pos = min_pos;
19421 wrap_row_min_bpos = min_bpos;
19422 wrap_row_max_pos = max_pos;
19423 wrap_row_max_bpos = max_bpos;
19424 may_wrap = 0;
19425 }
19426 }
19427 }
19428
19429 PRODUCE_GLYPHS (it);
19430
19431 /* If this display element was in marginal areas, continue with
19432 the next one. */
19433 if (it->area != TEXT_AREA)
19434 {
19435 row->ascent = max (row->ascent, it->max_ascent);
19436 row->height = max (row->height, it->max_ascent + it->max_descent);
19437 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19438 row->phys_height = max (row->phys_height,
19439 it->max_phys_ascent + it->max_phys_descent);
19440 row->extra_line_spacing = max (row->extra_line_spacing,
19441 it->max_extra_line_spacing);
19442 set_iterator_to_next (it, 1);
19443 continue;
19444 }
19445
19446 /* Does the display element fit on the line? If we truncate
19447 lines, we should draw past the right edge of the window. If
19448 we don't truncate, we want to stop so that we can display the
19449 continuation glyph before the right margin. If lines are
19450 continued, there are two possible strategies for characters
19451 resulting in more than 1 glyph (e.g. tabs): Display as many
19452 glyphs as possible in this line and leave the rest for the
19453 continuation line, or display the whole element in the next
19454 line. Original redisplay did the former, so we do it also. */
19455 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19456 hpos_before = it->hpos;
19457 x_before = x;
19458
19459 if (/* Not a newline. */
19460 nglyphs > 0
19461 /* Glyphs produced fit entirely in the line. */
19462 && it->current_x < it->last_visible_x)
19463 {
19464 it->hpos += nglyphs;
19465 row->ascent = max (row->ascent, it->max_ascent);
19466 row->height = max (row->height, it->max_ascent + it->max_descent);
19467 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19468 row->phys_height = max (row->phys_height,
19469 it->max_phys_ascent + it->max_phys_descent);
19470 row->extra_line_spacing = max (row->extra_line_spacing,
19471 it->max_extra_line_spacing);
19472 if (it->current_x - it->pixel_width < it->first_visible_x)
19473 row->x = x - it->first_visible_x;
19474 /* Record the maximum and minimum buffer positions seen so
19475 far in glyphs that will be displayed by this row. */
19476 if (it->bidi_p)
19477 RECORD_MAX_MIN_POS (it);
19478 }
19479 else
19480 {
19481 int i, new_x;
19482 struct glyph *glyph;
19483
19484 for (i = 0; i < nglyphs; ++i, x = new_x)
19485 {
19486 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19487 new_x = x + glyph->pixel_width;
19488
19489 if (/* Lines are continued. */
19490 it->line_wrap != TRUNCATE
19491 && (/* Glyph doesn't fit on the line. */
19492 new_x > it->last_visible_x
19493 /* Or it fits exactly on a window system frame. */
19494 || (new_x == it->last_visible_x
19495 && FRAME_WINDOW_P (it->f)
19496 && (row->reversed_p
19497 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19498 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19499 {
19500 /* End of a continued line. */
19501
19502 if (it->hpos == 0
19503 || (new_x == it->last_visible_x
19504 && FRAME_WINDOW_P (it->f)
19505 && (row->reversed_p
19506 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19507 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19508 {
19509 /* Current glyph is the only one on the line or
19510 fits exactly on the line. We must continue
19511 the line because we can't draw the cursor
19512 after the glyph. */
19513 row->continued_p = 1;
19514 it->current_x = new_x;
19515 it->continuation_lines_width += new_x;
19516 ++it->hpos;
19517 if (i == nglyphs - 1)
19518 {
19519 /* If line-wrap is on, check if a previous
19520 wrap point was found. */
19521 if (wrap_row_used > 0
19522 /* Even if there is a previous wrap
19523 point, continue the line here as
19524 usual, if (i) the previous character
19525 was a space or tab AND (ii) the
19526 current character is not. */
19527 && (!may_wrap
19528 || IT_DISPLAYING_WHITESPACE (it)))
19529 goto back_to_wrap;
19530
19531 /* Record the maximum and minimum buffer
19532 positions seen so far in glyphs that will be
19533 displayed by this row. */
19534 if (it->bidi_p)
19535 RECORD_MAX_MIN_POS (it);
19536 set_iterator_to_next (it, 1);
19537 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19538 {
19539 if (!get_next_display_element (it))
19540 {
19541 row->exact_window_width_line_p = 1;
19542 it->continuation_lines_width = 0;
19543 row->continued_p = 0;
19544 row->ends_at_zv_p = 1;
19545 }
19546 else if (ITERATOR_AT_END_OF_LINE_P (it))
19547 {
19548 row->continued_p = 0;
19549 row->exact_window_width_line_p = 1;
19550 }
19551 }
19552 }
19553 else if (it->bidi_p)
19554 RECORD_MAX_MIN_POS (it);
19555 }
19556 else if (CHAR_GLYPH_PADDING_P (*glyph)
19557 && !FRAME_WINDOW_P (it->f))
19558 {
19559 /* A padding glyph that doesn't fit on this line.
19560 This means the whole character doesn't fit
19561 on the line. */
19562 if (row->reversed_p)
19563 unproduce_glyphs (it, row->used[TEXT_AREA]
19564 - n_glyphs_before);
19565 row->used[TEXT_AREA] = n_glyphs_before;
19566
19567 /* Fill the rest of the row with continuation
19568 glyphs like in 20.x. */
19569 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19570 < row->glyphs[1 + TEXT_AREA])
19571 produce_special_glyphs (it, IT_CONTINUATION);
19572
19573 row->continued_p = 1;
19574 it->current_x = x_before;
19575 it->continuation_lines_width += x_before;
19576
19577 /* Restore the height to what it was before the
19578 element not fitting on the line. */
19579 it->max_ascent = ascent;
19580 it->max_descent = descent;
19581 it->max_phys_ascent = phys_ascent;
19582 it->max_phys_descent = phys_descent;
19583 }
19584 else if (wrap_row_used > 0)
19585 {
19586 back_to_wrap:
19587 if (row->reversed_p)
19588 unproduce_glyphs (it,
19589 row->used[TEXT_AREA] - wrap_row_used);
19590 RESTORE_IT (it, &wrap_it, wrap_data);
19591 it->continuation_lines_width += wrap_x;
19592 row->used[TEXT_AREA] = wrap_row_used;
19593 row->ascent = wrap_row_ascent;
19594 row->height = wrap_row_height;
19595 row->phys_ascent = wrap_row_phys_ascent;
19596 row->phys_height = wrap_row_phys_height;
19597 row->extra_line_spacing = wrap_row_extra_line_spacing;
19598 min_pos = wrap_row_min_pos;
19599 min_bpos = wrap_row_min_bpos;
19600 max_pos = wrap_row_max_pos;
19601 max_bpos = wrap_row_max_bpos;
19602 row->continued_p = 1;
19603 row->ends_at_zv_p = 0;
19604 row->exact_window_width_line_p = 0;
19605 it->continuation_lines_width += x;
19606
19607 /* Make sure that a non-default face is extended
19608 up to the right margin of the window. */
19609 extend_face_to_end_of_line (it);
19610 }
19611 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19612 {
19613 /* A TAB that extends past the right edge of the
19614 window. This produces a single glyph on
19615 window system frames. We leave the glyph in
19616 this row and let it fill the row, but don't
19617 consume the TAB. */
19618 if ((row->reversed_p
19619 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19620 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19621 produce_special_glyphs (it, IT_CONTINUATION);
19622 it->continuation_lines_width += it->last_visible_x;
19623 row->ends_in_middle_of_char_p = 1;
19624 row->continued_p = 1;
19625 glyph->pixel_width = it->last_visible_x - x;
19626 it->starts_in_middle_of_char_p = 1;
19627 }
19628 else
19629 {
19630 /* Something other than a TAB that draws past
19631 the right edge of the window. Restore
19632 positions to values before the element. */
19633 if (row->reversed_p)
19634 unproduce_glyphs (it, row->used[TEXT_AREA]
19635 - (n_glyphs_before + i));
19636 row->used[TEXT_AREA] = n_glyphs_before + i;
19637
19638 /* Display continuation glyphs. */
19639 it->current_x = x_before;
19640 it->continuation_lines_width += x;
19641 if (!FRAME_WINDOW_P (it->f)
19642 || (row->reversed_p
19643 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19644 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19645 produce_special_glyphs (it, IT_CONTINUATION);
19646 row->continued_p = 1;
19647
19648 extend_face_to_end_of_line (it);
19649
19650 if (nglyphs > 1 && i > 0)
19651 {
19652 row->ends_in_middle_of_char_p = 1;
19653 it->starts_in_middle_of_char_p = 1;
19654 }
19655
19656 /* Restore the height to what it was before the
19657 element not fitting on the line. */
19658 it->max_ascent = ascent;
19659 it->max_descent = descent;
19660 it->max_phys_ascent = phys_ascent;
19661 it->max_phys_descent = phys_descent;
19662 }
19663
19664 break;
19665 }
19666 else if (new_x > it->first_visible_x)
19667 {
19668 /* Increment number of glyphs actually displayed. */
19669 ++it->hpos;
19670
19671 /* Record the maximum and minimum buffer positions
19672 seen so far in glyphs that will be displayed by
19673 this row. */
19674 if (it->bidi_p)
19675 RECORD_MAX_MIN_POS (it);
19676
19677 if (x < it->first_visible_x)
19678 /* Glyph is partially visible, i.e. row starts at
19679 negative X position. */
19680 row->x = x - it->first_visible_x;
19681 }
19682 else
19683 {
19684 /* Glyph is completely off the left margin of the
19685 window. This should not happen because of the
19686 move_it_in_display_line at the start of this
19687 function, unless the text display area of the
19688 window is empty. */
19689 eassert (it->first_visible_x <= it->last_visible_x);
19690 }
19691 }
19692 /* Even if this display element produced no glyphs at all,
19693 we want to record its position. */
19694 if (it->bidi_p && nglyphs == 0)
19695 RECORD_MAX_MIN_POS (it);
19696
19697 row->ascent = max (row->ascent, it->max_ascent);
19698 row->height = max (row->height, it->max_ascent + it->max_descent);
19699 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19700 row->phys_height = max (row->phys_height,
19701 it->max_phys_ascent + it->max_phys_descent);
19702 row->extra_line_spacing = max (row->extra_line_spacing,
19703 it->max_extra_line_spacing);
19704
19705 /* End of this display line if row is continued. */
19706 if (row->continued_p || row->ends_at_zv_p)
19707 break;
19708 }
19709
19710 at_end_of_line:
19711 /* Is this a line end? If yes, we're also done, after making
19712 sure that a non-default face is extended up to the right
19713 margin of the window. */
19714 if (ITERATOR_AT_END_OF_LINE_P (it))
19715 {
19716 int used_before = row->used[TEXT_AREA];
19717
19718 row->ends_in_newline_from_string_p = STRINGP (it->object);
19719
19720 /* Add a space at the end of the line that is used to
19721 display the cursor there. */
19722 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19723 append_space_for_newline (it, 0);
19724
19725 /* Extend the face to the end of the line. */
19726 extend_face_to_end_of_line (it);
19727
19728 /* Make sure we have the position. */
19729 if (used_before == 0)
19730 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19731
19732 /* Record the position of the newline, for use in
19733 find_row_edges. */
19734 it->eol_pos = it->current.pos;
19735
19736 /* Consume the line end. This skips over invisible lines. */
19737 set_iterator_to_next (it, 1);
19738 it->continuation_lines_width = 0;
19739 break;
19740 }
19741
19742 /* Proceed with next display element. Note that this skips
19743 over lines invisible because of selective display. */
19744 set_iterator_to_next (it, 1);
19745
19746 /* If we truncate lines, we are done when the last displayed
19747 glyphs reach past the right margin of the window. */
19748 if (it->line_wrap == TRUNCATE
19749 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19750 ? (it->current_x >= it->last_visible_x)
19751 : (it->current_x > it->last_visible_x)))
19752 {
19753 /* Maybe add truncation glyphs. */
19754 if (!FRAME_WINDOW_P (it->f)
19755 || (row->reversed_p
19756 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19757 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19758 {
19759 int i, n;
19760
19761 if (!row->reversed_p)
19762 {
19763 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19764 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19765 break;
19766 }
19767 else
19768 {
19769 for (i = 0; i < row->used[TEXT_AREA]; i++)
19770 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19771 break;
19772 /* Remove any padding glyphs at the front of ROW, to
19773 make room for the truncation glyphs we will be
19774 adding below. The loop below always inserts at
19775 least one truncation glyph, so also remove the
19776 last glyph added to ROW. */
19777 unproduce_glyphs (it, i + 1);
19778 /* Adjust i for the loop below. */
19779 i = row->used[TEXT_AREA] - (i + 1);
19780 }
19781
19782 it->current_x = x_before;
19783 if (!FRAME_WINDOW_P (it->f))
19784 {
19785 for (n = row->used[TEXT_AREA]; i < n; ++i)
19786 {
19787 row->used[TEXT_AREA] = i;
19788 produce_special_glyphs (it, IT_TRUNCATION);
19789 }
19790 }
19791 else
19792 {
19793 row->used[TEXT_AREA] = i;
19794 produce_special_glyphs (it, IT_TRUNCATION);
19795 }
19796 }
19797 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19798 {
19799 /* Don't truncate if we can overflow newline into fringe. */
19800 if (!get_next_display_element (it))
19801 {
19802 it->continuation_lines_width = 0;
19803 row->ends_at_zv_p = 1;
19804 row->exact_window_width_line_p = 1;
19805 break;
19806 }
19807 if (ITERATOR_AT_END_OF_LINE_P (it))
19808 {
19809 row->exact_window_width_line_p = 1;
19810 goto at_end_of_line;
19811 }
19812 it->current_x = x_before;
19813 }
19814
19815 row->truncated_on_right_p = 1;
19816 it->continuation_lines_width = 0;
19817 reseat_at_next_visible_line_start (it, 0);
19818 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19819 it->hpos = hpos_before;
19820 break;
19821 }
19822 }
19823
19824 if (wrap_data)
19825 bidi_unshelve_cache (wrap_data, 1);
19826
19827 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19828 at the left window margin. */
19829 if (it->first_visible_x
19830 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19831 {
19832 if (!FRAME_WINDOW_P (it->f)
19833 || (row->reversed_p
19834 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19835 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19836 insert_left_trunc_glyphs (it);
19837 row->truncated_on_left_p = 1;
19838 }
19839
19840 /* Remember the position at which this line ends.
19841
19842 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19843 cannot be before the call to find_row_edges below, since that is
19844 where these positions are determined. */
19845 row->end = it->current;
19846 if (!it->bidi_p)
19847 {
19848 row->minpos = row->start.pos;
19849 row->maxpos = row->end.pos;
19850 }
19851 else
19852 {
19853 /* ROW->minpos and ROW->maxpos must be the smallest and
19854 `1 + the largest' buffer positions in ROW. But if ROW was
19855 bidi-reordered, these two positions can be anywhere in the
19856 row, so we must determine them now. */
19857 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19858 }
19859
19860 /* If the start of this line is the overlay arrow-position, then
19861 mark this glyph row as the one containing the overlay arrow.
19862 This is clearly a mess with variable size fonts. It would be
19863 better to let it be displayed like cursors under X. */
19864 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
19865 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19866 !NILP (overlay_arrow_string)))
19867 {
19868 /* Overlay arrow in window redisplay is a fringe bitmap. */
19869 if (STRINGP (overlay_arrow_string))
19870 {
19871 struct glyph_row *arrow_row
19872 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19873 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19874 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19875 struct glyph *p = row->glyphs[TEXT_AREA];
19876 struct glyph *p2, *end;
19877
19878 /* Copy the arrow glyphs. */
19879 while (glyph < arrow_end)
19880 *p++ = *glyph++;
19881
19882 /* Throw away padding glyphs. */
19883 p2 = p;
19884 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19885 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19886 ++p2;
19887 if (p2 > p)
19888 {
19889 while (p2 < end)
19890 *p++ = *p2++;
19891 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19892 }
19893 }
19894 else
19895 {
19896 eassert (INTEGERP (overlay_arrow_string));
19897 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19898 }
19899 overlay_arrow_seen = 1;
19900 }
19901
19902 /* Highlight trailing whitespace. */
19903 if (!NILP (Vshow_trailing_whitespace))
19904 highlight_trailing_whitespace (it->f, it->glyph_row);
19905
19906 /* Compute pixel dimensions of this line. */
19907 compute_line_metrics (it);
19908
19909 /* Implementation note: No changes in the glyphs of ROW or in their
19910 faces can be done past this point, because compute_line_metrics
19911 computes ROW's hash value and stores it within the glyph_row
19912 structure. */
19913
19914 /* Record whether this row ends inside an ellipsis. */
19915 row->ends_in_ellipsis_p
19916 = (it->method == GET_FROM_DISPLAY_VECTOR
19917 && it->ellipsis_p);
19918
19919 /* Save fringe bitmaps in this row. */
19920 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19921 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19922 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19923 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19924
19925 it->left_user_fringe_bitmap = 0;
19926 it->left_user_fringe_face_id = 0;
19927 it->right_user_fringe_bitmap = 0;
19928 it->right_user_fringe_face_id = 0;
19929
19930 /* Maybe set the cursor. */
19931 cvpos = it->w->cursor.vpos;
19932 if ((cvpos < 0
19933 /* In bidi-reordered rows, keep checking for proper cursor
19934 position even if one has been found already, because buffer
19935 positions in such rows change non-linearly with ROW->VPOS,
19936 when a line is continued. One exception: when we are at ZV,
19937 display cursor on the first suitable glyph row, since all
19938 the empty rows after that also have their position set to ZV. */
19939 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19940 lines' rows is implemented for bidi-reordered rows. */
19941 || (it->bidi_p
19942 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19943 && PT >= MATRIX_ROW_START_CHARPOS (row)
19944 && PT <= MATRIX_ROW_END_CHARPOS (row)
19945 && cursor_row_p (row))
19946 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19947
19948 /* Prepare for the next line. This line starts horizontally at (X
19949 HPOS) = (0 0). Vertical positions are incremented. As a
19950 convenience for the caller, IT->glyph_row is set to the next
19951 row to be used. */
19952 it->current_x = it->hpos = 0;
19953 it->current_y += row->height;
19954 SET_TEXT_POS (it->eol_pos, 0, 0);
19955 ++it->vpos;
19956 ++it->glyph_row;
19957 /* The next row should by default use the same value of the
19958 reversed_p flag as this one. set_iterator_to_next decides when
19959 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19960 the flag accordingly. */
19961 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19962 it->glyph_row->reversed_p = row->reversed_p;
19963 it->start = row->end;
19964 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
19965
19966 #undef RECORD_MAX_MIN_POS
19967 }
19968
19969 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19970 Scurrent_bidi_paragraph_direction, 0, 1, 0,
19971 doc: /* Return paragraph direction at point in BUFFER.
19972 Value is either `left-to-right' or `right-to-left'.
19973 If BUFFER is omitted or nil, it defaults to the current buffer.
19974
19975 Paragraph direction determines how the text in the paragraph is displayed.
19976 In left-to-right paragraphs, text begins at the left margin of the window
19977 and the reading direction is generally left to right. In right-to-left
19978 paragraphs, text begins at the right margin and is read from right to left.
19979
19980 See also `bidi-paragraph-direction'. */)
19981 (Lisp_Object buffer)
19982 {
19983 struct buffer *buf = current_buffer;
19984 struct buffer *old = buf;
19985
19986 if (! NILP (buffer))
19987 {
19988 CHECK_BUFFER (buffer);
19989 buf = XBUFFER (buffer);
19990 }
19991
19992 if (NILP (BVAR (buf, bidi_display_reordering))
19993 || NILP (BVAR (buf, enable_multibyte_characters))
19994 /* When we are loading loadup.el, the character property tables
19995 needed for bidi iteration are not yet available. */
19996 || !NILP (Vpurify_flag))
19997 return Qleft_to_right;
19998 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
19999 return BVAR (buf, bidi_paragraph_direction);
20000 else
20001 {
20002 /* Determine the direction from buffer text. We could try to
20003 use current_matrix if it is up to date, but this seems fast
20004 enough as it is. */
20005 struct bidi_it itb;
20006 ptrdiff_t pos = BUF_PT (buf);
20007 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20008 int c;
20009 void *itb_data = bidi_shelve_cache ();
20010
20011 set_buffer_temp (buf);
20012 /* bidi_paragraph_init finds the base direction of the paragraph
20013 by searching forward from paragraph start. We need the base
20014 direction of the current or _previous_ paragraph, so we need
20015 to make sure we are within that paragraph. To that end, find
20016 the previous non-empty line. */
20017 if (pos >= ZV && pos > BEGV)
20018 DEC_BOTH (pos, bytepos);
20019 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20020 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20021 {
20022 while ((c = FETCH_BYTE (bytepos)) == '\n'
20023 || c == ' ' || c == '\t' || c == '\f')
20024 {
20025 if (bytepos <= BEGV_BYTE)
20026 break;
20027 bytepos--;
20028 pos--;
20029 }
20030 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20031 bytepos--;
20032 }
20033 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20034 itb.paragraph_dir = NEUTRAL_DIR;
20035 itb.string.s = NULL;
20036 itb.string.lstring = Qnil;
20037 itb.string.bufpos = 0;
20038 itb.string.unibyte = 0;
20039 /* We have no window to use here for ignoring window-specific
20040 overlays. Using NULL for window pointer will cause
20041 compute_display_string_pos to use the current buffer. */
20042 itb.w = NULL;
20043 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20044 bidi_unshelve_cache (itb_data, 0);
20045 set_buffer_temp (old);
20046 switch (itb.paragraph_dir)
20047 {
20048 case L2R:
20049 return Qleft_to_right;
20050 break;
20051 case R2L:
20052 return Qright_to_left;
20053 break;
20054 default:
20055 emacs_abort ();
20056 }
20057 }
20058 }
20059
20060 DEFUN ("move-point-visually", Fmove_point_visually,
20061 Smove_point_visually, 1, 1, 0,
20062 doc: /* Move point in the visual order in the specified DIRECTION.
20063 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
20064 left.
20065
20066 Value is the new character position of point. */)
20067 (Lisp_Object direction)
20068 {
20069 struct window *w = XWINDOW (selected_window);
20070 struct buffer *b = XBUFFER (w->contents);
20071 struct glyph_row *row;
20072 int dir;
20073 Lisp_Object paragraph_dir;
20074
20075 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
20076 (!(ROW)->continued_p \
20077 && INTEGERP ((GLYPH)->object) \
20078 && (GLYPH)->type == CHAR_GLYPH \
20079 && (GLYPH)->u.ch == ' ' \
20080 && (GLYPH)->charpos >= 0 \
20081 && !(GLYPH)->avoid_cursor_p)
20082
20083 CHECK_NUMBER (direction);
20084 dir = XINT (direction);
20085 if (dir > 0)
20086 dir = 1;
20087 else
20088 dir = -1;
20089
20090 /* If current matrix is up-to-date, we can use the information
20091 recorded in the glyphs, at least as long as the goal is on the
20092 screen. */
20093 if (w->window_end_valid
20094 && !windows_or_buffers_changed
20095 && b
20096 && !b->clip_changed
20097 && !b->prevent_redisplay_optimizations_p
20098 && !window_outdated (w)
20099 && w->cursor.vpos >= 0
20100 && w->cursor.vpos < w->current_matrix->nrows
20101 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
20102 {
20103 struct glyph *g = row->glyphs[TEXT_AREA];
20104 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
20105 struct glyph *gpt = g + w->cursor.hpos;
20106
20107 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
20108 {
20109 if (BUFFERP (g->object) && g->charpos != PT)
20110 {
20111 SET_PT (g->charpos);
20112 w->cursor.vpos = -1;
20113 return make_number (PT);
20114 }
20115 else if (!INTEGERP (g->object) && !EQ (g->object, gpt->object))
20116 {
20117 ptrdiff_t new_pos;
20118
20119 if (BUFFERP (gpt->object))
20120 {
20121 new_pos = PT;
20122 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
20123 new_pos += (row->reversed_p ? -dir : dir);
20124 else
20125 new_pos -= (row->reversed_p ? -dir : dir);;
20126 }
20127 else if (BUFFERP (g->object))
20128 new_pos = g->charpos;
20129 else
20130 break;
20131 SET_PT (new_pos);
20132 w->cursor.vpos = -1;
20133 return make_number (PT);
20134 }
20135 else if (ROW_GLYPH_NEWLINE_P (row, g))
20136 {
20137 /* Glyphs inserted at the end of a non-empty line for
20138 positioning the cursor have zero charpos, so we must
20139 deduce the value of point by other means. */
20140 if (g->charpos > 0)
20141 SET_PT (g->charpos);
20142 else if (row->ends_at_zv_p && PT != ZV)
20143 SET_PT (ZV);
20144 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
20145 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20146 else
20147 break;
20148 w->cursor.vpos = -1;
20149 return make_number (PT);
20150 }
20151 }
20152 if (g == e || INTEGERP (g->object))
20153 {
20154 if (row->truncated_on_left_p || row->truncated_on_right_p)
20155 goto simulate_display;
20156 if (!row->reversed_p)
20157 row += dir;
20158 else
20159 row -= dir;
20160 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
20161 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
20162 goto simulate_display;
20163
20164 if (dir > 0)
20165 {
20166 if (row->reversed_p && !row->continued_p)
20167 {
20168 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20169 w->cursor.vpos = -1;
20170 return make_number (PT);
20171 }
20172 g = row->glyphs[TEXT_AREA];
20173 e = g + row->used[TEXT_AREA];
20174 for ( ; g < e; g++)
20175 {
20176 if (BUFFERP (g->object)
20177 /* Empty lines have only one glyph, which stands
20178 for the newline, and whose charpos is the
20179 buffer position of the newline. */
20180 || ROW_GLYPH_NEWLINE_P (row, g)
20181 /* When the buffer ends in a newline, the line at
20182 EOB also has one glyph, but its charpos is -1. */
20183 || (row->ends_at_zv_p
20184 && !row->reversed_p
20185 && INTEGERP (g->object)
20186 && g->type == CHAR_GLYPH
20187 && g->u.ch == ' '))
20188 {
20189 if (g->charpos > 0)
20190 SET_PT (g->charpos);
20191 else if (!row->reversed_p
20192 && row->ends_at_zv_p
20193 && PT != ZV)
20194 SET_PT (ZV);
20195 else
20196 continue;
20197 w->cursor.vpos = -1;
20198 return make_number (PT);
20199 }
20200 }
20201 }
20202 else
20203 {
20204 if (!row->reversed_p && !row->continued_p)
20205 {
20206 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20207 w->cursor.vpos = -1;
20208 return make_number (PT);
20209 }
20210 e = row->glyphs[TEXT_AREA];
20211 g = e + row->used[TEXT_AREA] - 1;
20212 for ( ; g >= e; g--)
20213 {
20214 if (BUFFERP (g->object)
20215 || (ROW_GLYPH_NEWLINE_P (row, g)
20216 && g->charpos > 0)
20217 /* Empty R2L lines on GUI frames have the buffer
20218 position of the newline stored in the stretch
20219 glyph. */
20220 || g->type == STRETCH_GLYPH
20221 || (row->ends_at_zv_p
20222 && row->reversed_p
20223 && INTEGERP (g->object)
20224 && g->type == CHAR_GLYPH
20225 && g->u.ch == ' '))
20226 {
20227 if (g->charpos > 0)
20228 SET_PT (g->charpos);
20229 else if (row->reversed_p
20230 && row->ends_at_zv_p
20231 && PT != ZV)
20232 SET_PT (ZV);
20233 else
20234 continue;
20235 w->cursor.vpos = -1;
20236 return make_number (PT);
20237 }
20238 }
20239 }
20240 }
20241 }
20242
20243 simulate_display:
20244
20245 /* If we wind up here, we failed to move by using the glyphs, so we
20246 need to simulate display instead. */
20247
20248 if (b)
20249 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
20250 else
20251 paragraph_dir = Qleft_to_right;
20252 if (EQ (paragraph_dir, Qright_to_left))
20253 dir = -dir;
20254 if (PT <= BEGV && dir < 0)
20255 xsignal0 (Qbeginning_of_buffer);
20256 else if (PT >= ZV && dir > 0)
20257 xsignal0 (Qend_of_buffer);
20258 else
20259 {
20260 struct text_pos pt;
20261 struct it it;
20262 int pt_x, target_x, pixel_width, pt_vpos;
20263 bool at_eol_p;
20264 bool overshoot_expected = false;
20265 bool target_is_eol_p = false;
20266
20267 /* Setup the arena. */
20268 SET_TEXT_POS (pt, PT, PT_BYTE);
20269 start_display (&it, w, pt);
20270
20271 if (it.cmp_it.id < 0
20272 && it.method == GET_FROM_STRING
20273 && it.area == TEXT_AREA
20274 && it.string_from_display_prop_p
20275 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
20276 overshoot_expected = true;
20277
20278 /* Find the X coordinate of point. We start from the beginning
20279 of this or previous line to make sure we are before point in
20280 the logical order (since the move_it_* functions can only
20281 move forward). */
20282 reseat_at_previous_visible_line_start (&it);
20283 it.current_x = it.hpos = it.current_y = it.vpos = 0;
20284 if (IT_CHARPOS (it) != PT)
20285 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
20286 -1, -1, -1, MOVE_TO_POS);
20287 pt_x = it.current_x;
20288 pt_vpos = it.vpos;
20289 if (dir > 0 || overshoot_expected)
20290 {
20291 struct glyph_row *row = it.glyph_row;
20292
20293 /* When point is at beginning of line, we don't have
20294 information about the glyph there loaded into struct
20295 it. Calling get_next_display_element fixes that. */
20296 if (pt_x == 0)
20297 get_next_display_element (&it);
20298 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20299 it.glyph_row = NULL;
20300 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
20301 it.glyph_row = row;
20302 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
20303 it, lest it will become out of sync with it's buffer
20304 position. */
20305 it.current_x = pt_x;
20306 }
20307 else
20308 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20309 pixel_width = it.pixel_width;
20310 if (overshoot_expected && at_eol_p)
20311 pixel_width = 0;
20312 else if (pixel_width <= 0)
20313 pixel_width = 1;
20314
20315 /* If there's a display string at point, we are actually at the
20316 glyph to the left of point, so we need to correct the X
20317 coordinate. */
20318 if (overshoot_expected)
20319 pt_x += pixel_width;
20320
20321 /* Compute target X coordinate, either to the left or to the
20322 right of point. On TTY frames, all characters have the same
20323 pixel width of 1, so we can use that. On GUI frames we don't
20324 have an easy way of getting at the pixel width of the
20325 character to the left of point, so we use a different method
20326 of getting to that place. */
20327 if (dir > 0)
20328 target_x = pt_x + pixel_width;
20329 else
20330 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
20331
20332 /* Target X coordinate could be one line above or below the line
20333 of point, in which case we need to adjust the target X
20334 coordinate. Also, if moving to the left, we need to begin at
20335 the left edge of the point's screen line. */
20336 if (dir < 0)
20337 {
20338 if (pt_x > 0)
20339 {
20340 start_display (&it, w, pt);
20341 reseat_at_previous_visible_line_start (&it);
20342 it.current_x = it.current_y = it.hpos = 0;
20343 if (pt_vpos != 0)
20344 move_it_by_lines (&it, pt_vpos);
20345 }
20346 else
20347 {
20348 move_it_by_lines (&it, -1);
20349 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
20350 target_is_eol_p = true;
20351 }
20352 }
20353 else
20354 {
20355 if (at_eol_p
20356 || (target_x >= it.last_visible_x
20357 && it.line_wrap != TRUNCATE))
20358 {
20359 if (pt_x > 0)
20360 move_it_by_lines (&it, 0);
20361 move_it_by_lines (&it, 1);
20362 target_x = 0;
20363 }
20364 }
20365
20366 /* Move to the target X coordinate. */
20367 #ifdef HAVE_WINDOW_SYSTEM
20368 /* On GUI frames, as we don't know the X coordinate of the
20369 character to the left of point, moving point to the left
20370 requires walking, one grapheme cluster at a time, until we
20371 find ourself at a place immediately to the left of the
20372 character at point. */
20373 if (FRAME_WINDOW_P (it.f) && dir < 0)
20374 {
20375 struct text_pos new_pos = it.current.pos;
20376 enum move_it_result rc = MOVE_X_REACHED;
20377
20378 while (it.current_x + it.pixel_width <= target_x
20379 && rc == MOVE_X_REACHED)
20380 {
20381 int new_x = it.current_x + it.pixel_width;
20382
20383 new_pos = it.current.pos;
20384 if (new_x == it.current_x)
20385 new_x++;
20386 rc = move_it_in_display_line_to (&it, ZV, new_x,
20387 MOVE_TO_POS | MOVE_TO_X);
20388 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
20389 break;
20390 }
20391 /* If we ended up on a composed character inside
20392 bidi-reordered text (e.g., Hebrew text with diacritics),
20393 the iterator gives us the buffer position of the last (in
20394 logical order) character of the composed grapheme cluster,
20395 which is not what we want. So we cheat: we compute the
20396 character position of the character that follows (in the
20397 logical order) the one where the above loop stopped. That
20398 character will appear on display to the left of point. */
20399 if (it.bidi_p
20400 && it.bidi_it.scan_dir == -1
20401 && new_pos.charpos - IT_CHARPOS (it) > 1)
20402 {
20403 new_pos.charpos = IT_CHARPOS (it) + 1;
20404 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
20405 }
20406 it.current.pos = new_pos;
20407 }
20408 else
20409 #endif
20410 if (it.current_x != target_x)
20411 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
20412
20413 /* When lines are truncated, the above loop will stop at the
20414 window edge. But we want to get to the end of line, even if
20415 it is beyond the window edge; automatic hscroll will then
20416 scroll the window to show point as appropriate. */
20417 if (target_is_eol_p && it.line_wrap == TRUNCATE
20418 && get_next_display_element (&it))
20419 {
20420 struct text_pos new_pos = it.current.pos;
20421
20422 while (!ITERATOR_AT_END_OF_LINE_P (&it))
20423 {
20424 set_iterator_to_next (&it, 0);
20425 if (it.method == GET_FROM_BUFFER)
20426 new_pos = it.current.pos;
20427 if (!get_next_display_element (&it))
20428 break;
20429 }
20430
20431 it.current.pos = new_pos;
20432 }
20433
20434 /* If we ended up in a display string that covers point, move to
20435 buffer position to the right in the visual order. */
20436 if (dir > 0)
20437 {
20438 while (IT_CHARPOS (it) == PT)
20439 {
20440 set_iterator_to_next (&it, 0);
20441 if (!get_next_display_element (&it))
20442 break;
20443 }
20444 }
20445
20446 /* Move point to that position. */
20447 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
20448 }
20449
20450 return make_number (PT);
20451
20452 #undef ROW_GLYPH_NEWLINE_P
20453 }
20454
20455 \f
20456 /***********************************************************************
20457 Menu Bar
20458 ***********************************************************************/
20459
20460 /* Redisplay the menu bar in the frame for window W.
20461
20462 The menu bar of X frames that don't have X toolkit support is
20463 displayed in a special window W->frame->menu_bar_window.
20464
20465 The menu bar of terminal frames is treated specially as far as
20466 glyph matrices are concerned. Menu bar lines are not part of
20467 windows, so the update is done directly on the frame matrix rows
20468 for the menu bar. */
20469
20470 static void
20471 display_menu_bar (struct window *w)
20472 {
20473 struct frame *f = XFRAME (WINDOW_FRAME (w));
20474 struct it it;
20475 Lisp_Object items;
20476 int i;
20477
20478 /* Don't do all this for graphical frames. */
20479 #ifdef HAVE_NTGUI
20480 if (FRAME_W32_P (f))
20481 return;
20482 #endif
20483 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20484 if (FRAME_X_P (f))
20485 return;
20486 #endif
20487
20488 #ifdef HAVE_NS
20489 if (FRAME_NS_P (f))
20490 return;
20491 #endif /* HAVE_NS */
20492
20493 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20494 eassert (!FRAME_WINDOW_P (f));
20495 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20496 it.first_visible_x = 0;
20497 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20498 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
20499 if (FRAME_WINDOW_P (f))
20500 {
20501 /* Menu bar lines are displayed in the desired matrix of the
20502 dummy window menu_bar_window. */
20503 struct window *menu_w;
20504 menu_w = XWINDOW (f->menu_bar_window);
20505 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20506 MENU_FACE_ID);
20507 it.first_visible_x = 0;
20508 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20509 }
20510 else
20511 #endif /* not USE_X_TOOLKIT and not USE_GTK */
20512 {
20513 /* This is a TTY frame, i.e. character hpos/vpos are used as
20514 pixel x/y. */
20515 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20516 MENU_FACE_ID);
20517 it.first_visible_x = 0;
20518 it.last_visible_x = FRAME_COLS (f);
20519 }
20520
20521 /* FIXME: This should be controlled by a user option. See the
20522 comments in redisplay_tool_bar and display_mode_line about
20523 this. */
20524 it.paragraph_embedding = L2R;
20525
20526 /* Clear all rows of the menu bar. */
20527 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20528 {
20529 struct glyph_row *row = it.glyph_row + i;
20530 clear_glyph_row (row);
20531 row->enabled_p = 1;
20532 row->full_width_p = 1;
20533 }
20534
20535 /* Display all items of the menu bar. */
20536 items = FRAME_MENU_BAR_ITEMS (it.f);
20537 for (i = 0; i < ASIZE (items); i += 4)
20538 {
20539 Lisp_Object string;
20540
20541 /* Stop at nil string. */
20542 string = AREF (items, i + 1);
20543 if (NILP (string))
20544 break;
20545
20546 /* Remember where item was displayed. */
20547 ASET (items, i + 3, make_number (it.hpos));
20548
20549 /* Display the item, pad with one space. */
20550 if (it.current_x < it.last_visible_x)
20551 display_string (NULL, string, Qnil, 0, 0, &it,
20552 SCHARS (string) + 1, 0, 0, -1);
20553 }
20554
20555 /* Fill out the line with spaces. */
20556 if (it.current_x < it.last_visible_x)
20557 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20558
20559 /* Compute the total height of the lines. */
20560 compute_line_metrics (&it);
20561 }
20562
20563
20564 \f
20565 /***********************************************************************
20566 Mode Line
20567 ***********************************************************************/
20568
20569 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20570 FORCE is non-zero, redisplay mode lines unconditionally.
20571 Otherwise, redisplay only mode lines that are garbaged. Value is
20572 the number of windows whose mode lines were redisplayed. */
20573
20574 static int
20575 redisplay_mode_lines (Lisp_Object window, int force)
20576 {
20577 int nwindows = 0;
20578
20579 while (!NILP (window))
20580 {
20581 struct window *w = XWINDOW (window);
20582
20583 if (WINDOWP (w->contents))
20584 nwindows += redisplay_mode_lines (w->contents, force);
20585 else if (force
20586 || FRAME_GARBAGED_P (XFRAME (w->frame))
20587 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20588 {
20589 struct text_pos lpoint;
20590 struct buffer *old = current_buffer;
20591
20592 /* Set the window's buffer for the mode line display. */
20593 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20594 set_buffer_internal_1 (XBUFFER (w->contents));
20595
20596 /* Point refers normally to the selected window. For any
20597 other window, set up appropriate value. */
20598 if (!EQ (window, selected_window))
20599 {
20600 struct text_pos pt;
20601
20602 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
20603 if (CHARPOS (pt) < BEGV)
20604 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
20605 else if (CHARPOS (pt) > (ZV - 1))
20606 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
20607 else
20608 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20609 }
20610
20611 /* Display mode lines. */
20612 clear_glyph_matrix (w->desired_matrix);
20613 if (display_mode_lines (w))
20614 {
20615 ++nwindows;
20616 w->must_be_updated_p = 1;
20617 }
20618
20619 /* Restore old settings. */
20620 set_buffer_internal_1 (old);
20621 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20622 }
20623
20624 window = w->next;
20625 }
20626
20627 return nwindows;
20628 }
20629
20630
20631 /* Display the mode and/or header line of window W. Value is the
20632 sum number of mode lines and header lines displayed. */
20633
20634 static int
20635 display_mode_lines (struct window *w)
20636 {
20637 Lisp_Object old_selected_window = selected_window;
20638 Lisp_Object old_selected_frame = selected_frame;
20639 Lisp_Object new_frame = w->frame;
20640 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
20641 int n = 0;
20642
20643 selected_frame = new_frame;
20644 /* FIXME: If we were to allow the mode-line's computation changing the buffer
20645 or window's point, then we'd need select_window_1 here as well. */
20646 XSETWINDOW (selected_window, w);
20647 XFRAME (new_frame)->selected_window = selected_window;
20648
20649 /* These will be set while the mode line specs are processed. */
20650 line_number_displayed = 0;
20651 w->column_number_displayed = -1;
20652
20653 if (WINDOW_WANTS_MODELINE_P (w))
20654 {
20655 struct window *sel_w = XWINDOW (old_selected_window);
20656
20657 /* Select mode line face based on the real selected window. */
20658 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20659 BVAR (current_buffer, mode_line_format));
20660 ++n;
20661 }
20662
20663 if (WINDOW_WANTS_HEADER_LINE_P (w))
20664 {
20665 display_mode_line (w, HEADER_LINE_FACE_ID,
20666 BVAR (current_buffer, header_line_format));
20667 ++n;
20668 }
20669
20670 XFRAME (new_frame)->selected_window = old_frame_selected_window;
20671 selected_frame = old_selected_frame;
20672 selected_window = old_selected_window;
20673 return n;
20674 }
20675
20676
20677 /* Display mode or header line of window W. FACE_ID specifies which
20678 line to display; it is either MODE_LINE_FACE_ID or
20679 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20680 display. Value is the pixel height of the mode/header line
20681 displayed. */
20682
20683 static int
20684 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20685 {
20686 struct it it;
20687 struct face *face;
20688 ptrdiff_t count = SPECPDL_INDEX ();
20689
20690 init_iterator (&it, w, -1, -1, NULL, face_id);
20691 /* Don't extend on a previously drawn mode-line.
20692 This may happen if called from pos_visible_p. */
20693 it.glyph_row->enabled_p = 0;
20694 prepare_desired_row (it.glyph_row);
20695
20696 it.glyph_row->mode_line_p = 1;
20697
20698 /* FIXME: This should be controlled by a user option. But
20699 supporting such an option is not trivial, since the mode line is
20700 made up of many separate strings. */
20701 it.paragraph_embedding = L2R;
20702
20703 record_unwind_protect (unwind_format_mode_line,
20704 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20705
20706 mode_line_target = MODE_LINE_DISPLAY;
20707
20708 /* Temporarily make frame's keyboard the current kboard so that
20709 kboard-local variables in the mode_line_format will get the right
20710 values. */
20711 push_kboard (FRAME_KBOARD (it.f));
20712 record_unwind_save_match_data ();
20713 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20714 pop_kboard ();
20715
20716 unbind_to (count, Qnil);
20717
20718 /* Fill up with spaces. */
20719 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20720
20721 compute_line_metrics (&it);
20722 it.glyph_row->full_width_p = 1;
20723 it.glyph_row->continued_p = 0;
20724 it.glyph_row->truncated_on_left_p = 0;
20725 it.glyph_row->truncated_on_right_p = 0;
20726
20727 /* Make a 3D mode-line have a shadow at its right end. */
20728 face = FACE_FROM_ID (it.f, face_id);
20729 extend_face_to_end_of_line (&it);
20730 if (face->box != FACE_NO_BOX)
20731 {
20732 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20733 + it.glyph_row->used[TEXT_AREA] - 1);
20734 last->right_box_line_p = 1;
20735 }
20736
20737 return it.glyph_row->height;
20738 }
20739
20740 /* Move element ELT in LIST to the front of LIST.
20741 Return the updated list. */
20742
20743 static Lisp_Object
20744 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20745 {
20746 register Lisp_Object tail, prev;
20747 register Lisp_Object tem;
20748
20749 tail = list;
20750 prev = Qnil;
20751 while (CONSP (tail))
20752 {
20753 tem = XCAR (tail);
20754
20755 if (EQ (elt, tem))
20756 {
20757 /* Splice out the link TAIL. */
20758 if (NILP (prev))
20759 list = XCDR (tail);
20760 else
20761 Fsetcdr (prev, XCDR (tail));
20762
20763 /* Now make it the first. */
20764 Fsetcdr (tail, list);
20765 return tail;
20766 }
20767 else
20768 prev = tail;
20769 tail = XCDR (tail);
20770 QUIT;
20771 }
20772
20773 /* Not found--return unchanged LIST. */
20774 return list;
20775 }
20776
20777 /* Contribute ELT to the mode line for window IT->w. How it
20778 translates into text depends on its data type.
20779
20780 IT describes the display environment in which we display, as usual.
20781
20782 DEPTH is the depth in recursion. It is used to prevent
20783 infinite recursion here.
20784
20785 FIELD_WIDTH is the number of characters the display of ELT should
20786 occupy in the mode line, and PRECISION is the maximum number of
20787 characters to display from ELT's representation. See
20788 display_string for details.
20789
20790 Returns the hpos of the end of the text generated by ELT.
20791
20792 PROPS is a property list to add to any string we encounter.
20793
20794 If RISKY is nonzero, remove (disregard) any properties in any string
20795 we encounter, and ignore :eval and :propertize.
20796
20797 The global variable `mode_line_target' determines whether the
20798 output is passed to `store_mode_line_noprop',
20799 `store_mode_line_string', or `display_string'. */
20800
20801 static int
20802 display_mode_element (struct it *it, int depth, int field_width, int precision,
20803 Lisp_Object elt, Lisp_Object props, int risky)
20804 {
20805 int n = 0, field, prec;
20806 int literal = 0;
20807
20808 tail_recurse:
20809 if (depth > 100)
20810 elt = build_string ("*too-deep*");
20811
20812 depth++;
20813
20814 switch (XTYPE (elt))
20815 {
20816 case Lisp_String:
20817 {
20818 /* A string: output it and check for %-constructs within it. */
20819 unsigned char c;
20820 ptrdiff_t offset = 0;
20821
20822 if (SCHARS (elt) > 0
20823 && (!NILP (props) || risky))
20824 {
20825 Lisp_Object oprops, aelt;
20826 oprops = Ftext_properties_at (make_number (0), elt);
20827
20828 /* If the starting string's properties are not what
20829 we want, translate the string. Also, if the string
20830 is risky, do that anyway. */
20831
20832 if (NILP (Fequal (props, oprops)) || risky)
20833 {
20834 /* If the starting string has properties,
20835 merge the specified ones onto the existing ones. */
20836 if (! NILP (oprops) && !risky)
20837 {
20838 Lisp_Object tem;
20839
20840 oprops = Fcopy_sequence (oprops);
20841 tem = props;
20842 while (CONSP (tem))
20843 {
20844 oprops = Fplist_put (oprops, XCAR (tem),
20845 XCAR (XCDR (tem)));
20846 tem = XCDR (XCDR (tem));
20847 }
20848 props = oprops;
20849 }
20850
20851 aelt = Fassoc (elt, mode_line_proptrans_alist);
20852 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20853 {
20854 /* AELT is what we want. Move it to the front
20855 without consing. */
20856 elt = XCAR (aelt);
20857 mode_line_proptrans_alist
20858 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20859 }
20860 else
20861 {
20862 Lisp_Object tem;
20863
20864 /* If AELT has the wrong props, it is useless.
20865 so get rid of it. */
20866 if (! NILP (aelt))
20867 mode_line_proptrans_alist
20868 = Fdelq (aelt, mode_line_proptrans_alist);
20869
20870 elt = Fcopy_sequence (elt);
20871 Fset_text_properties (make_number (0), Flength (elt),
20872 props, elt);
20873 /* Add this item to mode_line_proptrans_alist. */
20874 mode_line_proptrans_alist
20875 = Fcons (Fcons (elt, props),
20876 mode_line_proptrans_alist);
20877 /* Truncate mode_line_proptrans_alist
20878 to at most 50 elements. */
20879 tem = Fnthcdr (make_number (50),
20880 mode_line_proptrans_alist);
20881 if (! NILP (tem))
20882 XSETCDR (tem, Qnil);
20883 }
20884 }
20885 }
20886
20887 offset = 0;
20888
20889 if (literal)
20890 {
20891 prec = precision - n;
20892 switch (mode_line_target)
20893 {
20894 case MODE_LINE_NOPROP:
20895 case MODE_LINE_TITLE:
20896 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20897 break;
20898 case MODE_LINE_STRING:
20899 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20900 break;
20901 case MODE_LINE_DISPLAY:
20902 n += display_string (NULL, elt, Qnil, 0, 0, it,
20903 0, prec, 0, STRING_MULTIBYTE (elt));
20904 break;
20905 }
20906
20907 break;
20908 }
20909
20910 /* Handle the non-literal case. */
20911
20912 while ((precision <= 0 || n < precision)
20913 && SREF (elt, offset) != 0
20914 && (mode_line_target != MODE_LINE_DISPLAY
20915 || it->current_x < it->last_visible_x))
20916 {
20917 ptrdiff_t last_offset = offset;
20918
20919 /* Advance to end of string or next format specifier. */
20920 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20921 ;
20922
20923 if (offset - 1 != last_offset)
20924 {
20925 ptrdiff_t nchars, nbytes;
20926
20927 /* Output to end of string or up to '%'. Field width
20928 is length of string. Don't output more than
20929 PRECISION allows us. */
20930 offset--;
20931
20932 prec = c_string_width (SDATA (elt) + last_offset,
20933 offset - last_offset, precision - n,
20934 &nchars, &nbytes);
20935
20936 switch (mode_line_target)
20937 {
20938 case MODE_LINE_NOPROP:
20939 case MODE_LINE_TITLE:
20940 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20941 break;
20942 case MODE_LINE_STRING:
20943 {
20944 ptrdiff_t bytepos = last_offset;
20945 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20946 ptrdiff_t endpos = (precision <= 0
20947 ? string_byte_to_char (elt, offset)
20948 : charpos + nchars);
20949
20950 n += store_mode_line_string (NULL,
20951 Fsubstring (elt, make_number (charpos),
20952 make_number (endpos)),
20953 0, 0, 0, Qnil);
20954 }
20955 break;
20956 case MODE_LINE_DISPLAY:
20957 {
20958 ptrdiff_t bytepos = last_offset;
20959 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20960
20961 if (precision <= 0)
20962 nchars = string_byte_to_char (elt, offset) - charpos;
20963 n += display_string (NULL, elt, Qnil, 0, charpos,
20964 it, 0, nchars, 0,
20965 STRING_MULTIBYTE (elt));
20966 }
20967 break;
20968 }
20969 }
20970 else /* c == '%' */
20971 {
20972 ptrdiff_t percent_position = offset;
20973
20974 /* Get the specified minimum width. Zero means
20975 don't pad. */
20976 field = 0;
20977 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20978 field = field * 10 + c - '0';
20979
20980 /* Don't pad beyond the total padding allowed. */
20981 if (field_width - n > 0 && field > field_width - n)
20982 field = field_width - n;
20983
20984 /* Note that either PRECISION <= 0 or N < PRECISION. */
20985 prec = precision - n;
20986
20987 if (c == 'M')
20988 n += display_mode_element (it, depth, field, prec,
20989 Vglobal_mode_string, props,
20990 risky);
20991 else if (c != 0)
20992 {
20993 bool multibyte;
20994 ptrdiff_t bytepos, charpos;
20995 const char *spec;
20996 Lisp_Object string;
20997
20998 bytepos = percent_position;
20999 charpos = (STRING_MULTIBYTE (elt)
21000 ? string_byte_to_char (elt, bytepos)
21001 : bytepos);
21002 spec = decode_mode_spec (it->w, c, field, &string);
21003 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
21004
21005 switch (mode_line_target)
21006 {
21007 case MODE_LINE_NOPROP:
21008 case MODE_LINE_TITLE:
21009 n += store_mode_line_noprop (spec, field, prec);
21010 break;
21011 case MODE_LINE_STRING:
21012 {
21013 Lisp_Object tem = build_string (spec);
21014 props = Ftext_properties_at (make_number (charpos), elt);
21015 /* Should only keep face property in props */
21016 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
21017 }
21018 break;
21019 case MODE_LINE_DISPLAY:
21020 {
21021 int nglyphs_before, nwritten;
21022
21023 nglyphs_before = it->glyph_row->used[TEXT_AREA];
21024 nwritten = display_string (spec, string, elt,
21025 charpos, 0, it,
21026 field, prec, 0,
21027 multibyte);
21028
21029 /* Assign to the glyphs written above the
21030 string where the `%x' came from, position
21031 of the `%'. */
21032 if (nwritten > 0)
21033 {
21034 struct glyph *glyph
21035 = (it->glyph_row->glyphs[TEXT_AREA]
21036 + nglyphs_before);
21037 int i;
21038
21039 for (i = 0; i < nwritten; ++i)
21040 {
21041 glyph[i].object = elt;
21042 glyph[i].charpos = charpos;
21043 }
21044
21045 n += nwritten;
21046 }
21047 }
21048 break;
21049 }
21050 }
21051 else /* c == 0 */
21052 break;
21053 }
21054 }
21055 }
21056 break;
21057
21058 case Lisp_Symbol:
21059 /* A symbol: process the value of the symbol recursively
21060 as if it appeared here directly. Avoid error if symbol void.
21061 Special case: if value of symbol is a string, output the string
21062 literally. */
21063 {
21064 register Lisp_Object tem;
21065
21066 /* If the variable is not marked as risky to set
21067 then its contents are risky to use. */
21068 if (NILP (Fget (elt, Qrisky_local_variable)))
21069 risky = 1;
21070
21071 tem = Fboundp (elt);
21072 if (!NILP (tem))
21073 {
21074 tem = Fsymbol_value (elt);
21075 /* If value is a string, output that string literally:
21076 don't check for % within it. */
21077 if (STRINGP (tem))
21078 literal = 1;
21079
21080 if (!EQ (tem, elt))
21081 {
21082 /* Give up right away for nil or t. */
21083 elt = tem;
21084 goto tail_recurse;
21085 }
21086 }
21087 }
21088 break;
21089
21090 case Lisp_Cons:
21091 {
21092 register Lisp_Object car, tem;
21093
21094 /* A cons cell: five distinct cases.
21095 If first element is :eval or :propertize, do something special.
21096 If first element is a string or a cons, process all the elements
21097 and effectively concatenate them.
21098 If first element is a negative number, truncate displaying cdr to
21099 at most that many characters. If positive, pad (with spaces)
21100 to at least that many characters.
21101 If first element is a symbol, process the cadr or caddr recursively
21102 according to whether the symbol's value is non-nil or nil. */
21103 car = XCAR (elt);
21104 if (EQ (car, QCeval))
21105 {
21106 /* An element of the form (:eval FORM) means evaluate FORM
21107 and use the result as mode line elements. */
21108
21109 if (risky)
21110 break;
21111
21112 if (CONSP (XCDR (elt)))
21113 {
21114 Lisp_Object spec;
21115 spec = safe_eval (XCAR (XCDR (elt)));
21116 n += display_mode_element (it, depth, field_width - n,
21117 precision - n, spec, props,
21118 risky);
21119 }
21120 }
21121 else if (EQ (car, QCpropertize))
21122 {
21123 /* An element of the form (:propertize ELT PROPS...)
21124 means display ELT but applying properties PROPS. */
21125
21126 if (risky)
21127 break;
21128
21129 if (CONSP (XCDR (elt)))
21130 n += display_mode_element (it, depth, field_width - n,
21131 precision - n, XCAR (XCDR (elt)),
21132 XCDR (XCDR (elt)), risky);
21133 }
21134 else if (SYMBOLP (car))
21135 {
21136 tem = Fboundp (car);
21137 elt = XCDR (elt);
21138 if (!CONSP (elt))
21139 goto invalid;
21140 /* elt is now the cdr, and we know it is a cons cell.
21141 Use its car if CAR has a non-nil value. */
21142 if (!NILP (tem))
21143 {
21144 tem = Fsymbol_value (car);
21145 if (!NILP (tem))
21146 {
21147 elt = XCAR (elt);
21148 goto tail_recurse;
21149 }
21150 }
21151 /* Symbol's value is nil (or symbol is unbound)
21152 Get the cddr of the original list
21153 and if possible find the caddr and use that. */
21154 elt = XCDR (elt);
21155 if (NILP (elt))
21156 break;
21157 else if (!CONSP (elt))
21158 goto invalid;
21159 elt = XCAR (elt);
21160 goto tail_recurse;
21161 }
21162 else if (INTEGERP (car))
21163 {
21164 register int lim = XINT (car);
21165 elt = XCDR (elt);
21166 if (lim < 0)
21167 {
21168 /* Negative int means reduce maximum width. */
21169 if (precision <= 0)
21170 precision = -lim;
21171 else
21172 precision = min (precision, -lim);
21173 }
21174 else if (lim > 0)
21175 {
21176 /* Padding specified. Don't let it be more than
21177 current maximum. */
21178 if (precision > 0)
21179 lim = min (precision, lim);
21180
21181 /* If that's more padding than already wanted, queue it.
21182 But don't reduce padding already specified even if
21183 that is beyond the current truncation point. */
21184 field_width = max (lim, field_width);
21185 }
21186 goto tail_recurse;
21187 }
21188 else if (STRINGP (car) || CONSP (car))
21189 {
21190 Lisp_Object halftail = elt;
21191 int len = 0;
21192
21193 while (CONSP (elt)
21194 && (precision <= 0 || n < precision))
21195 {
21196 n += display_mode_element (it, depth,
21197 /* Do padding only after the last
21198 element in the list. */
21199 (! CONSP (XCDR (elt))
21200 ? field_width - n
21201 : 0),
21202 precision - n, XCAR (elt),
21203 props, risky);
21204 elt = XCDR (elt);
21205 len++;
21206 if ((len & 1) == 0)
21207 halftail = XCDR (halftail);
21208 /* Check for cycle. */
21209 if (EQ (halftail, elt))
21210 break;
21211 }
21212 }
21213 }
21214 break;
21215
21216 default:
21217 invalid:
21218 elt = build_string ("*invalid*");
21219 goto tail_recurse;
21220 }
21221
21222 /* Pad to FIELD_WIDTH. */
21223 if (field_width > 0 && n < field_width)
21224 {
21225 switch (mode_line_target)
21226 {
21227 case MODE_LINE_NOPROP:
21228 case MODE_LINE_TITLE:
21229 n += store_mode_line_noprop ("", field_width - n, 0);
21230 break;
21231 case MODE_LINE_STRING:
21232 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
21233 break;
21234 case MODE_LINE_DISPLAY:
21235 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
21236 0, 0, 0);
21237 break;
21238 }
21239 }
21240
21241 return n;
21242 }
21243
21244 /* Store a mode-line string element in mode_line_string_list.
21245
21246 If STRING is non-null, display that C string. Otherwise, the Lisp
21247 string LISP_STRING is displayed.
21248
21249 FIELD_WIDTH is the minimum number of output glyphs to produce.
21250 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21251 with spaces. FIELD_WIDTH <= 0 means don't pad.
21252
21253 PRECISION is the maximum number of characters to output from
21254 STRING. PRECISION <= 0 means don't truncate the string.
21255
21256 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
21257 properties to the string.
21258
21259 PROPS are the properties to add to the string.
21260 The mode_line_string_face face property is always added to the string.
21261 */
21262
21263 static int
21264 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
21265 int field_width, int precision, Lisp_Object props)
21266 {
21267 ptrdiff_t len;
21268 int n = 0;
21269
21270 if (string != NULL)
21271 {
21272 len = strlen (string);
21273 if (precision > 0 && len > precision)
21274 len = precision;
21275 lisp_string = make_string (string, len);
21276 if (NILP (props))
21277 props = mode_line_string_face_prop;
21278 else if (!NILP (mode_line_string_face))
21279 {
21280 Lisp_Object face = Fplist_get (props, Qface);
21281 props = Fcopy_sequence (props);
21282 if (NILP (face))
21283 face = mode_line_string_face;
21284 else
21285 face = list2 (face, mode_line_string_face);
21286 props = Fplist_put (props, Qface, face);
21287 }
21288 Fadd_text_properties (make_number (0), make_number (len),
21289 props, lisp_string);
21290 }
21291 else
21292 {
21293 len = XFASTINT (Flength (lisp_string));
21294 if (precision > 0 && len > precision)
21295 {
21296 len = precision;
21297 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
21298 precision = -1;
21299 }
21300 if (!NILP (mode_line_string_face))
21301 {
21302 Lisp_Object face;
21303 if (NILP (props))
21304 props = Ftext_properties_at (make_number (0), lisp_string);
21305 face = Fplist_get (props, Qface);
21306 if (NILP (face))
21307 face = mode_line_string_face;
21308 else
21309 face = list2 (face, mode_line_string_face);
21310 props = list2 (Qface, face);
21311 if (copy_string)
21312 lisp_string = Fcopy_sequence (lisp_string);
21313 }
21314 if (!NILP (props))
21315 Fadd_text_properties (make_number (0), make_number (len),
21316 props, lisp_string);
21317 }
21318
21319 if (len > 0)
21320 {
21321 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21322 n += len;
21323 }
21324
21325 if (field_width > len)
21326 {
21327 field_width -= len;
21328 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
21329 if (!NILP (props))
21330 Fadd_text_properties (make_number (0), make_number (field_width),
21331 props, lisp_string);
21332 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21333 n += field_width;
21334 }
21335
21336 return n;
21337 }
21338
21339
21340 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
21341 1, 4, 0,
21342 doc: /* Format a string out of a mode line format specification.
21343 First arg FORMAT specifies the mode line format (see `mode-line-format'
21344 for details) to use.
21345
21346 By default, the format is evaluated for the currently selected window.
21347
21348 Optional second arg FACE specifies the face property to put on all
21349 characters for which no face is specified. The value nil means the
21350 default face. The value t means whatever face the window's mode line
21351 currently uses (either `mode-line' or `mode-line-inactive',
21352 depending on whether the window is the selected window or not).
21353 An integer value means the value string has no text
21354 properties.
21355
21356 Optional third and fourth args WINDOW and BUFFER specify the window
21357 and buffer to use as the context for the formatting (defaults
21358 are the selected window and the WINDOW's buffer). */)
21359 (Lisp_Object format, Lisp_Object face,
21360 Lisp_Object window, Lisp_Object buffer)
21361 {
21362 struct it it;
21363 int len;
21364 struct window *w;
21365 struct buffer *old_buffer = NULL;
21366 int face_id;
21367 int no_props = INTEGERP (face);
21368 ptrdiff_t count = SPECPDL_INDEX ();
21369 Lisp_Object str;
21370 int string_start = 0;
21371
21372 w = decode_any_window (window);
21373 XSETWINDOW (window, w);
21374
21375 if (NILP (buffer))
21376 buffer = w->contents;
21377 CHECK_BUFFER (buffer);
21378
21379 /* Make formatting the modeline a non-op when noninteractive, otherwise
21380 there will be problems later caused by a partially initialized frame. */
21381 if (NILP (format) || noninteractive)
21382 return empty_unibyte_string;
21383
21384 if (no_props)
21385 face = Qnil;
21386
21387 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
21388 : EQ (face, Qt) ? (EQ (window, selected_window)
21389 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21390 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21391 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21392 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21393 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21394 : DEFAULT_FACE_ID;
21395
21396 old_buffer = current_buffer;
21397
21398 /* Save things including mode_line_proptrans_alist,
21399 and set that to nil so that we don't alter the outer value. */
21400 record_unwind_protect (unwind_format_mode_line,
21401 format_mode_line_unwind_data
21402 (XFRAME (WINDOW_FRAME (w)),
21403 old_buffer, selected_window, 1));
21404 mode_line_proptrans_alist = Qnil;
21405
21406 Fselect_window (window, Qt);
21407 set_buffer_internal_1 (XBUFFER (buffer));
21408
21409 init_iterator (&it, w, -1, -1, NULL, face_id);
21410
21411 if (no_props)
21412 {
21413 mode_line_target = MODE_LINE_NOPROP;
21414 mode_line_string_face_prop = Qnil;
21415 mode_line_string_list = Qnil;
21416 string_start = MODE_LINE_NOPROP_LEN (0);
21417 }
21418 else
21419 {
21420 mode_line_target = MODE_LINE_STRING;
21421 mode_line_string_list = Qnil;
21422 mode_line_string_face = face;
21423 mode_line_string_face_prop
21424 = NILP (face) ? Qnil : list2 (Qface, face);
21425 }
21426
21427 push_kboard (FRAME_KBOARD (it.f));
21428 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21429 pop_kboard ();
21430
21431 if (no_props)
21432 {
21433 len = MODE_LINE_NOPROP_LEN (string_start);
21434 str = make_string (mode_line_noprop_buf + string_start, len);
21435 }
21436 else
21437 {
21438 mode_line_string_list = Fnreverse (mode_line_string_list);
21439 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21440 empty_unibyte_string);
21441 }
21442
21443 unbind_to (count, Qnil);
21444 return str;
21445 }
21446
21447 /* Write a null-terminated, right justified decimal representation of
21448 the positive integer D to BUF using a minimal field width WIDTH. */
21449
21450 static void
21451 pint2str (register char *buf, register int width, register ptrdiff_t d)
21452 {
21453 register char *p = buf;
21454
21455 if (d <= 0)
21456 *p++ = '0';
21457 else
21458 {
21459 while (d > 0)
21460 {
21461 *p++ = d % 10 + '0';
21462 d /= 10;
21463 }
21464 }
21465
21466 for (width -= (int) (p - buf); width > 0; --width)
21467 *p++ = ' ';
21468 *p-- = '\0';
21469 while (p > buf)
21470 {
21471 d = *buf;
21472 *buf++ = *p;
21473 *p-- = d;
21474 }
21475 }
21476
21477 /* Write a null-terminated, right justified decimal and "human
21478 readable" representation of the nonnegative integer D to BUF using
21479 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21480
21481 static const char power_letter[] =
21482 {
21483 0, /* no letter */
21484 'k', /* kilo */
21485 'M', /* mega */
21486 'G', /* giga */
21487 'T', /* tera */
21488 'P', /* peta */
21489 'E', /* exa */
21490 'Z', /* zetta */
21491 'Y' /* yotta */
21492 };
21493
21494 static void
21495 pint2hrstr (char *buf, int width, ptrdiff_t d)
21496 {
21497 /* We aim to represent the nonnegative integer D as
21498 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21499 ptrdiff_t quotient = d;
21500 int remainder = 0;
21501 /* -1 means: do not use TENTHS. */
21502 int tenths = -1;
21503 int exponent = 0;
21504
21505 /* Length of QUOTIENT.TENTHS as a string. */
21506 int length;
21507
21508 char * psuffix;
21509 char * p;
21510
21511 if (quotient >= 1000)
21512 {
21513 /* Scale to the appropriate EXPONENT. */
21514 do
21515 {
21516 remainder = quotient % 1000;
21517 quotient /= 1000;
21518 exponent++;
21519 }
21520 while (quotient >= 1000);
21521
21522 /* Round to nearest and decide whether to use TENTHS or not. */
21523 if (quotient <= 9)
21524 {
21525 tenths = remainder / 100;
21526 if (remainder % 100 >= 50)
21527 {
21528 if (tenths < 9)
21529 tenths++;
21530 else
21531 {
21532 quotient++;
21533 if (quotient == 10)
21534 tenths = -1;
21535 else
21536 tenths = 0;
21537 }
21538 }
21539 }
21540 else
21541 if (remainder >= 500)
21542 {
21543 if (quotient < 999)
21544 quotient++;
21545 else
21546 {
21547 quotient = 1;
21548 exponent++;
21549 tenths = 0;
21550 }
21551 }
21552 }
21553
21554 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21555 if (tenths == -1 && quotient <= 99)
21556 if (quotient <= 9)
21557 length = 1;
21558 else
21559 length = 2;
21560 else
21561 length = 3;
21562 p = psuffix = buf + max (width, length);
21563
21564 /* Print EXPONENT. */
21565 *psuffix++ = power_letter[exponent];
21566 *psuffix = '\0';
21567
21568 /* Print TENTHS. */
21569 if (tenths >= 0)
21570 {
21571 *--p = '0' + tenths;
21572 *--p = '.';
21573 }
21574
21575 /* Print QUOTIENT. */
21576 do
21577 {
21578 int digit = quotient % 10;
21579 *--p = '0' + digit;
21580 }
21581 while ((quotient /= 10) != 0);
21582
21583 /* Print leading spaces. */
21584 while (buf < p)
21585 *--p = ' ';
21586 }
21587
21588 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21589 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21590 type of CODING_SYSTEM. Return updated pointer into BUF. */
21591
21592 static unsigned char invalid_eol_type[] = "(*invalid*)";
21593
21594 static char *
21595 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21596 {
21597 Lisp_Object val;
21598 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21599 const unsigned char *eol_str;
21600 int eol_str_len;
21601 /* The EOL conversion we are using. */
21602 Lisp_Object eoltype;
21603
21604 val = CODING_SYSTEM_SPEC (coding_system);
21605 eoltype = Qnil;
21606
21607 if (!VECTORP (val)) /* Not yet decided. */
21608 {
21609 *buf++ = multibyte ? '-' : ' ';
21610 if (eol_flag)
21611 eoltype = eol_mnemonic_undecided;
21612 /* Don't mention EOL conversion if it isn't decided. */
21613 }
21614 else
21615 {
21616 Lisp_Object attrs;
21617 Lisp_Object eolvalue;
21618
21619 attrs = AREF (val, 0);
21620 eolvalue = AREF (val, 2);
21621
21622 *buf++ = multibyte
21623 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21624 : ' ';
21625
21626 if (eol_flag)
21627 {
21628 /* The EOL conversion that is normal on this system. */
21629
21630 if (NILP (eolvalue)) /* Not yet decided. */
21631 eoltype = eol_mnemonic_undecided;
21632 else if (VECTORP (eolvalue)) /* Not yet decided. */
21633 eoltype = eol_mnemonic_undecided;
21634 else /* eolvalue is Qunix, Qdos, or Qmac. */
21635 eoltype = (EQ (eolvalue, Qunix)
21636 ? eol_mnemonic_unix
21637 : (EQ (eolvalue, Qdos) == 1
21638 ? eol_mnemonic_dos : eol_mnemonic_mac));
21639 }
21640 }
21641
21642 if (eol_flag)
21643 {
21644 /* Mention the EOL conversion if it is not the usual one. */
21645 if (STRINGP (eoltype))
21646 {
21647 eol_str = SDATA (eoltype);
21648 eol_str_len = SBYTES (eoltype);
21649 }
21650 else if (CHARACTERP (eoltype))
21651 {
21652 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21653 int c = XFASTINT (eoltype);
21654 eol_str_len = CHAR_STRING (c, tmp);
21655 eol_str = tmp;
21656 }
21657 else
21658 {
21659 eol_str = invalid_eol_type;
21660 eol_str_len = sizeof (invalid_eol_type) - 1;
21661 }
21662 memcpy (buf, eol_str, eol_str_len);
21663 buf += eol_str_len;
21664 }
21665
21666 return buf;
21667 }
21668
21669 /* Return a string for the output of a mode line %-spec for window W,
21670 generated by character C. FIELD_WIDTH > 0 means pad the string
21671 returned with spaces to that value. Return a Lisp string in
21672 *STRING if the resulting string is taken from that Lisp string.
21673
21674 Note we operate on the current buffer for most purposes. */
21675
21676 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21677
21678 static const char *
21679 decode_mode_spec (struct window *w, register int c, int field_width,
21680 Lisp_Object *string)
21681 {
21682 Lisp_Object obj;
21683 struct frame *f = XFRAME (WINDOW_FRAME (w));
21684 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21685 /* We are going to use f->decode_mode_spec_buffer as the buffer to
21686 produce strings from numerical values, so limit preposterously
21687 large values of FIELD_WIDTH to avoid overrunning the buffer's
21688 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
21689 bytes plus the terminating null. */
21690 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
21691 struct buffer *b = current_buffer;
21692
21693 obj = Qnil;
21694 *string = Qnil;
21695
21696 switch (c)
21697 {
21698 case '*':
21699 if (!NILP (BVAR (b, read_only)))
21700 return "%";
21701 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21702 return "*";
21703 return "-";
21704
21705 case '+':
21706 /* This differs from %* only for a modified read-only buffer. */
21707 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21708 return "*";
21709 if (!NILP (BVAR (b, read_only)))
21710 return "%";
21711 return "-";
21712
21713 case '&':
21714 /* This differs from %* in ignoring read-only-ness. */
21715 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21716 return "*";
21717 return "-";
21718
21719 case '%':
21720 return "%";
21721
21722 case '[':
21723 {
21724 int i;
21725 char *p;
21726
21727 if (command_loop_level > 5)
21728 return "[[[... ";
21729 p = decode_mode_spec_buf;
21730 for (i = 0; i < command_loop_level; i++)
21731 *p++ = '[';
21732 *p = 0;
21733 return decode_mode_spec_buf;
21734 }
21735
21736 case ']':
21737 {
21738 int i;
21739 char *p;
21740
21741 if (command_loop_level > 5)
21742 return " ...]]]";
21743 p = decode_mode_spec_buf;
21744 for (i = 0; i < command_loop_level; i++)
21745 *p++ = ']';
21746 *p = 0;
21747 return decode_mode_spec_buf;
21748 }
21749
21750 case '-':
21751 {
21752 register int i;
21753
21754 /* Let lots_of_dashes be a string of infinite length. */
21755 if (mode_line_target == MODE_LINE_NOPROP
21756 || mode_line_target == MODE_LINE_STRING)
21757 return "--";
21758 if (field_width <= 0
21759 || field_width > sizeof (lots_of_dashes))
21760 {
21761 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21762 decode_mode_spec_buf[i] = '-';
21763 decode_mode_spec_buf[i] = '\0';
21764 return decode_mode_spec_buf;
21765 }
21766 else
21767 return lots_of_dashes;
21768 }
21769
21770 case 'b':
21771 obj = BVAR (b, name);
21772 break;
21773
21774 case 'c':
21775 /* %c and %l are ignored in `frame-title-format'.
21776 (In redisplay_internal, the frame title is drawn _before_ the
21777 windows are updated, so the stuff which depends on actual
21778 window contents (such as %l) may fail to render properly, or
21779 even crash emacs.) */
21780 if (mode_line_target == MODE_LINE_TITLE)
21781 return "";
21782 else
21783 {
21784 ptrdiff_t col = current_column ();
21785 w->column_number_displayed = col;
21786 pint2str (decode_mode_spec_buf, width, col);
21787 return decode_mode_spec_buf;
21788 }
21789
21790 case 'e':
21791 #ifndef SYSTEM_MALLOC
21792 {
21793 if (NILP (Vmemory_full))
21794 return "";
21795 else
21796 return "!MEM FULL! ";
21797 }
21798 #else
21799 return "";
21800 #endif
21801
21802 case 'F':
21803 /* %F displays the frame name. */
21804 if (!NILP (f->title))
21805 return SSDATA (f->title);
21806 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21807 return SSDATA (f->name);
21808 return "Emacs";
21809
21810 case 'f':
21811 obj = BVAR (b, filename);
21812 break;
21813
21814 case 'i':
21815 {
21816 ptrdiff_t size = ZV - BEGV;
21817 pint2str (decode_mode_spec_buf, width, size);
21818 return decode_mode_spec_buf;
21819 }
21820
21821 case 'I':
21822 {
21823 ptrdiff_t size = ZV - BEGV;
21824 pint2hrstr (decode_mode_spec_buf, width, size);
21825 return decode_mode_spec_buf;
21826 }
21827
21828 case 'l':
21829 {
21830 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21831 ptrdiff_t topline, nlines, height;
21832 ptrdiff_t junk;
21833
21834 /* %c and %l are ignored in `frame-title-format'. */
21835 if (mode_line_target == MODE_LINE_TITLE)
21836 return "";
21837
21838 startpos = marker_position (w->start);
21839 startpos_byte = marker_byte_position (w->start);
21840 height = WINDOW_TOTAL_LINES (w);
21841
21842 /* If we decided that this buffer isn't suitable for line numbers,
21843 don't forget that too fast. */
21844 if (w->base_line_pos == -1)
21845 goto no_value;
21846
21847 /* If the buffer is very big, don't waste time. */
21848 if (INTEGERP (Vline_number_display_limit)
21849 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21850 {
21851 w->base_line_pos = 0;
21852 w->base_line_number = 0;
21853 goto no_value;
21854 }
21855
21856 if (w->base_line_number > 0
21857 && w->base_line_pos > 0
21858 && w->base_line_pos <= startpos)
21859 {
21860 line = w->base_line_number;
21861 linepos = w->base_line_pos;
21862 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21863 }
21864 else
21865 {
21866 line = 1;
21867 linepos = BUF_BEGV (b);
21868 linepos_byte = BUF_BEGV_BYTE (b);
21869 }
21870
21871 /* Count lines from base line to window start position. */
21872 nlines = display_count_lines (linepos_byte,
21873 startpos_byte,
21874 startpos, &junk);
21875
21876 topline = nlines + line;
21877
21878 /* Determine a new base line, if the old one is too close
21879 or too far away, or if we did not have one.
21880 "Too close" means it's plausible a scroll-down would
21881 go back past it. */
21882 if (startpos == BUF_BEGV (b))
21883 {
21884 w->base_line_number = topline;
21885 w->base_line_pos = BUF_BEGV (b);
21886 }
21887 else if (nlines < height + 25 || nlines > height * 3 + 50
21888 || linepos == BUF_BEGV (b))
21889 {
21890 ptrdiff_t limit = BUF_BEGV (b);
21891 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21892 ptrdiff_t position;
21893 ptrdiff_t distance =
21894 (height * 2 + 30) * line_number_display_limit_width;
21895
21896 if (startpos - distance > limit)
21897 {
21898 limit = startpos - distance;
21899 limit_byte = CHAR_TO_BYTE (limit);
21900 }
21901
21902 nlines = display_count_lines (startpos_byte,
21903 limit_byte,
21904 - (height * 2 + 30),
21905 &position);
21906 /* If we couldn't find the lines we wanted within
21907 line_number_display_limit_width chars per line,
21908 give up on line numbers for this window. */
21909 if (position == limit_byte && limit == startpos - distance)
21910 {
21911 w->base_line_pos = -1;
21912 w->base_line_number = 0;
21913 goto no_value;
21914 }
21915
21916 w->base_line_number = topline - nlines;
21917 w->base_line_pos = BYTE_TO_CHAR (position);
21918 }
21919
21920 /* Now count lines from the start pos to point. */
21921 nlines = display_count_lines (startpos_byte,
21922 PT_BYTE, PT, &junk);
21923
21924 /* Record that we did display the line number. */
21925 line_number_displayed = 1;
21926
21927 /* Make the string to show. */
21928 pint2str (decode_mode_spec_buf, width, topline + nlines);
21929 return decode_mode_spec_buf;
21930 no_value:
21931 {
21932 char* p = decode_mode_spec_buf;
21933 int pad = width - 2;
21934 while (pad-- > 0)
21935 *p++ = ' ';
21936 *p++ = '?';
21937 *p++ = '?';
21938 *p = '\0';
21939 return decode_mode_spec_buf;
21940 }
21941 }
21942 break;
21943
21944 case 'm':
21945 obj = BVAR (b, mode_name);
21946 break;
21947
21948 case 'n':
21949 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21950 return " Narrow";
21951 break;
21952
21953 case 'p':
21954 {
21955 ptrdiff_t pos = marker_position (w->start);
21956 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21957
21958 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
21959 {
21960 if (pos <= BUF_BEGV (b))
21961 return "All";
21962 else
21963 return "Bottom";
21964 }
21965 else if (pos <= BUF_BEGV (b))
21966 return "Top";
21967 else
21968 {
21969 if (total > 1000000)
21970 /* Do it differently for a large value, to avoid overflow. */
21971 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21972 else
21973 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21974 /* We can't normally display a 3-digit number,
21975 so get us a 2-digit number that is close. */
21976 if (total == 100)
21977 total = 99;
21978 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21979 return decode_mode_spec_buf;
21980 }
21981 }
21982
21983 /* Display percentage of size above the bottom of the screen. */
21984 case 'P':
21985 {
21986 ptrdiff_t toppos = marker_position (w->start);
21987 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
21988 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21989
21990 if (botpos >= BUF_ZV (b))
21991 {
21992 if (toppos <= BUF_BEGV (b))
21993 return "All";
21994 else
21995 return "Bottom";
21996 }
21997 else
21998 {
21999 if (total > 1000000)
22000 /* Do it differently for a large value, to avoid overflow. */
22001 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22002 else
22003 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
22004 /* We can't normally display a 3-digit number,
22005 so get us a 2-digit number that is close. */
22006 if (total == 100)
22007 total = 99;
22008 if (toppos <= BUF_BEGV (b))
22009 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
22010 else
22011 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22012 return decode_mode_spec_buf;
22013 }
22014 }
22015
22016 case 's':
22017 /* status of process */
22018 obj = Fget_buffer_process (Fcurrent_buffer ());
22019 if (NILP (obj))
22020 return "no process";
22021 #ifndef MSDOS
22022 obj = Fsymbol_name (Fprocess_status (obj));
22023 #endif
22024 break;
22025
22026 case '@':
22027 {
22028 ptrdiff_t count = inhibit_garbage_collection ();
22029 Lisp_Object val = call1 (intern ("file-remote-p"),
22030 BVAR (current_buffer, directory));
22031 unbind_to (count, Qnil);
22032
22033 if (NILP (val))
22034 return "-";
22035 else
22036 return "@";
22037 }
22038
22039 case 'z':
22040 /* coding-system (not including end-of-line format) */
22041 case 'Z':
22042 /* coding-system (including end-of-line type) */
22043 {
22044 int eol_flag = (c == 'Z');
22045 char *p = decode_mode_spec_buf;
22046
22047 if (! FRAME_WINDOW_P (f))
22048 {
22049 /* No need to mention EOL here--the terminal never needs
22050 to do EOL conversion. */
22051 p = decode_mode_spec_coding (CODING_ID_NAME
22052 (FRAME_KEYBOARD_CODING (f)->id),
22053 p, 0);
22054 p = decode_mode_spec_coding (CODING_ID_NAME
22055 (FRAME_TERMINAL_CODING (f)->id),
22056 p, 0);
22057 }
22058 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
22059 p, eol_flag);
22060
22061 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
22062 #ifdef subprocesses
22063 obj = Fget_buffer_process (Fcurrent_buffer ());
22064 if (PROCESSP (obj))
22065 {
22066 p = decode_mode_spec_coding
22067 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
22068 p = decode_mode_spec_coding
22069 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
22070 }
22071 #endif /* subprocesses */
22072 #endif /* 0 */
22073 *p = 0;
22074 return decode_mode_spec_buf;
22075 }
22076 }
22077
22078 if (STRINGP (obj))
22079 {
22080 *string = obj;
22081 return SSDATA (obj);
22082 }
22083 else
22084 return "";
22085 }
22086
22087
22088 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
22089 means count lines back from START_BYTE. But don't go beyond
22090 LIMIT_BYTE. Return the number of lines thus found (always
22091 nonnegative).
22092
22093 Set *BYTE_POS_PTR to the byte position where we stopped. This is
22094 either the position COUNT lines after/before START_BYTE, if we
22095 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
22096 COUNT lines. */
22097
22098 static ptrdiff_t
22099 display_count_lines (ptrdiff_t start_byte,
22100 ptrdiff_t limit_byte, ptrdiff_t count,
22101 ptrdiff_t *byte_pos_ptr)
22102 {
22103 register unsigned char *cursor;
22104 unsigned char *base;
22105
22106 register ptrdiff_t ceiling;
22107 register unsigned char *ceiling_addr;
22108 ptrdiff_t orig_count = count;
22109
22110 /* If we are not in selective display mode,
22111 check only for newlines. */
22112 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
22113 && !INTEGERP (BVAR (current_buffer, selective_display)));
22114
22115 if (count > 0)
22116 {
22117 while (start_byte < limit_byte)
22118 {
22119 ceiling = BUFFER_CEILING_OF (start_byte);
22120 ceiling = min (limit_byte - 1, ceiling);
22121 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
22122 base = (cursor = BYTE_POS_ADDR (start_byte));
22123
22124 do
22125 {
22126 if (selective_display)
22127 {
22128 while (*cursor != '\n' && *cursor != 015
22129 && ++cursor != ceiling_addr)
22130 continue;
22131 if (cursor == ceiling_addr)
22132 break;
22133 }
22134 else
22135 {
22136 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
22137 if (! cursor)
22138 break;
22139 }
22140
22141 cursor++;
22142
22143 if (--count == 0)
22144 {
22145 start_byte += cursor - base;
22146 *byte_pos_ptr = start_byte;
22147 return orig_count;
22148 }
22149 }
22150 while (cursor < ceiling_addr);
22151
22152 start_byte += ceiling_addr - base;
22153 }
22154 }
22155 else
22156 {
22157 while (start_byte > limit_byte)
22158 {
22159 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
22160 ceiling = max (limit_byte, ceiling);
22161 ceiling_addr = BYTE_POS_ADDR (ceiling);
22162 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
22163 while (1)
22164 {
22165 if (selective_display)
22166 {
22167 while (--cursor >= ceiling_addr
22168 && *cursor != '\n' && *cursor != 015)
22169 continue;
22170 if (cursor < ceiling_addr)
22171 break;
22172 }
22173 else
22174 {
22175 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
22176 if (! cursor)
22177 break;
22178 }
22179
22180 if (++count == 0)
22181 {
22182 start_byte += cursor - base + 1;
22183 *byte_pos_ptr = start_byte;
22184 /* When scanning backwards, we should
22185 not count the newline posterior to which we stop. */
22186 return - orig_count - 1;
22187 }
22188 }
22189 start_byte += ceiling_addr - base;
22190 }
22191 }
22192
22193 *byte_pos_ptr = limit_byte;
22194
22195 if (count < 0)
22196 return - orig_count + count;
22197 return orig_count - count;
22198
22199 }
22200
22201
22202 \f
22203 /***********************************************************************
22204 Displaying strings
22205 ***********************************************************************/
22206
22207 /* Display a NUL-terminated string, starting with index START.
22208
22209 If STRING is non-null, display that C string. Otherwise, the Lisp
22210 string LISP_STRING is displayed. There's a case that STRING is
22211 non-null and LISP_STRING is not nil. It means STRING is a string
22212 data of LISP_STRING. In that case, we display LISP_STRING while
22213 ignoring its text properties.
22214
22215 If FACE_STRING is not nil, FACE_STRING_POS is a position in
22216 FACE_STRING. Display STRING or LISP_STRING with the face at
22217 FACE_STRING_POS in FACE_STRING:
22218
22219 Display the string in the environment given by IT, but use the
22220 standard display table, temporarily.
22221
22222 FIELD_WIDTH is the minimum number of output glyphs to produce.
22223 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22224 with spaces. If STRING has more characters, more than FIELD_WIDTH
22225 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
22226
22227 PRECISION is the maximum number of characters to output from
22228 STRING. PRECISION < 0 means don't truncate the string.
22229
22230 This is roughly equivalent to printf format specifiers:
22231
22232 FIELD_WIDTH PRECISION PRINTF
22233 ----------------------------------------
22234 -1 -1 %s
22235 -1 10 %.10s
22236 10 -1 %10s
22237 20 10 %20.10s
22238
22239 MULTIBYTE zero means do not display multibyte chars, > 0 means do
22240 display them, and < 0 means obey the current buffer's value of
22241 enable_multibyte_characters.
22242
22243 Value is the number of columns displayed. */
22244
22245 static int
22246 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
22247 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
22248 int field_width, int precision, int max_x, int multibyte)
22249 {
22250 int hpos_at_start = it->hpos;
22251 int saved_face_id = it->face_id;
22252 struct glyph_row *row = it->glyph_row;
22253 ptrdiff_t it_charpos;
22254
22255 /* Initialize the iterator IT for iteration over STRING beginning
22256 with index START. */
22257 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
22258 precision, field_width, multibyte);
22259 if (string && STRINGP (lisp_string))
22260 /* LISP_STRING is the one returned by decode_mode_spec. We should
22261 ignore its text properties. */
22262 it->stop_charpos = it->end_charpos;
22263
22264 /* If displaying STRING, set up the face of the iterator from
22265 FACE_STRING, if that's given. */
22266 if (STRINGP (face_string))
22267 {
22268 ptrdiff_t endptr;
22269 struct face *face;
22270
22271 it->face_id
22272 = face_at_string_position (it->w, face_string, face_string_pos,
22273 0, it->region_beg_charpos,
22274 it->region_end_charpos,
22275 &endptr, it->base_face_id, 0);
22276 face = FACE_FROM_ID (it->f, it->face_id);
22277 it->face_box_p = face->box != FACE_NO_BOX;
22278 }
22279
22280 /* Set max_x to the maximum allowed X position. Don't let it go
22281 beyond the right edge of the window. */
22282 if (max_x <= 0)
22283 max_x = it->last_visible_x;
22284 else
22285 max_x = min (max_x, it->last_visible_x);
22286
22287 /* Skip over display elements that are not visible. because IT->w is
22288 hscrolled. */
22289 if (it->current_x < it->first_visible_x)
22290 move_it_in_display_line_to (it, 100000, it->first_visible_x,
22291 MOVE_TO_POS | MOVE_TO_X);
22292
22293 row->ascent = it->max_ascent;
22294 row->height = it->max_ascent + it->max_descent;
22295 row->phys_ascent = it->max_phys_ascent;
22296 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
22297 row->extra_line_spacing = it->max_extra_line_spacing;
22298
22299 if (STRINGP (it->string))
22300 it_charpos = IT_STRING_CHARPOS (*it);
22301 else
22302 it_charpos = IT_CHARPOS (*it);
22303
22304 /* This condition is for the case that we are called with current_x
22305 past last_visible_x. */
22306 while (it->current_x < max_x)
22307 {
22308 int x_before, x, n_glyphs_before, i, nglyphs;
22309
22310 /* Get the next display element. */
22311 if (!get_next_display_element (it))
22312 break;
22313
22314 /* Produce glyphs. */
22315 x_before = it->current_x;
22316 n_glyphs_before = row->used[TEXT_AREA];
22317 PRODUCE_GLYPHS (it);
22318
22319 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
22320 i = 0;
22321 x = x_before;
22322 while (i < nglyphs)
22323 {
22324 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
22325
22326 if (it->line_wrap != TRUNCATE
22327 && x + glyph->pixel_width > max_x)
22328 {
22329 /* End of continued line or max_x reached. */
22330 if (CHAR_GLYPH_PADDING_P (*glyph))
22331 {
22332 /* A wide character is unbreakable. */
22333 if (row->reversed_p)
22334 unproduce_glyphs (it, row->used[TEXT_AREA]
22335 - n_glyphs_before);
22336 row->used[TEXT_AREA] = n_glyphs_before;
22337 it->current_x = x_before;
22338 }
22339 else
22340 {
22341 if (row->reversed_p)
22342 unproduce_glyphs (it, row->used[TEXT_AREA]
22343 - (n_glyphs_before + i));
22344 row->used[TEXT_AREA] = n_glyphs_before + i;
22345 it->current_x = x;
22346 }
22347 break;
22348 }
22349 else if (x + glyph->pixel_width >= it->first_visible_x)
22350 {
22351 /* Glyph is at least partially visible. */
22352 ++it->hpos;
22353 if (x < it->first_visible_x)
22354 row->x = x - it->first_visible_x;
22355 }
22356 else
22357 {
22358 /* Glyph is off the left margin of the display area.
22359 Should not happen. */
22360 emacs_abort ();
22361 }
22362
22363 row->ascent = max (row->ascent, it->max_ascent);
22364 row->height = max (row->height, it->max_ascent + it->max_descent);
22365 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
22366 row->phys_height = max (row->phys_height,
22367 it->max_phys_ascent + it->max_phys_descent);
22368 row->extra_line_spacing = max (row->extra_line_spacing,
22369 it->max_extra_line_spacing);
22370 x += glyph->pixel_width;
22371 ++i;
22372 }
22373
22374 /* Stop if max_x reached. */
22375 if (i < nglyphs)
22376 break;
22377
22378 /* Stop at line ends. */
22379 if (ITERATOR_AT_END_OF_LINE_P (it))
22380 {
22381 it->continuation_lines_width = 0;
22382 break;
22383 }
22384
22385 set_iterator_to_next (it, 1);
22386 if (STRINGP (it->string))
22387 it_charpos = IT_STRING_CHARPOS (*it);
22388 else
22389 it_charpos = IT_CHARPOS (*it);
22390
22391 /* Stop if truncating at the right edge. */
22392 if (it->line_wrap == TRUNCATE
22393 && it->current_x >= it->last_visible_x)
22394 {
22395 /* Add truncation mark, but don't do it if the line is
22396 truncated at a padding space. */
22397 if (it_charpos < it->string_nchars)
22398 {
22399 if (!FRAME_WINDOW_P (it->f))
22400 {
22401 int ii, n;
22402
22403 if (it->current_x > it->last_visible_x)
22404 {
22405 if (!row->reversed_p)
22406 {
22407 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22408 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22409 break;
22410 }
22411 else
22412 {
22413 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22414 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22415 break;
22416 unproduce_glyphs (it, ii + 1);
22417 ii = row->used[TEXT_AREA] - (ii + 1);
22418 }
22419 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22420 {
22421 row->used[TEXT_AREA] = ii;
22422 produce_special_glyphs (it, IT_TRUNCATION);
22423 }
22424 }
22425 produce_special_glyphs (it, IT_TRUNCATION);
22426 }
22427 row->truncated_on_right_p = 1;
22428 }
22429 break;
22430 }
22431 }
22432
22433 /* Maybe insert a truncation at the left. */
22434 if (it->first_visible_x
22435 && it_charpos > 0)
22436 {
22437 if (!FRAME_WINDOW_P (it->f)
22438 || (row->reversed_p
22439 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22440 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22441 insert_left_trunc_glyphs (it);
22442 row->truncated_on_left_p = 1;
22443 }
22444
22445 it->face_id = saved_face_id;
22446
22447 /* Value is number of columns displayed. */
22448 return it->hpos - hpos_at_start;
22449 }
22450
22451
22452 \f
22453 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22454 appears as an element of LIST or as the car of an element of LIST.
22455 If PROPVAL is a list, compare each element against LIST in that
22456 way, and return 1/2 if any element of PROPVAL is found in LIST.
22457 Otherwise return 0. This function cannot quit.
22458 The return value is 2 if the text is invisible but with an ellipsis
22459 and 1 if it's invisible and without an ellipsis. */
22460
22461 int
22462 invisible_p (register Lisp_Object propval, Lisp_Object list)
22463 {
22464 register Lisp_Object tail, proptail;
22465
22466 for (tail = list; CONSP (tail); tail = XCDR (tail))
22467 {
22468 register Lisp_Object tem;
22469 tem = XCAR (tail);
22470 if (EQ (propval, tem))
22471 return 1;
22472 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22473 return NILP (XCDR (tem)) ? 1 : 2;
22474 }
22475
22476 if (CONSP (propval))
22477 {
22478 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22479 {
22480 Lisp_Object propelt;
22481 propelt = XCAR (proptail);
22482 for (tail = list; CONSP (tail); tail = XCDR (tail))
22483 {
22484 register Lisp_Object tem;
22485 tem = XCAR (tail);
22486 if (EQ (propelt, tem))
22487 return 1;
22488 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22489 return NILP (XCDR (tem)) ? 1 : 2;
22490 }
22491 }
22492 }
22493
22494 return 0;
22495 }
22496
22497 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22498 doc: /* Non-nil if the property makes the text invisible.
22499 POS-OR-PROP can be a marker or number, in which case it is taken to be
22500 a position in the current buffer and the value of the `invisible' property
22501 is checked; or it can be some other value, which is then presumed to be the
22502 value of the `invisible' property of the text of interest.
22503 The non-nil value returned can be t for truly invisible text or something
22504 else if the text is replaced by an ellipsis. */)
22505 (Lisp_Object pos_or_prop)
22506 {
22507 Lisp_Object prop
22508 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22509 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22510 : pos_or_prop);
22511 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22512 return (invis == 0 ? Qnil
22513 : invis == 1 ? Qt
22514 : make_number (invis));
22515 }
22516
22517 /* Calculate a width or height in pixels from a specification using
22518 the following elements:
22519
22520 SPEC ::=
22521 NUM - a (fractional) multiple of the default font width/height
22522 (NUM) - specifies exactly NUM pixels
22523 UNIT - a fixed number of pixels, see below.
22524 ELEMENT - size of a display element in pixels, see below.
22525 (NUM . SPEC) - equals NUM * SPEC
22526 (+ SPEC SPEC ...) - add pixel values
22527 (- SPEC SPEC ...) - subtract pixel values
22528 (- SPEC) - negate pixel value
22529
22530 NUM ::=
22531 INT or FLOAT - a number constant
22532 SYMBOL - use symbol's (buffer local) variable binding.
22533
22534 UNIT ::=
22535 in - pixels per inch *)
22536 mm - pixels per 1/1000 meter *)
22537 cm - pixels per 1/100 meter *)
22538 width - width of current font in pixels.
22539 height - height of current font in pixels.
22540
22541 *) using the ratio(s) defined in display-pixels-per-inch.
22542
22543 ELEMENT ::=
22544
22545 left-fringe - left fringe width in pixels
22546 right-fringe - right fringe width in pixels
22547
22548 left-margin - left margin width in pixels
22549 right-margin - right margin width in pixels
22550
22551 scroll-bar - scroll-bar area width in pixels
22552
22553 Examples:
22554
22555 Pixels corresponding to 5 inches:
22556 (5 . in)
22557
22558 Total width of non-text areas on left side of window (if scroll-bar is on left):
22559 '(space :width (+ left-fringe left-margin scroll-bar))
22560
22561 Align to first text column (in header line):
22562 '(space :align-to 0)
22563
22564 Align to middle of text area minus half the width of variable `my-image'
22565 containing a loaded image:
22566 '(space :align-to (0.5 . (- text my-image)))
22567
22568 Width of left margin minus width of 1 character in the default font:
22569 '(space :width (- left-margin 1))
22570
22571 Width of left margin minus width of 2 characters in the current font:
22572 '(space :width (- left-margin (2 . width)))
22573
22574 Center 1 character over left-margin (in header line):
22575 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22576
22577 Different ways to express width of left fringe plus left margin minus one pixel:
22578 '(space :width (- (+ left-fringe left-margin) (1)))
22579 '(space :width (+ left-fringe left-margin (- (1))))
22580 '(space :width (+ left-fringe left-margin (-1)))
22581
22582 */
22583
22584 static int
22585 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22586 struct font *font, int width_p, int *align_to)
22587 {
22588 double pixels;
22589
22590 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22591 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22592
22593 if (NILP (prop))
22594 return OK_PIXELS (0);
22595
22596 eassert (FRAME_LIVE_P (it->f));
22597
22598 if (SYMBOLP (prop))
22599 {
22600 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22601 {
22602 char *unit = SSDATA (SYMBOL_NAME (prop));
22603
22604 if (unit[0] == 'i' && unit[1] == 'n')
22605 pixels = 1.0;
22606 else if (unit[0] == 'm' && unit[1] == 'm')
22607 pixels = 25.4;
22608 else if (unit[0] == 'c' && unit[1] == 'm')
22609 pixels = 2.54;
22610 else
22611 pixels = 0;
22612 if (pixels > 0)
22613 {
22614 double ppi = (width_p ? FRAME_RES_X (it->f)
22615 : FRAME_RES_Y (it->f));
22616
22617 if (ppi > 0)
22618 return OK_PIXELS (ppi / pixels);
22619 return 0;
22620 }
22621 }
22622
22623 #ifdef HAVE_WINDOW_SYSTEM
22624 if (EQ (prop, Qheight))
22625 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22626 if (EQ (prop, Qwidth))
22627 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22628 #else
22629 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22630 return OK_PIXELS (1);
22631 #endif
22632
22633 if (EQ (prop, Qtext))
22634 return OK_PIXELS (width_p
22635 ? window_box_width (it->w, TEXT_AREA)
22636 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22637
22638 if (align_to && *align_to < 0)
22639 {
22640 *res = 0;
22641 if (EQ (prop, Qleft))
22642 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22643 if (EQ (prop, Qright))
22644 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22645 if (EQ (prop, Qcenter))
22646 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22647 + window_box_width (it->w, TEXT_AREA) / 2);
22648 if (EQ (prop, Qleft_fringe))
22649 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22650 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22651 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22652 if (EQ (prop, Qright_fringe))
22653 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22654 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22655 : window_box_right_offset (it->w, TEXT_AREA));
22656 if (EQ (prop, Qleft_margin))
22657 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22658 if (EQ (prop, Qright_margin))
22659 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22660 if (EQ (prop, Qscroll_bar))
22661 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22662 ? 0
22663 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22664 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22665 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22666 : 0)));
22667 }
22668 else
22669 {
22670 if (EQ (prop, Qleft_fringe))
22671 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22672 if (EQ (prop, Qright_fringe))
22673 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22674 if (EQ (prop, Qleft_margin))
22675 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22676 if (EQ (prop, Qright_margin))
22677 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22678 if (EQ (prop, Qscroll_bar))
22679 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22680 }
22681
22682 prop = buffer_local_value_1 (prop, it->w->contents);
22683 if (EQ (prop, Qunbound))
22684 prop = Qnil;
22685 }
22686
22687 if (INTEGERP (prop) || FLOATP (prop))
22688 {
22689 int base_unit = (width_p
22690 ? FRAME_COLUMN_WIDTH (it->f)
22691 : FRAME_LINE_HEIGHT (it->f));
22692 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22693 }
22694
22695 if (CONSP (prop))
22696 {
22697 Lisp_Object car = XCAR (prop);
22698 Lisp_Object cdr = XCDR (prop);
22699
22700 if (SYMBOLP (car))
22701 {
22702 #ifdef HAVE_WINDOW_SYSTEM
22703 if (FRAME_WINDOW_P (it->f)
22704 && valid_image_p (prop))
22705 {
22706 ptrdiff_t id = lookup_image (it->f, prop);
22707 struct image *img = IMAGE_FROM_ID (it->f, id);
22708
22709 return OK_PIXELS (width_p ? img->width : img->height);
22710 }
22711 #endif
22712 if (EQ (car, Qplus) || EQ (car, Qminus))
22713 {
22714 int first = 1;
22715 double px;
22716
22717 pixels = 0;
22718 while (CONSP (cdr))
22719 {
22720 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22721 font, width_p, align_to))
22722 return 0;
22723 if (first)
22724 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22725 else
22726 pixels += px;
22727 cdr = XCDR (cdr);
22728 }
22729 if (EQ (car, Qminus))
22730 pixels = -pixels;
22731 return OK_PIXELS (pixels);
22732 }
22733
22734 car = buffer_local_value_1 (car, it->w->contents);
22735 if (EQ (car, Qunbound))
22736 car = Qnil;
22737 }
22738
22739 if (INTEGERP (car) || FLOATP (car))
22740 {
22741 double fact;
22742 pixels = XFLOATINT (car);
22743 if (NILP (cdr))
22744 return OK_PIXELS (pixels);
22745 if (calc_pixel_width_or_height (&fact, it, cdr,
22746 font, width_p, align_to))
22747 return OK_PIXELS (pixels * fact);
22748 return 0;
22749 }
22750
22751 return 0;
22752 }
22753
22754 return 0;
22755 }
22756
22757 \f
22758 /***********************************************************************
22759 Glyph Display
22760 ***********************************************************************/
22761
22762 #ifdef HAVE_WINDOW_SYSTEM
22763
22764 #ifdef GLYPH_DEBUG
22765
22766 void
22767 dump_glyph_string (struct glyph_string *s)
22768 {
22769 fprintf (stderr, "glyph string\n");
22770 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22771 s->x, s->y, s->width, s->height);
22772 fprintf (stderr, " ybase = %d\n", s->ybase);
22773 fprintf (stderr, " hl = %d\n", s->hl);
22774 fprintf (stderr, " left overhang = %d, right = %d\n",
22775 s->left_overhang, s->right_overhang);
22776 fprintf (stderr, " nchars = %d\n", s->nchars);
22777 fprintf (stderr, " extends to end of line = %d\n",
22778 s->extends_to_end_of_line_p);
22779 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22780 fprintf (stderr, " bg width = %d\n", s->background_width);
22781 }
22782
22783 #endif /* GLYPH_DEBUG */
22784
22785 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22786 of XChar2b structures for S; it can't be allocated in
22787 init_glyph_string because it must be allocated via `alloca'. W
22788 is the window on which S is drawn. ROW and AREA are the glyph row
22789 and area within the row from which S is constructed. START is the
22790 index of the first glyph structure covered by S. HL is a
22791 face-override for drawing S. */
22792
22793 #ifdef HAVE_NTGUI
22794 #define OPTIONAL_HDC(hdc) HDC hdc,
22795 #define DECLARE_HDC(hdc) HDC hdc;
22796 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22797 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22798 #endif
22799
22800 #ifndef OPTIONAL_HDC
22801 #define OPTIONAL_HDC(hdc)
22802 #define DECLARE_HDC(hdc)
22803 #define ALLOCATE_HDC(hdc, f)
22804 #define RELEASE_HDC(hdc, f)
22805 #endif
22806
22807 static void
22808 init_glyph_string (struct glyph_string *s,
22809 OPTIONAL_HDC (hdc)
22810 XChar2b *char2b, struct window *w, struct glyph_row *row,
22811 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22812 {
22813 memset (s, 0, sizeof *s);
22814 s->w = w;
22815 s->f = XFRAME (w->frame);
22816 #ifdef HAVE_NTGUI
22817 s->hdc = hdc;
22818 #endif
22819 s->display = FRAME_X_DISPLAY (s->f);
22820 s->window = FRAME_X_WINDOW (s->f);
22821 s->char2b = char2b;
22822 s->hl = hl;
22823 s->row = row;
22824 s->area = area;
22825 s->first_glyph = row->glyphs[area] + start;
22826 s->height = row->height;
22827 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22828 s->ybase = s->y + row->ascent;
22829 }
22830
22831
22832 /* Append the list of glyph strings with head H and tail T to the list
22833 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22834
22835 static void
22836 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22837 struct glyph_string *h, struct glyph_string *t)
22838 {
22839 if (h)
22840 {
22841 if (*head)
22842 (*tail)->next = h;
22843 else
22844 *head = h;
22845 h->prev = *tail;
22846 *tail = t;
22847 }
22848 }
22849
22850
22851 /* Prepend the list of glyph strings with head H and tail T to the
22852 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22853 result. */
22854
22855 static void
22856 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22857 struct glyph_string *h, struct glyph_string *t)
22858 {
22859 if (h)
22860 {
22861 if (*head)
22862 (*head)->prev = t;
22863 else
22864 *tail = t;
22865 t->next = *head;
22866 *head = h;
22867 }
22868 }
22869
22870
22871 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22872 Set *HEAD and *TAIL to the resulting list. */
22873
22874 static void
22875 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22876 struct glyph_string *s)
22877 {
22878 s->next = s->prev = NULL;
22879 append_glyph_string_lists (head, tail, s, s);
22880 }
22881
22882
22883 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22884 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22885 make sure that X resources for the face returned are allocated.
22886 Value is a pointer to a realized face that is ready for display if
22887 DISPLAY_P is non-zero. */
22888
22889 static struct face *
22890 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22891 XChar2b *char2b, int display_p)
22892 {
22893 struct face *face = FACE_FROM_ID (f, face_id);
22894 unsigned code = 0;
22895
22896 if (face->font)
22897 {
22898 code = face->font->driver->encode_char (face->font, c);
22899
22900 if (code == FONT_INVALID_CODE)
22901 code = 0;
22902 }
22903 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22904
22905 /* Make sure X resources of the face are allocated. */
22906 #ifdef HAVE_X_WINDOWS
22907 if (display_p)
22908 #endif
22909 {
22910 eassert (face != NULL);
22911 PREPARE_FACE_FOR_DISPLAY (f, face);
22912 }
22913
22914 return face;
22915 }
22916
22917
22918 /* Get face and two-byte form of character glyph GLYPH on frame F.
22919 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22920 a pointer to a realized face that is ready for display. */
22921
22922 static struct face *
22923 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22924 XChar2b *char2b, int *two_byte_p)
22925 {
22926 struct face *face;
22927 unsigned code = 0;
22928
22929 eassert (glyph->type == CHAR_GLYPH);
22930 face = FACE_FROM_ID (f, glyph->face_id);
22931
22932 /* Make sure X resources of the face are allocated. */
22933 eassert (face != NULL);
22934 PREPARE_FACE_FOR_DISPLAY (f, face);
22935
22936 if (two_byte_p)
22937 *two_byte_p = 0;
22938
22939 if (face->font)
22940 {
22941 if (CHAR_BYTE8_P (glyph->u.ch))
22942 code = CHAR_TO_BYTE8 (glyph->u.ch);
22943 else
22944 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22945
22946 if (code == FONT_INVALID_CODE)
22947 code = 0;
22948 }
22949
22950 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22951 return face;
22952 }
22953
22954
22955 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22956 Return 1 if FONT has a glyph for C, otherwise return 0. */
22957
22958 static int
22959 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22960 {
22961 unsigned code;
22962
22963 if (CHAR_BYTE8_P (c))
22964 code = CHAR_TO_BYTE8 (c);
22965 else
22966 code = font->driver->encode_char (font, c);
22967
22968 if (code == FONT_INVALID_CODE)
22969 return 0;
22970 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22971 return 1;
22972 }
22973
22974
22975 /* Fill glyph string S with composition components specified by S->cmp.
22976
22977 BASE_FACE is the base face of the composition.
22978 S->cmp_from is the index of the first component for S.
22979
22980 OVERLAPS non-zero means S should draw the foreground only, and use
22981 its physical height for clipping. See also draw_glyphs.
22982
22983 Value is the index of a component not in S. */
22984
22985 static int
22986 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
22987 int overlaps)
22988 {
22989 int i;
22990 /* For all glyphs of this composition, starting at the offset
22991 S->cmp_from, until we reach the end of the definition or encounter a
22992 glyph that requires the different face, add it to S. */
22993 struct face *face;
22994
22995 eassert (s);
22996
22997 s->for_overlaps = overlaps;
22998 s->face = NULL;
22999 s->font = NULL;
23000 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
23001 {
23002 int c = COMPOSITION_GLYPH (s->cmp, i);
23003
23004 /* TAB in a composition means display glyphs with padding space
23005 on the left or right. */
23006 if (c != '\t')
23007 {
23008 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
23009 -1, Qnil);
23010
23011 face = get_char_face_and_encoding (s->f, c, face_id,
23012 s->char2b + i, 1);
23013 if (face)
23014 {
23015 if (! s->face)
23016 {
23017 s->face = face;
23018 s->font = s->face->font;
23019 }
23020 else if (s->face != face)
23021 break;
23022 }
23023 }
23024 ++s->nchars;
23025 }
23026 s->cmp_to = i;
23027
23028 if (s->face == NULL)
23029 {
23030 s->face = base_face->ascii_face;
23031 s->font = s->face->font;
23032 }
23033
23034 /* All glyph strings for the same composition has the same width,
23035 i.e. the width set for the first component of the composition. */
23036 s->width = s->first_glyph->pixel_width;
23037
23038 /* If the specified font could not be loaded, use the frame's
23039 default font, but record the fact that we couldn't load it in
23040 the glyph string so that we can draw rectangles for the
23041 characters of the glyph string. */
23042 if (s->font == NULL)
23043 {
23044 s->font_not_found_p = 1;
23045 s->font = FRAME_FONT (s->f);
23046 }
23047
23048 /* Adjust base line for subscript/superscript text. */
23049 s->ybase += s->first_glyph->voffset;
23050
23051 /* This glyph string must always be drawn with 16-bit functions. */
23052 s->two_byte_p = 1;
23053
23054 return s->cmp_to;
23055 }
23056
23057 static int
23058 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
23059 int start, int end, int overlaps)
23060 {
23061 struct glyph *glyph, *last;
23062 Lisp_Object lgstring;
23063 int i;
23064
23065 s->for_overlaps = overlaps;
23066 glyph = s->row->glyphs[s->area] + start;
23067 last = s->row->glyphs[s->area] + end;
23068 s->cmp_id = glyph->u.cmp.id;
23069 s->cmp_from = glyph->slice.cmp.from;
23070 s->cmp_to = glyph->slice.cmp.to + 1;
23071 s->face = FACE_FROM_ID (s->f, face_id);
23072 lgstring = composition_gstring_from_id (s->cmp_id);
23073 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
23074 glyph++;
23075 while (glyph < last
23076 && glyph->u.cmp.automatic
23077 && glyph->u.cmp.id == s->cmp_id
23078 && s->cmp_to == glyph->slice.cmp.from)
23079 s->cmp_to = (glyph++)->slice.cmp.to + 1;
23080
23081 for (i = s->cmp_from; i < s->cmp_to; i++)
23082 {
23083 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
23084 unsigned code = LGLYPH_CODE (lglyph);
23085
23086 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
23087 }
23088 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
23089 return glyph - s->row->glyphs[s->area];
23090 }
23091
23092
23093 /* Fill glyph string S from a sequence glyphs for glyphless characters.
23094 See the comment of fill_glyph_string for arguments.
23095 Value is the index of the first glyph not in S. */
23096
23097
23098 static int
23099 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
23100 int start, int end, int overlaps)
23101 {
23102 struct glyph *glyph, *last;
23103 int voffset;
23104
23105 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
23106 s->for_overlaps = overlaps;
23107 glyph = s->row->glyphs[s->area] + start;
23108 last = s->row->glyphs[s->area] + end;
23109 voffset = glyph->voffset;
23110 s->face = FACE_FROM_ID (s->f, face_id);
23111 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
23112 s->nchars = 1;
23113 s->width = glyph->pixel_width;
23114 glyph++;
23115 while (glyph < last
23116 && glyph->type == GLYPHLESS_GLYPH
23117 && glyph->voffset == voffset
23118 && glyph->face_id == face_id)
23119 {
23120 s->nchars++;
23121 s->width += glyph->pixel_width;
23122 glyph++;
23123 }
23124 s->ybase += voffset;
23125 return glyph - s->row->glyphs[s->area];
23126 }
23127
23128
23129 /* Fill glyph string S from a sequence of character glyphs.
23130
23131 FACE_ID is the face id of the string. START is the index of the
23132 first glyph to consider, END is the index of the last + 1.
23133 OVERLAPS non-zero means S should draw the foreground only, and use
23134 its physical height for clipping. See also draw_glyphs.
23135
23136 Value is the index of the first glyph not in S. */
23137
23138 static int
23139 fill_glyph_string (struct glyph_string *s, int face_id,
23140 int start, int end, int overlaps)
23141 {
23142 struct glyph *glyph, *last;
23143 int voffset;
23144 int glyph_not_available_p;
23145
23146 eassert (s->f == XFRAME (s->w->frame));
23147 eassert (s->nchars == 0);
23148 eassert (start >= 0 && end > start);
23149
23150 s->for_overlaps = overlaps;
23151 glyph = s->row->glyphs[s->area] + start;
23152 last = s->row->glyphs[s->area] + end;
23153 voffset = glyph->voffset;
23154 s->padding_p = glyph->padding_p;
23155 glyph_not_available_p = glyph->glyph_not_available_p;
23156
23157 while (glyph < last
23158 && glyph->type == CHAR_GLYPH
23159 && glyph->voffset == voffset
23160 /* Same face id implies same font, nowadays. */
23161 && glyph->face_id == face_id
23162 && glyph->glyph_not_available_p == glyph_not_available_p)
23163 {
23164 int two_byte_p;
23165
23166 s->face = get_glyph_face_and_encoding (s->f, glyph,
23167 s->char2b + s->nchars,
23168 &two_byte_p);
23169 s->two_byte_p = two_byte_p;
23170 ++s->nchars;
23171 eassert (s->nchars <= end - start);
23172 s->width += glyph->pixel_width;
23173 if (glyph++->padding_p != s->padding_p)
23174 break;
23175 }
23176
23177 s->font = s->face->font;
23178
23179 /* If the specified font could not be loaded, use the frame's font,
23180 but record the fact that we couldn't load it in
23181 S->font_not_found_p so that we can draw rectangles for the
23182 characters of the glyph string. */
23183 if (s->font == NULL || glyph_not_available_p)
23184 {
23185 s->font_not_found_p = 1;
23186 s->font = FRAME_FONT (s->f);
23187 }
23188
23189 /* Adjust base line for subscript/superscript text. */
23190 s->ybase += voffset;
23191
23192 eassert (s->face && s->face->gc);
23193 return glyph - s->row->glyphs[s->area];
23194 }
23195
23196
23197 /* Fill glyph string S from image glyph S->first_glyph. */
23198
23199 static void
23200 fill_image_glyph_string (struct glyph_string *s)
23201 {
23202 eassert (s->first_glyph->type == IMAGE_GLYPH);
23203 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
23204 eassert (s->img);
23205 s->slice = s->first_glyph->slice.img;
23206 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
23207 s->font = s->face->font;
23208 s->width = s->first_glyph->pixel_width;
23209
23210 /* Adjust base line for subscript/superscript text. */
23211 s->ybase += s->first_glyph->voffset;
23212 }
23213
23214
23215 /* Fill glyph string S from a sequence of stretch glyphs.
23216
23217 START is the index of the first glyph to consider,
23218 END is the index of the last + 1.
23219
23220 Value is the index of the first glyph not in S. */
23221
23222 static int
23223 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
23224 {
23225 struct glyph *glyph, *last;
23226 int voffset, face_id;
23227
23228 eassert (s->first_glyph->type == STRETCH_GLYPH);
23229
23230 glyph = s->row->glyphs[s->area] + start;
23231 last = s->row->glyphs[s->area] + end;
23232 face_id = glyph->face_id;
23233 s->face = FACE_FROM_ID (s->f, face_id);
23234 s->font = s->face->font;
23235 s->width = glyph->pixel_width;
23236 s->nchars = 1;
23237 voffset = glyph->voffset;
23238
23239 for (++glyph;
23240 (glyph < last
23241 && glyph->type == STRETCH_GLYPH
23242 && glyph->voffset == voffset
23243 && glyph->face_id == face_id);
23244 ++glyph)
23245 s->width += glyph->pixel_width;
23246
23247 /* Adjust base line for subscript/superscript text. */
23248 s->ybase += voffset;
23249
23250 /* The case that face->gc == 0 is handled when drawing the glyph
23251 string by calling PREPARE_FACE_FOR_DISPLAY. */
23252 eassert (s->face);
23253 return glyph - s->row->glyphs[s->area];
23254 }
23255
23256 static struct font_metrics *
23257 get_per_char_metric (struct font *font, XChar2b *char2b)
23258 {
23259 static struct font_metrics metrics;
23260 unsigned code;
23261
23262 if (! font)
23263 return NULL;
23264 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
23265 if (code == FONT_INVALID_CODE)
23266 return NULL;
23267 font->driver->text_extents (font, &code, 1, &metrics);
23268 return &metrics;
23269 }
23270
23271 /* EXPORT for RIF:
23272 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
23273 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
23274 assumed to be zero. */
23275
23276 void
23277 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
23278 {
23279 *left = *right = 0;
23280
23281 if (glyph->type == CHAR_GLYPH)
23282 {
23283 struct face *face;
23284 XChar2b char2b;
23285 struct font_metrics *pcm;
23286
23287 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
23288 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
23289 {
23290 if (pcm->rbearing > pcm->width)
23291 *right = pcm->rbearing - pcm->width;
23292 if (pcm->lbearing < 0)
23293 *left = -pcm->lbearing;
23294 }
23295 }
23296 else if (glyph->type == COMPOSITE_GLYPH)
23297 {
23298 if (! glyph->u.cmp.automatic)
23299 {
23300 struct composition *cmp = composition_table[glyph->u.cmp.id];
23301
23302 if (cmp->rbearing > cmp->pixel_width)
23303 *right = cmp->rbearing - cmp->pixel_width;
23304 if (cmp->lbearing < 0)
23305 *left = - cmp->lbearing;
23306 }
23307 else
23308 {
23309 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
23310 struct font_metrics metrics;
23311
23312 composition_gstring_width (gstring, glyph->slice.cmp.from,
23313 glyph->slice.cmp.to + 1, &metrics);
23314 if (metrics.rbearing > metrics.width)
23315 *right = metrics.rbearing - metrics.width;
23316 if (metrics.lbearing < 0)
23317 *left = - metrics.lbearing;
23318 }
23319 }
23320 }
23321
23322
23323 /* Return the index of the first glyph preceding glyph string S that
23324 is overwritten by S because of S's left overhang. Value is -1
23325 if no glyphs are overwritten. */
23326
23327 static int
23328 left_overwritten (struct glyph_string *s)
23329 {
23330 int k;
23331
23332 if (s->left_overhang)
23333 {
23334 int x = 0, i;
23335 struct glyph *glyphs = s->row->glyphs[s->area];
23336 int first = s->first_glyph - glyphs;
23337
23338 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
23339 x -= glyphs[i].pixel_width;
23340
23341 k = i + 1;
23342 }
23343 else
23344 k = -1;
23345
23346 return k;
23347 }
23348
23349
23350 /* Return the index of the first glyph preceding glyph string S that
23351 is overwriting S because of its right overhang. Value is -1 if no
23352 glyph in front of S overwrites S. */
23353
23354 static int
23355 left_overwriting (struct glyph_string *s)
23356 {
23357 int i, k, x;
23358 struct glyph *glyphs = s->row->glyphs[s->area];
23359 int first = s->first_glyph - glyphs;
23360
23361 k = -1;
23362 x = 0;
23363 for (i = first - 1; i >= 0; --i)
23364 {
23365 int left, right;
23366 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23367 if (x + right > 0)
23368 k = i;
23369 x -= glyphs[i].pixel_width;
23370 }
23371
23372 return k;
23373 }
23374
23375
23376 /* Return the index of the last glyph following glyph string S that is
23377 overwritten by S because of S's right overhang. Value is -1 if
23378 no such glyph is found. */
23379
23380 static int
23381 right_overwritten (struct glyph_string *s)
23382 {
23383 int k = -1;
23384
23385 if (s->right_overhang)
23386 {
23387 int x = 0, i;
23388 struct glyph *glyphs = s->row->glyphs[s->area];
23389 int first = (s->first_glyph - glyphs
23390 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23391 int end = s->row->used[s->area];
23392
23393 for (i = first; i < end && s->right_overhang > x; ++i)
23394 x += glyphs[i].pixel_width;
23395
23396 k = i;
23397 }
23398
23399 return k;
23400 }
23401
23402
23403 /* Return the index of the last glyph following glyph string S that
23404 overwrites S because of its left overhang. Value is negative
23405 if no such glyph is found. */
23406
23407 static int
23408 right_overwriting (struct glyph_string *s)
23409 {
23410 int i, k, x;
23411 int end = s->row->used[s->area];
23412 struct glyph *glyphs = s->row->glyphs[s->area];
23413 int first = (s->first_glyph - glyphs
23414 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23415
23416 k = -1;
23417 x = 0;
23418 for (i = first; i < end; ++i)
23419 {
23420 int left, right;
23421 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23422 if (x - left < 0)
23423 k = i;
23424 x += glyphs[i].pixel_width;
23425 }
23426
23427 return k;
23428 }
23429
23430
23431 /* Set background width of glyph string S. START is the index of the
23432 first glyph following S. LAST_X is the right-most x-position + 1
23433 in the drawing area. */
23434
23435 static void
23436 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23437 {
23438 /* If the face of this glyph string has to be drawn to the end of
23439 the drawing area, set S->extends_to_end_of_line_p. */
23440
23441 if (start == s->row->used[s->area]
23442 && s->area == TEXT_AREA
23443 && ((s->row->fill_line_p
23444 && (s->hl == DRAW_NORMAL_TEXT
23445 || s->hl == DRAW_IMAGE_RAISED
23446 || s->hl == DRAW_IMAGE_SUNKEN))
23447 || s->hl == DRAW_MOUSE_FACE))
23448 s->extends_to_end_of_line_p = 1;
23449
23450 /* If S extends its face to the end of the line, set its
23451 background_width to the distance to the right edge of the drawing
23452 area. */
23453 if (s->extends_to_end_of_line_p)
23454 s->background_width = last_x - s->x + 1;
23455 else
23456 s->background_width = s->width;
23457 }
23458
23459
23460 /* Compute overhangs and x-positions for glyph string S and its
23461 predecessors, or successors. X is the starting x-position for S.
23462 BACKWARD_P non-zero means process predecessors. */
23463
23464 static void
23465 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23466 {
23467 if (backward_p)
23468 {
23469 while (s)
23470 {
23471 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23472 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23473 x -= s->width;
23474 s->x = x;
23475 s = s->prev;
23476 }
23477 }
23478 else
23479 {
23480 while (s)
23481 {
23482 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23483 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23484 s->x = x;
23485 x += s->width;
23486 s = s->next;
23487 }
23488 }
23489 }
23490
23491
23492
23493 /* The following macros are only called from draw_glyphs below.
23494 They reference the following parameters of that function directly:
23495 `w', `row', `area', and `overlap_p'
23496 as well as the following local variables:
23497 `s', `f', and `hdc' (in W32) */
23498
23499 #ifdef HAVE_NTGUI
23500 /* On W32, silently add local `hdc' variable to argument list of
23501 init_glyph_string. */
23502 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23503 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23504 #else
23505 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23506 init_glyph_string (s, char2b, w, row, area, start, hl)
23507 #endif
23508
23509 /* Add a glyph string for a stretch glyph to the list of strings
23510 between HEAD and TAIL. START is the index of the stretch glyph in
23511 row area AREA of glyph row ROW. END is the index of the last glyph
23512 in that glyph row area. X is the current output position assigned
23513 to the new glyph string constructed. HL overrides that face of the
23514 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23515 is the right-most x-position of the drawing area. */
23516
23517 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23518 and below -- keep them on one line. */
23519 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23520 do \
23521 { \
23522 s = alloca (sizeof *s); \
23523 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23524 START = fill_stretch_glyph_string (s, START, END); \
23525 append_glyph_string (&HEAD, &TAIL, s); \
23526 s->x = (X); \
23527 } \
23528 while (0)
23529
23530
23531 /* Add a glyph string for an image glyph to the list of strings
23532 between HEAD and TAIL. START is the index of the image glyph in
23533 row area AREA of glyph row ROW. END is the index of the last glyph
23534 in that glyph row area. X is the current output position assigned
23535 to the new glyph string constructed. HL overrides that face of the
23536 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23537 is the right-most x-position of the drawing area. */
23538
23539 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23540 do \
23541 { \
23542 s = alloca (sizeof *s); \
23543 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23544 fill_image_glyph_string (s); \
23545 append_glyph_string (&HEAD, &TAIL, s); \
23546 ++START; \
23547 s->x = (X); \
23548 } \
23549 while (0)
23550
23551
23552 /* Add a glyph string for a sequence of character glyphs to the list
23553 of strings between HEAD and TAIL. START is the index of the first
23554 glyph in row area AREA of glyph row ROW that is part of the new
23555 glyph string. END is the index of the last glyph in that glyph row
23556 area. X is the current output position assigned to the new glyph
23557 string constructed. HL overrides that face of the glyph; e.g. it
23558 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23559 right-most x-position of the drawing area. */
23560
23561 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23562 do \
23563 { \
23564 int face_id; \
23565 XChar2b *char2b; \
23566 \
23567 face_id = (row)->glyphs[area][START].face_id; \
23568 \
23569 s = alloca (sizeof *s); \
23570 char2b = alloca ((END - START) * sizeof *char2b); \
23571 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23572 append_glyph_string (&HEAD, &TAIL, s); \
23573 s->x = (X); \
23574 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23575 } \
23576 while (0)
23577
23578
23579 /* Add a glyph string for a composite sequence to the list of strings
23580 between HEAD and TAIL. START is the index of the first glyph in
23581 row area AREA of glyph row ROW that is part of the new glyph
23582 string. END is the index of the last glyph in that glyph row area.
23583 X is the current output position assigned to the new glyph string
23584 constructed. HL overrides that face of the glyph; e.g. it is
23585 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23586 x-position of the drawing area. */
23587
23588 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23589 do { \
23590 int face_id = (row)->glyphs[area][START].face_id; \
23591 struct face *base_face = FACE_FROM_ID (f, face_id); \
23592 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23593 struct composition *cmp = composition_table[cmp_id]; \
23594 XChar2b *char2b; \
23595 struct glyph_string *first_s = NULL; \
23596 int n; \
23597 \
23598 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23599 \
23600 /* Make glyph_strings for each glyph sequence that is drawable by \
23601 the same face, and append them to HEAD/TAIL. */ \
23602 for (n = 0; n < cmp->glyph_len;) \
23603 { \
23604 s = alloca (sizeof *s); \
23605 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23606 append_glyph_string (&(HEAD), &(TAIL), s); \
23607 s->cmp = cmp; \
23608 s->cmp_from = n; \
23609 s->x = (X); \
23610 if (n == 0) \
23611 first_s = s; \
23612 n = fill_composite_glyph_string (s, base_face, overlaps); \
23613 } \
23614 \
23615 ++START; \
23616 s = first_s; \
23617 } while (0)
23618
23619
23620 /* Add a glyph string for a glyph-string sequence to the list of strings
23621 between HEAD and TAIL. */
23622
23623 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23624 do { \
23625 int face_id; \
23626 XChar2b *char2b; \
23627 Lisp_Object gstring; \
23628 \
23629 face_id = (row)->glyphs[area][START].face_id; \
23630 gstring = (composition_gstring_from_id \
23631 ((row)->glyphs[area][START].u.cmp.id)); \
23632 s = alloca (sizeof *s); \
23633 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23634 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23635 append_glyph_string (&(HEAD), &(TAIL), s); \
23636 s->x = (X); \
23637 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23638 } while (0)
23639
23640
23641 /* Add a glyph string for a sequence of glyphless character's glyphs
23642 to the list of strings between HEAD and TAIL. The meanings of
23643 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23644
23645 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23646 do \
23647 { \
23648 int face_id; \
23649 \
23650 face_id = (row)->glyphs[area][START].face_id; \
23651 \
23652 s = alloca (sizeof *s); \
23653 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23654 append_glyph_string (&HEAD, &TAIL, s); \
23655 s->x = (X); \
23656 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23657 overlaps); \
23658 } \
23659 while (0)
23660
23661
23662 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23663 of AREA of glyph row ROW on window W between indices START and END.
23664 HL overrides the face for drawing glyph strings, e.g. it is
23665 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23666 x-positions of the drawing area.
23667
23668 This is an ugly monster macro construct because we must use alloca
23669 to allocate glyph strings (because draw_glyphs can be called
23670 asynchronously). */
23671
23672 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23673 do \
23674 { \
23675 HEAD = TAIL = NULL; \
23676 while (START < END) \
23677 { \
23678 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23679 switch (first_glyph->type) \
23680 { \
23681 case CHAR_GLYPH: \
23682 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23683 HL, X, LAST_X); \
23684 break; \
23685 \
23686 case COMPOSITE_GLYPH: \
23687 if (first_glyph->u.cmp.automatic) \
23688 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23689 HL, X, LAST_X); \
23690 else \
23691 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23692 HL, X, LAST_X); \
23693 break; \
23694 \
23695 case STRETCH_GLYPH: \
23696 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23697 HL, X, LAST_X); \
23698 break; \
23699 \
23700 case IMAGE_GLYPH: \
23701 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23702 HL, X, LAST_X); \
23703 break; \
23704 \
23705 case GLYPHLESS_GLYPH: \
23706 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23707 HL, X, LAST_X); \
23708 break; \
23709 \
23710 default: \
23711 emacs_abort (); \
23712 } \
23713 \
23714 if (s) \
23715 { \
23716 set_glyph_string_background_width (s, START, LAST_X); \
23717 (X) += s->width; \
23718 } \
23719 } \
23720 } while (0)
23721
23722
23723 /* Draw glyphs between START and END in AREA of ROW on window W,
23724 starting at x-position X. X is relative to AREA in W. HL is a
23725 face-override with the following meaning:
23726
23727 DRAW_NORMAL_TEXT draw normally
23728 DRAW_CURSOR draw in cursor face
23729 DRAW_MOUSE_FACE draw in mouse face.
23730 DRAW_INVERSE_VIDEO draw in mode line face
23731 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23732 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23733
23734 If OVERLAPS is non-zero, draw only the foreground of characters and
23735 clip to the physical height of ROW. Non-zero value also defines
23736 the overlapping part to be drawn:
23737
23738 OVERLAPS_PRED overlap with preceding rows
23739 OVERLAPS_SUCC overlap with succeeding rows
23740 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23741 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23742
23743 Value is the x-position reached, relative to AREA of W. */
23744
23745 static int
23746 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23747 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23748 enum draw_glyphs_face hl, int overlaps)
23749 {
23750 struct glyph_string *head, *tail;
23751 struct glyph_string *s;
23752 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23753 int i, j, x_reached, last_x, area_left = 0;
23754 struct frame *f = XFRAME (WINDOW_FRAME (w));
23755 DECLARE_HDC (hdc);
23756
23757 ALLOCATE_HDC (hdc, f);
23758
23759 /* Let's rather be paranoid than getting a SEGV. */
23760 end = min (end, row->used[area]);
23761 start = clip_to_bounds (0, start, end);
23762
23763 /* Translate X to frame coordinates. Set last_x to the right
23764 end of the drawing area. */
23765 if (row->full_width_p)
23766 {
23767 /* X is relative to the left edge of W, without scroll bars
23768 or fringes. */
23769 area_left = WINDOW_LEFT_EDGE_X (w);
23770 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23771 }
23772 else
23773 {
23774 area_left = window_box_left (w, area);
23775 last_x = area_left + window_box_width (w, area);
23776 }
23777 x += area_left;
23778
23779 /* Build a doubly-linked list of glyph_string structures between
23780 head and tail from what we have to draw. Note that the macro
23781 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23782 the reason we use a separate variable `i'. */
23783 i = start;
23784 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23785 if (tail)
23786 x_reached = tail->x + tail->background_width;
23787 else
23788 x_reached = x;
23789
23790 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23791 the row, redraw some glyphs in front or following the glyph
23792 strings built above. */
23793 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23794 {
23795 struct glyph_string *h, *t;
23796 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23797 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23798 int check_mouse_face = 0;
23799 int dummy_x = 0;
23800
23801 /* If mouse highlighting is on, we may need to draw adjacent
23802 glyphs using mouse-face highlighting. */
23803 if (area == TEXT_AREA && row->mouse_face_p
23804 && hlinfo->mouse_face_beg_row >= 0
23805 && hlinfo->mouse_face_end_row >= 0)
23806 {
23807 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
23808
23809 if (row_vpos >= hlinfo->mouse_face_beg_row
23810 && row_vpos <= hlinfo->mouse_face_end_row)
23811 {
23812 check_mouse_face = 1;
23813 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
23814 ? hlinfo->mouse_face_beg_col : 0;
23815 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
23816 ? hlinfo->mouse_face_end_col
23817 : row->used[TEXT_AREA];
23818 }
23819 }
23820
23821 /* Compute overhangs for all glyph strings. */
23822 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23823 for (s = head; s; s = s->next)
23824 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23825
23826 /* Prepend glyph strings for glyphs in front of the first glyph
23827 string that are overwritten because of the first glyph
23828 string's left overhang. The background of all strings
23829 prepended must be drawn because the first glyph string
23830 draws over it. */
23831 i = left_overwritten (head);
23832 if (i >= 0)
23833 {
23834 enum draw_glyphs_face overlap_hl;
23835
23836 /* If this row contains mouse highlighting, attempt to draw
23837 the overlapped glyphs with the correct highlight. This
23838 code fails if the overlap encompasses more than one glyph
23839 and mouse-highlight spans only some of these glyphs.
23840 However, making it work perfectly involves a lot more
23841 code, and I don't know if the pathological case occurs in
23842 practice, so we'll stick to this for now. --- cyd */
23843 if (check_mouse_face
23844 && mouse_beg_col < start && mouse_end_col > i)
23845 overlap_hl = DRAW_MOUSE_FACE;
23846 else
23847 overlap_hl = DRAW_NORMAL_TEXT;
23848
23849 j = i;
23850 BUILD_GLYPH_STRINGS (j, start, h, t,
23851 overlap_hl, dummy_x, last_x);
23852 start = i;
23853 compute_overhangs_and_x (t, head->x, 1);
23854 prepend_glyph_string_lists (&head, &tail, h, t);
23855 clip_head = head;
23856 }
23857
23858 /* Prepend glyph strings for glyphs in front of the first glyph
23859 string that overwrite that glyph string because of their
23860 right overhang. For these strings, only the foreground must
23861 be drawn, because it draws over the glyph string at `head'.
23862 The background must not be drawn because this would overwrite
23863 right overhangs of preceding glyphs for which no glyph
23864 strings exist. */
23865 i = left_overwriting (head);
23866 if (i >= 0)
23867 {
23868 enum draw_glyphs_face overlap_hl;
23869
23870 if (check_mouse_face
23871 && mouse_beg_col < start && mouse_end_col > i)
23872 overlap_hl = DRAW_MOUSE_FACE;
23873 else
23874 overlap_hl = DRAW_NORMAL_TEXT;
23875
23876 clip_head = head;
23877 BUILD_GLYPH_STRINGS (i, start, h, t,
23878 overlap_hl, dummy_x, last_x);
23879 for (s = h; s; s = s->next)
23880 s->background_filled_p = 1;
23881 compute_overhangs_and_x (t, head->x, 1);
23882 prepend_glyph_string_lists (&head, &tail, h, t);
23883 }
23884
23885 /* Append glyphs strings for glyphs following the last glyph
23886 string tail that are overwritten by tail. The background of
23887 these strings has to be drawn because tail's foreground draws
23888 over it. */
23889 i = right_overwritten (tail);
23890 if (i >= 0)
23891 {
23892 enum draw_glyphs_face overlap_hl;
23893
23894 if (check_mouse_face
23895 && mouse_beg_col < i && mouse_end_col > end)
23896 overlap_hl = DRAW_MOUSE_FACE;
23897 else
23898 overlap_hl = DRAW_NORMAL_TEXT;
23899
23900 BUILD_GLYPH_STRINGS (end, i, h, t,
23901 overlap_hl, x, last_x);
23902 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23903 we don't have `end = i;' here. */
23904 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23905 append_glyph_string_lists (&head, &tail, h, t);
23906 clip_tail = tail;
23907 }
23908
23909 /* Append glyph strings for glyphs following the last glyph
23910 string tail that overwrite tail. The foreground of such
23911 glyphs has to be drawn because it writes into the background
23912 of tail. The background must not be drawn because it could
23913 paint over the foreground of following glyphs. */
23914 i = right_overwriting (tail);
23915 if (i >= 0)
23916 {
23917 enum draw_glyphs_face overlap_hl;
23918 if (check_mouse_face
23919 && mouse_beg_col < i && mouse_end_col > end)
23920 overlap_hl = DRAW_MOUSE_FACE;
23921 else
23922 overlap_hl = DRAW_NORMAL_TEXT;
23923
23924 clip_tail = tail;
23925 i++; /* We must include the Ith glyph. */
23926 BUILD_GLYPH_STRINGS (end, i, h, t,
23927 overlap_hl, x, last_x);
23928 for (s = h; s; s = s->next)
23929 s->background_filled_p = 1;
23930 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23931 append_glyph_string_lists (&head, &tail, h, t);
23932 }
23933 if (clip_head || clip_tail)
23934 for (s = head; s; s = s->next)
23935 {
23936 s->clip_head = clip_head;
23937 s->clip_tail = clip_tail;
23938 }
23939 }
23940
23941 /* Draw all strings. */
23942 for (s = head; s; s = s->next)
23943 FRAME_RIF (f)->draw_glyph_string (s);
23944
23945 #ifndef HAVE_NS
23946 /* When focus a sole frame and move horizontally, this sets on_p to 0
23947 causing a failure to erase prev cursor position. */
23948 if (area == TEXT_AREA
23949 && !row->full_width_p
23950 /* When drawing overlapping rows, only the glyph strings'
23951 foreground is drawn, which doesn't erase a cursor
23952 completely. */
23953 && !overlaps)
23954 {
23955 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23956 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23957 : (tail ? tail->x + tail->background_width : x));
23958 x0 -= area_left;
23959 x1 -= area_left;
23960
23961 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23962 row->y, MATRIX_ROW_BOTTOM_Y (row));
23963 }
23964 #endif
23965
23966 /* Value is the x-position up to which drawn, relative to AREA of W.
23967 This doesn't include parts drawn because of overhangs. */
23968 if (row->full_width_p)
23969 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23970 else
23971 x_reached -= area_left;
23972
23973 RELEASE_HDC (hdc, f);
23974
23975 return x_reached;
23976 }
23977
23978 /* Expand row matrix if too narrow. Don't expand if area
23979 is not present. */
23980
23981 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
23982 { \
23983 if (!fonts_changed_p \
23984 && (it->glyph_row->glyphs[area] \
23985 < it->glyph_row->glyphs[area + 1])) \
23986 { \
23987 it->w->ncols_scale_factor++; \
23988 fonts_changed_p = 1; \
23989 } \
23990 }
23991
23992 /* Store one glyph for IT->char_to_display in IT->glyph_row.
23993 Called from x_produce_glyphs when IT->glyph_row is non-null. */
23994
23995 static void
23996 append_glyph (struct it *it)
23997 {
23998 struct glyph *glyph;
23999 enum glyph_row_area area = it->area;
24000
24001 eassert (it->glyph_row);
24002 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
24003
24004 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24005 if (glyph < it->glyph_row->glyphs[area + 1])
24006 {
24007 /* If the glyph row is reversed, we need to prepend the glyph
24008 rather than append it. */
24009 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24010 {
24011 struct glyph *g;
24012
24013 /* Make room for the additional glyph. */
24014 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24015 g[1] = *g;
24016 glyph = it->glyph_row->glyphs[area];
24017 }
24018 glyph->charpos = CHARPOS (it->position);
24019 glyph->object = it->object;
24020 if (it->pixel_width > 0)
24021 {
24022 glyph->pixel_width = it->pixel_width;
24023 glyph->padding_p = 0;
24024 }
24025 else
24026 {
24027 /* Assure at least 1-pixel width. Otherwise, cursor can't
24028 be displayed correctly. */
24029 glyph->pixel_width = 1;
24030 glyph->padding_p = 1;
24031 }
24032 glyph->ascent = it->ascent;
24033 glyph->descent = it->descent;
24034 glyph->voffset = it->voffset;
24035 glyph->type = CHAR_GLYPH;
24036 glyph->avoid_cursor_p = it->avoid_cursor_p;
24037 glyph->multibyte_p = it->multibyte_p;
24038 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24039 {
24040 /* In R2L rows, the left and the right box edges need to be
24041 drawn in reverse direction. */
24042 glyph->right_box_line_p = it->start_of_box_run_p;
24043 glyph->left_box_line_p = it->end_of_box_run_p;
24044 }
24045 else
24046 {
24047 glyph->left_box_line_p = it->start_of_box_run_p;
24048 glyph->right_box_line_p = it->end_of_box_run_p;
24049 }
24050 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24051 || it->phys_descent > it->descent);
24052 glyph->glyph_not_available_p = it->glyph_not_available_p;
24053 glyph->face_id = it->face_id;
24054 glyph->u.ch = it->char_to_display;
24055 glyph->slice.img = null_glyph_slice;
24056 glyph->font_type = FONT_TYPE_UNKNOWN;
24057 if (it->bidi_p)
24058 {
24059 glyph->resolved_level = it->bidi_it.resolved_level;
24060 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24061 emacs_abort ();
24062 glyph->bidi_type = it->bidi_it.type;
24063 }
24064 else
24065 {
24066 glyph->resolved_level = 0;
24067 glyph->bidi_type = UNKNOWN_BT;
24068 }
24069 ++it->glyph_row->used[area];
24070 }
24071 else
24072 IT_EXPAND_MATRIX_WIDTH (it, area);
24073 }
24074
24075 /* Store one glyph for the composition IT->cmp_it.id in
24076 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
24077 non-null. */
24078
24079 static void
24080 append_composite_glyph (struct it *it)
24081 {
24082 struct glyph *glyph;
24083 enum glyph_row_area area = it->area;
24084
24085 eassert (it->glyph_row);
24086
24087 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24088 if (glyph < it->glyph_row->glyphs[area + 1])
24089 {
24090 /* If the glyph row is reversed, we need to prepend the glyph
24091 rather than append it. */
24092 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
24093 {
24094 struct glyph *g;
24095
24096 /* Make room for the new glyph. */
24097 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
24098 g[1] = *g;
24099 glyph = it->glyph_row->glyphs[it->area];
24100 }
24101 glyph->charpos = it->cmp_it.charpos;
24102 glyph->object = it->object;
24103 glyph->pixel_width = it->pixel_width;
24104 glyph->ascent = it->ascent;
24105 glyph->descent = it->descent;
24106 glyph->voffset = it->voffset;
24107 glyph->type = COMPOSITE_GLYPH;
24108 if (it->cmp_it.ch < 0)
24109 {
24110 glyph->u.cmp.automatic = 0;
24111 glyph->u.cmp.id = it->cmp_it.id;
24112 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
24113 }
24114 else
24115 {
24116 glyph->u.cmp.automatic = 1;
24117 glyph->u.cmp.id = it->cmp_it.id;
24118 glyph->slice.cmp.from = it->cmp_it.from;
24119 glyph->slice.cmp.to = it->cmp_it.to - 1;
24120 }
24121 glyph->avoid_cursor_p = it->avoid_cursor_p;
24122 glyph->multibyte_p = it->multibyte_p;
24123 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24124 {
24125 /* In R2L rows, the left and the right box edges need to be
24126 drawn in reverse direction. */
24127 glyph->right_box_line_p = it->start_of_box_run_p;
24128 glyph->left_box_line_p = it->end_of_box_run_p;
24129 }
24130 else
24131 {
24132 glyph->left_box_line_p = it->start_of_box_run_p;
24133 glyph->right_box_line_p = it->end_of_box_run_p;
24134 }
24135 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24136 || it->phys_descent > it->descent);
24137 glyph->padding_p = 0;
24138 glyph->glyph_not_available_p = 0;
24139 glyph->face_id = it->face_id;
24140 glyph->font_type = FONT_TYPE_UNKNOWN;
24141 if (it->bidi_p)
24142 {
24143 glyph->resolved_level = it->bidi_it.resolved_level;
24144 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24145 emacs_abort ();
24146 glyph->bidi_type = it->bidi_it.type;
24147 }
24148 ++it->glyph_row->used[area];
24149 }
24150 else
24151 IT_EXPAND_MATRIX_WIDTH (it, area);
24152 }
24153
24154
24155 /* Change IT->ascent and IT->height according to the setting of
24156 IT->voffset. */
24157
24158 static void
24159 take_vertical_position_into_account (struct it *it)
24160 {
24161 if (it->voffset)
24162 {
24163 if (it->voffset < 0)
24164 /* Increase the ascent so that we can display the text higher
24165 in the line. */
24166 it->ascent -= it->voffset;
24167 else
24168 /* Increase the descent so that we can display the text lower
24169 in the line. */
24170 it->descent += it->voffset;
24171 }
24172 }
24173
24174
24175 /* Produce glyphs/get display metrics for the image IT is loaded with.
24176 See the description of struct display_iterator in dispextern.h for
24177 an overview of struct display_iterator. */
24178
24179 static void
24180 produce_image_glyph (struct it *it)
24181 {
24182 struct image *img;
24183 struct face *face;
24184 int glyph_ascent, crop;
24185 struct glyph_slice slice;
24186
24187 eassert (it->what == IT_IMAGE);
24188
24189 face = FACE_FROM_ID (it->f, it->face_id);
24190 eassert (face);
24191 /* Make sure X resources of the face is loaded. */
24192 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24193
24194 if (it->image_id < 0)
24195 {
24196 /* Fringe bitmap. */
24197 it->ascent = it->phys_ascent = 0;
24198 it->descent = it->phys_descent = 0;
24199 it->pixel_width = 0;
24200 it->nglyphs = 0;
24201 return;
24202 }
24203
24204 img = IMAGE_FROM_ID (it->f, it->image_id);
24205 eassert (img);
24206 /* Make sure X resources of the image is loaded. */
24207 prepare_image_for_display (it->f, img);
24208
24209 slice.x = slice.y = 0;
24210 slice.width = img->width;
24211 slice.height = img->height;
24212
24213 if (INTEGERP (it->slice.x))
24214 slice.x = XINT (it->slice.x);
24215 else if (FLOATP (it->slice.x))
24216 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
24217
24218 if (INTEGERP (it->slice.y))
24219 slice.y = XINT (it->slice.y);
24220 else if (FLOATP (it->slice.y))
24221 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
24222
24223 if (INTEGERP (it->slice.width))
24224 slice.width = XINT (it->slice.width);
24225 else if (FLOATP (it->slice.width))
24226 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
24227
24228 if (INTEGERP (it->slice.height))
24229 slice.height = XINT (it->slice.height);
24230 else if (FLOATP (it->slice.height))
24231 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
24232
24233 if (slice.x >= img->width)
24234 slice.x = img->width;
24235 if (slice.y >= img->height)
24236 slice.y = img->height;
24237 if (slice.x + slice.width >= img->width)
24238 slice.width = img->width - slice.x;
24239 if (slice.y + slice.height > img->height)
24240 slice.height = img->height - slice.y;
24241
24242 if (slice.width == 0 || slice.height == 0)
24243 return;
24244
24245 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
24246
24247 it->descent = slice.height - glyph_ascent;
24248 if (slice.y == 0)
24249 it->descent += img->vmargin;
24250 if (slice.y + slice.height == img->height)
24251 it->descent += img->vmargin;
24252 it->phys_descent = it->descent;
24253
24254 it->pixel_width = slice.width;
24255 if (slice.x == 0)
24256 it->pixel_width += img->hmargin;
24257 if (slice.x + slice.width == img->width)
24258 it->pixel_width += img->hmargin;
24259
24260 /* It's quite possible for images to have an ascent greater than
24261 their height, so don't get confused in that case. */
24262 if (it->descent < 0)
24263 it->descent = 0;
24264
24265 it->nglyphs = 1;
24266
24267 if (face->box != FACE_NO_BOX)
24268 {
24269 if (face->box_line_width > 0)
24270 {
24271 if (slice.y == 0)
24272 it->ascent += face->box_line_width;
24273 if (slice.y + slice.height == img->height)
24274 it->descent += face->box_line_width;
24275 }
24276
24277 if (it->start_of_box_run_p && slice.x == 0)
24278 it->pixel_width += eabs (face->box_line_width);
24279 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
24280 it->pixel_width += eabs (face->box_line_width);
24281 }
24282
24283 take_vertical_position_into_account (it);
24284
24285 /* Automatically crop wide image glyphs at right edge so we can
24286 draw the cursor on same display row. */
24287 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
24288 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
24289 {
24290 it->pixel_width -= crop;
24291 slice.width -= crop;
24292 }
24293
24294 if (it->glyph_row)
24295 {
24296 struct glyph *glyph;
24297 enum glyph_row_area area = it->area;
24298
24299 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24300 if (glyph < it->glyph_row->glyphs[area + 1])
24301 {
24302 glyph->charpos = CHARPOS (it->position);
24303 glyph->object = it->object;
24304 glyph->pixel_width = it->pixel_width;
24305 glyph->ascent = glyph_ascent;
24306 glyph->descent = it->descent;
24307 glyph->voffset = it->voffset;
24308 glyph->type = IMAGE_GLYPH;
24309 glyph->avoid_cursor_p = it->avoid_cursor_p;
24310 glyph->multibyte_p = it->multibyte_p;
24311 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24312 {
24313 /* In R2L rows, the left and the right box edges need to be
24314 drawn in reverse direction. */
24315 glyph->right_box_line_p = it->start_of_box_run_p;
24316 glyph->left_box_line_p = it->end_of_box_run_p;
24317 }
24318 else
24319 {
24320 glyph->left_box_line_p = it->start_of_box_run_p;
24321 glyph->right_box_line_p = it->end_of_box_run_p;
24322 }
24323 glyph->overlaps_vertically_p = 0;
24324 glyph->padding_p = 0;
24325 glyph->glyph_not_available_p = 0;
24326 glyph->face_id = it->face_id;
24327 glyph->u.img_id = img->id;
24328 glyph->slice.img = slice;
24329 glyph->font_type = FONT_TYPE_UNKNOWN;
24330 if (it->bidi_p)
24331 {
24332 glyph->resolved_level = it->bidi_it.resolved_level;
24333 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24334 emacs_abort ();
24335 glyph->bidi_type = it->bidi_it.type;
24336 }
24337 ++it->glyph_row->used[area];
24338 }
24339 else
24340 IT_EXPAND_MATRIX_WIDTH (it, area);
24341 }
24342 }
24343
24344
24345 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
24346 of the glyph, WIDTH and HEIGHT are the width and height of the
24347 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
24348
24349 static void
24350 append_stretch_glyph (struct it *it, Lisp_Object object,
24351 int width, int height, int ascent)
24352 {
24353 struct glyph *glyph;
24354 enum glyph_row_area area = it->area;
24355
24356 eassert (ascent >= 0 && ascent <= height);
24357
24358 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24359 if (glyph < it->glyph_row->glyphs[area + 1])
24360 {
24361 /* If the glyph row is reversed, we need to prepend the glyph
24362 rather than append it. */
24363 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24364 {
24365 struct glyph *g;
24366
24367 /* Make room for the additional glyph. */
24368 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24369 g[1] = *g;
24370 glyph = it->glyph_row->glyphs[area];
24371 }
24372 glyph->charpos = CHARPOS (it->position);
24373 glyph->object = object;
24374 glyph->pixel_width = width;
24375 glyph->ascent = ascent;
24376 glyph->descent = height - ascent;
24377 glyph->voffset = it->voffset;
24378 glyph->type = STRETCH_GLYPH;
24379 glyph->avoid_cursor_p = it->avoid_cursor_p;
24380 glyph->multibyte_p = it->multibyte_p;
24381 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24382 {
24383 /* In R2L rows, the left and the right box edges need to be
24384 drawn in reverse direction. */
24385 glyph->right_box_line_p = it->start_of_box_run_p;
24386 glyph->left_box_line_p = it->end_of_box_run_p;
24387 }
24388 else
24389 {
24390 glyph->left_box_line_p = it->start_of_box_run_p;
24391 glyph->right_box_line_p = it->end_of_box_run_p;
24392 }
24393 glyph->overlaps_vertically_p = 0;
24394 glyph->padding_p = 0;
24395 glyph->glyph_not_available_p = 0;
24396 glyph->face_id = it->face_id;
24397 glyph->u.stretch.ascent = ascent;
24398 glyph->u.stretch.height = height;
24399 glyph->slice.img = null_glyph_slice;
24400 glyph->font_type = FONT_TYPE_UNKNOWN;
24401 if (it->bidi_p)
24402 {
24403 glyph->resolved_level = it->bidi_it.resolved_level;
24404 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24405 emacs_abort ();
24406 glyph->bidi_type = it->bidi_it.type;
24407 }
24408 else
24409 {
24410 glyph->resolved_level = 0;
24411 glyph->bidi_type = UNKNOWN_BT;
24412 }
24413 ++it->glyph_row->used[area];
24414 }
24415 else
24416 IT_EXPAND_MATRIX_WIDTH (it, area);
24417 }
24418
24419 #endif /* HAVE_WINDOW_SYSTEM */
24420
24421 /* Produce a stretch glyph for iterator IT. IT->object is the value
24422 of the glyph property displayed. The value must be a list
24423 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24424 being recognized:
24425
24426 1. `:width WIDTH' specifies that the space should be WIDTH *
24427 canonical char width wide. WIDTH may be an integer or floating
24428 point number.
24429
24430 2. `:relative-width FACTOR' specifies that the width of the stretch
24431 should be computed from the width of the first character having the
24432 `glyph' property, and should be FACTOR times that width.
24433
24434 3. `:align-to HPOS' specifies that the space should be wide enough
24435 to reach HPOS, a value in canonical character units.
24436
24437 Exactly one of the above pairs must be present.
24438
24439 4. `:height HEIGHT' specifies that the height of the stretch produced
24440 should be HEIGHT, measured in canonical character units.
24441
24442 5. `:relative-height FACTOR' specifies that the height of the
24443 stretch should be FACTOR times the height of the characters having
24444 the glyph property.
24445
24446 Either none or exactly one of 4 or 5 must be present.
24447
24448 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24449 of the stretch should be used for the ascent of the stretch.
24450 ASCENT must be in the range 0 <= ASCENT <= 100. */
24451
24452 void
24453 produce_stretch_glyph (struct it *it)
24454 {
24455 /* (space :width WIDTH :height HEIGHT ...) */
24456 Lisp_Object prop, plist;
24457 int width = 0, height = 0, align_to = -1;
24458 int zero_width_ok_p = 0;
24459 double tem;
24460 struct font *font = NULL;
24461
24462 #ifdef HAVE_WINDOW_SYSTEM
24463 int ascent = 0;
24464 int zero_height_ok_p = 0;
24465
24466 if (FRAME_WINDOW_P (it->f))
24467 {
24468 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24469 font = face->font ? face->font : FRAME_FONT (it->f);
24470 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24471 }
24472 #endif
24473
24474 /* List should start with `space'. */
24475 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24476 plist = XCDR (it->object);
24477
24478 /* Compute the width of the stretch. */
24479 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24480 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24481 {
24482 /* Absolute width `:width WIDTH' specified and valid. */
24483 zero_width_ok_p = 1;
24484 width = (int)tem;
24485 }
24486 #ifdef HAVE_WINDOW_SYSTEM
24487 else if (FRAME_WINDOW_P (it->f)
24488 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24489 {
24490 /* Relative width `:relative-width FACTOR' specified and valid.
24491 Compute the width of the characters having the `glyph'
24492 property. */
24493 struct it it2;
24494 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24495
24496 it2 = *it;
24497 if (it->multibyte_p)
24498 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24499 else
24500 {
24501 it2.c = it2.char_to_display = *p, it2.len = 1;
24502 if (! ASCII_CHAR_P (it2.c))
24503 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24504 }
24505
24506 it2.glyph_row = NULL;
24507 it2.what = IT_CHARACTER;
24508 x_produce_glyphs (&it2);
24509 width = NUMVAL (prop) * it2.pixel_width;
24510 }
24511 #endif /* HAVE_WINDOW_SYSTEM */
24512 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24513 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24514 {
24515 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24516 align_to = (align_to < 0
24517 ? 0
24518 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24519 else if (align_to < 0)
24520 align_to = window_box_left_offset (it->w, TEXT_AREA);
24521 width = max (0, (int)tem + align_to - it->current_x);
24522 zero_width_ok_p = 1;
24523 }
24524 else
24525 /* Nothing specified -> width defaults to canonical char width. */
24526 width = FRAME_COLUMN_WIDTH (it->f);
24527
24528 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24529 width = 1;
24530
24531 #ifdef HAVE_WINDOW_SYSTEM
24532 /* Compute height. */
24533 if (FRAME_WINDOW_P (it->f))
24534 {
24535 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24536 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24537 {
24538 height = (int)tem;
24539 zero_height_ok_p = 1;
24540 }
24541 else if (prop = Fplist_get (plist, QCrelative_height),
24542 NUMVAL (prop) > 0)
24543 height = FONT_HEIGHT (font) * NUMVAL (prop);
24544 else
24545 height = FONT_HEIGHT (font);
24546
24547 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24548 height = 1;
24549
24550 /* Compute percentage of height used for ascent. If
24551 `:ascent ASCENT' is present and valid, use that. Otherwise,
24552 derive the ascent from the font in use. */
24553 if (prop = Fplist_get (plist, QCascent),
24554 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24555 ascent = height * NUMVAL (prop) / 100.0;
24556 else if (!NILP (prop)
24557 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24558 ascent = min (max (0, (int)tem), height);
24559 else
24560 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24561 }
24562 else
24563 #endif /* HAVE_WINDOW_SYSTEM */
24564 height = 1;
24565
24566 if (width > 0 && it->line_wrap != TRUNCATE
24567 && it->current_x + width > it->last_visible_x)
24568 {
24569 width = it->last_visible_x - it->current_x;
24570 #ifdef HAVE_WINDOW_SYSTEM
24571 /* Subtract one more pixel from the stretch width, but only on
24572 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24573 width -= FRAME_WINDOW_P (it->f);
24574 #endif
24575 }
24576
24577 if (width > 0 && height > 0 && it->glyph_row)
24578 {
24579 Lisp_Object o_object = it->object;
24580 Lisp_Object object = it->stack[it->sp - 1].string;
24581 int n = width;
24582
24583 if (!STRINGP (object))
24584 object = it->w->contents;
24585 #ifdef HAVE_WINDOW_SYSTEM
24586 if (FRAME_WINDOW_P (it->f))
24587 append_stretch_glyph (it, object, width, height, ascent);
24588 else
24589 #endif
24590 {
24591 it->object = object;
24592 it->char_to_display = ' ';
24593 it->pixel_width = it->len = 1;
24594 while (n--)
24595 tty_append_glyph (it);
24596 it->object = o_object;
24597 }
24598 }
24599
24600 it->pixel_width = width;
24601 #ifdef HAVE_WINDOW_SYSTEM
24602 if (FRAME_WINDOW_P (it->f))
24603 {
24604 it->ascent = it->phys_ascent = ascent;
24605 it->descent = it->phys_descent = height - it->ascent;
24606 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24607 take_vertical_position_into_account (it);
24608 }
24609 else
24610 #endif
24611 it->nglyphs = width;
24612 }
24613
24614 /* Get information about special display element WHAT in an
24615 environment described by IT. WHAT is one of IT_TRUNCATION or
24616 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24617 non-null glyph_row member. This function ensures that fields like
24618 face_id, c, len of IT are left untouched. */
24619
24620 static void
24621 produce_special_glyphs (struct it *it, enum display_element_type what)
24622 {
24623 struct it temp_it;
24624 Lisp_Object gc;
24625 GLYPH glyph;
24626
24627 temp_it = *it;
24628 temp_it.object = make_number (0);
24629 memset (&temp_it.current, 0, sizeof temp_it.current);
24630
24631 if (what == IT_CONTINUATION)
24632 {
24633 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24634 if (it->bidi_it.paragraph_dir == R2L)
24635 SET_GLYPH_FROM_CHAR (glyph, '/');
24636 else
24637 SET_GLYPH_FROM_CHAR (glyph, '\\');
24638 if (it->dp
24639 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24640 {
24641 /* FIXME: Should we mirror GC for R2L lines? */
24642 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24643 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24644 }
24645 }
24646 else if (what == IT_TRUNCATION)
24647 {
24648 /* Truncation glyph. */
24649 SET_GLYPH_FROM_CHAR (glyph, '$');
24650 if (it->dp
24651 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24652 {
24653 /* FIXME: Should we mirror GC for R2L lines? */
24654 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24655 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24656 }
24657 }
24658 else
24659 emacs_abort ();
24660
24661 #ifdef HAVE_WINDOW_SYSTEM
24662 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24663 is turned off, we precede the truncation/continuation glyphs by a
24664 stretch glyph whose width is computed such that these special
24665 glyphs are aligned at the window margin, even when very different
24666 fonts are used in different glyph rows. */
24667 if (FRAME_WINDOW_P (temp_it.f)
24668 /* init_iterator calls this with it->glyph_row == NULL, and it
24669 wants only the pixel width of the truncation/continuation
24670 glyphs. */
24671 && temp_it.glyph_row
24672 /* insert_left_trunc_glyphs calls us at the beginning of the
24673 row, and it has its own calculation of the stretch glyph
24674 width. */
24675 && temp_it.glyph_row->used[TEXT_AREA] > 0
24676 && (temp_it.glyph_row->reversed_p
24677 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24678 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24679 {
24680 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24681
24682 if (stretch_width > 0)
24683 {
24684 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24685 struct font *font =
24686 face->font ? face->font : FRAME_FONT (temp_it.f);
24687 int stretch_ascent =
24688 (((temp_it.ascent + temp_it.descent)
24689 * FONT_BASE (font)) / FONT_HEIGHT (font));
24690
24691 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
24692 temp_it.ascent + temp_it.descent,
24693 stretch_ascent);
24694 }
24695 }
24696 #endif
24697
24698 temp_it.dp = NULL;
24699 temp_it.what = IT_CHARACTER;
24700 temp_it.len = 1;
24701 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24702 temp_it.face_id = GLYPH_FACE (glyph);
24703 temp_it.len = CHAR_BYTES (temp_it.c);
24704
24705 PRODUCE_GLYPHS (&temp_it);
24706 it->pixel_width = temp_it.pixel_width;
24707 it->nglyphs = temp_it.pixel_width;
24708 }
24709
24710 #ifdef HAVE_WINDOW_SYSTEM
24711
24712 /* Calculate line-height and line-spacing properties.
24713 An integer value specifies explicit pixel value.
24714 A float value specifies relative value to current face height.
24715 A cons (float . face-name) specifies relative value to
24716 height of specified face font.
24717
24718 Returns height in pixels, or nil. */
24719
24720
24721 static Lisp_Object
24722 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24723 int boff, int override)
24724 {
24725 Lisp_Object face_name = Qnil;
24726 int ascent, descent, height;
24727
24728 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24729 return val;
24730
24731 if (CONSP (val))
24732 {
24733 face_name = XCAR (val);
24734 val = XCDR (val);
24735 if (!NUMBERP (val))
24736 val = make_number (1);
24737 if (NILP (face_name))
24738 {
24739 height = it->ascent + it->descent;
24740 goto scale;
24741 }
24742 }
24743
24744 if (NILP (face_name))
24745 {
24746 font = FRAME_FONT (it->f);
24747 boff = FRAME_BASELINE_OFFSET (it->f);
24748 }
24749 else if (EQ (face_name, Qt))
24750 {
24751 override = 0;
24752 }
24753 else
24754 {
24755 int face_id;
24756 struct face *face;
24757
24758 face_id = lookup_named_face (it->f, face_name, 0);
24759 if (face_id < 0)
24760 return make_number (-1);
24761
24762 face = FACE_FROM_ID (it->f, face_id);
24763 font = face->font;
24764 if (font == NULL)
24765 return make_number (-1);
24766 boff = font->baseline_offset;
24767 if (font->vertical_centering)
24768 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24769 }
24770
24771 ascent = FONT_BASE (font) + boff;
24772 descent = FONT_DESCENT (font) - boff;
24773
24774 if (override)
24775 {
24776 it->override_ascent = ascent;
24777 it->override_descent = descent;
24778 it->override_boff = boff;
24779 }
24780
24781 height = ascent + descent;
24782
24783 scale:
24784 if (FLOATP (val))
24785 height = (int)(XFLOAT_DATA (val) * height);
24786 else if (INTEGERP (val))
24787 height *= XINT (val);
24788
24789 return make_number (height);
24790 }
24791
24792
24793 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24794 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24795 and only if this is for a character for which no font was found.
24796
24797 If the display method (it->glyphless_method) is
24798 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24799 length of the acronym or the hexadecimal string, UPPER_XOFF and
24800 UPPER_YOFF are pixel offsets for the upper part of the string,
24801 LOWER_XOFF and LOWER_YOFF are for the lower part.
24802
24803 For the other display methods, LEN through LOWER_YOFF are zero. */
24804
24805 static void
24806 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24807 short upper_xoff, short upper_yoff,
24808 short lower_xoff, short lower_yoff)
24809 {
24810 struct glyph *glyph;
24811 enum glyph_row_area area = it->area;
24812
24813 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24814 if (glyph < it->glyph_row->glyphs[area + 1])
24815 {
24816 /* If the glyph row is reversed, we need to prepend the glyph
24817 rather than append it. */
24818 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24819 {
24820 struct glyph *g;
24821
24822 /* Make room for the additional glyph. */
24823 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24824 g[1] = *g;
24825 glyph = it->glyph_row->glyphs[area];
24826 }
24827 glyph->charpos = CHARPOS (it->position);
24828 glyph->object = it->object;
24829 glyph->pixel_width = it->pixel_width;
24830 glyph->ascent = it->ascent;
24831 glyph->descent = it->descent;
24832 glyph->voffset = it->voffset;
24833 glyph->type = GLYPHLESS_GLYPH;
24834 glyph->u.glyphless.method = it->glyphless_method;
24835 glyph->u.glyphless.for_no_font = for_no_font;
24836 glyph->u.glyphless.len = len;
24837 glyph->u.glyphless.ch = it->c;
24838 glyph->slice.glyphless.upper_xoff = upper_xoff;
24839 glyph->slice.glyphless.upper_yoff = upper_yoff;
24840 glyph->slice.glyphless.lower_xoff = lower_xoff;
24841 glyph->slice.glyphless.lower_yoff = lower_yoff;
24842 glyph->avoid_cursor_p = it->avoid_cursor_p;
24843 glyph->multibyte_p = it->multibyte_p;
24844 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24845 {
24846 /* In R2L rows, the left and the right box edges need to be
24847 drawn in reverse direction. */
24848 glyph->right_box_line_p = it->start_of_box_run_p;
24849 glyph->left_box_line_p = it->end_of_box_run_p;
24850 }
24851 else
24852 {
24853 glyph->left_box_line_p = it->start_of_box_run_p;
24854 glyph->right_box_line_p = it->end_of_box_run_p;
24855 }
24856 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24857 || it->phys_descent > it->descent);
24858 glyph->padding_p = 0;
24859 glyph->glyph_not_available_p = 0;
24860 glyph->face_id = face_id;
24861 glyph->font_type = FONT_TYPE_UNKNOWN;
24862 if (it->bidi_p)
24863 {
24864 glyph->resolved_level = it->bidi_it.resolved_level;
24865 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24866 emacs_abort ();
24867 glyph->bidi_type = it->bidi_it.type;
24868 }
24869 ++it->glyph_row->used[area];
24870 }
24871 else
24872 IT_EXPAND_MATRIX_WIDTH (it, area);
24873 }
24874
24875
24876 /* Produce a glyph for a glyphless character for iterator IT.
24877 IT->glyphless_method specifies which method to use for displaying
24878 the character. See the description of enum
24879 glyphless_display_method in dispextern.h for the detail.
24880
24881 FOR_NO_FONT is nonzero if and only if this is for a character for
24882 which no font was found. ACRONYM, if non-nil, is an acronym string
24883 for the character. */
24884
24885 static void
24886 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24887 {
24888 int face_id;
24889 struct face *face;
24890 struct font *font;
24891 int base_width, base_height, width, height;
24892 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24893 int len;
24894
24895 /* Get the metrics of the base font. We always refer to the current
24896 ASCII face. */
24897 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24898 font = face->font ? face->font : FRAME_FONT (it->f);
24899 it->ascent = FONT_BASE (font) + font->baseline_offset;
24900 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24901 base_height = it->ascent + it->descent;
24902 base_width = font->average_width;
24903
24904 /* Get a face ID for the glyph by utilizing a cache (the same way as
24905 done for `escape-glyph' in get_next_display_element). */
24906 if (it->f == last_glyphless_glyph_frame
24907 && it->face_id == last_glyphless_glyph_face_id)
24908 {
24909 face_id = last_glyphless_glyph_merged_face_id;
24910 }
24911 else
24912 {
24913 /* Merge the `glyphless-char' face into the current face. */
24914 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
24915 last_glyphless_glyph_frame = it->f;
24916 last_glyphless_glyph_face_id = it->face_id;
24917 last_glyphless_glyph_merged_face_id = face_id;
24918 }
24919
24920 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24921 {
24922 it->pixel_width = THIN_SPACE_WIDTH;
24923 len = 0;
24924 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24925 }
24926 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24927 {
24928 width = CHAR_WIDTH (it->c);
24929 if (width == 0)
24930 width = 1;
24931 else if (width > 4)
24932 width = 4;
24933 it->pixel_width = base_width * width;
24934 len = 0;
24935 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24936 }
24937 else
24938 {
24939 char buf[7];
24940 const char *str;
24941 unsigned int code[6];
24942 int upper_len;
24943 int ascent, descent;
24944 struct font_metrics metrics_upper, metrics_lower;
24945
24946 face = FACE_FROM_ID (it->f, face_id);
24947 font = face->font ? face->font : FRAME_FONT (it->f);
24948 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24949
24950 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24951 {
24952 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
24953 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
24954 if (CONSP (acronym))
24955 acronym = XCAR (acronym);
24956 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24957 }
24958 else
24959 {
24960 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
24961 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
24962 str = buf;
24963 }
24964 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
24965 code[len] = font->driver->encode_char (font, str[len]);
24966 upper_len = (len + 1) / 2;
24967 font->driver->text_extents (font, code, upper_len,
24968 &metrics_upper);
24969 font->driver->text_extents (font, code + upper_len, len - upper_len,
24970 &metrics_lower);
24971
24972
24973
24974 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
24975 width = max (metrics_upper.width, metrics_lower.width) + 4;
24976 upper_xoff = upper_yoff = 2; /* the typical case */
24977 if (base_width >= width)
24978 {
24979 /* Align the upper to the left, the lower to the right. */
24980 it->pixel_width = base_width;
24981 lower_xoff = base_width - 2 - metrics_lower.width;
24982 }
24983 else
24984 {
24985 /* Center the shorter one. */
24986 it->pixel_width = width;
24987 if (metrics_upper.width >= metrics_lower.width)
24988 lower_xoff = (width - metrics_lower.width) / 2;
24989 else
24990 {
24991 /* FIXME: This code doesn't look right. It formerly was
24992 missing the "lower_xoff = 0;", which couldn't have
24993 been right since it left lower_xoff uninitialized. */
24994 lower_xoff = 0;
24995 upper_xoff = (width - metrics_upper.width) / 2;
24996 }
24997 }
24998
24999 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
25000 top, bottom, and between upper and lower strings. */
25001 height = (metrics_upper.ascent + metrics_upper.descent
25002 + metrics_lower.ascent + metrics_lower.descent) + 5;
25003 /* Center vertically.
25004 H:base_height, D:base_descent
25005 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
25006
25007 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
25008 descent = D - H/2 + h/2;
25009 lower_yoff = descent - 2 - ld;
25010 upper_yoff = lower_yoff - la - 1 - ud; */
25011 ascent = - (it->descent - (base_height + height + 1) / 2);
25012 descent = it->descent - (base_height - height) / 2;
25013 lower_yoff = descent - 2 - metrics_lower.descent;
25014 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
25015 - metrics_upper.descent);
25016 /* Don't make the height shorter than the base height. */
25017 if (height > base_height)
25018 {
25019 it->ascent = ascent;
25020 it->descent = descent;
25021 }
25022 }
25023
25024 it->phys_ascent = it->ascent;
25025 it->phys_descent = it->descent;
25026 if (it->glyph_row)
25027 append_glyphless_glyph (it, face_id, for_no_font, len,
25028 upper_xoff, upper_yoff,
25029 lower_xoff, lower_yoff);
25030 it->nglyphs = 1;
25031 take_vertical_position_into_account (it);
25032 }
25033
25034
25035 /* RIF:
25036 Produce glyphs/get display metrics for the display element IT is
25037 loaded with. See the description of struct it in dispextern.h
25038 for an overview of struct it. */
25039
25040 void
25041 x_produce_glyphs (struct it *it)
25042 {
25043 int extra_line_spacing = it->extra_line_spacing;
25044
25045 it->glyph_not_available_p = 0;
25046
25047 if (it->what == IT_CHARACTER)
25048 {
25049 XChar2b char2b;
25050 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25051 struct font *font = face->font;
25052 struct font_metrics *pcm = NULL;
25053 int boff; /* baseline offset */
25054
25055 if (font == NULL)
25056 {
25057 /* When no suitable font is found, display this character by
25058 the method specified in the first extra slot of
25059 Vglyphless_char_display. */
25060 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
25061
25062 eassert (it->what == IT_GLYPHLESS);
25063 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
25064 goto done;
25065 }
25066
25067 boff = font->baseline_offset;
25068 if (font->vertical_centering)
25069 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25070
25071 if (it->char_to_display != '\n' && it->char_to_display != '\t')
25072 {
25073 int stretched_p;
25074
25075 it->nglyphs = 1;
25076
25077 if (it->override_ascent >= 0)
25078 {
25079 it->ascent = it->override_ascent;
25080 it->descent = it->override_descent;
25081 boff = it->override_boff;
25082 }
25083 else
25084 {
25085 it->ascent = FONT_BASE (font) + boff;
25086 it->descent = FONT_DESCENT (font) - boff;
25087 }
25088
25089 if (get_char_glyph_code (it->char_to_display, font, &char2b))
25090 {
25091 pcm = get_per_char_metric (font, &char2b);
25092 if (pcm->width == 0
25093 && pcm->rbearing == 0 && pcm->lbearing == 0)
25094 pcm = NULL;
25095 }
25096
25097 if (pcm)
25098 {
25099 it->phys_ascent = pcm->ascent + boff;
25100 it->phys_descent = pcm->descent - boff;
25101 it->pixel_width = pcm->width;
25102 }
25103 else
25104 {
25105 it->glyph_not_available_p = 1;
25106 it->phys_ascent = it->ascent;
25107 it->phys_descent = it->descent;
25108 it->pixel_width = font->space_width;
25109 }
25110
25111 if (it->constrain_row_ascent_descent_p)
25112 {
25113 if (it->descent > it->max_descent)
25114 {
25115 it->ascent += it->descent - it->max_descent;
25116 it->descent = it->max_descent;
25117 }
25118 if (it->ascent > it->max_ascent)
25119 {
25120 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25121 it->ascent = it->max_ascent;
25122 }
25123 it->phys_ascent = min (it->phys_ascent, it->ascent);
25124 it->phys_descent = min (it->phys_descent, it->descent);
25125 extra_line_spacing = 0;
25126 }
25127
25128 /* If this is a space inside a region of text with
25129 `space-width' property, change its width. */
25130 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
25131 if (stretched_p)
25132 it->pixel_width *= XFLOATINT (it->space_width);
25133
25134 /* If face has a box, add the box thickness to the character
25135 height. If character has a box line to the left and/or
25136 right, add the box line width to the character's width. */
25137 if (face->box != FACE_NO_BOX)
25138 {
25139 int thick = face->box_line_width;
25140
25141 if (thick > 0)
25142 {
25143 it->ascent += thick;
25144 it->descent += thick;
25145 }
25146 else
25147 thick = -thick;
25148
25149 if (it->start_of_box_run_p)
25150 it->pixel_width += thick;
25151 if (it->end_of_box_run_p)
25152 it->pixel_width += thick;
25153 }
25154
25155 /* If face has an overline, add the height of the overline
25156 (1 pixel) and a 1 pixel margin to the character height. */
25157 if (face->overline_p)
25158 it->ascent += overline_margin;
25159
25160 if (it->constrain_row_ascent_descent_p)
25161 {
25162 if (it->ascent > it->max_ascent)
25163 it->ascent = it->max_ascent;
25164 if (it->descent > it->max_descent)
25165 it->descent = it->max_descent;
25166 }
25167
25168 take_vertical_position_into_account (it);
25169
25170 /* If we have to actually produce glyphs, do it. */
25171 if (it->glyph_row)
25172 {
25173 if (stretched_p)
25174 {
25175 /* Translate a space with a `space-width' property
25176 into a stretch glyph. */
25177 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
25178 / FONT_HEIGHT (font));
25179 append_stretch_glyph (it, it->object, it->pixel_width,
25180 it->ascent + it->descent, ascent);
25181 }
25182 else
25183 append_glyph (it);
25184
25185 /* If characters with lbearing or rbearing are displayed
25186 in this line, record that fact in a flag of the
25187 glyph row. This is used to optimize X output code. */
25188 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
25189 it->glyph_row->contains_overlapping_glyphs_p = 1;
25190 }
25191 if (! stretched_p && it->pixel_width == 0)
25192 /* We assure that all visible glyphs have at least 1-pixel
25193 width. */
25194 it->pixel_width = 1;
25195 }
25196 else if (it->char_to_display == '\n')
25197 {
25198 /* A newline has no width, but we need the height of the
25199 line. But if previous part of the line sets a height,
25200 don't increase that height */
25201
25202 Lisp_Object height;
25203 Lisp_Object total_height = Qnil;
25204
25205 it->override_ascent = -1;
25206 it->pixel_width = 0;
25207 it->nglyphs = 0;
25208
25209 height = get_it_property (it, Qline_height);
25210 /* Split (line-height total-height) list */
25211 if (CONSP (height)
25212 && CONSP (XCDR (height))
25213 && NILP (XCDR (XCDR (height))))
25214 {
25215 total_height = XCAR (XCDR (height));
25216 height = XCAR (height);
25217 }
25218 height = calc_line_height_property (it, height, font, boff, 1);
25219
25220 if (it->override_ascent >= 0)
25221 {
25222 it->ascent = it->override_ascent;
25223 it->descent = it->override_descent;
25224 boff = it->override_boff;
25225 }
25226 else
25227 {
25228 it->ascent = FONT_BASE (font) + boff;
25229 it->descent = FONT_DESCENT (font) - boff;
25230 }
25231
25232 if (EQ (height, Qt))
25233 {
25234 if (it->descent > it->max_descent)
25235 {
25236 it->ascent += it->descent - it->max_descent;
25237 it->descent = it->max_descent;
25238 }
25239 if (it->ascent > it->max_ascent)
25240 {
25241 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25242 it->ascent = it->max_ascent;
25243 }
25244 it->phys_ascent = min (it->phys_ascent, it->ascent);
25245 it->phys_descent = min (it->phys_descent, it->descent);
25246 it->constrain_row_ascent_descent_p = 1;
25247 extra_line_spacing = 0;
25248 }
25249 else
25250 {
25251 Lisp_Object spacing;
25252
25253 it->phys_ascent = it->ascent;
25254 it->phys_descent = it->descent;
25255
25256 if ((it->max_ascent > 0 || it->max_descent > 0)
25257 && face->box != FACE_NO_BOX
25258 && face->box_line_width > 0)
25259 {
25260 it->ascent += face->box_line_width;
25261 it->descent += face->box_line_width;
25262 }
25263 if (!NILP (height)
25264 && XINT (height) > it->ascent + it->descent)
25265 it->ascent = XINT (height) - it->descent;
25266
25267 if (!NILP (total_height))
25268 spacing = calc_line_height_property (it, total_height, font, boff, 0);
25269 else
25270 {
25271 spacing = get_it_property (it, Qline_spacing);
25272 spacing = calc_line_height_property (it, spacing, font, boff, 0);
25273 }
25274 if (INTEGERP (spacing))
25275 {
25276 extra_line_spacing = XINT (spacing);
25277 if (!NILP (total_height))
25278 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
25279 }
25280 }
25281 }
25282 else /* i.e. (it->char_to_display == '\t') */
25283 {
25284 if (font->space_width > 0)
25285 {
25286 int tab_width = it->tab_width * font->space_width;
25287 int x = it->current_x + it->continuation_lines_width;
25288 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
25289
25290 /* If the distance from the current position to the next tab
25291 stop is less than a space character width, use the
25292 tab stop after that. */
25293 if (next_tab_x - x < font->space_width)
25294 next_tab_x += tab_width;
25295
25296 it->pixel_width = next_tab_x - x;
25297 it->nglyphs = 1;
25298 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
25299 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
25300
25301 if (it->glyph_row)
25302 {
25303 append_stretch_glyph (it, it->object, it->pixel_width,
25304 it->ascent + it->descent, it->ascent);
25305 }
25306 }
25307 else
25308 {
25309 it->pixel_width = 0;
25310 it->nglyphs = 1;
25311 }
25312 }
25313 }
25314 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
25315 {
25316 /* A static composition.
25317
25318 Note: A composition is represented as one glyph in the
25319 glyph matrix. There are no padding glyphs.
25320
25321 Important note: pixel_width, ascent, and descent are the
25322 values of what is drawn by draw_glyphs (i.e. the values of
25323 the overall glyphs composed). */
25324 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25325 int boff; /* baseline offset */
25326 struct composition *cmp = composition_table[it->cmp_it.id];
25327 int glyph_len = cmp->glyph_len;
25328 struct font *font = face->font;
25329
25330 it->nglyphs = 1;
25331
25332 /* If we have not yet calculated pixel size data of glyphs of
25333 the composition for the current face font, calculate them
25334 now. Theoretically, we have to check all fonts for the
25335 glyphs, but that requires much time and memory space. So,
25336 here we check only the font of the first glyph. This may
25337 lead to incorrect display, but it's very rare, and C-l
25338 (recenter-top-bottom) can correct the display anyway. */
25339 if (! cmp->font || cmp->font != font)
25340 {
25341 /* Ascent and descent of the font of the first character
25342 of this composition (adjusted by baseline offset).
25343 Ascent and descent of overall glyphs should not be less
25344 than these, respectively. */
25345 int font_ascent, font_descent, font_height;
25346 /* Bounding box of the overall glyphs. */
25347 int leftmost, rightmost, lowest, highest;
25348 int lbearing, rbearing;
25349 int i, width, ascent, descent;
25350 int left_padded = 0, right_padded = 0;
25351 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
25352 XChar2b char2b;
25353 struct font_metrics *pcm;
25354 int font_not_found_p;
25355 ptrdiff_t pos;
25356
25357 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
25358 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
25359 break;
25360 if (glyph_len < cmp->glyph_len)
25361 right_padded = 1;
25362 for (i = 0; i < glyph_len; i++)
25363 {
25364 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
25365 break;
25366 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25367 }
25368 if (i > 0)
25369 left_padded = 1;
25370
25371 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
25372 : IT_CHARPOS (*it));
25373 /* If no suitable font is found, use the default font. */
25374 font_not_found_p = font == NULL;
25375 if (font_not_found_p)
25376 {
25377 face = face->ascii_face;
25378 font = face->font;
25379 }
25380 boff = font->baseline_offset;
25381 if (font->vertical_centering)
25382 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25383 font_ascent = FONT_BASE (font) + boff;
25384 font_descent = FONT_DESCENT (font) - boff;
25385 font_height = FONT_HEIGHT (font);
25386
25387 cmp->font = font;
25388
25389 pcm = NULL;
25390 if (! font_not_found_p)
25391 {
25392 get_char_face_and_encoding (it->f, c, it->face_id,
25393 &char2b, 0);
25394 pcm = get_per_char_metric (font, &char2b);
25395 }
25396
25397 /* Initialize the bounding box. */
25398 if (pcm)
25399 {
25400 width = cmp->glyph_len > 0 ? pcm->width : 0;
25401 ascent = pcm->ascent;
25402 descent = pcm->descent;
25403 lbearing = pcm->lbearing;
25404 rbearing = pcm->rbearing;
25405 }
25406 else
25407 {
25408 width = cmp->glyph_len > 0 ? font->space_width : 0;
25409 ascent = FONT_BASE (font);
25410 descent = FONT_DESCENT (font);
25411 lbearing = 0;
25412 rbearing = width;
25413 }
25414
25415 rightmost = width;
25416 leftmost = 0;
25417 lowest = - descent + boff;
25418 highest = ascent + boff;
25419
25420 if (! font_not_found_p
25421 && font->default_ascent
25422 && CHAR_TABLE_P (Vuse_default_ascent)
25423 && !NILP (Faref (Vuse_default_ascent,
25424 make_number (it->char_to_display))))
25425 highest = font->default_ascent + boff;
25426
25427 /* Draw the first glyph at the normal position. It may be
25428 shifted to right later if some other glyphs are drawn
25429 at the left. */
25430 cmp->offsets[i * 2] = 0;
25431 cmp->offsets[i * 2 + 1] = boff;
25432 cmp->lbearing = lbearing;
25433 cmp->rbearing = rbearing;
25434
25435 /* Set cmp->offsets for the remaining glyphs. */
25436 for (i++; i < glyph_len; i++)
25437 {
25438 int left, right, btm, top;
25439 int ch = COMPOSITION_GLYPH (cmp, i);
25440 int face_id;
25441 struct face *this_face;
25442
25443 if (ch == '\t')
25444 ch = ' ';
25445 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25446 this_face = FACE_FROM_ID (it->f, face_id);
25447 font = this_face->font;
25448
25449 if (font == NULL)
25450 pcm = NULL;
25451 else
25452 {
25453 get_char_face_and_encoding (it->f, ch, face_id,
25454 &char2b, 0);
25455 pcm = get_per_char_metric (font, &char2b);
25456 }
25457 if (! pcm)
25458 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25459 else
25460 {
25461 width = pcm->width;
25462 ascent = pcm->ascent;
25463 descent = pcm->descent;
25464 lbearing = pcm->lbearing;
25465 rbearing = pcm->rbearing;
25466 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25467 {
25468 /* Relative composition with or without
25469 alternate chars. */
25470 left = (leftmost + rightmost - width) / 2;
25471 btm = - descent + boff;
25472 if (font->relative_compose
25473 && (! CHAR_TABLE_P (Vignore_relative_composition)
25474 || NILP (Faref (Vignore_relative_composition,
25475 make_number (ch)))))
25476 {
25477
25478 if (- descent >= font->relative_compose)
25479 /* One extra pixel between two glyphs. */
25480 btm = highest + 1;
25481 else if (ascent <= 0)
25482 /* One extra pixel between two glyphs. */
25483 btm = lowest - 1 - ascent - descent;
25484 }
25485 }
25486 else
25487 {
25488 /* A composition rule is specified by an integer
25489 value that encodes global and new reference
25490 points (GREF and NREF). GREF and NREF are
25491 specified by numbers as below:
25492
25493 0---1---2 -- ascent
25494 | |
25495 | |
25496 | |
25497 9--10--11 -- center
25498 | |
25499 ---3---4---5--- baseline
25500 | |
25501 6---7---8 -- descent
25502 */
25503 int rule = COMPOSITION_RULE (cmp, i);
25504 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25505
25506 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25507 grefx = gref % 3, nrefx = nref % 3;
25508 grefy = gref / 3, nrefy = nref / 3;
25509 if (xoff)
25510 xoff = font_height * (xoff - 128) / 256;
25511 if (yoff)
25512 yoff = font_height * (yoff - 128) / 256;
25513
25514 left = (leftmost
25515 + grefx * (rightmost - leftmost) / 2
25516 - nrefx * width / 2
25517 + xoff);
25518
25519 btm = ((grefy == 0 ? highest
25520 : grefy == 1 ? 0
25521 : grefy == 2 ? lowest
25522 : (highest + lowest) / 2)
25523 - (nrefy == 0 ? ascent + descent
25524 : nrefy == 1 ? descent - boff
25525 : nrefy == 2 ? 0
25526 : (ascent + descent) / 2)
25527 + yoff);
25528 }
25529
25530 cmp->offsets[i * 2] = left;
25531 cmp->offsets[i * 2 + 1] = btm + descent;
25532
25533 /* Update the bounding box of the overall glyphs. */
25534 if (width > 0)
25535 {
25536 right = left + width;
25537 if (left < leftmost)
25538 leftmost = left;
25539 if (right > rightmost)
25540 rightmost = right;
25541 }
25542 top = btm + descent + ascent;
25543 if (top > highest)
25544 highest = top;
25545 if (btm < lowest)
25546 lowest = btm;
25547
25548 if (cmp->lbearing > left + lbearing)
25549 cmp->lbearing = left + lbearing;
25550 if (cmp->rbearing < left + rbearing)
25551 cmp->rbearing = left + rbearing;
25552 }
25553 }
25554
25555 /* If there are glyphs whose x-offsets are negative,
25556 shift all glyphs to the right and make all x-offsets
25557 non-negative. */
25558 if (leftmost < 0)
25559 {
25560 for (i = 0; i < cmp->glyph_len; i++)
25561 cmp->offsets[i * 2] -= leftmost;
25562 rightmost -= leftmost;
25563 cmp->lbearing -= leftmost;
25564 cmp->rbearing -= leftmost;
25565 }
25566
25567 if (left_padded && cmp->lbearing < 0)
25568 {
25569 for (i = 0; i < cmp->glyph_len; i++)
25570 cmp->offsets[i * 2] -= cmp->lbearing;
25571 rightmost -= cmp->lbearing;
25572 cmp->rbearing -= cmp->lbearing;
25573 cmp->lbearing = 0;
25574 }
25575 if (right_padded && rightmost < cmp->rbearing)
25576 {
25577 rightmost = cmp->rbearing;
25578 }
25579
25580 cmp->pixel_width = rightmost;
25581 cmp->ascent = highest;
25582 cmp->descent = - lowest;
25583 if (cmp->ascent < font_ascent)
25584 cmp->ascent = font_ascent;
25585 if (cmp->descent < font_descent)
25586 cmp->descent = font_descent;
25587 }
25588
25589 if (it->glyph_row
25590 && (cmp->lbearing < 0
25591 || cmp->rbearing > cmp->pixel_width))
25592 it->glyph_row->contains_overlapping_glyphs_p = 1;
25593
25594 it->pixel_width = cmp->pixel_width;
25595 it->ascent = it->phys_ascent = cmp->ascent;
25596 it->descent = it->phys_descent = cmp->descent;
25597 if (face->box != FACE_NO_BOX)
25598 {
25599 int thick = face->box_line_width;
25600
25601 if (thick > 0)
25602 {
25603 it->ascent += thick;
25604 it->descent += thick;
25605 }
25606 else
25607 thick = - thick;
25608
25609 if (it->start_of_box_run_p)
25610 it->pixel_width += thick;
25611 if (it->end_of_box_run_p)
25612 it->pixel_width += thick;
25613 }
25614
25615 /* If face has an overline, add the height of the overline
25616 (1 pixel) and a 1 pixel margin to the character height. */
25617 if (face->overline_p)
25618 it->ascent += overline_margin;
25619
25620 take_vertical_position_into_account (it);
25621 if (it->ascent < 0)
25622 it->ascent = 0;
25623 if (it->descent < 0)
25624 it->descent = 0;
25625
25626 if (it->glyph_row && cmp->glyph_len > 0)
25627 append_composite_glyph (it);
25628 }
25629 else if (it->what == IT_COMPOSITION)
25630 {
25631 /* A dynamic (automatic) composition. */
25632 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25633 Lisp_Object gstring;
25634 struct font_metrics metrics;
25635
25636 it->nglyphs = 1;
25637
25638 gstring = composition_gstring_from_id (it->cmp_it.id);
25639 it->pixel_width
25640 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25641 &metrics);
25642 if (it->glyph_row
25643 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25644 it->glyph_row->contains_overlapping_glyphs_p = 1;
25645 it->ascent = it->phys_ascent = metrics.ascent;
25646 it->descent = it->phys_descent = metrics.descent;
25647 if (face->box != FACE_NO_BOX)
25648 {
25649 int thick = face->box_line_width;
25650
25651 if (thick > 0)
25652 {
25653 it->ascent += thick;
25654 it->descent += thick;
25655 }
25656 else
25657 thick = - thick;
25658
25659 if (it->start_of_box_run_p)
25660 it->pixel_width += thick;
25661 if (it->end_of_box_run_p)
25662 it->pixel_width += thick;
25663 }
25664 /* If face has an overline, add the height of the overline
25665 (1 pixel) and a 1 pixel margin to the character height. */
25666 if (face->overline_p)
25667 it->ascent += overline_margin;
25668 take_vertical_position_into_account (it);
25669 if (it->ascent < 0)
25670 it->ascent = 0;
25671 if (it->descent < 0)
25672 it->descent = 0;
25673
25674 if (it->glyph_row)
25675 append_composite_glyph (it);
25676 }
25677 else if (it->what == IT_GLYPHLESS)
25678 produce_glyphless_glyph (it, 0, Qnil);
25679 else if (it->what == IT_IMAGE)
25680 produce_image_glyph (it);
25681 else if (it->what == IT_STRETCH)
25682 produce_stretch_glyph (it);
25683
25684 done:
25685 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25686 because this isn't true for images with `:ascent 100'. */
25687 eassert (it->ascent >= 0 && it->descent >= 0);
25688 if (it->area == TEXT_AREA)
25689 it->current_x += it->pixel_width;
25690
25691 if (extra_line_spacing > 0)
25692 {
25693 it->descent += extra_line_spacing;
25694 if (extra_line_spacing > it->max_extra_line_spacing)
25695 it->max_extra_line_spacing = extra_line_spacing;
25696 }
25697
25698 it->max_ascent = max (it->max_ascent, it->ascent);
25699 it->max_descent = max (it->max_descent, it->descent);
25700 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25701 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25702 }
25703
25704 /* EXPORT for RIF:
25705 Output LEN glyphs starting at START at the nominal cursor position.
25706 Advance the nominal cursor over the text. The global variable
25707 updated_row is the glyph row being updated, and updated_area is the
25708 area of that row being updated. */
25709
25710 void
25711 x_write_glyphs (struct window *w, struct glyph *start, int len)
25712 {
25713 int x, hpos, chpos = w->phys_cursor.hpos;
25714
25715 eassert (updated_row);
25716 /* When the window is hscrolled, cursor hpos can legitimately be out
25717 of bounds, but we draw the cursor at the corresponding window
25718 margin in that case. */
25719 if (!updated_row->reversed_p && chpos < 0)
25720 chpos = 0;
25721 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25722 chpos = updated_row->used[TEXT_AREA] - 1;
25723
25724 block_input ();
25725
25726 /* Write glyphs. */
25727
25728 hpos = start - updated_row->glyphs[updated_area];
25729 x = draw_glyphs (w, output_cursor.x,
25730 updated_row, updated_area,
25731 hpos, hpos + len,
25732 DRAW_NORMAL_TEXT, 0);
25733
25734 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25735 if (updated_area == TEXT_AREA
25736 && w->phys_cursor_on_p
25737 && w->phys_cursor.vpos == output_cursor.vpos
25738 && chpos >= hpos
25739 && chpos < hpos + len)
25740 w->phys_cursor_on_p = 0;
25741
25742 unblock_input ();
25743
25744 /* Advance the output cursor. */
25745 output_cursor.hpos += len;
25746 output_cursor.x = x;
25747 }
25748
25749
25750 /* EXPORT for RIF:
25751 Insert LEN glyphs from START at the nominal cursor position. */
25752
25753 void
25754 x_insert_glyphs (struct window *w, struct glyph *start, int len)
25755 {
25756 struct frame *f;
25757 int line_height, shift_by_width, shifted_region_width;
25758 struct glyph_row *row;
25759 struct glyph *glyph;
25760 int frame_x, frame_y;
25761 ptrdiff_t hpos;
25762
25763 eassert (updated_row);
25764 block_input ();
25765 f = XFRAME (WINDOW_FRAME (w));
25766
25767 /* Get the height of the line we are in. */
25768 row = updated_row;
25769 line_height = row->height;
25770
25771 /* Get the width of the glyphs to insert. */
25772 shift_by_width = 0;
25773 for (glyph = start; glyph < start + len; ++glyph)
25774 shift_by_width += glyph->pixel_width;
25775
25776 /* Get the width of the region to shift right. */
25777 shifted_region_width = (window_box_width (w, updated_area)
25778 - output_cursor.x
25779 - shift_by_width);
25780
25781 /* Shift right. */
25782 frame_x = window_box_left (w, updated_area) + output_cursor.x;
25783 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
25784
25785 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25786 line_height, shift_by_width);
25787
25788 /* Write the glyphs. */
25789 hpos = start - row->glyphs[updated_area];
25790 draw_glyphs (w, output_cursor.x, row, updated_area,
25791 hpos, hpos + len,
25792 DRAW_NORMAL_TEXT, 0);
25793
25794 /* Advance the output cursor. */
25795 output_cursor.hpos += len;
25796 output_cursor.x += shift_by_width;
25797 unblock_input ();
25798 }
25799
25800
25801 /* EXPORT for RIF:
25802 Erase the current text line from the nominal cursor position
25803 (inclusive) to pixel column TO_X (exclusive). The idea is that
25804 everything from TO_X onward is already erased.
25805
25806 TO_X is a pixel position relative to updated_area of currently
25807 updated window W. TO_X == -1 means clear to the end of this area. */
25808
25809 void
25810 x_clear_end_of_line (struct window *w, int to_x)
25811 {
25812 struct frame *f;
25813 int max_x, min_y, max_y;
25814 int from_x, from_y, to_y;
25815
25816 eassert (updated_row);
25817 f = XFRAME (w->frame);
25818
25819 if (updated_row->full_width_p)
25820 max_x = WINDOW_TOTAL_WIDTH (w);
25821 else
25822 max_x = window_box_width (w, updated_area);
25823 max_y = window_text_bottom_y (w);
25824
25825 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25826 of window. For TO_X > 0, truncate to end of drawing area. */
25827 if (to_x == 0)
25828 return;
25829 else if (to_x < 0)
25830 to_x = max_x;
25831 else
25832 to_x = min (to_x, max_x);
25833
25834 to_y = min (max_y, output_cursor.y + updated_row->height);
25835
25836 /* Notice if the cursor will be cleared by this operation. */
25837 if (!updated_row->full_width_p)
25838 notice_overwritten_cursor (w, updated_area,
25839 output_cursor.x, -1,
25840 updated_row->y,
25841 MATRIX_ROW_BOTTOM_Y (updated_row));
25842
25843 from_x = output_cursor.x;
25844
25845 /* Translate to frame coordinates. */
25846 if (updated_row->full_width_p)
25847 {
25848 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25849 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25850 }
25851 else
25852 {
25853 int area_left = window_box_left (w, updated_area);
25854 from_x += area_left;
25855 to_x += area_left;
25856 }
25857
25858 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25859 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
25860 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25861
25862 /* Prevent inadvertently clearing to end of the X window. */
25863 if (to_x > from_x && to_y > from_y)
25864 {
25865 block_input ();
25866 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25867 to_x - from_x, to_y - from_y);
25868 unblock_input ();
25869 }
25870 }
25871
25872 #endif /* HAVE_WINDOW_SYSTEM */
25873
25874
25875 \f
25876 /***********************************************************************
25877 Cursor types
25878 ***********************************************************************/
25879
25880 /* Value is the internal representation of the specified cursor type
25881 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25882 of the bar cursor. */
25883
25884 static enum text_cursor_kinds
25885 get_specified_cursor_type (Lisp_Object arg, int *width)
25886 {
25887 enum text_cursor_kinds type;
25888
25889 if (NILP (arg))
25890 return NO_CURSOR;
25891
25892 if (EQ (arg, Qbox))
25893 return FILLED_BOX_CURSOR;
25894
25895 if (EQ (arg, Qhollow))
25896 return HOLLOW_BOX_CURSOR;
25897
25898 if (EQ (arg, Qbar))
25899 {
25900 *width = 2;
25901 return BAR_CURSOR;
25902 }
25903
25904 if (CONSP (arg)
25905 && EQ (XCAR (arg), Qbar)
25906 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25907 {
25908 *width = XINT (XCDR (arg));
25909 return BAR_CURSOR;
25910 }
25911
25912 if (EQ (arg, Qhbar))
25913 {
25914 *width = 2;
25915 return HBAR_CURSOR;
25916 }
25917
25918 if (CONSP (arg)
25919 && EQ (XCAR (arg), Qhbar)
25920 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25921 {
25922 *width = XINT (XCDR (arg));
25923 return HBAR_CURSOR;
25924 }
25925
25926 /* Treat anything unknown as "hollow box cursor".
25927 It was bad to signal an error; people have trouble fixing
25928 .Xdefaults with Emacs, when it has something bad in it. */
25929 type = HOLLOW_BOX_CURSOR;
25930
25931 return type;
25932 }
25933
25934 /* Set the default cursor types for specified frame. */
25935 void
25936 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25937 {
25938 int width = 1;
25939 Lisp_Object tem;
25940
25941 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25942 FRAME_CURSOR_WIDTH (f) = width;
25943
25944 /* By default, set up the blink-off state depending on the on-state. */
25945
25946 tem = Fassoc (arg, Vblink_cursor_alist);
25947 if (!NILP (tem))
25948 {
25949 FRAME_BLINK_OFF_CURSOR (f)
25950 = get_specified_cursor_type (XCDR (tem), &width);
25951 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25952 }
25953 else
25954 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25955
25956 /* Make sure the cursor gets redrawn. */
25957 cursor_type_changed = 1;
25958 }
25959
25960
25961 #ifdef HAVE_WINDOW_SYSTEM
25962
25963 /* Return the cursor we want to be displayed in window W. Return
25964 width of bar/hbar cursor through WIDTH arg. Return with
25965 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
25966 (i.e. if the `system caret' should track this cursor).
25967
25968 In a mini-buffer window, we want the cursor only to appear if we
25969 are reading input from this window. For the selected window, we
25970 want the cursor type given by the frame parameter or buffer local
25971 setting of cursor-type. If explicitly marked off, draw no cursor.
25972 In all other cases, we want a hollow box cursor. */
25973
25974 static enum text_cursor_kinds
25975 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
25976 int *active_cursor)
25977 {
25978 struct frame *f = XFRAME (w->frame);
25979 struct buffer *b = XBUFFER (w->contents);
25980 int cursor_type = DEFAULT_CURSOR;
25981 Lisp_Object alt_cursor;
25982 int non_selected = 0;
25983
25984 *active_cursor = 1;
25985
25986 /* Echo area */
25987 if (cursor_in_echo_area
25988 && FRAME_HAS_MINIBUF_P (f)
25989 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
25990 {
25991 if (w == XWINDOW (echo_area_window))
25992 {
25993 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
25994 {
25995 *width = FRAME_CURSOR_WIDTH (f);
25996 return FRAME_DESIRED_CURSOR (f);
25997 }
25998 else
25999 return get_specified_cursor_type (BVAR (b, cursor_type), width);
26000 }
26001
26002 *active_cursor = 0;
26003 non_selected = 1;
26004 }
26005
26006 /* Detect a nonselected window or nonselected frame. */
26007 else if (w != XWINDOW (f->selected_window)
26008 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
26009 {
26010 *active_cursor = 0;
26011
26012 if (MINI_WINDOW_P (w) && minibuf_level == 0)
26013 return NO_CURSOR;
26014
26015 non_selected = 1;
26016 }
26017
26018 /* Never display a cursor in a window in which cursor-type is nil. */
26019 if (NILP (BVAR (b, cursor_type)))
26020 return NO_CURSOR;
26021
26022 /* Get the normal cursor type for this window. */
26023 if (EQ (BVAR (b, cursor_type), Qt))
26024 {
26025 cursor_type = FRAME_DESIRED_CURSOR (f);
26026 *width = FRAME_CURSOR_WIDTH (f);
26027 }
26028 else
26029 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
26030
26031 /* Use cursor-in-non-selected-windows instead
26032 for non-selected window or frame. */
26033 if (non_selected)
26034 {
26035 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
26036 if (!EQ (Qt, alt_cursor))
26037 return get_specified_cursor_type (alt_cursor, width);
26038 /* t means modify the normal cursor type. */
26039 if (cursor_type == FILLED_BOX_CURSOR)
26040 cursor_type = HOLLOW_BOX_CURSOR;
26041 else if (cursor_type == BAR_CURSOR && *width > 1)
26042 --*width;
26043 return cursor_type;
26044 }
26045
26046 /* Use normal cursor if not blinked off. */
26047 if (!w->cursor_off_p)
26048 {
26049 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26050 {
26051 if (cursor_type == FILLED_BOX_CURSOR)
26052 {
26053 /* Using a block cursor on large images can be very annoying.
26054 So use a hollow cursor for "large" images.
26055 If image is not transparent (no mask), also use hollow cursor. */
26056 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26057 if (img != NULL && IMAGEP (img->spec))
26058 {
26059 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
26060 where N = size of default frame font size.
26061 This should cover most of the "tiny" icons people may use. */
26062 if (!img->mask
26063 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
26064 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
26065 cursor_type = HOLLOW_BOX_CURSOR;
26066 }
26067 }
26068 else if (cursor_type != NO_CURSOR)
26069 {
26070 /* Display current only supports BOX and HOLLOW cursors for images.
26071 So for now, unconditionally use a HOLLOW cursor when cursor is
26072 not a solid box cursor. */
26073 cursor_type = HOLLOW_BOX_CURSOR;
26074 }
26075 }
26076 return cursor_type;
26077 }
26078
26079 /* Cursor is blinked off, so determine how to "toggle" it. */
26080
26081 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
26082 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
26083 return get_specified_cursor_type (XCDR (alt_cursor), width);
26084
26085 /* Then see if frame has specified a specific blink off cursor type. */
26086 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
26087 {
26088 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
26089 return FRAME_BLINK_OFF_CURSOR (f);
26090 }
26091
26092 #if 0
26093 /* Some people liked having a permanently visible blinking cursor,
26094 while others had very strong opinions against it. So it was
26095 decided to remove it. KFS 2003-09-03 */
26096
26097 /* Finally perform built-in cursor blinking:
26098 filled box <-> hollow box
26099 wide [h]bar <-> narrow [h]bar
26100 narrow [h]bar <-> no cursor
26101 other type <-> no cursor */
26102
26103 if (cursor_type == FILLED_BOX_CURSOR)
26104 return HOLLOW_BOX_CURSOR;
26105
26106 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
26107 {
26108 *width = 1;
26109 return cursor_type;
26110 }
26111 #endif
26112
26113 return NO_CURSOR;
26114 }
26115
26116
26117 /* Notice when the text cursor of window W has been completely
26118 overwritten by a drawing operation that outputs glyphs in AREA
26119 starting at X0 and ending at X1 in the line starting at Y0 and
26120 ending at Y1. X coordinates are area-relative. X1 < 0 means all
26121 the rest of the line after X0 has been written. Y coordinates
26122 are window-relative. */
26123
26124 static void
26125 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
26126 int x0, int x1, int y0, int y1)
26127 {
26128 int cx0, cx1, cy0, cy1;
26129 struct glyph_row *row;
26130
26131 if (!w->phys_cursor_on_p)
26132 return;
26133 if (area != TEXT_AREA)
26134 return;
26135
26136 if (w->phys_cursor.vpos < 0
26137 || w->phys_cursor.vpos >= w->current_matrix->nrows
26138 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
26139 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
26140 return;
26141
26142 if (row->cursor_in_fringe_p)
26143 {
26144 row->cursor_in_fringe_p = 0;
26145 draw_fringe_bitmap (w, row, row->reversed_p);
26146 w->phys_cursor_on_p = 0;
26147 return;
26148 }
26149
26150 cx0 = w->phys_cursor.x;
26151 cx1 = cx0 + w->phys_cursor_width;
26152 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
26153 return;
26154
26155 /* The cursor image will be completely removed from the
26156 screen if the output area intersects the cursor area in
26157 y-direction. When we draw in [y0 y1[, and some part of
26158 the cursor is at y < y0, that part must have been drawn
26159 before. When scrolling, the cursor is erased before
26160 actually scrolling, so we don't come here. When not
26161 scrolling, the rows above the old cursor row must have
26162 changed, and in this case these rows must have written
26163 over the cursor image.
26164
26165 Likewise if part of the cursor is below y1, with the
26166 exception of the cursor being in the first blank row at
26167 the buffer and window end because update_text_area
26168 doesn't draw that row. (Except when it does, but
26169 that's handled in update_text_area.) */
26170
26171 cy0 = w->phys_cursor.y;
26172 cy1 = cy0 + w->phys_cursor_height;
26173 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
26174 return;
26175
26176 w->phys_cursor_on_p = 0;
26177 }
26178
26179 #endif /* HAVE_WINDOW_SYSTEM */
26180
26181 \f
26182 /************************************************************************
26183 Mouse Face
26184 ************************************************************************/
26185
26186 #ifdef HAVE_WINDOW_SYSTEM
26187
26188 /* EXPORT for RIF:
26189 Fix the display of area AREA of overlapping row ROW in window W
26190 with respect to the overlapping part OVERLAPS. */
26191
26192 void
26193 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
26194 enum glyph_row_area area, int overlaps)
26195 {
26196 int i, x;
26197
26198 block_input ();
26199
26200 x = 0;
26201 for (i = 0; i < row->used[area];)
26202 {
26203 if (row->glyphs[area][i].overlaps_vertically_p)
26204 {
26205 int start = i, start_x = x;
26206
26207 do
26208 {
26209 x += row->glyphs[area][i].pixel_width;
26210 ++i;
26211 }
26212 while (i < row->used[area]
26213 && row->glyphs[area][i].overlaps_vertically_p);
26214
26215 draw_glyphs (w, start_x, row, area,
26216 start, i,
26217 DRAW_NORMAL_TEXT, overlaps);
26218 }
26219 else
26220 {
26221 x += row->glyphs[area][i].pixel_width;
26222 ++i;
26223 }
26224 }
26225
26226 unblock_input ();
26227 }
26228
26229
26230 /* EXPORT:
26231 Draw the cursor glyph of window W in glyph row ROW. See the
26232 comment of draw_glyphs for the meaning of HL. */
26233
26234 void
26235 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
26236 enum draw_glyphs_face hl)
26237 {
26238 /* If cursor hpos is out of bounds, don't draw garbage. This can
26239 happen in mini-buffer windows when switching between echo area
26240 glyphs and mini-buffer. */
26241 if ((row->reversed_p
26242 ? (w->phys_cursor.hpos >= 0)
26243 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
26244 {
26245 int on_p = w->phys_cursor_on_p;
26246 int x1;
26247 int hpos = w->phys_cursor.hpos;
26248
26249 /* When the window is hscrolled, cursor hpos can legitimately be
26250 out of bounds, but we draw the cursor at the corresponding
26251 window margin in that case. */
26252 if (!row->reversed_p && hpos < 0)
26253 hpos = 0;
26254 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26255 hpos = row->used[TEXT_AREA] - 1;
26256
26257 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
26258 hl, 0);
26259 w->phys_cursor_on_p = on_p;
26260
26261 if (hl == DRAW_CURSOR)
26262 w->phys_cursor_width = x1 - w->phys_cursor.x;
26263 /* When we erase the cursor, and ROW is overlapped by other
26264 rows, make sure that these overlapping parts of other rows
26265 are redrawn. */
26266 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
26267 {
26268 w->phys_cursor_width = x1 - w->phys_cursor.x;
26269
26270 if (row > w->current_matrix->rows
26271 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
26272 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
26273 OVERLAPS_ERASED_CURSOR);
26274
26275 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
26276 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
26277 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
26278 OVERLAPS_ERASED_CURSOR);
26279 }
26280 }
26281 }
26282
26283
26284 /* EXPORT:
26285 Erase the image of a cursor of window W from the screen. */
26286
26287 void
26288 erase_phys_cursor (struct window *w)
26289 {
26290 struct frame *f = XFRAME (w->frame);
26291 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26292 int hpos = w->phys_cursor.hpos;
26293 int vpos = w->phys_cursor.vpos;
26294 int mouse_face_here_p = 0;
26295 struct glyph_matrix *active_glyphs = w->current_matrix;
26296 struct glyph_row *cursor_row;
26297 struct glyph *cursor_glyph;
26298 enum draw_glyphs_face hl;
26299
26300 /* No cursor displayed or row invalidated => nothing to do on the
26301 screen. */
26302 if (w->phys_cursor_type == NO_CURSOR)
26303 goto mark_cursor_off;
26304
26305 /* VPOS >= active_glyphs->nrows means that window has been resized.
26306 Don't bother to erase the cursor. */
26307 if (vpos >= active_glyphs->nrows)
26308 goto mark_cursor_off;
26309
26310 /* If row containing cursor is marked invalid, there is nothing we
26311 can do. */
26312 cursor_row = MATRIX_ROW (active_glyphs, vpos);
26313 if (!cursor_row->enabled_p)
26314 goto mark_cursor_off;
26315
26316 /* If line spacing is > 0, old cursor may only be partially visible in
26317 window after split-window. So adjust visible height. */
26318 cursor_row->visible_height = min (cursor_row->visible_height,
26319 window_text_bottom_y (w) - cursor_row->y);
26320
26321 /* If row is completely invisible, don't attempt to delete a cursor which
26322 isn't there. This can happen if cursor is at top of a window, and
26323 we switch to a buffer with a header line in that window. */
26324 if (cursor_row->visible_height <= 0)
26325 goto mark_cursor_off;
26326
26327 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
26328 if (cursor_row->cursor_in_fringe_p)
26329 {
26330 cursor_row->cursor_in_fringe_p = 0;
26331 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
26332 goto mark_cursor_off;
26333 }
26334
26335 /* This can happen when the new row is shorter than the old one.
26336 In this case, either draw_glyphs or clear_end_of_line
26337 should have cleared the cursor. Note that we wouldn't be
26338 able to erase the cursor in this case because we don't have a
26339 cursor glyph at hand. */
26340 if ((cursor_row->reversed_p
26341 ? (w->phys_cursor.hpos < 0)
26342 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
26343 goto mark_cursor_off;
26344
26345 /* When the window is hscrolled, cursor hpos can legitimately be out
26346 of bounds, but we draw the cursor at the corresponding window
26347 margin in that case. */
26348 if (!cursor_row->reversed_p && hpos < 0)
26349 hpos = 0;
26350 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
26351 hpos = cursor_row->used[TEXT_AREA] - 1;
26352
26353 /* If the cursor is in the mouse face area, redisplay that when
26354 we clear the cursor. */
26355 if (! NILP (hlinfo->mouse_face_window)
26356 && coords_in_mouse_face_p (w, hpos, vpos)
26357 /* Don't redraw the cursor's spot in mouse face if it is at the
26358 end of a line (on a newline). The cursor appears there, but
26359 mouse highlighting does not. */
26360 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
26361 mouse_face_here_p = 1;
26362
26363 /* Maybe clear the display under the cursor. */
26364 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
26365 {
26366 int x, y, left_x;
26367 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
26368 int width;
26369
26370 cursor_glyph = get_phys_cursor_glyph (w);
26371 if (cursor_glyph == NULL)
26372 goto mark_cursor_off;
26373
26374 width = cursor_glyph->pixel_width;
26375 left_x = window_box_left_offset (w, TEXT_AREA);
26376 x = w->phys_cursor.x;
26377 if (x < left_x)
26378 width -= left_x - x;
26379 width = min (width, window_box_width (w, TEXT_AREA) - x);
26380 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
26381 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
26382
26383 if (width > 0)
26384 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
26385 }
26386
26387 /* Erase the cursor by redrawing the character underneath it. */
26388 if (mouse_face_here_p)
26389 hl = DRAW_MOUSE_FACE;
26390 else
26391 hl = DRAW_NORMAL_TEXT;
26392 draw_phys_cursor_glyph (w, cursor_row, hl);
26393
26394 mark_cursor_off:
26395 w->phys_cursor_on_p = 0;
26396 w->phys_cursor_type = NO_CURSOR;
26397 }
26398
26399
26400 /* EXPORT:
26401 Display or clear cursor of window W. If ON is zero, clear the
26402 cursor. If it is non-zero, display the cursor. If ON is nonzero,
26403 where to put the cursor is specified by HPOS, VPOS, X and Y. */
26404
26405 void
26406 display_and_set_cursor (struct window *w, int on,
26407 int hpos, int vpos, int x, int y)
26408 {
26409 struct frame *f = XFRAME (w->frame);
26410 int new_cursor_type;
26411 int new_cursor_width;
26412 int active_cursor;
26413 struct glyph_row *glyph_row;
26414 struct glyph *glyph;
26415
26416 /* This is pointless on invisible frames, and dangerous on garbaged
26417 windows and frames; in the latter case, the frame or window may
26418 be in the midst of changing its size, and x and y may be off the
26419 window. */
26420 if (! FRAME_VISIBLE_P (f)
26421 || FRAME_GARBAGED_P (f)
26422 || vpos >= w->current_matrix->nrows
26423 || hpos >= w->current_matrix->matrix_w)
26424 return;
26425
26426 /* If cursor is off and we want it off, return quickly. */
26427 if (!on && !w->phys_cursor_on_p)
26428 return;
26429
26430 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26431 /* If cursor row is not enabled, we don't really know where to
26432 display the cursor. */
26433 if (!glyph_row->enabled_p)
26434 {
26435 w->phys_cursor_on_p = 0;
26436 return;
26437 }
26438
26439 glyph = NULL;
26440 if (!glyph_row->exact_window_width_line_p
26441 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26442 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26443
26444 eassert (input_blocked_p ());
26445
26446 /* Set new_cursor_type to the cursor we want to be displayed. */
26447 new_cursor_type = get_window_cursor_type (w, glyph,
26448 &new_cursor_width, &active_cursor);
26449
26450 /* If cursor is currently being shown and we don't want it to be or
26451 it is in the wrong place, or the cursor type is not what we want,
26452 erase it. */
26453 if (w->phys_cursor_on_p
26454 && (!on
26455 || w->phys_cursor.x != x
26456 || w->phys_cursor.y != y
26457 || new_cursor_type != w->phys_cursor_type
26458 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26459 && new_cursor_width != w->phys_cursor_width)))
26460 erase_phys_cursor (w);
26461
26462 /* Don't check phys_cursor_on_p here because that flag is only set
26463 to zero in some cases where we know that the cursor has been
26464 completely erased, to avoid the extra work of erasing the cursor
26465 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26466 still not be visible, or it has only been partly erased. */
26467 if (on)
26468 {
26469 w->phys_cursor_ascent = glyph_row->ascent;
26470 w->phys_cursor_height = glyph_row->height;
26471
26472 /* Set phys_cursor_.* before x_draw_.* is called because some
26473 of them may need the information. */
26474 w->phys_cursor.x = x;
26475 w->phys_cursor.y = glyph_row->y;
26476 w->phys_cursor.hpos = hpos;
26477 w->phys_cursor.vpos = vpos;
26478 }
26479
26480 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26481 new_cursor_type, new_cursor_width,
26482 on, active_cursor);
26483 }
26484
26485
26486 /* Switch the display of W's cursor on or off, according to the value
26487 of ON. */
26488
26489 static void
26490 update_window_cursor (struct window *w, int on)
26491 {
26492 /* Don't update cursor in windows whose frame is in the process
26493 of being deleted. */
26494 if (w->current_matrix)
26495 {
26496 int hpos = w->phys_cursor.hpos;
26497 int vpos = w->phys_cursor.vpos;
26498 struct glyph_row *row;
26499
26500 if (vpos >= w->current_matrix->nrows
26501 || hpos >= w->current_matrix->matrix_w)
26502 return;
26503
26504 row = MATRIX_ROW (w->current_matrix, vpos);
26505
26506 /* When the window is hscrolled, cursor hpos can legitimately be
26507 out of bounds, but we draw the cursor at the corresponding
26508 window margin in that case. */
26509 if (!row->reversed_p && hpos < 0)
26510 hpos = 0;
26511 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26512 hpos = row->used[TEXT_AREA] - 1;
26513
26514 block_input ();
26515 display_and_set_cursor (w, on, hpos, vpos,
26516 w->phys_cursor.x, w->phys_cursor.y);
26517 unblock_input ();
26518 }
26519 }
26520
26521
26522 /* Call update_window_cursor with parameter ON_P on all leaf windows
26523 in the window tree rooted at W. */
26524
26525 static void
26526 update_cursor_in_window_tree (struct window *w, int on_p)
26527 {
26528 while (w)
26529 {
26530 if (WINDOWP (w->contents))
26531 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
26532 else
26533 update_window_cursor (w, on_p);
26534
26535 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26536 }
26537 }
26538
26539
26540 /* EXPORT:
26541 Display the cursor on window W, or clear it, according to ON_P.
26542 Don't change the cursor's position. */
26543
26544 void
26545 x_update_cursor (struct frame *f, int on_p)
26546 {
26547 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26548 }
26549
26550
26551 /* EXPORT:
26552 Clear the cursor of window W to background color, and mark the
26553 cursor as not shown. This is used when the text where the cursor
26554 is about to be rewritten. */
26555
26556 void
26557 x_clear_cursor (struct window *w)
26558 {
26559 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26560 update_window_cursor (w, 0);
26561 }
26562
26563 #endif /* HAVE_WINDOW_SYSTEM */
26564
26565 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26566 and MSDOS. */
26567 static void
26568 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26569 int start_hpos, int end_hpos,
26570 enum draw_glyphs_face draw)
26571 {
26572 #ifdef HAVE_WINDOW_SYSTEM
26573 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26574 {
26575 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26576 return;
26577 }
26578 #endif
26579 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26580 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26581 #endif
26582 }
26583
26584 /* Display the active region described by mouse_face_* according to DRAW. */
26585
26586 static void
26587 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26588 {
26589 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26590 struct frame *f = XFRAME (WINDOW_FRAME (w));
26591
26592 if (/* If window is in the process of being destroyed, don't bother
26593 to do anything. */
26594 w->current_matrix != NULL
26595 /* Don't update mouse highlight if hidden */
26596 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26597 /* Recognize when we are called to operate on rows that don't exist
26598 anymore. This can happen when a window is split. */
26599 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26600 {
26601 int phys_cursor_on_p = w->phys_cursor_on_p;
26602 struct glyph_row *row, *first, *last;
26603
26604 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26605 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26606
26607 for (row = first; row <= last && row->enabled_p; ++row)
26608 {
26609 int start_hpos, end_hpos, start_x;
26610
26611 /* For all but the first row, the highlight starts at column 0. */
26612 if (row == first)
26613 {
26614 /* R2L rows have BEG and END in reversed order, but the
26615 screen drawing geometry is always left to right. So
26616 we need to mirror the beginning and end of the
26617 highlighted area in R2L rows. */
26618 if (!row->reversed_p)
26619 {
26620 start_hpos = hlinfo->mouse_face_beg_col;
26621 start_x = hlinfo->mouse_face_beg_x;
26622 }
26623 else if (row == last)
26624 {
26625 start_hpos = hlinfo->mouse_face_end_col;
26626 start_x = hlinfo->mouse_face_end_x;
26627 }
26628 else
26629 {
26630 start_hpos = 0;
26631 start_x = 0;
26632 }
26633 }
26634 else if (row->reversed_p && row == last)
26635 {
26636 start_hpos = hlinfo->mouse_face_end_col;
26637 start_x = hlinfo->mouse_face_end_x;
26638 }
26639 else
26640 {
26641 start_hpos = 0;
26642 start_x = 0;
26643 }
26644
26645 if (row == last)
26646 {
26647 if (!row->reversed_p)
26648 end_hpos = hlinfo->mouse_face_end_col;
26649 else if (row == first)
26650 end_hpos = hlinfo->mouse_face_beg_col;
26651 else
26652 {
26653 end_hpos = row->used[TEXT_AREA];
26654 if (draw == DRAW_NORMAL_TEXT)
26655 row->fill_line_p = 1; /* Clear to end of line */
26656 }
26657 }
26658 else if (row->reversed_p && row == first)
26659 end_hpos = hlinfo->mouse_face_beg_col;
26660 else
26661 {
26662 end_hpos = row->used[TEXT_AREA];
26663 if (draw == DRAW_NORMAL_TEXT)
26664 row->fill_line_p = 1; /* Clear to end of line */
26665 }
26666
26667 if (end_hpos > start_hpos)
26668 {
26669 draw_row_with_mouse_face (w, start_x, row,
26670 start_hpos, end_hpos, draw);
26671
26672 row->mouse_face_p
26673 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26674 }
26675 }
26676
26677 #ifdef HAVE_WINDOW_SYSTEM
26678 /* When we've written over the cursor, arrange for it to
26679 be displayed again. */
26680 if (FRAME_WINDOW_P (f)
26681 && phys_cursor_on_p && !w->phys_cursor_on_p)
26682 {
26683 int hpos = w->phys_cursor.hpos;
26684
26685 /* When the window is hscrolled, cursor hpos can legitimately be
26686 out of bounds, but we draw the cursor at the corresponding
26687 window margin in that case. */
26688 if (!row->reversed_p && hpos < 0)
26689 hpos = 0;
26690 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26691 hpos = row->used[TEXT_AREA] - 1;
26692
26693 block_input ();
26694 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26695 w->phys_cursor.x, w->phys_cursor.y);
26696 unblock_input ();
26697 }
26698 #endif /* HAVE_WINDOW_SYSTEM */
26699 }
26700
26701 #ifdef HAVE_WINDOW_SYSTEM
26702 /* Change the mouse cursor. */
26703 if (FRAME_WINDOW_P (f))
26704 {
26705 if (draw == DRAW_NORMAL_TEXT
26706 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26707 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26708 else if (draw == DRAW_MOUSE_FACE)
26709 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26710 else
26711 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26712 }
26713 #endif /* HAVE_WINDOW_SYSTEM */
26714 }
26715
26716 /* EXPORT:
26717 Clear out the mouse-highlighted active region.
26718 Redraw it un-highlighted first. Value is non-zero if mouse
26719 face was actually drawn unhighlighted. */
26720
26721 int
26722 clear_mouse_face (Mouse_HLInfo *hlinfo)
26723 {
26724 int cleared = 0;
26725
26726 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26727 {
26728 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26729 cleared = 1;
26730 }
26731
26732 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26733 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26734 hlinfo->mouse_face_window = Qnil;
26735 hlinfo->mouse_face_overlay = Qnil;
26736 return cleared;
26737 }
26738
26739 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26740 within the mouse face on that window. */
26741 static int
26742 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26743 {
26744 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26745
26746 /* Quickly resolve the easy cases. */
26747 if (!(WINDOWP (hlinfo->mouse_face_window)
26748 && XWINDOW (hlinfo->mouse_face_window) == w))
26749 return 0;
26750 if (vpos < hlinfo->mouse_face_beg_row
26751 || vpos > hlinfo->mouse_face_end_row)
26752 return 0;
26753 if (vpos > hlinfo->mouse_face_beg_row
26754 && vpos < hlinfo->mouse_face_end_row)
26755 return 1;
26756
26757 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26758 {
26759 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26760 {
26761 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26762 return 1;
26763 }
26764 else if ((vpos == hlinfo->mouse_face_beg_row
26765 && hpos >= hlinfo->mouse_face_beg_col)
26766 || (vpos == hlinfo->mouse_face_end_row
26767 && hpos < hlinfo->mouse_face_end_col))
26768 return 1;
26769 }
26770 else
26771 {
26772 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26773 {
26774 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26775 return 1;
26776 }
26777 else if ((vpos == hlinfo->mouse_face_beg_row
26778 && hpos <= hlinfo->mouse_face_beg_col)
26779 || (vpos == hlinfo->mouse_face_end_row
26780 && hpos > hlinfo->mouse_face_end_col))
26781 return 1;
26782 }
26783 return 0;
26784 }
26785
26786
26787 /* EXPORT:
26788 Non-zero if physical cursor of window W is within mouse face. */
26789
26790 int
26791 cursor_in_mouse_face_p (struct window *w)
26792 {
26793 int hpos = w->phys_cursor.hpos;
26794 int vpos = w->phys_cursor.vpos;
26795 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26796
26797 /* When the window is hscrolled, cursor hpos can legitimately be out
26798 of bounds, but we draw the cursor at the corresponding window
26799 margin in that case. */
26800 if (!row->reversed_p && hpos < 0)
26801 hpos = 0;
26802 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26803 hpos = row->used[TEXT_AREA] - 1;
26804
26805 return coords_in_mouse_face_p (w, hpos, vpos);
26806 }
26807
26808
26809 \f
26810 /* Find the glyph rows START_ROW and END_ROW of window W that display
26811 characters between buffer positions START_CHARPOS and END_CHARPOS
26812 (excluding END_CHARPOS). DISP_STRING is a display string that
26813 covers these buffer positions. This is similar to
26814 row_containing_pos, but is more accurate when bidi reordering makes
26815 buffer positions change non-linearly with glyph rows. */
26816 static void
26817 rows_from_pos_range (struct window *w,
26818 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26819 Lisp_Object disp_string,
26820 struct glyph_row **start, struct glyph_row **end)
26821 {
26822 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26823 int last_y = window_text_bottom_y (w);
26824 struct glyph_row *row;
26825
26826 *start = NULL;
26827 *end = NULL;
26828
26829 while (!first->enabled_p
26830 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26831 first++;
26832
26833 /* Find the START row. */
26834 for (row = first;
26835 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26836 row++)
26837 {
26838 /* A row can potentially be the START row if the range of the
26839 characters it displays intersects the range
26840 [START_CHARPOS..END_CHARPOS). */
26841 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26842 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26843 /* See the commentary in row_containing_pos, for the
26844 explanation of the complicated way to check whether
26845 some position is beyond the end of the characters
26846 displayed by a row. */
26847 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26848 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26849 && !row->ends_at_zv_p
26850 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26851 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26852 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26853 && !row->ends_at_zv_p
26854 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26855 {
26856 /* Found a candidate row. Now make sure at least one of the
26857 glyphs it displays has a charpos from the range
26858 [START_CHARPOS..END_CHARPOS).
26859
26860 This is not obvious because bidi reordering could make
26861 buffer positions of a row be 1,2,3,102,101,100, and if we
26862 want to highlight characters in [50..60), we don't want
26863 this row, even though [50..60) does intersect [1..103),
26864 the range of character positions given by the row's start
26865 and end positions. */
26866 struct glyph *g = row->glyphs[TEXT_AREA];
26867 struct glyph *e = g + row->used[TEXT_AREA];
26868
26869 while (g < e)
26870 {
26871 if (((BUFFERP (g->object) || INTEGERP (g->object))
26872 && start_charpos <= g->charpos && g->charpos < end_charpos)
26873 /* A glyph that comes from DISP_STRING is by
26874 definition to be highlighted. */
26875 || EQ (g->object, disp_string))
26876 *start = row;
26877 g++;
26878 }
26879 if (*start)
26880 break;
26881 }
26882 }
26883
26884 /* Find the END row. */
26885 if (!*start
26886 /* If the last row is partially visible, start looking for END
26887 from that row, instead of starting from FIRST. */
26888 && !(row->enabled_p
26889 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26890 row = first;
26891 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26892 {
26893 struct glyph_row *next = row + 1;
26894 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26895
26896 if (!next->enabled_p
26897 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26898 /* The first row >= START whose range of displayed characters
26899 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26900 is the row END + 1. */
26901 || (start_charpos < next_start
26902 && end_charpos < next_start)
26903 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26904 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26905 && !next->ends_at_zv_p
26906 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26907 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26908 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26909 && !next->ends_at_zv_p
26910 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26911 {
26912 *end = row;
26913 break;
26914 }
26915 else
26916 {
26917 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26918 but none of the characters it displays are in the range, it is
26919 also END + 1. */
26920 struct glyph *g = next->glyphs[TEXT_AREA];
26921 struct glyph *s = g;
26922 struct glyph *e = g + next->used[TEXT_AREA];
26923
26924 while (g < e)
26925 {
26926 if (((BUFFERP (g->object) || INTEGERP (g->object))
26927 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26928 /* If the buffer position of the first glyph in
26929 the row is equal to END_CHARPOS, it means
26930 the last character to be highlighted is the
26931 newline of ROW, and we must consider NEXT as
26932 END, not END+1. */
26933 || (((!next->reversed_p && g == s)
26934 || (next->reversed_p && g == e - 1))
26935 && (g->charpos == end_charpos
26936 /* Special case for when NEXT is an
26937 empty line at ZV. */
26938 || (g->charpos == -1
26939 && !row->ends_at_zv_p
26940 && next_start == end_charpos)))))
26941 /* A glyph that comes from DISP_STRING is by
26942 definition to be highlighted. */
26943 || EQ (g->object, disp_string))
26944 break;
26945 g++;
26946 }
26947 if (g == e)
26948 {
26949 *end = row;
26950 break;
26951 }
26952 /* The first row that ends at ZV must be the last to be
26953 highlighted. */
26954 else if (next->ends_at_zv_p)
26955 {
26956 *end = next;
26957 break;
26958 }
26959 }
26960 }
26961 }
26962
26963 /* This function sets the mouse_face_* elements of HLINFO, assuming
26964 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
26965 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
26966 for the overlay or run of text properties specifying the mouse
26967 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
26968 before-string and after-string that must also be highlighted.
26969 DISP_STRING, if non-nil, is a display string that may cover some
26970 or all of the highlighted text. */
26971
26972 static void
26973 mouse_face_from_buffer_pos (Lisp_Object window,
26974 Mouse_HLInfo *hlinfo,
26975 ptrdiff_t mouse_charpos,
26976 ptrdiff_t start_charpos,
26977 ptrdiff_t end_charpos,
26978 Lisp_Object before_string,
26979 Lisp_Object after_string,
26980 Lisp_Object disp_string)
26981 {
26982 struct window *w = XWINDOW (window);
26983 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26984 struct glyph_row *r1, *r2;
26985 struct glyph *glyph, *end;
26986 ptrdiff_t ignore, pos;
26987 int x;
26988
26989 eassert (NILP (disp_string) || STRINGP (disp_string));
26990 eassert (NILP (before_string) || STRINGP (before_string));
26991 eassert (NILP (after_string) || STRINGP (after_string));
26992
26993 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
26994 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
26995 if (r1 == NULL)
26996 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
26997 /* If the before-string or display-string contains newlines,
26998 rows_from_pos_range skips to its last row. Move back. */
26999 if (!NILP (before_string) || !NILP (disp_string))
27000 {
27001 struct glyph_row *prev;
27002 while ((prev = r1 - 1, prev >= first)
27003 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
27004 && prev->used[TEXT_AREA] > 0)
27005 {
27006 struct glyph *beg = prev->glyphs[TEXT_AREA];
27007 glyph = beg + prev->used[TEXT_AREA];
27008 while (--glyph >= beg && INTEGERP (glyph->object));
27009 if (glyph < beg
27010 || !(EQ (glyph->object, before_string)
27011 || EQ (glyph->object, disp_string)))
27012 break;
27013 r1 = prev;
27014 }
27015 }
27016 if (r2 == NULL)
27017 {
27018 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27019 hlinfo->mouse_face_past_end = 1;
27020 }
27021 else if (!NILP (after_string))
27022 {
27023 /* If the after-string has newlines, advance to its last row. */
27024 struct glyph_row *next;
27025 struct glyph_row *last
27026 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27027
27028 for (next = r2 + 1;
27029 next <= last
27030 && next->used[TEXT_AREA] > 0
27031 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
27032 ++next)
27033 r2 = next;
27034 }
27035 /* The rest of the display engine assumes that mouse_face_beg_row is
27036 either above mouse_face_end_row or identical to it. But with
27037 bidi-reordered continued lines, the row for START_CHARPOS could
27038 be below the row for END_CHARPOS. If so, swap the rows and store
27039 them in correct order. */
27040 if (r1->y > r2->y)
27041 {
27042 struct glyph_row *tem = r2;
27043
27044 r2 = r1;
27045 r1 = tem;
27046 }
27047
27048 hlinfo->mouse_face_beg_y = r1->y;
27049 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
27050 hlinfo->mouse_face_end_y = r2->y;
27051 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
27052
27053 /* For a bidi-reordered row, the positions of BEFORE_STRING,
27054 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
27055 could be anywhere in the row and in any order. The strategy
27056 below is to find the leftmost and the rightmost glyph that
27057 belongs to either of these 3 strings, or whose position is
27058 between START_CHARPOS and END_CHARPOS, and highlight all the
27059 glyphs between those two. This may cover more than just the text
27060 between START_CHARPOS and END_CHARPOS if the range of characters
27061 strides the bidi level boundary, e.g. if the beginning is in R2L
27062 text while the end is in L2R text or vice versa. */
27063 if (!r1->reversed_p)
27064 {
27065 /* This row is in a left to right paragraph. Scan it left to
27066 right. */
27067 glyph = r1->glyphs[TEXT_AREA];
27068 end = glyph + r1->used[TEXT_AREA];
27069 x = r1->x;
27070
27071 /* Skip truncation glyphs at the start of the glyph row. */
27072 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27073 for (; glyph < end
27074 && INTEGERP (glyph->object)
27075 && glyph->charpos < 0;
27076 ++glyph)
27077 x += glyph->pixel_width;
27078
27079 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27080 or DISP_STRING, and the first glyph from buffer whose
27081 position is between START_CHARPOS and END_CHARPOS. */
27082 for (; glyph < end
27083 && !INTEGERP (glyph->object)
27084 && !EQ (glyph->object, disp_string)
27085 && !(BUFFERP (glyph->object)
27086 && (glyph->charpos >= start_charpos
27087 && glyph->charpos < end_charpos));
27088 ++glyph)
27089 {
27090 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27091 are present at buffer positions between START_CHARPOS and
27092 END_CHARPOS, or if they come from an overlay. */
27093 if (EQ (glyph->object, before_string))
27094 {
27095 pos = string_buffer_position (before_string,
27096 start_charpos);
27097 /* If pos == 0, it means before_string came from an
27098 overlay, not from a buffer position. */
27099 if (!pos || (pos >= start_charpos && pos < end_charpos))
27100 break;
27101 }
27102 else if (EQ (glyph->object, after_string))
27103 {
27104 pos = string_buffer_position (after_string, end_charpos);
27105 if (!pos || (pos >= start_charpos && pos < end_charpos))
27106 break;
27107 }
27108 x += glyph->pixel_width;
27109 }
27110 hlinfo->mouse_face_beg_x = x;
27111 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27112 }
27113 else
27114 {
27115 /* This row is in a right to left paragraph. Scan it right to
27116 left. */
27117 struct glyph *g;
27118
27119 end = r1->glyphs[TEXT_AREA] - 1;
27120 glyph = end + r1->used[TEXT_AREA];
27121
27122 /* Skip truncation glyphs at the start of the glyph row. */
27123 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27124 for (; glyph > end
27125 && INTEGERP (glyph->object)
27126 && glyph->charpos < 0;
27127 --glyph)
27128 ;
27129
27130 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27131 or DISP_STRING, and the first glyph from buffer whose
27132 position is between START_CHARPOS and END_CHARPOS. */
27133 for (; glyph > end
27134 && !INTEGERP (glyph->object)
27135 && !EQ (glyph->object, disp_string)
27136 && !(BUFFERP (glyph->object)
27137 && (glyph->charpos >= start_charpos
27138 && glyph->charpos < end_charpos));
27139 --glyph)
27140 {
27141 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27142 are present at buffer positions between START_CHARPOS and
27143 END_CHARPOS, or if they come from an overlay. */
27144 if (EQ (glyph->object, before_string))
27145 {
27146 pos = string_buffer_position (before_string, start_charpos);
27147 /* If pos == 0, it means before_string came from an
27148 overlay, not from a buffer position. */
27149 if (!pos || (pos >= start_charpos && pos < end_charpos))
27150 break;
27151 }
27152 else if (EQ (glyph->object, after_string))
27153 {
27154 pos = string_buffer_position (after_string, end_charpos);
27155 if (!pos || (pos >= start_charpos && pos < end_charpos))
27156 break;
27157 }
27158 }
27159
27160 glyph++; /* first glyph to the right of the highlighted area */
27161 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
27162 x += g->pixel_width;
27163 hlinfo->mouse_face_beg_x = x;
27164 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27165 }
27166
27167 /* If the highlight ends in a different row, compute GLYPH and END
27168 for the end row. Otherwise, reuse the values computed above for
27169 the row where the highlight begins. */
27170 if (r2 != r1)
27171 {
27172 if (!r2->reversed_p)
27173 {
27174 glyph = r2->glyphs[TEXT_AREA];
27175 end = glyph + r2->used[TEXT_AREA];
27176 x = r2->x;
27177 }
27178 else
27179 {
27180 end = r2->glyphs[TEXT_AREA] - 1;
27181 glyph = end + r2->used[TEXT_AREA];
27182 }
27183 }
27184
27185 if (!r2->reversed_p)
27186 {
27187 /* Skip truncation and continuation glyphs near the end of the
27188 row, and also blanks and stretch glyphs inserted by
27189 extend_face_to_end_of_line. */
27190 while (end > glyph
27191 && INTEGERP ((end - 1)->object))
27192 --end;
27193 /* Scan the rest of the glyph row from the end, looking for the
27194 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27195 DISP_STRING, or whose position is between START_CHARPOS
27196 and END_CHARPOS */
27197 for (--end;
27198 end > glyph
27199 && !INTEGERP (end->object)
27200 && !EQ (end->object, disp_string)
27201 && !(BUFFERP (end->object)
27202 && (end->charpos >= start_charpos
27203 && end->charpos < end_charpos));
27204 --end)
27205 {
27206 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27207 are present at buffer positions between START_CHARPOS and
27208 END_CHARPOS, or if they come from an overlay. */
27209 if (EQ (end->object, before_string))
27210 {
27211 pos = string_buffer_position (before_string, start_charpos);
27212 if (!pos || (pos >= start_charpos && pos < end_charpos))
27213 break;
27214 }
27215 else if (EQ (end->object, after_string))
27216 {
27217 pos = string_buffer_position (after_string, end_charpos);
27218 if (!pos || (pos >= start_charpos && pos < end_charpos))
27219 break;
27220 }
27221 }
27222 /* Find the X coordinate of the last glyph to be highlighted. */
27223 for (; glyph <= end; ++glyph)
27224 x += glyph->pixel_width;
27225
27226 hlinfo->mouse_face_end_x = x;
27227 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
27228 }
27229 else
27230 {
27231 /* Skip truncation and continuation glyphs near the end of the
27232 row, and also blanks and stretch glyphs inserted by
27233 extend_face_to_end_of_line. */
27234 x = r2->x;
27235 end++;
27236 while (end < glyph
27237 && INTEGERP (end->object))
27238 {
27239 x += end->pixel_width;
27240 ++end;
27241 }
27242 /* Scan the rest of the glyph row from the end, looking for the
27243 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27244 DISP_STRING, or whose position is between START_CHARPOS
27245 and END_CHARPOS */
27246 for ( ;
27247 end < glyph
27248 && !INTEGERP (end->object)
27249 && !EQ (end->object, disp_string)
27250 && !(BUFFERP (end->object)
27251 && (end->charpos >= start_charpos
27252 && end->charpos < end_charpos));
27253 ++end)
27254 {
27255 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27256 are present at buffer positions between START_CHARPOS and
27257 END_CHARPOS, or if they come from an overlay. */
27258 if (EQ (end->object, before_string))
27259 {
27260 pos = string_buffer_position (before_string, start_charpos);
27261 if (!pos || (pos >= start_charpos && pos < end_charpos))
27262 break;
27263 }
27264 else if (EQ (end->object, after_string))
27265 {
27266 pos = string_buffer_position (after_string, end_charpos);
27267 if (!pos || (pos >= start_charpos && pos < end_charpos))
27268 break;
27269 }
27270 x += end->pixel_width;
27271 }
27272 /* If we exited the above loop because we arrived at the last
27273 glyph of the row, and its buffer position is still not in
27274 range, it means the last character in range is the preceding
27275 newline. Bump the end column and x values to get past the
27276 last glyph. */
27277 if (end == glyph
27278 && BUFFERP (end->object)
27279 && (end->charpos < start_charpos
27280 || end->charpos >= end_charpos))
27281 {
27282 x += end->pixel_width;
27283 ++end;
27284 }
27285 hlinfo->mouse_face_end_x = x;
27286 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
27287 }
27288
27289 hlinfo->mouse_face_window = window;
27290 hlinfo->mouse_face_face_id
27291 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
27292 mouse_charpos + 1,
27293 !hlinfo->mouse_face_hidden, -1);
27294 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27295 }
27296
27297 /* The following function is not used anymore (replaced with
27298 mouse_face_from_string_pos), but I leave it here for the time
27299 being, in case someone would. */
27300
27301 #if 0 /* not used */
27302
27303 /* Find the position of the glyph for position POS in OBJECT in
27304 window W's current matrix, and return in *X, *Y the pixel
27305 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
27306
27307 RIGHT_P non-zero means return the position of the right edge of the
27308 glyph, RIGHT_P zero means return the left edge position.
27309
27310 If no glyph for POS exists in the matrix, return the position of
27311 the glyph with the next smaller position that is in the matrix, if
27312 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
27313 exists in the matrix, return the position of the glyph with the
27314 next larger position in OBJECT.
27315
27316 Value is non-zero if a glyph was found. */
27317
27318 static int
27319 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
27320 int *hpos, int *vpos, int *x, int *y, int right_p)
27321 {
27322 int yb = window_text_bottom_y (w);
27323 struct glyph_row *r;
27324 struct glyph *best_glyph = NULL;
27325 struct glyph_row *best_row = NULL;
27326 int best_x = 0;
27327
27328 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27329 r->enabled_p && r->y < yb;
27330 ++r)
27331 {
27332 struct glyph *g = r->glyphs[TEXT_AREA];
27333 struct glyph *e = g + r->used[TEXT_AREA];
27334 int gx;
27335
27336 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27337 if (EQ (g->object, object))
27338 {
27339 if (g->charpos == pos)
27340 {
27341 best_glyph = g;
27342 best_x = gx;
27343 best_row = r;
27344 goto found;
27345 }
27346 else if (best_glyph == NULL
27347 || ((eabs (g->charpos - pos)
27348 < eabs (best_glyph->charpos - pos))
27349 && (right_p
27350 ? g->charpos < pos
27351 : g->charpos > pos)))
27352 {
27353 best_glyph = g;
27354 best_x = gx;
27355 best_row = r;
27356 }
27357 }
27358 }
27359
27360 found:
27361
27362 if (best_glyph)
27363 {
27364 *x = best_x;
27365 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
27366
27367 if (right_p)
27368 {
27369 *x += best_glyph->pixel_width;
27370 ++*hpos;
27371 }
27372
27373 *y = best_row->y;
27374 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
27375 }
27376
27377 return best_glyph != NULL;
27378 }
27379 #endif /* not used */
27380
27381 /* Find the positions of the first and the last glyphs in window W's
27382 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
27383 (assumed to be a string), and return in HLINFO's mouse_face_*
27384 members the pixel and column/row coordinates of those glyphs. */
27385
27386 static void
27387 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
27388 Lisp_Object object,
27389 ptrdiff_t startpos, ptrdiff_t endpos)
27390 {
27391 int yb = window_text_bottom_y (w);
27392 struct glyph_row *r;
27393 struct glyph *g, *e;
27394 int gx;
27395 int found = 0;
27396
27397 /* Find the glyph row with at least one position in the range
27398 [STARTPOS..ENDPOS], and the first glyph in that row whose
27399 position belongs to that range. */
27400 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27401 r->enabled_p && r->y < yb;
27402 ++r)
27403 {
27404 if (!r->reversed_p)
27405 {
27406 g = r->glyphs[TEXT_AREA];
27407 e = g + r->used[TEXT_AREA];
27408 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27409 if (EQ (g->object, object)
27410 && startpos <= g->charpos && g->charpos <= endpos)
27411 {
27412 hlinfo->mouse_face_beg_row
27413 = MATRIX_ROW_VPOS (r, w->current_matrix);
27414 hlinfo->mouse_face_beg_y = r->y;
27415 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27416 hlinfo->mouse_face_beg_x = gx;
27417 found = 1;
27418 break;
27419 }
27420 }
27421 else
27422 {
27423 struct glyph *g1;
27424
27425 e = r->glyphs[TEXT_AREA];
27426 g = e + r->used[TEXT_AREA];
27427 for ( ; g > e; --g)
27428 if (EQ ((g-1)->object, object)
27429 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
27430 {
27431 hlinfo->mouse_face_beg_row
27432 = MATRIX_ROW_VPOS (r, w->current_matrix);
27433 hlinfo->mouse_face_beg_y = r->y;
27434 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27435 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27436 gx += g1->pixel_width;
27437 hlinfo->mouse_face_beg_x = gx;
27438 found = 1;
27439 break;
27440 }
27441 }
27442 if (found)
27443 break;
27444 }
27445
27446 if (!found)
27447 return;
27448
27449 /* Starting with the next row, look for the first row which does NOT
27450 include any glyphs whose positions are in the range. */
27451 for (++r; r->enabled_p && r->y < yb; ++r)
27452 {
27453 g = r->glyphs[TEXT_AREA];
27454 e = g + r->used[TEXT_AREA];
27455 found = 0;
27456 for ( ; g < e; ++g)
27457 if (EQ (g->object, object)
27458 && startpos <= g->charpos && g->charpos <= endpos)
27459 {
27460 found = 1;
27461 break;
27462 }
27463 if (!found)
27464 break;
27465 }
27466
27467 /* The highlighted region ends on the previous row. */
27468 r--;
27469
27470 /* Set the end row and its vertical pixel coordinate. */
27471 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
27472 hlinfo->mouse_face_end_y = r->y;
27473
27474 /* Compute and set the end column and the end column's horizontal
27475 pixel coordinate. */
27476 if (!r->reversed_p)
27477 {
27478 g = r->glyphs[TEXT_AREA];
27479 e = g + r->used[TEXT_AREA];
27480 for ( ; e > g; --e)
27481 if (EQ ((e-1)->object, object)
27482 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
27483 break;
27484 hlinfo->mouse_face_end_col = e - g;
27485
27486 for (gx = r->x; g < e; ++g)
27487 gx += g->pixel_width;
27488 hlinfo->mouse_face_end_x = gx;
27489 }
27490 else
27491 {
27492 e = r->glyphs[TEXT_AREA];
27493 g = e + r->used[TEXT_AREA];
27494 for (gx = r->x ; e < g; ++e)
27495 {
27496 if (EQ (e->object, object)
27497 && startpos <= e->charpos && e->charpos <= endpos)
27498 break;
27499 gx += e->pixel_width;
27500 }
27501 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27502 hlinfo->mouse_face_end_x = gx;
27503 }
27504 }
27505
27506 #ifdef HAVE_WINDOW_SYSTEM
27507
27508 /* See if position X, Y is within a hot-spot of an image. */
27509
27510 static int
27511 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27512 {
27513 if (!CONSP (hot_spot))
27514 return 0;
27515
27516 if (EQ (XCAR (hot_spot), Qrect))
27517 {
27518 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27519 Lisp_Object rect = XCDR (hot_spot);
27520 Lisp_Object tem;
27521 if (!CONSP (rect))
27522 return 0;
27523 if (!CONSP (XCAR (rect)))
27524 return 0;
27525 if (!CONSP (XCDR (rect)))
27526 return 0;
27527 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27528 return 0;
27529 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27530 return 0;
27531 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27532 return 0;
27533 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27534 return 0;
27535 return 1;
27536 }
27537 else if (EQ (XCAR (hot_spot), Qcircle))
27538 {
27539 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27540 Lisp_Object circ = XCDR (hot_spot);
27541 Lisp_Object lr, lx0, ly0;
27542 if (CONSP (circ)
27543 && CONSP (XCAR (circ))
27544 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27545 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27546 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27547 {
27548 double r = XFLOATINT (lr);
27549 double dx = XINT (lx0) - x;
27550 double dy = XINT (ly0) - y;
27551 return (dx * dx + dy * dy <= r * r);
27552 }
27553 }
27554 else if (EQ (XCAR (hot_spot), Qpoly))
27555 {
27556 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27557 if (VECTORP (XCDR (hot_spot)))
27558 {
27559 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27560 Lisp_Object *poly = v->contents;
27561 ptrdiff_t n = v->header.size;
27562 ptrdiff_t i;
27563 int inside = 0;
27564 Lisp_Object lx, ly;
27565 int x0, y0;
27566
27567 /* Need an even number of coordinates, and at least 3 edges. */
27568 if (n < 6 || n & 1)
27569 return 0;
27570
27571 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27572 If count is odd, we are inside polygon. Pixels on edges
27573 may or may not be included depending on actual geometry of the
27574 polygon. */
27575 if ((lx = poly[n-2], !INTEGERP (lx))
27576 || (ly = poly[n-1], !INTEGERP (lx)))
27577 return 0;
27578 x0 = XINT (lx), y0 = XINT (ly);
27579 for (i = 0; i < n; i += 2)
27580 {
27581 int x1 = x0, y1 = y0;
27582 if ((lx = poly[i], !INTEGERP (lx))
27583 || (ly = poly[i+1], !INTEGERP (ly)))
27584 return 0;
27585 x0 = XINT (lx), y0 = XINT (ly);
27586
27587 /* Does this segment cross the X line? */
27588 if (x0 >= x)
27589 {
27590 if (x1 >= x)
27591 continue;
27592 }
27593 else if (x1 < x)
27594 continue;
27595 if (y > y0 && y > y1)
27596 continue;
27597 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27598 inside = !inside;
27599 }
27600 return inside;
27601 }
27602 }
27603 return 0;
27604 }
27605
27606 Lisp_Object
27607 find_hot_spot (Lisp_Object map, int x, int y)
27608 {
27609 while (CONSP (map))
27610 {
27611 if (CONSP (XCAR (map))
27612 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27613 return XCAR (map);
27614 map = XCDR (map);
27615 }
27616
27617 return Qnil;
27618 }
27619
27620 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27621 3, 3, 0,
27622 doc: /* Lookup in image map MAP coordinates X and Y.
27623 An image map is an alist where each element has the format (AREA ID PLIST).
27624 An AREA is specified as either a rectangle, a circle, or a polygon:
27625 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27626 pixel coordinates of the upper left and bottom right corners.
27627 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27628 and the radius of the circle; r may be a float or integer.
27629 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27630 vector describes one corner in the polygon.
27631 Returns the alist element for the first matching AREA in MAP. */)
27632 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27633 {
27634 if (NILP (map))
27635 return Qnil;
27636
27637 CHECK_NUMBER (x);
27638 CHECK_NUMBER (y);
27639
27640 return find_hot_spot (map,
27641 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27642 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27643 }
27644
27645
27646 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27647 static void
27648 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27649 {
27650 /* Do not change cursor shape while dragging mouse. */
27651 if (!NILP (do_mouse_tracking))
27652 return;
27653
27654 if (!NILP (pointer))
27655 {
27656 if (EQ (pointer, Qarrow))
27657 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27658 else if (EQ (pointer, Qhand))
27659 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27660 else if (EQ (pointer, Qtext))
27661 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27662 else if (EQ (pointer, intern ("hdrag")))
27663 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27664 #ifdef HAVE_X_WINDOWS
27665 else if (EQ (pointer, intern ("vdrag")))
27666 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27667 #endif
27668 else if (EQ (pointer, intern ("hourglass")))
27669 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27670 else if (EQ (pointer, Qmodeline))
27671 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27672 else
27673 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27674 }
27675
27676 if (cursor != No_Cursor)
27677 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27678 }
27679
27680 #endif /* HAVE_WINDOW_SYSTEM */
27681
27682 /* Take proper action when mouse has moved to the mode or header line
27683 or marginal area AREA of window W, x-position X and y-position Y.
27684 X is relative to the start of the text display area of W, so the
27685 width of bitmap areas and scroll bars must be subtracted to get a
27686 position relative to the start of the mode line. */
27687
27688 static void
27689 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27690 enum window_part area)
27691 {
27692 struct window *w = XWINDOW (window);
27693 struct frame *f = XFRAME (w->frame);
27694 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27695 #ifdef HAVE_WINDOW_SYSTEM
27696 Display_Info *dpyinfo;
27697 #endif
27698 Cursor cursor = No_Cursor;
27699 Lisp_Object pointer = Qnil;
27700 int dx, dy, width, height;
27701 ptrdiff_t charpos;
27702 Lisp_Object string, object = Qnil;
27703 Lisp_Object pos IF_LINT (= Qnil), help;
27704
27705 Lisp_Object mouse_face;
27706 int original_x_pixel = x;
27707 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27708 struct glyph_row *row IF_LINT (= 0);
27709
27710 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27711 {
27712 int x0;
27713 struct glyph *end;
27714
27715 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27716 returns them in row/column units! */
27717 string = mode_line_string (w, area, &x, &y, &charpos,
27718 &object, &dx, &dy, &width, &height);
27719
27720 row = (area == ON_MODE_LINE
27721 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27722 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27723
27724 /* Find the glyph under the mouse pointer. */
27725 if (row->mode_line_p && row->enabled_p)
27726 {
27727 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27728 end = glyph + row->used[TEXT_AREA];
27729
27730 for (x0 = original_x_pixel;
27731 glyph < end && x0 >= glyph->pixel_width;
27732 ++glyph)
27733 x0 -= glyph->pixel_width;
27734
27735 if (glyph >= end)
27736 glyph = NULL;
27737 }
27738 }
27739 else
27740 {
27741 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27742 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27743 returns them in row/column units! */
27744 string = marginal_area_string (w, area, &x, &y, &charpos,
27745 &object, &dx, &dy, &width, &height);
27746 }
27747
27748 help = Qnil;
27749
27750 #ifdef HAVE_WINDOW_SYSTEM
27751 if (IMAGEP (object))
27752 {
27753 Lisp_Object image_map, hotspot;
27754 if ((image_map = Fplist_get (XCDR (object), QCmap),
27755 !NILP (image_map))
27756 && (hotspot = find_hot_spot (image_map, dx, dy),
27757 CONSP (hotspot))
27758 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27759 {
27760 Lisp_Object plist;
27761
27762 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27763 If so, we could look for mouse-enter, mouse-leave
27764 properties in PLIST (and do something...). */
27765 hotspot = XCDR (hotspot);
27766 if (CONSP (hotspot)
27767 && (plist = XCAR (hotspot), CONSP (plist)))
27768 {
27769 pointer = Fplist_get (plist, Qpointer);
27770 if (NILP (pointer))
27771 pointer = Qhand;
27772 help = Fplist_get (plist, Qhelp_echo);
27773 if (!NILP (help))
27774 {
27775 help_echo_string = help;
27776 XSETWINDOW (help_echo_window, w);
27777 help_echo_object = w->contents;
27778 help_echo_pos = charpos;
27779 }
27780 }
27781 }
27782 if (NILP (pointer))
27783 pointer = Fplist_get (XCDR (object), QCpointer);
27784 }
27785 #endif /* HAVE_WINDOW_SYSTEM */
27786
27787 if (STRINGP (string))
27788 pos = make_number (charpos);
27789
27790 /* Set the help text and mouse pointer. If the mouse is on a part
27791 of the mode line without any text (e.g. past the right edge of
27792 the mode line text), use the default help text and pointer. */
27793 if (STRINGP (string) || area == ON_MODE_LINE)
27794 {
27795 /* Arrange to display the help by setting the global variables
27796 help_echo_string, help_echo_object, and help_echo_pos. */
27797 if (NILP (help))
27798 {
27799 if (STRINGP (string))
27800 help = Fget_text_property (pos, Qhelp_echo, string);
27801
27802 if (!NILP (help))
27803 {
27804 help_echo_string = help;
27805 XSETWINDOW (help_echo_window, w);
27806 help_echo_object = string;
27807 help_echo_pos = charpos;
27808 }
27809 else if (area == ON_MODE_LINE)
27810 {
27811 Lisp_Object default_help
27812 = buffer_local_value_1 (Qmode_line_default_help_echo,
27813 w->contents);
27814
27815 if (STRINGP (default_help))
27816 {
27817 help_echo_string = default_help;
27818 XSETWINDOW (help_echo_window, w);
27819 help_echo_object = Qnil;
27820 help_echo_pos = -1;
27821 }
27822 }
27823 }
27824
27825 #ifdef HAVE_WINDOW_SYSTEM
27826 /* Change the mouse pointer according to what is under it. */
27827 if (FRAME_WINDOW_P (f))
27828 {
27829 dpyinfo = FRAME_X_DISPLAY_INFO (f);
27830 if (STRINGP (string))
27831 {
27832 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27833
27834 if (NILP (pointer))
27835 pointer = Fget_text_property (pos, Qpointer, string);
27836
27837 /* Change the mouse pointer according to what is under X/Y. */
27838 if (NILP (pointer)
27839 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27840 {
27841 Lisp_Object map;
27842 map = Fget_text_property (pos, Qlocal_map, string);
27843 if (!KEYMAPP (map))
27844 map = Fget_text_property (pos, Qkeymap, string);
27845 if (!KEYMAPP (map))
27846 cursor = dpyinfo->vertical_scroll_bar_cursor;
27847 }
27848 }
27849 else
27850 /* Default mode-line pointer. */
27851 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27852 }
27853 #endif
27854 }
27855
27856 /* Change the mouse face according to what is under X/Y. */
27857 if (STRINGP (string))
27858 {
27859 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27860 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
27861 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27862 && glyph)
27863 {
27864 Lisp_Object b, e;
27865
27866 struct glyph * tmp_glyph;
27867
27868 int gpos;
27869 int gseq_length;
27870 int total_pixel_width;
27871 ptrdiff_t begpos, endpos, ignore;
27872
27873 int vpos, hpos;
27874
27875 b = Fprevious_single_property_change (make_number (charpos + 1),
27876 Qmouse_face, string, Qnil);
27877 if (NILP (b))
27878 begpos = 0;
27879 else
27880 begpos = XINT (b);
27881
27882 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27883 if (NILP (e))
27884 endpos = SCHARS (string);
27885 else
27886 endpos = XINT (e);
27887
27888 /* Calculate the glyph position GPOS of GLYPH in the
27889 displayed string, relative to the beginning of the
27890 highlighted part of the string.
27891
27892 Note: GPOS is different from CHARPOS. CHARPOS is the
27893 position of GLYPH in the internal string object. A mode
27894 line string format has structures which are converted to
27895 a flattened string by the Emacs Lisp interpreter. The
27896 internal string is an element of those structures. The
27897 displayed string is the flattened string. */
27898 tmp_glyph = row_start_glyph;
27899 while (tmp_glyph < glyph
27900 && (!(EQ (tmp_glyph->object, glyph->object)
27901 && begpos <= tmp_glyph->charpos
27902 && tmp_glyph->charpos < endpos)))
27903 tmp_glyph++;
27904 gpos = glyph - tmp_glyph;
27905
27906 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27907 the highlighted part of the displayed string to which
27908 GLYPH belongs. Note: GSEQ_LENGTH is different from
27909 SCHARS (STRING), because the latter returns the length of
27910 the internal string. */
27911 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27912 tmp_glyph > glyph
27913 && (!(EQ (tmp_glyph->object, glyph->object)
27914 && begpos <= tmp_glyph->charpos
27915 && tmp_glyph->charpos < endpos));
27916 tmp_glyph--)
27917 ;
27918 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27919
27920 /* Calculate the total pixel width of all the glyphs between
27921 the beginning of the highlighted area and GLYPH. */
27922 total_pixel_width = 0;
27923 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27924 total_pixel_width += tmp_glyph->pixel_width;
27925
27926 /* Pre calculation of re-rendering position. Note: X is in
27927 column units here, after the call to mode_line_string or
27928 marginal_area_string. */
27929 hpos = x - gpos;
27930 vpos = (area == ON_MODE_LINE
27931 ? (w->current_matrix)->nrows - 1
27932 : 0);
27933
27934 /* If GLYPH's position is included in the region that is
27935 already drawn in mouse face, we have nothing to do. */
27936 if ( EQ (window, hlinfo->mouse_face_window)
27937 && (!row->reversed_p
27938 ? (hlinfo->mouse_face_beg_col <= hpos
27939 && hpos < hlinfo->mouse_face_end_col)
27940 /* In R2L rows we swap BEG and END, see below. */
27941 : (hlinfo->mouse_face_end_col <= hpos
27942 && hpos < hlinfo->mouse_face_beg_col))
27943 && hlinfo->mouse_face_beg_row == vpos )
27944 return;
27945
27946 if (clear_mouse_face (hlinfo))
27947 cursor = No_Cursor;
27948
27949 if (!row->reversed_p)
27950 {
27951 hlinfo->mouse_face_beg_col = hpos;
27952 hlinfo->mouse_face_beg_x = original_x_pixel
27953 - (total_pixel_width + dx);
27954 hlinfo->mouse_face_end_col = hpos + gseq_length;
27955 hlinfo->mouse_face_end_x = 0;
27956 }
27957 else
27958 {
27959 /* In R2L rows, show_mouse_face expects BEG and END
27960 coordinates to be swapped. */
27961 hlinfo->mouse_face_end_col = hpos;
27962 hlinfo->mouse_face_end_x = original_x_pixel
27963 - (total_pixel_width + dx);
27964 hlinfo->mouse_face_beg_col = hpos + gseq_length;
27965 hlinfo->mouse_face_beg_x = 0;
27966 }
27967
27968 hlinfo->mouse_face_beg_row = vpos;
27969 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
27970 hlinfo->mouse_face_beg_y = 0;
27971 hlinfo->mouse_face_end_y = 0;
27972 hlinfo->mouse_face_past_end = 0;
27973 hlinfo->mouse_face_window = window;
27974
27975 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
27976 charpos,
27977 0, 0, 0,
27978 &ignore,
27979 glyph->face_id,
27980 1);
27981 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27982
27983 if (NILP (pointer))
27984 pointer = Qhand;
27985 }
27986 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27987 clear_mouse_face (hlinfo);
27988 }
27989 #ifdef HAVE_WINDOW_SYSTEM
27990 if (FRAME_WINDOW_P (f))
27991 define_frame_cursor1 (f, cursor, pointer);
27992 #endif
27993 }
27994
27995
27996 /* EXPORT:
27997 Take proper action when the mouse has moved to position X, Y on
27998 frame F with regards to highlighting portions of display that have
27999 mouse-face properties. Also de-highlight portions of display where
28000 the mouse was before, set the mouse pointer shape as appropriate
28001 for the mouse coordinates, and activate help echo (tooltips).
28002 X and Y can be negative or out of range. */
28003
28004 void
28005 note_mouse_highlight (struct frame *f, int x, int y)
28006 {
28007 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28008 enum window_part part = ON_NOTHING;
28009 Lisp_Object window;
28010 struct window *w;
28011 Cursor cursor = No_Cursor;
28012 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
28013 struct buffer *b;
28014
28015 /* When a menu is active, don't highlight because this looks odd. */
28016 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
28017 if (popup_activated ())
28018 return;
28019 #endif
28020
28021 if (!f->glyphs_initialized_p
28022 || f->pointer_invisible)
28023 return;
28024
28025 hlinfo->mouse_face_mouse_x = x;
28026 hlinfo->mouse_face_mouse_y = y;
28027 hlinfo->mouse_face_mouse_frame = f;
28028
28029 if (hlinfo->mouse_face_defer)
28030 return;
28031
28032 /* Which window is that in? */
28033 window = window_from_coordinates (f, x, y, &part, 1);
28034
28035 /* If displaying active text in another window, clear that. */
28036 if (! EQ (window, hlinfo->mouse_face_window)
28037 /* Also clear if we move out of text area in same window. */
28038 || (!NILP (hlinfo->mouse_face_window)
28039 && !NILP (window)
28040 && part != ON_TEXT
28041 && part != ON_MODE_LINE
28042 && part != ON_HEADER_LINE))
28043 clear_mouse_face (hlinfo);
28044
28045 /* Not on a window -> return. */
28046 if (!WINDOWP (window))
28047 return;
28048
28049 /* Reset help_echo_string. It will get recomputed below. */
28050 help_echo_string = Qnil;
28051
28052 /* Convert to window-relative pixel coordinates. */
28053 w = XWINDOW (window);
28054 frame_to_window_pixel_xy (w, &x, &y);
28055
28056 #ifdef HAVE_WINDOW_SYSTEM
28057 /* Handle tool-bar window differently since it doesn't display a
28058 buffer. */
28059 if (EQ (window, f->tool_bar_window))
28060 {
28061 note_tool_bar_highlight (f, x, y);
28062 return;
28063 }
28064 #endif
28065
28066 /* Mouse is on the mode, header line or margin? */
28067 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
28068 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
28069 {
28070 note_mode_line_or_margin_highlight (window, x, y, part);
28071 return;
28072 }
28073
28074 #ifdef HAVE_WINDOW_SYSTEM
28075 if (part == ON_VERTICAL_BORDER)
28076 {
28077 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28078 help_echo_string = build_string ("drag-mouse-1: resize");
28079 }
28080 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
28081 || part == ON_SCROLL_BAR)
28082 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28083 else
28084 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28085 #endif
28086
28087 /* Are we in a window whose display is up to date?
28088 And verify the buffer's text has not changed. */
28089 b = XBUFFER (w->contents);
28090 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
28091 {
28092 int hpos, vpos, dx, dy, area = LAST_AREA;
28093 ptrdiff_t pos;
28094 struct glyph *glyph;
28095 Lisp_Object object;
28096 Lisp_Object mouse_face = Qnil, position;
28097 Lisp_Object *overlay_vec = NULL;
28098 ptrdiff_t i, noverlays;
28099 struct buffer *obuf;
28100 ptrdiff_t obegv, ozv;
28101 int same_region;
28102
28103 /* Find the glyph under X/Y. */
28104 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
28105
28106 #ifdef HAVE_WINDOW_SYSTEM
28107 /* Look for :pointer property on image. */
28108 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28109 {
28110 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
28111 if (img != NULL && IMAGEP (img->spec))
28112 {
28113 Lisp_Object image_map, hotspot;
28114 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
28115 !NILP (image_map))
28116 && (hotspot = find_hot_spot (image_map,
28117 glyph->slice.img.x + dx,
28118 glyph->slice.img.y + dy),
28119 CONSP (hotspot))
28120 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28121 {
28122 Lisp_Object plist;
28123
28124 /* Could check XCAR (hotspot) to see if we enter/leave
28125 this hot-spot.
28126 If so, we could look for mouse-enter, mouse-leave
28127 properties in PLIST (and do something...). */
28128 hotspot = XCDR (hotspot);
28129 if (CONSP (hotspot)
28130 && (plist = XCAR (hotspot), CONSP (plist)))
28131 {
28132 pointer = Fplist_get (plist, Qpointer);
28133 if (NILP (pointer))
28134 pointer = Qhand;
28135 help_echo_string = Fplist_get (plist, Qhelp_echo);
28136 if (!NILP (help_echo_string))
28137 {
28138 help_echo_window = window;
28139 help_echo_object = glyph->object;
28140 help_echo_pos = glyph->charpos;
28141 }
28142 }
28143 }
28144 if (NILP (pointer))
28145 pointer = Fplist_get (XCDR (img->spec), QCpointer);
28146 }
28147 }
28148 #endif /* HAVE_WINDOW_SYSTEM */
28149
28150 /* Clear mouse face if X/Y not over text. */
28151 if (glyph == NULL
28152 || area != TEXT_AREA
28153 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
28154 /* Glyph's OBJECT is an integer for glyphs inserted by the
28155 display engine for its internal purposes, like truncation
28156 and continuation glyphs and blanks beyond the end of
28157 line's text on text terminals. If we are over such a
28158 glyph, we are not over any text. */
28159 || INTEGERP (glyph->object)
28160 /* R2L rows have a stretch glyph at their front, which
28161 stands for no text, whereas L2R rows have no glyphs at
28162 all beyond the end of text. Treat such stretch glyphs
28163 like we do with NULL glyphs in L2R rows. */
28164 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
28165 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
28166 && glyph->type == STRETCH_GLYPH
28167 && glyph->avoid_cursor_p))
28168 {
28169 if (clear_mouse_face (hlinfo))
28170 cursor = No_Cursor;
28171 #ifdef HAVE_WINDOW_SYSTEM
28172 if (FRAME_WINDOW_P (f) && NILP (pointer))
28173 {
28174 if (area != TEXT_AREA)
28175 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28176 else
28177 pointer = Vvoid_text_area_pointer;
28178 }
28179 #endif
28180 goto set_cursor;
28181 }
28182
28183 pos = glyph->charpos;
28184 object = glyph->object;
28185 if (!STRINGP (object) && !BUFFERP (object))
28186 goto set_cursor;
28187
28188 /* If we get an out-of-range value, return now; avoid an error. */
28189 if (BUFFERP (object) && pos > BUF_Z (b))
28190 goto set_cursor;
28191
28192 /* Make the window's buffer temporarily current for
28193 overlays_at and compute_char_face. */
28194 obuf = current_buffer;
28195 current_buffer = b;
28196 obegv = BEGV;
28197 ozv = ZV;
28198 BEGV = BEG;
28199 ZV = Z;
28200
28201 /* Is this char mouse-active or does it have help-echo? */
28202 position = make_number (pos);
28203
28204 if (BUFFERP (object))
28205 {
28206 /* Put all the overlays we want in a vector in overlay_vec. */
28207 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
28208 /* Sort overlays into increasing priority order. */
28209 noverlays = sort_overlays (overlay_vec, noverlays, w);
28210 }
28211 else
28212 noverlays = 0;
28213
28214 if (NILP (Vmouse_highlight))
28215 {
28216 clear_mouse_face (hlinfo);
28217 goto check_help_echo;
28218 }
28219
28220 same_region = coords_in_mouse_face_p (w, hpos, vpos);
28221
28222 if (same_region)
28223 cursor = No_Cursor;
28224
28225 /* Check mouse-face highlighting. */
28226 if (! same_region
28227 /* If there exists an overlay with mouse-face overlapping
28228 the one we are currently highlighting, we have to
28229 check if we enter the overlapping overlay, and then
28230 highlight only that. */
28231 || (OVERLAYP (hlinfo->mouse_face_overlay)
28232 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
28233 {
28234 /* Find the highest priority overlay with a mouse-face. */
28235 Lisp_Object overlay = Qnil;
28236 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
28237 {
28238 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
28239 if (!NILP (mouse_face))
28240 overlay = overlay_vec[i];
28241 }
28242
28243 /* If we're highlighting the same overlay as before, there's
28244 no need to do that again. */
28245 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
28246 goto check_help_echo;
28247 hlinfo->mouse_face_overlay = overlay;
28248
28249 /* Clear the display of the old active region, if any. */
28250 if (clear_mouse_face (hlinfo))
28251 cursor = No_Cursor;
28252
28253 /* If no overlay applies, get a text property. */
28254 if (NILP (overlay))
28255 mouse_face = Fget_text_property (position, Qmouse_face, object);
28256
28257 /* Next, compute the bounds of the mouse highlighting and
28258 display it. */
28259 if (!NILP (mouse_face) && STRINGP (object))
28260 {
28261 /* The mouse-highlighting comes from a display string
28262 with a mouse-face. */
28263 Lisp_Object s, e;
28264 ptrdiff_t ignore;
28265
28266 s = Fprevious_single_property_change
28267 (make_number (pos + 1), Qmouse_face, object, Qnil);
28268 e = Fnext_single_property_change
28269 (position, Qmouse_face, object, Qnil);
28270 if (NILP (s))
28271 s = make_number (0);
28272 if (NILP (e))
28273 e = make_number (SCHARS (object) - 1);
28274 mouse_face_from_string_pos (w, hlinfo, object,
28275 XINT (s), XINT (e));
28276 hlinfo->mouse_face_past_end = 0;
28277 hlinfo->mouse_face_window = window;
28278 hlinfo->mouse_face_face_id
28279 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
28280 glyph->face_id, 1);
28281 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28282 cursor = No_Cursor;
28283 }
28284 else
28285 {
28286 /* The mouse-highlighting, if any, comes from an overlay
28287 or text property in the buffer. */
28288 Lisp_Object buffer IF_LINT (= Qnil);
28289 Lisp_Object disp_string IF_LINT (= Qnil);
28290
28291 if (STRINGP (object))
28292 {
28293 /* If we are on a display string with no mouse-face,
28294 check if the text under it has one. */
28295 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
28296 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28297 pos = string_buffer_position (object, start);
28298 if (pos > 0)
28299 {
28300 mouse_face = get_char_property_and_overlay
28301 (make_number (pos), Qmouse_face, w->contents, &overlay);
28302 buffer = w->contents;
28303 disp_string = object;
28304 }
28305 }
28306 else
28307 {
28308 buffer = object;
28309 disp_string = Qnil;
28310 }
28311
28312 if (!NILP (mouse_face))
28313 {
28314 Lisp_Object before, after;
28315 Lisp_Object before_string, after_string;
28316 /* To correctly find the limits of mouse highlight
28317 in a bidi-reordered buffer, we must not use the
28318 optimization of limiting the search in
28319 previous-single-property-change and
28320 next-single-property-change, because
28321 rows_from_pos_range needs the real start and end
28322 positions to DTRT in this case. That's because
28323 the first row visible in a window does not
28324 necessarily display the character whose position
28325 is the smallest. */
28326 Lisp_Object lim1 =
28327 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28328 ? Fmarker_position (w->start)
28329 : Qnil;
28330 Lisp_Object lim2 =
28331 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28332 ? make_number (BUF_Z (XBUFFER (buffer)) - w->window_end_pos)
28333 : Qnil;
28334
28335 if (NILP (overlay))
28336 {
28337 /* Handle the text property case. */
28338 before = Fprevious_single_property_change
28339 (make_number (pos + 1), Qmouse_face, buffer, lim1);
28340 after = Fnext_single_property_change
28341 (make_number (pos), Qmouse_face, buffer, lim2);
28342 before_string = after_string = Qnil;
28343 }
28344 else
28345 {
28346 /* Handle the overlay case. */
28347 before = Foverlay_start (overlay);
28348 after = Foverlay_end (overlay);
28349 before_string = Foverlay_get (overlay, Qbefore_string);
28350 after_string = Foverlay_get (overlay, Qafter_string);
28351
28352 if (!STRINGP (before_string)) before_string = Qnil;
28353 if (!STRINGP (after_string)) after_string = Qnil;
28354 }
28355
28356 mouse_face_from_buffer_pos (window, hlinfo, pos,
28357 NILP (before)
28358 ? 1
28359 : XFASTINT (before),
28360 NILP (after)
28361 ? BUF_Z (XBUFFER (buffer))
28362 : XFASTINT (after),
28363 before_string, after_string,
28364 disp_string);
28365 cursor = No_Cursor;
28366 }
28367 }
28368 }
28369
28370 check_help_echo:
28371
28372 /* Look for a `help-echo' property. */
28373 if (NILP (help_echo_string)) {
28374 Lisp_Object help, overlay;
28375
28376 /* Check overlays first. */
28377 help = overlay = Qnil;
28378 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
28379 {
28380 overlay = overlay_vec[i];
28381 help = Foverlay_get (overlay, Qhelp_echo);
28382 }
28383
28384 if (!NILP (help))
28385 {
28386 help_echo_string = help;
28387 help_echo_window = window;
28388 help_echo_object = overlay;
28389 help_echo_pos = pos;
28390 }
28391 else
28392 {
28393 Lisp_Object obj = glyph->object;
28394 ptrdiff_t charpos = glyph->charpos;
28395
28396 /* Try text properties. */
28397 if (STRINGP (obj)
28398 && charpos >= 0
28399 && charpos < SCHARS (obj))
28400 {
28401 help = Fget_text_property (make_number (charpos),
28402 Qhelp_echo, obj);
28403 if (NILP (help))
28404 {
28405 /* If the string itself doesn't specify a help-echo,
28406 see if the buffer text ``under'' it does. */
28407 struct glyph_row *r
28408 = MATRIX_ROW (w->current_matrix, vpos);
28409 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28410 ptrdiff_t p = string_buffer_position (obj, start);
28411 if (p > 0)
28412 {
28413 help = Fget_char_property (make_number (p),
28414 Qhelp_echo, w->contents);
28415 if (!NILP (help))
28416 {
28417 charpos = p;
28418 obj = w->contents;
28419 }
28420 }
28421 }
28422 }
28423 else if (BUFFERP (obj)
28424 && charpos >= BEGV
28425 && charpos < ZV)
28426 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28427 obj);
28428
28429 if (!NILP (help))
28430 {
28431 help_echo_string = help;
28432 help_echo_window = window;
28433 help_echo_object = obj;
28434 help_echo_pos = charpos;
28435 }
28436 }
28437 }
28438
28439 #ifdef HAVE_WINDOW_SYSTEM
28440 /* Look for a `pointer' property. */
28441 if (FRAME_WINDOW_P (f) && NILP (pointer))
28442 {
28443 /* Check overlays first. */
28444 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28445 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28446
28447 if (NILP (pointer))
28448 {
28449 Lisp_Object obj = glyph->object;
28450 ptrdiff_t charpos = glyph->charpos;
28451
28452 /* Try text properties. */
28453 if (STRINGP (obj)
28454 && charpos >= 0
28455 && charpos < SCHARS (obj))
28456 {
28457 pointer = Fget_text_property (make_number (charpos),
28458 Qpointer, obj);
28459 if (NILP (pointer))
28460 {
28461 /* If the string itself doesn't specify a pointer,
28462 see if the buffer text ``under'' it does. */
28463 struct glyph_row *r
28464 = MATRIX_ROW (w->current_matrix, vpos);
28465 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28466 ptrdiff_t p = string_buffer_position (obj, start);
28467 if (p > 0)
28468 pointer = Fget_char_property (make_number (p),
28469 Qpointer, w->contents);
28470 }
28471 }
28472 else if (BUFFERP (obj)
28473 && charpos >= BEGV
28474 && charpos < ZV)
28475 pointer = Fget_text_property (make_number (charpos),
28476 Qpointer, obj);
28477 }
28478 }
28479 #endif /* HAVE_WINDOW_SYSTEM */
28480
28481 BEGV = obegv;
28482 ZV = ozv;
28483 current_buffer = obuf;
28484 }
28485
28486 set_cursor:
28487
28488 #ifdef HAVE_WINDOW_SYSTEM
28489 if (FRAME_WINDOW_P (f))
28490 define_frame_cursor1 (f, cursor, pointer);
28491 #else
28492 /* This is here to prevent a compiler error, about "label at end of
28493 compound statement". */
28494 return;
28495 #endif
28496 }
28497
28498
28499 /* EXPORT for RIF:
28500 Clear any mouse-face on window W. This function is part of the
28501 redisplay interface, and is called from try_window_id and similar
28502 functions to ensure the mouse-highlight is off. */
28503
28504 void
28505 x_clear_window_mouse_face (struct window *w)
28506 {
28507 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28508 Lisp_Object window;
28509
28510 block_input ();
28511 XSETWINDOW (window, w);
28512 if (EQ (window, hlinfo->mouse_face_window))
28513 clear_mouse_face (hlinfo);
28514 unblock_input ();
28515 }
28516
28517
28518 /* EXPORT:
28519 Just discard the mouse face information for frame F, if any.
28520 This is used when the size of F is changed. */
28521
28522 void
28523 cancel_mouse_face (struct frame *f)
28524 {
28525 Lisp_Object window;
28526 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28527
28528 window = hlinfo->mouse_face_window;
28529 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28530 {
28531 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28532 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28533 hlinfo->mouse_face_window = Qnil;
28534 }
28535 }
28536
28537
28538 \f
28539 /***********************************************************************
28540 Exposure Events
28541 ***********************************************************************/
28542
28543 #ifdef HAVE_WINDOW_SYSTEM
28544
28545 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28546 which intersects rectangle R. R is in window-relative coordinates. */
28547
28548 static void
28549 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28550 enum glyph_row_area area)
28551 {
28552 struct glyph *first = row->glyphs[area];
28553 struct glyph *end = row->glyphs[area] + row->used[area];
28554 struct glyph *last;
28555 int first_x, start_x, x;
28556
28557 if (area == TEXT_AREA && row->fill_line_p)
28558 /* If row extends face to end of line write the whole line. */
28559 draw_glyphs (w, 0, row, area,
28560 0, row->used[area],
28561 DRAW_NORMAL_TEXT, 0);
28562 else
28563 {
28564 /* Set START_X to the window-relative start position for drawing glyphs of
28565 AREA. The first glyph of the text area can be partially visible.
28566 The first glyphs of other areas cannot. */
28567 start_x = window_box_left_offset (w, area);
28568 x = start_x;
28569 if (area == TEXT_AREA)
28570 x += row->x;
28571
28572 /* Find the first glyph that must be redrawn. */
28573 while (first < end
28574 && x + first->pixel_width < r->x)
28575 {
28576 x += first->pixel_width;
28577 ++first;
28578 }
28579
28580 /* Find the last one. */
28581 last = first;
28582 first_x = x;
28583 while (last < end
28584 && x < r->x + r->width)
28585 {
28586 x += last->pixel_width;
28587 ++last;
28588 }
28589
28590 /* Repaint. */
28591 if (last > first)
28592 draw_glyphs (w, first_x - start_x, row, area,
28593 first - row->glyphs[area], last - row->glyphs[area],
28594 DRAW_NORMAL_TEXT, 0);
28595 }
28596 }
28597
28598
28599 /* Redraw the parts of the glyph row ROW on window W intersecting
28600 rectangle R. R is in window-relative coordinates. Value is
28601 non-zero if mouse-face was overwritten. */
28602
28603 static int
28604 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28605 {
28606 eassert (row->enabled_p);
28607
28608 if (row->mode_line_p || w->pseudo_window_p)
28609 draw_glyphs (w, 0, row, TEXT_AREA,
28610 0, row->used[TEXT_AREA],
28611 DRAW_NORMAL_TEXT, 0);
28612 else
28613 {
28614 if (row->used[LEFT_MARGIN_AREA])
28615 expose_area (w, row, r, LEFT_MARGIN_AREA);
28616 if (row->used[TEXT_AREA])
28617 expose_area (w, row, r, TEXT_AREA);
28618 if (row->used[RIGHT_MARGIN_AREA])
28619 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28620 draw_row_fringe_bitmaps (w, row);
28621 }
28622
28623 return row->mouse_face_p;
28624 }
28625
28626
28627 /* Redraw those parts of glyphs rows during expose event handling that
28628 overlap other rows. Redrawing of an exposed line writes over parts
28629 of lines overlapping that exposed line; this function fixes that.
28630
28631 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28632 row in W's current matrix that is exposed and overlaps other rows.
28633 LAST_OVERLAPPING_ROW is the last such row. */
28634
28635 static void
28636 expose_overlaps (struct window *w,
28637 struct glyph_row *first_overlapping_row,
28638 struct glyph_row *last_overlapping_row,
28639 XRectangle *r)
28640 {
28641 struct glyph_row *row;
28642
28643 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28644 if (row->overlapping_p)
28645 {
28646 eassert (row->enabled_p && !row->mode_line_p);
28647
28648 row->clip = r;
28649 if (row->used[LEFT_MARGIN_AREA])
28650 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28651
28652 if (row->used[TEXT_AREA])
28653 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28654
28655 if (row->used[RIGHT_MARGIN_AREA])
28656 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28657 row->clip = NULL;
28658 }
28659 }
28660
28661
28662 /* Return non-zero if W's cursor intersects rectangle R. */
28663
28664 static int
28665 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28666 {
28667 XRectangle cr, result;
28668 struct glyph *cursor_glyph;
28669 struct glyph_row *row;
28670
28671 if (w->phys_cursor.vpos >= 0
28672 && w->phys_cursor.vpos < w->current_matrix->nrows
28673 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28674 row->enabled_p)
28675 && row->cursor_in_fringe_p)
28676 {
28677 /* Cursor is in the fringe. */
28678 cr.x = window_box_right_offset (w,
28679 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28680 ? RIGHT_MARGIN_AREA
28681 : TEXT_AREA));
28682 cr.y = row->y;
28683 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28684 cr.height = row->height;
28685 return x_intersect_rectangles (&cr, r, &result);
28686 }
28687
28688 cursor_glyph = get_phys_cursor_glyph (w);
28689 if (cursor_glyph)
28690 {
28691 /* r is relative to W's box, but w->phys_cursor.x is relative
28692 to left edge of W's TEXT area. Adjust it. */
28693 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28694 cr.y = w->phys_cursor.y;
28695 cr.width = cursor_glyph->pixel_width;
28696 cr.height = w->phys_cursor_height;
28697 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28698 I assume the effect is the same -- and this is portable. */
28699 return x_intersect_rectangles (&cr, r, &result);
28700 }
28701 /* If we don't understand the format, pretend we're not in the hot-spot. */
28702 return 0;
28703 }
28704
28705
28706 /* EXPORT:
28707 Draw a vertical window border to the right of window W if W doesn't
28708 have vertical scroll bars. */
28709
28710 void
28711 x_draw_vertical_border (struct window *w)
28712 {
28713 struct frame *f = XFRAME (WINDOW_FRAME (w));
28714
28715 /* We could do better, if we knew what type of scroll-bar the adjacent
28716 windows (on either side) have... But we don't :-(
28717 However, I think this works ok. ++KFS 2003-04-25 */
28718
28719 /* Redraw borders between horizontally adjacent windows. Don't
28720 do it for frames with vertical scroll bars because either the
28721 right scroll bar of a window, or the left scroll bar of its
28722 neighbor will suffice as a border. */
28723 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28724 return;
28725
28726 /* Note: It is necessary to redraw both the left and the right
28727 borders, for when only this single window W is being
28728 redisplayed. */
28729 if (!WINDOW_RIGHTMOST_P (w)
28730 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28731 {
28732 int x0, x1, y0, y1;
28733
28734 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28735 y1 -= 1;
28736
28737 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28738 x1 -= 1;
28739
28740 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28741 }
28742 if (!WINDOW_LEFTMOST_P (w)
28743 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28744 {
28745 int x0, x1, y0, y1;
28746
28747 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28748 y1 -= 1;
28749
28750 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28751 x0 -= 1;
28752
28753 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28754 }
28755 }
28756
28757
28758 /* Redraw the part of window W intersection rectangle FR. Pixel
28759 coordinates in FR are frame-relative. Call this function with
28760 input blocked. Value is non-zero if the exposure overwrites
28761 mouse-face. */
28762
28763 static int
28764 expose_window (struct window *w, XRectangle *fr)
28765 {
28766 struct frame *f = XFRAME (w->frame);
28767 XRectangle wr, r;
28768 int mouse_face_overwritten_p = 0;
28769
28770 /* If window is not yet fully initialized, do nothing. This can
28771 happen when toolkit scroll bars are used and a window is split.
28772 Reconfiguring the scroll bar will generate an expose for a newly
28773 created window. */
28774 if (w->current_matrix == NULL)
28775 return 0;
28776
28777 /* When we're currently updating the window, display and current
28778 matrix usually don't agree. Arrange for a thorough display
28779 later. */
28780 if (w->must_be_updated_p)
28781 {
28782 SET_FRAME_GARBAGED (f);
28783 return 0;
28784 }
28785
28786 /* Frame-relative pixel rectangle of W. */
28787 wr.x = WINDOW_LEFT_EDGE_X (w);
28788 wr.y = WINDOW_TOP_EDGE_Y (w);
28789 wr.width = WINDOW_TOTAL_WIDTH (w);
28790 wr.height = WINDOW_TOTAL_HEIGHT (w);
28791
28792 if (x_intersect_rectangles (fr, &wr, &r))
28793 {
28794 int yb = window_text_bottom_y (w);
28795 struct glyph_row *row;
28796 int cursor_cleared_p, phys_cursor_on_p;
28797 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28798
28799 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28800 r.x, r.y, r.width, r.height));
28801
28802 /* Convert to window coordinates. */
28803 r.x -= WINDOW_LEFT_EDGE_X (w);
28804 r.y -= WINDOW_TOP_EDGE_Y (w);
28805
28806 /* Turn off the cursor. */
28807 if (!w->pseudo_window_p
28808 && phys_cursor_in_rect_p (w, &r))
28809 {
28810 x_clear_cursor (w);
28811 cursor_cleared_p = 1;
28812 }
28813 else
28814 cursor_cleared_p = 0;
28815
28816 /* If the row containing the cursor extends face to end of line,
28817 then expose_area might overwrite the cursor outside the
28818 rectangle and thus notice_overwritten_cursor might clear
28819 w->phys_cursor_on_p. We remember the original value and
28820 check later if it is changed. */
28821 phys_cursor_on_p = w->phys_cursor_on_p;
28822
28823 /* Update lines intersecting rectangle R. */
28824 first_overlapping_row = last_overlapping_row = NULL;
28825 for (row = w->current_matrix->rows;
28826 row->enabled_p;
28827 ++row)
28828 {
28829 int y0 = row->y;
28830 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28831
28832 if ((y0 >= r.y && y0 < r.y + r.height)
28833 || (y1 > r.y && y1 < r.y + r.height)
28834 || (r.y >= y0 && r.y < y1)
28835 || (r.y + r.height > y0 && r.y + r.height < y1))
28836 {
28837 /* A header line may be overlapping, but there is no need
28838 to fix overlapping areas for them. KFS 2005-02-12 */
28839 if (row->overlapping_p && !row->mode_line_p)
28840 {
28841 if (first_overlapping_row == NULL)
28842 first_overlapping_row = row;
28843 last_overlapping_row = row;
28844 }
28845
28846 row->clip = fr;
28847 if (expose_line (w, row, &r))
28848 mouse_face_overwritten_p = 1;
28849 row->clip = NULL;
28850 }
28851 else if (row->overlapping_p)
28852 {
28853 /* We must redraw a row overlapping the exposed area. */
28854 if (y0 < r.y
28855 ? y0 + row->phys_height > r.y
28856 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28857 {
28858 if (first_overlapping_row == NULL)
28859 first_overlapping_row = row;
28860 last_overlapping_row = row;
28861 }
28862 }
28863
28864 if (y1 >= yb)
28865 break;
28866 }
28867
28868 /* Display the mode line if there is one. */
28869 if (WINDOW_WANTS_MODELINE_P (w)
28870 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28871 row->enabled_p)
28872 && row->y < r.y + r.height)
28873 {
28874 if (expose_line (w, row, &r))
28875 mouse_face_overwritten_p = 1;
28876 }
28877
28878 if (!w->pseudo_window_p)
28879 {
28880 /* Fix the display of overlapping rows. */
28881 if (first_overlapping_row)
28882 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28883 fr);
28884
28885 /* Draw border between windows. */
28886 x_draw_vertical_border (w);
28887
28888 /* Turn the cursor on again. */
28889 if (cursor_cleared_p
28890 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28891 update_window_cursor (w, 1);
28892 }
28893 }
28894
28895 return mouse_face_overwritten_p;
28896 }
28897
28898
28899
28900 /* Redraw (parts) of all windows in the window tree rooted at W that
28901 intersect R. R contains frame pixel coordinates. Value is
28902 non-zero if the exposure overwrites mouse-face. */
28903
28904 static int
28905 expose_window_tree (struct window *w, XRectangle *r)
28906 {
28907 struct frame *f = XFRAME (w->frame);
28908 int mouse_face_overwritten_p = 0;
28909
28910 while (w && !FRAME_GARBAGED_P (f))
28911 {
28912 if (WINDOWP (w->contents))
28913 mouse_face_overwritten_p
28914 |= expose_window_tree (XWINDOW (w->contents), r);
28915 else
28916 mouse_face_overwritten_p |= expose_window (w, r);
28917
28918 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28919 }
28920
28921 return mouse_face_overwritten_p;
28922 }
28923
28924
28925 /* EXPORT:
28926 Redisplay an exposed area of frame F. X and Y are the upper-left
28927 corner of the exposed rectangle. W and H are width and height of
28928 the exposed area. All are pixel values. W or H zero means redraw
28929 the entire frame. */
28930
28931 void
28932 expose_frame (struct frame *f, int x, int y, int w, int h)
28933 {
28934 XRectangle r;
28935 int mouse_face_overwritten_p = 0;
28936
28937 TRACE ((stderr, "expose_frame "));
28938
28939 /* No need to redraw if frame will be redrawn soon. */
28940 if (FRAME_GARBAGED_P (f))
28941 {
28942 TRACE ((stderr, " garbaged\n"));
28943 return;
28944 }
28945
28946 /* If basic faces haven't been realized yet, there is no point in
28947 trying to redraw anything. This can happen when we get an expose
28948 event while Emacs is starting, e.g. by moving another window. */
28949 if (FRAME_FACE_CACHE (f) == NULL
28950 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28951 {
28952 TRACE ((stderr, " no faces\n"));
28953 return;
28954 }
28955
28956 if (w == 0 || h == 0)
28957 {
28958 r.x = r.y = 0;
28959 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
28960 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
28961 }
28962 else
28963 {
28964 r.x = x;
28965 r.y = y;
28966 r.width = w;
28967 r.height = h;
28968 }
28969
28970 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
28971 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
28972
28973 if (WINDOWP (f->tool_bar_window))
28974 mouse_face_overwritten_p
28975 |= expose_window (XWINDOW (f->tool_bar_window), &r);
28976
28977 #ifdef HAVE_X_WINDOWS
28978 #ifndef MSDOS
28979 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
28980 if (WINDOWP (f->menu_bar_window))
28981 mouse_face_overwritten_p
28982 |= expose_window (XWINDOW (f->menu_bar_window), &r);
28983 #endif /* not USE_X_TOOLKIT and not USE_GTK */
28984 #endif
28985 #endif
28986
28987 /* Some window managers support a focus-follows-mouse style with
28988 delayed raising of frames. Imagine a partially obscured frame,
28989 and moving the mouse into partially obscured mouse-face on that
28990 frame. The visible part of the mouse-face will be highlighted,
28991 then the WM raises the obscured frame. With at least one WM, KDE
28992 2.1, Emacs is not getting any event for the raising of the frame
28993 (even tried with SubstructureRedirectMask), only Expose events.
28994 These expose events will draw text normally, i.e. not
28995 highlighted. Which means we must redo the highlight here.
28996 Subsume it under ``we love X''. --gerd 2001-08-15 */
28997 /* Included in Windows version because Windows most likely does not
28998 do the right thing if any third party tool offers
28999 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
29000 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
29001 {
29002 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29003 if (f == hlinfo->mouse_face_mouse_frame)
29004 {
29005 int mouse_x = hlinfo->mouse_face_mouse_x;
29006 int mouse_y = hlinfo->mouse_face_mouse_y;
29007 clear_mouse_face (hlinfo);
29008 note_mouse_highlight (f, mouse_x, mouse_y);
29009 }
29010 }
29011 }
29012
29013
29014 /* EXPORT:
29015 Determine the intersection of two rectangles R1 and R2. Return
29016 the intersection in *RESULT. Value is non-zero if RESULT is not
29017 empty. */
29018
29019 int
29020 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
29021 {
29022 XRectangle *left, *right;
29023 XRectangle *upper, *lower;
29024 int intersection_p = 0;
29025
29026 /* Rearrange so that R1 is the left-most rectangle. */
29027 if (r1->x < r2->x)
29028 left = r1, right = r2;
29029 else
29030 left = r2, right = r1;
29031
29032 /* X0 of the intersection is right.x0, if this is inside R1,
29033 otherwise there is no intersection. */
29034 if (right->x <= left->x + left->width)
29035 {
29036 result->x = right->x;
29037
29038 /* The right end of the intersection is the minimum of
29039 the right ends of left and right. */
29040 result->width = (min (left->x + left->width, right->x + right->width)
29041 - result->x);
29042
29043 /* Same game for Y. */
29044 if (r1->y < r2->y)
29045 upper = r1, lower = r2;
29046 else
29047 upper = r2, lower = r1;
29048
29049 /* The upper end of the intersection is lower.y0, if this is inside
29050 of upper. Otherwise, there is no intersection. */
29051 if (lower->y <= upper->y + upper->height)
29052 {
29053 result->y = lower->y;
29054
29055 /* The lower end of the intersection is the minimum of the lower
29056 ends of upper and lower. */
29057 result->height = (min (lower->y + lower->height,
29058 upper->y + upper->height)
29059 - result->y);
29060 intersection_p = 1;
29061 }
29062 }
29063
29064 return intersection_p;
29065 }
29066
29067 #endif /* HAVE_WINDOW_SYSTEM */
29068
29069 \f
29070 /***********************************************************************
29071 Initialization
29072 ***********************************************************************/
29073
29074 void
29075 syms_of_xdisp (void)
29076 {
29077 Vwith_echo_area_save_vector = Qnil;
29078 staticpro (&Vwith_echo_area_save_vector);
29079
29080 Vmessage_stack = Qnil;
29081 staticpro (&Vmessage_stack);
29082
29083 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
29084 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
29085
29086 message_dolog_marker1 = Fmake_marker ();
29087 staticpro (&message_dolog_marker1);
29088 message_dolog_marker2 = Fmake_marker ();
29089 staticpro (&message_dolog_marker2);
29090 message_dolog_marker3 = Fmake_marker ();
29091 staticpro (&message_dolog_marker3);
29092
29093 #ifdef GLYPH_DEBUG
29094 defsubr (&Sdump_frame_glyph_matrix);
29095 defsubr (&Sdump_glyph_matrix);
29096 defsubr (&Sdump_glyph_row);
29097 defsubr (&Sdump_tool_bar_row);
29098 defsubr (&Strace_redisplay);
29099 defsubr (&Strace_to_stderr);
29100 #endif
29101 #ifdef HAVE_WINDOW_SYSTEM
29102 defsubr (&Stool_bar_lines_needed);
29103 defsubr (&Slookup_image_map);
29104 #endif
29105 defsubr (&Sline_pixel_height);
29106 defsubr (&Sformat_mode_line);
29107 defsubr (&Sinvisible_p);
29108 defsubr (&Scurrent_bidi_paragraph_direction);
29109 defsubr (&Smove_point_visually);
29110
29111 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
29112 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
29113 DEFSYM (Qoverriding_local_map, "overriding-local-map");
29114 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
29115 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
29116 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
29117 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
29118 DEFSYM (Qeval, "eval");
29119 DEFSYM (QCdata, ":data");
29120 DEFSYM (Qdisplay, "display");
29121 DEFSYM (Qspace_width, "space-width");
29122 DEFSYM (Qraise, "raise");
29123 DEFSYM (Qslice, "slice");
29124 DEFSYM (Qspace, "space");
29125 DEFSYM (Qmargin, "margin");
29126 DEFSYM (Qpointer, "pointer");
29127 DEFSYM (Qleft_margin, "left-margin");
29128 DEFSYM (Qright_margin, "right-margin");
29129 DEFSYM (Qcenter, "center");
29130 DEFSYM (Qline_height, "line-height");
29131 DEFSYM (QCalign_to, ":align-to");
29132 DEFSYM (QCrelative_width, ":relative-width");
29133 DEFSYM (QCrelative_height, ":relative-height");
29134 DEFSYM (QCeval, ":eval");
29135 DEFSYM (QCpropertize, ":propertize");
29136 DEFSYM (QCfile, ":file");
29137 DEFSYM (Qfontified, "fontified");
29138 DEFSYM (Qfontification_functions, "fontification-functions");
29139 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
29140 DEFSYM (Qescape_glyph, "escape-glyph");
29141 DEFSYM (Qnobreak_space, "nobreak-space");
29142 DEFSYM (Qimage, "image");
29143 DEFSYM (Qtext, "text");
29144 DEFSYM (Qboth, "both");
29145 DEFSYM (Qboth_horiz, "both-horiz");
29146 DEFSYM (Qtext_image_horiz, "text-image-horiz");
29147 DEFSYM (QCmap, ":map");
29148 DEFSYM (QCpointer, ":pointer");
29149 DEFSYM (Qrect, "rect");
29150 DEFSYM (Qcircle, "circle");
29151 DEFSYM (Qpoly, "poly");
29152 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
29153 DEFSYM (Qgrow_only, "grow-only");
29154 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
29155 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
29156 DEFSYM (Qposition, "position");
29157 DEFSYM (Qbuffer_position, "buffer-position");
29158 DEFSYM (Qobject, "object");
29159 DEFSYM (Qbar, "bar");
29160 DEFSYM (Qhbar, "hbar");
29161 DEFSYM (Qbox, "box");
29162 DEFSYM (Qhollow, "hollow");
29163 DEFSYM (Qhand, "hand");
29164 DEFSYM (Qarrow, "arrow");
29165 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
29166
29167 list_of_error = list1 (list2 (intern_c_string ("error"),
29168 intern_c_string ("void-variable")));
29169 staticpro (&list_of_error);
29170
29171 DEFSYM (Qlast_arrow_position, "last-arrow-position");
29172 DEFSYM (Qlast_arrow_string, "last-arrow-string");
29173 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
29174 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
29175
29176 echo_buffer[0] = echo_buffer[1] = Qnil;
29177 staticpro (&echo_buffer[0]);
29178 staticpro (&echo_buffer[1]);
29179
29180 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
29181 staticpro (&echo_area_buffer[0]);
29182 staticpro (&echo_area_buffer[1]);
29183
29184 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
29185 staticpro (&Vmessages_buffer_name);
29186
29187 mode_line_proptrans_alist = Qnil;
29188 staticpro (&mode_line_proptrans_alist);
29189 mode_line_string_list = Qnil;
29190 staticpro (&mode_line_string_list);
29191 mode_line_string_face = Qnil;
29192 staticpro (&mode_line_string_face);
29193 mode_line_string_face_prop = Qnil;
29194 staticpro (&mode_line_string_face_prop);
29195 Vmode_line_unwind_vector = Qnil;
29196 staticpro (&Vmode_line_unwind_vector);
29197
29198 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
29199
29200 help_echo_string = Qnil;
29201 staticpro (&help_echo_string);
29202 help_echo_object = Qnil;
29203 staticpro (&help_echo_object);
29204 help_echo_window = Qnil;
29205 staticpro (&help_echo_window);
29206 previous_help_echo_string = Qnil;
29207 staticpro (&previous_help_echo_string);
29208 help_echo_pos = -1;
29209
29210 DEFSYM (Qright_to_left, "right-to-left");
29211 DEFSYM (Qleft_to_right, "left-to-right");
29212
29213 #ifdef HAVE_WINDOW_SYSTEM
29214 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
29215 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
29216 For example, if a block cursor is over a tab, it will be drawn as
29217 wide as that tab on the display. */);
29218 x_stretch_cursor_p = 0;
29219 #endif
29220
29221 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
29222 doc: /* Non-nil means highlight trailing whitespace.
29223 The face used for trailing whitespace is `trailing-whitespace'. */);
29224 Vshow_trailing_whitespace = Qnil;
29225
29226 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
29227 doc: /* Control highlighting of non-ASCII space and hyphen chars.
29228 If the value is t, Emacs highlights non-ASCII chars which have the
29229 same appearance as an ASCII space or hyphen, using the `nobreak-space'
29230 or `escape-glyph' face respectively.
29231
29232 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
29233 U+2011 (non-breaking hyphen) are affected.
29234
29235 Any other non-nil value means to display these characters as a escape
29236 glyph followed by an ordinary space or hyphen.
29237
29238 A value of nil means no special handling of these characters. */);
29239 Vnobreak_char_display = Qt;
29240
29241 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
29242 doc: /* The pointer shape to show in void text areas.
29243 A value of nil means to show the text pointer. Other options are `arrow',
29244 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
29245 Vvoid_text_area_pointer = Qarrow;
29246
29247 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
29248 doc: /* Non-nil means don't actually do any redisplay.
29249 This is used for internal purposes. */);
29250 Vinhibit_redisplay = Qnil;
29251
29252 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
29253 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
29254 Vglobal_mode_string = Qnil;
29255
29256 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
29257 doc: /* Marker for where to display an arrow on top of the buffer text.
29258 This must be the beginning of a line in order to work.
29259 See also `overlay-arrow-string'. */);
29260 Voverlay_arrow_position = Qnil;
29261
29262 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
29263 doc: /* String to display as an arrow in non-window frames.
29264 See also `overlay-arrow-position'. */);
29265 Voverlay_arrow_string = build_pure_c_string ("=>");
29266
29267 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
29268 doc: /* List of variables (symbols) which hold markers for overlay arrows.
29269 The symbols on this list are examined during redisplay to determine
29270 where to display overlay arrows. */);
29271 Voverlay_arrow_variable_list
29272 = list1 (intern_c_string ("overlay-arrow-position"));
29273
29274 DEFVAR_INT ("scroll-step", emacs_scroll_step,
29275 doc: /* The number of lines to try scrolling a window by when point moves out.
29276 If that fails to bring point back on frame, point is centered instead.
29277 If this is zero, point is always centered after it moves off frame.
29278 If you want scrolling to always be a line at a time, you should set
29279 `scroll-conservatively' to a large value rather than set this to 1. */);
29280
29281 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
29282 doc: /* Scroll up to this many lines, to bring point back on screen.
29283 If point moves off-screen, redisplay will scroll by up to
29284 `scroll-conservatively' lines in order to bring point just barely
29285 onto the screen again. If that cannot be done, then redisplay
29286 recenters point as usual.
29287
29288 If the value is greater than 100, redisplay will never recenter point,
29289 but will always scroll just enough text to bring point into view, even
29290 if you move far away.
29291
29292 A value of zero means always recenter point if it moves off screen. */);
29293 scroll_conservatively = 0;
29294
29295 DEFVAR_INT ("scroll-margin", scroll_margin,
29296 doc: /* Number of lines of margin at the top and bottom of a window.
29297 Recenter the window whenever point gets within this many lines
29298 of the top or bottom of the window. */);
29299 scroll_margin = 0;
29300
29301 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
29302 doc: /* Pixels per inch value for non-window system displays.
29303 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
29304 Vdisplay_pixels_per_inch = make_float (72.0);
29305
29306 #ifdef GLYPH_DEBUG
29307 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
29308 #endif
29309
29310 DEFVAR_LISP ("truncate-partial-width-windows",
29311 Vtruncate_partial_width_windows,
29312 doc: /* Non-nil means truncate lines in windows narrower than the frame.
29313 For an integer value, truncate lines in each window narrower than the
29314 full frame width, provided the window width is less than that integer;
29315 otherwise, respect the value of `truncate-lines'.
29316
29317 For any other non-nil value, truncate lines in all windows that do
29318 not span the full frame width.
29319
29320 A value of nil means to respect the value of `truncate-lines'.
29321
29322 If `word-wrap' is enabled, you might want to reduce this. */);
29323 Vtruncate_partial_width_windows = make_number (50);
29324
29325 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
29326 doc: /* Maximum buffer size for which line number should be displayed.
29327 If the buffer is bigger than this, the line number does not appear
29328 in the mode line. A value of nil means no limit. */);
29329 Vline_number_display_limit = Qnil;
29330
29331 DEFVAR_INT ("line-number-display-limit-width",
29332 line_number_display_limit_width,
29333 doc: /* Maximum line width (in characters) for line number display.
29334 If the average length of the lines near point is bigger than this, then the
29335 line number may be omitted from the mode line. */);
29336 line_number_display_limit_width = 200;
29337
29338 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
29339 doc: /* Non-nil means highlight region even in nonselected windows. */);
29340 highlight_nonselected_windows = 0;
29341
29342 DEFVAR_BOOL ("multiple-frames", multiple_frames,
29343 doc: /* Non-nil if more than one frame is visible on this display.
29344 Minibuffer-only frames don't count, but iconified frames do.
29345 This variable is not guaranteed to be accurate except while processing
29346 `frame-title-format' and `icon-title-format'. */);
29347
29348 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
29349 doc: /* Template for displaying the title bar of visible frames.
29350 \(Assuming the window manager supports this feature.)
29351
29352 This variable has the same structure as `mode-line-format', except that
29353 the %c and %l constructs are ignored. It is used only on frames for
29354 which no explicit name has been set \(see `modify-frame-parameters'). */);
29355
29356 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
29357 doc: /* Template for displaying the title bar of an iconified frame.
29358 \(Assuming the window manager supports this feature.)
29359 This variable has the same structure as `mode-line-format' (which see),
29360 and is used only on frames for which no explicit name has been set
29361 \(see `modify-frame-parameters'). */);
29362 Vicon_title_format
29363 = Vframe_title_format
29364 = listn (CONSTYPE_PURE, 3,
29365 intern_c_string ("multiple-frames"),
29366 build_pure_c_string ("%b"),
29367 listn (CONSTYPE_PURE, 4,
29368 empty_unibyte_string,
29369 intern_c_string ("invocation-name"),
29370 build_pure_c_string ("@"),
29371 intern_c_string ("system-name")));
29372
29373 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
29374 doc: /* Maximum number of lines to keep in the message log buffer.
29375 If nil, disable message logging. If t, log messages but don't truncate
29376 the buffer when it becomes large. */);
29377 Vmessage_log_max = make_number (1000);
29378
29379 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
29380 doc: /* Functions called before redisplay, if window sizes have changed.
29381 The value should be a list of functions that take one argument.
29382 Just before redisplay, for each frame, if any of its windows have changed
29383 size since the last redisplay, or have been split or deleted,
29384 all the functions in the list are called, with the frame as argument. */);
29385 Vwindow_size_change_functions = Qnil;
29386
29387 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
29388 doc: /* List of functions to call before redisplaying a window with scrolling.
29389 Each function is called with two arguments, the window and its new
29390 display-start position. Note that these functions are also called by
29391 `set-window-buffer'. Also note that the value of `window-end' is not
29392 valid when these functions are called.
29393
29394 Warning: Do not use this feature to alter the way the window
29395 is scrolled. It is not designed for that, and such use probably won't
29396 work. */);
29397 Vwindow_scroll_functions = Qnil;
29398
29399 DEFVAR_LISP ("window-text-change-functions",
29400 Vwindow_text_change_functions,
29401 doc: /* Functions to call in redisplay when text in the window might change. */);
29402 Vwindow_text_change_functions = Qnil;
29403
29404 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
29405 doc: /* Functions called when redisplay of a window reaches the end trigger.
29406 Each function is called with two arguments, the window and the end trigger value.
29407 See `set-window-redisplay-end-trigger'. */);
29408 Vredisplay_end_trigger_functions = Qnil;
29409
29410 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
29411 doc: /* Non-nil means autoselect window with mouse pointer.
29412 If nil, do not autoselect windows.
29413 A positive number means delay autoselection by that many seconds: a
29414 window is autoselected only after the mouse has remained in that
29415 window for the duration of the delay.
29416 A negative number has a similar effect, but causes windows to be
29417 autoselected only after the mouse has stopped moving. \(Because of
29418 the way Emacs compares mouse events, you will occasionally wait twice
29419 that time before the window gets selected.\)
29420 Any other value means to autoselect window instantaneously when the
29421 mouse pointer enters it.
29422
29423 Autoselection selects the minibuffer only if it is active, and never
29424 unselects the minibuffer if it is active.
29425
29426 When customizing this variable make sure that the actual value of
29427 `focus-follows-mouse' matches the behavior of your window manager. */);
29428 Vmouse_autoselect_window = Qnil;
29429
29430 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29431 doc: /* Non-nil means automatically resize tool-bars.
29432 This dynamically changes the tool-bar's height to the minimum height
29433 that is needed to make all tool-bar items visible.
29434 If value is `grow-only', the tool-bar's height is only increased
29435 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29436 Vauto_resize_tool_bars = Qt;
29437
29438 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29439 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29440 auto_raise_tool_bar_buttons_p = 1;
29441
29442 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29443 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29444 make_cursor_line_fully_visible_p = 1;
29445
29446 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29447 doc: /* Border below tool-bar in pixels.
29448 If an integer, use it as the height of the border.
29449 If it is one of `internal-border-width' or `border-width', use the
29450 value of the corresponding frame parameter.
29451 Otherwise, no border is added below the tool-bar. */);
29452 Vtool_bar_border = Qinternal_border_width;
29453
29454 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29455 doc: /* Margin around tool-bar buttons in pixels.
29456 If an integer, use that for both horizontal and vertical margins.
29457 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29458 HORZ specifying the horizontal margin, and VERT specifying the
29459 vertical margin. */);
29460 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29461
29462 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29463 doc: /* Relief thickness of tool-bar buttons. */);
29464 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29465
29466 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29467 doc: /* Tool bar style to use.
29468 It can be one of
29469 image - show images only
29470 text - show text only
29471 both - show both, text below image
29472 both-horiz - show text to the right of the image
29473 text-image-horiz - show text to the left of the image
29474 any other - use system default or image if no system default.
29475
29476 This variable only affects the GTK+ toolkit version of Emacs. */);
29477 Vtool_bar_style = Qnil;
29478
29479 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29480 doc: /* Maximum number of characters a label can have to be shown.
29481 The tool bar style must also show labels for this to have any effect, see
29482 `tool-bar-style'. */);
29483 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29484
29485 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29486 doc: /* List of functions to call to fontify regions of text.
29487 Each function is called with one argument POS. Functions must
29488 fontify a region starting at POS in the current buffer, and give
29489 fontified regions the property `fontified'. */);
29490 Vfontification_functions = Qnil;
29491 Fmake_variable_buffer_local (Qfontification_functions);
29492
29493 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29494 unibyte_display_via_language_environment,
29495 doc: /* Non-nil means display unibyte text according to language environment.
29496 Specifically, this means that raw bytes in the range 160-255 decimal
29497 are displayed by converting them to the equivalent multibyte characters
29498 according to the current language environment. As a result, they are
29499 displayed according to the current fontset.
29500
29501 Note that this variable affects only how these bytes are displayed,
29502 but does not change the fact they are interpreted as raw bytes. */);
29503 unibyte_display_via_language_environment = 0;
29504
29505 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29506 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29507 If a float, it specifies a fraction of the mini-window frame's height.
29508 If an integer, it specifies a number of lines. */);
29509 Vmax_mini_window_height = make_float (0.25);
29510
29511 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29512 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29513 A value of nil means don't automatically resize mini-windows.
29514 A value of t means resize them to fit the text displayed in them.
29515 A value of `grow-only', the default, means let mini-windows grow only;
29516 they return to their normal size when the minibuffer is closed, or the
29517 echo area becomes empty. */);
29518 Vresize_mini_windows = Qgrow_only;
29519
29520 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29521 doc: /* Alist specifying how to blink the cursor off.
29522 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29523 `cursor-type' frame-parameter or variable equals ON-STATE,
29524 comparing using `equal', Emacs uses OFF-STATE to specify
29525 how to blink it off. ON-STATE and OFF-STATE are values for
29526 the `cursor-type' frame parameter.
29527
29528 If a frame's ON-STATE has no entry in this list,
29529 the frame's other specifications determine how to blink the cursor off. */);
29530 Vblink_cursor_alist = Qnil;
29531
29532 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29533 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29534 If non-nil, windows are automatically scrolled horizontally to make
29535 point visible. */);
29536 automatic_hscrolling_p = 1;
29537 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29538
29539 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29540 doc: /* How many columns away from the window edge point is allowed to get
29541 before automatic hscrolling will horizontally scroll the window. */);
29542 hscroll_margin = 5;
29543
29544 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29545 doc: /* How many columns to scroll the window when point gets too close to the edge.
29546 When point is less than `hscroll-margin' columns from the window
29547 edge, automatic hscrolling will scroll the window by the amount of columns
29548 determined by this variable. If its value is a positive integer, scroll that
29549 many columns. If it's a positive floating-point number, it specifies the
29550 fraction of the window's width to scroll. If it's nil or zero, point will be
29551 centered horizontally after the scroll. Any other value, including negative
29552 numbers, are treated as if the value were zero.
29553
29554 Automatic hscrolling always moves point outside the scroll margin, so if
29555 point was more than scroll step columns inside the margin, the window will
29556 scroll more than the value given by the scroll step.
29557
29558 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29559 and `scroll-right' overrides this variable's effect. */);
29560 Vhscroll_step = make_number (0);
29561
29562 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29563 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29564 Bind this around calls to `message' to let it take effect. */);
29565 message_truncate_lines = 0;
29566
29567 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29568 doc: /* Normal hook run to update the menu bar definitions.
29569 Redisplay runs this hook before it redisplays the menu bar.
29570 This is used to update submenus such as Buffers,
29571 whose contents depend on various data. */);
29572 Vmenu_bar_update_hook = Qnil;
29573
29574 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29575 doc: /* Frame for which we are updating a menu.
29576 The enable predicate for a menu binding should check this variable. */);
29577 Vmenu_updating_frame = Qnil;
29578
29579 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29580 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29581 inhibit_menubar_update = 0;
29582
29583 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29584 doc: /* Prefix prepended to all continuation lines at display time.
29585 The value may be a string, an image, or a stretch-glyph; it is
29586 interpreted in the same way as the value of a `display' text property.
29587
29588 This variable is overridden by any `wrap-prefix' text or overlay
29589 property.
29590
29591 To add a prefix to non-continuation lines, use `line-prefix'. */);
29592 Vwrap_prefix = Qnil;
29593 DEFSYM (Qwrap_prefix, "wrap-prefix");
29594 Fmake_variable_buffer_local (Qwrap_prefix);
29595
29596 DEFVAR_LISP ("line-prefix", Vline_prefix,
29597 doc: /* Prefix prepended to all non-continuation lines at display time.
29598 The value may be a string, an image, or a stretch-glyph; it is
29599 interpreted in the same way as the value of a `display' text property.
29600
29601 This variable is overridden by any `line-prefix' text or overlay
29602 property.
29603
29604 To add a prefix to continuation lines, use `wrap-prefix'. */);
29605 Vline_prefix = Qnil;
29606 DEFSYM (Qline_prefix, "line-prefix");
29607 Fmake_variable_buffer_local (Qline_prefix);
29608
29609 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29610 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29611 inhibit_eval_during_redisplay = 0;
29612
29613 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29614 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29615 inhibit_free_realized_faces = 0;
29616
29617 #ifdef GLYPH_DEBUG
29618 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29619 doc: /* Inhibit try_window_id display optimization. */);
29620 inhibit_try_window_id = 0;
29621
29622 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29623 doc: /* Inhibit try_window_reusing display optimization. */);
29624 inhibit_try_window_reusing = 0;
29625
29626 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29627 doc: /* Inhibit try_cursor_movement display optimization. */);
29628 inhibit_try_cursor_movement = 0;
29629 #endif /* GLYPH_DEBUG */
29630
29631 DEFVAR_INT ("overline-margin", overline_margin,
29632 doc: /* Space between overline and text, in pixels.
29633 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29634 margin to the character height. */);
29635 overline_margin = 2;
29636
29637 DEFVAR_INT ("underline-minimum-offset",
29638 underline_minimum_offset,
29639 doc: /* Minimum distance between baseline and underline.
29640 This can improve legibility of underlined text at small font sizes,
29641 particularly when using variable `x-use-underline-position-properties'
29642 with fonts that specify an UNDERLINE_POSITION relatively close to the
29643 baseline. The default value is 1. */);
29644 underline_minimum_offset = 1;
29645
29646 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29647 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29648 This feature only works when on a window system that can change
29649 cursor shapes. */);
29650 display_hourglass_p = 1;
29651
29652 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29653 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29654 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29655
29656 hourglass_atimer = NULL;
29657 hourglass_shown_p = 0;
29658
29659 DEFSYM (Qglyphless_char, "glyphless-char");
29660 DEFSYM (Qhex_code, "hex-code");
29661 DEFSYM (Qempty_box, "empty-box");
29662 DEFSYM (Qthin_space, "thin-space");
29663 DEFSYM (Qzero_width, "zero-width");
29664
29665 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29666 /* Intern this now in case it isn't already done.
29667 Setting this variable twice is harmless.
29668 But don't staticpro it here--that is done in alloc.c. */
29669 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
29670 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29671
29672 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29673 doc: /* Char-table defining glyphless characters.
29674 Each element, if non-nil, should be one of the following:
29675 an ASCII acronym string: display this string in a box
29676 `hex-code': display the hexadecimal code of a character in a box
29677 `empty-box': display as an empty box
29678 `thin-space': display as 1-pixel width space
29679 `zero-width': don't display
29680 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29681 display method for graphical terminals and text terminals respectively.
29682 GRAPHICAL and TEXT should each have one of the values listed above.
29683
29684 The char-table has one extra slot to control the display of a character for
29685 which no font is found. This slot only takes effect on graphical terminals.
29686 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29687 `thin-space'. The default is `empty-box'. */);
29688 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29689 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29690 Qempty_box);
29691
29692 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
29693 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
29694 Vdebug_on_message = Qnil;
29695 }
29696
29697
29698 /* Initialize this module when Emacs starts. */
29699
29700 void
29701 init_xdisp (void)
29702 {
29703 current_header_line_height = current_mode_line_height = -1;
29704
29705 CHARPOS (this_line_start_pos) = 0;
29706
29707 if (!noninteractive)
29708 {
29709 struct window *m = XWINDOW (minibuf_window);
29710 Lisp_Object frame = m->frame;
29711 struct frame *f = XFRAME (frame);
29712 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29713 struct window *r = XWINDOW (root);
29714 int i;
29715
29716 echo_area_window = minibuf_window;
29717
29718 r->top_line = FRAME_TOP_MARGIN (f);
29719 r->total_lines = FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
29720 r->total_cols = FRAME_COLS (f);
29721
29722 m->top_line = FRAME_LINES (f) - 1;
29723 m->total_lines = 1;
29724 m->total_cols = FRAME_COLS (f);
29725
29726 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29727 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29728 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29729
29730 /* The default ellipsis glyphs `...'. */
29731 for (i = 0; i < 3; ++i)
29732 default_invis_vector[i] = make_number ('.');
29733 }
29734
29735 {
29736 /* Allocate the buffer for frame titles.
29737 Also used for `format-mode-line'. */
29738 int size = 100;
29739 mode_line_noprop_buf = xmalloc (size);
29740 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29741 mode_line_noprop_ptr = mode_line_noprop_buf;
29742 mode_line_target = MODE_LINE_DISPLAY;
29743 }
29744
29745 help_echo_showing_p = 0;
29746 }
29747
29748 /* Platform-independent portion of hourglass implementation. */
29749
29750 /* Cancel a currently active hourglass timer, and start a new one. */
29751 void
29752 start_hourglass (void)
29753 {
29754 #if defined (HAVE_WINDOW_SYSTEM)
29755 EMACS_TIME delay;
29756
29757 cancel_hourglass ();
29758
29759 if (INTEGERP (Vhourglass_delay)
29760 && XINT (Vhourglass_delay) > 0)
29761 delay = make_emacs_time (min (XINT (Vhourglass_delay),
29762 TYPE_MAXIMUM (time_t)),
29763 0);
29764 else if (FLOATP (Vhourglass_delay)
29765 && XFLOAT_DATA (Vhourglass_delay) > 0)
29766 delay = EMACS_TIME_FROM_DOUBLE (XFLOAT_DATA (Vhourglass_delay));
29767 else
29768 delay = make_emacs_time (DEFAULT_HOURGLASS_DELAY, 0);
29769
29770 #ifdef HAVE_NTGUI
29771 {
29772 extern void w32_note_current_window (void);
29773 w32_note_current_window ();
29774 }
29775 #endif /* HAVE_NTGUI */
29776
29777 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29778 show_hourglass, NULL);
29779 #endif
29780 }
29781
29782
29783 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29784 shown. */
29785 void
29786 cancel_hourglass (void)
29787 {
29788 #if defined (HAVE_WINDOW_SYSTEM)
29789 if (hourglass_atimer)
29790 {
29791 cancel_atimer (hourglass_atimer);
29792 hourglass_atimer = NULL;
29793 }
29794
29795 if (hourglass_shown_p)
29796 hide_hourglass ();
29797 #endif
29798 }