* dispextern.h (SET_TEXT_POS_FROM_MARKER): Indent.
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2
3 Copyright (C) 1985-1988, 1993-1995, 1997-2013 Free Software Foundation,
4 Inc.
5
6 This file is part of GNU Emacs.
7
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20
21 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
22
23 Redisplay.
24
25 Emacs separates the task of updating the display from code
26 modifying global state, e.g. buffer text. This way functions
27 operating on buffers don't also have to be concerned with updating
28 the display.
29
30 Updating the display is triggered by the Lisp interpreter when it
31 decides it's time to do it. This is done either automatically for
32 you as part of the interpreter's command loop or as the result of
33 calling Lisp functions like `sit-for'. The C function `redisplay'
34 in xdisp.c is the only entry into the inner redisplay code.
35
36 The following diagram shows how redisplay code is invoked. As you
37 can see, Lisp calls redisplay and vice versa. Under window systems
38 like X, some portions of the redisplay code are also called
39 asynchronously during mouse movement or expose events. It is very
40 important that these code parts do NOT use the C library (malloc,
41 free) because many C libraries under Unix are not reentrant. They
42 may also NOT call functions of the Lisp interpreter which could
43 change the interpreter's state. If you don't follow these rules,
44 you will encounter bugs which are very hard to explain.
45
46 +--------------+ redisplay +----------------+
47 | Lisp machine |---------------->| Redisplay code |<--+
48 +--------------+ (xdisp.c) +----------------+ |
49 ^ | |
50 +----------------------------------+ |
51 Don't use this path when called |
52 asynchronously! |
53 |
54 expose_window (asynchronous) |
55 |
56 X expose events -----+
57
58 What does redisplay do? Obviously, it has to figure out somehow what
59 has been changed since the last time the display has been updated,
60 and to make these changes visible. Preferably it would do that in
61 a moderately intelligent way, i.e. fast.
62
63 Changes in buffer text can be deduced from window and buffer
64 structures, and from some global variables like `beg_unchanged' and
65 `end_unchanged'. The contents of the display are additionally
66 recorded in a `glyph matrix', a two-dimensional matrix of glyph
67 structures. Each row in such a matrix corresponds to a line on the
68 display, and each glyph in a row corresponds to a column displaying
69 a character, an image, or what else. This matrix is called the
70 `current glyph matrix' or `current matrix' in redisplay
71 terminology.
72
73 For buffer parts that have been changed since the last update, a
74 second glyph matrix is constructed, the so called `desired glyph
75 matrix' or short `desired matrix'. Current and desired matrix are
76 then compared to find a cheap way to update the display, e.g. by
77 reusing part of the display by scrolling lines.
78
79 You will find a lot of redisplay optimizations when you start
80 looking at the innards of redisplay. The overall goal of all these
81 optimizations is to make redisplay fast because it is done
82 frequently. Some of these optimizations are implemented by the
83 following functions:
84
85 . try_cursor_movement
86
87 This function tries to update the display if the text in the
88 window did not change and did not scroll, only point moved, and
89 it did not move off the displayed portion of the text.
90
91 . try_window_reusing_current_matrix
92
93 This function reuses the current matrix of a window when text
94 has not changed, but the window start changed (e.g., due to
95 scrolling).
96
97 . try_window_id
98
99 This function attempts to redisplay a window by reusing parts of
100 its existing display. It finds and reuses the part that was not
101 changed, and redraws the rest.
102
103 . try_window
104
105 This function performs the full redisplay of a single window
106 assuming that its fonts were not changed and that the cursor
107 will not end up in the scroll margins. (Loading fonts requires
108 re-adjustment of dimensions of glyph matrices, which makes this
109 method impossible to use.)
110
111 These optimizations are tried in sequence (some can be skipped if
112 it is known that they are not applicable). If none of the
113 optimizations were successful, redisplay calls redisplay_windows,
114 which performs a full redisplay of all windows.
115
116 Desired matrices.
117
118 Desired matrices are always built per Emacs window. The function
119 `display_line' is the central function to look at if you are
120 interested. It constructs one row in a desired matrix given an
121 iterator structure containing both a buffer position and a
122 description of the environment in which the text is to be
123 displayed. But this is too early, read on.
124
125 Characters and pixmaps displayed for a range of buffer text depend
126 on various settings of buffers and windows, on overlays and text
127 properties, on display tables, on selective display. The good news
128 is that all this hairy stuff is hidden behind a small set of
129 interface functions taking an iterator structure (struct it)
130 argument.
131
132 Iteration over things to be displayed is then simple. It is
133 started by initializing an iterator with a call to init_iterator,
134 passing it the buffer position where to start iteration. For
135 iteration over strings, pass -1 as the position to init_iterator,
136 and call reseat_to_string when the string is ready, to initialize
137 the iterator for that string. Thereafter, calls to
138 get_next_display_element fill the iterator structure with relevant
139 information about the next thing to display. Calls to
140 set_iterator_to_next move the iterator to the next thing.
141
142 Besides this, an iterator also contains information about the
143 display environment in which glyphs for display elements are to be
144 produced. It has fields for the width and height of the display,
145 the information whether long lines are truncated or continued, a
146 current X and Y position, and lots of other stuff you can better
147 see in dispextern.h.
148
149 Glyphs in a desired matrix are normally constructed in a loop
150 calling get_next_display_element and then PRODUCE_GLYPHS. The call
151 to PRODUCE_GLYPHS will fill the iterator structure with pixel
152 information about the element being displayed and at the same time
153 produce glyphs for it. If the display element fits on the line
154 being displayed, set_iterator_to_next is called next, otherwise the
155 glyphs produced are discarded. The function display_line is the
156 workhorse of filling glyph rows in the desired matrix with glyphs.
157 In addition to producing glyphs, it also handles line truncation
158 and continuation, word wrap, and cursor positioning (for the
159 latter, see also set_cursor_from_row).
160
161 Frame matrices.
162
163 That just couldn't be all, could it? What about terminal types not
164 supporting operations on sub-windows of the screen? To update the
165 display on such a terminal, window-based glyph matrices are not
166 well suited. To be able to reuse part of the display (scrolling
167 lines up and down), we must instead have a view of the whole
168 screen. This is what `frame matrices' are for. They are a trick.
169
170 Frames on terminals like above have a glyph pool. Windows on such
171 a frame sub-allocate their glyph memory from their frame's glyph
172 pool. The frame itself is given its own glyph matrices. By
173 coincidence---or maybe something else---rows in window glyph
174 matrices are slices of corresponding rows in frame matrices. Thus
175 writing to window matrices implicitly updates a frame matrix which
176 provides us with the view of the whole screen that we originally
177 wanted to have without having to move many bytes around. To be
178 honest, there is a little bit more done, but not much more. If you
179 plan to extend that code, take a look at dispnew.c. The function
180 build_frame_matrix is a good starting point.
181
182 Bidirectional display.
183
184 Bidirectional display adds quite some hair to this already complex
185 design. The good news are that a large portion of that hairy stuff
186 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
187 reordering engine which is called by set_iterator_to_next and
188 returns the next character to display in the visual order. See
189 commentary on bidi.c for more details. As far as redisplay is
190 concerned, the effect of calling bidi_move_to_visually_next, the
191 main interface of the reordering engine, is that the iterator gets
192 magically placed on the buffer or string position that is to be
193 displayed next. In other words, a linear iteration through the
194 buffer/string is replaced with a non-linear one. All the rest of
195 the redisplay is oblivious to the bidi reordering.
196
197 Well, almost oblivious---there are still complications, most of
198 them due to the fact that buffer and string positions no longer
199 change monotonously with glyph indices in a glyph row. Moreover,
200 for continued lines, the buffer positions may not even be
201 monotonously changing with vertical positions. Also, accounting
202 for face changes, overlays, etc. becomes more complex because
203 non-linear iteration could potentially skip many positions with
204 changes, and then cross them again on the way back...
205
206 One other prominent effect of bidirectional display is that some
207 paragraphs of text need to be displayed starting at the right
208 margin of the window---the so-called right-to-left, or R2L
209 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
210 which have their reversed_p flag set. The bidi reordering engine
211 produces characters in such rows starting from the character which
212 should be the rightmost on display. PRODUCE_GLYPHS then reverses
213 the order, when it fills up the glyph row whose reversed_p flag is
214 set, by prepending each new glyph to what is already there, instead
215 of appending it. When the glyph row is complete, the function
216 extend_face_to_end_of_line fills the empty space to the left of the
217 leftmost character with special glyphs, which will display as,
218 well, empty. On text terminals, these special glyphs are simply
219 blank characters. On graphics terminals, there's a single stretch
220 glyph of a suitably computed width. Both the blanks and the
221 stretch glyph are given the face of the background of the line.
222 This way, the terminal-specific back-end can still draw the glyphs
223 left to right, even for R2L lines.
224
225 Bidirectional display and character compositions
226
227 Some scripts cannot be displayed by drawing each character
228 individually, because adjacent characters change each other's shape
229 on display. For example, Arabic and Indic scripts belong to this
230 category.
231
232 Emacs display supports this by providing "character compositions",
233 most of which is implemented in composite.c. During the buffer
234 scan that delivers characters to PRODUCE_GLYPHS, if the next
235 character to be delivered is a composed character, the iteration
236 calls composition_reseat_it and next_element_from_composition. If
237 they succeed to compose the character with one or more of the
238 following characters, the whole sequence of characters that where
239 composed is recorded in the `struct composition_it' object that is
240 part of the buffer iterator. The composed sequence could produce
241 one or more font glyphs (called "grapheme clusters") on the screen.
242 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
243 in the direction corresponding to the current bidi scan direction
244 (recorded in the scan_dir member of the `struct bidi_it' object
245 that is part of the buffer iterator). In particular, if the bidi
246 iterator currently scans the buffer backwards, the grapheme
247 clusters are delivered back to front. This reorders the grapheme
248 clusters as appropriate for the current bidi context. Note that
249 this means that the grapheme clusters are always stored in the
250 LGSTRING object (see composite.c) in the logical order.
251
252 Moving an iterator in bidirectional text
253 without producing glyphs
254
255 Note one important detail mentioned above: that the bidi reordering
256 engine, driven by the iterator, produces characters in R2L rows
257 starting at the character that will be the rightmost on display.
258 As far as the iterator is concerned, the geometry of such rows is
259 still left to right, i.e. the iterator "thinks" the first character
260 is at the leftmost pixel position. The iterator does not know that
261 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
262 delivers. This is important when functions from the move_it_*
263 family are used to get to certain screen position or to match
264 screen coordinates with buffer coordinates: these functions use the
265 iterator geometry, which is left to right even in R2L paragraphs.
266 This works well with most callers of move_it_*, because they need
267 to get to a specific column, and columns are still numbered in the
268 reading order, i.e. the rightmost character in a R2L paragraph is
269 still column zero. But some callers do not get well with this; a
270 notable example is mouse clicks that need to find the character
271 that corresponds to certain pixel coordinates. See
272 buffer_posn_from_coords in dispnew.c for how this is handled. */
273
274 #include <config.h>
275 #include <stdio.h>
276 #include <limits.h>
277
278 #include "lisp.h"
279 #include "atimer.h"
280 #include "keyboard.h"
281 #include "frame.h"
282 #include "window.h"
283 #include "termchar.h"
284 #include "dispextern.h"
285 #include "character.h"
286 #include "buffer.h"
287 #include "charset.h"
288 #include "indent.h"
289 #include "commands.h"
290 #include "keymap.h"
291 #include "macros.h"
292 #include "disptab.h"
293 #include "termhooks.h"
294 #include "termopts.h"
295 #include "intervals.h"
296 #include "coding.h"
297 #include "process.h"
298 #include "region-cache.h"
299 #include "font.h"
300 #include "fontset.h"
301 #include "blockinput.h"
302
303 #ifdef HAVE_X_WINDOWS
304 #include "xterm.h"
305 #endif
306 #ifdef HAVE_NTGUI
307 #include "w32term.h"
308 #endif
309 #ifdef HAVE_NS
310 #include "nsterm.h"
311 #endif
312 #ifdef USE_GTK
313 #include "gtkutil.h"
314 #endif
315
316 #ifndef FRAME_X_OUTPUT
317 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
318 #endif
319
320 #define INFINITY 10000000
321
322 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
323 Lisp_Object Qwindow_scroll_functions;
324 static Lisp_Object Qwindow_text_change_functions;
325 static Lisp_Object Qredisplay_end_trigger_functions;
326 Lisp_Object Qinhibit_point_motion_hooks;
327 static Lisp_Object QCeval, QCpropertize;
328 Lisp_Object QCfile, QCdata;
329 static Lisp_Object Qfontified;
330 static Lisp_Object Qgrow_only;
331 static Lisp_Object Qinhibit_eval_during_redisplay;
332 static Lisp_Object Qbuffer_position, Qposition, Qobject;
333 static Lisp_Object Qright_to_left, Qleft_to_right;
334
335 /* Cursor shapes. */
336 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
337
338 /* Pointer shapes. */
339 static Lisp_Object Qarrow, Qhand;
340 Lisp_Object Qtext;
341
342 /* Holds the list (error). */
343 static Lisp_Object list_of_error;
344
345 static Lisp_Object Qfontification_functions;
346
347 static Lisp_Object Qwrap_prefix;
348 static Lisp_Object Qline_prefix;
349 static Lisp_Object Qredisplay_internal;
350
351 /* Non-nil means don't actually do any redisplay. */
352
353 Lisp_Object Qinhibit_redisplay;
354
355 /* Names of text properties relevant for redisplay. */
356
357 Lisp_Object Qdisplay;
358
359 Lisp_Object Qspace, QCalign_to;
360 static Lisp_Object QCrelative_width, QCrelative_height;
361 Lisp_Object Qleft_margin, Qright_margin;
362 static Lisp_Object Qspace_width, Qraise;
363 static Lisp_Object Qslice;
364 Lisp_Object Qcenter;
365 static Lisp_Object Qmargin, Qpointer;
366 static Lisp_Object Qline_height;
367
368 #ifdef HAVE_WINDOW_SYSTEM
369
370 /* Test if overflow newline into fringe. Called with iterator IT
371 at or past right window margin, and with IT->current_x set. */
372
373 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
374 (!NILP (Voverflow_newline_into_fringe) \
375 && FRAME_WINDOW_P ((IT)->f) \
376 && ((IT)->bidi_it.paragraph_dir == R2L \
377 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
378 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
379 && (IT)->current_x == (IT)->last_visible_x)
380
381 #else /* !HAVE_WINDOW_SYSTEM */
382 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
383 #endif /* HAVE_WINDOW_SYSTEM */
384
385 /* Test if the display element loaded in IT, or the underlying buffer
386 or string character, is a space or a TAB character. This is used
387 to determine where word wrapping can occur. */
388
389 #define IT_DISPLAYING_WHITESPACE(it) \
390 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
391 || ((STRINGP (it->string) \
392 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
393 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
394 || (it->s \
395 && (it->s[IT_BYTEPOS (*it)] == ' ' \
396 || it->s[IT_BYTEPOS (*it)] == '\t')) \
397 || (IT_BYTEPOS (*it) < ZV_BYTE \
398 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
399 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
400
401 /* Name of the face used to highlight trailing whitespace. */
402
403 static Lisp_Object Qtrailing_whitespace;
404
405 /* Name and number of the face used to highlight escape glyphs. */
406
407 static Lisp_Object Qescape_glyph;
408
409 /* Name and number of the face used to highlight non-breaking spaces. */
410
411 static Lisp_Object Qnobreak_space;
412
413 /* The symbol `image' which is the car of the lists used to represent
414 images in Lisp. Also a tool bar style. */
415
416 Lisp_Object Qimage;
417
418 /* The image map types. */
419 Lisp_Object QCmap;
420 static Lisp_Object QCpointer;
421 static Lisp_Object Qrect, Qcircle, Qpoly;
422
423 /* Tool bar styles */
424 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
425
426 /* Non-zero means print newline to stdout before next mini-buffer
427 message. */
428
429 int noninteractive_need_newline;
430
431 /* Non-zero means print newline to message log before next message. */
432
433 static int message_log_need_newline;
434
435 /* Three markers that message_dolog uses.
436 It could allocate them itself, but that causes trouble
437 in handling memory-full errors. */
438 static Lisp_Object message_dolog_marker1;
439 static Lisp_Object message_dolog_marker2;
440 static Lisp_Object message_dolog_marker3;
441 \f
442 /* The buffer position of the first character appearing entirely or
443 partially on the line of the selected window which contains the
444 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
445 redisplay optimization in redisplay_internal. */
446
447 static struct text_pos this_line_start_pos;
448
449 /* Number of characters past the end of the line above, including the
450 terminating newline. */
451
452 static struct text_pos this_line_end_pos;
453
454 /* The vertical positions and the height of this line. */
455
456 static int this_line_vpos;
457 static int this_line_y;
458 static int this_line_pixel_height;
459
460 /* X position at which this display line starts. Usually zero;
461 negative if first character is partially visible. */
462
463 static int this_line_start_x;
464
465 /* The smallest character position seen by move_it_* functions as they
466 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
467 hscrolled lines, see display_line. */
468
469 static struct text_pos this_line_min_pos;
470
471 /* Buffer that this_line_.* variables are referring to. */
472
473 static struct buffer *this_line_buffer;
474
475
476 /* Values of those variables at last redisplay are stored as
477 properties on `overlay-arrow-position' symbol. However, if
478 Voverlay_arrow_position is a marker, last-arrow-position is its
479 numerical position. */
480
481 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
482
483 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
484 properties on a symbol in overlay-arrow-variable-list. */
485
486 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
487
488 Lisp_Object Qmenu_bar_update_hook;
489
490 /* Nonzero if an overlay arrow has been displayed in this window. */
491
492 static int overlay_arrow_seen;
493
494 /* Vector containing glyphs for an ellipsis `...'. */
495
496 static Lisp_Object default_invis_vector[3];
497
498 /* This is the window where the echo area message was displayed. It
499 is always a mini-buffer window, but it may not be the same window
500 currently active as a mini-buffer. */
501
502 Lisp_Object echo_area_window;
503
504 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
505 pushes the current message and the value of
506 message_enable_multibyte on the stack, the function restore_message
507 pops the stack and displays MESSAGE again. */
508
509 static Lisp_Object Vmessage_stack;
510
511 /* Nonzero means multibyte characters were enabled when the echo area
512 message was specified. */
513
514 static int message_enable_multibyte;
515
516 /* Nonzero if we should redraw the mode lines on the next redisplay. */
517
518 int update_mode_lines;
519
520 /* Nonzero if window sizes or contents have changed since last
521 redisplay that finished. */
522
523 int windows_or_buffers_changed;
524
525 /* Nonzero means a frame's cursor type has been changed. */
526
527 static int cursor_type_changed;
528
529 /* Nonzero after display_mode_line if %l was used and it displayed a
530 line number. */
531
532 static int line_number_displayed;
533
534 /* The name of the *Messages* buffer, a string. */
535
536 static Lisp_Object Vmessages_buffer_name;
537
538 /* Current, index 0, and last displayed echo area message. Either
539 buffers from echo_buffers, or nil to indicate no message. */
540
541 Lisp_Object echo_area_buffer[2];
542
543 /* The buffers referenced from echo_area_buffer. */
544
545 static Lisp_Object echo_buffer[2];
546
547 /* A vector saved used in with_area_buffer to reduce consing. */
548
549 static Lisp_Object Vwith_echo_area_save_vector;
550
551 /* Non-zero means display_echo_area should display the last echo area
552 message again. Set by redisplay_preserve_echo_area. */
553
554 static int display_last_displayed_message_p;
555
556 /* Nonzero if echo area is being used by print; zero if being used by
557 message. */
558
559 static int message_buf_print;
560
561 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
562
563 static Lisp_Object Qinhibit_menubar_update;
564 static Lisp_Object Qmessage_truncate_lines;
565
566 /* Set to 1 in clear_message to make redisplay_internal aware
567 of an emptied echo area. */
568
569 static int message_cleared_p;
570
571 /* A scratch glyph row with contents used for generating truncation
572 glyphs. Also used in direct_output_for_insert. */
573
574 #define MAX_SCRATCH_GLYPHS 100
575 static struct glyph_row scratch_glyph_row;
576 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
577
578 /* Ascent and height of the last line processed by move_it_to. */
579
580 static int last_height;
581
582 /* Non-zero if there's a help-echo in the echo area. */
583
584 int help_echo_showing_p;
585
586 /* If >= 0, computed, exact values of mode-line and header-line height
587 to use in the macros CURRENT_MODE_LINE_HEIGHT and
588 CURRENT_HEADER_LINE_HEIGHT. */
589
590 int current_mode_line_height, current_header_line_height;
591
592 /* The maximum distance to look ahead for text properties. Values
593 that are too small let us call compute_char_face and similar
594 functions too often which is expensive. Values that are too large
595 let us call compute_char_face and alike too often because we
596 might not be interested in text properties that far away. */
597
598 #define TEXT_PROP_DISTANCE_LIMIT 100
599
600 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
601 iterator state and later restore it. This is needed because the
602 bidi iterator on bidi.c keeps a stacked cache of its states, which
603 is really a singleton. When we use scratch iterator objects to
604 move around the buffer, we can cause the bidi cache to be pushed or
605 popped, and therefore we need to restore the cache state when we
606 return to the original iterator. */
607 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
608 do { \
609 if (CACHE) \
610 bidi_unshelve_cache (CACHE, 1); \
611 ITCOPY = ITORIG; \
612 CACHE = bidi_shelve_cache (); \
613 } while (0)
614
615 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
616 do { \
617 if (pITORIG != pITCOPY) \
618 *(pITORIG) = *(pITCOPY); \
619 bidi_unshelve_cache (CACHE, 0); \
620 CACHE = NULL; \
621 } while (0)
622
623 #ifdef GLYPH_DEBUG
624
625 /* Non-zero means print traces of redisplay if compiled with
626 GLYPH_DEBUG defined. */
627
628 int trace_redisplay_p;
629
630 #endif /* GLYPH_DEBUG */
631
632 #ifdef DEBUG_TRACE_MOVE
633 /* Non-zero means trace with TRACE_MOVE to stderr. */
634 int trace_move;
635
636 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
637 #else
638 #define TRACE_MOVE(x) (void) 0
639 #endif
640
641 static Lisp_Object Qauto_hscroll_mode;
642
643 /* Buffer being redisplayed -- for redisplay_window_error. */
644
645 static struct buffer *displayed_buffer;
646
647 /* Value returned from text property handlers (see below). */
648
649 enum prop_handled
650 {
651 HANDLED_NORMALLY,
652 HANDLED_RECOMPUTE_PROPS,
653 HANDLED_OVERLAY_STRING_CONSUMED,
654 HANDLED_RETURN
655 };
656
657 /* A description of text properties that redisplay is interested
658 in. */
659
660 struct props
661 {
662 /* The name of the property. */
663 Lisp_Object *name;
664
665 /* A unique index for the property. */
666 enum prop_idx idx;
667
668 /* A handler function called to set up iterator IT from the property
669 at IT's current position. Value is used to steer handle_stop. */
670 enum prop_handled (*handler) (struct it *it);
671 };
672
673 static enum prop_handled handle_face_prop (struct it *);
674 static enum prop_handled handle_invisible_prop (struct it *);
675 static enum prop_handled handle_display_prop (struct it *);
676 static enum prop_handled handle_composition_prop (struct it *);
677 static enum prop_handled handle_overlay_change (struct it *);
678 static enum prop_handled handle_fontified_prop (struct it *);
679
680 /* Properties handled by iterators. */
681
682 static struct props it_props[] =
683 {
684 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
685 /* Handle `face' before `display' because some sub-properties of
686 `display' need to know the face. */
687 {&Qface, FACE_PROP_IDX, handle_face_prop},
688 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
689 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
690 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
691 {NULL, 0, NULL}
692 };
693
694 /* Value is the position described by X. If X is a marker, value is
695 the marker_position of X. Otherwise, value is X. */
696
697 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
698
699 /* Enumeration returned by some move_it_.* functions internally. */
700
701 enum move_it_result
702 {
703 /* Not used. Undefined value. */
704 MOVE_UNDEFINED,
705
706 /* Move ended at the requested buffer position or ZV. */
707 MOVE_POS_MATCH_OR_ZV,
708
709 /* Move ended at the requested X pixel position. */
710 MOVE_X_REACHED,
711
712 /* Move within a line ended at the end of a line that must be
713 continued. */
714 MOVE_LINE_CONTINUED,
715
716 /* Move within a line ended at the end of a line that would
717 be displayed truncated. */
718 MOVE_LINE_TRUNCATED,
719
720 /* Move within a line ended at a line end. */
721 MOVE_NEWLINE_OR_CR
722 };
723
724 /* This counter is used to clear the face cache every once in a while
725 in redisplay_internal. It is incremented for each redisplay.
726 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
727 cleared. */
728
729 #define CLEAR_FACE_CACHE_COUNT 500
730 static int clear_face_cache_count;
731
732 /* Similarly for the image cache. */
733
734 #ifdef HAVE_WINDOW_SYSTEM
735 #define CLEAR_IMAGE_CACHE_COUNT 101
736 static int clear_image_cache_count;
737
738 /* Null glyph slice */
739 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
740 #endif
741
742 /* True while redisplay_internal is in progress. */
743
744 bool redisplaying_p;
745
746 static Lisp_Object Qinhibit_free_realized_faces;
747 static Lisp_Object Qmode_line_default_help_echo;
748
749 /* If a string, XTread_socket generates an event to display that string.
750 (The display is done in read_char.) */
751
752 Lisp_Object help_echo_string;
753 Lisp_Object help_echo_window;
754 Lisp_Object help_echo_object;
755 ptrdiff_t help_echo_pos;
756
757 /* Temporary variable for XTread_socket. */
758
759 Lisp_Object previous_help_echo_string;
760
761 /* Platform-independent portion of hourglass implementation. */
762
763 /* Non-zero means an hourglass cursor is currently shown. */
764 int hourglass_shown_p;
765
766 /* If non-null, an asynchronous timer that, when it expires, displays
767 an hourglass cursor on all frames. */
768 struct atimer *hourglass_atimer;
769
770 /* Name of the face used to display glyphless characters. */
771 Lisp_Object Qglyphless_char;
772
773 /* Symbol for the purpose of Vglyphless_char_display. */
774 static Lisp_Object Qglyphless_char_display;
775
776 /* Method symbols for Vglyphless_char_display. */
777 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
778
779 /* Default pixel width of `thin-space' display method. */
780 #define THIN_SPACE_WIDTH 1
781
782 /* Default number of seconds to wait before displaying an hourglass
783 cursor. */
784 #define DEFAULT_HOURGLASS_DELAY 1
785
786 \f
787 /* Function prototypes. */
788
789 static void setup_for_ellipsis (struct it *, int);
790 static void set_iterator_to_next (struct it *, int);
791 static void mark_window_display_accurate_1 (struct window *, int);
792 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
793 static int display_prop_string_p (Lisp_Object, Lisp_Object);
794 static int row_for_charpos_p (struct glyph_row *, ptrdiff_t);
795 static int cursor_row_p (struct glyph_row *);
796 static int redisplay_mode_lines (Lisp_Object, int);
797 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
798
799 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
800
801 static void handle_line_prefix (struct it *);
802
803 static void pint2str (char *, int, ptrdiff_t);
804 static void pint2hrstr (char *, int, ptrdiff_t);
805 static struct text_pos run_window_scroll_functions (Lisp_Object,
806 struct text_pos);
807 static int text_outside_line_unchanged_p (struct window *,
808 ptrdiff_t, ptrdiff_t);
809 static void store_mode_line_noprop_char (char);
810 static int store_mode_line_noprop (const char *, int, int);
811 static void handle_stop (struct it *);
812 static void handle_stop_backwards (struct it *, ptrdiff_t);
813 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
814 static void ensure_echo_area_buffers (void);
815 static void unwind_with_echo_area_buffer (Lisp_Object);
816 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
817 static int with_echo_area_buffer (struct window *, int,
818 int (*) (ptrdiff_t, Lisp_Object),
819 ptrdiff_t, Lisp_Object);
820 static void clear_garbaged_frames (void);
821 static int current_message_1 (ptrdiff_t, Lisp_Object);
822 static int truncate_message_1 (ptrdiff_t, Lisp_Object);
823 static void set_message (Lisp_Object);
824 static int set_message_1 (ptrdiff_t, Lisp_Object);
825 static int display_echo_area (struct window *);
826 static int display_echo_area_1 (ptrdiff_t, Lisp_Object);
827 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object);
828 static void unwind_redisplay (void);
829 static int string_char_and_length (const unsigned char *, int *);
830 static struct text_pos display_prop_end (struct it *, Lisp_Object,
831 struct text_pos);
832 static int compute_window_start_on_continuation_line (struct window *);
833 static void insert_left_trunc_glyphs (struct it *);
834 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
835 Lisp_Object);
836 static void extend_face_to_end_of_line (struct it *);
837 static int append_space_for_newline (struct it *, int);
838 static int cursor_row_fully_visible_p (struct window *, int, int);
839 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
840 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
841 static int trailing_whitespace_p (ptrdiff_t);
842 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
843 static void push_it (struct it *, struct text_pos *);
844 static void iterate_out_of_display_property (struct it *);
845 static void pop_it (struct it *);
846 static void sync_frame_with_window_matrix_rows (struct window *);
847 static void redisplay_internal (void);
848 static int echo_area_display (int);
849 static void redisplay_windows (Lisp_Object);
850 static void redisplay_window (Lisp_Object, int);
851 static Lisp_Object redisplay_window_error (Lisp_Object);
852 static Lisp_Object redisplay_window_0 (Lisp_Object);
853 static Lisp_Object redisplay_window_1 (Lisp_Object);
854 static int set_cursor_from_row (struct window *, struct glyph_row *,
855 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
856 int, int);
857 static int update_menu_bar (struct frame *, int, int);
858 static int try_window_reusing_current_matrix (struct window *);
859 static int try_window_id (struct window *);
860 static int display_line (struct it *);
861 static int display_mode_lines (struct window *);
862 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
863 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
864 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
865 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
866 static void display_menu_bar (struct window *);
867 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
868 ptrdiff_t *);
869 static int display_string (const char *, Lisp_Object, Lisp_Object,
870 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
871 static void compute_line_metrics (struct it *);
872 static void run_redisplay_end_trigger_hook (struct it *);
873 static int get_overlay_strings (struct it *, ptrdiff_t);
874 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
875 static void next_overlay_string (struct it *);
876 static void reseat (struct it *, struct text_pos, int);
877 static void reseat_1 (struct it *, struct text_pos, int);
878 static void back_to_previous_visible_line_start (struct it *);
879 static void reseat_at_next_visible_line_start (struct it *, int);
880 static int next_element_from_ellipsis (struct it *);
881 static int next_element_from_display_vector (struct it *);
882 static int next_element_from_string (struct it *);
883 static int next_element_from_c_string (struct it *);
884 static int next_element_from_buffer (struct it *);
885 static int next_element_from_composition (struct it *);
886 static int next_element_from_image (struct it *);
887 static int next_element_from_stretch (struct it *);
888 static void load_overlay_strings (struct it *, ptrdiff_t);
889 static int init_from_display_pos (struct it *, struct window *,
890 struct display_pos *);
891 static void reseat_to_string (struct it *, const char *,
892 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
893 static int get_next_display_element (struct it *);
894 static enum move_it_result
895 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
896 enum move_operation_enum);
897 static void get_visually_first_element (struct it *);
898 static void init_to_row_start (struct it *, struct window *,
899 struct glyph_row *);
900 static int init_to_row_end (struct it *, struct window *,
901 struct glyph_row *);
902 static void back_to_previous_line_start (struct it *);
903 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
904 static struct text_pos string_pos_nchars_ahead (struct text_pos,
905 Lisp_Object, ptrdiff_t);
906 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
907 static struct text_pos c_string_pos (ptrdiff_t, const char *, bool);
908 static ptrdiff_t number_of_chars (const char *, bool);
909 static void compute_stop_pos (struct it *);
910 static void compute_string_pos (struct text_pos *, struct text_pos,
911 Lisp_Object);
912 static int face_before_or_after_it_pos (struct it *, int);
913 static ptrdiff_t next_overlay_change (ptrdiff_t);
914 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
915 Lisp_Object, struct text_pos *, ptrdiff_t, int);
916 static int handle_single_display_spec (struct it *, Lisp_Object,
917 Lisp_Object, Lisp_Object,
918 struct text_pos *, ptrdiff_t, int, int);
919 static int underlying_face_id (struct it *);
920 static int in_ellipses_for_invisible_text_p (struct display_pos *,
921 struct window *);
922
923 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
924 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
925
926 #ifdef HAVE_WINDOW_SYSTEM
927
928 static void x_consider_frame_title (Lisp_Object);
929 static int tool_bar_lines_needed (struct frame *, int *);
930 static void update_tool_bar (struct frame *, int);
931 static void build_desired_tool_bar_string (struct frame *f);
932 static int redisplay_tool_bar (struct frame *);
933 static void display_tool_bar_line (struct it *, int);
934 static void notice_overwritten_cursor (struct window *,
935 enum glyph_row_area,
936 int, int, int, int);
937 static void append_stretch_glyph (struct it *, Lisp_Object,
938 int, int, int);
939
940
941 #endif /* HAVE_WINDOW_SYSTEM */
942
943 static void produce_special_glyphs (struct it *, enum display_element_type);
944 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
945 static int coords_in_mouse_face_p (struct window *, int, int);
946
947
948 \f
949 /***********************************************************************
950 Window display dimensions
951 ***********************************************************************/
952
953 /* Return the bottom boundary y-position for text lines in window W.
954 This is the first y position at which a line cannot start.
955 It is relative to the top of the window.
956
957 This is the height of W minus the height of a mode line, if any. */
958
959 int
960 window_text_bottom_y (struct window *w)
961 {
962 int height = WINDOW_TOTAL_HEIGHT (w);
963
964 if (WINDOW_WANTS_MODELINE_P (w))
965 height -= CURRENT_MODE_LINE_HEIGHT (w);
966 return height;
967 }
968
969 /* Return the pixel width of display area AREA of window W.
970 ANY_AREA means return the total width of W, not including
971 fringes to the left and right of the window. */
972
973 int
974 window_box_width (struct window *w, enum glyph_row_area 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. ANY_AREA 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, enum glyph_row_area 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. ANY_AREA 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, enum glyph_row_area 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. ANY_AREA 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, enum glyph_row_area 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. ANY_AREA 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, enum glyph_row_area 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. ANY_AREA 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, enum glyph_row_area area, int *box_x,
1132 int *box_y, 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 and both fringes of the window. Return in *TOP_LEFT_X
1151 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1152 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1153 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1154 box. */
1155
1156 static void
1157 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1158 int *bottom_right_x, int *bottom_right_y)
1159 {
1160 window_box (w, ANY_AREA, top_left_x, top_left_y,
1161 bottom_right_x, bottom_right_y);
1162 *bottom_right_x += *top_left_x;
1163 *bottom_right_y += *top_left_y;
1164 }
1165
1166
1167 \f
1168 /***********************************************************************
1169 Utilities
1170 ***********************************************************************/
1171
1172 /* Return the bottom y-position of the line the iterator IT is in.
1173 This can modify IT's settings. */
1174
1175 int
1176 line_bottom_y (struct it *it)
1177 {
1178 int line_height = it->max_ascent + it->max_descent;
1179 int line_top_y = it->current_y;
1180
1181 if (line_height == 0)
1182 {
1183 if (last_height)
1184 line_height = last_height;
1185 else if (IT_CHARPOS (*it) < ZV)
1186 {
1187 move_it_by_lines (it, 1);
1188 line_height = (it->max_ascent || it->max_descent
1189 ? it->max_ascent + it->max_descent
1190 : last_height);
1191 }
1192 else
1193 {
1194 struct glyph_row *row = it->glyph_row;
1195
1196 /* Use the default character height. */
1197 it->glyph_row = NULL;
1198 it->what = IT_CHARACTER;
1199 it->c = ' ';
1200 it->len = 1;
1201 PRODUCE_GLYPHS (it);
1202 line_height = it->ascent + it->descent;
1203 it->glyph_row = row;
1204 }
1205 }
1206
1207 return line_top_y + line_height;
1208 }
1209
1210 DEFUN ("line-pixel-height", Fline_pixel_height,
1211 Sline_pixel_height, 0, 0, 0,
1212 doc: /* Return height in pixels of text line in the selected window.
1213
1214 Value is the height in pixels of the line at point. */)
1215 (void)
1216 {
1217 struct it it;
1218 struct text_pos pt;
1219 struct window *w = XWINDOW (selected_window);
1220
1221 SET_TEXT_POS (pt, PT, PT_BYTE);
1222 start_display (&it, w, pt);
1223 it.vpos = it.current_y = 0;
1224 last_height = 0;
1225 return make_number (line_bottom_y (&it));
1226 }
1227
1228 /* Return the default pixel height of text lines in window W. The
1229 value is the canonical height of the W frame's default font, plus
1230 any extra space required by the line-spacing variable or frame
1231 parameter.
1232
1233 Implementation note: this ignores any line-spacing text properties
1234 put on the newline characters. This is because those properties
1235 only affect the _screen_ line ending in the newline (i.e., in a
1236 continued line, only the last screen line will be affected), which
1237 means only a small number of lines in a buffer can ever use this
1238 feature. Since this function is used to compute the default pixel
1239 equivalent of text lines in a window, we can safely ignore those
1240 few lines. For the same reasons, we ignore the line-height
1241 properties. */
1242 int
1243 default_line_pixel_height (struct window *w)
1244 {
1245 struct frame *f = WINDOW_XFRAME (w);
1246 int height = FRAME_LINE_HEIGHT (f);
1247
1248 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1249 {
1250 struct buffer *b = XBUFFER (w->contents);
1251 Lisp_Object val = BVAR (b, extra_line_spacing);
1252
1253 if (NILP (val))
1254 val = BVAR (&buffer_defaults, extra_line_spacing);
1255 if (!NILP (val))
1256 {
1257 if (RANGED_INTEGERP (0, val, INT_MAX))
1258 height += XFASTINT (val);
1259 else if (FLOATP (val))
1260 {
1261 int addon = XFLOAT_DATA (val) * height + 0.5;
1262
1263 if (addon >= 0)
1264 height += addon;
1265 }
1266 }
1267 else
1268 height += f->extra_line_spacing;
1269 }
1270
1271 return height;
1272 }
1273
1274 /* Subroutine of pos_visible_p below. Extracts a display string, if
1275 any, from the display spec given as its argument. */
1276 static Lisp_Object
1277 string_from_display_spec (Lisp_Object spec)
1278 {
1279 if (CONSP (spec))
1280 {
1281 while (CONSP (spec))
1282 {
1283 if (STRINGP (XCAR (spec)))
1284 return XCAR (spec);
1285 spec = XCDR (spec);
1286 }
1287 }
1288 else if (VECTORP (spec))
1289 {
1290 ptrdiff_t i;
1291
1292 for (i = 0; i < ASIZE (spec); i++)
1293 {
1294 if (STRINGP (AREF (spec, i)))
1295 return AREF (spec, i);
1296 }
1297 return Qnil;
1298 }
1299
1300 return spec;
1301 }
1302
1303
1304 /* Limit insanely large values of W->hscroll on frame F to the largest
1305 value that will still prevent first_visible_x and last_visible_x of
1306 'struct it' from overflowing an int. */
1307 static int
1308 window_hscroll_limited (struct window *w, struct frame *f)
1309 {
1310 ptrdiff_t window_hscroll = w->hscroll;
1311 int window_text_width = window_box_width (w, TEXT_AREA);
1312 int colwidth = FRAME_COLUMN_WIDTH (f);
1313
1314 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1315 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1316
1317 return window_hscroll;
1318 }
1319
1320 /* Return 1 if position CHARPOS is visible in window W.
1321 CHARPOS < 0 means return info about WINDOW_END position.
1322 If visible, set *X and *Y to pixel coordinates of top left corner.
1323 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1324 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1325
1326 int
1327 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1328 int *rtop, int *rbot, int *rowh, int *vpos)
1329 {
1330 struct it it;
1331 void *itdata = bidi_shelve_cache ();
1332 struct text_pos top;
1333 int visible_p = 0;
1334 struct buffer *old_buffer = NULL;
1335
1336 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1337 return visible_p;
1338
1339 if (XBUFFER (w->contents) != current_buffer)
1340 {
1341 old_buffer = current_buffer;
1342 set_buffer_internal_1 (XBUFFER (w->contents));
1343 }
1344
1345 SET_TEXT_POS_FROM_MARKER (top, w->start);
1346 /* Scrolling a minibuffer window via scroll bar when the echo area
1347 shows long text sometimes resets the minibuffer contents behind
1348 our backs. */
1349 if (CHARPOS (top) > ZV)
1350 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1351
1352 /* Compute exact mode line heights. */
1353 if (WINDOW_WANTS_MODELINE_P (w))
1354 current_mode_line_height
1355 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1356 BVAR (current_buffer, mode_line_format));
1357
1358 if (WINDOW_WANTS_HEADER_LINE_P (w))
1359 current_header_line_height
1360 = display_mode_line (w, HEADER_LINE_FACE_ID,
1361 BVAR (current_buffer, header_line_format));
1362
1363 start_display (&it, w, top);
1364 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1365 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1366
1367 if (charpos >= 0
1368 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1369 && IT_CHARPOS (it) >= charpos)
1370 /* When scanning backwards under bidi iteration, move_it_to
1371 stops at or _before_ CHARPOS, because it stops at or to
1372 the _right_ of the character at CHARPOS. */
1373 || (it.bidi_p && it.bidi_it.scan_dir == -1
1374 && IT_CHARPOS (it) <= charpos)))
1375 {
1376 /* We have reached CHARPOS, or passed it. How the call to
1377 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1378 or covered by a display property, move_it_to stops at the end
1379 of the invisible text, to the right of CHARPOS. (ii) If
1380 CHARPOS is in a display vector, move_it_to stops on its last
1381 glyph. */
1382 int top_x = it.current_x;
1383 int top_y = it.current_y;
1384 /* Calling line_bottom_y may change it.method, it.position, etc. */
1385 enum it_method it_method = it.method;
1386 int bottom_y = (last_height = 0, line_bottom_y (&it));
1387 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1388
1389 if (top_y < window_top_y)
1390 visible_p = bottom_y > window_top_y;
1391 else if (top_y < it.last_visible_y)
1392 visible_p = 1;
1393 if (bottom_y >= it.last_visible_y
1394 && it.bidi_p && it.bidi_it.scan_dir == -1
1395 && IT_CHARPOS (it) < charpos)
1396 {
1397 /* When the last line of the window is scanned backwards
1398 under bidi iteration, we could be duped into thinking
1399 that we have passed CHARPOS, when in fact move_it_to
1400 simply stopped short of CHARPOS because it reached
1401 last_visible_y. To see if that's what happened, we call
1402 move_it_to again with a slightly larger vertical limit,
1403 and see if it actually moved vertically; if it did, we
1404 didn't really reach CHARPOS, which is beyond window end. */
1405 struct it save_it = it;
1406 /* Why 10? because we don't know how many canonical lines
1407 will the height of the next line(s) be. So we guess. */
1408 int ten_more_lines = 10 * default_line_pixel_height (w);
1409
1410 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1411 MOVE_TO_POS | MOVE_TO_Y);
1412 if (it.current_y > top_y)
1413 visible_p = 0;
1414
1415 it = save_it;
1416 }
1417 if (visible_p)
1418 {
1419 if (it_method == GET_FROM_DISPLAY_VECTOR)
1420 {
1421 /* We stopped on the last glyph of a display vector.
1422 Try and recompute. Hack alert! */
1423 if (charpos < 2 || top.charpos >= charpos)
1424 top_x = it.glyph_row->x;
1425 else
1426 {
1427 struct it it2, it2_prev;
1428 /* The idea is to get to the previous buffer
1429 position, consume the character there, and use
1430 the pixel coordinates we get after that. But if
1431 the previous buffer position is also displayed
1432 from a display vector, we need to consume all of
1433 the glyphs from that display vector. */
1434 start_display (&it2, w, top);
1435 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1436 /* If we didn't get to CHARPOS - 1, there's some
1437 replacing display property at that position, and
1438 we stopped after it. That is exactly the place
1439 whose coordinates we want. */
1440 if (IT_CHARPOS (it2) != charpos - 1)
1441 it2_prev = it2;
1442 else
1443 {
1444 /* Iterate until we get out of the display
1445 vector that displays the character at
1446 CHARPOS - 1. */
1447 do {
1448 get_next_display_element (&it2);
1449 PRODUCE_GLYPHS (&it2);
1450 it2_prev = it2;
1451 set_iterator_to_next (&it2, 1);
1452 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1453 && IT_CHARPOS (it2) < charpos);
1454 }
1455 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1456 || it2_prev.current_x > it2_prev.last_visible_x)
1457 top_x = it.glyph_row->x;
1458 else
1459 {
1460 top_x = it2_prev.current_x;
1461 top_y = it2_prev.current_y;
1462 }
1463 }
1464 }
1465 else if (IT_CHARPOS (it) != charpos)
1466 {
1467 Lisp_Object cpos = make_number (charpos);
1468 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1469 Lisp_Object string = string_from_display_spec (spec);
1470 struct text_pos tpos;
1471 int replacing_spec_p;
1472 bool newline_in_string
1473 = (STRINGP (string)
1474 && memchr (SDATA (string), '\n', SBYTES (string)));
1475
1476 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1477 replacing_spec_p
1478 = (!NILP (spec)
1479 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1480 charpos, FRAME_WINDOW_P (it.f)));
1481 /* The tricky code below is needed because there's a
1482 discrepancy between move_it_to and how we set cursor
1483 when PT is at the beginning of a portion of text
1484 covered by a display property or an overlay with a
1485 display property, or the display line ends in a
1486 newline from a display string. move_it_to will stop
1487 _after_ such display strings, whereas
1488 set_cursor_from_row conspires with cursor_row_p to
1489 place the cursor on the first glyph produced from the
1490 display string. */
1491
1492 /* We have overshoot PT because it is covered by a
1493 display property that replaces the text it covers.
1494 If the string includes embedded newlines, we are also
1495 in the wrong display line. Backtrack to the correct
1496 line, where the display property begins. */
1497 if (replacing_spec_p)
1498 {
1499 Lisp_Object startpos, endpos;
1500 EMACS_INT start, end;
1501 struct it it3;
1502 int it3_moved;
1503
1504 /* Find the first and the last buffer positions
1505 covered by the display string. */
1506 endpos =
1507 Fnext_single_char_property_change (cpos, Qdisplay,
1508 Qnil, Qnil);
1509 startpos =
1510 Fprevious_single_char_property_change (endpos, Qdisplay,
1511 Qnil, Qnil);
1512 start = XFASTINT (startpos);
1513 end = XFASTINT (endpos);
1514 /* Move to the last buffer position before the
1515 display property. */
1516 start_display (&it3, w, top);
1517 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1518 /* Move forward one more line if the position before
1519 the display string is a newline or if it is the
1520 rightmost character on a line that is
1521 continued or word-wrapped. */
1522 if (it3.method == GET_FROM_BUFFER
1523 && (it3.c == '\n'
1524 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1525 move_it_by_lines (&it3, 1);
1526 else if (move_it_in_display_line_to (&it3, -1,
1527 it3.current_x
1528 + it3.pixel_width,
1529 MOVE_TO_X)
1530 == MOVE_LINE_CONTINUED)
1531 {
1532 move_it_by_lines (&it3, 1);
1533 /* When we are under word-wrap, the #$@%!
1534 move_it_by_lines moves 2 lines, so we need to
1535 fix that up. */
1536 if (it3.line_wrap == WORD_WRAP)
1537 move_it_by_lines (&it3, -1);
1538 }
1539
1540 /* Record the vertical coordinate of the display
1541 line where we wound up. */
1542 top_y = it3.current_y;
1543 if (it3.bidi_p)
1544 {
1545 /* When characters are reordered for display,
1546 the character displayed to the left of the
1547 display string could be _after_ the display
1548 property in the logical order. Use the
1549 smallest vertical position of these two. */
1550 start_display (&it3, w, top);
1551 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1552 if (it3.current_y < top_y)
1553 top_y = it3.current_y;
1554 }
1555 /* Move from the top of the window to the beginning
1556 of the display line where the display string
1557 begins. */
1558 start_display (&it3, w, top);
1559 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1560 /* If it3_moved stays zero after the 'while' loop
1561 below, that means we already were at a newline
1562 before the loop (e.g., the display string begins
1563 with a newline), so we don't need to (and cannot)
1564 inspect the glyphs of it3.glyph_row, because
1565 PRODUCE_GLYPHS will not produce anything for a
1566 newline, and thus it3.glyph_row stays at its
1567 stale content it got at top of the window. */
1568 it3_moved = 0;
1569 /* Finally, advance the iterator until we hit the
1570 first display element whose character position is
1571 CHARPOS, or until the first newline from the
1572 display string, which signals the end of the
1573 display line. */
1574 while (get_next_display_element (&it3))
1575 {
1576 PRODUCE_GLYPHS (&it3);
1577 if (IT_CHARPOS (it3) == charpos
1578 || ITERATOR_AT_END_OF_LINE_P (&it3))
1579 break;
1580 it3_moved = 1;
1581 set_iterator_to_next (&it3, 0);
1582 }
1583 top_x = it3.current_x - it3.pixel_width;
1584 /* Normally, we would exit the above loop because we
1585 found the display element whose character
1586 position is CHARPOS. For the contingency that we
1587 didn't, and stopped at the first newline from the
1588 display string, move back over the glyphs
1589 produced from the string, until we find the
1590 rightmost glyph not from the string. */
1591 if (it3_moved
1592 && newline_in_string
1593 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1594 {
1595 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1596 + it3.glyph_row->used[TEXT_AREA];
1597
1598 while (EQ ((g - 1)->object, string))
1599 {
1600 --g;
1601 top_x -= g->pixel_width;
1602 }
1603 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1604 + it3.glyph_row->used[TEXT_AREA]);
1605 }
1606 }
1607 }
1608
1609 *x = top_x;
1610 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1611 *rtop = max (0, window_top_y - top_y);
1612 *rbot = max (0, bottom_y - it.last_visible_y);
1613 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1614 - max (top_y, window_top_y)));
1615 *vpos = it.vpos;
1616 }
1617 }
1618 else
1619 {
1620 /* We were asked to provide info about WINDOW_END. */
1621 struct it it2;
1622 void *it2data = NULL;
1623
1624 SAVE_IT (it2, it, it2data);
1625 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1626 move_it_by_lines (&it, 1);
1627 if (charpos < IT_CHARPOS (it)
1628 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1629 {
1630 visible_p = 1;
1631 RESTORE_IT (&it2, &it2, it2data);
1632 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1633 *x = it2.current_x;
1634 *y = it2.current_y + it2.max_ascent - it2.ascent;
1635 *rtop = max (0, -it2.current_y);
1636 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1637 - it.last_visible_y));
1638 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1639 it.last_visible_y)
1640 - max (it2.current_y,
1641 WINDOW_HEADER_LINE_HEIGHT (w))));
1642 *vpos = it2.vpos;
1643 }
1644 else
1645 bidi_unshelve_cache (it2data, 1);
1646 }
1647 bidi_unshelve_cache (itdata, 0);
1648
1649 if (old_buffer)
1650 set_buffer_internal_1 (old_buffer);
1651
1652 current_header_line_height = current_mode_line_height = -1;
1653
1654 if (visible_p && w->hscroll > 0)
1655 *x -=
1656 window_hscroll_limited (w, WINDOW_XFRAME (w))
1657 * WINDOW_FRAME_COLUMN_WIDTH (w);
1658
1659 #if 0
1660 /* Debugging code. */
1661 if (visible_p)
1662 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1663 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1664 else
1665 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1666 #endif
1667
1668 return visible_p;
1669 }
1670
1671
1672 /* Return the next character from STR. Return in *LEN the length of
1673 the character. This is like STRING_CHAR_AND_LENGTH but never
1674 returns an invalid character. If we find one, we return a `?', but
1675 with the length of the invalid character. */
1676
1677 static int
1678 string_char_and_length (const unsigned char *str, int *len)
1679 {
1680 int c;
1681
1682 c = STRING_CHAR_AND_LENGTH (str, *len);
1683 if (!CHAR_VALID_P (c))
1684 /* We may not change the length here because other places in Emacs
1685 don't use this function, i.e. they silently accept invalid
1686 characters. */
1687 c = '?';
1688
1689 return c;
1690 }
1691
1692
1693
1694 /* Given a position POS containing a valid character and byte position
1695 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1696
1697 static struct text_pos
1698 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1699 {
1700 eassert (STRINGP (string) && nchars >= 0);
1701
1702 if (STRING_MULTIBYTE (string))
1703 {
1704 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1705 int len;
1706
1707 while (nchars--)
1708 {
1709 string_char_and_length (p, &len);
1710 p += len;
1711 CHARPOS (pos) += 1;
1712 BYTEPOS (pos) += len;
1713 }
1714 }
1715 else
1716 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1717
1718 return pos;
1719 }
1720
1721
1722 /* Value is the text position, i.e. character and byte position,
1723 for character position CHARPOS in STRING. */
1724
1725 static struct text_pos
1726 string_pos (ptrdiff_t charpos, Lisp_Object string)
1727 {
1728 struct text_pos pos;
1729 eassert (STRINGP (string));
1730 eassert (charpos >= 0);
1731 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1732 return pos;
1733 }
1734
1735
1736 /* Value is a text position, i.e. character and byte position, for
1737 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1738 means recognize multibyte characters. */
1739
1740 static struct text_pos
1741 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1742 {
1743 struct text_pos pos;
1744
1745 eassert (s != NULL);
1746 eassert (charpos >= 0);
1747
1748 if (multibyte_p)
1749 {
1750 int len;
1751
1752 SET_TEXT_POS (pos, 0, 0);
1753 while (charpos--)
1754 {
1755 string_char_and_length ((const unsigned char *) s, &len);
1756 s += len;
1757 CHARPOS (pos) += 1;
1758 BYTEPOS (pos) += len;
1759 }
1760 }
1761 else
1762 SET_TEXT_POS (pos, charpos, charpos);
1763
1764 return pos;
1765 }
1766
1767
1768 /* Value is the number of characters in C string S. MULTIBYTE_P
1769 non-zero means recognize multibyte characters. */
1770
1771 static ptrdiff_t
1772 number_of_chars (const char *s, bool multibyte_p)
1773 {
1774 ptrdiff_t nchars;
1775
1776 if (multibyte_p)
1777 {
1778 ptrdiff_t rest = strlen (s);
1779 int len;
1780 const unsigned char *p = (const unsigned char *) s;
1781
1782 for (nchars = 0; rest > 0; ++nchars)
1783 {
1784 string_char_and_length (p, &len);
1785 rest -= len, p += len;
1786 }
1787 }
1788 else
1789 nchars = strlen (s);
1790
1791 return nchars;
1792 }
1793
1794
1795 /* Compute byte position NEWPOS->bytepos corresponding to
1796 NEWPOS->charpos. POS is a known position in string STRING.
1797 NEWPOS->charpos must be >= POS.charpos. */
1798
1799 static void
1800 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1801 {
1802 eassert (STRINGP (string));
1803 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1804
1805 if (STRING_MULTIBYTE (string))
1806 *newpos = string_pos_nchars_ahead (pos, string,
1807 CHARPOS (*newpos) - CHARPOS (pos));
1808 else
1809 BYTEPOS (*newpos) = CHARPOS (*newpos);
1810 }
1811
1812 /* EXPORT:
1813 Return an estimation of the pixel height of mode or header lines on
1814 frame F. FACE_ID specifies what line's height to estimate. */
1815
1816 int
1817 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1818 {
1819 #ifdef HAVE_WINDOW_SYSTEM
1820 if (FRAME_WINDOW_P (f))
1821 {
1822 int height = FONT_HEIGHT (FRAME_FONT (f));
1823
1824 /* This function is called so early when Emacs starts that the face
1825 cache and mode line face are not yet initialized. */
1826 if (FRAME_FACE_CACHE (f))
1827 {
1828 struct face *face = FACE_FROM_ID (f, face_id);
1829 if (face)
1830 {
1831 if (face->font)
1832 height = FONT_HEIGHT (face->font);
1833 if (face->box_line_width > 0)
1834 height += 2 * face->box_line_width;
1835 }
1836 }
1837
1838 return height;
1839 }
1840 #endif
1841
1842 return 1;
1843 }
1844
1845 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1846 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1847 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1848 not force the value into range. */
1849
1850 void
1851 pixel_to_glyph_coords (struct frame *f, register int pix_x, register int pix_y,
1852 int *x, int *y, NativeRectangle *bounds, int noclip)
1853 {
1854
1855 #ifdef HAVE_WINDOW_SYSTEM
1856 if (FRAME_WINDOW_P (f))
1857 {
1858 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1859 even for negative values. */
1860 if (pix_x < 0)
1861 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1862 if (pix_y < 0)
1863 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1864
1865 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1866 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1867
1868 if (bounds)
1869 STORE_NATIVE_RECT (*bounds,
1870 FRAME_COL_TO_PIXEL_X (f, pix_x),
1871 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1872 FRAME_COLUMN_WIDTH (f) - 1,
1873 FRAME_LINE_HEIGHT (f) - 1);
1874
1875 if (!noclip)
1876 {
1877 if (pix_x < 0)
1878 pix_x = 0;
1879 else if (pix_x > FRAME_TOTAL_COLS (f))
1880 pix_x = FRAME_TOTAL_COLS (f);
1881
1882 if (pix_y < 0)
1883 pix_y = 0;
1884 else if (pix_y > FRAME_LINES (f))
1885 pix_y = FRAME_LINES (f);
1886 }
1887 }
1888 #endif
1889
1890 *x = pix_x;
1891 *y = pix_y;
1892 }
1893
1894
1895 /* Find the glyph under window-relative coordinates X/Y in window W.
1896 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1897 strings. Return in *HPOS and *VPOS the row and column number of
1898 the glyph found. Return in *AREA the glyph area containing X.
1899 Value is a pointer to the glyph found or null if X/Y is not on
1900 text, or we can't tell because W's current matrix is not up to
1901 date. */
1902
1903 static
1904 struct glyph *
1905 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1906 int *dx, int *dy, int *area)
1907 {
1908 struct glyph *glyph, *end;
1909 struct glyph_row *row = NULL;
1910 int x0, i;
1911
1912 /* Find row containing Y. Give up if some row is not enabled. */
1913 for (i = 0; i < w->current_matrix->nrows; ++i)
1914 {
1915 row = MATRIX_ROW (w->current_matrix, i);
1916 if (!row->enabled_p)
1917 return NULL;
1918 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1919 break;
1920 }
1921
1922 *vpos = i;
1923 *hpos = 0;
1924
1925 /* Give up if Y is not in the window. */
1926 if (i == w->current_matrix->nrows)
1927 return NULL;
1928
1929 /* Get the glyph area containing X. */
1930 if (w->pseudo_window_p)
1931 {
1932 *area = TEXT_AREA;
1933 x0 = 0;
1934 }
1935 else
1936 {
1937 if (x < window_box_left_offset (w, TEXT_AREA))
1938 {
1939 *area = LEFT_MARGIN_AREA;
1940 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1941 }
1942 else if (x < window_box_right_offset (w, TEXT_AREA))
1943 {
1944 *area = TEXT_AREA;
1945 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1946 }
1947 else
1948 {
1949 *area = RIGHT_MARGIN_AREA;
1950 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1951 }
1952 }
1953
1954 /* Find glyph containing X. */
1955 glyph = row->glyphs[*area];
1956 end = glyph + row->used[*area];
1957 x -= x0;
1958 while (glyph < end && x >= glyph->pixel_width)
1959 {
1960 x -= glyph->pixel_width;
1961 ++glyph;
1962 }
1963
1964 if (glyph == end)
1965 return NULL;
1966
1967 if (dx)
1968 {
1969 *dx = x;
1970 *dy = y - (row->y + row->ascent - glyph->ascent);
1971 }
1972
1973 *hpos = glyph - row->glyphs[*area];
1974 return glyph;
1975 }
1976
1977 /* Convert frame-relative x/y to coordinates relative to window W.
1978 Takes pseudo-windows into account. */
1979
1980 static void
1981 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1982 {
1983 if (w->pseudo_window_p)
1984 {
1985 /* A pseudo-window is always full-width, and starts at the
1986 left edge of the frame, plus a frame border. */
1987 struct frame *f = XFRAME (w->frame);
1988 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1989 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1990 }
1991 else
1992 {
1993 *x -= WINDOW_LEFT_EDGE_X (w);
1994 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1995 }
1996 }
1997
1998 #ifdef HAVE_WINDOW_SYSTEM
1999
2000 /* EXPORT:
2001 Return in RECTS[] at most N clipping rectangles for glyph string S.
2002 Return the number of stored rectangles. */
2003
2004 int
2005 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
2006 {
2007 XRectangle r;
2008
2009 if (n <= 0)
2010 return 0;
2011
2012 if (s->row->full_width_p)
2013 {
2014 /* Draw full-width. X coordinates are relative to S->w->left_col. */
2015 r.x = WINDOW_LEFT_EDGE_X (s->w);
2016 r.width = WINDOW_TOTAL_WIDTH (s->w);
2017
2018 /* Unless displaying a mode or menu bar line, which are always
2019 fully visible, clip to the visible part of the row. */
2020 if (s->w->pseudo_window_p)
2021 r.height = s->row->visible_height;
2022 else
2023 r.height = s->height;
2024 }
2025 else
2026 {
2027 /* This is a text line that may be partially visible. */
2028 r.x = window_box_left (s->w, s->area);
2029 r.width = window_box_width (s->w, s->area);
2030 r.height = s->row->visible_height;
2031 }
2032
2033 if (s->clip_head)
2034 if (r.x < s->clip_head->x)
2035 {
2036 if (r.width >= s->clip_head->x - r.x)
2037 r.width -= s->clip_head->x - r.x;
2038 else
2039 r.width = 0;
2040 r.x = s->clip_head->x;
2041 }
2042 if (s->clip_tail)
2043 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2044 {
2045 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2046 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2047 else
2048 r.width = 0;
2049 }
2050
2051 /* If S draws overlapping rows, it's sufficient to use the top and
2052 bottom of the window for clipping because this glyph string
2053 intentionally draws over other lines. */
2054 if (s->for_overlaps)
2055 {
2056 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2057 r.height = window_text_bottom_y (s->w) - r.y;
2058
2059 /* Alas, the above simple strategy does not work for the
2060 environments with anti-aliased text: if the same text is
2061 drawn onto the same place multiple times, it gets thicker.
2062 If the overlap we are processing is for the erased cursor, we
2063 take the intersection with the rectangle of the cursor. */
2064 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2065 {
2066 XRectangle rc, r_save = r;
2067
2068 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2069 rc.y = s->w->phys_cursor.y;
2070 rc.width = s->w->phys_cursor_width;
2071 rc.height = s->w->phys_cursor_height;
2072
2073 x_intersect_rectangles (&r_save, &rc, &r);
2074 }
2075 }
2076 else
2077 {
2078 /* Don't use S->y for clipping because it doesn't take partially
2079 visible lines into account. For example, it can be negative for
2080 partially visible lines at the top of a window. */
2081 if (!s->row->full_width_p
2082 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2083 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2084 else
2085 r.y = max (0, s->row->y);
2086 }
2087
2088 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2089
2090 /* If drawing the cursor, don't let glyph draw outside its
2091 advertised boundaries. Cleartype does this under some circumstances. */
2092 if (s->hl == DRAW_CURSOR)
2093 {
2094 struct glyph *glyph = s->first_glyph;
2095 int height, max_y;
2096
2097 if (s->x > r.x)
2098 {
2099 r.width -= s->x - r.x;
2100 r.x = s->x;
2101 }
2102 r.width = min (r.width, glyph->pixel_width);
2103
2104 /* If r.y is below window bottom, ensure that we still see a cursor. */
2105 height = min (glyph->ascent + glyph->descent,
2106 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2107 max_y = window_text_bottom_y (s->w) - height;
2108 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2109 if (s->ybase - glyph->ascent > max_y)
2110 {
2111 r.y = max_y;
2112 r.height = height;
2113 }
2114 else
2115 {
2116 /* Don't draw cursor glyph taller than our actual glyph. */
2117 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2118 if (height < r.height)
2119 {
2120 max_y = r.y + r.height;
2121 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2122 r.height = min (max_y - r.y, height);
2123 }
2124 }
2125 }
2126
2127 if (s->row->clip)
2128 {
2129 XRectangle r_save = r;
2130
2131 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2132 r.width = 0;
2133 }
2134
2135 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2136 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2137 {
2138 #ifdef CONVERT_FROM_XRECT
2139 CONVERT_FROM_XRECT (r, *rects);
2140 #else
2141 *rects = r;
2142 #endif
2143 return 1;
2144 }
2145 else
2146 {
2147 /* If we are processing overlapping and allowed to return
2148 multiple clipping rectangles, we exclude the row of the glyph
2149 string from the clipping rectangle. This is to avoid drawing
2150 the same text on the environment with anti-aliasing. */
2151 #ifdef CONVERT_FROM_XRECT
2152 XRectangle rs[2];
2153 #else
2154 XRectangle *rs = rects;
2155 #endif
2156 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2157
2158 if (s->for_overlaps & OVERLAPS_PRED)
2159 {
2160 rs[i] = r;
2161 if (r.y + r.height > row_y)
2162 {
2163 if (r.y < row_y)
2164 rs[i].height = row_y - r.y;
2165 else
2166 rs[i].height = 0;
2167 }
2168 i++;
2169 }
2170 if (s->for_overlaps & OVERLAPS_SUCC)
2171 {
2172 rs[i] = r;
2173 if (r.y < row_y + s->row->visible_height)
2174 {
2175 if (r.y + r.height > row_y + s->row->visible_height)
2176 {
2177 rs[i].y = row_y + s->row->visible_height;
2178 rs[i].height = r.y + r.height - rs[i].y;
2179 }
2180 else
2181 rs[i].height = 0;
2182 }
2183 i++;
2184 }
2185
2186 n = i;
2187 #ifdef CONVERT_FROM_XRECT
2188 for (i = 0; i < n; i++)
2189 CONVERT_FROM_XRECT (rs[i], rects[i]);
2190 #endif
2191 return n;
2192 }
2193 }
2194
2195 /* EXPORT:
2196 Return in *NR the clipping rectangle for glyph string S. */
2197
2198 void
2199 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2200 {
2201 get_glyph_string_clip_rects (s, nr, 1);
2202 }
2203
2204
2205 /* EXPORT:
2206 Return the position and height of the phys cursor in window W.
2207 Set w->phys_cursor_width to width of phys cursor.
2208 */
2209
2210 void
2211 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2212 struct glyph *glyph, int *xp, int *yp, int *heightp)
2213 {
2214 struct frame *f = XFRAME (WINDOW_FRAME (w));
2215 int x, y, wd, h, h0, y0;
2216
2217 /* Compute the width of the rectangle to draw. If on a stretch
2218 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2219 rectangle as wide as the glyph, but use a canonical character
2220 width instead. */
2221 wd = glyph->pixel_width - 1;
2222 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2223 wd++; /* Why? */
2224 #endif
2225
2226 x = w->phys_cursor.x;
2227 if (x < 0)
2228 {
2229 wd += x;
2230 x = 0;
2231 }
2232
2233 if (glyph->type == STRETCH_GLYPH
2234 && !x_stretch_cursor_p)
2235 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2236 w->phys_cursor_width = wd;
2237
2238 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2239
2240 /* If y is below window bottom, ensure that we still see a cursor. */
2241 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2242
2243 h = max (h0, glyph->ascent + glyph->descent);
2244 h0 = min (h0, glyph->ascent + glyph->descent);
2245
2246 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2247 if (y < y0)
2248 {
2249 h = max (h - (y0 - y) + 1, h0);
2250 y = y0 - 1;
2251 }
2252 else
2253 {
2254 y0 = window_text_bottom_y (w) - h0;
2255 if (y > y0)
2256 {
2257 h += y - y0;
2258 y = y0;
2259 }
2260 }
2261
2262 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2263 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2264 *heightp = h;
2265 }
2266
2267 /*
2268 * Remember which glyph the mouse is over.
2269 */
2270
2271 void
2272 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2273 {
2274 Lisp_Object window;
2275 struct window *w;
2276 struct glyph_row *r, *gr, *end_row;
2277 enum window_part part;
2278 enum glyph_row_area area;
2279 int x, y, width, height;
2280
2281 /* Try to determine frame pixel position and size of the glyph under
2282 frame pixel coordinates X/Y on frame F. */
2283
2284 if (!f->glyphs_initialized_p
2285 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2286 NILP (window)))
2287 {
2288 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2289 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2290 goto virtual_glyph;
2291 }
2292
2293 w = XWINDOW (window);
2294 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2295 height = WINDOW_FRAME_LINE_HEIGHT (w);
2296
2297 x = window_relative_x_coord (w, part, gx);
2298 y = gy - WINDOW_TOP_EDGE_Y (w);
2299
2300 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2301 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2302
2303 if (w->pseudo_window_p)
2304 {
2305 area = TEXT_AREA;
2306 part = ON_MODE_LINE; /* Don't adjust margin. */
2307 goto text_glyph;
2308 }
2309
2310 switch (part)
2311 {
2312 case ON_LEFT_MARGIN:
2313 area = LEFT_MARGIN_AREA;
2314 goto text_glyph;
2315
2316 case ON_RIGHT_MARGIN:
2317 area = RIGHT_MARGIN_AREA;
2318 goto text_glyph;
2319
2320 case ON_HEADER_LINE:
2321 case ON_MODE_LINE:
2322 gr = (part == ON_HEADER_LINE
2323 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2324 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2325 gy = gr->y;
2326 area = TEXT_AREA;
2327 goto text_glyph_row_found;
2328
2329 case ON_TEXT:
2330 area = TEXT_AREA;
2331
2332 text_glyph:
2333 gr = 0; gy = 0;
2334 for (; r <= end_row && r->enabled_p; ++r)
2335 if (r->y + r->height > y)
2336 {
2337 gr = r; gy = r->y;
2338 break;
2339 }
2340
2341 text_glyph_row_found:
2342 if (gr && gy <= y)
2343 {
2344 struct glyph *g = gr->glyphs[area];
2345 struct glyph *end = g + gr->used[area];
2346
2347 height = gr->height;
2348 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2349 if (gx + g->pixel_width > x)
2350 break;
2351
2352 if (g < end)
2353 {
2354 if (g->type == IMAGE_GLYPH)
2355 {
2356 /* Don't remember when mouse is over image, as
2357 image may have hot-spots. */
2358 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2359 return;
2360 }
2361 width = g->pixel_width;
2362 }
2363 else
2364 {
2365 /* Use nominal char spacing at end of line. */
2366 x -= gx;
2367 gx += (x / width) * width;
2368 }
2369
2370 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2371 gx += window_box_left_offset (w, area);
2372 }
2373 else
2374 {
2375 /* Use nominal line height at end of window. */
2376 gx = (x / width) * width;
2377 y -= gy;
2378 gy += (y / height) * height;
2379 }
2380 break;
2381
2382 case ON_LEFT_FRINGE:
2383 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2384 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2385 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2386 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2387 goto row_glyph;
2388
2389 case ON_RIGHT_FRINGE:
2390 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2391 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2392 : window_box_right_offset (w, TEXT_AREA));
2393 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2394 goto row_glyph;
2395
2396 case ON_SCROLL_BAR:
2397 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2398 ? 0
2399 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2400 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2401 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2402 : 0)));
2403 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2404
2405 row_glyph:
2406 gr = 0, gy = 0;
2407 for (; r <= end_row && r->enabled_p; ++r)
2408 if (r->y + r->height > y)
2409 {
2410 gr = r; gy = r->y;
2411 break;
2412 }
2413
2414 if (gr && gy <= y)
2415 height = gr->height;
2416 else
2417 {
2418 /* Use nominal line height at end of window. */
2419 y -= gy;
2420 gy += (y / height) * height;
2421 }
2422 break;
2423
2424 default:
2425 ;
2426 virtual_glyph:
2427 /* If there is no glyph under the mouse, then we divide the screen
2428 into a grid of the smallest glyph in the frame, and use that
2429 as our "glyph". */
2430
2431 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2432 round down even for negative values. */
2433 if (gx < 0)
2434 gx -= width - 1;
2435 if (gy < 0)
2436 gy -= height - 1;
2437
2438 gx = (gx / width) * width;
2439 gy = (gy / height) * height;
2440
2441 goto store_rect;
2442 }
2443
2444 gx += WINDOW_LEFT_EDGE_X (w);
2445 gy += WINDOW_TOP_EDGE_Y (w);
2446
2447 store_rect:
2448 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2449
2450 /* Visible feedback for debugging. */
2451 #if 0
2452 #if HAVE_X_WINDOWS
2453 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2454 f->output_data.x->normal_gc,
2455 gx, gy, width, height);
2456 #endif
2457 #endif
2458 }
2459
2460
2461 #endif /* HAVE_WINDOW_SYSTEM */
2462
2463 static void
2464 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2465 {
2466 eassert (w);
2467 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2468 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2469 w->window_end_vpos
2470 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2471 }
2472
2473 /***********************************************************************
2474 Lisp form evaluation
2475 ***********************************************************************/
2476
2477 /* Error handler for safe_eval and safe_call. */
2478
2479 static Lisp_Object
2480 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2481 {
2482 add_to_log ("Error during redisplay: %S signaled %S",
2483 Flist (nargs, args), arg);
2484 return Qnil;
2485 }
2486
2487 /* Call function FUNC with the rest of NARGS - 1 arguments
2488 following. Return the result, or nil if something went
2489 wrong. Prevent redisplay during the evaluation. */
2490
2491 Lisp_Object
2492 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2493 {
2494 Lisp_Object val;
2495
2496 if (inhibit_eval_during_redisplay)
2497 val = Qnil;
2498 else
2499 {
2500 va_list ap;
2501 ptrdiff_t i;
2502 ptrdiff_t count = SPECPDL_INDEX ();
2503 struct gcpro gcpro1;
2504 Lisp_Object *args = alloca (nargs * word_size);
2505
2506 args[0] = func;
2507 va_start (ap, func);
2508 for (i = 1; i < nargs; i++)
2509 args[i] = va_arg (ap, Lisp_Object);
2510 va_end (ap);
2511
2512 GCPRO1 (args[0]);
2513 gcpro1.nvars = nargs;
2514 specbind (Qinhibit_redisplay, Qt);
2515 /* Use Qt to ensure debugger does not run,
2516 so there is no possibility of wanting to redisplay. */
2517 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2518 safe_eval_handler);
2519 UNGCPRO;
2520 val = unbind_to (count, val);
2521 }
2522
2523 return val;
2524 }
2525
2526
2527 /* Call function FN with one argument ARG.
2528 Return the result, or nil if something went wrong. */
2529
2530 Lisp_Object
2531 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2532 {
2533 return safe_call (2, fn, arg);
2534 }
2535
2536 static Lisp_Object Qeval;
2537
2538 Lisp_Object
2539 safe_eval (Lisp_Object sexpr)
2540 {
2541 return safe_call1 (Qeval, sexpr);
2542 }
2543
2544 /* Call function FN with two arguments ARG1 and ARG2.
2545 Return the result, or nil if something went wrong. */
2546
2547 Lisp_Object
2548 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2549 {
2550 return safe_call (3, fn, arg1, arg2);
2551 }
2552
2553
2554 \f
2555 /***********************************************************************
2556 Debugging
2557 ***********************************************************************/
2558
2559 #if 0
2560
2561 /* Define CHECK_IT to perform sanity checks on iterators.
2562 This is for debugging. It is too slow to do unconditionally. */
2563
2564 static void
2565 check_it (struct it *it)
2566 {
2567 if (it->method == GET_FROM_STRING)
2568 {
2569 eassert (STRINGP (it->string));
2570 eassert (IT_STRING_CHARPOS (*it) >= 0);
2571 }
2572 else
2573 {
2574 eassert (IT_STRING_CHARPOS (*it) < 0);
2575 if (it->method == GET_FROM_BUFFER)
2576 {
2577 /* Check that character and byte positions agree. */
2578 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2579 }
2580 }
2581
2582 if (it->dpvec)
2583 eassert (it->current.dpvec_index >= 0);
2584 else
2585 eassert (it->current.dpvec_index < 0);
2586 }
2587
2588 #define CHECK_IT(IT) check_it ((IT))
2589
2590 #else /* not 0 */
2591
2592 #define CHECK_IT(IT) (void) 0
2593
2594 #endif /* not 0 */
2595
2596
2597 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2598
2599 /* Check that the window end of window W is what we expect it
2600 to be---the last row in the current matrix displaying text. */
2601
2602 static void
2603 check_window_end (struct window *w)
2604 {
2605 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2606 {
2607 struct glyph_row *row;
2608 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2609 !row->enabled_p
2610 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2611 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2612 }
2613 }
2614
2615 #define CHECK_WINDOW_END(W) check_window_end ((W))
2616
2617 #else
2618
2619 #define CHECK_WINDOW_END(W) (void) 0
2620
2621 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2622
2623 /* Return mark position if current buffer has the region of non-zero length,
2624 or -1 otherwise. */
2625
2626 static ptrdiff_t
2627 markpos_of_region (void)
2628 {
2629 if (!NILP (Vtransient_mark_mode)
2630 && !NILP (BVAR (current_buffer, mark_active))
2631 && XMARKER (BVAR (current_buffer, mark))->buffer != NULL)
2632 {
2633 ptrdiff_t markpos = XMARKER (BVAR (current_buffer, mark))->charpos;
2634
2635 if (markpos != PT)
2636 return markpos;
2637 }
2638 return -1;
2639 }
2640
2641 /***********************************************************************
2642 Iterator initialization
2643 ***********************************************************************/
2644
2645 /* Initialize IT for displaying current_buffer in window W, starting
2646 at character position CHARPOS. CHARPOS < 0 means that no buffer
2647 position is specified which is useful when the iterator is assigned
2648 a position later. BYTEPOS is the byte position corresponding to
2649 CHARPOS.
2650
2651 If ROW is not null, calls to produce_glyphs with IT as parameter
2652 will produce glyphs in that row.
2653
2654 BASE_FACE_ID is the id of a base face to use. It must be one of
2655 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2656 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2657 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2658
2659 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2660 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2661 will be initialized to use the corresponding mode line glyph row of
2662 the desired matrix of W. */
2663
2664 void
2665 init_iterator (struct it *it, struct window *w,
2666 ptrdiff_t charpos, ptrdiff_t bytepos,
2667 struct glyph_row *row, enum face_id base_face_id)
2668 {
2669 ptrdiff_t markpos;
2670 enum face_id remapped_base_face_id = base_face_id;
2671
2672 /* Some precondition checks. */
2673 eassert (w != NULL && it != NULL);
2674 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2675 && charpos <= ZV));
2676
2677 /* If face attributes have been changed since the last redisplay,
2678 free realized faces now because they depend on face definitions
2679 that might have changed. Don't free faces while there might be
2680 desired matrices pending which reference these faces. */
2681 if (face_change_count && !inhibit_free_realized_faces)
2682 {
2683 face_change_count = 0;
2684 free_all_realized_faces (Qnil);
2685 }
2686
2687 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2688 if (! NILP (Vface_remapping_alist))
2689 remapped_base_face_id
2690 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2691
2692 /* Use one of the mode line rows of W's desired matrix if
2693 appropriate. */
2694 if (row == NULL)
2695 {
2696 if (base_face_id == MODE_LINE_FACE_ID
2697 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2698 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2699 else if (base_face_id == HEADER_LINE_FACE_ID)
2700 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2701 }
2702
2703 /* Clear IT. */
2704 memset (it, 0, sizeof *it);
2705 it->current.overlay_string_index = -1;
2706 it->current.dpvec_index = -1;
2707 it->base_face_id = remapped_base_face_id;
2708 it->string = Qnil;
2709 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2710 it->paragraph_embedding = L2R;
2711 it->bidi_it.string.lstring = Qnil;
2712 it->bidi_it.string.s = NULL;
2713 it->bidi_it.string.bufpos = 0;
2714 it->bidi_it.w = w;
2715
2716 /* The window in which we iterate over current_buffer: */
2717 XSETWINDOW (it->window, w);
2718 it->w = w;
2719 it->f = XFRAME (w->frame);
2720
2721 it->cmp_it.id = -1;
2722
2723 /* Extra space between lines (on window systems only). */
2724 if (base_face_id == DEFAULT_FACE_ID
2725 && FRAME_WINDOW_P (it->f))
2726 {
2727 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2728 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2729 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2730 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2731 * FRAME_LINE_HEIGHT (it->f));
2732 else if (it->f->extra_line_spacing > 0)
2733 it->extra_line_spacing = it->f->extra_line_spacing;
2734 it->max_extra_line_spacing = 0;
2735 }
2736
2737 /* If realized faces have been removed, e.g. because of face
2738 attribute changes of named faces, recompute them. When running
2739 in batch mode, the face cache of the initial frame is null. If
2740 we happen to get called, make a dummy face cache. */
2741 if (FRAME_FACE_CACHE (it->f) == NULL)
2742 init_frame_faces (it->f);
2743 if (FRAME_FACE_CACHE (it->f)->used == 0)
2744 recompute_basic_faces (it->f);
2745
2746 /* Current value of the `slice', `space-width', and 'height' properties. */
2747 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2748 it->space_width = Qnil;
2749 it->font_height = Qnil;
2750 it->override_ascent = -1;
2751
2752 /* Are control characters displayed as `^C'? */
2753 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2754
2755 /* -1 means everything between a CR and the following line end
2756 is invisible. >0 means lines indented more than this value are
2757 invisible. */
2758 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2759 ? (clip_to_bounds
2760 (-1, XINT (BVAR (current_buffer, selective_display)),
2761 PTRDIFF_MAX))
2762 : (!NILP (BVAR (current_buffer, selective_display))
2763 ? -1 : 0));
2764 it->selective_display_ellipsis_p
2765 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2766
2767 /* Display table to use. */
2768 it->dp = window_display_table (w);
2769
2770 /* Are multibyte characters enabled in current_buffer? */
2771 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2772
2773 /* If visible region is of non-zero length, set IT->region_beg_charpos
2774 and IT->region_end_charpos to the start and end of a visible region
2775 in window IT->w. Set both to -1 to indicate no region. */
2776 markpos = markpos_of_region ();
2777 if (markpos >= 0
2778 /* Maybe highlight only in selected window. */
2779 && (/* Either show region everywhere. */
2780 highlight_nonselected_windows
2781 /* Or show region in the selected window. */
2782 || w == XWINDOW (selected_window)
2783 /* Or show the region if we are in the mini-buffer and W is
2784 the window the mini-buffer refers to. */
2785 || (MINI_WINDOW_P (XWINDOW (selected_window))
2786 && WINDOWP (minibuf_selected_window)
2787 && w == XWINDOW (minibuf_selected_window))))
2788 {
2789 it->region_beg_charpos = min (PT, markpos);
2790 it->region_end_charpos = max (PT, markpos);
2791 }
2792 else
2793 it->region_beg_charpos = it->region_end_charpos = -1;
2794
2795 /* Get the position at which the redisplay_end_trigger hook should
2796 be run, if it is to be run at all. */
2797 if (MARKERP (w->redisplay_end_trigger)
2798 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2799 it->redisplay_end_trigger_charpos
2800 = marker_position (w->redisplay_end_trigger);
2801 else if (INTEGERP (w->redisplay_end_trigger))
2802 it->redisplay_end_trigger_charpos =
2803 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2804
2805 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2806
2807 /* Are lines in the display truncated? */
2808 if (base_face_id != DEFAULT_FACE_ID
2809 || it->w->hscroll
2810 || (! WINDOW_FULL_WIDTH_P (it->w)
2811 && ((!NILP (Vtruncate_partial_width_windows)
2812 && !INTEGERP (Vtruncate_partial_width_windows))
2813 || (INTEGERP (Vtruncate_partial_width_windows)
2814 && (WINDOW_TOTAL_COLS (it->w)
2815 < XINT (Vtruncate_partial_width_windows))))))
2816 it->line_wrap = TRUNCATE;
2817 else if (NILP (BVAR (current_buffer, truncate_lines)))
2818 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2819 ? WINDOW_WRAP : WORD_WRAP;
2820 else
2821 it->line_wrap = TRUNCATE;
2822
2823 /* Get dimensions of truncation and continuation glyphs. These are
2824 displayed as fringe bitmaps under X, but we need them for such
2825 frames when the fringes are turned off. But leave the dimensions
2826 zero for tooltip frames, as these glyphs look ugly there and also
2827 sabotage calculations of tooltip dimensions in x-show-tip. */
2828 #ifdef HAVE_WINDOW_SYSTEM
2829 if (!(FRAME_WINDOW_P (it->f)
2830 && FRAMEP (tip_frame)
2831 && it->f == XFRAME (tip_frame)))
2832 #endif
2833 {
2834 if (it->line_wrap == TRUNCATE)
2835 {
2836 /* We will need the truncation glyph. */
2837 eassert (it->glyph_row == NULL);
2838 produce_special_glyphs (it, IT_TRUNCATION);
2839 it->truncation_pixel_width = it->pixel_width;
2840 }
2841 else
2842 {
2843 /* We will need the continuation glyph. */
2844 eassert (it->glyph_row == NULL);
2845 produce_special_glyphs (it, IT_CONTINUATION);
2846 it->continuation_pixel_width = it->pixel_width;
2847 }
2848 }
2849
2850 /* Reset these values to zero because the produce_special_glyphs
2851 above has changed them. */
2852 it->pixel_width = it->ascent = it->descent = 0;
2853 it->phys_ascent = it->phys_descent = 0;
2854
2855 /* Set this after getting the dimensions of truncation and
2856 continuation glyphs, so that we don't produce glyphs when calling
2857 produce_special_glyphs, above. */
2858 it->glyph_row = row;
2859 it->area = TEXT_AREA;
2860
2861 /* Forget any previous info about this row being reversed. */
2862 if (it->glyph_row)
2863 it->glyph_row->reversed_p = 0;
2864
2865 /* Get the dimensions of the display area. The display area
2866 consists of the visible window area plus a horizontally scrolled
2867 part to the left of the window. All x-values are relative to the
2868 start of this total display area. */
2869 if (base_face_id != DEFAULT_FACE_ID)
2870 {
2871 /* Mode lines, menu bar in terminal frames. */
2872 it->first_visible_x = 0;
2873 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2874 }
2875 else
2876 {
2877 it->first_visible_x =
2878 window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2879 it->last_visible_x = (it->first_visible_x
2880 + window_box_width (w, TEXT_AREA));
2881
2882 /* If we truncate lines, leave room for the truncation glyph(s) at
2883 the right margin. Otherwise, leave room for the continuation
2884 glyph(s). Done only if the window has no fringes. Since we
2885 don't know at this point whether there will be any R2L lines in
2886 the window, we reserve space for truncation/continuation glyphs
2887 even if only one of the fringes is absent. */
2888 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2889 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2890 {
2891 if (it->line_wrap == TRUNCATE)
2892 it->last_visible_x -= it->truncation_pixel_width;
2893 else
2894 it->last_visible_x -= it->continuation_pixel_width;
2895 }
2896
2897 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2898 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2899 }
2900
2901 /* Leave room for a border glyph. */
2902 if (!FRAME_WINDOW_P (it->f)
2903 && !WINDOW_RIGHTMOST_P (it->w))
2904 it->last_visible_x -= 1;
2905
2906 it->last_visible_y = window_text_bottom_y (w);
2907
2908 /* For mode lines and alike, arrange for the first glyph having a
2909 left box line if the face specifies a box. */
2910 if (base_face_id != DEFAULT_FACE_ID)
2911 {
2912 struct face *face;
2913
2914 it->face_id = remapped_base_face_id;
2915
2916 /* If we have a boxed mode line, make the first character appear
2917 with a left box line. */
2918 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2919 if (face->box != FACE_NO_BOX)
2920 it->start_of_box_run_p = 1;
2921 }
2922
2923 /* If a buffer position was specified, set the iterator there,
2924 getting overlays and face properties from that position. */
2925 if (charpos >= BUF_BEG (current_buffer))
2926 {
2927 it->end_charpos = ZV;
2928 eassert (charpos == BYTE_TO_CHAR (bytepos));
2929 IT_CHARPOS (*it) = charpos;
2930 IT_BYTEPOS (*it) = bytepos;
2931
2932 /* We will rely on `reseat' to set this up properly, via
2933 handle_face_prop. */
2934 it->face_id = it->base_face_id;
2935
2936 it->start = it->current;
2937 /* Do we need to reorder bidirectional text? Not if this is a
2938 unibyte buffer: by definition, none of the single-byte
2939 characters are strong R2L, so no reordering is needed. And
2940 bidi.c doesn't support unibyte buffers anyway. Also, don't
2941 reorder while we are loading loadup.el, since the tables of
2942 character properties needed for reordering are not yet
2943 available. */
2944 it->bidi_p =
2945 NILP (Vpurify_flag)
2946 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2947 && it->multibyte_p;
2948
2949 /* If we are to reorder bidirectional text, init the bidi
2950 iterator. */
2951 if (it->bidi_p)
2952 {
2953 /* Note the paragraph direction that this buffer wants to
2954 use. */
2955 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2956 Qleft_to_right))
2957 it->paragraph_embedding = L2R;
2958 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2959 Qright_to_left))
2960 it->paragraph_embedding = R2L;
2961 else
2962 it->paragraph_embedding = NEUTRAL_DIR;
2963 bidi_unshelve_cache (NULL, 0);
2964 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2965 &it->bidi_it);
2966 }
2967
2968 /* Compute faces etc. */
2969 reseat (it, it->current.pos, 1);
2970 }
2971
2972 CHECK_IT (it);
2973 }
2974
2975
2976 /* Initialize IT for the display of window W with window start POS. */
2977
2978 void
2979 start_display (struct it *it, struct window *w, struct text_pos pos)
2980 {
2981 struct glyph_row *row;
2982 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2983
2984 row = w->desired_matrix->rows + first_vpos;
2985 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2986 it->first_vpos = first_vpos;
2987
2988 /* Don't reseat to previous visible line start if current start
2989 position is in a string or image. */
2990 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2991 {
2992 int start_at_line_beg_p;
2993 int first_y = it->current_y;
2994
2995 /* If window start is not at a line start, skip forward to POS to
2996 get the correct continuation lines width. */
2997 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2998 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2999 if (!start_at_line_beg_p)
3000 {
3001 int new_x;
3002
3003 reseat_at_previous_visible_line_start (it);
3004 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3005
3006 new_x = it->current_x + it->pixel_width;
3007
3008 /* If lines are continued, this line may end in the middle
3009 of a multi-glyph character (e.g. a control character
3010 displayed as \003, or in the middle of an overlay
3011 string). In this case move_it_to above will not have
3012 taken us to the start of the continuation line but to the
3013 end of the continued line. */
3014 if (it->current_x > 0
3015 && it->line_wrap != TRUNCATE /* Lines are continued. */
3016 && (/* And glyph doesn't fit on the line. */
3017 new_x > it->last_visible_x
3018 /* Or it fits exactly and we're on a window
3019 system frame. */
3020 || (new_x == it->last_visible_x
3021 && FRAME_WINDOW_P (it->f)
3022 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3023 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3024 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3025 {
3026 if ((it->current.dpvec_index >= 0
3027 || it->current.overlay_string_index >= 0)
3028 /* If we are on a newline from a display vector or
3029 overlay string, then we are already at the end of
3030 a screen line; no need to go to the next line in
3031 that case, as this line is not really continued.
3032 (If we do go to the next line, C-e will not DTRT.) */
3033 && it->c != '\n')
3034 {
3035 set_iterator_to_next (it, 1);
3036 move_it_in_display_line_to (it, -1, -1, 0);
3037 }
3038
3039 it->continuation_lines_width += it->current_x;
3040 }
3041 /* If the character at POS is displayed via a display
3042 vector, move_it_to above stops at the final glyph of
3043 IT->dpvec. To make the caller redisplay that character
3044 again (a.k.a. start at POS), we need to reset the
3045 dpvec_index to the beginning of IT->dpvec. */
3046 else if (it->current.dpvec_index >= 0)
3047 it->current.dpvec_index = 0;
3048
3049 /* We're starting a new display line, not affected by the
3050 height of the continued line, so clear the appropriate
3051 fields in the iterator structure. */
3052 it->max_ascent = it->max_descent = 0;
3053 it->max_phys_ascent = it->max_phys_descent = 0;
3054
3055 it->current_y = first_y;
3056 it->vpos = 0;
3057 it->current_x = it->hpos = 0;
3058 }
3059 }
3060 }
3061
3062
3063 /* Return 1 if POS is a position in ellipses displayed for invisible
3064 text. W is the window we display, for text property lookup. */
3065
3066 static int
3067 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3068 {
3069 Lisp_Object prop, window;
3070 int ellipses_p = 0;
3071 ptrdiff_t charpos = CHARPOS (pos->pos);
3072
3073 /* If POS specifies a position in a display vector, this might
3074 be for an ellipsis displayed for invisible text. We won't
3075 get the iterator set up for delivering that ellipsis unless
3076 we make sure that it gets aware of the invisible text. */
3077 if (pos->dpvec_index >= 0
3078 && pos->overlay_string_index < 0
3079 && CHARPOS (pos->string_pos) < 0
3080 && charpos > BEGV
3081 && (XSETWINDOW (window, w),
3082 prop = Fget_char_property (make_number (charpos),
3083 Qinvisible, window),
3084 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3085 {
3086 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3087 window);
3088 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3089 }
3090
3091 return ellipses_p;
3092 }
3093
3094
3095 /* Initialize IT for stepping through current_buffer in window W,
3096 starting at position POS that includes overlay string and display
3097 vector/ control character translation position information. Value
3098 is zero if there are overlay strings with newlines at POS. */
3099
3100 static int
3101 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3102 {
3103 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3104 int i, overlay_strings_with_newlines = 0;
3105
3106 /* If POS specifies a position in a display vector, this might
3107 be for an ellipsis displayed for invisible text. We won't
3108 get the iterator set up for delivering that ellipsis unless
3109 we make sure that it gets aware of the invisible text. */
3110 if (in_ellipses_for_invisible_text_p (pos, w))
3111 {
3112 --charpos;
3113 bytepos = 0;
3114 }
3115
3116 /* Keep in mind: the call to reseat in init_iterator skips invisible
3117 text, so we might end up at a position different from POS. This
3118 is only a problem when POS is a row start after a newline and an
3119 overlay starts there with an after-string, and the overlay has an
3120 invisible property. Since we don't skip invisible text in
3121 display_line and elsewhere immediately after consuming the
3122 newline before the row start, such a POS will not be in a string,
3123 but the call to init_iterator below will move us to the
3124 after-string. */
3125 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3126
3127 /* This only scans the current chunk -- it should scan all chunks.
3128 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3129 to 16 in 22.1 to make this a lesser problem. */
3130 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3131 {
3132 const char *s = SSDATA (it->overlay_strings[i]);
3133 const char *e = s + SBYTES (it->overlay_strings[i]);
3134
3135 while (s < e && *s != '\n')
3136 ++s;
3137
3138 if (s < e)
3139 {
3140 overlay_strings_with_newlines = 1;
3141 break;
3142 }
3143 }
3144
3145 /* If position is within an overlay string, set up IT to the right
3146 overlay string. */
3147 if (pos->overlay_string_index >= 0)
3148 {
3149 int relative_index;
3150
3151 /* If the first overlay string happens to have a `display'
3152 property for an image, the iterator will be set up for that
3153 image, and we have to undo that setup first before we can
3154 correct the overlay string index. */
3155 if (it->method == GET_FROM_IMAGE)
3156 pop_it (it);
3157
3158 /* We already have the first chunk of overlay strings in
3159 IT->overlay_strings. Load more until the one for
3160 pos->overlay_string_index is in IT->overlay_strings. */
3161 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3162 {
3163 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3164 it->current.overlay_string_index = 0;
3165 while (n--)
3166 {
3167 load_overlay_strings (it, 0);
3168 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3169 }
3170 }
3171
3172 it->current.overlay_string_index = pos->overlay_string_index;
3173 relative_index = (it->current.overlay_string_index
3174 % OVERLAY_STRING_CHUNK_SIZE);
3175 it->string = it->overlay_strings[relative_index];
3176 eassert (STRINGP (it->string));
3177 it->current.string_pos = pos->string_pos;
3178 it->method = GET_FROM_STRING;
3179 it->end_charpos = SCHARS (it->string);
3180 /* Set up the bidi iterator for this overlay string. */
3181 if (it->bidi_p)
3182 {
3183 it->bidi_it.string.lstring = it->string;
3184 it->bidi_it.string.s = NULL;
3185 it->bidi_it.string.schars = SCHARS (it->string);
3186 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3187 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3188 it->bidi_it.string.unibyte = !it->multibyte_p;
3189 it->bidi_it.w = it->w;
3190 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3191 FRAME_WINDOW_P (it->f), &it->bidi_it);
3192
3193 /* Synchronize the state of the bidi iterator with
3194 pos->string_pos. For any string position other than
3195 zero, this will be done automagically when we resume
3196 iteration over the string and get_visually_first_element
3197 is called. But if string_pos is zero, and the string is
3198 to be reordered for display, we need to resync manually,
3199 since it could be that the iteration state recorded in
3200 pos ended at string_pos of 0 moving backwards in string. */
3201 if (CHARPOS (pos->string_pos) == 0)
3202 {
3203 get_visually_first_element (it);
3204 if (IT_STRING_CHARPOS (*it) != 0)
3205 do {
3206 /* Paranoia. */
3207 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3208 bidi_move_to_visually_next (&it->bidi_it);
3209 } while (it->bidi_it.charpos != 0);
3210 }
3211 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3212 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3213 }
3214 }
3215
3216 if (CHARPOS (pos->string_pos) >= 0)
3217 {
3218 /* Recorded position is not in an overlay string, but in another
3219 string. This can only be a string from a `display' property.
3220 IT should already be filled with that string. */
3221 it->current.string_pos = pos->string_pos;
3222 eassert (STRINGP (it->string));
3223 if (it->bidi_p)
3224 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3225 FRAME_WINDOW_P (it->f), &it->bidi_it);
3226 }
3227
3228 /* Restore position in display vector translations, control
3229 character translations or ellipses. */
3230 if (pos->dpvec_index >= 0)
3231 {
3232 if (it->dpvec == NULL)
3233 get_next_display_element (it);
3234 eassert (it->dpvec && it->current.dpvec_index == 0);
3235 it->current.dpvec_index = pos->dpvec_index;
3236 }
3237
3238 CHECK_IT (it);
3239 return !overlay_strings_with_newlines;
3240 }
3241
3242
3243 /* Initialize IT for stepping through current_buffer in window W
3244 starting at ROW->start. */
3245
3246 static void
3247 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3248 {
3249 init_from_display_pos (it, w, &row->start);
3250 it->start = row->start;
3251 it->continuation_lines_width = row->continuation_lines_width;
3252 CHECK_IT (it);
3253 }
3254
3255
3256 /* Initialize IT for stepping through current_buffer in window W
3257 starting in the line following ROW, i.e. starting at ROW->end.
3258 Value is zero if there are overlay strings with newlines at ROW's
3259 end position. */
3260
3261 static int
3262 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3263 {
3264 int success = 0;
3265
3266 if (init_from_display_pos (it, w, &row->end))
3267 {
3268 if (row->continued_p)
3269 it->continuation_lines_width
3270 = row->continuation_lines_width + row->pixel_width;
3271 CHECK_IT (it);
3272 success = 1;
3273 }
3274
3275 return success;
3276 }
3277
3278
3279
3280 \f
3281 /***********************************************************************
3282 Text properties
3283 ***********************************************************************/
3284
3285 /* Called when IT reaches IT->stop_charpos. Handle text property and
3286 overlay changes. Set IT->stop_charpos to the next position where
3287 to stop. */
3288
3289 static void
3290 handle_stop (struct it *it)
3291 {
3292 enum prop_handled handled;
3293 int handle_overlay_change_p;
3294 struct props *p;
3295
3296 it->dpvec = NULL;
3297 it->current.dpvec_index = -1;
3298 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3299 it->ignore_overlay_strings_at_pos_p = 0;
3300 it->ellipsis_p = 0;
3301
3302 /* Use face of preceding text for ellipsis (if invisible) */
3303 if (it->selective_display_ellipsis_p)
3304 it->saved_face_id = it->face_id;
3305
3306 do
3307 {
3308 handled = HANDLED_NORMALLY;
3309
3310 /* Call text property handlers. */
3311 for (p = it_props; p->handler; ++p)
3312 {
3313 handled = p->handler (it);
3314
3315 if (handled == HANDLED_RECOMPUTE_PROPS)
3316 break;
3317 else if (handled == HANDLED_RETURN)
3318 {
3319 /* We still want to show before and after strings from
3320 overlays even if the actual buffer text is replaced. */
3321 if (!handle_overlay_change_p
3322 || it->sp > 1
3323 /* Don't call get_overlay_strings_1 if we already
3324 have overlay strings loaded, because doing so
3325 will load them again and push the iterator state
3326 onto the stack one more time, which is not
3327 expected by the rest of the code that processes
3328 overlay strings. */
3329 || (it->current.overlay_string_index < 0
3330 ? !get_overlay_strings_1 (it, 0, 0)
3331 : 0))
3332 {
3333 if (it->ellipsis_p)
3334 setup_for_ellipsis (it, 0);
3335 /* When handling a display spec, we might load an
3336 empty string. In that case, discard it here. We
3337 used to discard it in handle_single_display_spec,
3338 but that causes get_overlay_strings_1, above, to
3339 ignore overlay strings that we must check. */
3340 if (STRINGP (it->string) && !SCHARS (it->string))
3341 pop_it (it);
3342 return;
3343 }
3344 else if (STRINGP (it->string) && !SCHARS (it->string))
3345 pop_it (it);
3346 else
3347 {
3348 it->ignore_overlay_strings_at_pos_p = 1;
3349 it->string_from_display_prop_p = 0;
3350 it->from_disp_prop_p = 0;
3351 handle_overlay_change_p = 0;
3352 }
3353 handled = HANDLED_RECOMPUTE_PROPS;
3354 break;
3355 }
3356 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3357 handle_overlay_change_p = 0;
3358 }
3359
3360 if (handled != HANDLED_RECOMPUTE_PROPS)
3361 {
3362 /* Don't check for overlay strings below when set to deliver
3363 characters from a display vector. */
3364 if (it->method == GET_FROM_DISPLAY_VECTOR)
3365 handle_overlay_change_p = 0;
3366
3367 /* Handle overlay changes.
3368 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3369 if it finds overlays. */
3370 if (handle_overlay_change_p)
3371 handled = handle_overlay_change (it);
3372 }
3373
3374 if (it->ellipsis_p)
3375 {
3376 setup_for_ellipsis (it, 0);
3377 break;
3378 }
3379 }
3380 while (handled == HANDLED_RECOMPUTE_PROPS);
3381
3382 /* Determine where to stop next. */
3383 if (handled == HANDLED_NORMALLY)
3384 compute_stop_pos (it);
3385 }
3386
3387
3388 /* Compute IT->stop_charpos from text property and overlay change
3389 information for IT's current position. */
3390
3391 static void
3392 compute_stop_pos (struct it *it)
3393 {
3394 register INTERVAL iv, next_iv;
3395 Lisp_Object object, limit, position;
3396 ptrdiff_t charpos, bytepos;
3397
3398 if (STRINGP (it->string))
3399 {
3400 /* Strings are usually short, so don't limit the search for
3401 properties. */
3402 it->stop_charpos = it->end_charpos;
3403 object = it->string;
3404 limit = Qnil;
3405 charpos = IT_STRING_CHARPOS (*it);
3406 bytepos = IT_STRING_BYTEPOS (*it);
3407 }
3408 else
3409 {
3410 ptrdiff_t pos;
3411
3412 /* If end_charpos is out of range for some reason, such as a
3413 misbehaving display function, rationalize it (Bug#5984). */
3414 if (it->end_charpos > ZV)
3415 it->end_charpos = ZV;
3416 it->stop_charpos = it->end_charpos;
3417
3418 /* If next overlay change is in front of the current stop pos
3419 (which is IT->end_charpos), stop there. Note: value of
3420 next_overlay_change is point-max if no overlay change
3421 follows. */
3422 charpos = IT_CHARPOS (*it);
3423 bytepos = IT_BYTEPOS (*it);
3424 pos = next_overlay_change (charpos);
3425 if (pos < it->stop_charpos)
3426 it->stop_charpos = pos;
3427
3428 /* If showing the region, we have to stop at the region
3429 start or end because the face might change there. */
3430 if (it->region_beg_charpos > 0)
3431 {
3432 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3433 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3434 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3435 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3436 }
3437
3438 /* Set up variables for computing the stop position from text
3439 property changes. */
3440 XSETBUFFER (object, current_buffer);
3441 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3442 }
3443
3444 /* Get the interval containing IT's position. Value is a null
3445 interval if there isn't such an interval. */
3446 position = make_number (charpos);
3447 iv = validate_interval_range (object, &position, &position, 0);
3448 if (iv)
3449 {
3450 Lisp_Object values_here[LAST_PROP_IDX];
3451 struct props *p;
3452
3453 /* Get properties here. */
3454 for (p = it_props; p->handler; ++p)
3455 values_here[p->idx] = textget (iv->plist, *p->name);
3456
3457 /* Look for an interval following iv that has different
3458 properties. */
3459 for (next_iv = next_interval (iv);
3460 (next_iv
3461 && (NILP (limit)
3462 || XFASTINT (limit) > next_iv->position));
3463 next_iv = next_interval (next_iv))
3464 {
3465 for (p = it_props; p->handler; ++p)
3466 {
3467 Lisp_Object new_value;
3468
3469 new_value = textget (next_iv->plist, *p->name);
3470 if (!EQ (values_here[p->idx], new_value))
3471 break;
3472 }
3473
3474 if (p->handler)
3475 break;
3476 }
3477
3478 if (next_iv)
3479 {
3480 if (INTEGERP (limit)
3481 && next_iv->position >= XFASTINT (limit))
3482 /* No text property change up to limit. */
3483 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3484 else
3485 /* Text properties change in next_iv. */
3486 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3487 }
3488 }
3489
3490 if (it->cmp_it.id < 0)
3491 {
3492 ptrdiff_t stoppos = it->end_charpos;
3493
3494 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3495 stoppos = -1;
3496 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3497 stoppos, it->string);
3498 }
3499
3500 eassert (STRINGP (it->string)
3501 || (it->stop_charpos >= BEGV
3502 && it->stop_charpos >= IT_CHARPOS (*it)));
3503 }
3504
3505
3506 /* Return the position of the next overlay change after POS in
3507 current_buffer. Value is point-max if no overlay change
3508 follows. This is like `next-overlay-change' but doesn't use
3509 xmalloc. */
3510
3511 static ptrdiff_t
3512 next_overlay_change (ptrdiff_t pos)
3513 {
3514 ptrdiff_t i, noverlays;
3515 ptrdiff_t endpos;
3516 Lisp_Object *overlays;
3517
3518 /* Get all overlays at the given position. */
3519 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3520
3521 /* If any of these overlays ends before endpos,
3522 use its ending point instead. */
3523 for (i = 0; i < noverlays; ++i)
3524 {
3525 Lisp_Object oend;
3526 ptrdiff_t oendpos;
3527
3528 oend = OVERLAY_END (overlays[i]);
3529 oendpos = OVERLAY_POSITION (oend);
3530 endpos = min (endpos, oendpos);
3531 }
3532
3533 return endpos;
3534 }
3535
3536 /* How many characters forward to search for a display property or
3537 display string. Searching too far forward makes the bidi display
3538 sluggish, especially in small windows. */
3539 #define MAX_DISP_SCAN 250
3540
3541 /* Return the character position of a display string at or after
3542 position specified by POSITION. If no display string exists at or
3543 after POSITION, return ZV. A display string is either an overlay
3544 with `display' property whose value is a string, or a `display'
3545 text property whose value is a string. STRING is data about the
3546 string to iterate; if STRING->lstring is nil, we are iterating a
3547 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3548 on a GUI frame. DISP_PROP is set to zero if we searched
3549 MAX_DISP_SCAN characters forward without finding any display
3550 strings, non-zero otherwise. It is set to 2 if the display string
3551 uses any kind of `(space ...)' spec that will produce a stretch of
3552 white space in the text area. */
3553 ptrdiff_t
3554 compute_display_string_pos (struct text_pos *position,
3555 struct bidi_string_data *string,
3556 struct window *w,
3557 int frame_window_p, int *disp_prop)
3558 {
3559 /* OBJECT = nil means current buffer. */
3560 Lisp_Object object, object1;
3561 Lisp_Object pos, spec, limpos;
3562 int string_p = (string && (STRINGP (string->lstring) || string->s));
3563 ptrdiff_t eob = string_p ? string->schars : ZV;
3564 ptrdiff_t begb = string_p ? 0 : BEGV;
3565 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3566 ptrdiff_t lim =
3567 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3568 struct text_pos tpos;
3569 int rv = 0;
3570
3571 if (string && STRINGP (string->lstring))
3572 object1 = object = string->lstring;
3573 else if (w && !string_p)
3574 {
3575 XSETWINDOW (object, w);
3576 object1 = Qnil;
3577 }
3578 else
3579 object1 = object = Qnil;
3580
3581 *disp_prop = 1;
3582
3583 if (charpos >= eob
3584 /* We don't support display properties whose values are strings
3585 that have display string properties. */
3586 || string->from_disp_str
3587 /* C strings cannot have display properties. */
3588 || (string->s && !STRINGP (object)))
3589 {
3590 *disp_prop = 0;
3591 return eob;
3592 }
3593
3594 /* If the character at CHARPOS is where the display string begins,
3595 return CHARPOS. */
3596 pos = make_number (charpos);
3597 if (STRINGP (object))
3598 bufpos = string->bufpos;
3599 else
3600 bufpos = charpos;
3601 tpos = *position;
3602 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3603 && (charpos <= begb
3604 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3605 object),
3606 spec))
3607 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3608 frame_window_p)))
3609 {
3610 if (rv == 2)
3611 *disp_prop = 2;
3612 return charpos;
3613 }
3614
3615 /* Look forward for the first character with a `display' property
3616 that will replace the underlying text when displayed. */
3617 limpos = make_number (lim);
3618 do {
3619 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3620 CHARPOS (tpos) = XFASTINT (pos);
3621 if (CHARPOS (tpos) >= lim)
3622 {
3623 *disp_prop = 0;
3624 break;
3625 }
3626 if (STRINGP (object))
3627 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3628 else
3629 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3630 spec = Fget_char_property (pos, Qdisplay, object);
3631 if (!STRINGP (object))
3632 bufpos = CHARPOS (tpos);
3633 } while (NILP (spec)
3634 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3635 bufpos, frame_window_p)));
3636 if (rv == 2)
3637 *disp_prop = 2;
3638
3639 return CHARPOS (tpos);
3640 }
3641
3642 /* Return the character position of the end of the display string that
3643 started at CHARPOS. If there's no display string at CHARPOS,
3644 return -1. A display string is either an overlay with `display'
3645 property whose value is a string or a `display' text property whose
3646 value is a string. */
3647 ptrdiff_t
3648 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3649 {
3650 /* OBJECT = nil means current buffer. */
3651 Lisp_Object object =
3652 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3653 Lisp_Object pos = make_number (charpos);
3654 ptrdiff_t eob =
3655 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3656
3657 if (charpos >= eob || (string->s && !STRINGP (object)))
3658 return eob;
3659
3660 /* It could happen that the display property or overlay was removed
3661 since we found it in compute_display_string_pos above. One way
3662 this can happen is if JIT font-lock was called (through
3663 handle_fontified_prop), and jit-lock-functions remove text
3664 properties or overlays from the portion of buffer that includes
3665 CHARPOS. Muse mode is known to do that, for example. In this
3666 case, we return -1 to the caller, to signal that no display
3667 string is actually present at CHARPOS. See bidi_fetch_char for
3668 how this is handled.
3669
3670 An alternative would be to never look for display properties past
3671 it->stop_charpos. But neither compute_display_string_pos nor
3672 bidi_fetch_char that calls it know or care where the next
3673 stop_charpos is. */
3674 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3675 return -1;
3676
3677 /* Look forward for the first character where the `display' property
3678 changes. */
3679 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3680
3681 return XFASTINT (pos);
3682 }
3683
3684
3685 \f
3686 /***********************************************************************
3687 Fontification
3688 ***********************************************************************/
3689
3690 /* Handle changes in the `fontified' property of the current buffer by
3691 calling hook functions from Qfontification_functions to fontify
3692 regions of text. */
3693
3694 static enum prop_handled
3695 handle_fontified_prop (struct it *it)
3696 {
3697 Lisp_Object prop, pos;
3698 enum prop_handled handled = HANDLED_NORMALLY;
3699
3700 if (!NILP (Vmemory_full))
3701 return handled;
3702
3703 /* Get the value of the `fontified' property at IT's current buffer
3704 position. (The `fontified' property doesn't have a special
3705 meaning in strings.) If the value is nil, call functions from
3706 Qfontification_functions. */
3707 if (!STRINGP (it->string)
3708 && it->s == NULL
3709 && !NILP (Vfontification_functions)
3710 && !NILP (Vrun_hooks)
3711 && (pos = make_number (IT_CHARPOS (*it)),
3712 prop = Fget_char_property (pos, Qfontified, Qnil),
3713 /* Ignore the special cased nil value always present at EOB since
3714 no amount of fontifying will be able to change it. */
3715 NILP (prop) && IT_CHARPOS (*it) < Z))
3716 {
3717 ptrdiff_t count = SPECPDL_INDEX ();
3718 Lisp_Object val;
3719 struct buffer *obuf = current_buffer;
3720 int begv = BEGV, zv = ZV;
3721 int old_clip_changed = current_buffer->clip_changed;
3722
3723 val = Vfontification_functions;
3724 specbind (Qfontification_functions, Qnil);
3725
3726 eassert (it->end_charpos == ZV);
3727
3728 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3729 safe_call1 (val, pos);
3730 else
3731 {
3732 Lisp_Object fns, fn;
3733 struct gcpro gcpro1, gcpro2;
3734
3735 fns = Qnil;
3736 GCPRO2 (val, fns);
3737
3738 for (; CONSP (val); val = XCDR (val))
3739 {
3740 fn = XCAR (val);
3741
3742 if (EQ (fn, Qt))
3743 {
3744 /* A value of t indicates this hook has a local
3745 binding; it means to run the global binding too.
3746 In a global value, t should not occur. If it
3747 does, we must ignore it to avoid an endless
3748 loop. */
3749 for (fns = Fdefault_value (Qfontification_functions);
3750 CONSP (fns);
3751 fns = XCDR (fns))
3752 {
3753 fn = XCAR (fns);
3754 if (!EQ (fn, Qt))
3755 safe_call1 (fn, pos);
3756 }
3757 }
3758 else
3759 safe_call1 (fn, pos);
3760 }
3761
3762 UNGCPRO;
3763 }
3764
3765 unbind_to (count, Qnil);
3766
3767 /* Fontification functions routinely call `save-restriction'.
3768 Normally, this tags clip_changed, which can confuse redisplay
3769 (see discussion in Bug#6671). Since we don't perform any
3770 special handling of fontification changes in the case where
3771 `save-restriction' isn't called, there's no point doing so in
3772 this case either. So, if the buffer's restrictions are
3773 actually left unchanged, reset clip_changed. */
3774 if (obuf == current_buffer)
3775 {
3776 if (begv == BEGV && zv == ZV)
3777 current_buffer->clip_changed = old_clip_changed;
3778 }
3779 /* There isn't much we can reasonably do to protect against
3780 misbehaving fontification, but here's a fig leaf. */
3781 else if (BUFFER_LIVE_P (obuf))
3782 set_buffer_internal_1 (obuf);
3783
3784 /* The fontification code may have added/removed text.
3785 It could do even a lot worse, but let's at least protect against
3786 the most obvious case where only the text past `pos' gets changed',
3787 as is/was done in grep.el where some escapes sequences are turned
3788 into face properties (bug#7876). */
3789 it->end_charpos = ZV;
3790
3791 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3792 something. This avoids an endless loop if they failed to
3793 fontify the text for which reason ever. */
3794 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3795 handled = HANDLED_RECOMPUTE_PROPS;
3796 }
3797
3798 return handled;
3799 }
3800
3801
3802 \f
3803 /***********************************************************************
3804 Faces
3805 ***********************************************************************/
3806
3807 /* Set up iterator IT from face properties at its current position.
3808 Called from handle_stop. */
3809
3810 static enum prop_handled
3811 handle_face_prop (struct it *it)
3812 {
3813 int new_face_id;
3814 ptrdiff_t next_stop;
3815
3816 if (!STRINGP (it->string))
3817 {
3818 new_face_id
3819 = face_at_buffer_position (it->w,
3820 IT_CHARPOS (*it),
3821 it->region_beg_charpos,
3822 it->region_end_charpos,
3823 &next_stop,
3824 (IT_CHARPOS (*it)
3825 + TEXT_PROP_DISTANCE_LIMIT),
3826 0, it->base_face_id);
3827
3828 /* Is this a start of a run of characters with box face?
3829 Caveat: this can be called for a freshly initialized
3830 iterator; face_id is -1 in this case. We know that the new
3831 face will not change until limit, i.e. if the new face has a
3832 box, all characters up to limit will have one. But, as
3833 usual, we don't know whether limit is really the end. */
3834 if (new_face_id != it->face_id)
3835 {
3836 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3837 /* If it->face_id is -1, old_face below will be NULL, see
3838 the definition of FACE_FROM_ID. This will happen if this
3839 is the initial call that gets the face. */
3840 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3841
3842 /* If the value of face_id of the iterator is -1, we have to
3843 look in front of IT's position and see whether there is a
3844 face there that's different from new_face_id. */
3845 if (!old_face && IT_CHARPOS (*it) > BEG)
3846 {
3847 int prev_face_id = face_before_it_pos (it);
3848
3849 old_face = FACE_FROM_ID (it->f, prev_face_id);
3850 }
3851
3852 /* If the new face has a box, but the old face does not,
3853 this is the start of a run of characters with box face,
3854 i.e. this character has a shadow on the left side. */
3855 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3856 && (old_face == NULL || !old_face->box));
3857 it->face_box_p = new_face->box != FACE_NO_BOX;
3858 }
3859 }
3860 else
3861 {
3862 int base_face_id;
3863 ptrdiff_t bufpos;
3864 int i;
3865 Lisp_Object from_overlay
3866 = (it->current.overlay_string_index >= 0
3867 ? it->string_overlays[it->current.overlay_string_index
3868 % OVERLAY_STRING_CHUNK_SIZE]
3869 : Qnil);
3870
3871 /* See if we got to this string directly or indirectly from
3872 an overlay property. That includes the before-string or
3873 after-string of an overlay, strings in display properties
3874 provided by an overlay, their text properties, etc.
3875
3876 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3877 if (! NILP (from_overlay))
3878 for (i = it->sp - 1; i >= 0; i--)
3879 {
3880 if (it->stack[i].current.overlay_string_index >= 0)
3881 from_overlay
3882 = it->string_overlays[it->stack[i].current.overlay_string_index
3883 % OVERLAY_STRING_CHUNK_SIZE];
3884 else if (! NILP (it->stack[i].from_overlay))
3885 from_overlay = it->stack[i].from_overlay;
3886
3887 if (!NILP (from_overlay))
3888 break;
3889 }
3890
3891 if (! NILP (from_overlay))
3892 {
3893 bufpos = IT_CHARPOS (*it);
3894 /* For a string from an overlay, the base face depends
3895 only on text properties and ignores overlays. */
3896 base_face_id
3897 = face_for_overlay_string (it->w,
3898 IT_CHARPOS (*it),
3899 it->region_beg_charpos,
3900 it->region_end_charpos,
3901 &next_stop,
3902 (IT_CHARPOS (*it)
3903 + TEXT_PROP_DISTANCE_LIMIT),
3904 0,
3905 from_overlay);
3906 }
3907 else
3908 {
3909 bufpos = 0;
3910
3911 /* For strings from a `display' property, use the face at
3912 IT's current buffer position as the base face to merge
3913 with, so that overlay strings appear in the same face as
3914 surrounding text, unless they specify their own faces.
3915 For strings from wrap-prefix and line-prefix properties,
3916 use the default face, possibly remapped via
3917 Vface_remapping_alist. */
3918 base_face_id = it->string_from_prefix_prop_p
3919 ? (!NILP (Vface_remapping_alist)
3920 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3921 : DEFAULT_FACE_ID)
3922 : underlying_face_id (it);
3923 }
3924
3925 new_face_id = face_at_string_position (it->w,
3926 it->string,
3927 IT_STRING_CHARPOS (*it),
3928 bufpos,
3929 it->region_beg_charpos,
3930 it->region_end_charpos,
3931 &next_stop,
3932 base_face_id, 0);
3933
3934 /* Is this a start of a run of characters with box? Caveat:
3935 this can be called for a freshly allocated iterator; face_id
3936 is -1 is this case. We know that the new face will not
3937 change until the next check pos, i.e. if the new face has a
3938 box, all characters up to that position will have a
3939 box. But, as usual, we don't know whether that position
3940 is really the end. */
3941 if (new_face_id != it->face_id)
3942 {
3943 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3944 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3945
3946 /* If new face has a box but old face hasn't, this is the
3947 start of a run of characters with box, i.e. it has a
3948 shadow on the left side. */
3949 it->start_of_box_run_p
3950 = new_face->box && (old_face == NULL || !old_face->box);
3951 it->face_box_p = new_face->box != FACE_NO_BOX;
3952 }
3953 }
3954
3955 it->face_id = new_face_id;
3956 return HANDLED_NORMALLY;
3957 }
3958
3959
3960 /* Return the ID of the face ``underlying'' IT's current position,
3961 which is in a string. If the iterator is associated with a
3962 buffer, return the face at IT's current buffer position.
3963 Otherwise, use the iterator's base_face_id. */
3964
3965 static int
3966 underlying_face_id (struct it *it)
3967 {
3968 int face_id = it->base_face_id, i;
3969
3970 eassert (STRINGP (it->string));
3971
3972 for (i = it->sp - 1; i >= 0; --i)
3973 if (NILP (it->stack[i].string))
3974 face_id = it->stack[i].face_id;
3975
3976 return face_id;
3977 }
3978
3979
3980 /* Compute the face one character before or after the current position
3981 of IT, in the visual order. BEFORE_P non-zero means get the face
3982 in front (to the left in L2R paragraphs, to the right in R2L
3983 paragraphs) of IT's screen position. Value is the ID of the face. */
3984
3985 static int
3986 face_before_or_after_it_pos (struct it *it, int before_p)
3987 {
3988 int face_id, limit;
3989 ptrdiff_t next_check_charpos;
3990 struct it it_copy;
3991 void *it_copy_data = NULL;
3992
3993 eassert (it->s == NULL);
3994
3995 if (STRINGP (it->string))
3996 {
3997 ptrdiff_t bufpos, charpos;
3998 int base_face_id;
3999
4000 /* No face change past the end of the string (for the case
4001 we are padding with spaces). No face change before the
4002 string start. */
4003 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
4004 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
4005 return it->face_id;
4006
4007 if (!it->bidi_p)
4008 {
4009 /* Set charpos to the position before or after IT's current
4010 position, in the logical order, which in the non-bidi
4011 case is the same as the visual order. */
4012 if (before_p)
4013 charpos = IT_STRING_CHARPOS (*it) - 1;
4014 else if (it->what == IT_COMPOSITION)
4015 /* For composition, we must check the character after the
4016 composition. */
4017 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4018 else
4019 charpos = IT_STRING_CHARPOS (*it) + 1;
4020 }
4021 else
4022 {
4023 if (before_p)
4024 {
4025 /* With bidi iteration, the character before the current
4026 in the visual order cannot be found by simple
4027 iteration, because "reverse" reordering is not
4028 supported. Instead, we need to use the move_it_*
4029 family of functions. */
4030 /* Ignore face changes before the first visible
4031 character on this display line. */
4032 if (it->current_x <= it->first_visible_x)
4033 return it->face_id;
4034 SAVE_IT (it_copy, *it, it_copy_data);
4035 /* Implementation note: Since move_it_in_display_line
4036 works in the iterator geometry, and thinks the first
4037 character is always the leftmost, even in R2L lines,
4038 we don't need to distinguish between the R2L and L2R
4039 cases here. */
4040 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
4041 it_copy.current_x - 1, MOVE_TO_X);
4042 charpos = IT_STRING_CHARPOS (it_copy);
4043 RESTORE_IT (it, it, it_copy_data);
4044 }
4045 else
4046 {
4047 /* Set charpos to the string position of the character
4048 that comes after IT's current position in the visual
4049 order. */
4050 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4051
4052 it_copy = *it;
4053 while (n--)
4054 bidi_move_to_visually_next (&it_copy.bidi_it);
4055
4056 charpos = it_copy.bidi_it.charpos;
4057 }
4058 }
4059 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4060
4061 if (it->current.overlay_string_index >= 0)
4062 bufpos = IT_CHARPOS (*it);
4063 else
4064 bufpos = 0;
4065
4066 base_face_id = underlying_face_id (it);
4067
4068 /* Get the face for ASCII, or unibyte. */
4069 face_id = face_at_string_position (it->w,
4070 it->string,
4071 charpos,
4072 bufpos,
4073 it->region_beg_charpos,
4074 it->region_end_charpos,
4075 &next_check_charpos,
4076 base_face_id, 0);
4077
4078 /* Correct the face for charsets different from ASCII. Do it
4079 for the multibyte case only. The face returned above is
4080 suitable for unibyte text if IT->string is unibyte. */
4081 if (STRING_MULTIBYTE (it->string))
4082 {
4083 struct text_pos pos1 = string_pos (charpos, it->string);
4084 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4085 int c, len;
4086 struct face *face = FACE_FROM_ID (it->f, face_id);
4087
4088 c = string_char_and_length (p, &len);
4089 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4090 }
4091 }
4092 else
4093 {
4094 struct text_pos pos;
4095
4096 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4097 || (IT_CHARPOS (*it) <= BEGV && before_p))
4098 return it->face_id;
4099
4100 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4101 pos = it->current.pos;
4102
4103 if (!it->bidi_p)
4104 {
4105 if (before_p)
4106 DEC_TEXT_POS (pos, it->multibyte_p);
4107 else
4108 {
4109 if (it->what == IT_COMPOSITION)
4110 {
4111 /* For composition, we must check the position after
4112 the composition. */
4113 pos.charpos += it->cmp_it.nchars;
4114 pos.bytepos += it->len;
4115 }
4116 else
4117 INC_TEXT_POS (pos, it->multibyte_p);
4118 }
4119 }
4120 else
4121 {
4122 if (before_p)
4123 {
4124 /* With bidi iteration, the character before the current
4125 in the visual order cannot be found by simple
4126 iteration, because "reverse" reordering is not
4127 supported. Instead, we need to use the move_it_*
4128 family of functions. */
4129 /* Ignore face changes before the first visible
4130 character on this display line. */
4131 if (it->current_x <= it->first_visible_x)
4132 return it->face_id;
4133 SAVE_IT (it_copy, *it, it_copy_data);
4134 /* Implementation note: Since move_it_in_display_line
4135 works in the iterator geometry, and thinks the first
4136 character is always the leftmost, even in R2L lines,
4137 we don't need to distinguish between the R2L and L2R
4138 cases here. */
4139 move_it_in_display_line (&it_copy, ZV,
4140 it_copy.current_x - 1, MOVE_TO_X);
4141 pos = it_copy.current.pos;
4142 RESTORE_IT (it, it, it_copy_data);
4143 }
4144 else
4145 {
4146 /* Set charpos to the buffer position of the character
4147 that comes after IT's current position in the visual
4148 order. */
4149 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4150
4151 it_copy = *it;
4152 while (n--)
4153 bidi_move_to_visually_next (&it_copy.bidi_it);
4154
4155 SET_TEXT_POS (pos,
4156 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4157 }
4158 }
4159 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4160
4161 /* Determine face for CHARSET_ASCII, or unibyte. */
4162 face_id = face_at_buffer_position (it->w,
4163 CHARPOS (pos),
4164 it->region_beg_charpos,
4165 it->region_end_charpos,
4166 &next_check_charpos,
4167 limit, 0, -1);
4168
4169 /* Correct the face for charsets different from ASCII. Do it
4170 for the multibyte case only. The face returned above is
4171 suitable for unibyte text if current_buffer is unibyte. */
4172 if (it->multibyte_p)
4173 {
4174 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4175 struct face *face = FACE_FROM_ID (it->f, face_id);
4176 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4177 }
4178 }
4179
4180 return face_id;
4181 }
4182
4183
4184 \f
4185 /***********************************************************************
4186 Invisible text
4187 ***********************************************************************/
4188
4189 /* Set up iterator IT from invisible properties at its current
4190 position. Called from handle_stop. */
4191
4192 static enum prop_handled
4193 handle_invisible_prop (struct it *it)
4194 {
4195 enum prop_handled handled = HANDLED_NORMALLY;
4196 int invis_p;
4197 Lisp_Object prop;
4198
4199 if (STRINGP (it->string))
4200 {
4201 Lisp_Object end_charpos, limit, charpos;
4202
4203 /* Get the value of the invisible text property at the
4204 current position. Value will be nil if there is no such
4205 property. */
4206 charpos = make_number (IT_STRING_CHARPOS (*it));
4207 prop = Fget_text_property (charpos, Qinvisible, it->string);
4208 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4209
4210 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4211 {
4212 /* Record whether we have to display an ellipsis for the
4213 invisible text. */
4214 int display_ellipsis_p = (invis_p == 2);
4215 ptrdiff_t len, endpos;
4216
4217 handled = HANDLED_RECOMPUTE_PROPS;
4218
4219 /* Get the position at which the next visible text can be
4220 found in IT->string, if any. */
4221 endpos = len = SCHARS (it->string);
4222 XSETINT (limit, len);
4223 do
4224 {
4225 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4226 it->string, limit);
4227 if (INTEGERP (end_charpos))
4228 {
4229 endpos = XFASTINT (end_charpos);
4230 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4231 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4232 if (invis_p == 2)
4233 display_ellipsis_p = 1;
4234 }
4235 }
4236 while (invis_p && endpos < len);
4237
4238 if (display_ellipsis_p)
4239 it->ellipsis_p = 1;
4240
4241 if (endpos < len)
4242 {
4243 /* Text at END_CHARPOS is visible. Move IT there. */
4244 struct text_pos old;
4245 ptrdiff_t oldpos;
4246
4247 old = it->current.string_pos;
4248 oldpos = CHARPOS (old);
4249 if (it->bidi_p)
4250 {
4251 if (it->bidi_it.first_elt
4252 && it->bidi_it.charpos < SCHARS (it->string))
4253 bidi_paragraph_init (it->paragraph_embedding,
4254 &it->bidi_it, 1);
4255 /* Bidi-iterate out of the invisible text. */
4256 do
4257 {
4258 bidi_move_to_visually_next (&it->bidi_it);
4259 }
4260 while (oldpos <= it->bidi_it.charpos
4261 && it->bidi_it.charpos < endpos);
4262
4263 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4264 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4265 if (IT_CHARPOS (*it) >= endpos)
4266 it->prev_stop = endpos;
4267 }
4268 else
4269 {
4270 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4271 compute_string_pos (&it->current.string_pos, old, it->string);
4272 }
4273 }
4274 else
4275 {
4276 /* The rest of the string is invisible. If this is an
4277 overlay string, proceed with the next overlay string
4278 or whatever comes and return a character from there. */
4279 if (it->current.overlay_string_index >= 0
4280 && !display_ellipsis_p)
4281 {
4282 next_overlay_string (it);
4283 /* Don't check for overlay strings when we just
4284 finished processing them. */
4285 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4286 }
4287 else
4288 {
4289 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4290 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4291 }
4292 }
4293 }
4294 }
4295 else
4296 {
4297 ptrdiff_t newpos, next_stop, start_charpos, tem;
4298 Lisp_Object pos, overlay;
4299
4300 /* First of all, is there invisible text at this position? */
4301 tem = start_charpos = IT_CHARPOS (*it);
4302 pos = make_number (tem);
4303 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4304 &overlay);
4305 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4306
4307 /* If we are on invisible text, skip over it. */
4308 if (invis_p && start_charpos < it->end_charpos)
4309 {
4310 /* Record whether we have to display an ellipsis for the
4311 invisible text. */
4312 int display_ellipsis_p = invis_p == 2;
4313
4314 handled = HANDLED_RECOMPUTE_PROPS;
4315
4316 /* Loop skipping over invisible text. The loop is left at
4317 ZV or with IT on the first char being visible again. */
4318 do
4319 {
4320 /* Try to skip some invisible text. Return value is the
4321 position reached which can be equal to where we start
4322 if there is nothing invisible there. This skips both
4323 over invisible text properties and overlays with
4324 invisible property. */
4325 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4326
4327 /* If we skipped nothing at all we weren't at invisible
4328 text in the first place. If everything to the end of
4329 the buffer was skipped, end the loop. */
4330 if (newpos == tem || newpos >= ZV)
4331 invis_p = 0;
4332 else
4333 {
4334 /* We skipped some characters but not necessarily
4335 all there are. Check if we ended up on visible
4336 text. Fget_char_property returns the property of
4337 the char before the given position, i.e. if we
4338 get invis_p = 0, this means that the char at
4339 newpos is visible. */
4340 pos = make_number (newpos);
4341 prop = Fget_char_property (pos, Qinvisible, it->window);
4342 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4343 }
4344
4345 /* If we ended up on invisible text, proceed to
4346 skip starting with next_stop. */
4347 if (invis_p)
4348 tem = next_stop;
4349
4350 /* If there are adjacent invisible texts, don't lose the
4351 second one's ellipsis. */
4352 if (invis_p == 2)
4353 display_ellipsis_p = 1;
4354 }
4355 while (invis_p);
4356
4357 /* The position newpos is now either ZV or on visible text. */
4358 if (it->bidi_p)
4359 {
4360 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4361 int on_newline =
4362 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4363 int after_newline =
4364 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4365
4366 /* If the invisible text ends on a newline or on a
4367 character after a newline, we can avoid the costly,
4368 character by character, bidi iteration to NEWPOS, and
4369 instead simply reseat the iterator there. That's
4370 because all bidi reordering information is tossed at
4371 the newline. This is a big win for modes that hide
4372 complete lines, like Outline, Org, etc. */
4373 if (on_newline || after_newline)
4374 {
4375 struct text_pos tpos;
4376 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4377
4378 SET_TEXT_POS (tpos, newpos, bpos);
4379 reseat_1 (it, tpos, 0);
4380 /* If we reseat on a newline/ZV, we need to prep the
4381 bidi iterator for advancing to the next character
4382 after the newline/EOB, keeping the current paragraph
4383 direction (so that PRODUCE_GLYPHS does TRT wrt
4384 prepending/appending glyphs to a glyph row). */
4385 if (on_newline)
4386 {
4387 it->bidi_it.first_elt = 0;
4388 it->bidi_it.paragraph_dir = pdir;
4389 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4390 it->bidi_it.nchars = 1;
4391 it->bidi_it.ch_len = 1;
4392 }
4393 }
4394 else /* Must use the slow method. */
4395 {
4396 /* With bidi iteration, the region of invisible text
4397 could start and/or end in the middle of a
4398 non-base embedding level. Therefore, we need to
4399 skip invisible text using the bidi iterator,
4400 starting at IT's current position, until we find
4401 ourselves outside of the invisible text.
4402 Skipping invisible text _after_ bidi iteration
4403 avoids affecting the visual order of the
4404 displayed text when invisible properties are
4405 added or removed. */
4406 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4407 {
4408 /* If we were `reseat'ed to a new paragraph,
4409 determine the paragraph base direction. We
4410 need to do it now because
4411 next_element_from_buffer may not have a
4412 chance to do it, if we are going to skip any
4413 text at the beginning, which resets the
4414 FIRST_ELT flag. */
4415 bidi_paragraph_init (it->paragraph_embedding,
4416 &it->bidi_it, 1);
4417 }
4418 do
4419 {
4420 bidi_move_to_visually_next (&it->bidi_it);
4421 }
4422 while (it->stop_charpos <= it->bidi_it.charpos
4423 && it->bidi_it.charpos < newpos);
4424 IT_CHARPOS (*it) = it->bidi_it.charpos;
4425 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4426 /* If we overstepped NEWPOS, record its position in
4427 the iterator, so that we skip invisible text if
4428 later the bidi iteration lands us in the
4429 invisible region again. */
4430 if (IT_CHARPOS (*it) >= newpos)
4431 it->prev_stop = newpos;
4432 }
4433 }
4434 else
4435 {
4436 IT_CHARPOS (*it) = newpos;
4437 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4438 }
4439
4440 /* If there are before-strings at the start of invisible
4441 text, and the text is invisible because of a text
4442 property, arrange to show before-strings because 20.x did
4443 it that way. (If the text is invisible because of an
4444 overlay property instead of a text property, this is
4445 already handled in the overlay code.) */
4446 if (NILP (overlay)
4447 && get_overlay_strings (it, it->stop_charpos))
4448 {
4449 handled = HANDLED_RECOMPUTE_PROPS;
4450 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4451 }
4452 else if (display_ellipsis_p)
4453 {
4454 /* Make sure that the glyphs of the ellipsis will get
4455 correct `charpos' values. If we would not update
4456 it->position here, the glyphs would belong to the
4457 last visible character _before_ the invisible
4458 text, which confuses `set_cursor_from_row'.
4459
4460 We use the last invisible position instead of the
4461 first because this way the cursor is always drawn on
4462 the first "." of the ellipsis, whenever PT is inside
4463 the invisible text. Otherwise the cursor would be
4464 placed _after_ the ellipsis when the point is after the
4465 first invisible character. */
4466 if (!STRINGP (it->object))
4467 {
4468 it->position.charpos = newpos - 1;
4469 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4470 }
4471 it->ellipsis_p = 1;
4472 /* Let the ellipsis display before
4473 considering any properties of the following char.
4474 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4475 handled = HANDLED_RETURN;
4476 }
4477 }
4478 }
4479
4480 return handled;
4481 }
4482
4483
4484 /* Make iterator IT return `...' next.
4485 Replaces LEN characters from buffer. */
4486
4487 static void
4488 setup_for_ellipsis (struct it *it, int len)
4489 {
4490 /* Use the display table definition for `...'. Invalid glyphs
4491 will be handled by the method returning elements from dpvec. */
4492 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4493 {
4494 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4495 it->dpvec = v->contents;
4496 it->dpend = v->contents + v->header.size;
4497 }
4498 else
4499 {
4500 /* Default `...'. */
4501 it->dpvec = default_invis_vector;
4502 it->dpend = default_invis_vector + 3;
4503 }
4504
4505 it->dpvec_char_len = len;
4506 it->current.dpvec_index = 0;
4507 it->dpvec_face_id = -1;
4508
4509 /* Remember the current face id in case glyphs specify faces.
4510 IT's face is restored in set_iterator_to_next.
4511 saved_face_id was set to preceding char's face in handle_stop. */
4512 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4513 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4514
4515 it->method = GET_FROM_DISPLAY_VECTOR;
4516 it->ellipsis_p = 1;
4517 }
4518
4519
4520 \f
4521 /***********************************************************************
4522 'display' property
4523 ***********************************************************************/
4524
4525 /* Set up iterator IT from `display' property at its current position.
4526 Called from handle_stop.
4527 We return HANDLED_RETURN if some part of the display property
4528 overrides the display of the buffer text itself.
4529 Otherwise we return HANDLED_NORMALLY. */
4530
4531 static enum prop_handled
4532 handle_display_prop (struct it *it)
4533 {
4534 Lisp_Object propval, object, overlay;
4535 struct text_pos *position;
4536 ptrdiff_t bufpos;
4537 /* Nonzero if some property replaces the display of the text itself. */
4538 int display_replaced_p = 0;
4539
4540 if (STRINGP (it->string))
4541 {
4542 object = it->string;
4543 position = &it->current.string_pos;
4544 bufpos = CHARPOS (it->current.pos);
4545 }
4546 else
4547 {
4548 XSETWINDOW (object, it->w);
4549 position = &it->current.pos;
4550 bufpos = CHARPOS (*position);
4551 }
4552
4553 /* Reset those iterator values set from display property values. */
4554 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4555 it->space_width = Qnil;
4556 it->font_height = Qnil;
4557 it->voffset = 0;
4558
4559 /* We don't support recursive `display' properties, i.e. string
4560 values that have a string `display' property, that have a string
4561 `display' property etc. */
4562 if (!it->string_from_display_prop_p)
4563 it->area = TEXT_AREA;
4564
4565 propval = get_char_property_and_overlay (make_number (position->charpos),
4566 Qdisplay, object, &overlay);
4567 if (NILP (propval))
4568 return HANDLED_NORMALLY;
4569 /* Now OVERLAY is the overlay that gave us this property, or nil
4570 if it was a text property. */
4571
4572 if (!STRINGP (it->string))
4573 object = it->w->contents;
4574
4575 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4576 position, bufpos,
4577 FRAME_WINDOW_P (it->f));
4578
4579 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4580 }
4581
4582 /* Subroutine of handle_display_prop. Returns non-zero if the display
4583 specification in SPEC is a replacing specification, i.e. it would
4584 replace the text covered by `display' property with something else,
4585 such as an image or a display string. If SPEC includes any kind or
4586 `(space ...) specification, the value is 2; this is used by
4587 compute_display_string_pos, which see.
4588
4589 See handle_single_display_spec for documentation of arguments.
4590 frame_window_p is non-zero if the window being redisplayed is on a
4591 GUI frame; this argument is used only if IT is NULL, see below.
4592
4593 IT can be NULL, if this is called by the bidi reordering code
4594 through compute_display_string_pos, which see. In that case, this
4595 function only examines SPEC, but does not otherwise "handle" it, in
4596 the sense that it doesn't set up members of IT from the display
4597 spec. */
4598 static int
4599 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4600 Lisp_Object overlay, struct text_pos *position,
4601 ptrdiff_t bufpos, int frame_window_p)
4602 {
4603 int replacing_p = 0;
4604 int rv;
4605
4606 if (CONSP (spec)
4607 /* Simple specifications. */
4608 && !EQ (XCAR (spec), Qimage)
4609 && !EQ (XCAR (spec), Qspace)
4610 && !EQ (XCAR (spec), Qwhen)
4611 && !EQ (XCAR (spec), Qslice)
4612 && !EQ (XCAR (spec), Qspace_width)
4613 && !EQ (XCAR (spec), Qheight)
4614 && !EQ (XCAR (spec), Qraise)
4615 /* Marginal area specifications. */
4616 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4617 && !EQ (XCAR (spec), Qleft_fringe)
4618 && !EQ (XCAR (spec), Qright_fringe)
4619 && !NILP (XCAR (spec)))
4620 {
4621 for (; CONSP (spec); spec = XCDR (spec))
4622 {
4623 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4624 overlay, position, bufpos,
4625 replacing_p, frame_window_p)))
4626 {
4627 replacing_p = rv;
4628 /* If some text in a string is replaced, `position' no
4629 longer points to the position of `object'. */
4630 if (!it || STRINGP (object))
4631 break;
4632 }
4633 }
4634 }
4635 else if (VECTORP (spec))
4636 {
4637 ptrdiff_t i;
4638 for (i = 0; i < ASIZE (spec); ++i)
4639 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4640 overlay, position, bufpos,
4641 replacing_p, frame_window_p)))
4642 {
4643 replacing_p = rv;
4644 /* If some text in a string is replaced, `position' no
4645 longer points to the position of `object'. */
4646 if (!it || STRINGP (object))
4647 break;
4648 }
4649 }
4650 else
4651 {
4652 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4653 position, bufpos, 0,
4654 frame_window_p)))
4655 replacing_p = rv;
4656 }
4657
4658 return replacing_p;
4659 }
4660
4661 /* Value is the position of the end of the `display' property starting
4662 at START_POS in OBJECT. */
4663
4664 static struct text_pos
4665 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4666 {
4667 Lisp_Object end;
4668 struct text_pos end_pos;
4669
4670 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4671 Qdisplay, object, Qnil);
4672 CHARPOS (end_pos) = XFASTINT (end);
4673 if (STRINGP (object))
4674 compute_string_pos (&end_pos, start_pos, it->string);
4675 else
4676 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4677
4678 return end_pos;
4679 }
4680
4681
4682 /* Set up IT from a single `display' property specification SPEC. OBJECT
4683 is the object in which the `display' property was found. *POSITION
4684 is the position in OBJECT at which the `display' property was found.
4685 BUFPOS is the buffer position of OBJECT (different from POSITION if
4686 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4687 previously saw a display specification which already replaced text
4688 display with something else, for example an image; we ignore such
4689 properties after the first one has been processed.
4690
4691 OVERLAY is the overlay this `display' property came from,
4692 or nil if it was a text property.
4693
4694 If SPEC is a `space' or `image' specification, and in some other
4695 cases too, set *POSITION to the position where the `display'
4696 property ends.
4697
4698 If IT is NULL, only examine the property specification in SPEC, but
4699 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4700 is intended to be displayed in a window on a GUI frame.
4701
4702 Value is non-zero if something was found which replaces the display
4703 of buffer or string text. */
4704
4705 static int
4706 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4707 Lisp_Object overlay, struct text_pos *position,
4708 ptrdiff_t bufpos, int display_replaced_p,
4709 int frame_window_p)
4710 {
4711 Lisp_Object form;
4712 Lisp_Object location, value;
4713 struct text_pos start_pos = *position;
4714 int valid_p;
4715
4716 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4717 If the result is non-nil, use VALUE instead of SPEC. */
4718 form = Qt;
4719 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4720 {
4721 spec = XCDR (spec);
4722 if (!CONSP (spec))
4723 return 0;
4724 form = XCAR (spec);
4725 spec = XCDR (spec);
4726 }
4727
4728 if (!NILP (form) && !EQ (form, Qt))
4729 {
4730 ptrdiff_t count = SPECPDL_INDEX ();
4731 struct gcpro gcpro1;
4732
4733 /* Bind `object' to the object having the `display' property, a
4734 buffer or string. Bind `position' to the position in the
4735 object where the property was found, and `buffer-position'
4736 to the current position in the buffer. */
4737
4738 if (NILP (object))
4739 XSETBUFFER (object, current_buffer);
4740 specbind (Qobject, object);
4741 specbind (Qposition, make_number (CHARPOS (*position)));
4742 specbind (Qbuffer_position, make_number (bufpos));
4743 GCPRO1 (form);
4744 form = safe_eval (form);
4745 UNGCPRO;
4746 unbind_to (count, Qnil);
4747 }
4748
4749 if (NILP (form))
4750 return 0;
4751
4752 /* Handle `(height HEIGHT)' specifications. */
4753 if (CONSP (spec)
4754 && EQ (XCAR (spec), Qheight)
4755 && CONSP (XCDR (spec)))
4756 {
4757 if (it)
4758 {
4759 if (!FRAME_WINDOW_P (it->f))
4760 return 0;
4761
4762 it->font_height = XCAR (XCDR (spec));
4763 if (!NILP (it->font_height))
4764 {
4765 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4766 int new_height = -1;
4767
4768 if (CONSP (it->font_height)
4769 && (EQ (XCAR (it->font_height), Qplus)
4770 || EQ (XCAR (it->font_height), Qminus))
4771 && CONSP (XCDR (it->font_height))
4772 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4773 {
4774 /* `(+ N)' or `(- N)' where N is an integer. */
4775 int steps = XINT (XCAR (XCDR (it->font_height)));
4776 if (EQ (XCAR (it->font_height), Qplus))
4777 steps = - steps;
4778 it->face_id = smaller_face (it->f, it->face_id, steps);
4779 }
4780 else if (FUNCTIONP (it->font_height))
4781 {
4782 /* Call function with current height as argument.
4783 Value is the new height. */
4784 Lisp_Object height;
4785 height = safe_call1 (it->font_height,
4786 face->lface[LFACE_HEIGHT_INDEX]);
4787 if (NUMBERP (height))
4788 new_height = XFLOATINT (height);
4789 }
4790 else if (NUMBERP (it->font_height))
4791 {
4792 /* Value is a multiple of the canonical char height. */
4793 struct face *f;
4794
4795 f = FACE_FROM_ID (it->f,
4796 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4797 new_height = (XFLOATINT (it->font_height)
4798 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4799 }
4800 else
4801 {
4802 /* Evaluate IT->font_height with `height' bound to the
4803 current specified height to get the new height. */
4804 ptrdiff_t count = SPECPDL_INDEX ();
4805
4806 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4807 value = safe_eval (it->font_height);
4808 unbind_to (count, Qnil);
4809
4810 if (NUMBERP (value))
4811 new_height = XFLOATINT (value);
4812 }
4813
4814 if (new_height > 0)
4815 it->face_id = face_with_height (it->f, it->face_id, new_height);
4816 }
4817 }
4818
4819 return 0;
4820 }
4821
4822 /* Handle `(space-width WIDTH)'. */
4823 if (CONSP (spec)
4824 && EQ (XCAR (spec), Qspace_width)
4825 && CONSP (XCDR (spec)))
4826 {
4827 if (it)
4828 {
4829 if (!FRAME_WINDOW_P (it->f))
4830 return 0;
4831
4832 value = XCAR (XCDR (spec));
4833 if (NUMBERP (value) && XFLOATINT (value) > 0)
4834 it->space_width = value;
4835 }
4836
4837 return 0;
4838 }
4839
4840 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4841 if (CONSP (spec)
4842 && EQ (XCAR (spec), Qslice))
4843 {
4844 Lisp_Object tem;
4845
4846 if (it)
4847 {
4848 if (!FRAME_WINDOW_P (it->f))
4849 return 0;
4850
4851 if (tem = XCDR (spec), CONSP (tem))
4852 {
4853 it->slice.x = XCAR (tem);
4854 if (tem = XCDR (tem), CONSP (tem))
4855 {
4856 it->slice.y = XCAR (tem);
4857 if (tem = XCDR (tem), CONSP (tem))
4858 {
4859 it->slice.width = XCAR (tem);
4860 if (tem = XCDR (tem), CONSP (tem))
4861 it->slice.height = XCAR (tem);
4862 }
4863 }
4864 }
4865 }
4866
4867 return 0;
4868 }
4869
4870 /* Handle `(raise FACTOR)'. */
4871 if (CONSP (spec)
4872 && EQ (XCAR (spec), Qraise)
4873 && CONSP (XCDR (spec)))
4874 {
4875 if (it)
4876 {
4877 if (!FRAME_WINDOW_P (it->f))
4878 return 0;
4879
4880 #ifdef HAVE_WINDOW_SYSTEM
4881 value = XCAR (XCDR (spec));
4882 if (NUMBERP (value))
4883 {
4884 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4885 it->voffset = - (XFLOATINT (value)
4886 * (FONT_HEIGHT (face->font)));
4887 }
4888 #endif /* HAVE_WINDOW_SYSTEM */
4889 }
4890
4891 return 0;
4892 }
4893
4894 /* Don't handle the other kinds of display specifications
4895 inside a string that we got from a `display' property. */
4896 if (it && it->string_from_display_prop_p)
4897 return 0;
4898
4899 /* Characters having this form of property are not displayed, so
4900 we have to find the end of the property. */
4901 if (it)
4902 {
4903 start_pos = *position;
4904 *position = display_prop_end (it, object, start_pos);
4905 }
4906 value = Qnil;
4907
4908 /* Stop the scan at that end position--we assume that all
4909 text properties change there. */
4910 if (it)
4911 it->stop_charpos = position->charpos;
4912
4913 /* Handle `(left-fringe BITMAP [FACE])'
4914 and `(right-fringe BITMAP [FACE])'. */
4915 if (CONSP (spec)
4916 && (EQ (XCAR (spec), Qleft_fringe)
4917 || EQ (XCAR (spec), Qright_fringe))
4918 && CONSP (XCDR (spec)))
4919 {
4920 int fringe_bitmap;
4921
4922 if (it)
4923 {
4924 if (!FRAME_WINDOW_P (it->f))
4925 /* If we return here, POSITION has been advanced
4926 across the text with this property. */
4927 {
4928 /* Synchronize the bidi iterator with POSITION. This is
4929 needed because we are not going to push the iterator
4930 on behalf of this display property, so there will be
4931 no pop_it call to do this synchronization for us. */
4932 if (it->bidi_p)
4933 {
4934 it->position = *position;
4935 iterate_out_of_display_property (it);
4936 *position = it->position;
4937 }
4938 return 1;
4939 }
4940 }
4941 else if (!frame_window_p)
4942 return 1;
4943
4944 #ifdef HAVE_WINDOW_SYSTEM
4945 value = XCAR (XCDR (spec));
4946 if (!SYMBOLP (value)
4947 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4948 /* If we return here, POSITION has been advanced
4949 across the text with this property. */
4950 {
4951 if (it && it->bidi_p)
4952 {
4953 it->position = *position;
4954 iterate_out_of_display_property (it);
4955 *position = it->position;
4956 }
4957 return 1;
4958 }
4959
4960 if (it)
4961 {
4962 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4963
4964 if (CONSP (XCDR (XCDR (spec))))
4965 {
4966 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4967 int face_id2 = lookup_derived_face (it->f, face_name,
4968 FRINGE_FACE_ID, 0);
4969 if (face_id2 >= 0)
4970 face_id = face_id2;
4971 }
4972
4973 /* Save current settings of IT so that we can restore them
4974 when we are finished with the glyph property value. */
4975 push_it (it, position);
4976
4977 it->area = TEXT_AREA;
4978 it->what = IT_IMAGE;
4979 it->image_id = -1; /* no image */
4980 it->position = start_pos;
4981 it->object = NILP (object) ? it->w->contents : object;
4982 it->method = GET_FROM_IMAGE;
4983 it->from_overlay = Qnil;
4984 it->face_id = face_id;
4985 it->from_disp_prop_p = 1;
4986
4987 /* Say that we haven't consumed the characters with
4988 `display' property yet. The call to pop_it in
4989 set_iterator_to_next will clean this up. */
4990 *position = start_pos;
4991
4992 if (EQ (XCAR (spec), Qleft_fringe))
4993 {
4994 it->left_user_fringe_bitmap = fringe_bitmap;
4995 it->left_user_fringe_face_id = face_id;
4996 }
4997 else
4998 {
4999 it->right_user_fringe_bitmap = fringe_bitmap;
5000 it->right_user_fringe_face_id = face_id;
5001 }
5002 }
5003 #endif /* HAVE_WINDOW_SYSTEM */
5004 return 1;
5005 }
5006
5007 /* Prepare to handle `((margin left-margin) ...)',
5008 `((margin right-margin) ...)' and `((margin nil) ...)'
5009 prefixes for display specifications. */
5010 location = Qunbound;
5011 if (CONSP (spec) && CONSP (XCAR (spec)))
5012 {
5013 Lisp_Object tem;
5014
5015 value = XCDR (spec);
5016 if (CONSP (value))
5017 value = XCAR (value);
5018
5019 tem = XCAR (spec);
5020 if (EQ (XCAR (tem), Qmargin)
5021 && (tem = XCDR (tem),
5022 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5023 (NILP (tem)
5024 || EQ (tem, Qleft_margin)
5025 || EQ (tem, Qright_margin))))
5026 location = tem;
5027 }
5028
5029 if (EQ (location, Qunbound))
5030 {
5031 location = Qnil;
5032 value = spec;
5033 }
5034
5035 /* After this point, VALUE is the property after any
5036 margin prefix has been stripped. It must be a string,
5037 an image specification, or `(space ...)'.
5038
5039 LOCATION specifies where to display: `left-margin',
5040 `right-margin' or nil. */
5041
5042 valid_p = (STRINGP (value)
5043 #ifdef HAVE_WINDOW_SYSTEM
5044 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5045 && valid_image_p (value))
5046 #endif /* not HAVE_WINDOW_SYSTEM */
5047 || (CONSP (value) && EQ (XCAR (value), Qspace)));
5048
5049 if (valid_p && !display_replaced_p)
5050 {
5051 int retval = 1;
5052
5053 if (!it)
5054 {
5055 /* Callers need to know whether the display spec is any kind
5056 of `(space ...)' spec that is about to affect text-area
5057 display. */
5058 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5059 retval = 2;
5060 return retval;
5061 }
5062
5063 /* Save current settings of IT so that we can restore them
5064 when we are finished with the glyph property value. */
5065 push_it (it, position);
5066 it->from_overlay = overlay;
5067 it->from_disp_prop_p = 1;
5068
5069 if (NILP (location))
5070 it->area = TEXT_AREA;
5071 else if (EQ (location, Qleft_margin))
5072 it->area = LEFT_MARGIN_AREA;
5073 else
5074 it->area = RIGHT_MARGIN_AREA;
5075
5076 if (STRINGP (value))
5077 {
5078 it->string = value;
5079 it->multibyte_p = STRING_MULTIBYTE (it->string);
5080 it->current.overlay_string_index = -1;
5081 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5082 it->end_charpos = it->string_nchars = SCHARS (it->string);
5083 it->method = GET_FROM_STRING;
5084 it->stop_charpos = 0;
5085 it->prev_stop = 0;
5086 it->base_level_stop = 0;
5087 it->string_from_display_prop_p = 1;
5088 /* Say that we haven't consumed the characters with
5089 `display' property yet. The call to pop_it in
5090 set_iterator_to_next will clean this up. */
5091 if (BUFFERP (object))
5092 *position = start_pos;
5093
5094 /* Force paragraph direction to be that of the parent
5095 object. If the parent object's paragraph direction is
5096 not yet determined, default to L2R. */
5097 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5098 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5099 else
5100 it->paragraph_embedding = L2R;
5101
5102 /* Set up the bidi iterator for this display string. */
5103 if (it->bidi_p)
5104 {
5105 it->bidi_it.string.lstring = it->string;
5106 it->bidi_it.string.s = NULL;
5107 it->bidi_it.string.schars = it->end_charpos;
5108 it->bidi_it.string.bufpos = bufpos;
5109 it->bidi_it.string.from_disp_str = 1;
5110 it->bidi_it.string.unibyte = !it->multibyte_p;
5111 it->bidi_it.w = it->w;
5112 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5113 }
5114 }
5115 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5116 {
5117 it->method = GET_FROM_STRETCH;
5118 it->object = value;
5119 *position = it->position = start_pos;
5120 retval = 1 + (it->area == TEXT_AREA);
5121 }
5122 #ifdef HAVE_WINDOW_SYSTEM
5123 else
5124 {
5125 it->what = IT_IMAGE;
5126 it->image_id = lookup_image (it->f, value);
5127 it->position = start_pos;
5128 it->object = NILP (object) ? it->w->contents : object;
5129 it->method = GET_FROM_IMAGE;
5130
5131 /* Say that we haven't consumed the characters with
5132 `display' property yet. The call to pop_it in
5133 set_iterator_to_next will clean this up. */
5134 *position = start_pos;
5135 }
5136 #endif /* HAVE_WINDOW_SYSTEM */
5137
5138 return retval;
5139 }
5140
5141 /* Invalid property or property not supported. Restore
5142 POSITION to what it was before. */
5143 *position = start_pos;
5144 return 0;
5145 }
5146
5147 /* Check if PROP is a display property value whose text should be
5148 treated as intangible. OVERLAY is the overlay from which PROP
5149 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5150 specify the buffer position covered by PROP. */
5151
5152 int
5153 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5154 ptrdiff_t charpos, ptrdiff_t bytepos)
5155 {
5156 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5157 struct text_pos position;
5158
5159 SET_TEXT_POS (position, charpos, bytepos);
5160 return handle_display_spec (NULL, prop, Qnil, overlay,
5161 &position, charpos, frame_window_p);
5162 }
5163
5164
5165 /* Return 1 if PROP is a display sub-property value containing STRING.
5166
5167 Implementation note: this and the following function are really
5168 special cases of handle_display_spec and
5169 handle_single_display_spec, and should ideally use the same code.
5170 Until they do, these two pairs must be consistent and must be
5171 modified in sync. */
5172
5173 static int
5174 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5175 {
5176 if (EQ (string, prop))
5177 return 1;
5178
5179 /* Skip over `when FORM'. */
5180 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5181 {
5182 prop = XCDR (prop);
5183 if (!CONSP (prop))
5184 return 0;
5185 /* Actually, the condition following `when' should be eval'ed,
5186 like handle_single_display_spec does, and we should return
5187 zero if it evaluates to nil. However, this function is
5188 called only when the buffer was already displayed and some
5189 glyph in the glyph matrix was found to come from a display
5190 string. Therefore, the condition was already evaluated, and
5191 the result was non-nil, otherwise the display string wouldn't
5192 have been displayed and we would have never been called for
5193 this property. Thus, we can skip the evaluation and assume
5194 its result is non-nil. */
5195 prop = XCDR (prop);
5196 }
5197
5198 if (CONSP (prop))
5199 /* Skip over `margin LOCATION'. */
5200 if (EQ (XCAR (prop), Qmargin))
5201 {
5202 prop = XCDR (prop);
5203 if (!CONSP (prop))
5204 return 0;
5205
5206 prop = XCDR (prop);
5207 if (!CONSP (prop))
5208 return 0;
5209 }
5210
5211 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5212 }
5213
5214
5215 /* Return 1 if STRING appears in the `display' property PROP. */
5216
5217 static int
5218 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5219 {
5220 if (CONSP (prop)
5221 && !EQ (XCAR (prop), Qwhen)
5222 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5223 {
5224 /* A list of sub-properties. */
5225 while (CONSP (prop))
5226 {
5227 if (single_display_spec_string_p (XCAR (prop), string))
5228 return 1;
5229 prop = XCDR (prop);
5230 }
5231 }
5232 else if (VECTORP (prop))
5233 {
5234 /* A vector of sub-properties. */
5235 ptrdiff_t i;
5236 for (i = 0; i < ASIZE (prop); ++i)
5237 if (single_display_spec_string_p (AREF (prop, i), string))
5238 return 1;
5239 }
5240 else
5241 return single_display_spec_string_p (prop, string);
5242
5243 return 0;
5244 }
5245
5246 /* Look for STRING in overlays and text properties in the current
5247 buffer, between character positions FROM and TO (excluding TO).
5248 BACK_P non-zero means look back (in this case, TO is supposed to be
5249 less than FROM).
5250 Value is the first character position where STRING was found, or
5251 zero if it wasn't found before hitting TO.
5252
5253 This function may only use code that doesn't eval because it is
5254 called asynchronously from note_mouse_highlight. */
5255
5256 static ptrdiff_t
5257 string_buffer_position_lim (Lisp_Object string,
5258 ptrdiff_t from, ptrdiff_t to, int back_p)
5259 {
5260 Lisp_Object limit, prop, pos;
5261 int found = 0;
5262
5263 pos = make_number (max (from, BEGV));
5264
5265 if (!back_p) /* looking forward */
5266 {
5267 limit = make_number (min (to, ZV));
5268 while (!found && !EQ (pos, limit))
5269 {
5270 prop = Fget_char_property (pos, Qdisplay, Qnil);
5271 if (!NILP (prop) && display_prop_string_p (prop, string))
5272 found = 1;
5273 else
5274 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5275 limit);
5276 }
5277 }
5278 else /* looking back */
5279 {
5280 limit = make_number (max (to, BEGV));
5281 while (!found && !EQ (pos, limit))
5282 {
5283 prop = Fget_char_property (pos, Qdisplay, Qnil);
5284 if (!NILP (prop) && display_prop_string_p (prop, string))
5285 found = 1;
5286 else
5287 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5288 limit);
5289 }
5290 }
5291
5292 return found ? XINT (pos) : 0;
5293 }
5294
5295 /* Determine which buffer position in current buffer STRING comes from.
5296 AROUND_CHARPOS is an approximate position where it could come from.
5297 Value is the buffer position or 0 if it couldn't be determined.
5298
5299 This function is necessary because we don't record buffer positions
5300 in glyphs generated from strings (to keep struct glyph small).
5301 This function may only use code that doesn't eval because it is
5302 called asynchronously from note_mouse_highlight. */
5303
5304 static ptrdiff_t
5305 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5306 {
5307 const int MAX_DISTANCE = 1000;
5308 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5309 around_charpos + MAX_DISTANCE,
5310 0);
5311
5312 if (!found)
5313 found = string_buffer_position_lim (string, around_charpos,
5314 around_charpos - MAX_DISTANCE, 1);
5315 return found;
5316 }
5317
5318
5319 \f
5320 /***********************************************************************
5321 `composition' property
5322 ***********************************************************************/
5323
5324 /* Set up iterator IT from `composition' property at its current
5325 position. Called from handle_stop. */
5326
5327 static enum prop_handled
5328 handle_composition_prop (struct it *it)
5329 {
5330 Lisp_Object prop, string;
5331 ptrdiff_t pos, pos_byte, start, end;
5332
5333 if (STRINGP (it->string))
5334 {
5335 unsigned char *s;
5336
5337 pos = IT_STRING_CHARPOS (*it);
5338 pos_byte = IT_STRING_BYTEPOS (*it);
5339 string = it->string;
5340 s = SDATA (string) + pos_byte;
5341 it->c = STRING_CHAR (s);
5342 }
5343 else
5344 {
5345 pos = IT_CHARPOS (*it);
5346 pos_byte = IT_BYTEPOS (*it);
5347 string = Qnil;
5348 it->c = FETCH_CHAR (pos_byte);
5349 }
5350
5351 /* If there's a valid composition and point is not inside of the
5352 composition (in the case that the composition is from the current
5353 buffer), draw a glyph composed from the composition components. */
5354 if (find_composition (pos, -1, &start, &end, &prop, string)
5355 && composition_valid_p (start, end, prop)
5356 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5357 {
5358 if (start < pos)
5359 /* As we can't handle this situation (perhaps font-lock added
5360 a new composition), we just return here hoping that next
5361 redisplay will detect this composition much earlier. */
5362 return HANDLED_NORMALLY;
5363 if (start != pos)
5364 {
5365 if (STRINGP (it->string))
5366 pos_byte = string_char_to_byte (it->string, start);
5367 else
5368 pos_byte = CHAR_TO_BYTE (start);
5369 }
5370 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5371 prop, string);
5372
5373 if (it->cmp_it.id >= 0)
5374 {
5375 it->cmp_it.ch = -1;
5376 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5377 it->cmp_it.nglyphs = -1;
5378 }
5379 }
5380
5381 return HANDLED_NORMALLY;
5382 }
5383
5384
5385 \f
5386 /***********************************************************************
5387 Overlay strings
5388 ***********************************************************************/
5389
5390 /* The following structure is used to record overlay strings for
5391 later sorting in load_overlay_strings. */
5392
5393 struct overlay_entry
5394 {
5395 Lisp_Object overlay;
5396 Lisp_Object string;
5397 EMACS_INT priority;
5398 int after_string_p;
5399 };
5400
5401
5402 /* Set up iterator IT from overlay strings at its current position.
5403 Called from handle_stop. */
5404
5405 static enum prop_handled
5406 handle_overlay_change (struct it *it)
5407 {
5408 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5409 return HANDLED_RECOMPUTE_PROPS;
5410 else
5411 return HANDLED_NORMALLY;
5412 }
5413
5414
5415 /* Set up the next overlay string for delivery by IT, if there is an
5416 overlay string to deliver. Called by set_iterator_to_next when the
5417 end of the current overlay string is reached. If there are more
5418 overlay strings to display, IT->string and
5419 IT->current.overlay_string_index are set appropriately here.
5420 Otherwise IT->string is set to nil. */
5421
5422 static void
5423 next_overlay_string (struct it *it)
5424 {
5425 ++it->current.overlay_string_index;
5426 if (it->current.overlay_string_index == it->n_overlay_strings)
5427 {
5428 /* No more overlay strings. Restore IT's settings to what
5429 they were before overlay strings were processed, and
5430 continue to deliver from current_buffer. */
5431
5432 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5433 pop_it (it);
5434 eassert (it->sp > 0
5435 || (NILP (it->string)
5436 && it->method == GET_FROM_BUFFER
5437 && it->stop_charpos >= BEGV
5438 && it->stop_charpos <= it->end_charpos));
5439 it->current.overlay_string_index = -1;
5440 it->n_overlay_strings = 0;
5441 it->overlay_strings_charpos = -1;
5442 /* If there's an empty display string on the stack, pop the
5443 stack, to resync the bidi iterator with IT's position. Such
5444 empty strings are pushed onto the stack in
5445 get_overlay_strings_1. */
5446 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5447 pop_it (it);
5448
5449 /* If we're at the end of the buffer, record that we have
5450 processed the overlay strings there already, so that
5451 next_element_from_buffer doesn't try it again. */
5452 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5453 it->overlay_strings_at_end_processed_p = 1;
5454 }
5455 else
5456 {
5457 /* There are more overlay strings to process. If
5458 IT->current.overlay_string_index has advanced to a position
5459 where we must load IT->overlay_strings with more strings, do
5460 it. We must load at the IT->overlay_strings_charpos where
5461 IT->n_overlay_strings was originally computed; when invisible
5462 text is present, this might not be IT_CHARPOS (Bug#7016). */
5463 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5464
5465 if (it->current.overlay_string_index && i == 0)
5466 load_overlay_strings (it, it->overlay_strings_charpos);
5467
5468 /* Initialize IT to deliver display elements from the overlay
5469 string. */
5470 it->string = it->overlay_strings[i];
5471 it->multibyte_p = STRING_MULTIBYTE (it->string);
5472 SET_TEXT_POS (it->current.string_pos, 0, 0);
5473 it->method = GET_FROM_STRING;
5474 it->stop_charpos = 0;
5475 it->end_charpos = SCHARS (it->string);
5476 if (it->cmp_it.stop_pos >= 0)
5477 it->cmp_it.stop_pos = 0;
5478 it->prev_stop = 0;
5479 it->base_level_stop = 0;
5480
5481 /* Set up the bidi iterator for this overlay string. */
5482 if (it->bidi_p)
5483 {
5484 it->bidi_it.string.lstring = it->string;
5485 it->bidi_it.string.s = NULL;
5486 it->bidi_it.string.schars = SCHARS (it->string);
5487 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5488 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5489 it->bidi_it.string.unibyte = !it->multibyte_p;
5490 it->bidi_it.w = it->w;
5491 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5492 }
5493 }
5494
5495 CHECK_IT (it);
5496 }
5497
5498
5499 /* Compare two overlay_entry structures E1 and E2. Used as a
5500 comparison function for qsort in load_overlay_strings. Overlay
5501 strings for the same position are sorted so that
5502
5503 1. All after-strings come in front of before-strings, except
5504 when they come from the same overlay.
5505
5506 2. Within after-strings, strings are sorted so that overlay strings
5507 from overlays with higher priorities come first.
5508
5509 2. Within before-strings, strings are sorted so that overlay
5510 strings from overlays with higher priorities come last.
5511
5512 Value is analogous to strcmp. */
5513
5514
5515 static int
5516 compare_overlay_entries (const void *e1, const void *e2)
5517 {
5518 struct overlay_entry const *entry1 = e1;
5519 struct overlay_entry const *entry2 = e2;
5520 int result;
5521
5522 if (entry1->after_string_p != entry2->after_string_p)
5523 {
5524 /* Let after-strings appear in front of before-strings if
5525 they come from different overlays. */
5526 if (EQ (entry1->overlay, entry2->overlay))
5527 result = entry1->after_string_p ? 1 : -1;
5528 else
5529 result = entry1->after_string_p ? -1 : 1;
5530 }
5531 else if (entry1->priority != entry2->priority)
5532 {
5533 if (entry1->after_string_p)
5534 /* After-strings sorted in order of decreasing priority. */
5535 result = entry2->priority < entry1->priority ? -1 : 1;
5536 else
5537 /* Before-strings sorted in order of increasing priority. */
5538 result = entry1->priority < entry2->priority ? -1 : 1;
5539 }
5540 else
5541 result = 0;
5542
5543 return result;
5544 }
5545
5546
5547 /* Load the vector IT->overlay_strings with overlay strings from IT's
5548 current buffer position, or from CHARPOS if that is > 0. Set
5549 IT->n_overlays to the total number of overlay strings found.
5550
5551 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5552 a time. On entry into load_overlay_strings,
5553 IT->current.overlay_string_index gives the number of overlay
5554 strings that have already been loaded by previous calls to this
5555 function.
5556
5557 IT->add_overlay_start contains an additional overlay start
5558 position to consider for taking overlay strings from, if non-zero.
5559 This position comes into play when the overlay has an `invisible'
5560 property, and both before and after-strings. When we've skipped to
5561 the end of the overlay, because of its `invisible' property, we
5562 nevertheless want its before-string to appear.
5563 IT->add_overlay_start will contain the overlay start position
5564 in this case.
5565
5566 Overlay strings are sorted so that after-string strings come in
5567 front of before-string strings. Within before and after-strings,
5568 strings are sorted by overlay priority. See also function
5569 compare_overlay_entries. */
5570
5571 static void
5572 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5573 {
5574 Lisp_Object overlay, window, str, invisible;
5575 struct Lisp_Overlay *ov;
5576 ptrdiff_t start, end;
5577 ptrdiff_t size = 20;
5578 ptrdiff_t n = 0, i, j;
5579 int invis_p;
5580 struct overlay_entry *entries = alloca (size * sizeof *entries);
5581 USE_SAFE_ALLOCA;
5582
5583 if (charpos <= 0)
5584 charpos = IT_CHARPOS (*it);
5585
5586 /* Append the overlay string STRING of overlay OVERLAY to vector
5587 `entries' which has size `size' and currently contains `n'
5588 elements. AFTER_P non-zero means STRING is an after-string of
5589 OVERLAY. */
5590 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5591 do \
5592 { \
5593 Lisp_Object priority; \
5594 \
5595 if (n == size) \
5596 { \
5597 struct overlay_entry *old = entries; \
5598 SAFE_NALLOCA (entries, 2, size); \
5599 memcpy (entries, old, size * sizeof *entries); \
5600 size *= 2; \
5601 } \
5602 \
5603 entries[n].string = (STRING); \
5604 entries[n].overlay = (OVERLAY); \
5605 priority = Foverlay_get ((OVERLAY), Qpriority); \
5606 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5607 entries[n].after_string_p = (AFTER_P); \
5608 ++n; \
5609 } \
5610 while (0)
5611
5612 /* Process overlay before the overlay center. */
5613 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5614 {
5615 XSETMISC (overlay, ov);
5616 eassert (OVERLAYP (overlay));
5617 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5618 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5619
5620 if (end < charpos)
5621 break;
5622
5623 /* Skip this overlay if it doesn't start or end at IT's current
5624 position. */
5625 if (end != charpos && start != charpos)
5626 continue;
5627
5628 /* Skip this overlay if it doesn't apply to IT->w. */
5629 window = Foverlay_get (overlay, Qwindow);
5630 if (WINDOWP (window) && XWINDOW (window) != it->w)
5631 continue;
5632
5633 /* If the text ``under'' the overlay is invisible, both before-
5634 and after-strings from this overlay are visible; start and
5635 end position are indistinguishable. */
5636 invisible = Foverlay_get (overlay, Qinvisible);
5637 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5638
5639 /* If overlay has a non-empty before-string, record it. */
5640 if ((start == charpos || (end == charpos && invis_p))
5641 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5642 && SCHARS (str))
5643 RECORD_OVERLAY_STRING (overlay, str, 0);
5644
5645 /* If overlay has a non-empty after-string, record it. */
5646 if ((end == charpos || (start == charpos && invis_p))
5647 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5648 && SCHARS (str))
5649 RECORD_OVERLAY_STRING (overlay, str, 1);
5650 }
5651
5652 /* Process overlays after the overlay center. */
5653 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5654 {
5655 XSETMISC (overlay, ov);
5656 eassert (OVERLAYP (overlay));
5657 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5658 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5659
5660 if (start > charpos)
5661 break;
5662
5663 /* Skip this overlay if it doesn't start or end at IT's current
5664 position. */
5665 if (end != charpos && start != charpos)
5666 continue;
5667
5668 /* Skip this overlay if it doesn't apply to IT->w. */
5669 window = Foverlay_get (overlay, Qwindow);
5670 if (WINDOWP (window) && XWINDOW (window) != it->w)
5671 continue;
5672
5673 /* If the text ``under'' the overlay is invisible, it has a zero
5674 dimension, and both before- and after-strings apply. */
5675 invisible = Foverlay_get (overlay, Qinvisible);
5676 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5677
5678 /* If overlay has a non-empty before-string, record it. */
5679 if ((start == charpos || (end == charpos && invis_p))
5680 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5681 && SCHARS (str))
5682 RECORD_OVERLAY_STRING (overlay, str, 0);
5683
5684 /* If overlay has a non-empty after-string, record it. */
5685 if ((end == charpos || (start == charpos && invis_p))
5686 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5687 && SCHARS (str))
5688 RECORD_OVERLAY_STRING (overlay, str, 1);
5689 }
5690
5691 #undef RECORD_OVERLAY_STRING
5692
5693 /* Sort entries. */
5694 if (n > 1)
5695 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5696
5697 /* Record number of overlay strings, and where we computed it. */
5698 it->n_overlay_strings = n;
5699 it->overlay_strings_charpos = charpos;
5700
5701 /* IT->current.overlay_string_index is the number of overlay strings
5702 that have already been consumed by IT. Copy some of the
5703 remaining overlay strings to IT->overlay_strings. */
5704 i = 0;
5705 j = it->current.overlay_string_index;
5706 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5707 {
5708 it->overlay_strings[i] = entries[j].string;
5709 it->string_overlays[i++] = entries[j++].overlay;
5710 }
5711
5712 CHECK_IT (it);
5713 SAFE_FREE ();
5714 }
5715
5716
5717 /* Get the first chunk of overlay strings at IT's current buffer
5718 position, or at CHARPOS if that is > 0. Value is non-zero if at
5719 least one overlay string was found. */
5720
5721 static int
5722 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5723 {
5724 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5725 process. This fills IT->overlay_strings with strings, and sets
5726 IT->n_overlay_strings to the total number of strings to process.
5727 IT->pos.overlay_string_index has to be set temporarily to zero
5728 because load_overlay_strings needs this; it must be set to -1
5729 when no overlay strings are found because a zero value would
5730 indicate a position in the first overlay string. */
5731 it->current.overlay_string_index = 0;
5732 load_overlay_strings (it, charpos);
5733
5734 /* If we found overlay strings, set up IT to deliver display
5735 elements from the first one. Otherwise set up IT to deliver
5736 from current_buffer. */
5737 if (it->n_overlay_strings)
5738 {
5739 /* Make sure we know settings in current_buffer, so that we can
5740 restore meaningful values when we're done with the overlay
5741 strings. */
5742 if (compute_stop_p)
5743 compute_stop_pos (it);
5744 eassert (it->face_id >= 0);
5745
5746 /* Save IT's settings. They are restored after all overlay
5747 strings have been processed. */
5748 eassert (!compute_stop_p || it->sp == 0);
5749
5750 /* When called from handle_stop, there might be an empty display
5751 string loaded. In that case, don't bother saving it. But
5752 don't use this optimization with the bidi iterator, since we
5753 need the corresponding pop_it call to resync the bidi
5754 iterator's position with IT's position, after we are done
5755 with the overlay strings. (The corresponding call to pop_it
5756 in case of an empty display string is in
5757 next_overlay_string.) */
5758 if (!(!it->bidi_p
5759 && STRINGP (it->string) && !SCHARS (it->string)))
5760 push_it (it, NULL);
5761
5762 /* Set up IT to deliver display elements from the first overlay
5763 string. */
5764 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5765 it->string = it->overlay_strings[0];
5766 it->from_overlay = Qnil;
5767 it->stop_charpos = 0;
5768 eassert (STRINGP (it->string));
5769 it->end_charpos = SCHARS (it->string);
5770 it->prev_stop = 0;
5771 it->base_level_stop = 0;
5772 it->multibyte_p = STRING_MULTIBYTE (it->string);
5773 it->method = GET_FROM_STRING;
5774 it->from_disp_prop_p = 0;
5775
5776 /* Force paragraph direction to be that of the parent
5777 buffer. */
5778 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5779 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5780 else
5781 it->paragraph_embedding = L2R;
5782
5783 /* Set up the bidi iterator for this overlay string. */
5784 if (it->bidi_p)
5785 {
5786 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5787
5788 it->bidi_it.string.lstring = it->string;
5789 it->bidi_it.string.s = NULL;
5790 it->bidi_it.string.schars = SCHARS (it->string);
5791 it->bidi_it.string.bufpos = pos;
5792 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5793 it->bidi_it.string.unibyte = !it->multibyte_p;
5794 it->bidi_it.w = it->w;
5795 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5796 }
5797 return 1;
5798 }
5799
5800 it->current.overlay_string_index = -1;
5801 return 0;
5802 }
5803
5804 static int
5805 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5806 {
5807 it->string = Qnil;
5808 it->method = GET_FROM_BUFFER;
5809
5810 (void) get_overlay_strings_1 (it, charpos, 1);
5811
5812 CHECK_IT (it);
5813
5814 /* Value is non-zero if we found at least one overlay string. */
5815 return STRINGP (it->string);
5816 }
5817
5818
5819 \f
5820 /***********************************************************************
5821 Saving and restoring state
5822 ***********************************************************************/
5823
5824 /* Save current settings of IT on IT->stack. Called, for example,
5825 before setting up IT for an overlay string, to be able to restore
5826 IT's settings to what they were after the overlay string has been
5827 processed. If POSITION is non-NULL, it is the position to save on
5828 the stack instead of IT->position. */
5829
5830 static void
5831 push_it (struct it *it, struct text_pos *position)
5832 {
5833 struct iterator_stack_entry *p;
5834
5835 eassert (it->sp < IT_STACK_SIZE);
5836 p = it->stack + it->sp;
5837
5838 p->stop_charpos = it->stop_charpos;
5839 p->prev_stop = it->prev_stop;
5840 p->base_level_stop = it->base_level_stop;
5841 p->cmp_it = it->cmp_it;
5842 eassert (it->face_id >= 0);
5843 p->face_id = it->face_id;
5844 p->string = it->string;
5845 p->method = it->method;
5846 p->from_overlay = it->from_overlay;
5847 switch (p->method)
5848 {
5849 case GET_FROM_IMAGE:
5850 p->u.image.object = it->object;
5851 p->u.image.image_id = it->image_id;
5852 p->u.image.slice = it->slice;
5853 break;
5854 case GET_FROM_STRETCH:
5855 p->u.stretch.object = it->object;
5856 break;
5857 }
5858 p->position = position ? *position : it->position;
5859 p->current = it->current;
5860 p->end_charpos = it->end_charpos;
5861 p->string_nchars = it->string_nchars;
5862 p->area = it->area;
5863 p->multibyte_p = it->multibyte_p;
5864 p->avoid_cursor_p = it->avoid_cursor_p;
5865 p->space_width = it->space_width;
5866 p->font_height = it->font_height;
5867 p->voffset = it->voffset;
5868 p->string_from_display_prop_p = it->string_from_display_prop_p;
5869 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5870 p->display_ellipsis_p = 0;
5871 p->line_wrap = it->line_wrap;
5872 p->bidi_p = it->bidi_p;
5873 p->paragraph_embedding = it->paragraph_embedding;
5874 p->from_disp_prop_p = it->from_disp_prop_p;
5875 ++it->sp;
5876
5877 /* Save the state of the bidi iterator as well. */
5878 if (it->bidi_p)
5879 bidi_push_it (&it->bidi_it);
5880 }
5881
5882 static void
5883 iterate_out_of_display_property (struct it *it)
5884 {
5885 int buffer_p = !STRINGP (it->string);
5886 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5887 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5888
5889 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5890
5891 /* Maybe initialize paragraph direction. If we are at the beginning
5892 of a new paragraph, next_element_from_buffer may not have a
5893 chance to do that. */
5894 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5895 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5896 /* prev_stop can be zero, so check against BEGV as well. */
5897 while (it->bidi_it.charpos >= bob
5898 && it->prev_stop <= it->bidi_it.charpos
5899 && it->bidi_it.charpos < CHARPOS (it->position)
5900 && it->bidi_it.charpos < eob)
5901 bidi_move_to_visually_next (&it->bidi_it);
5902 /* Record the stop_pos we just crossed, for when we cross it
5903 back, maybe. */
5904 if (it->bidi_it.charpos > CHARPOS (it->position))
5905 it->prev_stop = CHARPOS (it->position);
5906 /* If we ended up not where pop_it put us, resync IT's
5907 positional members with the bidi iterator. */
5908 if (it->bidi_it.charpos != CHARPOS (it->position))
5909 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5910 if (buffer_p)
5911 it->current.pos = it->position;
5912 else
5913 it->current.string_pos = it->position;
5914 }
5915
5916 /* Restore IT's settings from IT->stack. Called, for example, when no
5917 more overlay strings must be processed, and we return to delivering
5918 display elements from a buffer, or when the end of a string from a
5919 `display' property is reached and we return to delivering display
5920 elements from an overlay string, or from a buffer. */
5921
5922 static void
5923 pop_it (struct it *it)
5924 {
5925 struct iterator_stack_entry *p;
5926 int from_display_prop = it->from_disp_prop_p;
5927
5928 eassert (it->sp > 0);
5929 --it->sp;
5930 p = it->stack + it->sp;
5931 it->stop_charpos = p->stop_charpos;
5932 it->prev_stop = p->prev_stop;
5933 it->base_level_stop = p->base_level_stop;
5934 it->cmp_it = p->cmp_it;
5935 it->face_id = p->face_id;
5936 it->current = p->current;
5937 it->position = p->position;
5938 it->string = p->string;
5939 it->from_overlay = p->from_overlay;
5940 if (NILP (it->string))
5941 SET_TEXT_POS (it->current.string_pos, -1, -1);
5942 it->method = p->method;
5943 switch (it->method)
5944 {
5945 case GET_FROM_IMAGE:
5946 it->image_id = p->u.image.image_id;
5947 it->object = p->u.image.object;
5948 it->slice = p->u.image.slice;
5949 break;
5950 case GET_FROM_STRETCH:
5951 it->object = p->u.stretch.object;
5952 break;
5953 case GET_FROM_BUFFER:
5954 it->object = it->w->contents;
5955 break;
5956 case GET_FROM_STRING:
5957 it->object = it->string;
5958 break;
5959 case GET_FROM_DISPLAY_VECTOR:
5960 if (it->s)
5961 it->method = GET_FROM_C_STRING;
5962 else if (STRINGP (it->string))
5963 it->method = GET_FROM_STRING;
5964 else
5965 {
5966 it->method = GET_FROM_BUFFER;
5967 it->object = it->w->contents;
5968 }
5969 }
5970 it->end_charpos = p->end_charpos;
5971 it->string_nchars = p->string_nchars;
5972 it->area = p->area;
5973 it->multibyte_p = p->multibyte_p;
5974 it->avoid_cursor_p = p->avoid_cursor_p;
5975 it->space_width = p->space_width;
5976 it->font_height = p->font_height;
5977 it->voffset = p->voffset;
5978 it->string_from_display_prop_p = p->string_from_display_prop_p;
5979 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5980 it->line_wrap = p->line_wrap;
5981 it->bidi_p = p->bidi_p;
5982 it->paragraph_embedding = p->paragraph_embedding;
5983 it->from_disp_prop_p = p->from_disp_prop_p;
5984 if (it->bidi_p)
5985 {
5986 bidi_pop_it (&it->bidi_it);
5987 /* Bidi-iterate until we get out of the portion of text, if any,
5988 covered by a `display' text property or by an overlay with
5989 `display' property. (We cannot just jump there, because the
5990 internal coherency of the bidi iterator state can not be
5991 preserved across such jumps.) We also must determine the
5992 paragraph base direction if the overlay we just processed is
5993 at the beginning of a new paragraph. */
5994 if (from_display_prop
5995 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5996 iterate_out_of_display_property (it);
5997
5998 eassert ((BUFFERP (it->object)
5999 && IT_CHARPOS (*it) == it->bidi_it.charpos
6000 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
6001 || (STRINGP (it->object)
6002 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
6003 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
6004 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
6005 }
6006 }
6007
6008
6009 \f
6010 /***********************************************************************
6011 Moving over lines
6012 ***********************************************************************/
6013
6014 /* Set IT's current position to the previous line start. */
6015
6016 static void
6017 back_to_previous_line_start (struct it *it)
6018 {
6019 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6020
6021 DEC_BOTH (cp, bp);
6022 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6023 }
6024
6025
6026 /* Move IT to the next line start.
6027
6028 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
6029 we skipped over part of the text (as opposed to moving the iterator
6030 continuously over the text). Otherwise, don't change the value
6031 of *SKIPPED_P.
6032
6033 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6034 iterator on the newline, if it was found.
6035
6036 Newlines may come from buffer text, overlay strings, or strings
6037 displayed via the `display' property. That's the reason we can't
6038 simply use find_newline_no_quit.
6039
6040 Note that this function may not skip over invisible text that is so
6041 because of text properties and immediately follows a newline. If
6042 it would, function reseat_at_next_visible_line_start, when called
6043 from set_iterator_to_next, would effectively make invisible
6044 characters following a newline part of the wrong glyph row, which
6045 leads to wrong cursor motion. */
6046
6047 static int
6048 forward_to_next_line_start (struct it *it, int *skipped_p,
6049 struct bidi_it *bidi_it_prev)
6050 {
6051 ptrdiff_t old_selective;
6052 int newline_found_p, n;
6053 const int MAX_NEWLINE_DISTANCE = 500;
6054
6055 /* If already on a newline, just consume it to avoid unintended
6056 skipping over invisible text below. */
6057 if (it->what == IT_CHARACTER
6058 && it->c == '\n'
6059 && CHARPOS (it->position) == IT_CHARPOS (*it))
6060 {
6061 if (it->bidi_p && bidi_it_prev)
6062 *bidi_it_prev = it->bidi_it;
6063 set_iterator_to_next (it, 0);
6064 it->c = 0;
6065 return 1;
6066 }
6067
6068 /* Don't handle selective display in the following. It's (a)
6069 unnecessary because it's done by the caller, and (b) leads to an
6070 infinite recursion because next_element_from_ellipsis indirectly
6071 calls this function. */
6072 old_selective = it->selective;
6073 it->selective = 0;
6074
6075 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6076 from buffer text. */
6077 for (n = newline_found_p = 0;
6078 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6079 n += STRINGP (it->string) ? 0 : 1)
6080 {
6081 if (!get_next_display_element (it))
6082 return 0;
6083 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6084 if (newline_found_p && it->bidi_p && bidi_it_prev)
6085 *bidi_it_prev = it->bidi_it;
6086 set_iterator_to_next (it, 0);
6087 }
6088
6089 /* If we didn't find a newline near enough, see if we can use a
6090 short-cut. */
6091 if (!newline_found_p)
6092 {
6093 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6094 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6095 1, &bytepos);
6096 Lisp_Object pos;
6097
6098 eassert (!STRINGP (it->string));
6099
6100 /* If there isn't any `display' property in sight, and no
6101 overlays, we can just use the position of the newline in
6102 buffer text. */
6103 if (it->stop_charpos >= limit
6104 || ((pos = Fnext_single_property_change (make_number (start),
6105 Qdisplay, Qnil,
6106 make_number (limit)),
6107 NILP (pos))
6108 && next_overlay_change (start) == ZV))
6109 {
6110 if (!it->bidi_p)
6111 {
6112 IT_CHARPOS (*it) = limit;
6113 IT_BYTEPOS (*it) = bytepos;
6114 }
6115 else
6116 {
6117 struct bidi_it bprev;
6118
6119 /* Help bidi.c avoid expensive searches for display
6120 properties and overlays, by telling it that there are
6121 none up to `limit'. */
6122 if (it->bidi_it.disp_pos < limit)
6123 {
6124 it->bidi_it.disp_pos = limit;
6125 it->bidi_it.disp_prop = 0;
6126 }
6127 do {
6128 bprev = it->bidi_it;
6129 bidi_move_to_visually_next (&it->bidi_it);
6130 } while (it->bidi_it.charpos != limit);
6131 IT_CHARPOS (*it) = limit;
6132 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6133 if (bidi_it_prev)
6134 *bidi_it_prev = bprev;
6135 }
6136 *skipped_p = newline_found_p = 1;
6137 }
6138 else
6139 {
6140 while (get_next_display_element (it)
6141 && !newline_found_p)
6142 {
6143 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6144 if (newline_found_p && it->bidi_p && bidi_it_prev)
6145 *bidi_it_prev = it->bidi_it;
6146 set_iterator_to_next (it, 0);
6147 }
6148 }
6149 }
6150
6151 it->selective = old_selective;
6152 return newline_found_p;
6153 }
6154
6155
6156 /* Set IT's current position to the previous visible line start. Skip
6157 invisible text that is so either due to text properties or due to
6158 selective display. Caution: this does not change IT->current_x and
6159 IT->hpos. */
6160
6161 static void
6162 back_to_previous_visible_line_start (struct it *it)
6163 {
6164 while (IT_CHARPOS (*it) > BEGV)
6165 {
6166 back_to_previous_line_start (it);
6167
6168 if (IT_CHARPOS (*it) <= BEGV)
6169 break;
6170
6171 /* If selective > 0, then lines indented more than its value are
6172 invisible. */
6173 if (it->selective > 0
6174 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6175 it->selective))
6176 continue;
6177
6178 /* Check the newline before point for invisibility. */
6179 {
6180 Lisp_Object prop;
6181 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6182 Qinvisible, it->window);
6183 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6184 continue;
6185 }
6186
6187 if (IT_CHARPOS (*it) <= BEGV)
6188 break;
6189
6190 {
6191 struct it it2;
6192 void *it2data = NULL;
6193 ptrdiff_t pos;
6194 ptrdiff_t beg, end;
6195 Lisp_Object val, overlay;
6196
6197 SAVE_IT (it2, *it, it2data);
6198
6199 /* If newline is part of a composition, continue from start of composition */
6200 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6201 && beg < IT_CHARPOS (*it))
6202 goto replaced;
6203
6204 /* If newline is replaced by a display property, find start of overlay
6205 or interval and continue search from that point. */
6206 pos = --IT_CHARPOS (it2);
6207 --IT_BYTEPOS (it2);
6208 it2.sp = 0;
6209 bidi_unshelve_cache (NULL, 0);
6210 it2.string_from_display_prop_p = 0;
6211 it2.from_disp_prop_p = 0;
6212 if (handle_display_prop (&it2) == HANDLED_RETURN
6213 && !NILP (val = get_char_property_and_overlay
6214 (make_number (pos), Qdisplay, Qnil, &overlay))
6215 && (OVERLAYP (overlay)
6216 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6217 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6218 {
6219 RESTORE_IT (it, it, it2data);
6220 goto replaced;
6221 }
6222
6223 /* Newline is not replaced by anything -- so we are done. */
6224 RESTORE_IT (it, it, it2data);
6225 break;
6226
6227 replaced:
6228 if (beg < BEGV)
6229 beg = BEGV;
6230 IT_CHARPOS (*it) = beg;
6231 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6232 }
6233 }
6234
6235 it->continuation_lines_width = 0;
6236
6237 eassert (IT_CHARPOS (*it) >= BEGV);
6238 eassert (IT_CHARPOS (*it) == BEGV
6239 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6240 CHECK_IT (it);
6241 }
6242
6243
6244 /* Reseat iterator IT at the previous visible line start. Skip
6245 invisible text that is so either due to text properties or due to
6246 selective display. At the end, update IT's overlay information,
6247 face information etc. */
6248
6249 void
6250 reseat_at_previous_visible_line_start (struct it *it)
6251 {
6252 back_to_previous_visible_line_start (it);
6253 reseat (it, it->current.pos, 1);
6254 CHECK_IT (it);
6255 }
6256
6257
6258 /* Reseat iterator IT on the next visible line start in the current
6259 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6260 preceding the line start. Skip over invisible text that is so
6261 because of selective display. Compute faces, overlays etc at the
6262 new position. Note that this function does not skip over text that
6263 is invisible because of text properties. */
6264
6265 static void
6266 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6267 {
6268 int newline_found_p, skipped_p = 0;
6269 struct bidi_it bidi_it_prev;
6270
6271 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6272
6273 /* Skip over lines that are invisible because they are indented
6274 more than the value of IT->selective. */
6275 if (it->selective > 0)
6276 while (IT_CHARPOS (*it) < ZV
6277 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6278 it->selective))
6279 {
6280 eassert (IT_BYTEPOS (*it) == BEGV
6281 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6282 newline_found_p =
6283 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6284 }
6285
6286 /* Position on the newline if that's what's requested. */
6287 if (on_newline_p && newline_found_p)
6288 {
6289 if (STRINGP (it->string))
6290 {
6291 if (IT_STRING_CHARPOS (*it) > 0)
6292 {
6293 if (!it->bidi_p)
6294 {
6295 --IT_STRING_CHARPOS (*it);
6296 --IT_STRING_BYTEPOS (*it);
6297 }
6298 else
6299 {
6300 /* We need to restore the bidi iterator to the state
6301 it had on the newline, and resync the IT's
6302 position with that. */
6303 it->bidi_it = bidi_it_prev;
6304 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6305 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6306 }
6307 }
6308 }
6309 else if (IT_CHARPOS (*it) > BEGV)
6310 {
6311 if (!it->bidi_p)
6312 {
6313 --IT_CHARPOS (*it);
6314 --IT_BYTEPOS (*it);
6315 }
6316 else
6317 {
6318 /* We need to restore the bidi iterator to the state it
6319 had on the newline and resync IT with that. */
6320 it->bidi_it = bidi_it_prev;
6321 IT_CHARPOS (*it) = it->bidi_it.charpos;
6322 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6323 }
6324 reseat (it, it->current.pos, 0);
6325 }
6326 }
6327 else if (skipped_p)
6328 reseat (it, it->current.pos, 0);
6329
6330 CHECK_IT (it);
6331 }
6332
6333
6334 \f
6335 /***********************************************************************
6336 Changing an iterator's position
6337 ***********************************************************************/
6338
6339 /* Change IT's current position to POS in current_buffer. If FORCE_P
6340 is non-zero, always check for text properties at the new position.
6341 Otherwise, text properties are only looked up if POS >=
6342 IT->check_charpos of a property. */
6343
6344 static void
6345 reseat (struct it *it, struct text_pos pos, int force_p)
6346 {
6347 ptrdiff_t original_pos = IT_CHARPOS (*it);
6348
6349 reseat_1 (it, pos, 0);
6350
6351 /* Determine where to check text properties. Avoid doing it
6352 where possible because text property lookup is very expensive. */
6353 if (force_p
6354 || CHARPOS (pos) > it->stop_charpos
6355 || CHARPOS (pos) < original_pos)
6356 {
6357 if (it->bidi_p)
6358 {
6359 /* For bidi iteration, we need to prime prev_stop and
6360 base_level_stop with our best estimations. */
6361 /* Implementation note: Of course, POS is not necessarily a
6362 stop position, so assigning prev_pos to it is a lie; we
6363 should have called compute_stop_backwards. However, if
6364 the current buffer does not include any R2L characters,
6365 that call would be a waste of cycles, because the
6366 iterator will never move back, and thus never cross this
6367 "fake" stop position. So we delay that backward search
6368 until the time we really need it, in next_element_from_buffer. */
6369 if (CHARPOS (pos) != it->prev_stop)
6370 it->prev_stop = CHARPOS (pos);
6371 if (CHARPOS (pos) < it->base_level_stop)
6372 it->base_level_stop = 0; /* meaning it's unknown */
6373 handle_stop (it);
6374 }
6375 else
6376 {
6377 handle_stop (it);
6378 it->prev_stop = it->base_level_stop = 0;
6379 }
6380
6381 }
6382
6383 CHECK_IT (it);
6384 }
6385
6386
6387 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6388 IT->stop_pos to POS, also. */
6389
6390 static void
6391 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6392 {
6393 /* Don't call this function when scanning a C string. */
6394 eassert (it->s == NULL);
6395
6396 /* POS must be a reasonable value. */
6397 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6398
6399 it->current.pos = it->position = pos;
6400 it->end_charpos = ZV;
6401 it->dpvec = NULL;
6402 it->current.dpvec_index = -1;
6403 it->current.overlay_string_index = -1;
6404 IT_STRING_CHARPOS (*it) = -1;
6405 IT_STRING_BYTEPOS (*it) = -1;
6406 it->string = Qnil;
6407 it->method = GET_FROM_BUFFER;
6408 it->object = it->w->contents;
6409 it->area = TEXT_AREA;
6410 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6411 it->sp = 0;
6412 it->string_from_display_prop_p = 0;
6413 it->string_from_prefix_prop_p = 0;
6414
6415 it->from_disp_prop_p = 0;
6416 it->face_before_selective_p = 0;
6417 if (it->bidi_p)
6418 {
6419 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6420 &it->bidi_it);
6421 bidi_unshelve_cache (NULL, 0);
6422 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6423 it->bidi_it.string.s = NULL;
6424 it->bidi_it.string.lstring = Qnil;
6425 it->bidi_it.string.bufpos = 0;
6426 it->bidi_it.string.unibyte = 0;
6427 it->bidi_it.w = it->w;
6428 }
6429
6430 if (set_stop_p)
6431 {
6432 it->stop_charpos = CHARPOS (pos);
6433 it->base_level_stop = CHARPOS (pos);
6434 }
6435 /* This make the information stored in it->cmp_it invalidate. */
6436 it->cmp_it.id = -1;
6437 }
6438
6439
6440 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6441 If S is non-null, it is a C string to iterate over. Otherwise,
6442 STRING gives a Lisp string to iterate over.
6443
6444 If PRECISION > 0, don't return more then PRECISION number of
6445 characters from the string.
6446
6447 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6448 characters have been returned. FIELD_WIDTH < 0 means an infinite
6449 field width.
6450
6451 MULTIBYTE = 0 means disable processing of multibyte characters,
6452 MULTIBYTE > 0 means enable it,
6453 MULTIBYTE < 0 means use IT->multibyte_p.
6454
6455 IT must be initialized via a prior call to init_iterator before
6456 calling this function. */
6457
6458 static void
6459 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6460 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6461 int multibyte)
6462 {
6463 /* No region in strings. */
6464 it->region_beg_charpos = it->region_end_charpos = -1;
6465
6466 /* No text property checks performed by default, but see below. */
6467 it->stop_charpos = -1;
6468
6469 /* Set iterator position and end position. */
6470 memset (&it->current, 0, sizeof it->current);
6471 it->current.overlay_string_index = -1;
6472 it->current.dpvec_index = -1;
6473 eassert (charpos >= 0);
6474
6475 /* If STRING is specified, use its multibyteness, otherwise use the
6476 setting of MULTIBYTE, if specified. */
6477 if (multibyte >= 0)
6478 it->multibyte_p = multibyte > 0;
6479
6480 /* Bidirectional reordering of strings is controlled by the default
6481 value of bidi-display-reordering. Don't try to reorder while
6482 loading loadup.el, as the necessary character property tables are
6483 not yet available. */
6484 it->bidi_p =
6485 NILP (Vpurify_flag)
6486 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6487
6488 if (s == NULL)
6489 {
6490 eassert (STRINGP (string));
6491 it->string = string;
6492 it->s = NULL;
6493 it->end_charpos = it->string_nchars = SCHARS (string);
6494 it->method = GET_FROM_STRING;
6495 it->current.string_pos = string_pos (charpos, string);
6496
6497 if (it->bidi_p)
6498 {
6499 it->bidi_it.string.lstring = string;
6500 it->bidi_it.string.s = NULL;
6501 it->bidi_it.string.schars = it->end_charpos;
6502 it->bidi_it.string.bufpos = 0;
6503 it->bidi_it.string.from_disp_str = 0;
6504 it->bidi_it.string.unibyte = !it->multibyte_p;
6505 it->bidi_it.w = it->w;
6506 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6507 FRAME_WINDOW_P (it->f), &it->bidi_it);
6508 }
6509 }
6510 else
6511 {
6512 it->s = (const unsigned char *) s;
6513 it->string = Qnil;
6514
6515 /* Note that we use IT->current.pos, not it->current.string_pos,
6516 for displaying C strings. */
6517 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6518 if (it->multibyte_p)
6519 {
6520 it->current.pos = c_string_pos (charpos, s, 1);
6521 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6522 }
6523 else
6524 {
6525 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6526 it->end_charpos = it->string_nchars = strlen (s);
6527 }
6528
6529 if (it->bidi_p)
6530 {
6531 it->bidi_it.string.lstring = Qnil;
6532 it->bidi_it.string.s = (const unsigned char *) s;
6533 it->bidi_it.string.schars = it->end_charpos;
6534 it->bidi_it.string.bufpos = 0;
6535 it->bidi_it.string.from_disp_str = 0;
6536 it->bidi_it.string.unibyte = !it->multibyte_p;
6537 it->bidi_it.w = it->w;
6538 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6539 &it->bidi_it);
6540 }
6541 it->method = GET_FROM_C_STRING;
6542 }
6543
6544 /* PRECISION > 0 means don't return more than PRECISION characters
6545 from the string. */
6546 if (precision > 0 && it->end_charpos - charpos > precision)
6547 {
6548 it->end_charpos = it->string_nchars = charpos + precision;
6549 if (it->bidi_p)
6550 it->bidi_it.string.schars = it->end_charpos;
6551 }
6552
6553 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6554 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6555 FIELD_WIDTH < 0 means infinite field width. This is useful for
6556 padding with `-' at the end of a mode line. */
6557 if (field_width < 0)
6558 field_width = INFINITY;
6559 /* Implementation note: We deliberately don't enlarge
6560 it->bidi_it.string.schars here to fit it->end_charpos, because
6561 the bidi iterator cannot produce characters out of thin air. */
6562 if (field_width > it->end_charpos - charpos)
6563 it->end_charpos = charpos + field_width;
6564
6565 /* Use the standard display table for displaying strings. */
6566 if (DISP_TABLE_P (Vstandard_display_table))
6567 it->dp = XCHAR_TABLE (Vstandard_display_table);
6568
6569 it->stop_charpos = charpos;
6570 it->prev_stop = charpos;
6571 it->base_level_stop = 0;
6572 if (it->bidi_p)
6573 {
6574 it->bidi_it.first_elt = 1;
6575 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6576 it->bidi_it.disp_pos = -1;
6577 }
6578 if (s == NULL && it->multibyte_p)
6579 {
6580 ptrdiff_t endpos = SCHARS (it->string);
6581 if (endpos > it->end_charpos)
6582 endpos = it->end_charpos;
6583 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6584 it->string);
6585 }
6586 CHECK_IT (it);
6587 }
6588
6589
6590 \f
6591 /***********************************************************************
6592 Iteration
6593 ***********************************************************************/
6594
6595 /* Map enum it_method value to corresponding next_element_from_* function. */
6596
6597 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6598 {
6599 next_element_from_buffer,
6600 next_element_from_display_vector,
6601 next_element_from_string,
6602 next_element_from_c_string,
6603 next_element_from_image,
6604 next_element_from_stretch
6605 };
6606
6607 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6608
6609
6610 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6611 (possibly with the following characters). */
6612
6613 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6614 ((IT)->cmp_it.id >= 0 \
6615 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6616 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6617 END_CHARPOS, (IT)->w, \
6618 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6619 (IT)->string)))
6620
6621
6622 /* Lookup the char-table Vglyphless_char_display for character C (-1
6623 if we want information for no-font case), and return the display
6624 method symbol. By side-effect, update it->what and
6625 it->glyphless_method. This function is called from
6626 get_next_display_element for each character element, and from
6627 x_produce_glyphs when no suitable font was found. */
6628
6629 Lisp_Object
6630 lookup_glyphless_char_display (int c, struct it *it)
6631 {
6632 Lisp_Object glyphless_method = Qnil;
6633
6634 if (CHAR_TABLE_P (Vglyphless_char_display)
6635 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6636 {
6637 if (c >= 0)
6638 {
6639 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6640 if (CONSP (glyphless_method))
6641 glyphless_method = FRAME_WINDOW_P (it->f)
6642 ? XCAR (glyphless_method)
6643 : XCDR (glyphless_method);
6644 }
6645 else
6646 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6647 }
6648
6649 retry:
6650 if (NILP (glyphless_method))
6651 {
6652 if (c >= 0)
6653 /* The default is to display the character by a proper font. */
6654 return Qnil;
6655 /* The default for the no-font case is to display an empty box. */
6656 glyphless_method = Qempty_box;
6657 }
6658 if (EQ (glyphless_method, Qzero_width))
6659 {
6660 if (c >= 0)
6661 return glyphless_method;
6662 /* This method can't be used for the no-font case. */
6663 glyphless_method = Qempty_box;
6664 }
6665 if (EQ (glyphless_method, Qthin_space))
6666 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6667 else if (EQ (glyphless_method, Qempty_box))
6668 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6669 else if (EQ (glyphless_method, Qhex_code))
6670 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6671 else if (STRINGP (glyphless_method))
6672 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6673 else
6674 {
6675 /* Invalid value. We use the default method. */
6676 glyphless_method = Qnil;
6677 goto retry;
6678 }
6679 it->what = IT_GLYPHLESS;
6680 return glyphless_method;
6681 }
6682
6683 /* Load IT's display element fields with information about the next
6684 display element from the current position of IT. Value is zero if
6685 end of buffer (or C string) is reached. */
6686
6687 static struct frame *last_escape_glyph_frame = NULL;
6688 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6689 static int last_escape_glyph_merged_face_id = 0;
6690
6691 struct frame *last_glyphless_glyph_frame = NULL;
6692 int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6693 int last_glyphless_glyph_merged_face_id = 0;
6694
6695 static int
6696 get_next_display_element (struct it *it)
6697 {
6698 /* Non-zero means that we found a display element. Zero means that
6699 we hit the end of what we iterate over. Performance note: the
6700 function pointer `method' used here turns out to be faster than
6701 using a sequence of if-statements. */
6702 int success_p;
6703
6704 get_next:
6705 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6706
6707 if (it->what == IT_CHARACTER)
6708 {
6709 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6710 and only if (a) the resolved directionality of that character
6711 is R..." */
6712 /* FIXME: Do we need an exception for characters from display
6713 tables? */
6714 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6715 it->c = bidi_mirror_char (it->c);
6716 /* Map via display table or translate control characters.
6717 IT->c, IT->len etc. have been set to the next character by
6718 the function call above. If we have a display table, and it
6719 contains an entry for IT->c, translate it. Don't do this if
6720 IT->c itself comes from a display table, otherwise we could
6721 end up in an infinite recursion. (An alternative could be to
6722 count the recursion depth of this function and signal an
6723 error when a certain maximum depth is reached.) Is it worth
6724 it? */
6725 if (success_p && it->dpvec == NULL)
6726 {
6727 Lisp_Object dv;
6728 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6729 int nonascii_space_p = 0;
6730 int nonascii_hyphen_p = 0;
6731 int c = it->c; /* This is the character to display. */
6732
6733 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6734 {
6735 eassert (SINGLE_BYTE_CHAR_P (c));
6736 if (unibyte_display_via_language_environment)
6737 {
6738 c = DECODE_CHAR (unibyte, c);
6739 if (c < 0)
6740 c = BYTE8_TO_CHAR (it->c);
6741 }
6742 else
6743 c = BYTE8_TO_CHAR (it->c);
6744 }
6745
6746 if (it->dp
6747 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6748 VECTORP (dv)))
6749 {
6750 struct Lisp_Vector *v = XVECTOR (dv);
6751
6752 /* Return the first character from the display table
6753 entry, if not empty. If empty, don't display the
6754 current character. */
6755 if (v->header.size)
6756 {
6757 it->dpvec_char_len = it->len;
6758 it->dpvec = v->contents;
6759 it->dpend = v->contents + v->header.size;
6760 it->current.dpvec_index = 0;
6761 it->dpvec_face_id = -1;
6762 it->saved_face_id = it->face_id;
6763 it->method = GET_FROM_DISPLAY_VECTOR;
6764 it->ellipsis_p = 0;
6765 }
6766 else
6767 {
6768 set_iterator_to_next (it, 0);
6769 }
6770 goto get_next;
6771 }
6772
6773 if (! NILP (lookup_glyphless_char_display (c, it)))
6774 {
6775 if (it->what == IT_GLYPHLESS)
6776 goto done;
6777 /* Don't display this character. */
6778 set_iterator_to_next (it, 0);
6779 goto get_next;
6780 }
6781
6782 /* If `nobreak-char-display' is non-nil, we display
6783 non-ASCII spaces and hyphens specially. */
6784 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6785 {
6786 if (c == 0xA0)
6787 nonascii_space_p = 1;
6788 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6789 nonascii_hyphen_p = 1;
6790 }
6791
6792 /* Translate control characters into `\003' or `^C' form.
6793 Control characters coming from a display table entry are
6794 currently not translated because we use IT->dpvec to hold
6795 the translation. This could easily be changed but I
6796 don't believe that it is worth doing.
6797
6798 The characters handled by `nobreak-char-display' must be
6799 translated too.
6800
6801 Non-printable characters and raw-byte characters are also
6802 translated to octal form. */
6803 if (((c < ' ' || c == 127) /* ASCII control chars */
6804 ? (it->area != TEXT_AREA
6805 /* In mode line, treat \n, \t like other crl chars. */
6806 || (c != '\t'
6807 && it->glyph_row
6808 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6809 || (c != '\n' && c != '\t'))
6810 : (nonascii_space_p
6811 || nonascii_hyphen_p
6812 || CHAR_BYTE8_P (c)
6813 || ! CHAR_PRINTABLE_P (c))))
6814 {
6815 /* C is a control character, non-ASCII space/hyphen,
6816 raw-byte, or a non-printable character which must be
6817 displayed either as '\003' or as `^C' where the '\\'
6818 and '^' can be defined in the display table. Fill
6819 IT->ctl_chars with glyphs for what we have to
6820 display. Then, set IT->dpvec to these glyphs. */
6821 Lisp_Object gc;
6822 int ctl_len;
6823 int face_id;
6824 int lface_id = 0;
6825 int escape_glyph;
6826
6827 /* Handle control characters with ^. */
6828
6829 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6830 {
6831 int g;
6832
6833 g = '^'; /* default glyph for Control */
6834 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6835 if (it->dp
6836 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6837 {
6838 g = GLYPH_CODE_CHAR (gc);
6839 lface_id = GLYPH_CODE_FACE (gc);
6840 }
6841 if (lface_id)
6842 {
6843 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6844 }
6845 else if (it->f == last_escape_glyph_frame
6846 && it->face_id == last_escape_glyph_face_id)
6847 {
6848 face_id = last_escape_glyph_merged_face_id;
6849 }
6850 else
6851 {
6852 /* Merge the escape-glyph face into the current face. */
6853 face_id = merge_faces (it->f, Qescape_glyph, 0,
6854 it->face_id);
6855 last_escape_glyph_frame = it->f;
6856 last_escape_glyph_face_id = it->face_id;
6857 last_escape_glyph_merged_face_id = face_id;
6858 }
6859
6860 XSETINT (it->ctl_chars[0], g);
6861 XSETINT (it->ctl_chars[1], c ^ 0100);
6862 ctl_len = 2;
6863 goto display_control;
6864 }
6865
6866 /* Handle non-ascii space in the mode where it only gets
6867 highlighting. */
6868
6869 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6870 {
6871 /* Merge `nobreak-space' into the current face. */
6872 face_id = merge_faces (it->f, Qnobreak_space, 0,
6873 it->face_id);
6874 XSETINT (it->ctl_chars[0], ' ');
6875 ctl_len = 1;
6876 goto display_control;
6877 }
6878
6879 /* Handle sequences that start with the "escape glyph". */
6880
6881 /* the default escape glyph is \. */
6882 escape_glyph = '\\';
6883
6884 if (it->dp
6885 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6886 {
6887 escape_glyph = GLYPH_CODE_CHAR (gc);
6888 lface_id = GLYPH_CODE_FACE (gc);
6889 }
6890 if (lface_id)
6891 {
6892 /* The display table specified a face.
6893 Merge it into face_id and also into escape_glyph. */
6894 face_id = merge_faces (it->f, Qt, lface_id,
6895 it->face_id);
6896 }
6897 else if (it->f == last_escape_glyph_frame
6898 && it->face_id == last_escape_glyph_face_id)
6899 {
6900 face_id = last_escape_glyph_merged_face_id;
6901 }
6902 else
6903 {
6904 /* Merge the escape-glyph face into the current face. */
6905 face_id = merge_faces (it->f, Qescape_glyph, 0,
6906 it->face_id);
6907 last_escape_glyph_frame = it->f;
6908 last_escape_glyph_face_id = it->face_id;
6909 last_escape_glyph_merged_face_id = face_id;
6910 }
6911
6912 /* Draw non-ASCII hyphen with just highlighting: */
6913
6914 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6915 {
6916 XSETINT (it->ctl_chars[0], '-');
6917 ctl_len = 1;
6918 goto display_control;
6919 }
6920
6921 /* Draw non-ASCII space/hyphen with escape glyph: */
6922
6923 if (nonascii_space_p || nonascii_hyphen_p)
6924 {
6925 XSETINT (it->ctl_chars[0], escape_glyph);
6926 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6927 ctl_len = 2;
6928 goto display_control;
6929 }
6930
6931 {
6932 char str[10];
6933 int len, i;
6934
6935 if (CHAR_BYTE8_P (c))
6936 /* Display \200 instead of \17777600. */
6937 c = CHAR_TO_BYTE8 (c);
6938 len = sprintf (str, "%03o", c);
6939
6940 XSETINT (it->ctl_chars[0], escape_glyph);
6941 for (i = 0; i < len; i++)
6942 XSETINT (it->ctl_chars[i + 1], str[i]);
6943 ctl_len = len + 1;
6944 }
6945
6946 display_control:
6947 /* Set up IT->dpvec and return first character from it. */
6948 it->dpvec_char_len = it->len;
6949 it->dpvec = it->ctl_chars;
6950 it->dpend = it->dpvec + ctl_len;
6951 it->current.dpvec_index = 0;
6952 it->dpvec_face_id = face_id;
6953 it->saved_face_id = it->face_id;
6954 it->method = GET_FROM_DISPLAY_VECTOR;
6955 it->ellipsis_p = 0;
6956 goto get_next;
6957 }
6958 it->char_to_display = c;
6959 }
6960 else if (success_p)
6961 {
6962 it->char_to_display = it->c;
6963 }
6964 }
6965
6966 /* Adjust face id for a multibyte character. There are no multibyte
6967 character in unibyte text. */
6968 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6969 && it->multibyte_p
6970 && success_p
6971 && FRAME_WINDOW_P (it->f))
6972 {
6973 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6974
6975 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6976 {
6977 /* Automatic composition with glyph-string. */
6978 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6979
6980 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6981 }
6982 else
6983 {
6984 ptrdiff_t pos = (it->s ? -1
6985 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6986 : IT_CHARPOS (*it));
6987 int c;
6988
6989 if (it->what == IT_CHARACTER)
6990 c = it->char_to_display;
6991 else
6992 {
6993 struct composition *cmp = composition_table[it->cmp_it.id];
6994 int i;
6995
6996 c = ' ';
6997 for (i = 0; i < cmp->glyph_len; i++)
6998 /* TAB in a composition means display glyphs with
6999 padding space on the left or right. */
7000 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
7001 break;
7002 }
7003 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7004 }
7005 }
7006
7007 done:
7008 /* Is this character the last one of a run of characters with
7009 box? If yes, set IT->end_of_box_run_p to 1. */
7010 if (it->face_box_p
7011 && it->s == NULL)
7012 {
7013 if (it->method == GET_FROM_STRING && it->sp)
7014 {
7015 int face_id = underlying_face_id (it);
7016 struct face *face = FACE_FROM_ID (it->f, face_id);
7017
7018 if (face)
7019 {
7020 if (face->box == FACE_NO_BOX)
7021 {
7022 /* If the box comes from face properties in a
7023 display string, check faces in that string. */
7024 int string_face_id = face_after_it_pos (it);
7025 it->end_of_box_run_p
7026 = (FACE_FROM_ID (it->f, string_face_id)->box
7027 == FACE_NO_BOX);
7028 }
7029 /* Otherwise, the box comes from the underlying face.
7030 If this is the last string character displayed, check
7031 the next buffer location. */
7032 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7033 && (it->current.overlay_string_index
7034 == it->n_overlay_strings - 1))
7035 {
7036 ptrdiff_t ignore;
7037 int next_face_id;
7038 struct text_pos pos = it->current.pos;
7039 INC_TEXT_POS (pos, it->multibyte_p);
7040
7041 next_face_id = face_at_buffer_position
7042 (it->w, CHARPOS (pos), it->region_beg_charpos,
7043 it->region_end_charpos, &ignore,
7044 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
7045 -1);
7046 it->end_of_box_run_p
7047 = (FACE_FROM_ID (it->f, next_face_id)->box
7048 == FACE_NO_BOX);
7049 }
7050 }
7051 }
7052 /* next_element_from_display_vector sets this flag according to
7053 faces of the display vector glyphs, see there. */
7054 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7055 {
7056 int face_id = face_after_it_pos (it);
7057 it->end_of_box_run_p
7058 = (face_id != it->face_id
7059 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7060 }
7061 }
7062 /* If we reached the end of the object we've been iterating (e.g., a
7063 display string or an overlay string), and there's something on
7064 IT->stack, proceed with what's on the stack. It doesn't make
7065 sense to return zero if there's unprocessed stuff on the stack,
7066 because otherwise that stuff will never be displayed. */
7067 if (!success_p && it->sp > 0)
7068 {
7069 set_iterator_to_next (it, 0);
7070 success_p = get_next_display_element (it);
7071 }
7072
7073 /* Value is 0 if end of buffer or string reached. */
7074 return success_p;
7075 }
7076
7077
7078 /* Move IT to the next display element.
7079
7080 RESEAT_P non-zero means if called on a newline in buffer text,
7081 skip to the next visible line start.
7082
7083 Functions get_next_display_element and set_iterator_to_next are
7084 separate because I find this arrangement easier to handle than a
7085 get_next_display_element function that also increments IT's
7086 position. The way it is we can first look at an iterator's current
7087 display element, decide whether it fits on a line, and if it does,
7088 increment the iterator position. The other way around we probably
7089 would either need a flag indicating whether the iterator has to be
7090 incremented the next time, or we would have to implement a
7091 decrement position function which would not be easy to write. */
7092
7093 void
7094 set_iterator_to_next (struct it *it, int reseat_p)
7095 {
7096 /* Reset flags indicating start and end of a sequence of characters
7097 with box. Reset them at the start of this function because
7098 moving the iterator to a new position might set them. */
7099 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7100
7101 switch (it->method)
7102 {
7103 case GET_FROM_BUFFER:
7104 /* The current display element of IT is a character from
7105 current_buffer. Advance in the buffer, and maybe skip over
7106 invisible lines that are so because of selective display. */
7107 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7108 reseat_at_next_visible_line_start (it, 0);
7109 else if (it->cmp_it.id >= 0)
7110 {
7111 /* We are currently getting glyphs from a composition. */
7112 int i;
7113
7114 if (! it->bidi_p)
7115 {
7116 IT_CHARPOS (*it) += it->cmp_it.nchars;
7117 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7118 if (it->cmp_it.to < it->cmp_it.nglyphs)
7119 {
7120 it->cmp_it.from = it->cmp_it.to;
7121 }
7122 else
7123 {
7124 it->cmp_it.id = -1;
7125 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7126 IT_BYTEPOS (*it),
7127 it->end_charpos, Qnil);
7128 }
7129 }
7130 else if (! it->cmp_it.reversed_p)
7131 {
7132 /* Composition created while scanning forward. */
7133 /* Update IT's char/byte positions to point to the first
7134 character of the next grapheme cluster, or to the
7135 character visually after the current composition. */
7136 for (i = 0; i < it->cmp_it.nchars; i++)
7137 bidi_move_to_visually_next (&it->bidi_it);
7138 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7139 IT_CHARPOS (*it) = it->bidi_it.charpos;
7140
7141 if (it->cmp_it.to < it->cmp_it.nglyphs)
7142 {
7143 /* Proceed to the next grapheme cluster. */
7144 it->cmp_it.from = it->cmp_it.to;
7145 }
7146 else
7147 {
7148 /* No more grapheme clusters in this composition.
7149 Find the next stop position. */
7150 ptrdiff_t stop = it->end_charpos;
7151 if (it->bidi_it.scan_dir < 0)
7152 /* Now we are scanning backward and don't know
7153 where to stop. */
7154 stop = -1;
7155 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7156 IT_BYTEPOS (*it), stop, Qnil);
7157 }
7158 }
7159 else
7160 {
7161 /* Composition created while scanning backward. */
7162 /* Update IT's char/byte positions to point to the last
7163 character of the previous grapheme cluster, or the
7164 character visually after the current composition. */
7165 for (i = 0; i < it->cmp_it.nchars; i++)
7166 bidi_move_to_visually_next (&it->bidi_it);
7167 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7168 IT_CHARPOS (*it) = it->bidi_it.charpos;
7169 if (it->cmp_it.from > 0)
7170 {
7171 /* Proceed to the previous grapheme cluster. */
7172 it->cmp_it.to = it->cmp_it.from;
7173 }
7174 else
7175 {
7176 /* No more grapheme clusters in this composition.
7177 Find the next stop position. */
7178 ptrdiff_t stop = it->end_charpos;
7179 if (it->bidi_it.scan_dir < 0)
7180 /* Now we are scanning backward and don't know
7181 where to stop. */
7182 stop = -1;
7183 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7184 IT_BYTEPOS (*it), stop, Qnil);
7185 }
7186 }
7187 }
7188 else
7189 {
7190 eassert (it->len != 0);
7191
7192 if (!it->bidi_p)
7193 {
7194 IT_BYTEPOS (*it) += it->len;
7195 IT_CHARPOS (*it) += 1;
7196 }
7197 else
7198 {
7199 int prev_scan_dir = it->bidi_it.scan_dir;
7200 /* If this is a new paragraph, determine its base
7201 direction (a.k.a. its base embedding level). */
7202 if (it->bidi_it.new_paragraph)
7203 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7204 bidi_move_to_visually_next (&it->bidi_it);
7205 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7206 IT_CHARPOS (*it) = it->bidi_it.charpos;
7207 if (prev_scan_dir != it->bidi_it.scan_dir)
7208 {
7209 /* As the scan direction was changed, we must
7210 re-compute the stop position for composition. */
7211 ptrdiff_t stop = it->end_charpos;
7212 if (it->bidi_it.scan_dir < 0)
7213 stop = -1;
7214 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7215 IT_BYTEPOS (*it), stop, Qnil);
7216 }
7217 }
7218 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7219 }
7220 break;
7221
7222 case GET_FROM_C_STRING:
7223 /* Current display element of IT is from a C string. */
7224 if (!it->bidi_p
7225 /* If the string position is beyond string's end, it means
7226 next_element_from_c_string is padding the string with
7227 blanks, in which case we bypass the bidi iterator,
7228 because it cannot deal with such virtual characters. */
7229 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7230 {
7231 IT_BYTEPOS (*it) += it->len;
7232 IT_CHARPOS (*it) += 1;
7233 }
7234 else
7235 {
7236 bidi_move_to_visually_next (&it->bidi_it);
7237 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7238 IT_CHARPOS (*it) = it->bidi_it.charpos;
7239 }
7240 break;
7241
7242 case GET_FROM_DISPLAY_VECTOR:
7243 /* Current display element of IT is from a display table entry.
7244 Advance in the display table definition. Reset it to null if
7245 end reached, and continue with characters from buffers/
7246 strings. */
7247 ++it->current.dpvec_index;
7248
7249 /* Restore face of the iterator to what they were before the
7250 display vector entry (these entries may contain faces). */
7251 it->face_id = it->saved_face_id;
7252
7253 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7254 {
7255 int recheck_faces = it->ellipsis_p;
7256
7257 if (it->s)
7258 it->method = GET_FROM_C_STRING;
7259 else if (STRINGP (it->string))
7260 it->method = GET_FROM_STRING;
7261 else
7262 {
7263 it->method = GET_FROM_BUFFER;
7264 it->object = it->w->contents;
7265 }
7266
7267 it->dpvec = NULL;
7268 it->current.dpvec_index = -1;
7269
7270 /* Skip over characters which were displayed via IT->dpvec. */
7271 if (it->dpvec_char_len < 0)
7272 reseat_at_next_visible_line_start (it, 1);
7273 else if (it->dpvec_char_len > 0)
7274 {
7275 if (it->method == GET_FROM_STRING
7276 && it->current.overlay_string_index >= 0
7277 && it->n_overlay_strings > 0)
7278 it->ignore_overlay_strings_at_pos_p = 1;
7279 it->len = it->dpvec_char_len;
7280 set_iterator_to_next (it, reseat_p);
7281 }
7282
7283 /* Maybe recheck faces after display vector */
7284 if (recheck_faces)
7285 it->stop_charpos = IT_CHARPOS (*it);
7286 }
7287 break;
7288
7289 case GET_FROM_STRING:
7290 /* Current display element is a character from a Lisp string. */
7291 eassert (it->s == NULL && STRINGP (it->string));
7292 /* Don't advance past string end. These conditions are true
7293 when set_iterator_to_next is called at the end of
7294 get_next_display_element, in which case the Lisp string is
7295 already exhausted, and all we want is pop the iterator
7296 stack. */
7297 if (it->current.overlay_string_index >= 0)
7298 {
7299 /* This is an overlay string, so there's no padding with
7300 spaces, and the number of characters in the string is
7301 where the string ends. */
7302 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7303 goto consider_string_end;
7304 }
7305 else
7306 {
7307 /* Not an overlay string. There could be padding, so test
7308 against it->end_charpos . */
7309 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7310 goto consider_string_end;
7311 }
7312 if (it->cmp_it.id >= 0)
7313 {
7314 int i;
7315
7316 if (! it->bidi_p)
7317 {
7318 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7319 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7320 if (it->cmp_it.to < it->cmp_it.nglyphs)
7321 it->cmp_it.from = it->cmp_it.to;
7322 else
7323 {
7324 it->cmp_it.id = -1;
7325 composition_compute_stop_pos (&it->cmp_it,
7326 IT_STRING_CHARPOS (*it),
7327 IT_STRING_BYTEPOS (*it),
7328 it->end_charpos, it->string);
7329 }
7330 }
7331 else if (! it->cmp_it.reversed_p)
7332 {
7333 for (i = 0; i < it->cmp_it.nchars; i++)
7334 bidi_move_to_visually_next (&it->bidi_it);
7335 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7336 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7337
7338 if (it->cmp_it.to < it->cmp_it.nglyphs)
7339 it->cmp_it.from = it->cmp_it.to;
7340 else
7341 {
7342 ptrdiff_t stop = it->end_charpos;
7343 if (it->bidi_it.scan_dir < 0)
7344 stop = -1;
7345 composition_compute_stop_pos (&it->cmp_it,
7346 IT_STRING_CHARPOS (*it),
7347 IT_STRING_BYTEPOS (*it), stop,
7348 it->string);
7349 }
7350 }
7351 else
7352 {
7353 for (i = 0; i < it->cmp_it.nchars; i++)
7354 bidi_move_to_visually_next (&it->bidi_it);
7355 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7356 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7357 if (it->cmp_it.from > 0)
7358 it->cmp_it.to = it->cmp_it.from;
7359 else
7360 {
7361 ptrdiff_t stop = it->end_charpos;
7362 if (it->bidi_it.scan_dir < 0)
7363 stop = -1;
7364 composition_compute_stop_pos (&it->cmp_it,
7365 IT_STRING_CHARPOS (*it),
7366 IT_STRING_BYTEPOS (*it), stop,
7367 it->string);
7368 }
7369 }
7370 }
7371 else
7372 {
7373 if (!it->bidi_p
7374 /* If the string position is beyond string's end, it
7375 means next_element_from_string is padding the string
7376 with blanks, in which case we bypass the bidi
7377 iterator, because it cannot deal with such virtual
7378 characters. */
7379 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7380 {
7381 IT_STRING_BYTEPOS (*it) += it->len;
7382 IT_STRING_CHARPOS (*it) += 1;
7383 }
7384 else
7385 {
7386 int prev_scan_dir = it->bidi_it.scan_dir;
7387
7388 bidi_move_to_visually_next (&it->bidi_it);
7389 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7390 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7391 if (prev_scan_dir != it->bidi_it.scan_dir)
7392 {
7393 ptrdiff_t stop = it->end_charpos;
7394
7395 if (it->bidi_it.scan_dir < 0)
7396 stop = -1;
7397 composition_compute_stop_pos (&it->cmp_it,
7398 IT_STRING_CHARPOS (*it),
7399 IT_STRING_BYTEPOS (*it), stop,
7400 it->string);
7401 }
7402 }
7403 }
7404
7405 consider_string_end:
7406
7407 if (it->current.overlay_string_index >= 0)
7408 {
7409 /* IT->string is an overlay string. Advance to the
7410 next, if there is one. */
7411 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7412 {
7413 it->ellipsis_p = 0;
7414 next_overlay_string (it);
7415 if (it->ellipsis_p)
7416 setup_for_ellipsis (it, 0);
7417 }
7418 }
7419 else
7420 {
7421 /* IT->string is not an overlay string. If we reached
7422 its end, and there is something on IT->stack, proceed
7423 with what is on the stack. This can be either another
7424 string, this time an overlay string, or a buffer. */
7425 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7426 && it->sp > 0)
7427 {
7428 pop_it (it);
7429 if (it->method == GET_FROM_STRING)
7430 goto consider_string_end;
7431 }
7432 }
7433 break;
7434
7435 case GET_FROM_IMAGE:
7436 case GET_FROM_STRETCH:
7437 /* The position etc with which we have to proceed are on
7438 the stack. The position may be at the end of a string,
7439 if the `display' property takes up the whole string. */
7440 eassert (it->sp > 0);
7441 pop_it (it);
7442 if (it->method == GET_FROM_STRING)
7443 goto consider_string_end;
7444 break;
7445
7446 default:
7447 /* There are no other methods defined, so this should be a bug. */
7448 emacs_abort ();
7449 }
7450
7451 eassert (it->method != GET_FROM_STRING
7452 || (STRINGP (it->string)
7453 && IT_STRING_CHARPOS (*it) >= 0));
7454 }
7455
7456 /* Load IT's display element fields with information about the next
7457 display element which comes from a display table entry or from the
7458 result of translating a control character to one of the forms `^C'
7459 or `\003'.
7460
7461 IT->dpvec holds the glyphs to return as characters.
7462 IT->saved_face_id holds the face id before the display vector--it
7463 is restored into IT->face_id in set_iterator_to_next. */
7464
7465 static int
7466 next_element_from_display_vector (struct it *it)
7467 {
7468 Lisp_Object gc;
7469 int prev_face_id = it->face_id;
7470 int next_face_id;
7471
7472 /* Precondition. */
7473 eassert (it->dpvec && it->current.dpvec_index >= 0);
7474
7475 it->face_id = it->saved_face_id;
7476
7477 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7478 That seemed totally bogus - so I changed it... */
7479 gc = it->dpvec[it->current.dpvec_index];
7480
7481 if (GLYPH_CODE_P (gc))
7482 {
7483 struct face *this_face, *prev_face, *next_face;
7484
7485 it->c = GLYPH_CODE_CHAR (gc);
7486 it->len = CHAR_BYTES (it->c);
7487
7488 /* The entry may contain a face id to use. Such a face id is
7489 the id of a Lisp face, not a realized face. A face id of
7490 zero means no face is specified. */
7491 if (it->dpvec_face_id >= 0)
7492 it->face_id = it->dpvec_face_id;
7493 else
7494 {
7495 int lface_id = GLYPH_CODE_FACE (gc);
7496 if (lface_id > 0)
7497 it->face_id = merge_faces (it->f, Qt, lface_id,
7498 it->saved_face_id);
7499 }
7500
7501 /* Glyphs in the display vector could have the box face, so we
7502 need to set the related flags in the iterator, as
7503 appropriate. */
7504 this_face = FACE_FROM_ID (it->f, it->face_id);
7505 prev_face = FACE_FROM_ID (it->f, prev_face_id);
7506
7507 /* Is this character the first character of a box-face run? */
7508 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7509 && (!prev_face
7510 || prev_face->box == FACE_NO_BOX));
7511
7512 /* For the last character of the box-face run, we need to look
7513 either at the next glyph from the display vector, or at the
7514 face we saw before the display vector. */
7515 next_face_id = it->saved_face_id;
7516 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7517 {
7518 if (it->dpvec_face_id >= 0)
7519 next_face_id = it->dpvec_face_id;
7520 else
7521 {
7522 int lface_id =
7523 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7524
7525 if (lface_id > 0)
7526 next_face_id = merge_faces (it->f, Qt, lface_id,
7527 it->saved_face_id);
7528 }
7529 }
7530 next_face = FACE_FROM_ID (it->f, next_face_id);
7531 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7532 && (!next_face
7533 || next_face->box == FACE_NO_BOX));
7534 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7535 }
7536 else
7537 /* Display table entry is invalid. Return a space. */
7538 it->c = ' ', it->len = 1;
7539
7540 /* Don't change position and object of the iterator here. They are
7541 still the values of the character that had this display table
7542 entry or was translated, and that's what we want. */
7543 it->what = IT_CHARACTER;
7544 return 1;
7545 }
7546
7547 /* Get the first element of string/buffer in the visual order, after
7548 being reseated to a new position in a string or a buffer. */
7549 static void
7550 get_visually_first_element (struct it *it)
7551 {
7552 int string_p = STRINGP (it->string) || it->s;
7553 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7554 ptrdiff_t bob = (string_p ? 0 : BEGV);
7555
7556 if (STRINGP (it->string))
7557 {
7558 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7559 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7560 }
7561 else
7562 {
7563 it->bidi_it.charpos = IT_CHARPOS (*it);
7564 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7565 }
7566
7567 if (it->bidi_it.charpos == eob)
7568 {
7569 /* Nothing to do, but reset the FIRST_ELT flag, like
7570 bidi_paragraph_init does, because we are not going to
7571 call it. */
7572 it->bidi_it.first_elt = 0;
7573 }
7574 else if (it->bidi_it.charpos == bob
7575 || (!string_p
7576 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7577 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7578 {
7579 /* If we are at the beginning of a line/string, we can produce
7580 the next element right away. */
7581 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7582 bidi_move_to_visually_next (&it->bidi_it);
7583 }
7584 else
7585 {
7586 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7587
7588 /* We need to prime the bidi iterator starting at the line's or
7589 string's beginning, before we will be able to produce the
7590 next element. */
7591 if (string_p)
7592 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7593 else
7594 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7595 IT_BYTEPOS (*it), -1,
7596 &it->bidi_it.bytepos);
7597 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7598 do
7599 {
7600 /* Now return to buffer/string position where we were asked
7601 to get the next display element, and produce that. */
7602 bidi_move_to_visually_next (&it->bidi_it);
7603 }
7604 while (it->bidi_it.bytepos != orig_bytepos
7605 && it->bidi_it.charpos < eob);
7606 }
7607
7608 /* Adjust IT's position information to where we ended up. */
7609 if (STRINGP (it->string))
7610 {
7611 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7612 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7613 }
7614 else
7615 {
7616 IT_CHARPOS (*it) = it->bidi_it.charpos;
7617 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7618 }
7619
7620 if (STRINGP (it->string) || !it->s)
7621 {
7622 ptrdiff_t stop, charpos, bytepos;
7623
7624 if (STRINGP (it->string))
7625 {
7626 eassert (!it->s);
7627 stop = SCHARS (it->string);
7628 if (stop > it->end_charpos)
7629 stop = it->end_charpos;
7630 charpos = IT_STRING_CHARPOS (*it);
7631 bytepos = IT_STRING_BYTEPOS (*it);
7632 }
7633 else
7634 {
7635 stop = it->end_charpos;
7636 charpos = IT_CHARPOS (*it);
7637 bytepos = IT_BYTEPOS (*it);
7638 }
7639 if (it->bidi_it.scan_dir < 0)
7640 stop = -1;
7641 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7642 it->string);
7643 }
7644 }
7645
7646 /* Load IT with the next display element from Lisp string IT->string.
7647 IT->current.string_pos is the current position within the string.
7648 If IT->current.overlay_string_index >= 0, the Lisp string is an
7649 overlay string. */
7650
7651 static int
7652 next_element_from_string (struct it *it)
7653 {
7654 struct text_pos position;
7655
7656 eassert (STRINGP (it->string));
7657 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7658 eassert (IT_STRING_CHARPOS (*it) >= 0);
7659 position = it->current.string_pos;
7660
7661 /* With bidi reordering, the character to display might not be the
7662 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7663 that we were reseat()ed to a new string, whose paragraph
7664 direction is not known. */
7665 if (it->bidi_p && it->bidi_it.first_elt)
7666 {
7667 get_visually_first_element (it);
7668 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7669 }
7670
7671 /* Time to check for invisible text? */
7672 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7673 {
7674 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7675 {
7676 if (!(!it->bidi_p
7677 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7678 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7679 {
7680 /* With bidi non-linear iteration, we could find
7681 ourselves far beyond the last computed stop_charpos,
7682 with several other stop positions in between that we
7683 missed. Scan them all now, in buffer's logical
7684 order, until we find and handle the last stop_charpos
7685 that precedes our current position. */
7686 handle_stop_backwards (it, it->stop_charpos);
7687 return GET_NEXT_DISPLAY_ELEMENT (it);
7688 }
7689 else
7690 {
7691 if (it->bidi_p)
7692 {
7693 /* Take note of the stop position we just moved
7694 across, for when we will move back across it. */
7695 it->prev_stop = it->stop_charpos;
7696 /* If we are at base paragraph embedding level, take
7697 note of the last stop position seen at this
7698 level. */
7699 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7700 it->base_level_stop = it->stop_charpos;
7701 }
7702 handle_stop (it);
7703
7704 /* Since a handler may have changed IT->method, we must
7705 recurse here. */
7706 return GET_NEXT_DISPLAY_ELEMENT (it);
7707 }
7708 }
7709 else if (it->bidi_p
7710 /* If we are before prev_stop, we may have overstepped
7711 on our way backwards a stop_pos, and if so, we need
7712 to handle that stop_pos. */
7713 && IT_STRING_CHARPOS (*it) < it->prev_stop
7714 /* We can sometimes back up for reasons that have nothing
7715 to do with bidi reordering. E.g., compositions. The
7716 code below is only needed when we are above the base
7717 embedding level, so test for that explicitly. */
7718 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7719 {
7720 /* If we lost track of base_level_stop, we have no better
7721 place for handle_stop_backwards to start from than string
7722 beginning. This happens, e.g., when we were reseated to
7723 the previous screenful of text by vertical-motion. */
7724 if (it->base_level_stop <= 0
7725 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7726 it->base_level_stop = 0;
7727 handle_stop_backwards (it, it->base_level_stop);
7728 return GET_NEXT_DISPLAY_ELEMENT (it);
7729 }
7730 }
7731
7732 if (it->current.overlay_string_index >= 0)
7733 {
7734 /* Get the next character from an overlay string. In overlay
7735 strings, there is no field width or padding with spaces to
7736 do. */
7737 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7738 {
7739 it->what = IT_EOB;
7740 return 0;
7741 }
7742 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7743 IT_STRING_BYTEPOS (*it),
7744 it->bidi_it.scan_dir < 0
7745 ? -1
7746 : SCHARS (it->string))
7747 && next_element_from_composition (it))
7748 {
7749 return 1;
7750 }
7751 else if (STRING_MULTIBYTE (it->string))
7752 {
7753 const unsigned char *s = (SDATA (it->string)
7754 + IT_STRING_BYTEPOS (*it));
7755 it->c = string_char_and_length (s, &it->len);
7756 }
7757 else
7758 {
7759 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7760 it->len = 1;
7761 }
7762 }
7763 else
7764 {
7765 /* Get the next character from a Lisp string that is not an
7766 overlay string. Such strings come from the mode line, for
7767 example. We may have to pad with spaces, or truncate the
7768 string. See also next_element_from_c_string. */
7769 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7770 {
7771 it->what = IT_EOB;
7772 return 0;
7773 }
7774 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7775 {
7776 /* Pad with spaces. */
7777 it->c = ' ', it->len = 1;
7778 CHARPOS (position) = BYTEPOS (position) = -1;
7779 }
7780 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7781 IT_STRING_BYTEPOS (*it),
7782 it->bidi_it.scan_dir < 0
7783 ? -1
7784 : it->string_nchars)
7785 && next_element_from_composition (it))
7786 {
7787 return 1;
7788 }
7789 else if (STRING_MULTIBYTE (it->string))
7790 {
7791 const unsigned char *s = (SDATA (it->string)
7792 + IT_STRING_BYTEPOS (*it));
7793 it->c = string_char_and_length (s, &it->len);
7794 }
7795 else
7796 {
7797 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7798 it->len = 1;
7799 }
7800 }
7801
7802 /* Record what we have and where it came from. */
7803 it->what = IT_CHARACTER;
7804 it->object = it->string;
7805 it->position = position;
7806 return 1;
7807 }
7808
7809
7810 /* Load IT with next display element from C string IT->s.
7811 IT->string_nchars is the maximum number of characters to return
7812 from the string. IT->end_charpos may be greater than
7813 IT->string_nchars when this function is called, in which case we
7814 may have to return padding spaces. Value is zero if end of string
7815 reached, including padding spaces. */
7816
7817 static int
7818 next_element_from_c_string (struct it *it)
7819 {
7820 int success_p = 1;
7821
7822 eassert (it->s);
7823 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7824 it->what = IT_CHARACTER;
7825 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7826 it->object = Qnil;
7827
7828 /* With bidi reordering, the character to display might not be the
7829 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7830 we were reseated to a new string, whose paragraph direction is
7831 not known. */
7832 if (it->bidi_p && it->bidi_it.first_elt)
7833 get_visually_first_element (it);
7834
7835 /* IT's position can be greater than IT->string_nchars in case a
7836 field width or precision has been specified when the iterator was
7837 initialized. */
7838 if (IT_CHARPOS (*it) >= it->end_charpos)
7839 {
7840 /* End of the game. */
7841 it->what = IT_EOB;
7842 success_p = 0;
7843 }
7844 else if (IT_CHARPOS (*it) >= it->string_nchars)
7845 {
7846 /* Pad with spaces. */
7847 it->c = ' ', it->len = 1;
7848 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7849 }
7850 else if (it->multibyte_p)
7851 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7852 else
7853 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7854
7855 return success_p;
7856 }
7857
7858
7859 /* Set up IT to return characters from an ellipsis, if appropriate.
7860 The definition of the ellipsis glyphs may come from a display table
7861 entry. This function fills IT with the first glyph from the
7862 ellipsis if an ellipsis is to be displayed. */
7863
7864 static int
7865 next_element_from_ellipsis (struct it *it)
7866 {
7867 if (it->selective_display_ellipsis_p)
7868 setup_for_ellipsis (it, it->len);
7869 else
7870 {
7871 /* The face at the current position may be different from the
7872 face we find after the invisible text. Remember what it
7873 was in IT->saved_face_id, and signal that it's there by
7874 setting face_before_selective_p. */
7875 it->saved_face_id = it->face_id;
7876 it->method = GET_FROM_BUFFER;
7877 it->object = it->w->contents;
7878 reseat_at_next_visible_line_start (it, 1);
7879 it->face_before_selective_p = 1;
7880 }
7881
7882 return GET_NEXT_DISPLAY_ELEMENT (it);
7883 }
7884
7885
7886 /* Deliver an image display element. The iterator IT is already
7887 filled with image information (done in handle_display_prop). Value
7888 is always 1. */
7889
7890
7891 static int
7892 next_element_from_image (struct it *it)
7893 {
7894 it->what = IT_IMAGE;
7895 it->ignore_overlay_strings_at_pos_p = 0;
7896 return 1;
7897 }
7898
7899
7900 /* Fill iterator IT with next display element from a stretch glyph
7901 property. IT->object is the value of the text property. Value is
7902 always 1. */
7903
7904 static int
7905 next_element_from_stretch (struct it *it)
7906 {
7907 it->what = IT_STRETCH;
7908 return 1;
7909 }
7910
7911 /* Scan backwards from IT's current position until we find a stop
7912 position, or until BEGV. This is called when we find ourself
7913 before both the last known prev_stop and base_level_stop while
7914 reordering bidirectional text. */
7915
7916 static void
7917 compute_stop_pos_backwards (struct it *it)
7918 {
7919 const int SCAN_BACK_LIMIT = 1000;
7920 struct text_pos pos;
7921 struct display_pos save_current = it->current;
7922 struct text_pos save_position = it->position;
7923 ptrdiff_t charpos = IT_CHARPOS (*it);
7924 ptrdiff_t where_we_are = charpos;
7925 ptrdiff_t save_stop_pos = it->stop_charpos;
7926 ptrdiff_t save_end_pos = it->end_charpos;
7927
7928 eassert (NILP (it->string) && !it->s);
7929 eassert (it->bidi_p);
7930 it->bidi_p = 0;
7931 do
7932 {
7933 it->end_charpos = min (charpos + 1, ZV);
7934 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7935 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7936 reseat_1 (it, pos, 0);
7937 compute_stop_pos (it);
7938 /* We must advance forward, right? */
7939 if (it->stop_charpos <= charpos)
7940 emacs_abort ();
7941 }
7942 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7943
7944 if (it->stop_charpos <= where_we_are)
7945 it->prev_stop = it->stop_charpos;
7946 else
7947 it->prev_stop = BEGV;
7948 it->bidi_p = 1;
7949 it->current = save_current;
7950 it->position = save_position;
7951 it->stop_charpos = save_stop_pos;
7952 it->end_charpos = save_end_pos;
7953 }
7954
7955 /* Scan forward from CHARPOS in the current buffer/string, until we
7956 find a stop position > current IT's position. Then handle the stop
7957 position before that. This is called when we bump into a stop
7958 position while reordering bidirectional text. CHARPOS should be
7959 the last previously processed stop_pos (or BEGV/0, if none were
7960 processed yet) whose position is less that IT's current
7961 position. */
7962
7963 static void
7964 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7965 {
7966 int bufp = !STRINGP (it->string);
7967 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7968 struct display_pos save_current = it->current;
7969 struct text_pos save_position = it->position;
7970 struct text_pos pos1;
7971 ptrdiff_t next_stop;
7972
7973 /* Scan in strict logical order. */
7974 eassert (it->bidi_p);
7975 it->bidi_p = 0;
7976 do
7977 {
7978 it->prev_stop = charpos;
7979 if (bufp)
7980 {
7981 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7982 reseat_1 (it, pos1, 0);
7983 }
7984 else
7985 it->current.string_pos = string_pos (charpos, it->string);
7986 compute_stop_pos (it);
7987 /* We must advance forward, right? */
7988 if (it->stop_charpos <= it->prev_stop)
7989 emacs_abort ();
7990 charpos = it->stop_charpos;
7991 }
7992 while (charpos <= where_we_are);
7993
7994 it->bidi_p = 1;
7995 it->current = save_current;
7996 it->position = save_position;
7997 next_stop = it->stop_charpos;
7998 it->stop_charpos = it->prev_stop;
7999 handle_stop (it);
8000 it->stop_charpos = next_stop;
8001 }
8002
8003 /* Load IT with the next display element from current_buffer. Value
8004 is zero if end of buffer reached. IT->stop_charpos is the next
8005 position at which to stop and check for text properties or buffer
8006 end. */
8007
8008 static int
8009 next_element_from_buffer (struct it *it)
8010 {
8011 int success_p = 1;
8012
8013 eassert (IT_CHARPOS (*it) >= BEGV);
8014 eassert (NILP (it->string) && !it->s);
8015 eassert (!it->bidi_p
8016 || (EQ (it->bidi_it.string.lstring, Qnil)
8017 && it->bidi_it.string.s == NULL));
8018
8019 /* With bidi reordering, the character to display might not be the
8020 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
8021 we were reseat()ed to a new buffer position, which is potentially
8022 a different paragraph. */
8023 if (it->bidi_p && it->bidi_it.first_elt)
8024 {
8025 get_visually_first_element (it);
8026 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8027 }
8028
8029 if (IT_CHARPOS (*it) >= it->stop_charpos)
8030 {
8031 if (IT_CHARPOS (*it) >= it->end_charpos)
8032 {
8033 int overlay_strings_follow_p;
8034
8035 /* End of the game, except when overlay strings follow that
8036 haven't been returned yet. */
8037 if (it->overlay_strings_at_end_processed_p)
8038 overlay_strings_follow_p = 0;
8039 else
8040 {
8041 it->overlay_strings_at_end_processed_p = 1;
8042 overlay_strings_follow_p = get_overlay_strings (it, 0);
8043 }
8044
8045 if (overlay_strings_follow_p)
8046 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8047 else
8048 {
8049 it->what = IT_EOB;
8050 it->position = it->current.pos;
8051 success_p = 0;
8052 }
8053 }
8054 else if (!(!it->bidi_p
8055 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8056 || IT_CHARPOS (*it) == it->stop_charpos))
8057 {
8058 /* With bidi non-linear iteration, we could find ourselves
8059 far beyond the last computed stop_charpos, with several
8060 other stop positions in between that we missed. Scan
8061 them all now, in buffer's logical order, until we find
8062 and handle the last stop_charpos that precedes our
8063 current position. */
8064 handle_stop_backwards (it, it->stop_charpos);
8065 return GET_NEXT_DISPLAY_ELEMENT (it);
8066 }
8067 else
8068 {
8069 if (it->bidi_p)
8070 {
8071 /* Take note of the stop position we just moved across,
8072 for when we will move back across it. */
8073 it->prev_stop = it->stop_charpos;
8074 /* If we are at base paragraph embedding level, take
8075 note of the last stop position seen at this
8076 level. */
8077 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8078 it->base_level_stop = it->stop_charpos;
8079 }
8080 handle_stop (it);
8081 return GET_NEXT_DISPLAY_ELEMENT (it);
8082 }
8083 }
8084 else if (it->bidi_p
8085 /* If we are before prev_stop, we may have overstepped on
8086 our way backwards a stop_pos, and if so, we need to
8087 handle that stop_pos. */
8088 && IT_CHARPOS (*it) < it->prev_stop
8089 /* We can sometimes back up for reasons that have nothing
8090 to do with bidi reordering. E.g., compositions. The
8091 code below is only needed when we are above the base
8092 embedding level, so test for that explicitly. */
8093 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8094 {
8095 if (it->base_level_stop <= 0
8096 || IT_CHARPOS (*it) < it->base_level_stop)
8097 {
8098 /* If we lost track of base_level_stop, we need to find
8099 prev_stop by looking backwards. This happens, e.g., when
8100 we were reseated to the previous screenful of text by
8101 vertical-motion. */
8102 it->base_level_stop = BEGV;
8103 compute_stop_pos_backwards (it);
8104 handle_stop_backwards (it, it->prev_stop);
8105 }
8106 else
8107 handle_stop_backwards (it, it->base_level_stop);
8108 return GET_NEXT_DISPLAY_ELEMENT (it);
8109 }
8110 else
8111 {
8112 /* No face changes, overlays etc. in sight, so just return a
8113 character from current_buffer. */
8114 unsigned char *p;
8115 ptrdiff_t stop;
8116
8117 /* Maybe run the redisplay end trigger hook. Performance note:
8118 This doesn't seem to cost measurable time. */
8119 if (it->redisplay_end_trigger_charpos
8120 && it->glyph_row
8121 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8122 run_redisplay_end_trigger_hook (it);
8123
8124 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8125 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8126 stop)
8127 && next_element_from_composition (it))
8128 {
8129 return 1;
8130 }
8131
8132 /* Get the next character, maybe multibyte. */
8133 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8134 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8135 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8136 else
8137 it->c = *p, it->len = 1;
8138
8139 /* Record what we have and where it came from. */
8140 it->what = IT_CHARACTER;
8141 it->object = it->w->contents;
8142 it->position = it->current.pos;
8143
8144 /* Normally we return the character found above, except when we
8145 really want to return an ellipsis for selective display. */
8146 if (it->selective)
8147 {
8148 if (it->c == '\n')
8149 {
8150 /* A value of selective > 0 means hide lines indented more
8151 than that number of columns. */
8152 if (it->selective > 0
8153 && IT_CHARPOS (*it) + 1 < ZV
8154 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8155 IT_BYTEPOS (*it) + 1,
8156 it->selective))
8157 {
8158 success_p = next_element_from_ellipsis (it);
8159 it->dpvec_char_len = -1;
8160 }
8161 }
8162 else if (it->c == '\r' && it->selective == -1)
8163 {
8164 /* A value of selective == -1 means that everything from the
8165 CR to the end of the line is invisible, with maybe an
8166 ellipsis displayed for it. */
8167 success_p = next_element_from_ellipsis (it);
8168 it->dpvec_char_len = -1;
8169 }
8170 }
8171 }
8172
8173 /* Value is zero if end of buffer reached. */
8174 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8175 return success_p;
8176 }
8177
8178
8179 /* Run the redisplay end trigger hook for IT. */
8180
8181 static void
8182 run_redisplay_end_trigger_hook (struct it *it)
8183 {
8184 Lisp_Object args[3];
8185
8186 /* IT->glyph_row should be non-null, i.e. we should be actually
8187 displaying something, or otherwise we should not run the hook. */
8188 eassert (it->glyph_row);
8189
8190 /* Set up hook arguments. */
8191 args[0] = Qredisplay_end_trigger_functions;
8192 args[1] = it->window;
8193 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8194 it->redisplay_end_trigger_charpos = 0;
8195
8196 /* Since we are *trying* to run these functions, don't try to run
8197 them again, even if they get an error. */
8198 wset_redisplay_end_trigger (it->w, Qnil);
8199 Frun_hook_with_args (3, args);
8200
8201 /* Notice if it changed the face of the character we are on. */
8202 handle_face_prop (it);
8203 }
8204
8205
8206 /* Deliver a composition display element. Unlike the other
8207 next_element_from_XXX, this function is not registered in the array
8208 get_next_element[]. It is called from next_element_from_buffer and
8209 next_element_from_string when necessary. */
8210
8211 static int
8212 next_element_from_composition (struct it *it)
8213 {
8214 it->what = IT_COMPOSITION;
8215 it->len = it->cmp_it.nbytes;
8216 if (STRINGP (it->string))
8217 {
8218 if (it->c < 0)
8219 {
8220 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8221 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8222 return 0;
8223 }
8224 it->position = it->current.string_pos;
8225 it->object = it->string;
8226 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8227 IT_STRING_BYTEPOS (*it), it->string);
8228 }
8229 else
8230 {
8231 if (it->c < 0)
8232 {
8233 IT_CHARPOS (*it) += it->cmp_it.nchars;
8234 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8235 if (it->bidi_p)
8236 {
8237 if (it->bidi_it.new_paragraph)
8238 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8239 /* Resync the bidi iterator with IT's new position.
8240 FIXME: this doesn't support bidirectional text. */
8241 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8242 bidi_move_to_visually_next (&it->bidi_it);
8243 }
8244 return 0;
8245 }
8246 it->position = it->current.pos;
8247 it->object = it->w->contents;
8248 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8249 IT_BYTEPOS (*it), Qnil);
8250 }
8251 return 1;
8252 }
8253
8254
8255 \f
8256 /***********************************************************************
8257 Moving an iterator without producing glyphs
8258 ***********************************************************************/
8259
8260 /* Check if iterator is at a position corresponding to a valid buffer
8261 position after some move_it_ call. */
8262
8263 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8264 ((it)->method == GET_FROM_STRING \
8265 ? IT_STRING_CHARPOS (*it) == 0 \
8266 : 1)
8267
8268
8269 /* Move iterator IT to a specified buffer or X position within one
8270 line on the display without producing glyphs.
8271
8272 OP should be a bit mask including some or all of these bits:
8273 MOVE_TO_X: Stop upon reaching x-position TO_X.
8274 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8275 Regardless of OP's value, stop upon reaching the end of the display line.
8276
8277 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8278 This means, in particular, that TO_X includes window's horizontal
8279 scroll amount.
8280
8281 The return value has several possible values that
8282 say what condition caused the scan to stop:
8283
8284 MOVE_POS_MATCH_OR_ZV
8285 - when TO_POS or ZV was reached.
8286
8287 MOVE_X_REACHED
8288 -when TO_X was reached before TO_POS or ZV were reached.
8289
8290 MOVE_LINE_CONTINUED
8291 - when we reached the end of the display area and the line must
8292 be continued.
8293
8294 MOVE_LINE_TRUNCATED
8295 - when we reached the end of the display area and the line is
8296 truncated.
8297
8298 MOVE_NEWLINE_OR_CR
8299 - when we stopped at a line end, i.e. a newline or a CR and selective
8300 display is on. */
8301
8302 static enum move_it_result
8303 move_it_in_display_line_to (struct it *it,
8304 ptrdiff_t to_charpos, int to_x,
8305 enum move_operation_enum op)
8306 {
8307 enum move_it_result result = MOVE_UNDEFINED;
8308 struct glyph_row *saved_glyph_row;
8309 struct it wrap_it, atpos_it, atx_it, ppos_it;
8310 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8311 void *ppos_data = NULL;
8312 int may_wrap = 0;
8313 enum it_method prev_method = it->method;
8314 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8315 int saw_smaller_pos = prev_pos < to_charpos;
8316
8317 /* Don't produce glyphs in produce_glyphs. */
8318 saved_glyph_row = it->glyph_row;
8319 it->glyph_row = NULL;
8320
8321 /* Use wrap_it to save a copy of IT wherever a word wrap could
8322 occur. Use atpos_it to save a copy of IT at the desired buffer
8323 position, if found, so that we can scan ahead and check if the
8324 word later overshoots the window edge. Use atx_it similarly, for
8325 pixel positions. */
8326 wrap_it.sp = -1;
8327 atpos_it.sp = -1;
8328 atx_it.sp = -1;
8329
8330 /* Use ppos_it under bidi reordering to save a copy of IT for the
8331 position > CHARPOS that is the closest to CHARPOS. We restore
8332 that position in IT when we have scanned the entire display line
8333 without finding a match for CHARPOS and all the character
8334 positions are greater than CHARPOS. */
8335 if (it->bidi_p)
8336 {
8337 SAVE_IT (ppos_it, *it, ppos_data);
8338 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8339 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8340 SAVE_IT (ppos_it, *it, ppos_data);
8341 }
8342
8343 #define BUFFER_POS_REACHED_P() \
8344 ((op & MOVE_TO_POS) != 0 \
8345 && BUFFERP (it->object) \
8346 && (IT_CHARPOS (*it) == to_charpos \
8347 || ((!it->bidi_p \
8348 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8349 && IT_CHARPOS (*it) > to_charpos) \
8350 || (it->what == IT_COMPOSITION \
8351 && ((IT_CHARPOS (*it) > to_charpos \
8352 && to_charpos >= it->cmp_it.charpos) \
8353 || (IT_CHARPOS (*it) < to_charpos \
8354 && to_charpos <= it->cmp_it.charpos)))) \
8355 && (it->method == GET_FROM_BUFFER \
8356 || (it->method == GET_FROM_DISPLAY_VECTOR \
8357 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8358
8359 /* If there's a line-/wrap-prefix, handle it. */
8360 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8361 && it->current_y < it->last_visible_y)
8362 handle_line_prefix (it);
8363
8364 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8365 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8366
8367 while (1)
8368 {
8369 int x, i, ascent = 0, descent = 0;
8370
8371 /* Utility macro to reset an iterator with x, ascent, and descent. */
8372 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8373 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8374 (IT)->max_descent = descent)
8375
8376 /* Stop if we move beyond TO_CHARPOS (after an image or a
8377 display string or stretch glyph). */
8378 if ((op & MOVE_TO_POS) != 0
8379 && BUFFERP (it->object)
8380 && it->method == GET_FROM_BUFFER
8381 && (((!it->bidi_p
8382 /* When the iterator is at base embedding level, we
8383 are guaranteed that characters are delivered for
8384 display in strictly increasing order of their
8385 buffer positions. */
8386 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8387 && IT_CHARPOS (*it) > to_charpos)
8388 || (it->bidi_p
8389 && (prev_method == GET_FROM_IMAGE
8390 || prev_method == GET_FROM_STRETCH
8391 || prev_method == GET_FROM_STRING)
8392 /* Passed TO_CHARPOS from left to right. */
8393 && ((prev_pos < to_charpos
8394 && IT_CHARPOS (*it) > to_charpos)
8395 /* Passed TO_CHARPOS from right to left. */
8396 || (prev_pos > to_charpos
8397 && IT_CHARPOS (*it) < to_charpos)))))
8398 {
8399 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8400 {
8401 result = MOVE_POS_MATCH_OR_ZV;
8402 break;
8403 }
8404 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8405 /* If wrap_it is valid, the current position might be in a
8406 word that is wrapped. So, save the iterator in
8407 atpos_it and continue to see if wrapping happens. */
8408 SAVE_IT (atpos_it, *it, atpos_data);
8409 }
8410
8411 /* Stop when ZV reached.
8412 We used to stop here when TO_CHARPOS reached as well, but that is
8413 too soon if this glyph does not fit on this line. So we handle it
8414 explicitly below. */
8415 if (!get_next_display_element (it))
8416 {
8417 result = MOVE_POS_MATCH_OR_ZV;
8418 break;
8419 }
8420
8421 if (it->line_wrap == TRUNCATE)
8422 {
8423 if (BUFFER_POS_REACHED_P ())
8424 {
8425 result = MOVE_POS_MATCH_OR_ZV;
8426 break;
8427 }
8428 }
8429 else
8430 {
8431 if (it->line_wrap == WORD_WRAP)
8432 {
8433 if (IT_DISPLAYING_WHITESPACE (it))
8434 may_wrap = 1;
8435 else if (may_wrap)
8436 {
8437 /* We have reached a glyph that follows one or more
8438 whitespace characters. If the position is
8439 already found, we are done. */
8440 if (atpos_it.sp >= 0)
8441 {
8442 RESTORE_IT (it, &atpos_it, atpos_data);
8443 result = MOVE_POS_MATCH_OR_ZV;
8444 goto done;
8445 }
8446 if (atx_it.sp >= 0)
8447 {
8448 RESTORE_IT (it, &atx_it, atx_data);
8449 result = MOVE_X_REACHED;
8450 goto done;
8451 }
8452 /* Otherwise, we can wrap here. */
8453 SAVE_IT (wrap_it, *it, wrap_data);
8454 may_wrap = 0;
8455 }
8456 }
8457 }
8458
8459 /* Remember the line height for the current line, in case
8460 the next element doesn't fit on the line. */
8461 ascent = it->max_ascent;
8462 descent = it->max_descent;
8463
8464 /* The call to produce_glyphs will get the metrics of the
8465 display element IT is loaded with. Record the x-position
8466 before this display element, in case it doesn't fit on the
8467 line. */
8468 x = it->current_x;
8469
8470 PRODUCE_GLYPHS (it);
8471
8472 if (it->area != TEXT_AREA)
8473 {
8474 prev_method = it->method;
8475 if (it->method == GET_FROM_BUFFER)
8476 prev_pos = IT_CHARPOS (*it);
8477 set_iterator_to_next (it, 1);
8478 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8479 SET_TEXT_POS (this_line_min_pos,
8480 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8481 if (it->bidi_p
8482 && (op & MOVE_TO_POS)
8483 && IT_CHARPOS (*it) > to_charpos
8484 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8485 SAVE_IT (ppos_it, *it, ppos_data);
8486 continue;
8487 }
8488
8489 /* The number of glyphs we get back in IT->nglyphs will normally
8490 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8491 character on a terminal frame, or (iii) a line end. For the
8492 second case, IT->nglyphs - 1 padding glyphs will be present.
8493 (On X frames, there is only one glyph produced for a
8494 composite character.)
8495
8496 The behavior implemented below means, for continuation lines,
8497 that as many spaces of a TAB as fit on the current line are
8498 displayed there. For terminal frames, as many glyphs of a
8499 multi-glyph character are displayed in the current line, too.
8500 This is what the old redisplay code did, and we keep it that
8501 way. Under X, the whole shape of a complex character must
8502 fit on the line or it will be completely displayed in the
8503 next line.
8504
8505 Note that both for tabs and padding glyphs, all glyphs have
8506 the same width. */
8507 if (it->nglyphs)
8508 {
8509 /* More than one glyph or glyph doesn't fit on line. All
8510 glyphs have the same width. */
8511 int single_glyph_width = it->pixel_width / it->nglyphs;
8512 int new_x;
8513 int x_before_this_char = x;
8514 int hpos_before_this_char = it->hpos;
8515
8516 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8517 {
8518 new_x = x + single_glyph_width;
8519
8520 /* We want to leave anything reaching TO_X to the caller. */
8521 if ((op & MOVE_TO_X) && new_x > to_x)
8522 {
8523 if (BUFFER_POS_REACHED_P ())
8524 {
8525 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8526 goto buffer_pos_reached;
8527 if (atpos_it.sp < 0)
8528 {
8529 SAVE_IT (atpos_it, *it, atpos_data);
8530 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8531 }
8532 }
8533 else
8534 {
8535 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8536 {
8537 it->current_x = x;
8538 result = MOVE_X_REACHED;
8539 break;
8540 }
8541 if (atx_it.sp < 0)
8542 {
8543 SAVE_IT (atx_it, *it, atx_data);
8544 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8545 }
8546 }
8547 }
8548
8549 if (/* Lines are continued. */
8550 it->line_wrap != TRUNCATE
8551 && (/* And glyph doesn't fit on the line. */
8552 new_x > it->last_visible_x
8553 /* Or it fits exactly and we're on a window
8554 system frame. */
8555 || (new_x == it->last_visible_x
8556 && FRAME_WINDOW_P (it->f)
8557 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8558 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8559 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8560 {
8561 if (/* IT->hpos == 0 means the very first glyph
8562 doesn't fit on the line, e.g. a wide image. */
8563 it->hpos == 0
8564 || (new_x == it->last_visible_x
8565 && FRAME_WINDOW_P (it->f)))
8566 {
8567 ++it->hpos;
8568 it->current_x = new_x;
8569
8570 /* The character's last glyph just barely fits
8571 in this row. */
8572 if (i == it->nglyphs - 1)
8573 {
8574 /* If this is the destination position,
8575 return a position *before* it in this row,
8576 now that we know it fits in this row. */
8577 if (BUFFER_POS_REACHED_P ())
8578 {
8579 if (it->line_wrap != WORD_WRAP
8580 || wrap_it.sp < 0)
8581 {
8582 it->hpos = hpos_before_this_char;
8583 it->current_x = x_before_this_char;
8584 result = MOVE_POS_MATCH_OR_ZV;
8585 break;
8586 }
8587 if (it->line_wrap == WORD_WRAP
8588 && atpos_it.sp < 0)
8589 {
8590 SAVE_IT (atpos_it, *it, atpos_data);
8591 atpos_it.current_x = x_before_this_char;
8592 atpos_it.hpos = hpos_before_this_char;
8593 }
8594 }
8595
8596 prev_method = it->method;
8597 if (it->method == GET_FROM_BUFFER)
8598 prev_pos = IT_CHARPOS (*it);
8599 set_iterator_to_next (it, 1);
8600 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8601 SET_TEXT_POS (this_line_min_pos,
8602 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8603 /* On graphical terminals, newlines may
8604 "overflow" into the fringe if
8605 overflow-newline-into-fringe is non-nil.
8606 On text terminals, and on graphical
8607 terminals with no right margin, newlines
8608 may overflow into the last glyph on the
8609 display line.*/
8610 if (!FRAME_WINDOW_P (it->f)
8611 || ((it->bidi_p
8612 && it->bidi_it.paragraph_dir == R2L)
8613 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8614 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8615 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8616 {
8617 if (!get_next_display_element (it))
8618 {
8619 result = MOVE_POS_MATCH_OR_ZV;
8620 break;
8621 }
8622 if (BUFFER_POS_REACHED_P ())
8623 {
8624 if (ITERATOR_AT_END_OF_LINE_P (it))
8625 result = MOVE_POS_MATCH_OR_ZV;
8626 else
8627 result = MOVE_LINE_CONTINUED;
8628 break;
8629 }
8630 if (ITERATOR_AT_END_OF_LINE_P (it)
8631 && (it->line_wrap != WORD_WRAP
8632 || wrap_it.sp < 0))
8633 {
8634 result = MOVE_NEWLINE_OR_CR;
8635 break;
8636 }
8637 }
8638 }
8639 }
8640 else
8641 IT_RESET_X_ASCENT_DESCENT (it);
8642
8643 if (wrap_it.sp >= 0)
8644 {
8645 RESTORE_IT (it, &wrap_it, wrap_data);
8646 atpos_it.sp = -1;
8647 atx_it.sp = -1;
8648 }
8649
8650 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8651 IT_CHARPOS (*it)));
8652 result = MOVE_LINE_CONTINUED;
8653 break;
8654 }
8655
8656 if (BUFFER_POS_REACHED_P ())
8657 {
8658 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8659 goto buffer_pos_reached;
8660 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8661 {
8662 SAVE_IT (atpos_it, *it, atpos_data);
8663 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8664 }
8665 }
8666
8667 if (new_x > it->first_visible_x)
8668 {
8669 /* Glyph is visible. Increment number of glyphs that
8670 would be displayed. */
8671 ++it->hpos;
8672 }
8673 }
8674
8675 if (result != MOVE_UNDEFINED)
8676 break;
8677 }
8678 else if (BUFFER_POS_REACHED_P ())
8679 {
8680 buffer_pos_reached:
8681 IT_RESET_X_ASCENT_DESCENT (it);
8682 result = MOVE_POS_MATCH_OR_ZV;
8683 break;
8684 }
8685 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8686 {
8687 /* Stop when TO_X specified and reached. This check is
8688 necessary here because of lines consisting of a line end,
8689 only. The line end will not produce any glyphs and we
8690 would never get MOVE_X_REACHED. */
8691 eassert (it->nglyphs == 0);
8692 result = MOVE_X_REACHED;
8693 break;
8694 }
8695
8696 /* Is this a line end? If yes, we're done. */
8697 if (ITERATOR_AT_END_OF_LINE_P (it))
8698 {
8699 /* If we are past TO_CHARPOS, but never saw any character
8700 positions smaller than TO_CHARPOS, return
8701 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8702 did. */
8703 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8704 {
8705 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8706 {
8707 if (IT_CHARPOS (ppos_it) < ZV)
8708 {
8709 RESTORE_IT (it, &ppos_it, ppos_data);
8710 result = MOVE_POS_MATCH_OR_ZV;
8711 }
8712 else
8713 goto buffer_pos_reached;
8714 }
8715 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8716 && IT_CHARPOS (*it) > to_charpos)
8717 goto buffer_pos_reached;
8718 else
8719 result = MOVE_NEWLINE_OR_CR;
8720 }
8721 else
8722 result = MOVE_NEWLINE_OR_CR;
8723 break;
8724 }
8725
8726 prev_method = it->method;
8727 if (it->method == GET_FROM_BUFFER)
8728 prev_pos = IT_CHARPOS (*it);
8729 /* The current display element has been consumed. Advance
8730 to the next. */
8731 set_iterator_to_next (it, 1);
8732 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8733 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8734 if (IT_CHARPOS (*it) < to_charpos)
8735 saw_smaller_pos = 1;
8736 if (it->bidi_p
8737 && (op & MOVE_TO_POS)
8738 && IT_CHARPOS (*it) >= to_charpos
8739 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8740 SAVE_IT (ppos_it, *it, ppos_data);
8741
8742 /* Stop if lines are truncated and IT's current x-position is
8743 past the right edge of the window now. */
8744 if (it->line_wrap == TRUNCATE
8745 && it->current_x >= it->last_visible_x)
8746 {
8747 if (!FRAME_WINDOW_P (it->f)
8748 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8749 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8750 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8751 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8752 {
8753 int at_eob_p = 0;
8754
8755 if ((at_eob_p = !get_next_display_element (it))
8756 || BUFFER_POS_REACHED_P ()
8757 /* If we are past TO_CHARPOS, but never saw any
8758 character positions smaller than TO_CHARPOS,
8759 return MOVE_POS_MATCH_OR_ZV, like the
8760 unidirectional display did. */
8761 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8762 && !saw_smaller_pos
8763 && IT_CHARPOS (*it) > to_charpos))
8764 {
8765 if (it->bidi_p
8766 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8767 RESTORE_IT (it, &ppos_it, ppos_data);
8768 result = MOVE_POS_MATCH_OR_ZV;
8769 break;
8770 }
8771 if (ITERATOR_AT_END_OF_LINE_P (it))
8772 {
8773 result = MOVE_NEWLINE_OR_CR;
8774 break;
8775 }
8776 }
8777 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8778 && !saw_smaller_pos
8779 && IT_CHARPOS (*it) > to_charpos)
8780 {
8781 if (IT_CHARPOS (ppos_it) < ZV)
8782 RESTORE_IT (it, &ppos_it, ppos_data);
8783 result = MOVE_POS_MATCH_OR_ZV;
8784 break;
8785 }
8786 result = MOVE_LINE_TRUNCATED;
8787 break;
8788 }
8789 #undef IT_RESET_X_ASCENT_DESCENT
8790 }
8791
8792 #undef BUFFER_POS_REACHED_P
8793
8794 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8795 restore the saved iterator. */
8796 if (atpos_it.sp >= 0)
8797 RESTORE_IT (it, &atpos_it, atpos_data);
8798 else if (atx_it.sp >= 0)
8799 RESTORE_IT (it, &atx_it, atx_data);
8800
8801 done:
8802
8803 if (atpos_data)
8804 bidi_unshelve_cache (atpos_data, 1);
8805 if (atx_data)
8806 bidi_unshelve_cache (atx_data, 1);
8807 if (wrap_data)
8808 bidi_unshelve_cache (wrap_data, 1);
8809 if (ppos_data)
8810 bidi_unshelve_cache (ppos_data, 1);
8811
8812 /* Restore the iterator settings altered at the beginning of this
8813 function. */
8814 it->glyph_row = saved_glyph_row;
8815 return result;
8816 }
8817
8818 /* For external use. */
8819 void
8820 move_it_in_display_line (struct it *it,
8821 ptrdiff_t to_charpos, int to_x,
8822 enum move_operation_enum op)
8823 {
8824 if (it->line_wrap == WORD_WRAP
8825 && (op & MOVE_TO_X))
8826 {
8827 struct it save_it;
8828 void *save_data = NULL;
8829 int skip;
8830
8831 SAVE_IT (save_it, *it, save_data);
8832 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8833 /* When word-wrap is on, TO_X may lie past the end
8834 of a wrapped line. Then it->current is the
8835 character on the next line, so backtrack to the
8836 space before the wrap point. */
8837 if (skip == MOVE_LINE_CONTINUED)
8838 {
8839 int prev_x = max (it->current_x - 1, 0);
8840 RESTORE_IT (it, &save_it, save_data);
8841 move_it_in_display_line_to
8842 (it, -1, prev_x, MOVE_TO_X);
8843 }
8844 else
8845 bidi_unshelve_cache (save_data, 1);
8846 }
8847 else
8848 move_it_in_display_line_to (it, to_charpos, to_x, op);
8849 }
8850
8851
8852 /* Move IT forward until it satisfies one or more of the criteria in
8853 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8854
8855 OP is a bit-mask that specifies where to stop, and in particular,
8856 which of those four position arguments makes a difference. See the
8857 description of enum move_operation_enum.
8858
8859 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8860 screen line, this function will set IT to the next position that is
8861 displayed to the right of TO_CHARPOS on the screen. */
8862
8863 void
8864 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8865 {
8866 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8867 int line_height, line_start_x = 0, reached = 0;
8868 void *backup_data = NULL;
8869
8870 for (;;)
8871 {
8872 if (op & MOVE_TO_VPOS)
8873 {
8874 /* If no TO_CHARPOS and no TO_X specified, stop at the
8875 start of the line TO_VPOS. */
8876 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8877 {
8878 if (it->vpos == to_vpos)
8879 {
8880 reached = 1;
8881 break;
8882 }
8883 else
8884 skip = move_it_in_display_line_to (it, -1, -1, 0);
8885 }
8886 else
8887 {
8888 /* TO_VPOS >= 0 means stop at TO_X in the line at
8889 TO_VPOS, or at TO_POS, whichever comes first. */
8890 if (it->vpos == to_vpos)
8891 {
8892 reached = 2;
8893 break;
8894 }
8895
8896 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8897
8898 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8899 {
8900 reached = 3;
8901 break;
8902 }
8903 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8904 {
8905 /* We have reached TO_X but not in the line we want. */
8906 skip = move_it_in_display_line_to (it, to_charpos,
8907 -1, MOVE_TO_POS);
8908 if (skip == MOVE_POS_MATCH_OR_ZV)
8909 {
8910 reached = 4;
8911 break;
8912 }
8913 }
8914 }
8915 }
8916 else if (op & MOVE_TO_Y)
8917 {
8918 struct it it_backup;
8919
8920 if (it->line_wrap == WORD_WRAP)
8921 SAVE_IT (it_backup, *it, backup_data);
8922
8923 /* TO_Y specified means stop at TO_X in the line containing
8924 TO_Y---or at TO_CHARPOS if this is reached first. The
8925 problem is that we can't really tell whether the line
8926 contains TO_Y before we have completely scanned it, and
8927 this may skip past TO_X. What we do is to first scan to
8928 TO_X.
8929
8930 If TO_X is not specified, use a TO_X of zero. The reason
8931 is to make the outcome of this function more predictable.
8932 If we didn't use TO_X == 0, we would stop at the end of
8933 the line which is probably not what a caller would expect
8934 to happen. */
8935 skip = move_it_in_display_line_to
8936 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8937 (MOVE_TO_X | (op & MOVE_TO_POS)));
8938
8939 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8940 if (skip == MOVE_POS_MATCH_OR_ZV)
8941 reached = 5;
8942 else if (skip == MOVE_X_REACHED)
8943 {
8944 /* If TO_X was reached, we want to know whether TO_Y is
8945 in the line. We know this is the case if the already
8946 scanned glyphs make the line tall enough. Otherwise,
8947 we must check by scanning the rest of the line. */
8948 line_height = it->max_ascent + it->max_descent;
8949 if (to_y >= it->current_y
8950 && to_y < it->current_y + line_height)
8951 {
8952 reached = 6;
8953 break;
8954 }
8955 SAVE_IT (it_backup, *it, backup_data);
8956 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8957 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8958 op & MOVE_TO_POS);
8959 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8960 line_height = it->max_ascent + it->max_descent;
8961 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8962
8963 if (to_y >= it->current_y
8964 && to_y < it->current_y + line_height)
8965 {
8966 /* If TO_Y is in this line and TO_X was reached
8967 above, we scanned too far. We have to restore
8968 IT's settings to the ones before skipping. But
8969 keep the more accurate values of max_ascent and
8970 max_descent we've found while skipping the rest
8971 of the line, for the sake of callers, such as
8972 pos_visible_p, that need to know the line
8973 height. */
8974 int max_ascent = it->max_ascent;
8975 int max_descent = it->max_descent;
8976
8977 RESTORE_IT (it, &it_backup, backup_data);
8978 it->max_ascent = max_ascent;
8979 it->max_descent = max_descent;
8980 reached = 6;
8981 }
8982 else
8983 {
8984 skip = skip2;
8985 if (skip == MOVE_POS_MATCH_OR_ZV)
8986 reached = 7;
8987 }
8988 }
8989 else
8990 {
8991 /* Check whether TO_Y is in this line. */
8992 line_height = it->max_ascent + it->max_descent;
8993 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8994
8995 if (to_y >= it->current_y
8996 && to_y < it->current_y + line_height)
8997 {
8998 /* When word-wrap is on, TO_X may lie past the end
8999 of a wrapped line. Then it->current is the
9000 character on the next line, so backtrack to the
9001 space before the wrap point. */
9002 if (skip == MOVE_LINE_CONTINUED
9003 && it->line_wrap == WORD_WRAP)
9004 {
9005 int prev_x = max (it->current_x - 1, 0);
9006 RESTORE_IT (it, &it_backup, backup_data);
9007 skip = move_it_in_display_line_to
9008 (it, -1, prev_x, MOVE_TO_X);
9009 }
9010 reached = 6;
9011 }
9012 }
9013
9014 if (reached)
9015 break;
9016 }
9017 else if (BUFFERP (it->object)
9018 && (it->method == GET_FROM_BUFFER
9019 || it->method == GET_FROM_STRETCH)
9020 && IT_CHARPOS (*it) >= to_charpos
9021 /* Under bidi iteration, a call to set_iterator_to_next
9022 can scan far beyond to_charpos if the initial
9023 portion of the next line needs to be reordered. In
9024 that case, give move_it_in_display_line_to another
9025 chance below. */
9026 && !(it->bidi_p
9027 && it->bidi_it.scan_dir == -1))
9028 skip = MOVE_POS_MATCH_OR_ZV;
9029 else
9030 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9031
9032 switch (skip)
9033 {
9034 case MOVE_POS_MATCH_OR_ZV:
9035 reached = 8;
9036 goto out;
9037
9038 case MOVE_NEWLINE_OR_CR:
9039 set_iterator_to_next (it, 1);
9040 it->continuation_lines_width = 0;
9041 break;
9042
9043 case MOVE_LINE_TRUNCATED:
9044 it->continuation_lines_width = 0;
9045 reseat_at_next_visible_line_start (it, 0);
9046 if ((op & MOVE_TO_POS) != 0
9047 && IT_CHARPOS (*it) > to_charpos)
9048 {
9049 reached = 9;
9050 goto out;
9051 }
9052 break;
9053
9054 case MOVE_LINE_CONTINUED:
9055 /* For continued lines ending in a tab, some of the glyphs
9056 associated with the tab are displayed on the current
9057 line. Since it->current_x does not include these glyphs,
9058 we use it->last_visible_x instead. */
9059 if (it->c == '\t')
9060 {
9061 it->continuation_lines_width += it->last_visible_x;
9062 /* When moving by vpos, ensure that the iterator really
9063 advances to the next line (bug#847, bug#969). Fixme:
9064 do we need to do this in other circumstances? */
9065 if (it->current_x != it->last_visible_x
9066 && (op & MOVE_TO_VPOS)
9067 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9068 {
9069 line_start_x = it->current_x + it->pixel_width
9070 - it->last_visible_x;
9071 set_iterator_to_next (it, 0);
9072 }
9073 }
9074 else
9075 it->continuation_lines_width += it->current_x;
9076 break;
9077
9078 default:
9079 emacs_abort ();
9080 }
9081
9082 /* Reset/increment for the next run. */
9083 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9084 it->current_x = line_start_x;
9085 line_start_x = 0;
9086 it->hpos = 0;
9087 it->current_y += it->max_ascent + it->max_descent;
9088 ++it->vpos;
9089 last_height = it->max_ascent + it->max_descent;
9090 it->max_ascent = it->max_descent = 0;
9091 }
9092
9093 out:
9094
9095 /* On text terminals, we may stop at the end of a line in the middle
9096 of a multi-character glyph. If the glyph itself is continued,
9097 i.e. it is actually displayed on the next line, don't treat this
9098 stopping point as valid; move to the next line instead (unless
9099 that brings us offscreen). */
9100 if (!FRAME_WINDOW_P (it->f)
9101 && op & MOVE_TO_POS
9102 && IT_CHARPOS (*it) == to_charpos
9103 && it->what == IT_CHARACTER
9104 && it->nglyphs > 1
9105 && it->line_wrap == WINDOW_WRAP
9106 && it->current_x == it->last_visible_x - 1
9107 && it->c != '\n'
9108 && it->c != '\t'
9109 && it->vpos < it->w->window_end_vpos)
9110 {
9111 it->continuation_lines_width += it->current_x;
9112 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9113 it->current_y += it->max_ascent + it->max_descent;
9114 ++it->vpos;
9115 last_height = it->max_ascent + it->max_descent;
9116 }
9117
9118 if (backup_data)
9119 bidi_unshelve_cache (backup_data, 1);
9120
9121 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9122 }
9123
9124
9125 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9126
9127 If DY > 0, move IT backward at least that many pixels. DY = 0
9128 means move IT backward to the preceding line start or BEGV. This
9129 function may move over more than DY pixels if IT->current_y - DY
9130 ends up in the middle of a line; in this case IT->current_y will be
9131 set to the top of the line moved to. */
9132
9133 void
9134 move_it_vertically_backward (struct it *it, int dy)
9135 {
9136 int nlines, h;
9137 struct it it2, it3;
9138 void *it2data = NULL, *it3data = NULL;
9139 ptrdiff_t start_pos;
9140 int nchars_per_row
9141 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9142 ptrdiff_t pos_limit;
9143
9144 move_further_back:
9145 eassert (dy >= 0);
9146
9147 start_pos = IT_CHARPOS (*it);
9148
9149 /* Estimate how many newlines we must move back. */
9150 nlines = max (1, dy / default_line_pixel_height (it->w));
9151 if (it->line_wrap == TRUNCATE)
9152 pos_limit = BEGV;
9153 else
9154 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9155
9156 /* Set the iterator's position that many lines back. But don't go
9157 back more than NLINES full screen lines -- this wins a day with
9158 buffers which have very long lines. */
9159 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9160 back_to_previous_visible_line_start (it);
9161
9162 /* Reseat the iterator here. When moving backward, we don't want
9163 reseat to skip forward over invisible text, set up the iterator
9164 to deliver from overlay strings at the new position etc. So,
9165 use reseat_1 here. */
9166 reseat_1 (it, it->current.pos, 1);
9167
9168 /* We are now surely at a line start. */
9169 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9170 reordering is in effect. */
9171 it->continuation_lines_width = 0;
9172
9173 /* Move forward and see what y-distance we moved. First move to the
9174 start of the next line so that we get its height. We need this
9175 height to be able to tell whether we reached the specified
9176 y-distance. */
9177 SAVE_IT (it2, *it, it2data);
9178 it2.max_ascent = it2.max_descent = 0;
9179 do
9180 {
9181 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9182 MOVE_TO_POS | MOVE_TO_VPOS);
9183 }
9184 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9185 /* If we are in a display string which starts at START_POS,
9186 and that display string includes a newline, and we are
9187 right after that newline (i.e. at the beginning of a
9188 display line), exit the loop, because otherwise we will
9189 infloop, since move_it_to will see that it is already at
9190 START_POS and will not move. */
9191 || (it2.method == GET_FROM_STRING
9192 && IT_CHARPOS (it2) == start_pos
9193 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9194 eassert (IT_CHARPOS (*it) >= BEGV);
9195 SAVE_IT (it3, it2, it3data);
9196
9197 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9198 eassert (IT_CHARPOS (*it) >= BEGV);
9199 /* H is the actual vertical distance from the position in *IT
9200 and the starting position. */
9201 h = it2.current_y - it->current_y;
9202 /* NLINES is the distance in number of lines. */
9203 nlines = it2.vpos - it->vpos;
9204
9205 /* Correct IT's y and vpos position
9206 so that they are relative to the starting point. */
9207 it->vpos -= nlines;
9208 it->current_y -= h;
9209
9210 if (dy == 0)
9211 {
9212 /* DY == 0 means move to the start of the screen line. The
9213 value of nlines is > 0 if continuation lines were involved,
9214 or if the original IT position was at start of a line. */
9215 RESTORE_IT (it, it, it2data);
9216 if (nlines > 0)
9217 move_it_by_lines (it, nlines);
9218 /* The above code moves us to some position NLINES down,
9219 usually to its first glyph (leftmost in an L2R line), but
9220 that's not necessarily the start of the line, under bidi
9221 reordering. We want to get to the character position
9222 that is immediately after the newline of the previous
9223 line. */
9224 if (it->bidi_p
9225 && !it->continuation_lines_width
9226 && !STRINGP (it->string)
9227 && IT_CHARPOS (*it) > BEGV
9228 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9229 {
9230 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9231
9232 DEC_BOTH (cp, bp);
9233 cp = find_newline_no_quit (cp, bp, -1, NULL);
9234 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9235 }
9236 bidi_unshelve_cache (it3data, 1);
9237 }
9238 else
9239 {
9240 /* The y-position we try to reach, relative to *IT.
9241 Note that H has been subtracted in front of the if-statement. */
9242 int target_y = it->current_y + h - dy;
9243 int y0 = it3.current_y;
9244 int y1;
9245 int line_height;
9246
9247 RESTORE_IT (&it3, &it3, it3data);
9248 y1 = line_bottom_y (&it3);
9249 line_height = y1 - y0;
9250 RESTORE_IT (it, it, it2data);
9251 /* If we did not reach target_y, try to move further backward if
9252 we can. If we moved too far backward, try to move forward. */
9253 if (target_y < it->current_y
9254 /* This is heuristic. In a window that's 3 lines high, with
9255 a line height of 13 pixels each, recentering with point
9256 on the bottom line will try to move -39/2 = 19 pixels
9257 backward. Try to avoid moving into the first line. */
9258 && (it->current_y - target_y
9259 > min (window_box_height (it->w), line_height * 2 / 3))
9260 && IT_CHARPOS (*it) > BEGV)
9261 {
9262 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9263 target_y - it->current_y));
9264 dy = it->current_y - target_y;
9265 goto move_further_back;
9266 }
9267 else if (target_y >= it->current_y + line_height
9268 && IT_CHARPOS (*it) < ZV)
9269 {
9270 /* Should move forward by at least one line, maybe more.
9271
9272 Note: Calling move_it_by_lines can be expensive on
9273 terminal frames, where compute_motion is used (via
9274 vmotion) to do the job, when there are very long lines
9275 and truncate-lines is nil. That's the reason for
9276 treating terminal frames specially here. */
9277
9278 if (!FRAME_WINDOW_P (it->f))
9279 move_it_vertically (it, target_y - (it->current_y + line_height));
9280 else
9281 {
9282 do
9283 {
9284 move_it_by_lines (it, 1);
9285 }
9286 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9287 }
9288 }
9289 }
9290 }
9291
9292
9293 /* Move IT by a specified amount of pixel lines DY. DY negative means
9294 move backwards. DY = 0 means move to start of screen line. At the
9295 end, IT will be on the start of a screen line. */
9296
9297 void
9298 move_it_vertically (struct it *it, int dy)
9299 {
9300 if (dy <= 0)
9301 move_it_vertically_backward (it, -dy);
9302 else
9303 {
9304 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9305 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9306 MOVE_TO_POS | MOVE_TO_Y);
9307 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9308
9309 /* If buffer ends in ZV without a newline, move to the start of
9310 the line to satisfy the post-condition. */
9311 if (IT_CHARPOS (*it) == ZV
9312 && ZV > BEGV
9313 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9314 move_it_by_lines (it, 0);
9315 }
9316 }
9317
9318
9319 /* Move iterator IT past the end of the text line it is in. */
9320
9321 void
9322 move_it_past_eol (struct it *it)
9323 {
9324 enum move_it_result rc;
9325
9326 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9327 if (rc == MOVE_NEWLINE_OR_CR)
9328 set_iterator_to_next (it, 0);
9329 }
9330
9331
9332 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9333 negative means move up. DVPOS == 0 means move to the start of the
9334 screen line.
9335
9336 Optimization idea: If we would know that IT->f doesn't use
9337 a face with proportional font, we could be faster for
9338 truncate-lines nil. */
9339
9340 void
9341 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9342 {
9343
9344 /* The commented-out optimization uses vmotion on terminals. This
9345 gives bad results, because elements like it->what, on which
9346 callers such as pos_visible_p rely, aren't updated. */
9347 /* struct position pos;
9348 if (!FRAME_WINDOW_P (it->f))
9349 {
9350 struct text_pos textpos;
9351
9352 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9353 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9354 reseat (it, textpos, 1);
9355 it->vpos += pos.vpos;
9356 it->current_y += pos.vpos;
9357 }
9358 else */
9359
9360 if (dvpos == 0)
9361 {
9362 /* DVPOS == 0 means move to the start of the screen line. */
9363 move_it_vertically_backward (it, 0);
9364 /* Let next call to line_bottom_y calculate real line height */
9365 last_height = 0;
9366 }
9367 else if (dvpos > 0)
9368 {
9369 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9370 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9371 {
9372 /* Only move to the next buffer position if we ended up in a
9373 string from display property, not in an overlay string
9374 (before-string or after-string). That is because the
9375 latter don't conceal the underlying buffer position, so
9376 we can ask to move the iterator to the exact position we
9377 are interested in. Note that, even if we are already at
9378 IT_CHARPOS (*it), the call below is not a no-op, as it
9379 will detect that we are at the end of the string, pop the
9380 iterator, and compute it->current_x and it->hpos
9381 correctly. */
9382 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9383 -1, -1, -1, MOVE_TO_POS);
9384 }
9385 }
9386 else
9387 {
9388 struct it it2;
9389 void *it2data = NULL;
9390 ptrdiff_t start_charpos, i;
9391 int nchars_per_row
9392 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9393 ptrdiff_t pos_limit;
9394
9395 /* Start at the beginning of the screen line containing IT's
9396 position. This may actually move vertically backwards,
9397 in case of overlays, so adjust dvpos accordingly. */
9398 dvpos += it->vpos;
9399 move_it_vertically_backward (it, 0);
9400 dvpos -= it->vpos;
9401
9402 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9403 screen lines, and reseat the iterator there. */
9404 start_charpos = IT_CHARPOS (*it);
9405 if (it->line_wrap == TRUNCATE)
9406 pos_limit = BEGV;
9407 else
9408 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9409 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9410 back_to_previous_visible_line_start (it);
9411 reseat (it, it->current.pos, 1);
9412
9413 /* Move further back if we end up in a string or an image. */
9414 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9415 {
9416 /* First try to move to start of display line. */
9417 dvpos += it->vpos;
9418 move_it_vertically_backward (it, 0);
9419 dvpos -= it->vpos;
9420 if (IT_POS_VALID_AFTER_MOVE_P (it))
9421 break;
9422 /* If start of line is still in string or image,
9423 move further back. */
9424 back_to_previous_visible_line_start (it);
9425 reseat (it, it->current.pos, 1);
9426 dvpos--;
9427 }
9428
9429 it->current_x = it->hpos = 0;
9430
9431 /* Above call may have moved too far if continuation lines
9432 are involved. Scan forward and see if it did. */
9433 SAVE_IT (it2, *it, it2data);
9434 it2.vpos = it2.current_y = 0;
9435 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9436 it->vpos -= it2.vpos;
9437 it->current_y -= it2.current_y;
9438 it->current_x = it->hpos = 0;
9439
9440 /* If we moved too far back, move IT some lines forward. */
9441 if (it2.vpos > -dvpos)
9442 {
9443 int delta = it2.vpos + dvpos;
9444
9445 RESTORE_IT (&it2, &it2, it2data);
9446 SAVE_IT (it2, *it, it2data);
9447 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9448 /* Move back again if we got too far ahead. */
9449 if (IT_CHARPOS (*it) >= start_charpos)
9450 RESTORE_IT (it, &it2, it2data);
9451 else
9452 bidi_unshelve_cache (it2data, 1);
9453 }
9454 else
9455 RESTORE_IT (it, it, it2data);
9456 }
9457 }
9458
9459 /* Return 1 if IT points into the middle of a display vector. */
9460
9461 int
9462 in_display_vector_p (struct it *it)
9463 {
9464 return (it->method == GET_FROM_DISPLAY_VECTOR
9465 && it->current.dpvec_index > 0
9466 && it->dpvec + it->current.dpvec_index != it->dpend);
9467 }
9468
9469 \f
9470 /***********************************************************************
9471 Messages
9472 ***********************************************************************/
9473
9474
9475 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9476 to *Messages*. */
9477
9478 void
9479 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9480 {
9481 Lisp_Object args[3];
9482 Lisp_Object msg, fmt;
9483 char *buffer;
9484 ptrdiff_t len;
9485 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9486 USE_SAFE_ALLOCA;
9487
9488 fmt = msg = Qnil;
9489 GCPRO4 (fmt, msg, arg1, arg2);
9490
9491 args[0] = fmt = build_string (format);
9492 args[1] = arg1;
9493 args[2] = arg2;
9494 msg = Fformat (3, args);
9495
9496 len = SBYTES (msg) + 1;
9497 buffer = SAFE_ALLOCA (len);
9498 memcpy (buffer, SDATA (msg), len);
9499
9500 message_dolog (buffer, len - 1, 1, 0);
9501 SAFE_FREE ();
9502
9503 UNGCPRO;
9504 }
9505
9506
9507 /* Output a newline in the *Messages* buffer if "needs" one. */
9508
9509 void
9510 message_log_maybe_newline (void)
9511 {
9512 if (message_log_need_newline)
9513 message_dolog ("", 0, 1, 0);
9514 }
9515
9516
9517 /* Add a string M of length NBYTES to the message log, optionally
9518 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9519 true, means interpret the contents of M as multibyte. This
9520 function calls low-level routines in order to bypass text property
9521 hooks, etc. which might not be safe to run.
9522
9523 This may GC (insert may run before/after change hooks),
9524 so the buffer M must NOT point to a Lisp string. */
9525
9526 void
9527 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9528 {
9529 const unsigned char *msg = (const unsigned char *) m;
9530
9531 if (!NILP (Vmemory_full))
9532 return;
9533
9534 if (!NILP (Vmessage_log_max))
9535 {
9536 struct buffer *oldbuf;
9537 Lisp_Object oldpoint, oldbegv, oldzv;
9538 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9539 ptrdiff_t point_at_end = 0;
9540 ptrdiff_t zv_at_end = 0;
9541 Lisp_Object old_deactivate_mark;
9542 bool shown;
9543 struct gcpro gcpro1;
9544
9545 old_deactivate_mark = Vdeactivate_mark;
9546 oldbuf = current_buffer;
9547 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9548 bset_undo_list (current_buffer, Qt);
9549
9550 oldpoint = message_dolog_marker1;
9551 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9552 oldbegv = message_dolog_marker2;
9553 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9554 oldzv = message_dolog_marker3;
9555 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9556 GCPRO1 (old_deactivate_mark);
9557
9558 if (PT == Z)
9559 point_at_end = 1;
9560 if (ZV == Z)
9561 zv_at_end = 1;
9562
9563 BEGV = BEG;
9564 BEGV_BYTE = BEG_BYTE;
9565 ZV = Z;
9566 ZV_BYTE = Z_BYTE;
9567 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9568
9569 /* Insert the string--maybe converting multibyte to single byte
9570 or vice versa, so that all the text fits the buffer. */
9571 if (multibyte
9572 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9573 {
9574 ptrdiff_t i;
9575 int c, char_bytes;
9576 char work[1];
9577
9578 /* Convert a multibyte string to single-byte
9579 for the *Message* buffer. */
9580 for (i = 0; i < nbytes; i += char_bytes)
9581 {
9582 c = string_char_and_length (msg + i, &char_bytes);
9583 work[0] = (ASCII_CHAR_P (c)
9584 ? c
9585 : multibyte_char_to_unibyte (c));
9586 insert_1_both (work, 1, 1, 1, 0, 0);
9587 }
9588 }
9589 else if (! multibyte
9590 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9591 {
9592 ptrdiff_t i;
9593 int c, char_bytes;
9594 unsigned char str[MAX_MULTIBYTE_LENGTH];
9595 /* Convert a single-byte string to multibyte
9596 for the *Message* buffer. */
9597 for (i = 0; i < nbytes; i++)
9598 {
9599 c = msg[i];
9600 MAKE_CHAR_MULTIBYTE (c);
9601 char_bytes = CHAR_STRING (c, str);
9602 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9603 }
9604 }
9605 else if (nbytes)
9606 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9607
9608 if (nlflag)
9609 {
9610 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9611 printmax_t dups;
9612
9613 insert_1_both ("\n", 1, 1, 1, 0, 0);
9614
9615 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9616 this_bol = PT;
9617 this_bol_byte = PT_BYTE;
9618
9619 /* See if this line duplicates the previous one.
9620 If so, combine duplicates. */
9621 if (this_bol > BEG)
9622 {
9623 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9624 prev_bol = PT;
9625 prev_bol_byte = PT_BYTE;
9626
9627 dups = message_log_check_duplicate (prev_bol_byte,
9628 this_bol_byte);
9629 if (dups)
9630 {
9631 del_range_both (prev_bol, prev_bol_byte,
9632 this_bol, this_bol_byte, 0);
9633 if (dups > 1)
9634 {
9635 char dupstr[sizeof " [ times]"
9636 + INT_STRLEN_BOUND (printmax_t)];
9637
9638 /* If you change this format, don't forget to also
9639 change message_log_check_duplicate. */
9640 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9641 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9642 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9643 }
9644 }
9645 }
9646
9647 /* If we have more than the desired maximum number of lines
9648 in the *Messages* buffer now, delete the oldest ones.
9649 This is safe because we don't have undo in this buffer. */
9650
9651 if (NATNUMP (Vmessage_log_max))
9652 {
9653 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9654 -XFASTINT (Vmessage_log_max) - 1, 0);
9655 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9656 }
9657 }
9658 BEGV = marker_position (oldbegv);
9659 BEGV_BYTE = marker_byte_position (oldbegv);
9660
9661 if (zv_at_end)
9662 {
9663 ZV = Z;
9664 ZV_BYTE = Z_BYTE;
9665 }
9666 else
9667 {
9668 ZV = marker_position (oldzv);
9669 ZV_BYTE = marker_byte_position (oldzv);
9670 }
9671
9672 if (point_at_end)
9673 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9674 else
9675 /* We can't do Fgoto_char (oldpoint) because it will run some
9676 Lisp code. */
9677 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9678 marker_byte_position (oldpoint));
9679
9680 UNGCPRO;
9681 unchain_marker (XMARKER (oldpoint));
9682 unchain_marker (XMARKER (oldbegv));
9683 unchain_marker (XMARKER (oldzv));
9684
9685 shown = buffer_window_count (current_buffer) > 0;
9686 set_buffer_internal (oldbuf);
9687 /* We called insert_1_both above with its 5th argument (PREPARE)
9688 zero, which prevents insert_1_both from calling
9689 prepare_to_modify_buffer, which in turns prevents us from
9690 incrementing windows_or_buffers_changed even if *Messages* is
9691 shown in some window. So we must manually incrementing
9692 windows_or_buffers_changed here to make up for that. */
9693 if (shown)
9694 windows_or_buffers_changed++;
9695 else
9696 windows_or_buffers_changed = old_windows_or_buffers_changed;
9697 message_log_need_newline = !nlflag;
9698 Vdeactivate_mark = old_deactivate_mark;
9699 }
9700 }
9701
9702
9703 /* We are at the end of the buffer after just having inserted a newline.
9704 (Note: We depend on the fact we won't be crossing the gap.)
9705 Check to see if the most recent message looks a lot like the previous one.
9706 Return 0 if different, 1 if the new one should just replace it, or a
9707 value N > 1 if we should also append " [N times]". */
9708
9709 static intmax_t
9710 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9711 {
9712 ptrdiff_t i;
9713 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9714 int seen_dots = 0;
9715 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9716 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9717
9718 for (i = 0; i < len; i++)
9719 {
9720 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
9721 seen_dots = 1;
9722 if (p1[i] != p2[i])
9723 return seen_dots;
9724 }
9725 p1 += len;
9726 if (*p1 == '\n')
9727 return 2;
9728 if (*p1++ == ' ' && *p1++ == '[')
9729 {
9730 char *pend;
9731 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9732 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9733 return n + 1;
9734 }
9735 return 0;
9736 }
9737 \f
9738
9739 /* Display an echo area message M with a specified length of NBYTES
9740 bytes. The string may include null characters. If M is not a
9741 string, clear out any existing message, and let the mini-buffer
9742 text show through.
9743
9744 This function cancels echoing. */
9745
9746 void
9747 message3 (Lisp_Object m)
9748 {
9749 struct gcpro gcpro1;
9750
9751 GCPRO1 (m);
9752 clear_message (1,1);
9753 cancel_echoing ();
9754
9755 /* First flush out any partial line written with print. */
9756 message_log_maybe_newline ();
9757 if (STRINGP (m))
9758 {
9759 ptrdiff_t nbytes = SBYTES (m);
9760 bool multibyte = STRING_MULTIBYTE (m);
9761 USE_SAFE_ALLOCA;
9762 char *buffer = SAFE_ALLOCA (nbytes);
9763 memcpy (buffer, SDATA (m), nbytes);
9764 message_dolog (buffer, nbytes, 1, multibyte);
9765 SAFE_FREE ();
9766 }
9767 message3_nolog (m);
9768
9769 UNGCPRO;
9770 }
9771
9772
9773 /* The non-logging version of message3.
9774 This does not cancel echoing, because it is used for echoing.
9775 Perhaps we need to make a separate function for echoing
9776 and make this cancel echoing. */
9777
9778 void
9779 message3_nolog (Lisp_Object m)
9780 {
9781 struct frame *sf = SELECTED_FRAME ();
9782
9783 if (FRAME_INITIAL_P (sf))
9784 {
9785 if (noninteractive_need_newline)
9786 putc ('\n', stderr);
9787 noninteractive_need_newline = 0;
9788 if (STRINGP (m))
9789 fwrite (SDATA (m), SBYTES (m), 1, stderr);
9790 if (cursor_in_echo_area == 0)
9791 fprintf (stderr, "\n");
9792 fflush (stderr);
9793 }
9794 /* Error messages get reported properly by cmd_error, so this must be just an
9795 informative message; if the frame hasn't really been initialized yet, just
9796 toss it. */
9797 else if (INTERACTIVE && sf->glyphs_initialized_p)
9798 {
9799 /* Get the frame containing the mini-buffer
9800 that the selected frame is using. */
9801 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
9802 Lisp_Object frame = XWINDOW (mini_window)->frame;
9803 struct frame *f = XFRAME (frame);
9804
9805 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
9806 Fmake_frame_visible (frame);
9807
9808 if (STRINGP (m) && SCHARS (m) > 0)
9809 {
9810 set_message (m);
9811 if (minibuffer_auto_raise)
9812 Fraise_frame (frame);
9813 /* Assume we are not echoing.
9814 (If we are, echo_now will override this.) */
9815 echo_message_buffer = Qnil;
9816 }
9817 else
9818 clear_message (1, 1);
9819
9820 do_pending_window_change (0);
9821 echo_area_display (1);
9822 do_pending_window_change (0);
9823 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9824 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9825 }
9826 }
9827
9828
9829 /* Display a null-terminated echo area message M. If M is 0, clear
9830 out any existing message, and let the mini-buffer text show through.
9831
9832 The buffer M must continue to exist until after the echo area gets
9833 cleared or some other message gets displayed there. Do not pass
9834 text that is stored in a Lisp string. Do not pass text in a buffer
9835 that was alloca'd. */
9836
9837 void
9838 message1 (const char *m)
9839 {
9840 message3 (m ? build_unibyte_string (m) : Qnil);
9841 }
9842
9843
9844 /* The non-logging counterpart of message1. */
9845
9846 void
9847 message1_nolog (const char *m)
9848 {
9849 message3_nolog (m ? build_unibyte_string (m) : Qnil);
9850 }
9851
9852 /* Display a message M which contains a single %s
9853 which gets replaced with STRING. */
9854
9855 void
9856 message_with_string (const char *m, Lisp_Object string, int log)
9857 {
9858 CHECK_STRING (string);
9859
9860 if (noninteractive)
9861 {
9862 if (m)
9863 {
9864 if (noninteractive_need_newline)
9865 putc ('\n', stderr);
9866 noninteractive_need_newline = 0;
9867 fprintf (stderr, m, SDATA (string));
9868 if (!cursor_in_echo_area)
9869 fprintf (stderr, "\n");
9870 fflush (stderr);
9871 }
9872 }
9873 else if (INTERACTIVE)
9874 {
9875 /* The frame whose minibuffer we're going to display the message on.
9876 It may be larger than the selected frame, so we need
9877 to use its buffer, not the selected frame's buffer. */
9878 Lisp_Object mini_window;
9879 struct frame *f, *sf = SELECTED_FRAME ();
9880
9881 /* Get the frame containing the minibuffer
9882 that the selected frame is using. */
9883 mini_window = FRAME_MINIBUF_WINDOW (sf);
9884 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9885
9886 /* Error messages get reported properly by cmd_error, so this must be
9887 just an informative message; if the frame hasn't really been
9888 initialized yet, just toss it. */
9889 if (f->glyphs_initialized_p)
9890 {
9891 Lisp_Object args[2], msg;
9892 struct gcpro gcpro1, gcpro2;
9893
9894 args[0] = build_string (m);
9895 args[1] = msg = string;
9896 GCPRO2 (args[0], msg);
9897 gcpro1.nvars = 2;
9898
9899 msg = Fformat (2, args);
9900
9901 if (log)
9902 message3 (msg);
9903 else
9904 message3_nolog (msg);
9905
9906 UNGCPRO;
9907
9908 /* Print should start at the beginning of the message
9909 buffer next time. */
9910 message_buf_print = 0;
9911 }
9912 }
9913 }
9914
9915
9916 /* Dump an informative message to the minibuf. If M is 0, clear out
9917 any existing message, and let the mini-buffer text show through. */
9918
9919 static void
9920 vmessage (const char *m, va_list ap)
9921 {
9922 if (noninteractive)
9923 {
9924 if (m)
9925 {
9926 if (noninteractive_need_newline)
9927 putc ('\n', stderr);
9928 noninteractive_need_newline = 0;
9929 vfprintf (stderr, m, ap);
9930 if (cursor_in_echo_area == 0)
9931 fprintf (stderr, "\n");
9932 fflush (stderr);
9933 }
9934 }
9935 else if (INTERACTIVE)
9936 {
9937 /* The frame whose mini-buffer we're going to display the message
9938 on. It may be larger than the selected frame, so we need to
9939 use its buffer, not the selected frame's buffer. */
9940 Lisp_Object mini_window;
9941 struct frame *f, *sf = SELECTED_FRAME ();
9942
9943 /* Get the frame containing the mini-buffer
9944 that the selected frame is using. */
9945 mini_window = FRAME_MINIBUF_WINDOW (sf);
9946 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9947
9948 /* Error messages get reported properly by cmd_error, so this must be
9949 just an informative message; if the frame hasn't really been
9950 initialized yet, just toss it. */
9951 if (f->glyphs_initialized_p)
9952 {
9953 if (m)
9954 {
9955 ptrdiff_t len;
9956 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
9957 char *message_buf = alloca (maxsize + 1);
9958
9959 len = doprnt (message_buf, maxsize, m, 0, ap);
9960
9961 message3 (make_string (message_buf, len));
9962 }
9963 else
9964 message1 (0);
9965
9966 /* Print should start at the beginning of the message
9967 buffer next time. */
9968 message_buf_print = 0;
9969 }
9970 }
9971 }
9972
9973 void
9974 message (const char *m, ...)
9975 {
9976 va_list ap;
9977 va_start (ap, m);
9978 vmessage (m, ap);
9979 va_end (ap);
9980 }
9981
9982
9983 #if 0
9984 /* The non-logging version of message. */
9985
9986 void
9987 message_nolog (const char *m, ...)
9988 {
9989 Lisp_Object old_log_max;
9990 va_list ap;
9991 va_start (ap, m);
9992 old_log_max = Vmessage_log_max;
9993 Vmessage_log_max = Qnil;
9994 vmessage (m, ap);
9995 Vmessage_log_max = old_log_max;
9996 va_end (ap);
9997 }
9998 #endif
9999
10000
10001 /* Display the current message in the current mini-buffer. This is
10002 only called from error handlers in process.c, and is not time
10003 critical. */
10004
10005 void
10006 update_echo_area (void)
10007 {
10008 if (!NILP (echo_area_buffer[0]))
10009 {
10010 Lisp_Object string;
10011 string = Fcurrent_message ();
10012 message3 (string);
10013 }
10014 }
10015
10016
10017 /* Make sure echo area buffers in `echo_buffers' are live.
10018 If they aren't, make new ones. */
10019
10020 static void
10021 ensure_echo_area_buffers (void)
10022 {
10023 int i;
10024
10025 for (i = 0; i < 2; ++i)
10026 if (!BUFFERP (echo_buffer[i])
10027 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10028 {
10029 char name[30];
10030 Lisp_Object old_buffer;
10031 int j;
10032
10033 old_buffer = echo_buffer[i];
10034 echo_buffer[i] = Fget_buffer_create
10035 (make_formatted_string (name, " *Echo Area %d*", i));
10036 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10037 /* to force word wrap in echo area -
10038 it was decided to postpone this*/
10039 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10040
10041 for (j = 0; j < 2; ++j)
10042 if (EQ (old_buffer, echo_area_buffer[j]))
10043 echo_area_buffer[j] = echo_buffer[i];
10044 }
10045 }
10046
10047
10048 /* Call FN with args A1..A2 with either the current or last displayed
10049 echo_area_buffer as current buffer.
10050
10051 WHICH zero means use the current message buffer
10052 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10053 from echo_buffer[] and clear it.
10054
10055 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10056 suitable buffer from echo_buffer[] and clear it.
10057
10058 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10059 that the current message becomes the last displayed one, make
10060 choose a suitable buffer for echo_area_buffer[0], and clear it.
10061
10062 Value is what FN returns. */
10063
10064 static int
10065 with_echo_area_buffer (struct window *w, int which,
10066 int (*fn) (ptrdiff_t, Lisp_Object),
10067 ptrdiff_t a1, Lisp_Object a2)
10068 {
10069 Lisp_Object buffer;
10070 int this_one, the_other, clear_buffer_p, rc;
10071 ptrdiff_t count = SPECPDL_INDEX ();
10072
10073 /* If buffers aren't live, make new ones. */
10074 ensure_echo_area_buffers ();
10075
10076 clear_buffer_p = 0;
10077
10078 if (which == 0)
10079 this_one = 0, the_other = 1;
10080 else if (which > 0)
10081 this_one = 1, the_other = 0;
10082 else
10083 {
10084 this_one = 0, the_other = 1;
10085 clear_buffer_p = 1;
10086
10087 /* We need a fresh one in case the current echo buffer equals
10088 the one containing the last displayed echo area message. */
10089 if (!NILP (echo_area_buffer[this_one])
10090 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10091 echo_area_buffer[this_one] = Qnil;
10092 }
10093
10094 /* Choose a suitable buffer from echo_buffer[] is we don't
10095 have one. */
10096 if (NILP (echo_area_buffer[this_one]))
10097 {
10098 echo_area_buffer[this_one]
10099 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10100 ? echo_buffer[the_other]
10101 : echo_buffer[this_one]);
10102 clear_buffer_p = 1;
10103 }
10104
10105 buffer = echo_area_buffer[this_one];
10106
10107 /* Don't get confused by reusing the buffer used for echoing
10108 for a different purpose. */
10109 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10110 cancel_echoing ();
10111
10112 record_unwind_protect (unwind_with_echo_area_buffer,
10113 with_echo_area_buffer_unwind_data (w));
10114
10115 /* Make the echo area buffer current. Note that for display
10116 purposes, it is not necessary that the displayed window's buffer
10117 == current_buffer, except for text property lookup. So, let's
10118 only set that buffer temporarily here without doing a full
10119 Fset_window_buffer. We must also change w->pointm, though,
10120 because otherwise an assertions in unshow_buffer fails, and Emacs
10121 aborts. */
10122 set_buffer_internal_1 (XBUFFER (buffer));
10123 if (w)
10124 {
10125 wset_buffer (w, buffer);
10126 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10127 }
10128
10129 bset_undo_list (current_buffer, Qt);
10130 bset_read_only (current_buffer, Qnil);
10131 specbind (Qinhibit_read_only, Qt);
10132 specbind (Qinhibit_modification_hooks, Qt);
10133
10134 if (clear_buffer_p && Z > BEG)
10135 del_range (BEG, Z);
10136
10137 eassert (BEGV >= BEG);
10138 eassert (ZV <= Z && ZV >= BEGV);
10139
10140 rc = fn (a1, a2);
10141
10142 eassert (BEGV >= BEG);
10143 eassert (ZV <= Z && ZV >= BEGV);
10144
10145 unbind_to (count, Qnil);
10146 return rc;
10147 }
10148
10149
10150 /* Save state that should be preserved around the call to the function
10151 FN called in with_echo_area_buffer. */
10152
10153 static Lisp_Object
10154 with_echo_area_buffer_unwind_data (struct window *w)
10155 {
10156 int i = 0;
10157 Lisp_Object vector, tmp;
10158
10159 /* Reduce consing by keeping one vector in
10160 Vwith_echo_area_save_vector. */
10161 vector = Vwith_echo_area_save_vector;
10162 Vwith_echo_area_save_vector = Qnil;
10163
10164 if (NILP (vector))
10165 vector = Fmake_vector (make_number (9), Qnil);
10166
10167 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10168 ASET (vector, i, Vdeactivate_mark); ++i;
10169 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10170
10171 if (w)
10172 {
10173 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10174 ASET (vector, i, w->contents); ++i;
10175 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10176 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10177 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10178 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10179 }
10180 else
10181 {
10182 int end = i + 6;
10183 for (; i < end; ++i)
10184 ASET (vector, i, Qnil);
10185 }
10186
10187 eassert (i == ASIZE (vector));
10188 return vector;
10189 }
10190
10191
10192 /* Restore global state from VECTOR which was created by
10193 with_echo_area_buffer_unwind_data. */
10194
10195 static void
10196 unwind_with_echo_area_buffer (Lisp_Object vector)
10197 {
10198 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10199 Vdeactivate_mark = AREF (vector, 1);
10200 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10201
10202 if (WINDOWP (AREF (vector, 3)))
10203 {
10204 struct window *w;
10205 Lisp_Object buffer;
10206
10207 w = XWINDOW (AREF (vector, 3));
10208 buffer = AREF (vector, 4);
10209
10210 wset_buffer (w, buffer);
10211 set_marker_both (w->pointm, buffer,
10212 XFASTINT (AREF (vector, 5)),
10213 XFASTINT (AREF (vector, 6)));
10214 set_marker_both (w->start, buffer,
10215 XFASTINT (AREF (vector, 7)),
10216 XFASTINT (AREF (vector, 8)));
10217 }
10218
10219 Vwith_echo_area_save_vector = vector;
10220 }
10221
10222
10223 /* Set up the echo area for use by print functions. MULTIBYTE_P
10224 non-zero means we will print multibyte. */
10225
10226 void
10227 setup_echo_area_for_printing (int multibyte_p)
10228 {
10229 /* If we can't find an echo area any more, exit. */
10230 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10231 Fkill_emacs (Qnil);
10232
10233 ensure_echo_area_buffers ();
10234
10235 if (!message_buf_print)
10236 {
10237 /* A message has been output since the last time we printed.
10238 Choose a fresh echo area buffer. */
10239 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10240 echo_area_buffer[0] = echo_buffer[1];
10241 else
10242 echo_area_buffer[0] = echo_buffer[0];
10243
10244 /* Switch to that buffer and clear it. */
10245 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10246 bset_truncate_lines (current_buffer, Qnil);
10247
10248 if (Z > BEG)
10249 {
10250 ptrdiff_t count = SPECPDL_INDEX ();
10251 specbind (Qinhibit_read_only, Qt);
10252 /* Note that undo recording is always disabled. */
10253 del_range (BEG, Z);
10254 unbind_to (count, Qnil);
10255 }
10256 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10257
10258 /* Set up the buffer for the multibyteness we need. */
10259 if (multibyte_p
10260 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10261 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10262
10263 /* Raise the frame containing the echo area. */
10264 if (minibuffer_auto_raise)
10265 {
10266 struct frame *sf = SELECTED_FRAME ();
10267 Lisp_Object mini_window;
10268 mini_window = FRAME_MINIBUF_WINDOW (sf);
10269 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10270 }
10271
10272 message_log_maybe_newline ();
10273 message_buf_print = 1;
10274 }
10275 else
10276 {
10277 if (NILP (echo_area_buffer[0]))
10278 {
10279 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10280 echo_area_buffer[0] = echo_buffer[1];
10281 else
10282 echo_area_buffer[0] = echo_buffer[0];
10283 }
10284
10285 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10286 {
10287 /* Someone switched buffers between print requests. */
10288 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10289 bset_truncate_lines (current_buffer, Qnil);
10290 }
10291 }
10292 }
10293
10294
10295 /* Display an echo area message in window W. Value is non-zero if W's
10296 height is changed. If display_last_displayed_message_p is
10297 non-zero, display the message that was last displayed, otherwise
10298 display the current message. */
10299
10300 static int
10301 display_echo_area (struct window *w)
10302 {
10303 int i, no_message_p, window_height_changed_p;
10304
10305 /* Temporarily disable garbage collections while displaying the echo
10306 area. This is done because a GC can print a message itself.
10307 That message would modify the echo area buffer's contents while a
10308 redisplay of the buffer is going on, and seriously confuse
10309 redisplay. */
10310 ptrdiff_t count = inhibit_garbage_collection ();
10311
10312 /* If there is no message, we must call display_echo_area_1
10313 nevertheless because it resizes the window. But we will have to
10314 reset the echo_area_buffer in question to nil at the end because
10315 with_echo_area_buffer will sets it to an empty buffer. */
10316 i = display_last_displayed_message_p ? 1 : 0;
10317 no_message_p = NILP (echo_area_buffer[i]);
10318
10319 window_height_changed_p
10320 = with_echo_area_buffer (w, display_last_displayed_message_p,
10321 display_echo_area_1,
10322 (intptr_t) w, Qnil);
10323
10324 if (no_message_p)
10325 echo_area_buffer[i] = Qnil;
10326
10327 unbind_to (count, Qnil);
10328 return window_height_changed_p;
10329 }
10330
10331
10332 /* Helper for display_echo_area. Display the current buffer which
10333 contains the current echo area message in window W, a mini-window,
10334 a pointer to which is passed in A1. A2..A4 are currently not used.
10335 Change the height of W so that all of the message is displayed.
10336 Value is non-zero if height of W was changed. */
10337
10338 static int
10339 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10340 {
10341 intptr_t i1 = a1;
10342 struct window *w = (struct window *) i1;
10343 Lisp_Object window;
10344 struct text_pos start;
10345 int window_height_changed_p = 0;
10346
10347 /* Do this before displaying, so that we have a large enough glyph
10348 matrix for the display. If we can't get enough space for the
10349 whole text, display the last N lines. That works by setting w->start. */
10350 window_height_changed_p = resize_mini_window (w, 0);
10351
10352 /* Use the starting position chosen by resize_mini_window. */
10353 SET_TEXT_POS_FROM_MARKER (start, w->start);
10354
10355 /* Display. */
10356 clear_glyph_matrix (w->desired_matrix);
10357 XSETWINDOW (window, w);
10358 try_window (window, start, 0);
10359
10360 return window_height_changed_p;
10361 }
10362
10363
10364 /* Resize the echo area window to exactly the size needed for the
10365 currently displayed message, if there is one. If a mini-buffer
10366 is active, don't shrink it. */
10367
10368 void
10369 resize_echo_area_exactly (void)
10370 {
10371 if (BUFFERP (echo_area_buffer[0])
10372 && WINDOWP (echo_area_window))
10373 {
10374 struct window *w = XWINDOW (echo_area_window);
10375 int resized_p;
10376 Lisp_Object resize_exactly;
10377
10378 if (minibuf_level == 0)
10379 resize_exactly = Qt;
10380 else
10381 resize_exactly = Qnil;
10382
10383 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10384 (intptr_t) w, resize_exactly);
10385 if (resized_p)
10386 {
10387 ++windows_or_buffers_changed;
10388 ++update_mode_lines;
10389 redisplay_internal ();
10390 }
10391 }
10392 }
10393
10394
10395 /* Callback function for with_echo_area_buffer, when used from
10396 resize_echo_area_exactly. A1 contains a pointer to the window to
10397 resize, EXACTLY non-nil means resize the mini-window exactly to the
10398 size of the text displayed. A3 and A4 are not used. Value is what
10399 resize_mini_window returns. */
10400
10401 static int
10402 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10403 {
10404 intptr_t i1 = a1;
10405 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10406 }
10407
10408
10409 /* Resize mini-window W to fit the size of its contents. EXACT_P
10410 means size the window exactly to the size needed. Otherwise, it's
10411 only enlarged until W's buffer is empty.
10412
10413 Set W->start to the right place to begin display. If the whole
10414 contents fit, start at the beginning. Otherwise, start so as
10415 to make the end of the contents appear. This is particularly
10416 important for y-or-n-p, but seems desirable generally.
10417
10418 Value is non-zero if the window height has been changed. */
10419
10420 int
10421 resize_mini_window (struct window *w, int exact_p)
10422 {
10423 struct frame *f = XFRAME (w->frame);
10424 int window_height_changed_p = 0;
10425
10426 eassert (MINI_WINDOW_P (w));
10427
10428 /* By default, start display at the beginning. */
10429 set_marker_both (w->start, w->contents,
10430 BUF_BEGV (XBUFFER (w->contents)),
10431 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10432
10433 /* Don't resize windows while redisplaying a window; it would
10434 confuse redisplay functions when the size of the window they are
10435 displaying changes from under them. Such a resizing can happen,
10436 for instance, when which-func prints a long message while
10437 we are running fontification-functions. We're running these
10438 functions with safe_call which binds inhibit-redisplay to t. */
10439 if (!NILP (Vinhibit_redisplay))
10440 return 0;
10441
10442 /* Nil means don't try to resize. */
10443 if (NILP (Vresize_mini_windows)
10444 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10445 return 0;
10446
10447 if (!FRAME_MINIBUF_ONLY_P (f))
10448 {
10449 struct it it;
10450 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10451 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10452 int height;
10453 EMACS_INT max_height;
10454 int unit = FRAME_LINE_HEIGHT (f);
10455 struct text_pos start;
10456 struct buffer *old_current_buffer = NULL;
10457
10458 if (current_buffer != XBUFFER (w->contents))
10459 {
10460 old_current_buffer = current_buffer;
10461 set_buffer_internal (XBUFFER (w->contents));
10462 }
10463
10464 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10465
10466 /* Compute the max. number of lines specified by the user. */
10467 if (FLOATP (Vmax_mini_window_height))
10468 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10469 else if (INTEGERP (Vmax_mini_window_height))
10470 max_height = XINT (Vmax_mini_window_height);
10471 else
10472 max_height = total_height / 4;
10473
10474 /* Correct that max. height if it's bogus. */
10475 max_height = clip_to_bounds (1, max_height, total_height);
10476
10477 /* Find out the height of the text in the window. */
10478 if (it.line_wrap == TRUNCATE)
10479 height = 1;
10480 else
10481 {
10482 last_height = 0;
10483 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10484 if (it.max_ascent == 0 && it.max_descent == 0)
10485 height = it.current_y + last_height;
10486 else
10487 height = it.current_y + it.max_ascent + it.max_descent;
10488 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10489 height = (height + unit - 1) / unit;
10490 }
10491
10492 /* Compute a suitable window start. */
10493 if (height > max_height)
10494 {
10495 height = max_height;
10496 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10497 move_it_vertically_backward (&it, (height - 1) * unit);
10498 start = it.current.pos;
10499 }
10500 else
10501 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10502 SET_MARKER_FROM_TEXT_POS (w->start, start);
10503
10504 if (EQ (Vresize_mini_windows, Qgrow_only))
10505 {
10506 /* Let it grow only, until we display an empty message, in which
10507 case the window shrinks again. */
10508 if (height > WINDOW_TOTAL_LINES (w))
10509 {
10510 int old_height = WINDOW_TOTAL_LINES (w);
10511
10512 FRAME_WINDOWS_FROZEN (f) = 1;
10513 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10514 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10515 }
10516 else if (height < WINDOW_TOTAL_LINES (w)
10517 && (exact_p || BEGV == ZV))
10518 {
10519 int old_height = WINDOW_TOTAL_LINES (w);
10520
10521 FRAME_WINDOWS_FROZEN (f) = 0;
10522 shrink_mini_window (w);
10523 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10524 }
10525 }
10526 else
10527 {
10528 /* Always resize to exact size needed. */
10529 if (height > WINDOW_TOTAL_LINES (w))
10530 {
10531 int old_height = WINDOW_TOTAL_LINES (w);
10532
10533 FRAME_WINDOWS_FROZEN (f) = 1;
10534 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10535 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10536 }
10537 else if (height < WINDOW_TOTAL_LINES (w))
10538 {
10539 int old_height = WINDOW_TOTAL_LINES (w);
10540
10541 FRAME_WINDOWS_FROZEN (f) = 0;
10542 shrink_mini_window (w);
10543
10544 if (height)
10545 {
10546 FRAME_WINDOWS_FROZEN (f) = 1;
10547 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10548 }
10549
10550 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10551 }
10552 }
10553
10554 if (old_current_buffer)
10555 set_buffer_internal (old_current_buffer);
10556 }
10557
10558 return window_height_changed_p;
10559 }
10560
10561
10562 /* Value is the current message, a string, or nil if there is no
10563 current message. */
10564
10565 Lisp_Object
10566 current_message (void)
10567 {
10568 Lisp_Object msg;
10569
10570 if (!BUFFERP (echo_area_buffer[0]))
10571 msg = Qnil;
10572 else
10573 {
10574 with_echo_area_buffer (0, 0, current_message_1,
10575 (intptr_t) &msg, Qnil);
10576 if (NILP (msg))
10577 echo_area_buffer[0] = Qnil;
10578 }
10579
10580 return msg;
10581 }
10582
10583
10584 static int
10585 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10586 {
10587 intptr_t i1 = a1;
10588 Lisp_Object *msg = (Lisp_Object *) i1;
10589
10590 if (Z > BEG)
10591 *msg = make_buffer_string (BEG, Z, 1);
10592 else
10593 *msg = Qnil;
10594 return 0;
10595 }
10596
10597
10598 /* Push the current message on Vmessage_stack for later restoration
10599 by restore_message. Value is non-zero if the current message isn't
10600 empty. This is a relatively infrequent operation, so it's not
10601 worth optimizing. */
10602
10603 bool
10604 push_message (void)
10605 {
10606 Lisp_Object msg = current_message ();
10607 Vmessage_stack = Fcons (msg, Vmessage_stack);
10608 return STRINGP (msg);
10609 }
10610
10611
10612 /* Restore message display from the top of Vmessage_stack. */
10613
10614 void
10615 restore_message (void)
10616 {
10617 eassert (CONSP (Vmessage_stack));
10618 message3_nolog (XCAR (Vmessage_stack));
10619 }
10620
10621
10622 /* Handler for unwind-protect calling pop_message. */
10623
10624 void
10625 pop_message_unwind (void)
10626 {
10627 /* Pop the top-most entry off Vmessage_stack. */
10628 eassert (CONSP (Vmessage_stack));
10629 Vmessage_stack = XCDR (Vmessage_stack);
10630 }
10631
10632
10633 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10634 exits. If the stack is not empty, we have a missing pop_message
10635 somewhere. */
10636
10637 void
10638 check_message_stack (void)
10639 {
10640 if (!NILP (Vmessage_stack))
10641 emacs_abort ();
10642 }
10643
10644
10645 /* Truncate to NCHARS what will be displayed in the echo area the next
10646 time we display it---but don't redisplay it now. */
10647
10648 void
10649 truncate_echo_area (ptrdiff_t nchars)
10650 {
10651 if (nchars == 0)
10652 echo_area_buffer[0] = Qnil;
10653 else if (!noninteractive
10654 && INTERACTIVE
10655 && !NILP (echo_area_buffer[0]))
10656 {
10657 struct frame *sf = SELECTED_FRAME ();
10658 /* Error messages get reported properly by cmd_error, so this must be
10659 just an informative message; if the frame hasn't really been
10660 initialized yet, just toss it. */
10661 if (sf->glyphs_initialized_p)
10662 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10663 }
10664 }
10665
10666
10667 /* Helper function for truncate_echo_area. Truncate the current
10668 message to at most NCHARS characters. */
10669
10670 static int
10671 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10672 {
10673 if (BEG + nchars < Z)
10674 del_range (BEG + nchars, Z);
10675 if (Z == BEG)
10676 echo_area_buffer[0] = Qnil;
10677 return 0;
10678 }
10679
10680 /* Set the current message to STRING. */
10681
10682 static void
10683 set_message (Lisp_Object string)
10684 {
10685 eassert (STRINGP (string));
10686
10687 message_enable_multibyte = STRING_MULTIBYTE (string);
10688
10689 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10690 message_buf_print = 0;
10691 help_echo_showing_p = 0;
10692
10693 if (STRINGP (Vdebug_on_message)
10694 && STRINGP (string)
10695 && fast_string_match (Vdebug_on_message, string) >= 0)
10696 call_debugger (list2 (Qerror, string));
10697 }
10698
10699
10700 /* Helper function for set_message. First argument is ignored and second
10701 argument has the same meaning as for set_message.
10702 This function is called with the echo area buffer being current. */
10703
10704 static int
10705 set_message_1 (ptrdiff_t a1, Lisp_Object string)
10706 {
10707 eassert (STRINGP (string));
10708
10709 /* Change multibyteness of the echo buffer appropriately. */
10710 if (message_enable_multibyte
10711 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10712 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10713
10714 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10715 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10716 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10717
10718 /* Insert new message at BEG. */
10719 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10720
10721 /* This function takes care of single/multibyte conversion.
10722 We just have to ensure that the echo area buffer has the right
10723 setting of enable_multibyte_characters. */
10724 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
10725
10726 return 0;
10727 }
10728
10729
10730 /* Clear messages. CURRENT_P non-zero means clear the current
10731 message. LAST_DISPLAYED_P non-zero means clear the message
10732 last displayed. */
10733
10734 void
10735 clear_message (int current_p, int last_displayed_p)
10736 {
10737 if (current_p)
10738 {
10739 echo_area_buffer[0] = Qnil;
10740 message_cleared_p = 1;
10741 }
10742
10743 if (last_displayed_p)
10744 echo_area_buffer[1] = Qnil;
10745
10746 message_buf_print = 0;
10747 }
10748
10749 /* Clear garbaged frames.
10750
10751 This function is used where the old redisplay called
10752 redraw_garbaged_frames which in turn called redraw_frame which in
10753 turn called clear_frame. The call to clear_frame was a source of
10754 flickering. I believe a clear_frame is not necessary. It should
10755 suffice in the new redisplay to invalidate all current matrices,
10756 and ensure a complete redisplay of all windows. */
10757
10758 static void
10759 clear_garbaged_frames (void)
10760 {
10761 if (frame_garbaged)
10762 {
10763 Lisp_Object tail, frame;
10764 int changed_count = 0;
10765
10766 FOR_EACH_FRAME (tail, frame)
10767 {
10768 struct frame *f = XFRAME (frame);
10769
10770 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10771 {
10772 if (f->resized_p)
10773 {
10774 redraw_frame (f);
10775 f->force_flush_display_p = 1;
10776 }
10777 clear_current_matrices (f);
10778 changed_count++;
10779 f->garbaged = 0;
10780 f->resized_p = 0;
10781 }
10782 }
10783
10784 frame_garbaged = 0;
10785 if (changed_count)
10786 ++windows_or_buffers_changed;
10787 }
10788 }
10789
10790
10791 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10792 is non-zero update selected_frame. Value is non-zero if the
10793 mini-windows height has been changed. */
10794
10795 static int
10796 echo_area_display (int update_frame_p)
10797 {
10798 Lisp_Object mini_window;
10799 struct window *w;
10800 struct frame *f;
10801 int window_height_changed_p = 0;
10802 struct frame *sf = SELECTED_FRAME ();
10803
10804 mini_window = FRAME_MINIBUF_WINDOW (sf);
10805 w = XWINDOW (mini_window);
10806 f = XFRAME (WINDOW_FRAME (w));
10807
10808 /* Don't display if frame is invisible or not yet initialized. */
10809 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10810 return 0;
10811
10812 #ifdef HAVE_WINDOW_SYSTEM
10813 /* When Emacs starts, selected_frame may be the initial terminal
10814 frame. If we let this through, a message would be displayed on
10815 the terminal. */
10816 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10817 return 0;
10818 #endif /* HAVE_WINDOW_SYSTEM */
10819
10820 /* Redraw garbaged frames. */
10821 clear_garbaged_frames ();
10822
10823 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10824 {
10825 echo_area_window = mini_window;
10826 window_height_changed_p = display_echo_area (w);
10827 w->must_be_updated_p = 1;
10828
10829 /* Update the display, unless called from redisplay_internal.
10830 Also don't update the screen during redisplay itself. The
10831 update will happen at the end of redisplay, and an update
10832 here could cause confusion. */
10833 if (update_frame_p && !redisplaying_p)
10834 {
10835 int n = 0;
10836
10837 /* If the display update has been interrupted by pending
10838 input, update mode lines in the frame. Due to the
10839 pending input, it might have been that redisplay hasn't
10840 been called, so that mode lines above the echo area are
10841 garbaged. This looks odd, so we prevent it here. */
10842 if (!display_completed)
10843 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10844
10845 if (window_height_changed_p
10846 /* Don't do this if Emacs is shutting down. Redisplay
10847 needs to run hooks. */
10848 && !NILP (Vrun_hooks))
10849 {
10850 /* Must update other windows. Likewise as in other
10851 cases, don't let this update be interrupted by
10852 pending input. */
10853 ptrdiff_t count = SPECPDL_INDEX ();
10854 specbind (Qredisplay_dont_pause, Qt);
10855 windows_or_buffers_changed = 1;
10856 redisplay_internal ();
10857 unbind_to (count, Qnil);
10858 }
10859 else if (FRAME_WINDOW_P (f) && n == 0)
10860 {
10861 /* Window configuration is the same as before.
10862 Can do with a display update of the echo area,
10863 unless we displayed some mode lines. */
10864 update_single_window (w, 1);
10865 FRAME_RIF (f)->flush_display (f);
10866 }
10867 else
10868 update_frame (f, 1, 1);
10869
10870 /* If cursor is in the echo area, make sure that the next
10871 redisplay displays the minibuffer, so that the cursor will
10872 be replaced with what the minibuffer wants. */
10873 if (cursor_in_echo_area)
10874 ++windows_or_buffers_changed;
10875 }
10876 }
10877 else if (!EQ (mini_window, selected_window))
10878 windows_or_buffers_changed++;
10879
10880 /* Last displayed message is now the current message. */
10881 echo_area_buffer[1] = echo_area_buffer[0];
10882 /* Inform read_char that we're not echoing. */
10883 echo_message_buffer = Qnil;
10884
10885 /* Prevent redisplay optimization in redisplay_internal by resetting
10886 this_line_start_pos. This is done because the mini-buffer now
10887 displays the message instead of its buffer text. */
10888 if (EQ (mini_window, selected_window))
10889 CHARPOS (this_line_start_pos) = 0;
10890
10891 return window_height_changed_p;
10892 }
10893
10894 /* Nonzero if the current window's buffer is shown in more than one
10895 window and was modified since last redisplay. */
10896
10897 static int
10898 buffer_shared_and_changed (void)
10899 {
10900 return (buffer_window_count (current_buffer) > 1
10901 && UNCHANGED_MODIFIED < MODIFF);
10902 }
10903
10904 /* Nonzero if W's buffer was changed but not saved or Transient Mark mode
10905 is enabled and mark of W's buffer was changed since last W's update. */
10906
10907 static int
10908 window_buffer_changed (struct window *w)
10909 {
10910 struct buffer *b = XBUFFER (w->contents);
10911
10912 eassert (BUFFER_LIVE_P (b));
10913
10914 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star)
10915 || ((!NILP (Vtransient_mark_mode) && !NILP (BVAR (b, mark_active)))
10916 != (w->region_showing != 0)));
10917 }
10918
10919 /* Nonzero if W has %c in its mode line and mode line should be updated. */
10920
10921 static int
10922 mode_line_update_needed (struct window *w)
10923 {
10924 return (w->column_number_displayed != -1
10925 && !(PT == w->last_point && !window_outdated (w))
10926 && (w->column_number_displayed != current_column ()));
10927 }
10928
10929 /* Nonzero if window start of W is frozen and may not be changed during
10930 redisplay. */
10931
10932 static bool
10933 window_frozen_p (struct window *w)
10934 {
10935 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
10936 {
10937 Lisp_Object window;
10938
10939 XSETWINDOW (window, w);
10940 if (MINI_WINDOW_P (w))
10941 return 0;
10942 else if (EQ (window, selected_window))
10943 return 0;
10944 else if (MINI_WINDOW_P (XWINDOW (selected_window))
10945 && EQ (window, Vminibuf_scroll_window))
10946 /* This special window can't be frozen too. */
10947 return 0;
10948 else
10949 return 1;
10950 }
10951 return 0;
10952 }
10953
10954 /***********************************************************************
10955 Mode Lines and Frame Titles
10956 ***********************************************************************/
10957
10958 /* A buffer for constructing non-propertized mode-line strings and
10959 frame titles in it; allocated from the heap in init_xdisp and
10960 resized as needed in store_mode_line_noprop_char. */
10961
10962 static char *mode_line_noprop_buf;
10963
10964 /* The buffer's end, and a current output position in it. */
10965
10966 static char *mode_line_noprop_buf_end;
10967 static char *mode_line_noprop_ptr;
10968
10969 #define MODE_LINE_NOPROP_LEN(start) \
10970 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10971
10972 static enum {
10973 MODE_LINE_DISPLAY = 0,
10974 MODE_LINE_TITLE,
10975 MODE_LINE_NOPROP,
10976 MODE_LINE_STRING
10977 } mode_line_target;
10978
10979 /* Alist that caches the results of :propertize.
10980 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10981 static Lisp_Object mode_line_proptrans_alist;
10982
10983 /* List of strings making up the mode-line. */
10984 static Lisp_Object mode_line_string_list;
10985
10986 /* Base face property when building propertized mode line string. */
10987 static Lisp_Object mode_line_string_face;
10988 static Lisp_Object mode_line_string_face_prop;
10989
10990
10991 /* Unwind data for mode line strings */
10992
10993 static Lisp_Object Vmode_line_unwind_vector;
10994
10995 static Lisp_Object
10996 format_mode_line_unwind_data (struct frame *target_frame,
10997 struct buffer *obuf,
10998 Lisp_Object owin,
10999 int save_proptrans)
11000 {
11001 Lisp_Object vector, tmp;
11002
11003 /* Reduce consing by keeping one vector in
11004 Vwith_echo_area_save_vector. */
11005 vector = Vmode_line_unwind_vector;
11006 Vmode_line_unwind_vector = Qnil;
11007
11008 if (NILP (vector))
11009 vector = Fmake_vector (make_number (10), Qnil);
11010
11011 ASET (vector, 0, make_number (mode_line_target));
11012 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11013 ASET (vector, 2, mode_line_string_list);
11014 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11015 ASET (vector, 4, mode_line_string_face);
11016 ASET (vector, 5, mode_line_string_face_prop);
11017
11018 if (obuf)
11019 XSETBUFFER (tmp, obuf);
11020 else
11021 tmp = Qnil;
11022 ASET (vector, 6, tmp);
11023 ASET (vector, 7, owin);
11024 if (target_frame)
11025 {
11026 /* Similarly to `with-selected-window', if the operation selects
11027 a window on another frame, we must restore that frame's
11028 selected window, and (for a tty) the top-frame. */
11029 ASET (vector, 8, target_frame->selected_window);
11030 if (FRAME_TERMCAP_P (target_frame))
11031 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11032 }
11033
11034 return vector;
11035 }
11036
11037 static void
11038 unwind_format_mode_line (Lisp_Object vector)
11039 {
11040 Lisp_Object old_window = AREF (vector, 7);
11041 Lisp_Object target_frame_window = AREF (vector, 8);
11042 Lisp_Object old_top_frame = AREF (vector, 9);
11043
11044 mode_line_target = XINT (AREF (vector, 0));
11045 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11046 mode_line_string_list = AREF (vector, 2);
11047 if (! EQ (AREF (vector, 3), Qt))
11048 mode_line_proptrans_alist = AREF (vector, 3);
11049 mode_line_string_face = AREF (vector, 4);
11050 mode_line_string_face_prop = AREF (vector, 5);
11051
11052 /* Select window before buffer, since it may change the buffer. */
11053 if (!NILP (old_window))
11054 {
11055 /* If the operation that we are unwinding had selected a window
11056 on a different frame, reset its frame-selected-window. For a
11057 text terminal, reset its top-frame if necessary. */
11058 if (!NILP (target_frame_window))
11059 {
11060 Lisp_Object frame
11061 = WINDOW_FRAME (XWINDOW (target_frame_window));
11062
11063 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11064 Fselect_window (target_frame_window, Qt);
11065
11066 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11067 Fselect_frame (old_top_frame, Qt);
11068 }
11069
11070 Fselect_window (old_window, Qt);
11071 }
11072
11073 if (!NILP (AREF (vector, 6)))
11074 {
11075 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11076 ASET (vector, 6, Qnil);
11077 }
11078
11079 Vmode_line_unwind_vector = vector;
11080 }
11081
11082
11083 /* Store a single character C for the frame title in mode_line_noprop_buf.
11084 Re-allocate mode_line_noprop_buf if necessary. */
11085
11086 static void
11087 store_mode_line_noprop_char (char c)
11088 {
11089 /* If output position has reached the end of the allocated buffer,
11090 increase the buffer's size. */
11091 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11092 {
11093 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11094 ptrdiff_t size = len;
11095 mode_line_noprop_buf =
11096 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11097 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11098 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11099 }
11100
11101 *mode_line_noprop_ptr++ = c;
11102 }
11103
11104
11105 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11106 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11107 characters that yield more columns than PRECISION; PRECISION <= 0
11108 means copy the whole string. Pad with spaces until FIELD_WIDTH
11109 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11110 pad. Called from display_mode_element when it is used to build a
11111 frame title. */
11112
11113 static int
11114 store_mode_line_noprop (const char *string, int field_width, int precision)
11115 {
11116 const unsigned char *str = (const unsigned char *) string;
11117 int n = 0;
11118 ptrdiff_t dummy, nbytes;
11119
11120 /* Copy at most PRECISION chars from STR. */
11121 nbytes = strlen (string);
11122 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11123 while (nbytes--)
11124 store_mode_line_noprop_char (*str++);
11125
11126 /* Fill up with spaces until FIELD_WIDTH reached. */
11127 while (field_width > 0
11128 && n < field_width)
11129 {
11130 store_mode_line_noprop_char (' ');
11131 ++n;
11132 }
11133
11134 return n;
11135 }
11136
11137 /***********************************************************************
11138 Frame Titles
11139 ***********************************************************************/
11140
11141 #ifdef HAVE_WINDOW_SYSTEM
11142
11143 /* Set the title of FRAME, if it has changed. The title format is
11144 Vicon_title_format if FRAME is iconified, otherwise it is
11145 frame_title_format. */
11146
11147 static void
11148 x_consider_frame_title (Lisp_Object frame)
11149 {
11150 struct frame *f = XFRAME (frame);
11151
11152 if (FRAME_WINDOW_P (f)
11153 || FRAME_MINIBUF_ONLY_P (f)
11154 || f->explicit_name)
11155 {
11156 /* Do we have more than one visible frame on this X display? */
11157 Lisp_Object tail, other_frame, fmt;
11158 ptrdiff_t title_start;
11159 char *title;
11160 ptrdiff_t len;
11161 struct it it;
11162 ptrdiff_t count = SPECPDL_INDEX ();
11163
11164 FOR_EACH_FRAME (tail, other_frame)
11165 {
11166 struct frame *tf = XFRAME (other_frame);
11167
11168 if (tf != f
11169 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11170 && !FRAME_MINIBUF_ONLY_P (tf)
11171 && !EQ (other_frame, tip_frame)
11172 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11173 break;
11174 }
11175
11176 /* Set global variable indicating that multiple frames exist. */
11177 multiple_frames = CONSP (tail);
11178
11179 /* Switch to the buffer of selected window of the frame. Set up
11180 mode_line_target so that display_mode_element will output into
11181 mode_line_noprop_buf; then display the title. */
11182 record_unwind_protect (unwind_format_mode_line,
11183 format_mode_line_unwind_data
11184 (f, current_buffer, selected_window, 0));
11185
11186 Fselect_window (f->selected_window, Qt);
11187 set_buffer_internal_1
11188 (XBUFFER (XWINDOW (f->selected_window)->contents));
11189 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11190
11191 mode_line_target = MODE_LINE_TITLE;
11192 title_start = MODE_LINE_NOPROP_LEN (0);
11193 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11194 NULL, DEFAULT_FACE_ID);
11195 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11196 len = MODE_LINE_NOPROP_LEN (title_start);
11197 title = mode_line_noprop_buf + title_start;
11198 unbind_to (count, Qnil);
11199
11200 /* Set the title only if it's changed. This avoids consing in
11201 the common case where it hasn't. (If it turns out that we've
11202 already wasted too much time by walking through the list with
11203 display_mode_element, then we might need to optimize at a
11204 higher level than this.) */
11205 if (! STRINGP (f->name)
11206 || SBYTES (f->name) != len
11207 || memcmp (title, SDATA (f->name), len) != 0)
11208 x_implicitly_set_name (f, make_string (title, len), Qnil);
11209 }
11210 }
11211
11212 #endif /* not HAVE_WINDOW_SYSTEM */
11213
11214 \f
11215 /***********************************************************************
11216 Menu Bars
11217 ***********************************************************************/
11218
11219
11220 /* Prepare for redisplay by updating menu-bar item lists when
11221 appropriate. This can call eval. */
11222
11223 void
11224 prepare_menu_bars (void)
11225 {
11226 int all_windows;
11227 struct gcpro gcpro1, gcpro2;
11228 struct frame *f;
11229 Lisp_Object tooltip_frame;
11230
11231 #ifdef HAVE_WINDOW_SYSTEM
11232 tooltip_frame = tip_frame;
11233 #else
11234 tooltip_frame = Qnil;
11235 #endif
11236
11237 /* Update all frame titles based on their buffer names, etc. We do
11238 this before the menu bars so that the buffer-menu will show the
11239 up-to-date frame titles. */
11240 #ifdef HAVE_WINDOW_SYSTEM
11241 if (windows_or_buffers_changed || update_mode_lines)
11242 {
11243 Lisp_Object tail, frame;
11244
11245 FOR_EACH_FRAME (tail, frame)
11246 {
11247 f = XFRAME (frame);
11248 if (!EQ (frame, tooltip_frame)
11249 && (FRAME_ICONIFIED_P (f)
11250 || FRAME_VISIBLE_P (f) == 1
11251 /* Exclude TTY frames that are obscured because they
11252 are not the top frame on their console. This is
11253 because x_consider_frame_title actually switches
11254 to the frame, which for TTY frames means it is
11255 marked as garbaged, and will be completely
11256 redrawn on the next redisplay cycle. This causes
11257 TTY frames to be completely redrawn, when there
11258 are more than one of them, even though nothing
11259 should be changed on display. */
11260 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11261 x_consider_frame_title (frame);
11262 }
11263 }
11264 #endif /* HAVE_WINDOW_SYSTEM */
11265
11266 /* Update the menu bar item lists, if appropriate. This has to be
11267 done before any actual redisplay or generation of display lines. */
11268 all_windows = (update_mode_lines
11269 || buffer_shared_and_changed ()
11270 || windows_or_buffers_changed);
11271 if (all_windows)
11272 {
11273 Lisp_Object tail, frame;
11274 ptrdiff_t count = SPECPDL_INDEX ();
11275 /* 1 means that update_menu_bar has run its hooks
11276 so any further calls to update_menu_bar shouldn't do so again. */
11277 int menu_bar_hooks_run = 0;
11278
11279 record_unwind_save_match_data ();
11280
11281 FOR_EACH_FRAME (tail, frame)
11282 {
11283 f = XFRAME (frame);
11284
11285 /* Ignore tooltip frame. */
11286 if (EQ (frame, tooltip_frame))
11287 continue;
11288
11289 /* If a window on this frame changed size, report that to
11290 the user and clear the size-change flag. */
11291 if (FRAME_WINDOW_SIZES_CHANGED (f))
11292 {
11293 Lisp_Object functions;
11294
11295 /* Clear flag first in case we get an error below. */
11296 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11297 functions = Vwindow_size_change_functions;
11298 GCPRO2 (tail, functions);
11299
11300 while (CONSP (functions))
11301 {
11302 if (!EQ (XCAR (functions), Qt))
11303 call1 (XCAR (functions), frame);
11304 functions = XCDR (functions);
11305 }
11306 UNGCPRO;
11307 }
11308
11309 GCPRO1 (tail);
11310 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11311 #ifdef HAVE_WINDOW_SYSTEM
11312 update_tool_bar (f, 0);
11313 #endif
11314 #ifdef HAVE_NS
11315 if (windows_or_buffers_changed
11316 && FRAME_NS_P (f))
11317 ns_set_doc_edited
11318 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->contents));
11319 #endif
11320 UNGCPRO;
11321 }
11322
11323 unbind_to (count, Qnil);
11324 }
11325 else
11326 {
11327 struct frame *sf = SELECTED_FRAME ();
11328 update_menu_bar (sf, 1, 0);
11329 #ifdef HAVE_WINDOW_SYSTEM
11330 update_tool_bar (sf, 1);
11331 #endif
11332 }
11333 }
11334
11335
11336 /* Update the menu bar item list for frame F. This has to be done
11337 before we start to fill in any display lines, because it can call
11338 eval.
11339
11340 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11341
11342 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11343 already ran the menu bar hooks for this redisplay, so there
11344 is no need to run them again. The return value is the
11345 updated value of this flag, to pass to the next call. */
11346
11347 static int
11348 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11349 {
11350 Lisp_Object window;
11351 register struct window *w;
11352
11353 /* If called recursively during a menu update, do nothing. This can
11354 happen when, for instance, an activate-menubar-hook causes a
11355 redisplay. */
11356 if (inhibit_menubar_update)
11357 return hooks_run;
11358
11359 window = FRAME_SELECTED_WINDOW (f);
11360 w = XWINDOW (window);
11361
11362 if (FRAME_WINDOW_P (f)
11363 ?
11364 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11365 || defined (HAVE_NS) || defined (USE_GTK)
11366 FRAME_EXTERNAL_MENU_BAR (f)
11367 #else
11368 FRAME_MENU_BAR_LINES (f) > 0
11369 #endif
11370 : FRAME_MENU_BAR_LINES (f) > 0)
11371 {
11372 /* If the user has switched buffers or windows, we need to
11373 recompute to reflect the new bindings. But we'll
11374 recompute when update_mode_lines is set too; that means
11375 that people can use force-mode-line-update to request
11376 that the menu bar be recomputed. The adverse effect on
11377 the rest of the redisplay algorithm is about the same as
11378 windows_or_buffers_changed anyway. */
11379 if (windows_or_buffers_changed
11380 /* This used to test w->update_mode_line, but we believe
11381 there is no need to recompute the menu in that case. */
11382 || update_mode_lines
11383 || window_buffer_changed (w))
11384 {
11385 struct buffer *prev = current_buffer;
11386 ptrdiff_t count = SPECPDL_INDEX ();
11387
11388 specbind (Qinhibit_menubar_update, Qt);
11389
11390 set_buffer_internal_1 (XBUFFER (w->contents));
11391 if (save_match_data)
11392 record_unwind_save_match_data ();
11393 if (NILP (Voverriding_local_map_menu_flag))
11394 {
11395 specbind (Qoverriding_terminal_local_map, Qnil);
11396 specbind (Qoverriding_local_map, Qnil);
11397 }
11398
11399 if (!hooks_run)
11400 {
11401 /* Run the Lucid hook. */
11402 safe_run_hooks (Qactivate_menubar_hook);
11403
11404 /* If it has changed current-menubar from previous value,
11405 really recompute the menu-bar from the value. */
11406 if (! NILP (Vlucid_menu_bar_dirty_flag))
11407 call0 (Qrecompute_lucid_menubar);
11408
11409 safe_run_hooks (Qmenu_bar_update_hook);
11410
11411 hooks_run = 1;
11412 }
11413
11414 XSETFRAME (Vmenu_updating_frame, f);
11415 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11416
11417 /* Redisplay the menu bar in case we changed it. */
11418 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11419 || defined (HAVE_NS) || defined (USE_GTK)
11420 if (FRAME_WINDOW_P (f))
11421 {
11422 #if defined (HAVE_NS)
11423 /* All frames on Mac OS share the same menubar. So only
11424 the selected frame should be allowed to set it. */
11425 if (f == SELECTED_FRAME ())
11426 #endif
11427 set_frame_menubar (f, 0, 0);
11428 }
11429 else
11430 /* On a terminal screen, the menu bar is an ordinary screen
11431 line, and this makes it get updated. */
11432 w->update_mode_line = 1;
11433 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11434 /* In the non-toolkit version, the menu bar is an ordinary screen
11435 line, and this makes it get updated. */
11436 w->update_mode_line = 1;
11437 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11438
11439 unbind_to (count, Qnil);
11440 set_buffer_internal_1 (prev);
11441 }
11442 }
11443
11444 return hooks_run;
11445 }
11446
11447 /***********************************************************************
11448 Tool-bars
11449 ***********************************************************************/
11450
11451 #ifdef HAVE_WINDOW_SYSTEM
11452
11453 /* Where the mouse was last time we reported a mouse event. */
11454
11455 struct frame *last_mouse_frame;
11456
11457 /* Tool-bar item index of the item on which a mouse button was pressed
11458 or -1. */
11459
11460 int last_tool_bar_item;
11461
11462 /* Select `frame' temporarily without running all the code in
11463 do_switch_frame.
11464 FIXME: Maybe do_switch_frame should be trimmed down similarly
11465 when `norecord' is set. */
11466 static void
11467 fast_set_selected_frame (Lisp_Object frame)
11468 {
11469 if (!EQ (selected_frame, frame))
11470 {
11471 selected_frame = frame;
11472 selected_window = XFRAME (frame)->selected_window;
11473 }
11474 }
11475
11476 /* Update the tool-bar item list for frame F. This has to be done
11477 before we start to fill in any display lines. Called from
11478 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11479 and restore it here. */
11480
11481 static void
11482 update_tool_bar (struct frame *f, int save_match_data)
11483 {
11484 #if defined (USE_GTK) || defined (HAVE_NS)
11485 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11486 #else
11487 int do_update = WINDOWP (f->tool_bar_window)
11488 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11489 #endif
11490
11491 if (do_update)
11492 {
11493 Lisp_Object window;
11494 struct window *w;
11495
11496 window = FRAME_SELECTED_WINDOW (f);
11497 w = XWINDOW (window);
11498
11499 /* If the user has switched buffers or windows, we need to
11500 recompute to reflect the new bindings. But we'll
11501 recompute when update_mode_lines is set too; that means
11502 that people can use force-mode-line-update to request
11503 that the menu bar be recomputed. The adverse effect on
11504 the rest of the redisplay algorithm is about the same as
11505 windows_or_buffers_changed anyway. */
11506 if (windows_or_buffers_changed
11507 || w->update_mode_line
11508 || update_mode_lines
11509 || window_buffer_changed (w))
11510 {
11511 struct buffer *prev = current_buffer;
11512 ptrdiff_t count = SPECPDL_INDEX ();
11513 Lisp_Object frame, new_tool_bar;
11514 int new_n_tool_bar;
11515 struct gcpro gcpro1;
11516
11517 /* Set current_buffer to the buffer of the selected
11518 window of the frame, so that we get the right local
11519 keymaps. */
11520 set_buffer_internal_1 (XBUFFER (w->contents));
11521
11522 /* Save match data, if we must. */
11523 if (save_match_data)
11524 record_unwind_save_match_data ();
11525
11526 /* Make sure that we don't accidentally use bogus keymaps. */
11527 if (NILP (Voverriding_local_map_menu_flag))
11528 {
11529 specbind (Qoverriding_terminal_local_map, Qnil);
11530 specbind (Qoverriding_local_map, Qnil);
11531 }
11532
11533 GCPRO1 (new_tool_bar);
11534
11535 /* We must temporarily set the selected frame to this frame
11536 before calling tool_bar_items, because the calculation of
11537 the tool-bar keymap uses the selected frame (see
11538 `tool-bar-make-keymap' in tool-bar.el). */
11539 eassert (EQ (selected_window,
11540 /* Since we only explicitly preserve selected_frame,
11541 check that selected_window would be redundant. */
11542 XFRAME (selected_frame)->selected_window));
11543 record_unwind_protect (fast_set_selected_frame, selected_frame);
11544 XSETFRAME (frame, f);
11545 fast_set_selected_frame (frame);
11546
11547 /* Build desired tool-bar items from keymaps. */
11548 new_tool_bar
11549 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11550 &new_n_tool_bar);
11551
11552 /* Redisplay the tool-bar if we changed it. */
11553 if (new_n_tool_bar != f->n_tool_bar_items
11554 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11555 {
11556 /* Redisplay that happens asynchronously due to an expose event
11557 may access f->tool_bar_items. Make sure we update both
11558 variables within BLOCK_INPUT so no such event interrupts. */
11559 block_input ();
11560 fset_tool_bar_items (f, new_tool_bar);
11561 f->n_tool_bar_items = new_n_tool_bar;
11562 w->update_mode_line = 1;
11563 unblock_input ();
11564 }
11565
11566 UNGCPRO;
11567
11568 unbind_to (count, Qnil);
11569 set_buffer_internal_1 (prev);
11570 }
11571 }
11572 }
11573
11574
11575 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11576 F's desired tool-bar contents. F->tool_bar_items must have
11577 been set up previously by calling prepare_menu_bars. */
11578
11579 static void
11580 build_desired_tool_bar_string (struct frame *f)
11581 {
11582 int i, size, size_needed;
11583 struct gcpro gcpro1, gcpro2, gcpro3;
11584 Lisp_Object image, plist, props;
11585
11586 image = plist = props = Qnil;
11587 GCPRO3 (image, plist, props);
11588
11589 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11590 Otherwise, make a new string. */
11591
11592 /* The size of the string we might be able to reuse. */
11593 size = (STRINGP (f->desired_tool_bar_string)
11594 ? SCHARS (f->desired_tool_bar_string)
11595 : 0);
11596
11597 /* We need one space in the string for each image. */
11598 size_needed = f->n_tool_bar_items;
11599
11600 /* Reuse f->desired_tool_bar_string, if possible. */
11601 if (size < size_needed || NILP (f->desired_tool_bar_string))
11602 fset_desired_tool_bar_string
11603 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11604 else
11605 {
11606 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11607 Fremove_text_properties (make_number (0), make_number (size),
11608 props, f->desired_tool_bar_string);
11609 }
11610
11611 /* Put a `display' property on the string for the images to display,
11612 put a `menu_item' property on tool-bar items with a value that
11613 is the index of the item in F's tool-bar item vector. */
11614 for (i = 0; i < f->n_tool_bar_items; ++i)
11615 {
11616 #define PROP(IDX) \
11617 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11618
11619 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11620 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11621 int hmargin, vmargin, relief, idx, end;
11622
11623 /* If image is a vector, choose the image according to the
11624 button state. */
11625 image = PROP (TOOL_BAR_ITEM_IMAGES);
11626 if (VECTORP (image))
11627 {
11628 if (enabled_p)
11629 idx = (selected_p
11630 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11631 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11632 else
11633 idx = (selected_p
11634 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11635 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11636
11637 eassert (ASIZE (image) >= idx);
11638 image = AREF (image, idx);
11639 }
11640 else
11641 idx = -1;
11642
11643 /* Ignore invalid image specifications. */
11644 if (!valid_image_p (image))
11645 continue;
11646
11647 /* Display the tool-bar button pressed, or depressed. */
11648 plist = Fcopy_sequence (XCDR (image));
11649
11650 /* Compute margin and relief to draw. */
11651 relief = (tool_bar_button_relief >= 0
11652 ? tool_bar_button_relief
11653 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11654 hmargin = vmargin = relief;
11655
11656 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11657 INT_MAX - max (hmargin, vmargin)))
11658 {
11659 hmargin += XFASTINT (Vtool_bar_button_margin);
11660 vmargin += XFASTINT (Vtool_bar_button_margin);
11661 }
11662 else if (CONSP (Vtool_bar_button_margin))
11663 {
11664 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11665 INT_MAX - hmargin))
11666 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11667
11668 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11669 INT_MAX - vmargin))
11670 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11671 }
11672
11673 if (auto_raise_tool_bar_buttons_p)
11674 {
11675 /* Add a `:relief' property to the image spec if the item is
11676 selected. */
11677 if (selected_p)
11678 {
11679 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11680 hmargin -= relief;
11681 vmargin -= relief;
11682 }
11683 }
11684 else
11685 {
11686 /* If image is selected, display it pressed, i.e. with a
11687 negative relief. If it's not selected, display it with a
11688 raised relief. */
11689 plist = Fplist_put (plist, QCrelief,
11690 (selected_p
11691 ? make_number (-relief)
11692 : make_number (relief)));
11693 hmargin -= relief;
11694 vmargin -= relief;
11695 }
11696
11697 /* Put a margin around the image. */
11698 if (hmargin || vmargin)
11699 {
11700 if (hmargin == vmargin)
11701 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11702 else
11703 plist = Fplist_put (plist, QCmargin,
11704 Fcons (make_number (hmargin),
11705 make_number (vmargin)));
11706 }
11707
11708 /* If button is not enabled, and we don't have special images
11709 for the disabled state, make the image appear disabled by
11710 applying an appropriate algorithm to it. */
11711 if (!enabled_p && idx < 0)
11712 plist = Fplist_put (plist, QCconversion, Qdisabled);
11713
11714 /* Put a `display' text property on the string for the image to
11715 display. Put a `menu-item' property on the string that gives
11716 the start of this item's properties in the tool-bar items
11717 vector. */
11718 image = Fcons (Qimage, plist);
11719 props = list4 (Qdisplay, image,
11720 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11721
11722 /* Let the last image hide all remaining spaces in the tool bar
11723 string. The string can be longer than needed when we reuse a
11724 previous string. */
11725 if (i + 1 == f->n_tool_bar_items)
11726 end = SCHARS (f->desired_tool_bar_string);
11727 else
11728 end = i + 1;
11729 Fadd_text_properties (make_number (i), make_number (end),
11730 props, f->desired_tool_bar_string);
11731 #undef PROP
11732 }
11733
11734 UNGCPRO;
11735 }
11736
11737
11738 /* Display one line of the tool-bar of frame IT->f.
11739
11740 HEIGHT specifies the desired height of the tool-bar line.
11741 If the actual height of the glyph row is less than HEIGHT, the
11742 row's height is increased to HEIGHT, and the icons are centered
11743 vertically in the new height.
11744
11745 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11746 count a final empty row in case the tool-bar width exactly matches
11747 the window width.
11748 */
11749
11750 static void
11751 display_tool_bar_line (struct it *it, int height)
11752 {
11753 struct glyph_row *row = it->glyph_row;
11754 int max_x = it->last_visible_x;
11755 struct glyph *last;
11756
11757 prepare_desired_row (row);
11758 row->y = it->current_y;
11759
11760 /* Note that this isn't made use of if the face hasn't a box,
11761 so there's no need to check the face here. */
11762 it->start_of_box_run_p = 1;
11763
11764 while (it->current_x < max_x)
11765 {
11766 int x, n_glyphs_before, i, nglyphs;
11767 struct it it_before;
11768
11769 /* Get the next display element. */
11770 if (!get_next_display_element (it))
11771 {
11772 /* Don't count empty row if we are counting needed tool-bar lines. */
11773 if (height < 0 && !it->hpos)
11774 return;
11775 break;
11776 }
11777
11778 /* Produce glyphs. */
11779 n_glyphs_before = row->used[TEXT_AREA];
11780 it_before = *it;
11781
11782 PRODUCE_GLYPHS (it);
11783
11784 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11785 i = 0;
11786 x = it_before.current_x;
11787 while (i < nglyphs)
11788 {
11789 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11790
11791 if (x + glyph->pixel_width > max_x)
11792 {
11793 /* Glyph doesn't fit on line. Backtrack. */
11794 row->used[TEXT_AREA] = n_glyphs_before;
11795 *it = it_before;
11796 /* If this is the only glyph on this line, it will never fit on the
11797 tool-bar, so skip it. But ensure there is at least one glyph,
11798 so we don't accidentally disable the tool-bar. */
11799 if (n_glyphs_before == 0
11800 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11801 break;
11802 goto out;
11803 }
11804
11805 ++it->hpos;
11806 x += glyph->pixel_width;
11807 ++i;
11808 }
11809
11810 /* Stop at line end. */
11811 if (ITERATOR_AT_END_OF_LINE_P (it))
11812 break;
11813
11814 set_iterator_to_next (it, 1);
11815 }
11816
11817 out:;
11818
11819 row->displays_text_p = row->used[TEXT_AREA] != 0;
11820
11821 /* Use default face for the border below the tool bar.
11822
11823 FIXME: When auto-resize-tool-bars is grow-only, there is
11824 no additional border below the possibly empty tool-bar lines.
11825 So to make the extra empty lines look "normal", we have to
11826 use the tool-bar face for the border too. */
11827 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
11828 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11829 it->face_id = DEFAULT_FACE_ID;
11830
11831 extend_face_to_end_of_line (it);
11832 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11833 last->right_box_line_p = 1;
11834 if (last == row->glyphs[TEXT_AREA])
11835 last->left_box_line_p = 1;
11836
11837 /* Make line the desired height and center it vertically. */
11838 if ((height -= it->max_ascent + it->max_descent) > 0)
11839 {
11840 /* Don't add more than one line height. */
11841 height %= FRAME_LINE_HEIGHT (it->f);
11842 it->max_ascent += height / 2;
11843 it->max_descent += (height + 1) / 2;
11844 }
11845
11846 compute_line_metrics (it);
11847
11848 /* If line is empty, make it occupy the rest of the tool-bar. */
11849 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
11850 {
11851 row->height = row->phys_height = it->last_visible_y - row->y;
11852 row->visible_height = row->height;
11853 row->ascent = row->phys_ascent = 0;
11854 row->extra_line_spacing = 0;
11855 }
11856
11857 row->full_width_p = 1;
11858 row->continued_p = 0;
11859 row->truncated_on_left_p = 0;
11860 row->truncated_on_right_p = 0;
11861
11862 it->current_x = it->hpos = 0;
11863 it->current_y += row->height;
11864 ++it->vpos;
11865 ++it->glyph_row;
11866 }
11867
11868
11869 /* Max tool-bar height. */
11870
11871 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11872 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11873
11874 /* Value is the number of screen lines needed to make all tool-bar
11875 items of frame F visible. The number of actual rows needed is
11876 returned in *N_ROWS if non-NULL. */
11877
11878 static int
11879 tool_bar_lines_needed (struct frame *f, int *n_rows)
11880 {
11881 struct window *w = XWINDOW (f->tool_bar_window);
11882 struct it it;
11883 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11884 the desired matrix, so use (unused) mode-line row as temporary row to
11885 avoid destroying the first tool-bar row. */
11886 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11887
11888 /* Initialize an iterator for iteration over
11889 F->desired_tool_bar_string in the tool-bar window of frame F. */
11890 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11891 it.first_visible_x = 0;
11892 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11893 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11894 it.paragraph_embedding = L2R;
11895
11896 while (!ITERATOR_AT_END_P (&it))
11897 {
11898 clear_glyph_row (temp_row);
11899 it.glyph_row = temp_row;
11900 display_tool_bar_line (&it, -1);
11901 }
11902 clear_glyph_row (temp_row);
11903
11904 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11905 if (n_rows)
11906 *n_rows = it.vpos > 0 ? it.vpos : -1;
11907
11908 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11909 }
11910
11911
11912 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11913 0, 1, 0,
11914 doc: /* Return the number of lines occupied by the tool bar of FRAME.
11915 If FRAME is nil or omitted, use the selected frame. */)
11916 (Lisp_Object frame)
11917 {
11918 struct frame *f = decode_any_frame (frame);
11919 struct window *w;
11920 int nlines = 0;
11921
11922 if (WINDOWP (f->tool_bar_window)
11923 && (w = XWINDOW (f->tool_bar_window),
11924 WINDOW_TOTAL_LINES (w) > 0))
11925 {
11926 update_tool_bar (f, 1);
11927 if (f->n_tool_bar_items)
11928 {
11929 build_desired_tool_bar_string (f);
11930 nlines = tool_bar_lines_needed (f, NULL);
11931 }
11932 }
11933
11934 return make_number (nlines);
11935 }
11936
11937
11938 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11939 height should be changed. */
11940
11941 static int
11942 redisplay_tool_bar (struct frame *f)
11943 {
11944 struct window *w;
11945 struct it it;
11946 struct glyph_row *row;
11947
11948 #if defined (USE_GTK) || defined (HAVE_NS)
11949 if (FRAME_EXTERNAL_TOOL_BAR (f))
11950 update_frame_tool_bar (f);
11951 return 0;
11952 #endif
11953
11954 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11955 do anything. This means you must start with tool-bar-lines
11956 non-zero to get the auto-sizing effect. Or in other words, you
11957 can turn off tool-bars by specifying tool-bar-lines zero. */
11958 if (!WINDOWP (f->tool_bar_window)
11959 || (w = XWINDOW (f->tool_bar_window),
11960 WINDOW_TOTAL_LINES (w) == 0))
11961 return 0;
11962
11963 /* Set up an iterator for the tool-bar window. */
11964 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11965 it.first_visible_x = 0;
11966 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11967 row = it.glyph_row;
11968
11969 /* Build a string that represents the contents of the tool-bar. */
11970 build_desired_tool_bar_string (f);
11971 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11972 /* FIXME: This should be controlled by a user option. But it
11973 doesn't make sense to have an R2L tool bar if the menu bar cannot
11974 be drawn also R2L, and making the menu bar R2L is tricky due
11975 toolkit-specific code that implements it. If an R2L tool bar is
11976 ever supported, display_tool_bar_line should also be augmented to
11977 call unproduce_glyphs like display_line and display_string
11978 do. */
11979 it.paragraph_embedding = L2R;
11980
11981 if (f->n_tool_bar_rows == 0)
11982 {
11983 int nlines;
11984
11985 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11986 nlines != WINDOW_TOTAL_LINES (w)))
11987 {
11988 Lisp_Object frame;
11989 int old_height = WINDOW_TOTAL_LINES (w);
11990
11991 XSETFRAME (frame, f);
11992 Fmodify_frame_parameters (frame,
11993 list1 (Fcons (Qtool_bar_lines,
11994 make_number (nlines))));
11995 if (WINDOW_TOTAL_LINES (w) != old_height)
11996 {
11997 clear_glyph_matrix (w->desired_matrix);
11998 fonts_changed_p = 1;
11999 return 1;
12000 }
12001 }
12002 }
12003
12004 /* Display as many lines as needed to display all tool-bar items. */
12005
12006 if (f->n_tool_bar_rows > 0)
12007 {
12008 int border, rows, height, extra;
12009
12010 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12011 border = XINT (Vtool_bar_border);
12012 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12013 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12014 else if (EQ (Vtool_bar_border, Qborder_width))
12015 border = f->border_width;
12016 else
12017 border = 0;
12018 if (border < 0)
12019 border = 0;
12020
12021 rows = f->n_tool_bar_rows;
12022 height = max (1, (it.last_visible_y - border) / rows);
12023 extra = it.last_visible_y - border - height * rows;
12024
12025 while (it.current_y < it.last_visible_y)
12026 {
12027 int h = 0;
12028 if (extra > 0 && rows-- > 0)
12029 {
12030 h = (extra + rows - 1) / rows;
12031 extra -= h;
12032 }
12033 display_tool_bar_line (&it, height + h);
12034 }
12035 }
12036 else
12037 {
12038 while (it.current_y < it.last_visible_y)
12039 display_tool_bar_line (&it, 0);
12040 }
12041
12042 /* It doesn't make much sense to try scrolling in the tool-bar
12043 window, so don't do it. */
12044 w->desired_matrix->no_scrolling_p = 1;
12045 w->must_be_updated_p = 1;
12046
12047 if (!NILP (Vauto_resize_tool_bars))
12048 {
12049 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12050 int change_height_p = 0;
12051
12052 /* If we couldn't display everything, change the tool-bar's
12053 height if there is room for more. */
12054 if (IT_STRING_CHARPOS (it) < it.end_charpos
12055 && it.current_y < max_tool_bar_height)
12056 change_height_p = 1;
12057
12058 row = it.glyph_row - 1;
12059
12060 /* If there are blank lines at the end, except for a partially
12061 visible blank line at the end that is smaller than
12062 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12063 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12064 && row->height >= FRAME_LINE_HEIGHT (f))
12065 change_height_p = 1;
12066
12067 /* If row displays tool-bar items, but is partially visible,
12068 change the tool-bar's height. */
12069 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12070 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12071 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12072 change_height_p = 1;
12073
12074 /* Resize windows as needed by changing the `tool-bar-lines'
12075 frame parameter. */
12076 if (change_height_p)
12077 {
12078 Lisp_Object frame;
12079 int old_height = WINDOW_TOTAL_LINES (w);
12080 int nrows;
12081 int nlines = tool_bar_lines_needed (f, &nrows);
12082
12083 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12084 && !f->minimize_tool_bar_window_p)
12085 ? (nlines > old_height)
12086 : (nlines != old_height));
12087 f->minimize_tool_bar_window_p = 0;
12088
12089 if (change_height_p)
12090 {
12091 XSETFRAME (frame, f);
12092 Fmodify_frame_parameters (frame,
12093 list1 (Fcons (Qtool_bar_lines,
12094 make_number (nlines))));
12095 if (WINDOW_TOTAL_LINES (w) != old_height)
12096 {
12097 clear_glyph_matrix (w->desired_matrix);
12098 f->n_tool_bar_rows = nrows;
12099 fonts_changed_p = 1;
12100 return 1;
12101 }
12102 }
12103 }
12104 }
12105
12106 f->minimize_tool_bar_window_p = 0;
12107 return 0;
12108 }
12109
12110
12111 /* Get information about the tool-bar item which is displayed in GLYPH
12112 on frame F. Return in *PROP_IDX the index where tool-bar item
12113 properties start in F->tool_bar_items. Value is zero if
12114 GLYPH doesn't display a tool-bar item. */
12115
12116 static int
12117 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12118 {
12119 Lisp_Object prop;
12120 int success_p;
12121 int charpos;
12122
12123 /* This function can be called asynchronously, which means we must
12124 exclude any possibility that Fget_text_property signals an
12125 error. */
12126 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12127 charpos = max (0, charpos);
12128
12129 /* Get the text property `menu-item' at pos. The value of that
12130 property is the start index of this item's properties in
12131 F->tool_bar_items. */
12132 prop = Fget_text_property (make_number (charpos),
12133 Qmenu_item, f->current_tool_bar_string);
12134 if (INTEGERP (prop))
12135 {
12136 *prop_idx = XINT (prop);
12137 success_p = 1;
12138 }
12139 else
12140 success_p = 0;
12141
12142 return success_p;
12143 }
12144
12145 \f
12146 /* Get information about the tool-bar item at position X/Y on frame F.
12147 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12148 the current matrix of the tool-bar window of F, or NULL if not
12149 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12150 item in F->tool_bar_items. Value is
12151
12152 -1 if X/Y is not on a tool-bar item
12153 0 if X/Y is on the same item that was highlighted before.
12154 1 otherwise. */
12155
12156 static int
12157 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12158 int *hpos, int *vpos, int *prop_idx)
12159 {
12160 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12161 struct window *w = XWINDOW (f->tool_bar_window);
12162 int area;
12163
12164 /* Find the glyph under X/Y. */
12165 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12166 if (*glyph == NULL)
12167 return -1;
12168
12169 /* Get the start of this tool-bar item's properties in
12170 f->tool_bar_items. */
12171 if (!tool_bar_item_info (f, *glyph, prop_idx))
12172 return -1;
12173
12174 /* Is mouse on the highlighted item? */
12175 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12176 && *vpos >= hlinfo->mouse_face_beg_row
12177 && *vpos <= hlinfo->mouse_face_end_row
12178 && (*vpos > hlinfo->mouse_face_beg_row
12179 || *hpos >= hlinfo->mouse_face_beg_col)
12180 && (*vpos < hlinfo->mouse_face_end_row
12181 || *hpos < hlinfo->mouse_face_end_col
12182 || hlinfo->mouse_face_past_end))
12183 return 0;
12184
12185 return 1;
12186 }
12187
12188
12189 /* EXPORT:
12190 Handle mouse button event on the tool-bar of frame F, at
12191 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12192 0 for button release. MODIFIERS is event modifiers for button
12193 release. */
12194
12195 void
12196 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12197 int modifiers)
12198 {
12199 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12200 struct window *w = XWINDOW (f->tool_bar_window);
12201 int hpos, vpos, prop_idx;
12202 struct glyph *glyph;
12203 Lisp_Object enabled_p;
12204 int ts;
12205
12206 /* If not on the highlighted tool-bar item, and mouse-highlight is
12207 non-nil, return. This is so we generate the tool-bar button
12208 click only when the mouse button is released on the same item as
12209 where it was pressed. However, when mouse-highlight is disabled,
12210 generate the click when the button is released regardless of the
12211 highlight, since tool-bar items are not highlighted in that
12212 case. */
12213 frame_to_window_pixel_xy (w, &x, &y);
12214 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12215 if (ts == -1
12216 || (ts != 0 && !NILP (Vmouse_highlight)))
12217 return;
12218
12219 /* When mouse-highlight is off, generate the click for the item
12220 where the button was pressed, disregarding where it was
12221 released. */
12222 if (NILP (Vmouse_highlight) && !down_p)
12223 prop_idx = last_tool_bar_item;
12224
12225 /* If item is disabled, do nothing. */
12226 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12227 if (NILP (enabled_p))
12228 return;
12229
12230 if (down_p)
12231 {
12232 /* Show item in pressed state. */
12233 if (!NILP (Vmouse_highlight))
12234 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12235 last_tool_bar_item = prop_idx;
12236 }
12237 else
12238 {
12239 Lisp_Object key, frame;
12240 struct input_event event;
12241 EVENT_INIT (event);
12242
12243 /* Show item in released state. */
12244 if (!NILP (Vmouse_highlight))
12245 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12246
12247 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12248
12249 XSETFRAME (frame, f);
12250 event.kind = TOOL_BAR_EVENT;
12251 event.frame_or_window = frame;
12252 event.arg = frame;
12253 kbd_buffer_store_event (&event);
12254
12255 event.kind = TOOL_BAR_EVENT;
12256 event.frame_or_window = frame;
12257 event.arg = key;
12258 event.modifiers = modifiers;
12259 kbd_buffer_store_event (&event);
12260 last_tool_bar_item = -1;
12261 }
12262 }
12263
12264
12265 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12266 tool-bar window-relative coordinates X/Y. Called from
12267 note_mouse_highlight. */
12268
12269 static void
12270 note_tool_bar_highlight (struct frame *f, int x, int y)
12271 {
12272 Lisp_Object window = f->tool_bar_window;
12273 struct window *w = XWINDOW (window);
12274 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12275 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12276 int hpos, vpos;
12277 struct glyph *glyph;
12278 struct glyph_row *row;
12279 int i;
12280 Lisp_Object enabled_p;
12281 int prop_idx;
12282 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12283 int mouse_down_p, rc;
12284
12285 /* Function note_mouse_highlight is called with negative X/Y
12286 values when mouse moves outside of the frame. */
12287 if (x <= 0 || y <= 0)
12288 {
12289 clear_mouse_face (hlinfo);
12290 return;
12291 }
12292
12293 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12294 if (rc < 0)
12295 {
12296 /* Not on tool-bar item. */
12297 clear_mouse_face (hlinfo);
12298 return;
12299 }
12300 else if (rc == 0)
12301 /* On same tool-bar item as before. */
12302 goto set_help_echo;
12303
12304 clear_mouse_face (hlinfo);
12305
12306 /* Mouse is down, but on different tool-bar item? */
12307 mouse_down_p = (dpyinfo->grabbed
12308 && f == last_mouse_frame
12309 && FRAME_LIVE_P (f));
12310 if (mouse_down_p
12311 && last_tool_bar_item != prop_idx)
12312 return;
12313
12314 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12315
12316 /* If tool-bar item is not enabled, don't highlight it. */
12317 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12318 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12319 {
12320 /* Compute the x-position of the glyph. In front and past the
12321 image is a space. We include this in the highlighted area. */
12322 row = MATRIX_ROW (w->current_matrix, vpos);
12323 for (i = x = 0; i < hpos; ++i)
12324 x += row->glyphs[TEXT_AREA][i].pixel_width;
12325
12326 /* Record this as the current active region. */
12327 hlinfo->mouse_face_beg_col = hpos;
12328 hlinfo->mouse_face_beg_row = vpos;
12329 hlinfo->mouse_face_beg_x = x;
12330 hlinfo->mouse_face_past_end = 0;
12331
12332 hlinfo->mouse_face_end_col = hpos + 1;
12333 hlinfo->mouse_face_end_row = vpos;
12334 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12335 hlinfo->mouse_face_window = window;
12336 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12337
12338 /* Display it as active. */
12339 show_mouse_face (hlinfo, draw);
12340 }
12341
12342 set_help_echo:
12343
12344 /* Set help_echo_string to a help string to display for this tool-bar item.
12345 XTread_socket does the rest. */
12346 help_echo_object = help_echo_window = Qnil;
12347 help_echo_pos = -1;
12348 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12349 if (NILP (help_echo_string))
12350 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12351 }
12352
12353 #endif /* HAVE_WINDOW_SYSTEM */
12354
12355
12356 \f
12357 /************************************************************************
12358 Horizontal scrolling
12359 ************************************************************************/
12360
12361 static int hscroll_window_tree (Lisp_Object);
12362 static int hscroll_windows (Lisp_Object);
12363
12364 /* For all leaf windows in the window tree rooted at WINDOW, set their
12365 hscroll value so that PT is (i) visible in the window, and (ii) so
12366 that it is not within a certain margin at the window's left and
12367 right border. Value is non-zero if any window's hscroll has been
12368 changed. */
12369
12370 static int
12371 hscroll_window_tree (Lisp_Object window)
12372 {
12373 int hscrolled_p = 0;
12374 int hscroll_relative_p = FLOATP (Vhscroll_step);
12375 int hscroll_step_abs = 0;
12376 double hscroll_step_rel = 0;
12377
12378 if (hscroll_relative_p)
12379 {
12380 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12381 if (hscroll_step_rel < 0)
12382 {
12383 hscroll_relative_p = 0;
12384 hscroll_step_abs = 0;
12385 }
12386 }
12387 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12388 {
12389 hscroll_step_abs = XINT (Vhscroll_step);
12390 if (hscroll_step_abs < 0)
12391 hscroll_step_abs = 0;
12392 }
12393 else
12394 hscroll_step_abs = 0;
12395
12396 while (WINDOWP (window))
12397 {
12398 struct window *w = XWINDOW (window);
12399
12400 if (WINDOWP (w->contents))
12401 hscrolled_p |= hscroll_window_tree (w->contents);
12402 else if (w->cursor.vpos >= 0)
12403 {
12404 int h_margin;
12405 int text_area_width;
12406 struct glyph_row *current_cursor_row
12407 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12408 struct glyph_row *desired_cursor_row
12409 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12410 struct glyph_row *cursor_row
12411 = (desired_cursor_row->enabled_p
12412 ? desired_cursor_row
12413 : current_cursor_row);
12414 int row_r2l_p = cursor_row->reversed_p;
12415
12416 text_area_width = window_box_width (w, TEXT_AREA);
12417
12418 /* Scroll when cursor is inside this scroll margin. */
12419 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12420
12421 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12422 /* For left-to-right rows, hscroll when cursor is either
12423 (i) inside the right hscroll margin, or (ii) if it is
12424 inside the left margin and the window is already
12425 hscrolled. */
12426 && ((!row_r2l_p
12427 && ((w->hscroll
12428 && w->cursor.x <= h_margin)
12429 || (cursor_row->enabled_p
12430 && cursor_row->truncated_on_right_p
12431 && (w->cursor.x >= text_area_width - h_margin))))
12432 /* For right-to-left rows, the logic is similar,
12433 except that rules for scrolling to left and right
12434 are reversed. E.g., if cursor.x <= h_margin, we
12435 need to hscroll "to the right" unconditionally,
12436 and that will scroll the screen to the left so as
12437 to reveal the next portion of the row. */
12438 || (row_r2l_p
12439 && ((cursor_row->enabled_p
12440 /* FIXME: It is confusing to set the
12441 truncated_on_right_p flag when R2L rows
12442 are actually truncated on the left. */
12443 && cursor_row->truncated_on_right_p
12444 && w->cursor.x <= h_margin)
12445 || (w->hscroll
12446 && (w->cursor.x >= text_area_width - h_margin))))))
12447 {
12448 struct it it;
12449 ptrdiff_t hscroll;
12450 struct buffer *saved_current_buffer;
12451 ptrdiff_t pt;
12452 int wanted_x;
12453
12454 /* Find point in a display of infinite width. */
12455 saved_current_buffer = current_buffer;
12456 current_buffer = XBUFFER (w->contents);
12457
12458 if (w == XWINDOW (selected_window))
12459 pt = PT;
12460 else
12461 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12462
12463 /* Move iterator to pt starting at cursor_row->start in
12464 a line with infinite width. */
12465 init_to_row_start (&it, w, cursor_row);
12466 it.last_visible_x = INFINITY;
12467 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12468 current_buffer = saved_current_buffer;
12469
12470 /* Position cursor in window. */
12471 if (!hscroll_relative_p && hscroll_step_abs == 0)
12472 hscroll = max (0, (it.current_x
12473 - (ITERATOR_AT_END_OF_LINE_P (&it)
12474 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12475 : (text_area_width / 2))))
12476 / FRAME_COLUMN_WIDTH (it.f);
12477 else if ((!row_r2l_p
12478 && w->cursor.x >= text_area_width - h_margin)
12479 || (row_r2l_p && w->cursor.x <= h_margin))
12480 {
12481 if (hscroll_relative_p)
12482 wanted_x = text_area_width * (1 - hscroll_step_rel)
12483 - h_margin;
12484 else
12485 wanted_x = text_area_width
12486 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12487 - h_margin;
12488 hscroll
12489 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12490 }
12491 else
12492 {
12493 if (hscroll_relative_p)
12494 wanted_x = text_area_width * hscroll_step_rel
12495 + h_margin;
12496 else
12497 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12498 + h_margin;
12499 hscroll
12500 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12501 }
12502 hscroll = max (hscroll, w->min_hscroll);
12503
12504 /* Don't prevent redisplay optimizations if hscroll
12505 hasn't changed, as it will unnecessarily slow down
12506 redisplay. */
12507 if (w->hscroll != hscroll)
12508 {
12509 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12510 w->hscroll = hscroll;
12511 hscrolled_p = 1;
12512 }
12513 }
12514 }
12515
12516 window = w->next;
12517 }
12518
12519 /* Value is non-zero if hscroll of any leaf window has been changed. */
12520 return hscrolled_p;
12521 }
12522
12523
12524 /* Set hscroll so that cursor is visible and not inside horizontal
12525 scroll margins for all windows in the tree rooted at WINDOW. See
12526 also hscroll_window_tree above. Value is non-zero if any window's
12527 hscroll has been changed. If it has, desired matrices on the frame
12528 of WINDOW are cleared. */
12529
12530 static int
12531 hscroll_windows (Lisp_Object window)
12532 {
12533 int hscrolled_p = hscroll_window_tree (window);
12534 if (hscrolled_p)
12535 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12536 return hscrolled_p;
12537 }
12538
12539
12540 \f
12541 /************************************************************************
12542 Redisplay
12543 ************************************************************************/
12544
12545 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12546 to a non-zero value. This is sometimes handy to have in a debugger
12547 session. */
12548
12549 #ifdef GLYPH_DEBUG
12550
12551 /* First and last unchanged row for try_window_id. */
12552
12553 static int debug_first_unchanged_at_end_vpos;
12554 static int debug_last_unchanged_at_beg_vpos;
12555
12556 /* Delta vpos and y. */
12557
12558 static int debug_dvpos, debug_dy;
12559
12560 /* Delta in characters and bytes for try_window_id. */
12561
12562 static ptrdiff_t debug_delta, debug_delta_bytes;
12563
12564 /* Values of window_end_pos and window_end_vpos at the end of
12565 try_window_id. */
12566
12567 static ptrdiff_t debug_end_vpos;
12568
12569 /* Append a string to W->desired_matrix->method. FMT is a printf
12570 format string. If trace_redisplay_p is non-zero also printf the
12571 resulting string to stderr. */
12572
12573 static void debug_method_add (struct window *, char const *, ...)
12574 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12575
12576 static void
12577 debug_method_add (struct window *w, char const *fmt, ...)
12578 {
12579 void *ptr = w;
12580 char *method = w->desired_matrix->method;
12581 int len = strlen (method);
12582 int size = sizeof w->desired_matrix->method;
12583 int remaining = size - len - 1;
12584 va_list ap;
12585
12586 if (len && remaining)
12587 {
12588 method[len] = '|';
12589 --remaining, ++len;
12590 }
12591
12592 va_start (ap, fmt);
12593 vsnprintf (method + len, remaining + 1, fmt, ap);
12594 va_end (ap);
12595
12596 if (trace_redisplay_p)
12597 fprintf (stderr, "%p (%s): %s\n",
12598 ptr,
12599 ((BUFFERP (w->contents)
12600 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12601 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12602 : "no buffer"),
12603 method + len);
12604 }
12605
12606 #endif /* GLYPH_DEBUG */
12607
12608
12609 /* Value is non-zero if all changes in window W, which displays
12610 current_buffer, are in the text between START and END. START is a
12611 buffer position, END is given as a distance from Z. Used in
12612 redisplay_internal for display optimization. */
12613
12614 static int
12615 text_outside_line_unchanged_p (struct window *w,
12616 ptrdiff_t start, ptrdiff_t end)
12617 {
12618 int unchanged_p = 1;
12619
12620 /* If text or overlays have changed, see where. */
12621 if (window_outdated (w))
12622 {
12623 /* Gap in the line? */
12624 if (GPT < start || Z - GPT < end)
12625 unchanged_p = 0;
12626
12627 /* Changes start in front of the line, or end after it? */
12628 if (unchanged_p
12629 && (BEG_UNCHANGED < start - 1
12630 || END_UNCHANGED < end))
12631 unchanged_p = 0;
12632
12633 /* If selective display, can't optimize if changes start at the
12634 beginning of the line. */
12635 if (unchanged_p
12636 && INTEGERP (BVAR (current_buffer, selective_display))
12637 && XINT (BVAR (current_buffer, selective_display)) > 0
12638 && (BEG_UNCHANGED < start || GPT <= start))
12639 unchanged_p = 0;
12640
12641 /* If there are overlays at the start or end of the line, these
12642 may have overlay strings with newlines in them. A change at
12643 START, for instance, may actually concern the display of such
12644 overlay strings as well, and they are displayed on different
12645 lines. So, quickly rule out this case. (For the future, it
12646 might be desirable to implement something more telling than
12647 just BEG/END_UNCHANGED.) */
12648 if (unchanged_p)
12649 {
12650 if (BEG + BEG_UNCHANGED == start
12651 && overlay_touches_p (start))
12652 unchanged_p = 0;
12653 if (END_UNCHANGED == end
12654 && overlay_touches_p (Z - end))
12655 unchanged_p = 0;
12656 }
12657
12658 /* Under bidi reordering, adding or deleting a character in the
12659 beginning of a paragraph, before the first strong directional
12660 character, can change the base direction of the paragraph (unless
12661 the buffer specifies a fixed paragraph direction), which will
12662 require to redisplay the whole paragraph. It might be worthwhile
12663 to find the paragraph limits and widen the range of redisplayed
12664 lines to that, but for now just give up this optimization. */
12665 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
12666 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
12667 unchanged_p = 0;
12668 }
12669
12670 return unchanged_p;
12671 }
12672
12673
12674 /* Do a frame update, taking possible shortcuts into account. This is
12675 the main external entry point for redisplay.
12676
12677 If the last redisplay displayed an echo area message and that message
12678 is no longer requested, we clear the echo area or bring back the
12679 mini-buffer if that is in use. */
12680
12681 void
12682 redisplay (void)
12683 {
12684 redisplay_internal ();
12685 }
12686
12687
12688 static Lisp_Object
12689 overlay_arrow_string_or_property (Lisp_Object var)
12690 {
12691 Lisp_Object val;
12692
12693 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12694 return val;
12695
12696 return Voverlay_arrow_string;
12697 }
12698
12699 /* Return 1 if there are any overlay-arrows in current_buffer. */
12700 static int
12701 overlay_arrow_in_current_buffer_p (void)
12702 {
12703 Lisp_Object vlist;
12704
12705 for (vlist = Voverlay_arrow_variable_list;
12706 CONSP (vlist);
12707 vlist = XCDR (vlist))
12708 {
12709 Lisp_Object var = XCAR (vlist);
12710 Lisp_Object val;
12711
12712 if (!SYMBOLP (var))
12713 continue;
12714 val = find_symbol_value (var);
12715 if (MARKERP (val)
12716 && current_buffer == XMARKER (val)->buffer)
12717 return 1;
12718 }
12719 return 0;
12720 }
12721
12722
12723 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12724 has changed. */
12725
12726 static int
12727 overlay_arrows_changed_p (void)
12728 {
12729 Lisp_Object vlist;
12730
12731 for (vlist = Voverlay_arrow_variable_list;
12732 CONSP (vlist);
12733 vlist = XCDR (vlist))
12734 {
12735 Lisp_Object var = XCAR (vlist);
12736 Lisp_Object val, pstr;
12737
12738 if (!SYMBOLP (var))
12739 continue;
12740 val = find_symbol_value (var);
12741 if (!MARKERP (val))
12742 continue;
12743 if (! EQ (COERCE_MARKER (val),
12744 Fget (var, Qlast_arrow_position))
12745 || ! (pstr = overlay_arrow_string_or_property (var),
12746 EQ (pstr, Fget (var, Qlast_arrow_string))))
12747 return 1;
12748 }
12749 return 0;
12750 }
12751
12752 /* Mark overlay arrows to be updated on next redisplay. */
12753
12754 static void
12755 update_overlay_arrows (int up_to_date)
12756 {
12757 Lisp_Object vlist;
12758
12759 for (vlist = Voverlay_arrow_variable_list;
12760 CONSP (vlist);
12761 vlist = XCDR (vlist))
12762 {
12763 Lisp_Object var = XCAR (vlist);
12764
12765 if (!SYMBOLP (var))
12766 continue;
12767
12768 if (up_to_date > 0)
12769 {
12770 Lisp_Object val = find_symbol_value (var);
12771 Fput (var, Qlast_arrow_position,
12772 COERCE_MARKER (val));
12773 Fput (var, Qlast_arrow_string,
12774 overlay_arrow_string_or_property (var));
12775 }
12776 else if (up_to_date < 0
12777 || !NILP (Fget (var, Qlast_arrow_position)))
12778 {
12779 Fput (var, Qlast_arrow_position, Qt);
12780 Fput (var, Qlast_arrow_string, Qt);
12781 }
12782 }
12783 }
12784
12785
12786 /* Return overlay arrow string to display at row.
12787 Return integer (bitmap number) for arrow bitmap in left fringe.
12788 Return nil if no overlay arrow. */
12789
12790 static Lisp_Object
12791 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12792 {
12793 Lisp_Object vlist;
12794
12795 for (vlist = Voverlay_arrow_variable_list;
12796 CONSP (vlist);
12797 vlist = XCDR (vlist))
12798 {
12799 Lisp_Object var = XCAR (vlist);
12800 Lisp_Object val;
12801
12802 if (!SYMBOLP (var))
12803 continue;
12804
12805 val = find_symbol_value (var);
12806
12807 if (MARKERP (val)
12808 && current_buffer == XMARKER (val)->buffer
12809 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12810 {
12811 if (FRAME_WINDOW_P (it->f)
12812 /* FIXME: if ROW->reversed_p is set, this should test
12813 the right fringe, not the left one. */
12814 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12815 {
12816 #ifdef HAVE_WINDOW_SYSTEM
12817 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12818 {
12819 int fringe_bitmap;
12820 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12821 return make_number (fringe_bitmap);
12822 }
12823 #endif
12824 return make_number (-1); /* Use default arrow bitmap. */
12825 }
12826 return overlay_arrow_string_or_property (var);
12827 }
12828 }
12829
12830 return Qnil;
12831 }
12832
12833 /* Return 1 if point moved out of or into a composition. Otherwise
12834 return 0. PREV_BUF and PREV_PT are the last point buffer and
12835 position. BUF and PT are the current point buffer and position. */
12836
12837 static int
12838 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12839 struct buffer *buf, ptrdiff_t pt)
12840 {
12841 ptrdiff_t start, end;
12842 Lisp_Object prop;
12843 Lisp_Object buffer;
12844
12845 XSETBUFFER (buffer, buf);
12846 /* Check a composition at the last point if point moved within the
12847 same buffer. */
12848 if (prev_buf == buf)
12849 {
12850 if (prev_pt == pt)
12851 /* Point didn't move. */
12852 return 0;
12853
12854 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12855 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12856 && composition_valid_p (start, end, prop)
12857 && start < prev_pt && end > prev_pt)
12858 /* The last point was within the composition. Return 1 iff
12859 point moved out of the composition. */
12860 return (pt <= start || pt >= end);
12861 }
12862
12863 /* Check a composition at the current point. */
12864 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12865 && find_composition (pt, -1, &start, &end, &prop, buffer)
12866 && composition_valid_p (start, end, prop)
12867 && start < pt && end > pt);
12868 }
12869
12870 /* Reconsider the clip changes of buffer which is displayed in W. */
12871
12872 static void
12873 reconsider_clip_changes (struct window *w)
12874 {
12875 struct buffer *b = XBUFFER (w->contents);
12876
12877 if (b->clip_changed
12878 && w->window_end_valid
12879 && w->current_matrix->buffer == b
12880 && w->current_matrix->zv == BUF_ZV (b)
12881 && w->current_matrix->begv == BUF_BEGV (b))
12882 b->clip_changed = 0;
12883
12884 /* If display wasn't paused, and W is not a tool bar window, see if
12885 point has been moved into or out of a composition. In that case,
12886 we set b->clip_changed to 1 to force updating the screen. If
12887 b->clip_changed has already been set to 1, we can skip this
12888 check. */
12889 if (!b->clip_changed && w->window_end_valid)
12890 {
12891 ptrdiff_t pt = (w == XWINDOW (selected_window)
12892 ? PT : marker_position (w->pointm));
12893
12894 if ((w->current_matrix->buffer != b || pt != w->last_point)
12895 && check_point_in_composition (w->current_matrix->buffer,
12896 w->last_point, b, pt))
12897 b->clip_changed = 1;
12898 }
12899 }
12900
12901 #define STOP_POLLING \
12902 do { if (! polling_stopped_here) stop_polling (); \
12903 polling_stopped_here = 1; } while (0)
12904
12905 #define RESUME_POLLING \
12906 do { if (polling_stopped_here) start_polling (); \
12907 polling_stopped_here = 0; } while (0)
12908
12909
12910 /* Perhaps in the future avoid recentering windows if it
12911 is not necessary; currently that causes some problems. */
12912
12913 static void
12914 redisplay_internal (void)
12915 {
12916 struct window *w = XWINDOW (selected_window);
12917 struct window *sw;
12918 struct frame *fr;
12919 int pending;
12920 bool must_finish = 0, match_p;
12921 struct text_pos tlbufpos, tlendpos;
12922 int number_of_visible_frames;
12923 ptrdiff_t count;
12924 struct frame *sf;
12925 int polling_stopped_here = 0;
12926 Lisp_Object tail, frame;
12927
12928 /* Non-zero means redisplay has to consider all windows on all
12929 frames. Zero means, only selected_window is considered. */
12930 int consider_all_windows_p;
12931
12932 /* Non-zero means redisplay has to redisplay the miniwindow. */
12933 int update_miniwindow_p = 0;
12934
12935 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12936
12937 /* No redisplay if running in batch mode or frame is not yet fully
12938 initialized, or redisplay is explicitly turned off by setting
12939 Vinhibit_redisplay. */
12940 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12941 || !NILP (Vinhibit_redisplay))
12942 return;
12943
12944 /* Don't examine these until after testing Vinhibit_redisplay.
12945 When Emacs is shutting down, perhaps because its connection to
12946 X has dropped, we should not look at them at all. */
12947 fr = XFRAME (w->frame);
12948 sf = SELECTED_FRAME ();
12949
12950 if (!fr->glyphs_initialized_p)
12951 return;
12952
12953 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12954 if (popup_activated ())
12955 return;
12956 #endif
12957
12958 /* I don't think this happens but let's be paranoid. */
12959 if (redisplaying_p)
12960 return;
12961
12962 /* Record a function that clears redisplaying_p
12963 when we leave this function. */
12964 count = SPECPDL_INDEX ();
12965 record_unwind_protect_void (unwind_redisplay);
12966 redisplaying_p = 1;
12967 specbind (Qinhibit_free_realized_faces, Qnil);
12968
12969 /* Record this function, so it appears on the profiler's backtraces. */
12970 record_in_backtrace (Qredisplay_internal, &Qnil, 0);
12971
12972 FOR_EACH_FRAME (tail, frame)
12973 XFRAME (frame)->already_hscrolled_p = 0;
12974
12975 retry:
12976 /* Remember the currently selected window. */
12977 sw = w;
12978
12979 pending = 0;
12980 last_escape_glyph_frame = NULL;
12981 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12982 last_glyphless_glyph_frame = NULL;
12983 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12984
12985 /* If new fonts have been loaded that make a glyph matrix adjustment
12986 necessary, do it. */
12987 if (fonts_changed_p)
12988 {
12989 adjust_glyphs (NULL);
12990 ++windows_or_buffers_changed;
12991 fonts_changed_p = 0;
12992 }
12993
12994 /* If face_change_count is non-zero, init_iterator will free all
12995 realized faces, which includes the faces referenced from current
12996 matrices. So, we can't reuse current matrices in this case. */
12997 if (face_change_count)
12998 ++windows_or_buffers_changed;
12999
13000 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13001 && FRAME_TTY (sf)->previous_frame != sf)
13002 {
13003 /* Since frames on a single ASCII terminal share the same
13004 display area, displaying a different frame means redisplay
13005 the whole thing. */
13006 windows_or_buffers_changed++;
13007 SET_FRAME_GARBAGED (sf);
13008 #ifndef DOS_NT
13009 set_tty_color_mode (FRAME_TTY (sf), sf);
13010 #endif
13011 FRAME_TTY (sf)->previous_frame = sf;
13012 }
13013
13014 /* Set the visible flags for all frames. Do this before checking for
13015 resized or garbaged frames; they want to know if their frames are
13016 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13017 number_of_visible_frames = 0;
13018
13019 FOR_EACH_FRAME (tail, frame)
13020 {
13021 struct frame *f = XFRAME (frame);
13022
13023 if (FRAME_VISIBLE_P (f))
13024 ++number_of_visible_frames;
13025 clear_desired_matrices (f);
13026 }
13027
13028 /* Notice any pending interrupt request to change frame size. */
13029 do_pending_window_change (1);
13030
13031 /* do_pending_window_change could change the selected_window due to
13032 frame resizing which makes the selected window too small. */
13033 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13034 sw = w;
13035
13036 /* Clear frames marked as garbaged. */
13037 clear_garbaged_frames ();
13038
13039 /* Build menubar and tool-bar items. */
13040 if (NILP (Vmemory_full))
13041 prepare_menu_bars ();
13042
13043 if (windows_or_buffers_changed)
13044 update_mode_lines++;
13045
13046 reconsider_clip_changes (w);
13047
13048 /* In most cases selected window displays current buffer. */
13049 match_p = XBUFFER (w->contents) == current_buffer;
13050 if (match_p)
13051 {
13052 ptrdiff_t count1;
13053
13054 /* Detect case that we need to write or remove a star in the mode line. */
13055 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13056 {
13057 w->update_mode_line = 1;
13058 if (buffer_shared_and_changed ())
13059 update_mode_lines++;
13060 }
13061
13062 /* Avoid invocation of point motion hooks by `current_column' below. */
13063 count1 = SPECPDL_INDEX ();
13064 specbind (Qinhibit_point_motion_hooks, Qt);
13065
13066 if (mode_line_update_needed (w))
13067 w->update_mode_line = 1;
13068
13069 unbind_to (count1, Qnil);
13070 }
13071
13072 consider_all_windows_p = (update_mode_lines
13073 || buffer_shared_and_changed ()
13074 || cursor_type_changed);
13075
13076 /* If specs for an arrow have changed, do thorough redisplay
13077 to ensure we remove any arrow that should no longer exist. */
13078 if (overlay_arrows_changed_p ())
13079 consider_all_windows_p = windows_or_buffers_changed = 1;
13080
13081 /* Normally the message* functions will have already displayed and
13082 updated the echo area, but the frame may have been trashed, or
13083 the update may have been preempted, so display the echo area
13084 again here. Checking message_cleared_p captures the case that
13085 the echo area should be cleared. */
13086 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13087 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13088 || (message_cleared_p
13089 && minibuf_level == 0
13090 /* If the mini-window is currently selected, this means the
13091 echo-area doesn't show through. */
13092 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13093 {
13094 int window_height_changed_p = echo_area_display (0);
13095
13096 if (message_cleared_p)
13097 update_miniwindow_p = 1;
13098
13099 must_finish = 1;
13100
13101 /* If we don't display the current message, don't clear the
13102 message_cleared_p flag, because, if we did, we wouldn't clear
13103 the echo area in the next redisplay which doesn't preserve
13104 the echo area. */
13105 if (!display_last_displayed_message_p)
13106 message_cleared_p = 0;
13107
13108 if (fonts_changed_p)
13109 goto retry;
13110 else if (window_height_changed_p)
13111 {
13112 consider_all_windows_p = 1;
13113 ++update_mode_lines;
13114 ++windows_or_buffers_changed;
13115
13116 /* If window configuration was changed, frames may have been
13117 marked garbaged. Clear them or we will experience
13118 surprises wrt scrolling. */
13119 clear_garbaged_frames ();
13120 }
13121 }
13122 else if (EQ (selected_window, minibuf_window)
13123 && (current_buffer->clip_changed || window_outdated (w))
13124 && resize_mini_window (w, 0))
13125 {
13126 /* Resized active mini-window to fit the size of what it is
13127 showing if its contents might have changed. */
13128 must_finish = 1;
13129 /* FIXME: this causes all frames to be updated, which seems unnecessary
13130 since only the current frame needs to be considered. This function
13131 needs to be rewritten with two variables, consider_all_windows and
13132 consider_all_frames. */
13133 consider_all_windows_p = 1;
13134 ++windows_or_buffers_changed;
13135 ++update_mode_lines;
13136
13137 /* If window configuration was changed, frames may have been
13138 marked garbaged. Clear them or we will experience
13139 surprises wrt scrolling. */
13140 clear_garbaged_frames ();
13141 }
13142
13143 /* If showing the region, and mark has changed, we must redisplay
13144 the whole window. The assignment to this_line_start_pos prevents
13145 the optimization directly below this if-statement. */
13146 if (((!NILP (Vtransient_mark_mode)
13147 && !NILP (BVAR (XBUFFER (w->contents), mark_active)))
13148 != (w->region_showing > 0))
13149 || (w->region_showing
13150 && w->region_showing
13151 != XINT (Fmarker_position (BVAR (XBUFFER (w->contents), mark)))))
13152 CHARPOS (this_line_start_pos) = 0;
13153
13154 /* Optimize the case that only the line containing the cursor in the
13155 selected window has changed. Variables starting with this_ are
13156 set in display_line and record information about the line
13157 containing the cursor. */
13158 tlbufpos = this_line_start_pos;
13159 tlendpos = this_line_end_pos;
13160 if (!consider_all_windows_p
13161 && CHARPOS (tlbufpos) > 0
13162 && !w->update_mode_line
13163 && !current_buffer->clip_changed
13164 && !current_buffer->prevent_redisplay_optimizations_p
13165 && FRAME_VISIBLE_P (XFRAME (w->frame))
13166 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13167 /* Make sure recorded data applies to current buffer, etc. */
13168 && this_line_buffer == current_buffer
13169 && match_p
13170 && !w->force_start
13171 && !w->optional_new_start
13172 /* Point must be on the line that we have info recorded about. */
13173 && PT >= CHARPOS (tlbufpos)
13174 && PT <= Z - CHARPOS (tlendpos)
13175 /* All text outside that line, including its final newline,
13176 must be unchanged. */
13177 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13178 CHARPOS (tlendpos)))
13179 {
13180 if (CHARPOS (tlbufpos) > BEGV
13181 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13182 && (CHARPOS (tlbufpos) == ZV
13183 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13184 /* Former continuation line has disappeared by becoming empty. */
13185 goto cancel;
13186 else if (window_outdated (w) || MINI_WINDOW_P (w))
13187 {
13188 /* We have to handle the case of continuation around a
13189 wide-column character (see the comment in indent.c around
13190 line 1340).
13191
13192 For instance, in the following case:
13193
13194 -------- Insert --------
13195 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13196 J_I_ ==> J_I_ `^^' are cursors.
13197 ^^ ^^
13198 -------- --------
13199
13200 As we have to redraw the line above, we cannot use this
13201 optimization. */
13202
13203 struct it it;
13204 int line_height_before = this_line_pixel_height;
13205
13206 /* Note that start_display will handle the case that the
13207 line starting at tlbufpos is a continuation line. */
13208 start_display (&it, w, tlbufpos);
13209
13210 /* Implementation note: It this still necessary? */
13211 if (it.current_x != this_line_start_x)
13212 goto cancel;
13213
13214 TRACE ((stderr, "trying display optimization 1\n"));
13215 w->cursor.vpos = -1;
13216 overlay_arrow_seen = 0;
13217 it.vpos = this_line_vpos;
13218 it.current_y = this_line_y;
13219 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13220 display_line (&it);
13221
13222 /* If line contains point, is not continued,
13223 and ends at same distance from eob as before, we win. */
13224 if (w->cursor.vpos >= 0
13225 /* Line is not continued, otherwise this_line_start_pos
13226 would have been set to 0 in display_line. */
13227 && CHARPOS (this_line_start_pos)
13228 /* Line ends as before. */
13229 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13230 /* Line has same height as before. Otherwise other lines
13231 would have to be shifted up or down. */
13232 && this_line_pixel_height == line_height_before)
13233 {
13234 /* If this is not the window's last line, we must adjust
13235 the charstarts of the lines below. */
13236 if (it.current_y < it.last_visible_y)
13237 {
13238 struct glyph_row *row
13239 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13240 ptrdiff_t delta, delta_bytes;
13241
13242 /* We used to distinguish between two cases here,
13243 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13244 when the line ends in a newline or the end of the
13245 buffer's accessible portion. But both cases did
13246 the same, so they were collapsed. */
13247 delta = (Z
13248 - CHARPOS (tlendpos)
13249 - MATRIX_ROW_START_CHARPOS (row));
13250 delta_bytes = (Z_BYTE
13251 - BYTEPOS (tlendpos)
13252 - MATRIX_ROW_START_BYTEPOS (row));
13253
13254 increment_matrix_positions (w->current_matrix,
13255 this_line_vpos + 1,
13256 w->current_matrix->nrows,
13257 delta, delta_bytes);
13258 }
13259
13260 /* If this row displays text now but previously didn't,
13261 or vice versa, w->window_end_vpos may have to be
13262 adjusted. */
13263 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13264 {
13265 if (w->window_end_vpos < this_line_vpos)
13266 w->window_end_vpos = this_line_vpos;
13267 }
13268 else if (w->window_end_vpos == this_line_vpos
13269 && this_line_vpos > 0)
13270 w->window_end_vpos = this_line_vpos - 1;
13271 w->window_end_valid = 0;
13272
13273 /* Update hint: No need to try to scroll in update_window. */
13274 w->desired_matrix->no_scrolling_p = 1;
13275
13276 #ifdef GLYPH_DEBUG
13277 *w->desired_matrix->method = 0;
13278 debug_method_add (w, "optimization 1");
13279 #endif
13280 #ifdef HAVE_WINDOW_SYSTEM
13281 update_window_fringes (w, 0);
13282 #endif
13283 goto update;
13284 }
13285 else
13286 goto cancel;
13287 }
13288 else if (/* Cursor position hasn't changed. */
13289 PT == w->last_point
13290 /* Make sure the cursor was last displayed
13291 in this window. Otherwise we have to reposition it. */
13292 && 0 <= w->cursor.vpos
13293 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13294 {
13295 if (!must_finish)
13296 {
13297 do_pending_window_change (1);
13298 /* If selected_window changed, redisplay again. */
13299 if (WINDOWP (selected_window)
13300 && (w = XWINDOW (selected_window)) != sw)
13301 goto retry;
13302
13303 /* We used to always goto end_of_redisplay here, but this
13304 isn't enough if we have a blinking cursor. */
13305 if (w->cursor_off_p == w->last_cursor_off_p)
13306 goto end_of_redisplay;
13307 }
13308 goto update;
13309 }
13310 /* If highlighting the region, or if the cursor is in the echo area,
13311 then we can't just move the cursor. */
13312 else if (! (!NILP (Vtransient_mark_mode)
13313 && !NILP (BVAR (current_buffer, mark_active)))
13314 && (EQ (selected_window,
13315 BVAR (current_buffer, last_selected_window))
13316 || highlight_nonselected_windows)
13317 && !w->region_showing
13318 && NILP (Vshow_trailing_whitespace)
13319 && !cursor_in_echo_area)
13320 {
13321 struct it it;
13322 struct glyph_row *row;
13323
13324 /* Skip from tlbufpos to PT and see where it is. Note that
13325 PT may be in invisible text. If so, we will end at the
13326 next visible position. */
13327 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13328 NULL, DEFAULT_FACE_ID);
13329 it.current_x = this_line_start_x;
13330 it.current_y = this_line_y;
13331 it.vpos = this_line_vpos;
13332
13333 /* The call to move_it_to stops in front of PT, but
13334 moves over before-strings. */
13335 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13336
13337 if (it.vpos == this_line_vpos
13338 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13339 row->enabled_p))
13340 {
13341 eassert (this_line_vpos == it.vpos);
13342 eassert (this_line_y == it.current_y);
13343 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13344 #ifdef GLYPH_DEBUG
13345 *w->desired_matrix->method = 0;
13346 debug_method_add (w, "optimization 3");
13347 #endif
13348 goto update;
13349 }
13350 else
13351 goto cancel;
13352 }
13353
13354 cancel:
13355 /* Text changed drastically or point moved off of line. */
13356 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13357 }
13358
13359 CHARPOS (this_line_start_pos) = 0;
13360 consider_all_windows_p |= buffer_shared_and_changed ();
13361 ++clear_face_cache_count;
13362 #ifdef HAVE_WINDOW_SYSTEM
13363 ++clear_image_cache_count;
13364 #endif
13365
13366 /* Build desired matrices, and update the display. If
13367 consider_all_windows_p is non-zero, do it for all windows on all
13368 frames. Otherwise do it for selected_window, only. */
13369
13370 if (consider_all_windows_p)
13371 {
13372 FOR_EACH_FRAME (tail, frame)
13373 XFRAME (frame)->updated_p = 0;
13374
13375 FOR_EACH_FRAME (tail, frame)
13376 {
13377 struct frame *f = XFRAME (frame);
13378
13379 /* We don't have to do anything for unselected terminal
13380 frames. */
13381 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13382 && !EQ (FRAME_TTY (f)->top_frame, frame))
13383 continue;
13384
13385 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13386 {
13387 /* Mark all the scroll bars to be removed; we'll redeem
13388 the ones we want when we redisplay their windows. */
13389 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13390 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13391
13392 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13393 redisplay_windows (FRAME_ROOT_WINDOW (f));
13394
13395 /* The X error handler may have deleted that frame. */
13396 if (!FRAME_LIVE_P (f))
13397 continue;
13398
13399 /* Any scroll bars which redisplay_windows should have
13400 nuked should now go away. */
13401 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13402 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13403
13404 /* If fonts changed, display again. */
13405 /* ??? rms: I suspect it is a mistake to jump all the way
13406 back to retry here. It should just retry this frame. */
13407 if (fonts_changed_p)
13408 goto retry;
13409
13410 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13411 {
13412 /* See if we have to hscroll. */
13413 if (!f->already_hscrolled_p)
13414 {
13415 f->already_hscrolled_p = 1;
13416 if (hscroll_windows (f->root_window))
13417 goto retry;
13418 }
13419
13420 /* Prevent various kinds of signals during display
13421 update. stdio is not robust about handling
13422 signals, which can cause an apparent I/O
13423 error. */
13424 if (interrupt_input)
13425 unrequest_sigio ();
13426 STOP_POLLING;
13427
13428 /* Update the display. */
13429 set_window_update_flags (XWINDOW (f->root_window), 1);
13430 pending |= update_frame (f, 0, 0);
13431 f->updated_p = 1;
13432 }
13433 }
13434 }
13435
13436 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13437
13438 if (!pending)
13439 {
13440 /* Do the mark_window_display_accurate after all windows have
13441 been redisplayed because this call resets flags in buffers
13442 which are needed for proper redisplay. */
13443 FOR_EACH_FRAME (tail, frame)
13444 {
13445 struct frame *f = XFRAME (frame);
13446 if (f->updated_p)
13447 {
13448 mark_window_display_accurate (f->root_window, 1);
13449 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13450 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13451 }
13452 }
13453 }
13454 }
13455 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13456 {
13457 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13458 struct frame *mini_frame;
13459
13460 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13461 /* Use list_of_error, not Qerror, so that
13462 we catch only errors and don't run the debugger. */
13463 internal_condition_case_1 (redisplay_window_1, selected_window,
13464 list_of_error,
13465 redisplay_window_error);
13466 if (update_miniwindow_p)
13467 internal_condition_case_1 (redisplay_window_1, mini_window,
13468 list_of_error,
13469 redisplay_window_error);
13470
13471 /* Compare desired and current matrices, perform output. */
13472
13473 update:
13474 /* If fonts changed, display again. */
13475 if (fonts_changed_p)
13476 goto retry;
13477
13478 /* Prevent various kinds of signals during display update.
13479 stdio is not robust about handling signals,
13480 which can cause an apparent I/O error. */
13481 if (interrupt_input)
13482 unrequest_sigio ();
13483 STOP_POLLING;
13484
13485 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13486 {
13487 if (hscroll_windows (selected_window))
13488 goto retry;
13489
13490 XWINDOW (selected_window)->must_be_updated_p = 1;
13491 pending = update_frame (sf, 0, 0);
13492 }
13493
13494 /* We may have called echo_area_display at the top of this
13495 function. If the echo area is on another frame, that may
13496 have put text on a frame other than the selected one, so the
13497 above call to update_frame would not have caught it. Catch
13498 it here. */
13499 mini_window = FRAME_MINIBUF_WINDOW (sf);
13500 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13501
13502 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13503 {
13504 XWINDOW (mini_window)->must_be_updated_p = 1;
13505 pending |= update_frame (mini_frame, 0, 0);
13506 if (!pending && hscroll_windows (mini_window))
13507 goto retry;
13508 }
13509 }
13510
13511 /* If display was paused because of pending input, make sure we do a
13512 thorough update the next time. */
13513 if (pending)
13514 {
13515 /* Prevent the optimization at the beginning of
13516 redisplay_internal that tries a single-line update of the
13517 line containing the cursor in the selected window. */
13518 CHARPOS (this_line_start_pos) = 0;
13519
13520 /* Let the overlay arrow be updated the next time. */
13521 update_overlay_arrows (0);
13522
13523 /* If we pause after scrolling, some rows in the current
13524 matrices of some windows are not valid. */
13525 if (!WINDOW_FULL_WIDTH_P (w)
13526 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13527 update_mode_lines = 1;
13528 }
13529 else
13530 {
13531 if (!consider_all_windows_p)
13532 {
13533 /* This has already been done above if
13534 consider_all_windows_p is set. */
13535 mark_window_display_accurate_1 (w, 1);
13536
13537 /* Say overlay arrows are up to date. */
13538 update_overlay_arrows (1);
13539
13540 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13541 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13542 }
13543
13544 update_mode_lines = 0;
13545 windows_or_buffers_changed = 0;
13546 cursor_type_changed = 0;
13547 }
13548
13549 /* Start SIGIO interrupts coming again. Having them off during the
13550 code above makes it less likely one will discard output, but not
13551 impossible, since there might be stuff in the system buffer here.
13552 But it is much hairier to try to do anything about that. */
13553 if (interrupt_input)
13554 request_sigio ();
13555 RESUME_POLLING;
13556
13557 /* If a frame has become visible which was not before, redisplay
13558 again, so that we display it. Expose events for such a frame
13559 (which it gets when becoming visible) don't call the parts of
13560 redisplay constructing glyphs, so simply exposing a frame won't
13561 display anything in this case. So, we have to display these
13562 frames here explicitly. */
13563 if (!pending)
13564 {
13565 int new_count = 0;
13566
13567 FOR_EACH_FRAME (tail, frame)
13568 {
13569 int this_is_visible = 0;
13570
13571 if (XFRAME (frame)->visible)
13572 this_is_visible = 1;
13573
13574 if (this_is_visible)
13575 new_count++;
13576 }
13577
13578 if (new_count != number_of_visible_frames)
13579 windows_or_buffers_changed++;
13580 }
13581
13582 /* Change frame size now if a change is pending. */
13583 do_pending_window_change (1);
13584
13585 /* If we just did a pending size change, or have additional
13586 visible frames, or selected_window changed, redisplay again. */
13587 if ((windows_or_buffers_changed && !pending)
13588 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13589 goto retry;
13590
13591 /* Clear the face and image caches.
13592
13593 We used to do this only if consider_all_windows_p. But the cache
13594 needs to be cleared if a timer creates images in the current
13595 buffer (e.g. the test case in Bug#6230). */
13596
13597 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13598 {
13599 clear_face_cache (0);
13600 clear_face_cache_count = 0;
13601 }
13602
13603 #ifdef HAVE_WINDOW_SYSTEM
13604 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13605 {
13606 clear_image_caches (Qnil);
13607 clear_image_cache_count = 0;
13608 }
13609 #endif /* HAVE_WINDOW_SYSTEM */
13610
13611 end_of_redisplay:
13612 unbind_to (count, Qnil);
13613 RESUME_POLLING;
13614 }
13615
13616
13617 /* Redisplay, but leave alone any recent echo area message unless
13618 another message has been requested in its place.
13619
13620 This is useful in situations where you need to redisplay but no
13621 user action has occurred, making it inappropriate for the message
13622 area to be cleared. See tracking_off and
13623 wait_reading_process_output for examples of these situations.
13624
13625 FROM_WHERE is an integer saying from where this function was
13626 called. This is useful for debugging. */
13627
13628 void
13629 redisplay_preserve_echo_area (int from_where)
13630 {
13631 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13632
13633 if (!NILP (echo_area_buffer[1]))
13634 {
13635 /* We have a previously displayed message, but no current
13636 message. Redisplay the previous message. */
13637 display_last_displayed_message_p = 1;
13638 redisplay_internal ();
13639 display_last_displayed_message_p = 0;
13640 }
13641 else
13642 redisplay_internal ();
13643
13644 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13645 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13646 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13647 }
13648
13649
13650 /* Function registered with record_unwind_protect in redisplay_internal. */
13651
13652 static void
13653 unwind_redisplay (void)
13654 {
13655 redisplaying_p = 0;
13656 }
13657
13658
13659 /* Mark the display of leaf window W as accurate or inaccurate.
13660 If ACCURATE_P is non-zero mark display of W as accurate. If
13661 ACCURATE_P is zero, arrange for W to be redisplayed the next
13662 time redisplay_internal is called. */
13663
13664 static void
13665 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13666 {
13667 struct buffer *b = XBUFFER (w->contents);
13668
13669 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
13670 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
13671 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13672
13673 if (accurate_p)
13674 {
13675 b->clip_changed = 0;
13676 b->prevent_redisplay_optimizations_p = 0;
13677
13678 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13679 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13680 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13681 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13682
13683 w->current_matrix->buffer = b;
13684 w->current_matrix->begv = BUF_BEGV (b);
13685 w->current_matrix->zv = BUF_ZV (b);
13686
13687 w->last_cursor_vpos = w->cursor.vpos;
13688 w->last_cursor_off_p = w->cursor_off_p;
13689
13690 if (w == XWINDOW (selected_window))
13691 w->last_point = BUF_PT (b);
13692 else
13693 w->last_point = marker_position (w->pointm);
13694
13695 w->window_end_valid = 1;
13696 w->update_mode_line = 0;
13697 }
13698 }
13699
13700
13701 /* Mark the display of windows in the window tree rooted at WINDOW as
13702 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13703 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13704 be redisplayed the next time redisplay_internal is called. */
13705
13706 void
13707 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13708 {
13709 struct window *w;
13710
13711 for (; !NILP (window); window = w->next)
13712 {
13713 w = XWINDOW (window);
13714 if (WINDOWP (w->contents))
13715 mark_window_display_accurate (w->contents, accurate_p);
13716 else
13717 mark_window_display_accurate_1 (w, accurate_p);
13718 }
13719
13720 if (accurate_p)
13721 update_overlay_arrows (1);
13722 else
13723 /* Force a thorough redisplay the next time by setting
13724 last_arrow_position and last_arrow_string to t, which is
13725 unequal to any useful value of Voverlay_arrow_... */
13726 update_overlay_arrows (-1);
13727 }
13728
13729
13730 /* Return value in display table DP (Lisp_Char_Table *) for character
13731 C. Since a display table doesn't have any parent, we don't have to
13732 follow parent. Do not call this function directly but use the
13733 macro DISP_CHAR_VECTOR. */
13734
13735 Lisp_Object
13736 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13737 {
13738 Lisp_Object val;
13739
13740 if (ASCII_CHAR_P (c))
13741 {
13742 val = dp->ascii;
13743 if (SUB_CHAR_TABLE_P (val))
13744 val = XSUB_CHAR_TABLE (val)->contents[c];
13745 }
13746 else
13747 {
13748 Lisp_Object table;
13749
13750 XSETCHAR_TABLE (table, dp);
13751 val = char_table_ref (table, c);
13752 }
13753 if (NILP (val))
13754 val = dp->defalt;
13755 return val;
13756 }
13757
13758
13759 \f
13760 /***********************************************************************
13761 Window Redisplay
13762 ***********************************************************************/
13763
13764 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13765
13766 static void
13767 redisplay_windows (Lisp_Object window)
13768 {
13769 while (!NILP (window))
13770 {
13771 struct window *w = XWINDOW (window);
13772
13773 if (WINDOWP (w->contents))
13774 redisplay_windows (w->contents);
13775 else if (BUFFERP (w->contents))
13776 {
13777 displayed_buffer = XBUFFER (w->contents);
13778 /* Use list_of_error, not Qerror, so that
13779 we catch only errors and don't run the debugger. */
13780 internal_condition_case_1 (redisplay_window_0, window,
13781 list_of_error,
13782 redisplay_window_error);
13783 }
13784
13785 window = w->next;
13786 }
13787 }
13788
13789 static Lisp_Object
13790 redisplay_window_error (Lisp_Object ignore)
13791 {
13792 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13793 return Qnil;
13794 }
13795
13796 static Lisp_Object
13797 redisplay_window_0 (Lisp_Object window)
13798 {
13799 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13800 redisplay_window (window, 0);
13801 return Qnil;
13802 }
13803
13804 static Lisp_Object
13805 redisplay_window_1 (Lisp_Object window)
13806 {
13807 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13808 redisplay_window (window, 1);
13809 return Qnil;
13810 }
13811 \f
13812
13813 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13814 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13815 which positions recorded in ROW differ from current buffer
13816 positions.
13817
13818 Return 0 if cursor is not on this row, 1 otherwise. */
13819
13820 static int
13821 set_cursor_from_row (struct window *w, struct glyph_row *row,
13822 struct glyph_matrix *matrix,
13823 ptrdiff_t delta, ptrdiff_t delta_bytes,
13824 int dy, int dvpos)
13825 {
13826 struct glyph *glyph = row->glyphs[TEXT_AREA];
13827 struct glyph *end = glyph + row->used[TEXT_AREA];
13828 struct glyph *cursor = NULL;
13829 /* The last known character position in row. */
13830 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13831 int x = row->x;
13832 ptrdiff_t pt_old = PT - delta;
13833 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13834 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13835 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13836 /* A glyph beyond the edge of TEXT_AREA which we should never
13837 touch. */
13838 struct glyph *glyphs_end = end;
13839 /* Non-zero means we've found a match for cursor position, but that
13840 glyph has the avoid_cursor_p flag set. */
13841 int match_with_avoid_cursor = 0;
13842 /* Non-zero means we've seen at least one glyph that came from a
13843 display string. */
13844 int string_seen = 0;
13845 /* Largest and smallest buffer positions seen so far during scan of
13846 glyph row. */
13847 ptrdiff_t bpos_max = pos_before;
13848 ptrdiff_t bpos_min = pos_after;
13849 /* Last buffer position covered by an overlay string with an integer
13850 `cursor' property. */
13851 ptrdiff_t bpos_covered = 0;
13852 /* Non-zero means the display string on which to display the cursor
13853 comes from a text property, not from an overlay. */
13854 int string_from_text_prop = 0;
13855
13856 /* Don't even try doing anything if called for a mode-line or
13857 header-line row, since the rest of the code isn't prepared to
13858 deal with such calamities. */
13859 eassert (!row->mode_line_p);
13860 if (row->mode_line_p)
13861 return 0;
13862
13863 /* Skip over glyphs not having an object at the start and the end of
13864 the row. These are special glyphs like truncation marks on
13865 terminal frames. */
13866 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13867 {
13868 if (!row->reversed_p)
13869 {
13870 while (glyph < end
13871 && INTEGERP (glyph->object)
13872 && glyph->charpos < 0)
13873 {
13874 x += glyph->pixel_width;
13875 ++glyph;
13876 }
13877 while (end > glyph
13878 && INTEGERP ((end - 1)->object)
13879 /* CHARPOS is zero for blanks and stretch glyphs
13880 inserted by extend_face_to_end_of_line. */
13881 && (end - 1)->charpos <= 0)
13882 --end;
13883 glyph_before = glyph - 1;
13884 glyph_after = end;
13885 }
13886 else
13887 {
13888 struct glyph *g;
13889
13890 /* If the glyph row is reversed, we need to process it from back
13891 to front, so swap the edge pointers. */
13892 glyphs_end = end = glyph - 1;
13893 glyph += row->used[TEXT_AREA] - 1;
13894
13895 while (glyph > end + 1
13896 && INTEGERP (glyph->object)
13897 && glyph->charpos < 0)
13898 {
13899 --glyph;
13900 x -= glyph->pixel_width;
13901 }
13902 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13903 --glyph;
13904 /* By default, in reversed rows we put the cursor on the
13905 rightmost (first in the reading order) glyph. */
13906 for (g = end + 1; g < glyph; g++)
13907 x += g->pixel_width;
13908 while (end < glyph
13909 && INTEGERP ((end + 1)->object)
13910 && (end + 1)->charpos <= 0)
13911 ++end;
13912 glyph_before = glyph + 1;
13913 glyph_after = end;
13914 }
13915 }
13916 else if (row->reversed_p)
13917 {
13918 /* In R2L rows that don't display text, put the cursor on the
13919 rightmost glyph. Case in point: an empty last line that is
13920 part of an R2L paragraph. */
13921 cursor = end - 1;
13922 /* Avoid placing the cursor on the last glyph of the row, where
13923 on terminal frames we hold the vertical border between
13924 adjacent windows. */
13925 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13926 && !WINDOW_RIGHTMOST_P (w)
13927 && cursor == row->glyphs[LAST_AREA] - 1)
13928 cursor--;
13929 x = -1; /* will be computed below, at label compute_x */
13930 }
13931
13932 /* Step 1: Try to find the glyph whose character position
13933 corresponds to point. If that's not possible, find 2 glyphs
13934 whose character positions are the closest to point, one before
13935 point, the other after it. */
13936 if (!row->reversed_p)
13937 while (/* not marched to end of glyph row */
13938 glyph < end
13939 /* glyph was not inserted by redisplay for internal purposes */
13940 && !INTEGERP (glyph->object))
13941 {
13942 if (BUFFERP (glyph->object))
13943 {
13944 ptrdiff_t dpos = glyph->charpos - pt_old;
13945
13946 if (glyph->charpos > bpos_max)
13947 bpos_max = glyph->charpos;
13948 if (glyph->charpos < bpos_min)
13949 bpos_min = glyph->charpos;
13950 if (!glyph->avoid_cursor_p)
13951 {
13952 /* If we hit point, we've found the glyph on which to
13953 display the cursor. */
13954 if (dpos == 0)
13955 {
13956 match_with_avoid_cursor = 0;
13957 break;
13958 }
13959 /* See if we've found a better approximation to
13960 POS_BEFORE or to POS_AFTER. */
13961 if (0 > dpos && dpos > pos_before - pt_old)
13962 {
13963 pos_before = glyph->charpos;
13964 glyph_before = glyph;
13965 }
13966 else if (0 < dpos && dpos < pos_after - pt_old)
13967 {
13968 pos_after = glyph->charpos;
13969 glyph_after = glyph;
13970 }
13971 }
13972 else if (dpos == 0)
13973 match_with_avoid_cursor = 1;
13974 }
13975 else if (STRINGP (glyph->object))
13976 {
13977 Lisp_Object chprop;
13978 ptrdiff_t glyph_pos = glyph->charpos;
13979
13980 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13981 glyph->object);
13982 if (!NILP (chprop))
13983 {
13984 /* If the string came from a `display' text property,
13985 look up the buffer position of that property and
13986 use that position to update bpos_max, as if we
13987 actually saw such a position in one of the row's
13988 glyphs. This helps with supporting integer values
13989 of `cursor' property on the display string in
13990 situations where most or all of the row's buffer
13991 text is completely covered by display properties,
13992 so that no glyph with valid buffer positions is
13993 ever seen in the row. */
13994 ptrdiff_t prop_pos =
13995 string_buffer_position_lim (glyph->object, pos_before,
13996 pos_after, 0);
13997
13998 if (prop_pos >= pos_before)
13999 bpos_max = prop_pos - 1;
14000 }
14001 if (INTEGERP (chprop))
14002 {
14003 bpos_covered = bpos_max + XINT (chprop);
14004 /* If the `cursor' property covers buffer positions up
14005 to and including point, we should display cursor on
14006 this glyph. Note that, if a `cursor' property on one
14007 of the string's characters has an integer value, we
14008 will break out of the loop below _before_ we get to
14009 the position match above. IOW, integer values of
14010 the `cursor' property override the "exact match for
14011 point" strategy of positioning the cursor. */
14012 /* Implementation note: bpos_max == pt_old when, e.g.,
14013 we are in an empty line, where bpos_max is set to
14014 MATRIX_ROW_START_CHARPOS, see above. */
14015 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14016 {
14017 cursor = glyph;
14018 break;
14019 }
14020 }
14021
14022 string_seen = 1;
14023 }
14024 x += glyph->pixel_width;
14025 ++glyph;
14026 }
14027 else if (glyph > end) /* row is reversed */
14028 while (!INTEGERP (glyph->object))
14029 {
14030 if (BUFFERP (glyph->object))
14031 {
14032 ptrdiff_t dpos = glyph->charpos - pt_old;
14033
14034 if (glyph->charpos > bpos_max)
14035 bpos_max = glyph->charpos;
14036 if (glyph->charpos < bpos_min)
14037 bpos_min = glyph->charpos;
14038 if (!glyph->avoid_cursor_p)
14039 {
14040 if (dpos == 0)
14041 {
14042 match_with_avoid_cursor = 0;
14043 break;
14044 }
14045 if (0 > dpos && dpos > pos_before - pt_old)
14046 {
14047 pos_before = glyph->charpos;
14048 glyph_before = glyph;
14049 }
14050 else if (0 < dpos && dpos < pos_after - pt_old)
14051 {
14052 pos_after = glyph->charpos;
14053 glyph_after = glyph;
14054 }
14055 }
14056 else if (dpos == 0)
14057 match_with_avoid_cursor = 1;
14058 }
14059 else if (STRINGP (glyph->object))
14060 {
14061 Lisp_Object chprop;
14062 ptrdiff_t glyph_pos = glyph->charpos;
14063
14064 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14065 glyph->object);
14066 if (!NILP (chprop))
14067 {
14068 ptrdiff_t prop_pos =
14069 string_buffer_position_lim (glyph->object, pos_before,
14070 pos_after, 0);
14071
14072 if (prop_pos >= pos_before)
14073 bpos_max = prop_pos - 1;
14074 }
14075 if (INTEGERP (chprop))
14076 {
14077 bpos_covered = bpos_max + XINT (chprop);
14078 /* If the `cursor' property covers buffer positions up
14079 to and including point, we should display cursor on
14080 this glyph. */
14081 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14082 {
14083 cursor = glyph;
14084 break;
14085 }
14086 }
14087 string_seen = 1;
14088 }
14089 --glyph;
14090 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14091 {
14092 x--; /* can't use any pixel_width */
14093 break;
14094 }
14095 x -= glyph->pixel_width;
14096 }
14097
14098 /* Step 2: If we didn't find an exact match for point, we need to
14099 look for a proper place to put the cursor among glyphs between
14100 GLYPH_BEFORE and GLYPH_AFTER. */
14101 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14102 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14103 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14104 {
14105 /* An empty line has a single glyph whose OBJECT is zero and
14106 whose CHARPOS is the position of a newline on that line.
14107 Note that on a TTY, there are more glyphs after that, which
14108 were produced by extend_face_to_end_of_line, but their
14109 CHARPOS is zero or negative. */
14110 int empty_line_p =
14111 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14112 && INTEGERP (glyph->object) && glyph->charpos > 0
14113 /* On a TTY, continued and truncated rows also have a glyph at
14114 their end whose OBJECT is zero and whose CHARPOS is
14115 positive (the continuation and truncation glyphs), but such
14116 rows are obviously not "empty". */
14117 && !(row->continued_p || row->truncated_on_right_p);
14118
14119 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14120 {
14121 ptrdiff_t ellipsis_pos;
14122
14123 /* Scan back over the ellipsis glyphs. */
14124 if (!row->reversed_p)
14125 {
14126 ellipsis_pos = (glyph - 1)->charpos;
14127 while (glyph > row->glyphs[TEXT_AREA]
14128 && (glyph - 1)->charpos == ellipsis_pos)
14129 glyph--, x -= glyph->pixel_width;
14130 /* That loop always goes one position too far, including
14131 the glyph before the ellipsis. So scan forward over
14132 that one. */
14133 x += glyph->pixel_width;
14134 glyph++;
14135 }
14136 else /* row is reversed */
14137 {
14138 ellipsis_pos = (glyph + 1)->charpos;
14139 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14140 && (glyph + 1)->charpos == ellipsis_pos)
14141 glyph++, x += glyph->pixel_width;
14142 x -= glyph->pixel_width;
14143 glyph--;
14144 }
14145 }
14146 else if (match_with_avoid_cursor)
14147 {
14148 cursor = glyph_after;
14149 x = -1;
14150 }
14151 else if (string_seen)
14152 {
14153 int incr = row->reversed_p ? -1 : +1;
14154
14155 /* Need to find the glyph that came out of a string which is
14156 present at point. That glyph is somewhere between
14157 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14158 positioned between POS_BEFORE and POS_AFTER in the
14159 buffer. */
14160 struct glyph *start, *stop;
14161 ptrdiff_t pos = pos_before;
14162
14163 x = -1;
14164
14165 /* If the row ends in a newline from a display string,
14166 reordering could have moved the glyphs belonging to the
14167 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14168 in this case we extend the search to the last glyph in
14169 the row that was not inserted by redisplay. */
14170 if (row->ends_in_newline_from_string_p)
14171 {
14172 glyph_after = end;
14173 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14174 }
14175
14176 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14177 correspond to POS_BEFORE and POS_AFTER, respectively. We
14178 need START and STOP in the order that corresponds to the
14179 row's direction as given by its reversed_p flag. If the
14180 directionality of characters between POS_BEFORE and
14181 POS_AFTER is the opposite of the row's base direction,
14182 these characters will have been reordered for display,
14183 and we need to reverse START and STOP. */
14184 if (!row->reversed_p)
14185 {
14186 start = min (glyph_before, glyph_after);
14187 stop = max (glyph_before, glyph_after);
14188 }
14189 else
14190 {
14191 start = max (glyph_before, glyph_after);
14192 stop = min (glyph_before, glyph_after);
14193 }
14194 for (glyph = start + incr;
14195 row->reversed_p ? glyph > stop : glyph < stop; )
14196 {
14197
14198 /* Any glyphs that come from the buffer are here because
14199 of bidi reordering. Skip them, and only pay
14200 attention to glyphs that came from some string. */
14201 if (STRINGP (glyph->object))
14202 {
14203 Lisp_Object str;
14204 ptrdiff_t tem;
14205 /* If the display property covers the newline, we
14206 need to search for it one position farther. */
14207 ptrdiff_t lim = pos_after
14208 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14209
14210 string_from_text_prop = 0;
14211 str = glyph->object;
14212 tem = string_buffer_position_lim (str, pos, lim, 0);
14213 if (tem == 0 /* from overlay */
14214 || pos <= tem)
14215 {
14216 /* If the string from which this glyph came is
14217 found in the buffer at point, or at position
14218 that is closer to point than pos_after, then
14219 we've found the glyph we've been looking for.
14220 If it comes from an overlay (tem == 0), and
14221 it has the `cursor' property on one of its
14222 glyphs, record that glyph as a candidate for
14223 displaying the cursor. (As in the
14224 unidirectional version, we will display the
14225 cursor on the last candidate we find.) */
14226 if (tem == 0
14227 || tem == pt_old
14228 || (tem - pt_old > 0 && tem < pos_after))
14229 {
14230 /* The glyphs from this string could have
14231 been reordered. Find the one with the
14232 smallest string position. Or there could
14233 be a character in the string with the
14234 `cursor' property, which means display
14235 cursor on that character's glyph. */
14236 ptrdiff_t strpos = glyph->charpos;
14237
14238 if (tem)
14239 {
14240 cursor = glyph;
14241 string_from_text_prop = 1;
14242 }
14243 for ( ;
14244 (row->reversed_p ? glyph > stop : glyph < stop)
14245 && EQ (glyph->object, str);
14246 glyph += incr)
14247 {
14248 Lisp_Object cprop;
14249 ptrdiff_t gpos = glyph->charpos;
14250
14251 cprop = Fget_char_property (make_number (gpos),
14252 Qcursor,
14253 glyph->object);
14254 if (!NILP (cprop))
14255 {
14256 cursor = glyph;
14257 break;
14258 }
14259 if (tem && glyph->charpos < strpos)
14260 {
14261 strpos = glyph->charpos;
14262 cursor = glyph;
14263 }
14264 }
14265
14266 if (tem == pt_old
14267 || (tem - pt_old > 0 && tem < pos_after))
14268 goto compute_x;
14269 }
14270 if (tem)
14271 pos = tem + 1; /* don't find previous instances */
14272 }
14273 /* This string is not what we want; skip all of the
14274 glyphs that came from it. */
14275 while ((row->reversed_p ? glyph > stop : glyph < stop)
14276 && EQ (glyph->object, str))
14277 glyph += incr;
14278 }
14279 else
14280 glyph += incr;
14281 }
14282
14283 /* If we reached the end of the line, and END was from a string,
14284 the cursor is not on this line. */
14285 if (cursor == NULL
14286 && (row->reversed_p ? glyph <= end : glyph >= end)
14287 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14288 && STRINGP (end->object)
14289 && row->continued_p)
14290 return 0;
14291 }
14292 /* A truncated row may not include PT among its character positions.
14293 Setting the cursor inside the scroll margin will trigger
14294 recalculation of hscroll in hscroll_window_tree. But if a
14295 display string covers point, defer to the string-handling
14296 code below to figure this out. */
14297 else if (row->truncated_on_left_p && pt_old < bpos_min)
14298 {
14299 cursor = glyph_before;
14300 x = -1;
14301 }
14302 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14303 /* Zero-width characters produce no glyphs. */
14304 || (!empty_line_p
14305 && (row->reversed_p
14306 ? glyph_after > glyphs_end
14307 : glyph_after < glyphs_end)))
14308 {
14309 cursor = glyph_after;
14310 x = -1;
14311 }
14312 }
14313
14314 compute_x:
14315 if (cursor != NULL)
14316 glyph = cursor;
14317 else if (glyph == glyphs_end
14318 && pos_before == pos_after
14319 && STRINGP ((row->reversed_p
14320 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14321 : row->glyphs[TEXT_AREA])->object))
14322 {
14323 /* If all the glyphs of this row came from strings, put the
14324 cursor on the first glyph of the row. This avoids having the
14325 cursor outside of the text area in this very rare and hard
14326 use case. */
14327 glyph =
14328 row->reversed_p
14329 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14330 : row->glyphs[TEXT_AREA];
14331 }
14332 if (x < 0)
14333 {
14334 struct glyph *g;
14335
14336 /* Need to compute x that corresponds to GLYPH. */
14337 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14338 {
14339 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14340 emacs_abort ();
14341 x += g->pixel_width;
14342 }
14343 }
14344
14345 /* ROW could be part of a continued line, which, under bidi
14346 reordering, might have other rows whose start and end charpos
14347 occlude point. Only set w->cursor if we found a better
14348 approximation to the cursor position than we have from previously
14349 examined candidate rows belonging to the same continued line. */
14350 if (/* we already have a candidate row */
14351 w->cursor.vpos >= 0
14352 /* that candidate is not the row we are processing */
14353 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14354 /* Make sure cursor.vpos specifies a row whose start and end
14355 charpos occlude point, and it is valid candidate for being a
14356 cursor-row. This is because some callers of this function
14357 leave cursor.vpos at the row where the cursor was displayed
14358 during the last redisplay cycle. */
14359 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14360 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14361 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14362 {
14363 struct glyph *g1 =
14364 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14365
14366 /* Don't consider glyphs that are outside TEXT_AREA. */
14367 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14368 return 0;
14369 /* Keep the candidate whose buffer position is the closest to
14370 point or has the `cursor' property. */
14371 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14372 w->cursor.hpos >= 0
14373 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14374 && ((BUFFERP (g1->object)
14375 && (g1->charpos == pt_old /* an exact match always wins */
14376 || (BUFFERP (glyph->object)
14377 && eabs (g1->charpos - pt_old)
14378 < eabs (glyph->charpos - pt_old))))
14379 /* previous candidate is a glyph from a string that has
14380 a non-nil `cursor' property */
14381 || (STRINGP (g1->object)
14382 && (!NILP (Fget_char_property (make_number (g1->charpos),
14383 Qcursor, g1->object))
14384 /* previous candidate is from the same display
14385 string as this one, and the display string
14386 came from a text property */
14387 || (EQ (g1->object, glyph->object)
14388 && string_from_text_prop)
14389 /* this candidate is from newline and its
14390 position is not an exact match */
14391 || (INTEGERP (glyph->object)
14392 && glyph->charpos != pt_old)))))
14393 return 0;
14394 /* If this candidate gives an exact match, use that. */
14395 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14396 /* If this candidate is a glyph created for the
14397 terminating newline of a line, and point is on that
14398 newline, it wins because it's an exact match. */
14399 || (!row->continued_p
14400 && INTEGERP (glyph->object)
14401 && glyph->charpos == 0
14402 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14403 /* Otherwise, keep the candidate that comes from a row
14404 spanning less buffer positions. This may win when one or
14405 both candidate positions are on glyphs that came from
14406 display strings, for which we cannot compare buffer
14407 positions. */
14408 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14409 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14410 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14411 return 0;
14412 }
14413 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14414 w->cursor.x = x;
14415 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14416 w->cursor.y = row->y + dy;
14417
14418 if (w == XWINDOW (selected_window))
14419 {
14420 if (!row->continued_p
14421 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14422 && row->x == 0)
14423 {
14424 this_line_buffer = XBUFFER (w->contents);
14425
14426 CHARPOS (this_line_start_pos)
14427 = MATRIX_ROW_START_CHARPOS (row) + delta;
14428 BYTEPOS (this_line_start_pos)
14429 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14430
14431 CHARPOS (this_line_end_pos)
14432 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14433 BYTEPOS (this_line_end_pos)
14434 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14435
14436 this_line_y = w->cursor.y;
14437 this_line_pixel_height = row->height;
14438 this_line_vpos = w->cursor.vpos;
14439 this_line_start_x = row->x;
14440 }
14441 else
14442 CHARPOS (this_line_start_pos) = 0;
14443 }
14444
14445 return 1;
14446 }
14447
14448
14449 /* Run window scroll functions, if any, for WINDOW with new window
14450 start STARTP. Sets the window start of WINDOW to that position.
14451
14452 We assume that the window's buffer is really current. */
14453
14454 static struct text_pos
14455 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14456 {
14457 struct window *w = XWINDOW (window);
14458 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14459
14460 eassert (current_buffer == XBUFFER (w->contents));
14461
14462 if (!NILP (Vwindow_scroll_functions))
14463 {
14464 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14465 make_number (CHARPOS (startp)));
14466 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14467 /* In case the hook functions switch buffers. */
14468 set_buffer_internal (XBUFFER (w->contents));
14469 }
14470
14471 return startp;
14472 }
14473
14474
14475 /* Make sure the line containing the cursor is fully visible.
14476 A value of 1 means there is nothing to be done.
14477 (Either the line is fully visible, or it cannot be made so,
14478 or we cannot tell.)
14479
14480 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14481 is higher than window.
14482
14483 A value of 0 means the caller should do scrolling
14484 as if point had gone off the screen. */
14485
14486 static int
14487 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14488 {
14489 struct glyph_matrix *matrix;
14490 struct glyph_row *row;
14491 int window_height;
14492
14493 if (!make_cursor_line_fully_visible_p)
14494 return 1;
14495
14496 /* It's not always possible to find the cursor, e.g, when a window
14497 is full of overlay strings. Don't do anything in that case. */
14498 if (w->cursor.vpos < 0)
14499 return 1;
14500
14501 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14502 row = MATRIX_ROW (matrix, w->cursor.vpos);
14503
14504 /* If the cursor row is not partially visible, there's nothing to do. */
14505 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14506 return 1;
14507
14508 /* If the row the cursor is in is taller than the window's height,
14509 it's not clear what to do, so do nothing. */
14510 window_height = window_box_height (w);
14511 if (row->height >= window_height)
14512 {
14513 if (!force_p || MINI_WINDOW_P (w)
14514 || w->vscroll || w->cursor.vpos == 0)
14515 return 1;
14516 }
14517 return 0;
14518 }
14519
14520
14521 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14522 non-zero means only WINDOW is redisplayed in redisplay_internal.
14523 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14524 in redisplay_window to bring a partially visible line into view in
14525 the case that only the cursor has moved.
14526
14527 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14528 last screen line's vertical height extends past the end of the screen.
14529
14530 Value is
14531
14532 1 if scrolling succeeded
14533
14534 0 if scrolling didn't find point.
14535
14536 -1 if new fonts have been loaded so that we must interrupt
14537 redisplay, adjust glyph matrices, and try again. */
14538
14539 enum
14540 {
14541 SCROLLING_SUCCESS,
14542 SCROLLING_FAILED,
14543 SCROLLING_NEED_LARGER_MATRICES
14544 };
14545
14546 /* If scroll-conservatively is more than this, never recenter.
14547
14548 If you change this, don't forget to update the doc string of
14549 `scroll-conservatively' and the Emacs manual. */
14550 #define SCROLL_LIMIT 100
14551
14552 static int
14553 try_scrolling (Lisp_Object window, int just_this_one_p,
14554 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14555 int temp_scroll_step, int last_line_misfit)
14556 {
14557 struct window *w = XWINDOW (window);
14558 struct frame *f = XFRAME (w->frame);
14559 struct text_pos pos, startp;
14560 struct it it;
14561 int this_scroll_margin, scroll_max, rc, height;
14562 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14563 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14564 Lisp_Object aggressive;
14565 /* We will never try scrolling more than this number of lines. */
14566 int scroll_limit = SCROLL_LIMIT;
14567 int frame_line_height = default_line_pixel_height (w);
14568 int window_total_lines
14569 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14570
14571 #ifdef GLYPH_DEBUG
14572 debug_method_add (w, "try_scrolling");
14573 #endif
14574
14575 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14576
14577 /* Compute scroll margin height in pixels. We scroll when point is
14578 within this distance from the top or bottom of the window. */
14579 if (scroll_margin > 0)
14580 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
14581 * frame_line_height;
14582 else
14583 this_scroll_margin = 0;
14584
14585 /* Force arg_scroll_conservatively to have a reasonable value, to
14586 avoid scrolling too far away with slow move_it_* functions. Note
14587 that the user can supply scroll-conservatively equal to
14588 `most-positive-fixnum', which can be larger than INT_MAX. */
14589 if (arg_scroll_conservatively > scroll_limit)
14590 {
14591 arg_scroll_conservatively = scroll_limit + 1;
14592 scroll_max = scroll_limit * frame_line_height;
14593 }
14594 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14595 /* Compute how much we should try to scroll maximally to bring
14596 point into view. */
14597 scroll_max = (max (scroll_step,
14598 max (arg_scroll_conservatively, temp_scroll_step))
14599 * frame_line_height);
14600 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14601 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14602 /* We're trying to scroll because of aggressive scrolling but no
14603 scroll_step is set. Choose an arbitrary one. */
14604 scroll_max = 10 * frame_line_height;
14605 else
14606 scroll_max = 0;
14607
14608 too_near_end:
14609
14610 /* Decide whether to scroll down. */
14611 if (PT > CHARPOS (startp))
14612 {
14613 int scroll_margin_y;
14614
14615 /* Compute the pixel ypos of the scroll margin, then move IT to
14616 either that ypos or PT, whichever comes first. */
14617 start_display (&it, w, startp);
14618 scroll_margin_y = it.last_visible_y - this_scroll_margin
14619 - frame_line_height * extra_scroll_margin_lines;
14620 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14621 (MOVE_TO_POS | MOVE_TO_Y));
14622
14623 if (PT > CHARPOS (it.current.pos))
14624 {
14625 int y0 = line_bottom_y (&it);
14626 /* Compute how many pixels below window bottom to stop searching
14627 for PT. This avoids costly search for PT that is far away if
14628 the user limited scrolling by a small number of lines, but
14629 always finds PT if scroll_conservatively is set to a large
14630 number, such as most-positive-fixnum. */
14631 int slack = max (scroll_max, 10 * frame_line_height);
14632 int y_to_move = it.last_visible_y + slack;
14633
14634 /* Compute the distance from the scroll margin to PT or to
14635 the scroll limit, whichever comes first. This should
14636 include the height of the cursor line, to make that line
14637 fully visible. */
14638 move_it_to (&it, PT, -1, y_to_move,
14639 -1, MOVE_TO_POS | MOVE_TO_Y);
14640 dy = line_bottom_y (&it) - y0;
14641
14642 if (dy > scroll_max)
14643 return SCROLLING_FAILED;
14644
14645 if (dy > 0)
14646 scroll_down_p = 1;
14647 }
14648 }
14649
14650 if (scroll_down_p)
14651 {
14652 /* Point is in or below the bottom scroll margin, so move the
14653 window start down. If scrolling conservatively, move it just
14654 enough down to make point visible. If scroll_step is set,
14655 move it down by scroll_step. */
14656 if (arg_scroll_conservatively)
14657 amount_to_scroll
14658 = min (max (dy, frame_line_height),
14659 frame_line_height * arg_scroll_conservatively);
14660 else if (scroll_step || temp_scroll_step)
14661 amount_to_scroll = scroll_max;
14662 else
14663 {
14664 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14665 height = WINDOW_BOX_TEXT_HEIGHT (w);
14666 if (NUMBERP (aggressive))
14667 {
14668 double float_amount = XFLOATINT (aggressive) * height;
14669 int aggressive_scroll = float_amount;
14670 if (aggressive_scroll == 0 && float_amount > 0)
14671 aggressive_scroll = 1;
14672 /* Don't let point enter the scroll margin near top of
14673 the window. This could happen if the value of
14674 scroll_up_aggressively is too large and there are
14675 non-zero margins, because scroll_up_aggressively
14676 means put point that fraction of window height
14677 _from_the_bottom_margin_. */
14678 if (aggressive_scroll + 2*this_scroll_margin > height)
14679 aggressive_scroll = height - 2*this_scroll_margin;
14680 amount_to_scroll = dy + aggressive_scroll;
14681 }
14682 }
14683
14684 if (amount_to_scroll <= 0)
14685 return SCROLLING_FAILED;
14686
14687 start_display (&it, w, startp);
14688 if (arg_scroll_conservatively <= scroll_limit)
14689 move_it_vertically (&it, amount_to_scroll);
14690 else
14691 {
14692 /* Extra precision for users who set scroll-conservatively
14693 to a large number: make sure the amount we scroll
14694 the window start is never less than amount_to_scroll,
14695 which was computed as distance from window bottom to
14696 point. This matters when lines at window top and lines
14697 below window bottom have different height. */
14698 struct it it1;
14699 void *it1data = NULL;
14700 /* We use a temporary it1 because line_bottom_y can modify
14701 its argument, if it moves one line down; see there. */
14702 int start_y;
14703
14704 SAVE_IT (it1, it, it1data);
14705 start_y = line_bottom_y (&it1);
14706 do {
14707 RESTORE_IT (&it, &it, it1data);
14708 move_it_by_lines (&it, 1);
14709 SAVE_IT (it1, it, it1data);
14710 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14711 }
14712
14713 /* If STARTP is unchanged, move it down another screen line. */
14714 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14715 move_it_by_lines (&it, 1);
14716 startp = it.current.pos;
14717 }
14718 else
14719 {
14720 struct text_pos scroll_margin_pos = startp;
14721 int y_offset = 0;
14722
14723 /* See if point is inside the scroll margin at the top of the
14724 window. */
14725 if (this_scroll_margin)
14726 {
14727 int y_start;
14728
14729 start_display (&it, w, startp);
14730 y_start = it.current_y;
14731 move_it_vertically (&it, this_scroll_margin);
14732 scroll_margin_pos = it.current.pos;
14733 /* If we didn't move enough before hitting ZV, request
14734 additional amount of scroll, to move point out of the
14735 scroll margin. */
14736 if (IT_CHARPOS (it) == ZV
14737 && it.current_y - y_start < this_scroll_margin)
14738 y_offset = this_scroll_margin - (it.current_y - y_start);
14739 }
14740
14741 if (PT < CHARPOS (scroll_margin_pos))
14742 {
14743 /* Point is in the scroll margin at the top of the window or
14744 above what is displayed in the window. */
14745 int y0, y_to_move;
14746
14747 /* Compute the vertical distance from PT to the scroll
14748 margin position. Move as far as scroll_max allows, or
14749 one screenful, or 10 screen lines, whichever is largest.
14750 Give up if distance is greater than scroll_max or if we
14751 didn't reach the scroll margin position. */
14752 SET_TEXT_POS (pos, PT, PT_BYTE);
14753 start_display (&it, w, pos);
14754 y0 = it.current_y;
14755 y_to_move = max (it.last_visible_y,
14756 max (scroll_max, 10 * frame_line_height));
14757 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14758 y_to_move, -1,
14759 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14760 dy = it.current_y - y0;
14761 if (dy > scroll_max
14762 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
14763 return SCROLLING_FAILED;
14764
14765 /* Additional scroll for when ZV was too close to point. */
14766 dy += y_offset;
14767
14768 /* Compute new window start. */
14769 start_display (&it, w, startp);
14770
14771 if (arg_scroll_conservatively)
14772 amount_to_scroll = max (dy, frame_line_height *
14773 max (scroll_step, temp_scroll_step));
14774 else if (scroll_step || temp_scroll_step)
14775 amount_to_scroll = scroll_max;
14776 else
14777 {
14778 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14779 height = WINDOW_BOX_TEXT_HEIGHT (w);
14780 if (NUMBERP (aggressive))
14781 {
14782 double float_amount = XFLOATINT (aggressive) * height;
14783 int aggressive_scroll = float_amount;
14784 if (aggressive_scroll == 0 && float_amount > 0)
14785 aggressive_scroll = 1;
14786 /* Don't let point enter the scroll margin near
14787 bottom of the window, if the value of
14788 scroll_down_aggressively happens to be too
14789 large. */
14790 if (aggressive_scroll + 2*this_scroll_margin > height)
14791 aggressive_scroll = height - 2*this_scroll_margin;
14792 amount_to_scroll = dy + aggressive_scroll;
14793 }
14794 }
14795
14796 if (amount_to_scroll <= 0)
14797 return SCROLLING_FAILED;
14798
14799 move_it_vertically_backward (&it, amount_to_scroll);
14800 startp = it.current.pos;
14801 }
14802 }
14803
14804 /* Run window scroll functions. */
14805 startp = run_window_scroll_functions (window, startp);
14806
14807 /* Display the window. Give up if new fonts are loaded, or if point
14808 doesn't appear. */
14809 if (!try_window (window, startp, 0))
14810 rc = SCROLLING_NEED_LARGER_MATRICES;
14811 else if (w->cursor.vpos < 0)
14812 {
14813 clear_glyph_matrix (w->desired_matrix);
14814 rc = SCROLLING_FAILED;
14815 }
14816 else
14817 {
14818 /* Maybe forget recorded base line for line number display. */
14819 if (!just_this_one_p
14820 || current_buffer->clip_changed
14821 || BEG_UNCHANGED < CHARPOS (startp))
14822 w->base_line_number = 0;
14823
14824 /* If cursor ends up on a partially visible line,
14825 treat that as being off the bottom of the screen. */
14826 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14827 /* It's possible that the cursor is on the first line of the
14828 buffer, which is partially obscured due to a vscroll
14829 (Bug#7537). In that case, avoid looping forever . */
14830 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14831 {
14832 clear_glyph_matrix (w->desired_matrix);
14833 ++extra_scroll_margin_lines;
14834 goto too_near_end;
14835 }
14836 rc = SCROLLING_SUCCESS;
14837 }
14838
14839 return rc;
14840 }
14841
14842
14843 /* Compute a suitable window start for window W if display of W starts
14844 on a continuation line. Value is non-zero if a new window start
14845 was computed.
14846
14847 The new window start will be computed, based on W's width, starting
14848 from the start of the continued line. It is the start of the
14849 screen line with the minimum distance from the old start W->start. */
14850
14851 static int
14852 compute_window_start_on_continuation_line (struct window *w)
14853 {
14854 struct text_pos pos, start_pos;
14855 int window_start_changed_p = 0;
14856
14857 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14858
14859 /* If window start is on a continuation line... Window start may be
14860 < BEGV in case there's invisible text at the start of the
14861 buffer (M-x rmail, for example). */
14862 if (CHARPOS (start_pos) > BEGV
14863 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14864 {
14865 struct it it;
14866 struct glyph_row *row;
14867
14868 /* Handle the case that the window start is out of range. */
14869 if (CHARPOS (start_pos) < BEGV)
14870 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14871 else if (CHARPOS (start_pos) > ZV)
14872 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14873
14874 /* Find the start of the continued line. This should be fast
14875 because find_newline is fast (newline cache). */
14876 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14877 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14878 row, DEFAULT_FACE_ID);
14879 reseat_at_previous_visible_line_start (&it);
14880
14881 /* If the line start is "too far" away from the window start,
14882 say it takes too much time to compute a new window start. */
14883 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14884 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14885 {
14886 int min_distance, distance;
14887
14888 /* Move forward by display lines to find the new window
14889 start. If window width was enlarged, the new start can
14890 be expected to be > the old start. If window width was
14891 decreased, the new window start will be < the old start.
14892 So, we're looking for the display line start with the
14893 minimum distance from the old window start. */
14894 pos = it.current.pos;
14895 min_distance = INFINITY;
14896 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14897 distance < min_distance)
14898 {
14899 min_distance = distance;
14900 pos = it.current.pos;
14901 if (it.line_wrap == WORD_WRAP)
14902 {
14903 /* Under WORD_WRAP, move_it_by_lines is likely to
14904 overshoot and stop not at the first, but the
14905 second character from the left margin. So in
14906 that case, we need a more tight control on the X
14907 coordinate of the iterator than move_it_by_lines
14908 promises in its contract. The method is to first
14909 go to the last (rightmost) visible character of a
14910 line, then move to the leftmost character on the
14911 next line in a separate call. */
14912 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
14913 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14914 move_it_to (&it, ZV, 0,
14915 it.current_y + it.max_ascent + it.max_descent, -1,
14916 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14917 }
14918 else
14919 move_it_by_lines (&it, 1);
14920 }
14921
14922 /* Set the window start there. */
14923 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14924 window_start_changed_p = 1;
14925 }
14926 }
14927
14928 return window_start_changed_p;
14929 }
14930
14931
14932 /* Try cursor movement in case text has not changed in window WINDOW,
14933 with window start STARTP. Value is
14934
14935 CURSOR_MOVEMENT_SUCCESS if successful
14936
14937 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14938
14939 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14940 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14941 we want to scroll as if scroll-step were set to 1. See the code.
14942
14943 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14944 which case we have to abort this redisplay, and adjust matrices
14945 first. */
14946
14947 enum
14948 {
14949 CURSOR_MOVEMENT_SUCCESS,
14950 CURSOR_MOVEMENT_CANNOT_BE_USED,
14951 CURSOR_MOVEMENT_MUST_SCROLL,
14952 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14953 };
14954
14955 static int
14956 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14957 {
14958 struct window *w = XWINDOW (window);
14959 struct frame *f = XFRAME (w->frame);
14960 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14961
14962 #ifdef GLYPH_DEBUG
14963 if (inhibit_try_cursor_movement)
14964 return rc;
14965 #endif
14966
14967 /* Previously, there was a check for Lisp integer in the
14968 if-statement below. Now, this field is converted to
14969 ptrdiff_t, thus zero means invalid position in a buffer. */
14970 eassert (w->last_point > 0);
14971 /* Likewise there was a check whether window_end_vpos is nil or larger
14972 than the window. Now window_end_vpos is int and so never nil, but
14973 let's leave eassert to check whether it fits in the window. */
14974 eassert (w->window_end_vpos < w->current_matrix->nrows);
14975
14976 /* Handle case where text has not changed, only point, and it has
14977 not moved off the frame. */
14978 if (/* Point may be in this window. */
14979 PT >= CHARPOS (startp)
14980 /* Selective display hasn't changed. */
14981 && !current_buffer->clip_changed
14982 /* Function force-mode-line-update is used to force a thorough
14983 redisplay. It sets either windows_or_buffers_changed or
14984 update_mode_lines. So don't take a shortcut here for these
14985 cases. */
14986 && !update_mode_lines
14987 && !windows_or_buffers_changed
14988 && !cursor_type_changed
14989 /* Can't use this case if highlighting a region. When a
14990 region exists, cursor movement has to do more than just
14991 set the cursor. */
14992 && markpos_of_region () < 0
14993 && !w->region_showing
14994 && NILP (Vshow_trailing_whitespace)
14995 /* This code is not used for mini-buffer for the sake of the case
14996 of redisplaying to replace an echo area message; since in
14997 that case the mini-buffer contents per se are usually
14998 unchanged. This code is of no real use in the mini-buffer
14999 since the handling of this_line_start_pos, etc., in redisplay
15000 handles the same cases. */
15001 && !EQ (window, minibuf_window)
15002 && (FRAME_WINDOW_P (f)
15003 || !overlay_arrow_in_current_buffer_p ()))
15004 {
15005 int this_scroll_margin, top_scroll_margin;
15006 struct glyph_row *row = NULL;
15007 int frame_line_height = default_line_pixel_height (w);
15008 int window_total_lines
15009 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15010
15011 #ifdef GLYPH_DEBUG
15012 debug_method_add (w, "cursor movement");
15013 #endif
15014
15015 /* Scroll if point within this distance from the top or bottom
15016 of the window. This is a pixel value. */
15017 if (scroll_margin > 0)
15018 {
15019 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15020 this_scroll_margin *= frame_line_height;
15021 }
15022 else
15023 this_scroll_margin = 0;
15024
15025 top_scroll_margin = this_scroll_margin;
15026 if (WINDOW_WANTS_HEADER_LINE_P (w))
15027 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15028
15029 /* Start with the row the cursor was displayed during the last
15030 not paused redisplay. Give up if that row is not valid. */
15031 if (w->last_cursor_vpos < 0
15032 || w->last_cursor_vpos >= w->current_matrix->nrows)
15033 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15034 else
15035 {
15036 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15037 if (row->mode_line_p)
15038 ++row;
15039 if (!row->enabled_p)
15040 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15041 }
15042
15043 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15044 {
15045 int scroll_p = 0, must_scroll = 0;
15046 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15047
15048 if (PT > w->last_point)
15049 {
15050 /* Point has moved forward. */
15051 while (MATRIX_ROW_END_CHARPOS (row) < PT
15052 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15053 {
15054 eassert (row->enabled_p);
15055 ++row;
15056 }
15057
15058 /* If the end position of a row equals the start
15059 position of the next row, and PT is at that position,
15060 we would rather display cursor in the next line. */
15061 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15062 && MATRIX_ROW_END_CHARPOS (row) == PT
15063 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15064 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15065 && !cursor_row_p (row))
15066 ++row;
15067
15068 /* If within the scroll margin, scroll. Note that
15069 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15070 the next line would be drawn, and that
15071 this_scroll_margin can be zero. */
15072 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15073 || PT > MATRIX_ROW_END_CHARPOS (row)
15074 /* Line is completely visible last line in window
15075 and PT is to be set in the next line. */
15076 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15077 && PT == MATRIX_ROW_END_CHARPOS (row)
15078 && !row->ends_at_zv_p
15079 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15080 scroll_p = 1;
15081 }
15082 else if (PT < w->last_point)
15083 {
15084 /* Cursor has to be moved backward. Note that PT >=
15085 CHARPOS (startp) because of the outer if-statement. */
15086 while (!row->mode_line_p
15087 && (MATRIX_ROW_START_CHARPOS (row) > PT
15088 || (MATRIX_ROW_START_CHARPOS (row) == PT
15089 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15090 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15091 row > w->current_matrix->rows
15092 && (row-1)->ends_in_newline_from_string_p))))
15093 && (row->y > top_scroll_margin
15094 || CHARPOS (startp) == BEGV))
15095 {
15096 eassert (row->enabled_p);
15097 --row;
15098 }
15099
15100 /* Consider the following case: Window starts at BEGV,
15101 there is invisible, intangible text at BEGV, so that
15102 display starts at some point START > BEGV. It can
15103 happen that we are called with PT somewhere between
15104 BEGV and START. Try to handle that case. */
15105 if (row < w->current_matrix->rows
15106 || row->mode_line_p)
15107 {
15108 row = w->current_matrix->rows;
15109 if (row->mode_line_p)
15110 ++row;
15111 }
15112
15113 /* Due to newlines in overlay strings, we may have to
15114 skip forward over overlay strings. */
15115 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15116 && MATRIX_ROW_END_CHARPOS (row) == PT
15117 && !cursor_row_p (row))
15118 ++row;
15119
15120 /* If within the scroll margin, scroll. */
15121 if (row->y < top_scroll_margin
15122 && CHARPOS (startp) != BEGV)
15123 scroll_p = 1;
15124 }
15125 else
15126 {
15127 /* Cursor did not move. So don't scroll even if cursor line
15128 is partially visible, as it was so before. */
15129 rc = CURSOR_MOVEMENT_SUCCESS;
15130 }
15131
15132 if (PT < MATRIX_ROW_START_CHARPOS (row)
15133 || PT > MATRIX_ROW_END_CHARPOS (row))
15134 {
15135 /* if PT is not in the glyph row, give up. */
15136 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15137 must_scroll = 1;
15138 }
15139 else if (rc != CURSOR_MOVEMENT_SUCCESS
15140 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15141 {
15142 struct glyph_row *row1;
15143
15144 /* If rows are bidi-reordered and point moved, back up
15145 until we find a row that does not belong to a
15146 continuation line. This is because we must consider
15147 all rows of a continued line as candidates for the
15148 new cursor positioning, since row start and end
15149 positions change non-linearly with vertical position
15150 in such rows. */
15151 /* FIXME: Revisit this when glyph ``spilling'' in
15152 continuation lines' rows is implemented for
15153 bidi-reordered rows. */
15154 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15155 MATRIX_ROW_CONTINUATION_LINE_P (row);
15156 --row)
15157 {
15158 /* If we hit the beginning of the displayed portion
15159 without finding the first row of a continued
15160 line, give up. */
15161 if (row <= row1)
15162 {
15163 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15164 break;
15165 }
15166 eassert (row->enabled_p);
15167 }
15168 }
15169 if (must_scroll)
15170 ;
15171 else if (rc != CURSOR_MOVEMENT_SUCCESS
15172 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15173 /* Make sure this isn't a header line by any chance, since
15174 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15175 && !row->mode_line_p
15176 && make_cursor_line_fully_visible_p)
15177 {
15178 if (PT == MATRIX_ROW_END_CHARPOS (row)
15179 && !row->ends_at_zv_p
15180 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15181 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15182 else if (row->height > window_box_height (w))
15183 {
15184 /* If we end up in a partially visible line, let's
15185 make it fully visible, except when it's taller
15186 than the window, in which case we can't do much
15187 about it. */
15188 *scroll_step = 1;
15189 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15190 }
15191 else
15192 {
15193 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15194 if (!cursor_row_fully_visible_p (w, 0, 1))
15195 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15196 else
15197 rc = CURSOR_MOVEMENT_SUCCESS;
15198 }
15199 }
15200 else if (scroll_p)
15201 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15202 else if (rc != CURSOR_MOVEMENT_SUCCESS
15203 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15204 {
15205 /* With bidi-reordered rows, there could be more than
15206 one candidate row whose start and end positions
15207 occlude point. We need to let set_cursor_from_row
15208 find the best candidate. */
15209 /* FIXME: Revisit this when glyph ``spilling'' in
15210 continuation lines' rows is implemented for
15211 bidi-reordered rows. */
15212 int rv = 0;
15213
15214 do
15215 {
15216 int at_zv_p = 0, exact_match_p = 0;
15217
15218 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15219 && PT <= MATRIX_ROW_END_CHARPOS (row)
15220 && cursor_row_p (row))
15221 rv |= set_cursor_from_row (w, row, w->current_matrix,
15222 0, 0, 0, 0);
15223 /* As soon as we've found the exact match for point,
15224 or the first suitable row whose ends_at_zv_p flag
15225 is set, we are done. */
15226 at_zv_p =
15227 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15228 if (rv && !at_zv_p
15229 && w->cursor.hpos >= 0
15230 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15231 w->cursor.vpos))
15232 {
15233 struct glyph_row *candidate =
15234 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15235 struct glyph *g =
15236 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15237 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15238
15239 exact_match_p =
15240 (BUFFERP (g->object) && g->charpos == PT)
15241 || (INTEGERP (g->object)
15242 && (g->charpos == PT
15243 || (g->charpos == 0 && endpos - 1 == PT)));
15244 }
15245 if (rv && (at_zv_p || exact_match_p))
15246 {
15247 rc = CURSOR_MOVEMENT_SUCCESS;
15248 break;
15249 }
15250 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15251 break;
15252 ++row;
15253 }
15254 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15255 || row->continued_p)
15256 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15257 || (MATRIX_ROW_START_CHARPOS (row) == PT
15258 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15259 /* If we didn't find any candidate rows, or exited the
15260 loop before all the candidates were examined, signal
15261 to the caller that this method failed. */
15262 if (rc != CURSOR_MOVEMENT_SUCCESS
15263 && !(rv
15264 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15265 && !row->continued_p))
15266 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15267 else if (rv)
15268 rc = CURSOR_MOVEMENT_SUCCESS;
15269 }
15270 else
15271 {
15272 do
15273 {
15274 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15275 {
15276 rc = CURSOR_MOVEMENT_SUCCESS;
15277 break;
15278 }
15279 ++row;
15280 }
15281 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15282 && MATRIX_ROW_START_CHARPOS (row) == PT
15283 && cursor_row_p (row));
15284 }
15285 }
15286 }
15287
15288 return rc;
15289 }
15290
15291 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15292 static
15293 #endif
15294 void
15295 set_vertical_scroll_bar (struct window *w)
15296 {
15297 ptrdiff_t start, end, whole;
15298
15299 /* Calculate the start and end positions for the current window.
15300 At some point, it would be nice to choose between scrollbars
15301 which reflect the whole buffer size, with special markers
15302 indicating narrowing, and scrollbars which reflect only the
15303 visible region.
15304
15305 Note that mini-buffers sometimes aren't displaying any text. */
15306 if (!MINI_WINDOW_P (w)
15307 || (w == XWINDOW (minibuf_window)
15308 && NILP (echo_area_buffer[0])))
15309 {
15310 struct buffer *buf = XBUFFER (w->contents);
15311 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15312 start = marker_position (w->start) - BUF_BEGV (buf);
15313 /* I don't think this is guaranteed to be right. For the
15314 moment, we'll pretend it is. */
15315 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15316
15317 if (end < start)
15318 end = start;
15319 if (whole < (end - start))
15320 whole = end - start;
15321 }
15322 else
15323 start = end = whole = 0;
15324
15325 /* Indicate what this scroll bar ought to be displaying now. */
15326 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15327 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15328 (w, end - start, whole, start);
15329 }
15330
15331
15332 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15333 selected_window is redisplayed.
15334
15335 We can return without actually redisplaying the window if
15336 fonts_changed_p. In that case, redisplay_internal will
15337 retry. */
15338
15339 static void
15340 redisplay_window (Lisp_Object window, int just_this_one_p)
15341 {
15342 struct window *w = XWINDOW (window);
15343 struct frame *f = XFRAME (w->frame);
15344 struct buffer *buffer = XBUFFER (w->contents);
15345 struct buffer *old = current_buffer;
15346 struct text_pos lpoint, opoint, startp;
15347 int update_mode_line;
15348 int tem;
15349 struct it it;
15350 /* Record it now because it's overwritten. */
15351 int current_matrix_up_to_date_p = 0;
15352 int used_current_matrix_p = 0;
15353 /* This is less strict than current_matrix_up_to_date_p.
15354 It indicates that the buffer contents and narrowing are unchanged. */
15355 int buffer_unchanged_p = 0;
15356 int temp_scroll_step = 0;
15357 ptrdiff_t count = SPECPDL_INDEX ();
15358 int rc;
15359 int centering_position = -1;
15360 int last_line_misfit = 0;
15361 ptrdiff_t beg_unchanged, end_unchanged;
15362 int frame_line_height;
15363
15364 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15365 opoint = lpoint;
15366
15367 #ifdef GLYPH_DEBUG
15368 *w->desired_matrix->method = 0;
15369 #endif
15370
15371 /* Make sure that both W's markers are valid. */
15372 eassert (XMARKER (w->start)->buffer == buffer);
15373 eassert (XMARKER (w->pointm)->buffer == buffer);
15374
15375 restart:
15376 reconsider_clip_changes (w);
15377 frame_line_height = default_line_pixel_height (w);
15378
15379 /* Has the mode line to be updated? */
15380 update_mode_line = (w->update_mode_line
15381 || update_mode_lines
15382 || buffer->clip_changed
15383 || buffer->prevent_redisplay_optimizations_p);
15384
15385 if (MINI_WINDOW_P (w))
15386 {
15387 if (w == XWINDOW (echo_area_window)
15388 && !NILP (echo_area_buffer[0]))
15389 {
15390 if (update_mode_line)
15391 /* We may have to update a tty frame's menu bar or a
15392 tool-bar. Example `M-x C-h C-h C-g'. */
15393 goto finish_menu_bars;
15394 else
15395 /* We've already displayed the echo area glyphs in this window. */
15396 goto finish_scroll_bars;
15397 }
15398 else if ((w != XWINDOW (minibuf_window)
15399 || minibuf_level == 0)
15400 /* When buffer is nonempty, redisplay window normally. */
15401 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15402 /* Quail displays non-mini buffers in minibuffer window.
15403 In that case, redisplay the window normally. */
15404 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15405 {
15406 /* W is a mini-buffer window, but it's not active, so clear
15407 it. */
15408 int yb = window_text_bottom_y (w);
15409 struct glyph_row *row;
15410 int y;
15411
15412 for (y = 0, row = w->desired_matrix->rows;
15413 y < yb;
15414 y += row->height, ++row)
15415 blank_row (w, row, y);
15416 goto finish_scroll_bars;
15417 }
15418
15419 clear_glyph_matrix (w->desired_matrix);
15420 }
15421
15422 /* Otherwise set up data on this window; select its buffer and point
15423 value. */
15424 /* Really select the buffer, for the sake of buffer-local
15425 variables. */
15426 set_buffer_internal_1 (XBUFFER (w->contents));
15427
15428 current_matrix_up_to_date_p
15429 = (w->window_end_valid
15430 && !current_buffer->clip_changed
15431 && !current_buffer->prevent_redisplay_optimizations_p
15432 && !window_outdated (w));
15433
15434 /* Run the window-bottom-change-functions
15435 if it is possible that the text on the screen has changed
15436 (either due to modification of the text, or any other reason). */
15437 if (!current_matrix_up_to_date_p
15438 && !NILP (Vwindow_text_change_functions))
15439 {
15440 safe_run_hooks (Qwindow_text_change_functions);
15441 goto restart;
15442 }
15443
15444 beg_unchanged = BEG_UNCHANGED;
15445 end_unchanged = END_UNCHANGED;
15446
15447 SET_TEXT_POS (opoint, PT, PT_BYTE);
15448
15449 specbind (Qinhibit_point_motion_hooks, Qt);
15450
15451 buffer_unchanged_p
15452 = (w->window_end_valid
15453 && !current_buffer->clip_changed
15454 && !window_outdated (w));
15455
15456 /* When windows_or_buffers_changed is non-zero, we can't rely
15457 on the window end being valid, so set it to zero there. */
15458 if (windows_or_buffers_changed)
15459 {
15460 /* If window starts on a continuation line, maybe adjust the
15461 window start in case the window's width changed. */
15462 if (XMARKER (w->start)->buffer == current_buffer)
15463 compute_window_start_on_continuation_line (w);
15464
15465 w->window_end_valid = 0;
15466 /* If so, we also can't rely on current matrix
15467 and should not fool try_cursor_movement below. */
15468 current_matrix_up_to_date_p = 0;
15469 }
15470
15471 /* Some sanity checks. */
15472 CHECK_WINDOW_END (w);
15473 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15474 emacs_abort ();
15475 if (BYTEPOS (opoint) < CHARPOS (opoint))
15476 emacs_abort ();
15477
15478 if (mode_line_update_needed (w))
15479 update_mode_line = 1;
15480
15481 /* Point refers normally to the selected window. For any other
15482 window, set up appropriate value. */
15483 if (!EQ (window, selected_window))
15484 {
15485 ptrdiff_t new_pt = marker_position (w->pointm);
15486 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15487 if (new_pt < BEGV)
15488 {
15489 new_pt = BEGV;
15490 new_pt_byte = BEGV_BYTE;
15491 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15492 }
15493 else if (new_pt > (ZV - 1))
15494 {
15495 new_pt = ZV;
15496 new_pt_byte = ZV_BYTE;
15497 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15498 }
15499
15500 /* We don't use SET_PT so that the point-motion hooks don't run. */
15501 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15502 }
15503
15504 /* If any of the character widths specified in the display table
15505 have changed, invalidate the width run cache. It's true that
15506 this may be a bit late to catch such changes, but the rest of
15507 redisplay goes (non-fatally) haywire when the display table is
15508 changed, so why should we worry about doing any better? */
15509 if (current_buffer->width_run_cache)
15510 {
15511 struct Lisp_Char_Table *disptab = buffer_display_table ();
15512
15513 if (! disptab_matches_widthtab
15514 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15515 {
15516 invalidate_region_cache (current_buffer,
15517 current_buffer->width_run_cache,
15518 BEG, Z);
15519 recompute_width_table (current_buffer, disptab);
15520 }
15521 }
15522
15523 /* If window-start is screwed up, choose a new one. */
15524 if (XMARKER (w->start)->buffer != current_buffer)
15525 goto recenter;
15526
15527 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15528
15529 /* If someone specified a new starting point but did not insist,
15530 check whether it can be used. */
15531 if (w->optional_new_start
15532 && CHARPOS (startp) >= BEGV
15533 && CHARPOS (startp) <= ZV)
15534 {
15535 w->optional_new_start = 0;
15536 start_display (&it, w, startp);
15537 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15538 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15539 if (IT_CHARPOS (it) == PT)
15540 w->force_start = 1;
15541 /* IT may overshoot PT if text at PT is invisible. */
15542 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15543 w->force_start = 1;
15544 }
15545
15546 force_start:
15547
15548 /* Handle case where place to start displaying has been specified,
15549 unless the specified location is outside the accessible range. */
15550 if (w->force_start || window_frozen_p (w))
15551 {
15552 /* We set this later on if we have to adjust point. */
15553 int new_vpos = -1;
15554
15555 w->force_start = 0;
15556 w->vscroll = 0;
15557 w->window_end_valid = 0;
15558
15559 /* Forget any recorded base line for line number display. */
15560 if (!buffer_unchanged_p)
15561 w->base_line_number = 0;
15562
15563 /* Redisplay the mode line. Select the buffer properly for that.
15564 Also, run the hook window-scroll-functions
15565 because we have scrolled. */
15566 /* Note, we do this after clearing force_start because
15567 if there's an error, it is better to forget about force_start
15568 than to get into an infinite loop calling the hook functions
15569 and having them get more errors. */
15570 if (!update_mode_line
15571 || ! NILP (Vwindow_scroll_functions))
15572 {
15573 update_mode_line = 1;
15574 w->update_mode_line = 1;
15575 startp = run_window_scroll_functions (window, startp);
15576 }
15577
15578 if (CHARPOS (startp) < BEGV)
15579 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15580 else if (CHARPOS (startp) > ZV)
15581 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15582
15583 /* Redisplay, then check if cursor has been set during the
15584 redisplay. Give up if new fonts were loaded. */
15585 /* We used to issue a CHECK_MARGINS argument to try_window here,
15586 but this causes scrolling to fail when point begins inside
15587 the scroll margin (bug#148) -- cyd */
15588 if (!try_window (window, startp, 0))
15589 {
15590 w->force_start = 1;
15591 clear_glyph_matrix (w->desired_matrix);
15592 goto need_larger_matrices;
15593 }
15594
15595 if (w->cursor.vpos < 0 && !window_frozen_p (w))
15596 {
15597 /* If point does not appear, try to move point so it does
15598 appear. The desired matrix has been built above, so we
15599 can use it here. */
15600 new_vpos = window_box_height (w) / 2;
15601 }
15602
15603 if (!cursor_row_fully_visible_p (w, 0, 0))
15604 {
15605 /* Point does appear, but on a line partly visible at end of window.
15606 Move it back to a fully-visible line. */
15607 new_vpos = window_box_height (w);
15608 }
15609 else if (w->cursor.vpos >=0)
15610 {
15611 /* Some people insist on not letting point enter the scroll
15612 margin, even though this part handles windows that didn't
15613 scroll at all. */
15614 int window_total_lines
15615 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15616 int margin = min (scroll_margin, window_total_lines / 4);
15617 int pixel_margin = margin * frame_line_height;
15618 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15619
15620 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15621 below, which finds the row to move point to, advances by
15622 the Y coordinate of the _next_ row, see the definition of
15623 MATRIX_ROW_BOTTOM_Y. */
15624 if (w->cursor.vpos < margin + header_line)
15625 {
15626 w->cursor.vpos = -1;
15627 clear_glyph_matrix (w->desired_matrix);
15628 goto try_to_scroll;
15629 }
15630 else
15631 {
15632 int window_height = window_box_height (w);
15633
15634 if (header_line)
15635 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15636 if (w->cursor.y >= window_height - pixel_margin)
15637 {
15638 w->cursor.vpos = -1;
15639 clear_glyph_matrix (w->desired_matrix);
15640 goto try_to_scroll;
15641 }
15642 }
15643 }
15644
15645 /* If we need to move point for either of the above reasons,
15646 now actually do it. */
15647 if (new_vpos >= 0)
15648 {
15649 struct glyph_row *row;
15650
15651 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15652 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15653 ++row;
15654
15655 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15656 MATRIX_ROW_START_BYTEPOS (row));
15657
15658 if (w != XWINDOW (selected_window))
15659 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15660 else if (current_buffer == old)
15661 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15662
15663 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15664
15665 /* If we are highlighting the region, then we just changed
15666 the region, so redisplay to show it. */
15667 if (markpos_of_region () >= 0)
15668 {
15669 clear_glyph_matrix (w->desired_matrix);
15670 if (!try_window (window, startp, 0))
15671 goto need_larger_matrices;
15672 }
15673 }
15674
15675 #ifdef GLYPH_DEBUG
15676 debug_method_add (w, "forced window start");
15677 #endif
15678 goto done;
15679 }
15680
15681 /* Handle case where text has not changed, only point, and it has
15682 not moved off the frame, and we are not retrying after hscroll.
15683 (current_matrix_up_to_date_p is nonzero when retrying.) */
15684 if (current_matrix_up_to_date_p
15685 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15686 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15687 {
15688 switch (rc)
15689 {
15690 case CURSOR_MOVEMENT_SUCCESS:
15691 used_current_matrix_p = 1;
15692 goto done;
15693
15694 case CURSOR_MOVEMENT_MUST_SCROLL:
15695 goto try_to_scroll;
15696
15697 default:
15698 emacs_abort ();
15699 }
15700 }
15701 /* If current starting point was originally the beginning of a line
15702 but no longer is, find a new starting point. */
15703 else if (w->start_at_line_beg
15704 && !(CHARPOS (startp) <= BEGV
15705 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15706 {
15707 #ifdef GLYPH_DEBUG
15708 debug_method_add (w, "recenter 1");
15709 #endif
15710 goto recenter;
15711 }
15712
15713 /* Try scrolling with try_window_id. Value is > 0 if update has
15714 been done, it is -1 if we know that the same window start will
15715 not work. It is 0 if unsuccessful for some other reason. */
15716 else if ((tem = try_window_id (w)) != 0)
15717 {
15718 #ifdef GLYPH_DEBUG
15719 debug_method_add (w, "try_window_id %d", tem);
15720 #endif
15721
15722 if (fonts_changed_p)
15723 goto need_larger_matrices;
15724 if (tem > 0)
15725 goto done;
15726
15727 /* Otherwise try_window_id has returned -1 which means that we
15728 don't want the alternative below this comment to execute. */
15729 }
15730 else if (CHARPOS (startp) >= BEGV
15731 && CHARPOS (startp) <= ZV
15732 && PT >= CHARPOS (startp)
15733 && (CHARPOS (startp) < ZV
15734 /* Avoid starting at end of buffer. */
15735 || CHARPOS (startp) == BEGV
15736 || !window_outdated (w)))
15737 {
15738 int d1, d2, d3, d4, d5, d6;
15739
15740 /* If first window line is a continuation line, and window start
15741 is inside the modified region, but the first change is before
15742 current window start, we must select a new window start.
15743
15744 However, if this is the result of a down-mouse event (e.g. by
15745 extending the mouse-drag-overlay), we don't want to select a
15746 new window start, since that would change the position under
15747 the mouse, resulting in an unwanted mouse-movement rather
15748 than a simple mouse-click. */
15749 if (!w->start_at_line_beg
15750 && NILP (do_mouse_tracking)
15751 && CHARPOS (startp) > BEGV
15752 && CHARPOS (startp) > BEG + beg_unchanged
15753 && CHARPOS (startp) <= Z - end_unchanged
15754 /* Even if w->start_at_line_beg is nil, a new window may
15755 start at a line_beg, since that's how set_buffer_window
15756 sets it. So, we need to check the return value of
15757 compute_window_start_on_continuation_line. (See also
15758 bug#197). */
15759 && XMARKER (w->start)->buffer == current_buffer
15760 && compute_window_start_on_continuation_line (w)
15761 /* It doesn't make sense to force the window start like we
15762 do at label force_start if it is already known that point
15763 will not be visible in the resulting window, because
15764 doing so will move point from its correct position
15765 instead of scrolling the window to bring point into view.
15766 See bug#9324. */
15767 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15768 {
15769 w->force_start = 1;
15770 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15771 goto force_start;
15772 }
15773
15774 #ifdef GLYPH_DEBUG
15775 debug_method_add (w, "same window start");
15776 #endif
15777
15778 /* Try to redisplay starting at same place as before.
15779 If point has not moved off frame, accept the results. */
15780 if (!current_matrix_up_to_date_p
15781 /* Don't use try_window_reusing_current_matrix in this case
15782 because a window scroll function can have changed the
15783 buffer. */
15784 || !NILP (Vwindow_scroll_functions)
15785 || MINI_WINDOW_P (w)
15786 || !(used_current_matrix_p
15787 = try_window_reusing_current_matrix (w)))
15788 {
15789 IF_DEBUG (debug_method_add (w, "1"));
15790 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15791 /* -1 means we need to scroll.
15792 0 means we need new matrices, but fonts_changed_p
15793 is set in that case, so we will detect it below. */
15794 goto try_to_scroll;
15795 }
15796
15797 if (fonts_changed_p)
15798 goto need_larger_matrices;
15799
15800 if (w->cursor.vpos >= 0)
15801 {
15802 if (!just_this_one_p
15803 || current_buffer->clip_changed
15804 || BEG_UNCHANGED < CHARPOS (startp))
15805 /* Forget any recorded base line for line number display. */
15806 w->base_line_number = 0;
15807
15808 if (!cursor_row_fully_visible_p (w, 1, 0))
15809 {
15810 clear_glyph_matrix (w->desired_matrix);
15811 last_line_misfit = 1;
15812 }
15813 /* Drop through and scroll. */
15814 else
15815 goto done;
15816 }
15817 else
15818 clear_glyph_matrix (w->desired_matrix);
15819 }
15820
15821 try_to_scroll:
15822
15823 /* Redisplay the mode line. Select the buffer properly for that. */
15824 if (!update_mode_line)
15825 {
15826 update_mode_line = 1;
15827 w->update_mode_line = 1;
15828 }
15829
15830 /* Try to scroll by specified few lines. */
15831 if ((scroll_conservatively
15832 || emacs_scroll_step
15833 || temp_scroll_step
15834 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15835 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15836 && CHARPOS (startp) >= BEGV
15837 && CHARPOS (startp) <= ZV)
15838 {
15839 /* The function returns -1 if new fonts were loaded, 1 if
15840 successful, 0 if not successful. */
15841 int ss = try_scrolling (window, just_this_one_p,
15842 scroll_conservatively,
15843 emacs_scroll_step,
15844 temp_scroll_step, last_line_misfit);
15845 switch (ss)
15846 {
15847 case SCROLLING_SUCCESS:
15848 goto done;
15849
15850 case SCROLLING_NEED_LARGER_MATRICES:
15851 goto need_larger_matrices;
15852
15853 case SCROLLING_FAILED:
15854 break;
15855
15856 default:
15857 emacs_abort ();
15858 }
15859 }
15860
15861 /* Finally, just choose a place to start which positions point
15862 according to user preferences. */
15863
15864 recenter:
15865
15866 #ifdef GLYPH_DEBUG
15867 debug_method_add (w, "recenter");
15868 #endif
15869
15870 /* Forget any previously recorded base line for line number display. */
15871 if (!buffer_unchanged_p)
15872 w->base_line_number = 0;
15873
15874 /* Determine the window start relative to point. */
15875 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15876 it.current_y = it.last_visible_y;
15877 if (centering_position < 0)
15878 {
15879 int window_total_lines
15880 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15881 int margin =
15882 scroll_margin > 0
15883 ? min (scroll_margin, window_total_lines / 4)
15884 : 0;
15885 ptrdiff_t margin_pos = CHARPOS (startp);
15886 Lisp_Object aggressive;
15887 int scrolling_up;
15888
15889 /* If there is a scroll margin at the top of the window, find
15890 its character position. */
15891 if (margin
15892 /* Cannot call start_display if startp is not in the
15893 accessible region of the buffer. This can happen when we
15894 have just switched to a different buffer and/or changed
15895 its restriction. In that case, startp is initialized to
15896 the character position 1 (BEGV) because we did not yet
15897 have chance to display the buffer even once. */
15898 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15899 {
15900 struct it it1;
15901 void *it1data = NULL;
15902
15903 SAVE_IT (it1, it, it1data);
15904 start_display (&it1, w, startp);
15905 move_it_vertically (&it1, margin * frame_line_height);
15906 margin_pos = IT_CHARPOS (it1);
15907 RESTORE_IT (&it, &it, it1data);
15908 }
15909 scrolling_up = PT > margin_pos;
15910 aggressive =
15911 scrolling_up
15912 ? BVAR (current_buffer, scroll_up_aggressively)
15913 : BVAR (current_buffer, scroll_down_aggressively);
15914
15915 if (!MINI_WINDOW_P (w)
15916 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15917 {
15918 int pt_offset = 0;
15919
15920 /* Setting scroll-conservatively overrides
15921 scroll-*-aggressively. */
15922 if (!scroll_conservatively && NUMBERP (aggressive))
15923 {
15924 double float_amount = XFLOATINT (aggressive);
15925
15926 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15927 if (pt_offset == 0 && float_amount > 0)
15928 pt_offset = 1;
15929 if (pt_offset && margin > 0)
15930 margin -= 1;
15931 }
15932 /* Compute how much to move the window start backward from
15933 point so that point will be displayed where the user
15934 wants it. */
15935 if (scrolling_up)
15936 {
15937 centering_position = it.last_visible_y;
15938 if (pt_offset)
15939 centering_position -= pt_offset;
15940 centering_position -=
15941 frame_line_height * (1 + margin + (last_line_misfit != 0))
15942 + WINDOW_HEADER_LINE_HEIGHT (w);
15943 /* Don't let point enter the scroll margin near top of
15944 the window. */
15945 if (centering_position < margin * frame_line_height)
15946 centering_position = margin * frame_line_height;
15947 }
15948 else
15949 centering_position = margin * frame_line_height + pt_offset;
15950 }
15951 else
15952 /* Set the window start half the height of the window backward
15953 from point. */
15954 centering_position = window_box_height (w) / 2;
15955 }
15956 move_it_vertically_backward (&it, centering_position);
15957
15958 eassert (IT_CHARPOS (it) >= BEGV);
15959
15960 /* The function move_it_vertically_backward may move over more
15961 than the specified y-distance. If it->w is small, e.g. a
15962 mini-buffer window, we may end up in front of the window's
15963 display area. Start displaying at the start of the line
15964 containing PT in this case. */
15965 if (it.current_y <= 0)
15966 {
15967 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15968 move_it_vertically_backward (&it, 0);
15969 it.current_y = 0;
15970 }
15971
15972 it.current_x = it.hpos = 0;
15973
15974 /* Set the window start position here explicitly, to avoid an
15975 infinite loop in case the functions in window-scroll-functions
15976 get errors. */
15977 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15978
15979 /* Run scroll hooks. */
15980 startp = run_window_scroll_functions (window, it.current.pos);
15981
15982 /* Redisplay the window. */
15983 if (!current_matrix_up_to_date_p
15984 || windows_or_buffers_changed
15985 || cursor_type_changed
15986 /* Don't use try_window_reusing_current_matrix in this case
15987 because it can have changed the buffer. */
15988 || !NILP (Vwindow_scroll_functions)
15989 || !just_this_one_p
15990 || MINI_WINDOW_P (w)
15991 || !(used_current_matrix_p
15992 = try_window_reusing_current_matrix (w)))
15993 try_window (window, startp, 0);
15994
15995 /* If new fonts have been loaded (due to fontsets), give up. We
15996 have to start a new redisplay since we need to re-adjust glyph
15997 matrices. */
15998 if (fonts_changed_p)
15999 goto need_larger_matrices;
16000
16001 /* If cursor did not appear assume that the middle of the window is
16002 in the first line of the window. Do it again with the next line.
16003 (Imagine a window of height 100, displaying two lines of height
16004 60. Moving back 50 from it->last_visible_y will end in the first
16005 line.) */
16006 if (w->cursor.vpos < 0)
16007 {
16008 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16009 {
16010 clear_glyph_matrix (w->desired_matrix);
16011 move_it_by_lines (&it, 1);
16012 try_window (window, it.current.pos, 0);
16013 }
16014 else if (PT < IT_CHARPOS (it))
16015 {
16016 clear_glyph_matrix (w->desired_matrix);
16017 move_it_by_lines (&it, -1);
16018 try_window (window, it.current.pos, 0);
16019 }
16020 else
16021 {
16022 /* Not much we can do about it. */
16023 }
16024 }
16025
16026 /* Consider the following case: Window starts at BEGV, there is
16027 invisible, intangible text at BEGV, so that display starts at
16028 some point START > BEGV. It can happen that we are called with
16029 PT somewhere between BEGV and START. Try to handle that case. */
16030 if (w->cursor.vpos < 0)
16031 {
16032 struct glyph_row *row = w->current_matrix->rows;
16033 if (row->mode_line_p)
16034 ++row;
16035 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16036 }
16037
16038 if (!cursor_row_fully_visible_p (w, 0, 0))
16039 {
16040 /* If vscroll is enabled, disable it and try again. */
16041 if (w->vscroll)
16042 {
16043 w->vscroll = 0;
16044 clear_glyph_matrix (w->desired_matrix);
16045 goto recenter;
16046 }
16047
16048 /* Users who set scroll-conservatively to a large number want
16049 point just above/below the scroll margin. If we ended up
16050 with point's row partially visible, move the window start to
16051 make that row fully visible and out of the margin. */
16052 if (scroll_conservatively > SCROLL_LIMIT)
16053 {
16054 int window_total_lines
16055 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16056 int margin =
16057 scroll_margin > 0
16058 ? min (scroll_margin, window_total_lines / 4)
16059 : 0;
16060 int move_down = w->cursor.vpos >= window_total_lines / 2;
16061
16062 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16063 clear_glyph_matrix (w->desired_matrix);
16064 if (1 == try_window (window, it.current.pos,
16065 TRY_WINDOW_CHECK_MARGINS))
16066 goto done;
16067 }
16068
16069 /* If centering point failed to make the whole line visible,
16070 put point at the top instead. That has to make the whole line
16071 visible, if it can be done. */
16072 if (centering_position == 0)
16073 goto done;
16074
16075 clear_glyph_matrix (w->desired_matrix);
16076 centering_position = 0;
16077 goto recenter;
16078 }
16079
16080 done:
16081
16082 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16083 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16084 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16085
16086 /* Display the mode line, if we must. */
16087 if ((update_mode_line
16088 /* If window not full width, must redo its mode line
16089 if (a) the window to its side is being redone and
16090 (b) we do a frame-based redisplay. This is a consequence
16091 of how inverted lines are drawn in frame-based redisplay. */
16092 || (!just_this_one_p
16093 && !FRAME_WINDOW_P (f)
16094 && !WINDOW_FULL_WIDTH_P (w))
16095 /* Line number to display. */
16096 || w->base_line_pos > 0
16097 /* Column number is displayed and different from the one displayed. */
16098 || (w->column_number_displayed != -1
16099 && (w->column_number_displayed != current_column ())))
16100 /* This means that the window has a mode line. */
16101 && (WINDOW_WANTS_MODELINE_P (w)
16102 || WINDOW_WANTS_HEADER_LINE_P (w)))
16103 {
16104 display_mode_lines (w);
16105
16106 /* If mode line height has changed, arrange for a thorough
16107 immediate redisplay using the correct mode line height. */
16108 if (WINDOW_WANTS_MODELINE_P (w)
16109 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16110 {
16111 fonts_changed_p = 1;
16112 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16113 = DESIRED_MODE_LINE_HEIGHT (w);
16114 }
16115
16116 /* If header line height has changed, arrange for a thorough
16117 immediate redisplay using the correct header line height. */
16118 if (WINDOW_WANTS_HEADER_LINE_P (w)
16119 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16120 {
16121 fonts_changed_p = 1;
16122 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16123 = DESIRED_HEADER_LINE_HEIGHT (w);
16124 }
16125
16126 if (fonts_changed_p)
16127 goto need_larger_matrices;
16128 }
16129
16130 if (!line_number_displayed && w->base_line_pos != -1)
16131 {
16132 w->base_line_pos = 0;
16133 w->base_line_number = 0;
16134 }
16135
16136 finish_menu_bars:
16137
16138 /* When we reach a frame's selected window, redo the frame's menu bar. */
16139 if (update_mode_line
16140 && EQ (FRAME_SELECTED_WINDOW (f), window))
16141 {
16142 int redisplay_menu_p = 0;
16143
16144 if (FRAME_WINDOW_P (f))
16145 {
16146 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16147 || defined (HAVE_NS) || defined (USE_GTK)
16148 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16149 #else
16150 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16151 #endif
16152 }
16153 else
16154 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16155
16156 if (redisplay_menu_p)
16157 display_menu_bar (w);
16158
16159 #ifdef HAVE_WINDOW_SYSTEM
16160 if (FRAME_WINDOW_P (f))
16161 {
16162 #if defined (USE_GTK) || defined (HAVE_NS)
16163 if (FRAME_EXTERNAL_TOOL_BAR (f))
16164 redisplay_tool_bar (f);
16165 #else
16166 if (WINDOWP (f->tool_bar_window)
16167 && (FRAME_TOOL_BAR_LINES (f) > 0
16168 || !NILP (Vauto_resize_tool_bars))
16169 && redisplay_tool_bar (f))
16170 ignore_mouse_drag_p = 1;
16171 #endif
16172 }
16173 #endif
16174 }
16175
16176 #ifdef HAVE_WINDOW_SYSTEM
16177 if (FRAME_WINDOW_P (f)
16178 && update_window_fringes (w, (just_this_one_p
16179 || (!used_current_matrix_p && !overlay_arrow_seen)
16180 || w->pseudo_window_p)))
16181 {
16182 update_begin (f);
16183 block_input ();
16184 if (draw_window_fringes (w, 1))
16185 x_draw_vertical_border (w);
16186 unblock_input ();
16187 update_end (f);
16188 }
16189 #endif /* HAVE_WINDOW_SYSTEM */
16190
16191 /* We go to this label, with fonts_changed_p set,
16192 if it is necessary to try again using larger glyph matrices.
16193 We have to redeem the scroll bar even in this case,
16194 because the loop in redisplay_internal expects that. */
16195 need_larger_matrices:
16196 ;
16197 finish_scroll_bars:
16198
16199 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16200 {
16201 /* Set the thumb's position and size. */
16202 set_vertical_scroll_bar (w);
16203
16204 /* Note that we actually used the scroll bar attached to this
16205 window, so it shouldn't be deleted at the end of redisplay. */
16206 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16207 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16208 }
16209
16210 /* Restore current_buffer and value of point in it. The window
16211 update may have changed the buffer, so first make sure `opoint'
16212 is still valid (Bug#6177). */
16213 if (CHARPOS (opoint) < BEGV)
16214 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16215 else if (CHARPOS (opoint) > ZV)
16216 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16217 else
16218 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16219
16220 set_buffer_internal_1 (old);
16221 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16222 shorter. This can be caused by log truncation in *Messages*. */
16223 if (CHARPOS (lpoint) <= ZV)
16224 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16225
16226 unbind_to (count, Qnil);
16227 }
16228
16229
16230 /* Build the complete desired matrix of WINDOW with a window start
16231 buffer position POS.
16232
16233 Value is 1 if successful. It is zero if fonts were loaded during
16234 redisplay which makes re-adjusting glyph matrices necessary, and -1
16235 if point would appear in the scroll margins.
16236 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16237 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16238 set in FLAGS.) */
16239
16240 int
16241 try_window (Lisp_Object window, struct text_pos pos, int flags)
16242 {
16243 struct window *w = XWINDOW (window);
16244 struct it it;
16245 struct glyph_row *last_text_row = NULL;
16246 struct frame *f = XFRAME (w->frame);
16247 int frame_line_height = default_line_pixel_height (w);
16248
16249 /* Make POS the new window start. */
16250 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16251
16252 /* Mark cursor position as unknown. No overlay arrow seen. */
16253 w->cursor.vpos = -1;
16254 overlay_arrow_seen = 0;
16255
16256 /* Initialize iterator and info to start at POS. */
16257 start_display (&it, w, pos);
16258
16259 /* Display all lines of W. */
16260 while (it.current_y < it.last_visible_y)
16261 {
16262 if (display_line (&it))
16263 last_text_row = it.glyph_row - 1;
16264 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16265 return 0;
16266 }
16267
16268 /* Don't let the cursor end in the scroll margins. */
16269 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16270 && !MINI_WINDOW_P (w))
16271 {
16272 int this_scroll_margin;
16273 int window_total_lines
16274 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16275
16276 if (scroll_margin > 0)
16277 {
16278 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16279 this_scroll_margin *= frame_line_height;
16280 }
16281 else
16282 this_scroll_margin = 0;
16283
16284 if ((w->cursor.y >= 0 /* not vscrolled */
16285 && w->cursor.y < this_scroll_margin
16286 && CHARPOS (pos) > BEGV
16287 && IT_CHARPOS (it) < ZV)
16288 /* rms: considering make_cursor_line_fully_visible_p here
16289 seems to give wrong results. We don't want to recenter
16290 when the last line is partly visible, we want to allow
16291 that case to be handled in the usual way. */
16292 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16293 {
16294 w->cursor.vpos = -1;
16295 clear_glyph_matrix (w->desired_matrix);
16296 return -1;
16297 }
16298 }
16299
16300 /* If bottom moved off end of frame, change mode line percentage. */
16301 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
16302 w->update_mode_line = 1;
16303
16304 /* Set window_end_pos to the offset of the last character displayed
16305 on the window from the end of current_buffer. Set
16306 window_end_vpos to its row number. */
16307 if (last_text_row)
16308 {
16309 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16310 adjust_window_ends (w, last_text_row, 0);
16311 eassert
16312 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16313 w->window_end_vpos)));
16314 }
16315 else
16316 {
16317 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16318 w->window_end_pos = Z - ZV;
16319 w->window_end_vpos = 0;
16320 }
16321
16322 /* But that is not valid info until redisplay finishes. */
16323 w->window_end_valid = 0;
16324 return 1;
16325 }
16326
16327
16328 \f
16329 /************************************************************************
16330 Window redisplay reusing current matrix when buffer has not changed
16331 ************************************************************************/
16332
16333 /* Try redisplay of window W showing an unchanged buffer with a
16334 different window start than the last time it was displayed by
16335 reusing its current matrix. Value is non-zero if successful.
16336 W->start is the new window start. */
16337
16338 static int
16339 try_window_reusing_current_matrix (struct window *w)
16340 {
16341 struct frame *f = XFRAME (w->frame);
16342 struct glyph_row *bottom_row;
16343 struct it it;
16344 struct run run;
16345 struct text_pos start, new_start;
16346 int nrows_scrolled, i;
16347 struct glyph_row *last_text_row;
16348 struct glyph_row *last_reused_text_row;
16349 struct glyph_row *start_row;
16350 int start_vpos, min_y, max_y;
16351
16352 #ifdef GLYPH_DEBUG
16353 if (inhibit_try_window_reusing)
16354 return 0;
16355 #endif
16356
16357 if (/* This function doesn't handle terminal frames. */
16358 !FRAME_WINDOW_P (f)
16359 /* Don't try to reuse the display if windows have been split
16360 or such. */
16361 || windows_or_buffers_changed
16362 || cursor_type_changed)
16363 return 0;
16364
16365 /* Can't do this if region may have changed. */
16366 if (markpos_of_region () >= 0
16367 || w->region_showing
16368 || !NILP (Vshow_trailing_whitespace))
16369 return 0;
16370
16371 /* If top-line visibility has changed, give up. */
16372 if (WINDOW_WANTS_HEADER_LINE_P (w)
16373 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16374 return 0;
16375
16376 /* Give up if old or new display is scrolled vertically. We could
16377 make this function handle this, but right now it doesn't. */
16378 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16379 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16380 return 0;
16381
16382 /* The variable new_start now holds the new window start. The old
16383 start `start' can be determined from the current matrix. */
16384 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16385 start = start_row->minpos;
16386 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16387
16388 /* Clear the desired matrix for the display below. */
16389 clear_glyph_matrix (w->desired_matrix);
16390
16391 if (CHARPOS (new_start) <= CHARPOS (start))
16392 {
16393 /* Don't use this method if the display starts with an ellipsis
16394 displayed for invisible text. It's not easy to handle that case
16395 below, and it's certainly not worth the effort since this is
16396 not a frequent case. */
16397 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16398 return 0;
16399
16400 IF_DEBUG (debug_method_add (w, "twu1"));
16401
16402 /* Display up to a row that can be reused. The variable
16403 last_text_row is set to the last row displayed that displays
16404 text. Note that it.vpos == 0 if or if not there is a
16405 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16406 start_display (&it, w, new_start);
16407 w->cursor.vpos = -1;
16408 last_text_row = last_reused_text_row = NULL;
16409
16410 while (it.current_y < it.last_visible_y
16411 && !fonts_changed_p)
16412 {
16413 /* If we have reached into the characters in the START row,
16414 that means the line boundaries have changed. So we
16415 can't start copying with the row START. Maybe it will
16416 work to start copying with the following row. */
16417 while (IT_CHARPOS (it) > CHARPOS (start))
16418 {
16419 /* Advance to the next row as the "start". */
16420 start_row++;
16421 start = start_row->minpos;
16422 /* If there are no more rows to try, or just one, give up. */
16423 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16424 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16425 || CHARPOS (start) == ZV)
16426 {
16427 clear_glyph_matrix (w->desired_matrix);
16428 return 0;
16429 }
16430
16431 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16432 }
16433 /* If we have reached alignment, we can copy the rest of the
16434 rows. */
16435 if (IT_CHARPOS (it) == CHARPOS (start)
16436 /* Don't accept "alignment" inside a display vector,
16437 since start_row could have started in the middle of
16438 that same display vector (thus their character
16439 positions match), and we have no way of telling if
16440 that is the case. */
16441 && it.current.dpvec_index < 0)
16442 break;
16443
16444 if (display_line (&it))
16445 last_text_row = it.glyph_row - 1;
16446
16447 }
16448
16449 /* A value of current_y < last_visible_y means that we stopped
16450 at the previous window start, which in turn means that we
16451 have at least one reusable row. */
16452 if (it.current_y < it.last_visible_y)
16453 {
16454 struct glyph_row *row;
16455
16456 /* IT.vpos always starts from 0; it counts text lines. */
16457 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16458
16459 /* Find PT if not already found in the lines displayed. */
16460 if (w->cursor.vpos < 0)
16461 {
16462 int dy = it.current_y - start_row->y;
16463
16464 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16465 row = row_containing_pos (w, PT, row, NULL, dy);
16466 if (row)
16467 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16468 dy, nrows_scrolled);
16469 else
16470 {
16471 clear_glyph_matrix (w->desired_matrix);
16472 return 0;
16473 }
16474 }
16475
16476 /* Scroll the display. Do it before the current matrix is
16477 changed. The problem here is that update has not yet
16478 run, i.e. part of the current matrix is not up to date.
16479 scroll_run_hook will clear the cursor, and use the
16480 current matrix to get the height of the row the cursor is
16481 in. */
16482 run.current_y = start_row->y;
16483 run.desired_y = it.current_y;
16484 run.height = it.last_visible_y - it.current_y;
16485
16486 if (run.height > 0 && run.current_y != run.desired_y)
16487 {
16488 update_begin (f);
16489 FRAME_RIF (f)->update_window_begin_hook (w);
16490 FRAME_RIF (f)->clear_window_mouse_face (w);
16491 FRAME_RIF (f)->scroll_run_hook (w, &run);
16492 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16493 update_end (f);
16494 }
16495
16496 /* Shift current matrix down by nrows_scrolled lines. */
16497 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16498 rotate_matrix (w->current_matrix,
16499 start_vpos,
16500 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16501 nrows_scrolled);
16502
16503 /* Disable lines that must be updated. */
16504 for (i = 0; i < nrows_scrolled; ++i)
16505 (start_row + i)->enabled_p = 0;
16506
16507 /* Re-compute Y positions. */
16508 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16509 max_y = it.last_visible_y;
16510 for (row = start_row + nrows_scrolled;
16511 row < bottom_row;
16512 ++row)
16513 {
16514 row->y = it.current_y;
16515 row->visible_height = row->height;
16516
16517 if (row->y < min_y)
16518 row->visible_height -= min_y - row->y;
16519 if (row->y + row->height > max_y)
16520 row->visible_height -= row->y + row->height - max_y;
16521 if (row->fringe_bitmap_periodic_p)
16522 row->redraw_fringe_bitmaps_p = 1;
16523
16524 it.current_y += row->height;
16525
16526 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16527 last_reused_text_row = row;
16528 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16529 break;
16530 }
16531
16532 /* Disable lines in the current matrix which are now
16533 below the window. */
16534 for (++row; row < bottom_row; ++row)
16535 row->enabled_p = row->mode_line_p = 0;
16536 }
16537
16538 /* Update window_end_pos etc.; last_reused_text_row is the last
16539 reused row from the current matrix containing text, if any.
16540 The value of last_text_row is the last displayed line
16541 containing text. */
16542 if (last_reused_text_row)
16543 adjust_window_ends (w, last_reused_text_row, 1);
16544 else if (last_text_row)
16545 adjust_window_ends (w, last_text_row, 0);
16546 else
16547 {
16548 /* This window must be completely empty. */
16549 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16550 w->window_end_pos = Z - ZV;
16551 w->window_end_vpos = 0;
16552 }
16553 w->window_end_valid = 0;
16554
16555 /* Update hint: don't try scrolling again in update_window. */
16556 w->desired_matrix->no_scrolling_p = 1;
16557
16558 #ifdef GLYPH_DEBUG
16559 debug_method_add (w, "try_window_reusing_current_matrix 1");
16560 #endif
16561 return 1;
16562 }
16563 else if (CHARPOS (new_start) > CHARPOS (start))
16564 {
16565 struct glyph_row *pt_row, *row;
16566 struct glyph_row *first_reusable_row;
16567 struct glyph_row *first_row_to_display;
16568 int dy;
16569 int yb = window_text_bottom_y (w);
16570
16571 /* Find the row starting at new_start, if there is one. Don't
16572 reuse a partially visible line at the end. */
16573 first_reusable_row = start_row;
16574 while (first_reusable_row->enabled_p
16575 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16576 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16577 < CHARPOS (new_start)))
16578 ++first_reusable_row;
16579
16580 /* Give up if there is no row to reuse. */
16581 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16582 || !first_reusable_row->enabled_p
16583 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16584 != CHARPOS (new_start)))
16585 return 0;
16586
16587 /* We can reuse fully visible rows beginning with
16588 first_reusable_row to the end of the window. Set
16589 first_row_to_display to the first row that cannot be reused.
16590 Set pt_row to the row containing point, if there is any. */
16591 pt_row = NULL;
16592 for (first_row_to_display = first_reusable_row;
16593 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16594 ++first_row_to_display)
16595 {
16596 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16597 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16598 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16599 && first_row_to_display->ends_at_zv_p
16600 && pt_row == NULL)))
16601 pt_row = first_row_to_display;
16602 }
16603
16604 /* Start displaying at the start of first_row_to_display. */
16605 eassert (first_row_to_display->y < yb);
16606 init_to_row_start (&it, w, first_row_to_display);
16607
16608 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16609 - start_vpos);
16610 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16611 - nrows_scrolled);
16612 it.current_y = (first_row_to_display->y - first_reusable_row->y
16613 + WINDOW_HEADER_LINE_HEIGHT (w));
16614
16615 /* Display lines beginning with first_row_to_display in the
16616 desired matrix. Set last_text_row to the last row displayed
16617 that displays text. */
16618 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16619 if (pt_row == NULL)
16620 w->cursor.vpos = -1;
16621 last_text_row = NULL;
16622 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16623 if (display_line (&it))
16624 last_text_row = it.glyph_row - 1;
16625
16626 /* If point is in a reused row, adjust y and vpos of the cursor
16627 position. */
16628 if (pt_row)
16629 {
16630 w->cursor.vpos -= nrows_scrolled;
16631 w->cursor.y -= first_reusable_row->y - start_row->y;
16632 }
16633
16634 /* Give up if point isn't in a row displayed or reused. (This
16635 also handles the case where w->cursor.vpos < nrows_scrolled
16636 after the calls to display_line, which can happen with scroll
16637 margins. See bug#1295.) */
16638 if (w->cursor.vpos < 0)
16639 {
16640 clear_glyph_matrix (w->desired_matrix);
16641 return 0;
16642 }
16643
16644 /* Scroll the display. */
16645 run.current_y = first_reusable_row->y;
16646 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16647 run.height = it.last_visible_y - run.current_y;
16648 dy = run.current_y - run.desired_y;
16649
16650 if (run.height)
16651 {
16652 update_begin (f);
16653 FRAME_RIF (f)->update_window_begin_hook (w);
16654 FRAME_RIF (f)->clear_window_mouse_face (w);
16655 FRAME_RIF (f)->scroll_run_hook (w, &run);
16656 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16657 update_end (f);
16658 }
16659
16660 /* Adjust Y positions of reused rows. */
16661 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16662 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16663 max_y = it.last_visible_y;
16664 for (row = first_reusable_row; row < first_row_to_display; ++row)
16665 {
16666 row->y -= dy;
16667 row->visible_height = row->height;
16668 if (row->y < min_y)
16669 row->visible_height -= min_y - row->y;
16670 if (row->y + row->height > max_y)
16671 row->visible_height -= row->y + row->height - max_y;
16672 if (row->fringe_bitmap_periodic_p)
16673 row->redraw_fringe_bitmaps_p = 1;
16674 }
16675
16676 /* Scroll the current matrix. */
16677 eassert (nrows_scrolled > 0);
16678 rotate_matrix (w->current_matrix,
16679 start_vpos,
16680 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16681 -nrows_scrolled);
16682
16683 /* Disable rows not reused. */
16684 for (row -= nrows_scrolled; row < bottom_row; ++row)
16685 row->enabled_p = 0;
16686
16687 /* Point may have moved to a different line, so we cannot assume that
16688 the previous cursor position is valid; locate the correct row. */
16689 if (pt_row)
16690 {
16691 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16692 row < bottom_row
16693 && PT >= MATRIX_ROW_END_CHARPOS (row)
16694 && !row->ends_at_zv_p;
16695 row++)
16696 {
16697 w->cursor.vpos++;
16698 w->cursor.y = row->y;
16699 }
16700 if (row < bottom_row)
16701 {
16702 /* Can't simply scan the row for point with
16703 bidi-reordered glyph rows. Let set_cursor_from_row
16704 figure out where to put the cursor, and if it fails,
16705 give up. */
16706 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16707 {
16708 if (!set_cursor_from_row (w, row, w->current_matrix,
16709 0, 0, 0, 0))
16710 {
16711 clear_glyph_matrix (w->desired_matrix);
16712 return 0;
16713 }
16714 }
16715 else
16716 {
16717 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16718 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16719
16720 for (; glyph < end
16721 && (!BUFFERP (glyph->object)
16722 || glyph->charpos < PT);
16723 glyph++)
16724 {
16725 w->cursor.hpos++;
16726 w->cursor.x += glyph->pixel_width;
16727 }
16728 }
16729 }
16730 }
16731
16732 /* Adjust window end. A null value of last_text_row means that
16733 the window end is in reused rows which in turn means that
16734 only its vpos can have changed. */
16735 if (last_text_row)
16736 adjust_window_ends (w, last_text_row, 0);
16737 else
16738 w->window_end_vpos -= nrows_scrolled;
16739
16740 w->window_end_valid = 0;
16741 w->desired_matrix->no_scrolling_p = 1;
16742
16743 #ifdef GLYPH_DEBUG
16744 debug_method_add (w, "try_window_reusing_current_matrix 2");
16745 #endif
16746 return 1;
16747 }
16748
16749 return 0;
16750 }
16751
16752
16753 \f
16754 /************************************************************************
16755 Window redisplay reusing current matrix when buffer has changed
16756 ************************************************************************/
16757
16758 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16759 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16760 ptrdiff_t *, ptrdiff_t *);
16761 static struct glyph_row *
16762 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16763 struct glyph_row *);
16764
16765
16766 /* Return the last row in MATRIX displaying text. If row START is
16767 non-null, start searching with that row. IT gives the dimensions
16768 of the display. Value is null if matrix is empty; otherwise it is
16769 a pointer to the row found. */
16770
16771 static struct glyph_row *
16772 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16773 struct glyph_row *start)
16774 {
16775 struct glyph_row *row, *row_found;
16776
16777 /* Set row_found to the last row in IT->w's current matrix
16778 displaying text. The loop looks funny but think of partially
16779 visible lines. */
16780 row_found = NULL;
16781 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16782 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16783 {
16784 eassert (row->enabled_p);
16785 row_found = row;
16786 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16787 break;
16788 ++row;
16789 }
16790
16791 return row_found;
16792 }
16793
16794
16795 /* Return the last row in the current matrix of W that is not affected
16796 by changes at the start of current_buffer that occurred since W's
16797 current matrix was built. Value is null if no such row exists.
16798
16799 BEG_UNCHANGED us the number of characters unchanged at the start of
16800 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16801 first changed character in current_buffer. Characters at positions <
16802 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16803 when the current matrix was built. */
16804
16805 static struct glyph_row *
16806 find_last_unchanged_at_beg_row (struct window *w)
16807 {
16808 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16809 struct glyph_row *row;
16810 struct glyph_row *row_found = NULL;
16811 int yb = window_text_bottom_y (w);
16812
16813 /* Find the last row displaying unchanged text. */
16814 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16815 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16816 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16817 ++row)
16818 {
16819 if (/* If row ends before first_changed_pos, it is unchanged,
16820 except in some case. */
16821 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16822 /* When row ends in ZV and we write at ZV it is not
16823 unchanged. */
16824 && !row->ends_at_zv_p
16825 /* When first_changed_pos is the end of a continued line,
16826 row is not unchanged because it may be no longer
16827 continued. */
16828 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16829 && (row->continued_p
16830 || row->exact_window_width_line_p))
16831 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16832 needs to be recomputed, so don't consider this row as
16833 unchanged. This happens when the last line was
16834 bidi-reordered and was killed immediately before this
16835 redisplay cycle. In that case, ROW->end stores the
16836 buffer position of the first visual-order character of
16837 the killed text, which is now beyond ZV. */
16838 && CHARPOS (row->end.pos) <= ZV)
16839 row_found = row;
16840
16841 /* Stop if last visible row. */
16842 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16843 break;
16844 }
16845
16846 return row_found;
16847 }
16848
16849
16850 /* Find the first glyph row in the current matrix of W that is not
16851 affected by changes at the end of current_buffer since the
16852 time W's current matrix was built.
16853
16854 Return in *DELTA the number of chars by which buffer positions in
16855 unchanged text at the end of current_buffer must be adjusted.
16856
16857 Return in *DELTA_BYTES the corresponding number of bytes.
16858
16859 Value is null if no such row exists, i.e. all rows are affected by
16860 changes. */
16861
16862 static struct glyph_row *
16863 find_first_unchanged_at_end_row (struct window *w,
16864 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16865 {
16866 struct glyph_row *row;
16867 struct glyph_row *row_found = NULL;
16868
16869 *delta = *delta_bytes = 0;
16870
16871 /* Display must not have been paused, otherwise the current matrix
16872 is not up to date. */
16873 eassert (w->window_end_valid);
16874
16875 /* A value of window_end_pos >= END_UNCHANGED means that the window
16876 end is in the range of changed text. If so, there is no
16877 unchanged row at the end of W's current matrix. */
16878 if (w->window_end_pos >= END_UNCHANGED)
16879 return NULL;
16880
16881 /* Set row to the last row in W's current matrix displaying text. */
16882 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
16883
16884 /* If matrix is entirely empty, no unchanged row exists. */
16885 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16886 {
16887 /* The value of row is the last glyph row in the matrix having a
16888 meaningful buffer position in it. The end position of row
16889 corresponds to window_end_pos. This allows us to translate
16890 buffer positions in the current matrix to current buffer
16891 positions for characters not in changed text. */
16892 ptrdiff_t Z_old =
16893 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
16894 ptrdiff_t Z_BYTE_old =
16895 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16896 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16897 struct glyph_row *first_text_row
16898 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16899
16900 *delta = Z - Z_old;
16901 *delta_bytes = Z_BYTE - Z_BYTE_old;
16902
16903 /* Set last_unchanged_pos to the buffer position of the last
16904 character in the buffer that has not been changed. Z is the
16905 index + 1 of the last character in current_buffer, i.e. by
16906 subtracting END_UNCHANGED we get the index of the last
16907 unchanged character, and we have to add BEG to get its buffer
16908 position. */
16909 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16910 last_unchanged_pos_old = last_unchanged_pos - *delta;
16911
16912 /* Search backward from ROW for a row displaying a line that
16913 starts at a minimum position >= last_unchanged_pos_old. */
16914 for (; row > first_text_row; --row)
16915 {
16916 /* This used to abort, but it can happen.
16917 It is ok to just stop the search instead here. KFS. */
16918 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16919 break;
16920
16921 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16922 row_found = row;
16923 }
16924 }
16925
16926 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16927
16928 return row_found;
16929 }
16930
16931
16932 /* Make sure that glyph rows in the current matrix of window W
16933 reference the same glyph memory as corresponding rows in the
16934 frame's frame matrix. This function is called after scrolling W's
16935 current matrix on a terminal frame in try_window_id and
16936 try_window_reusing_current_matrix. */
16937
16938 static void
16939 sync_frame_with_window_matrix_rows (struct window *w)
16940 {
16941 struct frame *f = XFRAME (w->frame);
16942 struct glyph_row *window_row, *window_row_end, *frame_row;
16943
16944 /* Preconditions: W must be a leaf window and full-width. Its frame
16945 must have a frame matrix. */
16946 eassert (BUFFERP (w->contents));
16947 eassert (WINDOW_FULL_WIDTH_P (w));
16948 eassert (!FRAME_WINDOW_P (f));
16949
16950 /* If W is a full-width window, glyph pointers in W's current matrix
16951 have, by definition, to be the same as glyph pointers in the
16952 corresponding frame matrix. Note that frame matrices have no
16953 marginal areas (see build_frame_matrix). */
16954 window_row = w->current_matrix->rows;
16955 window_row_end = window_row + w->current_matrix->nrows;
16956 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16957 while (window_row < window_row_end)
16958 {
16959 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16960 struct glyph *end = window_row->glyphs[LAST_AREA];
16961
16962 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16963 frame_row->glyphs[TEXT_AREA] = start;
16964 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16965 frame_row->glyphs[LAST_AREA] = end;
16966
16967 /* Disable frame rows whose corresponding window rows have
16968 been disabled in try_window_id. */
16969 if (!window_row->enabled_p)
16970 frame_row->enabled_p = 0;
16971
16972 ++window_row, ++frame_row;
16973 }
16974 }
16975
16976
16977 /* Find the glyph row in window W containing CHARPOS. Consider all
16978 rows between START and END (not inclusive). END null means search
16979 all rows to the end of the display area of W. Value is the row
16980 containing CHARPOS or null. */
16981
16982 struct glyph_row *
16983 row_containing_pos (struct window *w, ptrdiff_t charpos,
16984 struct glyph_row *start, struct glyph_row *end, int dy)
16985 {
16986 struct glyph_row *row = start;
16987 struct glyph_row *best_row = NULL;
16988 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
16989 int last_y;
16990
16991 /* If we happen to start on a header-line, skip that. */
16992 if (row->mode_line_p)
16993 ++row;
16994
16995 if ((end && row >= end) || !row->enabled_p)
16996 return NULL;
16997
16998 last_y = window_text_bottom_y (w) - dy;
16999
17000 while (1)
17001 {
17002 /* Give up if we have gone too far. */
17003 if (end && row >= end)
17004 return NULL;
17005 /* This formerly returned if they were equal.
17006 I think that both quantities are of a "last plus one" type;
17007 if so, when they are equal, the row is within the screen. -- rms. */
17008 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17009 return NULL;
17010
17011 /* If it is in this row, return this row. */
17012 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17013 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17014 /* The end position of a row equals the start
17015 position of the next row. If CHARPOS is there, we
17016 would rather consider it displayed in the next
17017 line, except when this line ends in ZV. */
17018 && !row_for_charpos_p (row, charpos)))
17019 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17020 {
17021 struct glyph *g;
17022
17023 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17024 || (!best_row && !row->continued_p))
17025 return row;
17026 /* In bidi-reordered rows, there could be several rows whose
17027 edges surround CHARPOS, all of these rows belonging to
17028 the same continued line. We need to find the row which
17029 fits CHARPOS the best. */
17030 for (g = row->glyphs[TEXT_AREA];
17031 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17032 g++)
17033 {
17034 if (!STRINGP (g->object))
17035 {
17036 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17037 {
17038 mindif = eabs (g->charpos - charpos);
17039 best_row = row;
17040 /* Exact match always wins. */
17041 if (mindif == 0)
17042 return best_row;
17043 }
17044 }
17045 }
17046 }
17047 else if (best_row && !row->continued_p)
17048 return best_row;
17049 ++row;
17050 }
17051 }
17052
17053
17054 /* Try to redisplay window W by reusing its existing display. W's
17055 current matrix must be up to date when this function is called,
17056 i.e. window_end_valid must be nonzero.
17057
17058 Value is
17059
17060 1 if display has been updated
17061 0 if otherwise unsuccessful
17062 -1 if redisplay with same window start is known not to succeed
17063
17064 The following steps are performed:
17065
17066 1. Find the last row in the current matrix of W that is not
17067 affected by changes at the start of current_buffer. If no such row
17068 is found, give up.
17069
17070 2. Find the first row in W's current matrix that is not affected by
17071 changes at the end of current_buffer. Maybe there is no such row.
17072
17073 3. Display lines beginning with the row + 1 found in step 1 to the
17074 row found in step 2 or, if step 2 didn't find a row, to the end of
17075 the window.
17076
17077 4. If cursor is not known to appear on the window, give up.
17078
17079 5. If display stopped at the row found in step 2, scroll the
17080 display and current matrix as needed.
17081
17082 6. Maybe display some lines at the end of W, if we must. This can
17083 happen under various circumstances, like a partially visible line
17084 becoming fully visible, or because newly displayed lines are displayed
17085 in smaller font sizes.
17086
17087 7. Update W's window end information. */
17088
17089 static int
17090 try_window_id (struct window *w)
17091 {
17092 struct frame *f = XFRAME (w->frame);
17093 struct glyph_matrix *current_matrix = w->current_matrix;
17094 struct glyph_matrix *desired_matrix = w->desired_matrix;
17095 struct glyph_row *last_unchanged_at_beg_row;
17096 struct glyph_row *first_unchanged_at_end_row;
17097 struct glyph_row *row;
17098 struct glyph_row *bottom_row;
17099 int bottom_vpos;
17100 struct it it;
17101 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17102 int dvpos, dy;
17103 struct text_pos start_pos;
17104 struct run run;
17105 int first_unchanged_at_end_vpos = 0;
17106 struct glyph_row *last_text_row, *last_text_row_at_end;
17107 struct text_pos start;
17108 ptrdiff_t first_changed_charpos, last_changed_charpos;
17109
17110 #ifdef GLYPH_DEBUG
17111 if (inhibit_try_window_id)
17112 return 0;
17113 #endif
17114
17115 /* This is handy for debugging. */
17116 #if 0
17117 #define GIVE_UP(X) \
17118 do { \
17119 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17120 return 0; \
17121 } while (0)
17122 #else
17123 #define GIVE_UP(X) return 0
17124 #endif
17125
17126 SET_TEXT_POS_FROM_MARKER (start, w->start);
17127
17128 /* Don't use this for mini-windows because these can show
17129 messages and mini-buffers, and we don't handle that here. */
17130 if (MINI_WINDOW_P (w))
17131 GIVE_UP (1);
17132
17133 /* This flag is used to prevent redisplay optimizations. */
17134 if (windows_or_buffers_changed || cursor_type_changed)
17135 GIVE_UP (2);
17136
17137 /* Verify that narrowing has not changed.
17138 Also verify that we were not told to prevent redisplay optimizations.
17139 It would be nice to further
17140 reduce the number of cases where this prevents try_window_id. */
17141 if (current_buffer->clip_changed
17142 || current_buffer->prevent_redisplay_optimizations_p)
17143 GIVE_UP (3);
17144
17145 /* Window must either use window-based redisplay or be full width. */
17146 if (!FRAME_WINDOW_P (f)
17147 && (!FRAME_LINE_INS_DEL_OK (f)
17148 || !WINDOW_FULL_WIDTH_P (w)))
17149 GIVE_UP (4);
17150
17151 /* Give up if point is known NOT to appear in W. */
17152 if (PT < CHARPOS (start))
17153 GIVE_UP (5);
17154
17155 /* Another way to prevent redisplay optimizations. */
17156 if (w->last_modified == 0)
17157 GIVE_UP (6);
17158
17159 /* Verify that window is not hscrolled. */
17160 if (w->hscroll != 0)
17161 GIVE_UP (7);
17162
17163 /* Verify that display wasn't paused. */
17164 if (!w->window_end_valid)
17165 GIVE_UP (8);
17166
17167 /* Can't use this if highlighting a region because a cursor movement
17168 will do more than just set the cursor. */
17169 if (markpos_of_region () >= 0)
17170 GIVE_UP (9);
17171
17172 /* Likewise if highlighting trailing whitespace. */
17173 if (!NILP (Vshow_trailing_whitespace))
17174 GIVE_UP (11);
17175
17176 /* Likewise if showing a region. */
17177 if (w->region_showing)
17178 GIVE_UP (10);
17179
17180 /* Can't use this if overlay arrow position and/or string have
17181 changed. */
17182 if (overlay_arrows_changed_p ())
17183 GIVE_UP (12);
17184
17185 /* When word-wrap is on, adding a space to the first word of a
17186 wrapped line can change the wrap position, altering the line
17187 above it. It might be worthwhile to handle this more
17188 intelligently, but for now just redisplay from scratch. */
17189 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17190 GIVE_UP (21);
17191
17192 /* Under bidi reordering, adding or deleting a character in the
17193 beginning of a paragraph, before the first strong directional
17194 character, can change the base direction of the paragraph (unless
17195 the buffer specifies a fixed paragraph direction), which will
17196 require to redisplay the whole paragraph. It might be worthwhile
17197 to find the paragraph limits and widen the range of redisplayed
17198 lines to that, but for now just give up this optimization and
17199 redisplay from scratch. */
17200 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17201 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17202 GIVE_UP (22);
17203
17204 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17205 only if buffer has really changed. The reason is that the gap is
17206 initially at Z for freshly visited files. The code below would
17207 set end_unchanged to 0 in that case. */
17208 if (MODIFF > SAVE_MODIFF
17209 /* This seems to happen sometimes after saving a buffer. */
17210 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17211 {
17212 if (GPT - BEG < BEG_UNCHANGED)
17213 BEG_UNCHANGED = GPT - BEG;
17214 if (Z - GPT < END_UNCHANGED)
17215 END_UNCHANGED = Z - GPT;
17216 }
17217
17218 /* The position of the first and last character that has been changed. */
17219 first_changed_charpos = BEG + BEG_UNCHANGED;
17220 last_changed_charpos = Z - END_UNCHANGED;
17221
17222 /* If window starts after a line end, and the last change is in
17223 front of that newline, then changes don't affect the display.
17224 This case happens with stealth-fontification. Note that although
17225 the display is unchanged, glyph positions in the matrix have to
17226 be adjusted, of course. */
17227 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17228 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17229 && ((last_changed_charpos < CHARPOS (start)
17230 && CHARPOS (start) == BEGV)
17231 || (last_changed_charpos < CHARPOS (start) - 1
17232 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17233 {
17234 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17235 struct glyph_row *r0;
17236
17237 /* Compute how many chars/bytes have been added to or removed
17238 from the buffer. */
17239 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17240 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17241 Z_delta = Z - Z_old;
17242 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17243
17244 /* Give up if PT is not in the window. Note that it already has
17245 been checked at the start of try_window_id that PT is not in
17246 front of the window start. */
17247 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17248 GIVE_UP (13);
17249
17250 /* If window start is unchanged, we can reuse the whole matrix
17251 as is, after adjusting glyph positions. No need to compute
17252 the window end again, since its offset from Z hasn't changed. */
17253 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17254 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17255 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17256 /* PT must not be in a partially visible line. */
17257 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17258 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17259 {
17260 /* Adjust positions in the glyph matrix. */
17261 if (Z_delta || Z_delta_bytes)
17262 {
17263 struct glyph_row *r1
17264 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17265 increment_matrix_positions (w->current_matrix,
17266 MATRIX_ROW_VPOS (r0, current_matrix),
17267 MATRIX_ROW_VPOS (r1, current_matrix),
17268 Z_delta, Z_delta_bytes);
17269 }
17270
17271 /* Set the cursor. */
17272 row = row_containing_pos (w, PT, r0, NULL, 0);
17273 if (row)
17274 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17275 else
17276 emacs_abort ();
17277 return 1;
17278 }
17279 }
17280
17281 /* Handle the case that changes are all below what is displayed in
17282 the window, and that PT is in the window. This shortcut cannot
17283 be taken if ZV is visible in the window, and text has been added
17284 there that is visible in the window. */
17285 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17286 /* ZV is not visible in the window, or there are no
17287 changes at ZV, actually. */
17288 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17289 || first_changed_charpos == last_changed_charpos))
17290 {
17291 struct glyph_row *r0;
17292
17293 /* Give up if PT is not in the window. Note that it already has
17294 been checked at the start of try_window_id that PT is not in
17295 front of the window start. */
17296 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17297 GIVE_UP (14);
17298
17299 /* If window start is unchanged, we can reuse the whole matrix
17300 as is, without changing glyph positions since no text has
17301 been added/removed in front of the window end. */
17302 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17303 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17304 /* PT must not be in a partially visible line. */
17305 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17306 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17307 {
17308 /* We have to compute the window end anew since text
17309 could have been added/removed after it. */
17310 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17311 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17312
17313 /* Set the cursor. */
17314 row = row_containing_pos (w, PT, r0, NULL, 0);
17315 if (row)
17316 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17317 else
17318 emacs_abort ();
17319 return 2;
17320 }
17321 }
17322
17323 /* Give up if window start is in the changed area.
17324
17325 The condition used to read
17326
17327 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17328
17329 but why that was tested escapes me at the moment. */
17330 if (CHARPOS (start) >= first_changed_charpos
17331 && CHARPOS (start) <= last_changed_charpos)
17332 GIVE_UP (15);
17333
17334 /* Check that window start agrees with the start of the first glyph
17335 row in its current matrix. Check this after we know the window
17336 start is not in changed text, otherwise positions would not be
17337 comparable. */
17338 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17339 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17340 GIVE_UP (16);
17341
17342 /* Give up if the window ends in strings. Overlay strings
17343 at the end are difficult to handle, so don't try. */
17344 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
17345 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17346 GIVE_UP (20);
17347
17348 /* Compute the position at which we have to start displaying new
17349 lines. Some of the lines at the top of the window might be
17350 reusable because they are not displaying changed text. Find the
17351 last row in W's current matrix not affected by changes at the
17352 start of current_buffer. Value is null if changes start in the
17353 first line of window. */
17354 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17355 if (last_unchanged_at_beg_row)
17356 {
17357 /* Avoid starting to display in the middle of a character, a TAB
17358 for instance. This is easier than to set up the iterator
17359 exactly, and it's not a frequent case, so the additional
17360 effort wouldn't really pay off. */
17361 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17362 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17363 && last_unchanged_at_beg_row > w->current_matrix->rows)
17364 --last_unchanged_at_beg_row;
17365
17366 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17367 GIVE_UP (17);
17368
17369 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17370 GIVE_UP (18);
17371 start_pos = it.current.pos;
17372
17373 /* Start displaying new lines in the desired matrix at the same
17374 vpos we would use in the current matrix, i.e. below
17375 last_unchanged_at_beg_row. */
17376 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17377 current_matrix);
17378 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17379 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17380
17381 eassert (it.hpos == 0 && it.current_x == 0);
17382 }
17383 else
17384 {
17385 /* There are no reusable lines at the start of the window.
17386 Start displaying in the first text line. */
17387 start_display (&it, w, start);
17388 it.vpos = it.first_vpos;
17389 start_pos = it.current.pos;
17390 }
17391
17392 /* Find the first row that is not affected by changes at the end of
17393 the buffer. Value will be null if there is no unchanged row, in
17394 which case we must redisplay to the end of the window. delta
17395 will be set to the value by which buffer positions beginning with
17396 first_unchanged_at_end_row have to be adjusted due to text
17397 changes. */
17398 first_unchanged_at_end_row
17399 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17400 IF_DEBUG (debug_delta = delta);
17401 IF_DEBUG (debug_delta_bytes = delta_bytes);
17402
17403 /* Set stop_pos to the buffer position up to which we will have to
17404 display new lines. If first_unchanged_at_end_row != NULL, this
17405 is the buffer position of the start of the line displayed in that
17406 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17407 that we don't stop at a buffer position. */
17408 stop_pos = 0;
17409 if (first_unchanged_at_end_row)
17410 {
17411 eassert (last_unchanged_at_beg_row == NULL
17412 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17413
17414 /* If this is a continuation line, move forward to the next one
17415 that isn't. Changes in lines above affect this line.
17416 Caution: this may move first_unchanged_at_end_row to a row
17417 not displaying text. */
17418 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17419 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17420 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17421 < it.last_visible_y))
17422 ++first_unchanged_at_end_row;
17423
17424 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17425 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17426 >= it.last_visible_y))
17427 first_unchanged_at_end_row = NULL;
17428 else
17429 {
17430 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17431 + delta);
17432 first_unchanged_at_end_vpos
17433 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17434 eassert (stop_pos >= Z - END_UNCHANGED);
17435 }
17436 }
17437 else if (last_unchanged_at_beg_row == NULL)
17438 GIVE_UP (19);
17439
17440
17441 #ifdef GLYPH_DEBUG
17442
17443 /* Either there is no unchanged row at the end, or the one we have
17444 now displays text. This is a necessary condition for the window
17445 end pos calculation at the end of this function. */
17446 eassert (first_unchanged_at_end_row == NULL
17447 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17448
17449 debug_last_unchanged_at_beg_vpos
17450 = (last_unchanged_at_beg_row
17451 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17452 : -1);
17453 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17454
17455 #endif /* GLYPH_DEBUG */
17456
17457
17458 /* Display new lines. Set last_text_row to the last new line
17459 displayed which has text on it, i.e. might end up as being the
17460 line where the window_end_vpos is. */
17461 w->cursor.vpos = -1;
17462 last_text_row = NULL;
17463 overlay_arrow_seen = 0;
17464 while (it.current_y < it.last_visible_y
17465 && !fonts_changed_p
17466 && (first_unchanged_at_end_row == NULL
17467 || IT_CHARPOS (it) < stop_pos))
17468 {
17469 if (display_line (&it))
17470 last_text_row = it.glyph_row - 1;
17471 }
17472
17473 if (fonts_changed_p)
17474 return -1;
17475
17476
17477 /* Compute differences in buffer positions, y-positions etc. for
17478 lines reused at the bottom of the window. Compute what we can
17479 scroll. */
17480 if (first_unchanged_at_end_row
17481 /* No lines reused because we displayed everything up to the
17482 bottom of the window. */
17483 && it.current_y < it.last_visible_y)
17484 {
17485 dvpos = (it.vpos
17486 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17487 current_matrix));
17488 dy = it.current_y - first_unchanged_at_end_row->y;
17489 run.current_y = first_unchanged_at_end_row->y;
17490 run.desired_y = run.current_y + dy;
17491 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17492 }
17493 else
17494 {
17495 delta = delta_bytes = dvpos = dy
17496 = run.current_y = run.desired_y = run.height = 0;
17497 first_unchanged_at_end_row = NULL;
17498 }
17499 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17500
17501
17502 /* Find the cursor if not already found. We have to decide whether
17503 PT will appear on this window (it sometimes doesn't, but this is
17504 not a very frequent case.) This decision has to be made before
17505 the current matrix is altered. A value of cursor.vpos < 0 means
17506 that PT is either in one of the lines beginning at
17507 first_unchanged_at_end_row or below the window. Don't care for
17508 lines that might be displayed later at the window end; as
17509 mentioned, this is not a frequent case. */
17510 if (w->cursor.vpos < 0)
17511 {
17512 /* Cursor in unchanged rows at the top? */
17513 if (PT < CHARPOS (start_pos)
17514 && last_unchanged_at_beg_row)
17515 {
17516 row = row_containing_pos (w, PT,
17517 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17518 last_unchanged_at_beg_row + 1, 0);
17519 if (row)
17520 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17521 }
17522
17523 /* Start from first_unchanged_at_end_row looking for PT. */
17524 else if (first_unchanged_at_end_row)
17525 {
17526 row = row_containing_pos (w, PT - delta,
17527 first_unchanged_at_end_row, NULL, 0);
17528 if (row)
17529 set_cursor_from_row (w, row, w->current_matrix, delta,
17530 delta_bytes, dy, dvpos);
17531 }
17532
17533 /* Give up if cursor was not found. */
17534 if (w->cursor.vpos < 0)
17535 {
17536 clear_glyph_matrix (w->desired_matrix);
17537 return -1;
17538 }
17539 }
17540
17541 /* Don't let the cursor end in the scroll margins. */
17542 {
17543 int this_scroll_margin, cursor_height;
17544 int frame_line_height = default_line_pixel_height (w);
17545 int window_total_lines
17546 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
17547
17548 this_scroll_margin =
17549 max (0, min (scroll_margin, window_total_lines / 4));
17550 this_scroll_margin *= frame_line_height;
17551 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17552
17553 if ((w->cursor.y < this_scroll_margin
17554 && CHARPOS (start) > BEGV)
17555 /* Old redisplay didn't take scroll margin into account at the bottom,
17556 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17557 || (w->cursor.y + (make_cursor_line_fully_visible_p
17558 ? cursor_height + this_scroll_margin
17559 : 1)) > it.last_visible_y)
17560 {
17561 w->cursor.vpos = -1;
17562 clear_glyph_matrix (w->desired_matrix);
17563 return -1;
17564 }
17565 }
17566
17567 /* Scroll the display. Do it before changing the current matrix so
17568 that xterm.c doesn't get confused about where the cursor glyph is
17569 found. */
17570 if (dy && run.height)
17571 {
17572 update_begin (f);
17573
17574 if (FRAME_WINDOW_P (f))
17575 {
17576 FRAME_RIF (f)->update_window_begin_hook (w);
17577 FRAME_RIF (f)->clear_window_mouse_face (w);
17578 FRAME_RIF (f)->scroll_run_hook (w, &run);
17579 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17580 }
17581 else
17582 {
17583 /* Terminal frame. In this case, dvpos gives the number of
17584 lines to scroll by; dvpos < 0 means scroll up. */
17585 int from_vpos
17586 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17587 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17588 int end = (WINDOW_TOP_EDGE_LINE (w)
17589 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17590 + window_internal_height (w));
17591
17592 #if defined (HAVE_GPM) || defined (MSDOS)
17593 x_clear_window_mouse_face (w);
17594 #endif
17595 /* Perform the operation on the screen. */
17596 if (dvpos > 0)
17597 {
17598 /* Scroll last_unchanged_at_beg_row to the end of the
17599 window down dvpos lines. */
17600 set_terminal_window (f, end);
17601
17602 /* On dumb terminals delete dvpos lines at the end
17603 before inserting dvpos empty lines. */
17604 if (!FRAME_SCROLL_REGION_OK (f))
17605 ins_del_lines (f, end - dvpos, -dvpos);
17606
17607 /* Insert dvpos empty lines in front of
17608 last_unchanged_at_beg_row. */
17609 ins_del_lines (f, from, dvpos);
17610 }
17611 else if (dvpos < 0)
17612 {
17613 /* Scroll up last_unchanged_at_beg_vpos to the end of
17614 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17615 set_terminal_window (f, end);
17616
17617 /* Delete dvpos lines in front of
17618 last_unchanged_at_beg_vpos. ins_del_lines will set
17619 the cursor to the given vpos and emit |dvpos| delete
17620 line sequences. */
17621 ins_del_lines (f, from + dvpos, dvpos);
17622
17623 /* On a dumb terminal insert dvpos empty lines at the
17624 end. */
17625 if (!FRAME_SCROLL_REGION_OK (f))
17626 ins_del_lines (f, end + dvpos, -dvpos);
17627 }
17628
17629 set_terminal_window (f, 0);
17630 }
17631
17632 update_end (f);
17633 }
17634
17635 /* Shift reused rows of the current matrix to the right position.
17636 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17637 text. */
17638 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17639 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17640 if (dvpos < 0)
17641 {
17642 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17643 bottom_vpos, dvpos);
17644 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17645 bottom_vpos);
17646 }
17647 else if (dvpos > 0)
17648 {
17649 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17650 bottom_vpos, dvpos);
17651 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17652 first_unchanged_at_end_vpos + dvpos);
17653 }
17654
17655 /* For frame-based redisplay, make sure that current frame and window
17656 matrix are in sync with respect to glyph memory. */
17657 if (!FRAME_WINDOW_P (f))
17658 sync_frame_with_window_matrix_rows (w);
17659
17660 /* Adjust buffer positions in reused rows. */
17661 if (delta || delta_bytes)
17662 increment_matrix_positions (current_matrix,
17663 first_unchanged_at_end_vpos + dvpos,
17664 bottom_vpos, delta, delta_bytes);
17665
17666 /* Adjust Y positions. */
17667 if (dy)
17668 shift_glyph_matrix (w, current_matrix,
17669 first_unchanged_at_end_vpos + dvpos,
17670 bottom_vpos, dy);
17671
17672 if (first_unchanged_at_end_row)
17673 {
17674 first_unchanged_at_end_row += dvpos;
17675 if (first_unchanged_at_end_row->y >= it.last_visible_y
17676 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17677 first_unchanged_at_end_row = NULL;
17678 }
17679
17680 /* If scrolling up, there may be some lines to display at the end of
17681 the window. */
17682 last_text_row_at_end = NULL;
17683 if (dy < 0)
17684 {
17685 /* Scrolling up can leave for example a partially visible line
17686 at the end of the window to be redisplayed. */
17687 /* Set last_row to the glyph row in the current matrix where the
17688 window end line is found. It has been moved up or down in
17689 the matrix by dvpos. */
17690 int last_vpos = w->window_end_vpos + dvpos;
17691 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17692
17693 /* If last_row is the window end line, it should display text. */
17694 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
17695
17696 /* If window end line was partially visible before, begin
17697 displaying at that line. Otherwise begin displaying with the
17698 line following it. */
17699 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17700 {
17701 init_to_row_start (&it, w, last_row);
17702 it.vpos = last_vpos;
17703 it.current_y = last_row->y;
17704 }
17705 else
17706 {
17707 init_to_row_end (&it, w, last_row);
17708 it.vpos = 1 + last_vpos;
17709 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17710 ++last_row;
17711 }
17712
17713 /* We may start in a continuation line. If so, we have to
17714 get the right continuation_lines_width and current_x. */
17715 it.continuation_lines_width = last_row->continuation_lines_width;
17716 it.hpos = it.current_x = 0;
17717
17718 /* Display the rest of the lines at the window end. */
17719 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17720 while (it.current_y < it.last_visible_y
17721 && !fonts_changed_p)
17722 {
17723 /* Is it always sure that the display agrees with lines in
17724 the current matrix? I don't think so, so we mark rows
17725 displayed invalid in the current matrix by setting their
17726 enabled_p flag to zero. */
17727 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17728 if (display_line (&it))
17729 last_text_row_at_end = it.glyph_row - 1;
17730 }
17731 }
17732
17733 /* Update window_end_pos and window_end_vpos. */
17734 if (first_unchanged_at_end_row && !last_text_row_at_end)
17735 {
17736 /* Window end line if one of the preserved rows from the current
17737 matrix. Set row to the last row displaying text in current
17738 matrix starting at first_unchanged_at_end_row, after
17739 scrolling. */
17740 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17741 row = find_last_row_displaying_text (w->current_matrix, &it,
17742 first_unchanged_at_end_row);
17743 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17744 adjust_window_ends (w, row, 1);
17745 eassert (w->window_end_bytepos >= 0);
17746 IF_DEBUG (debug_method_add (w, "A"));
17747 }
17748 else if (last_text_row_at_end)
17749 {
17750 adjust_window_ends (w, last_text_row_at_end, 0);
17751 eassert (w->window_end_bytepos >= 0);
17752 IF_DEBUG (debug_method_add (w, "B"));
17753 }
17754 else if (last_text_row)
17755 {
17756 /* We have displayed either to the end of the window or at the
17757 end of the window, i.e. the last row with text is to be found
17758 in the desired matrix. */
17759 adjust_window_ends (w, last_text_row, 0);
17760 eassert (w->window_end_bytepos >= 0);
17761 }
17762 else if (first_unchanged_at_end_row == NULL
17763 && last_text_row == NULL
17764 && last_text_row_at_end == NULL)
17765 {
17766 /* Displayed to end of window, but no line containing text was
17767 displayed. Lines were deleted at the end of the window. */
17768 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17769 int vpos = w->window_end_vpos;
17770 struct glyph_row *current_row = current_matrix->rows + vpos;
17771 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17772
17773 for (row = NULL;
17774 row == NULL && vpos >= first_vpos;
17775 --vpos, --current_row, --desired_row)
17776 {
17777 if (desired_row->enabled_p)
17778 {
17779 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
17780 row = desired_row;
17781 }
17782 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
17783 row = current_row;
17784 }
17785
17786 eassert (row != NULL);
17787 w->window_end_vpos = vpos + 1;
17788 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17789 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17790 eassert (w->window_end_bytepos >= 0);
17791 IF_DEBUG (debug_method_add (w, "C"));
17792 }
17793 else
17794 emacs_abort ();
17795
17796 IF_DEBUG (debug_end_pos = w->window_end_pos;
17797 debug_end_vpos = w->window_end_vpos);
17798
17799 /* Record that display has not been completed. */
17800 w->window_end_valid = 0;
17801 w->desired_matrix->no_scrolling_p = 1;
17802 return 3;
17803
17804 #undef GIVE_UP
17805 }
17806
17807
17808 \f
17809 /***********************************************************************
17810 More debugging support
17811 ***********************************************************************/
17812
17813 #ifdef GLYPH_DEBUG
17814
17815 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17816 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17817 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17818
17819
17820 /* Dump the contents of glyph matrix MATRIX on stderr.
17821
17822 GLYPHS 0 means don't show glyph contents.
17823 GLYPHS 1 means show glyphs in short form
17824 GLYPHS > 1 means show glyphs in long form. */
17825
17826 void
17827 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17828 {
17829 int i;
17830 for (i = 0; i < matrix->nrows; ++i)
17831 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17832 }
17833
17834
17835 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17836 the glyph row and area where the glyph comes from. */
17837
17838 void
17839 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17840 {
17841 if (glyph->type == CHAR_GLYPH
17842 || glyph->type == GLYPHLESS_GLYPH)
17843 {
17844 fprintf (stderr,
17845 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17846 glyph - row->glyphs[TEXT_AREA],
17847 (glyph->type == CHAR_GLYPH
17848 ? 'C'
17849 : 'G'),
17850 glyph->charpos,
17851 (BUFFERP (glyph->object)
17852 ? 'B'
17853 : (STRINGP (glyph->object)
17854 ? 'S'
17855 : (INTEGERP (glyph->object)
17856 ? '0'
17857 : '-'))),
17858 glyph->pixel_width,
17859 glyph->u.ch,
17860 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17861 ? glyph->u.ch
17862 : '.'),
17863 glyph->face_id,
17864 glyph->left_box_line_p,
17865 glyph->right_box_line_p);
17866 }
17867 else if (glyph->type == STRETCH_GLYPH)
17868 {
17869 fprintf (stderr,
17870 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17871 glyph - row->glyphs[TEXT_AREA],
17872 'S',
17873 glyph->charpos,
17874 (BUFFERP (glyph->object)
17875 ? 'B'
17876 : (STRINGP (glyph->object)
17877 ? 'S'
17878 : (INTEGERP (glyph->object)
17879 ? '0'
17880 : '-'))),
17881 glyph->pixel_width,
17882 0,
17883 ' ',
17884 glyph->face_id,
17885 glyph->left_box_line_p,
17886 glyph->right_box_line_p);
17887 }
17888 else if (glyph->type == IMAGE_GLYPH)
17889 {
17890 fprintf (stderr,
17891 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17892 glyph - row->glyphs[TEXT_AREA],
17893 'I',
17894 glyph->charpos,
17895 (BUFFERP (glyph->object)
17896 ? 'B'
17897 : (STRINGP (glyph->object)
17898 ? 'S'
17899 : (INTEGERP (glyph->object)
17900 ? '0'
17901 : '-'))),
17902 glyph->pixel_width,
17903 glyph->u.img_id,
17904 '.',
17905 glyph->face_id,
17906 glyph->left_box_line_p,
17907 glyph->right_box_line_p);
17908 }
17909 else if (glyph->type == COMPOSITE_GLYPH)
17910 {
17911 fprintf (stderr,
17912 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
17913 glyph - row->glyphs[TEXT_AREA],
17914 '+',
17915 glyph->charpos,
17916 (BUFFERP (glyph->object)
17917 ? 'B'
17918 : (STRINGP (glyph->object)
17919 ? 'S'
17920 : (INTEGERP (glyph->object)
17921 ? '0'
17922 : '-'))),
17923 glyph->pixel_width,
17924 glyph->u.cmp.id);
17925 if (glyph->u.cmp.automatic)
17926 fprintf (stderr,
17927 "[%d-%d]",
17928 glyph->slice.cmp.from, glyph->slice.cmp.to);
17929 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17930 glyph->face_id,
17931 glyph->left_box_line_p,
17932 glyph->right_box_line_p);
17933 }
17934 }
17935
17936
17937 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17938 GLYPHS 0 means don't show glyph contents.
17939 GLYPHS 1 means show glyphs in short form
17940 GLYPHS > 1 means show glyphs in long form. */
17941
17942 void
17943 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17944 {
17945 if (glyphs != 1)
17946 {
17947 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17948 fprintf (stderr, "==============================================================================\n");
17949
17950 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17951 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17952 vpos,
17953 MATRIX_ROW_START_CHARPOS (row),
17954 MATRIX_ROW_END_CHARPOS (row),
17955 row->used[TEXT_AREA],
17956 row->contains_overlapping_glyphs_p,
17957 row->enabled_p,
17958 row->truncated_on_left_p,
17959 row->truncated_on_right_p,
17960 row->continued_p,
17961 MATRIX_ROW_CONTINUATION_LINE_P (row),
17962 MATRIX_ROW_DISPLAYS_TEXT_P (row),
17963 row->ends_at_zv_p,
17964 row->fill_line_p,
17965 row->ends_in_middle_of_char_p,
17966 row->starts_in_middle_of_char_p,
17967 row->mouse_face_p,
17968 row->x,
17969 row->y,
17970 row->pixel_width,
17971 row->height,
17972 row->visible_height,
17973 row->ascent,
17974 row->phys_ascent);
17975 /* The next 3 lines should align to "Start" in the header. */
17976 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
17977 row->end.overlay_string_index,
17978 row->continuation_lines_width);
17979 fprintf (stderr, " %9"pI"d %9"pI"d\n",
17980 CHARPOS (row->start.string_pos),
17981 CHARPOS (row->end.string_pos));
17982 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
17983 row->end.dpvec_index);
17984 }
17985
17986 if (glyphs > 1)
17987 {
17988 int area;
17989
17990 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17991 {
17992 struct glyph *glyph = row->glyphs[area];
17993 struct glyph *glyph_end = glyph + row->used[area];
17994
17995 /* Glyph for a line end in text. */
17996 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
17997 ++glyph_end;
17998
17999 if (glyph < glyph_end)
18000 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18001
18002 for (; glyph < glyph_end; ++glyph)
18003 dump_glyph (row, glyph, area);
18004 }
18005 }
18006 else if (glyphs == 1)
18007 {
18008 int area;
18009
18010 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18011 {
18012 char *s = alloca (row->used[area] + 4);
18013 int i;
18014
18015 for (i = 0; i < row->used[area]; ++i)
18016 {
18017 struct glyph *glyph = row->glyphs[area] + i;
18018 if (i == row->used[area] - 1
18019 && area == TEXT_AREA
18020 && INTEGERP (glyph->object)
18021 && glyph->type == CHAR_GLYPH
18022 && glyph->u.ch == ' ')
18023 {
18024 strcpy (&s[i], "[\\n]");
18025 i += 4;
18026 }
18027 else if (glyph->type == CHAR_GLYPH
18028 && glyph->u.ch < 0x80
18029 && glyph->u.ch >= ' ')
18030 s[i] = glyph->u.ch;
18031 else
18032 s[i] = '.';
18033 }
18034
18035 s[i] = '\0';
18036 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18037 }
18038 }
18039 }
18040
18041
18042 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18043 Sdump_glyph_matrix, 0, 1, "p",
18044 doc: /* Dump the current matrix of the selected window to stderr.
18045 Shows contents of glyph row structures. With non-nil
18046 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18047 glyphs in short form, otherwise show glyphs in long form. */)
18048 (Lisp_Object glyphs)
18049 {
18050 struct window *w = XWINDOW (selected_window);
18051 struct buffer *buffer = XBUFFER (w->contents);
18052
18053 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18054 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18055 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18056 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18057 fprintf (stderr, "=============================================\n");
18058 dump_glyph_matrix (w->current_matrix,
18059 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18060 return Qnil;
18061 }
18062
18063
18064 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18065 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18066 (void)
18067 {
18068 struct frame *f = XFRAME (selected_frame);
18069 dump_glyph_matrix (f->current_matrix, 1);
18070 return Qnil;
18071 }
18072
18073
18074 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18075 doc: /* Dump glyph row ROW to stderr.
18076 GLYPH 0 means don't dump glyphs.
18077 GLYPH 1 means dump glyphs in short form.
18078 GLYPH > 1 or omitted means dump glyphs in long form. */)
18079 (Lisp_Object row, Lisp_Object glyphs)
18080 {
18081 struct glyph_matrix *matrix;
18082 EMACS_INT vpos;
18083
18084 CHECK_NUMBER (row);
18085 matrix = XWINDOW (selected_window)->current_matrix;
18086 vpos = XINT (row);
18087 if (vpos >= 0 && vpos < matrix->nrows)
18088 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18089 vpos,
18090 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18091 return Qnil;
18092 }
18093
18094
18095 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18096 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18097 GLYPH 0 means don't dump glyphs.
18098 GLYPH 1 means dump glyphs in short form.
18099 GLYPH > 1 or omitted means dump glyphs in long form. */)
18100 (Lisp_Object row, Lisp_Object glyphs)
18101 {
18102 struct frame *sf = SELECTED_FRAME ();
18103 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18104 EMACS_INT vpos;
18105
18106 CHECK_NUMBER (row);
18107 vpos = XINT (row);
18108 if (vpos >= 0 && vpos < m->nrows)
18109 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18110 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18111 return Qnil;
18112 }
18113
18114
18115 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18116 doc: /* Toggle tracing of redisplay.
18117 With ARG, turn tracing on if and only if ARG is positive. */)
18118 (Lisp_Object arg)
18119 {
18120 if (NILP (arg))
18121 trace_redisplay_p = !trace_redisplay_p;
18122 else
18123 {
18124 arg = Fprefix_numeric_value (arg);
18125 trace_redisplay_p = XINT (arg) > 0;
18126 }
18127
18128 return Qnil;
18129 }
18130
18131
18132 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18133 doc: /* Like `format', but print result to stderr.
18134 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18135 (ptrdiff_t nargs, Lisp_Object *args)
18136 {
18137 Lisp_Object s = Fformat (nargs, args);
18138 fprintf (stderr, "%s", SDATA (s));
18139 return Qnil;
18140 }
18141
18142 #endif /* GLYPH_DEBUG */
18143
18144
18145 \f
18146 /***********************************************************************
18147 Building Desired Matrix Rows
18148 ***********************************************************************/
18149
18150 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18151 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18152
18153 static struct glyph_row *
18154 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18155 {
18156 struct frame *f = XFRAME (WINDOW_FRAME (w));
18157 struct buffer *buffer = XBUFFER (w->contents);
18158 struct buffer *old = current_buffer;
18159 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18160 int arrow_len = SCHARS (overlay_arrow_string);
18161 const unsigned char *arrow_end = arrow_string + arrow_len;
18162 const unsigned char *p;
18163 struct it it;
18164 bool multibyte_p;
18165 int n_glyphs_before;
18166
18167 set_buffer_temp (buffer);
18168 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18169 it.glyph_row->used[TEXT_AREA] = 0;
18170 SET_TEXT_POS (it.position, 0, 0);
18171
18172 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18173 p = arrow_string;
18174 while (p < arrow_end)
18175 {
18176 Lisp_Object face, ilisp;
18177
18178 /* Get the next character. */
18179 if (multibyte_p)
18180 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18181 else
18182 {
18183 it.c = it.char_to_display = *p, it.len = 1;
18184 if (! ASCII_CHAR_P (it.c))
18185 it.char_to_display = BYTE8_TO_CHAR (it.c);
18186 }
18187 p += it.len;
18188
18189 /* Get its face. */
18190 ilisp = make_number (p - arrow_string);
18191 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18192 it.face_id = compute_char_face (f, it.char_to_display, face);
18193
18194 /* Compute its width, get its glyphs. */
18195 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18196 SET_TEXT_POS (it.position, -1, -1);
18197 PRODUCE_GLYPHS (&it);
18198
18199 /* If this character doesn't fit any more in the line, we have
18200 to remove some glyphs. */
18201 if (it.current_x > it.last_visible_x)
18202 {
18203 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18204 break;
18205 }
18206 }
18207
18208 set_buffer_temp (old);
18209 return it.glyph_row;
18210 }
18211
18212
18213 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18214 glyphs to insert is determined by produce_special_glyphs. */
18215
18216 static void
18217 insert_left_trunc_glyphs (struct it *it)
18218 {
18219 struct it truncate_it;
18220 struct glyph *from, *end, *to, *toend;
18221
18222 eassert (!FRAME_WINDOW_P (it->f)
18223 || (!it->glyph_row->reversed_p
18224 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18225 || (it->glyph_row->reversed_p
18226 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18227
18228 /* Get the truncation glyphs. */
18229 truncate_it = *it;
18230 truncate_it.current_x = 0;
18231 truncate_it.face_id = DEFAULT_FACE_ID;
18232 truncate_it.glyph_row = &scratch_glyph_row;
18233 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18234 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18235 truncate_it.object = make_number (0);
18236 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18237
18238 /* Overwrite glyphs from IT with truncation glyphs. */
18239 if (!it->glyph_row->reversed_p)
18240 {
18241 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18242
18243 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18244 end = from + tused;
18245 to = it->glyph_row->glyphs[TEXT_AREA];
18246 toend = to + it->glyph_row->used[TEXT_AREA];
18247 if (FRAME_WINDOW_P (it->f))
18248 {
18249 /* On GUI frames, when variable-size fonts are displayed,
18250 the truncation glyphs may need more pixels than the row's
18251 glyphs they overwrite. We overwrite more glyphs to free
18252 enough screen real estate, and enlarge the stretch glyph
18253 on the right (see display_line), if there is one, to
18254 preserve the screen position of the truncation glyphs on
18255 the right. */
18256 int w = 0;
18257 struct glyph *g = to;
18258 short used;
18259
18260 /* The first glyph could be partially visible, in which case
18261 it->glyph_row->x will be negative. But we want the left
18262 truncation glyphs to be aligned at the left margin of the
18263 window, so we override the x coordinate at which the row
18264 will begin. */
18265 it->glyph_row->x = 0;
18266 while (g < toend && w < it->truncation_pixel_width)
18267 {
18268 w += g->pixel_width;
18269 ++g;
18270 }
18271 if (g - to - tused > 0)
18272 {
18273 memmove (to + tused, g, (toend - g) * sizeof(*g));
18274 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18275 }
18276 used = it->glyph_row->used[TEXT_AREA];
18277 if (it->glyph_row->truncated_on_right_p
18278 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18279 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18280 == STRETCH_GLYPH)
18281 {
18282 int extra = w - it->truncation_pixel_width;
18283
18284 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18285 }
18286 }
18287
18288 while (from < end)
18289 *to++ = *from++;
18290
18291 /* There may be padding glyphs left over. Overwrite them too. */
18292 if (!FRAME_WINDOW_P (it->f))
18293 {
18294 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18295 {
18296 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18297 while (from < end)
18298 *to++ = *from++;
18299 }
18300 }
18301
18302 if (to > toend)
18303 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18304 }
18305 else
18306 {
18307 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18308
18309 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18310 that back to front. */
18311 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18312 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18313 toend = it->glyph_row->glyphs[TEXT_AREA];
18314 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18315 if (FRAME_WINDOW_P (it->f))
18316 {
18317 int w = 0;
18318 struct glyph *g = to;
18319
18320 while (g >= toend && w < it->truncation_pixel_width)
18321 {
18322 w += g->pixel_width;
18323 --g;
18324 }
18325 if (to - g - tused > 0)
18326 to = g + tused;
18327 if (it->glyph_row->truncated_on_right_p
18328 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18329 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18330 {
18331 int extra = w - it->truncation_pixel_width;
18332
18333 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18334 }
18335 }
18336
18337 while (from >= end && to >= toend)
18338 *to-- = *from--;
18339 if (!FRAME_WINDOW_P (it->f))
18340 {
18341 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18342 {
18343 from =
18344 truncate_it.glyph_row->glyphs[TEXT_AREA]
18345 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18346 while (from >= end && to >= toend)
18347 *to-- = *from--;
18348 }
18349 }
18350 if (from >= end)
18351 {
18352 /* Need to free some room before prepending additional
18353 glyphs. */
18354 int move_by = from - end + 1;
18355 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18356 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18357
18358 for ( ; g >= g0; g--)
18359 g[move_by] = *g;
18360 while (from >= end)
18361 *to-- = *from--;
18362 it->glyph_row->used[TEXT_AREA] += move_by;
18363 }
18364 }
18365 }
18366
18367 /* Compute the hash code for ROW. */
18368 unsigned
18369 row_hash (struct glyph_row *row)
18370 {
18371 int area, k;
18372 unsigned hashval = 0;
18373
18374 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18375 for (k = 0; k < row->used[area]; ++k)
18376 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18377 + row->glyphs[area][k].u.val
18378 + row->glyphs[area][k].face_id
18379 + row->glyphs[area][k].padding_p
18380 + (row->glyphs[area][k].type << 2));
18381
18382 return hashval;
18383 }
18384
18385 /* Compute the pixel height and width of IT->glyph_row.
18386
18387 Most of the time, ascent and height of a display line will be equal
18388 to the max_ascent and max_height values of the display iterator
18389 structure. This is not the case if
18390
18391 1. We hit ZV without displaying anything. In this case, max_ascent
18392 and max_height will be zero.
18393
18394 2. We have some glyphs that don't contribute to the line height.
18395 (The glyph row flag contributes_to_line_height_p is for future
18396 pixmap extensions).
18397
18398 The first case is easily covered by using default values because in
18399 these cases, the line height does not really matter, except that it
18400 must not be zero. */
18401
18402 static void
18403 compute_line_metrics (struct it *it)
18404 {
18405 struct glyph_row *row = it->glyph_row;
18406
18407 if (FRAME_WINDOW_P (it->f))
18408 {
18409 int i, min_y, max_y;
18410
18411 /* The line may consist of one space only, that was added to
18412 place the cursor on it. If so, the row's height hasn't been
18413 computed yet. */
18414 if (row->height == 0)
18415 {
18416 if (it->max_ascent + it->max_descent == 0)
18417 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18418 row->ascent = it->max_ascent;
18419 row->height = it->max_ascent + it->max_descent;
18420 row->phys_ascent = it->max_phys_ascent;
18421 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18422 row->extra_line_spacing = it->max_extra_line_spacing;
18423 }
18424
18425 /* Compute the width of this line. */
18426 row->pixel_width = row->x;
18427 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18428 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18429
18430 eassert (row->pixel_width >= 0);
18431 eassert (row->ascent >= 0 && row->height > 0);
18432
18433 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18434 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18435
18436 /* If first line's physical ascent is larger than its logical
18437 ascent, use the physical ascent, and make the row taller.
18438 This makes accented characters fully visible. */
18439 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18440 && row->phys_ascent > row->ascent)
18441 {
18442 row->height += row->phys_ascent - row->ascent;
18443 row->ascent = row->phys_ascent;
18444 }
18445
18446 /* Compute how much of the line is visible. */
18447 row->visible_height = row->height;
18448
18449 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18450 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18451
18452 if (row->y < min_y)
18453 row->visible_height -= min_y - row->y;
18454 if (row->y + row->height > max_y)
18455 row->visible_height -= row->y + row->height - max_y;
18456 }
18457 else
18458 {
18459 row->pixel_width = row->used[TEXT_AREA];
18460 if (row->continued_p)
18461 row->pixel_width -= it->continuation_pixel_width;
18462 else if (row->truncated_on_right_p)
18463 row->pixel_width -= it->truncation_pixel_width;
18464 row->ascent = row->phys_ascent = 0;
18465 row->height = row->phys_height = row->visible_height = 1;
18466 row->extra_line_spacing = 0;
18467 }
18468
18469 /* Compute a hash code for this row. */
18470 row->hash = row_hash (row);
18471
18472 it->max_ascent = it->max_descent = 0;
18473 it->max_phys_ascent = it->max_phys_descent = 0;
18474 }
18475
18476
18477 /* Append one space to the glyph row of iterator IT if doing a
18478 window-based redisplay. The space has the same face as
18479 IT->face_id. Value is non-zero if a space was added.
18480
18481 This function is called to make sure that there is always one glyph
18482 at the end of a glyph row that the cursor can be set on under
18483 window-systems. (If there weren't such a glyph we would not know
18484 how wide and tall a box cursor should be displayed).
18485
18486 At the same time this space let's a nicely handle clearing to the
18487 end of the line if the row ends in italic text. */
18488
18489 static int
18490 append_space_for_newline (struct it *it, int default_face_p)
18491 {
18492 if (FRAME_WINDOW_P (it->f))
18493 {
18494 int n = it->glyph_row->used[TEXT_AREA];
18495
18496 if (it->glyph_row->glyphs[TEXT_AREA] + n
18497 < it->glyph_row->glyphs[1 + TEXT_AREA])
18498 {
18499 /* Save some values that must not be changed.
18500 Must save IT->c and IT->len because otherwise
18501 ITERATOR_AT_END_P wouldn't work anymore after
18502 append_space_for_newline has been called. */
18503 enum display_element_type saved_what = it->what;
18504 int saved_c = it->c, saved_len = it->len;
18505 int saved_char_to_display = it->char_to_display;
18506 int saved_x = it->current_x;
18507 int saved_face_id = it->face_id;
18508 int saved_box_end = it->end_of_box_run_p;
18509 struct text_pos saved_pos;
18510 Lisp_Object saved_object;
18511 struct face *face;
18512
18513 saved_object = it->object;
18514 saved_pos = it->position;
18515
18516 it->what = IT_CHARACTER;
18517 memset (&it->position, 0, sizeof it->position);
18518 it->object = make_number (0);
18519 it->c = it->char_to_display = ' ';
18520 it->len = 1;
18521
18522 /* If the default face was remapped, be sure to use the
18523 remapped face for the appended newline. */
18524 if (default_face_p)
18525 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18526 else if (it->face_before_selective_p)
18527 it->face_id = it->saved_face_id;
18528 face = FACE_FROM_ID (it->f, it->face_id);
18529 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18530 /* In R2L rows, we will prepend a stretch glyph that will
18531 have the end_of_box_run_p flag set for it, so there's no
18532 need for the appended newline glyph to have that flag
18533 set. */
18534 if (it->glyph_row->reversed_p
18535 /* But if the appended newline glyph goes all the way to
18536 the end of the row, there will be no stretch glyph,
18537 so leave the box flag set. */
18538 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18539 it->end_of_box_run_p = 0;
18540
18541 PRODUCE_GLYPHS (it);
18542
18543 it->override_ascent = -1;
18544 it->constrain_row_ascent_descent_p = 0;
18545 it->current_x = saved_x;
18546 it->object = saved_object;
18547 it->position = saved_pos;
18548 it->what = saved_what;
18549 it->face_id = saved_face_id;
18550 it->len = saved_len;
18551 it->c = saved_c;
18552 it->char_to_display = saved_char_to_display;
18553 it->end_of_box_run_p = saved_box_end;
18554 return 1;
18555 }
18556 }
18557
18558 return 0;
18559 }
18560
18561
18562 /* Extend the face of the last glyph in the text area of IT->glyph_row
18563 to the end of the display line. Called from display_line. If the
18564 glyph row is empty, add a space glyph to it so that we know the
18565 face to draw. Set the glyph row flag fill_line_p. If the glyph
18566 row is R2L, prepend a stretch glyph to cover the empty space to the
18567 left of the leftmost glyph. */
18568
18569 static void
18570 extend_face_to_end_of_line (struct it *it)
18571 {
18572 struct face *face, *default_face;
18573 struct frame *f = it->f;
18574
18575 /* If line is already filled, do nothing. Non window-system frames
18576 get a grace of one more ``pixel'' because their characters are
18577 1-``pixel'' wide, so they hit the equality too early. This grace
18578 is needed only for R2L rows that are not continued, to produce
18579 one extra blank where we could display the cursor. */
18580 if (it->current_x >= it->last_visible_x
18581 + (!FRAME_WINDOW_P (f)
18582 && it->glyph_row->reversed_p
18583 && !it->glyph_row->continued_p))
18584 return;
18585
18586 /* The default face, possibly remapped. */
18587 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18588
18589 /* Face extension extends the background and box of IT->face_id
18590 to the end of the line. If the background equals the background
18591 of the frame, we don't have to do anything. */
18592 if (it->face_before_selective_p)
18593 face = FACE_FROM_ID (f, it->saved_face_id);
18594 else
18595 face = FACE_FROM_ID (f, it->face_id);
18596
18597 if (FRAME_WINDOW_P (f)
18598 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
18599 && face->box == FACE_NO_BOX
18600 && face->background == FRAME_BACKGROUND_PIXEL (f)
18601 && !face->stipple
18602 && !it->glyph_row->reversed_p)
18603 return;
18604
18605 /* Set the glyph row flag indicating that the face of the last glyph
18606 in the text area has to be drawn to the end of the text area. */
18607 it->glyph_row->fill_line_p = 1;
18608
18609 /* If current character of IT is not ASCII, make sure we have the
18610 ASCII face. This will be automatically undone the next time
18611 get_next_display_element returns a multibyte character. Note
18612 that the character will always be single byte in unibyte
18613 text. */
18614 if (!ASCII_CHAR_P (it->c))
18615 {
18616 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18617 }
18618
18619 if (FRAME_WINDOW_P (f))
18620 {
18621 /* If the row is empty, add a space with the current face of IT,
18622 so that we know which face to draw. */
18623 if (it->glyph_row->used[TEXT_AREA] == 0)
18624 {
18625 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18626 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18627 it->glyph_row->used[TEXT_AREA] = 1;
18628 }
18629 #ifdef HAVE_WINDOW_SYSTEM
18630 if (it->glyph_row->reversed_p)
18631 {
18632 /* Prepend a stretch glyph to the row, such that the
18633 rightmost glyph will be drawn flushed all the way to the
18634 right margin of the window. The stretch glyph that will
18635 occupy the empty space, if any, to the left of the
18636 glyphs. */
18637 struct font *font = face->font ? face->font : FRAME_FONT (f);
18638 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18639 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18640 struct glyph *g;
18641 int row_width, stretch_ascent, stretch_width;
18642 struct text_pos saved_pos;
18643 int saved_face_id, saved_avoid_cursor, saved_box_start;
18644
18645 for (row_width = 0, g = row_start; g < row_end; g++)
18646 row_width += g->pixel_width;
18647 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18648 if (stretch_width > 0)
18649 {
18650 stretch_ascent =
18651 (((it->ascent + it->descent)
18652 * FONT_BASE (font)) / FONT_HEIGHT (font));
18653 saved_pos = it->position;
18654 memset (&it->position, 0, sizeof it->position);
18655 saved_avoid_cursor = it->avoid_cursor_p;
18656 it->avoid_cursor_p = 1;
18657 saved_face_id = it->face_id;
18658 saved_box_start = it->start_of_box_run_p;
18659 /* The last row's stretch glyph should get the default
18660 face, to avoid painting the rest of the window with
18661 the region face, if the region ends at ZV. */
18662 if (it->glyph_row->ends_at_zv_p)
18663 it->face_id = default_face->id;
18664 else
18665 it->face_id = face->id;
18666 it->start_of_box_run_p = 0;
18667 append_stretch_glyph (it, make_number (0), stretch_width,
18668 it->ascent + it->descent, stretch_ascent);
18669 it->position = saved_pos;
18670 it->avoid_cursor_p = saved_avoid_cursor;
18671 it->face_id = saved_face_id;
18672 it->start_of_box_run_p = saved_box_start;
18673 }
18674 }
18675 #endif /* HAVE_WINDOW_SYSTEM */
18676 }
18677 else
18678 {
18679 /* Save some values that must not be changed. */
18680 int saved_x = it->current_x;
18681 struct text_pos saved_pos;
18682 Lisp_Object saved_object;
18683 enum display_element_type saved_what = it->what;
18684 int saved_face_id = it->face_id;
18685
18686 saved_object = it->object;
18687 saved_pos = it->position;
18688
18689 it->what = IT_CHARACTER;
18690 memset (&it->position, 0, sizeof it->position);
18691 it->object = make_number (0);
18692 it->c = it->char_to_display = ' ';
18693 it->len = 1;
18694 /* The last row's blank glyphs should get the default face, to
18695 avoid painting the rest of the window with the region face,
18696 if the region ends at ZV. */
18697 if (it->glyph_row->ends_at_zv_p)
18698 it->face_id = default_face->id;
18699 else
18700 it->face_id = face->id;
18701
18702 PRODUCE_GLYPHS (it);
18703
18704 while (it->current_x <= it->last_visible_x)
18705 PRODUCE_GLYPHS (it);
18706
18707 /* Don't count these blanks really. It would let us insert a left
18708 truncation glyph below and make us set the cursor on them, maybe. */
18709 it->current_x = saved_x;
18710 it->object = saved_object;
18711 it->position = saved_pos;
18712 it->what = saved_what;
18713 it->face_id = saved_face_id;
18714 }
18715 }
18716
18717
18718 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18719 trailing whitespace. */
18720
18721 static int
18722 trailing_whitespace_p (ptrdiff_t charpos)
18723 {
18724 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18725 int c = 0;
18726
18727 while (bytepos < ZV_BYTE
18728 && (c = FETCH_CHAR (bytepos),
18729 c == ' ' || c == '\t'))
18730 ++bytepos;
18731
18732 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18733 {
18734 if (bytepos != PT_BYTE)
18735 return 1;
18736 }
18737 return 0;
18738 }
18739
18740
18741 /* Highlight trailing whitespace, if any, in ROW. */
18742
18743 static void
18744 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18745 {
18746 int used = row->used[TEXT_AREA];
18747
18748 if (used)
18749 {
18750 struct glyph *start = row->glyphs[TEXT_AREA];
18751 struct glyph *glyph = start + used - 1;
18752
18753 if (row->reversed_p)
18754 {
18755 /* Right-to-left rows need to be processed in the opposite
18756 direction, so swap the edge pointers. */
18757 glyph = start;
18758 start = row->glyphs[TEXT_AREA] + used - 1;
18759 }
18760
18761 /* Skip over glyphs inserted to display the cursor at the
18762 end of a line, for extending the face of the last glyph
18763 to the end of the line on terminals, and for truncation
18764 and continuation glyphs. */
18765 if (!row->reversed_p)
18766 {
18767 while (glyph >= start
18768 && glyph->type == CHAR_GLYPH
18769 && INTEGERP (glyph->object))
18770 --glyph;
18771 }
18772 else
18773 {
18774 while (glyph <= start
18775 && glyph->type == CHAR_GLYPH
18776 && INTEGERP (glyph->object))
18777 ++glyph;
18778 }
18779
18780 /* If last glyph is a space or stretch, and it's trailing
18781 whitespace, set the face of all trailing whitespace glyphs in
18782 IT->glyph_row to `trailing-whitespace'. */
18783 if ((row->reversed_p ? glyph <= start : glyph >= start)
18784 && BUFFERP (glyph->object)
18785 && (glyph->type == STRETCH_GLYPH
18786 || (glyph->type == CHAR_GLYPH
18787 && glyph->u.ch == ' '))
18788 && trailing_whitespace_p (glyph->charpos))
18789 {
18790 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18791 if (face_id < 0)
18792 return;
18793
18794 if (!row->reversed_p)
18795 {
18796 while (glyph >= start
18797 && BUFFERP (glyph->object)
18798 && (glyph->type == STRETCH_GLYPH
18799 || (glyph->type == CHAR_GLYPH
18800 && glyph->u.ch == ' ')))
18801 (glyph--)->face_id = face_id;
18802 }
18803 else
18804 {
18805 while (glyph <= start
18806 && BUFFERP (glyph->object)
18807 && (glyph->type == STRETCH_GLYPH
18808 || (glyph->type == CHAR_GLYPH
18809 && glyph->u.ch == ' ')))
18810 (glyph++)->face_id = face_id;
18811 }
18812 }
18813 }
18814 }
18815
18816
18817 /* Value is non-zero if glyph row ROW should be
18818 considered to hold the buffer position CHARPOS. */
18819
18820 static int
18821 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
18822 {
18823 int result = 1;
18824
18825 if (charpos == CHARPOS (row->end.pos)
18826 || charpos == MATRIX_ROW_END_CHARPOS (row))
18827 {
18828 /* Suppose the row ends on a string.
18829 Unless the row is continued, that means it ends on a newline
18830 in the string. If it's anything other than a display string
18831 (e.g., a before-string from an overlay), we don't want the
18832 cursor there. (This heuristic seems to give the optimal
18833 behavior for the various types of multi-line strings.)
18834 One exception: if the string has `cursor' property on one of
18835 its characters, we _do_ want the cursor there. */
18836 if (CHARPOS (row->end.string_pos) >= 0)
18837 {
18838 if (row->continued_p)
18839 result = 1;
18840 else
18841 {
18842 /* Check for `display' property. */
18843 struct glyph *beg = row->glyphs[TEXT_AREA];
18844 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18845 struct glyph *glyph;
18846
18847 result = 0;
18848 for (glyph = end; glyph >= beg; --glyph)
18849 if (STRINGP (glyph->object))
18850 {
18851 Lisp_Object prop
18852 = Fget_char_property (make_number (charpos),
18853 Qdisplay, Qnil);
18854 result =
18855 (!NILP (prop)
18856 && display_prop_string_p (prop, glyph->object));
18857 /* If there's a `cursor' property on one of the
18858 string's characters, this row is a cursor row,
18859 even though this is not a display string. */
18860 if (!result)
18861 {
18862 Lisp_Object s = glyph->object;
18863
18864 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18865 {
18866 ptrdiff_t gpos = glyph->charpos;
18867
18868 if (!NILP (Fget_char_property (make_number (gpos),
18869 Qcursor, s)))
18870 {
18871 result = 1;
18872 break;
18873 }
18874 }
18875 }
18876 break;
18877 }
18878 }
18879 }
18880 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18881 {
18882 /* If the row ends in middle of a real character,
18883 and the line is continued, we want the cursor here.
18884 That's because CHARPOS (ROW->end.pos) would equal
18885 PT if PT is before the character. */
18886 if (!row->ends_in_ellipsis_p)
18887 result = row->continued_p;
18888 else
18889 /* If the row ends in an ellipsis, then
18890 CHARPOS (ROW->end.pos) will equal point after the
18891 invisible text. We want that position to be displayed
18892 after the ellipsis. */
18893 result = 0;
18894 }
18895 /* If the row ends at ZV, display the cursor at the end of that
18896 row instead of at the start of the row below. */
18897 else if (row->ends_at_zv_p)
18898 result = 1;
18899 else
18900 result = 0;
18901 }
18902
18903 return result;
18904 }
18905
18906 /* Value is non-zero if glyph row ROW should be
18907 used to hold the cursor. */
18908
18909 static int
18910 cursor_row_p (struct glyph_row *row)
18911 {
18912 return row_for_charpos_p (row, PT);
18913 }
18914
18915 \f
18916
18917 /* Push the property PROP so that it will be rendered at the current
18918 position in IT. Return 1 if PROP was successfully pushed, 0
18919 otherwise. Called from handle_line_prefix to handle the
18920 `line-prefix' and `wrap-prefix' properties. */
18921
18922 static int
18923 push_prefix_prop (struct it *it, Lisp_Object prop)
18924 {
18925 struct text_pos pos =
18926 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18927
18928 eassert (it->method == GET_FROM_BUFFER
18929 || it->method == GET_FROM_DISPLAY_VECTOR
18930 || it->method == GET_FROM_STRING);
18931
18932 /* We need to save the current buffer/string position, so it will be
18933 restored by pop_it, because iterate_out_of_display_property
18934 depends on that being set correctly, but some situations leave
18935 it->position not yet set when this function is called. */
18936 push_it (it, &pos);
18937
18938 if (STRINGP (prop))
18939 {
18940 if (SCHARS (prop) == 0)
18941 {
18942 pop_it (it);
18943 return 0;
18944 }
18945
18946 it->string = prop;
18947 it->string_from_prefix_prop_p = 1;
18948 it->multibyte_p = STRING_MULTIBYTE (it->string);
18949 it->current.overlay_string_index = -1;
18950 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18951 it->end_charpos = it->string_nchars = SCHARS (it->string);
18952 it->method = GET_FROM_STRING;
18953 it->stop_charpos = 0;
18954 it->prev_stop = 0;
18955 it->base_level_stop = 0;
18956
18957 /* Force paragraph direction to be that of the parent
18958 buffer/string. */
18959 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18960 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18961 else
18962 it->paragraph_embedding = L2R;
18963
18964 /* Set up the bidi iterator for this display string. */
18965 if (it->bidi_p)
18966 {
18967 it->bidi_it.string.lstring = it->string;
18968 it->bidi_it.string.s = NULL;
18969 it->bidi_it.string.schars = it->end_charpos;
18970 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18971 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18972 it->bidi_it.string.unibyte = !it->multibyte_p;
18973 it->bidi_it.w = it->w;
18974 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18975 }
18976 }
18977 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18978 {
18979 it->method = GET_FROM_STRETCH;
18980 it->object = prop;
18981 }
18982 #ifdef HAVE_WINDOW_SYSTEM
18983 else if (IMAGEP (prop))
18984 {
18985 it->what = IT_IMAGE;
18986 it->image_id = lookup_image (it->f, prop);
18987 it->method = GET_FROM_IMAGE;
18988 }
18989 #endif /* HAVE_WINDOW_SYSTEM */
18990 else
18991 {
18992 pop_it (it); /* bogus display property, give up */
18993 return 0;
18994 }
18995
18996 return 1;
18997 }
18998
18999 /* Return the character-property PROP at the current position in IT. */
19000
19001 static Lisp_Object
19002 get_it_property (struct it *it, Lisp_Object prop)
19003 {
19004 Lisp_Object position, object = it->object;
19005
19006 if (STRINGP (object))
19007 position = make_number (IT_STRING_CHARPOS (*it));
19008 else if (BUFFERP (object))
19009 {
19010 position = make_number (IT_CHARPOS (*it));
19011 object = it->window;
19012 }
19013 else
19014 return Qnil;
19015
19016 return Fget_char_property (position, prop, object);
19017 }
19018
19019 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19020
19021 static void
19022 handle_line_prefix (struct it *it)
19023 {
19024 Lisp_Object prefix;
19025
19026 if (it->continuation_lines_width > 0)
19027 {
19028 prefix = get_it_property (it, Qwrap_prefix);
19029 if (NILP (prefix))
19030 prefix = Vwrap_prefix;
19031 }
19032 else
19033 {
19034 prefix = get_it_property (it, Qline_prefix);
19035 if (NILP (prefix))
19036 prefix = Vline_prefix;
19037 }
19038 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19039 {
19040 /* If the prefix is wider than the window, and we try to wrap
19041 it, it would acquire its own wrap prefix, and so on till the
19042 iterator stack overflows. So, don't wrap the prefix. */
19043 it->line_wrap = TRUNCATE;
19044 it->avoid_cursor_p = 1;
19045 }
19046 }
19047
19048 \f
19049
19050 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19051 only for R2L lines from display_line and display_string, when they
19052 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19053 the line/string needs to be continued on the next glyph row. */
19054 static void
19055 unproduce_glyphs (struct it *it, int n)
19056 {
19057 struct glyph *glyph, *end;
19058
19059 eassert (it->glyph_row);
19060 eassert (it->glyph_row->reversed_p);
19061 eassert (it->area == TEXT_AREA);
19062 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19063
19064 if (n > it->glyph_row->used[TEXT_AREA])
19065 n = it->glyph_row->used[TEXT_AREA];
19066 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19067 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19068 for ( ; glyph < end; glyph++)
19069 glyph[-n] = *glyph;
19070 }
19071
19072 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19073 and ROW->maxpos. */
19074 static void
19075 find_row_edges (struct it *it, struct glyph_row *row,
19076 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19077 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19078 {
19079 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19080 lines' rows is implemented for bidi-reordered rows. */
19081
19082 /* ROW->minpos is the value of min_pos, the minimal buffer position
19083 we have in ROW, or ROW->start.pos if that is smaller. */
19084 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19085 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19086 else
19087 /* We didn't find buffer positions smaller than ROW->start, or
19088 didn't find _any_ valid buffer positions in any of the glyphs,
19089 so we must trust the iterator's computed positions. */
19090 row->minpos = row->start.pos;
19091 if (max_pos <= 0)
19092 {
19093 max_pos = CHARPOS (it->current.pos);
19094 max_bpos = BYTEPOS (it->current.pos);
19095 }
19096
19097 /* Here are the various use-cases for ending the row, and the
19098 corresponding values for ROW->maxpos:
19099
19100 Line ends in a newline from buffer eol_pos + 1
19101 Line is continued from buffer max_pos + 1
19102 Line is truncated on right it->current.pos
19103 Line ends in a newline from string max_pos + 1(*)
19104 (*) + 1 only when line ends in a forward scan
19105 Line is continued from string max_pos
19106 Line is continued from display vector max_pos
19107 Line is entirely from a string min_pos == max_pos
19108 Line is entirely from a display vector min_pos == max_pos
19109 Line that ends at ZV ZV
19110
19111 If you discover other use-cases, please add them here as
19112 appropriate. */
19113 if (row->ends_at_zv_p)
19114 row->maxpos = it->current.pos;
19115 else if (row->used[TEXT_AREA])
19116 {
19117 int seen_this_string = 0;
19118 struct glyph_row *r1 = row - 1;
19119
19120 /* Did we see the same display string on the previous row? */
19121 if (STRINGP (it->object)
19122 /* this is not the first row */
19123 && row > it->w->desired_matrix->rows
19124 /* previous row is not the header line */
19125 && !r1->mode_line_p
19126 /* previous row also ends in a newline from a string */
19127 && r1->ends_in_newline_from_string_p)
19128 {
19129 struct glyph *start, *end;
19130
19131 /* Search for the last glyph of the previous row that came
19132 from buffer or string. Depending on whether the row is
19133 L2R or R2L, we need to process it front to back or the
19134 other way round. */
19135 if (!r1->reversed_p)
19136 {
19137 start = r1->glyphs[TEXT_AREA];
19138 end = start + r1->used[TEXT_AREA];
19139 /* Glyphs inserted by redisplay have an integer (zero)
19140 as their object. */
19141 while (end > start
19142 && INTEGERP ((end - 1)->object)
19143 && (end - 1)->charpos <= 0)
19144 --end;
19145 if (end > start)
19146 {
19147 if (EQ ((end - 1)->object, it->object))
19148 seen_this_string = 1;
19149 }
19150 else
19151 /* If all the glyphs of the previous row were inserted
19152 by redisplay, it means the previous row was
19153 produced from a single newline, which is only
19154 possible if that newline came from the same string
19155 as the one which produced this ROW. */
19156 seen_this_string = 1;
19157 }
19158 else
19159 {
19160 end = r1->glyphs[TEXT_AREA] - 1;
19161 start = end + r1->used[TEXT_AREA];
19162 while (end < start
19163 && INTEGERP ((end + 1)->object)
19164 && (end + 1)->charpos <= 0)
19165 ++end;
19166 if (end < start)
19167 {
19168 if (EQ ((end + 1)->object, it->object))
19169 seen_this_string = 1;
19170 }
19171 else
19172 seen_this_string = 1;
19173 }
19174 }
19175 /* Take note of each display string that covers a newline only
19176 once, the first time we see it. This is for when a display
19177 string includes more than one newline in it. */
19178 if (row->ends_in_newline_from_string_p && !seen_this_string)
19179 {
19180 /* If we were scanning the buffer forward when we displayed
19181 the string, we want to account for at least one buffer
19182 position that belongs to this row (position covered by
19183 the display string), so that cursor positioning will
19184 consider this row as a candidate when point is at the end
19185 of the visual line represented by this row. This is not
19186 required when scanning back, because max_pos will already
19187 have a much larger value. */
19188 if (CHARPOS (row->end.pos) > max_pos)
19189 INC_BOTH (max_pos, max_bpos);
19190 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19191 }
19192 else if (CHARPOS (it->eol_pos) > 0)
19193 SET_TEXT_POS (row->maxpos,
19194 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19195 else if (row->continued_p)
19196 {
19197 /* If max_pos is different from IT's current position, it
19198 means IT->method does not belong to the display element
19199 at max_pos. However, it also means that the display
19200 element at max_pos was displayed in its entirety on this
19201 line, which is equivalent to saying that the next line
19202 starts at the next buffer position. */
19203 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19204 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19205 else
19206 {
19207 INC_BOTH (max_pos, max_bpos);
19208 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19209 }
19210 }
19211 else if (row->truncated_on_right_p)
19212 /* display_line already called reseat_at_next_visible_line_start,
19213 which puts the iterator at the beginning of the next line, in
19214 the logical order. */
19215 row->maxpos = it->current.pos;
19216 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19217 /* A line that is entirely from a string/image/stretch... */
19218 row->maxpos = row->minpos;
19219 else
19220 emacs_abort ();
19221 }
19222 else
19223 row->maxpos = it->current.pos;
19224 }
19225
19226 /* Construct the glyph row IT->glyph_row in the desired matrix of
19227 IT->w from text at the current position of IT. See dispextern.h
19228 for an overview of struct it. Value is non-zero if
19229 IT->glyph_row displays text, as opposed to a line displaying ZV
19230 only. */
19231
19232 static int
19233 display_line (struct it *it)
19234 {
19235 struct glyph_row *row = it->glyph_row;
19236 Lisp_Object overlay_arrow_string;
19237 struct it wrap_it;
19238 void *wrap_data = NULL;
19239 int may_wrap = 0, wrap_x IF_LINT (= 0);
19240 int wrap_row_used = -1;
19241 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19242 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19243 int wrap_row_extra_line_spacing IF_LINT (= 0);
19244 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19245 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19246 int cvpos;
19247 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19248 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19249
19250 /* We always start displaying at hpos zero even if hscrolled. */
19251 eassert (it->hpos == 0 && it->current_x == 0);
19252
19253 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19254 >= it->w->desired_matrix->nrows)
19255 {
19256 it->w->nrows_scale_factor++;
19257 fonts_changed_p = 1;
19258 return 0;
19259 }
19260
19261 /* Is IT->w showing the region? */
19262 it->w->region_showing = it->region_beg_charpos > 0 ? it->region_beg_charpos : 0;
19263
19264 /* Clear the result glyph row and enable it. */
19265 prepare_desired_row (row);
19266
19267 row->y = it->current_y;
19268 row->start = it->start;
19269 row->continuation_lines_width = it->continuation_lines_width;
19270 row->displays_text_p = 1;
19271 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19272 it->starts_in_middle_of_char_p = 0;
19273
19274 /* Arrange the overlays nicely for our purposes. Usually, we call
19275 display_line on only one line at a time, in which case this
19276 can't really hurt too much, or we call it on lines which appear
19277 one after another in the buffer, in which case all calls to
19278 recenter_overlay_lists but the first will be pretty cheap. */
19279 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19280
19281 /* Move over display elements that are not visible because we are
19282 hscrolled. This may stop at an x-position < IT->first_visible_x
19283 if the first glyph is partially visible or if we hit a line end. */
19284 if (it->current_x < it->first_visible_x)
19285 {
19286 enum move_it_result move_result;
19287
19288 this_line_min_pos = row->start.pos;
19289 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19290 MOVE_TO_POS | MOVE_TO_X);
19291 /* If we are under a large hscroll, move_it_in_display_line_to
19292 could hit the end of the line without reaching
19293 it->first_visible_x. Pretend that we did reach it. This is
19294 especially important on a TTY, where we will call
19295 extend_face_to_end_of_line, which needs to know how many
19296 blank glyphs to produce. */
19297 if (it->current_x < it->first_visible_x
19298 && (move_result == MOVE_NEWLINE_OR_CR
19299 || move_result == MOVE_POS_MATCH_OR_ZV))
19300 it->current_x = it->first_visible_x;
19301
19302 /* Record the smallest positions seen while we moved over
19303 display elements that are not visible. This is needed by
19304 redisplay_internal for optimizing the case where the cursor
19305 stays inside the same line. The rest of this function only
19306 considers positions that are actually displayed, so
19307 RECORD_MAX_MIN_POS will not otherwise record positions that
19308 are hscrolled to the left of the left edge of the window. */
19309 min_pos = CHARPOS (this_line_min_pos);
19310 min_bpos = BYTEPOS (this_line_min_pos);
19311 }
19312 else
19313 {
19314 /* We only do this when not calling `move_it_in_display_line_to'
19315 above, because move_it_in_display_line_to calls
19316 handle_line_prefix itself. */
19317 handle_line_prefix (it);
19318 }
19319
19320 /* Get the initial row height. This is either the height of the
19321 text hscrolled, if there is any, or zero. */
19322 row->ascent = it->max_ascent;
19323 row->height = it->max_ascent + it->max_descent;
19324 row->phys_ascent = it->max_phys_ascent;
19325 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19326 row->extra_line_spacing = it->max_extra_line_spacing;
19327
19328 /* Utility macro to record max and min buffer positions seen until now. */
19329 #define RECORD_MAX_MIN_POS(IT) \
19330 do \
19331 { \
19332 int composition_p = !STRINGP ((IT)->string) \
19333 && ((IT)->what == IT_COMPOSITION); \
19334 ptrdiff_t current_pos = \
19335 composition_p ? (IT)->cmp_it.charpos \
19336 : IT_CHARPOS (*(IT)); \
19337 ptrdiff_t current_bpos = \
19338 composition_p ? CHAR_TO_BYTE (current_pos) \
19339 : IT_BYTEPOS (*(IT)); \
19340 if (current_pos < min_pos) \
19341 { \
19342 min_pos = current_pos; \
19343 min_bpos = current_bpos; \
19344 } \
19345 if (IT_CHARPOS (*it) > max_pos) \
19346 { \
19347 max_pos = IT_CHARPOS (*it); \
19348 max_bpos = IT_BYTEPOS (*it); \
19349 } \
19350 } \
19351 while (0)
19352
19353 /* Loop generating characters. The loop is left with IT on the next
19354 character to display. */
19355 while (1)
19356 {
19357 int n_glyphs_before, hpos_before, x_before;
19358 int x, nglyphs;
19359 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19360
19361 /* Retrieve the next thing to display. Value is zero if end of
19362 buffer reached. */
19363 if (!get_next_display_element (it))
19364 {
19365 /* Maybe add a space at the end of this line that is used to
19366 display the cursor there under X. Set the charpos of the
19367 first glyph of blank lines not corresponding to any text
19368 to -1. */
19369 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19370 row->exact_window_width_line_p = 1;
19371 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19372 || row->used[TEXT_AREA] == 0)
19373 {
19374 row->glyphs[TEXT_AREA]->charpos = -1;
19375 row->displays_text_p = 0;
19376
19377 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
19378 && (!MINI_WINDOW_P (it->w)
19379 || (minibuf_level && EQ (it->window, minibuf_window))))
19380 row->indicate_empty_line_p = 1;
19381 }
19382
19383 it->continuation_lines_width = 0;
19384 row->ends_at_zv_p = 1;
19385 /* A row that displays right-to-left text must always have
19386 its last face extended all the way to the end of line,
19387 even if this row ends in ZV, because we still write to
19388 the screen left to right. We also need to extend the
19389 last face if the default face is remapped to some
19390 different face, otherwise the functions that clear
19391 portions of the screen will clear with the default face's
19392 background color. */
19393 if (row->reversed_p
19394 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19395 extend_face_to_end_of_line (it);
19396 break;
19397 }
19398
19399 /* Now, get the metrics of what we want to display. This also
19400 generates glyphs in `row' (which is IT->glyph_row). */
19401 n_glyphs_before = row->used[TEXT_AREA];
19402 x = it->current_x;
19403
19404 /* Remember the line height so far in case the next element doesn't
19405 fit on the line. */
19406 if (it->line_wrap != TRUNCATE)
19407 {
19408 ascent = it->max_ascent;
19409 descent = it->max_descent;
19410 phys_ascent = it->max_phys_ascent;
19411 phys_descent = it->max_phys_descent;
19412
19413 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19414 {
19415 if (IT_DISPLAYING_WHITESPACE (it))
19416 may_wrap = 1;
19417 else if (may_wrap)
19418 {
19419 SAVE_IT (wrap_it, *it, wrap_data);
19420 wrap_x = x;
19421 wrap_row_used = row->used[TEXT_AREA];
19422 wrap_row_ascent = row->ascent;
19423 wrap_row_height = row->height;
19424 wrap_row_phys_ascent = row->phys_ascent;
19425 wrap_row_phys_height = row->phys_height;
19426 wrap_row_extra_line_spacing = row->extra_line_spacing;
19427 wrap_row_min_pos = min_pos;
19428 wrap_row_min_bpos = min_bpos;
19429 wrap_row_max_pos = max_pos;
19430 wrap_row_max_bpos = max_bpos;
19431 may_wrap = 0;
19432 }
19433 }
19434 }
19435
19436 PRODUCE_GLYPHS (it);
19437
19438 /* If this display element was in marginal areas, continue with
19439 the next one. */
19440 if (it->area != TEXT_AREA)
19441 {
19442 row->ascent = max (row->ascent, it->max_ascent);
19443 row->height = max (row->height, it->max_ascent + it->max_descent);
19444 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19445 row->phys_height = max (row->phys_height,
19446 it->max_phys_ascent + it->max_phys_descent);
19447 row->extra_line_spacing = max (row->extra_line_spacing,
19448 it->max_extra_line_spacing);
19449 set_iterator_to_next (it, 1);
19450 continue;
19451 }
19452
19453 /* Does the display element fit on the line? If we truncate
19454 lines, we should draw past the right edge of the window. If
19455 we don't truncate, we want to stop so that we can display the
19456 continuation glyph before the right margin. If lines are
19457 continued, there are two possible strategies for characters
19458 resulting in more than 1 glyph (e.g. tabs): Display as many
19459 glyphs as possible in this line and leave the rest for the
19460 continuation line, or display the whole element in the next
19461 line. Original redisplay did the former, so we do it also. */
19462 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19463 hpos_before = it->hpos;
19464 x_before = x;
19465
19466 if (/* Not a newline. */
19467 nglyphs > 0
19468 /* Glyphs produced fit entirely in the line. */
19469 && it->current_x < it->last_visible_x)
19470 {
19471 it->hpos += nglyphs;
19472 row->ascent = max (row->ascent, it->max_ascent);
19473 row->height = max (row->height, it->max_ascent + it->max_descent);
19474 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19475 row->phys_height = max (row->phys_height,
19476 it->max_phys_ascent + it->max_phys_descent);
19477 row->extra_line_spacing = max (row->extra_line_spacing,
19478 it->max_extra_line_spacing);
19479 if (it->current_x - it->pixel_width < it->first_visible_x)
19480 row->x = x - it->first_visible_x;
19481 /* Record the maximum and minimum buffer positions seen so
19482 far in glyphs that will be displayed by this row. */
19483 if (it->bidi_p)
19484 RECORD_MAX_MIN_POS (it);
19485 }
19486 else
19487 {
19488 int i, new_x;
19489 struct glyph *glyph;
19490
19491 for (i = 0; i < nglyphs; ++i, x = new_x)
19492 {
19493 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19494 new_x = x + glyph->pixel_width;
19495
19496 if (/* Lines are continued. */
19497 it->line_wrap != TRUNCATE
19498 && (/* Glyph doesn't fit on the line. */
19499 new_x > it->last_visible_x
19500 /* Or it fits exactly on a window system frame. */
19501 || (new_x == it->last_visible_x
19502 && FRAME_WINDOW_P (it->f)
19503 && (row->reversed_p
19504 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19505 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19506 {
19507 /* End of a continued line. */
19508
19509 if (it->hpos == 0
19510 || (new_x == it->last_visible_x
19511 && FRAME_WINDOW_P (it->f)
19512 && (row->reversed_p
19513 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19514 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19515 {
19516 /* Current glyph is the only one on the line or
19517 fits exactly on the line. We must continue
19518 the line because we can't draw the cursor
19519 after the glyph. */
19520 row->continued_p = 1;
19521 it->current_x = new_x;
19522 it->continuation_lines_width += new_x;
19523 ++it->hpos;
19524 if (i == nglyphs - 1)
19525 {
19526 /* If line-wrap is on, check if a previous
19527 wrap point was found. */
19528 if (wrap_row_used > 0
19529 /* Even if there is a previous wrap
19530 point, continue the line here as
19531 usual, if (i) the previous character
19532 was a space or tab AND (ii) the
19533 current character is not. */
19534 && (!may_wrap
19535 || IT_DISPLAYING_WHITESPACE (it)))
19536 goto back_to_wrap;
19537
19538 /* Record the maximum and minimum buffer
19539 positions seen so far in glyphs that will be
19540 displayed by this row. */
19541 if (it->bidi_p)
19542 RECORD_MAX_MIN_POS (it);
19543 set_iterator_to_next (it, 1);
19544 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19545 {
19546 if (!get_next_display_element (it))
19547 {
19548 row->exact_window_width_line_p = 1;
19549 it->continuation_lines_width = 0;
19550 row->continued_p = 0;
19551 row->ends_at_zv_p = 1;
19552 }
19553 else if (ITERATOR_AT_END_OF_LINE_P (it))
19554 {
19555 row->continued_p = 0;
19556 row->exact_window_width_line_p = 1;
19557 }
19558 }
19559 }
19560 else if (it->bidi_p)
19561 RECORD_MAX_MIN_POS (it);
19562 }
19563 else if (CHAR_GLYPH_PADDING_P (*glyph)
19564 && !FRAME_WINDOW_P (it->f))
19565 {
19566 /* A padding glyph that doesn't fit on this line.
19567 This means the whole character doesn't fit
19568 on the line. */
19569 if (row->reversed_p)
19570 unproduce_glyphs (it, row->used[TEXT_AREA]
19571 - n_glyphs_before);
19572 row->used[TEXT_AREA] = n_glyphs_before;
19573
19574 /* Fill the rest of the row with continuation
19575 glyphs like in 20.x. */
19576 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19577 < row->glyphs[1 + TEXT_AREA])
19578 produce_special_glyphs (it, IT_CONTINUATION);
19579
19580 row->continued_p = 1;
19581 it->current_x = x_before;
19582 it->continuation_lines_width += x_before;
19583
19584 /* Restore the height to what it was before the
19585 element not fitting on the line. */
19586 it->max_ascent = ascent;
19587 it->max_descent = descent;
19588 it->max_phys_ascent = phys_ascent;
19589 it->max_phys_descent = phys_descent;
19590 }
19591 else if (wrap_row_used > 0)
19592 {
19593 back_to_wrap:
19594 if (row->reversed_p)
19595 unproduce_glyphs (it,
19596 row->used[TEXT_AREA] - wrap_row_used);
19597 RESTORE_IT (it, &wrap_it, wrap_data);
19598 it->continuation_lines_width += wrap_x;
19599 row->used[TEXT_AREA] = wrap_row_used;
19600 row->ascent = wrap_row_ascent;
19601 row->height = wrap_row_height;
19602 row->phys_ascent = wrap_row_phys_ascent;
19603 row->phys_height = wrap_row_phys_height;
19604 row->extra_line_spacing = wrap_row_extra_line_spacing;
19605 min_pos = wrap_row_min_pos;
19606 min_bpos = wrap_row_min_bpos;
19607 max_pos = wrap_row_max_pos;
19608 max_bpos = wrap_row_max_bpos;
19609 row->continued_p = 1;
19610 row->ends_at_zv_p = 0;
19611 row->exact_window_width_line_p = 0;
19612 it->continuation_lines_width += x;
19613
19614 /* Make sure that a non-default face is extended
19615 up to the right margin of the window. */
19616 extend_face_to_end_of_line (it);
19617 }
19618 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19619 {
19620 /* A TAB that extends past the right edge of the
19621 window. This produces a single glyph on
19622 window system frames. We leave the glyph in
19623 this row and let it fill the row, but don't
19624 consume the TAB. */
19625 if ((row->reversed_p
19626 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19627 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19628 produce_special_glyphs (it, IT_CONTINUATION);
19629 it->continuation_lines_width += it->last_visible_x;
19630 row->ends_in_middle_of_char_p = 1;
19631 row->continued_p = 1;
19632 glyph->pixel_width = it->last_visible_x - x;
19633 it->starts_in_middle_of_char_p = 1;
19634 }
19635 else
19636 {
19637 /* Something other than a TAB that draws past
19638 the right edge of the window. Restore
19639 positions to values before the element. */
19640 if (row->reversed_p)
19641 unproduce_glyphs (it, row->used[TEXT_AREA]
19642 - (n_glyphs_before + i));
19643 row->used[TEXT_AREA] = n_glyphs_before + i;
19644
19645 /* Display continuation glyphs. */
19646 it->current_x = x_before;
19647 it->continuation_lines_width += x;
19648 if (!FRAME_WINDOW_P (it->f)
19649 || (row->reversed_p
19650 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19651 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19652 produce_special_glyphs (it, IT_CONTINUATION);
19653 row->continued_p = 1;
19654
19655 extend_face_to_end_of_line (it);
19656
19657 if (nglyphs > 1 && i > 0)
19658 {
19659 row->ends_in_middle_of_char_p = 1;
19660 it->starts_in_middle_of_char_p = 1;
19661 }
19662
19663 /* Restore the height to what it was before the
19664 element not fitting on the line. */
19665 it->max_ascent = ascent;
19666 it->max_descent = descent;
19667 it->max_phys_ascent = phys_ascent;
19668 it->max_phys_descent = phys_descent;
19669 }
19670
19671 break;
19672 }
19673 else if (new_x > it->first_visible_x)
19674 {
19675 /* Increment number of glyphs actually displayed. */
19676 ++it->hpos;
19677
19678 /* Record the maximum and minimum buffer positions
19679 seen so far in glyphs that will be displayed by
19680 this row. */
19681 if (it->bidi_p)
19682 RECORD_MAX_MIN_POS (it);
19683
19684 if (x < it->first_visible_x)
19685 /* Glyph is partially visible, i.e. row starts at
19686 negative X position. */
19687 row->x = x - it->first_visible_x;
19688 }
19689 else
19690 {
19691 /* Glyph is completely off the left margin of the
19692 window. This should not happen because of the
19693 move_it_in_display_line at the start of this
19694 function, unless the text display area of the
19695 window is empty. */
19696 eassert (it->first_visible_x <= it->last_visible_x);
19697 }
19698 }
19699 /* Even if this display element produced no glyphs at all,
19700 we want to record its position. */
19701 if (it->bidi_p && nglyphs == 0)
19702 RECORD_MAX_MIN_POS (it);
19703
19704 row->ascent = max (row->ascent, it->max_ascent);
19705 row->height = max (row->height, it->max_ascent + it->max_descent);
19706 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19707 row->phys_height = max (row->phys_height,
19708 it->max_phys_ascent + it->max_phys_descent);
19709 row->extra_line_spacing = max (row->extra_line_spacing,
19710 it->max_extra_line_spacing);
19711
19712 /* End of this display line if row is continued. */
19713 if (row->continued_p || row->ends_at_zv_p)
19714 break;
19715 }
19716
19717 at_end_of_line:
19718 /* Is this a line end? If yes, we're also done, after making
19719 sure that a non-default face is extended up to the right
19720 margin of the window. */
19721 if (ITERATOR_AT_END_OF_LINE_P (it))
19722 {
19723 int used_before = row->used[TEXT_AREA];
19724
19725 row->ends_in_newline_from_string_p = STRINGP (it->object);
19726
19727 /* Add a space at the end of the line that is used to
19728 display the cursor there. */
19729 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19730 append_space_for_newline (it, 0);
19731
19732 /* Extend the face to the end of the line. */
19733 extend_face_to_end_of_line (it);
19734
19735 /* Make sure we have the position. */
19736 if (used_before == 0)
19737 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19738
19739 /* Record the position of the newline, for use in
19740 find_row_edges. */
19741 it->eol_pos = it->current.pos;
19742
19743 /* Consume the line end. This skips over invisible lines. */
19744 set_iterator_to_next (it, 1);
19745 it->continuation_lines_width = 0;
19746 break;
19747 }
19748
19749 /* Proceed with next display element. Note that this skips
19750 over lines invisible because of selective display. */
19751 set_iterator_to_next (it, 1);
19752
19753 /* If we truncate lines, we are done when the last displayed
19754 glyphs reach past the right margin of the window. */
19755 if (it->line_wrap == TRUNCATE
19756 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19757 ? (it->current_x >= it->last_visible_x)
19758 : (it->current_x > it->last_visible_x)))
19759 {
19760 /* Maybe add truncation glyphs. */
19761 if (!FRAME_WINDOW_P (it->f)
19762 || (row->reversed_p
19763 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19764 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19765 {
19766 int i, n;
19767
19768 if (!row->reversed_p)
19769 {
19770 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19771 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19772 break;
19773 }
19774 else
19775 {
19776 for (i = 0; i < row->used[TEXT_AREA]; i++)
19777 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19778 break;
19779 /* Remove any padding glyphs at the front of ROW, to
19780 make room for the truncation glyphs we will be
19781 adding below. The loop below always inserts at
19782 least one truncation glyph, so also remove the
19783 last glyph added to ROW. */
19784 unproduce_glyphs (it, i + 1);
19785 /* Adjust i for the loop below. */
19786 i = row->used[TEXT_AREA] - (i + 1);
19787 }
19788
19789 it->current_x = x_before;
19790 if (!FRAME_WINDOW_P (it->f))
19791 {
19792 for (n = row->used[TEXT_AREA]; i < n; ++i)
19793 {
19794 row->used[TEXT_AREA] = i;
19795 produce_special_glyphs (it, IT_TRUNCATION);
19796 }
19797 }
19798 else
19799 {
19800 row->used[TEXT_AREA] = i;
19801 produce_special_glyphs (it, IT_TRUNCATION);
19802 }
19803 }
19804 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19805 {
19806 /* Don't truncate if we can overflow newline into fringe. */
19807 if (!get_next_display_element (it))
19808 {
19809 it->continuation_lines_width = 0;
19810 row->ends_at_zv_p = 1;
19811 row->exact_window_width_line_p = 1;
19812 break;
19813 }
19814 if (ITERATOR_AT_END_OF_LINE_P (it))
19815 {
19816 row->exact_window_width_line_p = 1;
19817 goto at_end_of_line;
19818 }
19819 it->current_x = x_before;
19820 }
19821
19822 row->truncated_on_right_p = 1;
19823 it->continuation_lines_width = 0;
19824 reseat_at_next_visible_line_start (it, 0);
19825 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19826 it->hpos = hpos_before;
19827 break;
19828 }
19829 }
19830
19831 if (wrap_data)
19832 bidi_unshelve_cache (wrap_data, 1);
19833
19834 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19835 at the left window margin. */
19836 if (it->first_visible_x
19837 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19838 {
19839 if (!FRAME_WINDOW_P (it->f)
19840 || (row->reversed_p
19841 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19842 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19843 insert_left_trunc_glyphs (it);
19844 row->truncated_on_left_p = 1;
19845 }
19846
19847 /* Remember the position at which this line ends.
19848
19849 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19850 cannot be before the call to find_row_edges below, since that is
19851 where these positions are determined. */
19852 row->end = it->current;
19853 if (!it->bidi_p)
19854 {
19855 row->minpos = row->start.pos;
19856 row->maxpos = row->end.pos;
19857 }
19858 else
19859 {
19860 /* ROW->minpos and ROW->maxpos must be the smallest and
19861 `1 + the largest' buffer positions in ROW. But if ROW was
19862 bidi-reordered, these two positions can be anywhere in the
19863 row, so we must determine them now. */
19864 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19865 }
19866
19867 /* If the start of this line is the overlay arrow-position, then
19868 mark this glyph row as the one containing the overlay arrow.
19869 This is clearly a mess with variable size fonts. It would be
19870 better to let it be displayed like cursors under X. */
19871 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
19872 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19873 !NILP (overlay_arrow_string)))
19874 {
19875 /* Overlay arrow in window redisplay is a fringe bitmap. */
19876 if (STRINGP (overlay_arrow_string))
19877 {
19878 struct glyph_row *arrow_row
19879 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19880 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19881 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19882 struct glyph *p = row->glyphs[TEXT_AREA];
19883 struct glyph *p2, *end;
19884
19885 /* Copy the arrow glyphs. */
19886 while (glyph < arrow_end)
19887 *p++ = *glyph++;
19888
19889 /* Throw away padding glyphs. */
19890 p2 = p;
19891 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19892 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19893 ++p2;
19894 if (p2 > p)
19895 {
19896 while (p2 < end)
19897 *p++ = *p2++;
19898 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19899 }
19900 }
19901 else
19902 {
19903 eassert (INTEGERP (overlay_arrow_string));
19904 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19905 }
19906 overlay_arrow_seen = 1;
19907 }
19908
19909 /* Highlight trailing whitespace. */
19910 if (!NILP (Vshow_trailing_whitespace))
19911 highlight_trailing_whitespace (it->f, it->glyph_row);
19912
19913 /* Compute pixel dimensions of this line. */
19914 compute_line_metrics (it);
19915
19916 /* Implementation note: No changes in the glyphs of ROW or in their
19917 faces can be done past this point, because compute_line_metrics
19918 computes ROW's hash value and stores it within the glyph_row
19919 structure. */
19920
19921 /* Record whether this row ends inside an ellipsis. */
19922 row->ends_in_ellipsis_p
19923 = (it->method == GET_FROM_DISPLAY_VECTOR
19924 && it->ellipsis_p);
19925
19926 /* Save fringe bitmaps in this row. */
19927 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19928 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19929 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19930 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19931
19932 it->left_user_fringe_bitmap = 0;
19933 it->left_user_fringe_face_id = 0;
19934 it->right_user_fringe_bitmap = 0;
19935 it->right_user_fringe_face_id = 0;
19936
19937 /* Maybe set the cursor. */
19938 cvpos = it->w->cursor.vpos;
19939 if ((cvpos < 0
19940 /* In bidi-reordered rows, keep checking for proper cursor
19941 position even if one has been found already, because buffer
19942 positions in such rows change non-linearly with ROW->VPOS,
19943 when a line is continued. One exception: when we are at ZV,
19944 display cursor on the first suitable glyph row, since all
19945 the empty rows after that also have their position set to ZV. */
19946 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19947 lines' rows is implemented for bidi-reordered rows. */
19948 || (it->bidi_p
19949 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19950 && PT >= MATRIX_ROW_START_CHARPOS (row)
19951 && PT <= MATRIX_ROW_END_CHARPOS (row)
19952 && cursor_row_p (row))
19953 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19954
19955 /* Prepare for the next line. This line starts horizontally at (X
19956 HPOS) = (0 0). Vertical positions are incremented. As a
19957 convenience for the caller, IT->glyph_row is set to the next
19958 row to be used. */
19959 it->current_x = it->hpos = 0;
19960 it->current_y += row->height;
19961 SET_TEXT_POS (it->eol_pos, 0, 0);
19962 ++it->vpos;
19963 ++it->glyph_row;
19964 /* The next row should by default use the same value of the
19965 reversed_p flag as this one. set_iterator_to_next decides when
19966 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19967 the flag accordingly. */
19968 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19969 it->glyph_row->reversed_p = row->reversed_p;
19970 it->start = row->end;
19971 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
19972
19973 #undef RECORD_MAX_MIN_POS
19974 }
19975
19976 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19977 Scurrent_bidi_paragraph_direction, 0, 1, 0,
19978 doc: /* Return paragraph direction at point in BUFFER.
19979 Value is either `left-to-right' or `right-to-left'.
19980 If BUFFER is omitted or nil, it defaults to the current buffer.
19981
19982 Paragraph direction determines how the text in the paragraph is displayed.
19983 In left-to-right paragraphs, text begins at the left margin of the window
19984 and the reading direction is generally left to right. In right-to-left
19985 paragraphs, text begins at the right margin and is read from right to left.
19986
19987 See also `bidi-paragraph-direction'. */)
19988 (Lisp_Object buffer)
19989 {
19990 struct buffer *buf = current_buffer;
19991 struct buffer *old = buf;
19992
19993 if (! NILP (buffer))
19994 {
19995 CHECK_BUFFER (buffer);
19996 buf = XBUFFER (buffer);
19997 }
19998
19999 if (NILP (BVAR (buf, bidi_display_reordering))
20000 || NILP (BVAR (buf, enable_multibyte_characters))
20001 /* When we are loading loadup.el, the character property tables
20002 needed for bidi iteration are not yet available. */
20003 || !NILP (Vpurify_flag))
20004 return Qleft_to_right;
20005 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20006 return BVAR (buf, bidi_paragraph_direction);
20007 else
20008 {
20009 /* Determine the direction from buffer text. We could try to
20010 use current_matrix if it is up to date, but this seems fast
20011 enough as it is. */
20012 struct bidi_it itb;
20013 ptrdiff_t pos = BUF_PT (buf);
20014 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20015 int c;
20016 void *itb_data = bidi_shelve_cache ();
20017
20018 set_buffer_temp (buf);
20019 /* bidi_paragraph_init finds the base direction of the paragraph
20020 by searching forward from paragraph start. We need the base
20021 direction of the current or _previous_ paragraph, so we need
20022 to make sure we are within that paragraph. To that end, find
20023 the previous non-empty line. */
20024 if (pos >= ZV && pos > BEGV)
20025 DEC_BOTH (pos, bytepos);
20026 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20027 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20028 {
20029 while ((c = FETCH_BYTE (bytepos)) == '\n'
20030 || c == ' ' || c == '\t' || c == '\f')
20031 {
20032 if (bytepos <= BEGV_BYTE)
20033 break;
20034 bytepos--;
20035 pos--;
20036 }
20037 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20038 bytepos--;
20039 }
20040 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20041 itb.paragraph_dir = NEUTRAL_DIR;
20042 itb.string.s = NULL;
20043 itb.string.lstring = Qnil;
20044 itb.string.bufpos = 0;
20045 itb.string.unibyte = 0;
20046 /* We have no window to use here for ignoring window-specific
20047 overlays. Using NULL for window pointer will cause
20048 compute_display_string_pos to use the current buffer. */
20049 itb.w = NULL;
20050 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20051 bidi_unshelve_cache (itb_data, 0);
20052 set_buffer_temp (old);
20053 switch (itb.paragraph_dir)
20054 {
20055 case L2R:
20056 return Qleft_to_right;
20057 break;
20058 case R2L:
20059 return Qright_to_left;
20060 break;
20061 default:
20062 emacs_abort ();
20063 }
20064 }
20065 }
20066
20067 DEFUN ("move-point-visually", Fmove_point_visually,
20068 Smove_point_visually, 1, 1, 0,
20069 doc: /* Move point in the visual order in the specified DIRECTION.
20070 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
20071 left.
20072
20073 Value is the new character position of point. */)
20074 (Lisp_Object direction)
20075 {
20076 struct window *w = XWINDOW (selected_window);
20077 struct buffer *b = XBUFFER (w->contents);
20078 struct glyph_row *row;
20079 int dir;
20080 Lisp_Object paragraph_dir;
20081
20082 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
20083 (!(ROW)->continued_p \
20084 && INTEGERP ((GLYPH)->object) \
20085 && (GLYPH)->type == CHAR_GLYPH \
20086 && (GLYPH)->u.ch == ' ' \
20087 && (GLYPH)->charpos >= 0 \
20088 && !(GLYPH)->avoid_cursor_p)
20089
20090 CHECK_NUMBER (direction);
20091 dir = XINT (direction);
20092 if (dir > 0)
20093 dir = 1;
20094 else
20095 dir = -1;
20096
20097 /* If current matrix is up-to-date, we can use the information
20098 recorded in the glyphs, at least as long as the goal is on the
20099 screen. */
20100 if (w->window_end_valid
20101 && !windows_or_buffers_changed
20102 && b
20103 && !b->clip_changed
20104 && !b->prevent_redisplay_optimizations_p
20105 && !window_outdated (w)
20106 && w->cursor.vpos >= 0
20107 && w->cursor.vpos < w->current_matrix->nrows
20108 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
20109 {
20110 struct glyph *g = row->glyphs[TEXT_AREA];
20111 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
20112 struct glyph *gpt = g + w->cursor.hpos;
20113
20114 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
20115 {
20116 if (BUFFERP (g->object) && g->charpos != PT)
20117 {
20118 SET_PT (g->charpos);
20119 w->cursor.vpos = -1;
20120 return make_number (PT);
20121 }
20122 else if (!INTEGERP (g->object) && !EQ (g->object, gpt->object))
20123 {
20124 ptrdiff_t new_pos;
20125
20126 if (BUFFERP (gpt->object))
20127 {
20128 new_pos = PT;
20129 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
20130 new_pos += (row->reversed_p ? -dir : dir);
20131 else
20132 new_pos -= (row->reversed_p ? -dir : dir);;
20133 }
20134 else if (BUFFERP (g->object))
20135 new_pos = g->charpos;
20136 else
20137 break;
20138 SET_PT (new_pos);
20139 w->cursor.vpos = -1;
20140 return make_number (PT);
20141 }
20142 else if (ROW_GLYPH_NEWLINE_P (row, g))
20143 {
20144 /* Glyphs inserted at the end of a non-empty line for
20145 positioning the cursor have zero charpos, so we must
20146 deduce the value of point by other means. */
20147 if (g->charpos > 0)
20148 SET_PT (g->charpos);
20149 else if (row->ends_at_zv_p && PT != ZV)
20150 SET_PT (ZV);
20151 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
20152 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20153 else
20154 break;
20155 w->cursor.vpos = -1;
20156 return make_number (PT);
20157 }
20158 }
20159 if (g == e || INTEGERP (g->object))
20160 {
20161 if (row->truncated_on_left_p || row->truncated_on_right_p)
20162 goto simulate_display;
20163 if (!row->reversed_p)
20164 row += dir;
20165 else
20166 row -= dir;
20167 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
20168 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
20169 goto simulate_display;
20170
20171 if (dir > 0)
20172 {
20173 if (row->reversed_p && !row->continued_p)
20174 {
20175 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20176 w->cursor.vpos = -1;
20177 return make_number (PT);
20178 }
20179 g = row->glyphs[TEXT_AREA];
20180 e = g + row->used[TEXT_AREA];
20181 for ( ; g < e; g++)
20182 {
20183 if (BUFFERP (g->object)
20184 /* Empty lines have only one glyph, which stands
20185 for the newline, and whose charpos is the
20186 buffer position of the newline. */
20187 || ROW_GLYPH_NEWLINE_P (row, g)
20188 /* When the buffer ends in a newline, the line at
20189 EOB also has one glyph, but its charpos is -1. */
20190 || (row->ends_at_zv_p
20191 && !row->reversed_p
20192 && INTEGERP (g->object)
20193 && g->type == CHAR_GLYPH
20194 && g->u.ch == ' '))
20195 {
20196 if (g->charpos > 0)
20197 SET_PT (g->charpos);
20198 else if (!row->reversed_p
20199 && row->ends_at_zv_p
20200 && PT != ZV)
20201 SET_PT (ZV);
20202 else
20203 continue;
20204 w->cursor.vpos = -1;
20205 return make_number (PT);
20206 }
20207 }
20208 }
20209 else
20210 {
20211 if (!row->reversed_p && !row->continued_p)
20212 {
20213 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20214 w->cursor.vpos = -1;
20215 return make_number (PT);
20216 }
20217 e = row->glyphs[TEXT_AREA];
20218 g = e + row->used[TEXT_AREA] - 1;
20219 for ( ; g >= e; g--)
20220 {
20221 if (BUFFERP (g->object)
20222 || (ROW_GLYPH_NEWLINE_P (row, g)
20223 && g->charpos > 0)
20224 /* Empty R2L lines on GUI frames have the buffer
20225 position of the newline stored in the stretch
20226 glyph. */
20227 || g->type == STRETCH_GLYPH
20228 || (row->ends_at_zv_p
20229 && row->reversed_p
20230 && INTEGERP (g->object)
20231 && g->type == CHAR_GLYPH
20232 && g->u.ch == ' '))
20233 {
20234 if (g->charpos > 0)
20235 SET_PT (g->charpos);
20236 else if (row->reversed_p
20237 && row->ends_at_zv_p
20238 && PT != ZV)
20239 SET_PT (ZV);
20240 else
20241 continue;
20242 w->cursor.vpos = -1;
20243 return make_number (PT);
20244 }
20245 }
20246 }
20247 }
20248 }
20249
20250 simulate_display:
20251
20252 /* If we wind up here, we failed to move by using the glyphs, so we
20253 need to simulate display instead. */
20254
20255 if (b)
20256 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
20257 else
20258 paragraph_dir = Qleft_to_right;
20259 if (EQ (paragraph_dir, Qright_to_left))
20260 dir = -dir;
20261 if (PT <= BEGV && dir < 0)
20262 xsignal0 (Qbeginning_of_buffer);
20263 else if (PT >= ZV && dir > 0)
20264 xsignal0 (Qend_of_buffer);
20265 else
20266 {
20267 struct text_pos pt;
20268 struct it it;
20269 int pt_x, target_x, pixel_width, pt_vpos;
20270 bool at_eol_p;
20271 bool overshoot_expected = false;
20272 bool target_is_eol_p = false;
20273
20274 /* Setup the arena. */
20275 SET_TEXT_POS (pt, PT, PT_BYTE);
20276 start_display (&it, w, pt);
20277
20278 if (it.cmp_it.id < 0
20279 && it.method == GET_FROM_STRING
20280 && it.area == TEXT_AREA
20281 && it.string_from_display_prop_p
20282 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
20283 overshoot_expected = true;
20284
20285 /* Find the X coordinate of point. We start from the beginning
20286 of this or previous line to make sure we are before point in
20287 the logical order (since the move_it_* functions can only
20288 move forward). */
20289 reseat_at_previous_visible_line_start (&it);
20290 it.current_x = it.hpos = it.current_y = it.vpos = 0;
20291 if (IT_CHARPOS (it) != PT)
20292 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
20293 -1, -1, -1, MOVE_TO_POS);
20294 pt_x = it.current_x;
20295 pt_vpos = it.vpos;
20296 if (dir > 0 || overshoot_expected)
20297 {
20298 struct glyph_row *row = it.glyph_row;
20299
20300 /* When point is at beginning of line, we don't have
20301 information about the glyph there loaded into struct
20302 it. Calling get_next_display_element fixes that. */
20303 if (pt_x == 0)
20304 get_next_display_element (&it);
20305 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20306 it.glyph_row = NULL;
20307 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
20308 it.glyph_row = row;
20309 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
20310 it, lest it will become out of sync with it's buffer
20311 position. */
20312 it.current_x = pt_x;
20313 }
20314 else
20315 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20316 pixel_width = it.pixel_width;
20317 if (overshoot_expected && at_eol_p)
20318 pixel_width = 0;
20319 else if (pixel_width <= 0)
20320 pixel_width = 1;
20321
20322 /* If there's a display string at point, we are actually at the
20323 glyph to the left of point, so we need to correct the X
20324 coordinate. */
20325 if (overshoot_expected)
20326 pt_x += pixel_width;
20327
20328 /* Compute target X coordinate, either to the left or to the
20329 right of point. On TTY frames, all characters have the same
20330 pixel width of 1, so we can use that. On GUI frames we don't
20331 have an easy way of getting at the pixel width of the
20332 character to the left of point, so we use a different method
20333 of getting to that place. */
20334 if (dir > 0)
20335 target_x = pt_x + pixel_width;
20336 else
20337 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
20338
20339 /* Target X coordinate could be one line above or below the line
20340 of point, in which case we need to adjust the target X
20341 coordinate. Also, if moving to the left, we need to begin at
20342 the left edge of the point's screen line. */
20343 if (dir < 0)
20344 {
20345 if (pt_x > 0)
20346 {
20347 start_display (&it, w, pt);
20348 reseat_at_previous_visible_line_start (&it);
20349 it.current_x = it.current_y = it.hpos = 0;
20350 if (pt_vpos != 0)
20351 move_it_by_lines (&it, pt_vpos);
20352 }
20353 else
20354 {
20355 move_it_by_lines (&it, -1);
20356 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
20357 target_is_eol_p = true;
20358 }
20359 }
20360 else
20361 {
20362 if (at_eol_p
20363 || (target_x >= it.last_visible_x
20364 && it.line_wrap != TRUNCATE))
20365 {
20366 if (pt_x > 0)
20367 move_it_by_lines (&it, 0);
20368 move_it_by_lines (&it, 1);
20369 target_x = 0;
20370 }
20371 }
20372
20373 /* Move to the target X coordinate. */
20374 #ifdef HAVE_WINDOW_SYSTEM
20375 /* On GUI frames, as we don't know the X coordinate of the
20376 character to the left of point, moving point to the left
20377 requires walking, one grapheme cluster at a time, until we
20378 find ourself at a place immediately to the left of the
20379 character at point. */
20380 if (FRAME_WINDOW_P (it.f) && dir < 0)
20381 {
20382 struct text_pos new_pos = it.current.pos;
20383 enum move_it_result rc = MOVE_X_REACHED;
20384
20385 while (it.current_x + it.pixel_width <= target_x
20386 && rc == MOVE_X_REACHED)
20387 {
20388 int new_x = it.current_x + it.pixel_width;
20389
20390 new_pos = it.current.pos;
20391 if (new_x == it.current_x)
20392 new_x++;
20393 rc = move_it_in_display_line_to (&it, ZV, new_x,
20394 MOVE_TO_POS | MOVE_TO_X);
20395 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
20396 break;
20397 }
20398 /* If we ended up on a composed character inside
20399 bidi-reordered text (e.g., Hebrew text with diacritics),
20400 the iterator gives us the buffer position of the last (in
20401 logical order) character of the composed grapheme cluster,
20402 which is not what we want. So we cheat: we compute the
20403 character position of the character that follows (in the
20404 logical order) the one where the above loop stopped. That
20405 character will appear on display to the left of point. */
20406 if (it.bidi_p
20407 && it.bidi_it.scan_dir == -1
20408 && new_pos.charpos - IT_CHARPOS (it) > 1)
20409 {
20410 new_pos.charpos = IT_CHARPOS (it) + 1;
20411 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
20412 }
20413 it.current.pos = new_pos;
20414 }
20415 else
20416 #endif
20417 if (it.current_x != target_x)
20418 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
20419
20420 /* When lines are truncated, the above loop will stop at the
20421 window edge. But we want to get to the end of line, even if
20422 it is beyond the window edge; automatic hscroll will then
20423 scroll the window to show point as appropriate. */
20424 if (target_is_eol_p && it.line_wrap == TRUNCATE
20425 && get_next_display_element (&it))
20426 {
20427 struct text_pos new_pos = it.current.pos;
20428
20429 while (!ITERATOR_AT_END_OF_LINE_P (&it))
20430 {
20431 set_iterator_to_next (&it, 0);
20432 if (it.method == GET_FROM_BUFFER)
20433 new_pos = it.current.pos;
20434 if (!get_next_display_element (&it))
20435 break;
20436 }
20437
20438 it.current.pos = new_pos;
20439 }
20440
20441 /* If we ended up in a display string that covers point, move to
20442 buffer position to the right in the visual order. */
20443 if (dir > 0)
20444 {
20445 while (IT_CHARPOS (it) == PT)
20446 {
20447 set_iterator_to_next (&it, 0);
20448 if (!get_next_display_element (&it))
20449 break;
20450 }
20451 }
20452
20453 /* Move point to that position. */
20454 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
20455 }
20456
20457 return make_number (PT);
20458
20459 #undef ROW_GLYPH_NEWLINE_P
20460 }
20461
20462 \f
20463 /***********************************************************************
20464 Menu Bar
20465 ***********************************************************************/
20466
20467 /* Redisplay the menu bar in the frame for window W.
20468
20469 The menu bar of X frames that don't have X toolkit support is
20470 displayed in a special window W->frame->menu_bar_window.
20471
20472 The menu bar of terminal frames is treated specially as far as
20473 glyph matrices are concerned. Menu bar lines are not part of
20474 windows, so the update is done directly on the frame matrix rows
20475 for the menu bar. */
20476
20477 static void
20478 display_menu_bar (struct window *w)
20479 {
20480 struct frame *f = XFRAME (WINDOW_FRAME (w));
20481 struct it it;
20482 Lisp_Object items;
20483 int i;
20484
20485 /* Don't do all this for graphical frames. */
20486 #ifdef HAVE_NTGUI
20487 if (FRAME_W32_P (f))
20488 return;
20489 #endif
20490 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20491 if (FRAME_X_P (f))
20492 return;
20493 #endif
20494
20495 #ifdef HAVE_NS
20496 if (FRAME_NS_P (f))
20497 return;
20498 #endif /* HAVE_NS */
20499
20500 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20501 eassert (!FRAME_WINDOW_P (f));
20502 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20503 it.first_visible_x = 0;
20504 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20505 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
20506 if (FRAME_WINDOW_P (f))
20507 {
20508 /* Menu bar lines are displayed in the desired matrix of the
20509 dummy window menu_bar_window. */
20510 struct window *menu_w;
20511 menu_w = XWINDOW (f->menu_bar_window);
20512 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20513 MENU_FACE_ID);
20514 it.first_visible_x = 0;
20515 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20516 }
20517 else
20518 #endif /* not USE_X_TOOLKIT and not USE_GTK */
20519 {
20520 /* This is a TTY frame, i.e. character hpos/vpos are used as
20521 pixel x/y. */
20522 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20523 MENU_FACE_ID);
20524 it.first_visible_x = 0;
20525 it.last_visible_x = FRAME_COLS (f);
20526 }
20527
20528 /* FIXME: This should be controlled by a user option. See the
20529 comments in redisplay_tool_bar and display_mode_line about
20530 this. */
20531 it.paragraph_embedding = L2R;
20532
20533 /* Clear all rows of the menu bar. */
20534 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20535 {
20536 struct glyph_row *row = it.glyph_row + i;
20537 clear_glyph_row (row);
20538 row->enabled_p = 1;
20539 row->full_width_p = 1;
20540 }
20541
20542 /* Display all items of the menu bar. */
20543 items = FRAME_MENU_BAR_ITEMS (it.f);
20544 for (i = 0; i < ASIZE (items); i += 4)
20545 {
20546 Lisp_Object string;
20547
20548 /* Stop at nil string. */
20549 string = AREF (items, i + 1);
20550 if (NILP (string))
20551 break;
20552
20553 /* Remember where item was displayed. */
20554 ASET (items, i + 3, make_number (it.hpos));
20555
20556 /* Display the item, pad with one space. */
20557 if (it.current_x < it.last_visible_x)
20558 display_string (NULL, string, Qnil, 0, 0, &it,
20559 SCHARS (string) + 1, 0, 0, -1);
20560 }
20561
20562 /* Fill out the line with spaces. */
20563 if (it.current_x < it.last_visible_x)
20564 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20565
20566 /* Compute the total height of the lines. */
20567 compute_line_metrics (&it);
20568 }
20569
20570
20571 \f
20572 /***********************************************************************
20573 Mode Line
20574 ***********************************************************************/
20575
20576 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20577 FORCE is non-zero, redisplay mode lines unconditionally.
20578 Otherwise, redisplay only mode lines that are garbaged. Value is
20579 the number of windows whose mode lines were redisplayed. */
20580
20581 static int
20582 redisplay_mode_lines (Lisp_Object window, int force)
20583 {
20584 int nwindows = 0;
20585
20586 while (!NILP (window))
20587 {
20588 struct window *w = XWINDOW (window);
20589
20590 if (WINDOWP (w->contents))
20591 nwindows += redisplay_mode_lines (w->contents, force);
20592 else if (force
20593 || FRAME_GARBAGED_P (XFRAME (w->frame))
20594 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20595 {
20596 struct text_pos lpoint;
20597 struct buffer *old = current_buffer;
20598
20599 /* Set the window's buffer for the mode line display. */
20600 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20601 set_buffer_internal_1 (XBUFFER (w->contents));
20602
20603 /* Point refers normally to the selected window. For any
20604 other window, set up appropriate value. */
20605 if (!EQ (window, selected_window))
20606 {
20607 struct text_pos pt;
20608
20609 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
20610 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20611 }
20612
20613 /* Display mode lines. */
20614 clear_glyph_matrix (w->desired_matrix);
20615 if (display_mode_lines (w))
20616 {
20617 ++nwindows;
20618 w->must_be_updated_p = 1;
20619 }
20620
20621 /* Restore old settings. */
20622 set_buffer_internal_1 (old);
20623 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20624 }
20625
20626 window = w->next;
20627 }
20628
20629 return nwindows;
20630 }
20631
20632
20633 /* Display the mode and/or header line of window W. Value is the
20634 sum number of mode lines and header lines displayed. */
20635
20636 static int
20637 display_mode_lines (struct window *w)
20638 {
20639 Lisp_Object old_selected_window = selected_window;
20640 Lisp_Object old_selected_frame = selected_frame;
20641 Lisp_Object new_frame = w->frame;
20642 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
20643 int n = 0;
20644
20645 selected_frame = new_frame;
20646 /* FIXME: If we were to allow the mode-line's computation changing the buffer
20647 or window's point, then we'd need select_window_1 here as well. */
20648 XSETWINDOW (selected_window, w);
20649 XFRAME (new_frame)->selected_window = selected_window;
20650
20651 /* These will be set while the mode line specs are processed. */
20652 line_number_displayed = 0;
20653 w->column_number_displayed = -1;
20654
20655 if (WINDOW_WANTS_MODELINE_P (w))
20656 {
20657 struct window *sel_w = XWINDOW (old_selected_window);
20658
20659 /* Select mode line face based on the real selected window. */
20660 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20661 BVAR (current_buffer, mode_line_format));
20662 ++n;
20663 }
20664
20665 if (WINDOW_WANTS_HEADER_LINE_P (w))
20666 {
20667 display_mode_line (w, HEADER_LINE_FACE_ID,
20668 BVAR (current_buffer, header_line_format));
20669 ++n;
20670 }
20671
20672 XFRAME (new_frame)->selected_window = old_frame_selected_window;
20673 selected_frame = old_selected_frame;
20674 selected_window = old_selected_window;
20675 return n;
20676 }
20677
20678
20679 /* Display mode or header line of window W. FACE_ID specifies which
20680 line to display; it is either MODE_LINE_FACE_ID or
20681 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20682 display. Value is the pixel height of the mode/header line
20683 displayed. */
20684
20685 static int
20686 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20687 {
20688 struct it it;
20689 struct face *face;
20690 ptrdiff_t count = SPECPDL_INDEX ();
20691
20692 init_iterator (&it, w, -1, -1, NULL, face_id);
20693 /* Don't extend on a previously drawn mode-line.
20694 This may happen if called from pos_visible_p. */
20695 it.glyph_row->enabled_p = 0;
20696 prepare_desired_row (it.glyph_row);
20697
20698 it.glyph_row->mode_line_p = 1;
20699
20700 /* FIXME: This should be controlled by a user option. But
20701 supporting such an option is not trivial, since the mode line is
20702 made up of many separate strings. */
20703 it.paragraph_embedding = L2R;
20704
20705 record_unwind_protect (unwind_format_mode_line,
20706 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20707
20708 mode_line_target = MODE_LINE_DISPLAY;
20709
20710 /* Temporarily make frame's keyboard the current kboard so that
20711 kboard-local variables in the mode_line_format will get the right
20712 values. */
20713 push_kboard (FRAME_KBOARD (it.f));
20714 record_unwind_save_match_data ();
20715 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20716 pop_kboard ();
20717
20718 unbind_to (count, Qnil);
20719
20720 /* Fill up with spaces. */
20721 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20722
20723 compute_line_metrics (&it);
20724 it.glyph_row->full_width_p = 1;
20725 it.glyph_row->continued_p = 0;
20726 it.glyph_row->truncated_on_left_p = 0;
20727 it.glyph_row->truncated_on_right_p = 0;
20728
20729 /* Make a 3D mode-line have a shadow at its right end. */
20730 face = FACE_FROM_ID (it.f, face_id);
20731 extend_face_to_end_of_line (&it);
20732 if (face->box != FACE_NO_BOX)
20733 {
20734 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20735 + it.glyph_row->used[TEXT_AREA] - 1);
20736 last->right_box_line_p = 1;
20737 }
20738
20739 return it.glyph_row->height;
20740 }
20741
20742 /* Move element ELT in LIST to the front of LIST.
20743 Return the updated list. */
20744
20745 static Lisp_Object
20746 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20747 {
20748 register Lisp_Object tail, prev;
20749 register Lisp_Object tem;
20750
20751 tail = list;
20752 prev = Qnil;
20753 while (CONSP (tail))
20754 {
20755 tem = XCAR (tail);
20756
20757 if (EQ (elt, tem))
20758 {
20759 /* Splice out the link TAIL. */
20760 if (NILP (prev))
20761 list = XCDR (tail);
20762 else
20763 Fsetcdr (prev, XCDR (tail));
20764
20765 /* Now make it the first. */
20766 Fsetcdr (tail, list);
20767 return tail;
20768 }
20769 else
20770 prev = tail;
20771 tail = XCDR (tail);
20772 QUIT;
20773 }
20774
20775 /* Not found--return unchanged LIST. */
20776 return list;
20777 }
20778
20779 /* Contribute ELT to the mode line for window IT->w. How it
20780 translates into text depends on its data type.
20781
20782 IT describes the display environment in which we display, as usual.
20783
20784 DEPTH is the depth in recursion. It is used to prevent
20785 infinite recursion here.
20786
20787 FIELD_WIDTH is the number of characters the display of ELT should
20788 occupy in the mode line, and PRECISION is the maximum number of
20789 characters to display from ELT's representation. See
20790 display_string for details.
20791
20792 Returns the hpos of the end of the text generated by ELT.
20793
20794 PROPS is a property list to add to any string we encounter.
20795
20796 If RISKY is nonzero, remove (disregard) any properties in any string
20797 we encounter, and ignore :eval and :propertize.
20798
20799 The global variable `mode_line_target' determines whether the
20800 output is passed to `store_mode_line_noprop',
20801 `store_mode_line_string', or `display_string'. */
20802
20803 static int
20804 display_mode_element (struct it *it, int depth, int field_width, int precision,
20805 Lisp_Object elt, Lisp_Object props, int risky)
20806 {
20807 int n = 0, field, prec;
20808 int literal = 0;
20809
20810 tail_recurse:
20811 if (depth > 100)
20812 elt = build_string ("*too-deep*");
20813
20814 depth++;
20815
20816 switch (XTYPE (elt))
20817 {
20818 case Lisp_String:
20819 {
20820 /* A string: output it and check for %-constructs within it. */
20821 unsigned char c;
20822 ptrdiff_t offset = 0;
20823
20824 if (SCHARS (elt) > 0
20825 && (!NILP (props) || risky))
20826 {
20827 Lisp_Object oprops, aelt;
20828 oprops = Ftext_properties_at (make_number (0), elt);
20829
20830 /* If the starting string's properties are not what
20831 we want, translate the string. Also, if the string
20832 is risky, do that anyway. */
20833
20834 if (NILP (Fequal (props, oprops)) || risky)
20835 {
20836 /* If the starting string has properties,
20837 merge the specified ones onto the existing ones. */
20838 if (! NILP (oprops) && !risky)
20839 {
20840 Lisp_Object tem;
20841
20842 oprops = Fcopy_sequence (oprops);
20843 tem = props;
20844 while (CONSP (tem))
20845 {
20846 oprops = Fplist_put (oprops, XCAR (tem),
20847 XCAR (XCDR (tem)));
20848 tem = XCDR (XCDR (tem));
20849 }
20850 props = oprops;
20851 }
20852
20853 aelt = Fassoc (elt, mode_line_proptrans_alist);
20854 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20855 {
20856 /* AELT is what we want. Move it to the front
20857 without consing. */
20858 elt = XCAR (aelt);
20859 mode_line_proptrans_alist
20860 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20861 }
20862 else
20863 {
20864 Lisp_Object tem;
20865
20866 /* If AELT has the wrong props, it is useless.
20867 so get rid of it. */
20868 if (! NILP (aelt))
20869 mode_line_proptrans_alist
20870 = Fdelq (aelt, mode_line_proptrans_alist);
20871
20872 elt = Fcopy_sequence (elt);
20873 Fset_text_properties (make_number (0), Flength (elt),
20874 props, elt);
20875 /* Add this item to mode_line_proptrans_alist. */
20876 mode_line_proptrans_alist
20877 = Fcons (Fcons (elt, props),
20878 mode_line_proptrans_alist);
20879 /* Truncate mode_line_proptrans_alist
20880 to at most 50 elements. */
20881 tem = Fnthcdr (make_number (50),
20882 mode_line_proptrans_alist);
20883 if (! NILP (tem))
20884 XSETCDR (tem, Qnil);
20885 }
20886 }
20887 }
20888
20889 offset = 0;
20890
20891 if (literal)
20892 {
20893 prec = precision - n;
20894 switch (mode_line_target)
20895 {
20896 case MODE_LINE_NOPROP:
20897 case MODE_LINE_TITLE:
20898 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20899 break;
20900 case MODE_LINE_STRING:
20901 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20902 break;
20903 case MODE_LINE_DISPLAY:
20904 n += display_string (NULL, elt, Qnil, 0, 0, it,
20905 0, prec, 0, STRING_MULTIBYTE (elt));
20906 break;
20907 }
20908
20909 break;
20910 }
20911
20912 /* Handle the non-literal case. */
20913
20914 while ((precision <= 0 || n < precision)
20915 && SREF (elt, offset) != 0
20916 && (mode_line_target != MODE_LINE_DISPLAY
20917 || it->current_x < it->last_visible_x))
20918 {
20919 ptrdiff_t last_offset = offset;
20920
20921 /* Advance to end of string or next format specifier. */
20922 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20923 ;
20924
20925 if (offset - 1 != last_offset)
20926 {
20927 ptrdiff_t nchars, nbytes;
20928
20929 /* Output to end of string or up to '%'. Field width
20930 is length of string. Don't output more than
20931 PRECISION allows us. */
20932 offset--;
20933
20934 prec = c_string_width (SDATA (elt) + last_offset,
20935 offset - last_offset, precision - n,
20936 &nchars, &nbytes);
20937
20938 switch (mode_line_target)
20939 {
20940 case MODE_LINE_NOPROP:
20941 case MODE_LINE_TITLE:
20942 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20943 break;
20944 case MODE_LINE_STRING:
20945 {
20946 ptrdiff_t bytepos = last_offset;
20947 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20948 ptrdiff_t endpos = (precision <= 0
20949 ? string_byte_to_char (elt, offset)
20950 : charpos + nchars);
20951
20952 n += store_mode_line_string (NULL,
20953 Fsubstring (elt, make_number (charpos),
20954 make_number (endpos)),
20955 0, 0, 0, Qnil);
20956 }
20957 break;
20958 case MODE_LINE_DISPLAY:
20959 {
20960 ptrdiff_t bytepos = last_offset;
20961 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20962
20963 if (precision <= 0)
20964 nchars = string_byte_to_char (elt, offset) - charpos;
20965 n += display_string (NULL, elt, Qnil, 0, charpos,
20966 it, 0, nchars, 0,
20967 STRING_MULTIBYTE (elt));
20968 }
20969 break;
20970 }
20971 }
20972 else /* c == '%' */
20973 {
20974 ptrdiff_t percent_position = offset;
20975
20976 /* Get the specified minimum width. Zero means
20977 don't pad. */
20978 field = 0;
20979 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20980 field = field * 10 + c - '0';
20981
20982 /* Don't pad beyond the total padding allowed. */
20983 if (field_width - n > 0 && field > field_width - n)
20984 field = field_width - n;
20985
20986 /* Note that either PRECISION <= 0 or N < PRECISION. */
20987 prec = precision - n;
20988
20989 if (c == 'M')
20990 n += display_mode_element (it, depth, field, prec,
20991 Vglobal_mode_string, props,
20992 risky);
20993 else if (c != 0)
20994 {
20995 bool multibyte;
20996 ptrdiff_t bytepos, charpos;
20997 const char *spec;
20998 Lisp_Object string;
20999
21000 bytepos = percent_position;
21001 charpos = (STRING_MULTIBYTE (elt)
21002 ? string_byte_to_char (elt, bytepos)
21003 : bytepos);
21004 spec = decode_mode_spec (it->w, c, field, &string);
21005 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
21006
21007 switch (mode_line_target)
21008 {
21009 case MODE_LINE_NOPROP:
21010 case MODE_LINE_TITLE:
21011 n += store_mode_line_noprop (spec, field, prec);
21012 break;
21013 case MODE_LINE_STRING:
21014 {
21015 Lisp_Object tem = build_string (spec);
21016 props = Ftext_properties_at (make_number (charpos), elt);
21017 /* Should only keep face property in props */
21018 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
21019 }
21020 break;
21021 case MODE_LINE_DISPLAY:
21022 {
21023 int nglyphs_before, nwritten;
21024
21025 nglyphs_before = it->glyph_row->used[TEXT_AREA];
21026 nwritten = display_string (spec, string, elt,
21027 charpos, 0, it,
21028 field, prec, 0,
21029 multibyte);
21030
21031 /* Assign to the glyphs written above the
21032 string where the `%x' came from, position
21033 of the `%'. */
21034 if (nwritten > 0)
21035 {
21036 struct glyph *glyph
21037 = (it->glyph_row->glyphs[TEXT_AREA]
21038 + nglyphs_before);
21039 int i;
21040
21041 for (i = 0; i < nwritten; ++i)
21042 {
21043 glyph[i].object = elt;
21044 glyph[i].charpos = charpos;
21045 }
21046
21047 n += nwritten;
21048 }
21049 }
21050 break;
21051 }
21052 }
21053 else /* c == 0 */
21054 break;
21055 }
21056 }
21057 }
21058 break;
21059
21060 case Lisp_Symbol:
21061 /* A symbol: process the value of the symbol recursively
21062 as if it appeared here directly. Avoid error if symbol void.
21063 Special case: if value of symbol is a string, output the string
21064 literally. */
21065 {
21066 register Lisp_Object tem;
21067
21068 /* If the variable is not marked as risky to set
21069 then its contents are risky to use. */
21070 if (NILP (Fget (elt, Qrisky_local_variable)))
21071 risky = 1;
21072
21073 tem = Fboundp (elt);
21074 if (!NILP (tem))
21075 {
21076 tem = Fsymbol_value (elt);
21077 /* If value is a string, output that string literally:
21078 don't check for % within it. */
21079 if (STRINGP (tem))
21080 literal = 1;
21081
21082 if (!EQ (tem, elt))
21083 {
21084 /* Give up right away for nil or t. */
21085 elt = tem;
21086 goto tail_recurse;
21087 }
21088 }
21089 }
21090 break;
21091
21092 case Lisp_Cons:
21093 {
21094 register Lisp_Object car, tem;
21095
21096 /* A cons cell: five distinct cases.
21097 If first element is :eval or :propertize, do something special.
21098 If first element is a string or a cons, process all the elements
21099 and effectively concatenate them.
21100 If first element is a negative number, truncate displaying cdr to
21101 at most that many characters. If positive, pad (with spaces)
21102 to at least that many characters.
21103 If first element is a symbol, process the cadr or caddr recursively
21104 according to whether the symbol's value is non-nil or nil. */
21105 car = XCAR (elt);
21106 if (EQ (car, QCeval))
21107 {
21108 /* An element of the form (:eval FORM) means evaluate FORM
21109 and use the result as mode line elements. */
21110
21111 if (risky)
21112 break;
21113
21114 if (CONSP (XCDR (elt)))
21115 {
21116 Lisp_Object spec;
21117 spec = safe_eval (XCAR (XCDR (elt)));
21118 n += display_mode_element (it, depth, field_width - n,
21119 precision - n, spec, props,
21120 risky);
21121 }
21122 }
21123 else if (EQ (car, QCpropertize))
21124 {
21125 /* An element of the form (:propertize ELT PROPS...)
21126 means display ELT but applying properties PROPS. */
21127
21128 if (risky)
21129 break;
21130
21131 if (CONSP (XCDR (elt)))
21132 n += display_mode_element (it, depth, field_width - n,
21133 precision - n, XCAR (XCDR (elt)),
21134 XCDR (XCDR (elt)), risky);
21135 }
21136 else if (SYMBOLP (car))
21137 {
21138 tem = Fboundp (car);
21139 elt = XCDR (elt);
21140 if (!CONSP (elt))
21141 goto invalid;
21142 /* elt is now the cdr, and we know it is a cons cell.
21143 Use its car if CAR has a non-nil value. */
21144 if (!NILP (tem))
21145 {
21146 tem = Fsymbol_value (car);
21147 if (!NILP (tem))
21148 {
21149 elt = XCAR (elt);
21150 goto tail_recurse;
21151 }
21152 }
21153 /* Symbol's value is nil (or symbol is unbound)
21154 Get the cddr of the original list
21155 and if possible find the caddr and use that. */
21156 elt = XCDR (elt);
21157 if (NILP (elt))
21158 break;
21159 else if (!CONSP (elt))
21160 goto invalid;
21161 elt = XCAR (elt);
21162 goto tail_recurse;
21163 }
21164 else if (INTEGERP (car))
21165 {
21166 register int lim = XINT (car);
21167 elt = XCDR (elt);
21168 if (lim < 0)
21169 {
21170 /* Negative int means reduce maximum width. */
21171 if (precision <= 0)
21172 precision = -lim;
21173 else
21174 precision = min (precision, -lim);
21175 }
21176 else if (lim > 0)
21177 {
21178 /* Padding specified. Don't let it be more than
21179 current maximum. */
21180 if (precision > 0)
21181 lim = min (precision, lim);
21182
21183 /* If that's more padding than already wanted, queue it.
21184 But don't reduce padding already specified even if
21185 that is beyond the current truncation point. */
21186 field_width = max (lim, field_width);
21187 }
21188 goto tail_recurse;
21189 }
21190 else if (STRINGP (car) || CONSP (car))
21191 {
21192 Lisp_Object halftail = elt;
21193 int len = 0;
21194
21195 while (CONSP (elt)
21196 && (precision <= 0 || n < precision))
21197 {
21198 n += display_mode_element (it, depth,
21199 /* Do padding only after the last
21200 element in the list. */
21201 (! CONSP (XCDR (elt))
21202 ? field_width - n
21203 : 0),
21204 precision - n, XCAR (elt),
21205 props, risky);
21206 elt = XCDR (elt);
21207 len++;
21208 if ((len & 1) == 0)
21209 halftail = XCDR (halftail);
21210 /* Check for cycle. */
21211 if (EQ (halftail, elt))
21212 break;
21213 }
21214 }
21215 }
21216 break;
21217
21218 default:
21219 invalid:
21220 elt = build_string ("*invalid*");
21221 goto tail_recurse;
21222 }
21223
21224 /* Pad to FIELD_WIDTH. */
21225 if (field_width > 0 && n < field_width)
21226 {
21227 switch (mode_line_target)
21228 {
21229 case MODE_LINE_NOPROP:
21230 case MODE_LINE_TITLE:
21231 n += store_mode_line_noprop ("", field_width - n, 0);
21232 break;
21233 case MODE_LINE_STRING:
21234 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
21235 break;
21236 case MODE_LINE_DISPLAY:
21237 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
21238 0, 0, 0);
21239 break;
21240 }
21241 }
21242
21243 return n;
21244 }
21245
21246 /* Store a mode-line string element in mode_line_string_list.
21247
21248 If STRING is non-null, display that C string. Otherwise, the Lisp
21249 string LISP_STRING is displayed.
21250
21251 FIELD_WIDTH is the minimum number of output glyphs to produce.
21252 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21253 with spaces. FIELD_WIDTH <= 0 means don't pad.
21254
21255 PRECISION is the maximum number of characters to output from
21256 STRING. PRECISION <= 0 means don't truncate the string.
21257
21258 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
21259 properties to the string.
21260
21261 PROPS are the properties to add to the string.
21262 The mode_line_string_face face property is always added to the string.
21263 */
21264
21265 static int
21266 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
21267 int field_width, int precision, Lisp_Object props)
21268 {
21269 ptrdiff_t len;
21270 int n = 0;
21271
21272 if (string != NULL)
21273 {
21274 len = strlen (string);
21275 if (precision > 0 && len > precision)
21276 len = precision;
21277 lisp_string = make_string (string, len);
21278 if (NILP (props))
21279 props = mode_line_string_face_prop;
21280 else if (!NILP (mode_line_string_face))
21281 {
21282 Lisp_Object face = Fplist_get (props, Qface);
21283 props = Fcopy_sequence (props);
21284 if (NILP (face))
21285 face = mode_line_string_face;
21286 else
21287 face = list2 (face, mode_line_string_face);
21288 props = Fplist_put (props, Qface, face);
21289 }
21290 Fadd_text_properties (make_number (0), make_number (len),
21291 props, lisp_string);
21292 }
21293 else
21294 {
21295 len = XFASTINT (Flength (lisp_string));
21296 if (precision > 0 && len > precision)
21297 {
21298 len = precision;
21299 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
21300 precision = -1;
21301 }
21302 if (!NILP (mode_line_string_face))
21303 {
21304 Lisp_Object face;
21305 if (NILP (props))
21306 props = Ftext_properties_at (make_number (0), lisp_string);
21307 face = Fplist_get (props, Qface);
21308 if (NILP (face))
21309 face = mode_line_string_face;
21310 else
21311 face = list2 (face, mode_line_string_face);
21312 props = list2 (Qface, face);
21313 if (copy_string)
21314 lisp_string = Fcopy_sequence (lisp_string);
21315 }
21316 if (!NILP (props))
21317 Fadd_text_properties (make_number (0), make_number (len),
21318 props, lisp_string);
21319 }
21320
21321 if (len > 0)
21322 {
21323 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21324 n += len;
21325 }
21326
21327 if (field_width > len)
21328 {
21329 field_width -= len;
21330 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
21331 if (!NILP (props))
21332 Fadd_text_properties (make_number (0), make_number (field_width),
21333 props, lisp_string);
21334 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21335 n += field_width;
21336 }
21337
21338 return n;
21339 }
21340
21341
21342 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
21343 1, 4, 0,
21344 doc: /* Format a string out of a mode line format specification.
21345 First arg FORMAT specifies the mode line format (see `mode-line-format'
21346 for details) to use.
21347
21348 By default, the format is evaluated for the currently selected window.
21349
21350 Optional second arg FACE specifies the face property to put on all
21351 characters for which no face is specified. The value nil means the
21352 default face. The value t means whatever face the window's mode line
21353 currently uses (either `mode-line' or `mode-line-inactive',
21354 depending on whether the window is the selected window or not).
21355 An integer value means the value string has no text
21356 properties.
21357
21358 Optional third and fourth args WINDOW and BUFFER specify the window
21359 and buffer to use as the context for the formatting (defaults
21360 are the selected window and the WINDOW's buffer). */)
21361 (Lisp_Object format, Lisp_Object face,
21362 Lisp_Object window, Lisp_Object buffer)
21363 {
21364 struct it it;
21365 int len;
21366 struct window *w;
21367 struct buffer *old_buffer = NULL;
21368 int face_id;
21369 int no_props = INTEGERP (face);
21370 ptrdiff_t count = SPECPDL_INDEX ();
21371 Lisp_Object str;
21372 int string_start = 0;
21373
21374 w = decode_any_window (window);
21375 XSETWINDOW (window, w);
21376
21377 if (NILP (buffer))
21378 buffer = w->contents;
21379 CHECK_BUFFER (buffer);
21380
21381 /* Make formatting the modeline a non-op when noninteractive, otherwise
21382 there will be problems later caused by a partially initialized frame. */
21383 if (NILP (format) || noninteractive)
21384 return empty_unibyte_string;
21385
21386 if (no_props)
21387 face = Qnil;
21388
21389 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
21390 : EQ (face, Qt) ? (EQ (window, selected_window)
21391 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21392 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21393 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21394 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21395 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21396 : DEFAULT_FACE_ID;
21397
21398 old_buffer = current_buffer;
21399
21400 /* Save things including mode_line_proptrans_alist,
21401 and set that to nil so that we don't alter the outer value. */
21402 record_unwind_protect (unwind_format_mode_line,
21403 format_mode_line_unwind_data
21404 (XFRAME (WINDOW_FRAME (w)),
21405 old_buffer, selected_window, 1));
21406 mode_line_proptrans_alist = Qnil;
21407
21408 Fselect_window (window, Qt);
21409 set_buffer_internal_1 (XBUFFER (buffer));
21410
21411 init_iterator (&it, w, -1, -1, NULL, face_id);
21412
21413 if (no_props)
21414 {
21415 mode_line_target = MODE_LINE_NOPROP;
21416 mode_line_string_face_prop = Qnil;
21417 mode_line_string_list = Qnil;
21418 string_start = MODE_LINE_NOPROP_LEN (0);
21419 }
21420 else
21421 {
21422 mode_line_target = MODE_LINE_STRING;
21423 mode_line_string_list = Qnil;
21424 mode_line_string_face = face;
21425 mode_line_string_face_prop
21426 = NILP (face) ? Qnil : list2 (Qface, face);
21427 }
21428
21429 push_kboard (FRAME_KBOARD (it.f));
21430 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21431 pop_kboard ();
21432
21433 if (no_props)
21434 {
21435 len = MODE_LINE_NOPROP_LEN (string_start);
21436 str = make_string (mode_line_noprop_buf + string_start, len);
21437 }
21438 else
21439 {
21440 mode_line_string_list = Fnreverse (mode_line_string_list);
21441 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21442 empty_unibyte_string);
21443 }
21444
21445 unbind_to (count, Qnil);
21446 return str;
21447 }
21448
21449 /* Write a null-terminated, right justified decimal representation of
21450 the positive integer D to BUF using a minimal field width WIDTH. */
21451
21452 static void
21453 pint2str (register char *buf, register int width, register ptrdiff_t d)
21454 {
21455 register char *p = buf;
21456
21457 if (d <= 0)
21458 *p++ = '0';
21459 else
21460 {
21461 while (d > 0)
21462 {
21463 *p++ = d % 10 + '0';
21464 d /= 10;
21465 }
21466 }
21467
21468 for (width -= (int) (p - buf); width > 0; --width)
21469 *p++ = ' ';
21470 *p-- = '\0';
21471 while (p > buf)
21472 {
21473 d = *buf;
21474 *buf++ = *p;
21475 *p-- = d;
21476 }
21477 }
21478
21479 /* Write a null-terminated, right justified decimal and "human
21480 readable" representation of the nonnegative integer D to BUF using
21481 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21482
21483 static const char power_letter[] =
21484 {
21485 0, /* no letter */
21486 'k', /* kilo */
21487 'M', /* mega */
21488 'G', /* giga */
21489 'T', /* tera */
21490 'P', /* peta */
21491 'E', /* exa */
21492 'Z', /* zetta */
21493 'Y' /* yotta */
21494 };
21495
21496 static void
21497 pint2hrstr (char *buf, int width, ptrdiff_t d)
21498 {
21499 /* We aim to represent the nonnegative integer D as
21500 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21501 ptrdiff_t quotient = d;
21502 int remainder = 0;
21503 /* -1 means: do not use TENTHS. */
21504 int tenths = -1;
21505 int exponent = 0;
21506
21507 /* Length of QUOTIENT.TENTHS as a string. */
21508 int length;
21509
21510 char * psuffix;
21511 char * p;
21512
21513 if (quotient >= 1000)
21514 {
21515 /* Scale to the appropriate EXPONENT. */
21516 do
21517 {
21518 remainder = quotient % 1000;
21519 quotient /= 1000;
21520 exponent++;
21521 }
21522 while (quotient >= 1000);
21523
21524 /* Round to nearest and decide whether to use TENTHS or not. */
21525 if (quotient <= 9)
21526 {
21527 tenths = remainder / 100;
21528 if (remainder % 100 >= 50)
21529 {
21530 if (tenths < 9)
21531 tenths++;
21532 else
21533 {
21534 quotient++;
21535 if (quotient == 10)
21536 tenths = -1;
21537 else
21538 tenths = 0;
21539 }
21540 }
21541 }
21542 else
21543 if (remainder >= 500)
21544 {
21545 if (quotient < 999)
21546 quotient++;
21547 else
21548 {
21549 quotient = 1;
21550 exponent++;
21551 tenths = 0;
21552 }
21553 }
21554 }
21555
21556 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21557 if (tenths == -1 && quotient <= 99)
21558 if (quotient <= 9)
21559 length = 1;
21560 else
21561 length = 2;
21562 else
21563 length = 3;
21564 p = psuffix = buf + max (width, length);
21565
21566 /* Print EXPONENT. */
21567 *psuffix++ = power_letter[exponent];
21568 *psuffix = '\0';
21569
21570 /* Print TENTHS. */
21571 if (tenths >= 0)
21572 {
21573 *--p = '0' + tenths;
21574 *--p = '.';
21575 }
21576
21577 /* Print QUOTIENT. */
21578 do
21579 {
21580 int digit = quotient % 10;
21581 *--p = '0' + digit;
21582 }
21583 while ((quotient /= 10) != 0);
21584
21585 /* Print leading spaces. */
21586 while (buf < p)
21587 *--p = ' ';
21588 }
21589
21590 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21591 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21592 type of CODING_SYSTEM. Return updated pointer into BUF. */
21593
21594 static unsigned char invalid_eol_type[] = "(*invalid*)";
21595
21596 static char *
21597 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21598 {
21599 Lisp_Object val;
21600 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21601 const unsigned char *eol_str;
21602 int eol_str_len;
21603 /* The EOL conversion we are using. */
21604 Lisp_Object eoltype;
21605
21606 val = CODING_SYSTEM_SPEC (coding_system);
21607 eoltype = Qnil;
21608
21609 if (!VECTORP (val)) /* Not yet decided. */
21610 {
21611 *buf++ = multibyte ? '-' : ' ';
21612 if (eol_flag)
21613 eoltype = eol_mnemonic_undecided;
21614 /* Don't mention EOL conversion if it isn't decided. */
21615 }
21616 else
21617 {
21618 Lisp_Object attrs;
21619 Lisp_Object eolvalue;
21620
21621 attrs = AREF (val, 0);
21622 eolvalue = AREF (val, 2);
21623
21624 *buf++ = multibyte
21625 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21626 : ' ';
21627
21628 if (eol_flag)
21629 {
21630 /* The EOL conversion that is normal on this system. */
21631
21632 if (NILP (eolvalue)) /* Not yet decided. */
21633 eoltype = eol_mnemonic_undecided;
21634 else if (VECTORP (eolvalue)) /* Not yet decided. */
21635 eoltype = eol_mnemonic_undecided;
21636 else /* eolvalue is Qunix, Qdos, or Qmac. */
21637 eoltype = (EQ (eolvalue, Qunix)
21638 ? eol_mnemonic_unix
21639 : (EQ (eolvalue, Qdos) == 1
21640 ? eol_mnemonic_dos : eol_mnemonic_mac));
21641 }
21642 }
21643
21644 if (eol_flag)
21645 {
21646 /* Mention the EOL conversion if it is not the usual one. */
21647 if (STRINGP (eoltype))
21648 {
21649 eol_str = SDATA (eoltype);
21650 eol_str_len = SBYTES (eoltype);
21651 }
21652 else if (CHARACTERP (eoltype))
21653 {
21654 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21655 int c = XFASTINT (eoltype);
21656 eol_str_len = CHAR_STRING (c, tmp);
21657 eol_str = tmp;
21658 }
21659 else
21660 {
21661 eol_str = invalid_eol_type;
21662 eol_str_len = sizeof (invalid_eol_type) - 1;
21663 }
21664 memcpy (buf, eol_str, eol_str_len);
21665 buf += eol_str_len;
21666 }
21667
21668 return buf;
21669 }
21670
21671 /* Return a string for the output of a mode line %-spec for window W,
21672 generated by character C. FIELD_WIDTH > 0 means pad the string
21673 returned with spaces to that value. Return a Lisp string in
21674 *STRING if the resulting string is taken from that Lisp string.
21675
21676 Note we operate on the current buffer for most purposes. */
21677
21678 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21679
21680 static const char *
21681 decode_mode_spec (struct window *w, register int c, int field_width,
21682 Lisp_Object *string)
21683 {
21684 Lisp_Object obj;
21685 struct frame *f = XFRAME (WINDOW_FRAME (w));
21686 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21687 /* We are going to use f->decode_mode_spec_buffer as the buffer to
21688 produce strings from numerical values, so limit preposterously
21689 large values of FIELD_WIDTH to avoid overrunning the buffer's
21690 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
21691 bytes plus the terminating null. */
21692 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
21693 struct buffer *b = current_buffer;
21694
21695 obj = Qnil;
21696 *string = Qnil;
21697
21698 switch (c)
21699 {
21700 case '*':
21701 if (!NILP (BVAR (b, read_only)))
21702 return "%";
21703 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21704 return "*";
21705 return "-";
21706
21707 case '+':
21708 /* This differs from %* only for a modified read-only buffer. */
21709 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21710 return "*";
21711 if (!NILP (BVAR (b, read_only)))
21712 return "%";
21713 return "-";
21714
21715 case '&':
21716 /* This differs from %* in ignoring read-only-ness. */
21717 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21718 return "*";
21719 return "-";
21720
21721 case '%':
21722 return "%";
21723
21724 case '[':
21725 {
21726 int i;
21727 char *p;
21728
21729 if (command_loop_level > 5)
21730 return "[[[... ";
21731 p = decode_mode_spec_buf;
21732 for (i = 0; i < command_loop_level; i++)
21733 *p++ = '[';
21734 *p = 0;
21735 return decode_mode_spec_buf;
21736 }
21737
21738 case ']':
21739 {
21740 int i;
21741 char *p;
21742
21743 if (command_loop_level > 5)
21744 return " ...]]]";
21745 p = decode_mode_spec_buf;
21746 for (i = 0; i < command_loop_level; i++)
21747 *p++ = ']';
21748 *p = 0;
21749 return decode_mode_spec_buf;
21750 }
21751
21752 case '-':
21753 {
21754 register int i;
21755
21756 /* Let lots_of_dashes be a string of infinite length. */
21757 if (mode_line_target == MODE_LINE_NOPROP
21758 || mode_line_target == MODE_LINE_STRING)
21759 return "--";
21760 if (field_width <= 0
21761 || field_width > sizeof (lots_of_dashes))
21762 {
21763 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21764 decode_mode_spec_buf[i] = '-';
21765 decode_mode_spec_buf[i] = '\0';
21766 return decode_mode_spec_buf;
21767 }
21768 else
21769 return lots_of_dashes;
21770 }
21771
21772 case 'b':
21773 obj = BVAR (b, name);
21774 break;
21775
21776 case 'c':
21777 /* %c and %l are ignored in `frame-title-format'.
21778 (In redisplay_internal, the frame title is drawn _before_ the
21779 windows are updated, so the stuff which depends on actual
21780 window contents (such as %l) may fail to render properly, or
21781 even crash emacs.) */
21782 if (mode_line_target == MODE_LINE_TITLE)
21783 return "";
21784 else
21785 {
21786 ptrdiff_t col = current_column ();
21787 w->column_number_displayed = col;
21788 pint2str (decode_mode_spec_buf, width, col);
21789 return decode_mode_spec_buf;
21790 }
21791
21792 case 'e':
21793 #ifndef SYSTEM_MALLOC
21794 {
21795 if (NILP (Vmemory_full))
21796 return "";
21797 else
21798 return "!MEM FULL! ";
21799 }
21800 #else
21801 return "";
21802 #endif
21803
21804 case 'F':
21805 /* %F displays the frame name. */
21806 if (!NILP (f->title))
21807 return SSDATA (f->title);
21808 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21809 return SSDATA (f->name);
21810 return "Emacs";
21811
21812 case 'f':
21813 obj = BVAR (b, filename);
21814 break;
21815
21816 case 'i':
21817 {
21818 ptrdiff_t size = ZV - BEGV;
21819 pint2str (decode_mode_spec_buf, width, size);
21820 return decode_mode_spec_buf;
21821 }
21822
21823 case 'I':
21824 {
21825 ptrdiff_t size = ZV - BEGV;
21826 pint2hrstr (decode_mode_spec_buf, width, size);
21827 return decode_mode_spec_buf;
21828 }
21829
21830 case 'l':
21831 {
21832 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21833 ptrdiff_t topline, nlines, height;
21834 ptrdiff_t junk;
21835
21836 /* %c and %l are ignored in `frame-title-format'. */
21837 if (mode_line_target == MODE_LINE_TITLE)
21838 return "";
21839
21840 startpos = marker_position (w->start);
21841 startpos_byte = marker_byte_position (w->start);
21842 height = WINDOW_TOTAL_LINES (w);
21843
21844 /* If we decided that this buffer isn't suitable for line numbers,
21845 don't forget that too fast. */
21846 if (w->base_line_pos == -1)
21847 goto no_value;
21848
21849 /* If the buffer is very big, don't waste time. */
21850 if (INTEGERP (Vline_number_display_limit)
21851 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21852 {
21853 w->base_line_pos = 0;
21854 w->base_line_number = 0;
21855 goto no_value;
21856 }
21857
21858 if (w->base_line_number > 0
21859 && w->base_line_pos > 0
21860 && w->base_line_pos <= startpos)
21861 {
21862 line = w->base_line_number;
21863 linepos = w->base_line_pos;
21864 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21865 }
21866 else
21867 {
21868 line = 1;
21869 linepos = BUF_BEGV (b);
21870 linepos_byte = BUF_BEGV_BYTE (b);
21871 }
21872
21873 /* Count lines from base line to window start position. */
21874 nlines = display_count_lines (linepos_byte,
21875 startpos_byte,
21876 startpos, &junk);
21877
21878 topline = nlines + line;
21879
21880 /* Determine a new base line, if the old one is too close
21881 or too far away, or if we did not have one.
21882 "Too close" means it's plausible a scroll-down would
21883 go back past it. */
21884 if (startpos == BUF_BEGV (b))
21885 {
21886 w->base_line_number = topline;
21887 w->base_line_pos = BUF_BEGV (b);
21888 }
21889 else if (nlines < height + 25 || nlines > height * 3 + 50
21890 || linepos == BUF_BEGV (b))
21891 {
21892 ptrdiff_t limit = BUF_BEGV (b);
21893 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21894 ptrdiff_t position;
21895 ptrdiff_t distance =
21896 (height * 2 + 30) * line_number_display_limit_width;
21897
21898 if (startpos - distance > limit)
21899 {
21900 limit = startpos - distance;
21901 limit_byte = CHAR_TO_BYTE (limit);
21902 }
21903
21904 nlines = display_count_lines (startpos_byte,
21905 limit_byte,
21906 - (height * 2 + 30),
21907 &position);
21908 /* If we couldn't find the lines we wanted within
21909 line_number_display_limit_width chars per line,
21910 give up on line numbers for this window. */
21911 if (position == limit_byte && limit == startpos - distance)
21912 {
21913 w->base_line_pos = -1;
21914 w->base_line_number = 0;
21915 goto no_value;
21916 }
21917
21918 w->base_line_number = topline - nlines;
21919 w->base_line_pos = BYTE_TO_CHAR (position);
21920 }
21921
21922 /* Now count lines from the start pos to point. */
21923 nlines = display_count_lines (startpos_byte,
21924 PT_BYTE, PT, &junk);
21925
21926 /* Record that we did display the line number. */
21927 line_number_displayed = 1;
21928
21929 /* Make the string to show. */
21930 pint2str (decode_mode_spec_buf, width, topline + nlines);
21931 return decode_mode_spec_buf;
21932 no_value:
21933 {
21934 char* p = decode_mode_spec_buf;
21935 int pad = width - 2;
21936 while (pad-- > 0)
21937 *p++ = ' ';
21938 *p++ = '?';
21939 *p++ = '?';
21940 *p = '\0';
21941 return decode_mode_spec_buf;
21942 }
21943 }
21944 break;
21945
21946 case 'm':
21947 obj = BVAR (b, mode_name);
21948 break;
21949
21950 case 'n':
21951 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21952 return " Narrow";
21953 break;
21954
21955 case 'p':
21956 {
21957 ptrdiff_t pos = marker_position (w->start);
21958 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21959
21960 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
21961 {
21962 if (pos <= BUF_BEGV (b))
21963 return "All";
21964 else
21965 return "Bottom";
21966 }
21967 else if (pos <= BUF_BEGV (b))
21968 return "Top";
21969 else
21970 {
21971 if (total > 1000000)
21972 /* Do it differently for a large value, to avoid overflow. */
21973 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21974 else
21975 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21976 /* We can't normally display a 3-digit number,
21977 so get us a 2-digit number that is close. */
21978 if (total == 100)
21979 total = 99;
21980 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21981 return decode_mode_spec_buf;
21982 }
21983 }
21984
21985 /* Display percentage of size above the bottom of the screen. */
21986 case 'P':
21987 {
21988 ptrdiff_t toppos = marker_position (w->start);
21989 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
21990 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21991
21992 if (botpos >= BUF_ZV (b))
21993 {
21994 if (toppos <= BUF_BEGV (b))
21995 return "All";
21996 else
21997 return "Bottom";
21998 }
21999 else
22000 {
22001 if (total > 1000000)
22002 /* Do it differently for a large value, to avoid overflow. */
22003 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22004 else
22005 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
22006 /* We can't normally display a 3-digit number,
22007 so get us a 2-digit number that is close. */
22008 if (total == 100)
22009 total = 99;
22010 if (toppos <= BUF_BEGV (b))
22011 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
22012 else
22013 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22014 return decode_mode_spec_buf;
22015 }
22016 }
22017
22018 case 's':
22019 /* status of process */
22020 obj = Fget_buffer_process (Fcurrent_buffer ());
22021 if (NILP (obj))
22022 return "no process";
22023 #ifndef MSDOS
22024 obj = Fsymbol_name (Fprocess_status (obj));
22025 #endif
22026 break;
22027
22028 case '@':
22029 {
22030 ptrdiff_t count = inhibit_garbage_collection ();
22031 Lisp_Object val = call1 (intern ("file-remote-p"),
22032 BVAR (current_buffer, directory));
22033 unbind_to (count, Qnil);
22034
22035 if (NILP (val))
22036 return "-";
22037 else
22038 return "@";
22039 }
22040
22041 case 'z':
22042 /* coding-system (not including end-of-line format) */
22043 case 'Z':
22044 /* coding-system (including end-of-line type) */
22045 {
22046 int eol_flag = (c == 'Z');
22047 char *p = decode_mode_spec_buf;
22048
22049 if (! FRAME_WINDOW_P (f))
22050 {
22051 /* No need to mention EOL here--the terminal never needs
22052 to do EOL conversion. */
22053 p = decode_mode_spec_coding (CODING_ID_NAME
22054 (FRAME_KEYBOARD_CODING (f)->id),
22055 p, 0);
22056 p = decode_mode_spec_coding (CODING_ID_NAME
22057 (FRAME_TERMINAL_CODING (f)->id),
22058 p, 0);
22059 }
22060 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
22061 p, eol_flag);
22062
22063 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
22064 #ifdef subprocesses
22065 obj = Fget_buffer_process (Fcurrent_buffer ());
22066 if (PROCESSP (obj))
22067 {
22068 p = decode_mode_spec_coding
22069 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
22070 p = decode_mode_spec_coding
22071 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
22072 }
22073 #endif /* subprocesses */
22074 #endif /* 0 */
22075 *p = 0;
22076 return decode_mode_spec_buf;
22077 }
22078 }
22079
22080 if (STRINGP (obj))
22081 {
22082 *string = obj;
22083 return SSDATA (obj);
22084 }
22085 else
22086 return "";
22087 }
22088
22089
22090 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
22091 means count lines back from START_BYTE. But don't go beyond
22092 LIMIT_BYTE. Return the number of lines thus found (always
22093 nonnegative).
22094
22095 Set *BYTE_POS_PTR to the byte position where we stopped. This is
22096 either the position COUNT lines after/before START_BYTE, if we
22097 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
22098 COUNT lines. */
22099
22100 static ptrdiff_t
22101 display_count_lines (ptrdiff_t start_byte,
22102 ptrdiff_t limit_byte, ptrdiff_t count,
22103 ptrdiff_t *byte_pos_ptr)
22104 {
22105 register unsigned char *cursor;
22106 unsigned char *base;
22107
22108 register ptrdiff_t ceiling;
22109 register unsigned char *ceiling_addr;
22110 ptrdiff_t orig_count = count;
22111
22112 /* If we are not in selective display mode,
22113 check only for newlines. */
22114 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
22115 && !INTEGERP (BVAR (current_buffer, selective_display)));
22116
22117 if (count > 0)
22118 {
22119 while (start_byte < limit_byte)
22120 {
22121 ceiling = BUFFER_CEILING_OF (start_byte);
22122 ceiling = min (limit_byte - 1, ceiling);
22123 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
22124 base = (cursor = BYTE_POS_ADDR (start_byte));
22125
22126 do
22127 {
22128 if (selective_display)
22129 {
22130 while (*cursor != '\n' && *cursor != 015
22131 && ++cursor != ceiling_addr)
22132 continue;
22133 if (cursor == ceiling_addr)
22134 break;
22135 }
22136 else
22137 {
22138 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
22139 if (! cursor)
22140 break;
22141 }
22142
22143 cursor++;
22144
22145 if (--count == 0)
22146 {
22147 start_byte += cursor - base;
22148 *byte_pos_ptr = start_byte;
22149 return orig_count;
22150 }
22151 }
22152 while (cursor < ceiling_addr);
22153
22154 start_byte += ceiling_addr - base;
22155 }
22156 }
22157 else
22158 {
22159 while (start_byte > limit_byte)
22160 {
22161 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
22162 ceiling = max (limit_byte, ceiling);
22163 ceiling_addr = BYTE_POS_ADDR (ceiling);
22164 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
22165 while (1)
22166 {
22167 if (selective_display)
22168 {
22169 while (--cursor >= ceiling_addr
22170 && *cursor != '\n' && *cursor != 015)
22171 continue;
22172 if (cursor < ceiling_addr)
22173 break;
22174 }
22175 else
22176 {
22177 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
22178 if (! cursor)
22179 break;
22180 }
22181
22182 if (++count == 0)
22183 {
22184 start_byte += cursor - base + 1;
22185 *byte_pos_ptr = start_byte;
22186 /* When scanning backwards, we should
22187 not count the newline posterior to which we stop. */
22188 return - orig_count - 1;
22189 }
22190 }
22191 start_byte += ceiling_addr - base;
22192 }
22193 }
22194
22195 *byte_pos_ptr = limit_byte;
22196
22197 if (count < 0)
22198 return - orig_count + count;
22199 return orig_count - count;
22200
22201 }
22202
22203
22204 \f
22205 /***********************************************************************
22206 Displaying strings
22207 ***********************************************************************/
22208
22209 /* Display a NUL-terminated string, starting with index START.
22210
22211 If STRING is non-null, display that C string. Otherwise, the Lisp
22212 string LISP_STRING is displayed. There's a case that STRING is
22213 non-null and LISP_STRING is not nil. It means STRING is a string
22214 data of LISP_STRING. In that case, we display LISP_STRING while
22215 ignoring its text properties.
22216
22217 If FACE_STRING is not nil, FACE_STRING_POS is a position in
22218 FACE_STRING. Display STRING or LISP_STRING with the face at
22219 FACE_STRING_POS in FACE_STRING:
22220
22221 Display the string in the environment given by IT, but use the
22222 standard display table, temporarily.
22223
22224 FIELD_WIDTH is the minimum number of output glyphs to produce.
22225 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22226 with spaces. If STRING has more characters, more than FIELD_WIDTH
22227 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
22228
22229 PRECISION is the maximum number of characters to output from
22230 STRING. PRECISION < 0 means don't truncate the string.
22231
22232 This is roughly equivalent to printf format specifiers:
22233
22234 FIELD_WIDTH PRECISION PRINTF
22235 ----------------------------------------
22236 -1 -1 %s
22237 -1 10 %.10s
22238 10 -1 %10s
22239 20 10 %20.10s
22240
22241 MULTIBYTE zero means do not display multibyte chars, > 0 means do
22242 display them, and < 0 means obey the current buffer's value of
22243 enable_multibyte_characters.
22244
22245 Value is the number of columns displayed. */
22246
22247 static int
22248 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
22249 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
22250 int field_width, int precision, int max_x, int multibyte)
22251 {
22252 int hpos_at_start = it->hpos;
22253 int saved_face_id = it->face_id;
22254 struct glyph_row *row = it->glyph_row;
22255 ptrdiff_t it_charpos;
22256
22257 /* Initialize the iterator IT for iteration over STRING beginning
22258 with index START. */
22259 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
22260 precision, field_width, multibyte);
22261 if (string && STRINGP (lisp_string))
22262 /* LISP_STRING is the one returned by decode_mode_spec. We should
22263 ignore its text properties. */
22264 it->stop_charpos = it->end_charpos;
22265
22266 /* If displaying STRING, set up the face of the iterator from
22267 FACE_STRING, if that's given. */
22268 if (STRINGP (face_string))
22269 {
22270 ptrdiff_t endptr;
22271 struct face *face;
22272
22273 it->face_id
22274 = face_at_string_position (it->w, face_string, face_string_pos,
22275 0, it->region_beg_charpos,
22276 it->region_end_charpos,
22277 &endptr, it->base_face_id, 0);
22278 face = FACE_FROM_ID (it->f, it->face_id);
22279 it->face_box_p = face->box != FACE_NO_BOX;
22280 }
22281
22282 /* Set max_x to the maximum allowed X position. Don't let it go
22283 beyond the right edge of the window. */
22284 if (max_x <= 0)
22285 max_x = it->last_visible_x;
22286 else
22287 max_x = min (max_x, it->last_visible_x);
22288
22289 /* Skip over display elements that are not visible. because IT->w is
22290 hscrolled. */
22291 if (it->current_x < it->first_visible_x)
22292 move_it_in_display_line_to (it, 100000, it->first_visible_x,
22293 MOVE_TO_POS | MOVE_TO_X);
22294
22295 row->ascent = it->max_ascent;
22296 row->height = it->max_ascent + it->max_descent;
22297 row->phys_ascent = it->max_phys_ascent;
22298 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
22299 row->extra_line_spacing = it->max_extra_line_spacing;
22300
22301 if (STRINGP (it->string))
22302 it_charpos = IT_STRING_CHARPOS (*it);
22303 else
22304 it_charpos = IT_CHARPOS (*it);
22305
22306 /* This condition is for the case that we are called with current_x
22307 past last_visible_x. */
22308 while (it->current_x < max_x)
22309 {
22310 int x_before, x, n_glyphs_before, i, nglyphs;
22311
22312 /* Get the next display element. */
22313 if (!get_next_display_element (it))
22314 break;
22315
22316 /* Produce glyphs. */
22317 x_before = it->current_x;
22318 n_glyphs_before = row->used[TEXT_AREA];
22319 PRODUCE_GLYPHS (it);
22320
22321 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
22322 i = 0;
22323 x = x_before;
22324 while (i < nglyphs)
22325 {
22326 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
22327
22328 if (it->line_wrap != TRUNCATE
22329 && x + glyph->pixel_width > max_x)
22330 {
22331 /* End of continued line or max_x reached. */
22332 if (CHAR_GLYPH_PADDING_P (*glyph))
22333 {
22334 /* A wide character is unbreakable. */
22335 if (row->reversed_p)
22336 unproduce_glyphs (it, row->used[TEXT_AREA]
22337 - n_glyphs_before);
22338 row->used[TEXT_AREA] = n_glyphs_before;
22339 it->current_x = x_before;
22340 }
22341 else
22342 {
22343 if (row->reversed_p)
22344 unproduce_glyphs (it, row->used[TEXT_AREA]
22345 - (n_glyphs_before + i));
22346 row->used[TEXT_AREA] = n_glyphs_before + i;
22347 it->current_x = x;
22348 }
22349 break;
22350 }
22351 else if (x + glyph->pixel_width >= it->first_visible_x)
22352 {
22353 /* Glyph is at least partially visible. */
22354 ++it->hpos;
22355 if (x < it->first_visible_x)
22356 row->x = x - it->first_visible_x;
22357 }
22358 else
22359 {
22360 /* Glyph is off the left margin of the display area.
22361 Should not happen. */
22362 emacs_abort ();
22363 }
22364
22365 row->ascent = max (row->ascent, it->max_ascent);
22366 row->height = max (row->height, it->max_ascent + it->max_descent);
22367 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
22368 row->phys_height = max (row->phys_height,
22369 it->max_phys_ascent + it->max_phys_descent);
22370 row->extra_line_spacing = max (row->extra_line_spacing,
22371 it->max_extra_line_spacing);
22372 x += glyph->pixel_width;
22373 ++i;
22374 }
22375
22376 /* Stop if max_x reached. */
22377 if (i < nglyphs)
22378 break;
22379
22380 /* Stop at line ends. */
22381 if (ITERATOR_AT_END_OF_LINE_P (it))
22382 {
22383 it->continuation_lines_width = 0;
22384 break;
22385 }
22386
22387 set_iterator_to_next (it, 1);
22388 if (STRINGP (it->string))
22389 it_charpos = IT_STRING_CHARPOS (*it);
22390 else
22391 it_charpos = IT_CHARPOS (*it);
22392
22393 /* Stop if truncating at the right edge. */
22394 if (it->line_wrap == TRUNCATE
22395 && it->current_x >= it->last_visible_x)
22396 {
22397 /* Add truncation mark, but don't do it if the line is
22398 truncated at a padding space. */
22399 if (it_charpos < it->string_nchars)
22400 {
22401 if (!FRAME_WINDOW_P (it->f))
22402 {
22403 int ii, n;
22404
22405 if (it->current_x > it->last_visible_x)
22406 {
22407 if (!row->reversed_p)
22408 {
22409 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22410 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22411 break;
22412 }
22413 else
22414 {
22415 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22416 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22417 break;
22418 unproduce_glyphs (it, ii + 1);
22419 ii = row->used[TEXT_AREA] - (ii + 1);
22420 }
22421 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22422 {
22423 row->used[TEXT_AREA] = ii;
22424 produce_special_glyphs (it, IT_TRUNCATION);
22425 }
22426 }
22427 produce_special_glyphs (it, IT_TRUNCATION);
22428 }
22429 row->truncated_on_right_p = 1;
22430 }
22431 break;
22432 }
22433 }
22434
22435 /* Maybe insert a truncation at the left. */
22436 if (it->first_visible_x
22437 && it_charpos > 0)
22438 {
22439 if (!FRAME_WINDOW_P (it->f)
22440 || (row->reversed_p
22441 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22442 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22443 insert_left_trunc_glyphs (it);
22444 row->truncated_on_left_p = 1;
22445 }
22446
22447 it->face_id = saved_face_id;
22448
22449 /* Value is number of columns displayed. */
22450 return it->hpos - hpos_at_start;
22451 }
22452
22453
22454 \f
22455 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22456 appears as an element of LIST or as the car of an element of LIST.
22457 If PROPVAL is a list, compare each element against LIST in that
22458 way, and return 1/2 if any element of PROPVAL is found in LIST.
22459 Otherwise return 0. This function cannot quit.
22460 The return value is 2 if the text is invisible but with an ellipsis
22461 and 1 if it's invisible and without an ellipsis. */
22462
22463 int
22464 invisible_p (register Lisp_Object propval, Lisp_Object list)
22465 {
22466 register Lisp_Object tail, proptail;
22467
22468 for (tail = list; CONSP (tail); tail = XCDR (tail))
22469 {
22470 register Lisp_Object tem;
22471 tem = XCAR (tail);
22472 if (EQ (propval, tem))
22473 return 1;
22474 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22475 return NILP (XCDR (tem)) ? 1 : 2;
22476 }
22477
22478 if (CONSP (propval))
22479 {
22480 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22481 {
22482 Lisp_Object propelt;
22483 propelt = XCAR (proptail);
22484 for (tail = list; CONSP (tail); tail = XCDR (tail))
22485 {
22486 register Lisp_Object tem;
22487 tem = XCAR (tail);
22488 if (EQ (propelt, tem))
22489 return 1;
22490 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22491 return NILP (XCDR (tem)) ? 1 : 2;
22492 }
22493 }
22494 }
22495
22496 return 0;
22497 }
22498
22499 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22500 doc: /* Non-nil if the property makes the text invisible.
22501 POS-OR-PROP can be a marker or number, in which case it is taken to be
22502 a position in the current buffer and the value of the `invisible' property
22503 is checked; or it can be some other value, which is then presumed to be the
22504 value of the `invisible' property of the text of interest.
22505 The non-nil value returned can be t for truly invisible text or something
22506 else if the text is replaced by an ellipsis. */)
22507 (Lisp_Object pos_or_prop)
22508 {
22509 Lisp_Object prop
22510 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22511 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22512 : pos_or_prop);
22513 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22514 return (invis == 0 ? Qnil
22515 : invis == 1 ? Qt
22516 : make_number (invis));
22517 }
22518
22519 /* Calculate a width or height in pixels from a specification using
22520 the following elements:
22521
22522 SPEC ::=
22523 NUM - a (fractional) multiple of the default font width/height
22524 (NUM) - specifies exactly NUM pixels
22525 UNIT - a fixed number of pixels, see below.
22526 ELEMENT - size of a display element in pixels, see below.
22527 (NUM . SPEC) - equals NUM * SPEC
22528 (+ SPEC SPEC ...) - add pixel values
22529 (- SPEC SPEC ...) - subtract pixel values
22530 (- SPEC) - negate pixel value
22531
22532 NUM ::=
22533 INT or FLOAT - a number constant
22534 SYMBOL - use symbol's (buffer local) variable binding.
22535
22536 UNIT ::=
22537 in - pixels per inch *)
22538 mm - pixels per 1/1000 meter *)
22539 cm - pixels per 1/100 meter *)
22540 width - width of current font in pixels.
22541 height - height of current font in pixels.
22542
22543 *) using the ratio(s) defined in display-pixels-per-inch.
22544
22545 ELEMENT ::=
22546
22547 left-fringe - left fringe width in pixels
22548 right-fringe - right fringe width in pixels
22549
22550 left-margin - left margin width in pixels
22551 right-margin - right margin width in pixels
22552
22553 scroll-bar - scroll-bar area width in pixels
22554
22555 Examples:
22556
22557 Pixels corresponding to 5 inches:
22558 (5 . in)
22559
22560 Total width of non-text areas on left side of window (if scroll-bar is on left):
22561 '(space :width (+ left-fringe left-margin scroll-bar))
22562
22563 Align to first text column (in header line):
22564 '(space :align-to 0)
22565
22566 Align to middle of text area minus half the width of variable `my-image'
22567 containing a loaded image:
22568 '(space :align-to (0.5 . (- text my-image)))
22569
22570 Width of left margin minus width of 1 character in the default font:
22571 '(space :width (- left-margin 1))
22572
22573 Width of left margin minus width of 2 characters in the current font:
22574 '(space :width (- left-margin (2 . width)))
22575
22576 Center 1 character over left-margin (in header line):
22577 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22578
22579 Different ways to express width of left fringe plus left margin minus one pixel:
22580 '(space :width (- (+ left-fringe left-margin) (1)))
22581 '(space :width (+ left-fringe left-margin (- (1))))
22582 '(space :width (+ left-fringe left-margin (-1)))
22583
22584 */
22585
22586 static int
22587 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22588 struct font *font, int width_p, int *align_to)
22589 {
22590 double pixels;
22591
22592 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22593 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22594
22595 if (NILP (prop))
22596 return OK_PIXELS (0);
22597
22598 eassert (FRAME_LIVE_P (it->f));
22599
22600 if (SYMBOLP (prop))
22601 {
22602 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22603 {
22604 char *unit = SSDATA (SYMBOL_NAME (prop));
22605
22606 if (unit[0] == 'i' && unit[1] == 'n')
22607 pixels = 1.0;
22608 else if (unit[0] == 'm' && unit[1] == 'm')
22609 pixels = 25.4;
22610 else if (unit[0] == 'c' && unit[1] == 'm')
22611 pixels = 2.54;
22612 else
22613 pixels = 0;
22614 if (pixels > 0)
22615 {
22616 double ppi = (width_p ? FRAME_RES_X (it->f)
22617 : FRAME_RES_Y (it->f));
22618
22619 if (ppi > 0)
22620 return OK_PIXELS (ppi / pixels);
22621 return 0;
22622 }
22623 }
22624
22625 #ifdef HAVE_WINDOW_SYSTEM
22626 if (EQ (prop, Qheight))
22627 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22628 if (EQ (prop, Qwidth))
22629 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22630 #else
22631 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22632 return OK_PIXELS (1);
22633 #endif
22634
22635 if (EQ (prop, Qtext))
22636 return OK_PIXELS (width_p
22637 ? window_box_width (it->w, TEXT_AREA)
22638 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22639
22640 if (align_to && *align_to < 0)
22641 {
22642 *res = 0;
22643 if (EQ (prop, Qleft))
22644 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22645 if (EQ (prop, Qright))
22646 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22647 if (EQ (prop, Qcenter))
22648 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22649 + window_box_width (it->w, TEXT_AREA) / 2);
22650 if (EQ (prop, Qleft_fringe))
22651 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22652 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22653 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22654 if (EQ (prop, Qright_fringe))
22655 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22656 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22657 : window_box_right_offset (it->w, TEXT_AREA));
22658 if (EQ (prop, Qleft_margin))
22659 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22660 if (EQ (prop, Qright_margin))
22661 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22662 if (EQ (prop, Qscroll_bar))
22663 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22664 ? 0
22665 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22666 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22667 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22668 : 0)));
22669 }
22670 else
22671 {
22672 if (EQ (prop, Qleft_fringe))
22673 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22674 if (EQ (prop, Qright_fringe))
22675 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22676 if (EQ (prop, Qleft_margin))
22677 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22678 if (EQ (prop, Qright_margin))
22679 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22680 if (EQ (prop, Qscroll_bar))
22681 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22682 }
22683
22684 prop = buffer_local_value_1 (prop, it->w->contents);
22685 if (EQ (prop, Qunbound))
22686 prop = Qnil;
22687 }
22688
22689 if (INTEGERP (prop) || FLOATP (prop))
22690 {
22691 int base_unit = (width_p
22692 ? FRAME_COLUMN_WIDTH (it->f)
22693 : FRAME_LINE_HEIGHT (it->f));
22694 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22695 }
22696
22697 if (CONSP (prop))
22698 {
22699 Lisp_Object car = XCAR (prop);
22700 Lisp_Object cdr = XCDR (prop);
22701
22702 if (SYMBOLP (car))
22703 {
22704 #ifdef HAVE_WINDOW_SYSTEM
22705 if (FRAME_WINDOW_P (it->f)
22706 && valid_image_p (prop))
22707 {
22708 ptrdiff_t id = lookup_image (it->f, prop);
22709 struct image *img = IMAGE_FROM_ID (it->f, id);
22710
22711 return OK_PIXELS (width_p ? img->width : img->height);
22712 }
22713 #endif
22714 if (EQ (car, Qplus) || EQ (car, Qminus))
22715 {
22716 int first = 1;
22717 double px;
22718
22719 pixels = 0;
22720 while (CONSP (cdr))
22721 {
22722 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22723 font, width_p, align_to))
22724 return 0;
22725 if (first)
22726 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22727 else
22728 pixels += px;
22729 cdr = XCDR (cdr);
22730 }
22731 if (EQ (car, Qminus))
22732 pixels = -pixels;
22733 return OK_PIXELS (pixels);
22734 }
22735
22736 car = buffer_local_value_1 (car, it->w->contents);
22737 if (EQ (car, Qunbound))
22738 car = Qnil;
22739 }
22740
22741 if (INTEGERP (car) || FLOATP (car))
22742 {
22743 double fact;
22744 pixels = XFLOATINT (car);
22745 if (NILP (cdr))
22746 return OK_PIXELS (pixels);
22747 if (calc_pixel_width_or_height (&fact, it, cdr,
22748 font, width_p, align_to))
22749 return OK_PIXELS (pixels * fact);
22750 return 0;
22751 }
22752
22753 return 0;
22754 }
22755
22756 return 0;
22757 }
22758
22759 \f
22760 /***********************************************************************
22761 Glyph Display
22762 ***********************************************************************/
22763
22764 #ifdef HAVE_WINDOW_SYSTEM
22765
22766 #ifdef GLYPH_DEBUG
22767
22768 void
22769 dump_glyph_string (struct glyph_string *s)
22770 {
22771 fprintf (stderr, "glyph string\n");
22772 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22773 s->x, s->y, s->width, s->height);
22774 fprintf (stderr, " ybase = %d\n", s->ybase);
22775 fprintf (stderr, " hl = %d\n", s->hl);
22776 fprintf (stderr, " left overhang = %d, right = %d\n",
22777 s->left_overhang, s->right_overhang);
22778 fprintf (stderr, " nchars = %d\n", s->nchars);
22779 fprintf (stderr, " extends to end of line = %d\n",
22780 s->extends_to_end_of_line_p);
22781 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22782 fprintf (stderr, " bg width = %d\n", s->background_width);
22783 }
22784
22785 #endif /* GLYPH_DEBUG */
22786
22787 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22788 of XChar2b structures for S; it can't be allocated in
22789 init_glyph_string because it must be allocated via `alloca'. W
22790 is the window on which S is drawn. ROW and AREA are the glyph row
22791 and area within the row from which S is constructed. START is the
22792 index of the first glyph structure covered by S. HL is a
22793 face-override for drawing S. */
22794
22795 #ifdef HAVE_NTGUI
22796 #define OPTIONAL_HDC(hdc) HDC hdc,
22797 #define DECLARE_HDC(hdc) HDC hdc;
22798 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22799 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22800 #endif
22801
22802 #ifndef OPTIONAL_HDC
22803 #define OPTIONAL_HDC(hdc)
22804 #define DECLARE_HDC(hdc)
22805 #define ALLOCATE_HDC(hdc, f)
22806 #define RELEASE_HDC(hdc, f)
22807 #endif
22808
22809 static void
22810 init_glyph_string (struct glyph_string *s,
22811 OPTIONAL_HDC (hdc)
22812 XChar2b *char2b, struct window *w, struct glyph_row *row,
22813 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22814 {
22815 memset (s, 0, sizeof *s);
22816 s->w = w;
22817 s->f = XFRAME (w->frame);
22818 #ifdef HAVE_NTGUI
22819 s->hdc = hdc;
22820 #endif
22821 s->display = FRAME_X_DISPLAY (s->f);
22822 s->window = FRAME_X_WINDOW (s->f);
22823 s->char2b = char2b;
22824 s->hl = hl;
22825 s->row = row;
22826 s->area = area;
22827 s->first_glyph = row->glyphs[area] + start;
22828 s->height = row->height;
22829 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22830 s->ybase = s->y + row->ascent;
22831 }
22832
22833
22834 /* Append the list of glyph strings with head H and tail T to the list
22835 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22836
22837 static void
22838 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22839 struct glyph_string *h, struct glyph_string *t)
22840 {
22841 if (h)
22842 {
22843 if (*head)
22844 (*tail)->next = h;
22845 else
22846 *head = h;
22847 h->prev = *tail;
22848 *tail = t;
22849 }
22850 }
22851
22852
22853 /* Prepend the list of glyph strings with head H and tail T to the
22854 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22855 result. */
22856
22857 static void
22858 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22859 struct glyph_string *h, struct glyph_string *t)
22860 {
22861 if (h)
22862 {
22863 if (*head)
22864 (*head)->prev = t;
22865 else
22866 *tail = t;
22867 t->next = *head;
22868 *head = h;
22869 }
22870 }
22871
22872
22873 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22874 Set *HEAD and *TAIL to the resulting list. */
22875
22876 static void
22877 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22878 struct glyph_string *s)
22879 {
22880 s->next = s->prev = NULL;
22881 append_glyph_string_lists (head, tail, s, s);
22882 }
22883
22884
22885 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22886 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22887 make sure that X resources for the face returned are allocated.
22888 Value is a pointer to a realized face that is ready for display if
22889 DISPLAY_P is non-zero. */
22890
22891 static struct face *
22892 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22893 XChar2b *char2b, int display_p)
22894 {
22895 struct face *face = FACE_FROM_ID (f, face_id);
22896 unsigned code = 0;
22897
22898 if (face->font)
22899 {
22900 code = face->font->driver->encode_char (face->font, c);
22901
22902 if (code == FONT_INVALID_CODE)
22903 code = 0;
22904 }
22905 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22906
22907 /* Make sure X resources of the face are allocated. */
22908 #ifdef HAVE_X_WINDOWS
22909 if (display_p)
22910 #endif
22911 {
22912 eassert (face != NULL);
22913 PREPARE_FACE_FOR_DISPLAY (f, face);
22914 }
22915
22916 return face;
22917 }
22918
22919
22920 /* Get face and two-byte form of character glyph GLYPH on frame F.
22921 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22922 a pointer to a realized face that is ready for display. */
22923
22924 static struct face *
22925 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22926 XChar2b *char2b, int *two_byte_p)
22927 {
22928 struct face *face;
22929 unsigned code = 0;
22930
22931 eassert (glyph->type == CHAR_GLYPH);
22932 face = FACE_FROM_ID (f, glyph->face_id);
22933
22934 /* Make sure X resources of the face are allocated. */
22935 eassert (face != NULL);
22936 PREPARE_FACE_FOR_DISPLAY (f, face);
22937
22938 if (two_byte_p)
22939 *two_byte_p = 0;
22940
22941 if (face->font)
22942 {
22943 if (CHAR_BYTE8_P (glyph->u.ch))
22944 code = CHAR_TO_BYTE8 (glyph->u.ch);
22945 else
22946 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22947
22948 if (code == FONT_INVALID_CODE)
22949 code = 0;
22950 }
22951
22952 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22953 return face;
22954 }
22955
22956
22957 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22958 Return 1 if FONT has a glyph for C, otherwise return 0. */
22959
22960 static int
22961 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22962 {
22963 unsigned code;
22964
22965 if (CHAR_BYTE8_P (c))
22966 code = CHAR_TO_BYTE8 (c);
22967 else
22968 code = font->driver->encode_char (font, c);
22969
22970 if (code == FONT_INVALID_CODE)
22971 return 0;
22972 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22973 return 1;
22974 }
22975
22976
22977 /* Fill glyph string S with composition components specified by S->cmp.
22978
22979 BASE_FACE is the base face of the composition.
22980 S->cmp_from is the index of the first component for S.
22981
22982 OVERLAPS non-zero means S should draw the foreground only, and use
22983 its physical height for clipping. See also draw_glyphs.
22984
22985 Value is the index of a component not in S. */
22986
22987 static int
22988 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
22989 int overlaps)
22990 {
22991 int i;
22992 /* For all glyphs of this composition, starting at the offset
22993 S->cmp_from, until we reach the end of the definition or encounter a
22994 glyph that requires the different face, add it to S. */
22995 struct face *face;
22996
22997 eassert (s);
22998
22999 s->for_overlaps = overlaps;
23000 s->face = NULL;
23001 s->font = NULL;
23002 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
23003 {
23004 int c = COMPOSITION_GLYPH (s->cmp, i);
23005
23006 /* TAB in a composition means display glyphs with padding space
23007 on the left or right. */
23008 if (c != '\t')
23009 {
23010 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
23011 -1, Qnil);
23012
23013 face = get_char_face_and_encoding (s->f, c, face_id,
23014 s->char2b + i, 1);
23015 if (face)
23016 {
23017 if (! s->face)
23018 {
23019 s->face = face;
23020 s->font = s->face->font;
23021 }
23022 else if (s->face != face)
23023 break;
23024 }
23025 }
23026 ++s->nchars;
23027 }
23028 s->cmp_to = i;
23029
23030 if (s->face == NULL)
23031 {
23032 s->face = base_face->ascii_face;
23033 s->font = s->face->font;
23034 }
23035
23036 /* All glyph strings for the same composition has the same width,
23037 i.e. the width set for the first component of the composition. */
23038 s->width = s->first_glyph->pixel_width;
23039
23040 /* If the specified font could not be loaded, use the frame's
23041 default font, but record the fact that we couldn't load it in
23042 the glyph string so that we can draw rectangles for the
23043 characters of the glyph string. */
23044 if (s->font == NULL)
23045 {
23046 s->font_not_found_p = 1;
23047 s->font = FRAME_FONT (s->f);
23048 }
23049
23050 /* Adjust base line for subscript/superscript text. */
23051 s->ybase += s->first_glyph->voffset;
23052
23053 /* This glyph string must always be drawn with 16-bit functions. */
23054 s->two_byte_p = 1;
23055
23056 return s->cmp_to;
23057 }
23058
23059 static int
23060 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
23061 int start, int end, int overlaps)
23062 {
23063 struct glyph *glyph, *last;
23064 Lisp_Object lgstring;
23065 int i;
23066
23067 s->for_overlaps = overlaps;
23068 glyph = s->row->glyphs[s->area] + start;
23069 last = s->row->glyphs[s->area] + end;
23070 s->cmp_id = glyph->u.cmp.id;
23071 s->cmp_from = glyph->slice.cmp.from;
23072 s->cmp_to = glyph->slice.cmp.to + 1;
23073 s->face = FACE_FROM_ID (s->f, face_id);
23074 lgstring = composition_gstring_from_id (s->cmp_id);
23075 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
23076 glyph++;
23077 while (glyph < last
23078 && glyph->u.cmp.automatic
23079 && glyph->u.cmp.id == s->cmp_id
23080 && s->cmp_to == glyph->slice.cmp.from)
23081 s->cmp_to = (glyph++)->slice.cmp.to + 1;
23082
23083 for (i = s->cmp_from; i < s->cmp_to; i++)
23084 {
23085 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
23086 unsigned code = LGLYPH_CODE (lglyph);
23087
23088 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
23089 }
23090 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
23091 return glyph - s->row->glyphs[s->area];
23092 }
23093
23094
23095 /* Fill glyph string S from a sequence glyphs for glyphless characters.
23096 See the comment of fill_glyph_string for arguments.
23097 Value is the index of the first glyph not in S. */
23098
23099
23100 static int
23101 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
23102 int start, int end, int overlaps)
23103 {
23104 struct glyph *glyph, *last;
23105 int voffset;
23106
23107 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
23108 s->for_overlaps = overlaps;
23109 glyph = s->row->glyphs[s->area] + start;
23110 last = s->row->glyphs[s->area] + end;
23111 voffset = glyph->voffset;
23112 s->face = FACE_FROM_ID (s->f, face_id);
23113 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
23114 s->nchars = 1;
23115 s->width = glyph->pixel_width;
23116 glyph++;
23117 while (glyph < last
23118 && glyph->type == GLYPHLESS_GLYPH
23119 && glyph->voffset == voffset
23120 && glyph->face_id == face_id)
23121 {
23122 s->nchars++;
23123 s->width += glyph->pixel_width;
23124 glyph++;
23125 }
23126 s->ybase += voffset;
23127 return glyph - s->row->glyphs[s->area];
23128 }
23129
23130
23131 /* Fill glyph string S from a sequence of character glyphs.
23132
23133 FACE_ID is the face id of the string. START is the index of the
23134 first glyph to consider, END is the index of the last + 1.
23135 OVERLAPS non-zero means S should draw the foreground only, and use
23136 its physical height for clipping. See also draw_glyphs.
23137
23138 Value is the index of the first glyph not in S. */
23139
23140 static int
23141 fill_glyph_string (struct glyph_string *s, int face_id,
23142 int start, int end, int overlaps)
23143 {
23144 struct glyph *glyph, *last;
23145 int voffset;
23146 int glyph_not_available_p;
23147
23148 eassert (s->f == XFRAME (s->w->frame));
23149 eassert (s->nchars == 0);
23150 eassert (start >= 0 && end > start);
23151
23152 s->for_overlaps = overlaps;
23153 glyph = s->row->glyphs[s->area] + start;
23154 last = s->row->glyphs[s->area] + end;
23155 voffset = glyph->voffset;
23156 s->padding_p = glyph->padding_p;
23157 glyph_not_available_p = glyph->glyph_not_available_p;
23158
23159 while (glyph < last
23160 && glyph->type == CHAR_GLYPH
23161 && glyph->voffset == voffset
23162 /* Same face id implies same font, nowadays. */
23163 && glyph->face_id == face_id
23164 && glyph->glyph_not_available_p == glyph_not_available_p)
23165 {
23166 int two_byte_p;
23167
23168 s->face = get_glyph_face_and_encoding (s->f, glyph,
23169 s->char2b + s->nchars,
23170 &two_byte_p);
23171 s->two_byte_p = two_byte_p;
23172 ++s->nchars;
23173 eassert (s->nchars <= end - start);
23174 s->width += glyph->pixel_width;
23175 if (glyph++->padding_p != s->padding_p)
23176 break;
23177 }
23178
23179 s->font = s->face->font;
23180
23181 /* If the specified font could not be loaded, use the frame's font,
23182 but record the fact that we couldn't load it in
23183 S->font_not_found_p so that we can draw rectangles for the
23184 characters of the glyph string. */
23185 if (s->font == NULL || glyph_not_available_p)
23186 {
23187 s->font_not_found_p = 1;
23188 s->font = FRAME_FONT (s->f);
23189 }
23190
23191 /* Adjust base line for subscript/superscript text. */
23192 s->ybase += voffset;
23193
23194 eassert (s->face && s->face->gc);
23195 return glyph - s->row->glyphs[s->area];
23196 }
23197
23198
23199 /* Fill glyph string S from image glyph S->first_glyph. */
23200
23201 static void
23202 fill_image_glyph_string (struct glyph_string *s)
23203 {
23204 eassert (s->first_glyph->type == IMAGE_GLYPH);
23205 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
23206 eassert (s->img);
23207 s->slice = s->first_glyph->slice.img;
23208 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
23209 s->font = s->face->font;
23210 s->width = s->first_glyph->pixel_width;
23211
23212 /* Adjust base line for subscript/superscript text. */
23213 s->ybase += s->first_glyph->voffset;
23214 }
23215
23216
23217 /* Fill glyph string S from a sequence of stretch glyphs.
23218
23219 START is the index of the first glyph to consider,
23220 END is the index of the last + 1.
23221
23222 Value is the index of the first glyph not in S. */
23223
23224 static int
23225 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
23226 {
23227 struct glyph *glyph, *last;
23228 int voffset, face_id;
23229
23230 eassert (s->first_glyph->type == STRETCH_GLYPH);
23231
23232 glyph = s->row->glyphs[s->area] + start;
23233 last = s->row->glyphs[s->area] + end;
23234 face_id = glyph->face_id;
23235 s->face = FACE_FROM_ID (s->f, face_id);
23236 s->font = s->face->font;
23237 s->width = glyph->pixel_width;
23238 s->nchars = 1;
23239 voffset = glyph->voffset;
23240
23241 for (++glyph;
23242 (glyph < last
23243 && glyph->type == STRETCH_GLYPH
23244 && glyph->voffset == voffset
23245 && glyph->face_id == face_id);
23246 ++glyph)
23247 s->width += glyph->pixel_width;
23248
23249 /* Adjust base line for subscript/superscript text. */
23250 s->ybase += voffset;
23251
23252 /* The case that face->gc == 0 is handled when drawing the glyph
23253 string by calling PREPARE_FACE_FOR_DISPLAY. */
23254 eassert (s->face);
23255 return glyph - s->row->glyphs[s->area];
23256 }
23257
23258 static struct font_metrics *
23259 get_per_char_metric (struct font *font, XChar2b *char2b)
23260 {
23261 static struct font_metrics metrics;
23262 unsigned code;
23263
23264 if (! font)
23265 return NULL;
23266 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
23267 if (code == FONT_INVALID_CODE)
23268 return NULL;
23269 font->driver->text_extents (font, &code, 1, &metrics);
23270 return &metrics;
23271 }
23272
23273 /* EXPORT for RIF:
23274 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
23275 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
23276 assumed to be zero. */
23277
23278 void
23279 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
23280 {
23281 *left = *right = 0;
23282
23283 if (glyph->type == CHAR_GLYPH)
23284 {
23285 struct face *face;
23286 XChar2b char2b;
23287 struct font_metrics *pcm;
23288
23289 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
23290 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
23291 {
23292 if (pcm->rbearing > pcm->width)
23293 *right = pcm->rbearing - pcm->width;
23294 if (pcm->lbearing < 0)
23295 *left = -pcm->lbearing;
23296 }
23297 }
23298 else if (glyph->type == COMPOSITE_GLYPH)
23299 {
23300 if (! glyph->u.cmp.automatic)
23301 {
23302 struct composition *cmp = composition_table[glyph->u.cmp.id];
23303
23304 if (cmp->rbearing > cmp->pixel_width)
23305 *right = cmp->rbearing - cmp->pixel_width;
23306 if (cmp->lbearing < 0)
23307 *left = - cmp->lbearing;
23308 }
23309 else
23310 {
23311 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
23312 struct font_metrics metrics;
23313
23314 composition_gstring_width (gstring, glyph->slice.cmp.from,
23315 glyph->slice.cmp.to + 1, &metrics);
23316 if (metrics.rbearing > metrics.width)
23317 *right = metrics.rbearing - metrics.width;
23318 if (metrics.lbearing < 0)
23319 *left = - metrics.lbearing;
23320 }
23321 }
23322 }
23323
23324
23325 /* Return the index of the first glyph preceding glyph string S that
23326 is overwritten by S because of S's left overhang. Value is -1
23327 if no glyphs are overwritten. */
23328
23329 static int
23330 left_overwritten (struct glyph_string *s)
23331 {
23332 int k;
23333
23334 if (s->left_overhang)
23335 {
23336 int x = 0, i;
23337 struct glyph *glyphs = s->row->glyphs[s->area];
23338 int first = s->first_glyph - glyphs;
23339
23340 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
23341 x -= glyphs[i].pixel_width;
23342
23343 k = i + 1;
23344 }
23345 else
23346 k = -1;
23347
23348 return k;
23349 }
23350
23351
23352 /* Return the index of the first glyph preceding glyph string S that
23353 is overwriting S because of its right overhang. Value is -1 if no
23354 glyph in front of S overwrites S. */
23355
23356 static int
23357 left_overwriting (struct glyph_string *s)
23358 {
23359 int i, k, x;
23360 struct glyph *glyphs = s->row->glyphs[s->area];
23361 int first = s->first_glyph - glyphs;
23362
23363 k = -1;
23364 x = 0;
23365 for (i = first - 1; i >= 0; --i)
23366 {
23367 int left, right;
23368 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23369 if (x + right > 0)
23370 k = i;
23371 x -= glyphs[i].pixel_width;
23372 }
23373
23374 return k;
23375 }
23376
23377
23378 /* Return the index of the last glyph following glyph string S that is
23379 overwritten by S because of S's right overhang. Value is -1 if
23380 no such glyph is found. */
23381
23382 static int
23383 right_overwritten (struct glyph_string *s)
23384 {
23385 int k = -1;
23386
23387 if (s->right_overhang)
23388 {
23389 int x = 0, i;
23390 struct glyph *glyphs = s->row->glyphs[s->area];
23391 int first = (s->first_glyph - glyphs
23392 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23393 int end = s->row->used[s->area];
23394
23395 for (i = first; i < end && s->right_overhang > x; ++i)
23396 x += glyphs[i].pixel_width;
23397
23398 k = i;
23399 }
23400
23401 return k;
23402 }
23403
23404
23405 /* Return the index of the last glyph following glyph string S that
23406 overwrites S because of its left overhang. Value is negative
23407 if no such glyph is found. */
23408
23409 static int
23410 right_overwriting (struct glyph_string *s)
23411 {
23412 int i, k, x;
23413 int end = s->row->used[s->area];
23414 struct glyph *glyphs = s->row->glyphs[s->area];
23415 int first = (s->first_glyph - glyphs
23416 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23417
23418 k = -1;
23419 x = 0;
23420 for (i = first; i < end; ++i)
23421 {
23422 int left, right;
23423 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23424 if (x - left < 0)
23425 k = i;
23426 x += glyphs[i].pixel_width;
23427 }
23428
23429 return k;
23430 }
23431
23432
23433 /* Set background width of glyph string S. START is the index of the
23434 first glyph following S. LAST_X is the right-most x-position + 1
23435 in the drawing area. */
23436
23437 static void
23438 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23439 {
23440 /* If the face of this glyph string has to be drawn to the end of
23441 the drawing area, set S->extends_to_end_of_line_p. */
23442
23443 if (start == s->row->used[s->area]
23444 && s->area == TEXT_AREA
23445 && ((s->row->fill_line_p
23446 && (s->hl == DRAW_NORMAL_TEXT
23447 || s->hl == DRAW_IMAGE_RAISED
23448 || s->hl == DRAW_IMAGE_SUNKEN))
23449 || s->hl == DRAW_MOUSE_FACE))
23450 s->extends_to_end_of_line_p = 1;
23451
23452 /* If S extends its face to the end of the line, set its
23453 background_width to the distance to the right edge of the drawing
23454 area. */
23455 if (s->extends_to_end_of_line_p)
23456 s->background_width = last_x - s->x + 1;
23457 else
23458 s->background_width = s->width;
23459 }
23460
23461
23462 /* Compute overhangs and x-positions for glyph string S and its
23463 predecessors, or successors. X is the starting x-position for S.
23464 BACKWARD_P non-zero means process predecessors. */
23465
23466 static void
23467 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23468 {
23469 if (backward_p)
23470 {
23471 while (s)
23472 {
23473 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23474 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23475 x -= s->width;
23476 s->x = x;
23477 s = s->prev;
23478 }
23479 }
23480 else
23481 {
23482 while (s)
23483 {
23484 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23485 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23486 s->x = x;
23487 x += s->width;
23488 s = s->next;
23489 }
23490 }
23491 }
23492
23493
23494
23495 /* The following macros are only called from draw_glyphs below.
23496 They reference the following parameters of that function directly:
23497 `w', `row', `area', and `overlap_p'
23498 as well as the following local variables:
23499 `s', `f', and `hdc' (in W32) */
23500
23501 #ifdef HAVE_NTGUI
23502 /* On W32, silently add local `hdc' variable to argument list of
23503 init_glyph_string. */
23504 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23505 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23506 #else
23507 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23508 init_glyph_string (s, char2b, w, row, area, start, hl)
23509 #endif
23510
23511 /* Add a glyph string for a stretch glyph to the list of strings
23512 between HEAD and TAIL. START is the index of the stretch glyph in
23513 row area AREA of glyph row ROW. END is the index of the last glyph
23514 in that glyph row area. X is the current output position assigned
23515 to the new glyph string constructed. HL overrides that face of the
23516 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23517 is the right-most x-position of the drawing area. */
23518
23519 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23520 and below -- keep them on one line. */
23521 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23522 do \
23523 { \
23524 s = alloca (sizeof *s); \
23525 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23526 START = fill_stretch_glyph_string (s, START, END); \
23527 append_glyph_string (&HEAD, &TAIL, s); \
23528 s->x = (X); \
23529 } \
23530 while (0)
23531
23532
23533 /* Add a glyph string for an image glyph to the list of strings
23534 between HEAD and TAIL. START is the index of the image glyph in
23535 row area AREA of glyph row ROW. END is the index of the last glyph
23536 in that glyph row area. X is the current output position assigned
23537 to the new glyph string constructed. HL overrides that face of the
23538 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23539 is the right-most x-position of the drawing area. */
23540
23541 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23542 do \
23543 { \
23544 s = alloca (sizeof *s); \
23545 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23546 fill_image_glyph_string (s); \
23547 append_glyph_string (&HEAD, &TAIL, s); \
23548 ++START; \
23549 s->x = (X); \
23550 } \
23551 while (0)
23552
23553
23554 /* Add a glyph string for a sequence of character glyphs to the list
23555 of strings between HEAD and TAIL. START is the index of the first
23556 glyph in row area AREA of glyph row ROW that is part of the new
23557 glyph string. END is the index of the last glyph in that glyph row
23558 area. X is the current output position assigned to the new glyph
23559 string constructed. HL overrides that face of the glyph; e.g. it
23560 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23561 right-most x-position of the drawing area. */
23562
23563 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23564 do \
23565 { \
23566 int face_id; \
23567 XChar2b *char2b; \
23568 \
23569 face_id = (row)->glyphs[area][START].face_id; \
23570 \
23571 s = alloca (sizeof *s); \
23572 char2b = alloca ((END - START) * sizeof *char2b); \
23573 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23574 append_glyph_string (&HEAD, &TAIL, s); \
23575 s->x = (X); \
23576 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23577 } \
23578 while (0)
23579
23580
23581 /* Add a glyph string for a composite sequence to the list of strings
23582 between HEAD and TAIL. START is the index of the first glyph in
23583 row area AREA of glyph row ROW that is part of the new glyph
23584 string. END is the index of the last glyph in that glyph row area.
23585 X is the current output position assigned to the new glyph string
23586 constructed. HL overrides that face of the glyph; e.g. it is
23587 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23588 x-position of the drawing area. */
23589
23590 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23591 do { \
23592 int face_id = (row)->glyphs[area][START].face_id; \
23593 struct face *base_face = FACE_FROM_ID (f, face_id); \
23594 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23595 struct composition *cmp = composition_table[cmp_id]; \
23596 XChar2b *char2b; \
23597 struct glyph_string *first_s = NULL; \
23598 int n; \
23599 \
23600 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23601 \
23602 /* Make glyph_strings for each glyph sequence that is drawable by \
23603 the same face, and append them to HEAD/TAIL. */ \
23604 for (n = 0; n < cmp->glyph_len;) \
23605 { \
23606 s = alloca (sizeof *s); \
23607 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23608 append_glyph_string (&(HEAD), &(TAIL), s); \
23609 s->cmp = cmp; \
23610 s->cmp_from = n; \
23611 s->x = (X); \
23612 if (n == 0) \
23613 first_s = s; \
23614 n = fill_composite_glyph_string (s, base_face, overlaps); \
23615 } \
23616 \
23617 ++START; \
23618 s = first_s; \
23619 } while (0)
23620
23621
23622 /* Add a glyph string for a glyph-string sequence to the list of strings
23623 between HEAD and TAIL. */
23624
23625 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23626 do { \
23627 int face_id; \
23628 XChar2b *char2b; \
23629 Lisp_Object gstring; \
23630 \
23631 face_id = (row)->glyphs[area][START].face_id; \
23632 gstring = (composition_gstring_from_id \
23633 ((row)->glyphs[area][START].u.cmp.id)); \
23634 s = alloca (sizeof *s); \
23635 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23636 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23637 append_glyph_string (&(HEAD), &(TAIL), s); \
23638 s->x = (X); \
23639 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23640 } while (0)
23641
23642
23643 /* Add a glyph string for a sequence of glyphless character's glyphs
23644 to the list of strings between HEAD and TAIL. The meanings of
23645 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23646
23647 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23648 do \
23649 { \
23650 int face_id; \
23651 \
23652 face_id = (row)->glyphs[area][START].face_id; \
23653 \
23654 s = alloca (sizeof *s); \
23655 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23656 append_glyph_string (&HEAD, &TAIL, s); \
23657 s->x = (X); \
23658 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23659 overlaps); \
23660 } \
23661 while (0)
23662
23663
23664 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23665 of AREA of glyph row ROW on window W between indices START and END.
23666 HL overrides the face for drawing glyph strings, e.g. it is
23667 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23668 x-positions of the drawing area.
23669
23670 This is an ugly monster macro construct because we must use alloca
23671 to allocate glyph strings (because draw_glyphs can be called
23672 asynchronously). */
23673
23674 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23675 do \
23676 { \
23677 HEAD = TAIL = NULL; \
23678 while (START < END) \
23679 { \
23680 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23681 switch (first_glyph->type) \
23682 { \
23683 case CHAR_GLYPH: \
23684 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23685 HL, X, LAST_X); \
23686 break; \
23687 \
23688 case COMPOSITE_GLYPH: \
23689 if (first_glyph->u.cmp.automatic) \
23690 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23691 HL, X, LAST_X); \
23692 else \
23693 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23694 HL, X, LAST_X); \
23695 break; \
23696 \
23697 case STRETCH_GLYPH: \
23698 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23699 HL, X, LAST_X); \
23700 break; \
23701 \
23702 case IMAGE_GLYPH: \
23703 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23704 HL, X, LAST_X); \
23705 break; \
23706 \
23707 case GLYPHLESS_GLYPH: \
23708 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23709 HL, X, LAST_X); \
23710 break; \
23711 \
23712 default: \
23713 emacs_abort (); \
23714 } \
23715 \
23716 if (s) \
23717 { \
23718 set_glyph_string_background_width (s, START, LAST_X); \
23719 (X) += s->width; \
23720 } \
23721 } \
23722 } while (0)
23723
23724
23725 /* Draw glyphs between START and END in AREA of ROW on window W,
23726 starting at x-position X. X is relative to AREA in W. HL is a
23727 face-override with the following meaning:
23728
23729 DRAW_NORMAL_TEXT draw normally
23730 DRAW_CURSOR draw in cursor face
23731 DRAW_MOUSE_FACE draw in mouse face.
23732 DRAW_INVERSE_VIDEO draw in mode line face
23733 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23734 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23735
23736 If OVERLAPS is non-zero, draw only the foreground of characters and
23737 clip to the physical height of ROW. Non-zero value also defines
23738 the overlapping part to be drawn:
23739
23740 OVERLAPS_PRED overlap with preceding rows
23741 OVERLAPS_SUCC overlap with succeeding rows
23742 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23743 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23744
23745 Value is the x-position reached, relative to AREA of W. */
23746
23747 static int
23748 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23749 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23750 enum draw_glyphs_face hl, int overlaps)
23751 {
23752 struct glyph_string *head, *tail;
23753 struct glyph_string *s;
23754 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23755 int i, j, x_reached, last_x, area_left = 0;
23756 struct frame *f = XFRAME (WINDOW_FRAME (w));
23757 DECLARE_HDC (hdc);
23758
23759 ALLOCATE_HDC (hdc, f);
23760
23761 /* Let's rather be paranoid than getting a SEGV. */
23762 end = min (end, row->used[area]);
23763 start = clip_to_bounds (0, start, end);
23764
23765 /* Translate X to frame coordinates. Set last_x to the right
23766 end of the drawing area. */
23767 if (row->full_width_p)
23768 {
23769 /* X is relative to the left edge of W, without scroll bars
23770 or fringes. */
23771 area_left = WINDOW_LEFT_EDGE_X (w);
23772 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23773 }
23774 else
23775 {
23776 area_left = window_box_left (w, area);
23777 last_x = area_left + window_box_width (w, area);
23778 }
23779 x += area_left;
23780
23781 /* Build a doubly-linked list of glyph_string structures between
23782 head and tail from what we have to draw. Note that the macro
23783 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23784 the reason we use a separate variable `i'. */
23785 i = start;
23786 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23787 if (tail)
23788 x_reached = tail->x + tail->background_width;
23789 else
23790 x_reached = x;
23791
23792 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23793 the row, redraw some glyphs in front or following the glyph
23794 strings built above. */
23795 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23796 {
23797 struct glyph_string *h, *t;
23798 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23799 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23800 int check_mouse_face = 0;
23801 int dummy_x = 0;
23802
23803 /* If mouse highlighting is on, we may need to draw adjacent
23804 glyphs using mouse-face highlighting. */
23805 if (area == TEXT_AREA && row->mouse_face_p
23806 && hlinfo->mouse_face_beg_row >= 0
23807 && hlinfo->mouse_face_end_row >= 0)
23808 {
23809 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
23810
23811 if (row_vpos >= hlinfo->mouse_face_beg_row
23812 && row_vpos <= hlinfo->mouse_face_end_row)
23813 {
23814 check_mouse_face = 1;
23815 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
23816 ? hlinfo->mouse_face_beg_col : 0;
23817 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
23818 ? hlinfo->mouse_face_end_col
23819 : row->used[TEXT_AREA];
23820 }
23821 }
23822
23823 /* Compute overhangs for all glyph strings. */
23824 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23825 for (s = head; s; s = s->next)
23826 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23827
23828 /* Prepend glyph strings for glyphs in front of the first glyph
23829 string that are overwritten because of the first glyph
23830 string's left overhang. The background of all strings
23831 prepended must be drawn because the first glyph string
23832 draws over it. */
23833 i = left_overwritten (head);
23834 if (i >= 0)
23835 {
23836 enum draw_glyphs_face overlap_hl;
23837
23838 /* If this row contains mouse highlighting, attempt to draw
23839 the overlapped glyphs with the correct highlight. This
23840 code fails if the overlap encompasses more than one glyph
23841 and mouse-highlight spans only some of these glyphs.
23842 However, making it work perfectly involves a lot more
23843 code, and I don't know if the pathological case occurs in
23844 practice, so we'll stick to this for now. --- cyd */
23845 if (check_mouse_face
23846 && mouse_beg_col < start && mouse_end_col > i)
23847 overlap_hl = DRAW_MOUSE_FACE;
23848 else
23849 overlap_hl = DRAW_NORMAL_TEXT;
23850
23851 j = i;
23852 BUILD_GLYPH_STRINGS (j, start, h, t,
23853 overlap_hl, dummy_x, last_x);
23854 start = i;
23855 compute_overhangs_and_x (t, head->x, 1);
23856 prepend_glyph_string_lists (&head, &tail, h, t);
23857 clip_head = head;
23858 }
23859
23860 /* Prepend glyph strings for glyphs in front of the first glyph
23861 string that overwrite that glyph string because of their
23862 right overhang. For these strings, only the foreground must
23863 be drawn, because it draws over the glyph string at `head'.
23864 The background must not be drawn because this would overwrite
23865 right overhangs of preceding glyphs for which no glyph
23866 strings exist. */
23867 i = left_overwriting (head);
23868 if (i >= 0)
23869 {
23870 enum draw_glyphs_face overlap_hl;
23871
23872 if (check_mouse_face
23873 && mouse_beg_col < start && mouse_end_col > i)
23874 overlap_hl = DRAW_MOUSE_FACE;
23875 else
23876 overlap_hl = DRAW_NORMAL_TEXT;
23877
23878 clip_head = head;
23879 BUILD_GLYPH_STRINGS (i, start, h, t,
23880 overlap_hl, dummy_x, last_x);
23881 for (s = h; s; s = s->next)
23882 s->background_filled_p = 1;
23883 compute_overhangs_and_x (t, head->x, 1);
23884 prepend_glyph_string_lists (&head, &tail, h, t);
23885 }
23886
23887 /* Append glyphs strings for glyphs following the last glyph
23888 string tail that are overwritten by tail. The background of
23889 these strings has to be drawn because tail's foreground draws
23890 over it. */
23891 i = right_overwritten (tail);
23892 if (i >= 0)
23893 {
23894 enum draw_glyphs_face overlap_hl;
23895
23896 if (check_mouse_face
23897 && mouse_beg_col < i && mouse_end_col > end)
23898 overlap_hl = DRAW_MOUSE_FACE;
23899 else
23900 overlap_hl = DRAW_NORMAL_TEXT;
23901
23902 BUILD_GLYPH_STRINGS (end, i, h, t,
23903 overlap_hl, x, last_x);
23904 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23905 we don't have `end = i;' here. */
23906 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23907 append_glyph_string_lists (&head, &tail, h, t);
23908 clip_tail = tail;
23909 }
23910
23911 /* Append glyph strings for glyphs following the last glyph
23912 string tail that overwrite tail. The foreground of such
23913 glyphs has to be drawn because it writes into the background
23914 of tail. The background must not be drawn because it could
23915 paint over the foreground of following glyphs. */
23916 i = right_overwriting (tail);
23917 if (i >= 0)
23918 {
23919 enum draw_glyphs_face overlap_hl;
23920 if (check_mouse_face
23921 && mouse_beg_col < i && mouse_end_col > end)
23922 overlap_hl = DRAW_MOUSE_FACE;
23923 else
23924 overlap_hl = DRAW_NORMAL_TEXT;
23925
23926 clip_tail = tail;
23927 i++; /* We must include the Ith glyph. */
23928 BUILD_GLYPH_STRINGS (end, i, h, t,
23929 overlap_hl, x, last_x);
23930 for (s = h; s; s = s->next)
23931 s->background_filled_p = 1;
23932 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23933 append_glyph_string_lists (&head, &tail, h, t);
23934 }
23935 if (clip_head || clip_tail)
23936 for (s = head; s; s = s->next)
23937 {
23938 s->clip_head = clip_head;
23939 s->clip_tail = clip_tail;
23940 }
23941 }
23942
23943 /* Draw all strings. */
23944 for (s = head; s; s = s->next)
23945 FRAME_RIF (f)->draw_glyph_string (s);
23946
23947 #ifndef HAVE_NS
23948 /* When focus a sole frame and move horizontally, this sets on_p to 0
23949 causing a failure to erase prev cursor position. */
23950 if (area == TEXT_AREA
23951 && !row->full_width_p
23952 /* When drawing overlapping rows, only the glyph strings'
23953 foreground is drawn, which doesn't erase a cursor
23954 completely. */
23955 && !overlaps)
23956 {
23957 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23958 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23959 : (tail ? tail->x + tail->background_width : x));
23960 x0 -= area_left;
23961 x1 -= area_left;
23962
23963 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23964 row->y, MATRIX_ROW_BOTTOM_Y (row));
23965 }
23966 #endif
23967
23968 /* Value is the x-position up to which drawn, relative to AREA of W.
23969 This doesn't include parts drawn because of overhangs. */
23970 if (row->full_width_p)
23971 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23972 else
23973 x_reached -= area_left;
23974
23975 RELEASE_HDC (hdc, f);
23976
23977 return x_reached;
23978 }
23979
23980 /* Expand row matrix if too narrow. Don't expand if area
23981 is not present. */
23982
23983 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
23984 { \
23985 if (!fonts_changed_p \
23986 && (it->glyph_row->glyphs[area] \
23987 < it->glyph_row->glyphs[area + 1])) \
23988 { \
23989 it->w->ncols_scale_factor++; \
23990 fonts_changed_p = 1; \
23991 } \
23992 }
23993
23994 /* Store one glyph for IT->char_to_display in IT->glyph_row.
23995 Called from x_produce_glyphs when IT->glyph_row is non-null. */
23996
23997 static void
23998 append_glyph (struct it *it)
23999 {
24000 struct glyph *glyph;
24001 enum glyph_row_area area = it->area;
24002
24003 eassert (it->glyph_row);
24004 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
24005
24006 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24007 if (glyph < it->glyph_row->glyphs[area + 1])
24008 {
24009 /* If the glyph row is reversed, we need to prepend the glyph
24010 rather than append it. */
24011 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24012 {
24013 struct glyph *g;
24014
24015 /* Make room for the additional glyph. */
24016 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24017 g[1] = *g;
24018 glyph = it->glyph_row->glyphs[area];
24019 }
24020 glyph->charpos = CHARPOS (it->position);
24021 glyph->object = it->object;
24022 if (it->pixel_width > 0)
24023 {
24024 glyph->pixel_width = it->pixel_width;
24025 glyph->padding_p = 0;
24026 }
24027 else
24028 {
24029 /* Assure at least 1-pixel width. Otherwise, cursor can't
24030 be displayed correctly. */
24031 glyph->pixel_width = 1;
24032 glyph->padding_p = 1;
24033 }
24034 glyph->ascent = it->ascent;
24035 glyph->descent = it->descent;
24036 glyph->voffset = it->voffset;
24037 glyph->type = CHAR_GLYPH;
24038 glyph->avoid_cursor_p = it->avoid_cursor_p;
24039 glyph->multibyte_p = it->multibyte_p;
24040 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24041 {
24042 /* In R2L rows, the left and the right box edges need to be
24043 drawn in reverse direction. */
24044 glyph->right_box_line_p = it->start_of_box_run_p;
24045 glyph->left_box_line_p = it->end_of_box_run_p;
24046 }
24047 else
24048 {
24049 glyph->left_box_line_p = it->start_of_box_run_p;
24050 glyph->right_box_line_p = it->end_of_box_run_p;
24051 }
24052 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24053 || it->phys_descent > it->descent);
24054 glyph->glyph_not_available_p = it->glyph_not_available_p;
24055 glyph->face_id = it->face_id;
24056 glyph->u.ch = it->char_to_display;
24057 glyph->slice.img = null_glyph_slice;
24058 glyph->font_type = FONT_TYPE_UNKNOWN;
24059 if (it->bidi_p)
24060 {
24061 glyph->resolved_level = it->bidi_it.resolved_level;
24062 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24063 emacs_abort ();
24064 glyph->bidi_type = it->bidi_it.type;
24065 }
24066 else
24067 {
24068 glyph->resolved_level = 0;
24069 glyph->bidi_type = UNKNOWN_BT;
24070 }
24071 ++it->glyph_row->used[area];
24072 }
24073 else
24074 IT_EXPAND_MATRIX_WIDTH (it, area);
24075 }
24076
24077 /* Store one glyph for the composition IT->cmp_it.id in
24078 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
24079 non-null. */
24080
24081 static void
24082 append_composite_glyph (struct it *it)
24083 {
24084 struct glyph *glyph;
24085 enum glyph_row_area area = it->area;
24086
24087 eassert (it->glyph_row);
24088
24089 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24090 if (glyph < it->glyph_row->glyphs[area + 1])
24091 {
24092 /* If the glyph row is reversed, we need to prepend the glyph
24093 rather than append it. */
24094 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
24095 {
24096 struct glyph *g;
24097
24098 /* Make room for the new glyph. */
24099 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
24100 g[1] = *g;
24101 glyph = it->glyph_row->glyphs[it->area];
24102 }
24103 glyph->charpos = it->cmp_it.charpos;
24104 glyph->object = it->object;
24105 glyph->pixel_width = it->pixel_width;
24106 glyph->ascent = it->ascent;
24107 glyph->descent = it->descent;
24108 glyph->voffset = it->voffset;
24109 glyph->type = COMPOSITE_GLYPH;
24110 if (it->cmp_it.ch < 0)
24111 {
24112 glyph->u.cmp.automatic = 0;
24113 glyph->u.cmp.id = it->cmp_it.id;
24114 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
24115 }
24116 else
24117 {
24118 glyph->u.cmp.automatic = 1;
24119 glyph->u.cmp.id = it->cmp_it.id;
24120 glyph->slice.cmp.from = it->cmp_it.from;
24121 glyph->slice.cmp.to = it->cmp_it.to - 1;
24122 }
24123 glyph->avoid_cursor_p = it->avoid_cursor_p;
24124 glyph->multibyte_p = it->multibyte_p;
24125 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24126 {
24127 /* In R2L rows, the left and the right box edges need to be
24128 drawn in reverse direction. */
24129 glyph->right_box_line_p = it->start_of_box_run_p;
24130 glyph->left_box_line_p = it->end_of_box_run_p;
24131 }
24132 else
24133 {
24134 glyph->left_box_line_p = it->start_of_box_run_p;
24135 glyph->right_box_line_p = it->end_of_box_run_p;
24136 }
24137 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24138 || it->phys_descent > it->descent);
24139 glyph->padding_p = 0;
24140 glyph->glyph_not_available_p = 0;
24141 glyph->face_id = it->face_id;
24142 glyph->font_type = FONT_TYPE_UNKNOWN;
24143 if (it->bidi_p)
24144 {
24145 glyph->resolved_level = it->bidi_it.resolved_level;
24146 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24147 emacs_abort ();
24148 glyph->bidi_type = it->bidi_it.type;
24149 }
24150 ++it->glyph_row->used[area];
24151 }
24152 else
24153 IT_EXPAND_MATRIX_WIDTH (it, area);
24154 }
24155
24156
24157 /* Change IT->ascent and IT->height according to the setting of
24158 IT->voffset. */
24159
24160 static void
24161 take_vertical_position_into_account (struct it *it)
24162 {
24163 if (it->voffset)
24164 {
24165 if (it->voffset < 0)
24166 /* Increase the ascent so that we can display the text higher
24167 in the line. */
24168 it->ascent -= it->voffset;
24169 else
24170 /* Increase the descent so that we can display the text lower
24171 in the line. */
24172 it->descent += it->voffset;
24173 }
24174 }
24175
24176
24177 /* Produce glyphs/get display metrics for the image IT is loaded with.
24178 See the description of struct display_iterator in dispextern.h for
24179 an overview of struct display_iterator. */
24180
24181 static void
24182 produce_image_glyph (struct it *it)
24183 {
24184 struct image *img;
24185 struct face *face;
24186 int glyph_ascent, crop;
24187 struct glyph_slice slice;
24188
24189 eassert (it->what == IT_IMAGE);
24190
24191 face = FACE_FROM_ID (it->f, it->face_id);
24192 eassert (face);
24193 /* Make sure X resources of the face is loaded. */
24194 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24195
24196 if (it->image_id < 0)
24197 {
24198 /* Fringe bitmap. */
24199 it->ascent = it->phys_ascent = 0;
24200 it->descent = it->phys_descent = 0;
24201 it->pixel_width = 0;
24202 it->nglyphs = 0;
24203 return;
24204 }
24205
24206 img = IMAGE_FROM_ID (it->f, it->image_id);
24207 eassert (img);
24208 /* Make sure X resources of the image is loaded. */
24209 prepare_image_for_display (it->f, img);
24210
24211 slice.x = slice.y = 0;
24212 slice.width = img->width;
24213 slice.height = img->height;
24214
24215 if (INTEGERP (it->slice.x))
24216 slice.x = XINT (it->slice.x);
24217 else if (FLOATP (it->slice.x))
24218 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
24219
24220 if (INTEGERP (it->slice.y))
24221 slice.y = XINT (it->slice.y);
24222 else if (FLOATP (it->slice.y))
24223 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
24224
24225 if (INTEGERP (it->slice.width))
24226 slice.width = XINT (it->slice.width);
24227 else if (FLOATP (it->slice.width))
24228 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
24229
24230 if (INTEGERP (it->slice.height))
24231 slice.height = XINT (it->slice.height);
24232 else if (FLOATP (it->slice.height))
24233 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
24234
24235 if (slice.x >= img->width)
24236 slice.x = img->width;
24237 if (slice.y >= img->height)
24238 slice.y = img->height;
24239 if (slice.x + slice.width >= img->width)
24240 slice.width = img->width - slice.x;
24241 if (slice.y + slice.height > img->height)
24242 slice.height = img->height - slice.y;
24243
24244 if (slice.width == 0 || slice.height == 0)
24245 return;
24246
24247 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
24248
24249 it->descent = slice.height - glyph_ascent;
24250 if (slice.y == 0)
24251 it->descent += img->vmargin;
24252 if (slice.y + slice.height == img->height)
24253 it->descent += img->vmargin;
24254 it->phys_descent = it->descent;
24255
24256 it->pixel_width = slice.width;
24257 if (slice.x == 0)
24258 it->pixel_width += img->hmargin;
24259 if (slice.x + slice.width == img->width)
24260 it->pixel_width += img->hmargin;
24261
24262 /* It's quite possible for images to have an ascent greater than
24263 their height, so don't get confused in that case. */
24264 if (it->descent < 0)
24265 it->descent = 0;
24266
24267 it->nglyphs = 1;
24268
24269 if (face->box != FACE_NO_BOX)
24270 {
24271 if (face->box_line_width > 0)
24272 {
24273 if (slice.y == 0)
24274 it->ascent += face->box_line_width;
24275 if (slice.y + slice.height == img->height)
24276 it->descent += face->box_line_width;
24277 }
24278
24279 if (it->start_of_box_run_p && slice.x == 0)
24280 it->pixel_width += eabs (face->box_line_width);
24281 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
24282 it->pixel_width += eabs (face->box_line_width);
24283 }
24284
24285 take_vertical_position_into_account (it);
24286
24287 /* Automatically crop wide image glyphs at right edge so we can
24288 draw the cursor on same display row. */
24289 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
24290 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
24291 {
24292 it->pixel_width -= crop;
24293 slice.width -= crop;
24294 }
24295
24296 if (it->glyph_row)
24297 {
24298 struct glyph *glyph;
24299 enum glyph_row_area area = it->area;
24300
24301 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24302 if (glyph < it->glyph_row->glyphs[area + 1])
24303 {
24304 glyph->charpos = CHARPOS (it->position);
24305 glyph->object = it->object;
24306 glyph->pixel_width = it->pixel_width;
24307 glyph->ascent = glyph_ascent;
24308 glyph->descent = it->descent;
24309 glyph->voffset = it->voffset;
24310 glyph->type = IMAGE_GLYPH;
24311 glyph->avoid_cursor_p = it->avoid_cursor_p;
24312 glyph->multibyte_p = it->multibyte_p;
24313 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24314 {
24315 /* In R2L rows, the left and the right box edges need to be
24316 drawn in reverse direction. */
24317 glyph->right_box_line_p = it->start_of_box_run_p;
24318 glyph->left_box_line_p = it->end_of_box_run_p;
24319 }
24320 else
24321 {
24322 glyph->left_box_line_p = it->start_of_box_run_p;
24323 glyph->right_box_line_p = it->end_of_box_run_p;
24324 }
24325 glyph->overlaps_vertically_p = 0;
24326 glyph->padding_p = 0;
24327 glyph->glyph_not_available_p = 0;
24328 glyph->face_id = it->face_id;
24329 glyph->u.img_id = img->id;
24330 glyph->slice.img = slice;
24331 glyph->font_type = FONT_TYPE_UNKNOWN;
24332 if (it->bidi_p)
24333 {
24334 glyph->resolved_level = it->bidi_it.resolved_level;
24335 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24336 emacs_abort ();
24337 glyph->bidi_type = it->bidi_it.type;
24338 }
24339 ++it->glyph_row->used[area];
24340 }
24341 else
24342 IT_EXPAND_MATRIX_WIDTH (it, area);
24343 }
24344 }
24345
24346
24347 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
24348 of the glyph, WIDTH and HEIGHT are the width and height of the
24349 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
24350
24351 static void
24352 append_stretch_glyph (struct it *it, Lisp_Object object,
24353 int width, int height, int ascent)
24354 {
24355 struct glyph *glyph;
24356 enum glyph_row_area area = it->area;
24357
24358 eassert (ascent >= 0 && ascent <= height);
24359
24360 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24361 if (glyph < it->glyph_row->glyphs[area + 1])
24362 {
24363 /* If the glyph row is reversed, we need to prepend the glyph
24364 rather than append it. */
24365 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24366 {
24367 struct glyph *g;
24368
24369 /* Make room for the additional glyph. */
24370 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24371 g[1] = *g;
24372 glyph = it->glyph_row->glyphs[area];
24373 }
24374 glyph->charpos = CHARPOS (it->position);
24375 glyph->object = object;
24376 glyph->pixel_width = width;
24377 glyph->ascent = ascent;
24378 glyph->descent = height - ascent;
24379 glyph->voffset = it->voffset;
24380 glyph->type = STRETCH_GLYPH;
24381 glyph->avoid_cursor_p = it->avoid_cursor_p;
24382 glyph->multibyte_p = it->multibyte_p;
24383 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24384 {
24385 /* In R2L rows, the left and the right box edges need to be
24386 drawn in reverse direction. */
24387 glyph->right_box_line_p = it->start_of_box_run_p;
24388 glyph->left_box_line_p = it->end_of_box_run_p;
24389 }
24390 else
24391 {
24392 glyph->left_box_line_p = it->start_of_box_run_p;
24393 glyph->right_box_line_p = it->end_of_box_run_p;
24394 }
24395 glyph->overlaps_vertically_p = 0;
24396 glyph->padding_p = 0;
24397 glyph->glyph_not_available_p = 0;
24398 glyph->face_id = it->face_id;
24399 glyph->u.stretch.ascent = ascent;
24400 glyph->u.stretch.height = height;
24401 glyph->slice.img = null_glyph_slice;
24402 glyph->font_type = FONT_TYPE_UNKNOWN;
24403 if (it->bidi_p)
24404 {
24405 glyph->resolved_level = it->bidi_it.resolved_level;
24406 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24407 emacs_abort ();
24408 glyph->bidi_type = it->bidi_it.type;
24409 }
24410 else
24411 {
24412 glyph->resolved_level = 0;
24413 glyph->bidi_type = UNKNOWN_BT;
24414 }
24415 ++it->glyph_row->used[area];
24416 }
24417 else
24418 IT_EXPAND_MATRIX_WIDTH (it, area);
24419 }
24420
24421 #endif /* HAVE_WINDOW_SYSTEM */
24422
24423 /* Produce a stretch glyph for iterator IT. IT->object is the value
24424 of the glyph property displayed. The value must be a list
24425 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24426 being recognized:
24427
24428 1. `:width WIDTH' specifies that the space should be WIDTH *
24429 canonical char width wide. WIDTH may be an integer or floating
24430 point number.
24431
24432 2. `:relative-width FACTOR' specifies that the width of the stretch
24433 should be computed from the width of the first character having the
24434 `glyph' property, and should be FACTOR times that width.
24435
24436 3. `:align-to HPOS' specifies that the space should be wide enough
24437 to reach HPOS, a value in canonical character units.
24438
24439 Exactly one of the above pairs must be present.
24440
24441 4. `:height HEIGHT' specifies that the height of the stretch produced
24442 should be HEIGHT, measured in canonical character units.
24443
24444 5. `:relative-height FACTOR' specifies that the height of the
24445 stretch should be FACTOR times the height of the characters having
24446 the glyph property.
24447
24448 Either none or exactly one of 4 or 5 must be present.
24449
24450 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24451 of the stretch should be used for the ascent of the stretch.
24452 ASCENT must be in the range 0 <= ASCENT <= 100. */
24453
24454 void
24455 produce_stretch_glyph (struct it *it)
24456 {
24457 /* (space :width WIDTH :height HEIGHT ...) */
24458 Lisp_Object prop, plist;
24459 int width = 0, height = 0, align_to = -1;
24460 int zero_width_ok_p = 0;
24461 double tem;
24462 struct font *font = NULL;
24463
24464 #ifdef HAVE_WINDOW_SYSTEM
24465 int ascent = 0;
24466 int zero_height_ok_p = 0;
24467
24468 if (FRAME_WINDOW_P (it->f))
24469 {
24470 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24471 font = face->font ? face->font : FRAME_FONT (it->f);
24472 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24473 }
24474 #endif
24475
24476 /* List should start with `space'. */
24477 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24478 plist = XCDR (it->object);
24479
24480 /* Compute the width of the stretch. */
24481 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24482 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24483 {
24484 /* Absolute width `:width WIDTH' specified and valid. */
24485 zero_width_ok_p = 1;
24486 width = (int)tem;
24487 }
24488 #ifdef HAVE_WINDOW_SYSTEM
24489 else if (FRAME_WINDOW_P (it->f)
24490 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24491 {
24492 /* Relative width `:relative-width FACTOR' specified and valid.
24493 Compute the width of the characters having the `glyph'
24494 property. */
24495 struct it it2;
24496 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24497
24498 it2 = *it;
24499 if (it->multibyte_p)
24500 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24501 else
24502 {
24503 it2.c = it2.char_to_display = *p, it2.len = 1;
24504 if (! ASCII_CHAR_P (it2.c))
24505 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24506 }
24507
24508 it2.glyph_row = NULL;
24509 it2.what = IT_CHARACTER;
24510 x_produce_glyphs (&it2);
24511 width = NUMVAL (prop) * it2.pixel_width;
24512 }
24513 #endif /* HAVE_WINDOW_SYSTEM */
24514 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24515 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24516 {
24517 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24518 align_to = (align_to < 0
24519 ? 0
24520 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24521 else if (align_to < 0)
24522 align_to = window_box_left_offset (it->w, TEXT_AREA);
24523 width = max (0, (int)tem + align_to - it->current_x);
24524 zero_width_ok_p = 1;
24525 }
24526 else
24527 /* Nothing specified -> width defaults to canonical char width. */
24528 width = FRAME_COLUMN_WIDTH (it->f);
24529
24530 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24531 width = 1;
24532
24533 #ifdef HAVE_WINDOW_SYSTEM
24534 /* Compute height. */
24535 if (FRAME_WINDOW_P (it->f))
24536 {
24537 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24538 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24539 {
24540 height = (int)tem;
24541 zero_height_ok_p = 1;
24542 }
24543 else if (prop = Fplist_get (plist, QCrelative_height),
24544 NUMVAL (prop) > 0)
24545 height = FONT_HEIGHT (font) * NUMVAL (prop);
24546 else
24547 height = FONT_HEIGHT (font);
24548
24549 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24550 height = 1;
24551
24552 /* Compute percentage of height used for ascent. If
24553 `:ascent ASCENT' is present and valid, use that. Otherwise,
24554 derive the ascent from the font in use. */
24555 if (prop = Fplist_get (plist, QCascent),
24556 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24557 ascent = height * NUMVAL (prop) / 100.0;
24558 else if (!NILP (prop)
24559 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24560 ascent = min (max (0, (int)tem), height);
24561 else
24562 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24563 }
24564 else
24565 #endif /* HAVE_WINDOW_SYSTEM */
24566 height = 1;
24567
24568 if (width > 0 && it->line_wrap != TRUNCATE
24569 && it->current_x + width > it->last_visible_x)
24570 {
24571 width = it->last_visible_x - it->current_x;
24572 #ifdef HAVE_WINDOW_SYSTEM
24573 /* Subtract one more pixel from the stretch width, but only on
24574 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24575 width -= FRAME_WINDOW_P (it->f);
24576 #endif
24577 }
24578
24579 if (width > 0 && height > 0 && it->glyph_row)
24580 {
24581 Lisp_Object o_object = it->object;
24582 Lisp_Object object = it->stack[it->sp - 1].string;
24583 int n = width;
24584
24585 if (!STRINGP (object))
24586 object = it->w->contents;
24587 #ifdef HAVE_WINDOW_SYSTEM
24588 if (FRAME_WINDOW_P (it->f))
24589 append_stretch_glyph (it, object, width, height, ascent);
24590 else
24591 #endif
24592 {
24593 it->object = object;
24594 it->char_to_display = ' ';
24595 it->pixel_width = it->len = 1;
24596 while (n--)
24597 tty_append_glyph (it);
24598 it->object = o_object;
24599 }
24600 }
24601
24602 it->pixel_width = width;
24603 #ifdef HAVE_WINDOW_SYSTEM
24604 if (FRAME_WINDOW_P (it->f))
24605 {
24606 it->ascent = it->phys_ascent = ascent;
24607 it->descent = it->phys_descent = height - it->ascent;
24608 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24609 take_vertical_position_into_account (it);
24610 }
24611 else
24612 #endif
24613 it->nglyphs = width;
24614 }
24615
24616 /* Get information about special display element WHAT in an
24617 environment described by IT. WHAT is one of IT_TRUNCATION or
24618 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24619 non-null glyph_row member. This function ensures that fields like
24620 face_id, c, len of IT are left untouched. */
24621
24622 static void
24623 produce_special_glyphs (struct it *it, enum display_element_type what)
24624 {
24625 struct it temp_it;
24626 Lisp_Object gc;
24627 GLYPH glyph;
24628
24629 temp_it = *it;
24630 temp_it.object = make_number (0);
24631 memset (&temp_it.current, 0, sizeof temp_it.current);
24632
24633 if (what == IT_CONTINUATION)
24634 {
24635 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24636 if (it->bidi_it.paragraph_dir == R2L)
24637 SET_GLYPH_FROM_CHAR (glyph, '/');
24638 else
24639 SET_GLYPH_FROM_CHAR (glyph, '\\');
24640 if (it->dp
24641 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24642 {
24643 /* FIXME: Should we mirror GC for R2L lines? */
24644 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24645 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24646 }
24647 }
24648 else if (what == IT_TRUNCATION)
24649 {
24650 /* Truncation glyph. */
24651 SET_GLYPH_FROM_CHAR (glyph, '$');
24652 if (it->dp
24653 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24654 {
24655 /* FIXME: Should we mirror GC for R2L lines? */
24656 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24657 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24658 }
24659 }
24660 else
24661 emacs_abort ();
24662
24663 #ifdef HAVE_WINDOW_SYSTEM
24664 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24665 is turned off, we precede the truncation/continuation glyphs by a
24666 stretch glyph whose width is computed such that these special
24667 glyphs are aligned at the window margin, even when very different
24668 fonts are used in different glyph rows. */
24669 if (FRAME_WINDOW_P (temp_it.f)
24670 /* init_iterator calls this with it->glyph_row == NULL, and it
24671 wants only the pixel width of the truncation/continuation
24672 glyphs. */
24673 && temp_it.glyph_row
24674 /* insert_left_trunc_glyphs calls us at the beginning of the
24675 row, and it has its own calculation of the stretch glyph
24676 width. */
24677 && temp_it.glyph_row->used[TEXT_AREA] > 0
24678 && (temp_it.glyph_row->reversed_p
24679 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24680 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24681 {
24682 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24683
24684 if (stretch_width > 0)
24685 {
24686 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24687 struct font *font =
24688 face->font ? face->font : FRAME_FONT (temp_it.f);
24689 int stretch_ascent =
24690 (((temp_it.ascent + temp_it.descent)
24691 * FONT_BASE (font)) / FONT_HEIGHT (font));
24692
24693 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
24694 temp_it.ascent + temp_it.descent,
24695 stretch_ascent);
24696 }
24697 }
24698 #endif
24699
24700 temp_it.dp = NULL;
24701 temp_it.what = IT_CHARACTER;
24702 temp_it.len = 1;
24703 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24704 temp_it.face_id = GLYPH_FACE (glyph);
24705 temp_it.len = CHAR_BYTES (temp_it.c);
24706
24707 PRODUCE_GLYPHS (&temp_it);
24708 it->pixel_width = temp_it.pixel_width;
24709 it->nglyphs = temp_it.pixel_width;
24710 }
24711
24712 #ifdef HAVE_WINDOW_SYSTEM
24713
24714 /* Calculate line-height and line-spacing properties.
24715 An integer value specifies explicit pixel value.
24716 A float value specifies relative value to current face height.
24717 A cons (float . face-name) specifies relative value to
24718 height of specified face font.
24719
24720 Returns height in pixels, or nil. */
24721
24722
24723 static Lisp_Object
24724 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24725 int boff, int override)
24726 {
24727 Lisp_Object face_name = Qnil;
24728 int ascent, descent, height;
24729
24730 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24731 return val;
24732
24733 if (CONSP (val))
24734 {
24735 face_name = XCAR (val);
24736 val = XCDR (val);
24737 if (!NUMBERP (val))
24738 val = make_number (1);
24739 if (NILP (face_name))
24740 {
24741 height = it->ascent + it->descent;
24742 goto scale;
24743 }
24744 }
24745
24746 if (NILP (face_name))
24747 {
24748 font = FRAME_FONT (it->f);
24749 boff = FRAME_BASELINE_OFFSET (it->f);
24750 }
24751 else if (EQ (face_name, Qt))
24752 {
24753 override = 0;
24754 }
24755 else
24756 {
24757 int face_id;
24758 struct face *face;
24759
24760 face_id = lookup_named_face (it->f, face_name, 0);
24761 if (face_id < 0)
24762 return make_number (-1);
24763
24764 face = FACE_FROM_ID (it->f, face_id);
24765 font = face->font;
24766 if (font == NULL)
24767 return make_number (-1);
24768 boff = font->baseline_offset;
24769 if (font->vertical_centering)
24770 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24771 }
24772
24773 ascent = FONT_BASE (font) + boff;
24774 descent = FONT_DESCENT (font) - boff;
24775
24776 if (override)
24777 {
24778 it->override_ascent = ascent;
24779 it->override_descent = descent;
24780 it->override_boff = boff;
24781 }
24782
24783 height = ascent + descent;
24784
24785 scale:
24786 if (FLOATP (val))
24787 height = (int)(XFLOAT_DATA (val) * height);
24788 else if (INTEGERP (val))
24789 height *= XINT (val);
24790
24791 return make_number (height);
24792 }
24793
24794
24795 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24796 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24797 and only if this is for a character for which no font was found.
24798
24799 If the display method (it->glyphless_method) is
24800 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24801 length of the acronym or the hexadecimal string, UPPER_XOFF and
24802 UPPER_YOFF are pixel offsets for the upper part of the string,
24803 LOWER_XOFF and LOWER_YOFF are for the lower part.
24804
24805 For the other display methods, LEN through LOWER_YOFF are zero. */
24806
24807 static void
24808 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24809 short upper_xoff, short upper_yoff,
24810 short lower_xoff, short lower_yoff)
24811 {
24812 struct glyph *glyph;
24813 enum glyph_row_area area = it->area;
24814
24815 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24816 if (glyph < it->glyph_row->glyphs[area + 1])
24817 {
24818 /* If the glyph row is reversed, we need to prepend the glyph
24819 rather than append it. */
24820 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24821 {
24822 struct glyph *g;
24823
24824 /* Make room for the additional glyph. */
24825 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24826 g[1] = *g;
24827 glyph = it->glyph_row->glyphs[area];
24828 }
24829 glyph->charpos = CHARPOS (it->position);
24830 glyph->object = it->object;
24831 glyph->pixel_width = it->pixel_width;
24832 glyph->ascent = it->ascent;
24833 glyph->descent = it->descent;
24834 glyph->voffset = it->voffset;
24835 glyph->type = GLYPHLESS_GLYPH;
24836 glyph->u.glyphless.method = it->glyphless_method;
24837 glyph->u.glyphless.for_no_font = for_no_font;
24838 glyph->u.glyphless.len = len;
24839 glyph->u.glyphless.ch = it->c;
24840 glyph->slice.glyphless.upper_xoff = upper_xoff;
24841 glyph->slice.glyphless.upper_yoff = upper_yoff;
24842 glyph->slice.glyphless.lower_xoff = lower_xoff;
24843 glyph->slice.glyphless.lower_yoff = lower_yoff;
24844 glyph->avoid_cursor_p = it->avoid_cursor_p;
24845 glyph->multibyte_p = it->multibyte_p;
24846 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24847 {
24848 /* In R2L rows, the left and the right box edges need to be
24849 drawn in reverse direction. */
24850 glyph->right_box_line_p = it->start_of_box_run_p;
24851 glyph->left_box_line_p = it->end_of_box_run_p;
24852 }
24853 else
24854 {
24855 glyph->left_box_line_p = it->start_of_box_run_p;
24856 glyph->right_box_line_p = it->end_of_box_run_p;
24857 }
24858 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24859 || it->phys_descent > it->descent);
24860 glyph->padding_p = 0;
24861 glyph->glyph_not_available_p = 0;
24862 glyph->face_id = face_id;
24863 glyph->font_type = FONT_TYPE_UNKNOWN;
24864 if (it->bidi_p)
24865 {
24866 glyph->resolved_level = it->bidi_it.resolved_level;
24867 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24868 emacs_abort ();
24869 glyph->bidi_type = it->bidi_it.type;
24870 }
24871 ++it->glyph_row->used[area];
24872 }
24873 else
24874 IT_EXPAND_MATRIX_WIDTH (it, area);
24875 }
24876
24877
24878 /* Produce a glyph for a glyphless character for iterator IT.
24879 IT->glyphless_method specifies which method to use for displaying
24880 the character. See the description of enum
24881 glyphless_display_method in dispextern.h for the detail.
24882
24883 FOR_NO_FONT is nonzero if and only if this is for a character for
24884 which no font was found. ACRONYM, if non-nil, is an acronym string
24885 for the character. */
24886
24887 static void
24888 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24889 {
24890 int face_id;
24891 struct face *face;
24892 struct font *font;
24893 int base_width, base_height, width, height;
24894 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24895 int len;
24896
24897 /* Get the metrics of the base font. We always refer to the current
24898 ASCII face. */
24899 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24900 font = face->font ? face->font : FRAME_FONT (it->f);
24901 it->ascent = FONT_BASE (font) + font->baseline_offset;
24902 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24903 base_height = it->ascent + it->descent;
24904 base_width = font->average_width;
24905
24906 /* Get a face ID for the glyph by utilizing a cache (the same way as
24907 done for `escape-glyph' in get_next_display_element). */
24908 if (it->f == last_glyphless_glyph_frame
24909 && it->face_id == last_glyphless_glyph_face_id)
24910 {
24911 face_id = last_glyphless_glyph_merged_face_id;
24912 }
24913 else
24914 {
24915 /* Merge the `glyphless-char' face into the current face. */
24916 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
24917 last_glyphless_glyph_frame = it->f;
24918 last_glyphless_glyph_face_id = it->face_id;
24919 last_glyphless_glyph_merged_face_id = face_id;
24920 }
24921
24922 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24923 {
24924 it->pixel_width = THIN_SPACE_WIDTH;
24925 len = 0;
24926 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24927 }
24928 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24929 {
24930 width = CHAR_WIDTH (it->c);
24931 if (width == 0)
24932 width = 1;
24933 else if (width > 4)
24934 width = 4;
24935 it->pixel_width = base_width * width;
24936 len = 0;
24937 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24938 }
24939 else
24940 {
24941 char buf[7];
24942 const char *str;
24943 unsigned int code[6];
24944 int upper_len;
24945 int ascent, descent;
24946 struct font_metrics metrics_upper, metrics_lower;
24947
24948 face = FACE_FROM_ID (it->f, face_id);
24949 font = face->font ? face->font : FRAME_FONT (it->f);
24950 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24951
24952 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24953 {
24954 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
24955 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
24956 if (CONSP (acronym))
24957 acronym = XCAR (acronym);
24958 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24959 }
24960 else
24961 {
24962 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
24963 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
24964 str = buf;
24965 }
24966 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
24967 code[len] = font->driver->encode_char (font, str[len]);
24968 upper_len = (len + 1) / 2;
24969 font->driver->text_extents (font, code, upper_len,
24970 &metrics_upper);
24971 font->driver->text_extents (font, code + upper_len, len - upper_len,
24972 &metrics_lower);
24973
24974
24975
24976 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
24977 width = max (metrics_upper.width, metrics_lower.width) + 4;
24978 upper_xoff = upper_yoff = 2; /* the typical case */
24979 if (base_width >= width)
24980 {
24981 /* Align the upper to the left, the lower to the right. */
24982 it->pixel_width = base_width;
24983 lower_xoff = base_width - 2 - metrics_lower.width;
24984 }
24985 else
24986 {
24987 /* Center the shorter one. */
24988 it->pixel_width = width;
24989 if (metrics_upper.width >= metrics_lower.width)
24990 lower_xoff = (width - metrics_lower.width) / 2;
24991 else
24992 {
24993 /* FIXME: This code doesn't look right. It formerly was
24994 missing the "lower_xoff = 0;", which couldn't have
24995 been right since it left lower_xoff uninitialized. */
24996 lower_xoff = 0;
24997 upper_xoff = (width - metrics_upper.width) / 2;
24998 }
24999 }
25000
25001 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
25002 top, bottom, and between upper and lower strings. */
25003 height = (metrics_upper.ascent + metrics_upper.descent
25004 + metrics_lower.ascent + metrics_lower.descent) + 5;
25005 /* Center vertically.
25006 H:base_height, D:base_descent
25007 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
25008
25009 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
25010 descent = D - H/2 + h/2;
25011 lower_yoff = descent - 2 - ld;
25012 upper_yoff = lower_yoff - la - 1 - ud; */
25013 ascent = - (it->descent - (base_height + height + 1) / 2);
25014 descent = it->descent - (base_height - height) / 2;
25015 lower_yoff = descent - 2 - metrics_lower.descent;
25016 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
25017 - metrics_upper.descent);
25018 /* Don't make the height shorter than the base height. */
25019 if (height > base_height)
25020 {
25021 it->ascent = ascent;
25022 it->descent = descent;
25023 }
25024 }
25025
25026 it->phys_ascent = it->ascent;
25027 it->phys_descent = it->descent;
25028 if (it->glyph_row)
25029 append_glyphless_glyph (it, face_id, for_no_font, len,
25030 upper_xoff, upper_yoff,
25031 lower_xoff, lower_yoff);
25032 it->nglyphs = 1;
25033 take_vertical_position_into_account (it);
25034 }
25035
25036
25037 /* RIF:
25038 Produce glyphs/get display metrics for the display element IT is
25039 loaded with. See the description of struct it in dispextern.h
25040 for an overview of struct it. */
25041
25042 void
25043 x_produce_glyphs (struct it *it)
25044 {
25045 int extra_line_spacing = it->extra_line_spacing;
25046
25047 it->glyph_not_available_p = 0;
25048
25049 if (it->what == IT_CHARACTER)
25050 {
25051 XChar2b char2b;
25052 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25053 struct font *font = face->font;
25054 struct font_metrics *pcm = NULL;
25055 int boff; /* baseline offset */
25056
25057 if (font == NULL)
25058 {
25059 /* When no suitable font is found, display this character by
25060 the method specified in the first extra slot of
25061 Vglyphless_char_display. */
25062 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
25063
25064 eassert (it->what == IT_GLYPHLESS);
25065 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
25066 goto done;
25067 }
25068
25069 boff = font->baseline_offset;
25070 if (font->vertical_centering)
25071 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25072
25073 if (it->char_to_display != '\n' && it->char_to_display != '\t')
25074 {
25075 int stretched_p;
25076
25077 it->nglyphs = 1;
25078
25079 if (it->override_ascent >= 0)
25080 {
25081 it->ascent = it->override_ascent;
25082 it->descent = it->override_descent;
25083 boff = it->override_boff;
25084 }
25085 else
25086 {
25087 it->ascent = FONT_BASE (font) + boff;
25088 it->descent = FONT_DESCENT (font) - boff;
25089 }
25090
25091 if (get_char_glyph_code (it->char_to_display, font, &char2b))
25092 {
25093 pcm = get_per_char_metric (font, &char2b);
25094 if (pcm->width == 0
25095 && pcm->rbearing == 0 && pcm->lbearing == 0)
25096 pcm = NULL;
25097 }
25098
25099 if (pcm)
25100 {
25101 it->phys_ascent = pcm->ascent + boff;
25102 it->phys_descent = pcm->descent - boff;
25103 it->pixel_width = pcm->width;
25104 }
25105 else
25106 {
25107 it->glyph_not_available_p = 1;
25108 it->phys_ascent = it->ascent;
25109 it->phys_descent = it->descent;
25110 it->pixel_width = font->space_width;
25111 }
25112
25113 if (it->constrain_row_ascent_descent_p)
25114 {
25115 if (it->descent > it->max_descent)
25116 {
25117 it->ascent += it->descent - it->max_descent;
25118 it->descent = it->max_descent;
25119 }
25120 if (it->ascent > it->max_ascent)
25121 {
25122 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25123 it->ascent = it->max_ascent;
25124 }
25125 it->phys_ascent = min (it->phys_ascent, it->ascent);
25126 it->phys_descent = min (it->phys_descent, it->descent);
25127 extra_line_spacing = 0;
25128 }
25129
25130 /* If this is a space inside a region of text with
25131 `space-width' property, change its width. */
25132 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
25133 if (stretched_p)
25134 it->pixel_width *= XFLOATINT (it->space_width);
25135
25136 /* If face has a box, add the box thickness to the character
25137 height. If character has a box line to the left and/or
25138 right, add the box line width to the character's width. */
25139 if (face->box != FACE_NO_BOX)
25140 {
25141 int thick = face->box_line_width;
25142
25143 if (thick > 0)
25144 {
25145 it->ascent += thick;
25146 it->descent += thick;
25147 }
25148 else
25149 thick = -thick;
25150
25151 if (it->start_of_box_run_p)
25152 it->pixel_width += thick;
25153 if (it->end_of_box_run_p)
25154 it->pixel_width += thick;
25155 }
25156
25157 /* If face has an overline, add the height of the overline
25158 (1 pixel) and a 1 pixel margin to the character height. */
25159 if (face->overline_p)
25160 it->ascent += overline_margin;
25161
25162 if (it->constrain_row_ascent_descent_p)
25163 {
25164 if (it->ascent > it->max_ascent)
25165 it->ascent = it->max_ascent;
25166 if (it->descent > it->max_descent)
25167 it->descent = it->max_descent;
25168 }
25169
25170 take_vertical_position_into_account (it);
25171
25172 /* If we have to actually produce glyphs, do it. */
25173 if (it->glyph_row)
25174 {
25175 if (stretched_p)
25176 {
25177 /* Translate a space with a `space-width' property
25178 into a stretch glyph. */
25179 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
25180 / FONT_HEIGHT (font));
25181 append_stretch_glyph (it, it->object, it->pixel_width,
25182 it->ascent + it->descent, ascent);
25183 }
25184 else
25185 append_glyph (it);
25186
25187 /* If characters with lbearing or rbearing are displayed
25188 in this line, record that fact in a flag of the
25189 glyph row. This is used to optimize X output code. */
25190 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
25191 it->glyph_row->contains_overlapping_glyphs_p = 1;
25192 }
25193 if (! stretched_p && it->pixel_width == 0)
25194 /* We assure that all visible glyphs have at least 1-pixel
25195 width. */
25196 it->pixel_width = 1;
25197 }
25198 else if (it->char_to_display == '\n')
25199 {
25200 /* A newline has no width, but we need the height of the
25201 line. But if previous part of the line sets a height,
25202 don't increase that height */
25203
25204 Lisp_Object height;
25205 Lisp_Object total_height = Qnil;
25206
25207 it->override_ascent = -1;
25208 it->pixel_width = 0;
25209 it->nglyphs = 0;
25210
25211 height = get_it_property (it, Qline_height);
25212 /* Split (line-height total-height) list */
25213 if (CONSP (height)
25214 && CONSP (XCDR (height))
25215 && NILP (XCDR (XCDR (height))))
25216 {
25217 total_height = XCAR (XCDR (height));
25218 height = XCAR (height);
25219 }
25220 height = calc_line_height_property (it, height, font, boff, 1);
25221
25222 if (it->override_ascent >= 0)
25223 {
25224 it->ascent = it->override_ascent;
25225 it->descent = it->override_descent;
25226 boff = it->override_boff;
25227 }
25228 else
25229 {
25230 it->ascent = FONT_BASE (font) + boff;
25231 it->descent = FONT_DESCENT (font) - boff;
25232 }
25233
25234 if (EQ (height, Qt))
25235 {
25236 if (it->descent > it->max_descent)
25237 {
25238 it->ascent += it->descent - it->max_descent;
25239 it->descent = it->max_descent;
25240 }
25241 if (it->ascent > it->max_ascent)
25242 {
25243 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25244 it->ascent = it->max_ascent;
25245 }
25246 it->phys_ascent = min (it->phys_ascent, it->ascent);
25247 it->phys_descent = min (it->phys_descent, it->descent);
25248 it->constrain_row_ascent_descent_p = 1;
25249 extra_line_spacing = 0;
25250 }
25251 else
25252 {
25253 Lisp_Object spacing;
25254
25255 it->phys_ascent = it->ascent;
25256 it->phys_descent = it->descent;
25257
25258 if ((it->max_ascent > 0 || it->max_descent > 0)
25259 && face->box != FACE_NO_BOX
25260 && face->box_line_width > 0)
25261 {
25262 it->ascent += face->box_line_width;
25263 it->descent += face->box_line_width;
25264 }
25265 if (!NILP (height)
25266 && XINT (height) > it->ascent + it->descent)
25267 it->ascent = XINT (height) - it->descent;
25268
25269 if (!NILP (total_height))
25270 spacing = calc_line_height_property (it, total_height, font, boff, 0);
25271 else
25272 {
25273 spacing = get_it_property (it, Qline_spacing);
25274 spacing = calc_line_height_property (it, spacing, font, boff, 0);
25275 }
25276 if (INTEGERP (spacing))
25277 {
25278 extra_line_spacing = XINT (spacing);
25279 if (!NILP (total_height))
25280 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
25281 }
25282 }
25283 }
25284 else /* i.e. (it->char_to_display == '\t') */
25285 {
25286 if (font->space_width > 0)
25287 {
25288 int tab_width = it->tab_width * font->space_width;
25289 int x = it->current_x + it->continuation_lines_width;
25290 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
25291
25292 /* If the distance from the current position to the next tab
25293 stop is less than a space character width, use the
25294 tab stop after that. */
25295 if (next_tab_x - x < font->space_width)
25296 next_tab_x += tab_width;
25297
25298 it->pixel_width = next_tab_x - x;
25299 it->nglyphs = 1;
25300 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
25301 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
25302
25303 if (it->glyph_row)
25304 {
25305 append_stretch_glyph (it, it->object, it->pixel_width,
25306 it->ascent + it->descent, it->ascent);
25307 }
25308 }
25309 else
25310 {
25311 it->pixel_width = 0;
25312 it->nglyphs = 1;
25313 }
25314 }
25315 }
25316 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
25317 {
25318 /* A static composition.
25319
25320 Note: A composition is represented as one glyph in the
25321 glyph matrix. There are no padding glyphs.
25322
25323 Important note: pixel_width, ascent, and descent are the
25324 values of what is drawn by draw_glyphs (i.e. the values of
25325 the overall glyphs composed). */
25326 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25327 int boff; /* baseline offset */
25328 struct composition *cmp = composition_table[it->cmp_it.id];
25329 int glyph_len = cmp->glyph_len;
25330 struct font *font = face->font;
25331
25332 it->nglyphs = 1;
25333
25334 /* If we have not yet calculated pixel size data of glyphs of
25335 the composition for the current face font, calculate them
25336 now. Theoretically, we have to check all fonts for the
25337 glyphs, but that requires much time and memory space. So,
25338 here we check only the font of the first glyph. This may
25339 lead to incorrect display, but it's very rare, and C-l
25340 (recenter-top-bottom) can correct the display anyway. */
25341 if (! cmp->font || cmp->font != font)
25342 {
25343 /* Ascent and descent of the font of the first character
25344 of this composition (adjusted by baseline offset).
25345 Ascent and descent of overall glyphs should not be less
25346 than these, respectively. */
25347 int font_ascent, font_descent, font_height;
25348 /* Bounding box of the overall glyphs. */
25349 int leftmost, rightmost, lowest, highest;
25350 int lbearing, rbearing;
25351 int i, width, ascent, descent;
25352 int left_padded = 0, right_padded = 0;
25353 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
25354 XChar2b char2b;
25355 struct font_metrics *pcm;
25356 int font_not_found_p;
25357 ptrdiff_t pos;
25358
25359 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
25360 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
25361 break;
25362 if (glyph_len < cmp->glyph_len)
25363 right_padded = 1;
25364 for (i = 0; i < glyph_len; i++)
25365 {
25366 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
25367 break;
25368 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25369 }
25370 if (i > 0)
25371 left_padded = 1;
25372
25373 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
25374 : IT_CHARPOS (*it));
25375 /* If no suitable font is found, use the default font. */
25376 font_not_found_p = font == NULL;
25377 if (font_not_found_p)
25378 {
25379 face = face->ascii_face;
25380 font = face->font;
25381 }
25382 boff = font->baseline_offset;
25383 if (font->vertical_centering)
25384 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25385 font_ascent = FONT_BASE (font) + boff;
25386 font_descent = FONT_DESCENT (font) - boff;
25387 font_height = FONT_HEIGHT (font);
25388
25389 cmp->font = font;
25390
25391 pcm = NULL;
25392 if (! font_not_found_p)
25393 {
25394 get_char_face_and_encoding (it->f, c, it->face_id,
25395 &char2b, 0);
25396 pcm = get_per_char_metric (font, &char2b);
25397 }
25398
25399 /* Initialize the bounding box. */
25400 if (pcm)
25401 {
25402 width = cmp->glyph_len > 0 ? pcm->width : 0;
25403 ascent = pcm->ascent;
25404 descent = pcm->descent;
25405 lbearing = pcm->lbearing;
25406 rbearing = pcm->rbearing;
25407 }
25408 else
25409 {
25410 width = cmp->glyph_len > 0 ? font->space_width : 0;
25411 ascent = FONT_BASE (font);
25412 descent = FONT_DESCENT (font);
25413 lbearing = 0;
25414 rbearing = width;
25415 }
25416
25417 rightmost = width;
25418 leftmost = 0;
25419 lowest = - descent + boff;
25420 highest = ascent + boff;
25421
25422 if (! font_not_found_p
25423 && font->default_ascent
25424 && CHAR_TABLE_P (Vuse_default_ascent)
25425 && !NILP (Faref (Vuse_default_ascent,
25426 make_number (it->char_to_display))))
25427 highest = font->default_ascent + boff;
25428
25429 /* Draw the first glyph at the normal position. It may be
25430 shifted to right later if some other glyphs are drawn
25431 at the left. */
25432 cmp->offsets[i * 2] = 0;
25433 cmp->offsets[i * 2 + 1] = boff;
25434 cmp->lbearing = lbearing;
25435 cmp->rbearing = rbearing;
25436
25437 /* Set cmp->offsets for the remaining glyphs. */
25438 for (i++; i < glyph_len; i++)
25439 {
25440 int left, right, btm, top;
25441 int ch = COMPOSITION_GLYPH (cmp, i);
25442 int face_id;
25443 struct face *this_face;
25444
25445 if (ch == '\t')
25446 ch = ' ';
25447 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25448 this_face = FACE_FROM_ID (it->f, face_id);
25449 font = this_face->font;
25450
25451 if (font == NULL)
25452 pcm = NULL;
25453 else
25454 {
25455 get_char_face_and_encoding (it->f, ch, face_id,
25456 &char2b, 0);
25457 pcm = get_per_char_metric (font, &char2b);
25458 }
25459 if (! pcm)
25460 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25461 else
25462 {
25463 width = pcm->width;
25464 ascent = pcm->ascent;
25465 descent = pcm->descent;
25466 lbearing = pcm->lbearing;
25467 rbearing = pcm->rbearing;
25468 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25469 {
25470 /* Relative composition with or without
25471 alternate chars. */
25472 left = (leftmost + rightmost - width) / 2;
25473 btm = - descent + boff;
25474 if (font->relative_compose
25475 && (! CHAR_TABLE_P (Vignore_relative_composition)
25476 || NILP (Faref (Vignore_relative_composition,
25477 make_number (ch)))))
25478 {
25479
25480 if (- descent >= font->relative_compose)
25481 /* One extra pixel between two glyphs. */
25482 btm = highest + 1;
25483 else if (ascent <= 0)
25484 /* One extra pixel between two glyphs. */
25485 btm = lowest - 1 - ascent - descent;
25486 }
25487 }
25488 else
25489 {
25490 /* A composition rule is specified by an integer
25491 value that encodes global and new reference
25492 points (GREF and NREF). GREF and NREF are
25493 specified by numbers as below:
25494
25495 0---1---2 -- ascent
25496 | |
25497 | |
25498 | |
25499 9--10--11 -- center
25500 | |
25501 ---3---4---5--- baseline
25502 | |
25503 6---7---8 -- descent
25504 */
25505 int rule = COMPOSITION_RULE (cmp, i);
25506 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25507
25508 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25509 grefx = gref % 3, nrefx = nref % 3;
25510 grefy = gref / 3, nrefy = nref / 3;
25511 if (xoff)
25512 xoff = font_height * (xoff - 128) / 256;
25513 if (yoff)
25514 yoff = font_height * (yoff - 128) / 256;
25515
25516 left = (leftmost
25517 + grefx * (rightmost - leftmost) / 2
25518 - nrefx * width / 2
25519 + xoff);
25520
25521 btm = ((grefy == 0 ? highest
25522 : grefy == 1 ? 0
25523 : grefy == 2 ? lowest
25524 : (highest + lowest) / 2)
25525 - (nrefy == 0 ? ascent + descent
25526 : nrefy == 1 ? descent - boff
25527 : nrefy == 2 ? 0
25528 : (ascent + descent) / 2)
25529 + yoff);
25530 }
25531
25532 cmp->offsets[i * 2] = left;
25533 cmp->offsets[i * 2 + 1] = btm + descent;
25534
25535 /* Update the bounding box of the overall glyphs. */
25536 if (width > 0)
25537 {
25538 right = left + width;
25539 if (left < leftmost)
25540 leftmost = left;
25541 if (right > rightmost)
25542 rightmost = right;
25543 }
25544 top = btm + descent + ascent;
25545 if (top > highest)
25546 highest = top;
25547 if (btm < lowest)
25548 lowest = btm;
25549
25550 if (cmp->lbearing > left + lbearing)
25551 cmp->lbearing = left + lbearing;
25552 if (cmp->rbearing < left + rbearing)
25553 cmp->rbearing = left + rbearing;
25554 }
25555 }
25556
25557 /* If there are glyphs whose x-offsets are negative,
25558 shift all glyphs to the right and make all x-offsets
25559 non-negative. */
25560 if (leftmost < 0)
25561 {
25562 for (i = 0; i < cmp->glyph_len; i++)
25563 cmp->offsets[i * 2] -= leftmost;
25564 rightmost -= leftmost;
25565 cmp->lbearing -= leftmost;
25566 cmp->rbearing -= leftmost;
25567 }
25568
25569 if (left_padded && cmp->lbearing < 0)
25570 {
25571 for (i = 0; i < cmp->glyph_len; i++)
25572 cmp->offsets[i * 2] -= cmp->lbearing;
25573 rightmost -= cmp->lbearing;
25574 cmp->rbearing -= cmp->lbearing;
25575 cmp->lbearing = 0;
25576 }
25577 if (right_padded && rightmost < cmp->rbearing)
25578 {
25579 rightmost = cmp->rbearing;
25580 }
25581
25582 cmp->pixel_width = rightmost;
25583 cmp->ascent = highest;
25584 cmp->descent = - lowest;
25585 if (cmp->ascent < font_ascent)
25586 cmp->ascent = font_ascent;
25587 if (cmp->descent < font_descent)
25588 cmp->descent = font_descent;
25589 }
25590
25591 if (it->glyph_row
25592 && (cmp->lbearing < 0
25593 || cmp->rbearing > cmp->pixel_width))
25594 it->glyph_row->contains_overlapping_glyphs_p = 1;
25595
25596 it->pixel_width = cmp->pixel_width;
25597 it->ascent = it->phys_ascent = cmp->ascent;
25598 it->descent = it->phys_descent = cmp->descent;
25599 if (face->box != FACE_NO_BOX)
25600 {
25601 int thick = face->box_line_width;
25602
25603 if (thick > 0)
25604 {
25605 it->ascent += thick;
25606 it->descent += thick;
25607 }
25608 else
25609 thick = - thick;
25610
25611 if (it->start_of_box_run_p)
25612 it->pixel_width += thick;
25613 if (it->end_of_box_run_p)
25614 it->pixel_width += thick;
25615 }
25616
25617 /* If face has an overline, add the height of the overline
25618 (1 pixel) and a 1 pixel margin to the character height. */
25619 if (face->overline_p)
25620 it->ascent += overline_margin;
25621
25622 take_vertical_position_into_account (it);
25623 if (it->ascent < 0)
25624 it->ascent = 0;
25625 if (it->descent < 0)
25626 it->descent = 0;
25627
25628 if (it->glyph_row && cmp->glyph_len > 0)
25629 append_composite_glyph (it);
25630 }
25631 else if (it->what == IT_COMPOSITION)
25632 {
25633 /* A dynamic (automatic) composition. */
25634 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25635 Lisp_Object gstring;
25636 struct font_metrics metrics;
25637
25638 it->nglyphs = 1;
25639
25640 gstring = composition_gstring_from_id (it->cmp_it.id);
25641 it->pixel_width
25642 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25643 &metrics);
25644 if (it->glyph_row
25645 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25646 it->glyph_row->contains_overlapping_glyphs_p = 1;
25647 it->ascent = it->phys_ascent = metrics.ascent;
25648 it->descent = it->phys_descent = metrics.descent;
25649 if (face->box != FACE_NO_BOX)
25650 {
25651 int thick = face->box_line_width;
25652
25653 if (thick > 0)
25654 {
25655 it->ascent += thick;
25656 it->descent += thick;
25657 }
25658 else
25659 thick = - thick;
25660
25661 if (it->start_of_box_run_p)
25662 it->pixel_width += thick;
25663 if (it->end_of_box_run_p)
25664 it->pixel_width += thick;
25665 }
25666 /* If face has an overline, add the height of the overline
25667 (1 pixel) and a 1 pixel margin to the character height. */
25668 if (face->overline_p)
25669 it->ascent += overline_margin;
25670 take_vertical_position_into_account (it);
25671 if (it->ascent < 0)
25672 it->ascent = 0;
25673 if (it->descent < 0)
25674 it->descent = 0;
25675
25676 if (it->glyph_row)
25677 append_composite_glyph (it);
25678 }
25679 else if (it->what == IT_GLYPHLESS)
25680 produce_glyphless_glyph (it, 0, Qnil);
25681 else if (it->what == IT_IMAGE)
25682 produce_image_glyph (it);
25683 else if (it->what == IT_STRETCH)
25684 produce_stretch_glyph (it);
25685
25686 done:
25687 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25688 because this isn't true for images with `:ascent 100'. */
25689 eassert (it->ascent >= 0 && it->descent >= 0);
25690 if (it->area == TEXT_AREA)
25691 it->current_x += it->pixel_width;
25692
25693 if (extra_line_spacing > 0)
25694 {
25695 it->descent += extra_line_spacing;
25696 if (extra_line_spacing > it->max_extra_line_spacing)
25697 it->max_extra_line_spacing = extra_line_spacing;
25698 }
25699
25700 it->max_ascent = max (it->max_ascent, it->ascent);
25701 it->max_descent = max (it->max_descent, it->descent);
25702 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25703 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25704 }
25705
25706 /* EXPORT for RIF:
25707 Output LEN glyphs starting at START at the nominal cursor position.
25708 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
25709 being updated, and UPDATED_AREA is the area of that row being updated. */
25710
25711 void
25712 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
25713 struct glyph *start, enum glyph_row_area updated_area, int len)
25714 {
25715 int x, hpos, chpos = w->phys_cursor.hpos;
25716
25717 eassert (updated_row);
25718 /* When the window is hscrolled, cursor hpos can legitimately be out
25719 of bounds, but we draw the cursor at the corresponding window
25720 margin in that case. */
25721 if (!updated_row->reversed_p && chpos < 0)
25722 chpos = 0;
25723 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25724 chpos = updated_row->used[TEXT_AREA] - 1;
25725
25726 block_input ();
25727
25728 /* Write glyphs. */
25729
25730 hpos = start - updated_row->glyphs[updated_area];
25731 x = draw_glyphs (w, w->output_cursor.x,
25732 updated_row, updated_area,
25733 hpos, hpos + len,
25734 DRAW_NORMAL_TEXT, 0);
25735
25736 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25737 if (updated_area == TEXT_AREA
25738 && w->phys_cursor_on_p
25739 && w->phys_cursor.vpos == w->output_cursor.vpos
25740 && chpos >= hpos
25741 && chpos < hpos + len)
25742 w->phys_cursor_on_p = 0;
25743
25744 unblock_input ();
25745
25746 /* Advance the output cursor. */
25747 w->output_cursor.hpos += len;
25748 w->output_cursor.x = x;
25749 }
25750
25751
25752 /* EXPORT for RIF:
25753 Insert LEN glyphs from START at the nominal cursor position. */
25754
25755 void
25756 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
25757 struct glyph *start, enum glyph_row_area updated_area, int len)
25758 {
25759 struct frame *f;
25760 int line_height, shift_by_width, shifted_region_width;
25761 struct glyph_row *row;
25762 struct glyph *glyph;
25763 int frame_x, frame_y;
25764 ptrdiff_t hpos;
25765
25766 eassert (updated_row);
25767 block_input ();
25768 f = XFRAME (WINDOW_FRAME (w));
25769
25770 /* Get the height of the line we are in. */
25771 row = updated_row;
25772 line_height = row->height;
25773
25774 /* Get the width of the glyphs to insert. */
25775 shift_by_width = 0;
25776 for (glyph = start; glyph < start + len; ++glyph)
25777 shift_by_width += glyph->pixel_width;
25778
25779 /* Get the width of the region to shift right. */
25780 shifted_region_width = (window_box_width (w, updated_area)
25781 - w->output_cursor.x
25782 - shift_by_width);
25783
25784 /* Shift right. */
25785 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
25786 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
25787
25788 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25789 line_height, shift_by_width);
25790
25791 /* Write the glyphs. */
25792 hpos = start - row->glyphs[updated_area];
25793 draw_glyphs (w, w->output_cursor.x, row, updated_area,
25794 hpos, hpos + len,
25795 DRAW_NORMAL_TEXT, 0);
25796
25797 /* Advance the output cursor. */
25798 w->output_cursor.hpos += len;
25799 w->output_cursor.x += shift_by_width;
25800 unblock_input ();
25801 }
25802
25803
25804 /* EXPORT for RIF:
25805 Erase the current text line from the nominal cursor position
25806 (inclusive) to pixel column TO_X (exclusive). The idea is that
25807 everything from TO_X onward is already erased.
25808
25809 TO_X is a pixel position relative to UPDATED_AREA of currently
25810 updated window W. TO_X == -1 means clear to the end of this area. */
25811
25812 void
25813 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
25814 enum glyph_row_area updated_area, int to_x)
25815 {
25816 struct frame *f;
25817 int max_x, min_y, max_y;
25818 int from_x, from_y, to_y;
25819
25820 eassert (updated_row);
25821 f = XFRAME (w->frame);
25822
25823 if (updated_row->full_width_p)
25824 max_x = WINDOW_TOTAL_WIDTH (w);
25825 else
25826 max_x = window_box_width (w, updated_area);
25827 max_y = window_text_bottom_y (w);
25828
25829 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25830 of window. For TO_X > 0, truncate to end of drawing area. */
25831 if (to_x == 0)
25832 return;
25833 else if (to_x < 0)
25834 to_x = max_x;
25835 else
25836 to_x = min (to_x, max_x);
25837
25838 to_y = min (max_y, w->output_cursor.y + updated_row->height);
25839
25840 /* Notice if the cursor will be cleared by this operation. */
25841 if (!updated_row->full_width_p)
25842 notice_overwritten_cursor (w, updated_area,
25843 w->output_cursor.x, -1,
25844 updated_row->y,
25845 MATRIX_ROW_BOTTOM_Y (updated_row));
25846
25847 from_x = w->output_cursor.x;
25848
25849 /* Translate to frame coordinates. */
25850 if (updated_row->full_width_p)
25851 {
25852 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25853 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25854 }
25855 else
25856 {
25857 int area_left = window_box_left (w, updated_area);
25858 from_x += area_left;
25859 to_x += area_left;
25860 }
25861
25862 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25863 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
25864 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25865
25866 /* Prevent inadvertently clearing to end of the X window. */
25867 if (to_x > from_x && to_y > from_y)
25868 {
25869 block_input ();
25870 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25871 to_x - from_x, to_y - from_y);
25872 unblock_input ();
25873 }
25874 }
25875
25876 #endif /* HAVE_WINDOW_SYSTEM */
25877
25878
25879 \f
25880 /***********************************************************************
25881 Cursor types
25882 ***********************************************************************/
25883
25884 /* Value is the internal representation of the specified cursor type
25885 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25886 of the bar cursor. */
25887
25888 static enum text_cursor_kinds
25889 get_specified_cursor_type (Lisp_Object arg, int *width)
25890 {
25891 enum text_cursor_kinds type;
25892
25893 if (NILP (arg))
25894 return NO_CURSOR;
25895
25896 if (EQ (arg, Qbox))
25897 return FILLED_BOX_CURSOR;
25898
25899 if (EQ (arg, Qhollow))
25900 return HOLLOW_BOX_CURSOR;
25901
25902 if (EQ (arg, Qbar))
25903 {
25904 *width = 2;
25905 return BAR_CURSOR;
25906 }
25907
25908 if (CONSP (arg)
25909 && EQ (XCAR (arg), Qbar)
25910 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25911 {
25912 *width = XINT (XCDR (arg));
25913 return BAR_CURSOR;
25914 }
25915
25916 if (EQ (arg, Qhbar))
25917 {
25918 *width = 2;
25919 return HBAR_CURSOR;
25920 }
25921
25922 if (CONSP (arg)
25923 && EQ (XCAR (arg), Qhbar)
25924 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25925 {
25926 *width = XINT (XCDR (arg));
25927 return HBAR_CURSOR;
25928 }
25929
25930 /* Treat anything unknown as "hollow box cursor".
25931 It was bad to signal an error; people have trouble fixing
25932 .Xdefaults with Emacs, when it has something bad in it. */
25933 type = HOLLOW_BOX_CURSOR;
25934
25935 return type;
25936 }
25937
25938 /* Set the default cursor types for specified frame. */
25939 void
25940 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25941 {
25942 int width = 1;
25943 Lisp_Object tem;
25944
25945 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25946 FRAME_CURSOR_WIDTH (f) = width;
25947
25948 /* By default, set up the blink-off state depending on the on-state. */
25949
25950 tem = Fassoc (arg, Vblink_cursor_alist);
25951 if (!NILP (tem))
25952 {
25953 FRAME_BLINK_OFF_CURSOR (f)
25954 = get_specified_cursor_type (XCDR (tem), &width);
25955 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25956 }
25957 else
25958 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25959
25960 /* Make sure the cursor gets redrawn. */
25961 cursor_type_changed = 1;
25962 }
25963
25964
25965 #ifdef HAVE_WINDOW_SYSTEM
25966
25967 /* Return the cursor we want to be displayed in window W. Return
25968 width of bar/hbar cursor through WIDTH arg. Return with
25969 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
25970 (i.e. if the `system caret' should track this cursor).
25971
25972 In a mini-buffer window, we want the cursor only to appear if we
25973 are reading input from this window. For the selected window, we
25974 want the cursor type given by the frame parameter or buffer local
25975 setting of cursor-type. If explicitly marked off, draw no cursor.
25976 In all other cases, we want a hollow box cursor. */
25977
25978 static enum text_cursor_kinds
25979 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
25980 int *active_cursor)
25981 {
25982 struct frame *f = XFRAME (w->frame);
25983 struct buffer *b = XBUFFER (w->contents);
25984 int cursor_type = DEFAULT_CURSOR;
25985 Lisp_Object alt_cursor;
25986 int non_selected = 0;
25987
25988 *active_cursor = 1;
25989
25990 /* Echo area */
25991 if (cursor_in_echo_area
25992 && FRAME_HAS_MINIBUF_P (f)
25993 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
25994 {
25995 if (w == XWINDOW (echo_area_window))
25996 {
25997 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
25998 {
25999 *width = FRAME_CURSOR_WIDTH (f);
26000 return FRAME_DESIRED_CURSOR (f);
26001 }
26002 else
26003 return get_specified_cursor_type (BVAR (b, cursor_type), width);
26004 }
26005
26006 *active_cursor = 0;
26007 non_selected = 1;
26008 }
26009
26010 /* Detect a nonselected window or nonselected frame. */
26011 else if (w != XWINDOW (f->selected_window)
26012 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
26013 {
26014 *active_cursor = 0;
26015
26016 if (MINI_WINDOW_P (w) && minibuf_level == 0)
26017 return NO_CURSOR;
26018
26019 non_selected = 1;
26020 }
26021
26022 /* Never display a cursor in a window in which cursor-type is nil. */
26023 if (NILP (BVAR (b, cursor_type)))
26024 return NO_CURSOR;
26025
26026 /* Get the normal cursor type for this window. */
26027 if (EQ (BVAR (b, cursor_type), Qt))
26028 {
26029 cursor_type = FRAME_DESIRED_CURSOR (f);
26030 *width = FRAME_CURSOR_WIDTH (f);
26031 }
26032 else
26033 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
26034
26035 /* Use cursor-in-non-selected-windows instead
26036 for non-selected window or frame. */
26037 if (non_selected)
26038 {
26039 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
26040 if (!EQ (Qt, alt_cursor))
26041 return get_specified_cursor_type (alt_cursor, width);
26042 /* t means modify the normal cursor type. */
26043 if (cursor_type == FILLED_BOX_CURSOR)
26044 cursor_type = HOLLOW_BOX_CURSOR;
26045 else if (cursor_type == BAR_CURSOR && *width > 1)
26046 --*width;
26047 return cursor_type;
26048 }
26049
26050 /* Use normal cursor if not blinked off. */
26051 if (!w->cursor_off_p)
26052 {
26053 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26054 {
26055 if (cursor_type == FILLED_BOX_CURSOR)
26056 {
26057 /* Using a block cursor on large images can be very annoying.
26058 So use a hollow cursor for "large" images.
26059 If image is not transparent (no mask), also use hollow cursor. */
26060 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26061 if (img != NULL && IMAGEP (img->spec))
26062 {
26063 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
26064 where N = size of default frame font size.
26065 This should cover most of the "tiny" icons people may use. */
26066 if (!img->mask
26067 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
26068 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
26069 cursor_type = HOLLOW_BOX_CURSOR;
26070 }
26071 }
26072 else if (cursor_type != NO_CURSOR)
26073 {
26074 /* Display current only supports BOX and HOLLOW cursors for images.
26075 So for now, unconditionally use a HOLLOW cursor when cursor is
26076 not a solid box cursor. */
26077 cursor_type = HOLLOW_BOX_CURSOR;
26078 }
26079 }
26080 return cursor_type;
26081 }
26082
26083 /* Cursor is blinked off, so determine how to "toggle" it. */
26084
26085 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
26086 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
26087 return get_specified_cursor_type (XCDR (alt_cursor), width);
26088
26089 /* Then see if frame has specified a specific blink off cursor type. */
26090 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
26091 {
26092 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
26093 return FRAME_BLINK_OFF_CURSOR (f);
26094 }
26095
26096 #if 0
26097 /* Some people liked having a permanently visible blinking cursor,
26098 while others had very strong opinions against it. So it was
26099 decided to remove it. KFS 2003-09-03 */
26100
26101 /* Finally perform built-in cursor blinking:
26102 filled box <-> hollow box
26103 wide [h]bar <-> narrow [h]bar
26104 narrow [h]bar <-> no cursor
26105 other type <-> no cursor */
26106
26107 if (cursor_type == FILLED_BOX_CURSOR)
26108 return HOLLOW_BOX_CURSOR;
26109
26110 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
26111 {
26112 *width = 1;
26113 return cursor_type;
26114 }
26115 #endif
26116
26117 return NO_CURSOR;
26118 }
26119
26120
26121 /* Notice when the text cursor of window W has been completely
26122 overwritten by a drawing operation that outputs glyphs in AREA
26123 starting at X0 and ending at X1 in the line starting at Y0 and
26124 ending at Y1. X coordinates are area-relative. X1 < 0 means all
26125 the rest of the line after X0 has been written. Y coordinates
26126 are window-relative. */
26127
26128 static void
26129 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
26130 int x0, int x1, int y0, int y1)
26131 {
26132 int cx0, cx1, cy0, cy1;
26133 struct glyph_row *row;
26134
26135 if (!w->phys_cursor_on_p)
26136 return;
26137 if (area != TEXT_AREA)
26138 return;
26139
26140 if (w->phys_cursor.vpos < 0
26141 || w->phys_cursor.vpos >= w->current_matrix->nrows
26142 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
26143 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
26144 return;
26145
26146 if (row->cursor_in_fringe_p)
26147 {
26148 row->cursor_in_fringe_p = 0;
26149 draw_fringe_bitmap (w, row, row->reversed_p);
26150 w->phys_cursor_on_p = 0;
26151 return;
26152 }
26153
26154 cx0 = w->phys_cursor.x;
26155 cx1 = cx0 + w->phys_cursor_width;
26156 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
26157 return;
26158
26159 /* The cursor image will be completely removed from the
26160 screen if the output area intersects the cursor area in
26161 y-direction. When we draw in [y0 y1[, and some part of
26162 the cursor is at y < y0, that part must have been drawn
26163 before. When scrolling, the cursor is erased before
26164 actually scrolling, so we don't come here. When not
26165 scrolling, the rows above the old cursor row must have
26166 changed, and in this case these rows must have written
26167 over the cursor image.
26168
26169 Likewise if part of the cursor is below y1, with the
26170 exception of the cursor being in the first blank row at
26171 the buffer and window end because update_text_area
26172 doesn't draw that row. (Except when it does, but
26173 that's handled in update_text_area.) */
26174
26175 cy0 = w->phys_cursor.y;
26176 cy1 = cy0 + w->phys_cursor_height;
26177 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
26178 return;
26179
26180 w->phys_cursor_on_p = 0;
26181 }
26182
26183 #endif /* HAVE_WINDOW_SYSTEM */
26184
26185 \f
26186 /************************************************************************
26187 Mouse Face
26188 ************************************************************************/
26189
26190 #ifdef HAVE_WINDOW_SYSTEM
26191
26192 /* EXPORT for RIF:
26193 Fix the display of area AREA of overlapping row ROW in window W
26194 with respect to the overlapping part OVERLAPS. */
26195
26196 void
26197 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
26198 enum glyph_row_area area, int overlaps)
26199 {
26200 int i, x;
26201
26202 block_input ();
26203
26204 x = 0;
26205 for (i = 0; i < row->used[area];)
26206 {
26207 if (row->glyphs[area][i].overlaps_vertically_p)
26208 {
26209 int start = i, start_x = x;
26210
26211 do
26212 {
26213 x += row->glyphs[area][i].pixel_width;
26214 ++i;
26215 }
26216 while (i < row->used[area]
26217 && row->glyphs[area][i].overlaps_vertically_p);
26218
26219 draw_glyphs (w, start_x, row, area,
26220 start, i,
26221 DRAW_NORMAL_TEXT, overlaps);
26222 }
26223 else
26224 {
26225 x += row->glyphs[area][i].pixel_width;
26226 ++i;
26227 }
26228 }
26229
26230 unblock_input ();
26231 }
26232
26233
26234 /* EXPORT:
26235 Draw the cursor glyph of window W in glyph row ROW. See the
26236 comment of draw_glyphs for the meaning of HL. */
26237
26238 void
26239 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
26240 enum draw_glyphs_face hl)
26241 {
26242 /* If cursor hpos is out of bounds, don't draw garbage. This can
26243 happen in mini-buffer windows when switching between echo area
26244 glyphs and mini-buffer. */
26245 if ((row->reversed_p
26246 ? (w->phys_cursor.hpos >= 0)
26247 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
26248 {
26249 int on_p = w->phys_cursor_on_p;
26250 int x1;
26251 int hpos = w->phys_cursor.hpos;
26252
26253 /* When the window is hscrolled, cursor hpos can legitimately be
26254 out of bounds, but we draw the cursor at the corresponding
26255 window margin in that case. */
26256 if (!row->reversed_p && hpos < 0)
26257 hpos = 0;
26258 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26259 hpos = row->used[TEXT_AREA] - 1;
26260
26261 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
26262 hl, 0);
26263 w->phys_cursor_on_p = on_p;
26264
26265 if (hl == DRAW_CURSOR)
26266 w->phys_cursor_width = x1 - w->phys_cursor.x;
26267 /* When we erase the cursor, and ROW is overlapped by other
26268 rows, make sure that these overlapping parts of other rows
26269 are redrawn. */
26270 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
26271 {
26272 w->phys_cursor_width = x1 - w->phys_cursor.x;
26273
26274 if (row > w->current_matrix->rows
26275 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
26276 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
26277 OVERLAPS_ERASED_CURSOR);
26278
26279 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
26280 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
26281 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
26282 OVERLAPS_ERASED_CURSOR);
26283 }
26284 }
26285 }
26286
26287
26288 /* EXPORT:
26289 Erase the image of a cursor of window W from the screen. */
26290
26291 void
26292 erase_phys_cursor (struct window *w)
26293 {
26294 struct frame *f = XFRAME (w->frame);
26295 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26296 int hpos = w->phys_cursor.hpos;
26297 int vpos = w->phys_cursor.vpos;
26298 int mouse_face_here_p = 0;
26299 struct glyph_matrix *active_glyphs = w->current_matrix;
26300 struct glyph_row *cursor_row;
26301 struct glyph *cursor_glyph;
26302 enum draw_glyphs_face hl;
26303
26304 /* No cursor displayed or row invalidated => nothing to do on the
26305 screen. */
26306 if (w->phys_cursor_type == NO_CURSOR)
26307 goto mark_cursor_off;
26308
26309 /* VPOS >= active_glyphs->nrows means that window has been resized.
26310 Don't bother to erase the cursor. */
26311 if (vpos >= active_glyphs->nrows)
26312 goto mark_cursor_off;
26313
26314 /* If row containing cursor is marked invalid, there is nothing we
26315 can do. */
26316 cursor_row = MATRIX_ROW (active_glyphs, vpos);
26317 if (!cursor_row->enabled_p)
26318 goto mark_cursor_off;
26319
26320 /* If line spacing is > 0, old cursor may only be partially visible in
26321 window after split-window. So adjust visible height. */
26322 cursor_row->visible_height = min (cursor_row->visible_height,
26323 window_text_bottom_y (w) - cursor_row->y);
26324
26325 /* If row is completely invisible, don't attempt to delete a cursor which
26326 isn't there. This can happen if cursor is at top of a window, and
26327 we switch to a buffer with a header line in that window. */
26328 if (cursor_row->visible_height <= 0)
26329 goto mark_cursor_off;
26330
26331 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
26332 if (cursor_row->cursor_in_fringe_p)
26333 {
26334 cursor_row->cursor_in_fringe_p = 0;
26335 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
26336 goto mark_cursor_off;
26337 }
26338
26339 /* This can happen when the new row is shorter than the old one.
26340 In this case, either draw_glyphs or clear_end_of_line
26341 should have cleared the cursor. Note that we wouldn't be
26342 able to erase the cursor in this case because we don't have a
26343 cursor glyph at hand. */
26344 if ((cursor_row->reversed_p
26345 ? (w->phys_cursor.hpos < 0)
26346 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
26347 goto mark_cursor_off;
26348
26349 /* When the window is hscrolled, cursor hpos can legitimately be out
26350 of bounds, but we draw the cursor at the corresponding window
26351 margin in that case. */
26352 if (!cursor_row->reversed_p && hpos < 0)
26353 hpos = 0;
26354 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
26355 hpos = cursor_row->used[TEXT_AREA] - 1;
26356
26357 /* If the cursor is in the mouse face area, redisplay that when
26358 we clear the cursor. */
26359 if (! NILP (hlinfo->mouse_face_window)
26360 && coords_in_mouse_face_p (w, hpos, vpos)
26361 /* Don't redraw the cursor's spot in mouse face if it is at the
26362 end of a line (on a newline). The cursor appears there, but
26363 mouse highlighting does not. */
26364 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
26365 mouse_face_here_p = 1;
26366
26367 /* Maybe clear the display under the cursor. */
26368 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
26369 {
26370 int x, y, left_x;
26371 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
26372 int width;
26373
26374 cursor_glyph = get_phys_cursor_glyph (w);
26375 if (cursor_glyph == NULL)
26376 goto mark_cursor_off;
26377
26378 width = cursor_glyph->pixel_width;
26379 left_x = window_box_left_offset (w, TEXT_AREA);
26380 x = w->phys_cursor.x;
26381 if (x < left_x)
26382 width -= left_x - x;
26383 width = min (width, window_box_width (w, TEXT_AREA) - x);
26384 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
26385 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
26386
26387 if (width > 0)
26388 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
26389 }
26390
26391 /* Erase the cursor by redrawing the character underneath it. */
26392 if (mouse_face_here_p)
26393 hl = DRAW_MOUSE_FACE;
26394 else
26395 hl = DRAW_NORMAL_TEXT;
26396 draw_phys_cursor_glyph (w, cursor_row, hl);
26397
26398 mark_cursor_off:
26399 w->phys_cursor_on_p = 0;
26400 w->phys_cursor_type = NO_CURSOR;
26401 }
26402
26403
26404 /* EXPORT:
26405 Display or clear cursor of window W. If ON is zero, clear the
26406 cursor. If it is non-zero, display the cursor. If ON is nonzero,
26407 where to put the cursor is specified by HPOS, VPOS, X and Y. */
26408
26409 void
26410 display_and_set_cursor (struct window *w, bool on,
26411 int hpos, int vpos, int x, int y)
26412 {
26413 struct frame *f = XFRAME (w->frame);
26414 int new_cursor_type;
26415 int new_cursor_width;
26416 int active_cursor;
26417 struct glyph_row *glyph_row;
26418 struct glyph *glyph;
26419
26420 /* This is pointless on invisible frames, and dangerous on garbaged
26421 windows and frames; in the latter case, the frame or window may
26422 be in the midst of changing its size, and x and y may be off the
26423 window. */
26424 if (! FRAME_VISIBLE_P (f)
26425 || FRAME_GARBAGED_P (f)
26426 || vpos >= w->current_matrix->nrows
26427 || hpos >= w->current_matrix->matrix_w)
26428 return;
26429
26430 /* If cursor is off and we want it off, return quickly. */
26431 if (!on && !w->phys_cursor_on_p)
26432 return;
26433
26434 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26435 /* If cursor row is not enabled, we don't really know where to
26436 display the cursor. */
26437 if (!glyph_row->enabled_p)
26438 {
26439 w->phys_cursor_on_p = 0;
26440 return;
26441 }
26442
26443 glyph = NULL;
26444 if (!glyph_row->exact_window_width_line_p
26445 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26446 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26447
26448 eassert (input_blocked_p ());
26449
26450 /* Set new_cursor_type to the cursor we want to be displayed. */
26451 new_cursor_type = get_window_cursor_type (w, glyph,
26452 &new_cursor_width, &active_cursor);
26453
26454 /* If cursor is currently being shown and we don't want it to be or
26455 it is in the wrong place, or the cursor type is not what we want,
26456 erase it. */
26457 if (w->phys_cursor_on_p
26458 && (!on
26459 || w->phys_cursor.x != x
26460 || w->phys_cursor.y != y
26461 || new_cursor_type != w->phys_cursor_type
26462 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26463 && new_cursor_width != w->phys_cursor_width)))
26464 erase_phys_cursor (w);
26465
26466 /* Don't check phys_cursor_on_p here because that flag is only set
26467 to zero in some cases where we know that the cursor has been
26468 completely erased, to avoid the extra work of erasing the cursor
26469 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26470 still not be visible, or it has only been partly erased. */
26471 if (on)
26472 {
26473 w->phys_cursor_ascent = glyph_row->ascent;
26474 w->phys_cursor_height = glyph_row->height;
26475
26476 /* Set phys_cursor_.* before x_draw_.* is called because some
26477 of them may need the information. */
26478 w->phys_cursor.x = x;
26479 w->phys_cursor.y = glyph_row->y;
26480 w->phys_cursor.hpos = hpos;
26481 w->phys_cursor.vpos = vpos;
26482 }
26483
26484 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26485 new_cursor_type, new_cursor_width,
26486 on, active_cursor);
26487 }
26488
26489
26490 /* Switch the display of W's cursor on or off, according to the value
26491 of ON. */
26492
26493 static void
26494 update_window_cursor (struct window *w, bool on)
26495 {
26496 /* Don't update cursor in windows whose frame is in the process
26497 of being deleted. */
26498 if (w->current_matrix)
26499 {
26500 int hpos = w->phys_cursor.hpos;
26501 int vpos = w->phys_cursor.vpos;
26502 struct glyph_row *row;
26503
26504 if (vpos >= w->current_matrix->nrows
26505 || hpos >= w->current_matrix->matrix_w)
26506 return;
26507
26508 row = MATRIX_ROW (w->current_matrix, vpos);
26509
26510 /* When the window is hscrolled, cursor hpos can legitimately be
26511 out of bounds, but we draw the cursor at the corresponding
26512 window margin in that case. */
26513 if (!row->reversed_p && hpos < 0)
26514 hpos = 0;
26515 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26516 hpos = row->used[TEXT_AREA] - 1;
26517
26518 block_input ();
26519 display_and_set_cursor (w, on, hpos, vpos,
26520 w->phys_cursor.x, w->phys_cursor.y);
26521 unblock_input ();
26522 }
26523 }
26524
26525
26526 /* Call update_window_cursor with parameter ON_P on all leaf windows
26527 in the window tree rooted at W. */
26528
26529 static void
26530 update_cursor_in_window_tree (struct window *w, bool on_p)
26531 {
26532 while (w)
26533 {
26534 if (WINDOWP (w->contents))
26535 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
26536 else
26537 update_window_cursor (w, on_p);
26538
26539 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26540 }
26541 }
26542
26543
26544 /* EXPORT:
26545 Display the cursor on window W, or clear it, according to ON_P.
26546 Don't change the cursor's position. */
26547
26548 void
26549 x_update_cursor (struct frame *f, bool on_p)
26550 {
26551 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26552 }
26553
26554
26555 /* EXPORT:
26556 Clear the cursor of window W to background color, and mark the
26557 cursor as not shown. This is used when the text where the cursor
26558 is about to be rewritten. */
26559
26560 void
26561 x_clear_cursor (struct window *w)
26562 {
26563 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26564 update_window_cursor (w, 0);
26565 }
26566
26567 #endif /* HAVE_WINDOW_SYSTEM */
26568
26569 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26570 and MSDOS. */
26571 static void
26572 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26573 int start_hpos, int end_hpos,
26574 enum draw_glyphs_face draw)
26575 {
26576 #ifdef HAVE_WINDOW_SYSTEM
26577 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26578 {
26579 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26580 return;
26581 }
26582 #endif
26583 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26584 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26585 #endif
26586 }
26587
26588 /* Display the active region described by mouse_face_* according to DRAW. */
26589
26590 static void
26591 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26592 {
26593 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26594 struct frame *f = XFRAME (WINDOW_FRAME (w));
26595
26596 if (/* If window is in the process of being destroyed, don't bother
26597 to do anything. */
26598 w->current_matrix != NULL
26599 /* Don't update mouse highlight if hidden */
26600 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26601 /* Recognize when we are called to operate on rows that don't exist
26602 anymore. This can happen when a window is split. */
26603 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26604 {
26605 int phys_cursor_on_p = w->phys_cursor_on_p;
26606 struct glyph_row *row, *first, *last;
26607
26608 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26609 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26610
26611 for (row = first; row <= last && row->enabled_p; ++row)
26612 {
26613 int start_hpos, end_hpos, start_x;
26614
26615 /* For all but the first row, the highlight starts at column 0. */
26616 if (row == first)
26617 {
26618 /* R2L rows have BEG and END in reversed order, but the
26619 screen drawing geometry is always left to right. So
26620 we need to mirror the beginning and end of the
26621 highlighted area in R2L rows. */
26622 if (!row->reversed_p)
26623 {
26624 start_hpos = hlinfo->mouse_face_beg_col;
26625 start_x = hlinfo->mouse_face_beg_x;
26626 }
26627 else if (row == last)
26628 {
26629 start_hpos = hlinfo->mouse_face_end_col;
26630 start_x = hlinfo->mouse_face_end_x;
26631 }
26632 else
26633 {
26634 start_hpos = 0;
26635 start_x = 0;
26636 }
26637 }
26638 else if (row->reversed_p && row == last)
26639 {
26640 start_hpos = hlinfo->mouse_face_end_col;
26641 start_x = hlinfo->mouse_face_end_x;
26642 }
26643 else
26644 {
26645 start_hpos = 0;
26646 start_x = 0;
26647 }
26648
26649 if (row == last)
26650 {
26651 if (!row->reversed_p)
26652 end_hpos = hlinfo->mouse_face_end_col;
26653 else if (row == first)
26654 end_hpos = hlinfo->mouse_face_beg_col;
26655 else
26656 {
26657 end_hpos = row->used[TEXT_AREA];
26658 if (draw == DRAW_NORMAL_TEXT)
26659 row->fill_line_p = 1; /* Clear to end of line */
26660 }
26661 }
26662 else if (row->reversed_p && row == first)
26663 end_hpos = hlinfo->mouse_face_beg_col;
26664 else
26665 {
26666 end_hpos = row->used[TEXT_AREA];
26667 if (draw == DRAW_NORMAL_TEXT)
26668 row->fill_line_p = 1; /* Clear to end of line */
26669 }
26670
26671 if (end_hpos > start_hpos)
26672 {
26673 draw_row_with_mouse_face (w, start_x, row,
26674 start_hpos, end_hpos, draw);
26675
26676 row->mouse_face_p
26677 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26678 }
26679 }
26680
26681 #ifdef HAVE_WINDOW_SYSTEM
26682 /* When we've written over the cursor, arrange for it to
26683 be displayed again. */
26684 if (FRAME_WINDOW_P (f)
26685 && phys_cursor_on_p && !w->phys_cursor_on_p)
26686 {
26687 int hpos = w->phys_cursor.hpos;
26688
26689 /* When the window is hscrolled, cursor hpos can legitimately be
26690 out of bounds, but we draw the cursor at the corresponding
26691 window margin in that case. */
26692 if (!row->reversed_p && hpos < 0)
26693 hpos = 0;
26694 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26695 hpos = row->used[TEXT_AREA] - 1;
26696
26697 block_input ();
26698 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26699 w->phys_cursor.x, w->phys_cursor.y);
26700 unblock_input ();
26701 }
26702 #endif /* HAVE_WINDOW_SYSTEM */
26703 }
26704
26705 #ifdef HAVE_WINDOW_SYSTEM
26706 /* Change the mouse cursor. */
26707 if (FRAME_WINDOW_P (f))
26708 {
26709 if (draw == DRAW_NORMAL_TEXT
26710 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26711 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26712 else if (draw == DRAW_MOUSE_FACE)
26713 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26714 else
26715 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26716 }
26717 #endif /* HAVE_WINDOW_SYSTEM */
26718 }
26719
26720 /* EXPORT:
26721 Clear out the mouse-highlighted active region.
26722 Redraw it un-highlighted first. Value is non-zero if mouse
26723 face was actually drawn unhighlighted. */
26724
26725 int
26726 clear_mouse_face (Mouse_HLInfo *hlinfo)
26727 {
26728 int cleared = 0;
26729
26730 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26731 {
26732 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26733 cleared = 1;
26734 }
26735
26736 reset_mouse_highlight (hlinfo);
26737 return cleared;
26738 }
26739
26740 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26741 within the mouse face on that window. */
26742 static int
26743 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26744 {
26745 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26746
26747 /* Quickly resolve the easy cases. */
26748 if (!(WINDOWP (hlinfo->mouse_face_window)
26749 && XWINDOW (hlinfo->mouse_face_window) == w))
26750 return 0;
26751 if (vpos < hlinfo->mouse_face_beg_row
26752 || vpos > hlinfo->mouse_face_end_row)
26753 return 0;
26754 if (vpos > hlinfo->mouse_face_beg_row
26755 && vpos < hlinfo->mouse_face_end_row)
26756 return 1;
26757
26758 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26759 {
26760 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26761 {
26762 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26763 return 1;
26764 }
26765 else if ((vpos == hlinfo->mouse_face_beg_row
26766 && hpos >= hlinfo->mouse_face_beg_col)
26767 || (vpos == hlinfo->mouse_face_end_row
26768 && hpos < hlinfo->mouse_face_end_col))
26769 return 1;
26770 }
26771 else
26772 {
26773 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26774 {
26775 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26776 return 1;
26777 }
26778 else if ((vpos == hlinfo->mouse_face_beg_row
26779 && hpos <= hlinfo->mouse_face_beg_col)
26780 || (vpos == hlinfo->mouse_face_end_row
26781 && hpos > hlinfo->mouse_face_end_col))
26782 return 1;
26783 }
26784 return 0;
26785 }
26786
26787
26788 /* EXPORT:
26789 Non-zero if physical cursor of window W is within mouse face. */
26790
26791 int
26792 cursor_in_mouse_face_p (struct window *w)
26793 {
26794 int hpos = w->phys_cursor.hpos;
26795 int vpos = w->phys_cursor.vpos;
26796 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26797
26798 /* When the window is hscrolled, cursor hpos can legitimately be out
26799 of bounds, but we draw the cursor at the corresponding window
26800 margin in that case. */
26801 if (!row->reversed_p && hpos < 0)
26802 hpos = 0;
26803 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26804 hpos = row->used[TEXT_AREA] - 1;
26805
26806 return coords_in_mouse_face_p (w, hpos, vpos);
26807 }
26808
26809
26810 \f
26811 /* Find the glyph rows START_ROW and END_ROW of window W that display
26812 characters between buffer positions START_CHARPOS and END_CHARPOS
26813 (excluding END_CHARPOS). DISP_STRING is a display string that
26814 covers these buffer positions. This is similar to
26815 row_containing_pos, but is more accurate when bidi reordering makes
26816 buffer positions change non-linearly with glyph rows. */
26817 static void
26818 rows_from_pos_range (struct window *w,
26819 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26820 Lisp_Object disp_string,
26821 struct glyph_row **start, struct glyph_row **end)
26822 {
26823 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26824 int last_y = window_text_bottom_y (w);
26825 struct glyph_row *row;
26826
26827 *start = NULL;
26828 *end = NULL;
26829
26830 while (!first->enabled_p
26831 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26832 first++;
26833
26834 /* Find the START row. */
26835 for (row = first;
26836 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26837 row++)
26838 {
26839 /* A row can potentially be the START row if the range of the
26840 characters it displays intersects the range
26841 [START_CHARPOS..END_CHARPOS). */
26842 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26843 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26844 /* See the commentary in row_containing_pos, for the
26845 explanation of the complicated way to check whether
26846 some position is beyond the end of the characters
26847 displayed by a row. */
26848 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26849 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26850 && !row->ends_at_zv_p
26851 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26852 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26853 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26854 && !row->ends_at_zv_p
26855 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26856 {
26857 /* Found a candidate row. Now make sure at least one of the
26858 glyphs it displays has a charpos from the range
26859 [START_CHARPOS..END_CHARPOS).
26860
26861 This is not obvious because bidi reordering could make
26862 buffer positions of a row be 1,2,3,102,101,100, and if we
26863 want to highlight characters in [50..60), we don't want
26864 this row, even though [50..60) does intersect [1..103),
26865 the range of character positions given by the row's start
26866 and end positions. */
26867 struct glyph *g = row->glyphs[TEXT_AREA];
26868 struct glyph *e = g + row->used[TEXT_AREA];
26869
26870 while (g < e)
26871 {
26872 if (((BUFFERP (g->object) || INTEGERP (g->object))
26873 && start_charpos <= g->charpos && g->charpos < end_charpos)
26874 /* A glyph that comes from DISP_STRING is by
26875 definition to be highlighted. */
26876 || EQ (g->object, disp_string))
26877 *start = row;
26878 g++;
26879 }
26880 if (*start)
26881 break;
26882 }
26883 }
26884
26885 /* Find the END row. */
26886 if (!*start
26887 /* If the last row is partially visible, start looking for END
26888 from that row, instead of starting from FIRST. */
26889 && !(row->enabled_p
26890 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26891 row = first;
26892 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26893 {
26894 struct glyph_row *next = row + 1;
26895 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26896
26897 if (!next->enabled_p
26898 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26899 /* The first row >= START whose range of displayed characters
26900 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26901 is the row END + 1. */
26902 || (start_charpos < next_start
26903 && end_charpos < next_start)
26904 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26905 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26906 && !next->ends_at_zv_p
26907 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26908 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26909 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26910 && !next->ends_at_zv_p
26911 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26912 {
26913 *end = row;
26914 break;
26915 }
26916 else
26917 {
26918 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26919 but none of the characters it displays are in the range, it is
26920 also END + 1. */
26921 struct glyph *g = next->glyphs[TEXT_AREA];
26922 struct glyph *s = g;
26923 struct glyph *e = g + next->used[TEXT_AREA];
26924
26925 while (g < e)
26926 {
26927 if (((BUFFERP (g->object) || INTEGERP (g->object))
26928 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26929 /* If the buffer position of the first glyph in
26930 the row is equal to END_CHARPOS, it means
26931 the last character to be highlighted is the
26932 newline of ROW, and we must consider NEXT as
26933 END, not END+1. */
26934 || (((!next->reversed_p && g == s)
26935 || (next->reversed_p && g == e - 1))
26936 && (g->charpos == end_charpos
26937 /* Special case for when NEXT is an
26938 empty line at ZV. */
26939 || (g->charpos == -1
26940 && !row->ends_at_zv_p
26941 && next_start == end_charpos)))))
26942 /* A glyph that comes from DISP_STRING is by
26943 definition to be highlighted. */
26944 || EQ (g->object, disp_string))
26945 break;
26946 g++;
26947 }
26948 if (g == e)
26949 {
26950 *end = row;
26951 break;
26952 }
26953 /* The first row that ends at ZV must be the last to be
26954 highlighted. */
26955 else if (next->ends_at_zv_p)
26956 {
26957 *end = next;
26958 break;
26959 }
26960 }
26961 }
26962 }
26963
26964 /* This function sets the mouse_face_* elements of HLINFO, assuming
26965 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
26966 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
26967 for the overlay or run of text properties specifying the mouse
26968 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
26969 before-string and after-string that must also be highlighted.
26970 DISP_STRING, if non-nil, is a display string that may cover some
26971 or all of the highlighted text. */
26972
26973 static void
26974 mouse_face_from_buffer_pos (Lisp_Object window,
26975 Mouse_HLInfo *hlinfo,
26976 ptrdiff_t mouse_charpos,
26977 ptrdiff_t start_charpos,
26978 ptrdiff_t end_charpos,
26979 Lisp_Object before_string,
26980 Lisp_Object after_string,
26981 Lisp_Object disp_string)
26982 {
26983 struct window *w = XWINDOW (window);
26984 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26985 struct glyph_row *r1, *r2;
26986 struct glyph *glyph, *end;
26987 ptrdiff_t ignore, pos;
26988 int x;
26989
26990 eassert (NILP (disp_string) || STRINGP (disp_string));
26991 eassert (NILP (before_string) || STRINGP (before_string));
26992 eassert (NILP (after_string) || STRINGP (after_string));
26993
26994 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
26995 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
26996 if (r1 == NULL)
26997 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
26998 /* If the before-string or display-string contains newlines,
26999 rows_from_pos_range skips to its last row. Move back. */
27000 if (!NILP (before_string) || !NILP (disp_string))
27001 {
27002 struct glyph_row *prev;
27003 while ((prev = r1 - 1, prev >= first)
27004 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
27005 && prev->used[TEXT_AREA] > 0)
27006 {
27007 struct glyph *beg = prev->glyphs[TEXT_AREA];
27008 glyph = beg + prev->used[TEXT_AREA];
27009 while (--glyph >= beg && INTEGERP (glyph->object));
27010 if (glyph < beg
27011 || !(EQ (glyph->object, before_string)
27012 || EQ (glyph->object, disp_string)))
27013 break;
27014 r1 = prev;
27015 }
27016 }
27017 if (r2 == NULL)
27018 {
27019 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27020 hlinfo->mouse_face_past_end = 1;
27021 }
27022 else if (!NILP (after_string))
27023 {
27024 /* If the after-string has newlines, advance to its last row. */
27025 struct glyph_row *next;
27026 struct glyph_row *last
27027 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27028
27029 for (next = r2 + 1;
27030 next <= last
27031 && next->used[TEXT_AREA] > 0
27032 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
27033 ++next)
27034 r2 = next;
27035 }
27036 /* The rest of the display engine assumes that mouse_face_beg_row is
27037 either above mouse_face_end_row or identical to it. But with
27038 bidi-reordered continued lines, the row for START_CHARPOS could
27039 be below the row for END_CHARPOS. If so, swap the rows and store
27040 them in correct order. */
27041 if (r1->y > r2->y)
27042 {
27043 struct glyph_row *tem = r2;
27044
27045 r2 = r1;
27046 r1 = tem;
27047 }
27048
27049 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
27050 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
27051
27052 /* For a bidi-reordered row, the positions of BEFORE_STRING,
27053 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
27054 could be anywhere in the row and in any order. The strategy
27055 below is to find the leftmost and the rightmost glyph that
27056 belongs to either of these 3 strings, or whose position is
27057 between START_CHARPOS and END_CHARPOS, and highlight all the
27058 glyphs between those two. This may cover more than just the text
27059 between START_CHARPOS and END_CHARPOS if the range of characters
27060 strides the bidi level boundary, e.g. if the beginning is in R2L
27061 text while the end is in L2R text or vice versa. */
27062 if (!r1->reversed_p)
27063 {
27064 /* This row is in a left to right paragraph. Scan it left to
27065 right. */
27066 glyph = r1->glyphs[TEXT_AREA];
27067 end = glyph + r1->used[TEXT_AREA];
27068 x = r1->x;
27069
27070 /* Skip truncation glyphs at the start of the glyph row. */
27071 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27072 for (; glyph < end
27073 && INTEGERP (glyph->object)
27074 && glyph->charpos < 0;
27075 ++glyph)
27076 x += glyph->pixel_width;
27077
27078 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27079 or DISP_STRING, and the first glyph from buffer whose
27080 position is between START_CHARPOS and END_CHARPOS. */
27081 for (; glyph < end
27082 && !INTEGERP (glyph->object)
27083 && !EQ (glyph->object, disp_string)
27084 && !(BUFFERP (glyph->object)
27085 && (glyph->charpos >= start_charpos
27086 && glyph->charpos < end_charpos));
27087 ++glyph)
27088 {
27089 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27090 are present at buffer positions between START_CHARPOS and
27091 END_CHARPOS, or if they come from an overlay. */
27092 if (EQ (glyph->object, before_string))
27093 {
27094 pos = string_buffer_position (before_string,
27095 start_charpos);
27096 /* If pos == 0, it means before_string came from an
27097 overlay, not from a buffer position. */
27098 if (!pos || (pos >= start_charpos && pos < end_charpos))
27099 break;
27100 }
27101 else if (EQ (glyph->object, after_string))
27102 {
27103 pos = string_buffer_position (after_string, end_charpos);
27104 if (!pos || (pos >= start_charpos && pos < end_charpos))
27105 break;
27106 }
27107 x += glyph->pixel_width;
27108 }
27109 hlinfo->mouse_face_beg_x = x;
27110 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27111 }
27112 else
27113 {
27114 /* This row is in a right to left paragraph. Scan it right to
27115 left. */
27116 struct glyph *g;
27117
27118 end = r1->glyphs[TEXT_AREA] - 1;
27119 glyph = end + r1->used[TEXT_AREA];
27120
27121 /* Skip truncation glyphs at the start of the glyph row. */
27122 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27123 for (; glyph > end
27124 && INTEGERP (glyph->object)
27125 && glyph->charpos < 0;
27126 --glyph)
27127 ;
27128
27129 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27130 or DISP_STRING, and the first glyph from buffer whose
27131 position is between START_CHARPOS and END_CHARPOS. */
27132 for (; glyph > end
27133 && !INTEGERP (glyph->object)
27134 && !EQ (glyph->object, disp_string)
27135 && !(BUFFERP (glyph->object)
27136 && (glyph->charpos >= start_charpos
27137 && glyph->charpos < end_charpos));
27138 --glyph)
27139 {
27140 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27141 are present at buffer positions between START_CHARPOS and
27142 END_CHARPOS, or if they come from an overlay. */
27143 if (EQ (glyph->object, before_string))
27144 {
27145 pos = string_buffer_position (before_string, start_charpos);
27146 /* If pos == 0, it means before_string came from an
27147 overlay, not from a buffer position. */
27148 if (!pos || (pos >= start_charpos && pos < end_charpos))
27149 break;
27150 }
27151 else if (EQ (glyph->object, after_string))
27152 {
27153 pos = string_buffer_position (after_string, end_charpos);
27154 if (!pos || (pos >= start_charpos && pos < end_charpos))
27155 break;
27156 }
27157 }
27158
27159 glyph++; /* first glyph to the right of the highlighted area */
27160 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
27161 x += g->pixel_width;
27162 hlinfo->mouse_face_beg_x = x;
27163 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27164 }
27165
27166 /* If the highlight ends in a different row, compute GLYPH and END
27167 for the end row. Otherwise, reuse the values computed above for
27168 the row where the highlight begins. */
27169 if (r2 != r1)
27170 {
27171 if (!r2->reversed_p)
27172 {
27173 glyph = r2->glyphs[TEXT_AREA];
27174 end = glyph + r2->used[TEXT_AREA];
27175 x = r2->x;
27176 }
27177 else
27178 {
27179 end = r2->glyphs[TEXT_AREA] - 1;
27180 glyph = end + r2->used[TEXT_AREA];
27181 }
27182 }
27183
27184 if (!r2->reversed_p)
27185 {
27186 /* Skip truncation and continuation glyphs near the end of the
27187 row, and also blanks and stretch glyphs inserted by
27188 extend_face_to_end_of_line. */
27189 while (end > glyph
27190 && INTEGERP ((end - 1)->object))
27191 --end;
27192 /* Scan the rest of the glyph row from the end, looking for the
27193 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27194 DISP_STRING, or whose position is between START_CHARPOS
27195 and END_CHARPOS */
27196 for (--end;
27197 end > glyph
27198 && !INTEGERP (end->object)
27199 && !EQ (end->object, disp_string)
27200 && !(BUFFERP (end->object)
27201 && (end->charpos >= start_charpos
27202 && end->charpos < end_charpos));
27203 --end)
27204 {
27205 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27206 are present at buffer positions between START_CHARPOS and
27207 END_CHARPOS, or if they come from an overlay. */
27208 if (EQ (end->object, before_string))
27209 {
27210 pos = string_buffer_position (before_string, start_charpos);
27211 if (!pos || (pos >= start_charpos && pos < end_charpos))
27212 break;
27213 }
27214 else if (EQ (end->object, after_string))
27215 {
27216 pos = string_buffer_position (after_string, end_charpos);
27217 if (!pos || (pos >= start_charpos && pos < end_charpos))
27218 break;
27219 }
27220 }
27221 /* Find the X coordinate of the last glyph to be highlighted. */
27222 for (; glyph <= end; ++glyph)
27223 x += glyph->pixel_width;
27224
27225 hlinfo->mouse_face_end_x = x;
27226 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
27227 }
27228 else
27229 {
27230 /* Skip truncation and continuation glyphs near the end of the
27231 row, and also blanks and stretch glyphs inserted by
27232 extend_face_to_end_of_line. */
27233 x = r2->x;
27234 end++;
27235 while (end < glyph
27236 && INTEGERP (end->object))
27237 {
27238 x += end->pixel_width;
27239 ++end;
27240 }
27241 /* Scan the rest of the glyph row from the end, looking for the
27242 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27243 DISP_STRING, or whose position is between START_CHARPOS
27244 and END_CHARPOS */
27245 for ( ;
27246 end < glyph
27247 && !INTEGERP (end->object)
27248 && !EQ (end->object, disp_string)
27249 && !(BUFFERP (end->object)
27250 && (end->charpos >= start_charpos
27251 && end->charpos < end_charpos));
27252 ++end)
27253 {
27254 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27255 are present at buffer positions between START_CHARPOS and
27256 END_CHARPOS, or if they come from an overlay. */
27257 if (EQ (end->object, before_string))
27258 {
27259 pos = string_buffer_position (before_string, start_charpos);
27260 if (!pos || (pos >= start_charpos && pos < end_charpos))
27261 break;
27262 }
27263 else if (EQ (end->object, after_string))
27264 {
27265 pos = string_buffer_position (after_string, end_charpos);
27266 if (!pos || (pos >= start_charpos && pos < end_charpos))
27267 break;
27268 }
27269 x += end->pixel_width;
27270 }
27271 /* If we exited the above loop because we arrived at the last
27272 glyph of the row, and its buffer position is still not in
27273 range, it means the last character in range is the preceding
27274 newline. Bump the end column and x values to get past the
27275 last glyph. */
27276 if (end == glyph
27277 && BUFFERP (end->object)
27278 && (end->charpos < start_charpos
27279 || end->charpos >= end_charpos))
27280 {
27281 x += end->pixel_width;
27282 ++end;
27283 }
27284 hlinfo->mouse_face_end_x = x;
27285 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
27286 }
27287
27288 hlinfo->mouse_face_window = window;
27289 hlinfo->mouse_face_face_id
27290 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
27291 mouse_charpos + 1,
27292 !hlinfo->mouse_face_hidden, -1);
27293 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27294 }
27295
27296 /* The following function is not used anymore (replaced with
27297 mouse_face_from_string_pos), but I leave it here for the time
27298 being, in case someone would. */
27299
27300 #if 0 /* not used */
27301
27302 /* Find the position of the glyph for position POS in OBJECT in
27303 window W's current matrix, and return in *X, *Y the pixel
27304 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
27305
27306 RIGHT_P non-zero means return the position of the right edge of the
27307 glyph, RIGHT_P zero means return the left edge position.
27308
27309 If no glyph for POS exists in the matrix, return the position of
27310 the glyph with the next smaller position that is in the matrix, if
27311 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
27312 exists in the matrix, return the position of the glyph with the
27313 next larger position in OBJECT.
27314
27315 Value is non-zero if a glyph was found. */
27316
27317 static int
27318 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
27319 int *hpos, int *vpos, int *x, int *y, int right_p)
27320 {
27321 int yb = window_text_bottom_y (w);
27322 struct glyph_row *r;
27323 struct glyph *best_glyph = NULL;
27324 struct glyph_row *best_row = NULL;
27325 int best_x = 0;
27326
27327 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27328 r->enabled_p && r->y < yb;
27329 ++r)
27330 {
27331 struct glyph *g = r->glyphs[TEXT_AREA];
27332 struct glyph *e = g + r->used[TEXT_AREA];
27333 int gx;
27334
27335 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27336 if (EQ (g->object, object))
27337 {
27338 if (g->charpos == pos)
27339 {
27340 best_glyph = g;
27341 best_x = gx;
27342 best_row = r;
27343 goto found;
27344 }
27345 else if (best_glyph == NULL
27346 || ((eabs (g->charpos - pos)
27347 < eabs (best_glyph->charpos - pos))
27348 && (right_p
27349 ? g->charpos < pos
27350 : g->charpos > pos)))
27351 {
27352 best_glyph = g;
27353 best_x = gx;
27354 best_row = r;
27355 }
27356 }
27357 }
27358
27359 found:
27360
27361 if (best_glyph)
27362 {
27363 *x = best_x;
27364 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
27365
27366 if (right_p)
27367 {
27368 *x += best_glyph->pixel_width;
27369 ++*hpos;
27370 }
27371
27372 *y = best_row->y;
27373 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
27374 }
27375
27376 return best_glyph != NULL;
27377 }
27378 #endif /* not used */
27379
27380 /* Find the positions of the first and the last glyphs in window W's
27381 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
27382 (assumed to be a string), and return in HLINFO's mouse_face_*
27383 members the pixel and column/row coordinates of those glyphs. */
27384
27385 static void
27386 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
27387 Lisp_Object object,
27388 ptrdiff_t startpos, ptrdiff_t endpos)
27389 {
27390 int yb = window_text_bottom_y (w);
27391 struct glyph_row *r;
27392 struct glyph *g, *e;
27393 int gx;
27394 int found = 0;
27395
27396 /* Find the glyph row with at least one position in the range
27397 [STARTPOS..ENDPOS], and the first glyph in that row whose
27398 position belongs to that range. */
27399 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27400 r->enabled_p && r->y < yb;
27401 ++r)
27402 {
27403 if (!r->reversed_p)
27404 {
27405 g = r->glyphs[TEXT_AREA];
27406 e = g + r->used[TEXT_AREA];
27407 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27408 if (EQ (g->object, object)
27409 && startpos <= g->charpos && g->charpos <= endpos)
27410 {
27411 hlinfo->mouse_face_beg_row
27412 = MATRIX_ROW_VPOS (r, w->current_matrix);
27413 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27414 hlinfo->mouse_face_beg_x = gx;
27415 found = 1;
27416 break;
27417 }
27418 }
27419 else
27420 {
27421 struct glyph *g1;
27422
27423 e = r->glyphs[TEXT_AREA];
27424 g = e + r->used[TEXT_AREA];
27425 for ( ; g > e; --g)
27426 if (EQ ((g-1)->object, object)
27427 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
27428 {
27429 hlinfo->mouse_face_beg_row
27430 = MATRIX_ROW_VPOS (r, w->current_matrix);
27431 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27432 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27433 gx += g1->pixel_width;
27434 hlinfo->mouse_face_beg_x = gx;
27435 found = 1;
27436 break;
27437 }
27438 }
27439 if (found)
27440 break;
27441 }
27442
27443 if (!found)
27444 return;
27445
27446 /* Starting with the next row, look for the first row which does NOT
27447 include any glyphs whose positions are in the range. */
27448 for (++r; r->enabled_p && r->y < yb; ++r)
27449 {
27450 g = r->glyphs[TEXT_AREA];
27451 e = g + r->used[TEXT_AREA];
27452 found = 0;
27453 for ( ; g < e; ++g)
27454 if (EQ (g->object, object)
27455 && startpos <= g->charpos && g->charpos <= endpos)
27456 {
27457 found = 1;
27458 break;
27459 }
27460 if (!found)
27461 break;
27462 }
27463
27464 /* The highlighted region ends on the previous row. */
27465 r--;
27466
27467 /* Set the end row. */
27468 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
27469
27470 /* Compute and set the end column and the end column's horizontal
27471 pixel coordinate. */
27472 if (!r->reversed_p)
27473 {
27474 g = r->glyphs[TEXT_AREA];
27475 e = g + r->used[TEXT_AREA];
27476 for ( ; e > g; --e)
27477 if (EQ ((e-1)->object, object)
27478 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
27479 break;
27480 hlinfo->mouse_face_end_col = e - g;
27481
27482 for (gx = r->x; g < e; ++g)
27483 gx += g->pixel_width;
27484 hlinfo->mouse_face_end_x = gx;
27485 }
27486 else
27487 {
27488 e = r->glyphs[TEXT_AREA];
27489 g = e + r->used[TEXT_AREA];
27490 for (gx = r->x ; e < g; ++e)
27491 {
27492 if (EQ (e->object, object)
27493 && startpos <= e->charpos && e->charpos <= endpos)
27494 break;
27495 gx += e->pixel_width;
27496 }
27497 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27498 hlinfo->mouse_face_end_x = gx;
27499 }
27500 }
27501
27502 #ifdef HAVE_WINDOW_SYSTEM
27503
27504 /* See if position X, Y is within a hot-spot of an image. */
27505
27506 static int
27507 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27508 {
27509 if (!CONSP (hot_spot))
27510 return 0;
27511
27512 if (EQ (XCAR (hot_spot), Qrect))
27513 {
27514 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27515 Lisp_Object rect = XCDR (hot_spot);
27516 Lisp_Object tem;
27517 if (!CONSP (rect))
27518 return 0;
27519 if (!CONSP (XCAR (rect)))
27520 return 0;
27521 if (!CONSP (XCDR (rect)))
27522 return 0;
27523 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27524 return 0;
27525 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27526 return 0;
27527 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27528 return 0;
27529 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27530 return 0;
27531 return 1;
27532 }
27533 else if (EQ (XCAR (hot_spot), Qcircle))
27534 {
27535 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27536 Lisp_Object circ = XCDR (hot_spot);
27537 Lisp_Object lr, lx0, ly0;
27538 if (CONSP (circ)
27539 && CONSP (XCAR (circ))
27540 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27541 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27542 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27543 {
27544 double r = XFLOATINT (lr);
27545 double dx = XINT (lx0) - x;
27546 double dy = XINT (ly0) - y;
27547 return (dx * dx + dy * dy <= r * r);
27548 }
27549 }
27550 else if (EQ (XCAR (hot_spot), Qpoly))
27551 {
27552 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27553 if (VECTORP (XCDR (hot_spot)))
27554 {
27555 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27556 Lisp_Object *poly = v->contents;
27557 ptrdiff_t n = v->header.size;
27558 ptrdiff_t i;
27559 int inside = 0;
27560 Lisp_Object lx, ly;
27561 int x0, y0;
27562
27563 /* Need an even number of coordinates, and at least 3 edges. */
27564 if (n < 6 || n & 1)
27565 return 0;
27566
27567 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27568 If count is odd, we are inside polygon. Pixels on edges
27569 may or may not be included depending on actual geometry of the
27570 polygon. */
27571 if ((lx = poly[n-2], !INTEGERP (lx))
27572 || (ly = poly[n-1], !INTEGERP (lx)))
27573 return 0;
27574 x0 = XINT (lx), y0 = XINT (ly);
27575 for (i = 0; i < n; i += 2)
27576 {
27577 int x1 = x0, y1 = y0;
27578 if ((lx = poly[i], !INTEGERP (lx))
27579 || (ly = poly[i+1], !INTEGERP (ly)))
27580 return 0;
27581 x0 = XINT (lx), y0 = XINT (ly);
27582
27583 /* Does this segment cross the X line? */
27584 if (x0 >= x)
27585 {
27586 if (x1 >= x)
27587 continue;
27588 }
27589 else if (x1 < x)
27590 continue;
27591 if (y > y0 && y > y1)
27592 continue;
27593 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27594 inside = !inside;
27595 }
27596 return inside;
27597 }
27598 }
27599 return 0;
27600 }
27601
27602 Lisp_Object
27603 find_hot_spot (Lisp_Object map, int x, int y)
27604 {
27605 while (CONSP (map))
27606 {
27607 if (CONSP (XCAR (map))
27608 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27609 return XCAR (map);
27610 map = XCDR (map);
27611 }
27612
27613 return Qnil;
27614 }
27615
27616 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27617 3, 3, 0,
27618 doc: /* Lookup in image map MAP coordinates X and Y.
27619 An image map is an alist where each element has the format (AREA ID PLIST).
27620 An AREA is specified as either a rectangle, a circle, or a polygon:
27621 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27622 pixel coordinates of the upper left and bottom right corners.
27623 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27624 and the radius of the circle; r may be a float or integer.
27625 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27626 vector describes one corner in the polygon.
27627 Returns the alist element for the first matching AREA in MAP. */)
27628 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27629 {
27630 if (NILP (map))
27631 return Qnil;
27632
27633 CHECK_NUMBER (x);
27634 CHECK_NUMBER (y);
27635
27636 return find_hot_spot (map,
27637 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27638 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27639 }
27640
27641
27642 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27643 static void
27644 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27645 {
27646 /* Do not change cursor shape while dragging mouse. */
27647 if (!NILP (do_mouse_tracking))
27648 return;
27649
27650 if (!NILP (pointer))
27651 {
27652 if (EQ (pointer, Qarrow))
27653 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27654 else if (EQ (pointer, Qhand))
27655 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27656 else if (EQ (pointer, Qtext))
27657 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27658 else if (EQ (pointer, intern ("hdrag")))
27659 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27660 #ifdef HAVE_X_WINDOWS
27661 else if (EQ (pointer, intern ("vdrag")))
27662 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27663 #endif
27664 else if (EQ (pointer, intern ("hourglass")))
27665 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27666 else if (EQ (pointer, Qmodeline))
27667 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27668 else
27669 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27670 }
27671
27672 if (cursor != No_Cursor)
27673 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27674 }
27675
27676 #endif /* HAVE_WINDOW_SYSTEM */
27677
27678 /* Take proper action when mouse has moved to the mode or header line
27679 or marginal area AREA of window W, x-position X and y-position Y.
27680 X is relative to the start of the text display area of W, so the
27681 width of bitmap areas and scroll bars must be subtracted to get a
27682 position relative to the start of the mode line. */
27683
27684 static void
27685 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27686 enum window_part area)
27687 {
27688 struct window *w = XWINDOW (window);
27689 struct frame *f = XFRAME (w->frame);
27690 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27691 #ifdef HAVE_WINDOW_SYSTEM
27692 Display_Info *dpyinfo;
27693 #endif
27694 Cursor cursor = No_Cursor;
27695 Lisp_Object pointer = Qnil;
27696 int dx, dy, width, height;
27697 ptrdiff_t charpos;
27698 Lisp_Object string, object = Qnil;
27699 Lisp_Object pos IF_LINT (= Qnil), help;
27700
27701 Lisp_Object mouse_face;
27702 int original_x_pixel = x;
27703 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27704 struct glyph_row *row IF_LINT (= 0);
27705
27706 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27707 {
27708 int x0;
27709 struct glyph *end;
27710
27711 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27712 returns them in row/column units! */
27713 string = mode_line_string (w, area, &x, &y, &charpos,
27714 &object, &dx, &dy, &width, &height);
27715
27716 row = (area == ON_MODE_LINE
27717 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27718 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27719
27720 /* Find the glyph under the mouse pointer. */
27721 if (row->mode_line_p && row->enabled_p)
27722 {
27723 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27724 end = glyph + row->used[TEXT_AREA];
27725
27726 for (x0 = original_x_pixel;
27727 glyph < end && x0 >= glyph->pixel_width;
27728 ++glyph)
27729 x0 -= glyph->pixel_width;
27730
27731 if (glyph >= end)
27732 glyph = NULL;
27733 }
27734 }
27735 else
27736 {
27737 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27738 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27739 returns them in row/column units! */
27740 string = marginal_area_string (w, area, &x, &y, &charpos,
27741 &object, &dx, &dy, &width, &height);
27742 }
27743
27744 help = Qnil;
27745
27746 #ifdef HAVE_WINDOW_SYSTEM
27747 if (IMAGEP (object))
27748 {
27749 Lisp_Object image_map, hotspot;
27750 if ((image_map = Fplist_get (XCDR (object), QCmap),
27751 !NILP (image_map))
27752 && (hotspot = find_hot_spot (image_map, dx, dy),
27753 CONSP (hotspot))
27754 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27755 {
27756 Lisp_Object plist;
27757
27758 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27759 If so, we could look for mouse-enter, mouse-leave
27760 properties in PLIST (and do something...). */
27761 hotspot = XCDR (hotspot);
27762 if (CONSP (hotspot)
27763 && (plist = XCAR (hotspot), CONSP (plist)))
27764 {
27765 pointer = Fplist_get (plist, Qpointer);
27766 if (NILP (pointer))
27767 pointer = Qhand;
27768 help = Fplist_get (plist, Qhelp_echo);
27769 if (!NILP (help))
27770 {
27771 help_echo_string = help;
27772 XSETWINDOW (help_echo_window, w);
27773 help_echo_object = w->contents;
27774 help_echo_pos = charpos;
27775 }
27776 }
27777 }
27778 if (NILP (pointer))
27779 pointer = Fplist_get (XCDR (object), QCpointer);
27780 }
27781 #endif /* HAVE_WINDOW_SYSTEM */
27782
27783 if (STRINGP (string))
27784 pos = make_number (charpos);
27785
27786 /* Set the help text and mouse pointer. If the mouse is on a part
27787 of the mode line without any text (e.g. past the right edge of
27788 the mode line text), use the default help text and pointer. */
27789 if (STRINGP (string) || area == ON_MODE_LINE)
27790 {
27791 /* Arrange to display the help by setting the global variables
27792 help_echo_string, help_echo_object, and help_echo_pos. */
27793 if (NILP (help))
27794 {
27795 if (STRINGP (string))
27796 help = Fget_text_property (pos, Qhelp_echo, string);
27797
27798 if (!NILP (help))
27799 {
27800 help_echo_string = help;
27801 XSETWINDOW (help_echo_window, w);
27802 help_echo_object = string;
27803 help_echo_pos = charpos;
27804 }
27805 else if (area == ON_MODE_LINE)
27806 {
27807 Lisp_Object default_help
27808 = buffer_local_value_1 (Qmode_line_default_help_echo,
27809 w->contents);
27810
27811 if (STRINGP (default_help))
27812 {
27813 help_echo_string = default_help;
27814 XSETWINDOW (help_echo_window, w);
27815 help_echo_object = Qnil;
27816 help_echo_pos = -1;
27817 }
27818 }
27819 }
27820
27821 #ifdef HAVE_WINDOW_SYSTEM
27822 /* Change the mouse pointer according to what is under it. */
27823 if (FRAME_WINDOW_P (f))
27824 {
27825 dpyinfo = FRAME_X_DISPLAY_INFO (f);
27826 if (STRINGP (string))
27827 {
27828 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27829
27830 if (NILP (pointer))
27831 pointer = Fget_text_property (pos, Qpointer, string);
27832
27833 /* Change the mouse pointer according to what is under X/Y. */
27834 if (NILP (pointer)
27835 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27836 {
27837 Lisp_Object map;
27838 map = Fget_text_property (pos, Qlocal_map, string);
27839 if (!KEYMAPP (map))
27840 map = Fget_text_property (pos, Qkeymap, string);
27841 if (!KEYMAPP (map))
27842 cursor = dpyinfo->vertical_scroll_bar_cursor;
27843 }
27844 }
27845 else
27846 /* Default mode-line pointer. */
27847 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27848 }
27849 #endif
27850 }
27851
27852 /* Change the mouse face according to what is under X/Y. */
27853 if (STRINGP (string))
27854 {
27855 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27856 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
27857 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27858 && glyph)
27859 {
27860 Lisp_Object b, e;
27861
27862 struct glyph * tmp_glyph;
27863
27864 int gpos;
27865 int gseq_length;
27866 int total_pixel_width;
27867 ptrdiff_t begpos, endpos, ignore;
27868
27869 int vpos, hpos;
27870
27871 b = Fprevious_single_property_change (make_number (charpos + 1),
27872 Qmouse_face, string, Qnil);
27873 if (NILP (b))
27874 begpos = 0;
27875 else
27876 begpos = XINT (b);
27877
27878 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27879 if (NILP (e))
27880 endpos = SCHARS (string);
27881 else
27882 endpos = XINT (e);
27883
27884 /* Calculate the glyph position GPOS of GLYPH in the
27885 displayed string, relative to the beginning of the
27886 highlighted part of the string.
27887
27888 Note: GPOS is different from CHARPOS. CHARPOS is the
27889 position of GLYPH in the internal string object. A mode
27890 line string format has structures which are converted to
27891 a flattened string by the Emacs Lisp interpreter. The
27892 internal string is an element of those structures. The
27893 displayed string is the flattened string. */
27894 tmp_glyph = row_start_glyph;
27895 while (tmp_glyph < glyph
27896 && (!(EQ (tmp_glyph->object, glyph->object)
27897 && begpos <= tmp_glyph->charpos
27898 && tmp_glyph->charpos < endpos)))
27899 tmp_glyph++;
27900 gpos = glyph - tmp_glyph;
27901
27902 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27903 the highlighted part of the displayed string to which
27904 GLYPH belongs. Note: GSEQ_LENGTH is different from
27905 SCHARS (STRING), because the latter returns the length of
27906 the internal string. */
27907 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27908 tmp_glyph > glyph
27909 && (!(EQ (tmp_glyph->object, glyph->object)
27910 && begpos <= tmp_glyph->charpos
27911 && tmp_glyph->charpos < endpos));
27912 tmp_glyph--)
27913 ;
27914 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27915
27916 /* Calculate the total pixel width of all the glyphs between
27917 the beginning of the highlighted area and GLYPH. */
27918 total_pixel_width = 0;
27919 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27920 total_pixel_width += tmp_glyph->pixel_width;
27921
27922 /* Pre calculation of re-rendering position. Note: X is in
27923 column units here, after the call to mode_line_string or
27924 marginal_area_string. */
27925 hpos = x - gpos;
27926 vpos = (area == ON_MODE_LINE
27927 ? (w->current_matrix)->nrows - 1
27928 : 0);
27929
27930 /* If GLYPH's position is included in the region that is
27931 already drawn in mouse face, we have nothing to do. */
27932 if ( EQ (window, hlinfo->mouse_face_window)
27933 && (!row->reversed_p
27934 ? (hlinfo->mouse_face_beg_col <= hpos
27935 && hpos < hlinfo->mouse_face_end_col)
27936 /* In R2L rows we swap BEG and END, see below. */
27937 : (hlinfo->mouse_face_end_col <= hpos
27938 && hpos < hlinfo->mouse_face_beg_col))
27939 && hlinfo->mouse_face_beg_row == vpos )
27940 return;
27941
27942 if (clear_mouse_face (hlinfo))
27943 cursor = No_Cursor;
27944
27945 if (!row->reversed_p)
27946 {
27947 hlinfo->mouse_face_beg_col = hpos;
27948 hlinfo->mouse_face_beg_x = original_x_pixel
27949 - (total_pixel_width + dx);
27950 hlinfo->mouse_face_end_col = hpos + gseq_length;
27951 hlinfo->mouse_face_end_x = 0;
27952 }
27953 else
27954 {
27955 /* In R2L rows, show_mouse_face expects BEG and END
27956 coordinates to be swapped. */
27957 hlinfo->mouse_face_end_col = hpos;
27958 hlinfo->mouse_face_end_x = original_x_pixel
27959 - (total_pixel_width + dx);
27960 hlinfo->mouse_face_beg_col = hpos + gseq_length;
27961 hlinfo->mouse_face_beg_x = 0;
27962 }
27963
27964 hlinfo->mouse_face_beg_row = vpos;
27965 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
27966 hlinfo->mouse_face_past_end = 0;
27967 hlinfo->mouse_face_window = window;
27968
27969 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
27970 charpos,
27971 0, 0, 0,
27972 &ignore,
27973 glyph->face_id,
27974 1);
27975 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27976
27977 if (NILP (pointer))
27978 pointer = Qhand;
27979 }
27980 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27981 clear_mouse_face (hlinfo);
27982 }
27983 #ifdef HAVE_WINDOW_SYSTEM
27984 if (FRAME_WINDOW_P (f))
27985 define_frame_cursor1 (f, cursor, pointer);
27986 #endif
27987 }
27988
27989
27990 /* EXPORT:
27991 Take proper action when the mouse has moved to position X, Y on
27992 frame F with regards to highlighting portions of display that have
27993 mouse-face properties. Also de-highlight portions of display where
27994 the mouse was before, set the mouse pointer shape as appropriate
27995 for the mouse coordinates, and activate help echo (tooltips).
27996 X and Y can be negative or out of range. */
27997
27998 void
27999 note_mouse_highlight (struct frame *f, int x, int y)
28000 {
28001 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28002 enum window_part part = ON_NOTHING;
28003 Lisp_Object window;
28004 struct window *w;
28005 Cursor cursor = No_Cursor;
28006 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
28007 struct buffer *b;
28008
28009 /* When a menu is active, don't highlight because this looks odd. */
28010 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
28011 if (popup_activated ())
28012 return;
28013 #endif
28014
28015 if (!f->glyphs_initialized_p
28016 || f->pointer_invisible)
28017 return;
28018
28019 hlinfo->mouse_face_mouse_x = x;
28020 hlinfo->mouse_face_mouse_y = y;
28021 hlinfo->mouse_face_mouse_frame = f;
28022
28023 if (hlinfo->mouse_face_defer)
28024 return;
28025
28026 /* Which window is that in? */
28027 window = window_from_coordinates (f, x, y, &part, 1);
28028
28029 /* If displaying active text in another window, clear that. */
28030 if (! EQ (window, hlinfo->mouse_face_window)
28031 /* Also clear if we move out of text area in same window. */
28032 || (!NILP (hlinfo->mouse_face_window)
28033 && !NILP (window)
28034 && part != ON_TEXT
28035 && part != ON_MODE_LINE
28036 && part != ON_HEADER_LINE))
28037 clear_mouse_face (hlinfo);
28038
28039 /* Not on a window -> return. */
28040 if (!WINDOWP (window))
28041 return;
28042
28043 /* Reset help_echo_string. It will get recomputed below. */
28044 help_echo_string = Qnil;
28045
28046 /* Convert to window-relative pixel coordinates. */
28047 w = XWINDOW (window);
28048 frame_to_window_pixel_xy (w, &x, &y);
28049
28050 #ifdef HAVE_WINDOW_SYSTEM
28051 /* Handle tool-bar window differently since it doesn't display a
28052 buffer. */
28053 if (EQ (window, f->tool_bar_window))
28054 {
28055 note_tool_bar_highlight (f, x, y);
28056 return;
28057 }
28058 #endif
28059
28060 /* Mouse is on the mode, header line or margin? */
28061 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
28062 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
28063 {
28064 note_mode_line_or_margin_highlight (window, x, y, part);
28065 return;
28066 }
28067
28068 #ifdef HAVE_WINDOW_SYSTEM
28069 if (part == ON_VERTICAL_BORDER)
28070 {
28071 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28072 help_echo_string = build_string ("drag-mouse-1: resize");
28073 }
28074 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
28075 || part == ON_SCROLL_BAR)
28076 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28077 else
28078 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28079 #endif
28080
28081 /* Are we in a window whose display is up to date?
28082 And verify the buffer's text has not changed. */
28083 b = XBUFFER (w->contents);
28084 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
28085 {
28086 int hpos, vpos, dx, dy, area = LAST_AREA;
28087 ptrdiff_t pos;
28088 struct glyph *glyph;
28089 Lisp_Object object;
28090 Lisp_Object mouse_face = Qnil, position;
28091 Lisp_Object *overlay_vec = NULL;
28092 ptrdiff_t i, noverlays;
28093 struct buffer *obuf;
28094 ptrdiff_t obegv, ozv;
28095 int same_region;
28096
28097 /* Find the glyph under X/Y. */
28098 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
28099
28100 #ifdef HAVE_WINDOW_SYSTEM
28101 /* Look for :pointer property on image. */
28102 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28103 {
28104 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
28105 if (img != NULL && IMAGEP (img->spec))
28106 {
28107 Lisp_Object image_map, hotspot;
28108 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
28109 !NILP (image_map))
28110 && (hotspot = find_hot_spot (image_map,
28111 glyph->slice.img.x + dx,
28112 glyph->slice.img.y + dy),
28113 CONSP (hotspot))
28114 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28115 {
28116 Lisp_Object plist;
28117
28118 /* Could check XCAR (hotspot) to see if we enter/leave
28119 this hot-spot.
28120 If so, we could look for mouse-enter, mouse-leave
28121 properties in PLIST (and do something...). */
28122 hotspot = XCDR (hotspot);
28123 if (CONSP (hotspot)
28124 && (plist = XCAR (hotspot), CONSP (plist)))
28125 {
28126 pointer = Fplist_get (plist, Qpointer);
28127 if (NILP (pointer))
28128 pointer = Qhand;
28129 help_echo_string = Fplist_get (plist, Qhelp_echo);
28130 if (!NILP (help_echo_string))
28131 {
28132 help_echo_window = window;
28133 help_echo_object = glyph->object;
28134 help_echo_pos = glyph->charpos;
28135 }
28136 }
28137 }
28138 if (NILP (pointer))
28139 pointer = Fplist_get (XCDR (img->spec), QCpointer);
28140 }
28141 }
28142 #endif /* HAVE_WINDOW_SYSTEM */
28143
28144 /* Clear mouse face if X/Y not over text. */
28145 if (glyph == NULL
28146 || area != TEXT_AREA
28147 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
28148 /* Glyph's OBJECT is an integer for glyphs inserted by the
28149 display engine for its internal purposes, like truncation
28150 and continuation glyphs and blanks beyond the end of
28151 line's text on text terminals. If we are over such a
28152 glyph, we are not over any text. */
28153 || INTEGERP (glyph->object)
28154 /* R2L rows have a stretch glyph at their front, which
28155 stands for no text, whereas L2R rows have no glyphs at
28156 all beyond the end of text. Treat such stretch glyphs
28157 like we do with NULL glyphs in L2R rows. */
28158 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
28159 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
28160 && glyph->type == STRETCH_GLYPH
28161 && glyph->avoid_cursor_p))
28162 {
28163 if (clear_mouse_face (hlinfo))
28164 cursor = No_Cursor;
28165 #ifdef HAVE_WINDOW_SYSTEM
28166 if (FRAME_WINDOW_P (f) && NILP (pointer))
28167 {
28168 if (area != TEXT_AREA)
28169 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28170 else
28171 pointer = Vvoid_text_area_pointer;
28172 }
28173 #endif
28174 goto set_cursor;
28175 }
28176
28177 pos = glyph->charpos;
28178 object = glyph->object;
28179 if (!STRINGP (object) && !BUFFERP (object))
28180 goto set_cursor;
28181
28182 /* If we get an out-of-range value, return now; avoid an error. */
28183 if (BUFFERP (object) && pos > BUF_Z (b))
28184 goto set_cursor;
28185
28186 /* Make the window's buffer temporarily current for
28187 overlays_at and compute_char_face. */
28188 obuf = current_buffer;
28189 current_buffer = b;
28190 obegv = BEGV;
28191 ozv = ZV;
28192 BEGV = BEG;
28193 ZV = Z;
28194
28195 /* Is this char mouse-active or does it have help-echo? */
28196 position = make_number (pos);
28197
28198 if (BUFFERP (object))
28199 {
28200 /* Put all the overlays we want in a vector in overlay_vec. */
28201 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
28202 /* Sort overlays into increasing priority order. */
28203 noverlays = sort_overlays (overlay_vec, noverlays, w);
28204 }
28205 else
28206 noverlays = 0;
28207
28208 if (NILP (Vmouse_highlight))
28209 {
28210 clear_mouse_face (hlinfo);
28211 goto check_help_echo;
28212 }
28213
28214 same_region = coords_in_mouse_face_p (w, hpos, vpos);
28215
28216 if (same_region)
28217 cursor = No_Cursor;
28218
28219 /* Check mouse-face highlighting. */
28220 if (! same_region
28221 /* If there exists an overlay with mouse-face overlapping
28222 the one we are currently highlighting, we have to
28223 check if we enter the overlapping overlay, and then
28224 highlight only that. */
28225 || (OVERLAYP (hlinfo->mouse_face_overlay)
28226 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
28227 {
28228 /* Find the highest priority overlay with a mouse-face. */
28229 Lisp_Object overlay = Qnil;
28230 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
28231 {
28232 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
28233 if (!NILP (mouse_face))
28234 overlay = overlay_vec[i];
28235 }
28236
28237 /* If we're highlighting the same overlay as before, there's
28238 no need to do that again. */
28239 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
28240 goto check_help_echo;
28241 hlinfo->mouse_face_overlay = overlay;
28242
28243 /* Clear the display of the old active region, if any. */
28244 if (clear_mouse_face (hlinfo))
28245 cursor = No_Cursor;
28246
28247 /* If no overlay applies, get a text property. */
28248 if (NILP (overlay))
28249 mouse_face = Fget_text_property (position, Qmouse_face, object);
28250
28251 /* Next, compute the bounds of the mouse highlighting and
28252 display it. */
28253 if (!NILP (mouse_face) && STRINGP (object))
28254 {
28255 /* The mouse-highlighting comes from a display string
28256 with a mouse-face. */
28257 Lisp_Object s, e;
28258 ptrdiff_t ignore;
28259
28260 s = Fprevious_single_property_change
28261 (make_number (pos + 1), Qmouse_face, object, Qnil);
28262 e = Fnext_single_property_change
28263 (position, Qmouse_face, object, Qnil);
28264 if (NILP (s))
28265 s = make_number (0);
28266 if (NILP (e))
28267 e = make_number (SCHARS (object) - 1);
28268 mouse_face_from_string_pos (w, hlinfo, object,
28269 XINT (s), XINT (e));
28270 hlinfo->mouse_face_past_end = 0;
28271 hlinfo->mouse_face_window = window;
28272 hlinfo->mouse_face_face_id
28273 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
28274 glyph->face_id, 1);
28275 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28276 cursor = No_Cursor;
28277 }
28278 else
28279 {
28280 /* The mouse-highlighting, if any, comes from an overlay
28281 or text property in the buffer. */
28282 Lisp_Object buffer IF_LINT (= Qnil);
28283 Lisp_Object disp_string IF_LINT (= Qnil);
28284
28285 if (STRINGP (object))
28286 {
28287 /* If we are on a display string with no mouse-face,
28288 check if the text under it has one. */
28289 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
28290 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28291 pos = string_buffer_position (object, start);
28292 if (pos > 0)
28293 {
28294 mouse_face = get_char_property_and_overlay
28295 (make_number (pos), Qmouse_face, w->contents, &overlay);
28296 buffer = w->contents;
28297 disp_string = object;
28298 }
28299 }
28300 else
28301 {
28302 buffer = object;
28303 disp_string = Qnil;
28304 }
28305
28306 if (!NILP (mouse_face))
28307 {
28308 Lisp_Object before, after;
28309 Lisp_Object before_string, after_string;
28310 /* To correctly find the limits of mouse highlight
28311 in a bidi-reordered buffer, we must not use the
28312 optimization of limiting the search in
28313 previous-single-property-change and
28314 next-single-property-change, because
28315 rows_from_pos_range needs the real start and end
28316 positions to DTRT in this case. That's because
28317 the first row visible in a window does not
28318 necessarily display the character whose position
28319 is the smallest. */
28320 Lisp_Object lim1 =
28321 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28322 ? Fmarker_position (w->start)
28323 : Qnil;
28324 Lisp_Object lim2 =
28325 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28326 ? make_number (BUF_Z (XBUFFER (buffer)) - w->window_end_pos)
28327 : Qnil;
28328
28329 if (NILP (overlay))
28330 {
28331 /* Handle the text property case. */
28332 before = Fprevious_single_property_change
28333 (make_number (pos + 1), Qmouse_face, buffer, lim1);
28334 after = Fnext_single_property_change
28335 (make_number (pos), Qmouse_face, buffer, lim2);
28336 before_string = after_string = Qnil;
28337 }
28338 else
28339 {
28340 /* Handle the overlay case. */
28341 before = Foverlay_start (overlay);
28342 after = Foverlay_end (overlay);
28343 before_string = Foverlay_get (overlay, Qbefore_string);
28344 after_string = Foverlay_get (overlay, Qafter_string);
28345
28346 if (!STRINGP (before_string)) before_string = Qnil;
28347 if (!STRINGP (after_string)) after_string = Qnil;
28348 }
28349
28350 mouse_face_from_buffer_pos (window, hlinfo, pos,
28351 NILP (before)
28352 ? 1
28353 : XFASTINT (before),
28354 NILP (after)
28355 ? BUF_Z (XBUFFER (buffer))
28356 : XFASTINT (after),
28357 before_string, after_string,
28358 disp_string);
28359 cursor = No_Cursor;
28360 }
28361 }
28362 }
28363
28364 check_help_echo:
28365
28366 /* Look for a `help-echo' property. */
28367 if (NILP (help_echo_string)) {
28368 Lisp_Object help, overlay;
28369
28370 /* Check overlays first. */
28371 help = overlay = Qnil;
28372 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
28373 {
28374 overlay = overlay_vec[i];
28375 help = Foverlay_get (overlay, Qhelp_echo);
28376 }
28377
28378 if (!NILP (help))
28379 {
28380 help_echo_string = help;
28381 help_echo_window = window;
28382 help_echo_object = overlay;
28383 help_echo_pos = pos;
28384 }
28385 else
28386 {
28387 Lisp_Object obj = glyph->object;
28388 ptrdiff_t charpos = glyph->charpos;
28389
28390 /* Try text properties. */
28391 if (STRINGP (obj)
28392 && charpos >= 0
28393 && charpos < SCHARS (obj))
28394 {
28395 help = Fget_text_property (make_number (charpos),
28396 Qhelp_echo, obj);
28397 if (NILP (help))
28398 {
28399 /* If the string itself doesn't specify a help-echo,
28400 see if the buffer text ``under'' it does. */
28401 struct glyph_row *r
28402 = MATRIX_ROW (w->current_matrix, vpos);
28403 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28404 ptrdiff_t p = string_buffer_position (obj, start);
28405 if (p > 0)
28406 {
28407 help = Fget_char_property (make_number (p),
28408 Qhelp_echo, w->contents);
28409 if (!NILP (help))
28410 {
28411 charpos = p;
28412 obj = w->contents;
28413 }
28414 }
28415 }
28416 }
28417 else if (BUFFERP (obj)
28418 && charpos >= BEGV
28419 && charpos < ZV)
28420 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28421 obj);
28422
28423 if (!NILP (help))
28424 {
28425 help_echo_string = help;
28426 help_echo_window = window;
28427 help_echo_object = obj;
28428 help_echo_pos = charpos;
28429 }
28430 }
28431 }
28432
28433 #ifdef HAVE_WINDOW_SYSTEM
28434 /* Look for a `pointer' property. */
28435 if (FRAME_WINDOW_P (f) && NILP (pointer))
28436 {
28437 /* Check overlays first. */
28438 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28439 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28440
28441 if (NILP (pointer))
28442 {
28443 Lisp_Object obj = glyph->object;
28444 ptrdiff_t charpos = glyph->charpos;
28445
28446 /* Try text properties. */
28447 if (STRINGP (obj)
28448 && charpos >= 0
28449 && charpos < SCHARS (obj))
28450 {
28451 pointer = Fget_text_property (make_number (charpos),
28452 Qpointer, obj);
28453 if (NILP (pointer))
28454 {
28455 /* If the string itself doesn't specify a pointer,
28456 see if the buffer text ``under'' it does. */
28457 struct glyph_row *r
28458 = MATRIX_ROW (w->current_matrix, vpos);
28459 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28460 ptrdiff_t p = string_buffer_position (obj, start);
28461 if (p > 0)
28462 pointer = Fget_char_property (make_number (p),
28463 Qpointer, w->contents);
28464 }
28465 }
28466 else if (BUFFERP (obj)
28467 && charpos >= BEGV
28468 && charpos < ZV)
28469 pointer = Fget_text_property (make_number (charpos),
28470 Qpointer, obj);
28471 }
28472 }
28473 #endif /* HAVE_WINDOW_SYSTEM */
28474
28475 BEGV = obegv;
28476 ZV = ozv;
28477 current_buffer = obuf;
28478 }
28479
28480 set_cursor:
28481
28482 #ifdef HAVE_WINDOW_SYSTEM
28483 if (FRAME_WINDOW_P (f))
28484 define_frame_cursor1 (f, cursor, pointer);
28485 #else
28486 /* This is here to prevent a compiler error, about "label at end of
28487 compound statement". */
28488 return;
28489 #endif
28490 }
28491
28492
28493 /* EXPORT for RIF:
28494 Clear any mouse-face on window W. This function is part of the
28495 redisplay interface, and is called from try_window_id and similar
28496 functions to ensure the mouse-highlight is off. */
28497
28498 void
28499 x_clear_window_mouse_face (struct window *w)
28500 {
28501 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28502 Lisp_Object window;
28503
28504 block_input ();
28505 XSETWINDOW (window, w);
28506 if (EQ (window, hlinfo->mouse_face_window))
28507 clear_mouse_face (hlinfo);
28508 unblock_input ();
28509 }
28510
28511
28512 /* EXPORT:
28513 Just discard the mouse face information for frame F, if any.
28514 This is used when the size of F is changed. */
28515
28516 void
28517 cancel_mouse_face (struct frame *f)
28518 {
28519 Lisp_Object window;
28520 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28521
28522 window = hlinfo->mouse_face_window;
28523 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28524 reset_mouse_highlight (hlinfo);
28525 }
28526
28527
28528 \f
28529 /***********************************************************************
28530 Exposure Events
28531 ***********************************************************************/
28532
28533 #ifdef HAVE_WINDOW_SYSTEM
28534
28535 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28536 which intersects rectangle R. R is in window-relative coordinates. */
28537
28538 static void
28539 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28540 enum glyph_row_area area)
28541 {
28542 struct glyph *first = row->glyphs[area];
28543 struct glyph *end = row->glyphs[area] + row->used[area];
28544 struct glyph *last;
28545 int first_x, start_x, x;
28546
28547 if (area == TEXT_AREA && row->fill_line_p)
28548 /* If row extends face to end of line write the whole line. */
28549 draw_glyphs (w, 0, row, area,
28550 0, row->used[area],
28551 DRAW_NORMAL_TEXT, 0);
28552 else
28553 {
28554 /* Set START_X to the window-relative start position for drawing glyphs of
28555 AREA. The first glyph of the text area can be partially visible.
28556 The first glyphs of other areas cannot. */
28557 start_x = window_box_left_offset (w, area);
28558 x = start_x;
28559 if (area == TEXT_AREA)
28560 x += row->x;
28561
28562 /* Find the first glyph that must be redrawn. */
28563 while (first < end
28564 && x + first->pixel_width < r->x)
28565 {
28566 x += first->pixel_width;
28567 ++first;
28568 }
28569
28570 /* Find the last one. */
28571 last = first;
28572 first_x = x;
28573 while (last < end
28574 && x < r->x + r->width)
28575 {
28576 x += last->pixel_width;
28577 ++last;
28578 }
28579
28580 /* Repaint. */
28581 if (last > first)
28582 draw_glyphs (w, first_x - start_x, row, area,
28583 first - row->glyphs[area], last - row->glyphs[area],
28584 DRAW_NORMAL_TEXT, 0);
28585 }
28586 }
28587
28588
28589 /* Redraw the parts of the glyph row ROW on window W intersecting
28590 rectangle R. R is in window-relative coordinates. Value is
28591 non-zero if mouse-face was overwritten. */
28592
28593 static int
28594 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28595 {
28596 eassert (row->enabled_p);
28597
28598 if (row->mode_line_p || w->pseudo_window_p)
28599 draw_glyphs (w, 0, row, TEXT_AREA,
28600 0, row->used[TEXT_AREA],
28601 DRAW_NORMAL_TEXT, 0);
28602 else
28603 {
28604 if (row->used[LEFT_MARGIN_AREA])
28605 expose_area (w, row, r, LEFT_MARGIN_AREA);
28606 if (row->used[TEXT_AREA])
28607 expose_area (w, row, r, TEXT_AREA);
28608 if (row->used[RIGHT_MARGIN_AREA])
28609 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28610 draw_row_fringe_bitmaps (w, row);
28611 }
28612
28613 return row->mouse_face_p;
28614 }
28615
28616
28617 /* Redraw those parts of glyphs rows during expose event handling that
28618 overlap other rows. Redrawing of an exposed line writes over parts
28619 of lines overlapping that exposed line; this function fixes that.
28620
28621 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28622 row in W's current matrix that is exposed and overlaps other rows.
28623 LAST_OVERLAPPING_ROW is the last such row. */
28624
28625 static void
28626 expose_overlaps (struct window *w,
28627 struct glyph_row *first_overlapping_row,
28628 struct glyph_row *last_overlapping_row,
28629 XRectangle *r)
28630 {
28631 struct glyph_row *row;
28632
28633 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28634 if (row->overlapping_p)
28635 {
28636 eassert (row->enabled_p && !row->mode_line_p);
28637
28638 row->clip = r;
28639 if (row->used[LEFT_MARGIN_AREA])
28640 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28641
28642 if (row->used[TEXT_AREA])
28643 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28644
28645 if (row->used[RIGHT_MARGIN_AREA])
28646 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28647 row->clip = NULL;
28648 }
28649 }
28650
28651
28652 /* Return non-zero if W's cursor intersects rectangle R. */
28653
28654 static int
28655 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28656 {
28657 XRectangle cr, result;
28658 struct glyph *cursor_glyph;
28659 struct glyph_row *row;
28660
28661 if (w->phys_cursor.vpos >= 0
28662 && w->phys_cursor.vpos < w->current_matrix->nrows
28663 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28664 row->enabled_p)
28665 && row->cursor_in_fringe_p)
28666 {
28667 /* Cursor is in the fringe. */
28668 cr.x = window_box_right_offset (w,
28669 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28670 ? RIGHT_MARGIN_AREA
28671 : TEXT_AREA));
28672 cr.y = row->y;
28673 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28674 cr.height = row->height;
28675 return x_intersect_rectangles (&cr, r, &result);
28676 }
28677
28678 cursor_glyph = get_phys_cursor_glyph (w);
28679 if (cursor_glyph)
28680 {
28681 /* r is relative to W's box, but w->phys_cursor.x is relative
28682 to left edge of W's TEXT area. Adjust it. */
28683 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28684 cr.y = w->phys_cursor.y;
28685 cr.width = cursor_glyph->pixel_width;
28686 cr.height = w->phys_cursor_height;
28687 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28688 I assume the effect is the same -- and this is portable. */
28689 return x_intersect_rectangles (&cr, r, &result);
28690 }
28691 /* If we don't understand the format, pretend we're not in the hot-spot. */
28692 return 0;
28693 }
28694
28695
28696 /* EXPORT:
28697 Draw a vertical window border to the right of window W if W doesn't
28698 have vertical scroll bars. */
28699
28700 void
28701 x_draw_vertical_border (struct window *w)
28702 {
28703 struct frame *f = XFRAME (WINDOW_FRAME (w));
28704
28705 /* We could do better, if we knew what type of scroll-bar the adjacent
28706 windows (on either side) have... But we don't :-(
28707 However, I think this works ok. ++KFS 2003-04-25 */
28708
28709 /* Redraw borders between horizontally adjacent windows. Don't
28710 do it for frames with vertical scroll bars because either the
28711 right scroll bar of a window, or the left scroll bar of its
28712 neighbor will suffice as a border. */
28713 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28714 return;
28715
28716 /* Note: It is necessary to redraw both the left and the right
28717 borders, for when only this single window W is being
28718 redisplayed. */
28719 if (!WINDOW_RIGHTMOST_P (w)
28720 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28721 {
28722 int x0, x1, y0, y1;
28723
28724 window_box_edges (w, &x0, &y0, &x1, &y1);
28725 y1 -= 1;
28726
28727 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28728 x1 -= 1;
28729
28730 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28731 }
28732 if (!WINDOW_LEFTMOST_P (w)
28733 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28734 {
28735 int x0, x1, y0, y1;
28736
28737 window_box_edges (w, &x0, &y0, &x1, &y1);
28738 y1 -= 1;
28739
28740 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28741 x0 -= 1;
28742
28743 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28744 }
28745 }
28746
28747
28748 /* Redraw the part of window W intersection rectangle FR. Pixel
28749 coordinates in FR are frame-relative. Call this function with
28750 input blocked. Value is non-zero if the exposure overwrites
28751 mouse-face. */
28752
28753 static int
28754 expose_window (struct window *w, XRectangle *fr)
28755 {
28756 struct frame *f = XFRAME (w->frame);
28757 XRectangle wr, r;
28758 int mouse_face_overwritten_p = 0;
28759
28760 /* If window is not yet fully initialized, do nothing. This can
28761 happen when toolkit scroll bars are used and a window is split.
28762 Reconfiguring the scroll bar will generate an expose for a newly
28763 created window. */
28764 if (w->current_matrix == NULL)
28765 return 0;
28766
28767 /* When we're currently updating the window, display and current
28768 matrix usually don't agree. Arrange for a thorough display
28769 later. */
28770 if (w->must_be_updated_p)
28771 {
28772 SET_FRAME_GARBAGED (f);
28773 return 0;
28774 }
28775
28776 /* Frame-relative pixel rectangle of W. */
28777 wr.x = WINDOW_LEFT_EDGE_X (w);
28778 wr.y = WINDOW_TOP_EDGE_Y (w);
28779 wr.width = WINDOW_TOTAL_WIDTH (w);
28780 wr.height = WINDOW_TOTAL_HEIGHT (w);
28781
28782 if (x_intersect_rectangles (fr, &wr, &r))
28783 {
28784 int yb = window_text_bottom_y (w);
28785 struct glyph_row *row;
28786 int cursor_cleared_p, phys_cursor_on_p;
28787 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28788
28789 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28790 r.x, r.y, r.width, r.height));
28791
28792 /* Convert to window coordinates. */
28793 r.x -= WINDOW_LEFT_EDGE_X (w);
28794 r.y -= WINDOW_TOP_EDGE_Y (w);
28795
28796 /* Turn off the cursor. */
28797 if (!w->pseudo_window_p
28798 && phys_cursor_in_rect_p (w, &r))
28799 {
28800 x_clear_cursor (w);
28801 cursor_cleared_p = 1;
28802 }
28803 else
28804 cursor_cleared_p = 0;
28805
28806 /* If the row containing the cursor extends face to end of line,
28807 then expose_area might overwrite the cursor outside the
28808 rectangle and thus notice_overwritten_cursor might clear
28809 w->phys_cursor_on_p. We remember the original value and
28810 check later if it is changed. */
28811 phys_cursor_on_p = w->phys_cursor_on_p;
28812
28813 /* Update lines intersecting rectangle R. */
28814 first_overlapping_row = last_overlapping_row = NULL;
28815 for (row = w->current_matrix->rows;
28816 row->enabled_p;
28817 ++row)
28818 {
28819 int y0 = row->y;
28820 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28821
28822 if ((y0 >= r.y && y0 < r.y + r.height)
28823 || (y1 > r.y && y1 < r.y + r.height)
28824 || (r.y >= y0 && r.y < y1)
28825 || (r.y + r.height > y0 && r.y + r.height < y1))
28826 {
28827 /* A header line may be overlapping, but there is no need
28828 to fix overlapping areas for them. KFS 2005-02-12 */
28829 if (row->overlapping_p && !row->mode_line_p)
28830 {
28831 if (first_overlapping_row == NULL)
28832 first_overlapping_row = row;
28833 last_overlapping_row = row;
28834 }
28835
28836 row->clip = fr;
28837 if (expose_line (w, row, &r))
28838 mouse_face_overwritten_p = 1;
28839 row->clip = NULL;
28840 }
28841 else if (row->overlapping_p)
28842 {
28843 /* We must redraw a row overlapping the exposed area. */
28844 if (y0 < r.y
28845 ? y0 + row->phys_height > r.y
28846 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28847 {
28848 if (first_overlapping_row == NULL)
28849 first_overlapping_row = row;
28850 last_overlapping_row = row;
28851 }
28852 }
28853
28854 if (y1 >= yb)
28855 break;
28856 }
28857
28858 /* Display the mode line if there is one. */
28859 if (WINDOW_WANTS_MODELINE_P (w)
28860 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28861 row->enabled_p)
28862 && row->y < r.y + r.height)
28863 {
28864 if (expose_line (w, row, &r))
28865 mouse_face_overwritten_p = 1;
28866 }
28867
28868 if (!w->pseudo_window_p)
28869 {
28870 /* Fix the display of overlapping rows. */
28871 if (first_overlapping_row)
28872 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28873 fr);
28874
28875 /* Draw border between windows. */
28876 x_draw_vertical_border (w);
28877
28878 /* Turn the cursor on again. */
28879 if (cursor_cleared_p
28880 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28881 update_window_cursor (w, 1);
28882 }
28883 }
28884
28885 return mouse_face_overwritten_p;
28886 }
28887
28888
28889
28890 /* Redraw (parts) of all windows in the window tree rooted at W that
28891 intersect R. R contains frame pixel coordinates. Value is
28892 non-zero if the exposure overwrites mouse-face. */
28893
28894 static int
28895 expose_window_tree (struct window *w, XRectangle *r)
28896 {
28897 struct frame *f = XFRAME (w->frame);
28898 int mouse_face_overwritten_p = 0;
28899
28900 while (w && !FRAME_GARBAGED_P (f))
28901 {
28902 if (WINDOWP (w->contents))
28903 mouse_face_overwritten_p
28904 |= expose_window_tree (XWINDOW (w->contents), r);
28905 else
28906 mouse_face_overwritten_p |= expose_window (w, r);
28907
28908 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28909 }
28910
28911 return mouse_face_overwritten_p;
28912 }
28913
28914
28915 /* EXPORT:
28916 Redisplay an exposed area of frame F. X and Y are the upper-left
28917 corner of the exposed rectangle. W and H are width and height of
28918 the exposed area. All are pixel values. W or H zero means redraw
28919 the entire frame. */
28920
28921 void
28922 expose_frame (struct frame *f, int x, int y, int w, int h)
28923 {
28924 XRectangle r;
28925 int mouse_face_overwritten_p = 0;
28926
28927 TRACE ((stderr, "expose_frame "));
28928
28929 /* No need to redraw if frame will be redrawn soon. */
28930 if (FRAME_GARBAGED_P (f))
28931 {
28932 TRACE ((stderr, " garbaged\n"));
28933 return;
28934 }
28935
28936 /* If basic faces haven't been realized yet, there is no point in
28937 trying to redraw anything. This can happen when we get an expose
28938 event while Emacs is starting, e.g. by moving another window. */
28939 if (FRAME_FACE_CACHE (f) == NULL
28940 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28941 {
28942 TRACE ((stderr, " no faces\n"));
28943 return;
28944 }
28945
28946 if (w == 0 || h == 0)
28947 {
28948 r.x = r.y = 0;
28949 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
28950 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
28951 }
28952 else
28953 {
28954 r.x = x;
28955 r.y = y;
28956 r.width = w;
28957 r.height = h;
28958 }
28959
28960 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
28961 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
28962
28963 if (WINDOWP (f->tool_bar_window))
28964 mouse_face_overwritten_p
28965 |= expose_window (XWINDOW (f->tool_bar_window), &r);
28966
28967 #ifdef HAVE_X_WINDOWS
28968 #ifndef MSDOS
28969 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
28970 if (WINDOWP (f->menu_bar_window))
28971 mouse_face_overwritten_p
28972 |= expose_window (XWINDOW (f->menu_bar_window), &r);
28973 #endif /* not USE_X_TOOLKIT and not USE_GTK */
28974 #endif
28975 #endif
28976
28977 /* Some window managers support a focus-follows-mouse style with
28978 delayed raising of frames. Imagine a partially obscured frame,
28979 and moving the mouse into partially obscured mouse-face on that
28980 frame. The visible part of the mouse-face will be highlighted,
28981 then the WM raises the obscured frame. With at least one WM, KDE
28982 2.1, Emacs is not getting any event for the raising of the frame
28983 (even tried with SubstructureRedirectMask), only Expose events.
28984 These expose events will draw text normally, i.e. not
28985 highlighted. Which means we must redo the highlight here.
28986 Subsume it under ``we love X''. --gerd 2001-08-15 */
28987 /* Included in Windows version because Windows most likely does not
28988 do the right thing if any third party tool offers
28989 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
28990 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
28991 {
28992 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28993 if (f == hlinfo->mouse_face_mouse_frame)
28994 {
28995 int mouse_x = hlinfo->mouse_face_mouse_x;
28996 int mouse_y = hlinfo->mouse_face_mouse_y;
28997 clear_mouse_face (hlinfo);
28998 note_mouse_highlight (f, mouse_x, mouse_y);
28999 }
29000 }
29001 }
29002
29003
29004 /* EXPORT:
29005 Determine the intersection of two rectangles R1 and R2. Return
29006 the intersection in *RESULT. Value is non-zero if RESULT is not
29007 empty. */
29008
29009 int
29010 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
29011 {
29012 XRectangle *left, *right;
29013 XRectangle *upper, *lower;
29014 int intersection_p = 0;
29015
29016 /* Rearrange so that R1 is the left-most rectangle. */
29017 if (r1->x < r2->x)
29018 left = r1, right = r2;
29019 else
29020 left = r2, right = r1;
29021
29022 /* X0 of the intersection is right.x0, if this is inside R1,
29023 otherwise there is no intersection. */
29024 if (right->x <= left->x + left->width)
29025 {
29026 result->x = right->x;
29027
29028 /* The right end of the intersection is the minimum of
29029 the right ends of left and right. */
29030 result->width = (min (left->x + left->width, right->x + right->width)
29031 - result->x);
29032
29033 /* Same game for Y. */
29034 if (r1->y < r2->y)
29035 upper = r1, lower = r2;
29036 else
29037 upper = r2, lower = r1;
29038
29039 /* The upper end of the intersection is lower.y0, if this is inside
29040 of upper. Otherwise, there is no intersection. */
29041 if (lower->y <= upper->y + upper->height)
29042 {
29043 result->y = lower->y;
29044
29045 /* The lower end of the intersection is the minimum of the lower
29046 ends of upper and lower. */
29047 result->height = (min (lower->y + lower->height,
29048 upper->y + upper->height)
29049 - result->y);
29050 intersection_p = 1;
29051 }
29052 }
29053
29054 return intersection_p;
29055 }
29056
29057 #endif /* HAVE_WINDOW_SYSTEM */
29058
29059 \f
29060 /***********************************************************************
29061 Initialization
29062 ***********************************************************************/
29063
29064 void
29065 syms_of_xdisp (void)
29066 {
29067 Vwith_echo_area_save_vector = Qnil;
29068 staticpro (&Vwith_echo_area_save_vector);
29069
29070 Vmessage_stack = Qnil;
29071 staticpro (&Vmessage_stack);
29072
29073 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
29074 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
29075
29076 message_dolog_marker1 = Fmake_marker ();
29077 staticpro (&message_dolog_marker1);
29078 message_dolog_marker2 = Fmake_marker ();
29079 staticpro (&message_dolog_marker2);
29080 message_dolog_marker3 = Fmake_marker ();
29081 staticpro (&message_dolog_marker3);
29082
29083 #ifdef GLYPH_DEBUG
29084 defsubr (&Sdump_frame_glyph_matrix);
29085 defsubr (&Sdump_glyph_matrix);
29086 defsubr (&Sdump_glyph_row);
29087 defsubr (&Sdump_tool_bar_row);
29088 defsubr (&Strace_redisplay);
29089 defsubr (&Strace_to_stderr);
29090 #endif
29091 #ifdef HAVE_WINDOW_SYSTEM
29092 defsubr (&Stool_bar_lines_needed);
29093 defsubr (&Slookup_image_map);
29094 #endif
29095 defsubr (&Sline_pixel_height);
29096 defsubr (&Sformat_mode_line);
29097 defsubr (&Sinvisible_p);
29098 defsubr (&Scurrent_bidi_paragraph_direction);
29099 defsubr (&Smove_point_visually);
29100
29101 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
29102 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
29103 DEFSYM (Qoverriding_local_map, "overriding-local-map");
29104 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
29105 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
29106 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
29107 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
29108 DEFSYM (Qeval, "eval");
29109 DEFSYM (QCdata, ":data");
29110 DEFSYM (Qdisplay, "display");
29111 DEFSYM (Qspace_width, "space-width");
29112 DEFSYM (Qraise, "raise");
29113 DEFSYM (Qslice, "slice");
29114 DEFSYM (Qspace, "space");
29115 DEFSYM (Qmargin, "margin");
29116 DEFSYM (Qpointer, "pointer");
29117 DEFSYM (Qleft_margin, "left-margin");
29118 DEFSYM (Qright_margin, "right-margin");
29119 DEFSYM (Qcenter, "center");
29120 DEFSYM (Qline_height, "line-height");
29121 DEFSYM (QCalign_to, ":align-to");
29122 DEFSYM (QCrelative_width, ":relative-width");
29123 DEFSYM (QCrelative_height, ":relative-height");
29124 DEFSYM (QCeval, ":eval");
29125 DEFSYM (QCpropertize, ":propertize");
29126 DEFSYM (QCfile, ":file");
29127 DEFSYM (Qfontified, "fontified");
29128 DEFSYM (Qfontification_functions, "fontification-functions");
29129 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
29130 DEFSYM (Qescape_glyph, "escape-glyph");
29131 DEFSYM (Qnobreak_space, "nobreak-space");
29132 DEFSYM (Qimage, "image");
29133 DEFSYM (Qtext, "text");
29134 DEFSYM (Qboth, "both");
29135 DEFSYM (Qboth_horiz, "both-horiz");
29136 DEFSYM (Qtext_image_horiz, "text-image-horiz");
29137 DEFSYM (QCmap, ":map");
29138 DEFSYM (QCpointer, ":pointer");
29139 DEFSYM (Qrect, "rect");
29140 DEFSYM (Qcircle, "circle");
29141 DEFSYM (Qpoly, "poly");
29142 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
29143 DEFSYM (Qgrow_only, "grow-only");
29144 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
29145 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
29146 DEFSYM (Qposition, "position");
29147 DEFSYM (Qbuffer_position, "buffer-position");
29148 DEFSYM (Qobject, "object");
29149 DEFSYM (Qbar, "bar");
29150 DEFSYM (Qhbar, "hbar");
29151 DEFSYM (Qbox, "box");
29152 DEFSYM (Qhollow, "hollow");
29153 DEFSYM (Qhand, "hand");
29154 DEFSYM (Qarrow, "arrow");
29155 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
29156
29157 list_of_error = list1 (list2 (intern_c_string ("error"),
29158 intern_c_string ("void-variable")));
29159 staticpro (&list_of_error);
29160
29161 DEFSYM (Qlast_arrow_position, "last-arrow-position");
29162 DEFSYM (Qlast_arrow_string, "last-arrow-string");
29163 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
29164 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
29165
29166 echo_buffer[0] = echo_buffer[1] = Qnil;
29167 staticpro (&echo_buffer[0]);
29168 staticpro (&echo_buffer[1]);
29169
29170 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
29171 staticpro (&echo_area_buffer[0]);
29172 staticpro (&echo_area_buffer[1]);
29173
29174 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
29175 staticpro (&Vmessages_buffer_name);
29176
29177 mode_line_proptrans_alist = Qnil;
29178 staticpro (&mode_line_proptrans_alist);
29179 mode_line_string_list = Qnil;
29180 staticpro (&mode_line_string_list);
29181 mode_line_string_face = Qnil;
29182 staticpro (&mode_line_string_face);
29183 mode_line_string_face_prop = Qnil;
29184 staticpro (&mode_line_string_face_prop);
29185 Vmode_line_unwind_vector = Qnil;
29186 staticpro (&Vmode_line_unwind_vector);
29187
29188 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
29189
29190 help_echo_string = Qnil;
29191 staticpro (&help_echo_string);
29192 help_echo_object = Qnil;
29193 staticpro (&help_echo_object);
29194 help_echo_window = Qnil;
29195 staticpro (&help_echo_window);
29196 previous_help_echo_string = Qnil;
29197 staticpro (&previous_help_echo_string);
29198 help_echo_pos = -1;
29199
29200 DEFSYM (Qright_to_left, "right-to-left");
29201 DEFSYM (Qleft_to_right, "left-to-right");
29202
29203 #ifdef HAVE_WINDOW_SYSTEM
29204 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
29205 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
29206 For example, if a block cursor is over a tab, it will be drawn as
29207 wide as that tab on the display. */);
29208 x_stretch_cursor_p = 0;
29209 #endif
29210
29211 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
29212 doc: /* Non-nil means highlight trailing whitespace.
29213 The face used for trailing whitespace is `trailing-whitespace'. */);
29214 Vshow_trailing_whitespace = Qnil;
29215
29216 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
29217 doc: /* Control highlighting of non-ASCII space and hyphen chars.
29218 If the value is t, Emacs highlights non-ASCII chars which have the
29219 same appearance as an ASCII space or hyphen, using the `nobreak-space'
29220 or `escape-glyph' face respectively.
29221
29222 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
29223 U+2011 (non-breaking hyphen) are affected.
29224
29225 Any other non-nil value means to display these characters as a escape
29226 glyph followed by an ordinary space or hyphen.
29227
29228 A value of nil means no special handling of these characters. */);
29229 Vnobreak_char_display = Qt;
29230
29231 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
29232 doc: /* The pointer shape to show in void text areas.
29233 A value of nil means to show the text pointer. Other options are `arrow',
29234 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
29235 Vvoid_text_area_pointer = Qarrow;
29236
29237 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
29238 doc: /* Non-nil means don't actually do any redisplay.
29239 This is used for internal purposes. */);
29240 Vinhibit_redisplay = Qnil;
29241
29242 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
29243 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
29244 Vglobal_mode_string = Qnil;
29245
29246 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
29247 doc: /* Marker for where to display an arrow on top of the buffer text.
29248 This must be the beginning of a line in order to work.
29249 See also `overlay-arrow-string'. */);
29250 Voverlay_arrow_position = Qnil;
29251
29252 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
29253 doc: /* String to display as an arrow in non-window frames.
29254 See also `overlay-arrow-position'. */);
29255 Voverlay_arrow_string = build_pure_c_string ("=>");
29256
29257 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
29258 doc: /* List of variables (symbols) which hold markers for overlay arrows.
29259 The symbols on this list are examined during redisplay to determine
29260 where to display overlay arrows. */);
29261 Voverlay_arrow_variable_list
29262 = list1 (intern_c_string ("overlay-arrow-position"));
29263
29264 DEFVAR_INT ("scroll-step", emacs_scroll_step,
29265 doc: /* The number of lines to try scrolling a window by when point moves out.
29266 If that fails to bring point back on frame, point is centered instead.
29267 If this is zero, point is always centered after it moves off frame.
29268 If you want scrolling to always be a line at a time, you should set
29269 `scroll-conservatively' to a large value rather than set this to 1. */);
29270
29271 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
29272 doc: /* Scroll up to this many lines, to bring point back on screen.
29273 If point moves off-screen, redisplay will scroll by up to
29274 `scroll-conservatively' lines in order to bring point just barely
29275 onto the screen again. If that cannot be done, then redisplay
29276 recenters point as usual.
29277
29278 If the value is greater than 100, redisplay will never recenter point,
29279 but will always scroll just enough text to bring point into view, even
29280 if you move far away.
29281
29282 A value of zero means always recenter point if it moves off screen. */);
29283 scroll_conservatively = 0;
29284
29285 DEFVAR_INT ("scroll-margin", scroll_margin,
29286 doc: /* Number of lines of margin at the top and bottom of a window.
29287 Recenter the window whenever point gets within this many lines
29288 of the top or bottom of the window. */);
29289 scroll_margin = 0;
29290
29291 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
29292 doc: /* Pixels per inch value for non-window system displays.
29293 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
29294 Vdisplay_pixels_per_inch = make_float (72.0);
29295
29296 #ifdef GLYPH_DEBUG
29297 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
29298 #endif
29299
29300 DEFVAR_LISP ("truncate-partial-width-windows",
29301 Vtruncate_partial_width_windows,
29302 doc: /* Non-nil means truncate lines in windows narrower than the frame.
29303 For an integer value, truncate lines in each window narrower than the
29304 full frame width, provided the window width is less than that integer;
29305 otherwise, respect the value of `truncate-lines'.
29306
29307 For any other non-nil value, truncate lines in all windows that do
29308 not span the full frame width.
29309
29310 A value of nil means to respect the value of `truncate-lines'.
29311
29312 If `word-wrap' is enabled, you might want to reduce this. */);
29313 Vtruncate_partial_width_windows = make_number (50);
29314
29315 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
29316 doc: /* Maximum buffer size for which line number should be displayed.
29317 If the buffer is bigger than this, the line number does not appear
29318 in the mode line. A value of nil means no limit. */);
29319 Vline_number_display_limit = Qnil;
29320
29321 DEFVAR_INT ("line-number-display-limit-width",
29322 line_number_display_limit_width,
29323 doc: /* Maximum line width (in characters) for line number display.
29324 If the average length of the lines near point is bigger than this, then the
29325 line number may be omitted from the mode line. */);
29326 line_number_display_limit_width = 200;
29327
29328 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
29329 doc: /* Non-nil means highlight region even in nonselected windows. */);
29330 highlight_nonselected_windows = 0;
29331
29332 DEFVAR_BOOL ("multiple-frames", multiple_frames,
29333 doc: /* Non-nil if more than one frame is visible on this display.
29334 Minibuffer-only frames don't count, but iconified frames do.
29335 This variable is not guaranteed to be accurate except while processing
29336 `frame-title-format' and `icon-title-format'. */);
29337
29338 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
29339 doc: /* Template for displaying the title bar of visible frames.
29340 \(Assuming the window manager supports this feature.)
29341
29342 This variable has the same structure as `mode-line-format', except that
29343 the %c and %l constructs are ignored. It is used only on frames for
29344 which no explicit name has been set \(see `modify-frame-parameters'). */);
29345
29346 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
29347 doc: /* Template for displaying the title bar of an iconified frame.
29348 \(Assuming the window manager supports this feature.)
29349 This variable has the same structure as `mode-line-format' (which see),
29350 and is used only on frames for which no explicit name has been set
29351 \(see `modify-frame-parameters'). */);
29352 Vicon_title_format
29353 = Vframe_title_format
29354 = listn (CONSTYPE_PURE, 3,
29355 intern_c_string ("multiple-frames"),
29356 build_pure_c_string ("%b"),
29357 listn (CONSTYPE_PURE, 4,
29358 empty_unibyte_string,
29359 intern_c_string ("invocation-name"),
29360 build_pure_c_string ("@"),
29361 intern_c_string ("system-name")));
29362
29363 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
29364 doc: /* Maximum number of lines to keep in the message log buffer.
29365 If nil, disable message logging. If t, log messages but don't truncate
29366 the buffer when it becomes large. */);
29367 Vmessage_log_max = make_number (1000);
29368
29369 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
29370 doc: /* Functions called before redisplay, if window sizes have changed.
29371 The value should be a list of functions that take one argument.
29372 Just before redisplay, for each frame, if any of its windows have changed
29373 size since the last redisplay, or have been split or deleted,
29374 all the functions in the list are called, with the frame as argument. */);
29375 Vwindow_size_change_functions = Qnil;
29376
29377 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
29378 doc: /* List of functions to call before redisplaying a window with scrolling.
29379 Each function is called with two arguments, the window and its new
29380 display-start position. Note that these functions are also called by
29381 `set-window-buffer'. Also note that the value of `window-end' is not
29382 valid when these functions are called.
29383
29384 Warning: Do not use this feature to alter the way the window
29385 is scrolled. It is not designed for that, and such use probably won't
29386 work. */);
29387 Vwindow_scroll_functions = Qnil;
29388
29389 DEFVAR_LISP ("window-text-change-functions",
29390 Vwindow_text_change_functions,
29391 doc: /* Functions to call in redisplay when text in the window might change. */);
29392 Vwindow_text_change_functions = Qnil;
29393
29394 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
29395 doc: /* Functions called when redisplay of a window reaches the end trigger.
29396 Each function is called with two arguments, the window and the end trigger value.
29397 See `set-window-redisplay-end-trigger'. */);
29398 Vredisplay_end_trigger_functions = Qnil;
29399
29400 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
29401 doc: /* Non-nil means autoselect window with mouse pointer.
29402 If nil, do not autoselect windows.
29403 A positive number means delay autoselection by that many seconds: a
29404 window is autoselected only after the mouse has remained in that
29405 window for the duration of the delay.
29406 A negative number has a similar effect, but causes windows to be
29407 autoselected only after the mouse has stopped moving. \(Because of
29408 the way Emacs compares mouse events, you will occasionally wait twice
29409 that time before the window gets selected.\)
29410 Any other value means to autoselect window instantaneously when the
29411 mouse pointer enters it.
29412
29413 Autoselection selects the minibuffer only if it is active, and never
29414 unselects the minibuffer if it is active.
29415
29416 When customizing this variable make sure that the actual value of
29417 `focus-follows-mouse' matches the behavior of your window manager. */);
29418 Vmouse_autoselect_window = Qnil;
29419
29420 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29421 doc: /* Non-nil means automatically resize tool-bars.
29422 This dynamically changes the tool-bar's height to the minimum height
29423 that is needed to make all tool-bar items visible.
29424 If value is `grow-only', the tool-bar's height is only increased
29425 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29426 Vauto_resize_tool_bars = Qt;
29427
29428 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29429 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29430 auto_raise_tool_bar_buttons_p = 1;
29431
29432 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29433 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29434 make_cursor_line_fully_visible_p = 1;
29435
29436 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29437 doc: /* Border below tool-bar in pixels.
29438 If an integer, use it as the height of the border.
29439 If it is one of `internal-border-width' or `border-width', use the
29440 value of the corresponding frame parameter.
29441 Otherwise, no border is added below the tool-bar. */);
29442 Vtool_bar_border = Qinternal_border_width;
29443
29444 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29445 doc: /* Margin around tool-bar buttons in pixels.
29446 If an integer, use that for both horizontal and vertical margins.
29447 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29448 HORZ specifying the horizontal margin, and VERT specifying the
29449 vertical margin. */);
29450 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29451
29452 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29453 doc: /* Relief thickness of tool-bar buttons. */);
29454 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29455
29456 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29457 doc: /* Tool bar style to use.
29458 It can be one of
29459 image - show images only
29460 text - show text only
29461 both - show both, text below image
29462 both-horiz - show text to the right of the image
29463 text-image-horiz - show text to the left of the image
29464 any other - use system default or image if no system default.
29465
29466 This variable only affects the GTK+ toolkit version of Emacs. */);
29467 Vtool_bar_style = Qnil;
29468
29469 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29470 doc: /* Maximum number of characters a label can have to be shown.
29471 The tool bar style must also show labels for this to have any effect, see
29472 `tool-bar-style'. */);
29473 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29474
29475 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29476 doc: /* List of functions to call to fontify regions of text.
29477 Each function is called with one argument POS. Functions must
29478 fontify a region starting at POS in the current buffer, and give
29479 fontified regions the property `fontified'. */);
29480 Vfontification_functions = Qnil;
29481 Fmake_variable_buffer_local (Qfontification_functions);
29482
29483 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29484 unibyte_display_via_language_environment,
29485 doc: /* Non-nil means display unibyte text according to language environment.
29486 Specifically, this means that raw bytes in the range 160-255 decimal
29487 are displayed by converting them to the equivalent multibyte characters
29488 according to the current language environment. As a result, they are
29489 displayed according to the current fontset.
29490
29491 Note that this variable affects only how these bytes are displayed,
29492 but does not change the fact they are interpreted as raw bytes. */);
29493 unibyte_display_via_language_environment = 0;
29494
29495 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29496 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29497 If a float, it specifies a fraction of the mini-window frame's height.
29498 If an integer, it specifies a number of lines. */);
29499 Vmax_mini_window_height = make_float (0.25);
29500
29501 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29502 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29503 A value of nil means don't automatically resize mini-windows.
29504 A value of t means resize them to fit the text displayed in them.
29505 A value of `grow-only', the default, means let mini-windows grow only;
29506 they return to their normal size when the minibuffer is closed, or the
29507 echo area becomes empty. */);
29508 Vresize_mini_windows = Qgrow_only;
29509
29510 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29511 doc: /* Alist specifying how to blink the cursor off.
29512 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29513 `cursor-type' frame-parameter or variable equals ON-STATE,
29514 comparing using `equal', Emacs uses OFF-STATE to specify
29515 how to blink it off. ON-STATE and OFF-STATE are values for
29516 the `cursor-type' frame parameter.
29517
29518 If a frame's ON-STATE has no entry in this list,
29519 the frame's other specifications determine how to blink the cursor off. */);
29520 Vblink_cursor_alist = Qnil;
29521
29522 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29523 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29524 If non-nil, windows are automatically scrolled horizontally to make
29525 point visible. */);
29526 automatic_hscrolling_p = 1;
29527 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29528
29529 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29530 doc: /* How many columns away from the window edge point is allowed to get
29531 before automatic hscrolling will horizontally scroll the window. */);
29532 hscroll_margin = 5;
29533
29534 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29535 doc: /* How many columns to scroll the window when point gets too close to the edge.
29536 When point is less than `hscroll-margin' columns from the window
29537 edge, automatic hscrolling will scroll the window by the amount of columns
29538 determined by this variable. If its value is a positive integer, scroll that
29539 many columns. If it's a positive floating-point number, it specifies the
29540 fraction of the window's width to scroll. If it's nil or zero, point will be
29541 centered horizontally after the scroll. Any other value, including negative
29542 numbers, are treated as if the value were zero.
29543
29544 Automatic hscrolling always moves point outside the scroll margin, so if
29545 point was more than scroll step columns inside the margin, the window will
29546 scroll more than the value given by the scroll step.
29547
29548 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29549 and `scroll-right' overrides this variable's effect. */);
29550 Vhscroll_step = make_number (0);
29551
29552 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29553 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29554 Bind this around calls to `message' to let it take effect. */);
29555 message_truncate_lines = 0;
29556
29557 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29558 doc: /* Normal hook run to update the menu bar definitions.
29559 Redisplay runs this hook before it redisplays the menu bar.
29560 This is used to update submenus such as Buffers,
29561 whose contents depend on various data. */);
29562 Vmenu_bar_update_hook = Qnil;
29563
29564 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29565 doc: /* Frame for which we are updating a menu.
29566 The enable predicate for a menu binding should check this variable. */);
29567 Vmenu_updating_frame = Qnil;
29568
29569 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29570 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29571 inhibit_menubar_update = 0;
29572
29573 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29574 doc: /* Prefix prepended to all continuation lines at display time.
29575 The value may be a string, an image, or a stretch-glyph; it is
29576 interpreted in the same way as the value of a `display' text property.
29577
29578 This variable is overridden by any `wrap-prefix' text or overlay
29579 property.
29580
29581 To add a prefix to non-continuation lines, use `line-prefix'. */);
29582 Vwrap_prefix = Qnil;
29583 DEFSYM (Qwrap_prefix, "wrap-prefix");
29584 Fmake_variable_buffer_local (Qwrap_prefix);
29585
29586 DEFVAR_LISP ("line-prefix", Vline_prefix,
29587 doc: /* Prefix prepended to all non-continuation lines at display time.
29588 The value may be a string, an image, or a stretch-glyph; it is
29589 interpreted in the same way as the value of a `display' text property.
29590
29591 This variable is overridden by any `line-prefix' text or overlay
29592 property.
29593
29594 To add a prefix to continuation lines, use `wrap-prefix'. */);
29595 Vline_prefix = Qnil;
29596 DEFSYM (Qline_prefix, "line-prefix");
29597 Fmake_variable_buffer_local (Qline_prefix);
29598
29599 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29600 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29601 inhibit_eval_during_redisplay = 0;
29602
29603 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29604 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29605 inhibit_free_realized_faces = 0;
29606
29607 #ifdef GLYPH_DEBUG
29608 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29609 doc: /* Inhibit try_window_id display optimization. */);
29610 inhibit_try_window_id = 0;
29611
29612 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29613 doc: /* Inhibit try_window_reusing display optimization. */);
29614 inhibit_try_window_reusing = 0;
29615
29616 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29617 doc: /* Inhibit try_cursor_movement display optimization. */);
29618 inhibit_try_cursor_movement = 0;
29619 #endif /* GLYPH_DEBUG */
29620
29621 DEFVAR_INT ("overline-margin", overline_margin,
29622 doc: /* Space between overline and text, in pixels.
29623 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29624 margin to the character height. */);
29625 overline_margin = 2;
29626
29627 DEFVAR_INT ("underline-minimum-offset",
29628 underline_minimum_offset,
29629 doc: /* Minimum distance between baseline and underline.
29630 This can improve legibility of underlined text at small font sizes,
29631 particularly when using variable `x-use-underline-position-properties'
29632 with fonts that specify an UNDERLINE_POSITION relatively close to the
29633 baseline. The default value is 1. */);
29634 underline_minimum_offset = 1;
29635
29636 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29637 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29638 This feature only works when on a window system that can change
29639 cursor shapes. */);
29640 display_hourglass_p = 1;
29641
29642 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29643 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29644 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29645
29646 hourglass_atimer = NULL;
29647 hourglass_shown_p = 0;
29648
29649 DEFSYM (Qglyphless_char, "glyphless-char");
29650 DEFSYM (Qhex_code, "hex-code");
29651 DEFSYM (Qempty_box, "empty-box");
29652 DEFSYM (Qthin_space, "thin-space");
29653 DEFSYM (Qzero_width, "zero-width");
29654
29655 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29656 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29657
29658 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29659 doc: /* Char-table defining glyphless characters.
29660 Each element, if non-nil, should be one of the following:
29661 an ASCII acronym string: display this string in a box
29662 `hex-code': display the hexadecimal code of a character in a box
29663 `empty-box': display as an empty box
29664 `thin-space': display as 1-pixel width space
29665 `zero-width': don't display
29666 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29667 display method for graphical terminals and text terminals respectively.
29668 GRAPHICAL and TEXT should each have one of the values listed above.
29669
29670 The char-table has one extra slot to control the display of a character for
29671 which no font is found. This slot only takes effect on graphical terminals.
29672 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29673 `thin-space'. The default is `empty-box'. */);
29674 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29675 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29676 Qempty_box);
29677
29678 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
29679 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
29680 Vdebug_on_message = Qnil;
29681 }
29682
29683
29684 /* Initialize this module when Emacs starts. */
29685
29686 void
29687 init_xdisp (void)
29688 {
29689 current_header_line_height = current_mode_line_height = -1;
29690
29691 CHARPOS (this_line_start_pos) = 0;
29692
29693 if (!noninteractive)
29694 {
29695 struct window *m = XWINDOW (minibuf_window);
29696 Lisp_Object frame = m->frame;
29697 struct frame *f = XFRAME (frame);
29698 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29699 struct window *r = XWINDOW (root);
29700 int i;
29701
29702 echo_area_window = minibuf_window;
29703
29704 r->top_line = FRAME_TOP_MARGIN (f);
29705 r->total_lines = FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
29706 r->total_cols = FRAME_COLS (f);
29707
29708 m->top_line = FRAME_LINES (f) - 1;
29709 m->total_lines = 1;
29710 m->total_cols = FRAME_COLS (f);
29711
29712 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29713 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29714 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29715
29716 /* The default ellipsis glyphs `...'. */
29717 for (i = 0; i < 3; ++i)
29718 default_invis_vector[i] = make_number ('.');
29719 }
29720
29721 {
29722 /* Allocate the buffer for frame titles.
29723 Also used for `format-mode-line'. */
29724 int size = 100;
29725 mode_line_noprop_buf = xmalloc (size);
29726 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29727 mode_line_noprop_ptr = mode_line_noprop_buf;
29728 mode_line_target = MODE_LINE_DISPLAY;
29729 }
29730
29731 help_echo_showing_p = 0;
29732 }
29733
29734 /* Platform-independent portion of hourglass implementation. */
29735
29736 /* Cancel a currently active hourglass timer, and start a new one. */
29737 void
29738 start_hourglass (void)
29739 {
29740 #if defined (HAVE_WINDOW_SYSTEM)
29741 struct timespec delay;
29742
29743 cancel_hourglass ();
29744
29745 if (INTEGERP (Vhourglass_delay)
29746 && XINT (Vhourglass_delay) > 0)
29747 delay = make_timespec (min (XINT (Vhourglass_delay),
29748 TYPE_MAXIMUM (time_t)),
29749 0);
29750 else if (FLOATP (Vhourglass_delay)
29751 && XFLOAT_DATA (Vhourglass_delay) > 0)
29752 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
29753 else
29754 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
29755
29756 #ifdef HAVE_NTGUI
29757 {
29758 extern void w32_note_current_window (void);
29759 w32_note_current_window ();
29760 }
29761 #endif /* HAVE_NTGUI */
29762
29763 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29764 show_hourglass, NULL);
29765 #endif
29766 }
29767
29768
29769 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29770 shown. */
29771 void
29772 cancel_hourglass (void)
29773 {
29774 #if defined (HAVE_WINDOW_SYSTEM)
29775 if (hourglass_atimer)
29776 {
29777 cancel_atimer (hourglass_atimer);
29778 hourglass_atimer = NULL;
29779 }
29780
29781 if (hourglass_shown_p)
29782 hide_hourglass ();
29783 #endif
29784 }